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 #define request_modules(dev)
194 #endif /* CONFIG_MODULES */
197 /* ----------------------------------------------------------------------- */
200 /* special timing tables from conexant... */
201 static u8 SRAM_Table[][60] =
203 /* PAL digital input over GPIO[7:0] */
205 45, // 45 bytes following
206 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
207 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
208 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
209 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
210 0x37,0x00,0xAF,0x21,0x00
212 /* NTSC digital input over GPIO[7:0] */
214 51, // 51 bytes following
215 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
216 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
217 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
218 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
219 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
222 // TGB_NTSC392 // quartzsight
223 // This table has been modified to be used for Fusion Rev D
225 0x2A, // size of table = 42
226 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
227 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
228 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
229 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
234 /* minhdelayx1 first video pixel we can capture on a line and
235 hdelayx1 start of active video, both relative to rising edge of
236 /HRESET pulse (0H) in 1 / fCLKx1.
237 swidth width of active video and
238 totalwidth total line width, both in 1 / fCLKx1.
239 sqwidth total line width in square pixels.
240 vdelay start of active video in 2 * field lines relative to
241 trailing edge of /VRESET pulse (VDELAY register).
242 sheight height of active video in 2 * field lines.
243 videostart0 ITU-R frame line number of the line corresponding
244 to vdelay in the first field. */
245 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
246 vdelay, sheight, videostart0) \
247 .cropcap.bounds.left = minhdelayx1, \
248 /* * 2 because vertically we count field lines times two, */ \
249 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
250 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
251 /* 4 is a safety margin at the end of the line. */ \
252 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
253 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
254 .cropcap.defrect.left = hdelayx1, \
255 .cropcap.defrect.top = (videostart0) * 2, \
256 .cropcap.defrect.width = swidth, \
257 .cropcap.defrect.height = sheight, \
258 .cropcap.pixelaspect.numerator = totalwidth, \
259 .cropcap.pixelaspect.denominator = sqwidth,
261 const struct bttv_tvnorm bttv_tvnorms[] = {
263 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
264 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
266 .v4l2_id = V4L2_STD_PAL,
274 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
275 .scaledtwidth = 1135,
279 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
281 /* ITU-R frame line number of the first VBI line
282 we can capture, of the first and second field.
283 The last line is determined by cropcap.bounds. */
284 .vbistart = { 7, 320 },
285 CROPCAP(/* minhdelayx1 */ 68,
287 /* Should be (768 * 1135 + 944 / 2) / 944.
288 cropcap.defrect is used for image width
289 checks, so we keep the old value 924. */
291 /* totalwidth */ 1135,
295 /* videostart0 */ 23)
296 /* bt878 (and bt848?) can capture another
297 line below active video. */
298 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
300 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
308 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
313 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
315 .vbistart = { 10, 273 },
316 CROPCAP(/* minhdelayx1 */ 68,
318 /* Should be (640 * 910 + 780 / 2) / 780? */
320 /* totalwidth */ 910,
324 /* videostart0 */ 23)
326 .v4l2_id = V4L2_STD_SECAM,
334 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
335 .scaledtwidth = 1135,
340 .sram = 0, /* like PAL, correct? */
341 .vbistart = { 7, 320 },
342 CROPCAP(/* minhdelayx1 */ 68,
345 /* totalwidth */ 1135,
349 /* videostart0 */ 23)
351 .v4l2_id = V4L2_STD_PAL_Nc,
359 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
366 .vbistart = { 7, 320 },
367 CROPCAP(/* minhdelayx1 */ 68,
369 /* swidth */ (640 * 910 + 780 / 2) / 780,
370 /* totalwidth */ 910,
374 /* videostart0 */ 23)
376 .v4l2_id = V4L2_STD_PAL_M,
384 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
391 .vbistart = { 10, 273 },
392 CROPCAP(/* minhdelayx1 */ 68,
394 /* swidth */ (640 * 910 + 780 / 2) / 780,
395 /* totalwidth */ 910,
399 /* videostart0 */ 23)
401 .v4l2_id = V4L2_STD_PAL_N,
409 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
416 .vbistart = { 7, 320 },
417 CROPCAP(/* minhdelayx1 */ 68,
419 /* swidth */ (768 * 1135 + 944 / 2) / 944,
420 /* totalwidth */ 1135,
424 /* videostart0 */ 23)
426 .v4l2_id = V4L2_STD_NTSC_M_JP,
434 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
441 .vbistart = { 10, 273 },
442 CROPCAP(/* minhdelayx1 */ 68,
444 /* swidth */ (640 * 910 + 780 / 2) / 780,
445 /* totalwidth */ 910,
449 /* videostart0 */ 23)
451 /* that one hopefully works with the strange timing
452 * which video recorders produce when playing a NTSC
453 * tape on a PAL TV ... */
454 .v4l2_id = V4L2_STD_PAL_60,
462 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
463 .scaledtwidth = 1135,
470 .vbistart = { 10, 273 },
471 CROPCAP(/* minhdelayx1 */ 68,
474 /* totalwidth */ 1135,
478 /* videostart0 */ 23)
481 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
483 /* ----------------------------------------------------------------------- */
485 packed pixel formats must come first */
486 static const struct bttv_format formats[] = {
488 .name = "8 bpp, gray",
489 .fourcc = V4L2_PIX_FMT_GREY,
490 .btformat = BT848_COLOR_FMT_Y8,
492 .flags = FORMAT_FLAGS_PACKED,
494 .name = "8 bpp, dithered color",
495 .fourcc = V4L2_PIX_FMT_HI240,
496 .btformat = BT848_COLOR_FMT_RGB8,
498 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
500 .name = "15 bpp RGB, le",
501 .fourcc = V4L2_PIX_FMT_RGB555,
502 .btformat = BT848_COLOR_FMT_RGB15,
504 .flags = FORMAT_FLAGS_PACKED,
506 .name = "15 bpp RGB, be",
507 .fourcc = V4L2_PIX_FMT_RGB555X,
508 .btformat = BT848_COLOR_FMT_RGB15,
509 .btswap = 0x03, /* byteswap */
511 .flags = FORMAT_FLAGS_PACKED,
513 .name = "16 bpp RGB, le",
514 .fourcc = V4L2_PIX_FMT_RGB565,
515 .btformat = BT848_COLOR_FMT_RGB16,
517 .flags = FORMAT_FLAGS_PACKED,
519 .name = "16 bpp RGB, be",
520 .fourcc = V4L2_PIX_FMT_RGB565X,
521 .btformat = BT848_COLOR_FMT_RGB16,
522 .btswap = 0x03, /* byteswap */
524 .flags = FORMAT_FLAGS_PACKED,
526 .name = "24 bpp RGB, le",
527 .fourcc = V4L2_PIX_FMT_BGR24,
528 .btformat = BT848_COLOR_FMT_RGB24,
530 .flags = FORMAT_FLAGS_PACKED,
532 .name = "32 bpp RGB, le",
533 .fourcc = V4L2_PIX_FMT_BGR32,
534 .btformat = BT848_COLOR_FMT_RGB32,
536 .flags = FORMAT_FLAGS_PACKED,
538 .name = "32 bpp RGB, be",
539 .fourcc = V4L2_PIX_FMT_RGB32,
540 .btformat = BT848_COLOR_FMT_RGB32,
541 .btswap = 0x0f, /* byte+word swap */
543 .flags = FORMAT_FLAGS_PACKED,
545 .name = "4:2:2, packed, YUYV",
546 .fourcc = V4L2_PIX_FMT_YUYV,
547 .btformat = BT848_COLOR_FMT_YUY2,
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, UYVY",
558 .fourcc = V4L2_PIX_FMT_UYVY,
559 .btformat = BT848_COLOR_FMT_YUY2,
560 .btswap = 0x03, /* byteswap */
562 .flags = FORMAT_FLAGS_PACKED,
564 .name = "4:2:2, planar, Y-Cb-Cr",
565 .fourcc = V4L2_PIX_FMT_YUV422P,
566 .btformat = BT848_COLOR_FMT_YCrCb422,
568 .flags = FORMAT_FLAGS_PLANAR,
572 .name = "4:2:0, planar, Y-Cb-Cr",
573 .fourcc = V4L2_PIX_FMT_YUV420,
574 .btformat = BT848_COLOR_FMT_YCrCb422,
576 .flags = FORMAT_FLAGS_PLANAR,
580 .name = "4:2:0, planar, Y-Cr-Cb",
581 .fourcc = V4L2_PIX_FMT_YVU420,
582 .btformat = BT848_COLOR_FMT_YCrCb422,
584 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
588 .name = "4:1:1, planar, Y-Cb-Cr",
589 .fourcc = V4L2_PIX_FMT_YUV411P,
590 .btformat = BT848_COLOR_FMT_YCrCb411,
592 .flags = FORMAT_FLAGS_PLANAR,
596 .name = "4:1:0, planar, Y-Cb-Cr",
597 .fourcc = V4L2_PIX_FMT_YUV410,
598 .btformat = BT848_COLOR_FMT_YCrCb411,
600 .flags = FORMAT_FLAGS_PLANAR,
604 .name = "4:1:0, planar, Y-Cr-Cb",
605 .fourcc = V4L2_PIX_FMT_YVU410,
606 .btformat = BT848_COLOR_FMT_YCrCb411,
608 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
612 .name = "raw scanlines",
614 .btformat = BT848_COLOR_FMT_RAW,
616 .flags = FORMAT_FLAGS_RAW,
619 static const unsigned int FORMATS = ARRAY_SIZE(formats);
621 /* ----------------------------------------------------------------------- */
623 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
624 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
625 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
626 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
627 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
628 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
629 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
630 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
631 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
632 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
633 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
634 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
636 static const struct v4l2_queryctrl no_ctl = {
638 .flags = V4L2_CTRL_FLAG_DISABLED,
640 static const struct v4l2_queryctrl bttv_ctls[] = {
643 .id = V4L2_CID_BRIGHTNESS,
644 .name = "Brightness",
648 .default_value = 32768,
649 .type = V4L2_CTRL_TYPE_INTEGER,
651 .id = V4L2_CID_CONTRAST,
656 .default_value = 32768,
657 .type = V4L2_CTRL_TYPE_INTEGER,
659 .id = V4L2_CID_SATURATION,
660 .name = "Saturation",
664 .default_value = 32768,
665 .type = V4L2_CTRL_TYPE_INTEGER,
672 .default_value = 32768,
673 .type = V4L2_CTRL_TYPE_INTEGER,
677 .id = V4L2_CID_AUDIO_MUTE,
681 .type = V4L2_CTRL_TYPE_BOOLEAN,
683 .id = V4L2_CID_AUDIO_VOLUME,
688 .default_value = 65535,
689 .type = V4L2_CTRL_TYPE_INTEGER,
691 .id = V4L2_CID_AUDIO_BALANCE,
696 .default_value = 32768,
697 .type = V4L2_CTRL_TYPE_INTEGER,
699 .id = V4L2_CID_AUDIO_BASS,
704 .default_value = 32768,
705 .type = V4L2_CTRL_TYPE_INTEGER,
707 .id = V4L2_CID_AUDIO_TREBLE,
712 .default_value = 32768,
713 .type = V4L2_CTRL_TYPE_INTEGER,
715 /* --- private --- */
717 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
718 .name = "chroma agc",
721 .type = V4L2_CTRL_TYPE_BOOLEAN,
723 .id = V4L2_CID_PRIVATE_COMBFILTER,
724 .name = "combfilter",
727 .type = V4L2_CTRL_TYPE_BOOLEAN,
729 .id = V4L2_CID_PRIVATE_AUTOMUTE,
733 .type = V4L2_CTRL_TYPE_BOOLEAN,
735 .id = V4L2_CID_PRIVATE_LUMAFILTER,
736 .name = "luma decimation filter",
739 .type = V4L2_CTRL_TYPE_BOOLEAN,
741 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
745 .type = V4L2_CTRL_TYPE_BOOLEAN,
747 .id = V4L2_CID_PRIVATE_VCR_HACK,
751 .type = V4L2_CTRL_TYPE_BOOLEAN,
753 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
754 .name = "whitecrush upper",
758 .default_value = 0xCF,
759 .type = V4L2_CTRL_TYPE_INTEGER,
761 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
762 .name = "whitecrush lower",
766 .default_value = 0x7F,
767 .type = V4L2_CTRL_TYPE_INTEGER,
769 .id = V4L2_CID_PRIVATE_UV_RATIO,
775 .type = V4L2_CTRL_TYPE_INTEGER,
777 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
778 .name = "full luma range",
781 .type = V4L2_CTRL_TYPE_BOOLEAN,
783 .id = V4L2_CID_PRIVATE_CORING,
789 .type = V4L2_CTRL_TYPE_INTEGER,
796 static const struct v4l2_queryctrl *ctrl_by_id(int id)
800 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
801 if (bttv_ctls[i].id == id)
807 /* ----------------------------------------------------------------------- */
808 /* resource management */
811 RESOURCE_ allocated by freed by
813 VIDEO_READ bttv_read 1) bttv_read 2)
815 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
816 VIDIOC_QBUF 1) bttv_release
819 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
820 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
823 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
824 VIDIOC_QBUF 1) bttv_release
825 bttv_read, bttv_poll 1) 4)
827 1) The resource must be allocated when we enter buffer prepare functions
828 and remain allocated while buffers are in the DMA queue.
829 2) This is a single frame read.
830 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
831 RESOURCE_OVERLAY is allocated.
832 4) This is a continuous read, implies VIDIOC_STREAMON.
834 Note this driver permits video input and standard changes regardless if
835 resources are allocated.
838 #define VBI_RESOURCES (RESOURCE_VBI)
839 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
840 RESOURCE_VIDEO_STREAM | \
844 int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
846 int xbits; /* mutual exclusive resources */
848 if (fh->resources & bit)
849 /* have it already allocated */
853 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
854 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
857 mutex_lock(&btv->lock);
858 if (btv->resources & xbits) {
859 /* no, someone else uses it */
863 if ((bit & VIDEO_RESOURCES)
864 && 0 == (btv->resources & VIDEO_RESOURCES)) {
865 /* Do crop - use current, don't - use default parameters. */
866 __s32 top = btv->crop[!!fh->do_crop].rect.top;
868 if (btv->vbi_end > top)
871 /* We cannot capture the same line as video and VBI data.
872 Claim scan lines crop[].rect.top to bottom. */
873 btv->crop_start = top;
874 } else if (bit & VBI_RESOURCES) {
875 __s32 end = fh->vbi_fmt.end;
877 if (end > btv->crop_start)
880 /* Claim scan lines above fh->vbi_fmt.end. */
884 /* it's free, grab it */
885 fh->resources |= bit;
886 btv->resources |= bit;
887 mutex_unlock(&btv->lock);
891 mutex_unlock(&btv->lock);
896 int check_btres(struct bttv_fh *fh, int bit)
898 return (fh->resources & bit);
902 int locked_btres(struct bttv *btv, int bit)
904 return (btv->resources & bit);
907 /* Call with btv->lock down. */
909 disclaim_vbi_lines(struct bttv *btv)
914 /* Call with btv->lock down. */
916 disclaim_video_lines(struct bttv *btv)
918 const struct bttv_tvnorm *tvnorm;
921 tvnorm = &bttv_tvnorms[btv->tvnorm];
922 btv->crop_start = tvnorm->cropcap.bounds.top
923 + tvnorm->cropcap.bounds.height;
925 /* VBI capturing ends at VDELAY, start of video capturing, no
926 matter how many lines the VBI RISC program expects. When video
927 capturing is off, it shall no longer "preempt" VBI capturing,
928 so we set VDELAY to maximum. */
929 crop = btread(BT848_E_CROP) | 0xc0;
930 btwrite(crop, BT848_E_CROP);
931 btwrite(0xfe, BT848_E_VDELAY_LO);
932 btwrite(crop, BT848_O_CROP);
933 btwrite(0xfe, BT848_O_VDELAY_LO);
937 void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
939 if ((fh->resources & bits) != bits) {
940 /* trying to free ressources not allocated by us ... */
941 printk("bttv: BUG! (btres)\n");
943 mutex_lock(&btv->lock);
944 fh->resources &= ~bits;
945 btv->resources &= ~bits;
947 bits = btv->resources;
949 if (0 == (bits & VIDEO_RESOURCES))
950 disclaim_video_lines(btv);
952 if (0 == (bits & VBI_RESOURCES))
953 disclaim_vbi_lines(btv);
955 mutex_unlock(&btv->lock);
958 /* ----------------------------------------------------------------------- */
959 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
961 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
962 PLL_X = Reference pre-divider (0=1, 1=2)
963 PLL_C = Post divider (0=6, 1=4)
964 PLL_I = Integer input
965 PLL_F = Fractional input
967 F_input = 28.636363 MHz:
968 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
971 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
973 unsigned char fl, fh, fi;
975 /* prevent overflows */
988 btwrite(fl, BT848_PLL_F_LO);
989 btwrite(fh, BT848_PLL_F_HI);
990 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
993 static void set_pll(struct bttv *btv)
997 if (!btv->pll.pll_crystal)
1000 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1001 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
1005 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1007 if (btv->pll.pll_current == 0)
1009 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
1010 btv->c.nr,btv->pll.pll_ifreq);
1011 btwrite(0x00,BT848_TGCTRL);
1012 btwrite(0x00,BT848_PLL_XCI);
1013 btv->pll.pll_current = 0;
1017 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
1018 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1019 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1021 for (i=0; i<10; i++) {
1022 /* Let other people run while the PLL stabilizes */
1026 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1027 btwrite(0,BT848_DSTATUS);
1029 btwrite(0x08,BT848_TGCTRL);
1030 btv->pll.pll_current = btv->pll.pll_ofreq;
1031 bttv_printk(" ok\n");
1035 btv->pll.pll_current = -1;
1036 bttv_printk("failed\n");
1040 /* used to switch between the bt848's analog/digital video capture modes */
1041 static void bt848A_set_timing(struct bttv *btv)
1044 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1045 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1047 if (btv->input == btv->dig) {
1048 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1049 btv->c.nr,table_idx);
1051 /* timing change...reset timing generator address */
1052 btwrite(0x00, BT848_TGCTRL);
1053 btwrite(0x02, BT848_TGCTRL);
1054 btwrite(0x00, BT848_TGCTRL);
1056 len=SRAM_Table[table_idx][0];
1057 for(i = 1; i <= len; i++)
1058 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1059 btv->pll.pll_ofreq = 27000000;
1062 btwrite(0x11, BT848_TGCTRL);
1063 btwrite(0x41, BT848_DVSIF);
1065 btv->pll.pll_ofreq = fsc;
1067 btwrite(0x0, BT848_DVSIF);
1071 /* ----------------------------------------------------------------------- */
1073 static void bt848_bright(struct bttv *btv, int bright)
1077 // printk("bttv: set bright: %d\n",bright); // DEBUG
1078 btv->bright = bright;
1080 /* We want -128 to 127 we get 0-65535 */
1081 value = (bright >> 8) - 128;
1082 btwrite(value & 0xff, BT848_BRIGHT);
1085 static void bt848_hue(struct bttv *btv, int hue)
1092 value = (hue >> 8) - 128;
1093 btwrite(value & 0xff, BT848_HUE);
1096 static void bt848_contrast(struct bttv *btv, int cont)
1100 btv->contrast = cont;
1103 value = (cont >> 7);
1104 hibit = (value >> 6) & 4;
1105 btwrite(value & 0xff, BT848_CONTRAST_LO);
1106 btaor(hibit, ~4, BT848_E_CONTROL);
1107 btaor(hibit, ~4, BT848_O_CONTROL);
1110 static void bt848_sat(struct bttv *btv, int color)
1112 int val_u,val_v,hibits;
1114 btv->saturation = color;
1116 /* 0-511 for the color */
1117 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1118 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1119 hibits = (val_u >> 7) & 2;
1120 hibits |= (val_v >> 8) & 1;
1121 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1122 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1123 btaor(hibits, ~3, BT848_E_CONTROL);
1124 btaor(hibits, ~3, BT848_O_CONTROL);
1127 /* ----------------------------------------------------------------------- */
1130 video_mux(struct bttv *btv, unsigned int input)
1134 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1137 /* needed by RemoteVideo MX */
1138 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1140 gpio_inout(mask2,mask2);
1142 if (input == btv->svhs) {
1143 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1144 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1146 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1147 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1149 mux = bttv_muxsel(btv, input);
1150 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1151 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1152 btv->c.nr,input,mux);
1154 /* card specific hook */
1155 if(bttv_tvcards[btv->c.type].muxsel_hook)
1156 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1160 static char *audio_modes[] = {
1161 "audio: tuner", "audio: radio", "audio: extern",
1162 "audio: intern", "audio: mute"
1166 audio_mux(struct bttv *btv, int input, int mute)
1168 int gpio_val, signal;
1169 struct v4l2_control ctrl;
1171 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1172 bttv_tvcards[btv->c.type].gpiomask);
1173 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1179 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1182 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1184 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1186 switch (btv->c.type) {
1187 case BTTV_BOARD_VOODOOTV_FM:
1188 case BTTV_BOARD_VOODOOTV_200:
1189 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1193 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1197 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1201 ctrl.id = V4L2_CID_AUDIO_MUTE;
1202 ctrl.value = btv->mute;
1203 bttv_call_all(btv, core, s_ctrl, &ctrl);
1204 if (btv->sd_msp34xx) {
1207 /* Note: the inputs tuner/radio/extern/intern are translated
1208 to msp routings. This assumes common behavior for all msp3400
1209 based TV cards. When this assumption fails, then the
1210 specific MSP routing must be added to the card table.
1211 For now this is sufficient. */
1213 case TVAUDIO_INPUT_RADIO:
1214 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1215 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1217 case TVAUDIO_INPUT_EXTERN:
1218 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1219 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1221 case TVAUDIO_INPUT_INTERN:
1222 /* Yes, this is the same input as for RADIO. I doubt
1223 if this is ever used. The only board with an INTERN
1224 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1225 that was tested. My guess is that the whole INTERN
1226 input does not work. */
1227 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1228 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1230 case TVAUDIO_INPUT_TUNER:
1232 /* This is the only card that uses TUNER2, and afaik,
1233 is the only difference between the VOODOOTV_FM
1235 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1236 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1237 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1239 in = MSP_INPUT_DEFAULT;
1242 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1243 in, MSP_OUTPUT_DEFAULT, 0);
1245 if (btv->sd_tvaudio) {
1246 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1253 audio_mute(struct bttv *btv, int mute)
1255 return audio_mux(btv, btv->audio, mute);
1259 audio_input(struct bttv *btv, int input)
1261 return audio_mux(btv, input, btv->mute);
1265 bttv_crop_calc_limits(struct bttv_crop *c)
1267 /* Scale factor min. 1:1, max. 16:1. Min. image size
1268 48 x 32. Scaled width must be a multiple of 4. */
1271 /* For bug compatibility with VIDIOCGCAP and image
1272 size checks in earlier driver versions. */
1273 c->min_scaled_width = 48;
1274 c->min_scaled_height = 32;
1276 c->min_scaled_width =
1277 (max(48, c->rect.width >> 4) + 3) & ~3;
1278 c->min_scaled_height =
1279 max(32, c->rect.height >> 4);
1282 c->max_scaled_width = c->rect.width & ~3;
1283 c->max_scaled_height = c->rect.height;
1287 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1289 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1290 bttv_crop_calc_limits(c);
1293 /* Call with btv->lock down. */
1295 set_tvnorm(struct bttv *btv, unsigned int norm)
1297 const struct bttv_tvnorm *tvnorm;
1300 BUG_ON(norm >= BTTV_TVNORMS);
1301 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
1303 tvnorm = &bttv_tvnorms[norm];
1305 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1306 sizeof (tvnorm->cropcap))) {
1307 bttv_crop_reset(&btv->crop[0], norm);
1308 btv->crop[1] = btv->crop[0]; /* current = default */
1310 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1311 btv->crop_start = tvnorm->cropcap.bounds.top
1312 + tvnorm->cropcap.bounds.height;
1318 btwrite(tvnorm->adelay, BT848_ADELAY);
1319 btwrite(tvnorm->bdelay, BT848_BDELAY);
1320 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1322 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1323 btwrite(1, BT848_VBI_PACK_DEL);
1324 bt848A_set_timing(btv);
1326 switch (btv->c.type) {
1327 case BTTV_BOARD_VOODOOTV_FM:
1328 case BTTV_BOARD_VOODOOTV_200:
1329 bttv_tda9880_setnorm(btv, gpio_read());
1332 id = tvnorm->v4l2_id;
1333 bttv_call_all(btv, core, s_std, id);
1338 /* Call with btv->lock down. */
1340 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1342 unsigned long flags;
1346 spin_lock_irqsave(&btv->s_lock,flags);
1347 if (btv->curr.frame_irq) {
1348 /* active capture -> delayed input switch */
1349 btv->new_input = input;
1351 video_mux(btv,input);
1353 spin_unlock_irqrestore(&btv->s_lock,flags);
1355 video_mux(btv,input);
1357 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1358 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
1359 set_tvnorm(btv, norm);
1362 static void init_irqreg(struct bttv *btv)
1365 btwrite(0xfffffUL, BT848_INT_STAT);
1367 if (bttv_tvcards[btv->c.type].no_video) {
1369 btwrite(BT848_INT_I2CDONE,
1373 btwrite((btv->triton1) |
1374 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1376 (fdsr ? BT848_INT_FDSR : 0) |
1377 BT848_INT_RISCI | BT848_INT_OCERR |
1378 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1384 static void init_bt848(struct bttv *btv)
1388 if (bttv_tvcards[btv->c.type].no_video) {
1389 /* very basic init only */
1394 btwrite(0x00, BT848_CAP_CTL);
1395 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1396 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1398 /* set planar and packed mode trigger points and */
1399 /* set rising edge of inverted GPINTR pin as irq trigger */
1400 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1401 BT848_GPIO_DMA_CTL_PLTP1_16|
1402 BT848_GPIO_DMA_CTL_PLTP23_16|
1403 BT848_GPIO_DMA_CTL_GPINTC|
1404 BT848_GPIO_DMA_CTL_GPINTI,
1405 BT848_GPIO_DMA_CTL);
1407 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1408 btwrite(val, BT848_E_SCLOOP);
1409 btwrite(val, BT848_O_SCLOOP);
1411 btwrite(0x20, BT848_E_VSCALE_HI);
1412 btwrite(0x20, BT848_O_VSCALE_HI);
1413 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1416 btwrite(whitecrush_upper, BT848_WC_UP);
1417 btwrite(whitecrush_lower, BT848_WC_DOWN);
1419 if (btv->opt_lumafilter) {
1420 btwrite(0, BT848_E_CONTROL);
1421 btwrite(0, BT848_O_CONTROL);
1423 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1424 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1427 bt848_bright(btv, btv->bright);
1428 bt848_hue(btv, btv->hue);
1429 bt848_contrast(btv, btv->contrast);
1430 bt848_sat(btv, btv->saturation);
1436 static void bttv_reinit_bt848(struct bttv *btv)
1438 unsigned long flags;
1441 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1442 spin_lock_irqsave(&btv->s_lock,flags);
1444 bttv_set_dma(btv,0);
1445 spin_unlock_irqrestore(&btv->s_lock,flags);
1448 btv->pll.pll_current = -1;
1449 set_input(btv, btv->input, btv->tvnorm);
1452 static int bttv_g_ctrl(struct file *file, void *priv,
1453 struct v4l2_control *c)
1455 struct bttv_fh *fh = priv;
1456 struct bttv *btv = fh->btv;
1459 case V4L2_CID_BRIGHTNESS:
1460 c->value = btv->bright;
1463 c->value = btv->hue;
1465 case V4L2_CID_CONTRAST:
1466 c->value = btv->contrast;
1468 case V4L2_CID_SATURATION:
1469 c->value = btv->saturation;
1472 case V4L2_CID_AUDIO_MUTE:
1473 case V4L2_CID_AUDIO_VOLUME:
1474 case V4L2_CID_AUDIO_BALANCE:
1475 case V4L2_CID_AUDIO_BASS:
1476 case V4L2_CID_AUDIO_TREBLE:
1477 bttv_call_all(btv, core, g_ctrl, c);
1480 case V4L2_CID_PRIVATE_CHROMA_AGC:
1481 c->value = btv->opt_chroma_agc;
1483 case V4L2_CID_PRIVATE_COMBFILTER:
1484 c->value = btv->opt_combfilter;
1486 case V4L2_CID_PRIVATE_LUMAFILTER:
1487 c->value = btv->opt_lumafilter;
1489 case V4L2_CID_PRIVATE_AUTOMUTE:
1490 c->value = btv->opt_automute;
1492 case V4L2_CID_PRIVATE_AGC_CRUSH:
1493 c->value = btv->opt_adc_crush;
1495 case V4L2_CID_PRIVATE_VCR_HACK:
1496 c->value = btv->opt_vcr_hack;
1498 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1499 c->value = btv->opt_whitecrush_upper;
1501 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1502 c->value = btv->opt_whitecrush_lower;
1504 case V4L2_CID_PRIVATE_UV_RATIO:
1505 c->value = btv->opt_uv_ratio;
1507 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1508 c->value = btv->opt_full_luma_range;
1510 case V4L2_CID_PRIVATE_CORING:
1511 c->value = btv->opt_coring;
1519 static int bttv_s_ctrl(struct file *file, void *f,
1520 struct v4l2_control *c)
1524 struct bttv_fh *fh = f;
1525 struct bttv *btv = fh->btv;
1527 err = v4l2_prio_check(&btv->prio, fh->prio);
1532 case V4L2_CID_BRIGHTNESS:
1533 bt848_bright(btv, c->value);
1536 bt848_hue(btv, c->value);
1538 case V4L2_CID_CONTRAST:
1539 bt848_contrast(btv, c->value);
1541 case V4L2_CID_SATURATION:
1542 bt848_sat(btv, c->value);
1544 case V4L2_CID_AUDIO_MUTE:
1545 audio_mute(btv, c->value);
1547 case V4L2_CID_AUDIO_VOLUME:
1548 if (btv->volume_gpio)
1549 btv->volume_gpio(btv, c->value);
1551 bttv_call_all(btv, core, s_ctrl, c);
1553 case V4L2_CID_AUDIO_BALANCE:
1554 case V4L2_CID_AUDIO_BASS:
1555 case V4L2_CID_AUDIO_TREBLE:
1556 bttv_call_all(btv, core, s_ctrl, c);
1559 case V4L2_CID_PRIVATE_CHROMA_AGC:
1560 btv->opt_chroma_agc = c->value;
1561 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1562 btwrite(val, BT848_E_SCLOOP);
1563 btwrite(val, BT848_O_SCLOOP);
1565 case V4L2_CID_PRIVATE_COMBFILTER:
1566 btv->opt_combfilter = c->value;
1568 case V4L2_CID_PRIVATE_LUMAFILTER:
1569 btv->opt_lumafilter = c->value;
1570 if (btv->opt_lumafilter) {
1571 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1572 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1574 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1575 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1578 case V4L2_CID_PRIVATE_AUTOMUTE:
1579 btv->opt_automute = c->value;
1581 case V4L2_CID_PRIVATE_AGC_CRUSH:
1582 btv->opt_adc_crush = c->value;
1583 btwrite(BT848_ADC_RESERVED |
1584 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1587 case V4L2_CID_PRIVATE_VCR_HACK:
1588 btv->opt_vcr_hack = c->value;
1590 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1591 btv->opt_whitecrush_upper = c->value;
1592 btwrite(c->value, BT848_WC_UP);
1594 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1595 btv->opt_whitecrush_lower = c->value;
1596 btwrite(c->value, BT848_WC_DOWN);
1598 case V4L2_CID_PRIVATE_UV_RATIO:
1599 btv->opt_uv_ratio = c->value;
1600 bt848_sat(btv, btv->saturation);
1602 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1603 btv->opt_full_luma_range = c->value;
1604 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1606 case V4L2_CID_PRIVATE_CORING:
1607 btv->opt_coring = c->value;
1608 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1616 /* ----------------------------------------------------------------------- */
1618 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1620 unsigned int outbits, data;
1621 outbits = btread(BT848_GPIO_OUT_EN);
1622 data = btread(BT848_GPIO_DATA);
1623 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1624 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1627 static void bttv_field_count(struct bttv *btv)
1635 /* start field counter */
1636 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1638 /* stop field counter */
1639 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1640 btv->field_count = 0;
1644 static const struct bttv_format*
1645 format_by_fourcc(int fourcc)
1649 for (i = 0; i < FORMATS; i++) {
1650 if (-1 == formats[i].fourcc)
1652 if (formats[i].fourcc == fourcc)
1658 /* ----------------------------------------------------------------------- */
1662 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1663 struct bttv_buffer *new)
1665 struct bttv_buffer *old;
1666 unsigned long flags;
1669 dprintk("switch_overlay: enter [new=%p]\n",new);
1671 new->vb.state = VIDEOBUF_DONE;
1672 spin_lock_irqsave(&btv->s_lock,flags);
1676 bttv_set_dma(btv, 0x03);
1677 spin_unlock_irqrestore(&btv->s_lock,flags);
1679 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1680 bttv_dma_free(&fh->cap,btv, old);
1684 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
1685 dprintk("switch_overlay: done\n");
1689 /* ----------------------------------------------------------------------- */
1690 /* video4linux (1) interface */
1692 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1693 struct bttv_buffer *buf,
1694 const struct bttv_format *fmt,
1695 unsigned int width, unsigned int height,
1696 enum v4l2_field field)
1698 struct bttv_fh *fh = q->priv_data;
1699 int redo_dma_risc = 0;
1704 /* check settings */
1707 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1709 height = RAW_LINES*2;
1710 if (width*height > buf->vb.bsize)
1712 buf->vb.size = buf->vb.bsize;
1714 /* Make sure tvnorm and vbi_end remain consistent
1715 until we're done. */
1716 mutex_lock(&btv->lock);
1720 /* In this mode capturing always starts at defrect.top
1721 (default VDELAY), ignoring cropping parameters. */
1722 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1723 mutex_unlock(&btv->lock);
1727 mutex_unlock(&btv->lock);
1729 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1731 mutex_lock(&btv->lock);
1734 c = btv->crop[!!fh->do_crop];
1736 mutex_unlock(&btv->lock);
1738 if (width < c.min_scaled_width ||
1739 width > c.max_scaled_width ||
1740 height < c.min_scaled_height)
1744 case V4L2_FIELD_TOP:
1745 case V4L2_FIELD_BOTTOM:
1746 case V4L2_FIELD_ALTERNATE:
1747 /* btv->crop counts frame lines. Max. scale
1748 factor is 16:1 for frames, 8:1 for fields. */
1749 if (height * 2 > c.max_scaled_height)
1754 if (height > c.max_scaled_height)
1759 buf->vb.size = (width * height * fmt->depth) >> 3;
1760 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1764 /* alloc + fill struct bttv_buffer (if changed) */
1765 if (buf->vb.width != width || buf->vb.height != height ||
1766 buf->vb.field != field ||
1767 buf->tvnorm != norm || buf->fmt != fmt ||
1768 buf->crop.top != c.rect.top ||
1769 buf->crop.left != c.rect.left ||
1770 buf->crop.width != c.rect.width ||
1771 buf->crop.height != c.rect.height) {
1772 buf->vb.width = width;
1773 buf->vb.height = height;
1774 buf->vb.field = field;
1781 /* alloc risc memory */
1782 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1784 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1789 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1792 buf->vb.state = VIDEOBUF_PREPARED;
1796 bttv_dma_free(q,btv,buf);
1801 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1803 struct bttv_fh *fh = q->priv_data;
1805 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1808 if (*size * *count > gbuffers * gbufsize)
1809 *count = (gbuffers * gbufsize) / *size;
1814 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1815 enum v4l2_field field)
1817 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1818 struct bttv_fh *fh = q->priv_data;
1820 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1821 fh->width, fh->height, field);
1825 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1827 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1828 struct bttv_fh *fh = q->priv_data;
1829 struct bttv *btv = fh->btv;
1831 buf->vb.state = VIDEOBUF_QUEUED;
1832 list_add_tail(&buf->vb.queue,&btv->capture);
1833 if (!btv->curr.frame_irq) {
1835 bttv_set_dma(btv, 0x03);
1839 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1841 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1842 struct bttv_fh *fh = q->priv_data;
1844 bttv_dma_free(q,fh->btv,buf);
1847 static struct videobuf_queue_ops bttv_video_qops = {
1848 .buf_setup = buffer_setup,
1849 .buf_prepare = buffer_prepare,
1850 .buf_queue = buffer_queue,
1851 .buf_release = buffer_release,
1854 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
1856 struct bttv_fh *fh = priv;
1857 struct bttv *btv = fh->btv;
1861 mutex_lock(&btv->lock);
1862 err = v4l2_prio_check(&btv->prio, fh->prio);
1866 for (i = 0; i < BTTV_TVNORMS; i++)
1867 if (*id & bttv_tvnorms[i].v4l2_id)
1869 if (i == BTTV_TVNORMS) {
1877 mutex_unlock(&btv->lock);
1882 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1884 struct bttv_fh *fh = f;
1885 struct bttv *btv = fh->btv;
1887 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1888 *id = V4L2_STD_625_50;
1890 *id = V4L2_STD_525_60;
1894 static int bttv_enum_input(struct file *file, void *priv,
1895 struct v4l2_input *i)
1897 struct bttv_fh *fh = priv;
1898 struct bttv *btv = fh->btv;
1901 mutex_lock(&btv->lock);
1902 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1907 i->type = V4L2_INPUT_TYPE_CAMERA;
1910 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1911 sprintf(i->name, "Television");
1912 i->type = V4L2_INPUT_TYPE_TUNER;
1914 } else if (i->index == btv->svhs) {
1915 sprintf(i->name, "S-Video");
1917 sprintf(i->name, "Composite%d", i->index);
1920 if (i->index == btv->input) {
1921 __u32 dstatus = btread(BT848_DSTATUS);
1922 if (0 == (dstatus & BT848_DSTATUS_PRES))
1923 i->status |= V4L2_IN_ST_NO_SIGNAL;
1924 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1925 i->status |= V4L2_IN_ST_NO_H_LOCK;
1928 i->std = BTTV_NORMS;
1931 mutex_unlock(&btv->lock);
1936 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1938 struct bttv_fh *fh = priv;
1939 struct bttv *btv = fh->btv;
1941 mutex_lock(&btv->lock);
1943 mutex_unlock(&btv->lock);
1948 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1950 struct bttv_fh *fh = priv;
1951 struct bttv *btv = fh->btv;
1955 mutex_lock(&btv->lock);
1956 err = v4l2_prio_check(&btv->prio, fh->prio);
1960 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1965 set_input(btv, i, btv->tvnorm);
1968 mutex_unlock(&btv->lock);
1972 static int bttv_s_tuner(struct file *file, void *priv,
1973 struct v4l2_tuner *t)
1975 struct bttv_fh *fh = priv;
1976 struct bttv *btv = fh->btv;
1979 if (unlikely(0 != t->index))
1982 mutex_lock(&btv->lock);
1983 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
1988 err = v4l2_prio_check(&btv->prio, fh->prio);
1992 bttv_call_all(btv, tuner, s_tuner, t);
1994 if (btv->audio_mode_gpio)
1995 btv->audio_mode_gpio(btv, t, 1);
1998 mutex_unlock(&btv->lock);
2003 static int bttv_g_frequency(struct file *file, void *priv,
2004 struct v4l2_frequency *f)
2006 struct bttv_fh *fh = priv;
2007 struct bttv *btv = fh->btv;
2009 mutex_lock(&btv->lock);
2010 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2011 f->frequency = btv->freq;
2012 mutex_unlock(&btv->lock);
2017 static int bttv_s_frequency(struct file *file, void *priv,
2018 struct v4l2_frequency *f)
2020 struct bttv_fh *fh = priv;
2021 struct bttv *btv = fh->btv;
2024 if (unlikely(f->tuner != 0))
2027 mutex_lock(&btv->lock);
2028 err = v4l2_prio_check(&btv->prio, fh->prio);
2032 if (unlikely(f->type != (btv->radio_user
2033 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2037 btv->freq = f->frequency;
2038 bttv_call_all(btv, tuner, s_frequency, f);
2039 if (btv->has_matchbox && btv->radio_user)
2040 tea5757_set_freq(btv, btv->freq);
2042 mutex_unlock(&btv->lock);
2047 static int bttv_log_status(struct file *file, void *f)
2049 struct bttv_fh *fh = f;
2050 struct bttv *btv = fh->btv;
2052 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2053 btv->c.nr, btv->c.nr);
2054 bttv_call_all(btv, core, log_status);
2055 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2056 btv->c.nr, btv->c.nr);
2060 #ifdef CONFIG_VIDEO_ADV_DEBUG
2061 static int bttv_g_register(struct file *file, void *f,
2062 struct v4l2_dbg_register *reg)
2064 struct bttv_fh *fh = f;
2065 struct bttv *btv = fh->btv;
2067 if (!capable(CAP_SYS_ADMIN))
2070 if (!v4l2_chip_match_host(®->match))
2073 /* bt848 has a 12-bit register space */
2075 reg->val = btread(reg->reg);
2081 static int bttv_s_register(struct file *file, void *f,
2082 struct v4l2_dbg_register *reg)
2084 struct bttv_fh *fh = f;
2085 struct bttv *btv = fh->btv;
2087 if (!capable(CAP_SYS_ADMIN))
2090 if (!v4l2_chip_match_host(®->match))
2093 /* bt848 has a 12-bit register space */
2095 btwrite(reg->val, reg->reg);
2101 /* Given cropping boundaries b and the scaled width and height of a
2102 single field or frame, which must not exceed hardware limits, this
2103 function adjusts the cropping parameters c. */
2105 bttv_crop_adjust (struct bttv_crop * c,
2106 const struct v4l2_rect * b,
2109 enum v4l2_field field)
2111 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2115 if (width < c->min_scaled_width) {
2116 /* Max. hor. scale factor 16:1. */
2117 c->rect.width = width * 16;
2118 } else if (width > c->max_scaled_width) {
2119 /* Min. hor. scale factor 1:1. */
2120 c->rect.width = width;
2122 max_left = b->left + b->width - width;
2123 max_left = min(max_left, (__s32) MAX_HDELAY);
2124 if (c->rect.left > max_left)
2125 c->rect.left = max_left;
2128 if (height < c->min_scaled_height) {
2129 /* Max. vert. scale factor 16:1, single fields 8:1. */
2130 c->rect.height = height * 16;
2131 } else if (frame_height > c->max_scaled_height) {
2132 /* Min. vert. scale factor 1:1.
2133 Top and height count field lines times two. */
2134 c->rect.height = (frame_height + 1) & ~1;
2136 max_top = b->top + b->height - c->rect.height;
2137 if (c->rect.top > max_top)
2138 c->rect.top = max_top;
2141 bttv_crop_calc_limits(c);
2144 /* Returns an error if scaling to a frame or single field with the given
2145 width and height is not possible with the current cropping parameters
2146 and width aligned according to width_mask. If adjust_size is TRUE the
2147 function may adjust the width and/or height instead, rounding width
2148 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2149 also adjust the current cropping parameters to get closer to the
2150 desired image size. */
2152 limit_scaled_size_lock (struct bttv_fh * fh,
2155 enum v4l2_field field,
2156 unsigned int width_mask,
2157 unsigned int width_bias,
2161 struct bttv *btv = fh->btv;
2162 const struct v4l2_rect *b;
2163 struct bttv_crop *c;
2170 BUG_ON((int) width_mask >= 0 ||
2171 width_bias >= (unsigned int) -width_mask);
2173 /* Make sure tvnorm, vbi_end and the current cropping parameters
2174 remain consistent until we're done. */
2175 mutex_lock(&btv->lock);
2177 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2179 /* Do crop - use current, don't - use default parameters. */
2180 c = &btv->crop[!!fh->do_crop];
2185 && !locked_btres(btv, VIDEO_RESOURCES)) {
2189 /* We cannot scale up. When the scaled image is larger
2190 than crop.rect we adjust the crop.rect as required
2191 by the V4L2 spec, hence cropcap.bounds are our limit. */
2192 max_width = min(b->width, (__s32) MAX_HACTIVE);
2193 max_height = b->height;
2195 /* We cannot capture the same line as video and VBI data.
2196 Note btv->vbi_end is really a minimum, see
2197 bttv_vbi_try_fmt(). */
2198 if (btv->vbi_end > b->top) {
2199 max_height -= btv->vbi_end - b->top;
2201 if (min_height > max_height)
2206 if (btv->vbi_end > c->rect.top)
2209 min_width = c->min_scaled_width;
2210 min_height = c->min_scaled_height;
2211 max_width = c->max_scaled_width;
2212 max_height = c->max_scaled_height;
2217 min_width = (min_width - width_mask - 1) & width_mask;
2218 max_width = max_width & width_mask;
2220 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2221 min_height = min_height;
2222 /* Min. scale factor is 1:1. */
2223 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2226 *width = clamp(*width, min_width, max_width);
2227 *height = clamp(*height, min_height, max_height);
2229 /* Round after clamping to avoid overflow. */
2230 *width = (*width + width_bias) & width_mask;
2233 bttv_crop_adjust(c, b, *width, *height, field);
2235 if (btv->vbi_end > c->rect.top) {
2236 /* Move the crop window out of the way. */
2237 c->rect.top = btv->vbi_end;
2242 if (*width < min_width ||
2243 *height < min_height ||
2244 *width > max_width ||
2245 *height > max_height ||
2246 0 != (*width & ~width_mask))
2250 rc = 0; /* success */
2253 mutex_unlock(&btv->lock);
2258 /* Returns an error if the given overlay window dimensions are not
2259 possible with the current cropping parameters. If adjust_size is
2260 TRUE the function may adjust the window width and/or height
2261 instead, however it always rounds the horizontal position and
2262 width as btcx_align() does. If adjust_crop is TRUE the function
2263 may also adjust the current cropping parameters to get closer
2264 to the desired window size. */
2266 verify_window_lock (struct bttv_fh * fh,
2267 struct v4l2_window * win,
2271 enum v4l2_field field;
2272 unsigned int width_mask;
2275 if (win->w.width < 48 || win->w.height < 32)
2277 if (win->clipcount > 2048)
2282 if (V4L2_FIELD_ANY == field) {
2285 mutex_lock(&fh->btv->lock);
2286 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2287 mutex_unlock(&fh->btv->lock);
2288 field = (win->w.height > height2)
2289 ? V4L2_FIELD_INTERLACED
2293 case V4L2_FIELD_TOP:
2294 case V4L2_FIELD_BOTTOM:
2295 case V4L2_FIELD_INTERLACED:
2301 /* 4-byte alignment. */
2302 if (NULL == fh->ovfmt)
2305 switch (fh->ovfmt->depth) {
2319 win->w.width -= win->w.left & ~width_mask;
2320 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2322 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
2324 /* width_bias: round down */ 0,
2325 adjust_size, adjust_crop);
2333 static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
2334 struct v4l2_window *win, int fixup)
2336 struct v4l2_clip *clips = NULL;
2337 int n,size,retval = 0;
2339 if (NULL == fh->ovfmt)
2341 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2343 retval = verify_window_lock(fh, win,
2344 /* adjust_size */ fixup,
2345 /* adjust_crop */ fixup);
2349 /* copy clips -- luckily v4l1 + v4l2 are binary
2350 compatible here ...*/
2352 size = sizeof(*clips)*(n+4);
2353 clips = kmalloc(size,GFP_KERNEL);
2357 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2363 mutex_lock(&fh->cap.vb_lock);
2364 /* clip against screen */
2365 if (NULL != btv->fbuf.base)
2366 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2368 btcx_sort_clips(clips,n);
2370 /* 4-byte alignments */
2371 switch (fh->ovfmt->depth) {
2374 btcx_align(&win->w, clips, n, 3);
2377 btcx_align(&win->w, clips, n, 1);
2380 /* no alignment fixups needed */
2386 kfree(fh->ov.clips);
2387 fh->ov.clips = clips;
2391 fh->ov.field = win->field;
2392 fh->ov.setup_ok = 1;
2395 * FIXME: btv is protected by btv->lock mutex, while btv->init
2396 * is protected by fh->cap.vb_lock. This seems to open the
2397 * possibility for some race situations. Maybe the better would
2398 * be to unify those locks or to use another way to store the
2399 * init values that will be consumed by videobuf callbacks
2401 btv->init.ov.w.width = win->w.width;
2402 btv->init.ov.w.height = win->w.height;
2403 btv->init.ov.field = win->field;
2405 /* update overlay if needed */
2407 if (check_btres(fh, RESOURCE_OVERLAY)) {
2408 struct bttv_buffer *new;
2410 new = videobuf_sg_alloc(sizeof(*new));
2411 new->crop = btv->crop[!!fh->do_crop].rect;
2412 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2413 retval = bttv_switch_overlay(btv,fh,new);
2415 mutex_unlock(&fh->cap.vb_lock);
2419 /* ----------------------------------------------------------------------- */
2421 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2423 struct videobuf_queue* q = NULL;
2426 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2429 case V4L2_BUF_TYPE_VBI_CAPTURE:
2438 static int bttv_resource(struct bttv_fh *fh)
2443 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2444 res = RESOURCE_VIDEO_STREAM;
2446 case V4L2_BUF_TYPE_VBI_CAPTURE:
2455 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2457 struct videobuf_queue *q = bttv_queue(fh);
2458 int res = bttv_resource(fh);
2460 if (check_btres(fh,res))
2462 if (videobuf_queue_is_busy(q))
2469 pix_format_set_size (struct v4l2_pix_format * f,
2470 const struct bttv_format * fmt,
2472 unsigned int height)
2477 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2478 f->bytesperline = width; /* Y plane */
2479 f->sizeimage = (width * height * fmt->depth) >> 3;
2481 f->bytesperline = (width * fmt->depth) >> 3;
2482 f->sizeimage = height * f->bytesperline;
2486 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2487 struct v4l2_format *f)
2489 struct bttv_fh *fh = priv;
2491 pix_format_set_size(&f->fmt.pix, fh->fmt,
2492 fh->width, fh->height);
2493 f->fmt.pix.field = fh->cap.field;
2494 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2499 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2500 struct v4l2_format *f)
2502 struct bttv_fh *fh = priv;
2504 f->fmt.win.w = fh->ov.w;
2505 f->fmt.win.field = fh->ov.field;
2510 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2511 struct v4l2_format *f)
2513 const struct bttv_format *fmt;
2514 struct bttv_fh *fh = priv;
2515 struct bttv *btv = fh->btv;
2516 enum v4l2_field field;
2517 __s32 width, height;
2520 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2524 field = f->fmt.pix.field;
2526 if (V4L2_FIELD_ANY == field) {
2529 mutex_lock(&btv->lock);
2530 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2531 mutex_unlock(&btv->lock);
2532 field = (f->fmt.pix.height > height2)
2533 ? V4L2_FIELD_INTERLACED
2534 : V4L2_FIELD_BOTTOM;
2537 if (V4L2_FIELD_SEQ_BT == field)
2538 field = V4L2_FIELD_SEQ_TB;
2541 case V4L2_FIELD_TOP:
2542 case V4L2_FIELD_BOTTOM:
2543 case V4L2_FIELD_ALTERNATE:
2544 case V4L2_FIELD_INTERLACED:
2546 case V4L2_FIELD_SEQ_TB:
2547 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2554 width = f->fmt.pix.width;
2555 height = f->fmt.pix.height;
2557 rc = limit_scaled_size_lock(fh, &width, &height, field,
2558 /* width_mask: 4 pixels */ ~3,
2559 /* width_bias: nearest */ 2,
2560 /* adjust_size */ 1,
2561 /* adjust_crop */ 0);
2565 /* update data for the application */
2566 f->fmt.pix.field = field;
2567 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2572 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2573 struct v4l2_format *f)
2575 struct bttv_fh *fh = priv;
2577 return verify_window_lock(fh, &f->fmt.win,
2578 /* adjust_size */ 1,
2579 /* adjust_crop */ 0);
2582 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2583 struct v4l2_format *f)
2586 const struct bttv_format *fmt;
2587 struct bttv_fh *fh = priv;
2588 struct bttv *btv = fh->btv;
2589 __s32 width, height;
2590 enum v4l2_field field;
2592 retval = bttv_switch_type(fh, f->type);
2596 retval = bttv_try_fmt_vid_cap(file, priv, f);
2600 width = f->fmt.pix.width;
2601 height = f->fmt.pix.height;
2602 field = f->fmt.pix.field;
2604 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
2605 /* width_mask: 4 pixels */ ~3,
2606 /* width_bias: nearest */ 2,
2607 /* adjust_size */ 1,
2608 /* adjust_crop */ 1);
2612 f->fmt.pix.field = field;
2614 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2616 /* update our state informations */
2617 mutex_lock(&fh->cap.vb_lock);
2619 fh->cap.field = f->fmt.pix.field;
2620 fh->cap.last = V4L2_FIELD_NONE;
2621 fh->width = f->fmt.pix.width;
2622 fh->height = f->fmt.pix.height;
2623 btv->init.fmt = fmt;
2624 btv->init.width = f->fmt.pix.width;
2625 btv->init.height = f->fmt.pix.height;
2626 mutex_unlock(&fh->cap.vb_lock);
2631 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2632 struct v4l2_format *f)
2634 struct bttv_fh *fh = priv;
2635 struct bttv *btv = fh->btv;
2637 if (no_overlay > 0) {
2638 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2642 return setup_window_lock(fh, btv, &f->fmt.win, 1);
2645 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2646 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2650 struct bttv_fh *fh = priv;
2652 mutex_lock(&fh->cap.vb_lock);
2653 retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
2656 mutex_unlock(&fh->cap.vb_lock);
2661 memset(mbuf, 0, sizeof(*mbuf));
2662 mbuf->frames = gbuffers;
2663 mbuf->size = gbuffers * gbufsize;
2665 for (i = 0; i < gbuffers; i++)
2666 mbuf->offsets[i] = i * gbufsize;
2668 mutex_unlock(&fh->cap.vb_lock);
2673 static int bttv_querycap(struct file *file, void *priv,
2674 struct v4l2_capability *cap)
2676 struct bttv_fh *fh = priv;
2677 struct bttv *btv = fh->btv;
2682 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2683 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2684 snprintf(cap->bus_info, sizeof(cap->bus_info),
2685 "PCI:%s", pci_name(btv->c.pci));
2686 cap->version = BTTV_VERSION_CODE;
2688 V4L2_CAP_VIDEO_CAPTURE |
2689 V4L2_CAP_VBI_CAPTURE |
2690 V4L2_CAP_READWRITE |
2692 if (no_overlay <= 0)
2693 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2696 * No need to lock here: those vars are initialized during board
2697 * probe and remains untouched during the rest of the driver lifecycle
2699 if (btv->has_saa6588)
2700 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2701 if (btv->tuner_type != TUNER_ABSENT)
2702 cap->capabilities |= V4L2_CAP_TUNER;
2706 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2710 for (i = 0; i < FORMATS; i++) {
2711 if (formats[i].fourcc != -1)
2713 if ((unsigned int)index == f->index)
2719 f->pixelformat = formats[i].fourcc;
2720 strlcpy(f->description, formats[i].name, sizeof(f->description));
2725 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2726 struct v4l2_fmtdesc *f)
2728 int rc = bttv_enum_fmt_cap_ovr(f);
2736 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
2737 struct v4l2_fmtdesc *f)
2741 if (no_overlay > 0) {
2742 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2746 rc = bttv_enum_fmt_cap_ovr(f);
2751 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
2757 static int bttv_g_fbuf(struct file *file, void *f,
2758 struct v4l2_framebuffer *fb)
2760 struct bttv_fh *fh = f;
2761 struct bttv *btv = fh->btv;
2764 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2766 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2770 static int bttv_overlay(struct file *file, void *f, unsigned int on)
2772 struct bttv_fh *fh = f;
2773 struct bttv *btv = fh->btv;
2774 struct bttv_buffer *new;
2778 mutex_lock(&fh->cap.vb_lock);
2780 if (unlikely(!btv->fbuf.base)) {
2781 mutex_unlock(&fh->cap.vb_lock);
2784 if (unlikely(!fh->ov.setup_ok)) {
2785 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2790 mutex_unlock(&fh->cap.vb_lock);
2793 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
2796 mutex_lock(&fh->cap.vb_lock);
2798 fh->ov.tvnorm = btv->tvnorm;
2799 new = videobuf_sg_alloc(sizeof(*new));
2800 new->crop = btv->crop[!!fh->do_crop].rect;
2801 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2807 retval = bttv_switch_overlay(btv, fh, new);
2808 mutex_unlock(&fh->cap.vb_lock);
2812 static int bttv_s_fbuf(struct file *file, void *f,
2813 struct v4l2_framebuffer *fb)
2815 struct bttv_fh *fh = f;
2816 struct bttv *btv = fh->btv;
2817 const struct bttv_format *fmt;
2820 if (!capable(CAP_SYS_ADMIN) &&
2821 !capable(CAP_SYS_RAWIO))
2825 fmt = format_by_fourcc(fb->fmt.pixelformat);
2828 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2832 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2833 __s32 width = fb->fmt.width;
2834 __s32 height = fb->fmt.height;
2836 retval = limit_scaled_size_lock(fh, &width, &height,
2837 V4L2_FIELD_INTERLACED,
2838 /* width_mask */ ~3,
2840 /* adjust_size */ 0,
2841 /* adjust_crop */ 0);
2847 mutex_lock(&fh->cap.vb_lock);
2848 btv->fbuf.base = fb->base;
2849 btv->fbuf.fmt.width = fb->fmt.width;
2850 btv->fbuf.fmt.height = fb->fmt.height;
2851 if (0 != fb->fmt.bytesperline)
2852 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2854 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2858 btv->init.ovfmt = fmt;
2859 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2862 fh->ov.w.width = fb->fmt.width;
2863 fh->ov.w.height = fb->fmt.height;
2864 btv->init.ov.w.width = fb->fmt.width;
2865 btv->init.ov.w.height = fb->fmt.height;
2866 kfree(fh->ov.clips);
2867 fh->ov.clips = NULL;
2870 if (check_btres(fh, RESOURCE_OVERLAY)) {
2871 struct bttv_buffer *new;
2873 new = videobuf_sg_alloc(sizeof(*new));
2874 new->crop = btv->crop[!!fh->do_crop].rect;
2875 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2876 retval = bttv_switch_overlay(btv, fh, new);
2879 mutex_unlock(&fh->cap.vb_lock);
2883 static int bttv_reqbufs(struct file *file, void *priv,
2884 struct v4l2_requestbuffers *p)
2886 struct bttv_fh *fh = priv;
2887 return videobuf_reqbufs(bttv_queue(fh), p);
2890 static int bttv_querybuf(struct file *file, void *priv,
2891 struct v4l2_buffer *b)
2893 struct bttv_fh *fh = priv;
2894 return videobuf_querybuf(bttv_queue(fh), b);
2897 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2899 struct bttv_fh *fh = priv;
2900 struct bttv *btv = fh->btv;
2901 int res = bttv_resource(fh);
2903 if (!check_alloc_btres_lock(btv, fh, res))
2906 return videobuf_qbuf(bttv_queue(fh), b);
2909 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2911 struct bttv_fh *fh = priv;
2912 return videobuf_dqbuf(bttv_queue(fh), b,
2913 file->f_flags & O_NONBLOCK);
2916 static int bttv_streamon(struct file *file, void *priv,
2917 enum v4l2_buf_type type)
2919 struct bttv_fh *fh = priv;
2920 struct bttv *btv = fh->btv;
2921 int res = bttv_resource(fh);
2923 if (!check_alloc_btres_lock(btv, fh, res))
2925 return videobuf_streamon(bttv_queue(fh));
2929 static int bttv_streamoff(struct file *file, void *priv,
2930 enum v4l2_buf_type type)
2932 struct bttv_fh *fh = priv;
2933 struct bttv *btv = fh->btv;
2935 int res = bttv_resource(fh);
2938 retval = videobuf_streamoff(bttv_queue(fh));
2941 free_btres_lock(btv, fh, res);
2945 static int bttv_queryctrl(struct file *file, void *priv,
2946 struct v4l2_queryctrl *c)
2948 struct bttv_fh *fh = priv;
2949 struct bttv *btv = fh->btv;
2950 const struct v4l2_queryctrl *ctrl;
2952 if ((c->id < V4L2_CID_BASE ||
2953 c->id >= V4L2_CID_LASTP1) &&
2954 (c->id < V4L2_CID_PRIVATE_BASE ||
2955 c->id >= V4L2_CID_PRIVATE_LASTP1))
2958 mutex_lock(&btv->lock);
2959 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2962 ctrl = ctrl_by_id(c->id);
2964 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2966 mutex_unlock(&btv->lock);
2971 static int bttv_g_parm(struct file *file, void *f,
2972 struct v4l2_streamparm *parm)
2974 struct bttv_fh *fh = f;
2975 struct bttv *btv = fh->btv;
2977 mutex_lock(&btv->lock);
2978 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2979 &parm->parm.capture.timeperframe);
2980 mutex_unlock(&btv->lock);
2985 static int bttv_g_tuner(struct file *file, void *priv,
2986 struct v4l2_tuner *t)
2988 struct bttv_fh *fh = priv;
2989 struct bttv *btv = fh->btv;
2991 if (btv->tuner_type == TUNER_ABSENT)
2996 mutex_lock(&btv->lock);
2997 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2998 bttv_call_all(btv, tuner, g_tuner, t);
2999 strcpy(t->name, "Television");
3000 t->capability = V4L2_TUNER_CAP_NORM;
3001 t->type = V4L2_TUNER_ANALOG_TV;
3002 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
3005 if (btv->audio_mode_gpio)
3006 btv->audio_mode_gpio(btv, t, 0);
3008 mutex_unlock(&btv->lock);
3012 static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
3014 struct bttv_fh *fh = f;
3015 struct bttv *btv = fh->btv;
3017 mutex_lock(&btv->lock);
3018 *p = v4l2_prio_max(&btv->prio);
3019 mutex_unlock(&btv->lock);
3024 static int bttv_s_priority(struct file *file, void *f,
3025 enum v4l2_priority prio)
3027 struct bttv_fh *fh = f;
3028 struct bttv *btv = fh->btv;
3031 mutex_lock(&btv->lock);
3032 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
3033 mutex_unlock(&btv->lock);
3038 static int bttv_cropcap(struct file *file, void *priv,
3039 struct v4l2_cropcap *cap)
3041 struct bttv_fh *fh = priv;
3042 struct bttv *btv = fh->btv;
3044 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3045 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3048 mutex_lock(&btv->lock);
3049 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3050 mutex_unlock(&btv->lock);
3055 static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
3057 struct bttv_fh *fh = f;
3058 struct bttv *btv = fh->btv;
3060 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3061 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3064 /* No fh->do_crop = 1; because btv->crop[1] may be
3065 inconsistent with fh->width or fh->height and apps
3066 do not expect a change here. */
3068 mutex_lock(&btv->lock);
3069 crop->c = btv->crop[!!fh->do_crop].rect;
3070 mutex_unlock(&btv->lock);
3075 static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
3077 struct bttv_fh *fh = f;
3078 struct bttv *btv = fh->btv;
3079 const struct v4l2_rect *b;
3087 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3088 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3091 /* Make sure tvnorm, vbi_end and the current cropping
3092 parameters remain consistent until we're done. Note
3093 read() may change vbi_end in check_alloc_btres_lock(). */
3094 mutex_lock(&btv->lock);
3095 retval = v4l2_prio_check(&btv->prio, fh->prio);
3097 mutex_unlock(&btv->lock);
3103 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3104 mutex_unlock(&btv->lock);
3108 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3111 b_right = b_left + b->width;
3112 b_bottom = b->top + b->height;
3114 b_top = max(b->top, btv->vbi_end);
3115 if (b_top + 32 >= b_bottom) {
3116 mutex_unlock(&btv->lock);
3120 /* Min. scaled size 48 x 32. */
3121 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3122 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3124 c.rect.width = clamp(crop->c.width,
3125 48, b_right - c.rect.left);
3127 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3128 /* Top and height must be a multiple of two. */
3129 c.rect.top = (c.rect.top + 1) & ~1;
3131 c.rect.height = clamp(crop->c.height,
3132 32, b_bottom - c.rect.top);
3133 c.rect.height = (c.rect.height + 1) & ~1;
3135 bttv_crop_calc_limits(&c);
3139 mutex_unlock(&btv->lock);
3143 mutex_lock(&fh->cap.vb_lock);
3145 if (fh->width < c.min_scaled_width) {
3146 fh->width = c.min_scaled_width;
3147 btv->init.width = c.min_scaled_width;
3148 } else if (fh->width > c.max_scaled_width) {
3149 fh->width = c.max_scaled_width;
3150 btv->init.width = c.max_scaled_width;
3153 if (fh->height < c.min_scaled_height) {
3154 fh->height = c.min_scaled_height;
3155 btv->init.height = c.min_scaled_height;
3156 } else if (fh->height > c.max_scaled_height) {
3157 fh->height = c.max_scaled_height;
3158 btv->init.height = c.max_scaled_height;
3161 mutex_unlock(&fh->cap.vb_lock);
3166 static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
3168 if (unlikely(a->index))
3171 strcpy(a->name, "audio");
3175 static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
3177 if (unlikely(a->index))
3183 static ssize_t bttv_read(struct file *file, char __user *data,
3184 size_t count, loff_t *ppos)
3186 struct bttv_fh *fh = file->private_data;
3189 if (fh->btv->errors)
3190 bttv_reinit_bt848(fh->btv);
3191 dprintk("bttv%d: read count=%d type=%s\n",
3192 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3195 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3196 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3197 /* VIDEO_READ in use by another fh,
3198 or VIDEO_STREAM by any fh. */
3201 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3202 file->f_flags & O_NONBLOCK);
3203 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
3205 case V4L2_BUF_TYPE_VBI_CAPTURE:
3206 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3208 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3209 file->f_flags & O_NONBLOCK);
3217 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3219 struct bttv_fh *fh = file->private_data;
3220 struct bttv_buffer *buf;
3221 enum v4l2_field field;
3222 unsigned int rc = POLLERR;
3224 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3225 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3227 return videobuf_poll_stream(file, &fh->vbi, wait);
3230 mutex_lock(&fh->cap.vb_lock);
3231 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3232 /* streaming capture */
3233 if (list_empty(&fh->cap.stream))
3235 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3237 /* read() capture */
3238 if (NULL == fh->cap.read_buf) {
3239 /* need to capture a new frame */
3240 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3242 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
3243 if (NULL == fh->cap.read_buf)
3245 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3246 field = videobuf_next_field(&fh->cap);
3247 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3248 kfree (fh->cap.read_buf);
3249 fh->cap.read_buf = NULL;
3252 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3253 fh->cap.read_off = 0;
3255 buf = (struct bttv_buffer*)fh->cap.read_buf;
3258 poll_wait(file, &buf->vb.done, wait);
3259 if (buf->vb.state == VIDEOBUF_DONE ||
3260 buf->vb.state == VIDEOBUF_ERROR)
3261 rc = POLLIN|POLLRDNORM;
3265 mutex_unlock(&fh->cap.vb_lock);
3269 static int bttv_open(struct file *file)
3271 struct video_device *vdev = video_devdata(file);
3272 struct bttv *btv = video_drvdata(file);
3274 enum v4l2_buf_type type = 0;
3276 dprintk(KERN_DEBUG "bttv: open dev=%s\n", video_device_node_name(vdev));
3278 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
3279 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3280 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
3281 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3287 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3288 btv->c.nr,v4l2_type_names[type]);
3290 /* allocate per filehandle data */
3291 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3294 file->private_data = fh;
3297 * btv is protected by btv->lock mutex, while btv->init and other
3298 * streaming vars are protected by fh->cap.vb_lock. We need to take
3299 * care of both locks to avoid troubles. However, vb_lock is used also
3300 * inside videobuf, without calling buf->lock. So, it is a very bad
3301 * idea to hold both locks at the same time.
3302 * Let's first copy btv->init at fh, holding cap.vb_lock, and then work
3303 * with the rest of init, holding btv->lock.
3305 mutex_lock(&fh->cap.vb_lock);
3307 mutex_unlock(&fh->cap.vb_lock);
3310 fh->ov.setup_ok = 0;
3312 mutex_lock(&btv->lock);
3313 v4l2_prio_open(&btv->prio, &fh->prio);
3315 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3316 &btv->c.pci->dev, &btv->s_lock,
3317 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3318 V4L2_FIELD_INTERLACED,
3319 sizeof(struct bttv_buffer),
3321 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3322 &btv->c.pci->dev, &btv->s_lock,
3323 V4L2_BUF_TYPE_VBI_CAPTURE,
3325 sizeof(struct bttv_buffer),
3327 set_tvnorm(btv,btv->tvnorm);
3328 set_input(btv, btv->input, btv->tvnorm);
3332 /* The V4L2 spec requires one global set of cropping parameters
3333 which only change on request. These are stored in btv->crop[1].
3334 However for compatibility with V4L apps and cropping unaware
3335 V4L2 apps we now reset the cropping parameters as seen through
3336 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3337 will use btv->crop[0], the default cropping parameters for the
3338 current video standard, and VIDIOC_S_FMT will not implicitely
3339 change the cropping parameters until VIDIOC_S_CROP has been
3341 fh->do_crop = !reset_crop; /* module parameter */
3343 /* Likewise there should be one global set of VBI capture
3344 parameters, but for compatibility with V4L apps and earlier
3345 driver versions each fh has its own parameters. */
3346 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3348 bttv_field_count(btv);
3349 mutex_unlock(&btv->lock);
3353 static int bttv_release(struct file *file)
3355 struct bttv_fh *fh = file->private_data;
3356 struct bttv *btv = fh->btv;
3358 mutex_lock(&btv->lock);
3359 /* turn off overlay */
3360 if (check_btres(fh, RESOURCE_OVERLAY))
3361 bttv_switch_overlay(btv,fh,NULL);
3363 /* stop video capture */
3364 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3365 videobuf_streamoff(&fh->cap);
3366 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
3368 if (fh->cap.read_buf) {
3369 buffer_release(&fh->cap,fh->cap.read_buf);
3370 kfree(fh->cap.read_buf);
3372 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3373 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
3376 /* stop vbi capture */
3377 if (check_btres(fh, RESOURCE_VBI)) {
3378 videobuf_stop(&fh->vbi);
3379 free_btres_lock(btv,fh,RESOURCE_VBI);
3385 * videobuf uses cap.vb_lock - we should avoid holding btv->lock,
3386 * otherwise we may have dead lock conditions
3388 mutex_unlock(&btv->lock);
3389 videobuf_mmap_free(&fh->cap);
3390 videobuf_mmap_free(&fh->vbi);
3391 mutex_lock(&btv->lock);
3392 v4l2_prio_close(&btv->prio, fh->prio);
3393 file->private_data = NULL;
3397 bttv_field_count(btv);
3401 mutex_unlock(&btv->lock);
3407 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3409 struct bttv_fh *fh = file->private_data;
3411 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3412 fh->btv->c.nr, v4l2_type_names[fh->type],
3413 vma->vm_start, vma->vm_end - vma->vm_start);
3414 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3417 static const struct v4l2_file_operations bttv_fops =
3419 .owner = THIS_MODULE,
3421 .release = bttv_release,
3422 .unlocked_ioctl = video_ioctl2,
3428 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3429 .vidioc_querycap = bttv_querycap,
3430 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3431 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3432 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3433 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3434 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3435 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3436 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3437 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
3438 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3439 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3440 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
3441 .vidioc_g_audio = bttv_g_audio,
3442 .vidioc_s_audio = bttv_s_audio,
3443 .vidioc_cropcap = bttv_cropcap,
3444 .vidioc_reqbufs = bttv_reqbufs,
3445 .vidioc_querybuf = bttv_querybuf,
3446 .vidioc_qbuf = bttv_qbuf,
3447 .vidioc_dqbuf = bttv_dqbuf,
3448 .vidioc_s_std = bttv_s_std,
3449 .vidioc_enum_input = bttv_enum_input,
3450 .vidioc_g_input = bttv_g_input,
3451 .vidioc_s_input = bttv_s_input,
3452 .vidioc_queryctrl = bttv_queryctrl,
3453 .vidioc_g_ctrl = bttv_g_ctrl,
3454 .vidioc_s_ctrl = bttv_s_ctrl,
3455 .vidioc_streamon = bttv_streamon,
3456 .vidioc_streamoff = bttv_streamoff,
3457 .vidioc_g_tuner = bttv_g_tuner,
3458 .vidioc_s_tuner = bttv_s_tuner,
3459 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3460 .vidiocgmbuf = vidiocgmbuf,
3462 .vidioc_g_crop = bttv_g_crop,
3463 .vidioc_s_crop = bttv_s_crop,
3464 .vidioc_g_fbuf = bttv_g_fbuf,
3465 .vidioc_s_fbuf = bttv_s_fbuf,
3466 .vidioc_overlay = bttv_overlay,
3467 .vidioc_g_priority = bttv_g_priority,
3468 .vidioc_s_priority = bttv_s_priority,
3469 .vidioc_g_parm = bttv_g_parm,
3470 .vidioc_g_frequency = bttv_g_frequency,
3471 .vidioc_s_frequency = bttv_s_frequency,
3472 .vidioc_log_status = bttv_log_status,
3473 .vidioc_querystd = bttv_querystd,
3474 #ifdef CONFIG_VIDEO_ADV_DEBUG
3475 .vidioc_g_register = bttv_g_register,
3476 .vidioc_s_register = bttv_s_register,
3480 static struct video_device bttv_video_template = {
3482 .ioctl_ops = &bttv_ioctl_ops,
3483 .tvnorms = BTTV_NORMS,
3484 .current_norm = V4L2_STD_PAL,
3487 /* ----------------------------------------------------------------------- */
3488 /* radio interface */
3490 static int radio_open(struct file *file)
3492 struct video_device *vdev = video_devdata(file);
3493 struct bttv *btv = video_drvdata(file);
3496 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev));
3498 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3500 /* allocate per filehandle data */
3501 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3504 file->private_data = fh;
3505 mutex_lock(&fh->cap.vb_lock);
3507 mutex_unlock(&fh->cap.vb_lock);
3509 mutex_lock(&btv->lock);
3510 v4l2_prio_open(&btv->prio, &fh->prio);
3514 bttv_call_all(btv, tuner, s_radio);
3515 audio_input(btv,TVAUDIO_INPUT_RADIO);
3517 mutex_unlock(&btv->lock);
3521 static int radio_release(struct file *file)
3523 struct bttv_fh *fh = file->private_data;
3524 struct bttv *btv = fh->btv;
3525 struct rds_command cmd;
3527 mutex_lock(&btv->lock);
3528 v4l2_prio_close(&btv->prio, fh->prio);
3529 file->private_data = NULL;
3534 bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
3535 mutex_unlock(&btv->lock);
3540 static int radio_querycap(struct file *file, void *priv,
3541 struct v4l2_capability *cap)
3543 struct bttv_fh *fh = priv;
3544 struct bttv *btv = fh->btv;
3546 strcpy(cap->driver, "bttv");
3547 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3548 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3549 cap->version = BTTV_VERSION_CODE;
3550 cap->capabilities = V4L2_CAP_TUNER;
3555 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3557 struct bttv_fh *fh = priv;
3558 struct bttv *btv = fh->btv;
3560 if (btv->tuner_type == TUNER_ABSENT)
3564 mutex_lock(&btv->lock);
3565 strcpy(t->name, "Radio");
3566 t->type = V4L2_TUNER_RADIO;
3568 bttv_call_all(btv, tuner, g_tuner, t);
3570 if (btv->audio_mode_gpio)
3571 btv->audio_mode_gpio(btv, t, 0);
3573 mutex_unlock(&btv->lock);
3578 static int radio_enum_input(struct file *file, void *priv,
3579 struct v4l2_input *i)
3584 strcpy(i->name, "Radio");
3585 i->type = V4L2_INPUT_TYPE_TUNER;
3590 static int radio_g_audio(struct file *file, void *priv,
3591 struct v4l2_audio *a)
3593 if (unlikely(a->index))
3596 strcpy(a->name, "Radio");
3601 static int radio_s_tuner(struct file *file, void *priv,
3602 struct v4l2_tuner *t)
3604 struct bttv_fh *fh = priv;
3605 struct bttv *btv = fh->btv;
3610 bttv_call_all(btv, tuner, g_tuner, t);
3614 static int radio_s_audio(struct file *file, void *priv,
3615 struct v4l2_audio *a)
3617 if (unlikely(a->index))
3623 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3631 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3636 static int radio_queryctrl(struct file *file, void *priv,
3637 struct v4l2_queryctrl *c)
3639 const struct v4l2_queryctrl *ctrl;
3641 if (c->id < V4L2_CID_BASE ||
3642 c->id >= V4L2_CID_LASTP1)
3645 if (c->id == V4L2_CID_AUDIO_MUTE) {
3646 ctrl = ctrl_by_id(c->id);
3654 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3660 static ssize_t radio_read(struct file *file, char __user *data,
3661 size_t count, loff_t *ppos)
3663 struct bttv_fh *fh = file->private_data;
3664 struct bttv *btv = fh->btv;
3665 struct rds_command cmd;
3666 cmd.block_count = count/3;
3668 cmd.instance = file;
3669 cmd.result = -ENODEV;
3671 bttv_call_all(btv, core, ioctl, RDS_CMD_READ, &cmd);
3676 static unsigned int radio_poll(struct file *file, poll_table *wait)
3678 struct bttv_fh *fh = file->private_data;
3679 struct bttv *btv = fh->btv;
3680 struct rds_command cmd;
3681 cmd.instance = file;
3682 cmd.event_list = wait;
3683 cmd.result = -ENODEV;
3684 bttv_call_all(btv, core, ioctl, RDS_CMD_POLL, &cmd);
3689 static const struct v4l2_file_operations radio_fops =
3691 .owner = THIS_MODULE,
3694 .release = radio_release,
3695 .ioctl = video_ioctl2,
3699 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3700 .vidioc_querycap = radio_querycap,
3701 .vidioc_g_tuner = radio_g_tuner,
3702 .vidioc_enum_input = radio_enum_input,
3703 .vidioc_g_audio = radio_g_audio,
3704 .vidioc_s_tuner = radio_s_tuner,
3705 .vidioc_s_audio = radio_s_audio,
3706 .vidioc_s_input = radio_s_input,
3707 .vidioc_s_std = radio_s_std,
3708 .vidioc_queryctrl = radio_queryctrl,
3709 .vidioc_g_input = radio_g_input,
3710 .vidioc_g_ctrl = bttv_g_ctrl,
3711 .vidioc_s_ctrl = bttv_s_ctrl,
3712 .vidioc_g_frequency = bttv_g_frequency,
3713 .vidioc_s_frequency = bttv_s_frequency,
3716 static struct video_device radio_template = {
3717 .fops = &radio_fops,
3718 .ioctl_ops = &radio_ioctl_ops,
3721 /* ----------------------------------------------------------------------- */
3722 /* some debug code */
3724 static int bttv_risc_decode(u32 risc)
3726 static char *instr[16] = {
3727 [ BT848_RISC_WRITE >> 28 ] = "write",
3728 [ BT848_RISC_SKIP >> 28 ] = "skip",
3729 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3730 [ BT848_RISC_JUMP >> 28 ] = "jump",
3731 [ BT848_RISC_SYNC >> 28 ] = "sync",
3732 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3733 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3734 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3736 static int incr[16] = {
3737 [ BT848_RISC_WRITE >> 28 ] = 2,
3738 [ BT848_RISC_JUMP >> 28 ] = 2,
3739 [ BT848_RISC_SYNC >> 28 ] = 2,
3740 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3741 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3742 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3744 static char *bits[] = {
3745 "be0", "be1", "be2", "be3/resync",
3746 "set0", "set1", "set2", "set3",
3747 "clr0", "clr1", "clr2", "clr3",
3748 "irq", "res", "eol", "sol",
3752 printk("0x%08x [ %s", risc,
3753 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3754 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3755 if (risc & (1 << (i + 12)))
3756 printk(" %s",bits[i]);
3757 printk(" count=%d ]\n", risc & 0xfff);
3758 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3761 static void bttv_risc_disasm(struct bttv *btv,
3762 struct btcx_riscmem *risc)
3766 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3767 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
3768 for (i = 0; i < (risc->size >> 2); i += n) {
3769 printk("%s: 0x%lx: ", btv->c.v4l2_dev.name,
3770 (unsigned long)(risc->dma + (i<<2)));
3771 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
3772 for (j = 1; j < n; j++)
3773 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3774 btv->c.v4l2_dev.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3776 if (0 == risc->cpu[i])
3781 static void bttv_print_riscaddr(struct bttv *btv)
3783 printk(" main: %08Lx\n",
3784 (unsigned long long)btv->main.dma);
3785 printk(" vbi : o=%08Lx e=%08Lx\n",
3786 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3787 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3788 printk(" cap : o=%08Lx e=%08Lx\n",
3789 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3790 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3791 printk(" scr : o=%08Lx e=%08Lx\n",
3792 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3793 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3794 bttv_risc_disasm(btv, &btv->main);
3797 /* ----------------------------------------------------------------------- */
3800 static char *irq_name[] = {
3801 "FMTCHG", // format change detected (525 vs. 625)
3802 "VSYNC", // vertical sync (new field)
3803 "HSYNC", // horizontal sync
3804 "OFLOW", // chroma/luma AGC overflow
3805 "HLOCK", // horizontal lock changed
3806 "VPRES", // video presence changed
3808 "I2CDONE", // hw irc operation finished
3809 "GPINT", // gpio port triggered irq
3811 "RISCI", // risc instruction triggered irq
3812 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3813 "FTRGT", // pixel data fifo overrun
3814 "FDSR", // fifo data stream resyncronisation
3815 "PPERR", // parity error (data transfer)
3816 "RIPERR", // parity error (read risc instructions)
3817 "PABORT", // pci abort
3818 "OCERR", // risc instruction error
3819 "SCERR", // syncronisation error
3822 static void bttv_print_irqbits(u32 print, u32 mark)
3827 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3828 if (print & (1 << i))
3829 printk(" %s",irq_name[i]);
3830 if (mark & (1 << i))
3835 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3837 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3839 (unsigned long)btv->main.dma,
3840 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3841 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3844 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3845 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3846 "Ok, then this is harmless, don't worry ;)\n",
3850 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3852 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3858 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3860 struct bttv_buffer *item;
3862 memset(set,0,sizeof(*set));
3864 /* capture request ? */
3865 if (!list_empty(&btv->capture)) {
3867 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3868 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3870 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3873 /* capture request for other field ? */
3874 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3875 (item->vb.queue.next != &btv->capture)) {
3876 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3877 /* Mike Isely <isely@pobox.com> - Only check
3878 * and set up the bottom field in the logic
3879 * below. Don't ever do the top field. This
3880 * of course means that if we set up the
3881 * bottom field in the above code that we'll
3882 * actually skip a field. But that's OK.
3883 * Having processed only a single buffer this
3884 * time, then the next time around the first
3885 * available buffer should be for a top field.
3886 * That will then cause us here to set up a
3887 * top then a bottom field in the normal way.
3888 * The alternative to this understanding is
3889 * that we set up the second available buffer
3890 * as a top field, but that's out of order
3891 * since this driver always processes the top
3892 * field first - the effect will be the two
3893 * buffers being returned in the wrong order,
3894 * with the second buffer also being delayed
3895 * by one field time (owing to the fifo nature
3896 * of videobuf). Worse still, we'll be stuck
3897 * doing fields out of order now every time
3898 * until something else causes a field to be
3899 * dropped. By effectively forcing a field to
3900 * drop this way then we always get back into
3901 * sync within a single frame time. (Out of
3902 * order fields can screw up deinterlacing
3904 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3905 if (NULL == set->bottom &&
3906 V4L2_FIELD_BOTTOM == item->vb.field) {
3909 if (NULL != set->top && NULL != set->bottom)
3915 /* screen overlay ? */
3916 if (NULL != btv->screen) {
3917 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3918 if (NULL == set->top && NULL == set->bottom) {
3919 set->top = btv->screen;
3920 set->bottom = btv->screen;
3923 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3925 set->top = btv->screen;
3927 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3928 NULL == set->bottom) {
3929 set->bottom = btv->screen;
3934 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3935 btv->c.nr,set->top, set->bottom,
3936 btv->screen,set->frame_irq,set->top_irq);
3941 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3942 struct bttv_buffer_set *curr, unsigned int state)
3946 do_gettimeofday(&ts);
3948 if (wakeup->top == wakeup->bottom) {
3949 if (NULL != wakeup->top && curr->top != wakeup->top) {
3951 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3952 wakeup->top->vb.ts = ts;
3953 wakeup->top->vb.field_count = btv->field_count;
3954 wakeup->top->vb.state = state;
3955 wake_up(&wakeup->top->vb.done);
3958 if (NULL != wakeup->top && curr->top != wakeup->top) {
3960 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3961 wakeup->top->vb.ts = ts;
3962 wakeup->top->vb.field_count = btv->field_count;
3963 wakeup->top->vb.state = state;
3964 wake_up(&wakeup->top->vb.done);
3966 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3968 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3969 wakeup->bottom->vb.ts = ts;
3970 wakeup->bottom->vb.field_count = btv->field_count;
3971 wakeup->bottom->vb.state = state;
3972 wake_up(&wakeup->bottom->vb.done);
3978 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3986 do_gettimeofday(&ts);
3988 wakeup->vb.field_count = btv->field_count;
3989 wakeup->vb.state = state;
3990 wake_up(&wakeup->vb.done);
3993 static void bttv_irq_timeout(unsigned long data)
3995 struct bttv *btv = (struct bttv *)data;
3996 struct bttv_buffer_set old,new;
3997 struct bttv_buffer *ovbi;
3998 struct bttv_buffer *item;
3999 unsigned long flags;
4002 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
4003 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
4004 btread(BT848_RISC_COUNT));
4005 bttv_print_irqbits(btread(BT848_INT_STAT),0);
4009 spin_lock_irqsave(&btv->s_lock,flags);
4011 /* deactivate stuff */
4012 memset(&new,0,sizeof(new));
4018 bttv_buffer_activate_video(btv, &new);
4019 bttv_buffer_activate_vbi(btv, NULL);
4020 bttv_set_dma(btv, 0);
4023 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
4024 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
4026 /* cancel all outstanding capture / vbi requests */
4027 while (!list_empty(&btv->capture)) {
4028 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4029 list_del(&item->vb.queue);
4030 item->vb.state = VIDEOBUF_ERROR;
4031 wake_up(&item->vb.done);
4033 while (!list_empty(&btv->vcapture)) {
4034 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4035 list_del(&item->vb.queue);
4036 item->vb.state = VIDEOBUF_ERROR;
4037 wake_up(&item->vb.done);
4041 spin_unlock_irqrestore(&btv->s_lock,flags);
4045 bttv_irq_wakeup_top(struct bttv *btv)
4047 struct bttv_buffer *wakeup = btv->curr.top;
4052 spin_lock(&btv->s_lock);
4053 btv->curr.top_irq = 0;
4054 btv->curr.top = NULL;
4055 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
4057 do_gettimeofday(&wakeup->vb.ts);
4058 wakeup->vb.field_count = btv->field_count;
4059 wakeup->vb.state = VIDEOBUF_DONE;
4060 wake_up(&wakeup->vb.done);
4061 spin_unlock(&btv->s_lock);
4064 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
4068 if (rc > risc->dma + risc->size)
4074 bttv_irq_switch_video(struct bttv *btv)
4076 struct bttv_buffer_set new;
4077 struct bttv_buffer_set old;
4080 spin_lock(&btv->s_lock);
4082 /* new buffer set */
4083 bttv_irq_next_video(btv, &new);
4084 rc = btread(BT848_RISC_COUNT);
4085 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4086 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4089 bttv_irq_debug_low_latency(btv, rc);
4090 spin_unlock(&btv->s_lock);
4097 btv->loop_irq &= ~1;
4098 bttv_buffer_activate_video(btv, &new);
4099 bttv_set_dma(btv, 0);
4102 if (UNSET != btv->new_input) {
4103 video_mux(btv,btv->new_input);
4104 btv->new_input = UNSET;
4107 /* wake up finished buffers */
4108 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
4109 spin_unlock(&btv->s_lock);
4113 bttv_irq_switch_vbi(struct bttv *btv)
4115 struct bttv_buffer *new = NULL;
4116 struct bttv_buffer *old;
4119 spin_lock(&btv->s_lock);
4121 if (!list_empty(&btv->vcapture))
4122 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4125 rc = btread(BT848_RISC_COUNT);
4126 if (NULL != old && (is_active(&old->top, rc) ||
4127 is_active(&old->bottom, rc))) {
4130 bttv_irq_debug_low_latency(btv, rc);
4131 spin_unlock(&btv->s_lock);
4137 btv->loop_irq &= ~4;
4138 bttv_buffer_activate_vbi(btv, new);
4139 bttv_set_dma(btv, 0);
4141 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
4142 spin_unlock(&btv->s_lock);
4145 static irqreturn_t bttv_irq(int irq, void *dev_id)
4153 btv=(struct bttv *)dev_id;
4155 if (btv->custom_irq)
4156 handled = btv->custom_irq(btv);
4160 /* get/clear interrupt status bits */
4161 stat=btread(BT848_INT_STAT);
4162 astat=stat&btread(BT848_INT_MASK);
4166 btwrite(stat,BT848_INT_STAT);
4168 /* get device status bits */
4169 dstat=btread(BT848_DSTATUS);
4172 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4173 "riscs=%x, riscc=%08x, ",
4174 btv->c.nr, count, btv->field_count,
4175 stat>>28, btread(BT848_RISC_COUNT));
4176 bttv_print_irqbits(stat,astat);
4177 if (stat & BT848_INT_HLOCK)
4178 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4180 if (stat & BT848_INT_VPRES)
4181 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4183 if (stat & BT848_INT_FMTCHG)
4184 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4189 if (astat&BT848_INT_VSYNC)
4192 if ((astat & BT848_INT_GPINT) && btv->remote) {
4193 wake_up(&btv->gpioq);
4194 bttv_input_irq(btv);
4197 if (astat & BT848_INT_I2CDONE) {
4198 btv->i2c_done = stat;
4199 wake_up(&btv->i2c_queue);
4202 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
4203 bttv_irq_switch_vbi(btv);
4205 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
4206 bttv_irq_wakeup_top(btv);
4208 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
4209 bttv_irq_switch_video(btv);
4211 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
4212 audio_mute(btv, btv->mute); /* trigger automute */
4214 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4215 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4216 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4217 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4218 btread(BT848_RISC_COUNT));
4219 bttv_print_irqbits(stat,astat);
4222 bttv_print_riscaddr(btv);
4224 if (fdsr && astat & BT848_INT_FDSR) {
4225 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4226 btv->c.nr,btread(BT848_RISC_COUNT));
4228 bttv_print_riscaddr(btv);
4234 if (count > 8 || !(astat & BT848_INT_GPINT)) {
4235 btwrite(0, BT848_INT_MASK);
4238 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4241 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4243 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4247 bttv_print_irqbits(stat,astat);
4255 return IRQ_RETVAL(handled);
4259 /* ----------------------------------------------------------------------- */
4260 /* initialitation */
4262 static struct video_device *vdev_init(struct bttv *btv,
4263 const struct video_device *template,
4264 const char *type_name)
4266 struct video_device *vfd;
4268 vfd = video_device_alloc();
4272 vfd->v4l2_dev = &btv->c.v4l2_dev;
4273 vfd->release = video_device_release;
4274 vfd->debug = bttv_debug;
4275 video_set_drvdata(vfd, btv);
4276 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4277 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4278 type_name, bttv_tvcards[btv->c.type].name);
4282 static void bttv_unregister_video(struct bttv *btv)
4284 if (btv->video_dev) {
4285 if (video_is_registered(btv->video_dev))
4286 video_unregister_device(btv->video_dev);
4288 video_device_release(btv->video_dev);
4289 btv->video_dev = NULL;
4292 if (video_is_registered(btv->vbi_dev))
4293 video_unregister_device(btv->vbi_dev);
4295 video_device_release(btv->vbi_dev);
4296 btv->vbi_dev = NULL;
4298 if (btv->radio_dev) {
4299 if (video_is_registered(btv->radio_dev))
4300 video_unregister_device(btv->radio_dev);
4302 video_device_release(btv->radio_dev);
4303 btv->radio_dev = NULL;
4307 /* register video4linux devices */
4308 static int __devinit bttv_register_video(struct bttv *btv)
4311 printk("bttv: Overlay support disabled.\n");
4314 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4316 if (NULL == btv->video_dev)
4318 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4319 video_nr[btv->c.nr]) < 0)
4321 printk(KERN_INFO "bttv%d: registered device %s\n",
4322 btv->c.nr, video_device_node_name(btv->video_dev));
4323 if (device_create_file(&btv->video_dev->dev,
4324 &dev_attr_card)<0) {
4325 printk(KERN_ERR "bttv%d: device_create_file 'card' "
4326 "failed\n", btv->c.nr);
4331 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
4333 if (NULL == btv->vbi_dev)
4335 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4336 vbi_nr[btv->c.nr]) < 0)
4338 printk(KERN_INFO "bttv%d: registered device %s\n",
4339 btv->c.nr, video_device_node_name(btv->vbi_dev));
4341 if (!btv->has_radio)
4344 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4345 if (NULL == btv->radio_dev)
4347 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4348 radio_nr[btv->c.nr]) < 0)
4350 printk(KERN_INFO "bttv%d: registered device %s\n",
4351 btv->c.nr, video_device_node_name(btv->radio_dev));
4357 bttv_unregister_video(btv);
4362 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4363 /* response on cards with no firmware is not enabled by OF */
4364 static void pci_set_command(struct pci_dev *dev)
4366 #if defined(__powerpc__)
4369 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4370 cmd = (cmd | PCI_COMMAND_MEMORY );
4371 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4375 static int __devinit bttv_probe(struct pci_dev *dev,
4376 const struct pci_device_id *pci_id)
4382 if (bttv_num == BTTV_MAX)
4384 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
4385 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
4387 printk(KERN_ERR "bttv: out of memory.\n");
4390 btv->c.nr = bttv_num;
4391 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4392 "bttv%d", btv->c.nr);
4394 /* initialize structs / fill in defaults */
4395 mutex_init(&btv->lock);
4396 spin_lock_init(&btv->s_lock);
4397 spin_lock_init(&btv->gpio_lock);
4398 init_waitqueue_head(&btv->gpioq);
4399 init_waitqueue_head(&btv->i2c_queue);
4400 INIT_LIST_HEAD(&btv->c.subs);
4401 INIT_LIST_HEAD(&btv->capture);
4402 INIT_LIST_HEAD(&btv->vcapture);
4403 v4l2_prio_init(&btv->prio);
4405 init_timer(&btv->timeout);
4406 btv->timeout.function = bttv_irq_timeout;
4407 btv->timeout.data = (unsigned long)btv;
4410 btv->tuner_type = UNSET;
4411 btv->new_input = UNSET;
4412 btv->has_radio=radio[btv->c.nr];
4414 /* pci stuff (init, get irq/mmio, ... */
4416 btv->id = dev->device;
4417 if (pci_enable_device(dev)) {
4418 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4422 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
4423 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4427 if (!request_mem_region(pci_resource_start(dev,0),
4428 pci_resource_len(dev,0),
4429 btv->c.v4l2_dev.name)) {
4430 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4432 (unsigned long long)pci_resource_start(dev,0));
4435 pci_set_master(dev);
4436 pci_set_command(dev);
4438 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4440 printk(KERN_WARNING "bttv%d: v4l2_device_register() failed\n", btv->c.nr);
4444 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4445 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4446 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4447 bttv_num,btv->id, btv->revision, pci_name(dev));
4448 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4449 btv->c.pci->irq, lat,
4450 (unsigned long long)pci_resource_start(dev,0));
4453 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4454 if (NULL == btv->bt848_mmio) {
4455 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4463 /* disable irqs, register irq handler */
4464 btwrite(0, BT848_INT_MASK);
4465 result = request_irq(btv->c.pci->irq, bttv_irq,
4466 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
4468 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4469 bttv_num,btv->c.pci->irq);
4473 if (0 != bttv_handle_chipset(btv)) {
4478 /* init options from insmod args */
4479 btv->opt_combfilter = combfilter;
4480 btv->opt_lumafilter = lumafilter;
4481 btv->opt_automute = automute;
4482 btv->opt_chroma_agc = chroma_agc;
4483 btv->opt_adc_crush = adc_crush;
4484 btv->opt_vcr_hack = vcr_hack;
4485 btv->opt_whitecrush_upper = whitecrush_upper;
4486 btv->opt_whitecrush_lower = whitecrush_lower;
4487 btv->opt_uv_ratio = uv_ratio;
4488 btv->opt_full_luma_range = full_luma_range;
4489 btv->opt_coring = coring;
4491 /* fill struct bttv with some useful defaults */
4492 btv->init.btv = btv;
4493 btv->init.ov.w.width = 320;
4494 btv->init.ov.w.height = 240;
4495 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4496 btv->init.width = 320;
4497 btv->init.height = 240;
4500 /* initialize hardware */
4502 bttv_gpio_tracking(btv,"pre-init");
4504 bttv_risc_init_main(btv);
4508 btwrite(0x00, BT848_GPIO_REG_INP);
4509 btwrite(0x00, BT848_GPIO_OUT_EN);
4511 bttv_gpio_tracking(btv,"init");
4513 /* needs to be done before i2c is registered */
4514 bttv_init_card1(btv);
4516 /* register i2c + gpio */
4519 /* some card-specific stuff (needs working i2c) */
4520 bttv_init_card2(btv);
4521 bttv_init_tuner(btv);
4524 /* register video4linux + input */
4525 if (!bttv_tvcards[btv->c.type].no_video) {
4526 bttv_register_video(btv);
4527 bt848_bright(btv,32768);
4528 bt848_contrast(btv,32768);
4529 bt848_hue(btv,32768);
4530 bt848_sat(btv,32768);
4532 set_input(btv, 0, btv->tvnorm);
4533 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4534 btv->crop[1] = btv->crop[0]; /* current = default */
4535 disclaim_vbi_lines(btv);
4536 disclaim_video_lines(btv);
4539 /* add subdevices and autoload dvb-bt8xx if needed */
4540 if (bttv_tvcards[btv->c.type].has_dvb) {
4541 bttv_sub_add_device(&btv->c, "dvb");
4542 request_modules(btv);
4546 init_bttv_i2c_ir(btv);
4547 bttv_input_init(btv);
4550 /* everything is fine */
4555 free_irq(btv->c.pci->irq,btv);
4558 v4l2_device_unregister(&btv->c.v4l2_dev);
4561 if (btv->bt848_mmio)
4562 iounmap(btv->bt848_mmio);
4563 release_mem_region(pci_resource_start(btv->c.pci,0),
4564 pci_resource_len(btv->c.pci,0));
4568 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4570 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4571 struct bttv *btv = to_bttv(v4l2_dev);
4574 printk("bttv%d: unloading\n",btv->c.nr);
4576 /* shutdown everything (DMA+IRQs) */
4577 btand(~15, BT848_GPIO_DMA_CTL);
4578 btwrite(0, BT848_INT_MASK);
4579 btwrite(~0x0, BT848_INT_STAT);
4580 btwrite(0x0, BT848_GPIO_OUT_EN);
4582 bttv_gpio_tracking(btv,"cleanup");
4584 /* tell gpio modules we are leaving ... */
4586 wake_up(&btv->gpioq);
4587 bttv_input_fini(btv);
4588 bttv_sub_del_devices(&btv->c);
4590 /* unregister i2c_bus + input */
4593 /* unregister video4linux */
4594 bttv_unregister_video(btv);
4596 /* free allocated memory */
4597 btcx_riscmem_free(btv->c.pci,&btv->main);
4599 /* free ressources */
4600 free_irq(btv->c.pci->irq,btv);
4601 iounmap(btv->bt848_mmio);
4602 release_mem_region(pci_resource_start(btv->c.pci,0),
4603 pci_resource_len(btv->c.pci,0));
4605 v4l2_device_unregister(&btv->c.v4l2_dev);
4606 bttvs[btv->c.nr] = NULL;
4613 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4615 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4616 struct bttv *btv = to_bttv(v4l2_dev);
4617 struct bttv_buffer_set idle;
4618 unsigned long flags;
4620 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4622 /* stop dma + irqs */
4623 spin_lock_irqsave(&btv->s_lock,flags);
4624 memset(&idle, 0, sizeof(idle));
4625 btv->state.video = btv->curr;
4626 btv->state.vbi = btv->cvbi;
4627 btv->state.loop_irq = btv->loop_irq;
4630 bttv_buffer_activate_video(btv, &idle);
4631 bttv_buffer_activate_vbi(btv, NULL);
4632 bttv_set_dma(btv, 0);
4633 btwrite(0, BT848_INT_MASK);
4634 spin_unlock_irqrestore(&btv->s_lock,flags);
4636 /* save bt878 state */
4637 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4638 btv->state.gpio_data = gpio_read();
4640 /* save pci state */
4641 pci_save_state(pci_dev);
4642 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4643 pci_disable_device(pci_dev);
4644 btv->state.disabled = 1;
4649 static int bttv_resume(struct pci_dev *pci_dev)
4651 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4652 struct bttv *btv = to_bttv(v4l2_dev);
4653 unsigned long flags;
4656 dprintk("bttv%d: resume\n", btv->c.nr);
4658 /* restore pci state */
4659 if (btv->state.disabled) {
4660 err=pci_enable_device(pci_dev);
4662 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4666 btv->state.disabled = 0;
4668 err=pci_set_power_state(pci_dev, PCI_D0);
4670 pci_disable_device(pci_dev);
4671 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4673 btv->state.disabled = 1;
4677 pci_restore_state(pci_dev);
4679 /* restore bt878 state */
4680 bttv_reinit_bt848(btv);
4681 gpio_inout(0xffffff, btv->state.gpio_enable);
4682 gpio_write(btv->state.gpio_data);
4685 spin_lock_irqsave(&btv->s_lock,flags);
4686 btv->curr = btv->state.video;
4687 btv->cvbi = btv->state.vbi;
4688 btv->loop_irq = btv->state.loop_irq;
4689 bttv_buffer_activate_video(btv, &btv->curr);
4690 bttv_buffer_activate_vbi(btv, btv->cvbi);
4691 bttv_set_dma(btv, 0);
4692 spin_unlock_irqrestore(&btv->s_lock,flags);
4697 static struct pci_device_id bttv_pci_tbl[] = {
4698 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4699 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4700 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4701 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
4705 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4707 static struct pci_driver bttv_pci_driver = {
4709 .id_table = bttv_pci_tbl,
4710 .probe = bttv_probe,
4711 .remove = __devexit_p(bttv_remove),
4713 .suspend = bttv_suspend,
4714 .resume = bttv_resume,
4718 static int __init bttv_init_module(void)
4724 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4725 (BTTV_VERSION_CODE >> 16) & 0xff,
4726 (BTTV_VERSION_CODE >> 8) & 0xff,
4727 BTTV_VERSION_CODE & 0xff);
4729 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4730 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4732 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4734 if (gbufsize > BTTV_MAX_FBUF)
4735 gbufsize = BTTV_MAX_FBUF;
4736 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4738 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4739 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4741 bttv_check_chipset();
4743 ret = bus_register(&bttv_sub_bus_type);
4745 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4748 ret = pci_register_driver(&bttv_pci_driver);
4750 bus_unregister(&bttv_sub_bus_type);
4755 static void __exit bttv_cleanup_module(void)
4757 pci_unregister_driver(&bttv_pci_driver);
4758 bus_unregister(&bttv_sub_bus_type);
4761 module_init(bttv_init_module);
4762 module_exit(bttv_cleanup_module);