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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/delay.h>
42 #include <linux/slab.h>
43 #include <linux/errno.h>
45 #include <linux/kernel.h>
46 #include <linux/sched.h>
47 #include <linux/interrupt.h>
48 #include <linux/kdev_t.h>
50 #include <media/v4l2-common.h>
51 #include <media/v4l2-ioctl.h>
52 #include <media/v4l2-event.h>
53 #include <media/i2c/tvaudio.h>
54 #include <media/drv-intf/msp3400.h>
56 #include <linux/dma-mapping.h>
59 #include <asm/byteorder.h>
61 #include <media/i2c/saa6588.h>
63 #define BTTV_VERSION "0.9.19"
65 unsigned int bttv_num; /* number of Bt848s in use */
66 struct bttv *bttvs[BTTV_MAX];
68 unsigned int bttv_debug;
69 unsigned int bttv_verbose = 1;
70 unsigned int bttv_gpio;
72 /* config variables */
74 static unsigned int bigendian=1;
76 static unsigned int bigendian;
78 static unsigned int radio[BTTV_MAX];
79 static unsigned int irq_debug;
80 static unsigned int gbuffers = 8;
81 static unsigned int gbufsize = 0x208000;
82 static unsigned int reset_crop = 1;
84 static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
85 static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
86 static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
87 static int debug_latency;
88 static int disable_ir;
90 static unsigned int fdsr;
93 static unsigned int combfilter;
94 static unsigned int lumafilter;
95 static unsigned int automute = 1;
96 static unsigned int chroma_agc;
97 static unsigned int agc_crush = 1;
98 static unsigned int whitecrush_upper = 0xCF;
99 static unsigned int whitecrush_lower = 0x7F;
100 static unsigned int vcr_hack;
101 static unsigned int irq_iswitch;
102 static unsigned int uv_ratio = 50;
103 static unsigned int full_luma_range;
104 static unsigned int coring;
106 /* API features (turn on/off stuff for testing) */
107 static unsigned int v4l2 = 1;
110 module_param(bttv_verbose, int, 0644);
111 module_param(bttv_gpio, int, 0644);
112 module_param(bttv_debug, int, 0644);
113 module_param(irq_debug, int, 0644);
114 module_param(debug_latency, int, 0644);
115 module_param(disable_ir, int, 0444);
117 module_param(fdsr, int, 0444);
118 module_param(gbuffers, int, 0444);
119 module_param(gbufsize, int, 0444);
120 module_param(reset_crop, int, 0444);
122 module_param(v4l2, int, 0644);
123 module_param(bigendian, int, 0644);
124 module_param(irq_iswitch, int, 0644);
125 module_param(combfilter, int, 0444);
126 module_param(lumafilter, int, 0444);
127 module_param(automute, int, 0444);
128 module_param(chroma_agc, int, 0444);
129 module_param(agc_crush, int, 0444);
130 module_param(whitecrush_upper, int, 0444);
131 module_param(whitecrush_lower, int, 0444);
132 module_param(vcr_hack, int, 0444);
133 module_param(uv_ratio, int, 0444);
134 module_param(full_luma_range, int, 0444);
135 module_param(coring, int, 0444);
137 module_param_array(radio, int, NULL, 0444);
138 module_param_array(video_nr, int, NULL, 0444);
139 module_param_array(radio_nr, int, NULL, 0444);
140 module_param_array(vbi_nr, int, NULL, 0444);
142 MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
143 MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
144 MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
145 MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
146 MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
147 MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
148 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
149 MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
150 MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
151 MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default "
152 "is 1 (yes) for compatibility with older applications");
153 MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
154 MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
155 MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
156 MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
157 MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
158 MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
159 MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
160 MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
161 MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
162 MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
163 MODULE_PARM_DESC(video_nr, "video device numbers");
164 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
165 MODULE_PARM_DESC(radio_nr, "radio device numbers");
167 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
168 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
169 MODULE_LICENSE("GPL");
170 MODULE_VERSION(BTTV_VERSION);
172 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
173 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
174 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
175 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
176 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
177 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
178 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
179 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
180 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
181 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
183 /* ----------------------------------------------------------------------- */
186 static ssize_t show_card(struct device *cd,
187 struct device_attribute *attr, char *buf)
189 struct video_device *vfd = to_video_device(cd);
190 struct bttv *btv = video_get_drvdata(vfd);
191 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
193 static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
195 /* ----------------------------------------------------------------------- */
196 /* dvb auto-load setup */
197 #if defined(CONFIG_MODULES) && defined(MODULE)
198 static void request_module_async(struct work_struct *work)
200 request_module("dvb-bt8xx");
203 static void request_modules(struct bttv *dev)
205 INIT_WORK(&dev->request_module_wk, request_module_async);
206 schedule_work(&dev->request_module_wk);
209 static void flush_request_modules(struct bttv *dev)
211 flush_work(&dev->request_module_wk);
214 #define request_modules(dev)
215 #define flush_request_modules(dev) do {} while(0)
216 #endif /* CONFIG_MODULES */
219 /* ----------------------------------------------------------------------- */
222 /* special timing tables from conexant... */
223 static u8 SRAM_Table[][60] =
225 /* PAL digital input over GPIO[7:0] */
227 45, // 45 bytes following
228 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
229 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
230 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
231 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
232 0x37,0x00,0xAF,0x21,0x00
234 /* NTSC digital input over GPIO[7:0] */
236 51, // 51 bytes following
237 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
238 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
239 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
240 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
241 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
244 // TGB_NTSC392 // quartzsight
245 // This table has been modified to be used for Fusion Rev D
247 0x2A, // size of table = 42
248 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
249 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
250 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
251 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
256 /* minhdelayx1 first video pixel we can capture on a line and
257 hdelayx1 start of active video, both relative to rising edge of
258 /HRESET pulse (0H) in 1 / fCLKx1.
259 swidth width of active video and
260 totalwidth total line width, both in 1 / fCLKx1.
261 sqwidth total line width in square pixels.
262 vdelay start of active video in 2 * field lines relative to
263 trailing edge of /VRESET pulse (VDELAY register).
264 sheight height of active video in 2 * field lines.
265 extraheight Added to sheight for cropcap.bounds.height only
266 videostart0 ITU-R frame line number of the line corresponding
267 to vdelay in the first field. */
268 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
269 vdelay, sheight, extraheight, videostart0) \
270 .cropcap.bounds.left = minhdelayx1, \
271 /* * 2 because vertically we count field lines times two, */ \
272 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
273 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
274 /* 4 is a safety margin at the end of the line. */ \
275 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
276 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
278 .cropcap.defrect.left = hdelayx1, \
279 .cropcap.defrect.top = (videostart0) * 2, \
280 .cropcap.defrect.width = swidth, \
281 .cropcap.defrect.height = sheight, \
282 .cropcap.pixelaspect.numerator = totalwidth, \
283 .cropcap.pixelaspect.denominator = sqwidth,
285 const struct bttv_tvnorm bttv_tvnorms[] = {
287 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
288 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
290 .v4l2_id = V4L2_STD_PAL,
298 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
299 .scaledtwidth = 1135,
303 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
305 /* ITU-R frame line number of the first VBI line
306 we can capture, of the first and second field.
307 The last line is determined by cropcap.bounds. */
308 .vbistart = { 7, 320 },
309 CROPCAP(/* minhdelayx1 */ 68,
311 /* Should be (768 * 1135 + 944 / 2) / 944.
312 cropcap.defrect is used for image width
313 checks, so we keep the old value 924. */
315 /* totalwidth */ 1135,
319 /* bt878 (and bt848?) can capture another
320 line below active video. */
322 /* videostart0 */ 23)
324 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
332 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
337 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
339 .vbistart = { 10, 273 },
340 CROPCAP(/* minhdelayx1 */ 68,
342 /* Should be (640 * 910 + 780 / 2) / 780? */
344 /* totalwidth */ 910,
349 /* videostart0 */ 23)
351 .v4l2_id = V4L2_STD_SECAM,
359 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
360 .scaledtwidth = 1135,
365 .sram = 0, /* like PAL, correct? */
366 .vbistart = { 7, 320 },
367 CROPCAP(/* minhdelayx1 */ 68,
370 /* totalwidth */ 1135,
375 /* videostart0 */ 23)
377 .v4l2_id = V4L2_STD_PAL_Nc,
385 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
392 .vbistart = { 7, 320 },
393 CROPCAP(/* minhdelayx1 */ 68,
395 /* swidth */ (640 * 910 + 780 / 2) / 780,
396 /* totalwidth */ 910,
401 /* videostart0 */ 23)
403 .v4l2_id = V4L2_STD_PAL_M,
411 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
418 .vbistart = { 10, 273 },
419 CROPCAP(/* minhdelayx1 */ 68,
421 /* swidth */ (640 * 910 + 780 / 2) / 780,
422 /* totalwidth */ 910,
427 /* videostart0 */ 23)
429 .v4l2_id = V4L2_STD_PAL_N,
437 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
444 .vbistart = { 7, 320 },
445 CROPCAP(/* minhdelayx1 */ 68,
447 /* swidth */ (768 * 1135 + 944 / 2) / 944,
448 /* totalwidth */ 1135,
453 /* videostart0 */ 23)
455 .v4l2_id = V4L2_STD_NTSC_M_JP,
463 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
470 .vbistart = { 10, 273 },
471 CROPCAP(/* minhdelayx1 */ 68,
473 /* swidth */ (640 * 910 + 780 / 2) / 780,
474 /* totalwidth */ 910,
479 /* videostart0 */ 23)
481 /* that one hopefully works with the strange timing
482 * which video recorders produce when playing a NTSC
483 * tape on a PAL TV ... */
484 .v4l2_id = V4L2_STD_PAL_60,
492 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
493 .scaledtwidth = 1135,
500 .vbistart = { 10, 273 },
501 CROPCAP(/* minhdelayx1 */ 68,
504 /* totalwidth */ 1135,
509 /* videostart0 */ 23)
512 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
514 /* ----------------------------------------------------------------------- */
516 packed pixel formats must come first */
517 static const struct bttv_format formats[] = {
519 .name = "8 bpp, gray",
520 .fourcc = V4L2_PIX_FMT_GREY,
521 .btformat = BT848_COLOR_FMT_Y8,
523 .flags = FORMAT_FLAGS_PACKED,
525 .name = "8 bpp, dithered color",
526 .fourcc = V4L2_PIX_FMT_HI240,
527 .btformat = BT848_COLOR_FMT_RGB8,
529 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
531 .name = "15 bpp RGB, le",
532 .fourcc = V4L2_PIX_FMT_RGB555,
533 .btformat = BT848_COLOR_FMT_RGB15,
535 .flags = FORMAT_FLAGS_PACKED,
537 .name = "15 bpp RGB, be",
538 .fourcc = V4L2_PIX_FMT_RGB555X,
539 .btformat = BT848_COLOR_FMT_RGB15,
540 .btswap = 0x03, /* byteswap */
542 .flags = FORMAT_FLAGS_PACKED,
544 .name = "16 bpp RGB, le",
545 .fourcc = V4L2_PIX_FMT_RGB565,
546 .btformat = BT848_COLOR_FMT_RGB16,
548 .flags = FORMAT_FLAGS_PACKED,
550 .name = "16 bpp RGB, be",
551 .fourcc = V4L2_PIX_FMT_RGB565X,
552 .btformat = BT848_COLOR_FMT_RGB16,
553 .btswap = 0x03, /* byteswap */
555 .flags = FORMAT_FLAGS_PACKED,
557 .name = "24 bpp RGB, le",
558 .fourcc = V4L2_PIX_FMT_BGR24,
559 .btformat = BT848_COLOR_FMT_RGB24,
561 .flags = FORMAT_FLAGS_PACKED,
563 .name = "32 bpp RGB, le",
564 .fourcc = V4L2_PIX_FMT_BGR32,
565 .btformat = BT848_COLOR_FMT_RGB32,
567 .flags = FORMAT_FLAGS_PACKED,
569 .name = "32 bpp RGB, be",
570 .fourcc = V4L2_PIX_FMT_RGB32,
571 .btformat = BT848_COLOR_FMT_RGB32,
572 .btswap = 0x0f, /* byte+word swap */
574 .flags = FORMAT_FLAGS_PACKED,
576 .name = "4:2:2, packed, YUYV",
577 .fourcc = V4L2_PIX_FMT_YUYV,
578 .btformat = BT848_COLOR_FMT_YUY2,
580 .flags = FORMAT_FLAGS_PACKED,
582 .name = "4:2:2, packed, UYVY",
583 .fourcc = V4L2_PIX_FMT_UYVY,
584 .btformat = BT848_COLOR_FMT_YUY2,
585 .btswap = 0x03, /* byteswap */
587 .flags = FORMAT_FLAGS_PACKED,
589 .name = "4:2:2, planar, Y-Cb-Cr",
590 .fourcc = V4L2_PIX_FMT_YUV422P,
591 .btformat = BT848_COLOR_FMT_YCrCb422,
593 .flags = FORMAT_FLAGS_PLANAR,
597 .name = "4:2:0, planar, Y-Cb-Cr",
598 .fourcc = V4L2_PIX_FMT_YUV420,
599 .btformat = BT848_COLOR_FMT_YCrCb422,
601 .flags = FORMAT_FLAGS_PLANAR,
605 .name = "4:2:0, planar, Y-Cr-Cb",
606 .fourcc = V4L2_PIX_FMT_YVU420,
607 .btformat = BT848_COLOR_FMT_YCrCb422,
609 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
613 .name = "4:1:1, planar, Y-Cb-Cr",
614 .fourcc = V4L2_PIX_FMT_YUV411P,
615 .btformat = BT848_COLOR_FMT_YCrCb411,
617 .flags = FORMAT_FLAGS_PLANAR,
621 .name = "4:1:0, planar, Y-Cb-Cr",
622 .fourcc = V4L2_PIX_FMT_YUV410,
623 .btformat = BT848_COLOR_FMT_YCrCb411,
625 .flags = FORMAT_FLAGS_PLANAR,
629 .name = "4:1:0, planar, Y-Cr-Cb",
630 .fourcc = V4L2_PIX_FMT_YVU410,
631 .btformat = BT848_COLOR_FMT_YCrCb411,
633 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
637 .name = "raw scanlines",
639 .btformat = BT848_COLOR_FMT_RAW,
641 .flags = FORMAT_FLAGS_RAW,
644 static const unsigned int FORMATS = ARRAY_SIZE(formats);
646 /* ----------------------------------------------------------------------- */
647 /* resource management */
650 RESOURCE_ allocated by freed by
652 VIDEO_READ bttv_read 1) bttv_read 2)
654 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
655 VIDIOC_QBUF 1) bttv_release
658 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
659 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
662 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
663 VIDIOC_QBUF 1) bttv_release
664 bttv_read, bttv_poll 1) 4)
666 1) The resource must be allocated when we enter buffer prepare functions
667 and remain allocated while buffers are in the DMA queue.
668 2) This is a single frame read.
669 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
670 RESOURCE_OVERLAY is allocated.
671 4) This is a continuous read, implies VIDIOC_STREAMON.
673 Note this driver permits video input and standard changes regardless if
674 resources are allocated.
677 #define VBI_RESOURCES (RESOURCE_VBI)
678 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
679 RESOURCE_VIDEO_STREAM | \
683 int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
685 int xbits; /* mutual exclusive resources */
687 if (fh->resources & bit)
688 /* have it already allocated */
692 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
693 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
696 if (btv->resources & xbits) {
697 /* no, someone else uses it */
701 if ((bit & VIDEO_RESOURCES)
702 && 0 == (btv->resources & VIDEO_RESOURCES)) {
703 /* Do crop - use current, don't - use default parameters. */
704 __s32 top = btv->crop[!!fh->do_crop].rect.top;
706 if (btv->vbi_end > top)
709 /* We cannot capture the same line as video and VBI data.
710 Claim scan lines crop[].rect.top to bottom. */
711 btv->crop_start = top;
712 } else if (bit & VBI_RESOURCES) {
713 __s32 end = fh->vbi_fmt.end;
715 if (end > btv->crop_start)
718 /* Claim scan lines above fh->vbi_fmt.end. */
722 /* it's free, grab it */
723 fh->resources |= bit;
724 btv->resources |= bit;
732 int check_btres(struct bttv_fh *fh, int bit)
734 return (fh->resources & bit);
738 int locked_btres(struct bttv *btv, int bit)
740 return (btv->resources & bit);
743 /* Call with btv->lock down. */
745 disclaim_vbi_lines(struct bttv *btv)
750 /* Call with btv->lock down. */
752 disclaim_video_lines(struct bttv *btv)
754 const struct bttv_tvnorm *tvnorm;
757 tvnorm = &bttv_tvnorms[btv->tvnorm];
758 btv->crop_start = tvnorm->cropcap.bounds.top
759 + tvnorm->cropcap.bounds.height;
761 /* VBI capturing ends at VDELAY, start of video capturing, no
762 matter how many lines the VBI RISC program expects. When video
763 capturing is off, it shall no longer "preempt" VBI capturing,
764 so we set VDELAY to maximum. */
765 crop = btread(BT848_E_CROP) | 0xc0;
766 btwrite(crop, BT848_E_CROP);
767 btwrite(0xfe, BT848_E_VDELAY_LO);
768 btwrite(crop, BT848_O_CROP);
769 btwrite(0xfe, BT848_O_VDELAY_LO);
773 void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
775 if ((fh->resources & bits) != bits) {
776 /* trying to free resources not allocated by us ... */
777 pr_err("BUG! (btres)\n");
779 fh->resources &= ~bits;
780 btv->resources &= ~bits;
782 bits = btv->resources;
784 if (0 == (bits & VIDEO_RESOURCES))
785 disclaim_video_lines(btv);
787 if (0 == (bits & VBI_RESOURCES))
788 disclaim_vbi_lines(btv);
791 /* ----------------------------------------------------------------------- */
792 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
794 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
795 PLL_X = Reference pre-divider (0=1, 1=2)
796 PLL_C = Post divider (0=6, 1=4)
797 PLL_I = Integer input
798 PLL_F = Fractional input
800 F_input = 28.636363 MHz:
801 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
804 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
806 unsigned char fl, fh, fi;
808 /* prevent overflows */
821 btwrite(fl, BT848_PLL_F_LO);
822 btwrite(fh, BT848_PLL_F_HI);
823 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
826 static void set_pll(struct bttv *btv)
830 if (!btv->pll.pll_crystal)
833 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
834 dprintk("%d: PLL: no change required\n", btv->c.nr);
838 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
840 if (btv->pll.pll_current == 0)
843 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
844 btv->c.nr, btv->pll.pll_ifreq);
845 btwrite(0x00,BT848_TGCTRL);
846 btwrite(0x00,BT848_PLL_XCI);
847 btv->pll.pll_current = 0;
852 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
854 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
855 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
857 for (i=0; i<10; i++) {
858 /* Let other people run while the PLL stabilizes */
861 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
862 btwrite(0,BT848_DSTATUS);
864 btwrite(0x08,BT848_TGCTRL);
865 btv->pll.pll_current = btv->pll.pll_ofreq;
867 pr_info("PLL set ok\n");
871 btv->pll.pll_current = -1;
873 pr_info("Setting PLL failed\n");
877 /* used to switch between the bt848's analog/digital video capture modes */
878 static void bt848A_set_timing(struct bttv *btv)
881 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
882 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
884 if (btv->input == btv->dig) {
885 dprintk("%d: load digital timing table (table_idx=%d)\n",
886 btv->c.nr,table_idx);
888 /* timing change...reset timing generator address */
889 btwrite(0x00, BT848_TGCTRL);
890 btwrite(0x02, BT848_TGCTRL);
891 btwrite(0x00, BT848_TGCTRL);
893 len=SRAM_Table[table_idx][0];
894 for(i = 1; i <= len; i++)
895 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
896 btv->pll.pll_ofreq = 27000000;
899 btwrite(0x11, BT848_TGCTRL);
900 btwrite(0x41, BT848_DVSIF);
902 btv->pll.pll_ofreq = fsc;
904 btwrite(0x0, BT848_DVSIF);
908 /* ----------------------------------------------------------------------- */
910 static void bt848_bright(struct bttv *btv, int bright)
914 // printk("set bright: %d\n", bright); // DEBUG
915 btv->bright = bright;
917 /* We want -128 to 127 we get 0-65535 */
918 value = (bright >> 8) - 128;
919 btwrite(value & 0xff, BT848_BRIGHT);
922 static void bt848_hue(struct bttv *btv, int hue)
929 value = (hue >> 8) - 128;
930 btwrite(value & 0xff, BT848_HUE);
933 static void bt848_contrast(struct bttv *btv, int cont)
937 btv->contrast = cont;
941 hibit = (value >> 6) & 4;
942 btwrite(value & 0xff, BT848_CONTRAST_LO);
943 btaor(hibit, ~4, BT848_E_CONTROL);
944 btaor(hibit, ~4, BT848_O_CONTROL);
947 static void bt848_sat(struct bttv *btv, int color)
949 int val_u,val_v,hibits;
951 btv->saturation = color;
953 /* 0-511 for the color */
954 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
955 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
956 hibits = (val_u >> 7) & 2;
957 hibits |= (val_v >> 8) & 1;
958 btwrite(val_u & 0xff, BT848_SAT_U_LO);
959 btwrite(val_v & 0xff, BT848_SAT_V_LO);
960 btaor(hibits, ~3, BT848_E_CONTROL);
961 btaor(hibits, ~3, BT848_O_CONTROL);
964 /* ----------------------------------------------------------------------- */
967 video_mux(struct bttv *btv, unsigned int input)
971 if (input >= bttv_tvcards[btv->c.type].video_inputs)
974 /* needed by RemoteVideo MX */
975 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
977 gpio_inout(mask2,mask2);
979 if (input == btv->svhs) {
980 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
981 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
983 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
984 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
986 mux = bttv_muxsel(btv, input);
987 btaor(mux<<5, ~(3<<5), BT848_IFORM);
988 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
990 /* card specific hook */
991 if(bttv_tvcards[btv->c.type].muxsel_hook)
992 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
996 static char *audio_modes[] = {
997 "audio: tuner", "audio: radio", "audio: extern",
998 "audio: intern", "audio: mute"
1002 audio_mux_gpio(struct bttv *btv, int input, int mute)
1004 int gpio_val, signal, mute_gpio;
1006 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1007 bttv_tvcards[btv->c.type].gpiomask);
1008 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1011 mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
1012 && !btv->has_radio_tuner);
1015 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1017 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1019 switch (btv->c.type) {
1020 case BTTV_BOARD_VOODOOTV_FM:
1021 case BTTV_BOARD_VOODOOTV_200:
1022 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1026 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1030 bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
1034 audio_mute(struct bttv *btv, int mute)
1036 struct v4l2_ctrl *ctrl;
1038 audio_mux_gpio(btv, btv->audio_input, mute);
1040 if (btv->sd_msp34xx) {
1041 ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1043 v4l2_ctrl_s_ctrl(ctrl, mute);
1045 if (btv->sd_tvaudio) {
1046 ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1048 v4l2_ctrl_s_ctrl(ctrl, mute);
1050 if (btv->sd_tda7432) {
1051 ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1053 v4l2_ctrl_s_ctrl(ctrl, mute);
1059 audio_input(struct bttv *btv, int input)
1061 audio_mux_gpio(btv, input, btv->mute);
1063 if (btv->sd_msp34xx) {
1066 /* Note: the inputs tuner/radio/extern/intern are translated
1067 to msp routings. This assumes common behavior for all msp3400
1068 based TV cards. When this assumption fails, then the
1069 specific MSP routing must be added to the card table.
1070 For now this is sufficient. */
1072 case TVAUDIO_INPUT_RADIO:
1073 /* Some boards need the msp do to the radio demod */
1074 if (btv->radio_uses_msp_demodulator) {
1075 in = MSP_INPUT_DEFAULT;
1078 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1079 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1081 case TVAUDIO_INPUT_EXTERN:
1082 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1083 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1085 case TVAUDIO_INPUT_INTERN:
1086 /* Yes, this is the same input as for RADIO. I doubt
1087 if this is ever used. The only board with an INTERN
1088 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1089 that was tested. My guess is that the whole INTERN
1090 input does not work. */
1091 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1092 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1094 case TVAUDIO_INPUT_TUNER:
1096 /* This is the only card that uses TUNER2, and afaik,
1097 is the only difference between the VOODOOTV_FM
1099 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1100 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1101 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1103 in = MSP_INPUT_DEFAULT;
1106 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1107 in, MSP_OUTPUT_DEFAULT, 0);
1109 if (btv->sd_tvaudio) {
1110 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1117 bttv_crop_calc_limits(struct bttv_crop *c)
1119 /* Scale factor min. 1:1, max. 16:1. Min. image size
1120 48 x 32. Scaled width must be a multiple of 4. */
1123 /* For bug compatibility with VIDIOCGCAP and image
1124 size checks in earlier driver versions. */
1125 c->min_scaled_width = 48;
1126 c->min_scaled_height = 32;
1128 c->min_scaled_width =
1129 (max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
1130 c->min_scaled_height =
1131 max_t(unsigned int, 32, c->rect.height >> 4);
1134 c->max_scaled_width = c->rect.width & ~3;
1135 c->max_scaled_height = c->rect.height;
1139 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1141 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1142 bttv_crop_calc_limits(c);
1145 /* Call with btv->lock down. */
1147 set_tvnorm(struct bttv *btv, unsigned int norm)
1149 const struct bttv_tvnorm *tvnorm;
1152 BUG_ON(norm >= BTTV_TVNORMS);
1153 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
1155 tvnorm = &bttv_tvnorms[norm];
1157 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1158 sizeof (tvnorm->cropcap))) {
1159 bttv_crop_reset(&btv->crop[0], norm);
1160 btv->crop[1] = btv->crop[0]; /* current = default */
1162 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1163 btv->crop_start = tvnorm->cropcap.bounds.top
1164 + tvnorm->cropcap.bounds.height;
1170 btwrite(tvnorm->adelay, BT848_ADELAY);
1171 btwrite(tvnorm->bdelay, BT848_BDELAY);
1172 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1174 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1175 btwrite(1, BT848_VBI_PACK_DEL);
1176 bt848A_set_timing(btv);
1178 switch (btv->c.type) {
1179 case BTTV_BOARD_VOODOOTV_FM:
1180 case BTTV_BOARD_VOODOOTV_200:
1181 bttv_tda9880_setnorm(btv, gpio_read());
1184 id = tvnorm->v4l2_id;
1185 bttv_call_all(btv, video, s_std, id);
1190 /* Call with btv->lock down. */
1192 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1194 unsigned long flags;
1198 spin_lock_irqsave(&btv->s_lock,flags);
1199 if (btv->curr.frame_irq) {
1200 /* active capture -> delayed input switch */
1201 btv->new_input = input;
1203 video_mux(btv,input);
1205 spin_unlock_irqrestore(&btv->s_lock,flags);
1207 video_mux(btv,input);
1209 btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1210 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
1211 audio_input(btv, btv->audio_input);
1212 set_tvnorm(btv, norm);
1215 static void init_irqreg(struct bttv *btv)
1218 btwrite(0xfffffUL, BT848_INT_STAT);
1220 if (bttv_tvcards[btv->c.type].no_video) {
1222 btwrite(BT848_INT_I2CDONE,
1226 btwrite((btv->triton1) |
1227 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1229 (fdsr ? BT848_INT_FDSR : 0) |
1230 BT848_INT_RISCI | BT848_INT_OCERR |
1231 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1237 static void init_bt848(struct bttv *btv)
1239 if (bttv_tvcards[btv->c.type].no_video) {
1240 /* very basic init only */
1245 btwrite(0x00, BT848_CAP_CTL);
1246 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1247 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1249 /* set planar and packed mode trigger points and */
1250 /* set rising edge of inverted GPINTR pin as irq trigger */
1251 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1252 BT848_GPIO_DMA_CTL_PLTP1_16|
1253 BT848_GPIO_DMA_CTL_PLTP23_16|
1254 BT848_GPIO_DMA_CTL_GPINTC|
1255 BT848_GPIO_DMA_CTL_GPINTI,
1256 BT848_GPIO_DMA_CTL);
1258 btwrite(0x20, BT848_E_VSCALE_HI);
1259 btwrite(0x20, BT848_O_VSCALE_HI);
1261 v4l2_ctrl_handler_setup(&btv->ctrl_handler);
1267 static void bttv_reinit_bt848(struct bttv *btv)
1269 unsigned long flags;
1272 pr_info("%d: reset, reinitialize\n", btv->c.nr);
1273 spin_lock_irqsave(&btv->s_lock,flags);
1275 bttv_set_dma(btv,0);
1276 spin_unlock_irqrestore(&btv->s_lock,flags);
1279 btv->pll.pll_current = -1;
1280 set_input(btv, btv->input, btv->tvnorm);
1283 static int bttv_s_ctrl(struct v4l2_ctrl *c)
1285 struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
1289 case V4L2_CID_BRIGHTNESS:
1290 bt848_bright(btv, c->val);
1293 bt848_hue(btv, c->val);
1295 case V4L2_CID_CONTRAST:
1296 bt848_contrast(btv, c->val);
1298 case V4L2_CID_SATURATION:
1299 bt848_sat(btv, c->val);
1301 case V4L2_CID_COLOR_KILLER:
1303 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1304 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1306 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1307 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1310 case V4L2_CID_AUDIO_MUTE:
1311 audio_mute(btv, c->val);
1314 case V4L2_CID_AUDIO_VOLUME:
1315 btv->volume_gpio(btv, c->val);
1318 case V4L2_CID_CHROMA_AGC:
1319 val = c->val ? BT848_SCLOOP_CAGC : 0;
1320 btwrite(val, BT848_E_SCLOOP);
1321 btwrite(val, BT848_O_SCLOOP);
1323 case V4L2_CID_PRIVATE_COMBFILTER:
1324 btv->opt_combfilter = c->val;
1326 case V4L2_CID_PRIVATE_LUMAFILTER:
1328 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1329 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1331 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1332 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1335 case V4L2_CID_PRIVATE_AUTOMUTE:
1336 btv->opt_automute = c->val;
1338 case V4L2_CID_PRIVATE_AGC_CRUSH:
1339 btwrite(BT848_ADC_RESERVED |
1340 (c->val ? BT848_ADC_CRUSH : 0),
1343 case V4L2_CID_PRIVATE_VCR_HACK:
1344 btv->opt_vcr_hack = c->val;
1346 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1347 btwrite(c->val, BT848_WC_UP);
1349 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1350 btwrite(c->val, BT848_WC_DOWN);
1352 case V4L2_CID_PRIVATE_UV_RATIO:
1353 btv->opt_uv_ratio = c->val;
1354 bt848_sat(btv, btv->saturation);
1356 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1357 btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
1359 case V4L2_CID_PRIVATE_CORING:
1360 btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
1368 /* ----------------------------------------------------------------------- */
1370 static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
1371 .s_ctrl = bttv_s_ctrl,
1374 static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
1375 .ops = &bttv_ctrl_ops,
1376 .id = V4L2_CID_PRIVATE_COMBFILTER,
1377 .name = "Comb Filter",
1378 .type = V4L2_CTRL_TYPE_BOOLEAN,
1385 static struct v4l2_ctrl_config bttv_ctrl_automute = {
1386 .ops = &bttv_ctrl_ops,
1387 .id = V4L2_CID_PRIVATE_AUTOMUTE,
1388 .name = "Auto Mute",
1389 .type = V4L2_CTRL_TYPE_BOOLEAN,
1396 static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
1397 .ops = &bttv_ctrl_ops,
1398 .id = V4L2_CID_PRIVATE_LUMAFILTER,
1399 .name = "Luma Decimation Filter",
1400 .type = V4L2_CTRL_TYPE_BOOLEAN,
1407 static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
1408 .ops = &bttv_ctrl_ops,
1409 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
1410 .name = "AGC Crush",
1411 .type = V4L2_CTRL_TYPE_BOOLEAN,
1418 static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
1419 .ops = &bttv_ctrl_ops,
1420 .id = V4L2_CID_PRIVATE_VCR_HACK,
1422 .type = V4L2_CTRL_TYPE_BOOLEAN,
1429 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
1430 .ops = &bttv_ctrl_ops,
1431 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
1432 .name = "Whitecrush Lower",
1433 .type = V4L2_CTRL_TYPE_INTEGER,
1440 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
1441 .ops = &bttv_ctrl_ops,
1442 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
1443 .name = "Whitecrush Upper",
1444 .type = V4L2_CTRL_TYPE_INTEGER,
1451 static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
1452 .ops = &bttv_ctrl_ops,
1453 .id = V4L2_CID_PRIVATE_UV_RATIO,
1455 .type = V4L2_CTRL_TYPE_INTEGER,
1462 static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
1463 .ops = &bttv_ctrl_ops,
1464 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
1465 .name = "Full Luma Range",
1466 .type = V4L2_CTRL_TYPE_BOOLEAN,
1472 static struct v4l2_ctrl_config bttv_ctrl_coring = {
1473 .ops = &bttv_ctrl_ops,
1474 .id = V4L2_CID_PRIVATE_CORING,
1476 .type = V4L2_CTRL_TYPE_INTEGER,
1483 /* ----------------------------------------------------------------------- */
1485 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1487 unsigned int outbits, data;
1488 outbits = btread(BT848_GPIO_OUT_EN);
1489 data = btread(BT848_GPIO_DATA);
1490 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1491 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
1494 static void bttv_field_count(struct bttv *btv)
1502 /* start field counter */
1503 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1505 /* stop field counter */
1506 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1507 btv->field_count = 0;
1511 static const struct bttv_format*
1512 format_by_fourcc(int fourcc)
1516 for (i = 0; i < FORMATS; i++) {
1517 if (-1 == formats[i].fourcc)
1519 if (formats[i].fourcc == fourcc)
1525 /* ----------------------------------------------------------------------- */
1529 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1530 struct bttv_buffer *new)
1532 struct bttv_buffer *old;
1533 unsigned long flags;
1535 dprintk("switch_overlay: enter [new=%p]\n", new);
1537 new->vb.state = VIDEOBUF_DONE;
1538 spin_lock_irqsave(&btv->s_lock,flags);
1542 bttv_set_dma(btv, 0x03);
1543 spin_unlock_irqrestore(&btv->s_lock,flags);
1545 dprintk("switch_overlay: old=%p state is %d\n",
1546 old, old->vb.state);
1547 bttv_dma_free(&fh->cap,btv, old);
1551 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
1552 dprintk("switch_overlay: done\n");
1556 /* ----------------------------------------------------------------------- */
1557 /* video4linux (1) interface */
1559 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1560 struct bttv_buffer *buf,
1561 const struct bttv_format *fmt,
1562 unsigned int width, unsigned int height,
1563 enum v4l2_field field)
1565 struct bttv_fh *fh = q->priv_data;
1566 int redo_dma_risc = 0;
1571 /* check settings */
1574 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1576 height = RAW_LINES*2;
1577 if (width*height > buf->vb.bsize)
1579 buf->vb.size = buf->vb.bsize;
1581 /* Make sure tvnorm and vbi_end remain consistent
1582 until we're done. */
1586 /* In this mode capturing always starts at defrect.top
1587 (default VDELAY), ignoring cropping parameters. */
1588 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1592 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1595 c = btv->crop[!!fh->do_crop];
1597 if (width < c.min_scaled_width ||
1598 width > c.max_scaled_width ||
1599 height < c.min_scaled_height)
1603 case V4L2_FIELD_TOP:
1604 case V4L2_FIELD_BOTTOM:
1605 case V4L2_FIELD_ALTERNATE:
1606 /* btv->crop counts frame lines. Max. scale
1607 factor is 16:1 for frames, 8:1 for fields. */
1608 if (height * 2 > c.max_scaled_height)
1613 if (height > c.max_scaled_height)
1618 buf->vb.size = (width * height * fmt->depth) >> 3;
1619 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1623 /* alloc + fill struct bttv_buffer (if changed) */
1624 if (buf->vb.width != width || buf->vb.height != height ||
1625 buf->vb.field != field ||
1626 buf->tvnorm != norm || buf->fmt != fmt ||
1627 buf->crop.top != c.rect.top ||
1628 buf->crop.left != c.rect.left ||
1629 buf->crop.width != c.rect.width ||
1630 buf->crop.height != c.rect.height) {
1631 buf->vb.width = width;
1632 buf->vb.height = height;
1633 buf->vb.field = field;
1640 /* alloc risc memory */
1641 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1643 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1648 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1651 buf->vb.state = VIDEOBUF_PREPARED;
1655 bttv_dma_free(q,btv,buf);
1660 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1662 struct bttv_fh *fh = q->priv_data;
1664 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1667 if (*size * *count > gbuffers * gbufsize)
1668 *count = (gbuffers * gbufsize) / *size;
1673 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1674 enum v4l2_field field)
1676 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1677 struct bttv_fh *fh = q->priv_data;
1679 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1680 fh->width, fh->height, field);
1684 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1686 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1687 struct bttv_fh *fh = q->priv_data;
1688 struct bttv *btv = fh->btv;
1690 buf->vb.state = VIDEOBUF_QUEUED;
1691 list_add_tail(&buf->vb.queue,&btv->capture);
1692 if (!btv->curr.frame_irq) {
1694 bttv_set_dma(btv, 0x03);
1698 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1700 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1701 struct bttv_fh *fh = q->priv_data;
1703 bttv_dma_free(q,fh->btv,buf);
1706 static struct videobuf_queue_ops bttv_video_qops = {
1707 .buf_setup = buffer_setup,
1708 .buf_prepare = buffer_prepare,
1709 .buf_queue = buffer_queue,
1710 .buf_release = buffer_release,
1713 static void radio_enable(struct bttv *btv)
1715 /* Switch to the radio tuner */
1716 if (!btv->has_radio_tuner) {
1717 btv->has_radio_tuner = 1;
1718 bttv_call_all(btv, tuner, s_radio);
1719 btv->audio_input = TVAUDIO_INPUT_RADIO;
1720 audio_input(btv, btv->audio_input);
1724 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
1726 struct bttv_fh *fh = priv;
1727 struct bttv *btv = fh->btv;
1730 for (i = 0; i < BTTV_TVNORMS; i++)
1731 if (id & bttv_tvnorms[i].v4l2_id)
1733 if (i == BTTV_TVNORMS)
1740 static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
1742 struct bttv_fh *fh = priv;
1743 struct bttv *btv = fh->btv;
1749 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1751 struct bttv_fh *fh = f;
1752 struct bttv *btv = fh->btv;
1754 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1755 *id &= V4L2_STD_625_50;
1757 *id &= V4L2_STD_525_60;
1761 static int bttv_enum_input(struct file *file, void *priv,
1762 struct v4l2_input *i)
1764 struct bttv_fh *fh = priv;
1765 struct bttv *btv = fh->btv;
1767 if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
1770 i->type = V4L2_INPUT_TYPE_CAMERA;
1773 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1774 sprintf(i->name, "Television");
1775 i->type = V4L2_INPUT_TYPE_TUNER;
1777 } else if (i->index == btv->svhs) {
1778 sprintf(i->name, "S-Video");
1780 sprintf(i->name, "Composite%d", i->index);
1783 if (i->index == btv->input) {
1784 __u32 dstatus = btread(BT848_DSTATUS);
1785 if (0 == (dstatus & BT848_DSTATUS_PRES))
1786 i->status |= V4L2_IN_ST_NO_SIGNAL;
1787 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1788 i->status |= V4L2_IN_ST_NO_H_LOCK;
1791 i->std = BTTV_NORMS;
1795 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1797 struct bttv_fh *fh = priv;
1798 struct bttv *btv = fh->btv;
1805 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1807 struct bttv_fh *fh = priv;
1808 struct bttv *btv = fh->btv;
1810 if (i >= bttv_tvcards[btv->c.type].video_inputs)
1813 set_input(btv, i, btv->tvnorm);
1817 static int bttv_s_tuner(struct file *file, void *priv,
1818 const struct v4l2_tuner *t)
1820 struct bttv_fh *fh = priv;
1821 struct bttv *btv = fh->btv;
1826 bttv_call_all(btv, tuner, s_tuner, t);
1828 if (btv->audio_mode_gpio) {
1829 struct v4l2_tuner copy = *t;
1831 btv->audio_mode_gpio(btv, ©, 1);
1836 static int bttv_g_frequency(struct file *file, void *priv,
1837 struct v4l2_frequency *f)
1839 struct bttv_fh *fh = priv;
1840 struct bttv *btv = fh->btv;
1845 if (f->type == V4L2_TUNER_RADIO)
1847 f->frequency = f->type == V4L2_TUNER_RADIO ?
1848 btv->radio_freq : btv->tv_freq;
1853 static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
1855 struct v4l2_frequency new_freq = *f;
1857 bttv_call_all(btv, tuner, s_frequency, f);
1858 /* s_frequency may clamp the frequency, so get the actual
1859 frequency before assigning radio/tv_freq. */
1860 bttv_call_all(btv, tuner, g_frequency, &new_freq);
1861 if (new_freq.type == V4L2_TUNER_RADIO) {
1863 btv->radio_freq = new_freq.frequency;
1864 if (btv->has_tea575x) {
1865 btv->tea.freq = btv->radio_freq;
1866 snd_tea575x_set_freq(&btv->tea);
1869 btv->tv_freq = new_freq.frequency;
1873 static int bttv_s_frequency(struct file *file, void *priv,
1874 const struct v4l2_frequency *f)
1876 struct bttv_fh *fh = priv;
1877 struct bttv *btv = fh->btv;
1882 bttv_set_frequency(btv, f);
1886 static int bttv_log_status(struct file *file, void *f)
1888 struct video_device *vdev = video_devdata(file);
1889 struct bttv_fh *fh = f;
1890 struct bttv *btv = fh->btv;
1892 v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
1893 bttv_call_all(btv, core, log_status);
1897 #ifdef CONFIG_VIDEO_ADV_DEBUG
1898 static int bttv_g_register(struct file *file, void *f,
1899 struct v4l2_dbg_register *reg)
1901 struct bttv_fh *fh = f;
1902 struct bttv *btv = fh->btv;
1904 /* bt848 has a 12-bit register space */
1906 reg->val = btread(reg->reg);
1912 static int bttv_s_register(struct file *file, void *f,
1913 const struct v4l2_dbg_register *reg)
1915 struct bttv_fh *fh = f;
1916 struct bttv *btv = fh->btv;
1918 /* bt848 has a 12-bit register space */
1919 btwrite(reg->val, reg->reg & 0xfff);
1925 /* Given cropping boundaries b and the scaled width and height of a
1926 single field or frame, which must not exceed hardware limits, this
1927 function adjusts the cropping parameters c. */
1929 bttv_crop_adjust (struct bttv_crop * c,
1930 const struct v4l2_rect * b,
1933 enum v4l2_field field)
1935 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
1939 if (width < c->min_scaled_width) {
1940 /* Max. hor. scale factor 16:1. */
1941 c->rect.width = width * 16;
1942 } else if (width > c->max_scaled_width) {
1943 /* Min. hor. scale factor 1:1. */
1944 c->rect.width = width;
1946 max_left = b->left + b->width - width;
1947 max_left = min(max_left, (__s32) MAX_HDELAY);
1948 if (c->rect.left > max_left)
1949 c->rect.left = max_left;
1952 if (height < c->min_scaled_height) {
1953 /* Max. vert. scale factor 16:1, single fields 8:1. */
1954 c->rect.height = height * 16;
1955 } else if (frame_height > c->max_scaled_height) {
1956 /* Min. vert. scale factor 1:1.
1957 Top and height count field lines times two. */
1958 c->rect.height = (frame_height + 1) & ~1;
1960 max_top = b->top + b->height - c->rect.height;
1961 if (c->rect.top > max_top)
1962 c->rect.top = max_top;
1965 bttv_crop_calc_limits(c);
1968 /* Returns an error if scaling to a frame or single field with the given
1969 width and height is not possible with the current cropping parameters
1970 and width aligned according to width_mask. If adjust_size is TRUE the
1971 function may adjust the width and/or height instead, rounding width
1972 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
1973 also adjust the current cropping parameters to get closer to the
1974 desired image size. */
1976 limit_scaled_size_lock (struct bttv_fh * fh,
1979 enum v4l2_field field,
1980 unsigned int width_mask,
1981 unsigned int width_bias,
1985 struct bttv *btv = fh->btv;
1986 const struct v4l2_rect *b;
1987 struct bttv_crop *c;
1994 BUG_ON((int) width_mask >= 0 ||
1995 width_bias >= (unsigned int) -width_mask);
1997 /* Make sure tvnorm, vbi_end and the current cropping parameters
1998 remain consistent until we're done. */
2000 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2002 /* Do crop - use current, don't - use default parameters. */
2003 c = &btv->crop[!!fh->do_crop];
2008 && !locked_btres(btv, VIDEO_RESOURCES)) {
2012 /* We cannot scale up. When the scaled image is larger
2013 than crop.rect we adjust the crop.rect as required
2014 by the V4L2 spec, hence cropcap.bounds are our limit. */
2015 max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
2016 max_height = b->height;
2018 /* We cannot capture the same line as video and VBI data.
2019 Note btv->vbi_end is really a minimum, see
2020 bttv_vbi_try_fmt(). */
2021 if (btv->vbi_end > b->top) {
2022 max_height -= btv->vbi_end - b->top;
2024 if (min_height > max_height)
2029 if (btv->vbi_end > c->rect.top)
2032 min_width = c->min_scaled_width;
2033 min_height = c->min_scaled_height;
2034 max_width = c->max_scaled_width;
2035 max_height = c->max_scaled_height;
2040 min_width = (min_width - width_mask - 1) & width_mask;
2041 max_width = max_width & width_mask;
2043 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2044 min_height = min_height;
2045 /* Min. scale factor is 1:1. */
2046 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2049 *width = clamp(*width, min_width, max_width);
2050 *height = clamp(*height, min_height, max_height);
2052 /* Round after clamping to avoid overflow. */
2053 *width = (*width + width_bias) & width_mask;
2056 bttv_crop_adjust(c, b, *width, *height, field);
2058 if (btv->vbi_end > c->rect.top) {
2059 /* Move the crop window out of the way. */
2060 c->rect.top = btv->vbi_end;
2065 if (*width < min_width ||
2066 *height < min_height ||
2067 *width > max_width ||
2068 *height > max_height ||
2069 0 != (*width & ~width_mask))
2073 rc = 0; /* success */
2080 /* Returns an error if the given overlay window dimensions are not
2081 possible with the current cropping parameters. If adjust_size is
2082 TRUE the function may adjust the window width and/or height
2083 instead, however it always rounds the horizontal position and
2084 width as btcx_align() does. If adjust_crop is TRUE the function
2085 may also adjust the current cropping parameters to get closer
2086 to the desired window size. */
2088 verify_window_lock(struct bttv_fh *fh, struct v4l2_window *win,
2089 int adjust_size, int adjust_crop)
2091 enum v4l2_field field;
2092 unsigned int width_mask;
2095 if (win->w.width < 48)
2097 if (win->w.height < 32)
2099 if (win->clipcount > 2048)
2100 win->clipcount = 2048;
2103 win->global_alpha = 0;
2107 case V4L2_FIELD_TOP:
2108 case V4L2_FIELD_BOTTOM:
2109 case V4L2_FIELD_INTERLACED:
2112 field = V4L2_FIELD_ANY;
2115 if (V4L2_FIELD_ANY == field) {
2118 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2119 field = (win->w.height > height2)
2120 ? V4L2_FIELD_INTERLACED
2125 if (NULL == fh->ovfmt)
2127 /* 4-byte alignment. */
2129 switch (fh->ovfmt->depth) {
2143 win->w.width -= win->w.left & ~width_mask;
2144 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2146 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
2148 /* width_bias: round down */ 0,
2149 adjust_size, adjust_crop);
2155 static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
2156 struct v4l2_window *win, int fixup)
2158 struct v4l2_clip *clips = NULL;
2159 int n,size,retval = 0;
2161 if (NULL == fh->ovfmt)
2163 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2165 retval = verify_window_lock(fh, win,
2166 /* adjust_size */ fixup,
2167 /* adjust_crop */ fixup);
2171 /* copy clips -- luckily v4l1 + v4l2 are binary
2172 compatible here ...*/
2174 size = sizeof(*clips)*(n+4);
2175 clips = kmalloc(size,GFP_KERNEL);
2179 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2185 /* clip against screen */
2186 if (NULL != btv->fbuf.base)
2187 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2189 btcx_sort_clips(clips,n);
2191 /* 4-byte alignments */
2192 switch (fh->ovfmt->depth) {
2195 btcx_align(&win->w, clips, n, 3);
2198 btcx_align(&win->w, clips, n, 1);
2201 /* no alignment fixups needed */
2207 kfree(fh->ov.clips);
2208 fh->ov.clips = clips;
2212 fh->ov.field = win->field;
2213 fh->ov.setup_ok = 1;
2215 btv->init.ov.w.width = win->w.width;
2216 btv->init.ov.w.height = win->w.height;
2217 btv->init.ov.field = win->field;
2219 /* update overlay if needed */
2221 if (check_btres(fh, RESOURCE_OVERLAY)) {
2222 struct bttv_buffer *new;
2224 new = videobuf_sg_alloc(sizeof(*new));
2225 new->crop = btv->crop[!!fh->do_crop].rect;
2226 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2227 retval = bttv_switch_overlay(btv,fh,new);
2232 /* ----------------------------------------------------------------------- */
2234 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2236 struct videobuf_queue* q = NULL;
2239 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2242 case V4L2_BUF_TYPE_VBI_CAPTURE:
2251 static int bttv_resource(struct bttv_fh *fh)
2256 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2257 res = RESOURCE_VIDEO_STREAM;
2259 case V4L2_BUF_TYPE_VBI_CAPTURE:
2268 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2270 struct videobuf_queue *q = bttv_queue(fh);
2271 int res = bttv_resource(fh);
2273 if (check_btres(fh,res))
2275 if (videobuf_queue_is_busy(q))
2282 pix_format_set_size (struct v4l2_pix_format * f,
2283 const struct bttv_format * fmt,
2285 unsigned int height)
2290 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2291 f->bytesperline = width; /* Y plane */
2292 f->sizeimage = (width * height * fmt->depth) >> 3;
2294 f->bytesperline = (width * fmt->depth) >> 3;
2295 f->sizeimage = height * f->bytesperline;
2299 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2300 struct v4l2_format *f)
2302 struct bttv_fh *fh = priv;
2304 pix_format_set_size(&f->fmt.pix, fh->fmt,
2305 fh->width, fh->height);
2306 f->fmt.pix.field = fh->cap.field;
2307 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2308 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2313 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2314 struct v4l2_format *f)
2316 struct bttv_fh *fh = priv;
2318 f->fmt.win.w = fh->ov.w;
2319 f->fmt.win.field = fh->ov.field;
2324 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2325 struct v4l2_format *f)
2327 const struct bttv_format *fmt;
2328 struct bttv_fh *fh = priv;
2329 struct bttv *btv = fh->btv;
2330 enum v4l2_field field;
2331 __s32 width, height;
2335 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2339 field = f->fmt.pix.field;
2342 case V4L2_FIELD_TOP:
2343 case V4L2_FIELD_BOTTOM:
2344 case V4L2_FIELD_ALTERNATE:
2345 case V4L2_FIELD_INTERLACED:
2347 case V4L2_FIELD_SEQ_BT:
2348 case V4L2_FIELD_SEQ_TB:
2349 if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
2350 field = V4L2_FIELD_SEQ_TB;
2354 default: /* FIELD_ANY case */
2355 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2356 field = (f->fmt.pix.height > height2)
2357 ? V4L2_FIELD_INTERLACED
2358 : V4L2_FIELD_BOTTOM;
2362 width = f->fmt.pix.width;
2363 height = f->fmt.pix.height;
2365 rc = limit_scaled_size_lock(fh, &width, &height, field,
2366 /* width_mask: 4 pixels */ ~3,
2367 /* width_bias: nearest */ 2,
2368 /* adjust_size */ 1,
2369 /* adjust_crop */ 0);
2373 /* update data for the application */
2374 f->fmt.pix.field = field;
2375 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2376 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2381 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2382 struct v4l2_format *f)
2384 struct bttv_fh *fh = priv;
2386 verify_window_lock(fh, &f->fmt.win,
2387 /* adjust_size */ 1,
2388 /* adjust_crop */ 0);
2392 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2393 struct v4l2_format *f)
2396 const struct bttv_format *fmt;
2397 struct bttv_fh *fh = priv;
2398 struct bttv *btv = fh->btv;
2399 __s32 width, height;
2400 enum v4l2_field field;
2402 retval = bttv_switch_type(fh, f->type);
2406 retval = bttv_try_fmt_vid_cap(file, priv, f);
2410 width = f->fmt.pix.width;
2411 height = f->fmt.pix.height;
2412 field = f->fmt.pix.field;
2414 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
2415 /* width_mask: 4 pixels */ ~3,
2416 /* width_bias: nearest */ 2,
2417 /* adjust_size */ 1,
2418 /* adjust_crop */ 1);
2422 f->fmt.pix.field = field;
2424 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2426 /* update our state informations */
2428 fh->cap.field = f->fmt.pix.field;
2429 fh->cap.last = V4L2_FIELD_NONE;
2430 fh->width = f->fmt.pix.width;
2431 fh->height = f->fmt.pix.height;
2432 btv->init.fmt = fmt;
2433 btv->init.width = f->fmt.pix.width;
2434 btv->init.height = f->fmt.pix.height;
2439 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2440 struct v4l2_format *f)
2442 struct bttv_fh *fh = priv;
2443 struct bttv *btv = fh->btv;
2445 if (no_overlay > 0) {
2446 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2450 return setup_window_lock(fh, btv, &f->fmt.win, 1);
2453 static int bttv_querycap(struct file *file, void *priv,
2454 struct v4l2_capability *cap)
2456 struct video_device *vdev = video_devdata(file);
2457 struct bttv_fh *fh = priv;
2458 struct bttv *btv = fh->btv;
2463 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2464 strlcpy(cap->card, btv->video_dev.name, sizeof(cap->card));
2465 snprintf(cap->bus_info, sizeof(cap->bus_info),
2466 "PCI:%s", pci_name(btv->c.pci));
2468 V4L2_CAP_VIDEO_CAPTURE |
2469 V4L2_CAP_READWRITE |
2470 V4L2_CAP_STREAMING |
2471 V4L2_CAP_DEVICE_CAPS;
2472 if (no_overlay <= 0)
2473 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2474 if (video_is_registered(&btv->vbi_dev))
2475 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2476 if (video_is_registered(&btv->radio_dev))
2477 cap->capabilities |= V4L2_CAP_RADIO;
2480 * No need to lock here: those vars are initialized during board
2481 * probe and remains untouched during the rest of the driver lifecycle
2483 if (btv->has_saa6588)
2484 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2485 if (btv->tuner_type != TUNER_ABSENT)
2486 cap->capabilities |= V4L2_CAP_TUNER;
2487 if (vdev->vfl_type == VFL_TYPE_GRABBER)
2488 cap->device_caps = cap->capabilities &
2489 (V4L2_CAP_VIDEO_CAPTURE |
2490 V4L2_CAP_READWRITE |
2491 V4L2_CAP_STREAMING |
2492 V4L2_CAP_VIDEO_OVERLAY |
2494 else if (vdev->vfl_type == VFL_TYPE_VBI)
2495 cap->device_caps = cap->capabilities &
2496 (V4L2_CAP_VBI_CAPTURE |
2497 V4L2_CAP_READWRITE |
2498 V4L2_CAP_STREAMING |
2501 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
2502 if (btv->has_saa6588)
2503 cap->device_caps |= V4L2_CAP_READWRITE |
2504 V4L2_CAP_RDS_CAPTURE;
2505 if (btv->has_tea575x)
2506 cap->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
2511 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2515 for (i = 0; i < FORMATS; i++) {
2516 if (formats[i].fourcc != -1)
2518 if ((unsigned int)index == f->index)
2524 f->pixelformat = formats[i].fourcc;
2525 strlcpy(f->description, formats[i].name, sizeof(f->description));
2530 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2531 struct v4l2_fmtdesc *f)
2533 int rc = bttv_enum_fmt_cap_ovr(f);
2541 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
2542 struct v4l2_fmtdesc *f)
2546 if (no_overlay > 0) {
2547 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2551 rc = bttv_enum_fmt_cap_ovr(f);
2556 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
2562 static int bttv_g_fbuf(struct file *file, void *f,
2563 struct v4l2_framebuffer *fb)
2565 struct bttv_fh *fh = f;
2566 struct bttv *btv = fh->btv;
2569 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2570 fb->flags = V4L2_FBUF_FLAG_PRIMARY;
2572 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2576 static int bttv_overlay(struct file *file, void *f, unsigned int on)
2578 struct bttv_fh *fh = f;
2579 struct bttv *btv = fh->btv;
2580 struct bttv_buffer *new;
2585 if (unlikely(!btv->fbuf.base)) {
2588 if (unlikely(!fh->ov.setup_ok)) {
2589 dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
2596 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
2600 fh->ov.tvnorm = btv->tvnorm;
2601 new = videobuf_sg_alloc(sizeof(*new));
2602 new->crop = btv->crop[!!fh->do_crop].rect;
2603 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2609 retval = bttv_switch_overlay(btv, fh, new);
2613 static int bttv_s_fbuf(struct file *file, void *f,
2614 const struct v4l2_framebuffer *fb)
2616 struct bttv_fh *fh = f;
2617 struct bttv *btv = fh->btv;
2618 const struct bttv_format *fmt;
2621 if (!capable(CAP_SYS_ADMIN) &&
2622 !capable(CAP_SYS_RAWIO))
2626 fmt = format_by_fourcc(fb->fmt.pixelformat);
2629 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2633 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2634 __s32 width = fb->fmt.width;
2635 __s32 height = fb->fmt.height;
2637 retval = limit_scaled_size_lock(fh, &width, &height,
2638 V4L2_FIELD_INTERLACED,
2639 /* width_mask */ ~3,
2641 /* adjust_size */ 0,
2642 /* adjust_crop */ 0);
2648 btv->fbuf.base = fb->base;
2649 btv->fbuf.fmt.width = fb->fmt.width;
2650 btv->fbuf.fmt.height = fb->fmt.height;
2651 if (0 != fb->fmt.bytesperline)
2652 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2654 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2658 btv->init.ovfmt = fmt;
2659 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2662 fh->ov.w.width = fb->fmt.width;
2663 fh->ov.w.height = fb->fmt.height;
2664 btv->init.ov.w.width = fb->fmt.width;
2665 btv->init.ov.w.height = fb->fmt.height;
2667 kfree(fh->ov.clips);
2668 fh->ov.clips = NULL;
2671 if (check_btres(fh, RESOURCE_OVERLAY)) {
2672 struct bttv_buffer *new;
2674 new = videobuf_sg_alloc(sizeof(*new));
2675 new->crop = btv->crop[!!fh->do_crop].rect;
2676 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2677 retval = bttv_switch_overlay(btv, fh, new);
2683 static int bttv_reqbufs(struct file *file, void *priv,
2684 struct v4l2_requestbuffers *p)
2686 struct bttv_fh *fh = priv;
2687 return videobuf_reqbufs(bttv_queue(fh), p);
2690 static int bttv_querybuf(struct file *file, void *priv,
2691 struct v4l2_buffer *b)
2693 struct bttv_fh *fh = priv;
2694 return videobuf_querybuf(bttv_queue(fh), b);
2697 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2699 struct bttv_fh *fh = priv;
2700 struct bttv *btv = fh->btv;
2701 int res = bttv_resource(fh);
2703 if (!check_alloc_btres_lock(btv, fh, res))
2706 return videobuf_qbuf(bttv_queue(fh), b);
2709 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2711 struct bttv_fh *fh = priv;
2712 return videobuf_dqbuf(bttv_queue(fh), b,
2713 file->f_flags & O_NONBLOCK);
2716 static int bttv_streamon(struct file *file, void *priv,
2717 enum v4l2_buf_type type)
2719 struct bttv_fh *fh = priv;
2720 struct bttv *btv = fh->btv;
2721 int res = bttv_resource(fh);
2723 if (!check_alloc_btres_lock(btv, fh, res))
2725 return videobuf_streamon(bttv_queue(fh));
2729 static int bttv_streamoff(struct file *file, void *priv,
2730 enum v4l2_buf_type type)
2732 struct bttv_fh *fh = priv;
2733 struct bttv *btv = fh->btv;
2735 int res = bttv_resource(fh);
2738 retval = videobuf_streamoff(bttv_queue(fh));
2741 free_btres_lock(btv, fh, res);
2745 static int bttv_g_parm(struct file *file, void *f,
2746 struct v4l2_streamparm *parm)
2748 struct bttv_fh *fh = f;
2749 struct bttv *btv = fh->btv;
2751 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2753 parm->parm.capture.readbuffers = gbuffers;
2754 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2755 &parm->parm.capture.timeperframe);
2760 static int bttv_g_tuner(struct file *file, void *priv,
2761 struct v4l2_tuner *t)
2763 struct bttv_fh *fh = priv;
2764 struct bttv *btv = fh->btv;
2769 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2770 t->capability = V4L2_TUNER_CAP_NORM;
2771 bttv_call_all(btv, tuner, g_tuner, t);
2772 strcpy(t->name, "Television");
2773 t->type = V4L2_TUNER_ANALOG_TV;
2774 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2777 if (btv->audio_mode_gpio)
2778 btv->audio_mode_gpio(btv, t, 0);
2783 static int bttv_cropcap(struct file *file, void *priv,
2784 struct v4l2_cropcap *cap)
2786 struct bttv_fh *fh = priv;
2787 struct bttv *btv = fh->btv;
2789 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2790 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2793 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2798 static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
2800 struct bttv_fh *fh = f;
2801 struct bttv *btv = fh->btv;
2803 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2804 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2807 /* No fh->do_crop = 1; because btv->crop[1] may be
2808 inconsistent with fh->width or fh->height and apps
2809 do not expect a change here. */
2811 crop->c = btv->crop[!!fh->do_crop].rect;
2816 static int bttv_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
2818 struct bttv_fh *fh = f;
2819 struct bttv *btv = fh->btv;
2820 const struct v4l2_rect *b;
2828 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2829 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2832 /* Make sure tvnorm, vbi_end and the current cropping
2833 parameters remain consistent until we're done. Note
2834 read() may change vbi_end in check_alloc_btres_lock(). */
2837 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
2841 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2844 b_right = b_left + b->width;
2845 b_bottom = b->top + b->height;
2847 b_top = max(b->top, btv->vbi_end);
2848 if (b_top + 32 >= b_bottom) {
2852 /* Min. scaled size 48 x 32. */
2853 c.rect.left = clamp_t(s32, crop->c.left, b_left, b_right - 48);
2854 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
2856 c.rect.width = clamp_t(s32, crop->c.width,
2857 48, b_right - c.rect.left);
2859 c.rect.top = clamp_t(s32, crop->c.top, b_top, b_bottom - 32);
2860 /* Top and height must be a multiple of two. */
2861 c.rect.top = (c.rect.top + 1) & ~1;
2863 c.rect.height = clamp_t(s32, crop->c.height,
2864 32, b_bottom - c.rect.top);
2865 c.rect.height = (c.rect.height + 1) & ~1;
2867 bttv_crop_calc_limits(&c);
2873 if (fh->width < c.min_scaled_width) {
2874 fh->width = c.min_scaled_width;
2875 btv->init.width = c.min_scaled_width;
2876 } else if (fh->width > c.max_scaled_width) {
2877 fh->width = c.max_scaled_width;
2878 btv->init.width = c.max_scaled_width;
2881 if (fh->height < c.min_scaled_height) {
2882 fh->height = c.min_scaled_height;
2883 btv->init.height = c.min_scaled_height;
2884 } else if (fh->height > c.max_scaled_height) {
2885 fh->height = c.max_scaled_height;
2886 btv->init.height = c.max_scaled_height;
2892 static ssize_t bttv_read(struct file *file, char __user *data,
2893 size_t count, loff_t *ppos)
2895 struct bttv_fh *fh = file->private_data;
2898 if (fh->btv->errors)
2899 bttv_reinit_bt848(fh->btv);
2900 dprintk("%d: read count=%d type=%s\n",
2901 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
2904 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2905 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
2906 /* VIDEO_READ in use by another fh,
2907 or VIDEO_STREAM by any fh. */
2910 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2911 file->f_flags & O_NONBLOCK);
2912 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
2914 case V4L2_BUF_TYPE_VBI_CAPTURE:
2915 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
2917 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2918 file->f_flags & O_NONBLOCK);
2926 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2928 struct bttv_fh *fh = file->private_data;
2929 struct bttv_buffer *buf;
2930 enum v4l2_field field;
2931 unsigned int rc = 0;
2932 unsigned long req_events = poll_requested_events(wait);
2934 if (v4l2_event_pending(&fh->fh))
2936 else if (req_events & POLLPRI)
2937 poll_wait(file, &fh->fh.wait, wait);
2939 if (!(req_events & (POLLIN | POLLRDNORM)))
2942 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2943 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
2944 return rc | POLLERR;
2945 return rc | videobuf_poll_stream(file, &fh->vbi, wait);
2948 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
2949 /* streaming capture */
2950 if (list_empty(&fh->cap.stream))
2951 return rc | POLLERR;
2952 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2954 /* read() capture */
2955 if (NULL == fh->cap.read_buf) {
2956 /* need to capture a new frame */
2957 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
2958 return rc | POLLERR;
2959 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
2960 if (NULL == fh->cap.read_buf)
2961 return rc | POLLERR;
2962 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2963 field = videobuf_next_field(&fh->cap);
2964 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2965 kfree (fh->cap.read_buf);
2966 fh->cap.read_buf = NULL;
2967 return rc | POLLERR;
2969 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2970 fh->cap.read_off = 0;
2972 buf = (struct bttv_buffer*)fh->cap.read_buf;
2975 poll_wait(file, &buf->vb.done, wait);
2976 if (buf->vb.state == VIDEOBUF_DONE ||
2977 buf->vb.state == VIDEOBUF_ERROR)
2978 rc = rc | POLLIN|POLLRDNORM;
2982 static int bttv_open(struct file *file)
2984 struct video_device *vdev = video_devdata(file);
2985 struct bttv *btv = video_drvdata(file);
2987 enum v4l2_buf_type type = 0;
2989 dprintk("open dev=%s\n", video_device_node_name(vdev));
2991 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
2992 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2993 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
2994 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3000 dprintk("%d: open called (type=%s)\n",
3001 btv->c.nr, v4l2_type_names[type]);
3003 /* allocate per filehandle data */
3004 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3008 file->private_data = fh;
3011 v4l2_fh_init(&fh->fh, vdev);
3014 fh->ov.setup_ok = 0;
3016 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3017 &btv->c.pci->dev, &btv->s_lock,
3018 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3019 V4L2_FIELD_INTERLACED,
3020 sizeof(struct bttv_buffer),
3022 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3023 &btv->c.pci->dev, &btv->s_lock,
3024 V4L2_BUF_TYPE_VBI_CAPTURE,
3026 sizeof(struct bttv_buffer),
3028 set_tvnorm(btv,btv->tvnorm);
3029 set_input(btv, btv->input, btv->tvnorm);
3030 audio_mute(btv, btv->mute);
3032 /* The V4L2 spec requires one global set of cropping parameters
3033 which only change on request. These are stored in btv->crop[1].
3034 However for compatibility with V4L apps and cropping unaware
3035 V4L2 apps we now reset the cropping parameters as seen through
3036 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3037 will use btv->crop[0], the default cropping parameters for the
3038 current video standard, and VIDIOC_S_FMT will not implicitely
3039 change the cropping parameters until VIDIOC_S_CROP has been
3041 fh->do_crop = !reset_crop; /* module parameter */
3043 /* Likewise there should be one global set of VBI capture
3044 parameters, but for compatibility with V4L apps and earlier
3045 driver versions each fh has its own parameters. */
3046 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3048 bttv_field_count(btv);
3049 v4l2_fh_add(&fh->fh);
3053 static int bttv_release(struct file *file)
3055 struct bttv_fh *fh = file->private_data;
3056 struct bttv *btv = fh->btv;
3058 /* turn off overlay */
3059 if (check_btres(fh, RESOURCE_OVERLAY))
3060 bttv_switch_overlay(btv,fh,NULL);
3062 /* stop video capture */
3063 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3064 videobuf_streamoff(&fh->cap);
3065 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
3067 if (fh->cap.read_buf) {
3068 buffer_release(&fh->cap,fh->cap.read_buf);
3069 kfree(fh->cap.read_buf);
3071 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3072 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
3075 /* stop vbi capture */
3076 if (check_btres(fh, RESOURCE_VBI)) {
3077 videobuf_stop(&fh->vbi);
3078 free_btres_lock(btv,fh,RESOURCE_VBI);
3083 videobuf_mmap_free(&fh->cap);
3084 videobuf_mmap_free(&fh->vbi);
3085 file->private_data = NULL;
3088 bttv_field_count(btv);
3091 audio_mute(btv, btv->mute);
3093 v4l2_fh_del(&fh->fh);
3094 v4l2_fh_exit(&fh->fh);
3100 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3102 struct bttv_fh *fh = file->private_data;
3104 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
3105 fh->btv->c.nr, v4l2_type_names[fh->type],
3106 vma->vm_start, vma->vm_end - vma->vm_start);
3107 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3110 static const struct v4l2_file_operations bttv_fops =
3112 .owner = THIS_MODULE,
3114 .release = bttv_release,
3115 .unlocked_ioctl = video_ioctl2,
3121 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3122 .vidioc_querycap = bttv_querycap,
3123 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3124 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3125 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3126 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3127 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3128 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3129 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3130 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
3131 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3132 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3133 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
3134 .vidioc_cropcap = bttv_cropcap,
3135 .vidioc_reqbufs = bttv_reqbufs,
3136 .vidioc_querybuf = bttv_querybuf,
3137 .vidioc_qbuf = bttv_qbuf,
3138 .vidioc_dqbuf = bttv_dqbuf,
3139 .vidioc_s_std = bttv_s_std,
3140 .vidioc_g_std = bttv_g_std,
3141 .vidioc_enum_input = bttv_enum_input,
3142 .vidioc_g_input = bttv_g_input,
3143 .vidioc_s_input = bttv_s_input,
3144 .vidioc_streamon = bttv_streamon,
3145 .vidioc_streamoff = bttv_streamoff,
3146 .vidioc_g_tuner = bttv_g_tuner,
3147 .vidioc_s_tuner = bttv_s_tuner,
3148 .vidioc_g_crop = bttv_g_crop,
3149 .vidioc_s_crop = bttv_s_crop,
3150 .vidioc_g_fbuf = bttv_g_fbuf,
3151 .vidioc_s_fbuf = bttv_s_fbuf,
3152 .vidioc_overlay = bttv_overlay,
3153 .vidioc_g_parm = bttv_g_parm,
3154 .vidioc_g_frequency = bttv_g_frequency,
3155 .vidioc_s_frequency = bttv_s_frequency,
3156 .vidioc_log_status = bttv_log_status,
3157 .vidioc_querystd = bttv_querystd,
3158 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
3159 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
3160 #ifdef CONFIG_VIDEO_ADV_DEBUG
3161 .vidioc_g_register = bttv_g_register,
3162 .vidioc_s_register = bttv_s_register,
3166 static struct video_device bttv_video_template = {
3168 .ioctl_ops = &bttv_ioctl_ops,
3169 .tvnorms = BTTV_NORMS,
3172 /* ----------------------------------------------------------------------- */
3173 /* radio interface */
3175 static int radio_open(struct file *file)
3177 struct video_device *vdev = video_devdata(file);
3178 struct bttv *btv = video_drvdata(file);
3181 dprintk("open dev=%s\n", video_device_node_name(vdev));
3183 dprintk("%d: open called (radio)\n", btv->c.nr);
3185 /* allocate per filehandle data */
3186 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3189 file->private_data = fh;
3191 v4l2_fh_init(&fh->fh, vdev);
3194 audio_mute(btv, btv->mute);
3196 v4l2_fh_add(&fh->fh);
3201 static int radio_release(struct file *file)
3203 struct bttv_fh *fh = file->private_data;
3204 struct bttv *btv = fh->btv;
3205 struct saa6588_command cmd;
3207 file->private_data = NULL;
3208 v4l2_fh_del(&fh->fh);
3209 v4l2_fh_exit(&fh->fh);
3214 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
3216 if (btv->radio_user == 0)
3217 btv->has_radio_tuner = 0;
3221 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3223 struct bttv_fh *fh = priv;
3224 struct bttv *btv = fh->btv;
3228 strcpy(t->name, "Radio");
3229 t->type = V4L2_TUNER_RADIO;
3232 bttv_call_all(btv, tuner, g_tuner, t);
3234 if (btv->audio_mode_gpio)
3235 btv->audio_mode_gpio(btv, t, 0);
3237 if (btv->has_tea575x)
3238 return snd_tea575x_g_tuner(&btv->tea, t);
3243 static int radio_s_tuner(struct file *file, void *priv,
3244 const struct v4l2_tuner *t)
3246 struct bttv_fh *fh = priv;
3247 struct bttv *btv = fh->btv;
3253 bttv_call_all(btv, tuner, s_tuner, t);
3257 static int radio_s_hw_freq_seek(struct file *file, void *priv,
3258 const struct v4l2_hw_freq_seek *a)
3260 struct bttv_fh *fh = priv;
3261 struct bttv *btv = fh->btv;
3263 if (btv->has_tea575x)
3264 return snd_tea575x_s_hw_freq_seek(file, &btv->tea, a);
3269 static int radio_enum_freq_bands(struct file *file, void *priv,
3270 struct v4l2_frequency_band *band)
3272 struct bttv_fh *fh = priv;
3273 struct bttv *btv = fh->btv;
3275 if (btv->has_tea575x)
3276 return snd_tea575x_enum_freq_bands(&btv->tea, band);
3281 static ssize_t radio_read(struct file *file, char __user *data,
3282 size_t count, loff_t *ppos)
3284 struct bttv_fh *fh = file->private_data;
3285 struct bttv *btv = fh->btv;
3286 struct saa6588_command cmd;
3288 cmd.block_count = count / 3;
3289 cmd.nonblocking = file->f_flags & O_NONBLOCK;
3291 cmd.instance = file;
3292 cmd.result = -ENODEV;
3295 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
3300 static unsigned int radio_poll(struct file *file, poll_table *wait)
3302 struct bttv_fh *fh = file->private_data;
3303 struct bttv *btv = fh->btv;
3304 unsigned long req_events = poll_requested_events(wait);
3305 struct saa6588_command cmd;
3306 unsigned int res = 0;
3308 if (v4l2_event_pending(&fh->fh))
3310 else if (req_events & POLLPRI)
3311 poll_wait(file, &fh->fh.wait, wait);
3313 cmd.instance = file;
3314 cmd.event_list = wait;
3316 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
3321 static const struct v4l2_file_operations radio_fops =
3323 .owner = THIS_MODULE,
3326 .release = radio_release,
3327 .unlocked_ioctl = video_ioctl2,
3331 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3332 .vidioc_querycap = bttv_querycap,
3333 .vidioc_log_status = bttv_log_status,
3334 .vidioc_g_tuner = radio_g_tuner,
3335 .vidioc_s_tuner = radio_s_tuner,
3336 .vidioc_g_frequency = bttv_g_frequency,
3337 .vidioc_s_frequency = bttv_s_frequency,
3338 .vidioc_s_hw_freq_seek = radio_s_hw_freq_seek,
3339 .vidioc_enum_freq_bands = radio_enum_freq_bands,
3340 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
3341 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
3344 static struct video_device radio_template = {
3345 .fops = &radio_fops,
3346 .ioctl_ops = &radio_ioctl_ops,
3349 /* ----------------------------------------------------------------------- */
3350 /* some debug code */
3352 static int bttv_risc_decode(u32 risc)
3354 static char *instr[16] = {
3355 [ BT848_RISC_WRITE >> 28 ] = "write",
3356 [ BT848_RISC_SKIP >> 28 ] = "skip",
3357 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3358 [ BT848_RISC_JUMP >> 28 ] = "jump",
3359 [ BT848_RISC_SYNC >> 28 ] = "sync",
3360 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3361 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3362 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3364 static int incr[16] = {
3365 [ BT848_RISC_WRITE >> 28 ] = 2,
3366 [ BT848_RISC_JUMP >> 28 ] = 2,
3367 [ BT848_RISC_SYNC >> 28 ] = 2,
3368 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3369 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3370 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3372 static char *bits[] = {
3373 "be0", "be1", "be2", "be3/resync",
3374 "set0", "set1", "set2", "set3",
3375 "clr0", "clr1", "clr2", "clr3",
3376 "irq", "res", "eol", "sol",
3380 pr_cont("0x%08x [ %s", risc,
3381 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3382 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3383 if (risc & (1 << (i + 12)))
3384 pr_cont(" %s", bits[i]);
3385 pr_cont(" count=%d ]\n", risc & 0xfff);
3386 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3389 static void bttv_risc_disasm(struct bttv *btv,
3390 struct btcx_riscmem *risc)
3394 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3395 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
3396 for (i = 0; i < (risc->size >> 2); i += n) {
3397 pr_info("%s: 0x%lx: ",
3398 btv->c.v4l2_dev.name,
3399 (unsigned long)(risc->dma + (i<<2)));
3400 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
3401 for (j = 1; j < n; j++)
3402 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3403 btv->c.v4l2_dev.name,
3404 (unsigned long)(risc->dma + ((i+j)<<2)),
3406 if (0 == risc->cpu[i])
3411 static void bttv_print_riscaddr(struct bttv *btv)
3413 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
3414 pr_info(" vbi : o=%08llx e=%08llx\n",
3415 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3416 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3417 pr_info(" cap : o=%08llx e=%08llx\n",
3419 ? (unsigned long long)btv->curr.top->top.dma : 0,
3421 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3422 pr_info(" scr : o=%08llx e=%08llx\n",
3423 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3424 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3425 bttv_risc_disasm(btv, &btv->main);
3428 /* ----------------------------------------------------------------------- */
3431 static char *irq_name[] = {
3432 "FMTCHG", // format change detected (525 vs. 625)
3433 "VSYNC", // vertical sync (new field)
3434 "HSYNC", // horizontal sync
3435 "OFLOW", // chroma/luma AGC overflow
3436 "HLOCK", // horizontal lock changed
3437 "VPRES", // video presence changed
3439 "I2CDONE", // hw irc operation finished
3440 "GPINT", // gpio port triggered irq
3442 "RISCI", // risc instruction triggered irq
3443 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3444 "FTRGT", // pixel data fifo overrun
3445 "FDSR", // fifo data stream resyncronisation
3446 "PPERR", // parity error (data transfer)
3447 "RIPERR", // parity error (read risc instructions)
3448 "PABORT", // pci abort
3449 "OCERR", // risc instruction error
3450 "SCERR", // syncronisation error
3453 static void bttv_print_irqbits(u32 print, u32 mark)
3458 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3459 if (print & (1 << i))
3460 pr_cont(" %s", irq_name[i]);
3461 if (mark & (1 << i))
3466 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3468 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3470 (unsigned long)btv->main.dma,
3471 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3472 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3475 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3476 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3477 "Ok, then this is harmless, don't worry ;)\n",
3481 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3483 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3489 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3491 struct bttv_buffer *item;
3493 memset(set,0,sizeof(*set));
3495 /* capture request ? */
3496 if (!list_empty(&btv->capture)) {
3498 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3499 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3501 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3504 /* capture request for other field ? */
3505 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3506 (item->vb.queue.next != &btv->capture)) {
3507 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3508 /* Mike Isely <isely@pobox.com> - Only check
3509 * and set up the bottom field in the logic
3510 * below. Don't ever do the top field. This
3511 * of course means that if we set up the
3512 * bottom field in the above code that we'll
3513 * actually skip a field. But that's OK.
3514 * Having processed only a single buffer this
3515 * time, then the next time around the first
3516 * available buffer should be for a top field.
3517 * That will then cause us here to set up a
3518 * top then a bottom field in the normal way.
3519 * The alternative to this understanding is
3520 * that we set up the second available buffer
3521 * as a top field, but that's out of order
3522 * since this driver always processes the top
3523 * field first - the effect will be the two
3524 * buffers being returned in the wrong order,
3525 * with the second buffer also being delayed
3526 * by one field time (owing to the fifo nature
3527 * of videobuf). Worse still, we'll be stuck
3528 * doing fields out of order now every time
3529 * until something else causes a field to be
3530 * dropped. By effectively forcing a field to
3531 * drop this way then we always get back into
3532 * sync within a single frame time. (Out of
3533 * order fields can screw up deinterlacing
3535 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3536 if (NULL == set->bottom &&
3537 V4L2_FIELD_BOTTOM == item->vb.field) {
3540 if (NULL != set->top && NULL != set->bottom)
3546 /* screen overlay ? */
3547 if (NULL != btv->screen) {
3548 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3549 if (NULL == set->top && NULL == set->bottom) {
3550 set->top = btv->screen;
3551 set->bottom = btv->screen;
3554 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3556 set->top = btv->screen;
3558 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3559 NULL == set->bottom) {
3560 set->bottom = btv->screen;
3565 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3566 btv->c.nr, set->top, set->bottom,
3567 btv->screen, set->frame_irq, set->top_irq);
3572 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3573 struct bttv_buffer_set *curr, unsigned int state)
3577 v4l2_get_timestamp(&ts);
3579 if (wakeup->top == wakeup->bottom) {
3580 if (NULL != wakeup->top && curr->top != wakeup->top) {
3582 pr_debug("%d: wakeup: both=%p\n",
3583 btv->c.nr, wakeup->top);
3584 wakeup->top->vb.ts = ts;
3585 wakeup->top->vb.field_count = btv->field_count;
3586 wakeup->top->vb.state = state;
3587 wake_up(&wakeup->top->vb.done);
3590 if (NULL != wakeup->top && curr->top != wakeup->top) {
3592 pr_debug("%d: wakeup: top=%p\n",
3593 btv->c.nr, wakeup->top);
3594 wakeup->top->vb.ts = ts;
3595 wakeup->top->vb.field_count = btv->field_count;
3596 wakeup->top->vb.state = state;
3597 wake_up(&wakeup->top->vb.done);
3599 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3601 pr_debug("%d: wakeup: bottom=%p\n",
3602 btv->c.nr, wakeup->bottom);
3603 wakeup->bottom->vb.ts = ts;
3604 wakeup->bottom->vb.field_count = btv->field_count;
3605 wakeup->bottom->vb.state = state;
3606 wake_up(&wakeup->bottom->vb.done);
3612 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3618 v4l2_get_timestamp(&wakeup->vb.ts);
3619 wakeup->vb.field_count = btv->field_count;
3620 wakeup->vb.state = state;
3621 wake_up(&wakeup->vb.done);
3624 static void bttv_irq_timeout(unsigned long data)
3626 struct bttv *btv = (struct bttv *)data;
3627 struct bttv_buffer_set old,new;
3628 struct bttv_buffer *ovbi;
3629 struct bttv_buffer *item;
3630 unsigned long flags;
3633 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3634 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3635 btread(BT848_RISC_COUNT));
3636 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3640 spin_lock_irqsave(&btv->s_lock,flags);
3642 /* deactivate stuff */
3643 memset(&new,0,sizeof(new));
3649 bttv_buffer_activate_video(btv, &new);
3650 bttv_buffer_activate_vbi(btv, NULL);
3651 bttv_set_dma(btv, 0);
3654 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3655 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
3657 /* cancel all outstanding capture / vbi requests */
3658 while (!list_empty(&btv->capture)) {
3659 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3660 list_del(&item->vb.queue);
3661 item->vb.state = VIDEOBUF_ERROR;
3662 wake_up(&item->vb.done);
3664 while (!list_empty(&btv->vcapture)) {
3665 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3666 list_del(&item->vb.queue);
3667 item->vb.state = VIDEOBUF_ERROR;
3668 wake_up(&item->vb.done);
3672 spin_unlock_irqrestore(&btv->s_lock,flags);
3676 bttv_irq_wakeup_top(struct bttv *btv)
3678 struct bttv_buffer *wakeup = btv->curr.top;
3683 spin_lock(&btv->s_lock);
3684 btv->curr.top_irq = 0;
3685 btv->curr.top = NULL;
3686 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3688 v4l2_get_timestamp(&wakeup->vb.ts);
3689 wakeup->vb.field_count = btv->field_count;
3690 wakeup->vb.state = VIDEOBUF_DONE;
3691 wake_up(&wakeup->vb.done);
3692 spin_unlock(&btv->s_lock);
3695 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3699 if (rc > risc->dma + risc->size)
3705 bttv_irq_switch_video(struct bttv *btv)
3707 struct bttv_buffer_set new;
3708 struct bttv_buffer_set old;
3711 spin_lock(&btv->s_lock);
3713 /* new buffer set */
3714 bttv_irq_next_video(btv, &new);
3715 rc = btread(BT848_RISC_COUNT);
3716 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3717 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3720 bttv_irq_debug_low_latency(btv, rc);
3721 spin_unlock(&btv->s_lock);
3728 btv->loop_irq &= ~1;
3729 bttv_buffer_activate_video(btv, &new);
3730 bttv_set_dma(btv, 0);
3733 if (UNSET != btv->new_input) {
3734 video_mux(btv,btv->new_input);
3735 btv->new_input = UNSET;
3738 /* wake up finished buffers */
3739 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
3740 spin_unlock(&btv->s_lock);
3744 bttv_irq_switch_vbi(struct bttv *btv)
3746 struct bttv_buffer *new = NULL;
3747 struct bttv_buffer *old;
3750 spin_lock(&btv->s_lock);
3752 if (!list_empty(&btv->vcapture))
3753 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3756 rc = btread(BT848_RISC_COUNT);
3757 if (NULL != old && (is_active(&old->top, rc) ||
3758 is_active(&old->bottom, rc))) {
3761 bttv_irq_debug_low_latency(btv, rc);
3762 spin_unlock(&btv->s_lock);
3768 btv->loop_irq &= ~4;
3769 bttv_buffer_activate_vbi(btv, new);
3770 bttv_set_dma(btv, 0);
3772 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
3773 spin_unlock(&btv->s_lock);
3776 static irqreturn_t bttv_irq(int irq, void *dev_id)
3784 btv=(struct bttv *)dev_id;
3788 /* get/clear interrupt status bits */
3789 stat=btread(BT848_INT_STAT);
3790 astat=stat&btread(BT848_INT_MASK);
3794 btwrite(stat,BT848_INT_STAT);
3796 /* get device status bits */
3797 dstat=btread(BT848_DSTATUS);
3800 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
3801 btv->c.nr, count, btv->field_count,
3802 stat>>28, btread(BT848_RISC_COUNT));
3803 bttv_print_irqbits(stat,astat);
3804 if (stat & BT848_INT_HLOCK)
3805 pr_cont(" HLOC => %s",
3806 dstat & BT848_DSTATUS_HLOC
3808 if (stat & BT848_INT_VPRES)
3809 pr_cont(" PRES => %s",
3810 dstat & BT848_DSTATUS_PRES
3812 if (stat & BT848_INT_FMTCHG)
3813 pr_cont(" NUML => %s",
3814 dstat & BT848_DSTATUS_NUML
3819 if (astat&BT848_INT_VSYNC)
3822 if ((astat & BT848_INT_GPINT) && btv->remote) {
3823 bttv_input_irq(btv);
3826 if (astat & BT848_INT_I2CDONE) {
3827 btv->i2c_done = stat;
3828 wake_up(&btv->i2c_queue);
3831 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3832 bttv_irq_switch_vbi(btv);
3834 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3835 bttv_irq_wakeup_top(btv);
3837 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3838 bttv_irq_switch_video(btv);
3840 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3841 /* trigger automute */
3842 audio_mux_gpio(btv, btv->audio_input, btv->mute);
3844 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3845 pr_info("%d: %s%s @ %08x,",
3847 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3848 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3849 btread(BT848_RISC_COUNT));
3850 bttv_print_irqbits(stat,astat);
3853 bttv_print_riscaddr(btv);
3855 if (fdsr && astat & BT848_INT_FDSR) {
3856 pr_info("%d: FDSR @ %08x\n",
3857 btv->c.nr, btread(BT848_RISC_COUNT));
3859 bttv_print_riscaddr(btv);
3865 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3866 btwrite(0, BT848_INT_MASK);
3868 pr_err("%d: IRQ lockup, cleared int mask [",
3871 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3874 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3878 bttv_print_irqbits(stat,astat);
3886 return IRQ_RETVAL(handled);
3890 /* ----------------------------------------------------------------------- */
3891 /* initialization */
3893 static void vdev_init(struct bttv *btv,
3894 struct video_device *vfd,
3895 const struct video_device *template,
3896 const char *type_name)
3899 vfd->v4l2_dev = &btv->c.v4l2_dev;
3900 vfd->release = video_device_release_empty;
3901 video_set_drvdata(vfd, btv);
3902 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3903 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3904 type_name, bttv_tvcards[btv->c.type].name);
3905 if (btv->tuner_type == TUNER_ABSENT) {
3906 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
3907 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
3908 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
3909 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
3913 static void bttv_unregister_video(struct bttv *btv)
3915 video_unregister_device(&btv->video_dev);
3916 video_unregister_device(&btv->vbi_dev);
3917 video_unregister_device(&btv->radio_dev);
3920 /* register video4linux devices */
3921 static int bttv_register_video(struct bttv *btv)
3924 pr_notice("Overlay support disabled\n");
3927 vdev_init(btv, &btv->video_dev, &bttv_video_template, "video");
3929 if (video_register_device(&btv->video_dev, VFL_TYPE_GRABBER,
3930 video_nr[btv->c.nr]) < 0)
3932 pr_info("%d: registered device %s\n",
3933 btv->c.nr, video_device_node_name(&btv->video_dev));
3934 if (device_create_file(&btv->video_dev.dev,
3935 &dev_attr_card)<0) {
3936 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
3941 vdev_init(btv, &btv->vbi_dev, &bttv_video_template, "vbi");
3943 if (video_register_device(&btv->vbi_dev, VFL_TYPE_VBI,
3944 vbi_nr[btv->c.nr]) < 0)
3946 pr_info("%d: registered device %s\n",
3947 btv->c.nr, video_device_node_name(&btv->vbi_dev));
3949 if (!btv->has_radio)
3952 vdev_init(btv, &btv->radio_dev, &radio_template, "radio");
3953 btv->radio_dev.ctrl_handler = &btv->radio_ctrl_handler;
3954 if (video_register_device(&btv->radio_dev, VFL_TYPE_RADIO,
3955 radio_nr[btv->c.nr]) < 0)
3957 pr_info("%d: registered device %s\n",
3958 btv->c.nr, video_device_node_name(&btv->radio_dev));
3964 bttv_unregister_video(btv);
3969 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3970 /* response on cards with no firmware is not enabled by OF */
3971 static void pci_set_command(struct pci_dev *dev)
3973 #if defined(__powerpc__)
3976 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3977 cmd = (cmd | PCI_COMMAND_MEMORY );
3978 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3982 static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
3984 struct v4l2_frequency init_freq = {
3986 .type = V4L2_TUNER_ANALOG_TV,
3992 struct v4l2_ctrl_handler *hdl;
3994 if (bttv_num == BTTV_MAX)
3996 pr_info("Bt8xx card found (%d)\n", bttv_num);
3997 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
3999 pr_err("out of memory\n");
4002 btv->c.nr = bttv_num;
4003 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4004 "bttv%d", btv->c.nr);
4006 /* initialize structs / fill in defaults */
4007 mutex_init(&btv->lock);
4008 spin_lock_init(&btv->s_lock);
4009 spin_lock_init(&btv->gpio_lock);
4010 init_waitqueue_head(&btv->i2c_queue);
4011 INIT_LIST_HEAD(&btv->c.subs);
4012 INIT_LIST_HEAD(&btv->capture);
4013 INIT_LIST_HEAD(&btv->vcapture);
4015 init_timer(&btv->timeout);
4016 btv->timeout.function = bttv_irq_timeout;
4017 btv->timeout.data = (unsigned long)btv;
4020 btv->tuner_type = UNSET;
4021 btv->new_input = UNSET;
4022 btv->has_radio=radio[btv->c.nr];
4024 /* pci stuff (init, get irq/mmio, ... */
4026 btv->id = dev->device;
4027 if (pci_enable_device(dev)) {
4028 pr_warn("%d: Can't enable device\n", btv->c.nr);
4031 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
4032 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
4035 if (!request_mem_region(pci_resource_start(dev,0),
4036 pci_resource_len(dev,0),
4037 btv->c.v4l2_dev.name)) {
4038 pr_warn("%d: can't request iomem (0x%llx)\n",
4040 (unsigned long long)pci_resource_start(dev, 0));
4043 pci_set_master(dev);
4044 pci_set_command(dev);
4046 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4048 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
4051 hdl = &btv->ctrl_handler;
4052 v4l2_ctrl_handler_init(hdl, 20);
4053 btv->c.v4l2_dev.ctrl_handler = hdl;
4054 v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
4056 btv->revision = dev->revision;
4057 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4058 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4059 bttv_num, btv->id, btv->revision, pci_name(dev),
4060 btv->c.pci->irq, lat,
4061 (unsigned long long)pci_resource_start(dev, 0));
4064 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4065 if (NULL == btv->bt848_mmio) {
4066 pr_err("%d: ioremap() failed\n", btv->c.nr);
4074 /* disable irqs, register irq handler */
4075 btwrite(0, BT848_INT_MASK);
4076 result = request_irq(btv->c.pci->irq, bttv_irq,
4077 IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
4079 pr_err("%d: can't get IRQ %d\n",
4080 bttv_num, btv->c.pci->irq);
4084 if (0 != bttv_handle_chipset(btv)) {
4089 /* init options from insmod args */
4090 btv->opt_combfilter = combfilter;
4091 bttv_ctrl_combfilter.def = combfilter;
4092 bttv_ctrl_lumafilter.def = lumafilter;
4093 btv->opt_automute = automute;
4094 bttv_ctrl_automute.def = automute;
4095 bttv_ctrl_agc_crush.def = agc_crush;
4096 btv->opt_vcr_hack = vcr_hack;
4097 bttv_ctrl_vcr_hack.def = vcr_hack;
4098 bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
4099 bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
4100 btv->opt_uv_ratio = uv_ratio;
4101 bttv_ctrl_uv_ratio.def = uv_ratio;
4102 bttv_ctrl_full_luma.def = full_luma_range;
4103 bttv_ctrl_coring.def = coring;
4105 /* fill struct bttv with some useful defaults */
4106 btv->init.btv = btv;
4107 btv->init.ov.w.width = 320;
4108 btv->init.ov.w.height = 240;
4109 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4110 btv->init.width = 320;
4111 btv->init.height = 240;
4112 btv->init.ov.w.width = 320;
4113 btv->init.ov.w.height = 240;
4114 btv->init.ov.field = V4L2_FIELD_INTERLACED;
4117 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4118 V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
4119 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4120 V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
4121 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4122 V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
4123 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4124 V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
4125 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4126 V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
4127 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4128 V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
4129 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4130 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
4131 if (btv->volume_gpio)
4132 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4133 V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
4134 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
4135 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
4136 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
4137 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
4138 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
4139 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
4140 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
4141 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
4142 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
4143 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
4145 /* initialize hardware */
4147 bttv_gpio_tracking(btv,"pre-init");
4149 bttv_risc_init_main(btv);
4153 btwrite(0x00, BT848_GPIO_REG_INP);
4154 btwrite(0x00, BT848_GPIO_OUT_EN);
4156 bttv_gpio_tracking(btv,"init");
4158 /* needs to be done before i2c is registered */
4159 bttv_init_card1(btv);
4161 /* register i2c + gpio */
4164 /* some card-specific stuff (needs working i2c) */
4165 bttv_init_card2(btv);
4166 bttv_init_tuner(btv);
4167 if (btv->tuner_type != TUNER_ABSENT) {
4168 bttv_set_frequency(btv, &init_freq);
4169 btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
4171 btv->std = V4L2_STD_PAL;
4173 if (!bttv_tvcards[btv->c.type].no_video)
4174 v4l2_ctrl_handler_setup(hdl);
4176 result = hdl->error;
4182 /* register video4linux + input */
4183 if (!bttv_tvcards[btv->c.type].no_video) {
4184 v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
4185 v4l2_ctrl_radio_filter);
4186 if (btv->radio_ctrl_handler.error) {
4187 result = btv->radio_ctrl_handler.error;
4190 set_input(btv, 0, btv->tvnorm);
4191 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4192 btv->crop[1] = btv->crop[0]; /* current = default */
4193 disclaim_vbi_lines(btv);
4194 disclaim_video_lines(btv);
4195 bttv_register_video(btv);
4198 /* add subdevices and autoload dvb-bt8xx if needed */
4199 if (bttv_tvcards[btv->c.type].has_dvb) {
4200 bttv_sub_add_device(&btv->c, "dvb");
4201 request_modules(btv);
4205 init_bttv_i2c_ir(btv);
4206 bttv_input_init(btv);
4209 /* everything is fine */
4214 free_irq(btv->c.pci->irq,btv);
4217 v4l2_ctrl_handler_free(&btv->ctrl_handler);
4218 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
4219 v4l2_device_unregister(&btv->c.v4l2_dev);
4222 if (btv->bt848_mmio)
4223 iounmap(btv->bt848_mmio);
4224 release_mem_region(pci_resource_start(btv->c.pci,0),
4225 pci_resource_len(btv->c.pci,0));
4226 pci_disable_device(btv->c.pci);
4230 static void bttv_remove(struct pci_dev *pci_dev)
4232 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4233 struct bttv *btv = to_bttv(v4l2_dev);
4236 pr_info("%d: unloading\n", btv->c.nr);
4238 if (bttv_tvcards[btv->c.type].has_dvb)
4239 flush_request_modules(btv);
4241 /* shutdown everything (DMA+IRQs) */
4242 btand(~15, BT848_GPIO_DMA_CTL);
4243 btwrite(0, BT848_INT_MASK);
4244 btwrite(~0x0, BT848_INT_STAT);
4245 btwrite(0x0, BT848_GPIO_OUT_EN);
4247 bttv_gpio_tracking(btv,"cleanup");
4249 /* tell gpio modules we are leaving ... */
4251 bttv_input_fini(btv);
4252 bttv_sub_del_devices(&btv->c);
4254 /* unregister i2c_bus + input */
4257 /* unregister video4linux */
4258 bttv_unregister_video(btv);
4260 /* free allocated memory */
4261 v4l2_ctrl_handler_free(&btv->ctrl_handler);
4262 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
4263 btcx_riscmem_free(btv->c.pci,&btv->main);
4265 /* free resources */
4266 free_irq(btv->c.pci->irq,btv);
4267 iounmap(btv->bt848_mmio);
4268 release_mem_region(pci_resource_start(btv->c.pci,0),
4269 pci_resource_len(btv->c.pci,0));
4270 pci_disable_device(btv->c.pci);
4272 v4l2_device_unregister(&btv->c.v4l2_dev);
4273 bttvs[btv->c.nr] = NULL;
4280 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4282 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4283 struct bttv *btv = to_bttv(v4l2_dev);
4284 struct bttv_buffer_set idle;
4285 unsigned long flags;
4287 dprintk("%d: suspend %d\n", btv->c.nr, state.event);
4289 /* stop dma + irqs */
4290 spin_lock_irqsave(&btv->s_lock,flags);
4291 memset(&idle, 0, sizeof(idle));
4292 btv->state.video = btv->curr;
4293 btv->state.vbi = btv->cvbi;
4294 btv->state.loop_irq = btv->loop_irq;
4297 bttv_buffer_activate_video(btv, &idle);
4298 bttv_buffer_activate_vbi(btv, NULL);
4299 bttv_set_dma(btv, 0);
4300 btwrite(0, BT848_INT_MASK);
4301 spin_unlock_irqrestore(&btv->s_lock,flags);
4303 /* save bt878 state */
4304 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4305 btv->state.gpio_data = gpio_read();
4307 /* save pci state */
4308 pci_save_state(pci_dev);
4309 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4310 pci_disable_device(pci_dev);
4311 btv->state.disabled = 1;
4316 static int bttv_resume(struct pci_dev *pci_dev)
4318 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4319 struct bttv *btv = to_bttv(v4l2_dev);
4320 unsigned long flags;
4323 dprintk("%d: resume\n", btv->c.nr);
4325 /* restore pci state */
4326 if (btv->state.disabled) {
4327 err=pci_enable_device(pci_dev);
4329 pr_warn("%d: Can't enable device\n", btv->c.nr);
4332 btv->state.disabled = 0;
4334 err=pci_set_power_state(pci_dev, PCI_D0);
4336 pci_disable_device(pci_dev);
4337 pr_warn("%d: Can't enable device\n", btv->c.nr);
4338 btv->state.disabled = 1;
4342 pci_restore_state(pci_dev);
4344 /* restore bt878 state */
4345 bttv_reinit_bt848(btv);
4346 gpio_inout(0xffffff, btv->state.gpio_enable);
4347 gpio_write(btv->state.gpio_data);
4350 spin_lock_irqsave(&btv->s_lock,flags);
4351 btv->curr = btv->state.video;
4352 btv->cvbi = btv->state.vbi;
4353 btv->loop_irq = btv->state.loop_irq;
4354 bttv_buffer_activate_video(btv, &btv->curr);
4355 bttv_buffer_activate_vbi(btv, btv->cvbi);
4356 bttv_set_dma(btv, 0);
4357 spin_unlock_irqrestore(&btv->s_lock,flags);
4362 static struct pci_device_id bttv_pci_tbl[] = {
4363 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4364 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4365 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4366 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
4367 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
4371 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4373 static struct pci_driver bttv_pci_driver = {
4375 .id_table = bttv_pci_tbl,
4376 .probe = bttv_probe,
4377 .remove = bttv_remove,
4379 .suspend = bttv_suspend,
4380 .resume = bttv_resume,
4384 static int __init bttv_init_module(void)
4390 pr_info("driver version %s loaded\n", BTTV_VERSION);
4391 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4393 if (gbufsize > BTTV_MAX_FBUF)
4394 gbufsize = BTTV_MAX_FBUF;
4395 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4397 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4398 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4400 bttv_check_chipset();
4402 ret = bus_register(&bttv_sub_bus_type);
4404 pr_warn("bus_register error: %d\n", ret);
4407 ret = pci_register_driver(&bttv_pci_driver);
4409 bus_unregister(&bttv_sub_bus_type);
4414 static void __exit bttv_cleanup_module(void)
4416 pci_unregister_driver(&bttv_pci_driver);
4417 bus_unregister(&bttv_sub_bus_type);
4420 module_init(bttv_init_module);
4421 module_exit(bttv_cleanup_module);