3 bttv - Bt848 frame grabber driver
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
6 & Marcus Metzler <mocm@thp.uni-koeln.de>
7 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
15 Fixes to be fully V4L2 compliant by
16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
18 Cropping and overscan support
19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
20 Sponsored by OPQ Systems AB
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/slab.h>
41 #include <linux/errno.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
45 #include <linux/interrupt.h>
46 #include <linux/kdev_t.h>
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-ioctl.h>
50 #include <media/tvaudio.h>
51 #include <media/msp3400.h>
53 #include <linux/dma-mapping.h>
56 #include <asm/byteorder.h>
58 #include <media/rds.h>
61 unsigned int bttv_num; /* number of Bt848s in use */
62 struct bttv *bttvs[BTTV_MAX];
64 unsigned int bttv_debug;
65 unsigned int bttv_verbose = 1;
66 unsigned int bttv_gpio;
68 /* config variables */
70 static unsigned int bigendian=1;
72 static unsigned int bigendian;
74 static unsigned int radio[BTTV_MAX];
75 static unsigned int irq_debug;
76 static unsigned int gbuffers = 8;
77 static unsigned int gbufsize = 0x208000;
78 static unsigned int reset_crop = 1;
80 static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
81 static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
82 static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
83 static int debug_latency;
84 static int disable_ir;
86 static unsigned int fdsr;
89 static unsigned int combfilter;
90 static unsigned int lumafilter;
91 static unsigned int automute = 1;
92 static unsigned int chroma_agc;
93 static unsigned int adc_crush = 1;
94 static unsigned int whitecrush_upper = 0xCF;
95 static unsigned int whitecrush_lower = 0x7F;
96 static unsigned int vcr_hack;
97 static unsigned int irq_iswitch;
98 static unsigned int uv_ratio = 50;
99 static unsigned int full_luma_range;
100 static unsigned int coring;
102 /* API features (turn on/off stuff for testing) */
103 static unsigned int v4l2 = 1;
106 module_param(bttv_verbose, int, 0644);
107 module_param(bttv_gpio, int, 0644);
108 module_param(bttv_debug, int, 0644);
109 module_param(irq_debug, int, 0644);
110 module_param(debug_latency, int, 0644);
111 module_param(disable_ir, int, 0444);
113 module_param(fdsr, int, 0444);
114 module_param(gbuffers, int, 0444);
115 module_param(gbufsize, int, 0444);
116 module_param(reset_crop, int, 0444);
118 module_param(v4l2, int, 0644);
119 module_param(bigendian, int, 0644);
120 module_param(irq_iswitch, int, 0644);
121 module_param(combfilter, int, 0444);
122 module_param(lumafilter, int, 0444);
123 module_param(automute, int, 0444);
124 module_param(chroma_agc, int, 0444);
125 module_param(adc_crush, int, 0444);
126 module_param(whitecrush_upper, int, 0444);
127 module_param(whitecrush_lower, int, 0444);
128 module_param(vcr_hack, int, 0444);
129 module_param(uv_ratio, int, 0444);
130 module_param(full_luma_range, int, 0444);
131 module_param(coring, int, 0444);
133 module_param_array(radio, int, NULL, 0444);
134 module_param_array(video_nr, int, NULL, 0444);
135 module_param_array(radio_nr, int, NULL, 0444);
136 module_param_array(vbi_nr, int, NULL, 0444);
138 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
139 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
140 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
141 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
142 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
143 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
144 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
145 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
146 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
147 MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
148 "is 1 (yes) for compatibility with older applications");
149 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
150 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
151 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
152 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
153 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
154 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
155 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
156 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
157 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
158 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
159 MODULE_PARM_DESC(video_nr, "video device numbers");
160 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
161 MODULE_PARM_DESC(radio_nr, "radio device numbers");
163 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
164 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
165 MODULE_LICENSE("GPL");
167 /* ----------------------------------------------------------------------- */
170 static ssize_t show_card(struct device *cd,
171 struct device_attribute *attr, char *buf)
173 struct video_device *vfd = container_of(cd, struct video_device, dev);
174 struct bttv *btv = video_get_drvdata(vfd);
175 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
177 static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
179 /* ----------------------------------------------------------------------- */
180 /* dvb auto-load setup */
181 #if defined(CONFIG_MODULES) && defined(MODULE)
182 static void request_module_async(struct work_struct *work)
184 request_module("dvb-bt8xx");
187 static void request_modules(struct bttv *dev)
189 INIT_WORK(&dev->request_module_wk, request_module_async);
190 schedule_work(&dev->request_module_wk);
193 static void flush_request_modules(struct bttv *dev)
195 flush_work_sync(&dev->request_module_wk);
198 #define request_modules(dev)
199 #define flush_request_modules(dev)
200 #endif /* CONFIG_MODULES */
203 /* ----------------------------------------------------------------------- */
206 /* special timing tables from conexant... */
207 static u8 SRAM_Table[][60] =
209 /* PAL digital input over GPIO[7:0] */
211 45, // 45 bytes following
212 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
213 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
214 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
215 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
216 0x37,0x00,0xAF,0x21,0x00
218 /* NTSC digital input over GPIO[7:0] */
220 51, // 51 bytes following
221 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
222 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
223 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
224 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
225 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
228 // TGB_NTSC392 // quartzsight
229 // This table has been modified to be used for Fusion Rev D
231 0x2A, // size of table = 42
232 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
233 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
234 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
235 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
240 /* minhdelayx1 first video pixel we can capture on a line and
241 hdelayx1 start of active video, both relative to rising edge of
242 /HRESET pulse (0H) in 1 / fCLKx1.
243 swidth width of active video and
244 totalwidth total line width, both in 1 / fCLKx1.
245 sqwidth total line width in square pixels.
246 vdelay start of active video in 2 * field lines relative to
247 trailing edge of /VRESET pulse (VDELAY register).
248 sheight height of active video in 2 * field lines.
249 videostart0 ITU-R frame line number of the line corresponding
250 to vdelay in the first field. */
251 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
252 vdelay, sheight, videostart0) \
253 .cropcap.bounds.left = minhdelayx1, \
254 /* * 2 because vertically we count field lines times two, */ \
255 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
256 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
257 /* 4 is a safety margin at the end of the line. */ \
258 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
259 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
260 .cropcap.defrect.left = hdelayx1, \
261 .cropcap.defrect.top = (videostart0) * 2, \
262 .cropcap.defrect.width = swidth, \
263 .cropcap.defrect.height = sheight, \
264 .cropcap.pixelaspect.numerator = totalwidth, \
265 .cropcap.pixelaspect.denominator = sqwidth,
267 const struct bttv_tvnorm bttv_tvnorms[] = {
269 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
270 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
272 .v4l2_id = V4L2_STD_PAL,
280 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
281 .scaledtwidth = 1135,
285 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
287 /* ITU-R frame line number of the first VBI line
288 we can capture, of the first and second field.
289 The last line is determined by cropcap.bounds. */
290 .vbistart = { 7, 320 },
291 CROPCAP(/* minhdelayx1 */ 68,
293 /* Should be (768 * 1135 + 944 / 2) / 944.
294 cropcap.defrect is used for image width
295 checks, so we keep the old value 924. */
297 /* totalwidth */ 1135,
301 /* videostart0 */ 23)
302 /* bt878 (and bt848?) can capture another
303 line below active video. */
304 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
306 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
314 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
319 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
321 .vbistart = { 10, 273 },
322 CROPCAP(/* minhdelayx1 */ 68,
324 /* Should be (640 * 910 + 780 / 2) / 780? */
326 /* totalwidth */ 910,
330 /* videostart0 */ 23)
332 .v4l2_id = V4L2_STD_SECAM,
340 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
341 .scaledtwidth = 1135,
346 .sram = 0, /* like PAL, correct? */
347 .vbistart = { 7, 320 },
348 CROPCAP(/* minhdelayx1 */ 68,
351 /* totalwidth */ 1135,
355 /* videostart0 */ 23)
357 .v4l2_id = V4L2_STD_PAL_Nc,
365 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
372 .vbistart = { 7, 320 },
373 CROPCAP(/* minhdelayx1 */ 68,
375 /* swidth */ (640 * 910 + 780 / 2) / 780,
376 /* totalwidth */ 910,
380 /* videostart0 */ 23)
382 .v4l2_id = V4L2_STD_PAL_M,
390 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
397 .vbistart = { 10, 273 },
398 CROPCAP(/* minhdelayx1 */ 68,
400 /* swidth */ (640 * 910 + 780 / 2) / 780,
401 /* totalwidth */ 910,
405 /* videostart0 */ 23)
407 .v4l2_id = V4L2_STD_PAL_N,
415 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
422 .vbistart = { 7, 320 },
423 CROPCAP(/* minhdelayx1 */ 68,
425 /* swidth */ (768 * 1135 + 944 / 2) / 944,
426 /* totalwidth */ 1135,
430 /* videostart0 */ 23)
432 .v4l2_id = V4L2_STD_NTSC_M_JP,
440 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
447 .vbistart = { 10, 273 },
448 CROPCAP(/* minhdelayx1 */ 68,
450 /* swidth */ (640 * 910 + 780 / 2) / 780,
451 /* totalwidth */ 910,
455 /* videostart0 */ 23)
457 /* that one hopefully works with the strange timing
458 * which video recorders produce when playing a NTSC
459 * tape on a PAL TV ... */
460 .v4l2_id = V4L2_STD_PAL_60,
468 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
469 .scaledtwidth = 1135,
476 .vbistart = { 10, 273 },
477 CROPCAP(/* minhdelayx1 */ 68,
480 /* totalwidth */ 1135,
484 /* videostart0 */ 23)
487 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
489 /* ----------------------------------------------------------------------- */
491 packed pixel formats must come first */
492 static const struct bttv_format formats[] = {
494 .name = "8 bpp, gray",
495 .fourcc = V4L2_PIX_FMT_GREY,
496 .btformat = BT848_COLOR_FMT_Y8,
498 .flags = FORMAT_FLAGS_PACKED,
500 .name = "8 bpp, dithered color",
501 .fourcc = V4L2_PIX_FMT_HI240,
502 .btformat = BT848_COLOR_FMT_RGB8,
504 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
506 .name = "15 bpp RGB, le",
507 .fourcc = V4L2_PIX_FMT_RGB555,
508 .btformat = BT848_COLOR_FMT_RGB15,
510 .flags = FORMAT_FLAGS_PACKED,
512 .name = "15 bpp RGB, be",
513 .fourcc = V4L2_PIX_FMT_RGB555X,
514 .btformat = BT848_COLOR_FMT_RGB15,
515 .btswap = 0x03, /* byteswap */
517 .flags = FORMAT_FLAGS_PACKED,
519 .name = "16 bpp RGB, le",
520 .fourcc = V4L2_PIX_FMT_RGB565,
521 .btformat = BT848_COLOR_FMT_RGB16,
523 .flags = FORMAT_FLAGS_PACKED,
525 .name = "16 bpp RGB, be",
526 .fourcc = V4L2_PIX_FMT_RGB565X,
527 .btformat = BT848_COLOR_FMT_RGB16,
528 .btswap = 0x03, /* byteswap */
530 .flags = FORMAT_FLAGS_PACKED,
532 .name = "24 bpp RGB, le",
533 .fourcc = V4L2_PIX_FMT_BGR24,
534 .btformat = BT848_COLOR_FMT_RGB24,
536 .flags = FORMAT_FLAGS_PACKED,
538 .name = "32 bpp RGB, le",
539 .fourcc = V4L2_PIX_FMT_BGR32,
540 .btformat = BT848_COLOR_FMT_RGB32,
542 .flags = FORMAT_FLAGS_PACKED,
544 .name = "32 bpp RGB, be",
545 .fourcc = V4L2_PIX_FMT_RGB32,
546 .btformat = BT848_COLOR_FMT_RGB32,
547 .btswap = 0x0f, /* byte+word swap */
549 .flags = FORMAT_FLAGS_PACKED,
551 .name = "4:2:2, packed, YUYV",
552 .fourcc = V4L2_PIX_FMT_YUYV,
553 .btformat = BT848_COLOR_FMT_YUY2,
555 .flags = FORMAT_FLAGS_PACKED,
557 .name = "4:2:2, packed, YUYV",
558 .fourcc = V4L2_PIX_FMT_YUYV,
559 .btformat = BT848_COLOR_FMT_YUY2,
561 .flags = FORMAT_FLAGS_PACKED,
563 .name = "4:2:2, packed, UYVY",
564 .fourcc = V4L2_PIX_FMT_UYVY,
565 .btformat = BT848_COLOR_FMT_YUY2,
566 .btswap = 0x03, /* byteswap */
568 .flags = FORMAT_FLAGS_PACKED,
570 .name = "4:2:2, planar, Y-Cb-Cr",
571 .fourcc = V4L2_PIX_FMT_YUV422P,
572 .btformat = BT848_COLOR_FMT_YCrCb422,
574 .flags = FORMAT_FLAGS_PLANAR,
578 .name = "4:2:0, planar, Y-Cb-Cr",
579 .fourcc = V4L2_PIX_FMT_YUV420,
580 .btformat = BT848_COLOR_FMT_YCrCb422,
582 .flags = FORMAT_FLAGS_PLANAR,
586 .name = "4:2:0, planar, Y-Cr-Cb",
587 .fourcc = V4L2_PIX_FMT_YVU420,
588 .btformat = BT848_COLOR_FMT_YCrCb422,
590 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
594 .name = "4:1:1, planar, Y-Cb-Cr",
595 .fourcc = V4L2_PIX_FMT_YUV411P,
596 .btformat = BT848_COLOR_FMT_YCrCb411,
598 .flags = FORMAT_FLAGS_PLANAR,
602 .name = "4:1:0, planar, Y-Cb-Cr",
603 .fourcc = V4L2_PIX_FMT_YUV410,
604 .btformat = BT848_COLOR_FMT_YCrCb411,
606 .flags = FORMAT_FLAGS_PLANAR,
610 .name = "4:1:0, planar, Y-Cr-Cb",
611 .fourcc = V4L2_PIX_FMT_YVU410,
612 .btformat = BT848_COLOR_FMT_YCrCb411,
614 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
618 .name = "raw scanlines",
620 .btformat = BT848_COLOR_FMT_RAW,
622 .flags = FORMAT_FLAGS_RAW,
625 static const unsigned int FORMATS = ARRAY_SIZE(formats);
627 /* ----------------------------------------------------------------------- */
629 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
630 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
631 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
632 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
633 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
634 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
635 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
636 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
637 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
638 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
639 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
640 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
642 static const struct v4l2_queryctrl no_ctl = {
644 .flags = V4L2_CTRL_FLAG_DISABLED,
646 static const struct v4l2_queryctrl bttv_ctls[] = {
649 .id = V4L2_CID_BRIGHTNESS,
650 .name = "Brightness",
654 .default_value = 32768,
655 .type = V4L2_CTRL_TYPE_INTEGER,
657 .id = V4L2_CID_CONTRAST,
662 .default_value = 32768,
663 .type = V4L2_CTRL_TYPE_INTEGER,
665 .id = V4L2_CID_SATURATION,
666 .name = "Saturation",
670 .default_value = 32768,
671 .type = V4L2_CTRL_TYPE_INTEGER,
678 .default_value = 32768,
679 .type = V4L2_CTRL_TYPE_INTEGER,
683 .id = V4L2_CID_AUDIO_MUTE,
687 .type = V4L2_CTRL_TYPE_BOOLEAN,
689 .id = V4L2_CID_AUDIO_VOLUME,
694 .default_value = 65535,
695 .type = V4L2_CTRL_TYPE_INTEGER,
697 .id = V4L2_CID_AUDIO_BALANCE,
702 .default_value = 32768,
703 .type = V4L2_CTRL_TYPE_INTEGER,
705 .id = V4L2_CID_AUDIO_BASS,
710 .default_value = 32768,
711 .type = V4L2_CTRL_TYPE_INTEGER,
713 .id = V4L2_CID_AUDIO_TREBLE,
718 .default_value = 32768,
719 .type = V4L2_CTRL_TYPE_INTEGER,
721 /* --- private --- */
723 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
724 .name = "chroma agc",
727 .type = V4L2_CTRL_TYPE_BOOLEAN,
729 .id = V4L2_CID_PRIVATE_COMBFILTER,
730 .name = "combfilter",
733 .type = V4L2_CTRL_TYPE_BOOLEAN,
735 .id = V4L2_CID_PRIVATE_AUTOMUTE,
739 .type = V4L2_CTRL_TYPE_BOOLEAN,
741 .id = V4L2_CID_PRIVATE_LUMAFILTER,
742 .name = "luma decimation filter",
745 .type = V4L2_CTRL_TYPE_BOOLEAN,
747 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
751 .type = V4L2_CTRL_TYPE_BOOLEAN,
753 .id = V4L2_CID_PRIVATE_VCR_HACK,
757 .type = V4L2_CTRL_TYPE_BOOLEAN,
759 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
760 .name = "whitecrush upper",
764 .default_value = 0xCF,
765 .type = V4L2_CTRL_TYPE_INTEGER,
767 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
768 .name = "whitecrush lower",
772 .default_value = 0x7F,
773 .type = V4L2_CTRL_TYPE_INTEGER,
775 .id = V4L2_CID_PRIVATE_UV_RATIO,
781 .type = V4L2_CTRL_TYPE_INTEGER,
783 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
784 .name = "full luma range",
787 .type = V4L2_CTRL_TYPE_BOOLEAN,
789 .id = V4L2_CID_PRIVATE_CORING,
795 .type = V4L2_CTRL_TYPE_INTEGER,
802 static const struct v4l2_queryctrl *ctrl_by_id(int id)
806 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
807 if (bttv_ctls[i].id == id)
813 /* ----------------------------------------------------------------------- */
814 /* resource management */
817 RESOURCE_ allocated by freed by
819 VIDEO_READ bttv_read 1) bttv_read 2)
821 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
822 VIDIOC_QBUF 1) bttv_release
825 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
826 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
829 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
830 VIDIOC_QBUF 1) bttv_release
831 bttv_read, bttv_poll 1) 4)
833 1) The resource must be allocated when we enter buffer prepare functions
834 and remain allocated while buffers are in the DMA queue.
835 2) This is a single frame read.
836 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
837 RESOURCE_OVERLAY is allocated.
838 4) This is a continuous read, implies VIDIOC_STREAMON.
840 Note this driver permits video input and standard changes regardless if
841 resources are allocated.
844 #define VBI_RESOURCES (RESOURCE_VBI)
845 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
846 RESOURCE_VIDEO_STREAM | \
850 int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
852 int xbits; /* mutual exclusive resources */
854 if (fh->resources & bit)
855 /* have it already allocated */
859 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
860 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
863 mutex_lock(&btv->lock);
864 if (btv->resources & xbits) {
865 /* no, someone else uses it */
869 if ((bit & VIDEO_RESOURCES)
870 && 0 == (btv->resources & VIDEO_RESOURCES)) {
871 /* Do crop - use current, don't - use default parameters. */
872 __s32 top = btv->crop[!!fh->do_crop].rect.top;
874 if (btv->vbi_end > top)
877 /* We cannot capture the same line as video and VBI data.
878 Claim scan lines crop[].rect.top to bottom. */
879 btv->crop_start = top;
880 } else if (bit & VBI_RESOURCES) {
881 __s32 end = fh->vbi_fmt.end;
883 if (end > btv->crop_start)
886 /* Claim scan lines above fh->vbi_fmt.end. */
890 /* it's free, grab it */
891 fh->resources |= bit;
892 btv->resources |= bit;
893 mutex_unlock(&btv->lock);
897 mutex_unlock(&btv->lock);
902 int check_btres(struct bttv_fh *fh, int bit)
904 return (fh->resources & bit);
908 int locked_btres(struct bttv *btv, int bit)
910 return (btv->resources & bit);
913 /* Call with btv->lock down. */
915 disclaim_vbi_lines(struct bttv *btv)
920 /* Call with btv->lock down. */
922 disclaim_video_lines(struct bttv *btv)
924 const struct bttv_tvnorm *tvnorm;
927 tvnorm = &bttv_tvnorms[btv->tvnorm];
928 btv->crop_start = tvnorm->cropcap.bounds.top
929 + tvnorm->cropcap.bounds.height;
931 /* VBI capturing ends at VDELAY, start of video capturing, no
932 matter how many lines the VBI RISC program expects. When video
933 capturing is off, it shall no longer "preempt" VBI capturing,
934 so we set VDELAY to maximum. */
935 crop = btread(BT848_E_CROP) | 0xc0;
936 btwrite(crop, BT848_E_CROP);
937 btwrite(0xfe, BT848_E_VDELAY_LO);
938 btwrite(crop, BT848_O_CROP);
939 btwrite(0xfe, BT848_O_VDELAY_LO);
943 void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
945 if ((fh->resources & bits) != bits) {
946 /* trying to free ressources not allocated by us ... */
947 printk("bttv: BUG! (btres)\n");
949 mutex_lock(&btv->lock);
950 fh->resources &= ~bits;
951 btv->resources &= ~bits;
953 bits = btv->resources;
955 if (0 == (bits & VIDEO_RESOURCES))
956 disclaim_video_lines(btv);
958 if (0 == (bits & VBI_RESOURCES))
959 disclaim_vbi_lines(btv);
961 mutex_unlock(&btv->lock);
964 /* ----------------------------------------------------------------------- */
965 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
967 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
968 PLL_X = Reference pre-divider (0=1, 1=2)
969 PLL_C = Post divider (0=6, 1=4)
970 PLL_I = Integer input
971 PLL_F = Fractional input
973 F_input = 28.636363 MHz:
974 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
977 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
979 unsigned char fl, fh, fi;
981 /* prevent overflows */
994 btwrite(fl, BT848_PLL_F_LO);
995 btwrite(fh, BT848_PLL_F_HI);
996 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
999 static void set_pll(struct bttv *btv)
1003 if (!btv->pll.pll_crystal)
1006 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1007 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
1011 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1013 if (btv->pll.pll_current == 0)
1015 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
1016 btv->c.nr,btv->pll.pll_ifreq);
1017 btwrite(0x00,BT848_TGCTRL);
1018 btwrite(0x00,BT848_PLL_XCI);
1019 btv->pll.pll_current = 0;
1023 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
1024 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1025 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1027 for (i=0; i<10; i++) {
1028 /* Let other people run while the PLL stabilizes */
1032 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1033 btwrite(0,BT848_DSTATUS);
1035 btwrite(0x08,BT848_TGCTRL);
1036 btv->pll.pll_current = btv->pll.pll_ofreq;
1037 bttv_printk(" ok\n");
1041 btv->pll.pll_current = -1;
1042 bttv_printk("failed\n");
1046 /* used to switch between the bt848's analog/digital video capture modes */
1047 static void bt848A_set_timing(struct bttv *btv)
1050 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1051 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1053 if (btv->input == btv->dig) {
1054 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1055 btv->c.nr,table_idx);
1057 /* timing change...reset timing generator address */
1058 btwrite(0x00, BT848_TGCTRL);
1059 btwrite(0x02, BT848_TGCTRL);
1060 btwrite(0x00, BT848_TGCTRL);
1062 len=SRAM_Table[table_idx][0];
1063 for(i = 1; i <= len; i++)
1064 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1065 btv->pll.pll_ofreq = 27000000;
1068 btwrite(0x11, BT848_TGCTRL);
1069 btwrite(0x41, BT848_DVSIF);
1071 btv->pll.pll_ofreq = fsc;
1073 btwrite(0x0, BT848_DVSIF);
1077 /* ----------------------------------------------------------------------- */
1079 static void bt848_bright(struct bttv *btv, int bright)
1083 // printk("bttv: set bright: %d\n",bright); // DEBUG
1084 btv->bright = bright;
1086 /* We want -128 to 127 we get 0-65535 */
1087 value = (bright >> 8) - 128;
1088 btwrite(value & 0xff, BT848_BRIGHT);
1091 static void bt848_hue(struct bttv *btv, int hue)
1098 value = (hue >> 8) - 128;
1099 btwrite(value & 0xff, BT848_HUE);
1102 static void bt848_contrast(struct bttv *btv, int cont)
1106 btv->contrast = cont;
1109 value = (cont >> 7);
1110 hibit = (value >> 6) & 4;
1111 btwrite(value & 0xff, BT848_CONTRAST_LO);
1112 btaor(hibit, ~4, BT848_E_CONTROL);
1113 btaor(hibit, ~4, BT848_O_CONTROL);
1116 static void bt848_sat(struct bttv *btv, int color)
1118 int val_u,val_v,hibits;
1120 btv->saturation = color;
1122 /* 0-511 for the color */
1123 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1124 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1125 hibits = (val_u >> 7) & 2;
1126 hibits |= (val_v >> 8) & 1;
1127 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1128 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1129 btaor(hibits, ~3, BT848_E_CONTROL);
1130 btaor(hibits, ~3, BT848_O_CONTROL);
1133 /* ----------------------------------------------------------------------- */
1136 video_mux(struct bttv *btv, unsigned int input)
1140 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1143 /* needed by RemoteVideo MX */
1144 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1146 gpio_inout(mask2,mask2);
1148 if (input == btv->svhs) {
1149 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1150 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1152 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1153 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1155 mux = bttv_muxsel(btv, input);
1156 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1157 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1158 btv->c.nr,input,mux);
1160 /* card specific hook */
1161 if(bttv_tvcards[btv->c.type].muxsel_hook)
1162 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1166 static char *audio_modes[] = {
1167 "audio: tuner", "audio: radio", "audio: extern",
1168 "audio: intern", "audio: mute"
1172 audio_mux(struct bttv *btv, int input, int mute)
1174 int gpio_val, signal;
1175 struct v4l2_control ctrl;
1177 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1178 bttv_tvcards[btv->c.type].gpiomask);
1179 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1185 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1188 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1190 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1192 switch (btv->c.type) {
1193 case BTTV_BOARD_VOODOOTV_FM:
1194 case BTTV_BOARD_VOODOOTV_200:
1195 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1199 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1203 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1207 ctrl.id = V4L2_CID_AUDIO_MUTE;
1208 ctrl.value = btv->mute;
1209 bttv_call_all(btv, core, s_ctrl, &ctrl);
1210 if (btv->sd_msp34xx) {
1213 /* Note: the inputs tuner/radio/extern/intern are translated
1214 to msp routings. This assumes common behavior for all msp3400
1215 based TV cards. When this assumption fails, then the
1216 specific MSP routing must be added to the card table.
1217 For now this is sufficient. */
1219 case TVAUDIO_INPUT_RADIO:
1220 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1221 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1223 case TVAUDIO_INPUT_EXTERN:
1224 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1225 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1227 case TVAUDIO_INPUT_INTERN:
1228 /* Yes, this is the same input as for RADIO. I doubt
1229 if this is ever used. The only board with an INTERN
1230 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1231 that was tested. My guess is that the whole INTERN
1232 input does not work. */
1233 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1234 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1236 case TVAUDIO_INPUT_TUNER:
1238 /* This is the only card that uses TUNER2, and afaik,
1239 is the only difference between the VOODOOTV_FM
1241 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1242 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1243 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1245 in = MSP_INPUT_DEFAULT;
1248 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1249 in, MSP_OUTPUT_DEFAULT, 0);
1251 if (btv->sd_tvaudio) {
1252 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1259 audio_mute(struct bttv *btv, int mute)
1261 return audio_mux(btv, btv->audio, mute);
1265 audio_input(struct bttv *btv, int input)
1267 return audio_mux(btv, input, btv->mute);
1271 bttv_crop_calc_limits(struct bttv_crop *c)
1273 /* Scale factor min. 1:1, max. 16:1. Min. image size
1274 48 x 32. Scaled width must be a multiple of 4. */
1277 /* For bug compatibility with VIDIOCGCAP and image
1278 size checks in earlier driver versions. */
1279 c->min_scaled_width = 48;
1280 c->min_scaled_height = 32;
1282 c->min_scaled_width =
1283 (max(48, c->rect.width >> 4) + 3) & ~3;
1284 c->min_scaled_height =
1285 max(32, c->rect.height >> 4);
1288 c->max_scaled_width = c->rect.width & ~3;
1289 c->max_scaled_height = c->rect.height;
1293 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1295 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1296 bttv_crop_calc_limits(c);
1299 /* Call with btv->lock down. */
1301 set_tvnorm(struct bttv *btv, unsigned int norm)
1303 const struct bttv_tvnorm *tvnorm;
1306 BUG_ON(norm >= BTTV_TVNORMS);
1307 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
1309 tvnorm = &bttv_tvnorms[norm];
1311 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1312 sizeof (tvnorm->cropcap))) {
1313 bttv_crop_reset(&btv->crop[0], norm);
1314 btv->crop[1] = btv->crop[0]; /* current = default */
1316 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1317 btv->crop_start = tvnorm->cropcap.bounds.top
1318 + tvnorm->cropcap.bounds.height;
1324 btwrite(tvnorm->adelay, BT848_ADELAY);
1325 btwrite(tvnorm->bdelay, BT848_BDELAY);
1326 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1328 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1329 btwrite(1, BT848_VBI_PACK_DEL);
1330 bt848A_set_timing(btv);
1332 switch (btv->c.type) {
1333 case BTTV_BOARD_VOODOOTV_FM:
1334 case BTTV_BOARD_VOODOOTV_200:
1335 bttv_tda9880_setnorm(btv, gpio_read());
1338 id = tvnorm->v4l2_id;
1339 bttv_call_all(btv, core, s_std, id);
1344 /* Call with btv->lock down. */
1346 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1348 unsigned long flags;
1352 spin_lock_irqsave(&btv->s_lock,flags);
1353 if (btv->curr.frame_irq) {
1354 /* active capture -> delayed input switch */
1355 btv->new_input = input;
1357 video_mux(btv,input);
1359 spin_unlock_irqrestore(&btv->s_lock,flags);
1361 video_mux(btv,input);
1363 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1364 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
1365 set_tvnorm(btv, norm);
1368 static void init_irqreg(struct bttv *btv)
1371 btwrite(0xfffffUL, BT848_INT_STAT);
1373 if (bttv_tvcards[btv->c.type].no_video) {
1375 btwrite(BT848_INT_I2CDONE,
1379 btwrite((btv->triton1) |
1380 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1382 (fdsr ? BT848_INT_FDSR : 0) |
1383 BT848_INT_RISCI | BT848_INT_OCERR |
1384 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1390 static void init_bt848(struct bttv *btv)
1394 if (bttv_tvcards[btv->c.type].no_video) {
1395 /* very basic init only */
1400 btwrite(0x00, BT848_CAP_CTL);
1401 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1402 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1404 /* set planar and packed mode trigger points and */
1405 /* set rising edge of inverted GPINTR pin as irq trigger */
1406 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1407 BT848_GPIO_DMA_CTL_PLTP1_16|
1408 BT848_GPIO_DMA_CTL_PLTP23_16|
1409 BT848_GPIO_DMA_CTL_GPINTC|
1410 BT848_GPIO_DMA_CTL_GPINTI,
1411 BT848_GPIO_DMA_CTL);
1413 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1414 btwrite(val, BT848_E_SCLOOP);
1415 btwrite(val, BT848_O_SCLOOP);
1417 btwrite(0x20, BT848_E_VSCALE_HI);
1418 btwrite(0x20, BT848_O_VSCALE_HI);
1419 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1422 btwrite(whitecrush_upper, BT848_WC_UP);
1423 btwrite(whitecrush_lower, BT848_WC_DOWN);
1425 if (btv->opt_lumafilter) {
1426 btwrite(0, BT848_E_CONTROL);
1427 btwrite(0, BT848_O_CONTROL);
1429 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1430 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1433 bt848_bright(btv, btv->bright);
1434 bt848_hue(btv, btv->hue);
1435 bt848_contrast(btv, btv->contrast);
1436 bt848_sat(btv, btv->saturation);
1442 static void bttv_reinit_bt848(struct bttv *btv)
1444 unsigned long flags;
1447 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1448 spin_lock_irqsave(&btv->s_lock,flags);
1450 bttv_set_dma(btv,0);
1451 spin_unlock_irqrestore(&btv->s_lock,flags);
1454 btv->pll.pll_current = -1;
1455 set_input(btv, btv->input, btv->tvnorm);
1458 static int bttv_g_ctrl(struct file *file, void *priv,
1459 struct v4l2_control *c)
1461 struct bttv_fh *fh = priv;
1462 struct bttv *btv = fh->btv;
1465 case V4L2_CID_BRIGHTNESS:
1466 c->value = btv->bright;
1469 c->value = btv->hue;
1471 case V4L2_CID_CONTRAST:
1472 c->value = btv->contrast;
1474 case V4L2_CID_SATURATION:
1475 c->value = btv->saturation;
1478 case V4L2_CID_AUDIO_MUTE:
1479 case V4L2_CID_AUDIO_VOLUME:
1480 case V4L2_CID_AUDIO_BALANCE:
1481 case V4L2_CID_AUDIO_BASS:
1482 case V4L2_CID_AUDIO_TREBLE:
1483 bttv_call_all(btv, core, g_ctrl, c);
1486 case V4L2_CID_PRIVATE_CHROMA_AGC:
1487 c->value = btv->opt_chroma_agc;
1489 case V4L2_CID_PRIVATE_COMBFILTER:
1490 c->value = btv->opt_combfilter;
1492 case V4L2_CID_PRIVATE_LUMAFILTER:
1493 c->value = btv->opt_lumafilter;
1495 case V4L2_CID_PRIVATE_AUTOMUTE:
1496 c->value = btv->opt_automute;
1498 case V4L2_CID_PRIVATE_AGC_CRUSH:
1499 c->value = btv->opt_adc_crush;
1501 case V4L2_CID_PRIVATE_VCR_HACK:
1502 c->value = btv->opt_vcr_hack;
1504 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1505 c->value = btv->opt_whitecrush_upper;
1507 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1508 c->value = btv->opt_whitecrush_lower;
1510 case V4L2_CID_PRIVATE_UV_RATIO:
1511 c->value = btv->opt_uv_ratio;
1513 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1514 c->value = btv->opt_full_luma_range;
1516 case V4L2_CID_PRIVATE_CORING:
1517 c->value = btv->opt_coring;
1525 static int bttv_s_ctrl(struct file *file, void *f,
1526 struct v4l2_control *c)
1530 struct bttv_fh *fh = f;
1531 struct bttv *btv = fh->btv;
1533 err = v4l2_prio_check(&btv->prio, fh->prio);
1538 case V4L2_CID_BRIGHTNESS:
1539 bt848_bright(btv, c->value);
1542 bt848_hue(btv, c->value);
1544 case V4L2_CID_CONTRAST:
1545 bt848_contrast(btv, c->value);
1547 case V4L2_CID_SATURATION:
1548 bt848_sat(btv, c->value);
1550 case V4L2_CID_AUDIO_MUTE:
1551 audio_mute(btv, c->value);
1553 case V4L2_CID_AUDIO_VOLUME:
1554 if (btv->volume_gpio)
1555 btv->volume_gpio(btv, c->value);
1557 bttv_call_all(btv, core, s_ctrl, c);
1559 case V4L2_CID_AUDIO_BALANCE:
1560 case V4L2_CID_AUDIO_BASS:
1561 case V4L2_CID_AUDIO_TREBLE:
1562 bttv_call_all(btv, core, s_ctrl, c);
1565 case V4L2_CID_PRIVATE_CHROMA_AGC:
1566 btv->opt_chroma_agc = c->value;
1567 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1568 btwrite(val, BT848_E_SCLOOP);
1569 btwrite(val, BT848_O_SCLOOP);
1571 case V4L2_CID_PRIVATE_COMBFILTER:
1572 btv->opt_combfilter = c->value;
1574 case V4L2_CID_PRIVATE_LUMAFILTER:
1575 btv->opt_lumafilter = c->value;
1576 if (btv->opt_lumafilter) {
1577 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1578 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1580 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1581 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1584 case V4L2_CID_PRIVATE_AUTOMUTE:
1585 btv->opt_automute = c->value;
1587 case V4L2_CID_PRIVATE_AGC_CRUSH:
1588 btv->opt_adc_crush = c->value;
1589 btwrite(BT848_ADC_RESERVED |
1590 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1593 case V4L2_CID_PRIVATE_VCR_HACK:
1594 btv->opt_vcr_hack = c->value;
1596 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1597 btv->opt_whitecrush_upper = c->value;
1598 btwrite(c->value, BT848_WC_UP);
1600 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1601 btv->opt_whitecrush_lower = c->value;
1602 btwrite(c->value, BT848_WC_DOWN);
1604 case V4L2_CID_PRIVATE_UV_RATIO:
1605 btv->opt_uv_ratio = c->value;
1606 bt848_sat(btv, btv->saturation);
1608 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1609 btv->opt_full_luma_range = c->value;
1610 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1612 case V4L2_CID_PRIVATE_CORING:
1613 btv->opt_coring = c->value;
1614 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1622 /* ----------------------------------------------------------------------- */
1624 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1626 unsigned int outbits, data;
1627 outbits = btread(BT848_GPIO_OUT_EN);
1628 data = btread(BT848_GPIO_DATA);
1629 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1630 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1633 static void bttv_field_count(struct bttv *btv)
1641 /* start field counter */
1642 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1644 /* stop field counter */
1645 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1646 btv->field_count = 0;
1650 static const struct bttv_format*
1651 format_by_fourcc(int fourcc)
1655 for (i = 0; i < FORMATS; i++) {
1656 if (-1 == formats[i].fourcc)
1658 if (formats[i].fourcc == fourcc)
1664 /* ----------------------------------------------------------------------- */
1668 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1669 struct bttv_buffer *new)
1671 struct bttv_buffer *old;
1672 unsigned long flags;
1675 dprintk("switch_overlay: enter [new=%p]\n",new);
1677 new->vb.state = VIDEOBUF_DONE;
1678 spin_lock_irqsave(&btv->s_lock,flags);
1682 bttv_set_dma(btv, 0x03);
1683 spin_unlock_irqrestore(&btv->s_lock,flags);
1685 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1686 bttv_dma_free(&fh->cap,btv, old);
1690 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
1691 dprintk("switch_overlay: done\n");
1695 /* ----------------------------------------------------------------------- */
1696 /* video4linux (1) interface */
1698 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1699 struct bttv_buffer *buf,
1700 const struct bttv_format *fmt,
1701 unsigned int width, unsigned int height,
1702 enum v4l2_field field)
1704 struct bttv_fh *fh = q->priv_data;
1705 int redo_dma_risc = 0;
1710 /* check settings */
1713 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1715 height = RAW_LINES*2;
1716 if (width*height > buf->vb.bsize)
1718 buf->vb.size = buf->vb.bsize;
1720 /* Make sure tvnorm and vbi_end remain consistent
1721 until we're done. */
1722 mutex_lock(&btv->lock);
1726 /* In this mode capturing always starts at defrect.top
1727 (default VDELAY), ignoring cropping parameters. */
1728 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1729 mutex_unlock(&btv->lock);
1733 mutex_unlock(&btv->lock);
1735 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1737 mutex_lock(&btv->lock);
1740 c = btv->crop[!!fh->do_crop];
1742 mutex_unlock(&btv->lock);
1744 if (width < c.min_scaled_width ||
1745 width > c.max_scaled_width ||
1746 height < c.min_scaled_height)
1750 case V4L2_FIELD_TOP:
1751 case V4L2_FIELD_BOTTOM:
1752 case V4L2_FIELD_ALTERNATE:
1753 /* btv->crop counts frame lines. Max. scale
1754 factor is 16:1 for frames, 8:1 for fields. */
1755 if (height * 2 > c.max_scaled_height)
1760 if (height > c.max_scaled_height)
1765 buf->vb.size = (width * height * fmt->depth) >> 3;
1766 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1770 /* alloc + fill struct bttv_buffer (if changed) */
1771 if (buf->vb.width != width || buf->vb.height != height ||
1772 buf->vb.field != field ||
1773 buf->tvnorm != norm || buf->fmt != fmt ||
1774 buf->crop.top != c.rect.top ||
1775 buf->crop.left != c.rect.left ||
1776 buf->crop.width != c.rect.width ||
1777 buf->crop.height != c.rect.height) {
1778 buf->vb.width = width;
1779 buf->vb.height = height;
1780 buf->vb.field = field;
1787 /* alloc risc memory */
1788 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1790 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1795 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1798 buf->vb.state = VIDEOBUF_PREPARED;
1802 bttv_dma_free(q,btv,buf);
1807 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1809 struct bttv_fh *fh = q->priv_data;
1811 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1814 if (*size * *count > gbuffers * gbufsize)
1815 *count = (gbuffers * gbufsize) / *size;
1820 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1821 enum v4l2_field field)
1823 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1824 struct bttv_fh *fh = q->priv_data;
1826 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1827 fh->width, fh->height, field);
1831 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1833 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1834 struct bttv_fh *fh = q->priv_data;
1835 struct bttv *btv = fh->btv;
1837 buf->vb.state = VIDEOBUF_QUEUED;
1838 list_add_tail(&buf->vb.queue,&btv->capture);
1839 if (!btv->curr.frame_irq) {
1841 bttv_set_dma(btv, 0x03);
1845 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1847 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1848 struct bttv_fh *fh = q->priv_data;
1850 bttv_dma_free(q,fh->btv,buf);
1853 static struct videobuf_queue_ops bttv_video_qops = {
1854 .buf_setup = buffer_setup,
1855 .buf_prepare = buffer_prepare,
1856 .buf_queue = buffer_queue,
1857 .buf_release = buffer_release,
1860 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
1862 struct bttv_fh *fh = priv;
1863 struct bttv *btv = fh->btv;
1867 mutex_lock(&btv->lock);
1868 err = v4l2_prio_check(&btv->prio, fh->prio);
1872 for (i = 0; i < BTTV_TVNORMS; i++)
1873 if (*id & bttv_tvnorms[i].v4l2_id)
1875 if (i == BTTV_TVNORMS) {
1883 mutex_unlock(&btv->lock);
1888 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1890 struct bttv_fh *fh = f;
1891 struct bttv *btv = fh->btv;
1893 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1894 *id = V4L2_STD_625_50;
1896 *id = V4L2_STD_525_60;
1900 static int bttv_enum_input(struct file *file, void *priv,
1901 struct v4l2_input *i)
1903 struct bttv_fh *fh = priv;
1904 struct bttv *btv = fh->btv;
1907 mutex_lock(&btv->lock);
1908 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1913 i->type = V4L2_INPUT_TYPE_CAMERA;
1916 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1917 sprintf(i->name, "Television");
1918 i->type = V4L2_INPUT_TYPE_TUNER;
1920 } else if (i->index == btv->svhs) {
1921 sprintf(i->name, "S-Video");
1923 sprintf(i->name, "Composite%d", i->index);
1926 if (i->index == btv->input) {
1927 __u32 dstatus = btread(BT848_DSTATUS);
1928 if (0 == (dstatus & BT848_DSTATUS_PRES))
1929 i->status |= V4L2_IN_ST_NO_SIGNAL;
1930 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1931 i->status |= V4L2_IN_ST_NO_H_LOCK;
1934 i->std = BTTV_NORMS;
1937 mutex_unlock(&btv->lock);
1942 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1944 struct bttv_fh *fh = priv;
1945 struct bttv *btv = fh->btv;
1947 mutex_lock(&btv->lock);
1949 mutex_unlock(&btv->lock);
1954 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1956 struct bttv_fh *fh = priv;
1957 struct bttv *btv = fh->btv;
1961 mutex_lock(&btv->lock);
1962 err = v4l2_prio_check(&btv->prio, fh->prio);
1966 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1971 set_input(btv, i, btv->tvnorm);
1974 mutex_unlock(&btv->lock);
1978 static int bttv_s_tuner(struct file *file, void *priv,
1979 struct v4l2_tuner *t)
1981 struct bttv_fh *fh = priv;
1982 struct bttv *btv = fh->btv;
1985 if (unlikely(0 != t->index))
1988 mutex_lock(&btv->lock);
1989 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
1994 err = v4l2_prio_check(&btv->prio, fh->prio);
1998 bttv_call_all(btv, tuner, s_tuner, t);
2000 if (btv->audio_mode_gpio)
2001 btv->audio_mode_gpio(btv, t, 1);
2004 mutex_unlock(&btv->lock);
2009 static int bttv_g_frequency(struct file *file, void *priv,
2010 struct v4l2_frequency *f)
2012 struct bttv_fh *fh = priv;
2013 struct bttv *btv = fh->btv;
2015 mutex_lock(&btv->lock);
2016 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2017 f->frequency = btv->freq;
2018 mutex_unlock(&btv->lock);
2023 static int bttv_s_frequency(struct file *file, void *priv,
2024 struct v4l2_frequency *f)
2026 struct bttv_fh *fh = priv;
2027 struct bttv *btv = fh->btv;
2030 if (unlikely(f->tuner != 0))
2033 mutex_lock(&btv->lock);
2034 err = v4l2_prio_check(&btv->prio, fh->prio);
2038 if (unlikely(f->type != (btv->radio_user
2039 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2043 btv->freq = f->frequency;
2044 bttv_call_all(btv, tuner, s_frequency, f);
2045 if (btv->has_matchbox && btv->radio_user)
2046 tea5757_set_freq(btv, btv->freq);
2048 mutex_unlock(&btv->lock);
2053 static int bttv_log_status(struct file *file, void *f)
2055 struct bttv_fh *fh = f;
2056 struct bttv *btv = fh->btv;
2058 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2059 btv->c.nr, btv->c.nr);
2060 bttv_call_all(btv, core, log_status);
2061 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2062 btv->c.nr, btv->c.nr);
2066 #ifdef CONFIG_VIDEO_ADV_DEBUG
2067 static int bttv_g_register(struct file *file, void *f,
2068 struct v4l2_dbg_register *reg)
2070 struct bttv_fh *fh = f;
2071 struct bttv *btv = fh->btv;
2073 if (!capable(CAP_SYS_ADMIN))
2076 if (!v4l2_chip_match_host(®->match))
2079 /* bt848 has a 12-bit register space */
2081 reg->val = btread(reg->reg);
2087 static int bttv_s_register(struct file *file, void *f,
2088 struct v4l2_dbg_register *reg)
2090 struct bttv_fh *fh = f;
2091 struct bttv *btv = fh->btv;
2093 if (!capable(CAP_SYS_ADMIN))
2096 if (!v4l2_chip_match_host(®->match))
2099 /* bt848 has a 12-bit register space */
2101 btwrite(reg->val, reg->reg);
2107 /* Given cropping boundaries b and the scaled width and height of a
2108 single field or frame, which must not exceed hardware limits, this
2109 function adjusts the cropping parameters c. */
2111 bttv_crop_adjust (struct bttv_crop * c,
2112 const struct v4l2_rect * b,
2115 enum v4l2_field field)
2117 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2121 if (width < c->min_scaled_width) {
2122 /* Max. hor. scale factor 16:1. */
2123 c->rect.width = width * 16;
2124 } else if (width > c->max_scaled_width) {
2125 /* Min. hor. scale factor 1:1. */
2126 c->rect.width = width;
2128 max_left = b->left + b->width - width;
2129 max_left = min(max_left, (__s32) MAX_HDELAY);
2130 if (c->rect.left > max_left)
2131 c->rect.left = max_left;
2134 if (height < c->min_scaled_height) {
2135 /* Max. vert. scale factor 16:1, single fields 8:1. */
2136 c->rect.height = height * 16;
2137 } else if (frame_height > c->max_scaled_height) {
2138 /* Min. vert. scale factor 1:1.
2139 Top and height count field lines times two. */
2140 c->rect.height = (frame_height + 1) & ~1;
2142 max_top = b->top + b->height - c->rect.height;
2143 if (c->rect.top > max_top)
2144 c->rect.top = max_top;
2147 bttv_crop_calc_limits(c);
2150 /* Returns an error if scaling to a frame or single field with the given
2151 width and height is not possible with the current cropping parameters
2152 and width aligned according to width_mask. If adjust_size is TRUE the
2153 function may adjust the width and/or height instead, rounding width
2154 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2155 also adjust the current cropping parameters to get closer to the
2156 desired image size. */
2158 limit_scaled_size_lock (struct bttv_fh * fh,
2161 enum v4l2_field field,
2162 unsigned int width_mask,
2163 unsigned int width_bias,
2167 struct bttv *btv = fh->btv;
2168 const struct v4l2_rect *b;
2169 struct bttv_crop *c;
2176 BUG_ON((int) width_mask >= 0 ||
2177 width_bias >= (unsigned int) -width_mask);
2179 /* Make sure tvnorm, vbi_end and the current cropping parameters
2180 remain consistent until we're done. */
2181 mutex_lock(&btv->lock);
2183 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2185 /* Do crop - use current, don't - use default parameters. */
2186 c = &btv->crop[!!fh->do_crop];
2191 && !locked_btres(btv, VIDEO_RESOURCES)) {
2195 /* We cannot scale up. When the scaled image is larger
2196 than crop.rect we adjust the crop.rect as required
2197 by the V4L2 spec, hence cropcap.bounds are our limit. */
2198 max_width = min(b->width, (__s32) MAX_HACTIVE);
2199 max_height = b->height;
2201 /* We cannot capture the same line as video and VBI data.
2202 Note btv->vbi_end is really a minimum, see
2203 bttv_vbi_try_fmt(). */
2204 if (btv->vbi_end > b->top) {
2205 max_height -= btv->vbi_end - b->top;
2207 if (min_height > max_height)
2212 if (btv->vbi_end > c->rect.top)
2215 min_width = c->min_scaled_width;
2216 min_height = c->min_scaled_height;
2217 max_width = c->max_scaled_width;
2218 max_height = c->max_scaled_height;
2223 min_width = (min_width - width_mask - 1) & width_mask;
2224 max_width = max_width & width_mask;
2226 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2227 min_height = min_height;
2228 /* Min. scale factor is 1:1. */
2229 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2232 *width = clamp(*width, min_width, max_width);
2233 *height = clamp(*height, min_height, max_height);
2235 /* Round after clamping to avoid overflow. */
2236 *width = (*width + width_bias) & width_mask;
2239 bttv_crop_adjust(c, b, *width, *height, field);
2241 if (btv->vbi_end > c->rect.top) {
2242 /* Move the crop window out of the way. */
2243 c->rect.top = btv->vbi_end;
2248 if (*width < min_width ||
2249 *height < min_height ||
2250 *width > max_width ||
2251 *height > max_height ||
2252 0 != (*width & ~width_mask))
2256 rc = 0; /* success */
2259 mutex_unlock(&btv->lock);
2264 /* Returns an error if the given overlay window dimensions are not
2265 possible with the current cropping parameters. If adjust_size is
2266 TRUE the function may adjust the window width and/or height
2267 instead, however it always rounds the horizontal position and
2268 width as btcx_align() does. If adjust_crop is TRUE the function
2269 may also adjust the current cropping parameters to get closer
2270 to the desired window size. */
2272 verify_window_lock (struct bttv_fh * fh,
2273 struct v4l2_window * win,
2277 enum v4l2_field field;
2278 unsigned int width_mask;
2281 if (win->w.width < 48 || win->w.height < 32)
2283 if (win->clipcount > 2048)
2288 if (V4L2_FIELD_ANY == field) {
2291 mutex_lock(&fh->btv->lock);
2292 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2293 mutex_unlock(&fh->btv->lock);
2294 field = (win->w.height > height2)
2295 ? V4L2_FIELD_INTERLACED
2299 case V4L2_FIELD_TOP:
2300 case V4L2_FIELD_BOTTOM:
2301 case V4L2_FIELD_INTERLACED:
2307 /* 4-byte alignment. */
2308 if (NULL == fh->ovfmt)
2311 switch (fh->ovfmt->depth) {
2325 win->w.width -= win->w.left & ~width_mask;
2326 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2328 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
2330 /* width_bias: round down */ 0,
2331 adjust_size, adjust_crop);
2339 static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
2340 struct v4l2_window *win, int fixup)
2342 struct v4l2_clip *clips = NULL;
2343 int n,size,retval = 0;
2345 if (NULL == fh->ovfmt)
2347 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2349 retval = verify_window_lock(fh, win,
2350 /* adjust_size */ fixup,
2351 /* adjust_crop */ fixup);
2355 /* copy clips -- luckily v4l1 + v4l2 are binary
2356 compatible here ...*/
2358 size = sizeof(*clips)*(n+4);
2359 clips = kmalloc(size,GFP_KERNEL);
2363 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2369 mutex_lock(&fh->cap.vb_lock);
2370 /* clip against screen */
2371 if (NULL != btv->fbuf.base)
2372 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2374 btcx_sort_clips(clips,n);
2376 /* 4-byte alignments */
2377 switch (fh->ovfmt->depth) {
2380 btcx_align(&win->w, clips, n, 3);
2383 btcx_align(&win->w, clips, n, 1);
2386 /* no alignment fixups needed */
2392 kfree(fh->ov.clips);
2393 fh->ov.clips = clips;
2397 fh->ov.field = win->field;
2398 fh->ov.setup_ok = 1;
2401 * FIXME: btv is protected by btv->lock mutex, while btv->init
2402 * is protected by fh->cap.vb_lock. This seems to open the
2403 * possibility for some race situations. Maybe the better would
2404 * be to unify those locks or to use another way to store the
2405 * init values that will be consumed by videobuf callbacks
2407 btv->init.ov.w.width = win->w.width;
2408 btv->init.ov.w.height = win->w.height;
2409 btv->init.ov.field = win->field;
2411 /* update overlay if needed */
2413 if (check_btres(fh, RESOURCE_OVERLAY)) {
2414 struct bttv_buffer *new;
2416 new = videobuf_sg_alloc(sizeof(*new));
2417 new->crop = btv->crop[!!fh->do_crop].rect;
2418 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2419 retval = bttv_switch_overlay(btv,fh,new);
2421 mutex_unlock(&fh->cap.vb_lock);
2425 /* ----------------------------------------------------------------------- */
2427 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2429 struct videobuf_queue* q = NULL;
2432 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2435 case V4L2_BUF_TYPE_VBI_CAPTURE:
2444 static int bttv_resource(struct bttv_fh *fh)
2449 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2450 res = RESOURCE_VIDEO_STREAM;
2452 case V4L2_BUF_TYPE_VBI_CAPTURE:
2461 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2463 struct videobuf_queue *q = bttv_queue(fh);
2464 int res = bttv_resource(fh);
2466 if (check_btres(fh,res))
2468 if (videobuf_queue_is_busy(q))
2475 pix_format_set_size (struct v4l2_pix_format * f,
2476 const struct bttv_format * fmt,
2478 unsigned int height)
2483 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2484 f->bytesperline = width; /* Y plane */
2485 f->sizeimage = (width * height * fmt->depth) >> 3;
2487 f->bytesperline = (width * fmt->depth) >> 3;
2488 f->sizeimage = height * f->bytesperline;
2492 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2493 struct v4l2_format *f)
2495 struct bttv_fh *fh = priv;
2497 pix_format_set_size(&f->fmt.pix, fh->fmt,
2498 fh->width, fh->height);
2499 f->fmt.pix.field = fh->cap.field;
2500 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2505 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2506 struct v4l2_format *f)
2508 struct bttv_fh *fh = priv;
2510 f->fmt.win.w = fh->ov.w;
2511 f->fmt.win.field = fh->ov.field;
2516 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2517 struct v4l2_format *f)
2519 const struct bttv_format *fmt;
2520 struct bttv_fh *fh = priv;
2521 struct bttv *btv = fh->btv;
2522 enum v4l2_field field;
2523 __s32 width, height;
2526 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2530 field = f->fmt.pix.field;
2532 if (V4L2_FIELD_ANY == field) {
2535 mutex_lock(&btv->lock);
2536 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2537 mutex_unlock(&btv->lock);
2538 field = (f->fmt.pix.height > height2)
2539 ? V4L2_FIELD_INTERLACED
2540 : V4L2_FIELD_BOTTOM;
2543 if (V4L2_FIELD_SEQ_BT == field)
2544 field = V4L2_FIELD_SEQ_TB;
2547 case V4L2_FIELD_TOP:
2548 case V4L2_FIELD_BOTTOM:
2549 case V4L2_FIELD_ALTERNATE:
2550 case V4L2_FIELD_INTERLACED:
2552 case V4L2_FIELD_SEQ_TB:
2553 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2560 width = f->fmt.pix.width;
2561 height = f->fmt.pix.height;
2563 rc = limit_scaled_size_lock(fh, &width, &height, field,
2564 /* width_mask: 4 pixels */ ~3,
2565 /* width_bias: nearest */ 2,
2566 /* adjust_size */ 1,
2567 /* adjust_crop */ 0);
2571 /* update data for the application */
2572 f->fmt.pix.field = field;
2573 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2578 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2579 struct v4l2_format *f)
2581 struct bttv_fh *fh = priv;
2583 return verify_window_lock(fh, &f->fmt.win,
2584 /* adjust_size */ 1,
2585 /* adjust_crop */ 0);
2588 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2589 struct v4l2_format *f)
2592 const struct bttv_format *fmt;
2593 struct bttv_fh *fh = priv;
2594 struct bttv *btv = fh->btv;
2595 __s32 width, height;
2596 enum v4l2_field field;
2598 retval = bttv_switch_type(fh, f->type);
2602 retval = bttv_try_fmt_vid_cap(file, priv, f);
2606 width = f->fmt.pix.width;
2607 height = f->fmt.pix.height;
2608 field = f->fmt.pix.field;
2610 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
2611 /* width_mask: 4 pixels */ ~3,
2612 /* width_bias: nearest */ 2,
2613 /* adjust_size */ 1,
2614 /* adjust_crop */ 1);
2618 f->fmt.pix.field = field;
2620 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2622 /* update our state informations */
2623 mutex_lock(&fh->cap.vb_lock);
2625 fh->cap.field = f->fmt.pix.field;
2626 fh->cap.last = V4L2_FIELD_NONE;
2627 fh->width = f->fmt.pix.width;
2628 fh->height = f->fmt.pix.height;
2629 btv->init.fmt = fmt;
2630 btv->init.width = f->fmt.pix.width;
2631 btv->init.height = f->fmt.pix.height;
2632 mutex_unlock(&fh->cap.vb_lock);
2637 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2638 struct v4l2_format *f)
2640 struct bttv_fh *fh = priv;
2641 struct bttv *btv = fh->btv;
2643 if (no_overlay > 0) {
2644 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2648 return setup_window_lock(fh, btv, &f->fmt.win, 1);
2651 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2652 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2656 struct bttv_fh *fh = priv;
2658 mutex_lock(&fh->cap.vb_lock);
2659 retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
2662 mutex_unlock(&fh->cap.vb_lock);
2667 memset(mbuf, 0, sizeof(*mbuf));
2668 mbuf->frames = gbuffers;
2669 mbuf->size = gbuffers * gbufsize;
2671 for (i = 0; i < gbuffers; i++)
2672 mbuf->offsets[i] = i * gbufsize;
2674 mutex_unlock(&fh->cap.vb_lock);
2679 static int bttv_querycap(struct file *file, void *priv,
2680 struct v4l2_capability *cap)
2682 struct bttv_fh *fh = priv;
2683 struct bttv *btv = fh->btv;
2688 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2689 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2690 snprintf(cap->bus_info, sizeof(cap->bus_info),
2691 "PCI:%s", pci_name(btv->c.pci));
2692 cap->version = BTTV_VERSION_CODE;
2694 V4L2_CAP_VIDEO_CAPTURE |
2695 V4L2_CAP_VBI_CAPTURE |
2696 V4L2_CAP_READWRITE |
2698 if (no_overlay <= 0)
2699 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2702 * No need to lock here: those vars are initialized during board
2703 * probe and remains untouched during the rest of the driver lifecycle
2705 if (btv->has_saa6588)
2706 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2707 if (btv->tuner_type != TUNER_ABSENT)
2708 cap->capabilities |= V4L2_CAP_TUNER;
2712 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2716 for (i = 0; i < FORMATS; i++) {
2717 if (formats[i].fourcc != -1)
2719 if ((unsigned int)index == f->index)
2725 f->pixelformat = formats[i].fourcc;
2726 strlcpy(f->description, formats[i].name, sizeof(f->description));
2731 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2732 struct v4l2_fmtdesc *f)
2734 int rc = bttv_enum_fmt_cap_ovr(f);
2742 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
2743 struct v4l2_fmtdesc *f)
2747 if (no_overlay > 0) {
2748 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2752 rc = bttv_enum_fmt_cap_ovr(f);
2757 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
2763 static int bttv_g_fbuf(struct file *file, void *f,
2764 struct v4l2_framebuffer *fb)
2766 struct bttv_fh *fh = f;
2767 struct bttv *btv = fh->btv;
2770 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2772 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2776 static int bttv_overlay(struct file *file, void *f, unsigned int on)
2778 struct bttv_fh *fh = f;
2779 struct bttv *btv = fh->btv;
2780 struct bttv_buffer *new;
2784 mutex_lock(&fh->cap.vb_lock);
2786 if (unlikely(!btv->fbuf.base)) {
2787 mutex_unlock(&fh->cap.vb_lock);
2790 if (unlikely(!fh->ov.setup_ok)) {
2791 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2796 mutex_unlock(&fh->cap.vb_lock);
2799 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
2802 mutex_lock(&fh->cap.vb_lock);
2804 fh->ov.tvnorm = btv->tvnorm;
2805 new = videobuf_sg_alloc(sizeof(*new));
2806 new->crop = btv->crop[!!fh->do_crop].rect;
2807 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2813 retval = bttv_switch_overlay(btv, fh, new);
2814 mutex_unlock(&fh->cap.vb_lock);
2818 static int bttv_s_fbuf(struct file *file, void *f,
2819 struct v4l2_framebuffer *fb)
2821 struct bttv_fh *fh = f;
2822 struct bttv *btv = fh->btv;
2823 const struct bttv_format *fmt;
2826 if (!capable(CAP_SYS_ADMIN) &&
2827 !capable(CAP_SYS_RAWIO))
2831 fmt = format_by_fourcc(fb->fmt.pixelformat);
2834 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2838 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2839 __s32 width = fb->fmt.width;
2840 __s32 height = fb->fmt.height;
2842 retval = limit_scaled_size_lock(fh, &width, &height,
2843 V4L2_FIELD_INTERLACED,
2844 /* width_mask */ ~3,
2846 /* adjust_size */ 0,
2847 /* adjust_crop */ 0);
2853 mutex_lock(&fh->cap.vb_lock);
2854 btv->fbuf.base = fb->base;
2855 btv->fbuf.fmt.width = fb->fmt.width;
2856 btv->fbuf.fmt.height = fb->fmt.height;
2857 if (0 != fb->fmt.bytesperline)
2858 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2860 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2864 btv->init.ovfmt = fmt;
2865 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2868 fh->ov.w.width = fb->fmt.width;
2869 fh->ov.w.height = fb->fmt.height;
2870 btv->init.ov.w.width = fb->fmt.width;
2871 btv->init.ov.w.height = fb->fmt.height;
2872 kfree(fh->ov.clips);
2873 fh->ov.clips = NULL;
2876 if (check_btres(fh, RESOURCE_OVERLAY)) {
2877 struct bttv_buffer *new;
2879 new = videobuf_sg_alloc(sizeof(*new));
2880 new->crop = btv->crop[!!fh->do_crop].rect;
2881 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2882 retval = bttv_switch_overlay(btv, fh, new);
2885 mutex_unlock(&fh->cap.vb_lock);
2889 static int bttv_reqbufs(struct file *file, void *priv,
2890 struct v4l2_requestbuffers *p)
2892 struct bttv_fh *fh = priv;
2893 return videobuf_reqbufs(bttv_queue(fh), p);
2896 static int bttv_querybuf(struct file *file, void *priv,
2897 struct v4l2_buffer *b)
2899 struct bttv_fh *fh = priv;
2900 return videobuf_querybuf(bttv_queue(fh), b);
2903 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2905 struct bttv_fh *fh = priv;
2906 struct bttv *btv = fh->btv;
2907 int res = bttv_resource(fh);
2909 if (!check_alloc_btres_lock(btv, fh, res))
2912 return videobuf_qbuf(bttv_queue(fh), b);
2915 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2917 struct bttv_fh *fh = priv;
2918 return videobuf_dqbuf(bttv_queue(fh), b,
2919 file->f_flags & O_NONBLOCK);
2922 static int bttv_streamon(struct file *file, void *priv,
2923 enum v4l2_buf_type type)
2925 struct bttv_fh *fh = priv;
2926 struct bttv *btv = fh->btv;
2927 int res = bttv_resource(fh);
2929 if (!check_alloc_btres_lock(btv, fh, res))
2931 return videobuf_streamon(bttv_queue(fh));
2935 static int bttv_streamoff(struct file *file, void *priv,
2936 enum v4l2_buf_type type)
2938 struct bttv_fh *fh = priv;
2939 struct bttv *btv = fh->btv;
2941 int res = bttv_resource(fh);
2944 retval = videobuf_streamoff(bttv_queue(fh));
2947 free_btres_lock(btv, fh, res);
2951 static int bttv_queryctrl(struct file *file, void *priv,
2952 struct v4l2_queryctrl *c)
2954 struct bttv_fh *fh = priv;
2955 struct bttv *btv = fh->btv;
2956 const struct v4l2_queryctrl *ctrl;
2958 if ((c->id < V4L2_CID_BASE ||
2959 c->id >= V4L2_CID_LASTP1) &&
2960 (c->id < V4L2_CID_PRIVATE_BASE ||
2961 c->id >= V4L2_CID_PRIVATE_LASTP1))
2964 mutex_lock(&btv->lock);
2965 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2968 ctrl = ctrl_by_id(c->id);
2970 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2972 mutex_unlock(&btv->lock);
2977 static int bttv_g_parm(struct file *file, void *f,
2978 struct v4l2_streamparm *parm)
2980 struct bttv_fh *fh = f;
2981 struct bttv *btv = fh->btv;
2983 mutex_lock(&btv->lock);
2984 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2985 &parm->parm.capture.timeperframe);
2986 mutex_unlock(&btv->lock);
2991 static int bttv_g_tuner(struct file *file, void *priv,
2992 struct v4l2_tuner *t)
2994 struct bttv_fh *fh = priv;
2995 struct bttv *btv = fh->btv;
2997 if (btv->tuner_type == TUNER_ABSENT)
3002 mutex_lock(&btv->lock);
3003 t->rxsubchans = V4L2_TUNER_SUB_MONO;
3004 bttv_call_all(btv, tuner, g_tuner, t);
3005 strcpy(t->name, "Television");
3006 t->capability = V4L2_TUNER_CAP_NORM;
3007 t->type = V4L2_TUNER_ANALOG_TV;
3008 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
3011 if (btv->audio_mode_gpio)
3012 btv->audio_mode_gpio(btv, t, 0);
3014 mutex_unlock(&btv->lock);
3018 static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
3020 struct bttv_fh *fh = f;
3021 struct bttv *btv = fh->btv;
3023 mutex_lock(&btv->lock);
3024 *p = v4l2_prio_max(&btv->prio);
3025 mutex_unlock(&btv->lock);
3030 static int bttv_s_priority(struct file *file, void *f,
3031 enum v4l2_priority prio)
3033 struct bttv_fh *fh = f;
3034 struct bttv *btv = fh->btv;
3037 mutex_lock(&btv->lock);
3038 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
3039 mutex_unlock(&btv->lock);
3044 static int bttv_cropcap(struct file *file, void *priv,
3045 struct v4l2_cropcap *cap)
3047 struct bttv_fh *fh = priv;
3048 struct bttv *btv = fh->btv;
3050 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3051 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3054 mutex_lock(&btv->lock);
3055 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3056 mutex_unlock(&btv->lock);
3061 static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
3063 struct bttv_fh *fh = f;
3064 struct bttv *btv = fh->btv;
3066 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3067 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3070 /* No fh->do_crop = 1; because btv->crop[1] may be
3071 inconsistent with fh->width or fh->height and apps
3072 do not expect a change here. */
3074 mutex_lock(&btv->lock);
3075 crop->c = btv->crop[!!fh->do_crop].rect;
3076 mutex_unlock(&btv->lock);
3081 static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
3083 struct bttv_fh *fh = f;
3084 struct bttv *btv = fh->btv;
3085 const struct v4l2_rect *b;
3093 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3094 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3097 /* Make sure tvnorm, vbi_end and the current cropping
3098 parameters remain consistent until we're done. Note
3099 read() may change vbi_end in check_alloc_btres_lock(). */
3100 mutex_lock(&btv->lock);
3101 retval = v4l2_prio_check(&btv->prio, fh->prio);
3103 mutex_unlock(&btv->lock);
3109 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3110 mutex_unlock(&btv->lock);
3114 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3117 b_right = b_left + b->width;
3118 b_bottom = b->top + b->height;
3120 b_top = max(b->top, btv->vbi_end);
3121 if (b_top + 32 >= b_bottom) {
3122 mutex_unlock(&btv->lock);
3126 /* Min. scaled size 48 x 32. */
3127 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3128 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3130 c.rect.width = clamp(crop->c.width,
3131 48, b_right - c.rect.left);
3133 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3134 /* Top and height must be a multiple of two. */
3135 c.rect.top = (c.rect.top + 1) & ~1;
3137 c.rect.height = clamp(crop->c.height,
3138 32, b_bottom - c.rect.top);
3139 c.rect.height = (c.rect.height + 1) & ~1;
3141 bttv_crop_calc_limits(&c);
3145 mutex_unlock(&btv->lock);
3149 mutex_lock(&fh->cap.vb_lock);
3151 if (fh->width < c.min_scaled_width) {
3152 fh->width = c.min_scaled_width;
3153 btv->init.width = c.min_scaled_width;
3154 } else if (fh->width > c.max_scaled_width) {
3155 fh->width = c.max_scaled_width;
3156 btv->init.width = c.max_scaled_width;
3159 if (fh->height < c.min_scaled_height) {
3160 fh->height = c.min_scaled_height;
3161 btv->init.height = c.min_scaled_height;
3162 } else if (fh->height > c.max_scaled_height) {
3163 fh->height = c.max_scaled_height;
3164 btv->init.height = c.max_scaled_height;
3167 mutex_unlock(&fh->cap.vb_lock);
3172 static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
3174 if (unlikely(a->index))
3177 strcpy(a->name, "audio");
3181 static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
3183 if (unlikely(a->index))
3189 static ssize_t bttv_read(struct file *file, char __user *data,
3190 size_t count, loff_t *ppos)
3192 struct bttv_fh *fh = file->private_data;
3195 if (fh->btv->errors)
3196 bttv_reinit_bt848(fh->btv);
3197 dprintk("bttv%d: read count=%d type=%s\n",
3198 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3201 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3202 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3203 /* VIDEO_READ in use by another fh,
3204 or VIDEO_STREAM by any fh. */
3207 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3208 file->f_flags & O_NONBLOCK);
3209 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
3211 case V4L2_BUF_TYPE_VBI_CAPTURE:
3212 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3214 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3215 file->f_flags & O_NONBLOCK);
3223 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3225 struct bttv_fh *fh = file->private_data;
3226 struct bttv_buffer *buf;
3227 enum v4l2_field field;
3228 unsigned int rc = POLLERR;
3230 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3231 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3233 return videobuf_poll_stream(file, &fh->vbi, wait);
3236 mutex_lock(&fh->cap.vb_lock);
3237 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3238 /* streaming capture */
3239 if (list_empty(&fh->cap.stream))
3241 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3243 /* read() capture */
3244 if (NULL == fh->cap.read_buf) {
3245 /* need to capture a new frame */
3246 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3248 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
3249 if (NULL == fh->cap.read_buf)
3251 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3252 field = videobuf_next_field(&fh->cap);
3253 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3254 kfree (fh->cap.read_buf);
3255 fh->cap.read_buf = NULL;
3258 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3259 fh->cap.read_off = 0;
3261 buf = (struct bttv_buffer*)fh->cap.read_buf;
3264 poll_wait(file, &buf->vb.done, wait);
3265 if (buf->vb.state == VIDEOBUF_DONE ||
3266 buf->vb.state == VIDEOBUF_ERROR)
3267 rc = POLLIN|POLLRDNORM;
3271 mutex_unlock(&fh->cap.vb_lock);
3275 static int bttv_open(struct file *file)
3277 struct video_device *vdev = video_devdata(file);
3278 struct bttv *btv = video_drvdata(file);
3280 enum v4l2_buf_type type = 0;
3282 dprintk(KERN_DEBUG "bttv: open dev=%s\n", video_device_node_name(vdev));
3284 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
3285 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3286 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
3287 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3293 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3294 btv->c.nr,v4l2_type_names[type]);
3296 /* allocate per filehandle data */
3297 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3300 file->private_data = fh;
3303 * btv is protected by btv->lock mutex, while btv->init and other
3304 * streaming vars are protected by fh->cap.vb_lock. We need to take
3305 * care of both locks to avoid troubles. However, vb_lock is used also
3306 * inside videobuf, without calling buf->lock. So, it is a very bad
3307 * idea to hold both locks at the same time.
3308 * Let's first copy btv->init at fh, holding cap.vb_lock, and then work
3309 * with the rest of init, holding btv->lock.
3311 mutex_lock(&fh->cap.vb_lock);
3313 mutex_unlock(&fh->cap.vb_lock);
3316 fh->ov.setup_ok = 0;
3318 mutex_lock(&btv->lock);
3319 v4l2_prio_open(&btv->prio, &fh->prio);
3321 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3322 &btv->c.pci->dev, &btv->s_lock,
3323 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3324 V4L2_FIELD_INTERLACED,
3325 sizeof(struct bttv_buffer),
3327 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3328 &btv->c.pci->dev, &btv->s_lock,
3329 V4L2_BUF_TYPE_VBI_CAPTURE,
3331 sizeof(struct bttv_buffer),
3333 set_tvnorm(btv,btv->tvnorm);
3334 set_input(btv, btv->input, btv->tvnorm);
3338 /* The V4L2 spec requires one global set of cropping parameters
3339 which only change on request. These are stored in btv->crop[1].
3340 However for compatibility with V4L apps and cropping unaware
3341 V4L2 apps we now reset the cropping parameters as seen through
3342 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3343 will use btv->crop[0], the default cropping parameters for the
3344 current video standard, and VIDIOC_S_FMT will not implicitely
3345 change the cropping parameters until VIDIOC_S_CROP has been
3347 fh->do_crop = !reset_crop; /* module parameter */
3349 /* Likewise there should be one global set of VBI capture
3350 parameters, but for compatibility with V4L apps and earlier
3351 driver versions each fh has its own parameters. */
3352 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3354 bttv_field_count(btv);
3355 mutex_unlock(&btv->lock);
3359 static int bttv_release(struct file *file)
3361 struct bttv_fh *fh = file->private_data;
3362 struct bttv *btv = fh->btv;
3364 mutex_lock(&btv->lock);
3365 /* turn off overlay */
3366 if (check_btres(fh, RESOURCE_OVERLAY))
3367 bttv_switch_overlay(btv,fh,NULL);
3369 /* stop video capture */
3370 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3371 videobuf_streamoff(&fh->cap);
3372 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
3374 if (fh->cap.read_buf) {
3375 buffer_release(&fh->cap,fh->cap.read_buf);
3376 kfree(fh->cap.read_buf);
3378 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3379 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
3382 /* stop vbi capture */
3383 if (check_btres(fh, RESOURCE_VBI)) {
3384 videobuf_stop(&fh->vbi);
3385 free_btres_lock(btv,fh,RESOURCE_VBI);
3391 * videobuf uses cap.vb_lock - we should avoid holding btv->lock,
3392 * otherwise we may have dead lock conditions
3394 mutex_unlock(&btv->lock);
3395 videobuf_mmap_free(&fh->cap);
3396 videobuf_mmap_free(&fh->vbi);
3397 mutex_lock(&btv->lock);
3398 v4l2_prio_close(&btv->prio, fh->prio);
3399 file->private_data = NULL;
3403 bttv_field_count(btv);
3407 mutex_unlock(&btv->lock);
3413 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3415 struct bttv_fh *fh = file->private_data;
3417 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3418 fh->btv->c.nr, v4l2_type_names[fh->type],
3419 vma->vm_start, vma->vm_end - vma->vm_start);
3420 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3423 static const struct v4l2_file_operations bttv_fops =
3425 .owner = THIS_MODULE,
3427 .release = bttv_release,
3428 .unlocked_ioctl = video_ioctl2,
3434 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3435 .vidioc_querycap = bttv_querycap,
3436 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3437 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3438 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3439 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3440 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3441 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3442 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3443 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
3444 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3445 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3446 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
3447 .vidioc_g_audio = bttv_g_audio,
3448 .vidioc_s_audio = bttv_s_audio,
3449 .vidioc_cropcap = bttv_cropcap,
3450 .vidioc_reqbufs = bttv_reqbufs,
3451 .vidioc_querybuf = bttv_querybuf,
3452 .vidioc_qbuf = bttv_qbuf,
3453 .vidioc_dqbuf = bttv_dqbuf,
3454 .vidioc_s_std = bttv_s_std,
3455 .vidioc_enum_input = bttv_enum_input,
3456 .vidioc_g_input = bttv_g_input,
3457 .vidioc_s_input = bttv_s_input,
3458 .vidioc_queryctrl = bttv_queryctrl,
3459 .vidioc_g_ctrl = bttv_g_ctrl,
3460 .vidioc_s_ctrl = bttv_s_ctrl,
3461 .vidioc_streamon = bttv_streamon,
3462 .vidioc_streamoff = bttv_streamoff,
3463 .vidioc_g_tuner = bttv_g_tuner,
3464 .vidioc_s_tuner = bttv_s_tuner,
3465 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3466 .vidiocgmbuf = vidiocgmbuf,
3468 .vidioc_g_crop = bttv_g_crop,
3469 .vidioc_s_crop = bttv_s_crop,
3470 .vidioc_g_fbuf = bttv_g_fbuf,
3471 .vidioc_s_fbuf = bttv_s_fbuf,
3472 .vidioc_overlay = bttv_overlay,
3473 .vidioc_g_priority = bttv_g_priority,
3474 .vidioc_s_priority = bttv_s_priority,
3475 .vidioc_g_parm = bttv_g_parm,
3476 .vidioc_g_frequency = bttv_g_frequency,
3477 .vidioc_s_frequency = bttv_s_frequency,
3478 .vidioc_log_status = bttv_log_status,
3479 .vidioc_querystd = bttv_querystd,
3480 #ifdef CONFIG_VIDEO_ADV_DEBUG
3481 .vidioc_g_register = bttv_g_register,
3482 .vidioc_s_register = bttv_s_register,
3486 static struct video_device bttv_video_template = {
3488 .ioctl_ops = &bttv_ioctl_ops,
3489 .tvnorms = BTTV_NORMS,
3490 .current_norm = V4L2_STD_PAL,
3493 /* ----------------------------------------------------------------------- */
3494 /* radio interface */
3496 static int radio_open(struct file *file)
3498 struct video_device *vdev = video_devdata(file);
3499 struct bttv *btv = video_drvdata(file);
3502 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev));
3504 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3506 /* allocate per filehandle data */
3507 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3510 file->private_data = fh;
3511 mutex_lock(&fh->cap.vb_lock);
3513 mutex_unlock(&fh->cap.vb_lock);
3515 mutex_lock(&btv->lock);
3516 v4l2_prio_open(&btv->prio, &fh->prio);
3520 bttv_call_all(btv, tuner, s_radio);
3521 audio_input(btv,TVAUDIO_INPUT_RADIO);
3523 mutex_unlock(&btv->lock);
3527 static int radio_release(struct file *file)
3529 struct bttv_fh *fh = file->private_data;
3530 struct bttv *btv = fh->btv;
3531 struct rds_command cmd;
3533 mutex_lock(&btv->lock);
3534 v4l2_prio_close(&btv->prio, fh->prio);
3535 file->private_data = NULL;
3540 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
3541 mutex_unlock(&btv->lock);
3546 static int radio_querycap(struct file *file, void *priv,
3547 struct v4l2_capability *cap)
3549 struct bttv_fh *fh = priv;
3550 struct bttv *btv = fh->btv;
3552 strcpy(cap->driver, "bttv");
3553 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3554 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3555 cap->version = BTTV_VERSION_CODE;
3556 cap->capabilities = V4L2_CAP_TUNER;
3561 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3563 struct bttv_fh *fh = priv;
3564 struct bttv *btv = fh->btv;
3566 if (btv->tuner_type == TUNER_ABSENT)
3570 mutex_lock(&btv->lock);
3571 strcpy(t->name, "Radio");
3572 t->type = V4L2_TUNER_RADIO;
3574 bttv_call_all(btv, tuner, g_tuner, t);
3576 if (btv->audio_mode_gpio)
3577 btv->audio_mode_gpio(btv, t, 0);
3579 mutex_unlock(&btv->lock);
3584 static int radio_enum_input(struct file *file, void *priv,
3585 struct v4l2_input *i)
3590 strcpy(i->name, "Radio");
3591 i->type = V4L2_INPUT_TYPE_TUNER;
3596 static int radio_g_audio(struct file *file, void *priv,
3597 struct v4l2_audio *a)
3599 if (unlikely(a->index))
3602 strcpy(a->name, "Radio");
3607 static int radio_s_tuner(struct file *file, void *priv,
3608 struct v4l2_tuner *t)
3610 struct bttv_fh *fh = priv;
3611 struct bttv *btv = fh->btv;
3616 bttv_call_all(btv, tuner, g_tuner, t);
3620 static int radio_s_audio(struct file *file, void *priv,
3621 struct v4l2_audio *a)
3623 if (unlikely(a->index))
3629 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3637 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3642 static int radio_queryctrl(struct file *file, void *priv,
3643 struct v4l2_queryctrl *c)
3645 const struct v4l2_queryctrl *ctrl;
3647 if (c->id < V4L2_CID_BASE ||
3648 c->id >= V4L2_CID_LASTP1)
3651 if (c->id == V4L2_CID_AUDIO_MUTE) {
3652 ctrl = ctrl_by_id(c->id);
3660 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3666 static ssize_t radio_read(struct file *file, char __user *data,
3667 size_t count, loff_t *ppos)
3669 struct bttv_fh *fh = file->private_data;
3670 struct bttv *btv = fh->btv;
3671 struct rds_command cmd;
3672 cmd.block_count = count/3;
3674 cmd.instance = file;
3675 cmd.result = -ENODEV;
3677 bttv_call_all(btv, core, ioctl, RDS_CMD_READ, &cmd);
3682 static unsigned int radio_poll(struct file *file, poll_table *wait)
3684 struct bttv_fh *fh = file->private_data;
3685 struct bttv *btv = fh->btv;
3686 struct rds_command cmd;
3687 cmd.instance = file;
3688 cmd.event_list = wait;
3689 cmd.result = -ENODEV;
3690 bttv_call_all(btv, core, ioctl, RDS_CMD_POLL, &cmd);
3695 static const struct v4l2_file_operations radio_fops =
3697 .owner = THIS_MODULE,
3700 .release = radio_release,
3701 .ioctl = video_ioctl2,
3705 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3706 .vidioc_querycap = radio_querycap,
3707 .vidioc_g_tuner = radio_g_tuner,
3708 .vidioc_enum_input = radio_enum_input,
3709 .vidioc_g_audio = radio_g_audio,
3710 .vidioc_s_tuner = radio_s_tuner,
3711 .vidioc_s_audio = radio_s_audio,
3712 .vidioc_s_input = radio_s_input,
3713 .vidioc_s_std = radio_s_std,
3714 .vidioc_queryctrl = radio_queryctrl,
3715 .vidioc_g_input = radio_g_input,
3716 .vidioc_g_ctrl = bttv_g_ctrl,
3717 .vidioc_s_ctrl = bttv_s_ctrl,
3718 .vidioc_g_frequency = bttv_g_frequency,
3719 .vidioc_s_frequency = bttv_s_frequency,
3722 static struct video_device radio_template = {
3723 .fops = &radio_fops,
3724 .ioctl_ops = &radio_ioctl_ops,
3727 /* ----------------------------------------------------------------------- */
3728 /* some debug code */
3730 static int bttv_risc_decode(u32 risc)
3732 static char *instr[16] = {
3733 [ BT848_RISC_WRITE >> 28 ] = "write",
3734 [ BT848_RISC_SKIP >> 28 ] = "skip",
3735 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3736 [ BT848_RISC_JUMP >> 28 ] = "jump",
3737 [ BT848_RISC_SYNC >> 28 ] = "sync",
3738 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3739 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3740 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3742 static int incr[16] = {
3743 [ BT848_RISC_WRITE >> 28 ] = 2,
3744 [ BT848_RISC_JUMP >> 28 ] = 2,
3745 [ BT848_RISC_SYNC >> 28 ] = 2,
3746 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3747 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3748 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3750 static char *bits[] = {
3751 "be0", "be1", "be2", "be3/resync",
3752 "set0", "set1", "set2", "set3",
3753 "clr0", "clr1", "clr2", "clr3",
3754 "irq", "res", "eol", "sol",
3758 printk("0x%08x [ %s", risc,
3759 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3760 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3761 if (risc & (1 << (i + 12)))
3762 printk(" %s",bits[i]);
3763 printk(" count=%d ]\n", risc & 0xfff);
3764 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3767 static void bttv_risc_disasm(struct bttv *btv,
3768 struct btcx_riscmem *risc)
3772 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3773 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
3774 for (i = 0; i < (risc->size >> 2); i += n) {
3775 printk("%s: 0x%lx: ", btv->c.v4l2_dev.name,
3776 (unsigned long)(risc->dma + (i<<2)));
3777 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
3778 for (j = 1; j < n; j++)
3779 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3780 btv->c.v4l2_dev.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3782 if (0 == risc->cpu[i])
3787 static void bttv_print_riscaddr(struct bttv *btv)
3789 printk(" main: %08Lx\n",
3790 (unsigned long long)btv->main.dma);
3791 printk(" vbi : o=%08Lx e=%08Lx\n",
3792 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3793 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3794 printk(" cap : o=%08Lx e=%08Lx\n",
3795 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3796 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3797 printk(" scr : o=%08Lx e=%08Lx\n",
3798 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3799 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3800 bttv_risc_disasm(btv, &btv->main);
3803 /* ----------------------------------------------------------------------- */
3806 static char *irq_name[] = {
3807 "FMTCHG", // format change detected (525 vs. 625)
3808 "VSYNC", // vertical sync (new field)
3809 "HSYNC", // horizontal sync
3810 "OFLOW", // chroma/luma AGC overflow
3811 "HLOCK", // horizontal lock changed
3812 "VPRES", // video presence changed
3814 "I2CDONE", // hw irc operation finished
3815 "GPINT", // gpio port triggered irq
3817 "RISCI", // risc instruction triggered irq
3818 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3819 "FTRGT", // pixel data fifo overrun
3820 "FDSR", // fifo data stream resyncronisation
3821 "PPERR", // parity error (data transfer)
3822 "RIPERR", // parity error (read risc instructions)
3823 "PABORT", // pci abort
3824 "OCERR", // risc instruction error
3825 "SCERR", // syncronisation error
3828 static void bttv_print_irqbits(u32 print, u32 mark)
3833 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3834 if (print & (1 << i))
3835 printk(" %s",irq_name[i]);
3836 if (mark & (1 << i))
3841 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3843 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3845 (unsigned long)btv->main.dma,
3846 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3847 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3850 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3851 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3852 "Ok, then this is harmless, don't worry ;)\n",
3856 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3858 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3864 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3866 struct bttv_buffer *item;
3868 memset(set,0,sizeof(*set));
3870 /* capture request ? */
3871 if (!list_empty(&btv->capture)) {
3873 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3874 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3876 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3879 /* capture request for other field ? */
3880 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3881 (item->vb.queue.next != &btv->capture)) {
3882 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3883 /* Mike Isely <isely@pobox.com> - Only check
3884 * and set up the bottom field in the logic
3885 * below. Don't ever do the top field. This
3886 * of course means that if we set up the
3887 * bottom field in the above code that we'll
3888 * actually skip a field. But that's OK.
3889 * Having processed only a single buffer this
3890 * time, then the next time around the first
3891 * available buffer should be for a top field.
3892 * That will then cause us here to set up a
3893 * top then a bottom field in the normal way.
3894 * The alternative to this understanding is
3895 * that we set up the second available buffer
3896 * as a top field, but that's out of order
3897 * since this driver always processes the top
3898 * field first - the effect will be the two
3899 * buffers being returned in the wrong order,
3900 * with the second buffer also being delayed
3901 * by one field time (owing to the fifo nature
3902 * of videobuf). Worse still, we'll be stuck
3903 * doing fields out of order now every time
3904 * until something else causes a field to be
3905 * dropped. By effectively forcing a field to
3906 * drop this way then we always get back into
3907 * sync within a single frame time. (Out of
3908 * order fields can screw up deinterlacing
3910 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3911 if (NULL == set->bottom &&
3912 V4L2_FIELD_BOTTOM == item->vb.field) {
3915 if (NULL != set->top && NULL != set->bottom)
3921 /* screen overlay ? */
3922 if (NULL != btv->screen) {
3923 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3924 if (NULL == set->top && NULL == set->bottom) {
3925 set->top = btv->screen;
3926 set->bottom = btv->screen;
3929 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3931 set->top = btv->screen;
3933 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3934 NULL == set->bottom) {
3935 set->bottom = btv->screen;
3940 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3941 btv->c.nr,set->top, set->bottom,
3942 btv->screen,set->frame_irq,set->top_irq);
3947 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3948 struct bttv_buffer_set *curr, unsigned int state)
3952 do_gettimeofday(&ts);
3954 if (wakeup->top == wakeup->bottom) {
3955 if (NULL != wakeup->top && curr->top != wakeup->top) {
3957 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3958 wakeup->top->vb.ts = ts;
3959 wakeup->top->vb.field_count = btv->field_count;
3960 wakeup->top->vb.state = state;
3961 wake_up(&wakeup->top->vb.done);
3964 if (NULL != wakeup->top && curr->top != wakeup->top) {
3966 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3967 wakeup->top->vb.ts = ts;
3968 wakeup->top->vb.field_count = btv->field_count;
3969 wakeup->top->vb.state = state;
3970 wake_up(&wakeup->top->vb.done);
3972 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3974 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3975 wakeup->bottom->vb.ts = ts;
3976 wakeup->bottom->vb.field_count = btv->field_count;
3977 wakeup->bottom->vb.state = state;
3978 wake_up(&wakeup->bottom->vb.done);
3984 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3992 do_gettimeofday(&ts);
3994 wakeup->vb.field_count = btv->field_count;
3995 wakeup->vb.state = state;
3996 wake_up(&wakeup->vb.done);
3999 static void bttv_irq_timeout(unsigned long data)
4001 struct bttv *btv = (struct bttv *)data;
4002 struct bttv_buffer_set old,new;
4003 struct bttv_buffer *ovbi;
4004 struct bttv_buffer *item;
4005 unsigned long flags;
4008 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
4009 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
4010 btread(BT848_RISC_COUNT));
4011 bttv_print_irqbits(btread(BT848_INT_STAT),0);
4015 spin_lock_irqsave(&btv->s_lock,flags);
4017 /* deactivate stuff */
4018 memset(&new,0,sizeof(new));
4024 bttv_buffer_activate_video(btv, &new);
4025 bttv_buffer_activate_vbi(btv, NULL);
4026 bttv_set_dma(btv, 0);
4029 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
4030 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
4032 /* cancel all outstanding capture / vbi requests */
4033 while (!list_empty(&btv->capture)) {
4034 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4035 list_del(&item->vb.queue);
4036 item->vb.state = VIDEOBUF_ERROR;
4037 wake_up(&item->vb.done);
4039 while (!list_empty(&btv->vcapture)) {
4040 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4041 list_del(&item->vb.queue);
4042 item->vb.state = VIDEOBUF_ERROR;
4043 wake_up(&item->vb.done);
4047 spin_unlock_irqrestore(&btv->s_lock,flags);
4051 bttv_irq_wakeup_top(struct bttv *btv)
4053 struct bttv_buffer *wakeup = btv->curr.top;
4058 spin_lock(&btv->s_lock);
4059 btv->curr.top_irq = 0;
4060 btv->curr.top = NULL;
4061 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
4063 do_gettimeofday(&wakeup->vb.ts);
4064 wakeup->vb.field_count = btv->field_count;
4065 wakeup->vb.state = VIDEOBUF_DONE;
4066 wake_up(&wakeup->vb.done);
4067 spin_unlock(&btv->s_lock);
4070 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
4074 if (rc > risc->dma + risc->size)
4080 bttv_irq_switch_video(struct bttv *btv)
4082 struct bttv_buffer_set new;
4083 struct bttv_buffer_set old;
4086 spin_lock(&btv->s_lock);
4088 /* new buffer set */
4089 bttv_irq_next_video(btv, &new);
4090 rc = btread(BT848_RISC_COUNT);
4091 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4092 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4095 bttv_irq_debug_low_latency(btv, rc);
4096 spin_unlock(&btv->s_lock);
4103 btv->loop_irq &= ~1;
4104 bttv_buffer_activate_video(btv, &new);
4105 bttv_set_dma(btv, 0);
4108 if (UNSET != btv->new_input) {
4109 video_mux(btv,btv->new_input);
4110 btv->new_input = UNSET;
4113 /* wake up finished buffers */
4114 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
4115 spin_unlock(&btv->s_lock);
4119 bttv_irq_switch_vbi(struct bttv *btv)
4121 struct bttv_buffer *new = NULL;
4122 struct bttv_buffer *old;
4125 spin_lock(&btv->s_lock);
4127 if (!list_empty(&btv->vcapture))
4128 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4131 rc = btread(BT848_RISC_COUNT);
4132 if (NULL != old && (is_active(&old->top, rc) ||
4133 is_active(&old->bottom, rc))) {
4136 bttv_irq_debug_low_latency(btv, rc);
4137 spin_unlock(&btv->s_lock);
4143 btv->loop_irq &= ~4;
4144 bttv_buffer_activate_vbi(btv, new);
4145 bttv_set_dma(btv, 0);
4147 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
4148 spin_unlock(&btv->s_lock);
4151 static irqreturn_t bttv_irq(int irq, void *dev_id)
4159 btv=(struct bttv *)dev_id;
4161 if (btv->custom_irq)
4162 handled = btv->custom_irq(btv);
4166 /* get/clear interrupt status bits */
4167 stat=btread(BT848_INT_STAT);
4168 astat=stat&btread(BT848_INT_MASK);
4172 btwrite(stat,BT848_INT_STAT);
4174 /* get device status bits */
4175 dstat=btread(BT848_DSTATUS);
4178 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4179 "riscs=%x, riscc=%08x, ",
4180 btv->c.nr, count, btv->field_count,
4181 stat>>28, btread(BT848_RISC_COUNT));
4182 bttv_print_irqbits(stat,astat);
4183 if (stat & BT848_INT_HLOCK)
4184 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4186 if (stat & BT848_INT_VPRES)
4187 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4189 if (stat & BT848_INT_FMTCHG)
4190 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4195 if (astat&BT848_INT_VSYNC)
4198 if ((astat & BT848_INT_GPINT) && btv->remote) {
4199 wake_up(&btv->gpioq);
4200 bttv_input_irq(btv);
4203 if (astat & BT848_INT_I2CDONE) {
4204 btv->i2c_done = stat;
4205 wake_up(&btv->i2c_queue);
4208 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
4209 bttv_irq_switch_vbi(btv);
4211 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
4212 bttv_irq_wakeup_top(btv);
4214 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
4215 bttv_irq_switch_video(btv);
4217 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
4218 audio_mute(btv, btv->mute); /* trigger automute */
4220 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4221 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4222 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4223 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4224 btread(BT848_RISC_COUNT));
4225 bttv_print_irqbits(stat,astat);
4228 bttv_print_riscaddr(btv);
4230 if (fdsr && astat & BT848_INT_FDSR) {
4231 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4232 btv->c.nr,btread(BT848_RISC_COUNT));
4234 bttv_print_riscaddr(btv);
4240 if (count > 8 || !(astat & BT848_INT_GPINT)) {
4241 btwrite(0, BT848_INT_MASK);
4244 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4247 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4249 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4253 bttv_print_irqbits(stat,astat);
4261 return IRQ_RETVAL(handled);
4265 /* ----------------------------------------------------------------------- */
4266 /* initialitation */
4268 static struct video_device *vdev_init(struct bttv *btv,
4269 const struct video_device *template,
4270 const char *type_name)
4272 struct video_device *vfd;
4274 vfd = video_device_alloc();
4278 vfd->v4l2_dev = &btv->c.v4l2_dev;
4279 vfd->release = video_device_release;
4280 vfd->debug = bttv_debug;
4281 video_set_drvdata(vfd, btv);
4282 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4283 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4284 type_name, bttv_tvcards[btv->c.type].name);
4288 static void bttv_unregister_video(struct bttv *btv)
4290 if (btv->video_dev) {
4291 if (video_is_registered(btv->video_dev))
4292 video_unregister_device(btv->video_dev);
4294 video_device_release(btv->video_dev);
4295 btv->video_dev = NULL;
4298 if (video_is_registered(btv->vbi_dev))
4299 video_unregister_device(btv->vbi_dev);
4301 video_device_release(btv->vbi_dev);
4302 btv->vbi_dev = NULL;
4304 if (btv->radio_dev) {
4305 if (video_is_registered(btv->radio_dev))
4306 video_unregister_device(btv->radio_dev);
4308 video_device_release(btv->radio_dev);
4309 btv->radio_dev = NULL;
4313 /* register video4linux devices */
4314 static int __devinit bttv_register_video(struct bttv *btv)
4317 printk("bttv: Overlay support disabled.\n");
4320 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4322 if (NULL == btv->video_dev)
4324 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4325 video_nr[btv->c.nr]) < 0)
4327 printk(KERN_INFO "bttv%d: registered device %s\n",
4328 btv->c.nr, video_device_node_name(btv->video_dev));
4329 if (device_create_file(&btv->video_dev->dev,
4330 &dev_attr_card)<0) {
4331 printk(KERN_ERR "bttv%d: device_create_file 'card' "
4332 "failed\n", btv->c.nr);
4337 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
4339 if (NULL == btv->vbi_dev)
4341 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4342 vbi_nr[btv->c.nr]) < 0)
4344 printk(KERN_INFO "bttv%d: registered device %s\n",
4345 btv->c.nr, video_device_node_name(btv->vbi_dev));
4347 if (!btv->has_radio)
4350 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4351 if (NULL == btv->radio_dev)
4353 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4354 radio_nr[btv->c.nr]) < 0)
4356 printk(KERN_INFO "bttv%d: registered device %s\n",
4357 btv->c.nr, video_device_node_name(btv->radio_dev));
4363 bttv_unregister_video(btv);
4368 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4369 /* response on cards with no firmware is not enabled by OF */
4370 static void pci_set_command(struct pci_dev *dev)
4372 #if defined(__powerpc__)
4375 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4376 cmd = (cmd | PCI_COMMAND_MEMORY );
4377 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4381 static int __devinit bttv_probe(struct pci_dev *dev,
4382 const struct pci_device_id *pci_id)
4388 if (bttv_num == BTTV_MAX)
4390 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
4391 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
4393 printk(KERN_ERR "bttv: out of memory.\n");
4396 btv->c.nr = bttv_num;
4397 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4398 "bttv%d", btv->c.nr);
4400 /* initialize structs / fill in defaults */
4401 mutex_init(&btv->lock);
4402 spin_lock_init(&btv->s_lock);
4403 spin_lock_init(&btv->gpio_lock);
4404 init_waitqueue_head(&btv->gpioq);
4405 init_waitqueue_head(&btv->i2c_queue);
4406 INIT_LIST_HEAD(&btv->c.subs);
4407 INIT_LIST_HEAD(&btv->capture);
4408 INIT_LIST_HEAD(&btv->vcapture);
4409 v4l2_prio_init(&btv->prio);
4411 init_timer(&btv->timeout);
4412 btv->timeout.function = bttv_irq_timeout;
4413 btv->timeout.data = (unsigned long)btv;
4416 btv->tuner_type = UNSET;
4417 btv->new_input = UNSET;
4418 btv->has_radio=radio[btv->c.nr];
4420 /* pci stuff (init, get irq/mmio, ... */
4422 btv->id = dev->device;
4423 if (pci_enable_device(dev)) {
4424 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4428 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
4429 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4433 if (!request_mem_region(pci_resource_start(dev,0),
4434 pci_resource_len(dev,0),
4435 btv->c.v4l2_dev.name)) {
4436 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4438 (unsigned long long)pci_resource_start(dev,0));
4441 pci_set_master(dev);
4442 pci_set_command(dev);
4444 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4446 printk(KERN_WARNING "bttv%d: v4l2_device_register() failed\n", btv->c.nr);
4450 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4451 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4452 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4453 bttv_num,btv->id, btv->revision, pci_name(dev));
4454 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4455 btv->c.pci->irq, lat,
4456 (unsigned long long)pci_resource_start(dev,0));
4459 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4460 if (NULL == btv->bt848_mmio) {
4461 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4469 /* disable irqs, register irq handler */
4470 btwrite(0, BT848_INT_MASK);
4471 result = request_irq(btv->c.pci->irq, bttv_irq,
4472 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
4474 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4475 bttv_num,btv->c.pci->irq);
4479 if (0 != bttv_handle_chipset(btv)) {
4484 /* init options from insmod args */
4485 btv->opt_combfilter = combfilter;
4486 btv->opt_lumafilter = lumafilter;
4487 btv->opt_automute = automute;
4488 btv->opt_chroma_agc = chroma_agc;
4489 btv->opt_adc_crush = adc_crush;
4490 btv->opt_vcr_hack = vcr_hack;
4491 btv->opt_whitecrush_upper = whitecrush_upper;
4492 btv->opt_whitecrush_lower = whitecrush_lower;
4493 btv->opt_uv_ratio = uv_ratio;
4494 btv->opt_full_luma_range = full_luma_range;
4495 btv->opt_coring = coring;
4497 /* fill struct bttv with some useful defaults */
4498 btv->init.btv = btv;
4499 btv->init.ov.w.width = 320;
4500 btv->init.ov.w.height = 240;
4501 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4502 btv->init.width = 320;
4503 btv->init.height = 240;
4506 /* initialize hardware */
4508 bttv_gpio_tracking(btv,"pre-init");
4510 bttv_risc_init_main(btv);
4514 btwrite(0x00, BT848_GPIO_REG_INP);
4515 btwrite(0x00, BT848_GPIO_OUT_EN);
4517 bttv_gpio_tracking(btv,"init");
4519 /* needs to be done before i2c is registered */
4520 bttv_init_card1(btv);
4522 /* register i2c + gpio */
4525 /* some card-specific stuff (needs working i2c) */
4526 bttv_init_card2(btv);
4527 bttv_init_tuner(btv);
4530 /* register video4linux + input */
4531 if (!bttv_tvcards[btv->c.type].no_video) {
4532 bttv_register_video(btv);
4533 bt848_bright(btv,32768);
4534 bt848_contrast(btv,32768);
4535 bt848_hue(btv,32768);
4536 bt848_sat(btv,32768);
4538 set_input(btv, 0, btv->tvnorm);
4539 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4540 btv->crop[1] = btv->crop[0]; /* current = default */
4541 disclaim_vbi_lines(btv);
4542 disclaim_video_lines(btv);
4545 /* add subdevices and autoload dvb-bt8xx if needed */
4546 if (bttv_tvcards[btv->c.type].has_dvb) {
4547 bttv_sub_add_device(&btv->c, "dvb");
4548 request_modules(btv);
4552 init_bttv_i2c_ir(btv);
4553 bttv_input_init(btv);
4556 /* everything is fine */
4561 free_irq(btv->c.pci->irq,btv);
4564 v4l2_device_unregister(&btv->c.v4l2_dev);
4567 if (btv->bt848_mmio)
4568 iounmap(btv->bt848_mmio);
4569 release_mem_region(pci_resource_start(btv->c.pci,0),
4570 pci_resource_len(btv->c.pci,0));
4574 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4576 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4577 struct bttv *btv = to_bttv(v4l2_dev);
4580 printk("bttv%d: unloading\n",btv->c.nr);
4582 if (bttv_tvcards[btv->c.type].has_dvb)
4583 flush_request_modules(btv);
4585 /* shutdown everything (DMA+IRQs) */
4586 btand(~15, BT848_GPIO_DMA_CTL);
4587 btwrite(0, BT848_INT_MASK);
4588 btwrite(~0x0, BT848_INT_STAT);
4589 btwrite(0x0, BT848_GPIO_OUT_EN);
4591 bttv_gpio_tracking(btv,"cleanup");
4593 /* tell gpio modules we are leaving ... */
4595 wake_up(&btv->gpioq);
4596 bttv_input_fini(btv);
4597 bttv_sub_del_devices(&btv->c);
4599 /* unregister i2c_bus + input */
4602 /* unregister video4linux */
4603 bttv_unregister_video(btv);
4605 /* free allocated memory */
4606 btcx_riscmem_free(btv->c.pci,&btv->main);
4608 /* free ressources */
4609 free_irq(btv->c.pci->irq,btv);
4610 iounmap(btv->bt848_mmio);
4611 release_mem_region(pci_resource_start(btv->c.pci,0),
4612 pci_resource_len(btv->c.pci,0));
4614 v4l2_device_unregister(&btv->c.v4l2_dev);
4615 bttvs[btv->c.nr] = NULL;
4622 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4624 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4625 struct bttv *btv = to_bttv(v4l2_dev);
4626 struct bttv_buffer_set idle;
4627 unsigned long flags;
4629 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4631 /* stop dma + irqs */
4632 spin_lock_irqsave(&btv->s_lock,flags);
4633 memset(&idle, 0, sizeof(idle));
4634 btv->state.video = btv->curr;
4635 btv->state.vbi = btv->cvbi;
4636 btv->state.loop_irq = btv->loop_irq;
4639 bttv_buffer_activate_video(btv, &idle);
4640 bttv_buffer_activate_vbi(btv, NULL);
4641 bttv_set_dma(btv, 0);
4642 btwrite(0, BT848_INT_MASK);
4643 spin_unlock_irqrestore(&btv->s_lock,flags);
4645 /* save bt878 state */
4646 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4647 btv->state.gpio_data = gpio_read();
4649 /* save pci state */
4650 pci_save_state(pci_dev);
4651 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4652 pci_disable_device(pci_dev);
4653 btv->state.disabled = 1;
4658 static int bttv_resume(struct pci_dev *pci_dev)
4660 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4661 struct bttv *btv = to_bttv(v4l2_dev);
4662 unsigned long flags;
4665 dprintk("bttv%d: resume\n", btv->c.nr);
4667 /* restore pci state */
4668 if (btv->state.disabled) {
4669 err=pci_enable_device(pci_dev);
4671 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4675 btv->state.disabled = 0;
4677 err=pci_set_power_state(pci_dev, PCI_D0);
4679 pci_disable_device(pci_dev);
4680 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4682 btv->state.disabled = 1;
4686 pci_restore_state(pci_dev);
4688 /* restore bt878 state */
4689 bttv_reinit_bt848(btv);
4690 gpio_inout(0xffffff, btv->state.gpio_enable);
4691 gpio_write(btv->state.gpio_data);
4694 spin_lock_irqsave(&btv->s_lock,flags);
4695 btv->curr = btv->state.video;
4696 btv->cvbi = btv->state.vbi;
4697 btv->loop_irq = btv->state.loop_irq;
4698 bttv_buffer_activate_video(btv, &btv->curr);
4699 bttv_buffer_activate_vbi(btv, btv->cvbi);
4700 bttv_set_dma(btv, 0);
4701 spin_unlock_irqrestore(&btv->s_lock,flags);
4706 static struct pci_device_id bttv_pci_tbl[] = {
4707 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4708 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4709 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4710 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
4714 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4716 static struct pci_driver bttv_pci_driver = {
4718 .id_table = bttv_pci_tbl,
4719 .probe = bttv_probe,
4720 .remove = __devexit_p(bttv_remove),
4722 .suspend = bttv_suspend,
4723 .resume = bttv_resume,
4727 static int __init bttv_init_module(void)
4733 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4734 (BTTV_VERSION_CODE >> 16) & 0xff,
4735 (BTTV_VERSION_CODE >> 8) & 0xff,
4736 BTTV_VERSION_CODE & 0xff);
4738 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4739 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4741 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4743 if (gbufsize > BTTV_MAX_FBUF)
4744 gbufsize = BTTV_MAX_FBUF;
4745 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4747 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4748 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4750 bttv_check_chipset();
4752 ret = bus_register(&bttv_sub_bus_type);
4754 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4757 ret = pci_register_driver(&bttv_pci_driver);
4759 bus_unregister(&bttv_sub_bus_type);
4764 static void __exit bttv_cleanup_module(void)
4766 pci_unregister_driver(&bttv_pci_driver);
4767 bus_unregister(&bttv_sub_bus_type);
4770 module_init(bttv_init_module);
4771 module_exit(bttv_cleanup_module);