3 bttv - Bt848 frame grabber driver
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
6 & Marcus Metzler <mocm@thp.uni-koeln.de>
7 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
15 Fixes to be fully V4L2 compliant by
16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
18 Cropping and overscan support
19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
20 Sponsored by OPQ Systems AB
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/delay.h>
42 #include <linux/slab.h>
43 #include <linux/errno.h>
45 #include <linux/kernel.h>
46 #include <linux/sched.h>
47 #include <linux/interrupt.h>
48 #include <linux/kdev_t.h>
50 #include <media/v4l2-common.h>
51 #include <media/v4l2-ioctl.h>
52 #include <media/tvaudio.h>
53 #include <media/msp3400.h>
55 #include <linux/dma-mapping.h>
58 #include <asm/byteorder.h>
60 #include <media/saa6588.h>
62 #define BTTV_VERSION "0.9.19"
64 unsigned int bttv_num; /* number of Bt848s in use */
65 struct bttv *bttvs[BTTV_MAX];
67 unsigned int bttv_debug;
68 unsigned int bttv_verbose = 1;
69 unsigned int bttv_gpio;
71 /* config variables */
73 static unsigned int bigendian=1;
75 static unsigned int bigendian;
77 static unsigned int radio[BTTV_MAX];
78 static unsigned int irq_debug;
79 static unsigned int gbuffers = 8;
80 static unsigned int gbufsize = 0x208000;
81 static unsigned int reset_crop = 1;
83 static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
84 static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
85 static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
86 static int debug_latency;
87 static int disable_ir;
89 static unsigned int fdsr;
92 static unsigned int combfilter;
93 static unsigned int lumafilter;
94 static unsigned int automute = 1;
95 static unsigned int chroma_agc;
96 static unsigned int adc_crush = 1;
97 static unsigned int whitecrush_upper = 0xCF;
98 static unsigned int whitecrush_lower = 0x7F;
99 static unsigned int vcr_hack;
100 static unsigned int irq_iswitch;
101 static unsigned int uv_ratio = 50;
102 static unsigned int full_luma_range;
103 static unsigned int coring;
105 /* API features (turn on/off stuff for testing) */
106 static unsigned int v4l2 = 1;
109 module_param(bttv_verbose, int, 0644);
110 module_param(bttv_gpio, int, 0644);
111 module_param(bttv_debug, int, 0644);
112 module_param(irq_debug, int, 0644);
113 module_param(debug_latency, int, 0644);
114 module_param(disable_ir, int, 0444);
116 module_param(fdsr, int, 0444);
117 module_param(gbuffers, int, 0444);
118 module_param(gbufsize, int, 0444);
119 module_param(reset_crop, int, 0444);
121 module_param(v4l2, int, 0644);
122 module_param(bigendian, int, 0644);
123 module_param(irq_iswitch, int, 0644);
124 module_param(combfilter, int, 0444);
125 module_param(lumafilter, int, 0444);
126 module_param(automute, int, 0444);
127 module_param(chroma_agc, int, 0444);
128 module_param(adc_crush, int, 0444);
129 module_param(whitecrush_upper, int, 0444);
130 module_param(whitecrush_lower, int, 0444);
131 module_param(vcr_hack, int, 0444);
132 module_param(uv_ratio, int, 0444);
133 module_param(full_luma_range, int, 0444);
134 module_param(coring, int, 0444);
136 module_param_array(radio, int, NULL, 0444);
137 module_param_array(video_nr, int, NULL, 0444);
138 module_param_array(radio_nr, int, NULL, 0444);
139 module_param_array(vbi_nr, int, NULL, 0444);
141 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
142 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
143 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
144 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
145 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
146 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
147 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
148 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
149 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
150 MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
151 "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(adc_crush,"enables the luminance ADC 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 /* ----------------------------------------------------------------------- */
174 static ssize_t show_card(struct device *cd,
175 struct device_attribute *attr, char *buf)
177 struct video_device *vfd = container_of(cd, struct video_device, dev);
178 struct bttv *btv = video_get_drvdata(vfd);
179 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
181 static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
183 /* ----------------------------------------------------------------------- */
184 /* dvb auto-load setup */
185 #if defined(CONFIG_MODULES) && defined(MODULE)
186 static void request_module_async(struct work_struct *work)
188 request_module("dvb-bt8xx");
191 static void request_modules(struct bttv *dev)
193 INIT_WORK(&dev->request_module_wk, request_module_async);
194 schedule_work(&dev->request_module_wk);
197 static void flush_request_modules(struct bttv *dev)
199 flush_work_sync(&dev->request_module_wk);
202 #define request_modules(dev)
203 #define flush_request_modules(dev)
204 #endif /* CONFIG_MODULES */
207 /* ----------------------------------------------------------------------- */
210 /* special timing tables from conexant... */
211 static u8 SRAM_Table[][60] =
213 /* PAL digital input over GPIO[7:0] */
215 45, // 45 bytes following
216 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
217 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
218 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
219 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
220 0x37,0x00,0xAF,0x21,0x00
222 /* NTSC digital input over GPIO[7:0] */
224 51, // 51 bytes following
225 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
226 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
227 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
228 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
229 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
232 // TGB_NTSC392 // quartzsight
233 // This table has been modified to be used for Fusion Rev D
235 0x2A, // size of table = 42
236 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
237 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
238 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
239 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
244 /* minhdelayx1 first video pixel we can capture on a line and
245 hdelayx1 start of active video, both relative to rising edge of
246 /HRESET pulse (0H) in 1 / fCLKx1.
247 swidth width of active video and
248 totalwidth total line width, both in 1 / fCLKx1.
249 sqwidth total line width in square pixels.
250 vdelay start of active video in 2 * field lines relative to
251 trailing edge of /VRESET pulse (VDELAY register).
252 sheight height of active video in 2 * field lines.
253 videostart0 ITU-R frame line number of the line corresponding
254 to vdelay in the first field. */
255 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
256 vdelay, sheight, videostart0) \
257 .cropcap.bounds.left = minhdelayx1, \
258 /* * 2 because vertically we count field lines times two, */ \
259 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
260 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
261 /* 4 is a safety margin at the end of the line. */ \
262 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
263 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
264 .cropcap.defrect.left = hdelayx1, \
265 .cropcap.defrect.top = (videostart0) * 2, \
266 .cropcap.defrect.width = swidth, \
267 .cropcap.defrect.height = sheight, \
268 .cropcap.pixelaspect.numerator = totalwidth, \
269 .cropcap.pixelaspect.denominator = sqwidth,
271 const struct bttv_tvnorm bttv_tvnorms[] = {
273 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
274 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
276 .v4l2_id = V4L2_STD_PAL,
284 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
285 .scaledtwidth = 1135,
289 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
291 /* ITU-R frame line number of the first VBI line
292 we can capture, of the first and second field.
293 The last line is determined by cropcap.bounds. */
294 .vbistart = { 7, 320 },
295 CROPCAP(/* minhdelayx1 */ 68,
297 /* Should be (768 * 1135 + 944 / 2) / 944.
298 cropcap.defrect is used for image width
299 checks, so we keep the old value 924. */
301 /* totalwidth */ 1135,
305 /* videostart0 */ 23)
306 /* bt878 (and bt848?) can capture another
307 line below active video. */
308 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
310 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
318 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
323 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
325 .vbistart = { 10, 273 },
326 CROPCAP(/* minhdelayx1 */ 68,
328 /* Should be (640 * 910 + 780 / 2) / 780? */
330 /* totalwidth */ 910,
334 /* videostart0 */ 23)
336 .v4l2_id = V4L2_STD_SECAM,
344 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
345 .scaledtwidth = 1135,
350 .sram = 0, /* like PAL, correct? */
351 .vbistart = { 7, 320 },
352 CROPCAP(/* minhdelayx1 */ 68,
355 /* totalwidth */ 1135,
359 /* videostart0 */ 23)
361 .v4l2_id = V4L2_STD_PAL_Nc,
369 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
376 .vbistart = { 7, 320 },
377 CROPCAP(/* minhdelayx1 */ 68,
379 /* swidth */ (640 * 910 + 780 / 2) / 780,
380 /* totalwidth */ 910,
384 /* videostart0 */ 23)
386 .v4l2_id = V4L2_STD_PAL_M,
394 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
401 .vbistart = { 10, 273 },
402 CROPCAP(/* minhdelayx1 */ 68,
404 /* swidth */ (640 * 910 + 780 / 2) / 780,
405 /* totalwidth */ 910,
409 /* videostart0 */ 23)
411 .v4l2_id = V4L2_STD_PAL_N,
419 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
426 .vbistart = { 7, 320 },
427 CROPCAP(/* minhdelayx1 */ 68,
429 /* swidth */ (768 * 1135 + 944 / 2) / 944,
430 /* totalwidth */ 1135,
434 /* videostart0 */ 23)
436 .v4l2_id = V4L2_STD_NTSC_M_JP,
444 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
451 .vbistart = { 10, 273 },
452 CROPCAP(/* minhdelayx1 */ 68,
454 /* swidth */ (640 * 910 + 780 / 2) / 780,
455 /* totalwidth */ 910,
459 /* videostart0 */ 23)
461 /* that one hopefully works with the strange timing
462 * which video recorders produce when playing a NTSC
463 * tape on a PAL TV ... */
464 .v4l2_id = V4L2_STD_PAL_60,
472 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
473 .scaledtwidth = 1135,
480 .vbistart = { 10, 273 },
481 CROPCAP(/* minhdelayx1 */ 68,
484 /* totalwidth */ 1135,
488 /* videostart0 */ 23)
491 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
493 /* ----------------------------------------------------------------------- */
495 packed pixel formats must come first */
496 static const struct bttv_format formats[] = {
498 .name = "8 bpp, gray",
499 .fourcc = V4L2_PIX_FMT_GREY,
500 .btformat = BT848_COLOR_FMT_Y8,
502 .flags = FORMAT_FLAGS_PACKED,
504 .name = "8 bpp, dithered color",
505 .fourcc = V4L2_PIX_FMT_HI240,
506 .btformat = BT848_COLOR_FMT_RGB8,
508 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
510 .name = "15 bpp RGB, le",
511 .fourcc = V4L2_PIX_FMT_RGB555,
512 .btformat = BT848_COLOR_FMT_RGB15,
514 .flags = FORMAT_FLAGS_PACKED,
516 .name = "15 bpp RGB, be",
517 .fourcc = V4L2_PIX_FMT_RGB555X,
518 .btformat = BT848_COLOR_FMT_RGB15,
519 .btswap = 0x03, /* byteswap */
521 .flags = FORMAT_FLAGS_PACKED,
523 .name = "16 bpp RGB, le",
524 .fourcc = V4L2_PIX_FMT_RGB565,
525 .btformat = BT848_COLOR_FMT_RGB16,
527 .flags = FORMAT_FLAGS_PACKED,
529 .name = "16 bpp RGB, be",
530 .fourcc = V4L2_PIX_FMT_RGB565X,
531 .btformat = BT848_COLOR_FMT_RGB16,
532 .btswap = 0x03, /* byteswap */
534 .flags = FORMAT_FLAGS_PACKED,
536 .name = "24 bpp RGB, le",
537 .fourcc = V4L2_PIX_FMT_BGR24,
538 .btformat = BT848_COLOR_FMT_RGB24,
540 .flags = FORMAT_FLAGS_PACKED,
542 .name = "32 bpp RGB, le",
543 .fourcc = V4L2_PIX_FMT_BGR32,
544 .btformat = BT848_COLOR_FMT_RGB32,
546 .flags = FORMAT_FLAGS_PACKED,
548 .name = "32 bpp RGB, be",
549 .fourcc = V4L2_PIX_FMT_RGB32,
550 .btformat = BT848_COLOR_FMT_RGB32,
551 .btswap = 0x0f, /* byte+word swap */
553 .flags = FORMAT_FLAGS_PACKED,
555 .name = "4:2:2, packed, YUYV",
556 .fourcc = V4L2_PIX_FMT_YUYV,
557 .btformat = BT848_COLOR_FMT_YUY2,
559 .flags = FORMAT_FLAGS_PACKED,
561 .name = "4:2:2, packed, YUYV",
562 .fourcc = V4L2_PIX_FMT_YUYV,
563 .btformat = BT848_COLOR_FMT_YUY2,
565 .flags = FORMAT_FLAGS_PACKED,
567 .name = "4:2:2, packed, UYVY",
568 .fourcc = V4L2_PIX_FMT_UYVY,
569 .btformat = BT848_COLOR_FMT_YUY2,
570 .btswap = 0x03, /* byteswap */
572 .flags = FORMAT_FLAGS_PACKED,
574 .name = "4:2:2, planar, Y-Cb-Cr",
575 .fourcc = V4L2_PIX_FMT_YUV422P,
576 .btformat = BT848_COLOR_FMT_YCrCb422,
578 .flags = FORMAT_FLAGS_PLANAR,
582 .name = "4:2:0, planar, Y-Cb-Cr",
583 .fourcc = V4L2_PIX_FMT_YUV420,
584 .btformat = BT848_COLOR_FMT_YCrCb422,
586 .flags = FORMAT_FLAGS_PLANAR,
590 .name = "4:2:0, planar, Y-Cr-Cb",
591 .fourcc = V4L2_PIX_FMT_YVU420,
592 .btformat = BT848_COLOR_FMT_YCrCb422,
594 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
598 .name = "4:1:1, planar, Y-Cb-Cr",
599 .fourcc = V4L2_PIX_FMT_YUV411P,
600 .btformat = BT848_COLOR_FMT_YCrCb411,
602 .flags = FORMAT_FLAGS_PLANAR,
606 .name = "4:1:0, planar, Y-Cb-Cr",
607 .fourcc = V4L2_PIX_FMT_YUV410,
608 .btformat = BT848_COLOR_FMT_YCrCb411,
610 .flags = FORMAT_FLAGS_PLANAR,
614 .name = "4:1:0, planar, Y-Cr-Cb",
615 .fourcc = V4L2_PIX_FMT_YVU410,
616 .btformat = BT848_COLOR_FMT_YCrCb411,
618 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
622 .name = "raw scanlines",
624 .btformat = BT848_COLOR_FMT_RAW,
626 .flags = FORMAT_FLAGS_RAW,
629 static const unsigned int FORMATS = ARRAY_SIZE(formats);
631 /* ----------------------------------------------------------------------- */
633 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
634 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
635 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
636 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
637 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
638 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
639 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
640 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
641 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
642 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
643 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
644 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
646 static const struct v4l2_queryctrl no_ctl = {
648 .flags = V4L2_CTRL_FLAG_DISABLED,
650 static const struct v4l2_queryctrl bttv_ctls[] = {
653 .id = V4L2_CID_BRIGHTNESS,
654 .name = "Brightness",
658 .default_value = 32768,
659 .type = V4L2_CTRL_TYPE_INTEGER,
661 .id = V4L2_CID_CONTRAST,
666 .default_value = 27648,
667 .type = V4L2_CTRL_TYPE_INTEGER,
669 .id = V4L2_CID_SATURATION,
670 .name = "Saturation",
674 .default_value = 32768,
675 .type = V4L2_CTRL_TYPE_INTEGER,
682 .default_value = 32768,
683 .type = V4L2_CTRL_TYPE_INTEGER,
687 .id = V4L2_CID_AUDIO_MUTE,
691 .type = V4L2_CTRL_TYPE_BOOLEAN,
693 .id = V4L2_CID_AUDIO_VOLUME,
698 .default_value = 65535,
699 .type = V4L2_CTRL_TYPE_INTEGER,
701 .id = V4L2_CID_AUDIO_BALANCE,
706 .default_value = 32768,
707 .type = V4L2_CTRL_TYPE_INTEGER,
709 .id = V4L2_CID_AUDIO_BASS,
714 .default_value = 32768,
715 .type = V4L2_CTRL_TYPE_INTEGER,
717 .id = V4L2_CID_AUDIO_TREBLE,
722 .default_value = 32768,
723 .type = V4L2_CTRL_TYPE_INTEGER,
725 /* --- private --- */
727 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
728 .name = "chroma agc",
731 .type = V4L2_CTRL_TYPE_BOOLEAN,
733 .id = V4L2_CID_PRIVATE_COMBFILTER,
734 .name = "combfilter",
737 .type = V4L2_CTRL_TYPE_BOOLEAN,
739 .id = V4L2_CID_PRIVATE_AUTOMUTE,
743 .type = V4L2_CTRL_TYPE_BOOLEAN,
745 .id = V4L2_CID_PRIVATE_LUMAFILTER,
746 .name = "luma decimation filter",
749 .type = V4L2_CTRL_TYPE_BOOLEAN,
751 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
755 .type = V4L2_CTRL_TYPE_BOOLEAN,
757 .id = V4L2_CID_PRIVATE_VCR_HACK,
761 .type = V4L2_CTRL_TYPE_BOOLEAN,
763 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
764 .name = "whitecrush upper",
768 .default_value = 0xCF,
769 .type = V4L2_CTRL_TYPE_INTEGER,
771 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
772 .name = "whitecrush lower",
776 .default_value = 0x7F,
777 .type = V4L2_CTRL_TYPE_INTEGER,
779 .id = V4L2_CID_PRIVATE_UV_RATIO,
785 .type = V4L2_CTRL_TYPE_INTEGER,
787 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
788 .name = "full luma range",
791 .type = V4L2_CTRL_TYPE_BOOLEAN,
793 .id = V4L2_CID_PRIVATE_CORING,
799 .type = V4L2_CTRL_TYPE_INTEGER,
806 static const struct v4l2_queryctrl *ctrl_by_id(int id)
810 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
811 if (bttv_ctls[i].id == id)
817 /* ----------------------------------------------------------------------- */
818 /* resource management */
821 RESOURCE_ allocated by freed by
823 VIDEO_READ bttv_read 1) bttv_read 2)
825 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
826 VIDIOC_QBUF 1) bttv_release
829 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
830 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
833 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
834 VIDIOC_QBUF 1) bttv_release
835 bttv_read, bttv_poll 1) 4)
837 1) The resource must be allocated when we enter buffer prepare functions
838 and remain allocated while buffers are in the DMA queue.
839 2) This is a single frame read.
840 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
841 RESOURCE_OVERLAY is allocated.
842 4) This is a continuous read, implies VIDIOC_STREAMON.
844 Note this driver permits video input and standard changes regardless if
845 resources are allocated.
848 #define VBI_RESOURCES (RESOURCE_VBI)
849 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
850 RESOURCE_VIDEO_STREAM | \
854 int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
856 int xbits; /* mutual exclusive resources */
858 if (fh->resources & bit)
859 /* have it already allocated */
863 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
864 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
867 if (btv->resources & xbits) {
868 /* no, someone else uses it */
872 if ((bit & VIDEO_RESOURCES)
873 && 0 == (btv->resources & VIDEO_RESOURCES)) {
874 /* Do crop - use current, don't - use default parameters. */
875 __s32 top = btv->crop[!!fh->do_crop].rect.top;
877 if (btv->vbi_end > top)
880 /* We cannot capture the same line as video and VBI data.
881 Claim scan lines crop[].rect.top to bottom. */
882 btv->crop_start = top;
883 } else if (bit & VBI_RESOURCES) {
884 __s32 end = fh->vbi_fmt.end;
886 if (end > btv->crop_start)
889 /* Claim scan lines above fh->vbi_fmt.end. */
893 /* it's free, grab it */
894 fh->resources |= bit;
895 btv->resources |= bit;
903 int check_btres(struct bttv_fh *fh, int bit)
905 return (fh->resources & bit);
909 int locked_btres(struct bttv *btv, int bit)
911 return (btv->resources & bit);
914 /* Call with btv->lock down. */
916 disclaim_vbi_lines(struct bttv *btv)
921 /* Call with btv->lock down. */
923 disclaim_video_lines(struct bttv *btv)
925 const struct bttv_tvnorm *tvnorm;
928 tvnorm = &bttv_tvnorms[btv->tvnorm];
929 btv->crop_start = tvnorm->cropcap.bounds.top
930 + tvnorm->cropcap.bounds.height;
932 /* VBI capturing ends at VDELAY, start of video capturing, no
933 matter how many lines the VBI RISC program expects. When video
934 capturing is off, it shall no longer "preempt" VBI capturing,
935 so we set VDELAY to maximum. */
936 crop = btread(BT848_E_CROP) | 0xc0;
937 btwrite(crop, BT848_E_CROP);
938 btwrite(0xfe, BT848_E_VDELAY_LO);
939 btwrite(crop, BT848_O_CROP);
940 btwrite(0xfe, BT848_O_VDELAY_LO);
944 void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
946 if ((fh->resources & bits) != bits) {
947 /* trying to free resources not allocated by us ... */
948 pr_err("BUG! (btres)\n");
950 fh->resources &= ~bits;
951 btv->resources &= ~bits;
953 bits = btv->resources;
955 if (0 == (bits & VIDEO_RESOURCES))
956 disclaim_video_lines(btv);
958 if (0 == (bits & VBI_RESOURCES))
959 disclaim_vbi_lines(btv);
962 /* ----------------------------------------------------------------------- */
963 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
965 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
966 PLL_X = Reference pre-divider (0=1, 1=2)
967 PLL_C = Post divider (0=6, 1=4)
968 PLL_I = Integer input
969 PLL_F = Fractional input
971 F_input = 28.636363 MHz:
972 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
975 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
977 unsigned char fl, fh, fi;
979 /* prevent overflows */
992 btwrite(fl, BT848_PLL_F_LO);
993 btwrite(fh, BT848_PLL_F_HI);
994 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
997 static void set_pll(struct bttv *btv)
1001 if (!btv->pll.pll_crystal)
1004 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1005 dprintk("%d: PLL: no change required\n", btv->c.nr);
1009 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1011 if (btv->pll.pll_current == 0)
1014 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
1015 btv->c.nr, btv->pll.pll_ifreq);
1016 btwrite(0x00,BT848_TGCTRL);
1017 btwrite(0x00,BT848_PLL_XCI);
1018 btv->pll.pll_current = 0;
1023 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
1025 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1026 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1028 for (i=0; i<10; i++) {
1029 /* Let other people run while the PLL stabilizes */
1032 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1033 btwrite(0,BT848_DSTATUS);
1035 btwrite(0x08,BT848_TGCTRL);
1036 btv->pll.pll_current = btv->pll.pll_ofreq;
1038 pr_info("PLL set ok\n");
1042 btv->pll.pll_current = -1;
1044 pr_info("Setting PLL failed\n");
1048 /* used to switch between the bt848's analog/digital video capture modes */
1049 static void bt848A_set_timing(struct bttv *btv)
1052 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1053 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1055 if (btv->input == btv->dig) {
1056 dprintk("%d: load digital timing table (table_idx=%d)\n",
1057 btv->c.nr,table_idx);
1059 /* timing change...reset timing generator address */
1060 btwrite(0x00, BT848_TGCTRL);
1061 btwrite(0x02, BT848_TGCTRL);
1062 btwrite(0x00, BT848_TGCTRL);
1064 len=SRAM_Table[table_idx][0];
1065 for(i = 1; i <= len; i++)
1066 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1067 btv->pll.pll_ofreq = 27000000;
1070 btwrite(0x11, BT848_TGCTRL);
1071 btwrite(0x41, BT848_DVSIF);
1073 btv->pll.pll_ofreq = fsc;
1075 btwrite(0x0, BT848_DVSIF);
1079 /* ----------------------------------------------------------------------- */
1081 static void bt848_bright(struct bttv *btv, int bright)
1085 // printk("set bright: %d\n", bright); // DEBUG
1086 btv->bright = bright;
1088 /* We want -128 to 127 we get 0-65535 */
1089 value = (bright >> 8) - 128;
1090 btwrite(value & 0xff, BT848_BRIGHT);
1093 static void bt848_hue(struct bttv *btv, int hue)
1100 value = (hue >> 8) - 128;
1101 btwrite(value & 0xff, BT848_HUE);
1104 static void bt848_contrast(struct bttv *btv, int cont)
1108 btv->contrast = cont;
1111 value = (cont >> 7);
1112 hibit = (value >> 6) & 4;
1113 btwrite(value & 0xff, BT848_CONTRAST_LO);
1114 btaor(hibit, ~4, BT848_E_CONTROL);
1115 btaor(hibit, ~4, BT848_O_CONTROL);
1118 static void bt848_sat(struct bttv *btv, int color)
1120 int val_u,val_v,hibits;
1122 btv->saturation = color;
1124 /* 0-511 for the color */
1125 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1126 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1127 hibits = (val_u >> 7) & 2;
1128 hibits |= (val_v >> 8) & 1;
1129 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1130 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1131 btaor(hibits, ~3, BT848_E_CONTROL);
1132 btaor(hibits, ~3, BT848_O_CONTROL);
1135 /* ----------------------------------------------------------------------- */
1138 video_mux(struct bttv *btv, unsigned int input)
1142 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1145 /* needed by RemoteVideo MX */
1146 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1148 gpio_inout(mask2,mask2);
1150 if (input == btv->svhs) {
1151 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1152 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1154 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1155 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1157 mux = bttv_muxsel(btv, input);
1158 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1159 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
1161 /* card specific hook */
1162 if(bttv_tvcards[btv->c.type].muxsel_hook)
1163 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1167 static char *audio_modes[] = {
1168 "audio: tuner", "audio: radio", "audio: extern",
1169 "audio: intern", "audio: mute"
1173 audio_mux(struct bttv *btv, int input, int mute)
1175 int gpio_val, signal;
1176 struct v4l2_control ctrl;
1178 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1179 bttv_tvcards[btv->c.type].gpiomask);
1180 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1186 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1189 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1191 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1193 switch (btv->c.type) {
1194 case BTTV_BOARD_VOODOOTV_FM:
1195 case BTTV_BOARD_VOODOOTV_200:
1196 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1200 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1204 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1208 ctrl.id = V4L2_CID_AUDIO_MUTE;
1209 ctrl.value = btv->mute;
1210 bttv_call_all(btv, core, s_ctrl, &ctrl);
1211 if (btv->sd_msp34xx) {
1214 /* Note: the inputs tuner/radio/extern/intern are translated
1215 to msp routings. This assumes common behavior for all msp3400
1216 based TV cards. When this assumption fails, then the
1217 specific MSP routing must be added to the card table.
1218 For now this is sufficient. */
1220 case TVAUDIO_INPUT_RADIO:
1221 /* Some boards need the msp do to the radio demod */
1222 if (btv->radio_uses_msp_demodulator) {
1223 in = MSP_INPUT_DEFAULT;
1226 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1227 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1229 case TVAUDIO_INPUT_EXTERN:
1230 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1231 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1233 case TVAUDIO_INPUT_INTERN:
1234 /* Yes, this is the same input as for RADIO. I doubt
1235 if this is ever used. The only board with an INTERN
1236 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1237 that was tested. My guess is that the whole INTERN
1238 input does not work. */
1239 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1240 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1242 case TVAUDIO_INPUT_TUNER:
1244 /* This is the only card that uses TUNER2, and afaik,
1245 is the only difference between the VOODOOTV_FM
1247 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1248 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1249 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1251 in = MSP_INPUT_DEFAULT;
1254 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1255 in, MSP_OUTPUT_DEFAULT, 0);
1257 if (btv->sd_tvaudio) {
1258 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1265 audio_mute(struct bttv *btv, int mute)
1267 return audio_mux(btv, btv->audio, mute);
1271 audio_input(struct bttv *btv, int input)
1273 return audio_mux(btv, input, btv->mute);
1277 bttv_crop_calc_limits(struct bttv_crop *c)
1279 /* Scale factor min. 1:1, max. 16:1. Min. image size
1280 48 x 32. Scaled width must be a multiple of 4. */
1283 /* For bug compatibility with VIDIOCGCAP and image
1284 size checks in earlier driver versions. */
1285 c->min_scaled_width = 48;
1286 c->min_scaled_height = 32;
1288 c->min_scaled_width =
1289 (max(48, c->rect.width >> 4) + 3) & ~3;
1290 c->min_scaled_height =
1291 max(32, c->rect.height >> 4);
1294 c->max_scaled_width = c->rect.width & ~3;
1295 c->max_scaled_height = c->rect.height;
1299 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1301 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1302 bttv_crop_calc_limits(c);
1305 /* Call with btv->lock down. */
1307 set_tvnorm(struct bttv *btv, unsigned int norm)
1309 const struct bttv_tvnorm *tvnorm;
1312 BUG_ON(norm >= BTTV_TVNORMS);
1313 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
1315 tvnorm = &bttv_tvnorms[norm];
1317 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1318 sizeof (tvnorm->cropcap))) {
1319 bttv_crop_reset(&btv->crop[0], norm);
1320 btv->crop[1] = btv->crop[0]; /* current = default */
1322 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1323 btv->crop_start = tvnorm->cropcap.bounds.top
1324 + tvnorm->cropcap.bounds.height;
1330 btwrite(tvnorm->adelay, BT848_ADELAY);
1331 btwrite(tvnorm->bdelay, BT848_BDELAY);
1332 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1334 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1335 btwrite(1, BT848_VBI_PACK_DEL);
1336 bt848A_set_timing(btv);
1338 switch (btv->c.type) {
1339 case BTTV_BOARD_VOODOOTV_FM:
1340 case BTTV_BOARD_VOODOOTV_200:
1341 bttv_tda9880_setnorm(btv, gpio_read());
1344 id = tvnorm->v4l2_id;
1345 bttv_call_all(btv, core, s_std, id);
1350 /* Call with btv->lock down. */
1352 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1354 unsigned long flags;
1358 spin_lock_irqsave(&btv->s_lock,flags);
1359 if (btv->curr.frame_irq) {
1360 /* active capture -> delayed input switch */
1361 btv->new_input = input;
1363 video_mux(btv,input);
1365 spin_unlock_irqrestore(&btv->s_lock,flags);
1367 video_mux(btv,input);
1369 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1370 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
1371 set_tvnorm(btv, norm);
1374 static void init_irqreg(struct bttv *btv)
1377 btwrite(0xfffffUL, BT848_INT_STAT);
1379 if (bttv_tvcards[btv->c.type].no_video) {
1381 btwrite(BT848_INT_I2CDONE,
1385 btwrite((btv->triton1) |
1386 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1388 (fdsr ? BT848_INT_FDSR : 0) |
1389 BT848_INT_RISCI | BT848_INT_OCERR |
1390 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1396 static void init_bt848(struct bttv *btv)
1400 if (bttv_tvcards[btv->c.type].no_video) {
1401 /* very basic init only */
1406 btwrite(0x00, BT848_CAP_CTL);
1407 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1408 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1410 /* set planar and packed mode trigger points and */
1411 /* set rising edge of inverted GPINTR pin as irq trigger */
1412 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1413 BT848_GPIO_DMA_CTL_PLTP1_16|
1414 BT848_GPIO_DMA_CTL_PLTP23_16|
1415 BT848_GPIO_DMA_CTL_GPINTC|
1416 BT848_GPIO_DMA_CTL_GPINTI,
1417 BT848_GPIO_DMA_CTL);
1419 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1420 btwrite(val, BT848_E_SCLOOP);
1421 btwrite(val, BT848_O_SCLOOP);
1423 btwrite(0x20, BT848_E_VSCALE_HI);
1424 btwrite(0x20, BT848_O_VSCALE_HI);
1425 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1428 btwrite(whitecrush_upper, BT848_WC_UP);
1429 btwrite(whitecrush_lower, BT848_WC_DOWN);
1431 if (btv->opt_lumafilter) {
1432 btwrite(0, BT848_E_CONTROL);
1433 btwrite(0, BT848_O_CONTROL);
1435 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1436 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1439 bt848_bright(btv, btv->bright);
1440 bt848_hue(btv, btv->hue);
1441 bt848_contrast(btv, btv->contrast);
1442 bt848_sat(btv, btv->saturation);
1448 static void bttv_reinit_bt848(struct bttv *btv)
1450 unsigned long flags;
1453 pr_info("%d: reset, reinitialize\n", btv->c.nr);
1454 spin_lock_irqsave(&btv->s_lock,flags);
1456 bttv_set_dma(btv,0);
1457 spin_unlock_irqrestore(&btv->s_lock,flags);
1460 btv->pll.pll_current = -1;
1461 set_input(btv, btv->input, btv->tvnorm);
1464 static int bttv_g_ctrl(struct file *file, void *priv,
1465 struct v4l2_control *c)
1467 struct bttv_fh *fh = priv;
1468 struct bttv *btv = fh->btv;
1471 case V4L2_CID_BRIGHTNESS:
1472 c->value = btv->bright;
1475 c->value = btv->hue;
1477 case V4L2_CID_CONTRAST:
1478 c->value = btv->contrast;
1480 case V4L2_CID_SATURATION:
1481 c->value = btv->saturation;
1484 case V4L2_CID_AUDIO_MUTE:
1485 case V4L2_CID_AUDIO_VOLUME:
1486 case V4L2_CID_AUDIO_BALANCE:
1487 case V4L2_CID_AUDIO_BASS:
1488 case V4L2_CID_AUDIO_TREBLE:
1489 bttv_call_all(btv, core, g_ctrl, c);
1492 case V4L2_CID_PRIVATE_CHROMA_AGC:
1493 c->value = btv->opt_chroma_agc;
1495 case V4L2_CID_PRIVATE_COMBFILTER:
1496 c->value = btv->opt_combfilter;
1498 case V4L2_CID_PRIVATE_LUMAFILTER:
1499 c->value = btv->opt_lumafilter;
1501 case V4L2_CID_PRIVATE_AUTOMUTE:
1502 c->value = btv->opt_automute;
1504 case V4L2_CID_PRIVATE_AGC_CRUSH:
1505 c->value = btv->opt_adc_crush;
1507 case V4L2_CID_PRIVATE_VCR_HACK:
1508 c->value = btv->opt_vcr_hack;
1510 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1511 c->value = btv->opt_whitecrush_upper;
1513 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1514 c->value = btv->opt_whitecrush_lower;
1516 case V4L2_CID_PRIVATE_UV_RATIO:
1517 c->value = btv->opt_uv_ratio;
1519 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1520 c->value = btv->opt_full_luma_range;
1522 case V4L2_CID_PRIVATE_CORING:
1523 c->value = btv->opt_coring;
1531 static int bttv_s_ctrl(struct file *file, void *f,
1532 struct v4l2_control *c)
1536 struct bttv_fh *fh = f;
1537 struct bttv *btv = fh->btv;
1539 err = v4l2_prio_check(&btv->prio, fh->prio);
1544 case V4L2_CID_BRIGHTNESS:
1545 bt848_bright(btv, c->value);
1548 bt848_hue(btv, c->value);
1550 case V4L2_CID_CONTRAST:
1551 bt848_contrast(btv, c->value);
1553 case V4L2_CID_SATURATION:
1554 bt848_sat(btv, c->value);
1556 case V4L2_CID_AUDIO_MUTE:
1557 audio_mute(btv, c->value);
1559 case V4L2_CID_AUDIO_VOLUME:
1560 if (btv->volume_gpio)
1561 btv->volume_gpio(btv, c->value);
1563 bttv_call_all(btv, core, s_ctrl, c);
1565 case V4L2_CID_AUDIO_BALANCE:
1566 case V4L2_CID_AUDIO_BASS:
1567 case V4L2_CID_AUDIO_TREBLE:
1568 bttv_call_all(btv, core, s_ctrl, c);
1571 case V4L2_CID_PRIVATE_CHROMA_AGC:
1572 btv->opt_chroma_agc = c->value;
1573 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1574 btwrite(val, BT848_E_SCLOOP);
1575 btwrite(val, BT848_O_SCLOOP);
1577 case V4L2_CID_PRIVATE_COMBFILTER:
1578 btv->opt_combfilter = c->value;
1580 case V4L2_CID_PRIVATE_LUMAFILTER:
1581 btv->opt_lumafilter = c->value;
1582 if (btv->opt_lumafilter) {
1583 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1584 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1586 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1587 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1590 case V4L2_CID_PRIVATE_AUTOMUTE:
1591 btv->opt_automute = c->value;
1593 case V4L2_CID_PRIVATE_AGC_CRUSH:
1594 btv->opt_adc_crush = c->value;
1595 btwrite(BT848_ADC_RESERVED |
1596 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1599 case V4L2_CID_PRIVATE_VCR_HACK:
1600 btv->opt_vcr_hack = c->value;
1602 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1603 btv->opt_whitecrush_upper = c->value;
1604 btwrite(c->value, BT848_WC_UP);
1606 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1607 btv->opt_whitecrush_lower = c->value;
1608 btwrite(c->value, BT848_WC_DOWN);
1610 case V4L2_CID_PRIVATE_UV_RATIO:
1611 btv->opt_uv_ratio = c->value;
1612 bt848_sat(btv, btv->saturation);
1614 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1615 btv->opt_full_luma_range = c->value;
1616 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1618 case V4L2_CID_PRIVATE_CORING:
1619 btv->opt_coring = c->value;
1620 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1628 /* ----------------------------------------------------------------------- */
1630 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1632 unsigned int outbits, data;
1633 outbits = btread(BT848_GPIO_OUT_EN);
1634 data = btread(BT848_GPIO_DATA);
1635 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1636 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
1639 static void bttv_field_count(struct bttv *btv)
1647 /* start field counter */
1648 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1650 /* stop field counter */
1651 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1652 btv->field_count = 0;
1656 static const struct bttv_format*
1657 format_by_fourcc(int fourcc)
1661 for (i = 0; i < FORMATS; i++) {
1662 if (-1 == formats[i].fourcc)
1664 if (formats[i].fourcc == fourcc)
1670 /* ----------------------------------------------------------------------- */
1674 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1675 struct bttv_buffer *new)
1677 struct bttv_buffer *old;
1678 unsigned long flags;
1681 dprintk("switch_overlay: enter [new=%p]\n", new);
1683 new->vb.state = VIDEOBUF_DONE;
1684 spin_lock_irqsave(&btv->s_lock,flags);
1688 bttv_set_dma(btv, 0x03);
1689 spin_unlock_irqrestore(&btv->s_lock,flags);
1691 dprintk("switch_overlay: old=%p state is %d\n",
1692 old, old->vb.state);
1693 bttv_dma_free(&fh->cap,btv, old);
1697 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
1698 dprintk("switch_overlay: done\n");
1702 /* ----------------------------------------------------------------------- */
1703 /* video4linux (1) interface */
1705 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1706 struct bttv_buffer *buf,
1707 const struct bttv_format *fmt,
1708 unsigned int width, unsigned int height,
1709 enum v4l2_field field)
1711 struct bttv_fh *fh = q->priv_data;
1712 int redo_dma_risc = 0;
1717 /* check settings */
1720 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1722 height = RAW_LINES*2;
1723 if (width*height > buf->vb.bsize)
1725 buf->vb.size = buf->vb.bsize;
1727 /* Make sure tvnorm and vbi_end remain consistent
1728 until we're done. */
1732 /* In this mode capturing always starts at defrect.top
1733 (default VDELAY), ignoring cropping parameters. */
1734 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1738 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1741 c = btv->crop[!!fh->do_crop];
1743 if (width < c.min_scaled_width ||
1744 width > c.max_scaled_width ||
1745 height < c.min_scaled_height)
1749 case V4L2_FIELD_TOP:
1750 case V4L2_FIELD_BOTTOM:
1751 case V4L2_FIELD_ALTERNATE:
1752 /* btv->crop counts frame lines. Max. scale
1753 factor is 16:1 for frames, 8:1 for fields. */
1754 if (height * 2 > c.max_scaled_height)
1759 if (height > c.max_scaled_height)
1764 buf->vb.size = (width * height * fmt->depth) >> 3;
1765 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1769 /* alloc + fill struct bttv_buffer (if changed) */
1770 if (buf->vb.width != width || buf->vb.height != height ||
1771 buf->vb.field != field ||
1772 buf->tvnorm != norm || buf->fmt != fmt ||
1773 buf->crop.top != c.rect.top ||
1774 buf->crop.left != c.rect.left ||
1775 buf->crop.width != c.rect.width ||
1776 buf->crop.height != c.rect.height) {
1777 buf->vb.width = width;
1778 buf->vb.height = height;
1779 buf->vb.field = field;
1786 /* alloc risc memory */
1787 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1789 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1794 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1797 buf->vb.state = VIDEOBUF_PREPARED;
1801 bttv_dma_free(q,btv,buf);
1806 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1808 struct bttv_fh *fh = q->priv_data;
1810 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1813 if (*size * *count > gbuffers * gbufsize)
1814 *count = (gbuffers * gbufsize) / *size;
1819 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1820 enum v4l2_field field)
1822 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1823 struct bttv_fh *fh = q->priv_data;
1825 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1826 fh->width, fh->height, field);
1830 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1832 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1833 struct bttv_fh *fh = q->priv_data;
1834 struct bttv *btv = fh->btv;
1836 buf->vb.state = VIDEOBUF_QUEUED;
1837 list_add_tail(&buf->vb.queue,&btv->capture);
1838 if (!btv->curr.frame_irq) {
1840 bttv_set_dma(btv, 0x03);
1844 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1846 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1847 struct bttv_fh *fh = q->priv_data;
1849 bttv_dma_free(q,fh->btv,buf);
1852 static struct videobuf_queue_ops bttv_video_qops = {
1853 .buf_setup = buffer_setup,
1854 .buf_prepare = buffer_prepare,
1855 .buf_queue = buffer_queue,
1856 .buf_release = buffer_release,
1859 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
1861 struct bttv_fh *fh = priv;
1862 struct bttv *btv = fh->btv;
1866 err = v4l2_prio_check(&btv->prio, fh->prio);
1870 for (i = 0; i < BTTV_TVNORMS; i++)
1871 if (*id & bttv_tvnorms[i].v4l2_id)
1873 if (i == BTTV_TVNORMS) {
1885 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1887 struct bttv_fh *fh = f;
1888 struct bttv *btv = fh->btv;
1890 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1891 *id = V4L2_STD_625_50;
1893 *id = V4L2_STD_525_60;
1897 static int bttv_enum_input(struct file *file, void *priv,
1898 struct v4l2_input *i)
1900 struct bttv_fh *fh = priv;
1901 struct bttv *btv = fh->btv;
1904 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1909 i->type = V4L2_INPUT_TYPE_CAMERA;
1912 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1913 sprintf(i->name, "Television");
1914 i->type = V4L2_INPUT_TYPE_TUNER;
1916 } else if (i->index == btv->svhs) {
1917 sprintf(i->name, "S-Video");
1919 sprintf(i->name, "Composite%d", i->index);
1922 if (i->index == btv->input) {
1923 __u32 dstatus = btread(BT848_DSTATUS);
1924 if (0 == (dstatus & BT848_DSTATUS_PRES))
1925 i->status |= V4L2_IN_ST_NO_SIGNAL;
1926 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1927 i->status |= V4L2_IN_ST_NO_H_LOCK;
1930 i->std = BTTV_NORMS;
1937 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1939 struct bttv_fh *fh = priv;
1940 struct bttv *btv = fh->btv;
1947 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1949 struct bttv_fh *fh = priv;
1950 struct bttv *btv = fh->btv;
1954 err = v4l2_prio_check(&btv->prio, fh->prio);
1958 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1963 set_input(btv, i, btv->tvnorm);
1969 static int bttv_s_tuner(struct file *file, void *priv,
1970 struct v4l2_tuner *t)
1972 struct bttv_fh *fh = priv;
1973 struct bttv *btv = fh->btv;
1976 if (unlikely(0 != t->index))
1979 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
1984 err = v4l2_prio_check(&btv->prio, fh->prio);
1988 bttv_call_all(btv, tuner, s_tuner, t);
1990 if (btv->audio_mode_gpio)
1991 btv->audio_mode_gpio(btv, t, 1);
1998 static int bttv_g_frequency(struct file *file, void *priv,
1999 struct v4l2_frequency *f)
2001 struct bttv_fh *fh = priv;
2002 struct bttv *btv = fh->btv;
2004 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2005 f->frequency = btv->freq;
2010 static int bttv_s_frequency(struct file *file, void *priv,
2011 struct v4l2_frequency *f)
2013 struct bttv_fh *fh = priv;
2014 struct bttv *btv = fh->btv;
2017 if (unlikely(f->tuner != 0))
2020 err = v4l2_prio_check(&btv->prio, fh->prio);
2024 if (unlikely(f->type != (btv->radio_user
2025 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2029 btv->freq = f->frequency;
2030 bttv_call_all(btv, tuner, s_frequency, f);
2031 if (btv->has_matchbox && btv->radio_user)
2032 tea5757_set_freq(btv, btv->freq);
2038 static int bttv_log_status(struct file *file, void *f)
2040 struct bttv_fh *fh = f;
2041 struct bttv *btv = fh->btv;
2043 bttv_call_all(btv, core, log_status);
2047 #ifdef CONFIG_VIDEO_ADV_DEBUG
2048 static int bttv_g_register(struct file *file, void *f,
2049 struct v4l2_dbg_register *reg)
2051 struct bttv_fh *fh = f;
2052 struct bttv *btv = fh->btv;
2054 if (!capable(CAP_SYS_ADMIN))
2057 if (!v4l2_chip_match_host(®->match))
2060 /* bt848 has a 12-bit register space */
2062 reg->val = btread(reg->reg);
2068 static int bttv_s_register(struct file *file, void *f,
2069 struct v4l2_dbg_register *reg)
2071 struct bttv_fh *fh = f;
2072 struct bttv *btv = fh->btv;
2074 if (!capable(CAP_SYS_ADMIN))
2077 if (!v4l2_chip_match_host(®->match))
2080 /* bt848 has a 12-bit register space */
2082 btwrite(reg->val, reg->reg);
2088 /* Given cropping boundaries b and the scaled width and height of a
2089 single field or frame, which must not exceed hardware limits, this
2090 function adjusts the cropping parameters c. */
2092 bttv_crop_adjust (struct bttv_crop * c,
2093 const struct v4l2_rect * b,
2096 enum v4l2_field field)
2098 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2102 if (width < c->min_scaled_width) {
2103 /* Max. hor. scale factor 16:1. */
2104 c->rect.width = width * 16;
2105 } else if (width > c->max_scaled_width) {
2106 /* Min. hor. scale factor 1:1. */
2107 c->rect.width = width;
2109 max_left = b->left + b->width - width;
2110 max_left = min(max_left, (__s32) MAX_HDELAY);
2111 if (c->rect.left > max_left)
2112 c->rect.left = max_left;
2115 if (height < c->min_scaled_height) {
2116 /* Max. vert. scale factor 16:1, single fields 8:1. */
2117 c->rect.height = height * 16;
2118 } else if (frame_height > c->max_scaled_height) {
2119 /* Min. vert. scale factor 1:1.
2120 Top and height count field lines times two. */
2121 c->rect.height = (frame_height + 1) & ~1;
2123 max_top = b->top + b->height - c->rect.height;
2124 if (c->rect.top > max_top)
2125 c->rect.top = max_top;
2128 bttv_crop_calc_limits(c);
2131 /* Returns an error if scaling to a frame or single field with the given
2132 width and height is not possible with the current cropping parameters
2133 and width aligned according to width_mask. If adjust_size is TRUE the
2134 function may adjust the width and/or height instead, rounding width
2135 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2136 also adjust the current cropping parameters to get closer to the
2137 desired image size. */
2139 limit_scaled_size_lock (struct bttv_fh * fh,
2142 enum v4l2_field field,
2143 unsigned int width_mask,
2144 unsigned int width_bias,
2148 struct bttv *btv = fh->btv;
2149 const struct v4l2_rect *b;
2150 struct bttv_crop *c;
2157 BUG_ON((int) width_mask >= 0 ||
2158 width_bias >= (unsigned int) -width_mask);
2160 /* Make sure tvnorm, vbi_end and the current cropping parameters
2161 remain consistent until we're done. */
2163 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2165 /* Do crop - use current, don't - use default parameters. */
2166 c = &btv->crop[!!fh->do_crop];
2171 && !locked_btres(btv, VIDEO_RESOURCES)) {
2175 /* We cannot scale up. When the scaled image is larger
2176 than crop.rect we adjust the crop.rect as required
2177 by the V4L2 spec, hence cropcap.bounds are our limit. */
2178 max_width = min(b->width, (__s32) MAX_HACTIVE);
2179 max_height = b->height;
2181 /* We cannot capture the same line as video and VBI data.
2182 Note btv->vbi_end is really a minimum, see
2183 bttv_vbi_try_fmt(). */
2184 if (btv->vbi_end > b->top) {
2185 max_height -= btv->vbi_end - b->top;
2187 if (min_height > max_height)
2192 if (btv->vbi_end > c->rect.top)
2195 min_width = c->min_scaled_width;
2196 min_height = c->min_scaled_height;
2197 max_width = c->max_scaled_width;
2198 max_height = c->max_scaled_height;
2203 min_width = (min_width - width_mask - 1) & width_mask;
2204 max_width = max_width & width_mask;
2206 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2207 min_height = min_height;
2208 /* Min. scale factor is 1:1. */
2209 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2212 *width = clamp(*width, min_width, max_width);
2213 *height = clamp(*height, min_height, max_height);
2215 /* Round after clamping to avoid overflow. */
2216 *width = (*width + width_bias) & width_mask;
2219 bttv_crop_adjust(c, b, *width, *height, field);
2221 if (btv->vbi_end > c->rect.top) {
2222 /* Move the crop window out of the way. */
2223 c->rect.top = btv->vbi_end;
2228 if (*width < min_width ||
2229 *height < min_height ||
2230 *width > max_width ||
2231 *height > max_height ||
2232 0 != (*width & ~width_mask))
2236 rc = 0; /* success */
2243 /* Returns an error if the given overlay window dimensions are not
2244 possible with the current cropping parameters. If adjust_size is
2245 TRUE the function may adjust the window width and/or height
2246 instead, however it always rounds the horizontal position and
2247 width as btcx_align() does. If adjust_crop is TRUE the function
2248 may also adjust the current cropping parameters to get closer
2249 to the desired window size. */
2251 verify_window_lock (struct bttv_fh * fh,
2252 struct v4l2_window * win,
2256 enum v4l2_field field;
2257 unsigned int width_mask;
2260 if (win->w.width < 48 || win->w.height < 32)
2262 if (win->clipcount > 2048)
2267 if (V4L2_FIELD_ANY == field) {
2270 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2271 field = (win->w.height > height2)
2272 ? V4L2_FIELD_INTERLACED
2276 case V4L2_FIELD_TOP:
2277 case V4L2_FIELD_BOTTOM:
2278 case V4L2_FIELD_INTERLACED:
2284 /* 4-byte alignment. */
2285 if (NULL == fh->ovfmt)
2288 switch (fh->ovfmt->depth) {
2302 win->w.width -= win->w.left & ~width_mask;
2303 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2305 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
2307 /* width_bias: round down */ 0,
2308 adjust_size, adjust_crop);
2316 static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
2317 struct v4l2_window *win, int fixup)
2319 struct v4l2_clip *clips = NULL;
2320 int n,size,retval = 0;
2322 if (NULL == fh->ovfmt)
2324 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2326 retval = verify_window_lock(fh, win,
2327 /* adjust_size */ fixup,
2328 /* adjust_crop */ fixup);
2332 /* copy clips -- luckily v4l1 + v4l2 are binary
2333 compatible here ...*/
2335 size = sizeof(*clips)*(n+4);
2336 clips = kmalloc(size,GFP_KERNEL);
2340 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2346 /* clip against screen */
2347 if (NULL != btv->fbuf.base)
2348 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2350 btcx_sort_clips(clips,n);
2352 /* 4-byte alignments */
2353 switch (fh->ovfmt->depth) {
2356 btcx_align(&win->w, clips, n, 3);
2359 btcx_align(&win->w, clips, n, 1);
2362 /* no alignment fixups needed */
2368 kfree(fh->ov.clips);
2369 fh->ov.clips = clips;
2373 fh->ov.field = win->field;
2374 fh->ov.setup_ok = 1;
2376 btv->init.ov.w.width = win->w.width;
2377 btv->init.ov.w.height = win->w.height;
2378 btv->init.ov.field = win->field;
2380 /* update overlay if needed */
2382 if (check_btres(fh, RESOURCE_OVERLAY)) {
2383 struct bttv_buffer *new;
2385 new = videobuf_sg_alloc(sizeof(*new));
2386 new->crop = btv->crop[!!fh->do_crop].rect;
2387 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2388 retval = bttv_switch_overlay(btv,fh,new);
2393 /* ----------------------------------------------------------------------- */
2395 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2397 struct videobuf_queue* q = NULL;
2400 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2403 case V4L2_BUF_TYPE_VBI_CAPTURE:
2412 static int bttv_resource(struct bttv_fh *fh)
2417 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2418 res = RESOURCE_VIDEO_STREAM;
2420 case V4L2_BUF_TYPE_VBI_CAPTURE:
2429 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2431 struct videobuf_queue *q = bttv_queue(fh);
2432 int res = bttv_resource(fh);
2434 if (check_btres(fh,res))
2436 if (videobuf_queue_is_busy(q))
2443 pix_format_set_size (struct v4l2_pix_format * f,
2444 const struct bttv_format * fmt,
2446 unsigned int height)
2451 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2452 f->bytesperline = width; /* Y plane */
2453 f->sizeimage = (width * height * fmt->depth) >> 3;
2455 f->bytesperline = (width * fmt->depth) >> 3;
2456 f->sizeimage = height * f->bytesperline;
2460 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2461 struct v4l2_format *f)
2463 struct bttv_fh *fh = priv;
2465 pix_format_set_size(&f->fmt.pix, fh->fmt,
2466 fh->width, fh->height);
2467 f->fmt.pix.field = fh->cap.field;
2468 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2473 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2474 struct v4l2_format *f)
2476 struct bttv_fh *fh = priv;
2478 f->fmt.win.w = fh->ov.w;
2479 f->fmt.win.field = fh->ov.field;
2484 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2485 struct v4l2_format *f)
2487 const struct bttv_format *fmt;
2488 struct bttv_fh *fh = priv;
2489 struct bttv *btv = fh->btv;
2490 enum v4l2_field field;
2491 __s32 width, height;
2494 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2498 field = f->fmt.pix.field;
2500 if (V4L2_FIELD_ANY == field) {
2503 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2504 field = (f->fmt.pix.height > height2)
2505 ? V4L2_FIELD_INTERLACED
2506 : V4L2_FIELD_BOTTOM;
2509 if (V4L2_FIELD_SEQ_BT == field)
2510 field = V4L2_FIELD_SEQ_TB;
2513 case V4L2_FIELD_TOP:
2514 case V4L2_FIELD_BOTTOM:
2515 case V4L2_FIELD_ALTERNATE:
2516 case V4L2_FIELD_INTERLACED:
2518 case V4L2_FIELD_SEQ_TB:
2519 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2526 width = f->fmt.pix.width;
2527 height = f->fmt.pix.height;
2529 rc = limit_scaled_size_lock(fh, &width, &height, field,
2530 /* width_mask: 4 pixels */ ~3,
2531 /* width_bias: nearest */ 2,
2532 /* adjust_size */ 1,
2533 /* adjust_crop */ 0);
2537 /* update data for the application */
2538 f->fmt.pix.field = field;
2539 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2544 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2545 struct v4l2_format *f)
2547 struct bttv_fh *fh = priv;
2549 return verify_window_lock(fh, &f->fmt.win,
2550 /* adjust_size */ 1,
2551 /* adjust_crop */ 0);
2554 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2555 struct v4l2_format *f)
2558 const struct bttv_format *fmt;
2559 struct bttv_fh *fh = priv;
2560 struct bttv *btv = fh->btv;
2561 __s32 width, height;
2562 enum v4l2_field field;
2564 retval = bttv_switch_type(fh, f->type);
2568 retval = bttv_try_fmt_vid_cap(file, priv, f);
2572 width = f->fmt.pix.width;
2573 height = f->fmt.pix.height;
2574 field = f->fmt.pix.field;
2576 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
2577 /* width_mask: 4 pixels */ ~3,
2578 /* width_bias: nearest */ 2,
2579 /* adjust_size */ 1,
2580 /* adjust_crop */ 1);
2584 f->fmt.pix.field = field;
2586 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2588 /* update our state informations */
2590 fh->cap.field = f->fmt.pix.field;
2591 fh->cap.last = V4L2_FIELD_NONE;
2592 fh->width = f->fmt.pix.width;
2593 fh->height = f->fmt.pix.height;
2594 btv->init.fmt = fmt;
2595 btv->init.width = f->fmt.pix.width;
2596 btv->init.height = f->fmt.pix.height;
2601 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2602 struct v4l2_format *f)
2604 struct bttv_fh *fh = priv;
2605 struct bttv *btv = fh->btv;
2607 if (no_overlay > 0) {
2608 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2612 return setup_window_lock(fh, btv, &f->fmt.win, 1);
2615 static int bttv_querycap(struct file *file, void *priv,
2616 struct v4l2_capability *cap)
2618 struct bttv_fh *fh = priv;
2619 struct bttv *btv = fh->btv;
2624 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2625 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2626 snprintf(cap->bus_info, sizeof(cap->bus_info),
2627 "PCI:%s", pci_name(btv->c.pci));
2629 V4L2_CAP_VIDEO_CAPTURE |
2630 V4L2_CAP_VBI_CAPTURE |
2631 V4L2_CAP_READWRITE |
2633 if (no_overlay <= 0)
2634 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2637 * No need to lock here: those vars are initialized during board
2638 * probe and remains untouched during the rest of the driver lifecycle
2640 if (btv->has_saa6588)
2641 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2642 if (btv->tuner_type != TUNER_ABSENT)
2643 cap->capabilities |= V4L2_CAP_TUNER;
2647 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2651 for (i = 0; i < FORMATS; i++) {
2652 if (formats[i].fourcc != -1)
2654 if ((unsigned int)index == f->index)
2660 f->pixelformat = formats[i].fourcc;
2661 strlcpy(f->description, formats[i].name, sizeof(f->description));
2666 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2667 struct v4l2_fmtdesc *f)
2669 int rc = bttv_enum_fmt_cap_ovr(f);
2677 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
2678 struct v4l2_fmtdesc *f)
2682 if (no_overlay > 0) {
2683 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2687 rc = bttv_enum_fmt_cap_ovr(f);
2692 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
2698 static int bttv_g_fbuf(struct file *file, void *f,
2699 struct v4l2_framebuffer *fb)
2701 struct bttv_fh *fh = f;
2702 struct bttv *btv = fh->btv;
2705 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2707 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2711 static int bttv_overlay(struct file *file, void *f, unsigned int on)
2713 struct bttv_fh *fh = f;
2714 struct bttv *btv = fh->btv;
2715 struct bttv_buffer *new;
2720 if (unlikely(!btv->fbuf.base)) {
2723 if (unlikely(!fh->ov.setup_ok)) {
2724 dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
2731 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
2735 fh->ov.tvnorm = btv->tvnorm;
2736 new = videobuf_sg_alloc(sizeof(*new));
2737 new->crop = btv->crop[!!fh->do_crop].rect;
2738 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2744 retval = bttv_switch_overlay(btv, fh, new);
2748 static int bttv_s_fbuf(struct file *file, void *f,
2749 struct v4l2_framebuffer *fb)
2751 struct bttv_fh *fh = f;
2752 struct bttv *btv = fh->btv;
2753 const struct bttv_format *fmt;
2756 if (!capable(CAP_SYS_ADMIN) &&
2757 !capable(CAP_SYS_RAWIO))
2761 fmt = format_by_fourcc(fb->fmt.pixelformat);
2764 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2768 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2769 __s32 width = fb->fmt.width;
2770 __s32 height = fb->fmt.height;
2772 retval = limit_scaled_size_lock(fh, &width, &height,
2773 V4L2_FIELD_INTERLACED,
2774 /* width_mask */ ~3,
2776 /* adjust_size */ 0,
2777 /* adjust_crop */ 0);
2783 btv->fbuf.base = fb->base;
2784 btv->fbuf.fmt.width = fb->fmt.width;
2785 btv->fbuf.fmt.height = fb->fmt.height;
2786 if (0 != fb->fmt.bytesperline)
2787 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2789 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2793 btv->init.ovfmt = fmt;
2794 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2797 fh->ov.w.width = fb->fmt.width;
2798 fh->ov.w.height = fb->fmt.height;
2799 btv->init.ov.w.width = fb->fmt.width;
2800 btv->init.ov.w.height = fb->fmt.height;
2801 kfree(fh->ov.clips);
2802 fh->ov.clips = NULL;
2805 if (check_btres(fh, RESOURCE_OVERLAY)) {
2806 struct bttv_buffer *new;
2808 new = videobuf_sg_alloc(sizeof(*new));
2809 new->crop = btv->crop[!!fh->do_crop].rect;
2810 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2811 retval = bttv_switch_overlay(btv, fh, new);
2817 static int bttv_reqbufs(struct file *file, void *priv,
2818 struct v4l2_requestbuffers *p)
2820 struct bttv_fh *fh = priv;
2821 return videobuf_reqbufs(bttv_queue(fh), p);
2824 static int bttv_querybuf(struct file *file, void *priv,
2825 struct v4l2_buffer *b)
2827 struct bttv_fh *fh = priv;
2828 return videobuf_querybuf(bttv_queue(fh), b);
2831 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2833 struct bttv_fh *fh = priv;
2834 struct bttv *btv = fh->btv;
2835 int res = bttv_resource(fh);
2837 if (!check_alloc_btres_lock(btv, fh, res))
2840 return videobuf_qbuf(bttv_queue(fh), b);
2843 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2845 struct bttv_fh *fh = priv;
2846 return videobuf_dqbuf(bttv_queue(fh), b,
2847 file->f_flags & O_NONBLOCK);
2850 static int bttv_streamon(struct file *file, void *priv,
2851 enum v4l2_buf_type type)
2853 struct bttv_fh *fh = priv;
2854 struct bttv *btv = fh->btv;
2855 int res = bttv_resource(fh);
2857 if (!check_alloc_btres_lock(btv, fh, res))
2859 return videobuf_streamon(bttv_queue(fh));
2863 static int bttv_streamoff(struct file *file, void *priv,
2864 enum v4l2_buf_type type)
2866 struct bttv_fh *fh = priv;
2867 struct bttv *btv = fh->btv;
2869 int res = bttv_resource(fh);
2872 retval = videobuf_streamoff(bttv_queue(fh));
2875 free_btres_lock(btv, fh, res);
2879 static int bttv_queryctrl(struct file *file, void *priv,
2880 struct v4l2_queryctrl *c)
2882 struct bttv_fh *fh = priv;
2883 struct bttv *btv = fh->btv;
2884 const struct v4l2_queryctrl *ctrl;
2886 if ((c->id < V4L2_CID_BASE ||
2887 c->id >= V4L2_CID_LASTP1) &&
2888 (c->id < V4L2_CID_PRIVATE_BASE ||
2889 c->id >= V4L2_CID_PRIVATE_LASTP1))
2892 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2895 ctrl = ctrl_by_id(c->id);
2897 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2903 static int bttv_g_parm(struct file *file, void *f,
2904 struct v4l2_streamparm *parm)
2906 struct bttv_fh *fh = f;
2907 struct bttv *btv = fh->btv;
2909 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2910 &parm->parm.capture.timeperframe);
2915 static int bttv_g_tuner(struct file *file, void *priv,
2916 struct v4l2_tuner *t)
2918 struct bttv_fh *fh = priv;
2919 struct bttv *btv = fh->btv;
2921 if (btv->tuner_type == TUNER_ABSENT)
2926 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2927 bttv_call_all(btv, tuner, g_tuner, t);
2928 strcpy(t->name, "Television");
2929 t->capability = V4L2_TUNER_CAP_NORM;
2930 t->type = V4L2_TUNER_ANALOG_TV;
2931 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2934 if (btv->audio_mode_gpio)
2935 btv->audio_mode_gpio(btv, t, 0);
2940 static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2942 struct bttv_fh *fh = f;
2943 struct bttv *btv = fh->btv;
2945 *p = v4l2_prio_max(&btv->prio);
2950 static int bttv_s_priority(struct file *file, void *f,
2951 enum v4l2_priority prio)
2953 struct bttv_fh *fh = f;
2954 struct bttv *btv = fh->btv;
2957 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
2962 static int bttv_cropcap(struct file *file, void *priv,
2963 struct v4l2_cropcap *cap)
2965 struct bttv_fh *fh = priv;
2966 struct bttv *btv = fh->btv;
2968 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2969 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2972 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
2977 static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
2979 struct bttv_fh *fh = f;
2980 struct bttv *btv = fh->btv;
2982 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2983 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2986 /* No fh->do_crop = 1; because btv->crop[1] may be
2987 inconsistent with fh->width or fh->height and apps
2988 do not expect a change here. */
2990 crop->c = btv->crop[!!fh->do_crop].rect;
2995 static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
2997 struct bttv_fh *fh = f;
2998 struct bttv *btv = fh->btv;
2999 const struct v4l2_rect *b;
3007 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3008 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3011 /* Make sure tvnorm, vbi_end and the current cropping
3012 parameters remain consistent until we're done. Note
3013 read() may change vbi_end in check_alloc_btres_lock(). */
3014 retval = v4l2_prio_check(&btv->prio, fh->prio);
3021 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3025 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3028 b_right = b_left + b->width;
3029 b_bottom = b->top + b->height;
3031 b_top = max(b->top, btv->vbi_end);
3032 if (b_top + 32 >= b_bottom) {
3036 /* Min. scaled size 48 x 32. */
3037 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3038 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3040 c.rect.width = clamp(crop->c.width,
3041 48, b_right - c.rect.left);
3043 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3044 /* Top and height must be a multiple of two. */
3045 c.rect.top = (c.rect.top + 1) & ~1;
3047 c.rect.height = clamp(crop->c.height,
3048 32, b_bottom - c.rect.top);
3049 c.rect.height = (c.rect.height + 1) & ~1;
3051 bttv_crop_calc_limits(&c);
3057 if (fh->width < c.min_scaled_width) {
3058 fh->width = c.min_scaled_width;
3059 btv->init.width = c.min_scaled_width;
3060 } else if (fh->width > c.max_scaled_width) {
3061 fh->width = c.max_scaled_width;
3062 btv->init.width = c.max_scaled_width;
3065 if (fh->height < c.min_scaled_height) {
3066 fh->height = c.min_scaled_height;
3067 btv->init.height = c.min_scaled_height;
3068 } else if (fh->height > c.max_scaled_height) {
3069 fh->height = c.max_scaled_height;
3070 btv->init.height = c.max_scaled_height;
3076 static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
3078 if (unlikely(a->index))
3081 strcpy(a->name, "audio");
3085 static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
3087 if (unlikely(a->index))
3093 static ssize_t bttv_read(struct file *file, char __user *data,
3094 size_t count, loff_t *ppos)
3096 struct bttv_fh *fh = file->private_data;
3099 if (fh->btv->errors)
3100 bttv_reinit_bt848(fh->btv);
3101 dprintk("%d: read count=%d type=%s\n",
3102 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
3105 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3106 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3107 /* VIDEO_READ in use by another fh,
3108 or VIDEO_STREAM by any fh. */
3111 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3112 file->f_flags & O_NONBLOCK);
3113 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
3115 case V4L2_BUF_TYPE_VBI_CAPTURE:
3116 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3118 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3119 file->f_flags & O_NONBLOCK);
3127 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3129 struct bttv_fh *fh = file->private_data;
3130 struct bttv_buffer *buf;
3131 enum v4l2_field field;
3132 unsigned int rc = POLLERR;
3134 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3135 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3137 return videobuf_poll_stream(file, &fh->vbi, wait);
3140 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3141 /* streaming capture */
3142 if (list_empty(&fh->cap.stream))
3144 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3146 /* read() capture */
3147 if (NULL == fh->cap.read_buf) {
3148 /* need to capture a new frame */
3149 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3151 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
3152 if (NULL == fh->cap.read_buf)
3154 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3155 field = videobuf_next_field(&fh->cap);
3156 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3157 kfree (fh->cap.read_buf);
3158 fh->cap.read_buf = NULL;
3161 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3162 fh->cap.read_off = 0;
3164 buf = (struct bttv_buffer*)fh->cap.read_buf;
3167 poll_wait(file, &buf->vb.done, wait);
3168 if (buf->vb.state == VIDEOBUF_DONE ||
3169 buf->vb.state == VIDEOBUF_ERROR)
3170 rc = POLLIN|POLLRDNORM;
3177 static int bttv_open(struct file *file)
3179 struct video_device *vdev = video_devdata(file);
3180 struct bttv *btv = video_drvdata(file);
3182 enum v4l2_buf_type type = 0;
3184 dprintk("open dev=%s\n", video_device_node_name(vdev));
3186 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
3187 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3188 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
3189 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3195 dprintk("%d: open called (type=%s)\n",
3196 btv->c.nr, v4l2_type_names[type]);
3198 /* allocate per filehandle data */
3199 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3202 file->private_data = fh;
3207 fh->ov.setup_ok = 0;
3209 v4l2_prio_open(&btv->prio, &fh->prio);
3211 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3212 &btv->c.pci->dev, &btv->s_lock,
3213 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3214 V4L2_FIELD_INTERLACED,
3215 sizeof(struct bttv_buffer),
3217 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3218 &btv->c.pci->dev, &btv->s_lock,
3219 V4L2_BUF_TYPE_VBI_CAPTURE,
3221 sizeof(struct bttv_buffer),
3223 set_tvnorm(btv,btv->tvnorm);
3224 set_input(btv, btv->input, btv->tvnorm);
3228 /* The V4L2 spec requires one global set of cropping parameters
3229 which only change on request. These are stored in btv->crop[1].
3230 However for compatibility with V4L apps and cropping unaware
3231 V4L2 apps we now reset the cropping parameters as seen through
3232 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3233 will use btv->crop[0], the default cropping parameters for the
3234 current video standard, and VIDIOC_S_FMT will not implicitely
3235 change the cropping parameters until VIDIOC_S_CROP has been
3237 fh->do_crop = !reset_crop; /* module parameter */
3239 /* Likewise there should be one global set of VBI capture
3240 parameters, but for compatibility with V4L apps and earlier
3241 driver versions each fh has its own parameters. */
3242 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3244 bttv_field_count(btv);
3248 static int bttv_release(struct file *file)
3250 struct bttv_fh *fh = file->private_data;
3251 struct bttv *btv = fh->btv;
3253 /* turn off overlay */
3254 if (check_btres(fh, RESOURCE_OVERLAY))
3255 bttv_switch_overlay(btv,fh,NULL);
3257 /* stop video capture */
3258 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3259 videobuf_streamoff(&fh->cap);
3260 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
3262 if (fh->cap.read_buf) {
3263 buffer_release(&fh->cap,fh->cap.read_buf);
3264 kfree(fh->cap.read_buf);
3266 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3267 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
3270 /* stop vbi capture */
3271 if (check_btres(fh, RESOURCE_VBI)) {
3272 videobuf_stop(&fh->vbi);
3273 free_btres_lock(btv,fh,RESOURCE_VBI);
3278 videobuf_mmap_free(&fh->cap);
3279 videobuf_mmap_free(&fh->vbi);
3280 v4l2_prio_close(&btv->prio, fh->prio);
3281 file->private_data = NULL;
3285 bttv_field_count(btv);
3294 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3296 struct bttv_fh *fh = file->private_data;
3298 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
3299 fh->btv->c.nr, v4l2_type_names[fh->type],
3300 vma->vm_start, vma->vm_end - vma->vm_start);
3301 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3304 static const struct v4l2_file_operations bttv_fops =
3306 .owner = THIS_MODULE,
3308 .release = bttv_release,
3309 .unlocked_ioctl = video_ioctl2,
3315 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3316 .vidioc_querycap = bttv_querycap,
3317 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3318 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3319 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3320 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3321 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3322 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3323 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3324 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
3325 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3326 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3327 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
3328 .vidioc_g_audio = bttv_g_audio,
3329 .vidioc_s_audio = bttv_s_audio,
3330 .vidioc_cropcap = bttv_cropcap,
3331 .vidioc_reqbufs = bttv_reqbufs,
3332 .vidioc_querybuf = bttv_querybuf,
3333 .vidioc_qbuf = bttv_qbuf,
3334 .vidioc_dqbuf = bttv_dqbuf,
3335 .vidioc_s_std = bttv_s_std,
3336 .vidioc_enum_input = bttv_enum_input,
3337 .vidioc_g_input = bttv_g_input,
3338 .vidioc_s_input = bttv_s_input,
3339 .vidioc_queryctrl = bttv_queryctrl,
3340 .vidioc_g_ctrl = bttv_g_ctrl,
3341 .vidioc_s_ctrl = bttv_s_ctrl,
3342 .vidioc_streamon = bttv_streamon,
3343 .vidioc_streamoff = bttv_streamoff,
3344 .vidioc_g_tuner = bttv_g_tuner,
3345 .vidioc_s_tuner = bttv_s_tuner,
3346 .vidioc_g_crop = bttv_g_crop,
3347 .vidioc_s_crop = bttv_s_crop,
3348 .vidioc_g_fbuf = bttv_g_fbuf,
3349 .vidioc_s_fbuf = bttv_s_fbuf,
3350 .vidioc_overlay = bttv_overlay,
3351 .vidioc_g_priority = bttv_g_priority,
3352 .vidioc_s_priority = bttv_s_priority,
3353 .vidioc_g_parm = bttv_g_parm,
3354 .vidioc_g_frequency = bttv_g_frequency,
3355 .vidioc_s_frequency = bttv_s_frequency,
3356 .vidioc_log_status = bttv_log_status,
3357 .vidioc_querystd = bttv_querystd,
3358 #ifdef CONFIG_VIDEO_ADV_DEBUG
3359 .vidioc_g_register = bttv_g_register,
3360 .vidioc_s_register = bttv_s_register,
3364 static struct video_device bttv_video_template = {
3366 .ioctl_ops = &bttv_ioctl_ops,
3367 .tvnorms = BTTV_NORMS,
3368 .current_norm = V4L2_STD_PAL,
3371 /* ----------------------------------------------------------------------- */
3372 /* radio interface */
3374 static int radio_open(struct file *file)
3376 struct video_device *vdev = video_devdata(file);
3377 struct bttv *btv = video_drvdata(file);
3380 dprintk("open dev=%s\n", video_device_node_name(vdev));
3382 dprintk("%d: open called (radio)\n", btv->c.nr);
3384 /* allocate per filehandle data */
3385 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3388 file->private_data = fh;
3391 v4l2_prio_open(&btv->prio, &fh->prio);
3395 bttv_call_all(btv, tuner, s_radio);
3396 audio_input(btv,TVAUDIO_INPUT_RADIO);
3401 static int radio_release(struct file *file)
3403 struct bttv_fh *fh = file->private_data;
3404 struct bttv *btv = fh->btv;
3405 struct saa6588_command cmd;
3407 v4l2_prio_close(&btv->prio, fh->prio);
3408 file->private_data = NULL;
3413 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
3418 static int radio_querycap(struct file *file, void *priv,
3419 struct v4l2_capability *cap)
3421 struct bttv_fh *fh = priv;
3422 struct bttv *btv = fh->btv;
3424 strcpy(cap->driver, "bttv");
3425 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3426 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3427 cap->capabilities = V4L2_CAP_TUNER;
3432 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3434 struct bttv_fh *fh = priv;
3435 struct bttv *btv = fh->btv;
3437 if (btv->tuner_type == TUNER_ABSENT)
3441 strcpy(t->name, "Radio");
3442 t->type = V4L2_TUNER_RADIO;
3444 bttv_call_all(btv, tuner, g_tuner, t);
3446 if (btv->audio_mode_gpio)
3447 btv->audio_mode_gpio(btv, t, 0);
3452 static int radio_enum_input(struct file *file, void *priv,
3453 struct v4l2_input *i)
3458 strcpy(i->name, "Radio");
3459 i->type = V4L2_INPUT_TYPE_TUNER;
3464 static int radio_g_audio(struct file *file, void *priv,
3465 struct v4l2_audio *a)
3467 if (unlikely(a->index))
3470 strcpy(a->name, "Radio");
3475 static int radio_s_tuner(struct file *file, void *priv,
3476 struct v4l2_tuner *t)
3478 struct bttv_fh *fh = priv;
3479 struct bttv *btv = fh->btv;
3484 bttv_call_all(btv, tuner, s_tuner, t);
3488 static int radio_s_audio(struct file *file, void *priv,
3489 struct v4l2_audio *a)
3491 if (unlikely(a->index))
3497 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3505 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3510 static int radio_queryctrl(struct file *file, void *priv,
3511 struct v4l2_queryctrl *c)
3513 const struct v4l2_queryctrl *ctrl;
3515 if (c->id < V4L2_CID_BASE ||
3516 c->id >= V4L2_CID_LASTP1)
3519 if (c->id == V4L2_CID_AUDIO_MUTE) {
3520 ctrl = ctrl_by_id(c->id);
3528 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3534 static ssize_t radio_read(struct file *file, char __user *data,
3535 size_t count, loff_t *ppos)
3537 struct bttv_fh *fh = file->private_data;
3538 struct bttv *btv = fh->btv;
3539 struct saa6588_command cmd;
3540 cmd.block_count = count/3;
3542 cmd.instance = file;
3543 cmd.result = -ENODEV;
3545 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
3550 static unsigned int radio_poll(struct file *file, poll_table *wait)
3552 struct bttv_fh *fh = file->private_data;
3553 struct bttv *btv = fh->btv;
3554 struct saa6588_command cmd;
3555 cmd.instance = file;
3556 cmd.event_list = wait;
3557 cmd.result = -ENODEV;
3558 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
3563 static const struct v4l2_file_operations radio_fops =
3565 .owner = THIS_MODULE,
3568 .release = radio_release,
3569 .unlocked_ioctl = video_ioctl2,
3573 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3574 .vidioc_querycap = radio_querycap,
3575 .vidioc_g_tuner = radio_g_tuner,
3576 .vidioc_enum_input = radio_enum_input,
3577 .vidioc_g_audio = radio_g_audio,
3578 .vidioc_s_tuner = radio_s_tuner,
3579 .vidioc_s_audio = radio_s_audio,
3580 .vidioc_s_input = radio_s_input,
3581 .vidioc_s_std = radio_s_std,
3582 .vidioc_queryctrl = radio_queryctrl,
3583 .vidioc_g_input = radio_g_input,
3584 .vidioc_g_ctrl = bttv_g_ctrl,
3585 .vidioc_s_ctrl = bttv_s_ctrl,
3586 .vidioc_g_frequency = bttv_g_frequency,
3587 .vidioc_s_frequency = bttv_s_frequency,
3590 static struct video_device radio_template = {
3591 .fops = &radio_fops,
3592 .ioctl_ops = &radio_ioctl_ops,
3595 /* ----------------------------------------------------------------------- */
3596 /* some debug code */
3598 static int bttv_risc_decode(u32 risc)
3600 static char *instr[16] = {
3601 [ BT848_RISC_WRITE >> 28 ] = "write",
3602 [ BT848_RISC_SKIP >> 28 ] = "skip",
3603 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3604 [ BT848_RISC_JUMP >> 28 ] = "jump",
3605 [ BT848_RISC_SYNC >> 28 ] = "sync",
3606 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3607 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3608 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3610 static int incr[16] = {
3611 [ BT848_RISC_WRITE >> 28 ] = 2,
3612 [ BT848_RISC_JUMP >> 28 ] = 2,
3613 [ BT848_RISC_SYNC >> 28 ] = 2,
3614 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3615 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3616 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3618 static char *bits[] = {
3619 "be0", "be1", "be2", "be3/resync",
3620 "set0", "set1", "set2", "set3",
3621 "clr0", "clr1", "clr2", "clr3",
3622 "irq", "res", "eol", "sol",
3626 pr_cont("0x%08x [ %s", risc,
3627 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3628 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3629 if (risc & (1 << (i + 12)))
3630 pr_cont(" %s", bits[i]);
3631 pr_cont(" count=%d ]\n", risc & 0xfff);
3632 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3635 static void bttv_risc_disasm(struct bttv *btv,
3636 struct btcx_riscmem *risc)
3640 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3641 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
3642 for (i = 0; i < (risc->size >> 2); i += n) {
3643 pr_info("%s: 0x%lx: ",
3644 btv->c.v4l2_dev.name,
3645 (unsigned long)(risc->dma + (i<<2)));
3646 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
3647 for (j = 1; j < n; j++)
3648 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3649 btv->c.v4l2_dev.name,
3650 (unsigned long)(risc->dma + ((i+j)<<2)),
3652 if (0 == risc->cpu[i])
3657 static void bttv_print_riscaddr(struct bttv *btv)
3659 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
3660 pr_info(" vbi : o=%08llx e=%08llx\n",
3661 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3662 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3663 pr_info(" cap : o=%08llx e=%08llx\n",
3665 ? (unsigned long long)btv->curr.top->top.dma : 0,
3667 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3668 pr_info(" scr : o=%08llx e=%08llx\n",
3669 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3670 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3671 bttv_risc_disasm(btv, &btv->main);
3674 /* ----------------------------------------------------------------------- */
3677 static char *irq_name[] = {
3678 "FMTCHG", // format change detected (525 vs. 625)
3679 "VSYNC", // vertical sync (new field)
3680 "HSYNC", // horizontal sync
3681 "OFLOW", // chroma/luma AGC overflow
3682 "HLOCK", // horizontal lock changed
3683 "VPRES", // video presence changed
3685 "I2CDONE", // hw irc operation finished
3686 "GPINT", // gpio port triggered irq
3688 "RISCI", // risc instruction triggered irq
3689 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3690 "FTRGT", // pixel data fifo overrun
3691 "FDSR", // fifo data stream resyncronisation
3692 "PPERR", // parity error (data transfer)
3693 "RIPERR", // parity error (read risc instructions)
3694 "PABORT", // pci abort
3695 "OCERR", // risc instruction error
3696 "SCERR", // syncronisation error
3699 static void bttv_print_irqbits(u32 print, u32 mark)
3704 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3705 if (print & (1 << i))
3706 pr_cont(" %s", irq_name[i]);
3707 if (mark & (1 << i))
3712 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3714 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3716 (unsigned long)btv->main.dma,
3717 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3718 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3721 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3722 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3723 "Ok, then this is harmless, don't worry ;)\n",
3727 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3729 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3735 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3737 struct bttv_buffer *item;
3739 memset(set,0,sizeof(*set));
3741 /* capture request ? */
3742 if (!list_empty(&btv->capture)) {
3744 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3745 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3747 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3750 /* capture request for other field ? */
3751 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3752 (item->vb.queue.next != &btv->capture)) {
3753 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3754 /* Mike Isely <isely@pobox.com> - Only check
3755 * and set up the bottom field in the logic
3756 * below. Don't ever do the top field. This
3757 * of course means that if we set up the
3758 * bottom field in the above code that we'll
3759 * actually skip a field. But that's OK.
3760 * Having processed only a single buffer this
3761 * time, then the next time around the first
3762 * available buffer should be for a top field.
3763 * That will then cause us here to set up a
3764 * top then a bottom field in the normal way.
3765 * The alternative to this understanding is
3766 * that we set up the second available buffer
3767 * as a top field, but that's out of order
3768 * since this driver always processes the top
3769 * field first - the effect will be the two
3770 * buffers being returned in the wrong order,
3771 * with the second buffer also being delayed
3772 * by one field time (owing to the fifo nature
3773 * of videobuf). Worse still, we'll be stuck
3774 * doing fields out of order now every time
3775 * until something else causes a field to be
3776 * dropped. By effectively forcing a field to
3777 * drop this way then we always get back into
3778 * sync within a single frame time. (Out of
3779 * order fields can screw up deinterlacing
3781 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3782 if (NULL == set->bottom &&
3783 V4L2_FIELD_BOTTOM == item->vb.field) {
3786 if (NULL != set->top && NULL != set->bottom)
3792 /* screen overlay ? */
3793 if (NULL != btv->screen) {
3794 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3795 if (NULL == set->top && NULL == set->bottom) {
3796 set->top = btv->screen;
3797 set->bottom = btv->screen;
3800 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3802 set->top = btv->screen;
3804 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3805 NULL == set->bottom) {
3806 set->bottom = btv->screen;
3811 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3812 btv->c.nr, set->top, set->bottom,
3813 btv->screen, set->frame_irq, set->top_irq);
3818 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3819 struct bttv_buffer_set *curr, unsigned int state)
3823 do_gettimeofday(&ts);
3825 if (wakeup->top == wakeup->bottom) {
3826 if (NULL != wakeup->top && curr->top != wakeup->top) {
3828 pr_debug("%d: wakeup: both=%p\n",
3829 btv->c.nr, wakeup->top);
3830 wakeup->top->vb.ts = ts;
3831 wakeup->top->vb.field_count = btv->field_count;
3832 wakeup->top->vb.state = state;
3833 wake_up(&wakeup->top->vb.done);
3836 if (NULL != wakeup->top && curr->top != wakeup->top) {
3838 pr_debug("%d: wakeup: top=%p\n",
3839 btv->c.nr, wakeup->top);
3840 wakeup->top->vb.ts = ts;
3841 wakeup->top->vb.field_count = btv->field_count;
3842 wakeup->top->vb.state = state;
3843 wake_up(&wakeup->top->vb.done);
3845 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3847 pr_debug("%d: wakeup: bottom=%p\n",
3848 btv->c.nr, wakeup->bottom);
3849 wakeup->bottom->vb.ts = ts;
3850 wakeup->bottom->vb.field_count = btv->field_count;
3851 wakeup->bottom->vb.state = state;
3852 wake_up(&wakeup->bottom->vb.done);
3858 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3866 do_gettimeofday(&ts);
3868 wakeup->vb.field_count = btv->field_count;
3869 wakeup->vb.state = state;
3870 wake_up(&wakeup->vb.done);
3873 static void bttv_irq_timeout(unsigned long data)
3875 struct bttv *btv = (struct bttv *)data;
3876 struct bttv_buffer_set old,new;
3877 struct bttv_buffer *ovbi;
3878 struct bttv_buffer *item;
3879 unsigned long flags;
3882 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3883 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3884 btread(BT848_RISC_COUNT));
3885 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3889 spin_lock_irqsave(&btv->s_lock,flags);
3891 /* deactivate stuff */
3892 memset(&new,0,sizeof(new));
3898 bttv_buffer_activate_video(btv, &new);
3899 bttv_buffer_activate_vbi(btv, NULL);
3900 bttv_set_dma(btv, 0);
3903 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3904 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
3906 /* cancel all outstanding capture / vbi requests */
3907 while (!list_empty(&btv->capture)) {
3908 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3909 list_del(&item->vb.queue);
3910 item->vb.state = VIDEOBUF_ERROR;
3911 wake_up(&item->vb.done);
3913 while (!list_empty(&btv->vcapture)) {
3914 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3915 list_del(&item->vb.queue);
3916 item->vb.state = VIDEOBUF_ERROR;
3917 wake_up(&item->vb.done);
3921 spin_unlock_irqrestore(&btv->s_lock,flags);
3925 bttv_irq_wakeup_top(struct bttv *btv)
3927 struct bttv_buffer *wakeup = btv->curr.top;
3932 spin_lock(&btv->s_lock);
3933 btv->curr.top_irq = 0;
3934 btv->curr.top = NULL;
3935 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3937 do_gettimeofday(&wakeup->vb.ts);
3938 wakeup->vb.field_count = btv->field_count;
3939 wakeup->vb.state = VIDEOBUF_DONE;
3940 wake_up(&wakeup->vb.done);
3941 spin_unlock(&btv->s_lock);
3944 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3948 if (rc > risc->dma + risc->size)
3954 bttv_irq_switch_video(struct bttv *btv)
3956 struct bttv_buffer_set new;
3957 struct bttv_buffer_set old;
3960 spin_lock(&btv->s_lock);
3962 /* new buffer set */
3963 bttv_irq_next_video(btv, &new);
3964 rc = btread(BT848_RISC_COUNT);
3965 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3966 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3969 bttv_irq_debug_low_latency(btv, rc);
3970 spin_unlock(&btv->s_lock);
3977 btv->loop_irq &= ~1;
3978 bttv_buffer_activate_video(btv, &new);
3979 bttv_set_dma(btv, 0);
3982 if (UNSET != btv->new_input) {
3983 video_mux(btv,btv->new_input);
3984 btv->new_input = UNSET;
3987 /* wake up finished buffers */
3988 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
3989 spin_unlock(&btv->s_lock);
3993 bttv_irq_switch_vbi(struct bttv *btv)
3995 struct bttv_buffer *new = NULL;
3996 struct bttv_buffer *old;
3999 spin_lock(&btv->s_lock);
4001 if (!list_empty(&btv->vcapture))
4002 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4005 rc = btread(BT848_RISC_COUNT);
4006 if (NULL != old && (is_active(&old->top, rc) ||
4007 is_active(&old->bottom, rc))) {
4010 bttv_irq_debug_low_latency(btv, rc);
4011 spin_unlock(&btv->s_lock);
4017 btv->loop_irq &= ~4;
4018 bttv_buffer_activate_vbi(btv, new);
4019 bttv_set_dma(btv, 0);
4021 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
4022 spin_unlock(&btv->s_lock);
4025 static irqreturn_t bttv_irq(int irq, void *dev_id)
4033 btv=(struct bttv *)dev_id;
4037 /* get/clear interrupt status bits */
4038 stat=btread(BT848_INT_STAT);
4039 astat=stat&btread(BT848_INT_MASK);
4043 btwrite(stat,BT848_INT_STAT);
4045 /* get device status bits */
4046 dstat=btread(BT848_DSTATUS);
4049 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
4050 btv->c.nr, count, btv->field_count,
4051 stat>>28, btread(BT848_RISC_COUNT));
4052 bttv_print_irqbits(stat,astat);
4053 if (stat & BT848_INT_HLOCK)
4054 pr_cont(" HLOC => %s",
4055 dstat & BT848_DSTATUS_HLOC
4057 if (stat & BT848_INT_VPRES)
4058 pr_cont(" PRES => %s",
4059 dstat & BT848_DSTATUS_PRES
4061 if (stat & BT848_INT_FMTCHG)
4062 pr_cont(" NUML => %s",
4063 dstat & BT848_DSTATUS_NUML
4068 if (astat&BT848_INT_VSYNC)
4071 if ((astat & BT848_INT_GPINT) && btv->remote) {
4072 bttv_input_irq(btv);
4075 if (astat & BT848_INT_I2CDONE) {
4076 btv->i2c_done = stat;
4077 wake_up(&btv->i2c_queue);
4080 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
4081 bttv_irq_switch_vbi(btv);
4083 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
4084 bttv_irq_wakeup_top(btv);
4086 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
4087 bttv_irq_switch_video(btv);
4089 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
4090 audio_mute(btv, btv->mute); /* trigger automute */
4092 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4093 pr_info("%d: %s%s @ %08x,",
4095 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4096 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4097 btread(BT848_RISC_COUNT));
4098 bttv_print_irqbits(stat,astat);
4101 bttv_print_riscaddr(btv);
4103 if (fdsr && astat & BT848_INT_FDSR) {
4104 pr_info("%d: FDSR @ %08x\n",
4105 btv->c.nr, btread(BT848_RISC_COUNT));
4107 bttv_print_riscaddr(btv);
4113 if (count > 8 || !(astat & BT848_INT_GPINT)) {
4114 btwrite(0, BT848_INT_MASK);
4116 pr_err("%d: IRQ lockup, cleared int mask [",
4119 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
4122 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4126 bttv_print_irqbits(stat,astat);
4134 return IRQ_RETVAL(handled);
4138 /* ----------------------------------------------------------------------- */
4139 /* initialitation */
4141 static struct video_device *vdev_init(struct bttv *btv,
4142 const struct video_device *template,
4143 const char *type_name)
4145 struct video_device *vfd;
4147 vfd = video_device_alloc();
4151 vfd->v4l2_dev = &btv->c.v4l2_dev;
4152 vfd->release = video_device_release;
4153 vfd->debug = bttv_debug;
4154 video_set_drvdata(vfd, btv);
4155 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4156 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4157 type_name, bttv_tvcards[btv->c.type].name);
4161 static void bttv_unregister_video(struct bttv *btv)
4163 if (btv->video_dev) {
4164 if (video_is_registered(btv->video_dev))
4165 video_unregister_device(btv->video_dev);
4167 video_device_release(btv->video_dev);
4168 btv->video_dev = NULL;
4171 if (video_is_registered(btv->vbi_dev))
4172 video_unregister_device(btv->vbi_dev);
4174 video_device_release(btv->vbi_dev);
4175 btv->vbi_dev = NULL;
4177 if (btv->radio_dev) {
4178 if (video_is_registered(btv->radio_dev))
4179 video_unregister_device(btv->radio_dev);
4181 video_device_release(btv->radio_dev);
4182 btv->radio_dev = NULL;
4186 /* register video4linux devices */
4187 static int __devinit bttv_register_video(struct bttv *btv)
4190 pr_notice("Overlay support disabled\n");
4193 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4195 if (NULL == btv->video_dev)
4197 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4198 video_nr[btv->c.nr]) < 0)
4200 pr_info("%d: registered device %s\n",
4201 btv->c.nr, video_device_node_name(btv->video_dev));
4202 if (device_create_file(&btv->video_dev->dev,
4203 &dev_attr_card)<0) {
4204 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
4209 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
4211 if (NULL == btv->vbi_dev)
4213 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4214 vbi_nr[btv->c.nr]) < 0)
4216 pr_info("%d: registered device %s\n",
4217 btv->c.nr, video_device_node_name(btv->vbi_dev));
4219 if (!btv->has_radio)
4222 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4223 if (NULL == btv->radio_dev)
4225 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4226 radio_nr[btv->c.nr]) < 0)
4228 pr_info("%d: registered device %s\n",
4229 btv->c.nr, video_device_node_name(btv->radio_dev));
4235 bttv_unregister_video(btv);
4240 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4241 /* response on cards with no firmware is not enabled by OF */
4242 static void pci_set_command(struct pci_dev *dev)
4244 #if defined(__powerpc__)
4247 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4248 cmd = (cmd | PCI_COMMAND_MEMORY );
4249 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4253 static int __devinit bttv_probe(struct pci_dev *dev,
4254 const struct pci_device_id *pci_id)
4260 if (bttv_num == BTTV_MAX)
4262 pr_info("Bt8xx card found (%d)\n", bttv_num);
4263 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
4265 pr_err("out of memory\n");
4268 btv->c.nr = bttv_num;
4269 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4270 "bttv%d", btv->c.nr);
4272 /* initialize structs / fill in defaults */
4273 mutex_init(&btv->lock);
4274 spin_lock_init(&btv->s_lock);
4275 spin_lock_init(&btv->gpio_lock);
4276 init_waitqueue_head(&btv->i2c_queue);
4277 INIT_LIST_HEAD(&btv->c.subs);
4278 INIT_LIST_HEAD(&btv->capture);
4279 INIT_LIST_HEAD(&btv->vcapture);
4280 v4l2_prio_init(&btv->prio);
4282 init_timer(&btv->timeout);
4283 btv->timeout.function = bttv_irq_timeout;
4284 btv->timeout.data = (unsigned long)btv;
4287 btv->tuner_type = UNSET;
4288 btv->new_input = UNSET;
4289 btv->has_radio=radio[btv->c.nr];
4291 /* pci stuff (init, get irq/mmio, ... */
4293 btv->id = dev->device;
4294 if (pci_enable_device(dev)) {
4295 pr_warn("%d: Can't enable device\n", btv->c.nr);
4298 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
4299 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
4302 if (!request_mem_region(pci_resource_start(dev,0),
4303 pci_resource_len(dev,0),
4304 btv->c.v4l2_dev.name)) {
4305 pr_warn("%d: can't request iomem (0x%llx)\n",
4307 (unsigned long long)pci_resource_start(dev, 0));
4310 pci_set_master(dev);
4311 pci_set_command(dev);
4313 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4315 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
4319 btv->revision = dev->revision;
4320 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4321 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4322 bttv_num, btv->id, btv->revision, pci_name(dev),
4323 btv->c.pci->irq, lat,
4324 (unsigned long long)pci_resource_start(dev, 0));
4327 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4328 if (NULL == btv->bt848_mmio) {
4329 pr_err("%d: ioremap() failed\n", btv->c.nr);
4337 /* disable irqs, register irq handler */
4338 btwrite(0, BT848_INT_MASK);
4339 result = request_irq(btv->c.pci->irq, bttv_irq,
4340 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
4342 pr_err("%d: can't get IRQ %d\n",
4343 bttv_num, btv->c.pci->irq);
4347 if (0 != bttv_handle_chipset(btv)) {
4352 /* init options from insmod args */
4353 btv->opt_combfilter = combfilter;
4354 btv->opt_lumafilter = lumafilter;
4355 btv->opt_automute = automute;
4356 btv->opt_chroma_agc = chroma_agc;
4357 btv->opt_adc_crush = adc_crush;
4358 btv->opt_vcr_hack = vcr_hack;
4359 btv->opt_whitecrush_upper = whitecrush_upper;
4360 btv->opt_whitecrush_lower = whitecrush_lower;
4361 btv->opt_uv_ratio = uv_ratio;
4362 btv->opt_full_luma_range = full_luma_range;
4363 btv->opt_coring = coring;
4365 /* fill struct bttv with some useful defaults */
4366 btv->init.btv = btv;
4367 btv->init.ov.w.width = 320;
4368 btv->init.ov.w.height = 240;
4369 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4370 btv->init.width = 320;
4371 btv->init.height = 240;
4374 /* initialize hardware */
4376 bttv_gpio_tracking(btv,"pre-init");
4378 bttv_risc_init_main(btv);
4382 btwrite(0x00, BT848_GPIO_REG_INP);
4383 btwrite(0x00, BT848_GPIO_OUT_EN);
4385 bttv_gpio_tracking(btv,"init");
4387 /* needs to be done before i2c is registered */
4388 bttv_init_card1(btv);
4390 /* register i2c + gpio */
4393 /* some card-specific stuff (needs working i2c) */
4394 bttv_init_card2(btv);
4395 bttv_init_tuner(btv);
4398 /* register video4linux + input */
4399 if (!bttv_tvcards[btv->c.type].no_video) {
4400 bttv_register_video(btv);
4401 bt848_bright(btv,32768);
4402 bt848_contrast(btv, 27648);
4403 bt848_hue(btv,32768);
4404 bt848_sat(btv,32768);
4406 set_input(btv, 0, btv->tvnorm);
4407 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4408 btv->crop[1] = btv->crop[0]; /* current = default */
4409 disclaim_vbi_lines(btv);
4410 disclaim_video_lines(btv);
4413 /* add subdevices and autoload dvb-bt8xx if needed */
4414 if (bttv_tvcards[btv->c.type].has_dvb) {
4415 bttv_sub_add_device(&btv->c, "dvb");
4416 request_modules(btv);
4420 init_bttv_i2c_ir(btv);
4421 bttv_input_init(btv);
4424 /* everything is fine */
4429 free_irq(btv->c.pci->irq,btv);
4432 v4l2_device_unregister(&btv->c.v4l2_dev);
4435 if (btv->bt848_mmio)
4436 iounmap(btv->bt848_mmio);
4437 release_mem_region(pci_resource_start(btv->c.pci,0),
4438 pci_resource_len(btv->c.pci,0));
4442 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4444 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4445 struct bttv *btv = to_bttv(v4l2_dev);
4448 pr_info("%d: unloading\n", btv->c.nr);
4450 if (bttv_tvcards[btv->c.type].has_dvb)
4451 flush_request_modules(btv);
4453 /* shutdown everything (DMA+IRQs) */
4454 btand(~15, BT848_GPIO_DMA_CTL);
4455 btwrite(0, BT848_INT_MASK);
4456 btwrite(~0x0, BT848_INT_STAT);
4457 btwrite(0x0, BT848_GPIO_OUT_EN);
4459 bttv_gpio_tracking(btv,"cleanup");
4461 /* tell gpio modules we are leaving ... */
4463 bttv_input_fini(btv);
4464 bttv_sub_del_devices(&btv->c);
4466 /* unregister i2c_bus + input */
4469 /* unregister video4linux */
4470 bttv_unregister_video(btv);
4472 /* free allocated memory */
4473 btcx_riscmem_free(btv->c.pci,&btv->main);
4475 /* free ressources */
4476 free_irq(btv->c.pci->irq,btv);
4477 iounmap(btv->bt848_mmio);
4478 release_mem_region(pci_resource_start(btv->c.pci,0),
4479 pci_resource_len(btv->c.pci,0));
4481 v4l2_device_unregister(&btv->c.v4l2_dev);
4482 bttvs[btv->c.nr] = NULL;
4489 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4491 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4492 struct bttv *btv = to_bttv(v4l2_dev);
4493 struct bttv_buffer_set idle;
4494 unsigned long flags;
4496 dprintk("%d: suspend %d\n", btv->c.nr, state.event);
4498 /* stop dma + irqs */
4499 spin_lock_irqsave(&btv->s_lock,flags);
4500 memset(&idle, 0, sizeof(idle));
4501 btv->state.video = btv->curr;
4502 btv->state.vbi = btv->cvbi;
4503 btv->state.loop_irq = btv->loop_irq;
4506 bttv_buffer_activate_video(btv, &idle);
4507 bttv_buffer_activate_vbi(btv, NULL);
4508 bttv_set_dma(btv, 0);
4509 btwrite(0, BT848_INT_MASK);
4510 spin_unlock_irqrestore(&btv->s_lock,flags);
4512 /* save bt878 state */
4513 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4514 btv->state.gpio_data = gpio_read();
4516 /* save pci state */
4517 pci_save_state(pci_dev);
4518 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4519 pci_disable_device(pci_dev);
4520 btv->state.disabled = 1;
4525 static int bttv_resume(struct pci_dev *pci_dev)
4527 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4528 struct bttv *btv = to_bttv(v4l2_dev);
4529 unsigned long flags;
4532 dprintk("%d: resume\n", btv->c.nr);
4534 /* restore pci state */
4535 if (btv->state.disabled) {
4536 err=pci_enable_device(pci_dev);
4538 pr_warn("%d: Can't enable device\n", btv->c.nr);
4541 btv->state.disabled = 0;
4543 err=pci_set_power_state(pci_dev, PCI_D0);
4545 pci_disable_device(pci_dev);
4546 pr_warn("%d: Can't enable device\n", btv->c.nr);
4547 btv->state.disabled = 1;
4551 pci_restore_state(pci_dev);
4553 /* restore bt878 state */
4554 bttv_reinit_bt848(btv);
4555 gpio_inout(0xffffff, btv->state.gpio_enable);
4556 gpio_write(btv->state.gpio_data);
4559 spin_lock_irqsave(&btv->s_lock,flags);
4560 btv->curr = btv->state.video;
4561 btv->cvbi = btv->state.vbi;
4562 btv->loop_irq = btv->state.loop_irq;
4563 bttv_buffer_activate_video(btv, &btv->curr);
4564 bttv_buffer_activate_vbi(btv, btv->cvbi);
4565 bttv_set_dma(btv, 0);
4566 spin_unlock_irqrestore(&btv->s_lock,flags);
4571 static struct pci_device_id bttv_pci_tbl[] = {
4572 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4573 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4574 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4575 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
4576 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
4580 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4582 static struct pci_driver bttv_pci_driver = {
4584 .id_table = bttv_pci_tbl,
4585 .probe = bttv_probe,
4586 .remove = __devexit_p(bttv_remove),
4588 .suspend = bttv_suspend,
4589 .resume = bttv_resume,
4593 static int __init bttv_init_module(void)
4599 pr_info("driver version %s loaded\n", BTTV_VERSION);
4600 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4602 if (gbufsize > BTTV_MAX_FBUF)
4603 gbufsize = BTTV_MAX_FBUF;
4604 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4606 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4607 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4609 bttv_check_chipset();
4611 ret = bus_register(&bttv_sub_bus_type);
4613 pr_warn("bus_register error: %d\n", ret);
4616 ret = pci_register_driver(&bttv_pci_driver);
4618 bus_unregister(&bttv_sub_bus_type);
4623 static void __exit bttv_cleanup_module(void)
4625 pci_unregister_driver(&bttv_pci_driver);
4626 bus_unregister(&bttv_sub_bus_type);
4629 module_init(bttv_init_module);
4630 module_exit(bttv_cleanup_module);