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/errno.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/interrupt.h>
45 #include <linux/kdev_t.h>
47 #include <media/v4l2-common.h>
48 #include <media/tvaudio.h>
49 #include <media/msp3400.h>
51 #include <linux/dma-mapping.h>
54 #include <asm/byteorder.h>
56 #include <media/rds.h>
59 unsigned int bttv_num; /* number of Bt848s in use */
60 struct bttv bttvs[BTTV_MAX];
62 unsigned int bttv_debug;
63 unsigned int bttv_verbose = 1;
64 unsigned int bttv_gpio;
66 /* config variables */
68 static unsigned int bigendian=1;
70 static unsigned int bigendian;
72 static unsigned int radio[BTTV_MAX];
73 static unsigned int irq_debug;
74 static unsigned int gbuffers = 8;
75 static unsigned int gbufsize = 0x208000;
76 static unsigned int reset_crop = 1;
78 static int video_nr = -1;
79 static int radio_nr = -1;
80 static int vbi_nr = -1;
81 static int debug_latency;
83 static unsigned int fdsr;
86 static unsigned int combfilter;
87 static unsigned int lumafilter;
88 static unsigned int automute = 1;
89 static unsigned int chroma_agc;
90 static unsigned int adc_crush = 1;
91 static unsigned int whitecrush_upper = 0xCF;
92 static unsigned int whitecrush_lower = 0x7F;
93 static unsigned int vcr_hack;
94 static unsigned int irq_iswitch;
95 static unsigned int uv_ratio = 50;
96 static unsigned int full_luma_range;
97 static unsigned int coring;
98 extern int no_overlay;
100 /* API features (turn on/off stuff for testing) */
101 static unsigned int v4l2 = 1;
104 module_param(bttv_verbose, int, 0644);
105 module_param(bttv_gpio, int, 0644);
106 module_param(bttv_debug, int, 0644);
107 module_param(irq_debug, int, 0644);
108 module_param(debug_latency, int, 0644);
110 module_param(fdsr, int, 0444);
111 module_param(video_nr, int, 0444);
112 module_param(radio_nr, int, 0444);
113 module_param(vbi_nr, 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);
135 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
136 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
137 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
138 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
139 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
140 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
141 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
142 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
143 MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
144 "is 1 (yes) for compatibility with older applications");
145 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
146 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
147 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
148 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
149 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
150 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
151 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
152 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
153 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
154 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
156 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
157 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
158 MODULE_LICENSE("GPL");
160 /* ----------------------------------------------------------------------- */
163 static ssize_t show_card(struct device *cd,
164 struct device_attribute *attr, char *buf)
166 struct video_device *vfd = container_of(cd, struct video_device, class_dev);
167 struct bttv *btv = dev_get_drvdata(vfd->dev);
168 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
170 static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
172 /* ----------------------------------------------------------------------- */
173 /* dvb auto-load setup */
174 #if defined(CONFIG_MODULES) && defined(MODULE)
175 static void request_module_async(struct work_struct *work)
177 request_module("dvb-bt8xx");
180 static void request_modules(struct bttv *dev)
182 INIT_WORK(&dev->request_module_wk, request_module_async);
183 schedule_work(&dev->request_module_wk);
186 #define request_modules(dev)
187 #endif /* CONFIG_MODULES */
190 /* ----------------------------------------------------------------------- */
193 /* special timing tables from conexant... */
194 static u8 SRAM_Table[][60] =
196 /* PAL digital input over GPIO[7:0] */
198 45, // 45 bytes following
199 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
200 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
201 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
202 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
203 0x37,0x00,0xAF,0x21,0x00
205 /* NTSC digital input over GPIO[7:0] */
207 51, // 51 bytes following
208 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
209 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
210 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
211 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
212 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
215 // TGB_NTSC392 // quartzsight
216 // This table has been modified to be used for Fusion Rev D
218 0x2A, // size of table = 42
219 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
220 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
221 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
222 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
227 /* minhdelayx1 first video pixel we can capture on a line and
228 hdelayx1 start of active video, both relative to rising edge of
229 /HRESET pulse (0H) in 1 / fCLKx1.
230 swidth width of active video and
231 totalwidth total line width, both in 1 / fCLKx1.
232 sqwidth total line width in square pixels.
233 vdelay start of active video in 2 * field lines relative to
234 trailing edge of /VRESET pulse (VDELAY register).
235 sheight height of active video in 2 * field lines.
236 videostart0 ITU-R frame line number of the line corresponding
237 to vdelay in the first field. */
238 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
239 vdelay, sheight, videostart0) \
240 .cropcap.bounds.left = minhdelayx1, \
241 /* * 2 because vertically we count field lines times two, */ \
242 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
243 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
244 /* 4 is a safety margin at the end of the line. */ \
245 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
246 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
247 .cropcap.defrect.left = hdelayx1, \
248 .cropcap.defrect.top = (videostart0) * 2, \
249 .cropcap.defrect.width = swidth, \
250 .cropcap.defrect.height = sheight, \
251 .cropcap.pixelaspect.numerator = totalwidth, \
252 .cropcap.pixelaspect.denominator = sqwidth,
254 const struct bttv_tvnorm bttv_tvnorms[] = {
256 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
257 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
259 .v4l2_id = V4L2_STD_PAL,
267 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
268 .scaledtwidth = 1135,
272 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
274 /* ITU-R frame line number of the first VBI line
275 we can capture, of the first and second field.
276 The last line is determined by cropcap.bounds. */
277 .vbistart = { 7, 320 },
278 CROPCAP(/* minhdelayx1 */ 68,
280 /* Should be (768 * 1135 + 944 / 2) / 944.
281 cropcap.defrect is used for image width
282 checks, so we keep the old value 924. */
284 /* totalwidth */ 1135,
288 /* videostart0 */ 23)
289 /* bt878 (and bt848?) can capture another
290 line below active video. */
291 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
293 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
301 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
306 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
308 .vbistart = { 10, 273 },
309 CROPCAP(/* minhdelayx1 */ 68,
311 /* Should be (640 * 910 + 780 / 2) / 780? */
313 /* totalwidth */ 910,
317 /* videostart0 */ 23)
319 .v4l2_id = V4L2_STD_SECAM,
327 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
328 .scaledtwidth = 1135,
333 .sram = 0, /* like PAL, correct? */
334 .vbistart = { 7, 320 },
335 CROPCAP(/* minhdelayx1 */ 68,
338 /* totalwidth */ 1135,
342 /* videostart0 */ 23)
344 .v4l2_id = V4L2_STD_PAL_Nc,
352 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
359 .vbistart = { 7, 320 },
360 CROPCAP(/* minhdelayx1 */ 68,
362 /* swidth */ (640 * 910 + 780 / 2) / 780,
363 /* totalwidth */ 910,
367 /* videostart0 */ 23)
369 .v4l2_id = V4L2_STD_PAL_M,
377 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
384 .vbistart = { 10, 273 },
385 CROPCAP(/* minhdelayx1 */ 68,
387 /* swidth */ (640 * 910 + 780 / 2) / 780,
388 /* totalwidth */ 910,
392 /* videostart0 */ 23)
394 .v4l2_id = V4L2_STD_PAL_N,
402 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
409 .vbistart = { 7, 320 },
410 CROPCAP(/* minhdelayx1 */ 68,
412 /* swidth */ (768 * 1135 + 944 / 2) / 944,
413 /* totalwidth */ 1135,
417 /* videostart0 */ 23)
419 .v4l2_id = V4L2_STD_NTSC_M_JP,
427 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
434 .vbistart = { 10, 273 },
435 CROPCAP(/* minhdelayx1 */ 68,
437 /* swidth */ (640 * 910 + 780 / 2) / 780,
438 /* totalwidth */ 910,
442 /* videostart0 */ 23)
444 /* that one hopefully works with the strange timing
445 * which video recorders produce when playing a NTSC
446 * tape on a PAL TV ... */
447 .v4l2_id = V4L2_STD_PAL_60,
455 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
456 .scaledtwidth = 1135,
463 .vbistart = { 10, 273 },
464 CROPCAP(/* minhdelayx1 */ 68,
467 /* totalwidth */ 1135,
471 /* videostart0 */ 23)
474 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
476 /* ----------------------------------------------------------------------- */
478 packed pixel formats must come first */
479 static const struct bttv_format bttv_formats[] = {
481 .name = "8 bpp, gray",
482 .fourcc = V4L2_PIX_FMT_GREY,
483 .btformat = BT848_COLOR_FMT_Y8,
485 .flags = FORMAT_FLAGS_PACKED,
487 .name = "8 bpp, dithered color",
488 .fourcc = V4L2_PIX_FMT_HI240,
489 .btformat = BT848_COLOR_FMT_RGB8,
491 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
493 .name = "15 bpp RGB, le",
494 .fourcc = V4L2_PIX_FMT_RGB555,
495 .btformat = BT848_COLOR_FMT_RGB15,
497 .flags = FORMAT_FLAGS_PACKED,
499 .name = "15 bpp RGB, be",
500 .fourcc = V4L2_PIX_FMT_RGB555X,
501 .btformat = BT848_COLOR_FMT_RGB15,
502 .btswap = 0x03, /* byteswap */
504 .flags = FORMAT_FLAGS_PACKED,
506 .name = "16 bpp RGB, le",
507 .fourcc = V4L2_PIX_FMT_RGB565,
508 .btformat = BT848_COLOR_FMT_RGB16,
510 .flags = FORMAT_FLAGS_PACKED,
512 .name = "16 bpp RGB, be",
513 .fourcc = V4L2_PIX_FMT_RGB565X,
514 .btformat = BT848_COLOR_FMT_RGB16,
515 .btswap = 0x03, /* byteswap */
517 .flags = FORMAT_FLAGS_PACKED,
519 .name = "24 bpp RGB, le",
520 .fourcc = V4L2_PIX_FMT_BGR24,
521 .btformat = BT848_COLOR_FMT_RGB24,
523 .flags = FORMAT_FLAGS_PACKED,
525 .name = "32 bpp RGB, le",
526 .fourcc = V4L2_PIX_FMT_BGR32,
527 .btformat = BT848_COLOR_FMT_RGB32,
529 .flags = FORMAT_FLAGS_PACKED,
531 .name = "32 bpp RGB, be",
532 .fourcc = V4L2_PIX_FMT_RGB32,
533 .btformat = BT848_COLOR_FMT_RGB32,
534 .btswap = 0x0f, /* byte+word swap */
536 .flags = FORMAT_FLAGS_PACKED,
538 .name = "4:2:2, packed, YUYV",
539 .fourcc = V4L2_PIX_FMT_YUYV,
540 .btformat = BT848_COLOR_FMT_YUY2,
542 .flags = FORMAT_FLAGS_PACKED,
544 .name = "4:2:2, packed, YUYV",
545 .fourcc = V4L2_PIX_FMT_YUYV,
546 .btformat = BT848_COLOR_FMT_YUY2,
548 .flags = FORMAT_FLAGS_PACKED,
550 .name = "4:2:2, packed, UYVY",
551 .fourcc = V4L2_PIX_FMT_UYVY,
552 .btformat = BT848_COLOR_FMT_YUY2,
553 .btswap = 0x03, /* byteswap */
555 .flags = FORMAT_FLAGS_PACKED,
557 .name = "4:2:2, planar, Y-Cb-Cr",
558 .fourcc = V4L2_PIX_FMT_YUV422P,
559 .btformat = BT848_COLOR_FMT_YCrCb422,
561 .flags = FORMAT_FLAGS_PLANAR,
565 .name = "4:2:0, planar, Y-Cb-Cr",
566 .fourcc = V4L2_PIX_FMT_YUV420,
567 .btformat = BT848_COLOR_FMT_YCrCb422,
569 .flags = FORMAT_FLAGS_PLANAR,
573 .name = "4:2:0, planar, Y-Cr-Cb",
574 .fourcc = V4L2_PIX_FMT_YVU420,
575 .btformat = BT848_COLOR_FMT_YCrCb422,
577 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
581 .name = "4:1:1, planar, Y-Cb-Cr",
582 .fourcc = V4L2_PIX_FMT_YUV411P,
583 .btformat = BT848_COLOR_FMT_YCrCb411,
585 .flags = FORMAT_FLAGS_PLANAR,
589 .name = "4:1:0, planar, Y-Cb-Cr",
590 .fourcc = V4L2_PIX_FMT_YUV410,
591 .btformat = BT848_COLOR_FMT_YCrCb411,
593 .flags = FORMAT_FLAGS_PLANAR,
597 .name = "4:1:0, planar, Y-Cr-Cb",
598 .fourcc = V4L2_PIX_FMT_YVU410,
599 .btformat = BT848_COLOR_FMT_YCrCb411,
601 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
605 .name = "raw scanlines",
607 .btformat = BT848_COLOR_FMT_RAW,
609 .flags = FORMAT_FLAGS_RAW,
612 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
614 /* ----------------------------------------------------------------------- */
616 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
617 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
618 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
619 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
620 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
621 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
622 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
623 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
624 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
625 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
626 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
627 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
629 static const struct v4l2_queryctrl no_ctl = {
631 .flags = V4L2_CTRL_FLAG_DISABLED,
633 static const struct v4l2_queryctrl bttv_ctls[] = {
636 .id = V4L2_CID_BRIGHTNESS,
637 .name = "Brightness",
641 .default_value = 32768,
642 .type = V4L2_CTRL_TYPE_INTEGER,
644 .id = V4L2_CID_CONTRAST,
649 .default_value = 32768,
650 .type = V4L2_CTRL_TYPE_INTEGER,
652 .id = V4L2_CID_SATURATION,
653 .name = "Saturation",
657 .default_value = 32768,
658 .type = V4L2_CTRL_TYPE_INTEGER,
665 .default_value = 32768,
666 .type = V4L2_CTRL_TYPE_INTEGER,
670 .id = V4L2_CID_AUDIO_MUTE,
674 .type = V4L2_CTRL_TYPE_BOOLEAN,
676 .id = V4L2_CID_AUDIO_VOLUME,
681 .default_value = 65535,
682 .type = V4L2_CTRL_TYPE_INTEGER,
684 .id = V4L2_CID_AUDIO_BALANCE,
689 .default_value = 32768,
690 .type = V4L2_CTRL_TYPE_INTEGER,
692 .id = V4L2_CID_AUDIO_BASS,
697 .default_value = 32768,
698 .type = V4L2_CTRL_TYPE_INTEGER,
700 .id = V4L2_CID_AUDIO_TREBLE,
705 .default_value = 32768,
706 .type = V4L2_CTRL_TYPE_INTEGER,
708 /* --- private --- */
710 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
711 .name = "chroma agc",
714 .type = V4L2_CTRL_TYPE_BOOLEAN,
716 .id = V4L2_CID_PRIVATE_COMBFILTER,
717 .name = "combfilter",
720 .type = V4L2_CTRL_TYPE_BOOLEAN,
722 .id = V4L2_CID_PRIVATE_AUTOMUTE,
726 .type = V4L2_CTRL_TYPE_BOOLEAN,
728 .id = V4L2_CID_PRIVATE_LUMAFILTER,
729 .name = "luma decimation filter",
732 .type = V4L2_CTRL_TYPE_BOOLEAN,
734 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
738 .type = V4L2_CTRL_TYPE_BOOLEAN,
740 .id = V4L2_CID_PRIVATE_VCR_HACK,
744 .type = V4L2_CTRL_TYPE_BOOLEAN,
746 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
747 .name = "whitecrush upper",
751 .default_value = 0xCF,
752 .type = V4L2_CTRL_TYPE_INTEGER,
754 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
755 .name = "whitecrush lower",
759 .default_value = 0x7F,
760 .type = V4L2_CTRL_TYPE_INTEGER,
762 .id = V4L2_CID_PRIVATE_UV_RATIO,
768 .type = V4L2_CTRL_TYPE_INTEGER,
770 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
771 .name = "full luma range",
774 .type = V4L2_CTRL_TYPE_BOOLEAN,
776 .id = V4L2_CID_PRIVATE_CORING,
782 .type = V4L2_CTRL_TYPE_INTEGER,
788 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
790 /* ----------------------------------------------------------------------- */
791 /* resource management */
794 RESOURCE_ allocated by freed by
796 VIDEO_READ bttv_read 1) bttv_read 2)
798 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
799 VIDIOC_QBUF 1) bttv_release
802 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
803 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
806 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
807 VIDIOC_QBUF 1) bttv_release
808 bttv_read, bttv_poll 1) 4)
810 1) The resource must be allocated when we enter buffer prepare functions
811 and remain allocated while buffers are in the DMA queue.
812 2) This is a single frame read.
813 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
814 RESOURCE_OVERLAY is allocated.
815 4) This is a continuous read, implies VIDIOC_STREAMON.
817 Note this driver permits video input and standard changes regardless if
818 resources are allocated.
821 #define VBI_RESOURCES (RESOURCE_VBI)
822 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
823 RESOURCE_VIDEO_STREAM | \
827 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
829 int xbits; /* mutual exclusive resources */
831 if (fh->resources & bit)
832 /* have it already allocated */
836 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
837 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
840 mutex_lock(&btv->lock);
841 if (btv->resources & xbits) {
842 /* no, someone else uses it */
846 if ((bit & VIDEO_RESOURCES)
847 && 0 == (btv->resources & VIDEO_RESOURCES)) {
848 /* Do crop - use current, don't - use default parameters. */
849 __s32 top = btv->crop[!!fh->do_crop].rect.top;
851 if (btv->vbi_end > top)
854 /* We cannot capture the same line as video and VBI data.
855 Claim scan lines crop[].rect.top to bottom. */
856 btv->crop_start = top;
857 } else if (bit & VBI_RESOURCES) {
858 __s32 end = fh->vbi_fmt.end;
860 if (end > btv->crop_start)
863 /* Claim scan lines above fh->vbi_fmt.end. */
867 /* it's free, grab it */
868 fh->resources |= bit;
869 btv->resources |= bit;
870 mutex_unlock(&btv->lock);
874 mutex_unlock(&btv->lock);
879 int check_btres(struct bttv_fh *fh, int bit)
881 return (fh->resources & bit);
885 int locked_btres(struct bttv *btv, int bit)
887 return (btv->resources & bit);
890 /* Call with btv->lock down. */
892 disclaim_vbi_lines(struct bttv *btv)
897 /* Call with btv->lock down. */
899 disclaim_video_lines(struct bttv *btv)
901 const struct bttv_tvnorm *tvnorm;
904 tvnorm = &bttv_tvnorms[btv->tvnorm];
905 btv->crop_start = tvnorm->cropcap.bounds.top
906 + tvnorm->cropcap.bounds.height;
908 /* VBI capturing ends at VDELAY, start of video capturing, no
909 matter how many lines the VBI RISC program expects. When video
910 capturing is off, it shall no longer "preempt" VBI capturing,
911 so we set VDELAY to maximum. */
912 crop = btread(BT848_E_CROP) | 0xc0;
913 btwrite(crop, BT848_E_CROP);
914 btwrite(0xfe, BT848_E_VDELAY_LO);
915 btwrite(crop, BT848_O_CROP);
916 btwrite(0xfe, BT848_O_VDELAY_LO);
920 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
922 if ((fh->resources & bits) != bits) {
923 /* trying to free ressources not allocated by us ... */
924 printk("bttv: BUG! (btres)\n");
926 mutex_lock(&btv->lock);
927 fh->resources &= ~bits;
928 btv->resources &= ~bits;
930 bits = btv->resources;
932 if (0 == (bits & VIDEO_RESOURCES))
933 disclaim_video_lines(btv);
935 if (0 == (bits & VBI_RESOURCES))
936 disclaim_vbi_lines(btv);
938 mutex_unlock(&btv->lock);
941 /* ----------------------------------------------------------------------- */
942 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
944 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
945 PLL_X = Reference pre-divider (0=1, 1=2)
946 PLL_C = Post divider (0=6, 1=4)
947 PLL_I = Integer input
948 PLL_F = Fractional input
950 F_input = 28.636363 MHz:
951 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
954 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
956 unsigned char fl, fh, fi;
958 /* prevent overflows */
971 btwrite(fl, BT848_PLL_F_LO);
972 btwrite(fh, BT848_PLL_F_HI);
973 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
976 static void set_pll(struct bttv *btv)
980 if (!btv->pll.pll_crystal)
983 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
984 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
988 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
990 if (btv->pll.pll_current == 0)
992 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
993 btv->c.nr,btv->pll.pll_ifreq);
994 btwrite(0x00,BT848_TGCTRL);
995 btwrite(0x00,BT848_PLL_XCI);
996 btv->pll.pll_current = 0;
1000 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
1001 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1002 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1004 for (i=0; i<10; i++) {
1005 /* Let other people run while the PLL stabilizes */
1009 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1010 btwrite(0,BT848_DSTATUS);
1012 btwrite(0x08,BT848_TGCTRL);
1013 btv->pll.pll_current = btv->pll.pll_ofreq;
1014 bttv_printk(" ok\n");
1018 btv->pll.pll_current = -1;
1019 bttv_printk("failed\n");
1023 /* used to switch between the bt848's analog/digital video capture modes */
1024 static void bt848A_set_timing(struct bttv *btv)
1027 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1028 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1030 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
1031 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1032 btv->c.nr,table_idx);
1034 /* timing change...reset timing generator address */
1035 btwrite(0x00, BT848_TGCTRL);
1036 btwrite(0x02, BT848_TGCTRL);
1037 btwrite(0x00, BT848_TGCTRL);
1039 len=SRAM_Table[table_idx][0];
1040 for(i = 1; i <= len; i++)
1041 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1042 btv->pll.pll_ofreq = 27000000;
1045 btwrite(0x11, BT848_TGCTRL);
1046 btwrite(0x41, BT848_DVSIF);
1048 btv->pll.pll_ofreq = fsc;
1050 btwrite(0x0, BT848_DVSIF);
1054 /* ----------------------------------------------------------------------- */
1056 static void bt848_bright(struct bttv *btv, int bright)
1060 // printk("bttv: set bright: %d\n",bright); // DEBUG
1061 btv->bright = bright;
1063 /* We want -128 to 127 we get 0-65535 */
1064 value = (bright >> 8) - 128;
1065 btwrite(value & 0xff, BT848_BRIGHT);
1068 static void bt848_hue(struct bttv *btv, int hue)
1075 value = (hue >> 8) - 128;
1076 btwrite(value & 0xff, BT848_HUE);
1079 static void bt848_contrast(struct bttv *btv, int cont)
1083 btv->contrast = cont;
1086 value = (cont >> 7);
1087 hibit = (value >> 6) & 4;
1088 btwrite(value & 0xff, BT848_CONTRAST_LO);
1089 btaor(hibit, ~4, BT848_E_CONTROL);
1090 btaor(hibit, ~4, BT848_O_CONTROL);
1093 static void bt848_sat(struct bttv *btv, int color)
1095 int val_u,val_v,hibits;
1097 btv->saturation = color;
1099 /* 0-511 for the color */
1100 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1101 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1102 hibits = (val_u >> 7) & 2;
1103 hibits |= (val_v >> 8) & 1;
1104 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1105 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1106 btaor(hibits, ~3, BT848_E_CONTROL);
1107 btaor(hibits, ~3, BT848_O_CONTROL);
1110 /* ----------------------------------------------------------------------- */
1113 video_mux(struct bttv *btv, unsigned int input)
1117 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1120 /* needed by RemoteVideo MX */
1121 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1123 gpio_inout(mask2,mask2);
1125 if (input == btv->svhs) {
1126 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1127 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1129 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1130 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1132 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
1133 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1134 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1135 btv->c.nr,input,mux);
1137 /* card specific hook */
1138 if(bttv_tvcards[btv->c.type].muxsel_hook)
1139 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1143 static char *audio_modes[] = {
1144 "audio: tuner", "audio: radio", "audio: extern",
1145 "audio: intern", "audio: mute"
1149 audio_mux(struct bttv *btv, int input, int mute)
1151 int gpio_val, signal;
1152 struct v4l2_control ctrl;
1153 struct i2c_client *c;
1155 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1156 bttv_tvcards[btv->c.type].gpiomask);
1157 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1163 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1166 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1168 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1170 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1172 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1176 ctrl.id = V4L2_CID_AUDIO_MUTE;
1177 ctrl.value = btv->mute;
1178 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
1179 c = btv->i2c_msp34xx_client;
1181 struct v4l2_routing route;
1183 /* Note: the inputs tuner/radio/extern/intern are translated
1184 to msp routings. This assumes common behavior for all msp3400
1185 based TV cards. When this assumption fails, then the
1186 specific MSP routing must be added to the card table.
1187 For now this is sufficient. */
1189 case TVAUDIO_INPUT_RADIO:
1190 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1191 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1193 case TVAUDIO_INPUT_EXTERN:
1194 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1195 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1197 case TVAUDIO_INPUT_INTERN:
1198 /* Yes, this is the same input as for RADIO. I doubt
1199 if this is ever used. The only board with an INTERN
1200 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1201 that was tested. My guess is that the whole INTERN
1202 input does not work. */
1203 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1204 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1206 case TVAUDIO_INPUT_TUNER:
1208 /* This is the only card that uses TUNER2, and afaik,
1209 is the only difference between the VOODOOTV_FM
1211 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1212 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1213 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1215 route.input = MSP_INPUT_DEFAULT;
1218 route.output = MSP_OUTPUT_DEFAULT;
1219 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1221 c = btv->i2c_tvaudio_client;
1223 struct v4l2_routing route;
1225 route.input = input;
1227 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1233 audio_mute(struct bttv *btv, int mute)
1235 return audio_mux(btv, btv->audio, mute);
1239 audio_input(struct bttv *btv, int input)
1241 return audio_mux(btv, input, btv->mute);
1245 bttv_crop_calc_limits(struct bttv_crop *c)
1247 /* Scale factor min. 1:1, max. 16:1. Min. image size
1248 48 x 32. Scaled width must be a multiple of 4. */
1251 /* For bug compatibility with VIDIOCGCAP and image
1252 size checks in earlier driver versions. */
1253 c->min_scaled_width = 48;
1254 c->min_scaled_height = 32;
1256 c->min_scaled_width =
1257 (max(48, c->rect.width >> 4) + 3) & ~3;
1258 c->min_scaled_height =
1259 max(32, c->rect.height >> 4);
1262 c->max_scaled_width = c->rect.width & ~3;
1263 c->max_scaled_height = c->rect.height;
1267 bttv_crop_reset(struct bttv_crop *c, int norm)
1269 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1270 bttv_crop_calc_limits(c);
1273 /* Call with btv->lock down. */
1275 set_tvnorm(struct bttv *btv, unsigned int norm)
1277 const struct bttv_tvnorm *tvnorm;
1280 if (norm < 0 || norm >= BTTV_TVNORMS)
1283 tvnorm = &bttv_tvnorms[norm];
1285 if (btv->tvnorm < 0 ||
1286 btv->tvnorm >= BTTV_TVNORMS ||
1287 0 != memcmp(&bttv_tvnorms[btv->tvnorm].cropcap,
1289 sizeof (tvnorm->cropcap))) {
1290 bttv_crop_reset(&btv->crop[0], norm);
1291 btv->crop[1] = btv->crop[0]; /* current = default */
1293 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1294 btv->crop_start = tvnorm->cropcap.bounds.top
1295 + tvnorm->cropcap.bounds.height;
1301 btwrite(tvnorm->adelay, BT848_ADELAY);
1302 btwrite(tvnorm->bdelay, BT848_BDELAY);
1303 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1305 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1306 btwrite(1, BT848_VBI_PACK_DEL);
1307 bt848A_set_timing(btv);
1309 switch (btv->c.type) {
1310 case BTTV_BOARD_VOODOOTV_FM:
1311 case BTTV_BOARD_VOODOOTV_200:
1312 bttv_tda9880_setnorm(btv,norm);
1315 id = tvnorm->v4l2_id;
1316 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &id);
1321 /* Call with btv->lock down. */
1323 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1325 unsigned long flags;
1329 spin_lock_irqsave(&btv->s_lock,flags);
1330 if (btv->curr.frame_irq) {
1331 /* active capture -> delayed input switch */
1332 btv->new_input = input;
1334 video_mux(btv,input);
1336 spin_unlock_irqrestore(&btv->s_lock,flags);
1338 video_mux(btv,input);
1340 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1341 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
1342 set_tvnorm(btv, norm);
1345 static void init_irqreg(struct bttv *btv)
1348 btwrite(0xfffffUL, BT848_INT_STAT);
1350 if (bttv_tvcards[btv->c.type].no_video) {
1352 btwrite(BT848_INT_I2CDONE,
1356 btwrite((btv->triton1) |
1357 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1359 (fdsr ? BT848_INT_FDSR : 0) |
1360 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1361 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1367 static void init_bt848(struct bttv *btv)
1371 if (bttv_tvcards[btv->c.type].no_video) {
1372 /* very basic init only */
1377 btwrite(0x00, BT848_CAP_CTL);
1378 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1379 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1381 /* set planar and packed mode trigger points and */
1382 /* set rising edge of inverted GPINTR pin as irq trigger */
1383 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1384 BT848_GPIO_DMA_CTL_PLTP1_16|
1385 BT848_GPIO_DMA_CTL_PLTP23_16|
1386 BT848_GPIO_DMA_CTL_GPINTC|
1387 BT848_GPIO_DMA_CTL_GPINTI,
1388 BT848_GPIO_DMA_CTL);
1390 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1391 btwrite(val, BT848_E_SCLOOP);
1392 btwrite(val, BT848_O_SCLOOP);
1394 btwrite(0x20, BT848_E_VSCALE_HI);
1395 btwrite(0x20, BT848_O_VSCALE_HI);
1396 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1399 btwrite(whitecrush_upper, BT848_WC_UP);
1400 btwrite(whitecrush_lower, BT848_WC_DOWN);
1402 if (btv->opt_lumafilter) {
1403 btwrite(0, BT848_E_CONTROL);
1404 btwrite(0, BT848_O_CONTROL);
1406 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1407 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1410 bt848_bright(btv, btv->bright);
1411 bt848_hue(btv, btv->hue);
1412 bt848_contrast(btv, btv->contrast);
1413 bt848_sat(btv, btv->saturation);
1419 static void bttv_reinit_bt848(struct bttv *btv)
1421 unsigned long flags;
1424 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1425 spin_lock_irqsave(&btv->s_lock,flags);
1427 bttv_set_dma(btv,0);
1428 spin_unlock_irqrestore(&btv->s_lock,flags);
1431 btv->pll.pll_current = -1;
1432 set_input(btv, btv->input, btv->tvnorm);
1435 static int get_control(struct bttv *btv, struct v4l2_control *c)
1439 for (i = 0; i < BTTV_CTLS; i++)
1440 if (bttv_ctls[i].id == c->id)
1444 #ifdef CONFIG_VIDEO_V4L1
1445 if (btv->audio_hook && i >= 4 && i <= 8) {
1446 struct video_audio va;
1448 memset(&va,0,sizeof(va));
1449 btv->audio_hook(btv,&va,0);
1451 case V4L2_CID_AUDIO_MUTE:
1452 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1454 case V4L2_CID_AUDIO_VOLUME:
1455 c->value = va.volume;
1462 case V4L2_CID_BRIGHTNESS:
1463 c->value = btv->bright;
1466 c->value = btv->hue;
1468 case V4L2_CID_CONTRAST:
1469 c->value = btv->contrast;
1471 case V4L2_CID_SATURATION:
1472 c->value = btv->saturation;
1475 case V4L2_CID_AUDIO_MUTE:
1476 case V4L2_CID_AUDIO_VOLUME:
1477 case V4L2_CID_AUDIO_BALANCE:
1478 case V4L2_CID_AUDIO_BASS:
1479 case V4L2_CID_AUDIO_TREBLE:
1480 bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c);
1483 case V4L2_CID_PRIVATE_CHROMA_AGC:
1484 c->value = btv->opt_chroma_agc;
1486 case V4L2_CID_PRIVATE_COMBFILTER:
1487 c->value = btv->opt_combfilter;
1489 case V4L2_CID_PRIVATE_LUMAFILTER:
1490 c->value = btv->opt_lumafilter;
1492 case V4L2_CID_PRIVATE_AUTOMUTE:
1493 c->value = btv->opt_automute;
1495 case V4L2_CID_PRIVATE_AGC_CRUSH:
1496 c->value = btv->opt_adc_crush;
1498 case V4L2_CID_PRIVATE_VCR_HACK:
1499 c->value = btv->opt_vcr_hack;
1501 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1502 c->value = btv->opt_whitecrush_upper;
1504 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1505 c->value = btv->opt_whitecrush_lower;
1507 case V4L2_CID_PRIVATE_UV_RATIO:
1508 c->value = btv->opt_uv_ratio;
1510 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1511 c->value = btv->opt_full_luma_range;
1513 case V4L2_CID_PRIVATE_CORING:
1514 c->value = btv->opt_coring;
1522 static int set_control(struct bttv *btv, struct v4l2_control *c)
1526 for (i = 0; i < BTTV_CTLS; i++)
1527 if (bttv_ctls[i].id == c->id)
1531 #ifdef CONFIG_VIDEO_V4L1
1532 if (btv->audio_hook && i >= 4 && i <= 8) {
1533 struct video_audio va;
1535 memset(&va,0,sizeof(va));
1536 btv->audio_hook(btv,&va,0);
1538 case V4L2_CID_AUDIO_MUTE:
1540 va.flags |= VIDEO_AUDIO_MUTE;
1543 va.flags &= ~VIDEO_AUDIO_MUTE;
1548 case V4L2_CID_AUDIO_VOLUME:
1549 va.volume = c->value;
1552 btv->audio_hook(btv,&va,1);
1557 case V4L2_CID_BRIGHTNESS:
1558 bt848_bright(btv,c->value);
1561 bt848_hue(btv,c->value);
1563 case V4L2_CID_CONTRAST:
1564 bt848_contrast(btv,c->value);
1566 case V4L2_CID_SATURATION:
1567 bt848_sat(btv,c->value);
1569 case V4L2_CID_AUDIO_MUTE:
1570 audio_mute(btv, c->value);
1572 case V4L2_CID_AUDIO_VOLUME:
1573 case V4L2_CID_AUDIO_BALANCE:
1574 case V4L2_CID_AUDIO_BASS:
1575 case V4L2_CID_AUDIO_TREBLE:
1576 bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c);
1579 case V4L2_CID_PRIVATE_CHROMA_AGC:
1580 btv->opt_chroma_agc = c->value;
1581 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1582 btwrite(val, BT848_E_SCLOOP);
1583 btwrite(val, BT848_O_SCLOOP);
1585 case V4L2_CID_PRIVATE_COMBFILTER:
1586 btv->opt_combfilter = c->value;
1588 case V4L2_CID_PRIVATE_LUMAFILTER:
1589 btv->opt_lumafilter = c->value;
1590 if (btv->opt_lumafilter) {
1591 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1592 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1594 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1595 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1598 case V4L2_CID_PRIVATE_AUTOMUTE:
1599 btv->opt_automute = c->value;
1601 case V4L2_CID_PRIVATE_AGC_CRUSH:
1602 btv->opt_adc_crush = c->value;
1603 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1606 case V4L2_CID_PRIVATE_VCR_HACK:
1607 btv->opt_vcr_hack = c->value;
1609 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1610 btv->opt_whitecrush_upper = c->value;
1611 btwrite(c->value, BT848_WC_UP);
1613 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1614 btv->opt_whitecrush_lower = c->value;
1615 btwrite(c->value, BT848_WC_DOWN);
1617 case V4L2_CID_PRIVATE_UV_RATIO:
1618 btv->opt_uv_ratio = c->value;
1619 bt848_sat(btv, btv->saturation);
1621 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1622 btv->opt_full_luma_range = c->value;
1623 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1625 case V4L2_CID_PRIVATE_CORING:
1626 btv->opt_coring = c->value;
1627 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1635 /* ----------------------------------------------------------------------- */
1637 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1639 unsigned int outbits, data;
1640 outbits = btread(BT848_GPIO_OUT_EN);
1641 data = btread(BT848_GPIO_DATA);
1642 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1643 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1646 static void bttv_field_count(struct bttv *btv)
1654 /* start field counter */
1655 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1657 /* stop field counter */
1658 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1659 btv->field_count = 0;
1663 static const struct bttv_format*
1664 format_by_fourcc(int fourcc)
1668 for (i = 0; i < BTTV_FORMATS; i++) {
1669 if (-1 == bttv_formats[i].fourcc)
1671 if (bttv_formats[i].fourcc == fourcc)
1672 return bttv_formats+i;
1677 /* ----------------------------------------------------------------------- */
1681 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1682 struct bttv_buffer *new)
1684 struct bttv_buffer *old;
1685 unsigned long flags;
1688 dprintk("switch_overlay: enter [new=%p]\n",new);
1690 new->vb.state = STATE_DONE;
1691 spin_lock_irqsave(&btv->s_lock,flags);
1695 bttv_set_dma(btv, 0x03);
1696 spin_unlock_irqrestore(&btv->s_lock,flags);
1698 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1699 bttv_dma_free(&fh->cap,btv, old);
1703 free_btres(btv,fh,RESOURCE_OVERLAY);
1704 dprintk("switch_overlay: done\n");
1708 /* ----------------------------------------------------------------------- */
1709 /* video4linux (1) interface */
1711 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1712 struct bttv_buffer *buf,
1713 const struct bttv_format *fmt,
1714 unsigned int width, unsigned int height,
1715 enum v4l2_field field)
1717 struct bttv_fh *fh = q->priv_data;
1718 int redo_dma_risc = 0;
1723 /* check settings */
1726 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1728 height = RAW_LINES*2;
1729 if (width*height > buf->vb.bsize)
1731 buf->vb.size = buf->vb.bsize;
1733 /* Make sure tvnorm and vbi_end remain consistent
1734 until we're done. */
1735 mutex_lock(&btv->lock);
1739 /* In this mode capturing always starts at defrect.top
1740 (default VDELAY), ignoring cropping parameters. */
1741 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1742 mutex_unlock(&btv->lock);
1746 mutex_unlock(&btv->lock);
1748 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1750 mutex_lock(&btv->lock);
1753 c = btv->crop[!!fh->do_crop];
1755 mutex_unlock(&btv->lock);
1757 if (width < c.min_scaled_width ||
1758 width > c.max_scaled_width ||
1759 height < c.min_scaled_height)
1763 case V4L2_FIELD_TOP:
1764 case V4L2_FIELD_BOTTOM:
1765 case V4L2_FIELD_ALTERNATE:
1766 /* btv->crop counts frame lines. Max. scale
1767 factor is 16:1 for frames, 8:1 for fields. */
1768 if (height * 2 > c.max_scaled_height)
1773 if (height > c.max_scaled_height)
1778 buf->vb.size = (width * height * fmt->depth) >> 3;
1779 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1783 /* alloc + fill struct bttv_buffer (if changed) */
1784 if (buf->vb.width != width || buf->vb.height != height ||
1785 buf->vb.field != field ||
1786 buf->tvnorm != norm || buf->fmt != fmt ||
1787 buf->crop.top != c.rect.top ||
1788 buf->crop.left != c.rect.left ||
1789 buf->crop.width != c.rect.width ||
1790 buf->crop.height != c.rect.height) {
1791 buf->vb.width = width;
1792 buf->vb.height = height;
1793 buf->vb.field = field;
1800 /* alloc risc memory */
1801 if (STATE_NEEDS_INIT == buf->vb.state) {
1803 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1808 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1811 buf->vb.state = STATE_PREPARED;
1815 bttv_dma_free(q,btv,buf);
1820 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1822 struct bttv_fh *fh = q->priv_data;
1824 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1827 while (*size * *count > gbuffers * gbufsize)
1833 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1834 enum v4l2_field field)
1836 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1837 struct bttv_fh *fh = q->priv_data;
1839 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1840 fh->width, fh->height, field);
1844 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1846 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1847 struct bttv_fh *fh = q->priv_data;
1848 struct bttv *btv = fh->btv;
1850 buf->vb.state = STATE_QUEUED;
1851 list_add_tail(&buf->vb.queue,&btv->capture);
1852 if (!btv->curr.frame_irq) {
1854 bttv_set_dma(btv, 0x03);
1858 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1860 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1861 struct bttv_fh *fh = q->priv_data;
1863 bttv_dma_free(q,fh->btv,buf);
1866 static struct videobuf_queue_ops bttv_video_qops = {
1867 .buf_setup = buffer_setup,
1868 .buf_prepare = buffer_prepare,
1869 .buf_queue = buffer_queue,
1870 .buf_release = buffer_release,
1873 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1876 case VIDIOC_ENUMSTD:
1878 struct v4l2_standard *e = arg;
1879 unsigned int index = e->index;
1881 if (index >= BTTV_TVNORMS)
1883 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1884 bttv_tvnorms[e->index].name);
1890 v4l2_std_id *id = arg;
1891 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1896 v4l2_std_id *id = arg;
1899 for (i = 0; i < BTTV_TVNORMS; i++)
1900 if (*id & bttv_tvnorms[i].v4l2_id)
1902 if (i == BTTV_TVNORMS)
1905 mutex_lock(&btv->lock);
1907 mutex_unlock(&btv->lock);
1910 case VIDIOC_QUERYSTD:
1912 v4l2_std_id *id = arg;
1914 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1915 *id = V4L2_STD_625_50;
1917 *id = V4L2_STD_525_60;
1921 case VIDIOC_ENUMINPUT:
1923 struct v4l2_input *i = arg;
1927 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1929 memset(i,0,sizeof(*i));
1931 i->type = V4L2_INPUT_TYPE_CAMERA;
1933 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1934 sprintf(i->name, "Television");
1935 i->type = V4L2_INPUT_TYPE_TUNER;
1937 } else if (i->index == btv->svhs) {
1938 sprintf(i->name, "S-Video");
1940 sprintf(i->name,"Composite%d",i->index);
1942 if (i->index == btv->input) {
1943 __u32 dstatus = btread(BT848_DSTATUS);
1944 if (0 == (dstatus & BT848_DSTATUS_PRES))
1945 i->status |= V4L2_IN_ST_NO_SIGNAL;
1946 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1947 i->status |= V4L2_IN_ST_NO_H_LOCK;
1949 for (n = 0; n < BTTV_TVNORMS; n++)
1950 i->std |= bttv_tvnorms[n].v4l2_id;
1953 case VIDIOC_G_INPUT:
1959 case VIDIOC_S_INPUT:
1961 unsigned int *i = arg;
1963 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1965 mutex_lock(&btv->lock);
1966 set_input(btv, *i, btv->tvnorm);
1967 mutex_unlock(&btv->lock);
1971 case VIDIOC_S_TUNER:
1973 struct v4l2_tuner *t = arg;
1975 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1979 mutex_lock(&btv->lock);
1980 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
1981 #ifdef CONFIG_VIDEO_V4L1
1982 if (btv->audio_hook) {
1983 struct video_audio va;
1984 memset(&va, 0, sizeof(struct video_audio));
1985 if (t->audmode == V4L2_TUNER_MODE_MONO)
1986 va.mode = VIDEO_SOUND_MONO;
1987 else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
1988 t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
1989 va.mode = VIDEO_SOUND_STEREO;
1990 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1991 va.mode = VIDEO_SOUND_LANG1;
1992 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1993 va.mode = VIDEO_SOUND_LANG2;
1994 btv->audio_hook(btv,&va,1);
1997 mutex_unlock(&btv->lock);
2001 case VIDIOC_G_FREQUENCY:
2003 struct v4l2_frequency *f = arg;
2005 memset(f,0,sizeof(*f));
2006 f->type = V4L2_TUNER_ANALOG_TV;
2007 f->frequency = btv->freq;
2010 case VIDIOC_S_FREQUENCY:
2012 struct v4l2_frequency *f = arg;
2014 if (unlikely(f->tuner != 0))
2016 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
2018 mutex_lock(&btv->lock);
2019 btv->freq = f->frequency;
2020 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
2021 if (btv->has_matchbox && btv->radio_user)
2022 tea5757_set_freq(btv,btv->freq);
2023 mutex_unlock(&btv->lock);
2026 case VIDIOC_LOG_STATUS:
2028 printk(KERN_INFO "bttv%d: ================= START STATUS CARD #%d =================\n", btv->c.nr, btv->c.nr);
2029 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
2030 printk(KERN_INFO "bttv%d: ================== END STATUS CARD #%d ==================\n", btv->c.nr, btv->c.nr);
2034 return get_control(btv,arg);
2036 return set_control(btv,arg);
2037 #ifdef CONFIG_VIDEO_ADV_DEBUG
2038 case VIDIOC_DBG_G_REGISTER:
2039 case VIDIOC_DBG_S_REGISTER:
2041 struct v4l2_register *reg = arg;
2042 if (!capable(CAP_SYS_ADMIN))
2044 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2046 /* bt848 has a 12-bit register space */
2048 if (cmd == VIDIOC_DBG_G_REGISTER)
2049 reg->val = btread(reg->reg);
2051 btwrite(reg->val, reg->reg);
2057 return -ENOIOCTLCMD;
2063 /* Given cropping boundaries b and the scaled width and height of a
2064 single field or frame, which must not exceed hardware limits, this
2065 function adjusts the cropping parameters c. */
2067 bttv_crop_adjust (struct bttv_crop * c,
2068 const struct v4l2_rect * b,
2071 enum v4l2_field field)
2073 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2077 if (width < c->min_scaled_width) {
2078 /* Max. hor. scale factor 16:1. */
2079 c->rect.width = width * 16;
2080 } else if (width > c->max_scaled_width) {
2081 /* Min. hor. scale factor 1:1. */
2082 c->rect.width = width;
2084 max_left = b->left + b->width - width;
2085 max_left = min(max_left, (__s32) MAX_HDELAY);
2086 if (c->rect.left > max_left)
2087 c->rect.left = max_left;
2090 if (height < c->min_scaled_height) {
2091 /* Max. vert. scale factor 16:1, single fields 8:1. */
2092 c->rect.height = height * 16;
2093 } else if (frame_height > c->max_scaled_height) {
2094 /* Min. vert. scale factor 1:1.
2095 Top and height count field lines times two. */
2096 c->rect.height = (frame_height + 1) & ~1;
2098 max_top = b->top + b->height - c->rect.height;
2099 if (c->rect.top > max_top)
2100 c->rect.top = max_top;
2103 bttv_crop_calc_limits(c);
2106 /* Returns an error if scaling to a frame or single field with the given
2107 width and height is not possible with the current cropping parameters
2108 and width aligned according to width_mask. If adjust_size is TRUE the
2109 function may adjust the width and/or height instead, rounding width
2110 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2111 also adjust the current cropping parameters to get closer to the
2112 desired image size. */
2114 limit_scaled_size (struct bttv_fh * fh,
2117 enum v4l2_field field,
2118 unsigned int width_mask,
2119 unsigned int width_bias,
2123 struct bttv *btv = fh->btv;
2124 const struct v4l2_rect *b;
2125 struct bttv_crop *c;
2132 BUG_ON((int) width_mask >= 0 ||
2133 width_bias >= (unsigned int) -width_mask);
2135 /* Make sure tvnorm, vbi_end and the current cropping parameters
2136 remain consistent until we're done. */
2137 mutex_lock(&btv->lock);
2139 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2141 /* Do crop - use current, don't - use default parameters. */
2142 c = &btv->crop[!!fh->do_crop];
2147 && !locked_btres(btv, VIDEO_RESOURCES)) {
2151 /* We cannot scale up. When the scaled image is larger
2152 than crop.rect we adjust the crop.rect as required
2153 by the V4L2 spec, hence cropcap.bounds are our limit. */
2154 max_width = min(b->width, (__s32) MAX_HACTIVE);
2155 max_height = b->height;
2157 /* We cannot capture the same line as video and VBI data.
2158 Note btv->vbi_end is really a minimum, see
2159 bttv_vbi_try_fmt(). */
2160 if (btv->vbi_end > b->top) {
2161 max_height -= btv->vbi_end - b->top;
2163 if (min_height > max_height)
2168 if (btv->vbi_end > c->rect.top)
2171 min_width = c->min_scaled_width;
2172 min_height = c->min_scaled_height;
2173 max_width = c->max_scaled_width;
2174 max_height = c->max_scaled_height;
2179 min_width = (min_width - width_mask - 1) & width_mask;
2180 max_width = max_width & width_mask;
2182 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2183 min_height = min_height;
2184 /* Min. scale factor is 1:1. */
2185 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2188 *width = clamp(*width, min_width, max_width);
2189 *height = clamp(*height, min_height, max_height);
2191 /* Round after clamping to avoid overflow. */
2192 *width = (*width + width_bias) & width_mask;
2195 bttv_crop_adjust(c, b, *width, *height, field);
2197 if (btv->vbi_end > c->rect.top) {
2198 /* Move the crop window out of the way. */
2199 c->rect.top = btv->vbi_end;
2204 if (*width < min_width ||
2205 *height < min_height ||
2206 *width > max_width ||
2207 *height > max_height ||
2208 0 != (*width & ~width_mask))
2212 rc = 0; /* success */
2215 mutex_unlock(&btv->lock);
2220 /* Returns an error if the given overlay window dimensions are not
2221 possible with the current cropping parameters. If adjust_size is
2222 TRUE the function may adjust the window width and/or height
2223 instead, however it always rounds the horizontal position and
2224 width as btcx_align() does. If adjust_crop is TRUE the function
2225 may also adjust the current cropping parameters to get closer
2226 to the desired window size. */
2228 verify_window (struct bttv_fh * fh,
2229 struct v4l2_window * win,
2233 enum v4l2_field field;
2234 unsigned int width_mask;
2237 if (win->w.width < 48 || win->w.height < 32)
2239 if (win->clipcount > 2048)
2244 if (V4L2_FIELD_ANY == field) {
2247 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2248 field = (win->w.height > height2)
2249 ? V4L2_FIELD_INTERLACED
2253 case V4L2_FIELD_TOP:
2254 case V4L2_FIELD_BOTTOM:
2255 case V4L2_FIELD_INTERLACED:
2261 /* 4-byte alignment. */
2262 if (NULL == fh->ovfmt)
2265 switch (fh->ovfmt->depth) {
2279 win->w.width -= win->w.left & ~width_mask;
2280 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2282 rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
2284 /* width_bias: round down */ 0,
2285 adjust_size, adjust_crop);
2293 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2294 struct v4l2_window *win, int fixup)
2296 struct v4l2_clip *clips = NULL;
2297 int n,size,retval = 0;
2299 if (NULL == fh->ovfmt)
2301 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2303 retval = verify_window(fh, win,
2304 /* adjust_size */ fixup,
2305 /* adjust_crop */ fixup);
2309 /* copy clips -- luckily v4l1 + v4l2 are binary
2310 compatible here ...*/
2312 size = sizeof(*clips)*(n+4);
2313 clips = kmalloc(size,GFP_KERNEL);
2317 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2322 /* clip against screen */
2323 if (NULL != btv->fbuf.base)
2324 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2326 btcx_sort_clips(clips,n);
2328 /* 4-byte alignments */
2329 switch (fh->ovfmt->depth) {
2332 btcx_align(&win->w, clips, n, 3);
2335 btcx_align(&win->w, clips, n, 1);
2338 /* no alignment fixups needed */
2344 mutex_lock(&fh->cap.lock);
2345 kfree(fh->ov.clips);
2346 fh->ov.clips = clips;
2350 fh->ov.field = win->field;
2351 fh->ov.setup_ok = 1;
2352 btv->init.ov.w.width = win->w.width;
2353 btv->init.ov.w.height = win->w.height;
2354 btv->init.ov.field = win->field;
2356 /* update overlay if needed */
2358 if (check_btres(fh, RESOURCE_OVERLAY)) {
2359 struct bttv_buffer *new;
2361 new = videobuf_pci_alloc(sizeof(*new));
2362 new->crop = btv->crop[!!fh->do_crop].rect;
2363 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2364 retval = bttv_switch_overlay(btv,fh,new);
2366 mutex_unlock(&fh->cap.lock);
2370 /* ----------------------------------------------------------------------- */
2372 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2374 struct videobuf_queue* q = NULL;
2377 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2380 case V4L2_BUF_TYPE_VBI_CAPTURE:
2389 static int bttv_resource(struct bttv_fh *fh)
2394 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2395 res = RESOURCE_VIDEO_STREAM;
2397 case V4L2_BUF_TYPE_VBI_CAPTURE:
2406 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2408 struct videobuf_queue *q = bttv_queue(fh);
2409 int res = bttv_resource(fh);
2411 if (check_btres(fh,res))
2413 if (videobuf_queue_is_busy(q))
2420 pix_format_set_size (struct v4l2_pix_format * f,
2421 const struct bttv_format * fmt,
2423 unsigned int height)
2428 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2429 f->bytesperline = width; /* Y plane */
2430 f->sizeimage = (width * height * fmt->depth) >> 3;
2432 f->bytesperline = (width * fmt->depth) >> 3;
2433 f->sizeimage = height * f->bytesperline;
2437 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2440 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2441 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2442 pix_format_set_size (&f->fmt.pix, fh->fmt,
2443 fh->width, fh->height);
2444 f->fmt.pix.field = fh->cap.field;
2445 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2447 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2448 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2449 f->fmt.win.w = fh->ov.w;
2450 f->fmt.win.field = fh->ov.field;
2452 case V4L2_BUF_TYPE_VBI_CAPTURE:
2453 bttv_vbi_get_fmt(fh, &f->fmt.vbi);
2460 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2461 struct v4l2_format *f, int adjust_crop)
2464 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2466 const struct bttv_format *fmt;
2467 enum v4l2_field field;
2468 __s32 width, height;
2471 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2475 field = f->fmt.pix.field;
2476 if (V4L2_FIELD_ANY == field) {
2479 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2480 field = (f->fmt.pix.height > height2)
2481 ? V4L2_FIELD_INTERLACED
2482 : V4L2_FIELD_BOTTOM;
2484 if (V4L2_FIELD_SEQ_BT == field)
2485 field = V4L2_FIELD_SEQ_TB;
2487 case V4L2_FIELD_TOP:
2488 case V4L2_FIELD_BOTTOM:
2489 case V4L2_FIELD_ALTERNATE:
2490 case V4L2_FIELD_INTERLACED:
2492 case V4L2_FIELD_SEQ_TB:
2493 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2500 width = f->fmt.pix.width;
2501 height = f->fmt.pix.height;
2503 rc = limit_scaled_size(fh, &width, &height, field,
2504 /* width_mask: 4 pixels */ ~3,
2505 /* width_bias: nearest */ 2,
2506 /* adjust_size */ 1,
2511 /* update data for the application */
2512 f->fmt.pix.field = field;
2513 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2517 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2518 return verify_window(fh, &f->fmt.win,
2519 /* adjust_size */ 1,
2520 /* adjust_crop */ 0);
2521 case V4L2_BUF_TYPE_VBI_CAPTURE:
2522 return bttv_vbi_try_fmt(fh, &f->fmt.vbi);
2528 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2529 struct v4l2_format *f)
2534 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2536 const struct bttv_format *fmt;
2538 retval = bttv_switch_type(fh,f->type);
2541 retval = bttv_try_fmt(fh,btv,f, /* adjust_crop */ 1);
2544 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2546 /* update our state informations */
2547 mutex_lock(&fh->cap.lock);
2549 fh->cap.field = f->fmt.pix.field;
2550 fh->cap.last = V4L2_FIELD_NONE;
2551 fh->width = f->fmt.pix.width;
2552 fh->height = f->fmt.pix.height;
2553 btv->init.fmt = fmt;
2554 btv->init.width = f->fmt.pix.width;
2555 btv->init.height = f->fmt.pix.height;
2556 mutex_unlock(&fh->cap.lock);
2560 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2561 if (no_overlay > 0) {
2562 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2565 return setup_window(fh, btv, &f->fmt.win, 1);
2566 case V4L2_BUF_TYPE_VBI_CAPTURE:
2567 retval = bttv_switch_type(fh,f->type);
2570 return bttv_vbi_set_fmt(fh, &f->fmt.vbi);
2576 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2577 unsigned int cmd, void *arg)
2579 struct bttv_fh *fh = file->private_data;
2580 struct bttv *btv = fh->btv;
2584 v4l_print_ioctl(btv->c.name, cmd);
2587 bttv_reinit_bt848(btv);
2592 case VIDIOC_S_INPUT:
2593 case VIDIOC_S_TUNER:
2594 case VIDIOC_S_FREQUENCY:
2595 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2601 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2604 struct video_mbuf *mbuf = arg;
2607 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2613 memset(mbuf,0,sizeof(*mbuf));
2614 mbuf->frames = gbuffers;
2615 mbuf->size = gbuffers * gbufsize;
2616 for (i = 0; i < gbuffers; i++)
2617 mbuf->offsets[i] = i * gbufsize;
2622 /* *** v4l2 *** ************************************************ */
2623 case VIDIOC_QUERYCAP:
2625 struct v4l2_capability *cap = arg;
2629 memset(cap, 0, sizeof (*cap));
2630 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
2631 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
2632 snprintf(cap->bus_info, sizeof (cap->bus_info),
2633 "PCI:%s", pci_name(btv->c.pci));
2634 cap->version = BTTV_VERSION_CODE;
2636 V4L2_CAP_VIDEO_CAPTURE |
2637 V4L2_CAP_VBI_CAPTURE |
2638 V4L2_CAP_READWRITE |
2640 if (no_overlay <= 0)
2641 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2643 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2644 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2645 cap->capabilities |= V4L2_CAP_TUNER;
2648 case VIDIOC_ENUM_FMT:
2650 struct v4l2_fmtdesc *f = arg;
2651 enum v4l2_buf_type type;
2656 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2661 memset(f,0,sizeof(*f));
2664 f->pixelformat = V4L2_PIX_FMT_GREY;
2665 strcpy(f->description,"vbi data");
2669 /* video capture + overlay */
2671 for (i = 0; i < BTTV_FORMATS; i++) {
2672 if (bttv_formats[i].fourcc != -1)
2674 if ((unsigned int)index == f->index)
2677 if (BTTV_FORMATS == i)
2681 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2683 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2684 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2690 memset(f,0,sizeof(*f));
2693 f->pixelformat = bttv_formats[i].fourcc;
2694 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2697 case VIDIOC_TRY_FMT:
2699 struct v4l2_format *f = arg;
2700 return bttv_try_fmt(fh,btv,f, /* adjust_crop */ 0);
2704 struct v4l2_format *f = arg;
2705 return bttv_g_fmt(fh,f);
2709 struct v4l2_format *f = arg;
2710 return bttv_s_fmt(fh,btv,f);
2715 struct v4l2_framebuffer *fb = arg;
2718 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2720 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2723 case VIDIOC_OVERLAY:
2725 struct bttv_buffer *new;
2730 if (NULL == btv->fbuf.base)
2732 if (!fh->ov.setup_ok) {
2733 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2738 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2741 mutex_lock(&fh->cap.lock);
2743 fh->ov.tvnorm = btv->tvnorm;
2744 new = videobuf_pci_alloc(sizeof(*new));
2745 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2751 retval = bttv_switch_overlay(btv,fh,new);
2752 mutex_unlock(&fh->cap.lock);
2757 struct v4l2_framebuffer *fb = arg;
2758 const struct bttv_format *fmt;
2760 if(!capable(CAP_SYS_ADMIN) &&
2761 !capable(CAP_SYS_RAWIO))
2765 fmt = format_by_fourcc(fb->fmt.pixelformat);
2768 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2772 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2773 __s32 width = fb->fmt.width;
2774 __s32 height = fb->fmt.height;
2776 retval = limit_scaled_size(fh, &width, &height,
2777 V4L2_FIELD_INTERLACED,
2778 /* width_mask */ ~3,
2780 /* adjust_size */ 0,
2781 /* adjust_crop */ 0);
2787 mutex_lock(&fh->cap.lock);
2788 btv->fbuf.base = fb->base;
2789 btv->fbuf.fmt.width = fb->fmt.width;
2790 btv->fbuf.fmt.height = fb->fmt.height;
2791 if (0 != fb->fmt.bytesperline)
2792 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2794 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2798 btv->init.ovfmt = fmt;
2799 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2802 fh->ov.w.width = fb->fmt.width;
2803 fh->ov.w.height = fb->fmt.height;
2804 btv->init.ov.w.width = fb->fmt.width;
2805 btv->init.ov.w.height = fb->fmt.height;
2806 kfree(fh->ov.clips);
2807 fh->ov.clips = NULL;
2810 if (check_btres(fh, RESOURCE_OVERLAY)) {
2811 struct bttv_buffer *new;
2813 new = videobuf_pci_alloc(sizeof(*new));
2814 new->crop = btv->crop[!!fh->do_crop].rect;
2815 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2816 retval = bttv_switch_overlay(btv,fh,new);
2819 mutex_unlock(&fh->cap.lock);
2822 case VIDIOC_REQBUFS:
2823 return videobuf_reqbufs(bttv_queue(fh),arg);
2824 case VIDIOC_QUERYBUF:
2825 return videobuf_querybuf(bttv_queue(fh),arg);
2828 int res = bttv_resource(fh);
2830 if (!check_alloc_btres(btv, fh, res))
2832 return videobuf_qbuf(bttv_queue(fh),arg);
2835 return videobuf_dqbuf(bttv_queue(fh),arg,
2836 file->f_flags & O_NONBLOCK);
2837 case VIDIOC_STREAMON:
2839 int res = bttv_resource(fh);
2841 if (!check_alloc_btres(btv,fh,res))
2843 return videobuf_streamon(bttv_queue(fh));
2845 case VIDIOC_STREAMOFF:
2847 int res = bttv_resource(fh);
2849 retval = videobuf_streamoff(bttv_queue(fh));
2852 free_btres(btv,fh,res);
2856 case VIDIOC_QUERYCTRL:
2858 struct v4l2_queryctrl *c = arg;
2861 if ((c->id < V4L2_CID_BASE ||
2862 c->id >= V4L2_CID_LASTP1) &&
2863 (c->id < V4L2_CID_PRIVATE_BASE ||
2864 c->id >= V4L2_CID_PRIVATE_LASTP1))
2866 for (i = 0; i < BTTV_CTLS; i++)
2867 if (bttv_ctls[i].id == c->id)
2869 if (i == BTTV_CTLS) {
2874 #ifdef CONFIG_VIDEO_V4L1
2875 if (btv->audio_hook && i >= 4 && i <= 8) {
2876 struct video_audio va;
2877 memset(&va,0,sizeof(va));
2878 btv->audio_hook(btv,&va,0);
2879 switch (bttv_ctls[i].id) {
2880 case V4L2_CID_AUDIO_VOLUME:
2881 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2891 struct v4l2_streamparm *parm = arg;
2892 struct v4l2_standard s;
2893 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2895 memset(parm,0,sizeof(*parm));
2896 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2897 bttv_tvnorms[btv->tvnorm].name);
2898 parm->parm.capture.timeperframe = s.frameperiod;
2901 case VIDIOC_G_TUNER:
2903 struct v4l2_tuner *t = arg;
2905 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2909 mutex_lock(&btv->lock);
2910 memset(t,0,sizeof(*t));
2911 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2912 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
2913 strcpy(t->name, "Television");
2914 t->capability = V4L2_TUNER_CAP_NORM;
2915 t->type = V4L2_TUNER_ANALOG_TV;
2916 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2918 #ifdef CONFIG_VIDEO_V4L1
2919 if (btv->audio_hook) {
2921 struct video_audio va;
2922 memset(&va, 0, sizeof(struct video_audio));
2923 btv->audio_hook(btv,&va,0);
2924 t->audmode = V4L2_TUNER_MODE_MONO;
2925 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2926 if(va.mode & VIDEO_SOUND_STEREO) {
2927 t->audmode = V4L2_TUNER_MODE_STEREO;
2928 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
2930 if(va.mode & VIDEO_SOUND_LANG2) {
2931 t->audmode = V4L2_TUNER_MODE_LANG1;
2932 t->rxsubchans = V4L2_TUNER_SUB_LANG1
2933 | V4L2_TUNER_SUB_LANG2;
2937 /* FIXME: fill capability+audmode */
2938 mutex_unlock(&btv->lock);
2942 case VIDIOC_G_PRIORITY:
2944 enum v4l2_priority *p = arg;
2946 *p = v4l2_prio_max(&btv->prio);
2949 case VIDIOC_S_PRIORITY:
2951 enum v4l2_priority *prio = arg;
2953 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2956 case VIDIOC_CROPCAP:
2958 struct v4l2_cropcap *cap = arg;
2959 enum v4l2_buf_type type;
2963 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2964 type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2967 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2974 struct v4l2_crop * crop = arg;
2976 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2977 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2980 /* No fh->do_crop = 1; because btv->crop[1] may be
2981 inconsistent with fh->width or fh->height and apps
2982 do not expect a change here. */
2984 crop->c = btv->crop[!!fh->do_crop].rect;
2990 struct v4l2_crop *crop = arg;
2991 const struct v4l2_rect *b;
2998 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2999 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3002 retval = v4l2_prio_check(&btv->prio,&fh->prio);
3006 /* Make sure tvnorm, vbi_end and the current cropping
3007 parameters remain consistent until we're done. Note
3008 read() may change vbi_end in check_alloc_btres(). */
3009 mutex_lock(&btv->lock);
3013 if (locked_btres(fh->btv, VIDEO_RESOURCES))
3014 goto btv_unlock_and_return;
3016 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3019 b_right = b_left + b->width;
3020 b_bottom = b->top + b->height;
3022 b_top = max(b->top, btv->vbi_end);
3023 if (b_top + 32 >= b_bottom)
3024 goto btv_unlock_and_return;
3026 /* Min. scaled size 48 x 32. */
3027 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3028 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3030 c.rect.width = clamp(crop->c.width,
3031 48, b_right - c.rect.left);
3033 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3034 /* Top and height must be a multiple of two. */
3035 c.rect.top = (c.rect.top + 1) & ~1;
3037 c.rect.height = clamp(crop->c.height,
3038 32, b_bottom - c.rect.top);
3039 c.rect.height = (c.rect.height + 1) & ~1;
3041 bttv_crop_calc_limits(&c);
3045 mutex_unlock(&btv->lock);
3049 mutex_lock(&fh->cap.lock);
3051 if (fh->width < c.min_scaled_width) {
3052 fh->width = c.min_scaled_width;
3053 btv->init.width = c.min_scaled_width;
3054 } else if (fh->width > c.max_scaled_width) {
3055 fh->width = c.max_scaled_width;
3056 btv->init.width = c.max_scaled_width;
3059 if (fh->height < c.min_scaled_height) {
3060 fh->height = c.min_scaled_height;
3061 btv->init.height = c.min_scaled_height;
3062 } else if (fh->height > c.max_scaled_height) {
3063 fh->height = c.max_scaled_height;
3064 btv->init.height = c.max_scaled_height;
3067 mutex_unlock(&fh->cap.lock);
3072 case VIDIOC_ENUMSTD:
3075 case VIDIOC_ENUMINPUT:
3076 case VIDIOC_G_INPUT:
3077 case VIDIOC_S_INPUT:
3078 case VIDIOC_S_TUNER:
3079 case VIDIOC_G_FREQUENCY:
3080 case VIDIOC_S_FREQUENCY:
3081 case VIDIOC_LOG_STATUS:
3084 case VIDIOC_DBG_G_REGISTER:
3085 case VIDIOC_DBG_S_REGISTER:
3086 return bttv_common_ioctls(btv,cmd,arg);
3088 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
3093 fh_unlock_and_return:
3094 mutex_unlock(&fh->cap.lock);
3097 btv_unlock_and_return:
3098 mutex_unlock(&btv->lock);
3102 static int bttv_ioctl(struct inode *inode, struct file *file,
3103 unsigned int cmd, unsigned long arg)
3105 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
3108 static ssize_t bttv_read(struct file *file, char __user *data,
3109 size_t count, loff_t *ppos)
3111 struct bttv_fh *fh = file->private_data;
3114 if (fh->btv->errors)
3115 bttv_reinit_bt848(fh->btv);
3116 dprintk("bttv%d: read count=%d type=%s\n",
3117 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3120 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3121 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3122 /* VIDEO_READ in use by another fh,
3123 or VIDEO_STREAM by any fh. */
3126 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3127 file->f_flags & O_NONBLOCK);
3128 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
3130 case V4L2_BUF_TYPE_VBI_CAPTURE:
3131 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3133 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3134 file->f_flags & O_NONBLOCK);
3142 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3144 struct bttv_fh *fh = file->private_data;
3145 struct bttv_buffer *buf;
3146 enum v4l2_field field;
3148 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3149 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3151 return videobuf_poll_stream(file, &fh->vbi, wait);
3154 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3155 /* streaming capture */
3156 if (list_empty(&fh->cap.stream))
3158 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3160 /* read() capture */
3161 mutex_lock(&fh->cap.lock);
3162 if (NULL == fh->cap.read_buf) {
3163 /* need to capture a new frame */
3164 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM)) {
3165 mutex_unlock(&fh->cap.lock);
3168 fh->cap.read_buf = videobuf_pci_alloc(fh->cap.msize);
3169 if (NULL == fh->cap.read_buf) {
3170 mutex_unlock(&fh->cap.lock);
3173 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3174 field = videobuf_next_field(&fh->cap);
3175 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3176 kfree (fh->cap.read_buf);
3177 fh->cap.read_buf = NULL;
3178 mutex_unlock(&fh->cap.lock);
3181 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3182 fh->cap.read_off = 0;
3184 mutex_unlock(&fh->cap.lock);
3185 buf = (struct bttv_buffer*)fh->cap.read_buf;
3188 poll_wait(file, &buf->vb.done, wait);
3189 if (buf->vb.state == STATE_DONE ||
3190 buf->vb.state == STATE_ERROR)
3191 return POLLIN|POLLRDNORM;
3195 static int bttv_open(struct inode *inode, struct file *file)
3197 int minor = iminor(inode);
3198 struct bttv *btv = NULL;
3200 enum v4l2_buf_type type = 0;
3203 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3205 for (i = 0; i < bttv_num; i++) {
3206 if (bttvs[i].video_dev &&
3207 bttvs[i].video_dev->minor == minor) {
3209 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3212 if (bttvs[i].vbi_dev &&
3213 bttvs[i].vbi_dev->minor == minor) {
3215 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3222 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3223 btv->c.nr,v4l2_type_names[type]);
3225 /* allocate per filehandle data */
3226 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3229 file->private_data = fh;
3232 fh->ov.setup_ok = 0;
3233 v4l2_prio_open(&btv->prio,&fh->prio);
3235 videobuf_queue_pci_init(&fh->cap, &bttv_video_qops,
3236 btv->c.pci, &btv->s_lock,
3237 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3238 V4L2_FIELD_INTERLACED,
3239 sizeof(struct bttv_buffer),
3241 videobuf_queue_pci_init(&fh->vbi, &bttv_vbi_qops,
3242 btv->c.pci, &btv->s_lock,
3243 V4L2_BUF_TYPE_VBI_CAPTURE,
3245 sizeof(struct bttv_buffer),
3247 set_tvnorm(btv,btv->tvnorm);
3251 /* The V4L2 spec requires one global set of cropping parameters
3252 which only change on request. These are stored in btv->crop[1].
3253 However for compatibility with V4L apps and cropping unaware
3254 V4L2 apps we now reset the cropping parameters as seen through
3255 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3256 will use btv->crop[0], the default cropping parameters for the
3257 current video standard, and VIDIOC_S_FMT will not implicitely
3258 change the cropping parameters until VIDIOC_S_CROP has been
3260 fh->do_crop = !reset_crop; /* module parameter */
3262 /* Likewise there should be one global set of VBI capture
3263 parameters, but for compatibility with V4L apps and earlier
3264 driver versions each fh has its own parameters. */
3265 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3267 bttv_field_count(btv);
3271 static int bttv_release(struct inode *inode, struct file *file)
3273 struct bttv_fh *fh = file->private_data;
3274 struct bttv *btv = fh->btv;
3276 /* turn off overlay */
3277 if (check_btres(fh, RESOURCE_OVERLAY))
3278 bttv_switch_overlay(btv,fh,NULL);
3280 /* stop video capture */
3281 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3282 videobuf_streamoff(&fh->cap);
3283 free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
3285 if (fh->cap.read_buf) {
3286 buffer_release(&fh->cap,fh->cap.read_buf);
3287 kfree(fh->cap.read_buf);
3289 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3290 free_btres(btv, fh, RESOURCE_VIDEO_READ);
3293 /* stop vbi capture */
3294 if (check_btres(fh, RESOURCE_VBI)) {
3295 videobuf_stop(&fh->vbi);
3296 free_btres(btv,fh,RESOURCE_VBI);
3300 videobuf_mmap_free(&fh->cap);
3301 videobuf_mmap_free(&fh->vbi);
3302 v4l2_prio_close(&btv->prio,&fh->prio);
3303 file->private_data = NULL;
3307 bttv_field_count(btv);
3312 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3314 struct bttv_fh *fh = file->private_data;
3316 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3317 fh->btv->c.nr, v4l2_type_names[fh->type],
3318 vma->vm_start, vma->vm_end - vma->vm_start);
3319 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3322 static const struct file_operations bttv_fops =
3324 .owner = THIS_MODULE,
3326 .release = bttv_release,
3327 .ioctl = bttv_ioctl,
3328 .compat_ioctl = v4l_compat_ioctl32,
3329 .llseek = no_llseek,
3335 static struct video_device bttv_video_template =
3338 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3339 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3344 static struct video_device bttv_vbi_template =
3346 .name = "bt848/878 vbi",
3347 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3352 /* ----------------------------------------------------------------------- */
3353 /* radio interface */
3355 static int radio_open(struct inode *inode, struct file *file)
3357 int minor = iminor(inode);
3358 struct bttv *btv = NULL;
3361 dprintk("bttv: open minor=%d\n",minor);
3363 for (i = 0; i < bttv_num; i++) {
3364 if (bttvs[i].radio_dev->minor == minor) {
3372 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3373 mutex_lock(&btv->lock);
3377 file->private_data = btv;
3379 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3380 audio_input(btv,TVAUDIO_INPUT_RADIO);
3382 mutex_unlock(&btv->lock);
3386 static int radio_release(struct inode *inode, struct file *file)
3388 struct bttv *btv = file->private_data;
3389 struct rds_command cmd;
3393 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3398 static int radio_do_ioctl(struct inode *inode, struct file *file,
3399 unsigned int cmd, void *arg)
3401 struct bttv *btv = file->private_data;
3404 case VIDIOC_QUERYCAP:
3406 struct v4l2_capability *cap = arg;
3408 memset(cap,0,sizeof(*cap));
3409 strcpy(cap->driver, "bttv");
3410 strlcpy(cap->card, btv->radio_dev->name,sizeof(cap->card));
3411 sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
3412 cap->version = BTTV_VERSION_CODE;
3413 cap->capabilities = V4L2_CAP_TUNER;
3416 case VIDIOC_G_TUNER:
3418 struct v4l2_tuner *t = arg;
3420 if (UNSET == bttv_tvcards[btv->c.type].tuner)
3424 mutex_lock(&btv->lock);
3425 memset(t,0,sizeof(*t));
3426 strcpy(t->name, "Radio");
3427 t->type = V4L2_TUNER_RADIO;
3429 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
3431 mutex_unlock(&btv->lock);
3435 case VIDIOC_S_TUNER:
3436 case VIDIOC_G_FREQUENCY:
3437 case VIDIOC_S_FREQUENCY:
3440 case VIDIOC_LOG_STATUS:
3441 case VIDIOC_DBG_G_REGISTER:
3442 case VIDIOC_DBG_S_REGISTER:
3443 return bttv_common_ioctls(btv,cmd,arg);
3445 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
3451 static int radio_ioctl(struct inode *inode, struct file *file,
3452 unsigned int cmd, unsigned long arg)
3454 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3457 static ssize_t radio_read(struct file *file, char __user *data,
3458 size_t count, loff_t *ppos)
3460 struct bttv *btv = file->private_data;
3461 struct rds_command cmd;
3462 cmd.block_count = count/3;
3464 cmd.instance = file;
3465 cmd.result = -ENODEV;
3467 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3472 static unsigned int radio_poll(struct file *file, poll_table *wait)
3474 struct bttv *btv = file->private_data;
3475 struct rds_command cmd;
3476 cmd.instance = file;
3477 cmd.event_list = wait;
3478 cmd.result = -ENODEV;
3479 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3484 static const struct file_operations radio_fops =
3486 .owner = THIS_MODULE,
3489 .release = radio_release,
3490 .ioctl = radio_ioctl,
3491 .llseek = no_llseek,
3495 static struct video_device radio_template =
3497 .name = "bt848/878 radio",
3498 .type = VID_TYPE_TUNER,
3499 .fops = &radio_fops,
3503 /* ----------------------------------------------------------------------- */
3504 /* some debug code */
3506 static int bttv_risc_decode(u32 risc)
3508 static char *instr[16] = {
3509 [ BT848_RISC_WRITE >> 28 ] = "write",
3510 [ BT848_RISC_SKIP >> 28 ] = "skip",
3511 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3512 [ BT848_RISC_JUMP >> 28 ] = "jump",
3513 [ BT848_RISC_SYNC >> 28 ] = "sync",
3514 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3515 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3516 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3518 static int incr[16] = {
3519 [ BT848_RISC_WRITE >> 28 ] = 2,
3520 [ BT848_RISC_JUMP >> 28 ] = 2,
3521 [ BT848_RISC_SYNC >> 28 ] = 2,
3522 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3523 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3524 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3526 static char *bits[] = {
3527 "be0", "be1", "be2", "be3/resync",
3528 "set0", "set1", "set2", "set3",
3529 "clr0", "clr1", "clr2", "clr3",
3530 "irq", "res", "eol", "sol",
3534 printk("0x%08x [ %s", risc,
3535 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3536 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3537 if (risc & (1 << (i + 12)))
3538 printk(" %s",bits[i]);
3539 printk(" count=%d ]\n", risc & 0xfff);
3540 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3543 static void bttv_risc_disasm(struct bttv *btv,
3544 struct btcx_riscmem *risc)
3548 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3549 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3550 for (i = 0; i < (risc->size >> 2); i += n) {
3551 printk("%s: 0x%lx: ", btv->c.name,
3552 (unsigned long)(risc->dma + (i<<2)));
3553 n = bttv_risc_decode(risc->cpu[i]);
3554 for (j = 1; j < n; j++)
3555 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3556 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3558 if (0 == risc->cpu[i])
3563 static void bttv_print_riscaddr(struct bttv *btv)
3565 printk(" main: %08Lx\n",
3566 (unsigned long long)btv->main.dma);
3567 printk(" vbi : o=%08Lx e=%08Lx\n",
3568 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3569 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3570 printk(" cap : o=%08Lx e=%08Lx\n",
3571 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3572 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3573 printk(" scr : o=%08Lx e=%08Lx\n",
3574 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3575 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3576 bttv_risc_disasm(btv, &btv->main);
3579 /* ----------------------------------------------------------------------- */
3582 static char *irq_name[] = {
3583 "FMTCHG", // format change detected (525 vs. 625)
3584 "VSYNC", // vertical sync (new field)
3585 "HSYNC", // horizontal sync
3586 "OFLOW", // chroma/luma AGC overflow
3587 "HLOCK", // horizontal lock changed
3588 "VPRES", // video presence changed
3590 "I2CDONE", // hw irc operation finished
3591 "GPINT", // gpio port triggered irq
3593 "RISCI", // risc instruction triggered irq
3594 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3595 "FTRGT", // pixel data fifo overrun
3596 "FDSR", // fifo data stream resyncronisation
3597 "PPERR", // parity error (data transfer)
3598 "RIPERR", // parity error (read risc instructions)
3599 "PABORT", // pci abort
3600 "OCERR", // risc instruction error
3601 "SCERR", // syncronisation error
3604 static void bttv_print_irqbits(u32 print, u32 mark)
3609 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3610 if (print & (1 << i))
3611 printk(" %s",irq_name[i]);
3612 if (mark & (1 << i))
3617 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3619 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3621 (unsigned long)btv->main.dma,
3622 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3623 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3626 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3627 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3628 "Ok, then this is harmless, don't worry ;)\n",
3632 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3634 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3640 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3642 struct bttv_buffer *item;
3644 memset(set,0,sizeof(*set));
3646 /* capture request ? */
3647 if (!list_empty(&btv->capture)) {
3649 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3650 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3652 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3655 /* capture request for other field ? */
3656 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3657 (item->vb.queue.next != &btv->capture)) {
3658 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3659 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3660 if (NULL == set->top &&
3661 V4L2_FIELD_TOP == item->vb.field) {
3664 if (NULL == set->bottom &&
3665 V4L2_FIELD_BOTTOM == item->vb.field) {
3668 if (NULL != set->top && NULL != set->bottom)
3674 /* screen overlay ? */
3675 if (NULL != btv->screen) {
3676 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3677 if (NULL == set->top && NULL == set->bottom) {
3678 set->top = btv->screen;
3679 set->bottom = btv->screen;
3682 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3684 set->top = btv->screen;
3686 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3687 NULL == set->bottom) {
3688 set->bottom = btv->screen;
3693 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3694 btv->c.nr,set->top, set->bottom,
3695 btv->screen,set->frame_irq,set->top_irq);
3700 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3701 struct bttv_buffer_set *curr, unsigned int state)
3705 do_gettimeofday(&ts);
3707 if (wakeup->top == wakeup->bottom) {
3708 if (NULL != wakeup->top && curr->top != wakeup->top) {
3710 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3711 wakeup->top->vb.ts = ts;
3712 wakeup->top->vb.field_count = btv->field_count;
3713 wakeup->top->vb.state = state;
3714 wake_up(&wakeup->top->vb.done);
3717 if (NULL != wakeup->top && curr->top != wakeup->top) {
3719 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3720 wakeup->top->vb.ts = ts;
3721 wakeup->top->vb.field_count = btv->field_count;
3722 wakeup->top->vb.state = state;
3723 wake_up(&wakeup->top->vb.done);
3725 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3727 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3728 wakeup->bottom->vb.ts = ts;
3729 wakeup->bottom->vb.field_count = btv->field_count;
3730 wakeup->bottom->vb.state = state;
3731 wake_up(&wakeup->bottom->vb.done);
3737 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3745 do_gettimeofday(&ts);
3747 wakeup->vb.field_count = btv->field_count;
3748 wakeup->vb.state = state;
3749 wake_up(&wakeup->vb.done);
3752 static void bttv_irq_timeout(unsigned long data)
3754 struct bttv *btv = (struct bttv *)data;
3755 struct bttv_buffer_set old,new;
3756 struct bttv_buffer *ovbi;
3757 struct bttv_buffer *item;
3758 unsigned long flags;
3761 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3762 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3763 btread(BT848_RISC_COUNT));
3764 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3768 spin_lock_irqsave(&btv->s_lock,flags);
3770 /* deactivate stuff */
3771 memset(&new,0,sizeof(new));
3777 bttv_buffer_activate_video(btv, &new);
3778 bttv_buffer_activate_vbi(btv, NULL);
3779 bttv_set_dma(btv, 0);
3782 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3783 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3785 /* cancel all outstanding capture / vbi requests */
3786 while (!list_empty(&btv->capture)) {
3787 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3788 list_del(&item->vb.queue);
3789 item->vb.state = STATE_ERROR;
3790 wake_up(&item->vb.done);
3792 while (!list_empty(&btv->vcapture)) {
3793 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3794 list_del(&item->vb.queue);
3795 item->vb.state = STATE_ERROR;
3796 wake_up(&item->vb.done);
3800 spin_unlock_irqrestore(&btv->s_lock,flags);
3804 bttv_irq_wakeup_top(struct bttv *btv)
3806 struct bttv_buffer *wakeup = btv->curr.top;
3811 spin_lock(&btv->s_lock);
3812 btv->curr.top_irq = 0;
3813 btv->curr.top = NULL;
3814 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3816 do_gettimeofday(&wakeup->vb.ts);
3817 wakeup->vb.field_count = btv->field_count;
3818 wakeup->vb.state = STATE_DONE;
3819 wake_up(&wakeup->vb.done);
3820 spin_unlock(&btv->s_lock);
3823 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3827 if (rc > risc->dma + risc->size)
3833 bttv_irq_switch_video(struct bttv *btv)
3835 struct bttv_buffer_set new;
3836 struct bttv_buffer_set old;
3839 spin_lock(&btv->s_lock);
3841 /* new buffer set */
3842 bttv_irq_next_video(btv, &new);
3843 rc = btread(BT848_RISC_COUNT);
3844 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3845 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3848 bttv_irq_debug_low_latency(btv, rc);
3849 spin_unlock(&btv->s_lock);
3856 btv->loop_irq &= ~1;
3857 bttv_buffer_activate_video(btv, &new);
3858 bttv_set_dma(btv, 0);
3861 if (UNSET != btv->new_input) {
3862 video_mux(btv,btv->new_input);
3863 btv->new_input = UNSET;
3866 /* wake up finished buffers */
3867 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3868 spin_unlock(&btv->s_lock);
3872 bttv_irq_switch_vbi(struct bttv *btv)
3874 struct bttv_buffer *new = NULL;
3875 struct bttv_buffer *old;
3878 spin_lock(&btv->s_lock);
3880 if (!list_empty(&btv->vcapture))
3881 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3884 rc = btread(BT848_RISC_COUNT);
3885 if (NULL != old && (is_active(&old->top, rc) ||
3886 is_active(&old->bottom, rc))) {
3889 bttv_irq_debug_low_latency(btv, rc);
3890 spin_unlock(&btv->s_lock);
3896 btv->loop_irq &= ~4;
3897 bttv_buffer_activate_vbi(btv, new);
3898 bttv_set_dma(btv, 0);
3900 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3901 spin_unlock(&btv->s_lock);
3904 static irqreturn_t bttv_irq(int irq, void *dev_id)
3912 btv=(struct bttv *)dev_id;
3914 if (btv->custom_irq)
3915 handled = btv->custom_irq(btv);
3919 /* get/clear interrupt status bits */
3920 stat=btread(BT848_INT_STAT);
3921 astat=stat&btread(BT848_INT_MASK);
3925 btwrite(stat,BT848_INT_STAT);
3927 /* get device status bits */
3928 dstat=btread(BT848_DSTATUS);
3931 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3932 "riscs=%x, riscc=%08x, ",
3933 btv->c.nr, count, btv->field_count,
3934 stat>>28, btread(BT848_RISC_COUNT));
3935 bttv_print_irqbits(stat,astat);
3936 if (stat & BT848_INT_HLOCK)
3937 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3939 if (stat & BT848_INT_VPRES)
3940 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3942 if (stat & BT848_INT_FMTCHG)
3943 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3948 if (astat&BT848_INT_VSYNC)
3951 if ((astat & BT848_INT_GPINT) && btv->remote) {
3952 wake_up(&btv->gpioq);
3953 bttv_input_irq(btv);
3956 if (astat & BT848_INT_I2CDONE) {
3957 btv->i2c_done = stat;
3958 wake_up(&btv->i2c_queue);
3961 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3962 bttv_irq_switch_vbi(btv);
3964 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3965 bttv_irq_wakeup_top(btv);
3967 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3968 bttv_irq_switch_video(btv);
3970 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3971 audio_mute(btv, btv->mute); /* trigger automute */
3973 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3974 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3975 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3976 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3977 btread(BT848_RISC_COUNT));
3978 bttv_print_irqbits(stat,astat);
3981 bttv_print_riscaddr(btv);
3983 if (fdsr && astat & BT848_INT_FDSR) {
3984 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3985 btv->c.nr,btread(BT848_RISC_COUNT));
3987 bttv_print_riscaddr(btv);
3993 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3994 btwrite(0, BT848_INT_MASK);
3997 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4000 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4002 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4006 bttv_print_irqbits(stat,astat);
4014 return IRQ_RETVAL(handled);
4018 /* ----------------------------------------------------------------------- */
4019 /* initialitation */
4021 static struct video_device *vdev_init(struct bttv *btv,
4022 struct video_device *template,
4025 struct video_device *vfd;
4027 vfd = video_device_alloc();
4032 vfd->dev = &btv->c.pci->dev;
4033 vfd->release = video_device_release;
4034 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4035 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4036 type, bttv_tvcards[btv->c.type].name);
4040 static void bttv_unregister_video(struct bttv *btv)
4042 if (btv->video_dev) {
4043 if (-1 != btv->video_dev->minor)
4044 video_unregister_device(btv->video_dev);
4046 video_device_release(btv->video_dev);
4047 btv->video_dev = NULL;
4050 if (-1 != btv->vbi_dev->minor)
4051 video_unregister_device(btv->vbi_dev);
4053 video_device_release(btv->vbi_dev);
4054 btv->vbi_dev = NULL;
4056 if (btv->radio_dev) {
4057 if (-1 != btv->radio_dev->minor)
4058 video_unregister_device(btv->radio_dev);
4060 video_device_release(btv->radio_dev);
4061 btv->radio_dev = NULL;
4065 /* register video4linux devices */
4066 static int __devinit bttv_register_video(struct bttv *btv)
4068 if (no_overlay <= 0) {
4069 bttv_video_template.type |= VID_TYPE_OVERLAY;
4071 printk("bttv: Overlay support disabled.\n");
4075 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4076 if (NULL == btv->video_dev)
4078 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
4080 printk(KERN_INFO "bttv%d: registered device video%d\n",
4081 btv->c.nr,btv->video_dev->minor & 0x1f);
4082 if (device_create_file(&btv->video_dev->class_dev,
4083 &dev_attr_card)<0) {
4084 printk(KERN_ERR "bttv%d: device_create_file 'card' "
4085 "failed\n", btv->c.nr);
4090 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
4091 if (NULL == btv->vbi_dev)
4093 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
4095 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
4096 btv->c.nr,btv->vbi_dev->minor & 0x1f);
4098 if (!btv->has_radio)
4101 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4102 if (NULL == btv->radio_dev)
4104 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
4106 printk(KERN_INFO "bttv%d: registered device radio%d\n",
4107 btv->c.nr,btv->radio_dev->minor & 0x1f);
4113 bttv_unregister_video(btv);
4118 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4119 /* response on cards with no firmware is not enabled by OF */
4120 static void pci_set_command(struct pci_dev *dev)
4122 #if defined(__powerpc__)
4125 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4126 cmd = (cmd | PCI_COMMAND_MEMORY );
4127 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4131 static int __devinit bttv_probe(struct pci_dev *dev,
4132 const struct pci_device_id *pci_id)
4138 if (bttv_num == BTTV_MAX)
4140 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
4141 btv=&bttvs[bttv_num];
4142 memset(btv,0,sizeof(*btv));
4143 btv->c.nr = bttv_num;
4144 sprintf(btv->c.name,"bttv%d",btv->c.nr);
4146 /* initialize structs / fill in defaults */
4147 mutex_init(&btv->lock);
4148 spin_lock_init(&btv->s_lock);
4149 spin_lock_init(&btv->gpio_lock);
4150 init_waitqueue_head(&btv->gpioq);
4151 init_waitqueue_head(&btv->i2c_queue);
4152 INIT_LIST_HEAD(&btv->c.subs);
4153 INIT_LIST_HEAD(&btv->capture);
4154 INIT_LIST_HEAD(&btv->vcapture);
4155 v4l2_prio_init(&btv->prio);
4157 init_timer(&btv->timeout);
4158 btv->timeout.function = bttv_irq_timeout;
4159 btv->timeout.data = (unsigned long)btv;
4162 btv->tuner_type = UNSET;
4163 btv->new_input = UNSET;
4164 btv->has_radio=radio[btv->c.nr];
4166 /* pci stuff (init, get irq/mmio, ... */
4168 btv->id = dev->device;
4169 if (pci_enable_device(dev)) {
4170 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4174 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
4175 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4179 if (!request_mem_region(pci_resource_start(dev,0),
4180 pci_resource_len(dev,0),
4182 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4184 (unsigned long long)pci_resource_start(dev,0));
4187 pci_set_master(dev);
4188 pci_set_command(dev);
4189 pci_set_drvdata(dev,btv);
4191 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4192 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4193 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4194 bttv_num,btv->id, btv->revision, pci_name(dev));
4195 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4196 btv->c.pci->irq, lat,
4197 (unsigned long long)pci_resource_start(dev,0));
4200 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4201 if (NULL == btv->bt848_mmio) {
4202 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4210 /* disable irqs, register irq handler */
4211 btwrite(0, BT848_INT_MASK);
4212 result = request_irq(btv->c.pci->irq, bttv_irq,
4213 IRQF_SHARED | IRQF_DISABLED,btv->c.name,(void *)btv);
4215 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4216 bttv_num,btv->c.pci->irq);
4220 if (0 != bttv_handle_chipset(btv)) {
4225 /* init options from insmod args */
4226 btv->opt_combfilter = combfilter;
4227 btv->opt_lumafilter = lumafilter;
4228 btv->opt_automute = automute;
4229 btv->opt_chroma_agc = chroma_agc;
4230 btv->opt_adc_crush = adc_crush;
4231 btv->opt_vcr_hack = vcr_hack;
4232 btv->opt_whitecrush_upper = whitecrush_upper;
4233 btv->opt_whitecrush_lower = whitecrush_lower;
4234 btv->opt_uv_ratio = uv_ratio;
4235 btv->opt_full_luma_range = full_luma_range;
4236 btv->opt_coring = coring;
4238 /* fill struct bttv with some useful defaults */
4239 btv->init.btv = btv;
4240 btv->init.ov.w.width = 320;
4241 btv->init.ov.w.height = 240;
4242 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4243 btv->init.width = 320;
4244 btv->init.height = 240;
4247 /* initialize hardware */
4249 bttv_gpio_tracking(btv,"pre-init");
4251 bttv_risc_init_main(btv);
4255 btwrite(0x00, BT848_GPIO_REG_INP);
4256 btwrite(0x00, BT848_GPIO_OUT_EN);
4258 bttv_gpio_tracking(btv,"init");
4260 /* needs to be done before i2c is registered */
4261 bttv_init_card1(btv);
4263 /* register i2c + gpio */
4266 /* some card-specific stuff (needs working i2c) */
4267 bttv_init_card2(btv);
4270 /* register video4linux + input */
4271 if (!bttv_tvcards[btv->c.type].no_video) {
4272 bttv_register_video(btv);
4273 bt848_bright(btv,32768);
4274 bt848_contrast(btv,32768);
4275 bt848_hue(btv,32768);
4276 bt848_sat(btv,32768);
4278 set_input(btv, 0, btv->tvnorm);
4279 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4280 btv->crop[1] = btv->crop[0]; /* current = default */
4281 disclaim_vbi_lines(btv);
4282 disclaim_video_lines(btv);
4285 /* add subdevices and autoload dvb-bt8xx if needed */
4286 if (bttv_tvcards[btv->c.type].has_dvb) {
4287 bttv_sub_add_device(&btv->c, "dvb");
4288 request_modules(btv);
4291 bttv_input_init(btv);
4293 /* everything is fine */
4298 free_irq(btv->c.pci->irq,btv);
4301 if (btv->bt848_mmio)
4302 iounmap(btv->bt848_mmio);
4303 release_mem_region(pci_resource_start(btv->c.pci,0),
4304 pci_resource_len(btv->c.pci,0));
4305 pci_set_drvdata(dev,NULL);
4309 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4311 struct bttv *btv = pci_get_drvdata(pci_dev);
4314 printk("bttv%d: unloading\n",btv->c.nr);
4316 /* shutdown everything (DMA+IRQs) */
4317 btand(~15, BT848_GPIO_DMA_CTL);
4318 btwrite(0, BT848_INT_MASK);
4319 btwrite(~0x0, BT848_INT_STAT);
4320 btwrite(0x0, BT848_GPIO_OUT_EN);
4322 bttv_gpio_tracking(btv,"cleanup");
4324 /* tell gpio modules we are leaving ... */
4326 wake_up(&btv->gpioq);
4327 bttv_input_fini(btv);
4328 bttv_sub_del_devices(&btv->c);
4330 /* unregister i2c_bus + input */
4333 /* unregister video4linux */
4334 bttv_unregister_video(btv);
4336 /* free allocated memory */
4337 btcx_riscmem_free(btv->c.pci,&btv->main);
4339 /* free ressources */
4340 free_irq(btv->c.pci->irq,btv);
4341 iounmap(btv->bt848_mmio);
4342 release_mem_region(pci_resource_start(btv->c.pci,0),
4343 pci_resource_len(btv->c.pci,0));
4345 pci_set_drvdata(pci_dev, NULL);
4350 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4352 struct bttv *btv = pci_get_drvdata(pci_dev);
4353 struct bttv_buffer_set idle;
4354 unsigned long flags;
4356 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4358 /* stop dma + irqs */
4359 spin_lock_irqsave(&btv->s_lock,flags);
4360 memset(&idle, 0, sizeof(idle));
4361 btv->state.video = btv->curr;
4362 btv->state.vbi = btv->cvbi;
4363 btv->state.loop_irq = btv->loop_irq;
4366 bttv_buffer_activate_video(btv, &idle);
4367 bttv_buffer_activate_vbi(btv, NULL);
4368 bttv_set_dma(btv, 0);
4369 btwrite(0, BT848_INT_MASK);
4370 spin_unlock_irqrestore(&btv->s_lock,flags);
4372 /* save bt878 state */
4373 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4374 btv->state.gpio_data = gpio_read();
4376 /* save pci state */
4377 pci_save_state(pci_dev);
4378 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4379 pci_disable_device(pci_dev);
4380 btv->state.disabled = 1;
4385 static int bttv_resume(struct pci_dev *pci_dev)
4387 struct bttv *btv = pci_get_drvdata(pci_dev);
4388 unsigned long flags;
4391 dprintk("bttv%d: resume\n", btv->c.nr);
4393 /* restore pci state */
4394 if (btv->state.disabled) {
4395 err=pci_enable_device(pci_dev);
4397 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4401 btv->state.disabled = 0;
4403 err=pci_set_power_state(pci_dev, PCI_D0);
4405 pci_disable_device(pci_dev);
4406 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4408 btv->state.disabled = 1;
4412 pci_restore_state(pci_dev);
4414 /* restore bt878 state */
4415 bttv_reinit_bt848(btv);
4416 gpio_inout(0xffffff, btv->state.gpio_enable);
4417 gpio_write(btv->state.gpio_data);
4420 spin_lock_irqsave(&btv->s_lock,flags);
4421 btv->curr = btv->state.video;
4422 btv->cvbi = btv->state.vbi;
4423 btv->loop_irq = btv->state.loop_irq;
4424 bttv_buffer_activate_video(btv, &btv->curr);
4425 bttv_buffer_activate_vbi(btv, btv->cvbi);
4426 bttv_set_dma(btv, 0);
4427 spin_unlock_irqrestore(&btv->s_lock,flags);
4432 static struct pci_device_id bttv_pci_tbl[] = {
4433 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4434 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4435 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4436 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4437 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4438 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4439 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4440 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4444 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4446 static struct pci_driver bttv_pci_driver = {
4448 .id_table = bttv_pci_tbl,
4449 .probe = bttv_probe,
4450 .remove = __devexit_p(bttv_remove),
4452 .suspend = bttv_suspend,
4453 .resume = bttv_resume,
4457 static int __init bttv_init_module(void)
4463 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4464 (BTTV_VERSION_CODE >> 16) & 0xff,
4465 (BTTV_VERSION_CODE >> 8) & 0xff,
4466 BTTV_VERSION_CODE & 0xff);
4468 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4469 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4471 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4473 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4474 gbufsize = BTTV_MAX_FBUF;
4475 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4477 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4478 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4480 bttv_check_chipset();
4482 ret = bus_register(&bttv_sub_bus_type);
4484 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4487 return pci_register_driver(&bttv_pci_driver);
4490 static void __exit bttv_cleanup_module(void)
4492 pci_unregister_driver(&bttv_pci_driver);
4493 bus_unregister(&bttv_sub_bus_type);
4497 module_init(bttv_init_module);
4498 module_exit(bttv_cleanup_module);