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/v4l2-ioctl.h>
49 #include <media/tvaudio.h>
50 #include <media/msp3400.h>
52 #include <linux/dma-mapping.h>
55 #include <asm/byteorder.h>
57 #include <media/rds.h>
60 unsigned int bttv_num; /* number of Bt848s in use */
61 struct bttv bttvs[BTTV_MAX];
63 unsigned int bttv_debug;
64 unsigned int bttv_verbose = 1;
65 unsigned int bttv_gpio;
67 /* config variables */
69 static unsigned int bigendian=1;
71 static unsigned int bigendian;
73 static unsigned int radio[BTTV_MAX];
74 static unsigned int irq_debug;
75 static unsigned int gbuffers = 8;
76 static unsigned int gbufsize = 0x208000;
77 static unsigned int reset_crop = 1;
79 static int video_nr = -1;
80 static int radio_nr = -1;
81 static int vbi_nr = -1;
82 static int debug_latency;
84 static unsigned int fdsr;
87 static unsigned int combfilter;
88 static unsigned int lumafilter;
89 static unsigned int automute = 1;
90 static unsigned int chroma_agc;
91 static unsigned int adc_crush = 1;
92 static unsigned int whitecrush_upper = 0xCF;
93 static unsigned int whitecrush_lower = 0x7F;
94 static unsigned int vcr_hack;
95 static unsigned int irq_iswitch;
96 static unsigned int uv_ratio = 50;
97 static unsigned int full_luma_range;
98 static unsigned int coring;
99 extern int no_overlay;
101 /* API features (turn on/off stuff for testing) */
102 static unsigned int v4l2 = 1;
105 module_param(bttv_verbose, int, 0644);
106 module_param(bttv_gpio, int, 0644);
107 module_param(bttv_debug, int, 0644);
108 module_param(irq_debug, int, 0644);
109 module_param(debug_latency, int, 0644);
111 module_param(fdsr, int, 0444);
112 module_param(video_nr, int, 0444);
113 module_param(radio_nr, int, 0444);
114 module_param(vbi_nr, int, 0444);
115 module_param(gbuffers, int, 0444);
116 module_param(gbufsize, int, 0444);
117 module_param(reset_crop, int, 0444);
119 module_param(v4l2, int, 0644);
120 module_param(bigendian, int, 0644);
121 module_param(irq_iswitch, int, 0644);
122 module_param(combfilter, int, 0444);
123 module_param(lumafilter, int, 0444);
124 module_param(automute, int, 0444);
125 module_param(chroma_agc, int, 0444);
126 module_param(adc_crush, int, 0444);
127 module_param(whitecrush_upper, int, 0444);
128 module_param(whitecrush_lower, int, 0444);
129 module_param(vcr_hack, int, 0444);
130 module_param(uv_ratio, int, 0444);
131 module_param(full_luma_range, int, 0444);
132 module_param(coring, int, 0444);
134 module_param_array(radio, int, NULL, 0444);
136 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
137 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
138 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
139 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
140 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
141 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
142 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
143 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
144 MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
145 "is 1 (yes) for compatibility with older applications");
146 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
147 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
148 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
149 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
150 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
151 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
152 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
153 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
154 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
155 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
157 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
158 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
159 MODULE_LICENSE("GPL");
161 /* ----------------------------------------------------------------------- */
164 static ssize_t show_card(struct device *cd,
165 struct device_attribute *attr, char *buf)
167 struct video_device *vfd = container_of(cd, struct video_device, dev);
168 struct bttv *btv = dev_get_drvdata(vfd->parent);
169 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
171 static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
173 /* ----------------------------------------------------------------------- */
174 /* dvb auto-load setup */
175 #if defined(CONFIG_MODULES) && defined(MODULE)
176 static void request_module_async(struct work_struct *work)
178 request_module("dvb-bt8xx");
181 static void request_modules(struct bttv *dev)
183 INIT_WORK(&dev->request_module_wk, request_module_async);
184 schedule_work(&dev->request_module_wk);
187 #define request_modules(dev)
188 #endif /* CONFIG_MODULES */
191 /* ----------------------------------------------------------------------- */
194 /* special timing tables from conexant... */
195 static u8 SRAM_Table[][60] =
197 /* PAL digital input over GPIO[7:0] */
199 45, // 45 bytes following
200 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
201 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
202 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
203 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
204 0x37,0x00,0xAF,0x21,0x00
206 /* NTSC digital input over GPIO[7:0] */
208 51, // 51 bytes following
209 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
210 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
211 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
212 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
213 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
216 // TGB_NTSC392 // quartzsight
217 // This table has been modified to be used for Fusion Rev D
219 0x2A, // size of table = 42
220 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
221 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
222 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
223 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
228 /* minhdelayx1 first video pixel we can capture on a line and
229 hdelayx1 start of active video, both relative to rising edge of
230 /HRESET pulse (0H) in 1 / fCLKx1.
231 swidth width of active video and
232 totalwidth total line width, both in 1 / fCLKx1.
233 sqwidth total line width in square pixels.
234 vdelay start of active video in 2 * field lines relative to
235 trailing edge of /VRESET pulse (VDELAY register).
236 sheight height of active video in 2 * field lines.
237 videostart0 ITU-R frame line number of the line corresponding
238 to vdelay in the first field. */
239 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
240 vdelay, sheight, videostart0) \
241 .cropcap.bounds.left = minhdelayx1, \
242 /* * 2 because vertically we count field lines times two, */ \
243 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
244 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
245 /* 4 is a safety margin at the end of the line. */ \
246 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
247 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
248 .cropcap.defrect.left = hdelayx1, \
249 .cropcap.defrect.top = (videostart0) * 2, \
250 .cropcap.defrect.width = swidth, \
251 .cropcap.defrect.height = sheight, \
252 .cropcap.pixelaspect.numerator = totalwidth, \
253 .cropcap.pixelaspect.denominator = sqwidth,
255 const struct bttv_tvnorm bttv_tvnorms[] = {
257 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
258 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
260 .v4l2_id = V4L2_STD_PAL,
268 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
269 .scaledtwidth = 1135,
273 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
275 /* ITU-R frame line number of the first VBI line
276 we can capture, of the first and second field.
277 The last line is determined by cropcap.bounds. */
278 .vbistart = { 7, 320 },
279 CROPCAP(/* minhdelayx1 */ 68,
281 /* Should be (768 * 1135 + 944 / 2) / 944.
282 cropcap.defrect is used for image width
283 checks, so we keep the old value 924. */
285 /* totalwidth */ 1135,
289 /* videostart0 */ 23)
290 /* bt878 (and bt848?) can capture another
291 line below active video. */
292 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
294 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
302 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
307 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
309 .vbistart = { 10, 273 },
310 CROPCAP(/* minhdelayx1 */ 68,
312 /* Should be (640 * 910 + 780 / 2) / 780? */
314 /* totalwidth */ 910,
318 /* videostart0 */ 23)
320 .v4l2_id = V4L2_STD_SECAM,
328 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
329 .scaledtwidth = 1135,
334 .sram = 0, /* like PAL, correct? */
335 .vbistart = { 7, 320 },
336 CROPCAP(/* minhdelayx1 */ 68,
339 /* totalwidth */ 1135,
343 /* videostart0 */ 23)
345 .v4l2_id = V4L2_STD_PAL_Nc,
353 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
360 .vbistart = { 7, 320 },
361 CROPCAP(/* minhdelayx1 */ 68,
363 /* swidth */ (640 * 910 + 780 / 2) / 780,
364 /* totalwidth */ 910,
368 /* videostart0 */ 23)
370 .v4l2_id = V4L2_STD_PAL_M,
378 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
385 .vbistart = { 10, 273 },
386 CROPCAP(/* minhdelayx1 */ 68,
388 /* swidth */ (640 * 910 + 780 / 2) / 780,
389 /* totalwidth */ 910,
393 /* videostart0 */ 23)
395 .v4l2_id = V4L2_STD_PAL_N,
403 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
410 .vbistart = { 7, 320 },
411 CROPCAP(/* minhdelayx1 */ 68,
413 /* swidth */ (768 * 1135 + 944 / 2) / 944,
414 /* totalwidth */ 1135,
418 /* videostart0 */ 23)
420 .v4l2_id = V4L2_STD_NTSC_M_JP,
428 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
435 .vbistart = { 10, 273 },
436 CROPCAP(/* minhdelayx1 */ 68,
438 /* swidth */ (640 * 910 + 780 / 2) / 780,
439 /* totalwidth */ 910,
443 /* videostart0 */ 23)
445 /* that one hopefully works with the strange timing
446 * which video recorders produce when playing a NTSC
447 * tape on a PAL TV ... */
448 .v4l2_id = V4L2_STD_PAL_60,
456 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
457 .scaledtwidth = 1135,
464 .vbistart = { 10, 273 },
465 CROPCAP(/* minhdelayx1 */ 68,
468 /* totalwidth */ 1135,
472 /* videostart0 */ 23)
475 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
477 /* ----------------------------------------------------------------------- */
479 packed pixel formats must come first */
480 static const struct bttv_format formats[] = {
482 .name = "8 bpp, gray",
483 .fourcc = V4L2_PIX_FMT_GREY,
484 .btformat = BT848_COLOR_FMT_Y8,
486 .flags = FORMAT_FLAGS_PACKED,
488 .name = "8 bpp, dithered color",
489 .fourcc = V4L2_PIX_FMT_HI240,
490 .btformat = BT848_COLOR_FMT_RGB8,
492 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
494 .name = "15 bpp RGB, le",
495 .fourcc = V4L2_PIX_FMT_RGB555,
496 .btformat = BT848_COLOR_FMT_RGB15,
498 .flags = FORMAT_FLAGS_PACKED,
500 .name = "15 bpp RGB, be",
501 .fourcc = V4L2_PIX_FMT_RGB555X,
502 .btformat = BT848_COLOR_FMT_RGB15,
503 .btswap = 0x03, /* byteswap */
505 .flags = FORMAT_FLAGS_PACKED,
507 .name = "16 bpp RGB, le",
508 .fourcc = V4L2_PIX_FMT_RGB565,
509 .btformat = BT848_COLOR_FMT_RGB16,
511 .flags = FORMAT_FLAGS_PACKED,
513 .name = "16 bpp RGB, be",
514 .fourcc = V4L2_PIX_FMT_RGB565X,
515 .btformat = BT848_COLOR_FMT_RGB16,
516 .btswap = 0x03, /* byteswap */
518 .flags = FORMAT_FLAGS_PACKED,
520 .name = "24 bpp RGB, le",
521 .fourcc = V4L2_PIX_FMT_BGR24,
522 .btformat = BT848_COLOR_FMT_RGB24,
524 .flags = FORMAT_FLAGS_PACKED,
526 .name = "32 bpp RGB, le",
527 .fourcc = V4L2_PIX_FMT_BGR32,
528 .btformat = BT848_COLOR_FMT_RGB32,
530 .flags = FORMAT_FLAGS_PACKED,
532 .name = "32 bpp RGB, be",
533 .fourcc = V4L2_PIX_FMT_RGB32,
534 .btformat = BT848_COLOR_FMT_RGB32,
535 .btswap = 0x0f, /* byte+word swap */
537 .flags = FORMAT_FLAGS_PACKED,
539 .name = "4:2:2, packed, YUYV",
540 .fourcc = V4L2_PIX_FMT_YUYV,
541 .btformat = BT848_COLOR_FMT_YUY2,
543 .flags = FORMAT_FLAGS_PACKED,
545 .name = "4:2:2, packed, YUYV",
546 .fourcc = V4L2_PIX_FMT_YUYV,
547 .btformat = BT848_COLOR_FMT_YUY2,
549 .flags = FORMAT_FLAGS_PACKED,
551 .name = "4:2:2, packed, UYVY",
552 .fourcc = V4L2_PIX_FMT_UYVY,
553 .btformat = BT848_COLOR_FMT_YUY2,
554 .btswap = 0x03, /* byteswap */
556 .flags = FORMAT_FLAGS_PACKED,
558 .name = "4:2:2, planar, Y-Cb-Cr",
559 .fourcc = V4L2_PIX_FMT_YUV422P,
560 .btformat = BT848_COLOR_FMT_YCrCb422,
562 .flags = FORMAT_FLAGS_PLANAR,
566 .name = "4:2:0, planar, Y-Cb-Cr",
567 .fourcc = V4L2_PIX_FMT_YUV420,
568 .btformat = BT848_COLOR_FMT_YCrCb422,
570 .flags = FORMAT_FLAGS_PLANAR,
574 .name = "4:2:0, planar, Y-Cr-Cb",
575 .fourcc = V4L2_PIX_FMT_YVU420,
576 .btformat = BT848_COLOR_FMT_YCrCb422,
578 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
582 .name = "4:1:1, planar, Y-Cb-Cr",
583 .fourcc = V4L2_PIX_FMT_YUV411P,
584 .btformat = BT848_COLOR_FMT_YCrCb411,
586 .flags = FORMAT_FLAGS_PLANAR,
590 .name = "4:1:0, planar, Y-Cb-Cr",
591 .fourcc = V4L2_PIX_FMT_YUV410,
592 .btformat = BT848_COLOR_FMT_YCrCb411,
594 .flags = FORMAT_FLAGS_PLANAR,
598 .name = "4:1:0, planar, Y-Cr-Cb",
599 .fourcc = V4L2_PIX_FMT_YVU410,
600 .btformat = BT848_COLOR_FMT_YCrCb411,
602 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
606 .name = "raw scanlines",
608 .btformat = BT848_COLOR_FMT_RAW,
610 .flags = FORMAT_FLAGS_RAW,
613 static const unsigned int FORMATS = ARRAY_SIZE(formats);
615 /* ----------------------------------------------------------------------- */
617 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
618 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
619 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
620 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
621 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
622 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
623 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
624 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
625 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
626 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
627 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
628 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
630 static const struct v4l2_queryctrl no_ctl = {
632 .flags = V4L2_CTRL_FLAG_DISABLED,
634 static const struct v4l2_queryctrl bttv_ctls[] = {
637 .id = V4L2_CID_BRIGHTNESS,
638 .name = "Brightness",
642 .default_value = 32768,
643 .type = V4L2_CTRL_TYPE_INTEGER,
645 .id = V4L2_CID_CONTRAST,
650 .default_value = 32768,
651 .type = V4L2_CTRL_TYPE_INTEGER,
653 .id = V4L2_CID_SATURATION,
654 .name = "Saturation",
658 .default_value = 32768,
659 .type = V4L2_CTRL_TYPE_INTEGER,
666 .default_value = 32768,
667 .type = V4L2_CTRL_TYPE_INTEGER,
671 .id = V4L2_CID_AUDIO_MUTE,
675 .type = V4L2_CTRL_TYPE_BOOLEAN,
677 .id = V4L2_CID_AUDIO_VOLUME,
682 .default_value = 65535,
683 .type = V4L2_CTRL_TYPE_INTEGER,
685 .id = V4L2_CID_AUDIO_BALANCE,
690 .default_value = 32768,
691 .type = V4L2_CTRL_TYPE_INTEGER,
693 .id = V4L2_CID_AUDIO_BASS,
698 .default_value = 32768,
699 .type = V4L2_CTRL_TYPE_INTEGER,
701 .id = V4L2_CID_AUDIO_TREBLE,
706 .default_value = 32768,
707 .type = V4L2_CTRL_TYPE_INTEGER,
709 /* --- private --- */
711 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
712 .name = "chroma agc",
715 .type = V4L2_CTRL_TYPE_BOOLEAN,
717 .id = V4L2_CID_PRIVATE_COMBFILTER,
718 .name = "combfilter",
721 .type = V4L2_CTRL_TYPE_BOOLEAN,
723 .id = V4L2_CID_PRIVATE_AUTOMUTE,
727 .type = V4L2_CTRL_TYPE_BOOLEAN,
729 .id = V4L2_CID_PRIVATE_LUMAFILTER,
730 .name = "luma decimation filter",
733 .type = V4L2_CTRL_TYPE_BOOLEAN,
735 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
739 .type = V4L2_CTRL_TYPE_BOOLEAN,
741 .id = V4L2_CID_PRIVATE_VCR_HACK,
745 .type = V4L2_CTRL_TYPE_BOOLEAN,
747 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
748 .name = "whitecrush upper",
752 .default_value = 0xCF,
753 .type = V4L2_CTRL_TYPE_INTEGER,
755 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
756 .name = "whitecrush lower",
760 .default_value = 0x7F,
761 .type = V4L2_CTRL_TYPE_INTEGER,
763 .id = V4L2_CID_PRIVATE_UV_RATIO,
769 .type = V4L2_CTRL_TYPE_INTEGER,
771 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
772 .name = "full luma range",
775 .type = V4L2_CTRL_TYPE_BOOLEAN,
777 .id = V4L2_CID_PRIVATE_CORING,
783 .type = V4L2_CTRL_TYPE_INTEGER,
790 static const struct v4l2_queryctrl *ctrl_by_id(int id)
794 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
795 if (bttv_ctls[i].id == id)
801 /* ----------------------------------------------------------------------- */
802 /* resource management */
805 RESOURCE_ allocated by freed by
807 VIDEO_READ bttv_read 1) bttv_read 2)
809 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
810 VIDIOC_QBUF 1) bttv_release
813 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
814 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
817 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
818 VIDIOC_QBUF 1) bttv_release
819 bttv_read, bttv_poll 1) 4)
821 1) The resource must be allocated when we enter buffer prepare functions
822 and remain allocated while buffers are in the DMA queue.
823 2) This is a single frame read.
824 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
825 RESOURCE_OVERLAY is allocated.
826 4) This is a continuous read, implies VIDIOC_STREAMON.
828 Note this driver permits video input and standard changes regardless if
829 resources are allocated.
832 #define VBI_RESOURCES (RESOURCE_VBI)
833 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
834 RESOURCE_VIDEO_STREAM | \
838 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
840 int xbits; /* mutual exclusive resources */
842 if (fh->resources & bit)
843 /* have it already allocated */
847 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
848 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
851 mutex_lock(&btv->lock);
852 if (btv->resources & xbits) {
853 /* no, someone else uses it */
857 if ((bit & VIDEO_RESOURCES)
858 && 0 == (btv->resources & VIDEO_RESOURCES)) {
859 /* Do crop - use current, don't - use default parameters. */
860 __s32 top = btv->crop[!!fh->do_crop].rect.top;
862 if (btv->vbi_end > top)
865 /* We cannot capture the same line as video and VBI data.
866 Claim scan lines crop[].rect.top to bottom. */
867 btv->crop_start = top;
868 } else if (bit & VBI_RESOURCES) {
869 __s32 end = fh->vbi_fmt.end;
871 if (end > btv->crop_start)
874 /* Claim scan lines above fh->vbi_fmt.end. */
878 /* it's free, grab it */
879 fh->resources |= bit;
880 btv->resources |= bit;
881 mutex_unlock(&btv->lock);
885 mutex_unlock(&btv->lock);
890 int check_btres(struct bttv_fh *fh, int bit)
892 return (fh->resources & bit);
896 int locked_btres(struct bttv *btv, int bit)
898 return (btv->resources & bit);
901 /* Call with btv->lock down. */
903 disclaim_vbi_lines(struct bttv *btv)
908 /* Call with btv->lock down. */
910 disclaim_video_lines(struct bttv *btv)
912 const struct bttv_tvnorm *tvnorm;
915 tvnorm = &bttv_tvnorms[btv->tvnorm];
916 btv->crop_start = tvnorm->cropcap.bounds.top
917 + tvnorm->cropcap.bounds.height;
919 /* VBI capturing ends at VDELAY, start of video capturing, no
920 matter how many lines the VBI RISC program expects. When video
921 capturing is off, it shall no longer "preempt" VBI capturing,
922 so we set VDELAY to maximum. */
923 crop = btread(BT848_E_CROP) | 0xc0;
924 btwrite(crop, BT848_E_CROP);
925 btwrite(0xfe, BT848_E_VDELAY_LO);
926 btwrite(crop, BT848_O_CROP);
927 btwrite(0xfe, BT848_O_VDELAY_LO);
931 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
933 if ((fh->resources & bits) != bits) {
934 /* trying to free ressources not allocated by us ... */
935 printk("bttv: BUG! (btres)\n");
937 mutex_lock(&btv->lock);
938 fh->resources &= ~bits;
939 btv->resources &= ~bits;
941 bits = btv->resources;
943 if (0 == (bits & VIDEO_RESOURCES))
944 disclaim_video_lines(btv);
946 if (0 == (bits & VBI_RESOURCES))
947 disclaim_vbi_lines(btv);
949 mutex_unlock(&btv->lock);
952 /* ----------------------------------------------------------------------- */
953 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
955 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
956 PLL_X = Reference pre-divider (0=1, 1=2)
957 PLL_C = Post divider (0=6, 1=4)
958 PLL_I = Integer input
959 PLL_F = Fractional input
961 F_input = 28.636363 MHz:
962 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
965 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
967 unsigned char fl, fh, fi;
969 /* prevent overflows */
982 btwrite(fl, BT848_PLL_F_LO);
983 btwrite(fh, BT848_PLL_F_HI);
984 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
987 static void set_pll(struct bttv *btv)
991 if (!btv->pll.pll_crystal)
994 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
995 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
999 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1001 if (btv->pll.pll_current == 0)
1003 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
1004 btv->c.nr,btv->pll.pll_ifreq);
1005 btwrite(0x00,BT848_TGCTRL);
1006 btwrite(0x00,BT848_PLL_XCI);
1007 btv->pll.pll_current = 0;
1011 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
1012 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1013 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1015 for (i=0; i<10; i++) {
1016 /* Let other people run while the PLL stabilizes */
1020 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1021 btwrite(0,BT848_DSTATUS);
1023 btwrite(0x08,BT848_TGCTRL);
1024 btv->pll.pll_current = btv->pll.pll_ofreq;
1025 bttv_printk(" ok\n");
1029 btv->pll.pll_current = -1;
1030 bttv_printk("failed\n");
1034 /* used to switch between the bt848's analog/digital video capture modes */
1035 static void bt848A_set_timing(struct bttv *btv)
1038 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1039 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1041 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
1042 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1043 btv->c.nr,table_idx);
1045 /* timing change...reset timing generator address */
1046 btwrite(0x00, BT848_TGCTRL);
1047 btwrite(0x02, BT848_TGCTRL);
1048 btwrite(0x00, BT848_TGCTRL);
1050 len=SRAM_Table[table_idx][0];
1051 for(i = 1; i <= len; i++)
1052 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1053 btv->pll.pll_ofreq = 27000000;
1056 btwrite(0x11, BT848_TGCTRL);
1057 btwrite(0x41, BT848_DVSIF);
1059 btv->pll.pll_ofreq = fsc;
1061 btwrite(0x0, BT848_DVSIF);
1065 /* ----------------------------------------------------------------------- */
1067 static void bt848_bright(struct bttv *btv, int bright)
1071 // printk("bttv: set bright: %d\n",bright); // DEBUG
1072 btv->bright = bright;
1074 /* We want -128 to 127 we get 0-65535 */
1075 value = (bright >> 8) - 128;
1076 btwrite(value & 0xff, BT848_BRIGHT);
1079 static void bt848_hue(struct bttv *btv, int hue)
1086 value = (hue >> 8) - 128;
1087 btwrite(value & 0xff, BT848_HUE);
1090 static void bt848_contrast(struct bttv *btv, int cont)
1094 btv->contrast = cont;
1097 value = (cont >> 7);
1098 hibit = (value >> 6) & 4;
1099 btwrite(value & 0xff, BT848_CONTRAST_LO);
1100 btaor(hibit, ~4, BT848_E_CONTROL);
1101 btaor(hibit, ~4, BT848_O_CONTROL);
1104 static void bt848_sat(struct bttv *btv, int color)
1106 int val_u,val_v,hibits;
1108 btv->saturation = color;
1110 /* 0-511 for the color */
1111 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1112 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1113 hibits = (val_u >> 7) & 2;
1114 hibits |= (val_v >> 8) & 1;
1115 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1116 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1117 btaor(hibits, ~3, BT848_E_CONTROL);
1118 btaor(hibits, ~3, BT848_O_CONTROL);
1121 /* ----------------------------------------------------------------------- */
1124 video_mux(struct bttv *btv, unsigned int input)
1128 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1131 /* needed by RemoteVideo MX */
1132 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1134 gpio_inout(mask2,mask2);
1136 if (input == btv->svhs) {
1137 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1138 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1140 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1141 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1143 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
1144 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1145 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1146 btv->c.nr,input,mux);
1148 /* card specific hook */
1149 if(bttv_tvcards[btv->c.type].muxsel_hook)
1150 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1154 static char *audio_modes[] = {
1155 "audio: tuner", "audio: radio", "audio: extern",
1156 "audio: intern", "audio: mute"
1160 audio_mux(struct bttv *btv, int input, int mute)
1162 int gpio_val, signal;
1163 struct v4l2_control ctrl;
1164 struct i2c_client *c;
1166 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1167 bttv_tvcards[btv->c.type].gpiomask);
1168 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1174 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1177 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1179 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1181 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1183 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1187 ctrl.id = V4L2_CID_AUDIO_MUTE;
1188 ctrl.value = btv->mute;
1189 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
1190 c = btv->i2c_msp34xx_client;
1192 struct v4l2_routing route;
1194 /* Note: the inputs tuner/radio/extern/intern are translated
1195 to msp routings. This assumes common behavior for all msp3400
1196 based TV cards. When this assumption fails, then the
1197 specific MSP routing must be added to the card table.
1198 For now this is sufficient. */
1200 case TVAUDIO_INPUT_RADIO:
1201 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1202 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1204 case TVAUDIO_INPUT_EXTERN:
1205 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1206 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1208 case TVAUDIO_INPUT_INTERN:
1209 /* Yes, this is the same input as for RADIO. I doubt
1210 if this is ever used. The only board with an INTERN
1211 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1212 that was tested. My guess is that the whole INTERN
1213 input does not work. */
1214 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1215 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1217 case TVAUDIO_INPUT_TUNER:
1219 /* This is the only card that uses TUNER2, and afaik,
1220 is the only difference between the VOODOOTV_FM
1222 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1223 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1224 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1226 route.input = MSP_INPUT_DEFAULT;
1229 route.output = MSP_OUTPUT_DEFAULT;
1230 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1232 c = btv->i2c_tvaudio_client;
1234 struct v4l2_routing route;
1236 route.input = input;
1238 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1244 audio_mute(struct bttv *btv, int mute)
1246 return audio_mux(btv, btv->audio, mute);
1250 audio_input(struct bttv *btv, int input)
1252 return audio_mux(btv, input, btv->mute);
1256 bttv_crop_calc_limits(struct bttv_crop *c)
1258 /* Scale factor min. 1:1, max. 16:1. Min. image size
1259 48 x 32. Scaled width must be a multiple of 4. */
1262 /* For bug compatibility with VIDIOCGCAP and image
1263 size checks in earlier driver versions. */
1264 c->min_scaled_width = 48;
1265 c->min_scaled_height = 32;
1267 c->min_scaled_width =
1268 (max(48, c->rect.width >> 4) + 3) & ~3;
1269 c->min_scaled_height =
1270 max(32, c->rect.height >> 4);
1273 c->max_scaled_width = c->rect.width & ~3;
1274 c->max_scaled_height = c->rect.height;
1278 bttv_crop_reset(struct bttv_crop *c, int norm)
1280 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1281 bttv_crop_calc_limits(c);
1284 /* Call with btv->lock down. */
1286 set_tvnorm(struct bttv *btv, unsigned int norm)
1288 const struct bttv_tvnorm *tvnorm;
1291 if (norm < 0 || norm >= BTTV_TVNORMS)
1294 tvnorm = &bttv_tvnorms[norm];
1296 if (btv->tvnorm < 0 ||
1297 btv->tvnorm >= BTTV_TVNORMS ||
1298 0 != memcmp(&bttv_tvnorms[btv->tvnorm].cropcap,
1300 sizeof (tvnorm->cropcap))) {
1301 bttv_crop_reset(&btv->crop[0], norm);
1302 btv->crop[1] = btv->crop[0]; /* current = default */
1304 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1305 btv->crop_start = tvnorm->cropcap.bounds.top
1306 + tvnorm->cropcap.bounds.height;
1312 btwrite(tvnorm->adelay, BT848_ADELAY);
1313 btwrite(tvnorm->bdelay, BT848_BDELAY);
1314 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1316 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1317 btwrite(1, BT848_VBI_PACK_DEL);
1318 bt848A_set_timing(btv);
1320 switch (btv->c.type) {
1321 case BTTV_BOARD_VOODOOTV_FM:
1322 case BTTV_BOARD_VOODOOTV_200:
1323 bttv_tda9880_setnorm(btv,norm);
1326 id = tvnorm->v4l2_id;
1327 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &id);
1332 /* Call with btv->lock down. */
1334 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1336 unsigned long flags;
1340 spin_lock_irqsave(&btv->s_lock,flags);
1341 if (btv->curr.frame_irq) {
1342 /* active capture -> delayed input switch */
1343 btv->new_input = input;
1345 video_mux(btv,input);
1347 spin_unlock_irqrestore(&btv->s_lock,flags);
1349 video_mux(btv,input);
1351 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1352 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
1353 set_tvnorm(btv, norm);
1356 static void init_irqreg(struct bttv *btv)
1359 btwrite(0xfffffUL, BT848_INT_STAT);
1361 if (bttv_tvcards[btv->c.type].no_video) {
1363 btwrite(BT848_INT_I2CDONE,
1367 btwrite((btv->triton1) |
1368 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1370 (fdsr ? BT848_INT_FDSR : 0) |
1371 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1372 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1378 static void init_bt848(struct bttv *btv)
1382 if (bttv_tvcards[btv->c.type].no_video) {
1383 /* very basic init only */
1388 btwrite(0x00, BT848_CAP_CTL);
1389 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1390 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1392 /* set planar and packed mode trigger points and */
1393 /* set rising edge of inverted GPINTR pin as irq trigger */
1394 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1395 BT848_GPIO_DMA_CTL_PLTP1_16|
1396 BT848_GPIO_DMA_CTL_PLTP23_16|
1397 BT848_GPIO_DMA_CTL_GPINTC|
1398 BT848_GPIO_DMA_CTL_GPINTI,
1399 BT848_GPIO_DMA_CTL);
1401 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1402 btwrite(val, BT848_E_SCLOOP);
1403 btwrite(val, BT848_O_SCLOOP);
1405 btwrite(0x20, BT848_E_VSCALE_HI);
1406 btwrite(0x20, BT848_O_VSCALE_HI);
1407 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1410 btwrite(whitecrush_upper, BT848_WC_UP);
1411 btwrite(whitecrush_lower, BT848_WC_DOWN);
1413 if (btv->opt_lumafilter) {
1414 btwrite(0, BT848_E_CONTROL);
1415 btwrite(0, BT848_O_CONTROL);
1417 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1418 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1421 bt848_bright(btv, btv->bright);
1422 bt848_hue(btv, btv->hue);
1423 bt848_contrast(btv, btv->contrast);
1424 bt848_sat(btv, btv->saturation);
1430 static void bttv_reinit_bt848(struct bttv *btv)
1432 unsigned long flags;
1435 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1436 spin_lock_irqsave(&btv->s_lock,flags);
1438 bttv_set_dma(btv,0);
1439 spin_unlock_irqrestore(&btv->s_lock,flags);
1442 btv->pll.pll_current = -1;
1443 set_input(btv, btv->input, btv->tvnorm);
1446 static int bttv_g_ctrl(struct file *file, void *priv,
1447 struct v4l2_control *c)
1449 struct bttv_fh *fh = priv;
1450 struct bttv *btv = fh->btv;
1453 case V4L2_CID_BRIGHTNESS:
1454 c->value = btv->bright;
1457 c->value = btv->hue;
1459 case V4L2_CID_CONTRAST:
1460 c->value = btv->contrast;
1462 case V4L2_CID_SATURATION:
1463 c->value = btv->saturation;
1466 case V4L2_CID_AUDIO_MUTE:
1467 case V4L2_CID_AUDIO_VOLUME:
1468 case V4L2_CID_AUDIO_BALANCE:
1469 case V4L2_CID_AUDIO_BASS:
1470 case V4L2_CID_AUDIO_TREBLE:
1471 bttv_call_i2c_clients(btv, VIDIOC_G_CTRL, c);
1474 case V4L2_CID_PRIVATE_CHROMA_AGC:
1475 c->value = btv->opt_chroma_agc;
1477 case V4L2_CID_PRIVATE_COMBFILTER:
1478 c->value = btv->opt_combfilter;
1480 case V4L2_CID_PRIVATE_LUMAFILTER:
1481 c->value = btv->opt_lumafilter;
1483 case V4L2_CID_PRIVATE_AUTOMUTE:
1484 c->value = btv->opt_automute;
1486 case V4L2_CID_PRIVATE_AGC_CRUSH:
1487 c->value = btv->opt_adc_crush;
1489 case V4L2_CID_PRIVATE_VCR_HACK:
1490 c->value = btv->opt_vcr_hack;
1492 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1493 c->value = btv->opt_whitecrush_upper;
1495 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1496 c->value = btv->opt_whitecrush_lower;
1498 case V4L2_CID_PRIVATE_UV_RATIO:
1499 c->value = btv->opt_uv_ratio;
1501 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1502 c->value = btv->opt_full_luma_range;
1504 case V4L2_CID_PRIVATE_CORING:
1505 c->value = btv->opt_coring;
1513 static int bttv_s_ctrl(struct file *file, void *f,
1514 struct v4l2_control *c)
1518 struct bttv_fh *fh = f;
1519 struct bttv *btv = fh->btv;
1521 err = v4l2_prio_check(&btv->prio, &fh->prio);
1526 case V4L2_CID_BRIGHTNESS:
1527 bt848_bright(btv, c->value);
1530 bt848_hue(btv, c->value);
1532 case V4L2_CID_CONTRAST:
1533 bt848_contrast(btv, c->value);
1535 case V4L2_CID_SATURATION:
1536 bt848_sat(btv, c->value);
1538 case V4L2_CID_AUDIO_MUTE:
1539 audio_mute(btv, c->value);
1541 case V4L2_CID_AUDIO_VOLUME:
1542 if (btv->volume_gpio)
1543 btv->volume_gpio(btv, c->value);
1545 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, c);
1547 case V4L2_CID_AUDIO_BALANCE:
1548 case V4L2_CID_AUDIO_BASS:
1549 case V4L2_CID_AUDIO_TREBLE:
1550 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, c);
1553 case V4L2_CID_PRIVATE_CHROMA_AGC:
1554 btv->opt_chroma_agc = c->value;
1555 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1556 btwrite(val, BT848_E_SCLOOP);
1557 btwrite(val, BT848_O_SCLOOP);
1559 case V4L2_CID_PRIVATE_COMBFILTER:
1560 btv->opt_combfilter = c->value;
1562 case V4L2_CID_PRIVATE_LUMAFILTER:
1563 btv->opt_lumafilter = c->value;
1564 if (btv->opt_lumafilter) {
1565 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1566 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1568 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1569 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1572 case V4L2_CID_PRIVATE_AUTOMUTE:
1573 btv->opt_automute = c->value;
1575 case V4L2_CID_PRIVATE_AGC_CRUSH:
1576 btv->opt_adc_crush = c->value;
1577 btwrite(BT848_ADC_RESERVED |
1578 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1581 case V4L2_CID_PRIVATE_VCR_HACK:
1582 btv->opt_vcr_hack = c->value;
1584 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1585 btv->opt_whitecrush_upper = c->value;
1586 btwrite(c->value, BT848_WC_UP);
1588 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1589 btv->opt_whitecrush_lower = c->value;
1590 btwrite(c->value, BT848_WC_DOWN);
1592 case V4L2_CID_PRIVATE_UV_RATIO:
1593 btv->opt_uv_ratio = c->value;
1594 bt848_sat(btv, btv->saturation);
1596 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1597 btv->opt_full_luma_range = c->value;
1598 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1600 case V4L2_CID_PRIVATE_CORING:
1601 btv->opt_coring = c->value;
1602 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1610 /* ----------------------------------------------------------------------- */
1612 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1614 unsigned int outbits, data;
1615 outbits = btread(BT848_GPIO_OUT_EN);
1616 data = btread(BT848_GPIO_DATA);
1617 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1618 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1621 static void bttv_field_count(struct bttv *btv)
1629 /* start field counter */
1630 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1632 /* stop field counter */
1633 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1634 btv->field_count = 0;
1638 static const struct bttv_format*
1639 format_by_fourcc(int fourcc)
1643 for (i = 0; i < FORMATS; i++) {
1644 if (-1 == formats[i].fourcc)
1646 if (formats[i].fourcc == fourcc)
1652 /* ----------------------------------------------------------------------- */
1656 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1657 struct bttv_buffer *new)
1659 struct bttv_buffer *old;
1660 unsigned long flags;
1663 dprintk("switch_overlay: enter [new=%p]\n",new);
1665 new->vb.state = VIDEOBUF_DONE;
1666 spin_lock_irqsave(&btv->s_lock,flags);
1670 bttv_set_dma(btv, 0x03);
1671 spin_unlock_irqrestore(&btv->s_lock,flags);
1673 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1674 bttv_dma_free(&fh->cap,btv, old);
1678 free_btres(btv,fh,RESOURCE_OVERLAY);
1679 dprintk("switch_overlay: done\n");
1683 /* ----------------------------------------------------------------------- */
1684 /* video4linux (1) interface */
1686 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1687 struct bttv_buffer *buf,
1688 const struct bttv_format *fmt,
1689 unsigned int width, unsigned int height,
1690 enum v4l2_field field)
1692 struct bttv_fh *fh = q->priv_data;
1693 int redo_dma_risc = 0;
1698 /* check settings */
1701 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1703 height = RAW_LINES*2;
1704 if (width*height > buf->vb.bsize)
1706 buf->vb.size = buf->vb.bsize;
1708 /* Make sure tvnorm and vbi_end remain consistent
1709 until we're done. */
1710 mutex_lock(&btv->lock);
1714 /* In this mode capturing always starts at defrect.top
1715 (default VDELAY), ignoring cropping parameters. */
1716 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1717 mutex_unlock(&btv->lock);
1721 mutex_unlock(&btv->lock);
1723 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1725 mutex_lock(&btv->lock);
1728 c = btv->crop[!!fh->do_crop];
1730 mutex_unlock(&btv->lock);
1732 if (width < c.min_scaled_width ||
1733 width > c.max_scaled_width ||
1734 height < c.min_scaled_height)
1738 case V4L2_FIELD_TOP:
1739 case V4L2_FIELD_BOTTOM:
1740 case V4L2_FIELD_ALTERNATE:
1741 /* btv->crop counts frame lines. Max. scale
1742 factor is 16:1 for frames, 8:1 for fields. */
1743 if (height * 2 > c.max_scaled_height)
1748 if (height > c.max_scaled_height)
1753 buf->vb.size = (width * height * fmt->depth) >> 3;
1754 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1758 /* alloc + fill struct bttv_buffer (if changed) */
1759 if (buf->vb.width != width || buf->vb.height != height ||
1760 buf->vb.field != field ||
1761 buf->tvnorm != norm || buf->fmt != fmt ||
1762 buf->crop.top != c.rect.top ||
1763 buf->crop.left != c.rect.left ||
1764 buf->crop.width != c.rect.width ||
1765 buf->crop.height != c.rect.height) {
1766 buf->vb.width = width;
1767 buf->vb.height = height;
1768 buf->vb.field = field;
1775 /* alloc risc memory */
1776 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1778 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1783 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1786 buf->vb.state = VIDEOBUF_PREPARED;
1790 bttv_dma_free(q,btv,buf);
1795 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1797 struct bttv_fh *fh = q->priv_data;
1799 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1802 while (*size * *count > gbuffers * gbufsize)
1808 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1809 enum v4l2_field field)
1811 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1812 struct bttv_fh *fh = q->priv_data;
1814 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1815 fh->width, fh->height, field);
1819 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1821 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1822 struct bttv_fh *fh = q->priv_data;
1823 struct bttv *btv = fh->btv;
1825 buf->vb.state = VIDEOBUF_QUEUED;
1826 list_add_tail(&buf->vb.queue,&btv->capture);
1827 if (!btv->curr.frame_irq) {
1829 bttv_set_dma(btv, 0x03);
1833 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1835 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1836 struct bttv_fh *fh = q->priv_data;
1838 bttv_dma_free(q,fh->btv,buf);
1841 static struct videobuf_queue_ops bttv_video_qops = {
1842 .buf_setup = buffer_setup,
1843 .buf_prepare = buffer_prepare,
1844 .buf_queue = buffer_queue,
1845 .buf_release = buffer_release,
1848 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
1850 struct bttv_fh *fh = priv;
1851 struct bttv *btv = fh->btv;
1855 err = v4l2_prio_check(&btv->prio, &fh->prio);
1859 for (i = 0; i < BTTV_TVNORMS; i++)
1860 if (*id & bttv_tvnorms[i].v4l2_id)
1862 if (i == BTTV_TVNORMS)
1865 mutex_lock(&btv->lock);
1867 mutex_unlock(&btv->lock);
1872 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1874 struct bttv_fh *fh = f;
1875 struct bttv *btv = fh->btv;
1877 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1878 *id = V4L2_STD_625_50;
1880 *id = V4L2_STD_525_60;
1884 static int bttv_enum_input(struct file *file, void *priv,
1885 struct v4l2_input *i)
1887 struct bttv_fh *fh = priv;
1888 struct bttv *btv = fh->btv;
1893 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1896 memset(i, 0, sizeof(*i));
1899 i->type = V4L2_INPUT_TYPE_CAMERA;
1902 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1903 sprintf(i->name, "Television");
1904 i->type = V4L2_INPUT_TYPE_TUNER;
1906 } else if (i->index == btv->svhs) {
1907 sprintf(i->name, "S-Video");
1909 sprintf(i->name, "Composite%d", i->index);
1912 if (i->index == btv->input) {
1913 __u32 dstatus = btread(BT848_DSTATUS);
1914 if (0 == (dstatus & BT848_DSTATUS_PRES))
1915 i->status |= V4L2_IN_ST_NO_SIGNAL;
1916 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1917 i->status |= V4L2_IN_ST_NO_H_LOCK;
1920 for (n = 0; n < BTTV_TVNORMS; n++)
1921 i->std |= bttv_tvnorms[n].v4l2_id;
1926 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1928 struct bttv_fh *fh = priv;
1929 struct bttv *btv = fh->btv;
1935 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1937 struct bttv_fh *fh = priv;
1938 struct bttv *btv = fh->btv;
1942 err = v4l2_prio_check(&btv->prio, &fh->prio);
1946 if (i > bttv_tvcards[btv->c.type].video_inputs)
1949 mutex_lock(&btv->lock);
1950 set_input(btv, i, btv->tvnorm);
1951 mutex_unlock(&btv->lock);
1955 static int bttv_s_tuner(struct file *file, void *priv,
1956 struct v4l2_tuner *t)
1958 struct bttv_fh *fh = priv;
1959 struct bttv *btv = fh->btv;
1962 err = v4l2_prio_check(&btv->prio, &fh->prio);
1966 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1972 mutex_lock(&btv->lock);
1973 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
1975 if (btv->audio_mode_gpio)
1976 btv->audio_mode_gpio(btv, t, 1);
1978 mutex_unlock(&btv->lock);
1983 static int bttv_g_frequency(struct file *file, void *priv,
1984 struct v4l2_frequency *f)
1986 struct bttv_fh *fh = priv;
1987 struct bttv *btv = fh->btv;
1990 err = v4l2_prio_check(&btv->prio, &fh->prio);
1994 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1995 f->frequency = btv->freq;
2000 static int bttv_s_frequency(struct file *file, void *priv,
2001 struct v4l2_frequency *f)
2003 struct bttv_fh *fh = priv;
2004 struct bttv *btv = fh->btv;
2007 err = v4l2_prio_check(&btv->prio, &fh->prio);
2011 if (unlikely(f->tuner != 0))
2013 if (unlikely(f->type != (btv->radio_user
2014 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV)))
2016 mutex_lock(&btv->lock);
2017 btv->freq = f->frequency;
2018 bttv_call_i2c_clients(btv, VIDIOC_S_FREQUENCY, f);
2019 if (btv->has_matchbox && btv->radio_user)
2020 tea5757_set_freq(btv, btv->freq);
2021 mutex_unlock(&btv->lock);
2025 static int bttv_log_status(struct file *file, void *f)
2027 struct bttv_fh *fh = f;
2028 struct bttv *btv = fh->btv;
2030 printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
2031 btv->c.nr, btv->c.nr);
2032 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
2033 printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
2034 btv->c.nr, btv->c.nr);
2038 #ifdef CONFIG_VIDEO_ADV_DEBUG
2039 static int bttv_g_register(struct file *file, void *f,
2040 struct v4l2_register *reg)
2042 struct bttv_fh *fh = f;
2043 struct bttv *btv = fh->btv;
2045 if (!capable(CAP_SYS_ADMIN))
2048 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2051 /* bt848 has a 12-bit register space */
2053 reg->val = btread(reg->reg);
2058 static int bttv_s_register(struct file *file, void *f,
2059 struct v4l2_register *reg)
2061 struct bttv_fh *fh = f;
2062 struct bttv *btv = fh->btv;
2064 if (!capable(CAP_SYS_ADMIN))
2067 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2070 /* bt848 has a 12-bit register space */
2072 btwrite(reg->val, reg->reg);
2078 /* Given cropping boundaries b and the scaled width and height of a
2079 single field or frame, which must not exceed hardware limits, this
2080 function adjusts the cropping parameters c. */
2082 bttv_crop_adjust (struct bttv_crop * c,
2083 const struct v4l2_rect * b,
2086 enum v4l2_field field)
2088 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2092 if (width < c->min_scaled_width) {
2093 /* Max. hor. scale factor 16:1. */
2094 c->rect.width = width * 16;
2095 } else if (width > c->max_scaled_width) {
2096 /* Min. hor. scale factor 1:1. */
2097 c->rect.width = width;
2099 max_left = b->left + b->width - width;
2100 max_left = min(max_left, (__s32) MAX_HDELAY);
2101 if (c->rect.left > max_left)
2102 c->rect.left = max_left;
2105 if (height < c->min_scaled_height) {
2106 /* Max. vert. scale factor 16:1, single fields 8:1. */
2107 c->rect.height = height * 16;
2108 } else if (frame_height > c->max_scaled_height) {
2109 /* Min. vert. scale factor 1:1.
2110 Top and height count field lines times two. */
2111 c->rect.height = (frame_height + 1) & ~1;
2113 max_top = b->top + b->height - c->rect.height;
2114 if (c->rect.top > max_top)
2115 c->rect.top = max_top;
2118 bttv_crop_calc_limits(c);
2121 /* Returns an error if scaling to a frame or single field with the given
2122 width and height is not possible with the current cropping parameters
2123 and width aligned according to width_mask. If adjust_size is TRUE the
2124 function may adjust the width and/or height instead, rounding width
2125 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2126 also adjust the current cropping parameters to get closer to the
2127 desired image size. */
2129 limit_scaled_size (struct bttv_fh * fh,
2132 enum v4l2_field field,
2133 unsigned int width_mask,
2134 unsigned int width_bias,
2138 struct bttv *btv = fh->btv;
2139 const struct v4l2_rect *b;
2140 struct bttv_crop *c;
2147 BUG_ON((int) width_mask >= 0 ||
2148 width_bias >= (unsigned int) -width_mask);
2150 /* Make sure tvnorm, vbi_end and the current cropping parameters
2151 remain consistent until we're done. */
2152 mutex_lock(&btv->lock);
2154 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2156 /* Do crop - use current, don't - use default parameters. */
2157 c = &btv->crop[!!fh->do_crop];
2162 && !locked_btres(btv, VIDEO_RESOURCES)) {
2166 /* We cannot scale up. When the scaled image is larger
2167 than crop.rect we adjust the crop.rect as required
2168 by the V4L2 spec, hence cropcap.bounds are our limit. */
2169 max_width = min(b->width, (__s32) MAX_HACTIVE);
2170 max_height = b->height;
2172 /* We cannot capture the same line as video and VBI data.
2173 Note btv->vbi_end is really a minimum, see
2174 bttv_vbi_try_fmt(). */
2175 if (btv->vbi_end > b->top) {
2176 max_height -= btv->vbi_end - b->top;
2178 if (min_height > max_height)
2183 if (btv->vbi_end > c->rect.top)
2186 min_width = c->min_scaled_width;
2187 min_height = c->min_scaled_height;
2188 max_width = c->max_scaled_width;
2189 max_height = c->max_scaled_height;
2194 min_width = (min_width - width_mask - 1) & width_mask;
2195 max_width = max_width & width_mask;
2197 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2198 min_height = min_height;
2199 /* Min. scale factor is 1:1. */
2200 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2203 *width = clamp(*width, min_width, max_width);
2204 *height = clamp(*height, min_height, max_height);
2206 /* Round after clamping to avoid overflow. */
2207 *width = (*width + width_bias) & width_mask;
2210 bttv_crop_adjust(c, b, *width, *height, field);
2212 if (btv->vbi_end > c->rect.top) {
2213 /* Move the crop window out of the way. */
2214 c->rect.top = btv->vbi_end;
2219 if (*width < min_width ||
2220 *height < min_height ||
2221 *width > max_width ||
2222 *height > max_height ||
2223 0 != (*width & ~width_mask))
2227 rc = 0; /* success */
2230 mutex_unlock(&btv->lock);
2235 /* Returns an error if the given overlay window dimensions are not
2236 possible with the current cropping parameters. If adjust_size is
2237 TRUE the function may adjust the window width and/or height
2238 instead, however it always rounds the horizontal position and
2239 width as btcx_align() does. If adjust_crop is TRUE the function
2240 may also adjust the current cropping parameters to get closer
2241 to the desired window size. */
2243 verify_window (struct bttv_fh * fh,
2244 struct v4l2_window * win,
2248 enum v4l2_field field;
2249 unsigned int width_mask;
2252 if (win->w.width < 48 || win->w.height < 32)
2254 if (win->clipcount > 2048)
2259 if (V4L2_FIELD_ANY == field) {
2262 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2263 field = (win->w.height > height2)
2264 ? V4L2_FIELD_INTERLACED
2268 case V4L2_FIELD_TOP:
2269 case V4L2_FIELD_BOTTOM:
2270 case V4L2_FIELD_INTERLACED:
2276 /* 4-byte alignment. */
2277 if (NULL == fh->ovfmt)
2280 switch (fh->ovfmt->depth) {
2294 win->w.width -= win->w.left & ~width_mask;
2295 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2297 rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
2299 /* width_bias: round down */ 0,
2300 adjust_size, adjust_crop);
2308 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2309 struct v4l2_window *win, int fixup)
2311 struct v4l2_clip *clips = NULL;
2312 int n,size,retval = 0;
2314 if (NULL == fh->ovfmt)
2316 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2318 retval = verify_window(fh, win,
2319 /* adjust_size */ fixup,
2320 /* adjust_crop */ fixup);
2324 /* copy clips -- luckily v4l1 + v4l2 are binary
2325 compatible here ...*/
2327 size = sizeof(*clips)*(n+4);
2328 clips = kmalloc(size,GFP_KERNEL);
2332 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2337 /* clip against screen */
2338 if (NULL != btv->fbuf.base)
2339 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2341 btcx_sort_clips(clips,n);
2343 /* 4-byte alignments */
2344 switch (fh->ovfmt->depth) {
2347 btcx_align(&win->w, clips, n, 3);
2350 btcx_align(&win->w, clips, n, 1);
2353 /* no alignment fixups needed */
2359 mutex_lock(&fh->cap.vb_lock);
2360 kfree(fh->ov.clips);
2361 fh->ov.clips = clips;
2365 fh->ov.field = win->field;
2366 fh->ov.setup_ok = 1;
2367 btv->init.ov.w.width = win->w.width;
2368 btv->init.ov.w.height = win->w.height;
2369 btv->init.ov.field = win->field;
2371 /* update overlay if needed */
2373 if (check_btres(fh, RESOURCE_OVERLAY)) {
2374 struct bttv_buffer *new;
2376 new = videobuf_sg_alloc(sizeof(*new));
2377 new->crop = btv->crop[!!fh->do_crop].rect;
2378 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2379 retval = bttv_switch_overlay(btv,fh,new);
2381 mutex_unlock(&fh->cap.vb_lock);
2385 /* ----------------------------------------------------------------------- */
2387 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2389 struct videobuf_queue* q = NULL;
2392 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2395 case V4L2_BUF_TYPE_VBI_CAPTURE:
2404 static int bttv_resource(struct bttv_fh *fh)
2409 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2410 res = RESOURCE_VIDEO_STREAM;
2412 case V4L2_BUF_TYPE_VBI_CAPTURE:
2421 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2423 struct videobuf_queue *q = bttv_queue(fh);
2424 int res = bttv_resource(fh);
2426 if (check_btres(fh,res))
2428 if (videobuf_queue_is_busy(q))
2435 pix_format_set_size (struct v4l2_pix_format * f,
2436 const struct bttv_format * fmt,
2438 unsigned int height)
2443 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2444 f->bytesperline = width; /* Y plane */
2445 f->sizeimage = (width * height * fmt->depth) >> 3;
2447 f->bytesperline = (width * fmt->depth) >> 3;
2448 f->sizeimage = height * f->bytesperline;
2452 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2453 struct v4l2_format *f)
2455 struct bttv_fh *fh = priv;
2457 pix_format_set_size(&f->fmt.pix, fh->fmt,
2458 fh->width, fh->height);
2459 f->fmt.pix.field = fh->cap.field;
2460 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2465 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2466 struct v4l2_format *f)
2468 struct bttv_fh *fh = priv;
2470 f->fmt.win.w = fh->ov.w;
2471 f->fmt.win.field = fh->ov.field;
2476 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2477 struct v4l2_format *f)
2479 const struct bttv_format *fmt;
2480 struct bttv_fh *fh = priv;
2481 struct bttv *btv = fh->btv;
2482 enum v4l2_field field;
2483 __s32 width, height;
2486 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2490 field = f->fmt.pix.field;
2492 if (V4L2_FIELD_ANY == field) {
2495 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2496 field = (f->fmt.pix.height > height2)
2497 ? V4L2_FIELD_INTERLACED
2498 : V4L2_FIELD_BOTTOM;
2501 if (V4L2_FIELD_SEQ_BT == field)
2502 field = V4L2_FIELD_SEQ_TB;
2505 case V4L2_FIELD_TOP:
2506 case V4L2_FIELD_BOTTOM:
2507 case V4L2_FIELD_ALTERNATE:
2508 case V4L2_FIELD_INTERLACED:
2510 case V4L2_FIELD_SEQ_TB:
2511 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2518 width = f->fmt.pix.width;
2519 height = f->fmt.pix.height;
2521 rc = limit_scaled_size(fh, &width, &height, field,
2522 /* width_mask: 4 pixels */ ~3,
2523 /* width_bias: nearest */ 2,
2524 /* adjust_size */ 1,
2525 /* adjust_crop */ 0);
2529 /* update data for the application */
2530 f->fmt.pix.field = field;
2531 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2536 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2537 struct v4l2_format *f)
2539 struct bttv_fh *fh = priv;
2541 return verify_window(fh, &f->fmt.win,
2542 /* adjust_size */ 1,
2543 /* adjust_crop */ 0);
2546 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2547 struct v4l2_format *f)
2550 const struct bttv_format *fmt;
2551 struct bttv_fh *fh = priv;
2552 struct bttv *btv = fh->btv;
2553 __s32 width, height;
2554 enum v4l2_field field;
2556 retval = bttv_switch_type(fh, f->type);
2560 retval = bttv_try_fmt_vid_cap(file, priv, f);
2564 width = f->fmt.pix.width;
2565 height = f->fmt.pix.height;
2566 field = f->fmt.pix.field;
2568 retval = limit_scaled_size(fh, &width, &height, f->fmt.pix.field,
2569 /* width_mask: 4 pixels */ ~3,
2570 /* width_bias: nearest */ 2,
2571 /* adjust_size */ 1,
2572 /* adjust_crop */ 1);
2576 f->fmt.pix.field = field;
2578 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2580 /* update our state informations */
2581 mutex_lock(&fh->cap.vb_lock);
2583 fh->cap.field = f->fmt.pix.field;
2584 fh->cap.last = V4L2_FIELD_NONE;
2585 fh->width = f->fmt.pix.width;
2586 fh->height = f->fmt.pix.height;
2587 btv->init.fmt = fmt;
2588 btv->init.width = f->fmt.pix.width;
2589 btv->init.height = f->fmt.pix.height;
2590 mutex_unlock(&fh->cap.vb_lock);
2595 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2596 struct v4l2_format *f)
2598 struct bttv_fh *fh = priv;
2599 struct bttv *btv = fh->btv;
2601 if (no_overlay > 0) {
2602 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2606 return setup_window(fh, btv, &f->fmt.win, 1);
2609 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2610 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
2614 struct bttv_fh *fh = priv;
2616 mutex_lock(&fh->cap.vb_lock);
2617 retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
2620 mutex_unlock(&fh->cap.vb_lock);
2625 memset(mbuf, 0, sizeof(*mbuf));
2626 mbuf->frames = gbuffers;
2627 mbuf->size = gbuffers * gbufsize;
2629 for (i = 0; i < gbuffers; i++)
2630 mbuf->offsets[i] = i * gbufsize;
2632 mutex_unlock(&fh->cap.vb_lock);
2637 static int bttv_querycap(struct file *file, void *priv,
2638 struct v4l2_capability *cap)
2640 struct bttv_fh *fh = priv;
2641 struct bttv *btv = fh->btv;
2646 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2647 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2648 snprintf(cap->bus_info, sizeof(cap->bus_info),
2649 "PCI:%s", pci_name(btv->c.pci));
2650 cap->version = BTTV_VERSION_CODE;
2652 V4L2_CAP_VIDEO_CAPTURE |
2653 V4L2_CAP_VBI_CAPTURE |
2654 V4L2_CAP_READWRITE |
2656 if (no_overlay <= 0)
2657 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2659 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2660 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2661 cap->capabilities |= V4L2_CAP_TUNER;
2665 static int bttv_enum_fmt_vbi_cap(struct file *file, void *priv,
2666 struct v4l2_fmtdesc *f)
2671 f->pixelformat = V4L2_PIX_FMT_GREY;
2672 strcpy(f->description, "vbi data");
2677 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2681 for (i = 0; i < FORMATS; i++) {
2682 if (formats[i].fourcc != -1)
2684 if ((unsigned int)index == f->index)
2690 f->pixelformat = formats[i].fourcc;
2691 strlcpy(f->description, formats[i].name, sizeof(f->description));
2696 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2697 struct v4l2_fmtdesc *f)
2699 int rc = bttv_enum_fmt_cap_ovr(f);
2707 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
2708 struct v4l2_fmtdesc *f)
2712 if (no_overlay > 0) {
2713 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2717 rc = bttv_enum_fmt_cap_ovr(f);
2722 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
2728 static int bttv_g_fbuf(struct file *file, void *f,
2729 struct v4l2_framebuffer *fb)
2731 struct bttv_fh *fh = f;
2732 struct bttv *btv = fh->btv;
2735 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2737 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2741 static int bttv_overlay(struct file *file, void *f, unsigned int on)
2743 struct bttv_fh *fh = f;
2744 struct bttv *btv = fh->btv;
2745 struct bttv_buffer *new;
2750 if (NULL == btv->fbuf.base)
2752 if (!fh->ov.setup_ok) {
2753 dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
2758 if (!check_alloc_btres(btv, fh, RESOURCE_OVERLAY))
2761 mutex_lock(&fh->cap.vb_lock);
2763 fh->ov.tvnorm = btv->tvnorm;
2764 new = videobuf_sg_alloc(sizeof(*new));
2765 new->crop = btv->crop[!!fh->do_crop].rect;
2766 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2772 retval = bttv_switch_overlay(btv, fh, new);
2773 mutex_unlock(&fh->cap.vb_lock);
2777 static int bttv_s_fbuf(struct file *file, void *f,
2778 struct v4l2_framebuffer *fb)
2780 struct bttv_fh *fh = f;
2781 struct bttv *btv = fh->btv;
2782 const struct bttv_format *fmt;
2785 if (!capable(CAP_SYS_ADMIN) &&
2786 !capable(CAP_SYS_RAWIO))
2790 fmt = format_by_fourcc(fb->fmt.pixelformat);
2793 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2797 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2798 __s32 width = fb->fmt.width;
2799 __s32 height = fb->fmt.height;
2801 retval = limit_scaled_size(fh, &width, &height,
2802 V4L2_FIELD_INTERLACED,
2803 /* width_mask */ ~3,
2805 /* adjust_size */ 0,
2806 /* adjust_crop */ 0);
2812 mutex_lock(&fh->cap.vb_lock);
2813 btv->fbuf.base = fb->base;
2814 btv->fbuf.fmt.width = fb->fmt.width;
2815 btv->fbuf.fmt.height = fb->fmt.height;
2816 if (0 != fb->fmt.bytesperline)
2817 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2819 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2823 btv->init.ovfmt = fmt;
2824 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2827 fh->ov.w.width = fb->fmt.width;
2828 fh->ov.w.height = fb->fmt.height;
2829 btv->init.ov.w.width = fb->fmt.width;
2830 btv->init.ov.w.height = fb->fmt.height;
2831 kfree(fh->ov.clips);
2832 fh->ov.clips = NULL;
2835 if (check_btres(fh, RESOURCE_OVERLAY)) {
2836 struct bttv_buffer *new;
2838 new = videobuf_sg_alloc(sizeof(*new));
2839 new->crop = btv->crop[!!fh->do_crop].rect;
2840 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2841 retval = bttv_switch_overlay(btv, fh, new);
2844 mutex_unlock(&fh->cap.vb_lock);
2848 static int bttv_reqbufs(struct file *file, void *priv,
2849 struct v4l2_requestbuffers *p)
2851 struct bttv_fh *fh = priv;
2852 return videobuf_reqbufs(bttv_queue(fh), p);
2855 static int bttv_querybuf(struct file *file, void *priv,
2856 struct v4l2_buffer *b)
2858 struct bttv_fh *fh = priv;
2859 return videobuf_querybuf(bttv_queue(fh), b);
2862 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2864 struct bttv_fh *fh = priv;
2865 struct bttv *btv = fh->btv;
2866 int res = bttv_resource(fh);
2868 if (!check_alloc_btres(btv, fh, res))
2871 return videobuf_qbuf(bttv_queue(fh), b);
2874 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2876 struct bttv_fh *fh = priv;
2877 return videobuf_dqbuf(bttv_queue(fh), b,
2878 file->f_flags & O_NONBLOCK);
2881 static int bttv_streamon(struct file *file, void *priv,
2882 enum v4l2_buf_type type)
2884 struct bttv_fh *fh = priv;
2885 struct bttv *btv = fh->btv;
2886 int res = bttv_resource(fh);
2888 if (!check_alloc_btres(btv, fh, res))
2890 return videobuf_streamon(bttv_queue(fh));
2894 static int bttv_streamoff(struct file *file, void *priv,
2895 enum v4l2_buf_type type)
2897 struct bttv_fh *fh = priv;
2898 struct bttv *btv = fh->btv;
2900 int res = bttv_resource(fh);
2903 retval = videobuf_streamoff(bttv_queue(fh));
2906 free_btres(btv, fh, res);
2910 static int bttv_queryctrl(struct file *file, void *priv,
2911 struct v4l2_queryctrl *c)
2913 struct bttv_fh *fh = priv;
2914 struct bttv *btv = fh->btv;
2915 const struct v4l2_queryctrl *ctrl;
2917 if ((c->id < V4L2_CID_BASE ||
2918 c->id >= V4L2_CID_LASTP1) &&
2919 (c->id < V4L2_CID_PRIVATE_BASE ||
2920 c->id >= V4L2_CID_PRIVATE_LASTP1))
2923 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2926 ctrl = ctrl_by_id(c->id);
2928 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2934 static int bttv_g_parm(struct file *file, void *f,
2935 struct v4l2_streamparm *parm)
2937 struct bttv_fh *fh = f;
2938 struct bttv *btv = fh->btv;
2939 struct v4l2_standard s;
2941 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2943 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2944 bttv_tvnorms[btv->tvnorm].name);
2945 parm->parm.capture.timeperframe = s.frameperiod;
2949 static int bttv_g_tuner(struct file *file, void *priv,
2950 struct v4l2_tuner *t)
2952 struct bttv_fh *fh = priv;
2953 struct bttv *btv = fh->btv;
2955 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2960 mutex_lock(&btv->lock);
2961 memset(t, 0, sizeof(*t));
2962 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2963 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
2964 strcpy(t->name, "Television");
2965 t->capability = V4L2_TUNER_CAP_NORM;
2966 t->type = V4L2_TUNER_ANALOG_TV;
2967 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2970 if (btv->audio_mode_gpio)
2971 btv->audio_mode_gpio(btv, t, 0);
2973 mutex_unlock(&btv->lock);
2977 static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2979 struct bttv_fh *fh = f;
2980 struct bttv *btv = fh->btv;
2982 *p = v4l2_prio_max(&btv->prio);
2987 static int bttv_s_priority(struct file *file, void *f,
2988 enum v4l2_priority prio)
2990 struct bttv_fh *fh = f;
2991 struct bttv *btv = fh->btv;
2993 return v4l2_prio_change(&btv->prio, &fh->prio, prio);
2996 static int bttv_cropcap(struct file *file, void *priv,
2997 struct v4l2_cropcap *cap)
2999 struct bttv_fh *fh = priv;
3000 struct bttv *btv = fh->btv;
3002 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3003 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3006 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3011 static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
3013 struct bttv_fh *fh = f;
3014 struct bttv *btv = fh->btv;
3016 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3017 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3020 /* No fh->do_crop = 1; because btv->crop[1] may be
3021 inconsistent with fh->width or fh->height and apps
3022 do not expect a change here. */
3024 crop->c = btv->crop[!!fh->do_crop].rect;
3029 static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
3031 struct bttv_fh *fh = f;
3032 struct bttv *btv = fh->btv;
3033 const struct v4l2_rect *b;
3041 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3042 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3045 retval = v4l2_prio_check(&btv->prio, &fh->prio);
3049 /* Make sure tvnorm, vbi_end and the current cropping
3050 parameters remain consistent until we're done. Note
3051 read() may change vbi_end in check_alloc_btres(). */
3052 mutex_lock(&btv->lock);
3056 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3057 mutex_unlock(&btv->lock);
3061 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3064 b_right = b_left + b->width;
3065 b_bottom = b->top + b->height;
3067 b_top = max(b->top, btv->vbi_end);
3068 if (b_top + 32 >= b_bottom) {
3069 mutex_unlock(&btv->lock);
3073 /* Min. scaled size 48 x 32. */
3074 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3075 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3077 c.rect.width = clamp(crop->c.width,
3078 48, b_right - c.rect.left);
3080 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3081 /* Top and height must be a multiple of two. */
3082 c.rect.top = (c.rect.top + 1) & ~1;
3084 c.rect.height = clamp(crop->c.height,
3085 32, b_bottom - c.rect.top);
3086 c.rect.height = (c.rect.height + 1) & ~1;
3088 bttv_crop_calc_limits(&c);
3092 mutex_unlock(&btv->lock);
3096 mutex_lock(&fh->cap.vb_lock);
3098 if (fh->width < c.min_scaled_width) {
3099 fh->width = c.min_scaled_width;
3100 btv->init.width = c.min_scaled_width;
3101 } else if (fh->width > c.max_scaled_width) {
3102 fh->width = c.max_scaled_width;
3103 btv->init.width = c.max_scaled_width;
3106 if (fh->height < c.min_scaled_height) {
3107 fh->height = c.min_scaled_height;
3108 btv->init.height = c.min_scaled_height;
3109 } else if (fh->height > c.max_scaled_height) {
3110 fh->height = c.max_scaled_height;
3111 btv->init.height = c.max_scaled_height;
3114 mutex_unlock(&fh->cap.vb_lock);
3119 static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
3121 if (unlikely(a->index))
3124 strcpy(a->name, "audio");
3128 static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
3130 if (unlikely(a->index))
3136 static ssize_t bttv_read(struct file *file, char __user *data,
3137 size_t count, loff_t *ppos)
3139 struct bttv_fh *fh = file->private_data;
3142 if (fh->btv->errors)
3143 bttv_reinit_bt848(fh->btv);
3144 dprintk("bttv%d: read count=%d type=%s\n",
3145 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3148 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3149 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3150 /* VIDEO_READ in use by another fh,
3151 or VIDEO_STREAM by any fh. */
3154 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3155 file->f_flags & O_NONBLOCK);
3156 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
3158 case V4L2_BUF_TYPE_VBI_CAPTURE:
3159 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3161 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3162 file->f_flags & O_NONBLOCK);
3170 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3172 struct bttv_fh *fh = file->private_data;
3173 struct bttv_buffer *buf;
3174 enum v4l2_field field;
3176 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3177 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3179 return videobuf_poll_stream(file, &fh->vbi, wait);
3182 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3183 /* streaming capture */
3184 if (list_empty(&fh->cap.stream))
3186 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3188 /* read() capture */
3189 mutex_lock(&fh->cap.vb_lock);
3190 if (NULL == fh->cap.read_buf) {
3191 /* need to capture a new frame */
3192 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3194 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
3195 if (NULL == fh->cap.read_buf)
3197 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3198 field = videobuf_next_field(&fh->cap);
3199 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3200 kfree (fh->cap.read_buf);
3201 fh->cap.read_buf = NULL;
3204 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3205 fh->cap.read_off = 0;
3207 mutex_unlock(&fh->cap.vb_lock);
3208 buf = (struct bttv_buffer*)fh->cap.read_buf;
3211 poll_wait(file, &buf->vb.done, wait);
3212 if (buf->vb.state == VIDEOBUF_DONE ||
3213 buf->vb.state == VIDEOBUF_ERROR)
3214 return POLLIN|POLLRDNORM;
3217 mutex_unlock(&fh->cap.vb_lock);
3221 static int bttv_open(struct inode *inode, struct file *file)
3223 int minor = iminor(inode);
3224 struct bttv *btv = NULL;
3226 enum v4l2_buf_type type = 0;
3229 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3231 for (i = 0; i < bttv_num; i++) {
3232 if (bttvs[i].video_dev &&
3233 bttvs[i].video_dev->minor == minor) {
3235 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3238 if (bttvs[i].vbi_dev &&
3239 bttvs[i].vbi_dev->minor == minor) {
3241 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3248 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3249 btv->c.nr,v4l2_type_names[type]);
3251 /* allocate per filehandle data */
3252 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3255 file->private_data = fh;
3258 fh->ov.setup_ok = 0;
3259 v4l2_prio_open(&btv->prio,&fh->prio);
3261 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3262 &btv->c.pci->dev, &btv->s_lock,
3263 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3264 V4L2_FIELD_INTERLACED,
3265 sizeof(struct bttv_buffer),
3267 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3268 &btv->c.pci->dev, &btv->s_lock,
3269 V4L2_BUF_TYPE_VBI_CAPTURE,
3271 sizeof(struct bttv_buffer),
3273 set_tvnorm(btv,btv->tvnorm);
3277 /* The V4L2 spec requires one global set of cropping parameters
3278 which only change on request. These are stored in btv->crop[1].
3279 However for compatibility with V4L apps and cropping unaware
3280 V4L2 apps we now reset the cropping parameters as seen through
3281 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3282 will use btv->crop[0], the default cropping parameters for the
3283 current video standard, and VIDIOC_S_FMT will not implicitely
3284 change the cropping parameters until VIDIOC_S_CROP has been
3286 fh->do_crop = !reset_crop; /* module parameter */
3288 /* Likewise there should be one global set of VBI capture
3289 parameters, but for compatibility with V4L apps and earlier
3290 driver versions each fh has its own parameters. */
3291 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3293 bttv_field_count(btv);
3297 static int bttv_release(struct inode *inode, struct file *file)
3299 struct bttv_fh *fh = file->private_data;
3300 struct bttv *btv = fh->btv;
3302 /* turn off overlay */
3303 if (check_btres(fh, RESOURCE_OVERLAY))
3304 bttv_switch_overlay(btv,fh,NULL);
3306 /* stop video capture */
3307 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3308 videobuf_streamoff(&fh->cap);
3309 free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
3311 if (fh->cap.read_buf) {
3312 buffer_release(&fh->cap,fh->cap.read_buf);
3313 kfree(fh->cap.read_buf);
3315 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3316 free_btres(btv, fh, RESOURCE_VIDEO_READ);
3319 /* stop vbi capture */
3320 if (check_btres(fh, RESOURCE_VBI)) {
3321 videobuf_stop(&fh->vbi);
3322 free_btres(btv,fh,RESOURCE_VBI);
3326 videobuf_mmap_free(&fh->cap);
3327 videobuf_mmap_free(&fh->vbi);
3328 v4l2_prio_close(&btv->prio,&fh->prio);
3329 file->private_data = NULL;
3333 bttv_field_count(btv);
3338 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3340 struct bttv_fh *fh = file->private_data;
3342 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3343 fh->btv->c.nr, v4l2_type_names[fh->type],
3344 vma->vm_start, vma->vm_end - vma->vm_start);
3345 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3348 static const struct file_operations bttv_fops =
3350 .owner = THIS_MODULE,
3352 .release = bttv_release,
3353 .ioctl = video_ioctl2,
3354 .compat_ioctl = v4l_compat_ioctl32,
3355 .llseek = no_llseek,
3361 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3362 .vidioc_querycap = bttv_querycap,
3363 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3364 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3365 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3366 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3367 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3368 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3369 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3370 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
3371 .vidioc_enum_fmt_vbi_cap = bttv_enum_fmt_vbi_cap,
3372 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3373 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3374 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
3375 .vidioc_g_audio = bttv_g_audio,
3376 .vidioc_s_audio = bttv_s_audio,
3377 .vidioc_cropcap = bttv_cropcap,
3378 .vidioc_reqbufs = bttv_reqbufs,
3379 .vidioc_querybuf = bttv_querybuf,
3380 .vidioc_qbuf = bttv_qbuf,
3381 .vidioc_dqbuf = bttv_dqbuf,
3382 .vidioc_s_std = bttv_s_std,
3383 .vidioc_enum_input = bttv_enum_input,
3384 .vidioc_g_input = bttv_g_input,
3385 .vidioc_s_input = bttv_s_input,
3386 .vidioc_queryctrl = bttv_queryctrl,
3387 .vidioc_g_ctrl = bttv_g_ctrl,
3388 .vidioc_s_ctrl = bttv_s_ctrl,
3389 .vidioc_streamon = bttv_streamon,
3390 .vidioc_streamoff = bttv_streamoff,
3391 .vidioc_g_tuner = bttv_g_tuner,
3392 .vidioc_s_tuner = bttv_s_tuner,
3393 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3394 .vidiocgmbuf = vidiocgmbuf,
3396 .vidioc_g_crop = bttv_g_crop,
3397 .vidioc_s_crop = bttv_s_crop,
3398 .vidioc_g_fbuf = bttv_g_fbuf,
3399 .vidioc_s_fbuf = bttv_s_fbuf,
3400 .vidioc_overlay = bttv_overlay,
3401 .vidioc_g_priority = bttv_g_priority,
3402 .vidioc_s_priority = bttv_s_priority,
3403 .vidioc_g_parm = bttv_g_parm,
3404 .vidioc_g_frequency = bttv_g_frequency,
3405 .vidioc_s_frequency = bttv_s_frequency,
3406 .vidioc_log_status = bttv_log_status,
3407 .vidioc_querystd = bttv_querystd,
3408 #ifdef CONFIG_VIDEO_ADV_DEBUG
3409 .vidioc_g_register = bttv_g_register,
3410 .vidioc_s_register = bttv_s_register,
3414 static struct video_device bttv_video_template = {
3417 .ioctl_ops = &bttv_ioctl_ops,
3418 .tvnorms = BTTV_NORMS,
3419 .current_norm = V4L2_STD_PAL,
3422 /* ----------------------------------------------------------------------- */
3423 /* radio interface */
3425 static int radio_open(struct inode *inode, struct file *file)
3427 int minor = iminor(inode);
3428 struct bttv *btv = NULL;
3432 dprintk("bttv: open minor=%d\n",minor);
3434 for (i = 0; i < bttv_num; i++) {
3435 if (bttvs[i].radio_dev->minor == minor) {
3443 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3445 /* allocate per filehandle data */
3446 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3449 file->private_data = fh;
3451 v4l2_prio_open(&btv->prio, &fh->prio);
3453 mutex_lock(&btv->lock);
3457 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3458 audio_input(btv,TVAUDIO_INPUT_RADIO);
3460 mutex_unlock(&btv->lock);
3464 static int radio_release(struct inode *inode, struct file *file)
3466 struct bttv_fh *fh = file->private_data;
3467 struct bttv *btv = fh->btv;
3468 struct rds_command cmd;
3470 file->private_data = NULL;
3475 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3480 static int radio_querycap(struct file *file, void *priv,
3481 struct v4l2_capability *cap)
3483 struct bttv_fh *fh = priv;
3484 struct bttv *btv = fh->btv;
3486 strcpy(cap->driver, "bttv");
3487 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3488 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3489 cap->version = BTTV_VERSION_CODE;
3490 cap->capabilities = V4L2_CAP_TUNER;
3495 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3497 struct bttv_fh *fh = priv;
3498 struct bttv *btv = fh->btv;
3500 if (UNSET == bttv_tvcards[btv->c.type].tuner)
3504 mutex_lock(&btv->lock);
3505 memset(t, 0, sizeof(*t));
3506 strcpy(t->name, "Radio");
3507 t->type = V4L2_TUNER_RADIO;
3509 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
3511 if (btv->audio_mode_gpio)
3512 btv->audio_mode_gpio(btv, t, 0);
3514 mutex_unlock(&btv->lock);
3519 static int radio_enum_input(struct file *file, void *priv,
3520 struct v4l2_input *i)
3525 strcpy(i->name, "Radio");
3526 i->type = V4L2_INPUT_TYPE_TUNER;
3531 static int radio_g_audio(struct file *file, void *priv,
3532 struct v4l2_audio *a)
3534 if (unlikely(a->index))
3537 strcpy(a->name, "Radio");
3542 static int radio_s_tuner(struct file *file, void *priv,
3543 struct v4l2_tuner *t)
3545 struct bttv_fh *fh = priv;
3546 struct bttv *btv = fh->btv;
3551 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
3555 static int radio_s_audio(struct file *file, void *priv,
3556 struct v4l2_audio *a)
3558 if (unlikely(a->index))
3564 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3572 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3577 static int radio_queryctrl(struct file *file, void *priv,
3578 struct v4l2_queryctrl *c)
3580 const struct v4l2_queryctrl *ctrl;
3582 if (c->id < V4L2_CID_BASE ||
3583 c->id >= V4L2_CID_LASTP1)
3586 if (c->id == V4L2_CID_AUDIO_MUTE) {
3587 ctrl = ctrl_by_id(c->id);
3595 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3601 static ssize_t radio_read(struct file *file, char __user *data,
3602 size_t count, loff_t *ppos)
3604 struct bttv_fh *fh = file->private_data;
3605 struct bttv *btv = fh->btv;
3606 struct rds_command cmd;
3607 cmd.block_count = count/3;
3609 cmd.instance = file;
3610 cmd.result = -ENODEV;
3612 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3617 static unsigned int radio_poll(struct file *file, poll_table *wait)
3619 struct bttv_fh *fh = file->private_data;
3620 struct bttv *btv = fh->btv;
3621 struct rds_command cmd;
3622 cmd.instance = file;
3623 cmd.event_list = wait;
3624 cmd.result = -ENODEV;
3625 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3630 static const struct file_operations radio_fops =
3632 .owner = THIS_MODULE,
3635 .release = radio_release,
3636 .compat_ioctl = v4l_compat_ioctl32,
3637 .ioctl = video_ioctl2,
3638 .llseek = no_llseek,
3642 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3643 .vidioc_querycap = radio_querycap,
3644 .vidioc_g_tuner = radio_g_tuner,
3645 .vidioc_enum_input = radio_enum_input,
3646 .vidioc_g_audio = radio_g_audio,
3647 .vidioc_s_tuner = radio_s_tuner,
3648 .vidioc_s_audio = radio_s_audio,
3649 .vidioc_s_input = radio_s_input,
3650 .vidioc_s_std = radio_s_std,
3651 .vidioc_queryctrl = radio_queryctrl,
3652 .vidioc_g_input = radio_g_input,
3653 .vidioc_g_ctrl = bttv_g_ctrl,
3654 .vidioc_s_ctrl = bttv_s_ctrl,
3655 .vidioc_g_frequency = bttv_g_frequency,
3656 .vidioc_s_frequency = bttv_s_frequency,
3659 static struct video_device radio_template = {
3660 .fops = &radio_fops,
3662 .ioctl_ops = &radio_ioctl_ops,
3665 /* ----------------------------------------------------------------------- */
3666 /* some debug code */
3668 static int bttv_risc_decode(u32 risc)
3670 static char *instr[16] = {
3671 [ BT848_RISC_WRITE >> 28 ] = "write",
3672 [ BT848_RISC_SKIP >> 28 ] = "skip",
3673 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3674 [ BT848_RISC_JUMP >> 28 ] = "jump",
3675 [ BT848_RISC_SYNC >> 28 ] = "sync",
3676 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3677 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3678 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3680 static int incr[16] = {
3681 [ BT848_RISC_WRITE >> 28 ] = 2,
3682 [ BT848_RISC_JUMP >> 28 ] = 2,
3683 [ BT848_RISC_SYNC >> 28 ] = 2,
3684 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3685 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3686 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3688 static char *bits[] = {
3689 "be0", "be1", "be2", "be3/resync",
3690 "set0", "set1", "set2", "set3",
3691 "clr0", "clr1", "clr2", "clr3",
3692 "irq", "res", "eol", "sol",
3696 printk("0x%08x [ %s", risc,
3697 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3698 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3699 if (risc & (1 << (i + 12)))
3700 printk(" %s",bits[i]);
3701 printk(" count=%d ]\n", risc & 0xfff);
3702 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3705 static void bttv_risc_disasm(struct bttv *btv,
3706 struct btcx_riscmem *risc)
3710 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3711 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3712 for (i = 0; i < (risc->size >> 2); i += n) {
3713 printk("%s: 0x%lx: ", btv->c.name,
3714 (unsigned long)(risc->dma + (i<<2)));
3715 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
3716 for (j = 1; j < n; j++)
3717 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3718 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3720 if (0 == risc->cpu[i])
3725 static void bttv_print_riscaddr(struct bttv *btv)
3727 printk(" main: %08Lx\n",
3728 (unsigned long long)btv->main.dma);
3729 printk(" vbi : o=%08Lx e=%08Lx\n",
3730 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3731 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3732 printk(" cap : o=%08Lx e=%08Lx\n",
3733 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3734 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3735 printk(" scr : o=%08Lx e=%08Lx\n",
3736 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3737 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3738 bttv_risc_disasm(btv, &btv->main);
3741 /* ----------------------------------------------------------------------- */
3744 static char *irq_name[] = {
3745 "FMTCHG", // format change detected (525 vs. 625)
3746 "VSYNC", // vertical sync (new field)
3747 "HSYNC", // horizontal sync
3748 "OFLOW", // chroma/luma AGC overflow
3749 "HLOCK", // horizontal lock changed
3750 "VPRES", // video presence changed
3752 "I2CDONE", // hw irc operation finished
3753 "GPINT", // gpio port triggered irq
3755 "RISCI", // risc instruction triggered irq
3756 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3757 "FTRGT", // pixel data fifo overrun
3758 "FDSR", // fifo data stream resyncronisation
3759 "PPERR", // parity error (data transfer)
3760 "RIPERR", // parity error (read risc instructions)
3761 "PABORT", // pci abort
3762 "OCERR", // risc instruction error
3763 "SCERR", // syncronisation error
3766 static void bttv_print_irqbits(u32 print, u32 mark)
3771 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3772 if (print & (1 << i))
3773 printk(" %s",irq_name[i]);
3774 if (mark & (1 << i))
3779 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3781 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3783 (unsigned long)btv->main.dma,
3784 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3785 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3788 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3789 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3790 "Ok, then this is harmless, don't worry ;)\n",
3794 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3796 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3802 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3804 struct bttv_buffer *item;
3806 memset(set,0,sizeof(*set));
3808 /* capture request ? */
3809 if (!list_empty(&btv->capture)) {
3811 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3812 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3814 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3817 /* capture request for other field ? */
3818 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3819 (item->vb.queue.next != &btv->capture)) {
3820 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3821 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3822 if (NULL == set->top &&
3823 V4L2_FIELD_TOP == item->vb.field) {
3826 if (NULL == set->bottom &&
3827 V4L2_FIELD_BOTTOM == item->vb.field) {
3830 if (NULL != set->top && NULL != set->bottom)
3836 /* screen overlay ? */
3837 if (NULL != btv->screen) {
3838 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3839 if (NULL == set->top && NULL == set->bottom) {
3840 set->top = btv->screen;
3841 set->bottom = btv->screen;
3844 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3846 set->top = btv->screen;
3848 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3849 NULL == set->bottom) {
3850 set->bottom = btv->screen;
3855 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3856 btv->c.nr,set->top, set->bottom,
3857 btv->screen,set->frame_irq,set->top_irq);
3862 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3863 struct bttv_buffer_set *curr, unsigned int state)
3867 do_gettimeofday(&ts);
3869 if (wakeup->top == wakeup->bottom) {
3870 if (NULL != wakeup->top && curr->top != wakeup->top) {
3872 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3873 wakeup->top->vb.ts = ts;
3874 wakeup->top->vb.field_count = btv->field_count;
3875 wakeup->top->vb.state = state;
3876 wake_up(&wakeup->top->vb.done);
3879 if (NULL != wakeup->top && curr->top != wakeup->top) {
3881 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3882 wakeup->top->vb.ts = ts;
3883 wakeup->top->vb.field_count = btv->field_count;
3884 wakeup->top->vb.state = state;
3885 wake_up(&wakeup->top->vb.done);
3887 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3889 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3890 wakeup->bottom->vb.ts = ts;
3891 wakeup->bottom->vb.field_count = btv->field_count;
3892 wakeup->bottom->vb.state = state;
3893 wake_up(&wakeup->bottom->vb.done);
3899 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3907 do_gettimeofday(&ts);
3909 wakeup->vb.field_count = btv->field_count;
3910 wakeup->vb.state = state;
3911 wake_up(&wakeup->vb.done);
3914 static void bttv_irq_timeout(unsigned long data)
3916 struct bttv *btv = (struct bttv *)data;
3917 struct bttv_buffer_set old,new;
3918 struct bttv_buffer *ovbi;
3919 struct bttv_buffer *item;
3920 unsigned long flags;
3923 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3924 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3925 btread(BT848_RISC_COUNT));
3926 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3930 spin_lock_irqsave(&btv->s_lock,flags);
3932 /* deactivate stuff */
3933 memset(&new,0,sizeof(new));
3939 bttv_buffer_activate_video(btv, &new);
3940 bttv_buffer_activate_vbi(btv, NULL);
3941 bttv_set_dma(btv, 0);
3944 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3945 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
3947 /* cancel all outstanding capture / vbi requests */
3948 while (!list_empty(&btv->capture)) {
3949 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3950 list_del(&item->vb.queue);
3951 item->vb.state = VIDEOBUF_ERROR;
3952 wake_up(&item->vb.done);
3954 while (!list_empty(&btv->vcapture)) {
3955 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3956 list_del(&item->vb.queue);
3957 item->vb.state = VIDEOBUF_ERROR;
3958 wake_up(&item->vb.done);
3962 spin_unlock_irqrestore(&btv->s_lock,flags);
3966 bttv_irq_wakeup_top(struct bttv *btv)
3968 struct bttv_buffer *wakeup = btv->curr.top;
3973 spin_lock(&btv->s_lock);
3974 btv->curr.top_irq = 0;
3975 btv->curr.top = NULL;
3976 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3978 do_gettimeofday(&wakeup->vb.ts);
3979 wakeup->vb.field_count = btv->field_count;
3980 wakeup->vb.state = VIDEOBUF_DONE;
3981 wake_up(&wakeup->vb.done);
3982 spin_unlock(&btv->s_lock);
3985 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3989 if (rc > risc->dma + risc->size)
3995 bttv_irq_switch_video(struct bttv *btv)
3997 struct bttv_buffer_set new;
3998 struct bttv_buffer_set old;
4001 spin_lock(&btv->s_lock);
4003 /* new buffer set */
4004 bttv_irq_next_video(btv, &new);
4005 rc = btread(BT848_RISC_COUNT);
4006 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4007 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4010 bttv_irq_debug_low_latency(btv, rc);
4011 spin_unlock(&btv->s_lock);
4018 btv->loop_irq &= ~1;
4019 bttv_buffer_activate_video(btv, &new);
4020 bttv_set_dma(btv, 0);
4023 if (UNSET != btv->new_input) {
4024 video_mux(btv,btv->new_input);
4025 btv->new_input = UNSET;
4028 /* wake up finished buffers */
4029 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
4030 spin_unlock(&btv->s_lock);
4034 bttv_irq_switch_vbi(struct bttv *btv)
4036 struct bttv_buffer *new = NULL;
4037 struct bttv_buffer *old;
4040 spin_lock(&btv->s_lock);
4042 if (!list_empty(&btv->vcapture))
4043 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4046 rc = btread(BT848_RISC_COUNT);
4047 if (NULL != old && (is_active(&old->top, rc) ||
4048 is_active(&old->bottom, rc))) {
4051 bttv_irq_debug_low_latency(btv, rc);
4052 spin_unlock(&btv->s_lock);
4058 btv->loop_irq &= ~4;
4059 bttv_buffer_activate_vbi(btv, new);
4060 bttv_set_dma(btv, 0);
4062 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
4063 spin_unlock(&btv->s_lock);
4066 static irqreturn_t bttv_irq(int irq, void *dev_id)
4074 btv=(struct bttv *)dev_id;
4076 if (btv->custom_irq)
4077 handled = btv->custom_irq(btv);
4081 /* get/clear interrupt status bits */
4082 stat=btread(BT848_INT_STAT);
4083 astat=stat&btread(BT848_INT_MASK);
4087 btwrite(stat,BT848_INT_STAT);
4089 /* get device status bits */
4090 dstat=btread(BT848_DSTATUS);
4093 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4094 "riscs=%x, riscc=%08x, ",
4095 btv->c.nr, count, btv->field_count,
4096 stat>>28, btread(BT848_RISC_COUNT));
4097 bttv_print_irqbits(stat,astat);
4098 if (stat & BT848_INT_HLOCK)
4099 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4101 if (stat & BT848_INT_VPRES)
4102 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4104 if (stat & BT848_INT_FMTCHG)
4105 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4110 if (astat&BT848_INT_VSYNC)
4113 if ((astat & BT848_INT_GPINT) && btv->remote) {
4114 wake_up(&btv->gpioq);
4115 bttv_input_irq(btv);
4118 if (astat & BT848_INT_I2CDONE) {
4119 btv->i2c_done = stat;
4120 wake_up(&btv->i2c_queue);
4123 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
4124 bttv_irq_switch_vbi(btv);
4126 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
4127 bttv_irq_wakeup_top(btv);
4129 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
4130 bttv_irq_switch_video(btv);
4132 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
4133 audio_mute(btv, btv->mute); /* trigger automute */
4135 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4136 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4137 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4138 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4139 btread(BT848_RISC_COUNT));
4140 bttv_print_irqbits(stat,astat);
4143 bttv_print_riscaddr(btv);
4145 if (fdsr && astat & BT848_INT_FDSR) {
4146 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4147 btv->c.nr,btread(BT848_RISC_COUNT));
4149 bttv_print_riscaddr(btv);
4155 if (count > 8 || !(astat & BT848_INT_GPINT)) {
4156 btwrite(0, BT848_INT_MASK);
4159 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4162 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4164 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4168 bttv_print_irqbits(stat,astat);
4176 return IRQ_RETVAL(handled);
4180 /* ----------------------------------------------------------------------- */
4181 /* initialitation */
4183 static struct video_device *vdev_init(struct bttv *btv,
4184 const struct video_device *template,
4185 const char *type_name)
4187 struct video_device *vfd;
4189 vfd = video_device_alloc();
4194 vfd->parent = &btv->c.pci->dev;
4195 vfd->release = video_device_release;
4196 vfd->debug = bttv_debug;
4197 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4198 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4199 type_name, bttv_tvcards[btv->c.type].name);
4203 static void bttv_unregister_video(struct bttv *btv)
4205 if (btv->video_dev) {
4206 if (-1 != btv->video_dev->minor)
4207 video_unregister_device(btv->video_dev);
4209 video_device_release(btv->video_dev);
4210 btv->video_dev = NULL;
4213 if (-1 != btv->vbi_dev->minor)
4214 video_unregister_device(btv->vbi_dev);
4216 video_device_release(btv->vbi_dev);
4217 btv->vbi_dev = NULL;
4219 if (btv->radio_dev) {
4220 if (-1 != btv->radio_dev->minor)
4221 video_unregister_device(btv->radio_dev);
4223 video_device_release(btv->radio_dev);
4224 btv->radio_dev = NULL;
4228 /* register video4linux devices */
4229 static int __devinit bttv_register_video(struct bttv *btv)
4232 printk("bttv: Overlay support disabled.\n");
4235 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4237 if (NULL == btv->video_dev)
4239 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
4241 printk(KERN_INFO "bttv%d: registered device video%d\n",
4242 btv->c.nr,btv->video_dev->minor & 0x1f);
4243 if (device_create_file(&btv->video_dev->dev,
4244 &dev_attr_card)<0) {
4245 printk(KERN_ERR "bttv%d: device_create_file 'card' "
4246 "failed\n", btv->c.nr);
4251 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
4253 if (NULL == btv->vbi_dev)
4255 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
4257 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
4258 btv->c.nr,btv->vbi_dev->minor & 0x1f);
4260 if (!btv->has_radio)
4263 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4264 if (NULL == btv->radio_dev)
4266 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
4268 printk(KERN_INFO "bttv%d: registered device radio%d\n",
4269 btv->c.nr,btv->radio_dev->minor & 0x1f);
4275 bttv_unregister_video(btv);
4280 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4281 /* response on cards with no firmware is not enabled by OF */
4282 static void pci_set_command(struct pci_dev *dev)
4284 #if defined(__powerpc__)
4287 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4288 cmd = (cmd | PCI_COMMAND_MEMORY );
4289 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4293 static int __devinit bttv_probe(struct pci_dev *dev,
4294 const struct pci_device_id *pci_id)
4300 if (bttv_num == BTTV_MAX)
4302 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
4303 btv=&bttvs[bttv_num];
4304 memset(btv,0,sizeof(*btv));
4305 btv->c.nr = bttv_num;
4306 sprintf(btv->c.name,"bttv%d",btv->c.nr);
4308 /* initialize structs / fill in defaults */
4309 mutex_init(&btv->lock);
4310 spin_lock_init(&btv->s_lock);
4311 spin_lock_init(&btv->gpio_lock);
4312 init_waitqueue_head(&btv->gpioq);
4313 init_waitqueue_head(&btv->i2c_queue);
4314 INIT_LIST_HEAD(&btv->c.subs);
4315 INIT_LIST_HEAD(&btv->capture);
4316 INIT_LIST_HEAD(&btv->vcapture);
4317 v4l2_prio_init(&btv->prio);
4319 init_timer(&btv->timeout);
4320 btv->timeout.function = bttv_irq_timeout;
4321 btv->timeout.data = (unsigned long)btv;
4324 btv->tuner_type = UNSET;
4325 btv->new_input = UNSET;
4326 btv->has_radio=radio[btv->c.nr];
4328 /* pci stuff (init, get irq/mmio, ... */
4330 btv->id = dev->device;
4331 if (pci_enable_device(dev)) {
4332 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4336 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
4337 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4341 if (!request_mem_region(pci_resource_start(dev,0),
4342 pci_resource_len(dev,0),
4344 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4346 (unsigned long long)pci_resource_start(dev,0));
4349 pci_set_master(dev);
4350 pci_set_command(dev);
4351 pci_set_drvdata(dev,btv);
4353 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4354 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4355 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4356 bttv_num,btv->id, btv->revision, pci_name(dev));
4357 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4358 btv->c.pci->irq, lat,
4359 (unsigned long long)pci_resource_start(dev,0));
4362 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4363 if (NULL == btv->bt848_mmio) {
4364 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4372 /* disable irqs, register irq handler */
4373 btwrite(0, BT848_INT_MASK);
4374 result = request_irq(btv->c.pci->irq, bttv_irq,
4375 IRQF_SHARED | IRQF_DISABLED,btv->c.name,(void *)btv);
4377 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4378 bttv_num,btv->c.pci->irq);
4382 if (0 != bttv_handle_chipset(btv)) {
4387 /* init options from insmod args */
4388 btv->opt_combfilter = combfilter;
4389 btv->opt_lumafilter = lumafilter;
4390 btv->opt_automute = automute;
4391 btv->opt_chroma_agc = chroma_agc;
4392 btv->opt_adc_crush = adc_crush;
4393 btv->opt_vcr_hack = vcr_hack;
4394 btv->opt_whitecrush_upper = whitecrush_upper;
4395 btv->opt_whitecrush_lower = whitecrush_lower;
4396 btv->opt_uv_ratio = uv_ratio;
4397 btv->opt_full_luma_range = full_luma_range;
4398 btv->opt_coring = coring;
4400 /* fill struct bttv with some useful defaults */
4401 btv->init.btv = btv;
4402 btv->init.ov.w.width = 320;
4403 btv->init.ov.w.height = 240;
4404 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4405 btv->init.width = 320;
4406 btv->init.height = 240;
4409 /* initialize hardware */
4411 bttv_gpio_tracking(btv,"pre-init");
4413 bttv_risc_init_main(btv);
4417 btwrite(0x00, BT848_GPIO_REG_INP);
4418 btwrite(0x00, BT848_GPIO_OUT_EN);
4420 bttv_gpio_tracking(btv,"init");
4422 /* needs to be done before i2c is registered */
4423 bttv_init_card1(btv);
4425 /* register i2c + gpio */
4428 /* some card-specific stuff (needs working i2c) */
4429 bttv_init_card2(btv);
4432 /* register video4linux + input */
4433 if (!bttv_tvcards[btv->c.type].no_video) {
4434 bttv_register_video(btv);
4435 bt848_bright(btv,32768);
4436 bt848_contrast(btv,32768);
4437 bt848_hue(btv,32768);
4438 bt848_sat(btv,32768);
4440 set_input(btv, 0, btv->tvnorm);
4441 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4442 btv->crop[1] = btv->crop[0]; /* current = default */
4443 disclaim_vbi_lines(btv);
4444 disclaim_video_lines(btv);
4447 /* add subdevices and autoload dvb-bt8xx if needed */
4448 if (bttv_tvcards[btv->c.type].has_dvb) {
4449 bttv_sub_add_device(&btv->c, "dvb");
4450 request_modules(btv);
4453 bttv_input_init(btv);
4455 /* everything is fine */
4460 free_irq(btv->c.pci->irq,btv);
4463 if (btv->bt848_mmio)
4464 iounmap(btv->bt848_mmio);
4465 release_mem_region(pci_resource_start(btv->c.pci,0),
4466 pci_resource_len(btv->c.pci,0));
4467 pci_set_drvdata(dev,NULL);
4471 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4473 struct bttv *btv = pci_get_drvdata(pci_dev);
4476 printk("bttv%d: unloading\n",btv->c.nr);
4478 /* shutdown everything (DMA+IRQs) */
4479 btand(~15, BT848_GPIO_DMA_CTL);
4480 btwrite(0, BT848_INT_MASK);
4481 btwrite(~0x0, BT848_INT_STAT);
4482 btwrite(0x0, BT848_GPIO_OUT_EN);
4484 bttv_gpio_tracking(btv,"cleanup");
4486 /* tell gpio modules we are leaving ... */
4488 wake_up(&btv->gpioq);
4489 bttv_input_fini(btv);
4490 bttv_sub_del_devices(&btv->c);
4492 /* unregister i2c_bus + input */
4495 /* unregister video4linux */
4496 bttv_unregister_video(btv);
4498 /* free allocated memory */
4499 btcx_riscmem_free(btv->c.pci,&btv->main);
4501 /* free ressources */
4502 free_irq(btv->c.pci->irq,btv);
4503 iounmap(btv->bt848_mmio);
4504 release_mem_region(pci_resource_start(btv->c.pci,0),
4505 pci_resource_len(btv->c.pci,0));
4507 pci_set_drvdata(pci_dev, NULL);
4512 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4514 struct bttv *btv = pci_get_drvdata(pci_dev);
4515 struct bttv_buffer_set idle;
4516 unsigned long flags;
4518 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4520 /* stop dma + irqs */
4521 spin_lock_irqsave(&btv->s_lock,flags);
4522 memset(&idle, 0, sizeof(idle));
4523 btv->state.video = btv->curr;
4524 btv->state.vbi = btv->cvbi;
4525 btv->state.loop_irq = btv->loop_irq;
4528 bttv_buffer_activate_video(btv, &idle);
4529 bttv_buffer_activate_vbi(btv, NULL);
4530 bttv_set_dma(btv, 0);
4531 btwrite(0, BT848_INT_MASK);
4532 spin_unlock_irqrestore(&btv->s_lock,flags);
4534 /* save bt878 state */
4535 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4536 btv->state.gpio_data = gpio_read();
4538 /* save pci state */
4539 pci_save_state(pci_dev);
4540 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4541 pci_disable_device(pci_dev);
4542 btv->state.disabled = 1;
4547 static int bttv_resume(struct pci_dev *pci_dev)
4549 struct bttv *btv = pci_get_drvdata(pci_dev);
4550 unsigned long flags;
4553 dprintk("bttv%d: resume\n", btv->c.nr);
4555 /* restore pci state */
4556 if (btv->state.disabled) {
4557 err=pci_enable_device(pci_dev);
4559 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4563 btv->state.disabled = 0;
4565 err=pci_set_power_state(pci_dev, PCI_D0);
4567 pci_disable_device(pci_dev);
4568 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4570 btv->state.disabled = 1;
4574 pci_restore_state(pci_dev);
4576 /* restore bt878 state */
4577 bttv_reinit_bt848(btv);
4578 gpio_inout(0xffffff, btv->state.gpio_enable);
4579 gpio_write(btv->state.gpio_data);
4582 spin_lock_irqsave(&btv->s_lock,flags);
4583 btv->curr = btv->state.video;
4584 btv->cvbi = btv->state.vbi;
4585 btv->loop_irq = btv->state.loop_irq;
4586 bttv_buffer_activate_video(btv, &btv->curr);
4587 bttv_buffer_activate_vbi(btv, btv->cvbi);
4588 bttv_set_dma(btv, 0);
4589 spin_unlock_irqrestore(&btv->s_lock,flags);
4594 static struct pci_device_id bttv_pci_tbl[] = {
4595 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4596 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4597 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4598 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4599 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4600 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4601 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4602 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4606 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4608 static struct pci_driver bttv_pci_driver = {
4610 .id_table = bttv_pci_tbl,
4611 .probe = bttv_probe,
4612 .remove = __devexit_p(bttv_remove),
4614 .suspend = bttv_suspend,
4615 .resume = bttv_resume,
4619 static int __init bttv_init_module(void)
4625 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4626 (BTTV_VERSION_CODE >> 16) & 0xff,
4627 (BTTV_VERSION_CODE >> 8) & 0xff,
4628 BTTV_VERSION_CODE & 0xff);
4630 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4631 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4633 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4635 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4636 gbufsize = BTTV_MAX_FBUF;
4637 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4639 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4640 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4642 bttv_check_chipset();
4644 ret = bus_register(&bttv_sub_bus_type);
4646 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4649 ret = pci_register_driver(&bttv_pci_driver);
4651 bus_unregister(&bttv_sub_bus_type);
4656 static void __exit bttv_cleanup_module(void)
4658 pci_unregister_driver(&bttv_pci_driver);
4659 bus_unregister(&bttv_sub_bus_type);
4662 module_init(bttv_init_module);
4663 module_exit(bttv_cleanup_module);