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@kernel.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 is 1 (yes) for compatibility with older applications");
152 MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
153 MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
154 MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
155 MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
156 MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
157 MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
158 MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
159 MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
160 MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
161 MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
162 MODULE_PARM_DESC(video_nr, "video device numbers");
163 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
164 MODULE_PARM_DESC(radio_nr, "radio device numbers");
166 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
167 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
168 MODULE_LICENSE("GPL");
169 MODULE_VERSION(BTTV_VERSION);
171 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
172 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
173 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
174 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
175 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
176 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
177 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
178 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
179 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
180 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
182 /* ----------------------------------------------------------------------- */
185 static ssize_t show_card(struct device *cd,
186 struct device_attribute *attr, char *buf)
188 struct video_device *vfd = to_video_device(cd);
189 struct bttv *btv = video_get_drvdata(vfd);
190 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
192 static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
194 /* ----------------------------------------------------------------------- */
195 /* dvb auto-load setup */
196 #if defined(CONFIG_MODULES) && defined(MODULE)
197 static void request_module_async(struct work_struct *work)
199 request_module("dvb-bt8xx");
202 static void request_modules(struct bttv *dev)
204 INIT_WORK(&dev->request_module_wk, request_module_async);
205 schedule_work(&dev->request_module_wk);
208 static void flush_request_modules(struct bttv *dev)
210 flush_work(&dev->request_module_wk);
213 #define request_modules(dev)
214 #define flush_request_modules(dev) do {} while(0)
215 #endif /* CONFIG_MODULES */
218 /* ----------------------------------------------------------------------- */
221 /* special timing tables from conexant... */
222 static u8 SRAM_Table[][60] =
224 /* PAL digital input over GPIO[7:0] */
226 45, // 45 bytes following
227 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
228 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
229 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
230 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
231 0x37,0x00,0xAF,0x21,0x00
233 /* NTSC digital input over GPIO[7:0] */
235 51, // 51 bytes following
236 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
237 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
238 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
239 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
240 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
243 // TGB_NTSC392 // quartzsight
244 // This table has been modified to be used for Fusion Rev D
246 0x2A, // size of table = 42
247 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
248 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
249 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
250 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
255 /* minhdelayx1 first video pixel we can capture on a line and
256 hdelayx1 start of active video, both relative to rising edge of
257 /HRESET pulse (0H) in 1 / fCLKx1.
258 swidth width of active video and
259 totalwidth total line width, both in 1 / fCLKx1.
260 sqwidth total line width in square pixels.
261 vdelay start of active video in 2 * field lines relative to
262 trailing edge of /VRESET pulse (VDELAY register).
263 sheight height of active video in 2 * field lines.
264 extraheight Added to sheight for cropcap.bounds.height only
265 videostart0 ITU-R frame line number of the line corresponding
266 to vdelay in the first field. */
267 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
268 vdelay, sheight, extraheight, videostart0) \
269 .cropcap.bounds.left = minhdelayx1, \
270 /* * 2 because vertically we count field lines times two, */ \
271 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
272 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
273 /* 4 is a safety margin at the end of the line. */ \
274 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
275 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
277 .cropcap.defrect.left = hdelayx1, \
278 .cropcap.defrect.top = (videostart0) * 2, \
279 .cropcap.defrect.width = swidth, \
280 .cropcap.defrect.height = sheight, \
281 .cropcap.pixelaspect.numerator = totalwidth, \
282 .cropcap.pixelaspect.denominator = sqwidth,
284 const struct bttv_tvnorm bttv_tvnorms[] = {
286 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
287 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
289 .v4l2_id = V4L2_STD_PAL,
297 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
298 .scaledtwidth = 1135,
302 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
304 /* ITU-R frame line number of the first VBI line
305 we can capture, of the first and second field.
306 The last line is determined by cropcap.bounds. */
307 .vbistart = { 7, 320 },
308 CROPCAP(/* minhdelayx1 */ 68,
310 /* Should be (768 * 1135 + 944 / 2) / 944.
311 cropcap.defrect is used for image width
312 checks, so we keep the old value 924. */
314 /* totalwidth */ 1135,
318 /* bt878 (and bt848?) can capture another
319 line below active video. */
321 /* videostart0 */ 23)
323 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
331 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
336 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
338 .vbistart = { 10, 273 },
339 CROPCAP(/* minhdelayx1 */ 68,
341 /* Should be (640 * 910 + 780 / 2) / 780? */
343 /* totalwidth */ 910,
348 /* videostart0 */ 23)
350 .v4l2_id = V4L2_STD_SECAM,
358 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
359 .scaledtwidth = 1135,
364 .sram = 0, /* like PAL, correct? */
365 .vbistart = { 7, 320 },
366 CROPCAP(/* minhdelayx1 */ 68,
369 /* totalwidth */ 1135,
374 /* videostart0 */ 23)
376 .v4l2_id = V4L2_STD_PAL_Nc,
384 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
391 .vbistart = { 7, 320 },
392 CROPCAP(/* minhdelayx1 */ 68,
394 /* swidth */ (640 * 910 + 780 / 2) / 780,
395 /* totalwidth */ 910,
400 /* videostart0 */ 23)
402 .v4l2_id = V4L2_STD_PAL_M,
410 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
417 .vbistart = { 10, 273 },
418 CROPCAP(/* minhdelayx1 */ 68,
420 /* swidth */ (640 * 910 + 780 / 2) / 780,
421 /* totalwidth */ 910,
426 /* videostart0 */ 23)
428 .v4l2_id = V4L2_STD_PAL_N,
436 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
443 .vbistart = { 7, 320 },
444 CROPCAP(/* minhdelayx1 */ 68,
446 /* swidth */ (768 * 1135 + 944 / 2) / 944,
447 /* totalwidth */ 1135,
452 /* videostart0 */ 23)
454 .v4l2_id = V4L2_STD_NTSC_M_JP,
462 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
469 .vbistart = { 10, 273 },
470 CROPCAP(/* minhdelayx1 */ 68,
472 /* swidth */ (640 * 910 + 780 / 2) / 780,
473 /* totalwidth */ 910,
478 /* videostart0 */ 23)
480 /* that one hopefully works with the strange timing
481 * which video recorders produce when playing a NTSC
482 * tape on a PAL TV ... */
483 .v4l2_id = V4L2_STD_PAL_60,
491 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
492 .scaledtwidth = 1135,
499 .vbistart = { 10, 273 },
500 CROPCAP(/* minhdelayx1 */ 68,
503 /* totalwidth */ 1135,
508 /* videostart0 */ 23)
511 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
513 /* ----------------------------------------------------------------------- */
515 packed pixel formats must come first */
516 static const struct bttv_format formats[] = {
518 .name = "8 bpp, gray",
519 .fourcc = V4L2_PIX_FMT_GREY,
520 .btformat = BT848_COLOR_FMT_Y8,
522 .flags = FORMAT_FLAGS_PACKED,
524 .name = "8 bpp, dithered color",
525 .fourcc = V4L2_PIX_FMT_HI240,
526 .btformat = BT848_COLOR_FMT_RGB8,
528 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
530 .name = "15 bpp RGB, le",
531 .fourcc = V4L2_PIX_FMT_RGB555,
532 .btformat = BT848_COLOR_FMT_RGB15,
534 .flags = FORMAT_FLAGS_PACKED,
536 .name = "15 bpp RGB, be",
537 .fourcc = V4L2_PIX_FMT_RGB555X,
538 .btformat = BT848_COLOR_FMT_RGB15,
539 .btswap = 0x03, /* byteswap */
541 .flags = FORMAT_FLAGS_PACKED,
543 .name = "16 bpp RGB, le",
544 .fourcc = V4L2_PIX_FMT_RGB565,
545 .btformat = BT848_COLOR_FMT_RGB16,
547 .flags = FORMAT_FLAGS_PACKED,
549 .name = "16 bpp RGB, be",
550 .fourcc = V4L2_PIX_FMT_RGB565X,
551 .btformat = BT848_COLOR_FMT_RGB16,
552 .btswap = 0x03, /* byteswap */
554 .flags = FORMAT_FLAGS_PACKED,
556 .name = "24 bpp RGB, le",
557 .fourcc = V4L2_PIX_FMT_BGR24,
558 .btformat = BT848_COLOR_FMT_RGB24,
560 .flags = FORMAT_FLAGS_PACKED,
562 .name = "32 bpp RGB, le",
563 .fourcc = V4L2_PIX_FMT_BGR32,
564 .btformat = BT848_COLOR_FMT_RGB32,
566 .flags = FORMAT_FLAGS_PACKED,
568 .name = "32 bpp RGB, be",
569 .fourcc = V4L2_PIX_FMT_RGB32,
570 .btformat = BT848_COLOR_FMT_RGB32,
571 .btswap = 0x0f, /* byte+word swap */
573 .flags = FORMAT_FLAGS_PACKED,
575 .name = "4:2:2, packed, YUYV",
576 .fourcc = V4L2_PIX_FMT_YUYV,
577 .btformat = BT848_COLOR_FMT_YUY2,
579 .flags = FORMAT_FLAGS_PACKED,
581 .name = "4:2:2, packed, UYVY",
582 .fourcc = V4L2_PIX_FMT_UYVY,
583 .btformat = BT848_COLOR_FMT_YUY2,
584 .btswap = 0x03, /* byteswap */
586 .flags = FORMAT_FLAGS_PACKED,
588 .name = "4:2:2, planar, Y-Cb-Cr",
589 .fourcc = V4L2_PIX_FMT_YUV422P,
590 .btformat = BT848_COLOR_FMT_YCrCb422,
592 .flags = FORMAT_FLAGS_PLANAR,
596 .name = "4:2:0, planar, Y-Cb-Cr",
597 .fourcc = V4L2_PIX_FMT_YUV420,
598 .btformat = BT848_COLOR_FMT_YCrCb422,
600 .flags = FORMAT_FLAGS_PLANAR,
604 .name = "4:2:0, planar, Y-Cr-Cb",
605 .fourcc = V4L2_PIX_FMT_YVU420,
606 .btformat = BT848_COLOR_FMT_YCrCb422,
608 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
612 .name = "4:1:1, planar, Y-Cb-Cr",
613 .fourcc = V4L2_PIX_FMT_YUV411P,
614 .btformat = BT848_COLOR_FMT_YCrCb411,
616 .flags = FORMAT_FLAGS_PLANAR,
620 .name = "4:1:0, planar, Y-Cb-Cr",
621 .fourcc = V4L2_PIX_FMT_YUV410,
622 .btformat = BT848_COLOR_FMT_YCrCb411,
624 .flags = FORMAT_FLAGS_PLANAR,
628 .name = "4:1:0, planar, Y-Cr-Cb",
629 .fourcc = V4L2_PIX_FMT_YVU410,
630 .btformat = BT848_COLOR_FMT_YCrCb411,
632 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
636 .name = "raw scanlines",
638 .btformat = BT848_COLOR_FMT_RAW,
640 .flags = FORMAT_FLAGS_RAW,
643 static const unsigned int FORMATS = ARRAY_SIZE(formats);
645 /* ----------------------------------------------------------------------- */
646 /* resource management */
649 RESOURCE_ allocated by freed by
651 VIDEO_READ bttv_read 1) bttv_read 2)
653 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
654 VIDIOC_QBUF 1) bttv_release
657 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
658 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
661 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
662 VIDIOC_QBUF 1) bttv_release
663 bttv_read, bttv_poll 1) 4)
665 1) The resource must be allocated when we enter buffer prepare functions
666 and remain allocated while buffers are in the DMA queue.
667 2) This is a single frame read.
668 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
669 RESOURCE_OVERLAY is allocated.
670 4) This is a continuous read, implies VIDIOC_STREAMON.
672 Note this driver permits video input and standard changes regardless if
673 resources are allocated.
676 #define VBI_RESOURCES (RESOURCE_VBI)
677 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
678 RESOURCE_VIDEO_STREAM | \
682 int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
684 int xbits; /* mutual exclusive resources */
686 if (fh->resources & bit)
687 /* have it already allocated */
691 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
692 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
695 if (btv->resources & xbits) {
696 /* no, someone else uses it */
700 if ((bit & VIDEO_RESOURCES)
701 && 0 == (btv->resources & VIDEO_RESOURCES)) {
702 /* Do crop - use current, don't - use default parameters. */
703 __s32 top = btv->crop[!!fh->do_crop].rect.top;
705 if (btv->vbi_end > top)
708 /* We cannot capture the same line as video and VBI data.
709 Claim scan lines crop[].rect.top to bottom. */
710 btv->crop_start = top;
711 } else if (bit & VBI_RESOURCES) {
712 __s32 end = fh->vbi_fmt.end;
714 if (end > btv->crop_start)
717 /* Claim scan lines above fh->vbi_fmt.end. */
721 /* it's free, grab it */
722 fh->resources |= bit;
723 btv->resources |= bit;
731 int check_btres(struct bttv_fh *fh, int bit)
733 return (fh->resources & bit);
737 int locked_btres(struct bttv *btv, int bit)
739 return (btv->resources & bit);
742 /* Call with btv->lock down. */
744 disclaim_vbi_lines(struct bttv *btv)
749 /* Call with btv->lock down. */
751 disclaim_video_lines(struct bttv *btv)
753 const struct bttv_tvnorm *tvnorm;
756 tvnorm = &bttv_tvnorms[btv->tvnorm];
757 btv->crop_start = tvnorm->cropcap.bounds.top
758 + tvnorm->cropcap.bounds.height;
760 /* VBI capturing ends at VDELAY, start of video capturing, no
761 matter how many lines the VBI RISC program expects. When video
762 capturing is off, it shall no longer "preempt" VBI capturing,
763 so we set VDELAY to maximum. */
764 crop = btread(BT848_E_CROP) | 0xc0;
765 btwrite(crop, BT848_E_CROP);
766 btwrite(0xfe, BT848_E_VDELAY_LO);
767 btwrite(crop, BT848_O_CROP);
768 btwrite(0xfe, BT848_O_VDELAY_LO);
772 void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
774 if ((fh->resources & bits) != bits) {
775 /* trying to free resources not allocated by us ... */
776 pr_err("BUG! (btres)\n");
778 fh->resources &= ~bits;
779 btv->resources &= ~bits;
781 bits = btv->resources;
783 if (0 == (bits & VIDEO_RESOURCES))
784 disclaim_video_lines(btv);
786 if (0 == (bits & VBI_RESOURCES))
787 disclaim_vbi_lines(btv);
790 /* ----------------------------------------------------------------------- */
791 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
793 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
794 PLL_X = Reference pre-divider (0=1, 1=2)
795 PLL_C = Post divider (0=6, 1=4)
796 PLL_I = Integer input
797 PLL_F = Fractional input
799 F_input = 28.636363 MHz:
800 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
803 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
805 unsigned char fl, fh, fi;
807 /* prevent overflows */
820 btwrite(fl, BT848_PLL_F_LO);
821 btwrite(fh, BT848_PLL_F_HI);
822 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
825 static void set_pll(struct bttv *btv)
829 if (!btv->pll.pll_crystal)
832 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
833 dprintk("%d: PLL: no change required\n", btv->c.nr);
837 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
839 if (btv->pll.pll_current == 0)
842 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
843 btv->c.nr, btv->pll.pll_ifreq);
844 btwrite(0x00,BT848_TGCTRL);
845 btwrite(0x00,BT848_PLL_XCI);
846 btv->pll.pll_current = 0;
851 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
853 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
854 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
856 for (i=0; i<10; i++) {
857 /* Let other people run while the PLL stabilizes */
860 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
861 btwrite(0,BT848_DSTATUS);
863 btwrite(0x08,BT848_TGCTRL);
864 btv->pll.pll_current = btv->pll.pll_ofreq;
866 pr_info("PLL set ok\n");
870 btv->pll.pll_current = -1;
872 pr_info("Setting PLL failed\n");
876 /* used to switch between the bt848's analog/digital video capture modes */
877 static void bt848A_set_timing(struct bttv *btv)
880 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
881 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
883 if (btv->input == btv->dig) {
884 dprintk("%d: load digital timing table (table_idx=%d)\n",
885 btv->c.nr,table_idx);
887 /* timing change...reset timing generator address */
888 btwrite(0x00, BT848_TGCTRL);
889 btwrite(0x02, BT848_TGCTRL);
890 btwrite(0x00, BT848_TGCTRL);
892 len=SRAM_Table[table_idx][0];
893 for(i = 1; i <= len; i++)
894 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
895 btv->pll.pll_ofreq = 27000000;
898 btwrite(0x11, BT848_TGCTRL);
899 btwrite(0x41, BT848_DVSIF);
901 btv->pll.pll_ofreq = fsc;
903 btwrite(0x0, BT848_DVSIF);
907 /* ----------------------------------------------------------------------- */
909 static void bt848_bright(struct bttv *btv, int bright)
913 // printk("set bright: %d\n", bright); // DEBUG
914 btv->bright = bright;
916 /* We want -128 to 127 we get 0-65535 */
917 value = (bright >> 8) - 128;
918 btwrite(value & 0xff, BT848_BRIGHT);
921 static void bt848_hue(struct bttv *btv, int hue)
928 value = (hue >> 8) - 128;
929 btwrite(value & 0xff, BT848_HUE);
932 static void bt848_contrast(struct bttv *btv, int cont)
936 btv->contrast = cont;
940 hibit = (value >> 6) & 4;
941 btwrite(value & 0xff, BT848_CONTRAST_LO);
942 btaor(hibit, ~4, BT848_E_CONTROL);
943 btaor(hibit, ~4, BT848_O_CONTROL);
946 static void bt848_sat(struct bttv *btv, int color)
948 int val_u,val_v,hibits;
950 btv->saturation = color;
952 /* 0-511 for the color */
953 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
954 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
955 hibits = (val_u >> 7) & 2;
956 hibits |= (val_v >> 8) & 1;
957 btwrite(val_u & 0xff, BT848_SAT_U_LO);
958 btwrite(val_v & 0xff, BT848_SAT_V_LO);
959 btaor(hibits, ~3, BT848_E_CONTROL);
960 btaor(hibits, ~3, BT848_O_CONTROL);
963 /* ----------------------------------------------------------------------- */
966 video_mux(struct bttv *btv, unsigned int input)
970 if (input >= bttv_tvcards[btv->c.type].video_inputs)
973 /* needed by RemoteVideo MX */
974 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
976 gpio_inout(mask2,mask2);
978 if (input == btv->svhs) {
979 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
980 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
982 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
983 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
985 mux = bttv_muxsel(btv, input);
986 btaor(mux<<5, ~(3<<5), BT848_IFORM);
987 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
989 /* card specific hook */
990 if(bttv_tvcards[btv->c.type].muxsel_hook)
991 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
995 static char *audio_modes[] = {
996 "audio: tuner", "audio: radio", "audio: extern",
997 "audio: intern", "audio: mute"
1001 audio_mux_gpio(struct bttv *btv, int input, int mute)
1003 int gpio_val, signal, mute_gpio;
1005 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1006 bttv_tvcards[btv->c.type].gpiomask);
1007 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1010 mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
1011 && !btv->has_radio_tuner);
1014 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1016 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1018 switch (btv->c.type) {
1019 case BTTV_BOARD_VOODOOTV_FM:
1020 case BTTV_BOARD_VOODOOTV_200:
1021 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1025 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1029 bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
1033 audio_mute(struct bttv *btv, int mute)
1035 struct v4l2_ctrl *ctrl;
1037 audio_mux_gpio(btv, btv->audio_input, mute);
1039 if (btv->sd_msp34xx) {
1040 ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1042 v4l2_ctrl_s_ctrl(ctrl, mute);
1044 if (btv->sd_tvaudio) {
1045 ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1047 v4l2_ctrl_s_ctrl(ctrl, mute);
1049 if (btv->sd_tda7432) {
1050 ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1052 v4l2_ctrl_s_ctrl(ctrl, mute);
1058 audio_input(struct bttv *btv, int input)
1060 audio_mux_gpio(btv, input, btv->mute);
1062 if (btv->sd_msp34xx) {
1065 /* Note: the inputs tuner/radio/extern/intern are translated
1066 to msp routings. This assumes common behavior for all msp3400
1067 based TV cards. When this assumption fails, then the
1068 specific MSP routing must be added to the card table.
1069 For now this is sufficient. */
1071 case TVAUDIO_INPUT_RADIO:
1072 /* Some boards need the msp do to the radio demod */
1073 if (btv->radio_uses_msp_demodulator) {
1074 in = MSP_INPUT_DEFAULT;
1077 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1078 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1080 case TVAUDIO_INPUT_EXTERN:
1081 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1082 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1084 case TVAUDIO_INPUT_INTERN:
1085 /* Yes, this is the same input as for RADIO. I doubt
1086 if this is ever used. The only board with an INTERN
1087 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1088 that was tested. My guess is that the whole INTERN
1089 input does not work. */
1090 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1091 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1093 case TVAUDIO_INPUT_TUNER:
1095 /* This is the only card that uses TUNER2, and afaik,
1096 is the only difference between the VOODOOTV_FM
1098 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1099 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1100 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1102 in = MSP_INPUT_DEFAULT;
1105 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1106 in, MSP_OUTPUT_DEFAULT, 0);
1108 if (btv->sd_tvaudio) {
1109 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1116 bttv_crop_calc_limits(struct bttv_crop *c)
1118 /* Scale factor min. 1:1, max. 16:1. Min. image size
1119 48 x 32. Scaled width must be a multiple of 4. */
1122 /* For bug compatibility with VIDIOCGCAP and image
1123 size checks in earlier driver versions. */
1124 c->min_scaled_width = 48;
1125 c->min_scaled_height = 32;
1127 c->min_scaled_width =
1128 (max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
1129 c->min_scaled_height =
1130 max_t(unsigned int, 32, c->rect.height >> 4);
1133 c->max_scaled_width = c->rect.width & ~3;
1134 c->max_scaled_height = c->rect.height;
1138 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1140 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1141 bttv_crop_calc_limits(c);
1144 /* Call with btv->lock down. */
1146 set_tvnorm(struct bttv *btv, unsigned int norm)
1148 const struct bttv_tvnorm *tvnorm;
1151 BUG_ON(norm >= BTTV_TVNORMS);
1152 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
1154 tvnorm = &bttv_tvnorms[norm];
1156 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1157 sizeof (tvnorm->cropcap))) {
1158 bttv_crop_reset(&btv->crop[0], norm);
1159 btv->crop[1] = btv->crop[0]; /* current = default */
1161 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1162 btv->crop_start = tvnorm->cropcap.bounds.top
1163 + tvnorm->cropcap.bounds.height;
1169 btwrite(tvnorm->adelay, BT848_ADELAY);
1170 btwrite(tvnorm->bdelay, BT848_BDELAY);
1171 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1173 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1174 btwrite(1, BT848_VBI_PACK_DEL);
1175 bt848A_set_timing(btv);
1177 switch (btv->c.type) {
1178 case BTTV_BOARD_VOODOOTV_FM:
1179 case BTTV_BOARD_VOODOOTV_200:
1180 bttv_tda9880_setnorm(btv, gpio_read());
1183 id = tvnorm->v4l2_id;
1184 bttv_call_all(btv, video, s_std, id);
1189 /* Call with btv->lock down. */
1191 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1193 unsigned long flags;
1197 spin_lock_irqsave(&btv->s_lock,flags);
1198 if (btv->curr.frame_irq) {
1199 /* active capture -> delayed input switch */
1200 btv->new_input = input;
1202 video_mux(btv,input);
1204 spin_unlock_irqrestore(&btv->s_lock,flags);
1206 video_mux(btv,input);
1208 btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1209 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
1210 audio_input(btv, btv->audio_input);
1211 set_tvnorm(btv, norm);
1214 static void init_irqreg(struct bttv *btv)
1217 btwrite(0xfffffUL, BT848_INT_STAT);
1219 if (bttv_tvcards[btv->c.type].no_video) {
1221 btwrite(BT848_INT_I2CDONE,
1225 btwrite((btv->triton1) |
1226 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1228 (fdsr ? BT848_INT_FDSR : 0) |
1229 BT848_INT_RISCI | BT848_INT_OCERR |
1230 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1236 static void init_bt848(struct bttv *btv)
1238 if (bttv_tvcards[btv->c.type].no_video) {
1239 /* very basic init only */
1244 btwrite(0x00, BT848_CAP_CTL);
1245 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1246 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1248 /* set planar and packed mode trigger points and */
1249 /* set rising edge of inverted GPINTR pin as irq trigger */
1250 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1251 BT848_GPIO_DMA_CTL_PLTP1_16|
1252 BT848_GPIO_DMA_CTL_PLTP23_16|
1253 BT848_GPIO_DMA_CTL_GPINTC|
1254 BT848_GPIO_DMA_CTL_GPINTI,
1255 BT848_GPIO_DMA_CTL);
1257 btwrite(0x20, BT848_E_VSCALE_HI);
1258 btwrite(0x20, BT848_O_VSCALE_HI);
1260 v4l2_ctrl_handler_setup(&btv->ctrl_handler);
1266 static void bttv_reinit_bt848(struct bttv *btv)
1268 unsigned long flags;
1271 pr_info("%d: reset, reinitialize\n", btv->c.nr);
1272 spin_lock_irqsave(&btv->s_lock,flags);
1274 bttv_set_dma(btv,0);
1275 spin_unlock_irqrestore(&btv->s_lock,flags);
1278 btv->pll.pll_current = -1;
1279 set_input(btv, btv->input, btv->tvnorm);
1282 static int bttv_s_ctrl(struct v4l2_ctrl *c)
1284 struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
1288 case V4L2_CID_BRIGHTNESS:
1289 bt848_bright(btv, c->val);
1292 bt848_hue(btv, c->val);
1294 case V4L2_CID_CONTRAST:
1295 bt848_contrast(btv, c->val);
1297 case V4L2_CID_SATURATION:
1298 bt848_sat(btv, c->val);
1300 case V4L2_CID_COLOR_KILLER:
1302 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1303 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1305 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1306 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1309 case V4L2_CID_AUDIO_MUTE:
1310 audio_mute(btv, c->val);
1313 case V4L2_CID_AUDIO_VOLUME:
1314 btv->volume_gpio(btv, c->val);
1317 case V4L2_CID_CHROMA_AGC:
1318 val = c->val ? BT848_SCLOOP_CAGC : 0;
1319 btwrite(val, BT848_E_SCLOOP);
1320 btwrite(val, BT848_O_SCLOOP);
1322 case V4L2_CID_PRIVATE_COMBFILTER:
1323 btv->opt_combfilter = c->val;
1325 case V4L2_CID_PRIVATE_LUMAFILTER:
1327 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1328 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1330 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1331 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1334 case V4L2_CID_PRIVATE_AUTOMUTE:
1335 btv->opt_automute = c->val;
1337 case V4L2_CID_PRIVATE_AGC_CRUSH:
1338 btwrite(BT848_ADC_RESERVED |
1339 (c->val ? BT848_ADC_CRUSH : 0),
1342 case V4L2_CID_PRIVATE_VCR_HACK:
1343 btv->opt_vcr_hack = c->val;
1345 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1346 btwrite(c->val, BT848_WC_UP);
1348 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1349 btwrite(c->val, BT848_WC_DOWN);
1351 case V4L2_CID_PRIVATE_UV_RATIO:
1352 btv->opt_uv_ratio = c->val;
1353 bt848_sat(btv, btv->saturation);
1355 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1356 btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
1358 case V4L2_CID_PRIVATE_CORING:
1359 btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
1367 /* ----------------------------------------------------------------------- */
1369 static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
1370 .s_ctrl = bttv_s_ctrl,
1373 static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
1374 .ops = &bttv_ctrl_ops,
1375 .id = V4L2_CID_PRIVATE_COMBFILTER,
1376 .name = "Comb Filter",
1377 .type = V4L2_CTRL_TYPE_BOOLEAN,
1384 static struct v4l2_ctrl_config bttv_ctrl_automute = {
1385 .ops = &bttv_ctrl_ops,
1386 .id = V4L2_CID_PRIVATE_AUTOMUTE,
1387 .name = "Auto Mute",
1388 .type = V4L2_CTRL_TYPE_BOOLEAN,
1395 static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
1396 .ops = &bttv_ctrl_ops,
1397 .id = V4L2_CID_PRIVATE_LUMAFILTER,
1398 .name = "Luma Decimation Filter",
1399 .type = V4L2_CTRL_TYPE_BOOLEAN,
1406 static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
1407 .ops = &bttv_ctrl_ops,
1408 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
1409 .name = "AGC Crush",
1410 .type = V4L2_CTRL_TYPE_BOOLEAN,
1417 static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
1418 .ops = &bttv_ctrl_ops,
1419 .id = V4L2_CID_PRIVATE_VCR_HACK,
1421 .type = V4L2_CTRL_TYPE_BOOLEAN,
1428 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
1429 .ops = &bttv_ctrl_ops,
1430 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
1431 .name = "Whitecrush Lower",
1432 .type = V4L2_CTRL_TYPE_INTEGER,
1439 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
1440 .ops = &bttv_ctrl_ops,
1441 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
1442 .name = "Whitecrush Upper",
1443 .type = V4L2_CTRL_TYPE_INTEGER,
1450 static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
1451 .ops = &bttv_ctrl_ops,
1452 .id = V4L2_CID_PRIVATE_UV_RATIO,
1454 .type = V4L2_CTRL_TYPE_INTEGER,
1461 static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
1462 .ops = &bttv_ctrl_ops,
1463 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
1464 .name = "Full Luma Range",
1465 .type = V4L2_CTRL_TYPE_BOOLEAN,
1471 static struct v4l2_ctrl_config bttv_ctrl_coring = {
1472 .ops = &bttv_ctrl_ops,
1473 .id = V4L2_CID_PRIVATE_CORING,
1475 .type = V4L2_CTRL_TYPE_INTEGER,
1482 /* ----------------------------------------------------------------------- */
1484 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1486 unsigned int outbits, data;
1487 outbits = btread(BT848_GPIO_OUT_EN);
1488 data = btread(BT848_GPIO_DATA);
1489 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1490 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
1493 static void bttv_field_count(struct bttv *btv)
1501 /* start field counter */
1502 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1504 /* stop field counter */
1505 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1506 btv->field_count = 0;
1510 static const struct bttv_format*
1511 format_by_fourcc(int fourcc)
1515 for (i = 0; i < FORMATS; i++) {
1516 if (-1 == formats[i].fourcc)
1518 if (formats[i].fourcc == fourcc)
1524 /* ----------------------------------------------------------------------- */
1528 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1529 struct bttv_buffer *new)
1531 struct bttv_buffer *old;
1532 unsigned long flags;
1534 dprintk("switch_overlay: enter [new=%p]\n", new);
1536 new->vb.state = VIDEOBUF_DONE;
1537 spin_lock_irqsave(&btv->s_lock,flags);
1541 bttv_set_dma(btv, 0x03);
1542 spin_unlock_irqrestore(&btv->s_lock,flags);
1544 dprintk("switch_overlay: old=%p state is %d\n",
1545 old, old->vb.state);
1546 bttv_dma_free(&fh->cap,btv, old);
1550 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
1551 dprintk("switch_overlay: done\n");
1555 /* ----------------------------------------------------------------------- */
1556 /* video4linux (1) interface */
1558 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1559 struct bttv_buffer *buf,
1560 const struct bttv_format *fmt,
1561 unsigned int width, unsigned int height,
1562 enum v4l2_field field)
1564 struct bttv_fh *fh = q->priv_data;
1565 int redo_dma_risc = 0;
1570 /* check settings */
1573 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1575 height = RAW_LINES*2;
1576 if (width*height > buf->vb.bsize)
1578 buf->vb.size = buf->vb.bsize;
1580 /* Make sure tvnorm and vbi_end remain consistent
1581 until we're done. */
1585 /* In this mode capturing always starts at defrect.top
1586 (default VDELAY), ignoring cropping parameters. */
1587 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1591 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1594 c = btv->crop[!!fh->do_crop];
1596 if (width < c.min_scaled_width ||
1597 width > c.max_scaled_width ||
1598 height < c.min_scaled_height)
1602 case V4L2_FIELD_TOP:
1603 case V4L2_FIELD_BOTTOM:
1604 case V4L2_FIELD_ALTERNATE:
1605 /* btv->crop counts frame lines. Max. scale
1606 factor is 16:1 for frames, 8:1 for fields. */
1607 if (height * 2 > c.max_scaled_height)
1612 if (height > c.max_scaled_height)
1617 buf->vb.size = (width * height * fmt->depth) >> 3;
1618 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1622 /* alloc + fill struct bttv_buffer (if changed) */
1623 if (buf->vb.width != width || buf->vb.height != height ||
1624 buf->vb.field != field ||
1625 buf->tvnorm != norm || buf->fmt != fmt ||
1626 buf->crop.top != c.rect.top ||
1627 buf->crop.left != c.rect.left ||
1628 buf->crop.width != c.rect.width ||
1629 buf->crop.height != c.rect.height) {
1630 buf->vb.width = width;
1631 buf->vb.height = height;
1632 buf->vb.field = field;
1639 /* alloc risc memory */
1640 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1642 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1647 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1650 buf->vb.state = VIDEOBUF_PREPARED;
1654 bttv_dma_free(q,btv,buf);
1659 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1661 struct bttv_fh *fh = q->priv_data;
1663 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1666 if (*size * *count > gbuffers * gbufsize)
1667 *count = (gbuffers * gbufsize) / *size;
1672 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1673 enum v4l2_field field)
1675 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1676 struct bttv_fh *fh = q->priv_data;
1678 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1679 fh->width, fh->height, field);
1683 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1685 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1686 struct bttv_fh *fh = q->priv_data;
1687 struct bttv *btv = fh->btv;
1689 buf->vb.state = VIDEOBUF_QUEUED;
1690 list_add_tail(&buf->vb.queue,&btv->capture);
1691 if (!btv->curr.frame_irq) {
1693 bttv_set_dma(btv, 0x03);
1697 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1699 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1700 struct bttv_fh *fh = q->priv_data;
1702 bttv_dma_free(q,fh->btv,buf);
1705 static const struct videobuf_queue_ops bttv_video_qops = {
1706 .buf_setup = buffer_setup,
1707 .buf_prepare = buffer_prepare,
1708 .buf_queue = buffer_queue,
1709 .buf_release = buffer_release,
1712 static void radio_enable(struct bttv *btv)
1714 /* Switch to the radio tuner */
1715 if (!btv->has_radio_tuner) {
1716 btv->has_radio_tuner = 1;
1717 bttv_call_all(btv, tuner, s_radio);
1718 btv->audio_input = TVAUDIO_INPUT_RADIO;
1719 audio_input(btv, btv->audio_input);
1723 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
1725 struct bttv_fh *fh = priv;
1726 struct bttv *btv = fh->btv;
1729 for (i = 0; i < BTTV_TVNORMS; i++)
1730 if (id & bttv_tvnorms[i].v4l2_id)
1732 if (i == BTTV_TVNORMS)
1739 static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
1741 struct bttv_fh *fh = priv;
1742 struct bttv *btv = fh->btv;
1748 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1750 struct bttv_fh *fh = f;
1751 struct bttv *btv = fh->btv;
1753 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1754 *id &= V4L2_STD_625_50;
1756 *id &= V4L2_STD_525_60;
1760 static int bttv_enum_input(struct file *file, void *priv,
1761 struct v4l2_input *i)
1763 struct bttv_fh *fh = priv;
1764 struct bttv *btv = fh->btv;
1766 if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
1769 i->type = V4L2_INPUT_TYPE_CAMERA;
1772 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1773 sprintf(i->name, "Television");
1774 i->type = V4L2_INPUT_TYPE_TUNER;
1776 } else if (i->index == btv->svhs) {
1777 sprintf(i->name, "S-Video");
1779 sprintf(i->name, "Composite%d", i->index);
1782 if (i->index == btv->input) {
1783 __u32 dstatus = btread(BT848_DSTATUS);
1784 if (0 == (dstatus & BT848_DSTATUS_PRES))
1785 i->status |= V4L2_IN_ST_NO_SIGNAL;
1786 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1787 i->status |= V4L2_IN_ST_NO_H_LOCK;
1790 i->std = BTTV_NORMS;
1794 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1796 struct bttv_fh *fh = priv;
1797 struct bttv *btv = fh->btv;
1804 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1806 struct bttv_fh *fh = priv;
1807 struct bttv *btv = fh->btv;
1809 if (i >= bttv_tvcards[btv->c.type].video_inputs)
1812 set_input(btv, i, btv->tvnorm);
1816 static int bttv_s_tuner(struct file *file, void *priv,
1817 const struct v4l2_tuner *t)
1819 struct bttv_fh *fh = priv;
1820 struct bttv *btv = fh->btv;
1825 bttv_call_all(btv, tuner, s_tuner, t);
1827 if (btv->audio_mode_gpio) {
1828 struct v4l2_tuner copy = *t;
1830 btv->audio_mode_gpio(btv, ©, 1);
1835 static int bttv_g_frequency(struct file *file, void *priv,
1836 struct v4l2_frequency *f)
1838 struct bttv_fh *fh = priv;
1839 struct bttv *btv = fh->btv;
1844 if (f->type == V4L2_TUNER_RADIO)
1846 f->frequency = f->type == V4L2_TUNER_RADIO ?
1847 btv->radio_freq : btv->tv_freq;
1852 static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
1854 struct v4l2_frequency new_freq = *f;
1856 bttv_call_all(btv, tuner, s_frequency, f);
1857 /* s_frequency may clamp the frequency, so get the actual
1858 frequency before assigning radio/tv_freq. */
1859 bttv_call_all(btv, tuner, g_frequency, &new_freq);
1860 if (new_freq.type == V4L2_TUNER_RADIO) {
1862 btv->radio_freq = new_freq.frequency;
1863 if (btv->has_tea575x) {
1864 btv->tea.freq = btv->radio_freq;
1865 snd_tea575x_set_freq(&btv->tea);
1868 btv->tv_freq = new_freq.frequency;
1872 static int bttv_s_frequency(struct file *file, void *priv,
1873 const struct v4l2_frequency *f)
1875 struct bttv_fh *fh = priv;
1876 struct bttv *btv = fh->btv;
1881 bttv_set_frequency(btv, f);
1885 static int bttv_log_status(struct file *file, void *f)
1887 struct video_device *vdev = video_devdata(file);
1888 struct bttv_fh *fh = f;
1889 struct bttv *btv = fh->btv;
1891 v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
1892 bttv_call_all(btv, core, log_status);
1896 #ifdef CONFIG_VIDEO_ADV_DEBUG
1897 static int bttv_g_register(struct file *file, void *f,
1898 struct v4l2_dbg_register *reg)
1900 struct bttv_fh *fh = f;
1901 struct bttv *btv = fh->btv;
1903 /* bt848 has a 12-bit register space */
1905 reg->val = btread(reg->reg);
1911 static int bttv_s_register(struct file *file, void *f,
1912 const struct v4l2_dbg_register *reg)
1914 struct bttv_fh *fh = f;
1915 struct bttv *btv = fh->btv;
1917 /* bt848 has a 12-bit register space */
1918 btwrite(reg->val, reg->reg & 0xfff);
1924 /* Given cropping boundaries b and the scaled width and height of a
1925 single field or frame, which must not exceed hardware limits, this
1926 function adjusts the cropping parameters c. */
1928 bttv_crop_adjust (struct bttv_crop * c,
1929 const struct v4l2_rect * b,
1932 enum v4l2_field field)
1934 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
1938 if (width < c->min_scaled_width) {
1939 /* Max. hor. scale factor 16:1. */
1940 c->rect.width = width * 16;
1941 } else if (width > c->max_scaled_width) {
1942 /* Min. hor. scale factor 1:1. */
1943 c->rect.width = width;
1945 max_left = b->left + b->width - width;
1946 max_left = min(max_left, (__s32) MAX_HDELAY);
1947 if (c->rect.left > max_left)
1948 c->rect.left = max_left;
1951 if (height < c->min_scaled_height) {
1952 /* Max. vert. scale factor 16:1, single fields 8:1. */
1953 c->rect.height = height * 16;
1954 } else if (frame_height > c->max_scaled_height) {
1955 /* Min. vert. scale factor 1:1.
1956 Top and height count field lines times two. */
1957 c->rect.height = (frame_height + 1) & ~1;
1959 max_top = b->top + b->height - c->rect.height;
1960 if (c->rect.top > max_top)
1961 c->rect.top = max_top;
1964 bttv_crop_calc_limits(c);
1967 /* Returns an error if scaling to a frame or single field with the given
1968 width and height is not possible with the current cropping parameters
1969 and width aligned according to width_mask. If adjust_size is TRUE the
1970 function may adjust the width and/or height instead, rounding width
1971 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
1972 also adjust the current cropping parameters to get closer to the
1973 desired image size. */
1975 limit_scaled_size_lock (struct bttv_fh * fh,
1978 enum v4l2_field field,
1979 unsigned int width_mask,
1980 unsigned int width_bias,
1984 struct bttv *btv = fh->btv;
1985 const struct v4l2_rect *b;
1986 struct bttv_crop *c;
1993 BUG_ON((int) width_mask >= 0 ||
1994 width_bias >= (unsigned int) -width_mask);
1996 /* Make sure tvnorm, vbi_end and the current cropping parameters
1997 remain consistent until we're done. */
1999 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2001 /* Do crop - use current, don't - use default parameters. */
2002 c = &btv->crop[!!fh->do_crop];
2007 && !locked_btres(btv, VIDEO_RESOURCES)) {
2011 /* We cannot scale up. When the scaled image is larger
2012 than crop.rect we adjust the crop.rect as required
2013 by the V4L2 spec, hence cropcap.bounds are our limit. */
2014 max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
2015 max_height = b->height;
2017 /* We cannot capture the same line as video and VBI data.
2018 Note btv->vbi_end is really a minimum, see
2019 bttv_vbi_try_fmt(). */
2020 if (btv->vbi_end > b->top) {
2021 max_height -= btv->vbi_end - b->top;
2023 if (min_height > max_height)
2028 if (btv->vbi_end > c->rect.top)
2031 min_width = c->min_scaled_width;
2032 min_height = c->min_scaled_height;
2033 max_width = c->max_scaled_width;
2034 max_height = c->max_scaled_height;
2039 min_width = (min_width - width_mask - 1) & width_mask;
2040 max_width = max_width & width_mask;
2042 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2043 /* Min. scale factor is 1:1. */
2044 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2047 *width = clamp(*width, min_width, max_width);
2048 *height = clamp(*height, min_height, max_height);
2050 /* Round after clamping to avoid overflow. */
2051 *width = (*width + width_bias) & width_mask;
2054 bttv_crop_adjust(c, b, *width, *height, field);
2056 if (btv->vbi_end > c->rect.top) {
2057 /* Move the crop window out of the way. */
2058 c->rect.top = btv->vbi_end;
2063 if (*width < min_width ||
2064 *height < min_height ||
2065 *width > max_width ||
2066 *height > max_height ||
2067 0 != (*width & ~width_mask))
2071 rc = 0; /* success */
2078 /* Returns an error if the given overlay window dimensions are not
2079 possible with the current cropping parameters. If adjust_size is
2080 TRUE the function may adjust the window width and/or height
2081 instead, however it always rounds the horizontal position and
2082 width as btcx_align() does. If adjust_crop is TRUE the function
2083 may also adjust the current cropping parameters to get closer
2084 to the desired window size. */
2086 verify_window_lock(struct bttv_fh *fh, struct v4l2_window *win,
2087 int adjust_size, int adjust_crop)
2089 enum v4l2_field field;
2090 unsigned int width_mask;
2093 if (win->w.width < 48)
2095 if (win->w.height < 32)
2097 if (win->clipcount > 2048)
2098 win->clipcount = 2048;
2101 win->global_alpha = 0;
2105 case V4L2_FIELD_TOP:
2106 case V4L2_FIELD_BOTTOM:
2107 case V4L2_FIELD_INTERLACED:
2110 field = V4L2_FIELD_ANY;
2113 if (V4L2_FIELD_ANY == field) {
2116 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2117 field = (win->w.height > height2)
2118 ? V4L2_FIELD_INTERLACED
2123 if (NULL == fh->ovfmt)
2125 /* 4-byte alignment. */
2127 switch (fh->ovfmt->depth) {
2141 win->w.width -= win->w.left & ~width_mask;
2142 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2144 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
2146 /* width_bias: round down */ 0,
2147 adjust_size, adjust_crop);
2153 static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
2154 struct v4l2_window *win, int fixup)
2156 struct v4l2_clip *clips = NULL;
2157 int n,size,retval = 0;
2159 if (NULL == fh->ovfmt)
2161 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2163 retval = verify_window_lock(fh, win,
2164 /* adjust_size */ fixup,
2165 /* adjust_crop */ fixup);
2169 /* copy clips -- luckily v4l1 + v4l2 are binary
2170 compatible here ...*/
2172 size = sizeof(*clips)*(n+4);
2173 clips = kmalloc(size,GFP_KERNEL);
2177 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2183 /* clip against screen */
2184 if (NULL != btv->fbuf.base)
2185 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2187 btcx_sort_clips(clips,n);
2189 /* 4-byte alignments */
2190 switch (fh->ovfmt->depth) {
2193 btcx_align(&win->w, clips, n, 3);
2196 btcx_align(&win->w, clips, n, 1);
2199 /* no alignment fixups needed */
2205 kfree(fh->ov.clips);
2206 fh->ov.clips = clips;
2210 fh->ov.field = win->field;
2211 fh->ov.setup_ok = 1;
2213 btv->init.ov.w.width = win->w.width;
2214 btv->init.ov.w.height = win->w.height;
2215 btv->init.ov.field = win->field;
2217 /* update overlay if needed */
2219 if (check_btres(fh, RESOURCE_OVERLAY)) {
2220 struct bttv_buffer *new;
2222 new = videobuf_sg_alloc(sizeof(*new));
2223 new->crop = btv->crop[!!fh->do_crop].rect;
2224 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2225 retval = bttv_switch_overlay(btv,fh,new);
2230 /* ----------------------------------------------------------------------- */
2232 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2234 struct videobuf_queue* q = NULL;
2237 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2240 case V4L2_BUF_TYPE_VBI_CAPTURE:
2249 static int bttv_resource(struct bttv_fh *fh)
2254 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2255 res = RESOURCE_VIDEO_STREAM;
2257 case V4L2_BUF_TYPE_VBI_CAPTURE:
2266 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2268 struct videobuf_queue *q = bttv_queue(fh);
2269 int res = bttv_resource(fh);
2271 if (check_btres(fh,res))
2273 if (videobuf_queue_is_busy(q))
2280 pix_format_set_size (struct v4l2_pix_format * f,
2281 const struct bttv_format * fmt,
2283 unsigned int height)
2288 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2289 f->bytesperline = width; /* Y plane */
2290 f->sizeimage = (width * height * fmt->depth) >> 3;
2292 f->bytesperline = (width * fmt->depth) >> 3;
2293 f->sizeimage = height * f->bytesperline;
2297 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2298 struct v4l2_format *f)
2300 struct bttv_fh *fh = priv;
2302 pix_format_set_size(&f->fmt.pix, fh->fmt,
2303 fh->width, fh->height);
2304 f->fmt.pix.field = fh->cap.field;
2305 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2306 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2311 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2312 struct v4l2_format *f)
2314 struct bttv_fh *fh = priv;
2316 f->fmt.win.w = fh->ov.w;
2317 f->fmt.win.field = fh->ov.field;
2322 static void bttv_get_width_mask_vid_cap(const struct bttv_format *fmt,
2323 unsigned int *width_mask,
2324 unsigned int *width_bias)
2326 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2327 *width_mask = ~15; /* width must be a multiple of 16 pixels */
2328 *width_bias = 8; /* nearest */
2330 *width_mask = ~3; /* width must be a multiple of 4 pixels */
2331 *width_bias = 2; /* nearest */
2335 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2336 struct v4l2_format *f)
2338 const struct bttv_format *fmt;
2339 struct bttv_fh *fh = priv;
2340 struct bttv *btv = fh->btv;
2341 enum v4l2_field field;
2342 __s32 width, height;
2344 unsigned int width_mask, width_bias;
2347 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2351 field = f->fmt.pix.field;
2354 case V4L2_FIELD_TOP:
2355 case V4L2_FIELD_BOTTOM:
2356 case V4L2_FIELD_ALTERNATE:
2357 case V4L2_FIELD_INTERLACED:
2359 case V4L2_FIELD_SEQ_BT:
2360 case V4L2_FIELD_SEQ_TB:
2361 if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
2362 field = V4L2_FIELD_SEQ_TB;
2366 default: /* FIELD_ANY case */
2367 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2368 field = (f->fmt.pix.height > height2)
2369 ? V4L2_FIELD_INTERLACED
2370 : V4L2_FIELD_BOTTOM;
2374 width = f->fmt.pix.width;
2375 height = f->fmt.pix.height;
2377 bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2378 rc = limit_scaled_size_lock(fh, &width, &height, field,
2379 width_mask, width_bias,
2380 /* adjust_size */ 1,
2381 /* adjust_crop */ 0);
2385 /* update data for the application */
2386 f->fmt.pix.field = field;
2387 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2388 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2393 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2394 struct v4l2_format *f)
2396 struct bttv_fh *fh = priv;
2398 verify_window_lock(fh, &f->fmt.win,
2399 /* adjust_size */ 1,
2400 /* adjust_crop */ 0);
2404 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2405 struct v4l2_format *f)
2408 const struct bttv_format *fmt;
2409 struct bttv_fh *fh = priv;
2410 struct bttv *btv = fh->btv;
2411 __s32 width, height;
2412 unsigned int width_mask, width_bias;
2413 enum v4l2_field field;
2415 retval = bttv_switch_type(fh, f->type);
2419 retval = bttv_try_fmt_vid_cap(file, priv, f);
2423 width = f->fmt.pix.width;
2424 height = f->fmt.pix.height;
2425 field = f->fmt.pix.field;
2427 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2428 bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2429 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
2430 width_mask, width_bias,
2431 /* adjust_size */ 1,
2432 /* adjust_crop */ 1);
2436 f->fmt.pix.field = field;
2438 /* update our state information */
2440 fh->cap.field = f->fmt.pix.field;
2441 fh->cap.last = V4L2_FIELD_NONE;
2442 fh->width = f->fmt.pix.width;
2443 fh->height = f->fmt.pix.height;
2444 btv->init.fmt = fmt;
2445 btv->init.width = f->fmt.pix.width;
2446 btv->init.height = f->fmt.pix.height;
2451 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2452 struct v4l2_format *f)
2454 struct bttv_fh *fh = priv;
2455 struct bttv *btv = fh->btv;
2457 if (no_overlay > 0) {
2458 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2462 return setup_window_lock(fh, btv, &f->fmt.win, 1);
2465 static int bttv_querycap(struct file *file, void *priv,
2466 struct v4l2_capability *cap)
2468 struct video_device *vdev = video_devdata(file);
2469 struct bttv_fh *fh = priv;
2470 struct bttv *btv = fh->btv;
2475 strscpy(cap->driver, "bttv", sizeof(cap->driver));
2476 strscpy(cap->card, btv->video_dev.name, sizeof(cap->card));
2477 snprintf(cap->bus_info, sizeof(cap->bus_info),
2478 "PCI:%s", pci_name(btv->c.pci));
2480 V4L2_CAP_VIDEO_CAPTURE |
2481 V4L2_CAP_READWRITE |
2482 V4L2_CAP_STREAMING |
2483 V4L2_CAP_DEVICE_CAPS;
2484 if (no_overlay <= 0)
2485 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2486 if (video_is_registered(&btv->vbi_dev))
2487 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2488 if (video_is_registered(&btv->radio_dev))
2489 cap->capabilities |= V4L2_CAP_RADIO;
2492 * No need to lock here: those vars are initialized during board
2493 * probe and remains untouched during the rest of the driver lifecycle
2495 if (btv->has_saa6588)
2496 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2497 if (btv->tuner_type != TUNER_ABSENT)
2498 cap->capabilities |= V4L2_CAP_TUNER;
2499 if (vdev->vfl_type == VFL_TYPE_GRABBER)
2500 cap->device_caps = cap->capabilities &
2501 (V4L2_CAP_VIDEO_CAPTURE |
2502 V4L2_CAP_READWRITE |
2503 V4L2_CAP_STREAMING |
2504 V4L2_CAP_VIDEO_OVERLAY |
2506 else if (vdev->vfl_type == VFL_TYPE_VBI)
2507 cap->device_caps = cap->capabilities &
2508 (V4L2_CAP_VBI_CAPTURE |
2509 V4L2_CAP_READWRITE |
2510 V4L2_CAP_STREAMING |
2513 cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
2514 if (btv->has_saa6588)
2515 cap->device_caps |= V4L2_CAP_READWRITE |
2516 V4L2_CAP_RDS_CAPTURE;
2517 if (btv->has_tea575x)
2518 cap->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
2523 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2527 for (i = 0; i < FORMATS; i++) {
2528 if (formats[i].fourcc != -1)
2530 if ((unsigned int)index == f->index)
2536 f->pixelformat = formats[i].fourcc;
2537 strscpy(f->description, formats[i].name, sizeof(f->description));
2542 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2543 struct v4l2_fmtdesc *f)
2545 int rc = bttv_enum_fmt_cap_ovr(f);
2553 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
2554 struct v4l2_fmtdesc *f)
2558 if (no_overlay > 0) {
2559 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2563 rc = bttv_enum_fmt_cap_ovr(f);
2568 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
2574 static int bttv_g_fbuf(struct file *file, void *f,
2575 struct v4l2_framebuffer *fb)
2577 struct bttv_fh *fh = f;
2578 struct bttv *btv = fh->btv;
2581 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2582 fb->flags = V4L2_FBUF_FLAG_PRIMARY;
2584 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2588 static int bttv_overlay(struct file *file, void *f, unsigned int on)
2590 struct bttv_fh *fh = f;
2591 struct bttv *btv = fh->btv;
2592 struct bttv_buffer *new;
2597 if (unlikely(!btv->fbuf.base)) {
2600 if (unlikely(!fh->ov.setup_ok)) {
2601 dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
2608 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
2612 fh->ov.tvnorm = btv->tvnorm;
2613 new = videobuf_sg_alloc(sizeof(*new));
2614 new->crop = btv->crop[!!fh->do_crop].rect;
2615 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2621 retval = bttv_switch_overlay(btv, fh, new);
2625 static int bttv_s_fbuf(struct file *file, void *f,
2626 const struct v4l2_framebuffer *fb)
2628 struct bttv_fh *fh = f;
2629 struct bttv *btv = fh->btv;
2630 const struct bttv_format *fmt;
2633 if (!capable(CAP_SYS_ADMIN) &&
2634 !capable(CAP_SYS_RAWIO))
2638 fmt = format_by_fourcc(fb->fmt.pixelformat);
2641 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2645 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2646 __s32 width = fb->fmt.width;
2647 __s32 height = fb->fmt.height;
2649 retval = limit_scaled_size_lock(fh, &width, &height,
2650 V4L2_FIELD_INTERLACED,
2651 /* width_mask */ ~3,
2653 /* adjust_size */ 0,
2654 /* adjust_crop */ 0);
2660 btv->fbuf.base = fb->base;
2661 btv->fbuf.fmt.width = fb->fmt.width;
2662 btv->fbuf.fmt.height = fb->fmt.height;
2663 if (0 != fb->fmt.bytesperline)
2664 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2666 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2670 btv->init.ovfmt = fmt;
2671 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2674 fh->ov.w.width = fb->fmt.width;
2675 fh->ov.w.height = fb->fmt.height;
2676 btv->init.ov.w.width = fb->fmt.width;
2677 btv->init.ov.w.height = fb->fmt.height;
2679 kfree(fh->ov.clips);
2680 fh->ov.clips = NULL;
2683 if (check_btres(fh, RESOURCE_OVERLAY)) {
2684 struct bttv_buffer *new;
2686 new = videobuf_sg_alloc(sizeof(*new));
2687 new->crop = btv->crop[!!fh->do_crop].rect;
2688 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2689 retval = bttv_switch_overlay(btv, fh, new);
2695 static int bttv_reqbufs(struct file *file, void *priv,
2696 struct v4l2_requestbuffers *p)
2698 struct bttv_fh *fh = priv;
2699 return videobuf_reqbufs(bttv_queue(fh), p);
2702 static int bttv_querybuf(struct file *file, void *priv,
2703 struct v4l2_buffer *b)
2705 struct bttv_fh *fh = priv;
2706 return videobuf_querybuf(bttv_queue(fh), b);
2709 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2711 struct bttv_fh *fh = priv;
2712 struct bttv *btv = fh->btv;
2713 int res = bttv_resource(fh);
2715 if (!check_alloc_btres_lock(btv, fh, res))
2718 return videobuf_qbuf(bttv_queue(fh), b);
2721 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2723 struct bttv_fh *fh = priv;
2724 return videobuf_dqbuf(bttv_queue(fh), b,
2725 file->f_flags & O_NONBLOCK);
2728 static int bttv_streamon(struct file *file, void *priv,
2729 enum v4l2_buf_type type)
2731 struct bttv_fh *fh = priv;
2732 struct bttv *btv = fh->btv;
2733 int res = bttv_resource(fh);
2735 if (!check_alloc_btres_lock(btv, fh, res))
2737 return videobuf_streamon(bttv_queue(fh));
2741 static int bttv_streamoff(struct file *file, void *priv,
2742 enum v4l2_buf_type type)
2744 struct bttv_fh *fh = priv;
2745 struct bttv *btv = fh->btv;
2747 int res = bttv_resource(fh);
2750 retval = videobuf_streamoff(bttv_queue(fh));
2753 free_btres_lock(btv, fh, res);
2757 static int bttv_g_parm(struct file *file, void *f,
2758 struct v4l2_streamparm *parm)
2760 struct bttv_fh *fh = f;
2761 struct bttv *btv = fh->btv;
2763 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2765 parm->parm.capture.readbuffers = gbuffers;
2766 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2767 &parm->parm.capture.timeperframe);
2772 static int bttv_g_tuner(struct file *file, void *priv,
2773 struct v4l2_tuner *t)
2775 struct bttv_fh *fh = priv;
2776 struct bttv *btv = fh->btv;
2781 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2782 t->capability = V4L2_TUNER_CAP_NORM;
2783 bttv_call_all(btv, tuner, g_tuner, t);
2784 strscpy(t->name, "Television", sizeof(t->name));
2785 t->type = V4L2_TUNER_ANALOG_TV;
2786 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2789 if (btv->audio_mode_gpio)
2790 btv->audio_mode_gpio(btv, t, 0);
2795 static int bttv_g_pixelaspect(struct file *file, void *priv,
2796 int type, struct v4l2_fract *f)
2798 struct bttv_fh *fh = priv;
2799 struct bttv *btv = fh->btv;
2801 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2804 /* defrect and bounds are set via g_selection */
2805 *f = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
2809 static int bttv_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
2811 struct bttv_fh *fh = f;
2812 struct bttv *btv = fh->btv;
2814 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2815 sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2818 switch (sel->target) {
2819 case V4L2_SEL_TGT_CROP:
2821 * No fh->do_crop = 1; because btv->crop[1] may be
2822 * inconsistent with fh->width or fh->height and apps
2823 * do not expect a change here.
2825 sel->r = btv->crop[!!fh->do_crop].rect;
2827 case V4L2_SEL_TGT_CROP_DEFAULT:
2828 sel->r = bttv_tvnorms[btv->tvnorm].cropcap.defrect;
2830 case V4L2_SEL_TGT_CROP_BOUNDS:
2831 sel->r = bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2840 static int bttv_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
2842 struct bttv_fh *fh = f;
2843 struct bttv *btv = fh->btv;
2844 const struct v4l2_rect *b;
2852 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2853 sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2856 if (sel->target != V4L2_SEL_TGT_CROP)
2859 /* Make sure tvnorm, vbi_end and the current cropping
2860 parameters remain consistent until we're done. Note
2861 read() may change vbi_end in check_alloc_btres_lock(). */
2864 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
2868 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2871 b_right = b_left + b->width;
2872 b_bottom = b->top + b->height;
2874 b_top = max(b->top, btv->vbi_end);
2875 if (b_top + 32 >= b_bottom) {
2879 /* Min. scaled size 48 x 32. */
2880 c.rect.left = clamp_t(s32, sel->r.left, b_left, b_right - 48);
2881 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
2883 c.rect.width = clamp_t(s32, sel->r.width,
2884 48, b_right - c.rect.left);
2886 c.rect.top = clamp_t(s32, sel->r.top, b_top, b_bottom - 32);
2887 /* Top and height must be a multiple of two. */
2888 c.rect.top = (c.rect.top + 1) & ~1;
2890 c.rect.height = clamp_t(s32, sel->r.height,
2891 32, b_bottom - c.rect.top);
2892 c.rect.height = (c.rect.height + 1) & ~1;
2894 bttv_crop_calc_limits(&c);
2902 if (fh->width < c.min_scaled_width) {
2903 fh->width = c.min_scaled_width;
2904 btv->init.width = c.min_scaled_width;
2905 } else if (fh->width > c.max_scaled_width) {
2906 fh->width = c.max_scaled_width;
2907 btv->init.width = c.max_scaled_width;
2910 if (fh->height < c.min_scaled_height) {
2911 fh->height = c.min_scaled_height;
2912 btv->init.height = c.min_scaled_height;
2913 } else if (fh->height > c.max_scaled_height) {
2914 fh->height = c.max_scaled_height;
2915 btv->init.height = c.max_scaled_height;
2921 static ssize_t bttv_read(struct file *file, char __user *data,
2922 size_t count, loff_t *ppos)
2924 struct bttv_fh *fh = file->private_data;
2927 if (fh->btv->errors)
2928 bttv_reinit_bt848(fh->btv);
2929 dprintk("%d: read count=%d type=%s\n",
2930 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
2933 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2934 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
2935 /* VIDEO_READ in use by another fh,
2936 or VIDEO_STREAM by any fh. */
2939 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2940 file->f_flags & O_NONBLOCK);
2941 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
2943 case V4L2_BUF_TYPE_VBI_CAPTURE:
2944 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
2946 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2947 file->f_flags & O_NONBLOCK);
2955 static __poll_t bttv_poll(struct file *file, poll_table *wait)
2957 struct bttv_fh *fh = file->private_data;
2958 struct bttv_buffer *buf;
2959 enum v4l2_field field;
2961 __poll_t req_events = poll_requested_events(wait);
2963 if (v4l2_event_pending(&fh->fh))
2965 else if (req_events & EPOLLPRI)
2966 poll_wait(file, &fh->fh.wait, wait);
2968 if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
2971 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2972 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
2973 return rc | EPOLLERR;
2974 return rc | videobuf_poll_stream(file, &fh->vbi, wait);
2977 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
2978 /* streaming capture */
2979 if (list_empty(&fh->cap.stream))
2980 return rc | EPOLLERR;
2981 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2983 /* read() capture */
2984 if (NULL == fh->cap.read_buf) {
2985 /* need to capture a new frame */
2986 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
2987 return rc | EPOLLERR;
2988 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
2989 if (NULL == fh->cap.read_buf)
2990 return rc | EPOLLERR;
2991 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2992 field = videobuf_next_field(&fh->cap);
2993 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2994 kfree (fh->cap.read_buf);
2995 fh->cap.read_buf = NULL;
2996 return rc | EPOLLERR;
2998 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2999 fh->cap.read_off = 0;
3001 buf = (struct bttv_buffer*)fh->cap.read_buf;
3004 poll_wait(file, &buf->vb.done, wait);
3005 if (buf->vb.state == VIDEOBUF_DONE ||
3006 buf->vb.state == VIDEOBUF_ERROR)
3007 rc = rc | EPOLLIN|EPOLLRDNORM;
3011 static int bttv_open(struct file *file)
3013 struct video_device *vdev = video_devdata(file);
3014 struct bttv *btv = video_drvdata(file);
3016 enum v4l2_buf_type type = 0;
3018 dprintk("open dev=%s\n", video_device_node_name(vdev));
3020 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
3021 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3022 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
3023 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3029 dprintk("%d: open called (type=%s)\n",
3030 btv->c.nr, v4l2_type_names[type]);
3032 /* allocate per filehandle data */
3033 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3037 file->private_data = fh;
3040 v4l2_fh_init(&fh->fh, vdev);
3043 fh->ov.setup_ok = 0;
3045 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3046 &btv->c.pci->dev, &btv->s_lock,
3047 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3048 V4L2_FIELD_INTERLACED,
3049 sizeof(struct bttv_buffer),
3051 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3052 &btv->c.pci->dev, &btv->s_lock,
3053 V4L2_BUF_TYPE_VBI_CAPTURE,
3055 sizeof(struct bttv_buffer),
3057 set_tvnorm(btv,btv->tvnorm);
3058 set_input(btv, btv->input, btv->tvnorm);
3059 audio_mute(btv, btv->mute);
3061 /* The V4L2 spec requires one global set of cropping parameters
3062 which only change on request. These are stored in btv->crop[1].
3063 However for compatibility with V4L apps and cropping unaware
3064 V4L2 apps we now reset the cropping parameters as seen through
3065 this fh, which is to say VIDIOC_G_SELECTION and scaling limit checks
3066 will use btv->crop[0], the default cropping parameters for the
3067 current video standard, and VIDIOC_S_FMT will not implicitly
3068 change the cropping parameters until VIDIOC_S_SELECTION has been
3070 fh->do_crop = !reset_crop; /* module parameter */
3072 /* Likewise there should be one global set of VBI capture
3073 parameters, but for compatibility with V4L apps and earlier
3074 driver versions each fh has its own parameters. */
3075 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3077 bttv_field_count(btv);
3078 v4l2_fh_add(&fh->fh);
3082 static int bttv_release(struct file *file)
3084 struct bttv_fh *fh = file->private_data;
3085 struct bttv *btv = fh->btv;
3087 /* turn off overlay */
3088 if (check_btres(fh, RESOURCE_OVERLAY))
3089 bttv_switch_overlay(btv,fh,NULL);
3091 /* stop video capture */
3092 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3093 videobuf_streamoff(&fh->cap);
3094 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
3096 if (fh->cap.read_buf) {
3097 buffer_release(&fh->cap,fh->cap.read_buf);
3098 kfree(fh->cap.read_buf);
3100 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3101 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
3104 /* stop vbi capture */
3105 if (check_btres(fh, RESOURCE_VBI)) {
3106 videobuf_stop(&fh->vbi);
3107 free_btres_lock(btv,fh,RESOURCE_VBI);
3112 videobuf_mmap_free(&fh->cap);
3113 videobuf_mmap_free(&fh->vbi);
3114 file->private_data = NULL;
3117 bttv_field_count(btv);
3120 audio_mute(btv, btv->mute);
3122 v4l2_fh_del(&fh->fh);
3123 v4l2_fh_exit(&fh->fh);
3129 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3131 struct bttv_fh *fh = file->private_data;
3133 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
3134 fh->btv->c.nr, v4l2_type_names[fh->type],
3135 vma->vm_start, vma->vm_end - vma->vm_start);
3136 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3139 static const struct v4l2_file_operations bttv_fops =
3141 .owner = THIS_MODULE,
3143 .release = bttv_release,
3144 .unlocked_ioctl = video_ioctl2,
3150 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3151 .vidioc_querycap = bttv_querycap,
3152 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3153 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3154 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3155 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3156 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3157 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3158 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3159 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
3160 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3161 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3162 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
3163 .vidioc_g_pixelaspect = bttv_g_pixelaspect,
3164 .vidioc_reqbufs = bttv_reqbufs,
3165 .vidioc_querybuf = bttv_querybuf,
3166 .vidioc_qbuf = bttv_qbuf,
3167 .vidioc_dqbuf = bttv_dqbuf,
3168 .vidioc_s_std = bttv_s_std,
3169 .vidioc_g_std = bttv_g_std,
3170 .vidioc_enum_input = bttv_enum_input,
3171 .vidioc_g_input = bttv_g_input,
3172 .vidioc_s_input = bttv_s_input,
3173 .vidioc_streamon = bttv_streamon,
3174 .vidioc_streamoff = bttv_streamoff,
3175 .vidioc_g_tuner = bttv_g_tuner,
3176 .vidioc_s_tuner = bttv_s_tuner,
3177 .vidioc_g_selection = bttv_g_selection,
3178 .vidioc_s_selection = bttv_s_selection,
3179 .vidioc_g_fbuf = bttv_g_fbuf,
3180 .vidioc_s_fbuf = bttv_s_fbuf,
3181 .vidioc_overlay = bttv_overlay,
3182 .vidioc_g_parm = bttv_g_parm,
3183 .vidioc_g_frequency = bttv_g_frequency,
3184 .vidioc_s_frequency = bttv_s_frequency,
3185 .vidioc_log_status = bttv_log_status,
3186 .vidioc_querystd = bttv_querystd,
3187 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
3188 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
3189 #ifdef CONFIG_VIDEO_ADV_DEBUG
3190 .vidioc_g_register = bttv_g_register,
3191 .vidioc_s_register = bttv_s_register,
3195 static struct video_device bttv_video_template = {
3197 .ioctl_ops = &bttv_ioctl_ops,
3198 .tvnorms = BTTV_NORMS,
3201 /* ----------------------------------------------------------------------- */
3202 /* radio interface */
3204 static int radio_open(struct file *file)
3206 struct video_device *vdev = video_devdata(file);
3207 struct bttv *btv = video_drvdata(file);
3210 dprintk("open dev=%s\n", video_device_node_name(vdev));
3212 dprintk("%d: open called (radio)\n", btv->c.nr);
3214 /* allocate per filehandle data */
3215 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3218 file->private_data = fh;
3220 v4l2_fh_init(&fh->fh, vdev);
3223 audio_mute(btv, btv->mute);
3225 v4l2_fh_add(&fh->fh);
3230 static int radio_release(struct file *file)
3232 struct bttv_fh *fh = file->private_data;
3233 struct bttv *btv = fh->btv;
3234 struct saa6588_command cmd;
3236 file->private_data = NULL;
3237 v4l2_fh_del(&fh->fh);
3238 v4l2_fh_exit(&fh->fh);
3243 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
3245 if (btv->radio_user == 0)
3246 btv->has_radio_tuner = 0;
3250 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3252 struct bttv_fh *fh = priv;
3253 struct bttv *btv = fh->btv;
3257 strscpy(t->name, "Radio", sizeof(t->name));
3258 t->type = V4L2_TUNER_RADIO;
3261 bttv_call_all(btv, tuner, g_tuner, t);
3263 if (btv->audio_mode_gpio)
3264 btv->audio_mode_gpio(btv, t, 0);
3266 if (btv->has_tea575x)
3267 return snd_tea575x_g_tuner(&btv->tea, t);
3272 static int radio_s_tuner(struct file *file, void *priv,
3273 const struct v4l2_tuner *t)
3275 struct bttv_fh *fh = priv;
3276 struct bttv *btv = fh->btv;
3282 bttv_call_all(btv, tuner, s_tuner, t);
3286 static int radio_s_hw_freq_seek(struct file *file, void *priv,
3287 const struct v4l2_hw_freq_seek *a)
3289 struct bttv_fh *fh = priv;
3290 struct bttv *btv = fh->btv;
3292 if (btv->has_tea575x)
3293 return snd_tea575x_s_hw_freq_seek(file, &btv->tea, a);
3298 static int radio_enum_freq_bands(struct file *file, void *priv,
3299 struct v4l2_frequency_band *band)
3301 struct bttv_fh *fh = priv;
3302 struct bttv *btv = fh->btv;
3304 if (btv->has_tea575x)
3305 return snd_tea575x_enum_freq_bands(&btv->tea, band);
3310 static ssize_t radio_read(struct file *file, char __user *data,
3311 size_t count, loff_t *ppos)
3313 struct bttv_fh *fh = file->private_data;
3314 struct bttv *btv = fh->btv;
3315 struct saa6588_command cmd;
3317 cmd.block_count = count / 3;
3318 cmd.nonblocking = file->f_flags & O_NONBLOCK;
3320 cmd.instance = file;
3321 cmd.result = -ENODEV;
3324 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
3329 static __poll_t radio_poll(struct file *file, poll_table *wait)
3331 struct bttv_fh *fh = file->private_data;
3332 struct bttv *btv = fh->btv;
3333 __poll_t req_events = poll_requested_events(wait);
3334 struct saa6588_command cmd;
3337 if (v4l2_event_pending(&fh->fh))
3339 else if (req_events & EPOLLPRI)
3340 poll_wait(file, &fh->fh.wait, wait);
3342 cmd.instance = file;
3343 cmd.event_list = wait;
3344 cmd.poll_mask = res;
3345 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
3347 return cmd.poll_mask;
3350 static const struct v4l2_file_operations radio_fops =
3352 .owner = THIS_MODULE,
3355 .release = radio_release,
3356 .unlocked_ioctl = video_ioctl2,
3360 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3361 .vidioc_querycap = bttv_querycap,
3362 .vidioc_log_status = bttv_log_status,
3363 .vidioc_g_tuner = radio_g_tuner,
3364 .vidioc_s_tuner = radio_s_tuner,
3365 .vidioc_g_frequency = bttv_g_frequency,
3366 .vidioc_s_frequency = bttv_s_frequency,
3367 .vidioc_s_hw_freq_seek = radio_s_hw_freq_seek,
3368 .vidioc_enum_freq_bands = radio_enum_freq_bands,
3369 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
3370 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
3373 static struct video_device radio_template = {
3374 .fops = &radio_fops,
3375 .ioctl_ops = &radio_ioctl_ops,
3378 /* ----------------------------------------------------------------------- */
3379 /* some debug code */
3381 static int bttv_risc_decode(u32 risc)
3383 static char *instr[16] = {
3384 [ BT848_RISC_WRITE >> 28 ] = "write",
3385 [ BT848_RISC_SKIP >> 28 ] = "skip",
3386 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3387 [ BT848_RISC_JUMP >> 28 ] = "jump",
3388 [ BT848_RISC_SYNC >> 28 ] = "sync",
3389 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3390 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3391 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3393 static int incr[16] = {
3394 [ BT848_RISC_WRITE >> 28 ] = 2,
3395 [ BT848_RISC_JUMP >> 28 ] = 2,
3396 [ BT848_RISC_SYNC >> 28 ] = 2,
3397 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3398 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3399 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3401 static char *bits[] = {
3402 "be0", "be1", "be2", "be3/resync",
3403 "set0", "set1", "set2", "set3",
3404 "clr0", "clr1", "clr2", "clr3",
3405 "irq", "res", "eol", "sol",
3409 pr_cont("0x%08x [ %s", risc,
3410 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3411 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3412 if (risc & (1 << (i + 12)))
3413 pr_cont(" %s", bits[i]);
3414 pr_cont(" count=%d ]\n", risc & 0xfff);
3415 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3418 static void bttv_risc_disasm(struct bttv *btv,
3419 struct btcx_riscmem *risc)
3423 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3424 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
3425 for (i = 0; i < (risc->size >> 2); i += n) {
3426 pr_info("%s: 0x%lx: ",
3427 btv->c.v4l2_dev.name,
3428 (unsigned long)(risc->dma + (i<<2)));
3429 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
3430 for (j = 1; j < n; j++)
3431 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3432 btv->c.v4l2_dev.name,
3433 (unsigned long)(risc->dma + ((i+j)<<2)),
3435 if (0 == risc->cpu[i])
3440 static void bttv_print_riscaddr(struct bttv *btv)
3442 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
3443 pr_info(" vbi : o=%08llx e=%08llx\n",
3444 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3445 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3446 pr_info(" cap : o=%08llx e=%08llx\n",
3448 ? (unsigned long long)btv->curr.top->top.dma : 0,
3450 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3451 pr_info(" scr : o=%08llx e=%08llx\n",
3452 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3453 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3454 bttv_risc_disasm(btv, &btv->main);
3457 /* ----------------------------------------------------------------------- */
3460 static char *irq_name[] = {
3461 "FMTCHG", // format change detected (525 vs. 625)
3462 "VSYNC", // vertical sync (new field)
3463 "HSYNC", // horizontal sync
3464 "OFLOW", // chroma/luma AGC overflow
3465 "HLOCK", // horizontal lock changed
3466 "VPRES", // video presence changed
3468 "I2CDONE", // hw irc operation finished
3469 "GPINT", // gpio port triggered irq
3471 "RISCI", // risc instruction triggered irq
3472 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3473 "FTRGT", // pixel data fifo overrun
3474 "FDSR", // fifo data stream resyncronisation
3475 "PPERR", // parity error (data transfer)
3476 "RIPERR", // parity error (read risc instructions)
3477 "PABORT", // pci abort
3478 "OCERR", // risc instruction error
3479 "SCERR", // syncronisation error
3482 static void bttv_print_irqbits(u32 print, u32 mark)
3487 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3488 if (print & (1 << i))
3489 pr_cont(" %s", irq_name[i]);
3490 if (mark & (1 << i))
3495 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3497 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3499 (unsigned long)btv->main.dma,
3500 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3501 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3504 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3505 pr_notice("%d: Oh, there (temporarily?) is no input signal. Ok, then this is harmless, don't worry ;)\n",
3509 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3511 pr_notice("%d: Lets try to catch the culprit red-handed ...\n",
3517 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3519 struct bttv_buffer *item;
3521 memset(set,0,sizeof(*set));
3523 /* capture request ? */
3524 if (!list_empty(&btv->capture)) {
3526 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3527 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3529 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3532 /* capture request for other field ? */
3533 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3534 (item->vb.queue.next != &btv->capture)) {
3535 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3536 /* Mike Isely <isely@pobox.com> - Only check
3537 * and set up the bottom field in the logic
3538 * below. Don't ever do the top field. This
3539 * of course means that if we set up the
3540 * bottom field in the above code that we'll
3541 * actually skip a field. But that's OK.
3542 * Having processed only a single buffer this
3543 * time, then the next time around the first
3544 * available buffer should be for a top field.
3545 * That will then cause us here to set up a
3546 * top then a bottom field in the normal way.
3547 * The alternative to this understanding is
3548 * that we set up the second available buffer
3549 * as a top field, but that's out of order
3550 * since this driver always processes the top
3551 * field first - the effect will be the two
3552 * buffers being returned in the wrong order,
3553 * with the second buffer also being delayed
3554 * by one field time (owing to the fifo nature
3555 * of videobuf). Worse still, we'll be stuck
3556 * doing fields out of order now every time
3557 * until something else causes a field to be
3558 * dropped. By effectively forcing a field to
3559 * drop this way then we always get back into
3560 * sync within a single frame time. (Out of
3561 * order fields can screw up deinterlacing
3563 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3564 if (NULL == set->bottom &&
3565 V4L2_FIELD_BOTTOM == item->vb.field) {
3568 if (NULL != set->top && NULL != set->bottom)
3574 /* screen overlay ? */
3575 if (NULL != btv->screen) {
3576 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3577 if (NULL == set->top && NULL == set->bottom) {
3578 set->top = btv->screen;
3579 set->bottom = btv->screen;
3582 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3584 set->top = btv->screen;
3586 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3587 NULL == set->bottom) {
3588 set->bottom = btv->screen;
3593 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3594 btv->c.nr, set->top, set->bottom,
3595 btv->screen, set->frame_irq, set->top_irq);
3600 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3601 struct bttv_buffer_set *curr, unsigned int state)
3603 u64 ts = ktime_get_ns();
3605 if (wakeup->top == wakeup->bottom) {
3606 if (NULL != wakeup->top && curr->top != wakeup->top) {
3608 pr_debug("%d: wakeup: both=%p\n",
3609 btv->c.nr, wakeup->top);
3610 wakeup->top->vb.ts = ts;
3611 wakeup->top->vb.field_count = btv->field_count;
3612 wakeup->top->vb.state = state;
3613 wake_up(&wakeup->top->vb.done);
3616 if (NULL != wakeup->top && curr->top != wakeup->top) {
3618 pr_debug("%d: wakeup: top=%p\n",
3619 btv->c.nr, wakeup->top);
3620 wakeup->top->vb.ts = ts;
3621 wakeup->top->vb.field_count = btv->field_count;
3622 wakeup->top->vb.state = state;
3623 wake_up(&wakeup->top->vb.done);
3625 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3627 pr_debug("%d: wakeup: bottom=%p\n",
3628 btv->c.nr, wakeup->bottom);
3629 wakeup->bottom->vb.ts = ts;
3630 wakeup->bottom->vb.field_count = btv->field_count;
3631 wakeup->bottom->vb.state = state;
3632 wake_up(&wakeup->bottom->vb.done);
3638 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3644 wakeup->vb.ts = ktime_get_ns();
3645 wakeup->vb.field_count = btv->field_count;
3646 wakeup->vb.state = state;
3647 wake_up(&wakeup->vb.done);
3650 static void bttv_irq_timeout(struct timer_list *t)
3652 struct bttv *btv = from_timer(btv, t, timeout);
3653 struct bttv_buffer_set old,new;
3654 struct bttv_buffer *ovbi;
3655 struct bttv_buffer *item;
3656 unsigned long flags;
3659 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3660 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3661 btread(BT848_RISC_COUNT));
3662 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3666 spin_lock_irqsave(&btv->s_lock,flags);
3668 /* deactivate stuff */
3669 memset(&new,0,sizeof(new));
3675 bttv_buffer_activate_video(btv, &new);
3676 bttv_buffer_activate_vbi(btv, NULL);
3677 bttv_set_dma(btv, 0);
3680 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3681 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
3683 /* cancel all outstanding capture / vbi requests */
3684 while (!list_empty(&btv->capture)) {
3685 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3686 list_del(&item->vb.queue);
3687 item->vb.state = VIDEOBUF_ERROR;
3688 wake_up(&item->vb.done);
3690 while (!list_empty(&btv->vcapture)) {
3691 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3692 list_del(&item->vb.queue);
3693 item->vb.state = VIDEOBUF_ERROR;
3694 wake_up(&item->vb.done);
3698 spin_unlock_irqrestore(&btv->s_lock,flags);
3702 bttv_irq_wakeup_top(struct bttv *btv)
3704 struct bttv_buffer *wakeup = btv->curr.top;
3709 spin_lock(&btv->s_lock);
3710 btv->curr.top_irq = 0;
3711 btv->curr.top = NULL;
3712 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3714 wakeup->vb.ts = ktime_get_ns();
3715 wakeup->vb.field_count = btv->field_count;
3716 wakeup->vb.state = VIDEOBUF_DONE;
3717 wake_up(&wakeup->vb.done);
3718 spin_unlock(&btv->s_lock);
3721 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3725 if (rc > risc->dma + risc->size)
3731 bttv_irq_switch_video(struct bttv *btv)
3733 struct bttv_buffer_set new;
3734 struct bttv_buffer_set old;
3737 spin_lock(&btv->s_lock);
3739 /* new buffer set */
3740 bttv_irq_next_video(btv, &new);
3741 rc = btread(BT848_RISC_COUNT);
3742 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3743 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3746 bttv_irq_debug_low_latency(btv, rc);
3747 spin_unlock(&btv->s_lock);
3754 btv->loop_irq &= ~1;
3755 bttv_buffer_activate_video(btv, &new);
3756 bttv_set_dma(btv, 0);
3759 if (UNSET != btv->new_input) {
3760 video_mux(btv,btv->new_input);
3761 btv->new_input = UNSET;
3764 /* wake up finished buffers */
3765 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
3766 spin_unlock(&btv->s_lock);
3770 bttv_irq_switch_vbi(struct bttv *btv)
3772 struct bttv_buffer *new = NULL;
3773 struct bttv_buffer *old;
3776 spin_lock(&btv->s_lock);
3778 if (!list_empty(&btv->vcapture))
3779 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3782 rc = btread(BT848_RISC_COUNT);
3783 if (NULL != old && (is_active(&old->top, rc) ||
3784 is_active(&old->bottom, rc))) {
3787 bttv_irq_debug_low_latency(btv, rc);
3788 spin_unlock(&btv->s_lock);
3794 btv->loop_irq &= ~4;
3795 bttv_buffer_activate_vbi(btv, new);
3796 bttv_set_dma(btv, 0);
3798 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
3799 spin_unlock(&btv->s_lock);
3802 static irqreturn_t bttv_irq(int irq, void *dev_id)
3810 btv=(struct bttv *)dev_id;
3814 /* get/clear interrupt status bits */
3815 stat=btread(BT848_INT_STAT);
3816 astat=stat&btread(BT848_INT_MASK);
3820 btwrite(stat,BT848_INT_STAT);
3822 /* get device status bits */
3823 dstat=btread(BT848_DSTATUS);
3826 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
3827 btv->c.nr, count, btv->field_count,
3828 stat>>28, btread(BT848_RISC_COUNT));
3829 bttv_print_irqbits(stat,astat);
3830 if (stat & BT848_INT_HLOCK)
3831 pr_cont(" HLOC => %s",
3832 dstat & BT848_DSTATUS_HLOC
3834 if (stat & BT848_INT_VPRES)
3835 pr_cont(" PRES => %s",
3836 dstat & BT848_DSTATUS_PRES
3838 if (stat & BT848_INT_FMTCHG)
3839 pr_cont(" NUML => %s",
3840 dstat & BT848_DSTATUS_NUML
3845 if (astat&BT848_INT_VSYNC)
3848 if ((astat & BT848_INT_GPINT) && btv->remote) {
3849 bttv_input_irq(btv);
3852 if (astat & BT848_INT_I2CDONE) {
3853 btv->i2c_done = stat;
3854 wake_up(&btv->i2c_queue);
3857 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3858 bttv_irq_switch_vbi(btv);
3860 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3861 bttv_irq_wakeup_top(btv);
3863 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3864 bttv_irq_switch_video(btv);
3866 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3867 /* trigger automute */
3868 audio_mux_gpio(btv, btv->audio_input, btv->mute);
3870 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3871 pr_info("%d: %s%s @ %08x,",
3873 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3874 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3875 btread(BT848_RISC_COUNT));
3876 bttv_print_irqbits(stat,astat);
3879 bttv_print_riscaddr(btv);
3881 if (fdsr && astat & BT848_INT_FDSR) {
3882 pr_info("%d: FDSR @ %08x\n",
3883 btv->c.nr, btread(BT848_RISC_COUNT));
3885 bttv_print_riscaddr(btv);
3891 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3892 btwrite(0, BT848_INT_MASK);
3894 pr_err("%d: IRQ lockup, cleared int mask [",
3897 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3900 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3904 bttv_print_irqbits(stat,astat);
3912 return IRQ_RETVAL(handled);
3916 /* ----------------------------------------------------------------------- */
3917 /* initialization */
3919 static void vdev_init(struct bttv *btv,
3920 struct video_device *vfd,
3921 const struct video_device *template,
3922 const char *type_name)
3925 vfd->v4l2_dev = &btv->c.v4l2_dev;
3926 vfd->release = video_device_release_empty;
3927 video_set_drvdata(vfd, btv);
3928 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3929 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3930 type_name, bttv_tvcards[btv->c.type].name);
3931 if (btv->tuner_type == TUNER_ABSENT) {
3932 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
3933 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
3934 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
3935 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
3939 static void bttv_unregister_video(struct bttv *btv)
3941 video_unregister_device(&btv->video_dev);
3942 video_unregister_device(&btv->vbi_dev);
3943 video_unregister_device(&btv->radio_dev);
3946 /* register video4linux devices */
3947 static int bttv_register_video(struct bttv *btv)
3950 pr_notice("Overlay support disabled\n");
3953 vdev_init(btv, &btv->video_dev, &bttv_video_template, "video");
3955 if (video_register_device(&btv->video_dev, VFL_TYPE_GRABBER,
3956 video_nr[btv->c.nr]) < 0)
3958 pr_info("%d: registered device %s\n",
3959 btv->c.nr, video_device_node_name(&btv->video_dev));
3960 if (device_create_file(&btv->video_dev.dev,
3961 &dev_attr_card)<0) {
3962 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
3967 vdev_init(btv, &btv->vbi_dev, &bttv_video_template, "vbi");
3969 if (video_register_device(&btv->vbi_dev, VFL_TYPE_VBI,
3970 vbi_nr[btv->c.nr]) < 0)
3972 pr_info("%d: registered device %s\n",
3973 btv->c.nr, video_device_node_name(&btv->vbi_dev));
3975 if (!btv->has_radio)
3978 vdev_init(btv, &btv->radio_dev, &radio_template, "radio");
3979 btv->radio_dev.ctrl_handler = &btv->radio_ctrl_handler;
3980 if (video_register_device(&btv->radio_dev, VFL_TYPE_RADIO,
3981 radio_nr[btv->c.nr]) < 0)
3983 pr_info("%d: registered device %s\n",
3984 btv->c.nr, video_device_node_name(&btv->radio_dev));
3990 bttv_unregister_video(btv);
3995 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3996 /* response on cards with no firmware is not enabled by OF */
3997 static void pci_set_command(struct pci_dev *dev)
3999 #if defined(__powerpc__)
4002 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4003 cmd = (cmd | PCI_COMMAND_MEMORY );
4004 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4008 static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
4010 struct v4l2_frequency init_freq = {
4012 .type = V4L2_TUNER_ANALOG_TV,
4018 struct v4l2_ctrl_handler *hdl;
4020 if (bttv_num == BTTV_MAX)
4022 pr_info("Bt8xx card found (%d)\n", bttv_num);
4023 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
4025 pr_err("out of memory\n");
4028 btv->c.nr = bttv_num;
4029 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4030 "bttv%d", btv->c.nr);
4032 /* initialize structs / fill in defaults */
4033 mutex_init(&btv->lock);
4034 spin_lock_init(&btv->s_lock);
4035 spin_lock_init(&btv->gpio_lock);
4036 init_waitqueue_head(&btv->i2c_queue);
4037 INIT_LIST_HEAD(&btv->c.subs);
4038 INIT_LIST_HEAD(&btv->capture);
4039 INIT_LIST_HEAD(&btv->vcapture);
4041 timer_setup(&btv->timeout, bttv_irq_timeout, 0);
4044 btv->tuner_type = UNSET;
4045 btv->new_input = UNSET;
4046 btv->has_radio=radio[btv->c.nr];
4048 /* pci stuff (init, get irq/mmio, ... */
4050 btv->id = dev->device;
4051 if (pci_enable_device(dev)) {
4052 pr_warn("%d: Can't enable device\n", btv->c.nr);
4055 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
4056 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
4059 if (!request_mem_region(pci_resource_start(dev,0),
4060 pci_resource_len(dev,0),
4061 btv->c.v4l2_dev.name)) {
4062 pr_warn("%d: can't request iomem (0x%llx)\n",
4064 (unsigned long long)pci_resource_start(dev, 0));
4067 pci_set_master(dev);
4068 pci_set_command(dev);
4070 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4072 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
4075 hdl = &btv->ctrl_handler;
4076 v4l2_ctrl_handler_init(hdl, 20);
4077 btv->c.v4l2_dev.ctrl_handler = hdl;
4078 v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
4080 btv->revision = dev->revision;
4081 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4082 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4083 bttv_num, btv->id, btv->revision, pci_name(dev),
4084 btv->c.pci->irq, lat,
4085 (unsigned long long)pci_resource_start(dev, 0));
4088 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4089 if (NULL == btv->bt848_mmio) {
4090 pr_err("%d: ioremap() failed\n", btv->c.nr);
4098 /* disable irqs, register irq handler */
4099 btwrite(0, BT848_INT_MASK);
4100 result = request_irq(btv->c.pci->irq, bttv_irq,
4101 IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
4103 pr_err("%d: can't get IRQ %d\n",
4104 bttv_num, btv->c.pci->irq);
4108 if (0 != bttv_handle_chipset(btv)) {
4113 /* init options from insmod args */
4114 btv->opt_combfilter = combfilter;
4115 bttv_ctrl_combfilter.def = combfilter;
4116 bttv_ctrl_lumafilter.def = lumafilter;
4117 btv->opt_automute = automute;
4118 bttv_ctrl_automute.def = automute;
4119 bttv_ctrl_agc_crush.def = agc_crush;
4120 btv->opt_vcr_hack = vcr_hack;
4121 bttv_ctrl_vcr_hack.def = vcr_hack;
4122 bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
4123 bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
4124 btv->opt_uv_ratio = uv_ratio;
4125 bttv_ctrl_uv_ratio.def = uv_ratio;
4126 bttv_ctrl_full_luma.def = full_luma_range;
4127 bttv_ctrl_coring.def = coring;
4129 /* fill struct bttv with some useful defaults */
4130 btv->init.btv = btv;
4131 btv->init.ov.w.width = 320;
4132 btv->init.ov.w.height = 240;
4133 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4134 btv->init.width = 320;
4135 btv->init.height = 240;
4136 btv->init.ov.w.width = 320;
4137 btv->init.ov.w.height = 240;
4138 btv->init.ov.field = V4L2_FIELD_INTERLACED;
4141 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4142 V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
4143 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4144 V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
4145 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4146 V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
4147 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4148 V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
4149 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4150 V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
4151 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4152 V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
4153 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4154 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
4155 if (btv->volume_gpio)
4156 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
4157 V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
4158 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
4159 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
4160 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
4161 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
4162 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
4163 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
4164 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
4165 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
4166 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
4167 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
4169 /* initialize hardware */
4171 bttv_gpio_tracking(btv,"pre-init");
4173 bttv_risc_init_main(btv);
4177 btwrite(0x00, BT848_GPIO_REG_INP);
4178 btwrite(0x00, BT848_GPIO_OUT_EN);
4180 bttv_gpio_tracking(btv,"init");
4182 /* needs to be done before i2c is registered */
4183 bttv_init_card1(btv);
4185 /* register i2c + gpio */
4188 /* some card-specific stuff (needs working i2c) */
4189 bttv_init_card2(btv);
4190 bttv_init_tuner(btv);
4191 if (btv->tuner_type != TUNER_ABSENT) {
4192 bttv_set_frequency(btv, &init_freq);
4193 btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
4195 btv->std = V4L2_STD_PAL;
4197 if (!bttv_tvcards[btv->c.type].no_video)
4198 v4l2_ctrl_handler_setup(hdl);
4200 result = hdl->error;
4206 /* register video4linux + input */
4207 if (!bttv_tvcards[btv->c.type].no_video) {
4208 v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
4209 v4l2_ctrl_radio_filter, false);
4210 if (btv->radio_ctrl_handler.error) {
4211 result = btv->radio_ctrl_handler.error;
4214 set_input(btv, 0, btv->tvnorm);
4215 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4216 btv->crop[1] = btv->crop[0]; /* current = default */
4217 disclaim_vbi_lines(btv);
4218 disclaim_video_lines(btv);
4219 bttv_register_video(btv);
4222 /* add subdevices and autoload dvb-bt8xx if needed */
4223 if (bttv_tvcards[btv->c.type].has_dvb) {
4224 bttv_sub_add_device(&btv->c, "dvb");
4225 request_modules(btv);
4229 init_bttv_i2c_ir(btv);
4230 bttv_input_init(btv);
4233 /* everything is fine */
4238 free_irq(btv->c.pci->irq,btv);
4241 v4l2_ctrl_handler_free(&btv->ctrl_handler);
4242 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
4243 v4l2_device_unregister(&btv->c.v4l2_dev);
4246 if (btv->bt848_mmio)
4247 iounmap(btv->bt848_mmio);
4248 release_mem_region(pci_resource_start(btv->c.pci,0),
4249 pci_resource_len(btv->c.pci,0));
4250 pci_disable_device(btv->c.pci);
4254 static void bttv_remove(struct pci_dev *pci_dev)
4256 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4257 struct bttv *btv = to_bttv(v4l2_dev);
4260 pr_info("%d: unloading\n", btv->c.nr);
4262 if (bttv_tvcards[btv->c.type].has_dvb)
4263 flush_request_modules(btv);
4265 /* shutdown everything (DMA+IRQs) */
4266 btand(~15, BT848_GPIO_DMA_CTL);
4267 btwrite(0, BT848_INT_MASK);
4268 btwrite(~0x0, BT848_INT_STAT);
4269 btwrite(0x0, BT848_GPIO_OUT_EN);
4271 bttv_gpio_tracking(btv,"cleanup");
4273 /* tell gpio modules we are leaving ... */
4275 bttv_input_fini(btv);
4276 bttv_sub_del_devices(&btv->c);
4278 /* unregister i2c_bus + input */
4281 /* unregister video4linux */
4282 bttv_unregister_video(btv);
4284 /* free allocated memory */
4285 v4l2_ctrl_handler_free(&btv->ctrl_handler);
4286 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
4287 btcx_riscmem_free(btv->c.pci,&btv->main);
4289 /* free resources */
4290 free_irq(btv->c.pci->irq,btv);
4291 iounmap(btv->bt848_mmio);
4292 release_mem_region(pci_resource_start(btv->c.pci,0),
4293 pci_resource_len(btv->c.pci,0));
4294 pci_disable_device(btv->c.pci);
4296 v4l2_device_unregister(&btv->c.v4l2_dev);
4297 bttvs[btv->c.nr] = NULL;
4304 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4306 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4307 struct bttv *btv = to_bttv(v4l2_dev);
4308 struct bttv_buffer_set idle;
4309 unsigned long flags;
4311 dprintk("%d: suspend %d\n", btv->c.nr, state.event);
4313 /* stop dma + irqs */
4314 spin_lock_irqsave(&btv->s_lock,flags);
4315 memset(&idle, 0, sizeof(idle));
4316 btv->state.video = btv->curr;
4317 btv->state.vbi = btv->cvbi;
4318 btv->state.loop_irq = btv->loop_irq;
4321 bttv_buffer_activate_video(btv, &idle);
4322 bttv_buffer_activate_vbi(btv, NULL);
4323 bttv_set_dma(btv, 0);
4324 btwrite(0, BT848_INT_MASK);
4325 spin_unlock_irqrestore(&btv->s_lock,flags);
4327 /* save bt878 state */
4328 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4329 btv->state.gpio_data = gpio_read();
4331 /* save pci state */
4332 pci_save_state(pci_dev);
4333 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4334 pci_disable_device(pci_dev);
4335 btv->state.disabled = 1;
4340 static int bttv_resume(struct pci_dev *pci_dev)
4342 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4343 struct bttv *btv = to_bttv(v4l2_dev);
4344 unsigned long flags;
4347 dprintk("%d: resume\n", btv->c.nr);
4349 /* restore pci state */
4350 if (btv->state.disabled) {
4351 err=pci_enable_device(pci_dev);
4353 pr_warn("%d: Can't enable device\n", btv->c.nr);
4356 btv->state.disabled = 0;
4358 err=pci_set_power_state(pci_dev, PCI_D0);
4360 pci_disable_device(pci_dev);
4361 pr_warn("%d: Can't enable device\n", btv->c.nr);
4362 btv->state.disabled = 1;
4366 pci_restore_state(pci_dev);
4368 /* restore bt878 state */
4369 bttv_reinit_bt848(btv);
4370 gpio_inout(0xffffff, btv->state.gpio_enable);
4371 gpio_write(btv->state.gpio_data);
4374 spin_lock_irqsave(&btv->s_lock,flags);
4375 btv->curr = btv->state.video;
4376 btv->cvbi = btv->state.vbi;
4377 btv->loop_irq = btv->state.loop_irq;
4378 bttv_buffer_activate_video(btv, &btv->curr);
4379 bttv_buffer_activate_vbi(btv, btv->cvbi);
4380 bttv_set_dma(btv, 0);
4381 spin_unlock_irqrestore(&btv->s_lock,flags);
4386 static const struct pci_device_id bttv_pci_tbl[] = {
4387 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4388 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4389 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4390 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
4391 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
4395 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4397 static struct pci_driver bttv_pci_driver = {
4399 .id_table = bttv_pci_tbl,
4400 .probe = bttv_probe,
4401 .remove = bttv_remove,
4403 .suspend = bttv_suspend,
4404 .resume = bttv_resume,
4408 static int __init bttv_init_module(void)
4414 pr_info("driver version %s loaded\n", BTTV_VERSION);
4415 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4417 if (gbufsize > BTTV_MAX_FBUF)
4418 gbufsize = BTTV_MAX_FBUF;
4419 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4421 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4422 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4424 bttv_check_chipset();
4426 ret = bus_register(&bttv_sub_bus_type);
4428 pr_warn("bus_register error: %d\n", ret);
4431 ret = pci_register_driver(&bttv_pci_driver);
4433 bus_unregister(&bttv_sub_bus_type);
4438 static void __exit bttv_cleanup_module(void)
4440 pci_unregister_driver(&bttv_pci_driver);
4441 bus_unregister(&bttv_sub_bus_type);
4444 module_init(bttv_init_module);
4445 module_exit(bttv_cleanup_module);