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(&dev->request_module_wk);
202 #define request_modules(dev)
203 #define flush_request_modules(dev) do {} while(0)
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 extraheight Added to sheight for cropcap.bounds.height only
254 videostart0 ITU-R frame line number of the line corresponding
255 to vdelay in the first field. */
256 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
257 vdelay, sheight, extraheight, videostart0) \
258 .cropcap.bounds.left = minhdelayx1, \
259 /* * 2 because vertically we count field lines times two, */ \
260 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
261 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
262 /* 4 is a safety margin at the end of the line. */ \
263 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
264 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
266 .cropcap.defrect.left = hdelayx1, \
267 .cropcap.defrect.top = (videostart0) * 2, \
268 .cropcap.defrect.width = swidth, \
269 .cropcap.defrect.height = sheight, \
270 .cropcap.pixelaspect.numerator = totalwidth, \
271 .cropcap.pixelaspect.denominator = sqwidth,
273 const struct bttv_tvnorm bttv_tvnorms[] = {
275 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
276 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
278 .v4l2_id = V4L2_STD_PAL,
286 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
287 .scaledtwidth = 1135,
291 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
293 /* ITU-R frame line number of the first VBI line
294 we can capture, of the first and second field.
295 The last line is determined by cropcap.bounds. */
296 .vbistart = { 7, 320 },
297 CROPCAP(/* minhdelayx1 */ 68,
299 /* Should be (768 * 1135 + 944 / 2) / 944.
300 cropcap.defrect is used for image width
301 checks, so we keep the old value 924. */
303 /* totalwidth */ 1135,
307 /* bt878 (and bt848?) can capture another
308 line below active video. */
310 /* videostart0 */ 23)
312 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
320 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
325 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
327 .vbistart = { 10, 273 },
328 CROPCAP(/* minhdelayx1 */ 68,
330 /* Should be (640 * 910 + 780 / 2) / 780? */
332 /* totalwidth */ 910,
337 /* videostart0 */ 23)
339 .v4l2_id = V4L2_STD_SECAM,
347 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
348 .scaledtwidth = 1135,
353 .sram = 0, /* like PAL, correct? */
354 .vbistart = { 7, 320 },
355 CROPCAP(/* minhdelayx1 */ 68,
358 /* totalwidth */ 1135,
363 /* videostart0 */ 23)
365 .v4l2_id = V4L2_STD_PAL_Nc,
373 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
380 .vbistart = { 7, 320 },
381 CROPCAP(/* minhdelayx1 */ 68,
383 /* swidth */ (640 * 910 + 780 / 2) / 780,
384 /* totalwidth */ 910,
389 /* videostart0 */ 23)
391 .v4l2_id = V4L2_STD_PAL_M,
399 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
406 .vbistart = { 10, 273 },
407 CROPCAP(/* minhdelayx1 */ 68,
409 /* swidth */ (640 * 910 + 780 / 2) / 780,
410 /* totalwidth */ 910,
415 /* videostart0 */ 23)
417 .v4l2_id = V4L2_STD_PAL_N,
425 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
432 .vbistart = { 7, 320 },
433 CROPCAP(/* minhdelayx1 */ 68,
435 /* swidth */ (768 * 1135 + 944 / 2) / 944,
436 /* totalwidth */ 1135,
441 /* videostart0 */ 23)
443 .v4l2_id = V4L2_STD_NTSC_M_JP,
451 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
458 .vbistart = { 10, 273 },
459 CROPCAP(/* minhdelayx1 */ 68,
461 /* swidth */ (640 * 910 + 780 / 2) / 780,
462 /* totalwidth */ 910,
467 /* videostart0 */ 23)
469 /* that one hopefully works with the strange timing
470 * which video recorders produce when playing a NTSC
471 * tape on a PAL TV ... */
472 .v4l2_id = V4L2_STD_PAL_60,
480 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
481 .scaledtwidth = 1135,
488 .vbistart = { 10, 273 },
489 CROPCAP(/* minhdelayx1 */ 68,
492 /* totalwidth */ 1135,
497 /* videostart0 */ 23)
500 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
502 /* ----------------------------------------------------------------------- */
504 packed pixel formats must come first */
505 static const struct bttv_format formats[] = {
507 .name = "8 bpp, gray",
508 .fourcc = V4L2_PIX_FMT_GREY,
509 .btformat = BT848_COLOR_FMT_Y8,
511 .flags = FORMAT_FLAGS_PACKED,
513 .name = "8 bpp, dithered color",
514 .fourcc = V4L2_PIX_FMT_HI240,
515 .btformat = BT848_COLOR_FMT_RGB8,
517 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
519 .name = "15 bpp RGB, le",
520 .fourcc = V4L2_PIX_FMT_RGB555,
521 .btformat = BT848_COLOR_FMT_RGB15,
523 .flags = FORMAT_FLAGS_PACKED,
525 .name = "15 bpp RGB, be",
526 .fourcc = V4L2_PIX_FMT_RGB555X,
527 .btformat = BT848_COLOR_FMT_RGB15,
528 .btswap = 0x03, /* byteswap */
530 .flags = FORMAT_FLAGS_PACKED,
532 .name = "16 bpp RGB, le",
533 .fourcc = V4L2_PIX_FMT_RGB565,
534 .btformat = BT848_COLOR_FMT_RGB16,
536 .flags = FORMAT_FLAGS_PACKED,
538 .name = "16 bpp RGB, be",
539 .fourcc = V4L2_PIX_FMT_RGB565X,
540 .btformat = BT848_COLOR_FMT_RGB16,
541 .btswap = 0x03, /* byteswap */
543 .flags = FORMAT_FLAGS_PACKED,
545 .name = "24 bpp RGB, le",
546 .fourcc = V4L2_PIX_FMT_BGR24,
547 .btformat = BT848_COLOR_FMT_RGB24,
549 .flags = FORMAT_FLAGS_PACKED,
551 .name = "32 bpp RGB, le",
552 .fourcc = V4L2_PIX_FMT_BGR32,
553 .btformat = BT848_COLOR_FMT_RGB32,
555 .flags = FORMAT_FLAGS_PACKED,
557 .name = "32 bpp RGB, be",
558 .fourcc = V4L2_PIX_FMT_RGB32,
559 .btformat = BT848_COLOR_FMT_RGB32,
560 .btswap = 0x0f, /* byte+word swap */
562 .flags = FORMAT_FLAGS_PACKED,
564 .name = "4:2:2, packed, YUYV",
565 .fourcc = V4L2_PIX_FMT_YUYV,
566 .btformat = BT848_COLOR_FMT_YUY2,
568 .flags = FORMAT_FLAGS_PACKED,
570 .name = "4:2:2, packed, UYVY",
571 .fourcc = V4L2_PIX_FMT_UYVY,
572 .btformat = BT848_COLOR_FMT_YUY2,
573 .btswap = 0x03, /* byteswap */
575 .flags = FORMAT_FLAGS_PACKED,
577 .name = "4:2:2, planar, Y-Cb-Cr",
578 .fourcc = V4L2_PIX_FMT_YUV422P,
579 .btformat = BT848_COLOR_FMT_YCrCb422,
581 .flags = FORMAT_FLAGS_PLANAR,
585 .name = "4:2:0, planar, Y-Cb-Cr",
586 .fourcc = V4L2_PIX_FMT_YUV420,
587 .btformat = BT848_COLOR_FMT_YCrCb422,
589 .flags = FORMAT_FLAGS_PLANAR,
593 .name = "4:2:0, planar, Y-Cr-Cb",
594 .fourcc = V4L2_PIX_FMT_YVU420,
595 .btformat = BT848_COLOR_FMT_YCrCb422,
597 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
601 .name = "4:1:1, planar, Y-Cb-Cr",
602 .fourcc = V4L2_PIX_FMT_YUV411P,
603 .btformat = BT848_COLOR_FMT_YCrCb411,
605 .flags = FORMAT_FLAGS_PLANAR,
609 .name = "4:1:0, planar, Y-Cb-Cr",
610 .fourcc = V4L2_PIX_FMT_YUV410,
611 .btformat = BT848_COLOR_FMT_YCrCb411,
613 .flags = FORMAT_FLAGS_PLANAR,
617 .name = "4:1:0, planar, Y-Cr-Cb",
618 .fourcc = V4L2_PIX_FMT_YVU410,
619 .btformat = BT848_COLOR_FMT_YCrCb411,
621 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
625 .name = "raw scanlines",
627 .btformat = BT848_COLOR_FMT_RAW,
629 .flags = FORMAT_FLAGS_RAW,
632 static const unsigned int FORMATS = ARRAY_SIZE(formats);
634 /* ----------------------------------------------------------------------- */
636 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
637 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
638 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
639 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
640 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
641 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
642 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
643 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
644 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
645 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
646 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
647 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
649 static const struct v4l2_queryctrl no_ctl = {
651 .flags = V4L2_CTRL_FLAG_DISABLED,
653 static const struct v4l2_queryctrl bttv_ctls[] = {
656 .id = V4L2_CID_BRIGHTNESS,
657 .name = "Brightness",
661 .default_value = 32768,
662 .type = V4L2_CTRL_TYPE_INTEGER,
664 .id = V4L2_CID_CONTRAST,
669 .default_value = 27648,
670 .type = V4L2_CTRL_TYPE_INTEGER,
672 .id = V4L2_CID_SATURATION,
673 .name = "Saturation",
677 .default_value = 32768,
678 .type = V4L2_CTRL_TYPE_INTEGER,
680 .id = V4L2_CID_COLOR_KILLER,
681 .name = "Color killer",
684 .type = V4L2_CTRL_TYPE_BOOLEAN,
691 .default_value = 32768,
692 .type = V4L2_CTRL_TYPE_INTEGER,
696 .id = V4L2_CID_AUDIO_MUTE,
700 .type = V4L2_CTRL_TYPE_BOOLEAN,
702 .id = V4L2_CID_AUDIO_VOLUME,
707 .default_value = 65535,
708 .type = V4L2_CTRL_TYPE_INTEGER,
710 .id = V4L2_CID_AUDIO_BALANCE,
715 .default_value = 32768,
716 .type = V4L2_CTRL_TYPE_INTEGER,
718 .id = V4L2_CID_AUDIO_BASS,
723 .default_value = 32768,
724 .type = V4L2_CTRL_TYPE_INTEGER,
726 .id = V4L2_CID_AUDIO_TREBLE,
731 .default_value = 32768,
732 .type = V4L2_CTRL_TYPE_INTEGER,
734 /* --- private --- */
736 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
737 .name = "chroma agc",
740 .type = V4L2_CTRL_TYPE_BOOLEAN,
742 .id = V4L2_CID_PRIVATE_COMBFILTER,
743 .name = "combfilter",
746 .type = V4L2_CTRL_TYPE_BOOLEAN,
748 .id = V4L2_CID_PRIVATE_AUTOMUTE,
752 .type = V4L2_CTRL_TYPE_BOOLEAN,
754 .id = V4L2_CID_PRIVATE_LUMAFILTER,
755 .name = "luma decimation filter",
758 .type = V4L2_CTRL_TYPE_BOOLEAN,
760 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
764 .type = V4L2_CTRL_TYPE_BOOLEAN,
766 .id = V4L2_CID_PRIVATE_VCR_HACK,
770 .type = V4L2_CTRL_TYPE_BOOLEAN,
772 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
773 .name = "whitecrush upper",
777 .default_value = 0xCF,
778 .type = V4L2_CTRL_TYPE_INTEGER,
780 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
781 .name = "whitecrush lower",
785 .default_value = 0x7F,
786 .type = V4L2_CTRL_TYPE_INTEGER,
788 .id = V4L2_CID_PRIVATE_UV_RATIO,
794 .type = V4L2_CTRL_TYPE_INTEGER,
796 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
797 .name = "full luma range",
800 .type = V4L2_CTRL_TYPE_BOOLEAN,
802 .id = V4L2_CID_PRIVATE_CORING,
808 .type = V4L2_CTRL_TYPE_INTEGER,
815 static const struct v4l2_queryctrl *ctrl_by_id(int id)
819 for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
820 if (bttv_ctls[i].id == id)
826 /* ----------------------------------------------------------------------- */
827 /* resource management */
830 RESOURCE_ allocated by freed by
832 VIDEO_READ bttv_read 1) bttv_read 2)
834 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
835 VIDIOC_QBUF 1) bttv_release
838 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
839 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
842 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
843 VIDIOC_QBUF 1) bttv_release
844 bttv_read, bttv_poll 1) 4)
846 1) The resource must be allocated when we enter buffer prepare functions
847 and remain allocated while buffers are in the DMA queue.
848 2) This is a single frame read.
849 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
850 RESOURCE_OVERLAY is allocated.
851 4) This is a continuous read, implies VIDIOC_STREAMON.
853 Note this driver permits video input and standard changes regardless if
854 resources are allocated.
857 #define VBI_RESOURCES (RESOURCE_VBI)
858 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
859 RESOURCE_VIDEO_STREAM | \
863 int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
865 int xbits; /* mutual exclusive resources */
867 if (fh->resources & bit)
868 /* have it already allocated */
872 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
873 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
876 if (btv->resources & xbits) {
877 /* no, someone else uses it */
881 if ((bit & VIDEO_RESOURCES)
882 && 0 == (btv->resources & VIDEO_RESOURCES)) {
883 /* Do crop - use current, don't - use default parameters. */
884 __s32 top = btv->crop[!!fh->do_crop].rect.top;
886 if (btv->vbi_end > top)
889 /* We cannot capture the same line as video and VBI data.
890 Claim scan lines crop[].rect.top to bottom. */
891 btv->crop_start = top;
892 } else if (bit & VBI_RESOURCES) {
893 __s32 end = fh->vbi_fmt.end;
895 if (end > btv->crop_start)
898 /* Claim scan lines above fh->vbi_fmt.end. */
902 /* it's free, grab it */
903 fh->resources |= bit;
904 btv->resources |= bit;
912 int check_btres(struct bttv_fh *fh, int bit)
914 return (fh->resources & bit);
918 int locked_btres(struct bttv *btv, int bit)
920 return (btv->resources & bit);
923 /* Call with btv->lock down. */
925 disclaim_vbi_lines(struct bttv *btv)
930 /* Call with btv->lock down. */
932 disclaim_video_lines(struct bttv *btv)
934 const struct bttv_tvnorm *tvnorm;
937 tvnorm = &bttv_tvnorms[btv->tvnorm];
938 btv->crop_start = tvnorm->cropcap.bounds.top
939 + tvnorm->cropcap.bounds.height;
941 /* VBI capturing ends at VDELAY, start of video capturing, no
942 matter how many lines the VBI RISC program expects. When video
943 capturing is off, it shall no longer "preempt" VBI capturing,
944 so we set VDELAY to maximum. */
945 crop = btread(BT848_E_CROP) | 0xc0;
946 btwrite(crop, BT848_E_CROP);
947 btwrite(0xfe, BT848_E_VDELAY_LO);
948 btwrite(crop, BT848_O_CROP);
949 btwrite(0xfe, BT848_O_VDELAY_LO);
953 void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
955 if ((fh->resources & bits) != bits) {
956 /* trying to free resources not allocated by us ... */
957 pr_err("BUG! (btres)\n");
959 fh->resources &= ~bits;
960 btv->resources &= ~bits;
962 bits = btv->resources;
964 if (0 == (bits & VIDEO_RESOURCES))
965 disclaim_video_lines(btv);
967 if (0 == (bits & VBI_RESOURCES))
968 disclaim_vbi_lines(btv);
971 /* ----------------------------------------------------------------------- */
972 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
974 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
975 PLL_X = Reference pre-divider (0=1, 1=2)
976 PLL_C = Post divider (0=6, 1=4)
977 PLL_I = Integer input
978 PLL_F = Fractional input
980 F_input = 28.636363 MHz:
981 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
984 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
986 unsigned char fl, fh, fi;
988 /* prevent overflows */
1001 btwrite(fl, BT848_PLL_F_LO);
1002 btwrite(fh, BT848_PLL_F_HI);
1003 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
1006 static void set_pll(struct bttv *btv)
1010 if (!btv->pll.pll_crystal)
1013 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1014 dprintk("%d: PLL: no change required\n", btv->c.nr);
1018 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1020 if (btv->pll.pll_current == 0)
1023 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
1024 btv->c.nr, btv->pll.pll_ifreq);
1025 btwrite(0x00,BT848_TGCTRL);
1026 btwrite(0x00,BT848_PLL_XCI);
1027 btv->pll.pll_current = 0;
1032 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
1034 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1035 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1037 for (i=0; i<10; i++) {
1038 /* Let other people run while the PLL stabilizes */
1041 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1042 btwrite(0,BT848_DSTATUS);
1044 btwrite(0x08,BT848_TGCTRL);
1045 btv->pll.pll_current = btv->pll.pll_ofreq;
1047 pr_info("PLL set ok\n");
1051 btv->pll.pll_current = -1;
1053 pr_info("Setting PLL failed\n");
1057 /* used to switch between the bt848's analog/digital video capture modes */
1058 static void bt848A_set_timing(struct bttv *btv)
1061 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1062 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1064 if (btv->input == btv->dig) {
1065 dprintk("%d: load digital timing table (table_idx=%d)\n",
1066 btv->c.nr,table_idx);
1068 /* timing change...reset timing generator address */
1069 btwrite(0x00, BT848_TGCTRL);
1070 btwrite(0x02, BT848_TGCTRL);
1071 btwrite(0x00, BT848_TGCTRL);
1073 len=SRAM_Table[table_idx][0];
1074 for(i = 1; i <= len; i++)
1075 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1076 btv->pll.pll_ofreq = 27000000;
1079 btwrite(0x11, BT848_TGCTRL);
1080 btwrite(0x41, BT848_DVSIF);
1082 btv->pll.pll_ofreq = fsc;
1084 btwrite(0x0, BT848_DVSIF);
1088 /* ----------------------------------------------------------------------- */
1090 static void bt848_bright(struct bttv *btv, int bright)
1094 // printk("set bright: %d\n", bright); // DEBUG
1095 btv->bright = bright;
1097 /* We want -128 to 127 we get 0-65535 */
1098 value = (bright >> 8) - 128;
1099 btwrite(value & 0xff, BT848_BRIGHT);
1102 static void bt848_hue(struct bttv *btv, int hue)
1109 value = (hue >> 8) - 128;
1110 btwrite(value & 0xff, BT848_HUE);
1113 static void bt848_contrast(struct bttv *btv, int cont)
1117 btv->contrast = cont;
1120 value = (cont >> 7);
1121 hibit = (value >> 6) & 4;
1122 btwrite(value & 0xff, BT848_CONTRAST_LO);
1123 btaor(hibit, ~4, BT848_E_CONTROL);
1124 btaor(hibit, ~4, BT848_O_CONTROL);
1127 static void bt848_sat(struct bttv *btv, int color)
1129 int val_u,val_v,hibits;
1131 btv->saturation = color;
1133 /* 0-511 for the color */
1134 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1135 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1136 hibits = (val_u >> 7) & 2;
1137 hibits |= (val_v >> 8) & 1;
1138 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1139 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1140 btaor(hibits, ~3, BT848_E_CONTROL);
1141 btaor(hibits, ~3, BT848_O_CONTROL);
1144 /* ----------------------------------------------------------------------- */
1147 video_mux(struct bttv *btv, unsigned int input)
1151 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1154 /* needed by RemoteVideo MX */
1155 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1157 gpio_inout(mask2,mask2);
1159 if (input == btv->svhs) {
1160 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1161 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1163 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1164 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1166 mux = bttv_muxsel(btv, input);
1167 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1168 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
1170 /* card specific hook */
1171 if(bttv_tvcards[btv->c.type].muxsel_hook)
1172 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1176 static char *audio_modes[] = {
1177 "audio: tuner", "audio: radio", "audio: extern",
1178 "audio: intern", "audio: mute"
1182 audio_mux(struct bttv *btv, int input, int mute)
1184 int gpio_val, signal;
1185 struct v4l2_control ctrl;
1187 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1188 bttv_tvcards[btv->c.type].gpiomask);
1189 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1195 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1198 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1200 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1202 switch (btv->c.type) {
1203 case BTTV_BOARD_VOODOOTV_FM:
1204 case BTTV_BOARD_VOODOOTV_200:
1205 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
1209 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1213 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1217 ctrl.id = V4L2_CID_AUDIO_MUTE;
1218 ctrl.value = btv->mute;
1219 bttv_call_all(btv, core, s_ctrl, &ctrl);
1220 if (btv->sd_msp34xx) {
1223 /* Note: the inputs tuner/radio/extern/intern are translated
1224 to msp routings. This assumes common behavior for all msp3400
1225 based TV cards. When this assumption fails, then the
1226 specific MSP routing must be added to the card table.
1227 For now this is sufficient. */
1229 case TVAUDIO_INPUT_RADIO:
1230 /* Some boards need the msp do to the radio demod */
1231 if (btv->radio_uses_msp_demodulator) {
1232 in = MSP_INPUT_DEFAULT;
1235 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1236 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1238 case TVAUDIO_INPUT_EXTERN:
1239 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1240 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1242 case TVAUDIO_INPUT_INTERN:
1243 /* Yes, this is the same input as for RADIO. I doubt
1244 if this is ever used. The only board with an INTERN
1245 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1246 that was tested. My guess is that the whole INTERN
1247 input does not work. */
1248 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1249 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1251 case TVAUDIO_INPUT_TUNER:
1253 /* This is the only card that uses TUNER2, and afaik,
1254 is the only difference between the VOODOOTV_FM
1256 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1257 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1258 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1260 in = MSP_INPUT_DEFAULT;
1263 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1264 in, MSP_OUTPUT_DEFAULT, 0);
1266 if (btv->sd_tvaudio) {
1267 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1274 audio_mute(struct bttv *btv, int mute)
1276 return audio_mux(btv, btv->audio, mute);
1280 audio_input(struct bttv *btv, int input)
1282 return audio_mux(btv, input, btv->mute);
1286 bttv_crop_calc_limits(struct bttv_crop *c)
1288 /* Scale factor min. 1:1, max. 16:1. Min. image size
1289 48 x 32. Scaled width must be a multiple of 4. */
1292 /* For bug compatibility with VIDIOCGCAP and image
1293 size checks in earlier driver versions. */
1294 c->min_scaled_width = 48;
1295 c->min_scaled_height = 32;
1297 c->min_scaled_width =
1298 (max(48, c->rect.width >> 4) + 3) & ~3;
1299 c->min_scaled_height =
1300 max(32, c->rect.height >> 4);
1303 c->max_scaled_width = c->rect.width & ~3;
1304 c->max_scaled_height = c->rect.height;
1308 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1310 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1311 bttv_crop_calc_limits(c);
1314 /* Call with btv->lock down. */
1316 set_tvnorm(struct bttv *btv, unsigned int norm)
1318 const struct bttv_tvnorm *tvnorm;
1321 BUG_ON(norm >= BTTV_TVNORMS);
1322 BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
1324 tvnorm = &bttv_tvnorms[norm];
1326 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1327 sizeof (tvnorm->cropcap))) {
1328 bttv_crop_reset(&btv->crop[0], norm);
1329 btv->crop[1] = btv->crop[0]; /* current = default */
1331 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1332 btv->crop_start = tvnorm->cropcap.bounds.top
1333 + tvnorm->cropcap.bounds.height;
1339 btwrite(tvnorm->adelay, BT848_ADELAY);
1340 btwrite(tvnorm->bdelay, BT848_BDELAY);
1341 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1343 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1344 btwrite(1, BT848_VBI_PACK_DEL);
1345 bt848A_set_timing(btv);
1347 switch (btv->c.type) {
1348 case BTTV_BOARD_VOODOOTV_FM:
1349 case BTTV_BOARD_VOODOOTV_200:
1350 bttv_tda9880_setnorm(btv, gpio_read());
1353 id = tvnorm->v4l2_id;
1354 bttv_call_all(btv, core, s_std, id);
1359 /* Call with btv->lock down. */
1361 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1363 unsigned long flags;
1367 spin_lock_irqsave(&btv->s_lock,flags);
1368 if (btv->curr.frame_irq) {
1369 /* active capture -> delayed input switch */
1370 btv->new_input = input;
1372 video_mux(btv,input);
1374 spin_unlock_irqrestore(&btv->s_lock,flags);
1376 video_mux(btv,input);
1378 audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1379 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
1380 set_tvnorm(btv, norm);
1383 static void init_irqreg(struct bttv *btv)
1386 btwrite(0xfffffUL, BT848_INT_STAT);
1388 if (bttv_tvcards[btv->c.type].no_video) {
1390 btwrite(BT848_INT_I2CDONE,
1394 btwrite((btv->triton1) |
1395 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1397 (fdsr ? BT848_INT_FDSR : 0) |
1398 BT848_INT_RISCI | BT848_INT_OCERR |
1399 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1405 static void init_bt848(struct bttv *btv)
1409 if (bttv_tvcards[btv->c.type].no_video) {
1410 /* very basic init only */
1415 btwrite(0x00, BT848_CAP_CTL);
1416 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1417 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1419 /* set planar and packed mode trigger points and */
1420 /* set rising edge of inverted GPINTR pin as irq trigger */
1421 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1422 BT848_GPIO_DMA_CTL_PLTP1_16|
1423 BT848_GPIO_DMA_CTL_PLTP23_16|
1424 BT848_GPIO_DMA_CTL_GPINTC|
1425 BT848_GPIO_DMA_CTL_GPINTI,
1426 BT848_GPIO_DMA_CTL);
1428 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1429 btwrite(val, BT848_E_SCLOOP);
1430 btwrite(val, BT848_O_SCLOOP);
1432 btwrite(0x20, BT848_E_VSCALE_HI);
1433 btwrite(0x20, BT848_O_VSCALE_HI);
1434 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1437 btwrite(whitecrush_upper, BT848_WC_UP);
1438 btwrite(whitecrush_lower, BT848_WC_DOWN);
1440 if (btv->opt_lumafilter) {
1441 btwrite(0, BT848_E_CONTROL);
1442 btwrite(0, BT848_O_CONTROL);
1444 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1445 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1448 bt848_bright(btv, btv->bright);
1449 bt848_hue(btv, btv->hue);
1450 bt848_contrast(btv, btv->contrast);
1451 bt848_sat(btv, btv->saturation);
1457 static void bttv_reinit_bt848(struct bttv *btv)
1459 unsigned long flags;
1462 pr_info("%d: reset, reinitialize\n", btv->c.nr);
1463 spin_lock_irqsave(&btv->s_lock,flags);
1465 bttv_set_dma(btv,0);
1466 spin_unlock_irqrestore(&btv->s_lock,flags);
1469 btv->pll.pll_current = -1;
1470 set_input(btv, btv->input, btv->tvnorm);
1473 static int bttv_g_ctrl(struct file *file, void *priv,
1474 struct v4l2_control *c)
1476 struct bttv_fh *fh = priv;
1477 struct bttv *btv = fh->btv;
1480 case V4L2_CID_BRIGHTNESS:
1481 c->value = btv->bright;
1484 c->value = btv->hue;
1486 case V4L2_CID_CONTRAST:
1487 c->value = btv->contrast;
1489 case V4L2_CID_SATURATION:
1490 c->value = btv->saturation;
1492 case V4L2_CID_COLOR_KILLER:
1493 c->value = btv->opt_color_killer;
1496 case V4L2_CID_AUDIO_MUTE:
1497 case V4L2_CID_AUDIO_VOLUME:
1498 case V4L2_CID_AUDIO_BALANCE:
1499 case V4L2_CID_AUDIO_BASS:
1500 case V4L2_CID_AUDIO_TREBLE:
1501 bttv_call_all(btv, core, g_ctrl, c);
1504 case V4L2_CID_PRIVATE_CHROMA_AGC:
1505 c->value = btv->opt_chroma_agc;
1507 case V4L2_CID_PRIVATE_COMBFILTER:
1508 c->value = btv->opt_combfilter;
1510 case V4L2_CID_PRIVATE_LUMAFILTER:
1511 c->value = btv->opt_lumafilter;
1513 case V4L2_CID_PRIVATE_AUTOMUTE:
1514 c->value = btv->opt_automute;
1516 case V4L2_CID_PRIVATE_AGC_CRUSH:
1517 c->value = btv->opt_adc_crush;
1519 case V4L2_CID_PRIVATE_VCR_HACK:
1520 c->value = btv->opt_vcr_hack;
1522 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1523 c->value = btv->opt_whitecrush_upper;
1525 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1526 c->value = btv->opt_whitecrush_lower;
1528 case V4L2_CID_PRIVATE_UV_RATIO:
1529 c->value = btv->opt_uv_ratio;
1531 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1532 c->value = btv->opt_full_luma_range;
1534 case V4L2_CID_PRIVATE_CORING:
1535 c->value = btv->opt_coring;
1543 static int bttv_s_ctrl(struct file *file, void *f,
1544 struct v4l2_control *c)
1547 struct bttv_fh *fh = f;
1548 struct bttv *btv = fh->btv;
1550 err = v4l2_prio_check(&btv->prio, fh->prio);
1555 case V4L2_CID_BRIGHTNESS:
1556 bt848_bright(btv, c->value);
1559 bt848_hue(btv, c->value);
1561 case V4L2_CID_CONTRAST:
1562 bt848_contrast(btv, c->value);
1564 case V4L2_CID_SATURATION:
1565 bt848_sat(btv, c->value);
1567 case V4L2_CID_COLOR_KILLER:
1568 btv->opt_color_killer = c->value;
1569 if (btv->opt_color_killer) {
1570 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1571 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1573 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1574 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1577 case V4L2_CID_AUDIO_MUTE:
1578 audio_mute(btv, c->value);
1580 case V4L2_CID_AUDIO_VOLUME:
1581 if (btv->volume_gpio)
1582 btv->volume_gpio(btv, c->value);
1584 bttv_call_all(btv, core, s_ctrl, c);
1586 case V4L2_CID_AUDIO_BALANCE:
1587 case V4L2_CID_AUDIO_BASS:
1588 case V4L2_CID_AUDIO_TREBLE:
1589 bttv_call_all(btv, core, s_ctrl, c);
1592 case V4L2_CID_PRIVATE_CHROMA_AGC:
1593 btv->opt_chroma_agc = c->value;
1594 if (btv->opt_chroma_agc) {
1595 btor(BT848_SCLOOP_CAGC, BT848_E_SCLOOP);
1596 btor(BT848_SCLOOP_CAGC, BT848_O_SCLOOP);
1598 btand(~BT848_SCLOOP_CAGC, BT848_E_SCLOOP);
1599 btand(~BT848_SCLOOP_CAGC, BT848_O_SCLOOP);
1602 case V4L2_CID_PRIVATE_COMBFILTER:
1603 btv->opt_combfilter = c->value;
1605 case V4L2_CID_PRIVATE_LUMAFILTER:
1606 btv->opt_lumafilter = c->value;
1607 if (btv->opt_lumafilter) {
1608 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1609 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1611 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1612 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1615 case V4L2_CID_PRIVATE_AUTOMUTE:
1616 btv->opt_automute = c->value;
1618 case V4L2_CID_PRIVATE_AGC_CRUSH:
1619 btv->opt_adc_crush = c->value;
1620 btwrite(BT848_ADC_RESERVED |
1621 (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1624 case V4L2_CID_PRIVATE_VCR_HACK:
1625 btv->opt_vcr_hack = c->value;
1627 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1628 btv->opt_whitecrush_upper = c->value;
1629 btwrite(c->value, BT848_WC_UP);
1631 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1632 btv->opt_whitecrush_lower = c->value;
1633 btwrite(c->value, BT848_WC_DOWN);
1635 case V4L2_CID_PRIVATE_UV_RATIO:
1636 btv->opt_uv_ratio = c->value;
1637 bt848_sat(btv, btv->saturation);
1639 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1640 btv->opt_full_luma_range = c->value;
1641 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1643 case V4L2_CID_PRIVATE_CORING:
1644 btv->opt_coring = c->value;
1645 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1653 /* ----------------------------------------------------------------------- */
1655 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1657 unsigned int outbits, data;
1658 outbits = btread(BT848_GPIO_OUT_EN);
1659 data = btread(BT848_GPIO_DATA);
1660 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1661 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
1664 static void bttv_field_count(struct bttv *btv)
1672 /* start field counter */
1673 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1675 /* stop field counter */
1676 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1677 btv->field_count = 0;
1681 static const struct bttv_format*
1682 format_by_fourcc(int fourcc)
1686 for (i = 0; i < FORMATS; i++) {
1687 if (-1 == formats[i].fourcc)
1689 if (formats[i].fourcc == fourcc)
1695 /* ----------------------------------------------------------------------- */
1699 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1700 struct bttv_buffer *new)
1702 struct bttv_buffer *old;
1703 unsigned long flags;
1706 dprintk("switch_overlay: enter [new=%p]\n", new);
1708 new->vb.state = VIDEOBUF_DONE;
1709 spin_lock_irqsave(&btv->s_lock,flags);
1713 bttv_set_dma(btv, 0x03);
1714 spin_unlock_irqrestore(&btv->s_lock,flags);
1716 dprintk("switch_overlay: old=%p state is %d\n",
1717 old, old->vb.state);
1718 bttv_dma_free(&fh->cap,btv, old);
1722 free_btres_lock(btv,fh,RESOURCE_OVERLAY);
1723 dprintk("switch_overlay: done\n");
1727 /* ----------------------------------------------------------------------- */
1728 /* video4linux (1) interface */
1730 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1731 struct bttv_buffer *buf,
1732 const struct bttv_format *fmt,
1733 unsigned int width, unsigned int height,
1734 enum v4l2_field field)
1736 struct bttv_fh *fh = q->priv_data;
1737 int redo_dma_risc = 0;
1742 /* check settings */
1745 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1747 height = RAW_LINES*2;
1748 if (width*height > buf->vb.bsize)
1750 buf->vb.size = buf->vb.bsize;
1752 /* Make sure tvnorm and vbi_end remain consistent
1753 until we're done. */
1757 /* In this mode capturing always starts at defrect.top
1758 (default VDELAY), ignoring cropping parameters. */
1759 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1763 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1766 c = btv->crop[!!fh->do_crop];
1768 if (width < c.min_scaled_width ||
1769 width > c.max_scaled_width ||
1770 height < c.min_scaled_height)
1774 case V4L2_FIELD_TOP:
1775 case V4L2_FIELD_BOTTOM:
1776 case V4L2_FIELD_ALTERNATE:
1777 /* btv->crop counts frame lines. Max. scale
1778 factor is 16:1 for frames, 8:1 for fields. */
1779 if (height * 2 > c.max_scaled_height)
1784 if (height > c.max_scaled_height)
1789 buf->vb.size = (width * height * fmt->depth) >> 3;
1790 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1794 /* alloc + fill struct bttv_buffer (if changed) */
1795 if (buf->vb.width != width || buf->vb.height != height ||
1796 buf->vb.field != field ||
1797 buf->tvnorm != norm || buf->fmt != fmt ||
1798 buf->crop.top != c.rect.top ||
1799 buf->crop.left != c.rect.left ||
1800 buf->crop.width != c.rect.width ||
1801 buf->crop.height != c.rect.height) {
1802 buf->vb.width = width;
1803 buf->vb.height = height;
1804 buf->vb.field = field;
1811 /* alloc risc memory */
1812 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
1814 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1819 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1822 buf->vb.state = VIDEOBUF_PREPARED;
1826 bttv_dma_free(q,btv,buf);
1831 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1833 struct bttv_fh *fh = q->priv_data;
1835 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1838 if (*size * *count > gbuffers * gbufsize)
1839 *count = (gbuffers * gbufsize) / *size;
1844 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1845 enum v4l2_field field)
1847 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1848 struct bttv_fh *fh = q->priv_data;
1850 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1851 fh->width, fh->height, field);
1855 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1857 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1858 struct bttv_fh *fh = q->priv_data;
1859 struct bttv *btv = fh->btv;
1861 buf->vb.state = VIDEOBUF_QUEUED;
1862 list_add_tail(&buf->vb.queue,&btv->capture);
1863 if (!btv->curr.frame_irq) {
1865 bttv_set_dma(btv, 0x03);
1869 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1871 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1872 struct bttv_fh *fh = q->priv_data;
1874 bttv_dma_free(q,fh->btv,buf);
1877 static struct videobuf_queue_ops bttv_video_qops = {
1878 .buf_setup = buffer_setup,
1879 .buf_prepare = buffer_prepare,
1880 .buf_queue = buffer_queue,
1881 .buf_release = buffer_release,
1884 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
1886 struct bttv_fh *fh = priv;
1887 struct bttv *btv = fh->btv;
1891 err = v4l2_prio_check(&btv->prio, fh->prio);
1895 for (i = 0; i < BTTV_TVNORMS; i++)
1896 if (*id & bttv_tvnorms[i].v4l2_id)
1898 if (i == BTTV_TVNORMS) {
1910 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1912 struct bttv_fh *fh = f;
1913 struct bttv *btv = fh->btv;
1915 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1916 *id = V4L2_STD_625_50;
1918 *id = V4L2_STD_525_60;
1922 static int bttv_enum_input(struct file *file, void *priv,
1923 struct v4l2_input *i)
1925 struct bttv_fh *fh = priv;
1926 struct bttv *btv = fh->btv;
1929 if (i->index >= bttv_tvcards[btv->c.type].video_inputs) {
1934 i->type = V4L2_INPUT_TYPE_CAMERA;
1937 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1938 sprintf(i->name, "Television");
1939 i->type = V4L2_INPUT_TYPE_TUNER;
1941 } else if (i->index == btv->svhs) {
1942 sprintf(i->name, "S-Video");
1944 sprintf(i->name, "Composite%d", i->index);
1947 if (i->index == btv->input) {
1948 __u32 dstatus = btread(BT848_DSTATUS);
1949 if (0 == (dstatus & BT848_DSTATUS_PRES))
1950 i->status |= V4L2_IN_ST_NO_SIGNAL;
1951 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1952 i->status |= V4L2_IN_ST_NO_H_LOCK;
1955 i->std = BTTV_NORMS;
1962 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1964 struct bttv_fh *fh = priv;
1965 struct bttv *btv = fh->btv;
1972 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1974 struct bttv_fh *fh = priv;
1975 struct bttv *btv = fh->btv;
1979 err = v4l2_prio_check(&btv->prio, fh->prio);
1983 if (i > bttv_tvcards[btv->c.type].video_inputs) {
1988 set_input(btv, i, btv->tvnorm);
1994 static int bttv_s_tuner(struct file *file, void *priv,
1995 struct v4l2_tuner *t)
1997 struct bttv_fh *fh = priv;
1998 struct bttv *btv = fh->btv;
2001 if (unlikely(0 != t->index))
2004 if (unlikely(btv->tuner_type == TUNER_ABSENT)) {
2009 err = v4l2_prio_check(&btv->prio, fh->prio);
2013 bttv_call_all(btv, tuner, s_tuner, t);
2015 if (btv->audio_mode_gpio)
2016 btv->audio_mode_gpio(btv, t, 1);
2023 static int bttv_g_frequency(struct file *file, void *priv,
2024 struct v4l2_frequency *f)
2026 struct bttv_fh *fh = priv;
2027 struct bttv *btv = fh->btv;
2029 f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2030 f->frequency = btv->freq;
2035 static int bttv_s_frequency(struct file *file, void *priv,
2036 struct v4l2_frequency *f)
2038 struct bttv_fh *fh = priv;
2039 struct bttv *btv = fh->btv;
2042 if (unlikely(f->tuner != 0))
2045 err = v4l2_prio_check(&btv->prio, fh->prio);
2049 if (unlikely(f->type != (btv->radio_user
2050 ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV))) {
2054 btv->freq = f->frequency;
2055 bttv_call_all(btv, tuner, s_frequency, f);
2056 if (btv->has_matchbox && btv->radio_user)
2057 tea5757_set_freq(btv, btv->freq);
2063 static int bttv_log_status(struct file *file, void *f)
2065 struct bttv_fh *fh = f;
2066 struct bttv *btv = fh->btv;
2068 bttv_call_all(btv, core, log_status);
2072 #ifdef CONFIG_VIDEO_ADV_DEBUG
2073 static int bttv_g_register(struct file *file, void *f,
2074 struct v4l2_dbg_register *reg)
2076 struct bttv_fh *fh = f;
2077 struct bttv *btv = fh->btv;
2079 if (!capable(CAP_SYS_ADMIN))
2082 if (!v4l2_chip_match_host(®->match))
2085 /* bt848 has a 12-bit register space */
2087 reg->val = btread(reg->reg);
2093 static int bttv_s_register(struct file *file, void *f,
2094 struct v4l2_dbg_register *reg)
2096 struct bttv_fh *fh = f;
2097 struct bttv *btv = fh->btv;
2099 if (!capable(CAP_SYS_ADMIN))
2102 if (!v4l2_chip_match_host(®->match))
2105 /* bt848 has a 12-bit register space */
2107 btwrite(reg->val, reg->reg);
2113 /* Given cropping boundaries b and the scaled width and height of a
2114 single field or frame, which must not exceed hardware limits, this
2115 function adjusts the cropping parameters c. */
2117 bttv_crop_adjust (struct bttv_crop * c,
2118 const struct v4l2_rect * b,
2121 enum v4l2_field field)
2123 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2127 if (width < c->min_scaled_width) {
2128 /* Max. hor. scale factor 16:1. */
2129 c->rect.width = width * 16;
2130 } else if (width > c->max_scaled_width) {
2131 /* Min. hor. scale factor 1:1. */
2132 c->rect.width = width;
2134 max_left = b->left + b->width - width;
2135 max_left = min(max_left, (__s32) MAX_HDELAY);
2136 if (c->rect.left > max_left)
2137 c->rect.left = max_left;
2140 if (height < c->min_scaled_height) {
2141 /* Max. vert. scale factor 16:1, single fields 8:1. */
2142 c->rect.height = height * 16;
2143 } else if (frame_height > c->max_scaled_height) {
2144 /* Min. vert. scale factor 1:1.
2145 Top and height count field lines times two. */
2146 c->rect.height = (frame_height + 1) & ~1;
2148 max_top = b->top + b->height - c->rect.height;
2149 if (c->rect.top > max_top)
2150 c->rect.top = max_top;
2153 bttv_crop_calc_limits(c);
2156 /* Returns an error if scaling to a frame or single field with the given
2157 width and height is not possible with the current cropping parameters
2158 and width aligned according to width_mask. If adjust_size is TRUE the
2159 function may adjust the width and/or height instead, rounding width
2160 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2161 also adjust the current cropping parameters to get closer to the
2162 desired image size. */
2164 limit_scaled_size_lock (struct bttv_fh * fh,
2167 enum v4l2_field field,
2168 unsigned int width_mask,
2169 unsigned int width_bias,
2173 struct bttv *btv = fh->btv;
2174 const struct v4l2_rect *b;
2175 struct bttv_crop *c;
2182 BUG_ON((int) width_mask >= 0 ||
2183 width_bias >= (unsigned int) -width_mask);
2185 /* Make sure tvnorm, vbi_end and the current cropping parameters
2186 remain consistent until we're done. */
2188 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2190 /* Do crop - use current, don't - use default parameters. */
2191 c = &btv->crop[!!fh->do_crop];
2196 && !locked_btres(btv, VIDEO_RESOURCES)) {
2200 /* We cannot scale up. When the scaled image is larger
2201 than crop.rect we adjust the crop.rect as required
2202 by the V4L2 spec, hence cropcap.bounds are our limit. */
2203 max_width = min(b->width, (__s32) MAX_HACTIVE);
2204 max_height = b->height;
2206 /* We cannot capture the same line as video and VBI data.
2207 Note btv->vbi_end is really a minimum, see
2208 bttv_vbi_try_fmt(). */
2209 if (btv->vbi_end > b->top) {
2210 max_height -= btv->vbi_end - b->top;
2212 if (min_height > max_height)
2217 if (btv->vbi_end > c->rect.top)
2220 min_width = c->min_scaled_width;
2221 min_height = c->min_scaled_height;
2222 max_width = c->max_scaled_width;
2223 max_height = c->max_scaled_height;
2228 min_width = (min_width - width_mask - 1) & width_mask;
2229 max_width = max_width & width_mask;
2231 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2232 min_height = min_height;
2233 /* Min. scale factor is 1:1. */
2234 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2237 *width = clamp(*width, min_width, max_width);
2238 *height = clamp(*height, min_height, max_height);
2240 /* Round after clamping to avoid overflow. */
2241 *width = (*width + width_bias) & width_mask;
2244 bttv_crop_adjust(c, b, *width, *height, field);
2246 if (btv->vbi_end > c->rect.top) {
2247 /* Move the crop window out of the way. */
2248 c->rect.top = btv->vbi_end;
2253 if (*width < min_width ||
2254 *height < min_height ||
2255 *width > max_width ||
2256 *height > max_height ||
2257 0 != (*width & ~width_mask))
2261 rc = 0; /* success */
2268 /* Returns an error if the given overlay window dimensions are not
2269 possible with the current cropping parameters. If adjust_size is
2270 TRUE the function may adjust the window width and/or height
2271 instead, however it always rounds the horizontal position and
2272 width as btcx_align() does. If adjust_crop is TRUE the function
2273 may also adjust the current cropping parameters to get closer
2274 to the desired window size. */
2276 verify_window_lock (struct bttv_fh * fh,
2277 struct v4l2_window * win,
2281 enum v4l2_field field;
2282 unsigned int width_mask;
2285 if (win->w.width < 48 || win->w.height < 32)
2287 if (win->clipcount > 2048)
2292 if (V4L2_FIELD_ANY == field) {
2295 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2296 field = (win->w.height > height2)
2297 ? V4L2_FIELD_INTERLACED
2301 case V4L2_FIELD_TOP:
2302 case V4L2_FIELD_BOTTOM:
2303 case V4L2_FIELD_INTERLACED:
2309 /* 4-byte alignment. */
2310 if (NULL == fh->ovfmt)
2313 switch (fh->ovfmt->depth) {
2327 win->w.width -= win->w.left & ~width_mask;
2328 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2330 rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
2332 /* width_bias: round down */ 0,
2333 adjust_size, adjust_crop);
2341 static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
2342 struct v4l2_window *win, int fixup)
2344 struct v4l2_clip *clips = NULL;
2345 int n,size,retval = 0;
2347 if (NULL == fh->ovfmt)
2349 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2351 retval = verify_window_lock(fh, win,
2352 /* adjust_size */ fixup,
2353 /* adjust_crop */ fixup);
2357 /* copy clips -- luckily v4l1 + v4l2 are binary
2358 compatible here ...*/
2360 size = sizeof(*clips)*(n+4);
2361 clips = kmalloc(size,GFP_KERNEL);
2365 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2371 /* clip against screen */
2372 if (NULL != btv->fbuf.base)
2373 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2375 btcx_sort_clips(clips,n);
2377 /* 4-byte alignments */
2378 switch (fh->ovfmt->depth) {
2381 btcx_align(&win->w, clips, n, 3);
2384 btcx_align(&win->w, clips, n, 1);
2387 /* no alignment fixups needed */
2393 kfree(fh->ov.clips);
2394 fh->ov.clips = clips;
2398 fh->ov.field = win->field;
2399 fh->ov.setup_ok = 1;
2401 btv->init.ov.w.width = win->w.width;
2402 btv->init.ov.w.height = win->w.height;
2403 btv->init.ov.field = win->field;
2405 /* update overlay if needed */
2407 if (check_btres(fh, RESOURCE_OVERLAY)) {
2408 struct bttv_buffer *new;
2410 new = videobuf_sg_alloc(sizeof(*new));
2411 new->crop = btv->crop[!!fh->do_crop].rect;
2412 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2413 retval = bttv_switch_overlay(btv,fh,new);
2418 /* ----------------------------------------------------------------------- */
2420 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2422 struct videobuf_queue* q = NULL;
2425 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2428 case V4L2_BUF_TYPE_VBI_CAPTURE:
2437 static int bttv_resource(struct bttv_fh *fh)
2442 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2443 res = RESOURCE_VIDEO_STREAM;
2445 case V4L2_BUF_TYPE_VBI_CAPTURE:
2454 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2456 struct videobuf_queue *q = bttv_queue(fh);
2457 int res = bttv_resource(fh);
2459 if (check_btres(fh,res))
2461 if (videobuf_queue_is_busy(q))
2468 pix_format_set_size (struct v4l2_pix_format * f,
2469 const struct bttv_format * fmt,
2471 unsigned int height)
2476 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2477 f->bytesperline = width; /* Y plane */
2478 f->sizeimage = (width * height * fmt->depth) >> 3;
2480 f->bytesperline = (width * fmt->depth) >> 3;
2481 f->sizeimage = height * f->bytesperline;
2485 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
2486 struct v4l2_format *f)
2488 struct bttv_fh *fh = priv;
2490 pix_format_set_size(&f->fmt.pix, fh->fmt,
2491 fh->width, fh->height);
2492 f->fmt.pix.field = fh->cap.field;
2493 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2498 static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
2499 struct v4l2_format *f)
2501 struct bttv_fh *fh = priv;
2503 f->fmt.win.w = fh->ov.w;
2504 f->fmt.win.field = fh->ov.field;
2509 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2510 struct v4l2_format *f)
2512 const struct bttv_format *fmt;
2513 struct bttv_fh *fh = priv;
2514 struct bttv *btv = fh->btv;
2515 enum v4l2_field field;
2516 __s32 width, height;
2519 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2523 field = f->fmt.pix.field;
2525 if (V4L2_FIELD_ANY == field) {
2528 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2529 field = (f->fmt.pix.height > height2)
2530 ? V4L2_FIELD_INTERLACED
2531 : V4L2_FIELD_BOTTOM;
2534 if (V4L2_FIELD_SEQ_BT == field)
2535 field = V4L2_FIELD_SEQ_TB;
2538 case V4L2_FIELD_TOP:
2539 case V4L2_FIELD_BOTTOM:
2540 case V4L2_FIELD_ALTERNATE:
2541 case V4L2_FIELD_INTERLACED:
2543 case V4L2_FIELD_SEQ_TB:
2544 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2551 width = f->fmt.pix.width;
2552 height = f->fmt.pix.height;
2554 rc = limit_scaled_size_lock(fh, &width, &height, field,
2555 /* width_mask: 4 pixels */ ~3,
2556 /* width_bias: nearest */ 2,
2557 /* adjust_size */ 1,
2558 /* adjust_crop */ 0);
2562 /* update data for the application */
2563 f->fmt.pix.field = field;
2564 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2569 static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
2570 struct v4l2_format *f)
2572 struct bttv_fh *fh = priv;
2574 return verify_window_lock(fh, &f->fmt.win,
2575 /* adjust_size */ 1,
2576 /* adjust_crop */ 0);
2579 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2580 struct v4l2_format *f)
2583 const struct bttv_format *fmt;
2584 struct bttv_fh *fh = priv;
2585 struct bttv *btv = fh->btv;
2586 __s32 width, height;
2587 enum v4l2_field field;
2589 retval = bttv_switch_type(fh, f->type);
2593 retval = bttv_try_fmt_vid_cap(file, priv, f);
2597 width = f->fmt.pix.width;
2598 height = f->fmt.pix.height;
2599 field = f->fmt.pix.field;
2601 retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
2602 /* width_mask: 4 pixels */ ~3,
2603 /* width_bias: nearest */ 2,
2604 /* adjust_size */ 1,
2605 /* adjust_crop */ 1);
2609 f->fmt.pix.field = field;
2611 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2613 /* update our state informations */
2615 fh->cap.field = f->fmt.pix.field;
2616 fh->cap.last = V4L2_FIELD_NONE;
2617 fh->width = f->fmt.pix.width;
2618 fh->height = f->fmt.pix.height;
2619 btv->init.fmt = fmt;
2620 btv->init.width = f->fmt.pix.width;
2621 btv->init.height = f->fmt.pix.height;
2626 static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
2627 struct v4l2_format *f)
2629 struct bttv_fh *fh = priv;
2630 struct bttv *btv = fh->btv;
2632 if (no_overlay > 0) {
2633 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2637 return setup_window_lock(fh, btv, &f->fmt.win, 1);
2640 static int bttv_querycap(struct file *file, void *priv,
2641 struct v4l2_capability *cap)
2643 struct bttv_fh *fh = priv;
2644 struct bttv *btv = fh->btv;
2649 strlcpy(cap->driver, "bttv", sizeof(cap->driver));
2650 strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
2651 snprintf(cap->bus_info, sizeof(cap->bus_info),
2652 "PCI:%s", pci_name(btv->c.pci));
2654 V4L2_CAP_VIDEO_CAPTURE |
2655 V4L2_CAP_VBI_CAPTURE |
2656 V4L2_CAP_READWRITE |
2658 if (no_overlay <= 0)
2659 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2662 * No need to lock here: those vars are initialized during board
2663 * probe and remains untouched during the rest of the driver lifecycle
2665 if (btv->has_saa6588)
2666 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2667 if (btv->tuner_type != TUNER_ABSENT)
2668 cap->capabilities |= V4L2_CAP_TUNER;
2672 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
2676 for (i = 0; i < FORMATS; i++) {
2677 if (formats[i].fourcc != -1)
2679 if ((unsigned int)index == f->index)
2685 f->pixelformat = formats[i].fourcc;
2686 strlcpy(f->description, formats[i].name, sizeof(f->description));
2691 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2692 struct v4l2_fmtdesc *f)
2694 int rc = bttv_enum_fmt_cap_ovr(f);
2702 static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
2703 struct v4l2_fmtdesc *f)
2707 if (no_overlay > 0) {
2708 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2712 rc = bttv_enum_fmt_cap_ovr(f);
2717 if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
2723 static int bttv_g_fbuf(struct file *file, void *f,
2724 struct v4l2_framebuffer *fb)
2726 struct bttv_fh *fh = f;
2727 struct bttv *btv = fh->btv;
2730 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2732 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2736 static int bttv_overlay(struct file *file, void *f, unsigned int on)
2738 struct bttv_fh *fh = f;
2739 struct bttv *btv = fh->btv;
2740 struct bttv_buffer *new;
2745 if (unlikely(!btv->fbuf.base)) {
2748 if (unlikely(!fh->ov.setup_ok)) {
2749 dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
2756 if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
2760 fh->ov.tvnorm = btv->tvnorm;
2761 new = videobuf_sg_alloc(sizeof(*new));
2762 new->crop = btv->crop[!!fh->do_crop].rect;
2763 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2769 retval = bttv_switch_overlay(btv, fh, new);
2773 static int bttv_s_fbuf(struct file *file, void *f,
2774 const struct v4l2_framebuffer *fb)
2776 struct bttv_fh *fh = f;
2777 struct bttv *btv = fh->btv;
2778 const struct bttv_format *fmt;
2781 if (!capable(CAP_SYS_ADMIN) &&
2782 !capable(CAP_SYS_RAWIO))
2786 fmt = format_by_fourcc(fb->fmt.pixelformat);
2789 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2793 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2794 __s32 width = fb->fmt.width;
2795 __s32 height = fb->fmt.height;
2797 retval = limit_scaled_size_lock(fh, &width, &height,
2798 V4L2_FIELD_INTERLACED,
2799 /* width_mask */ ~3,
2801 /* adjust_size */ 0,
2802 /* adjust_crop */ 0);
2808 btv->fbuf.base = fb->base;
2809 btv->fbuf.fmt.width = fb->fmt.width;
2810 btv->fbuf.fmt.height = fb->fmt.height;
2811 if (0 != fb->fmt.bytesperline)
2812 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2814 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2818 btv->init.ovfmt = fmt;
2819 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2822 fh->ov.w.width = fb->fmt.width;
2823 fh->ov.w.height = fb->fmt.height;
2824 btv->init.ov.w.width = fb->fmt.width;
2825 btv->init.ov.w.height = fb->fmt.height;
2826 kfree(fh->ov.clips);
2827 fh->ov.clips = NULL;
2830 if (check_btres(fh, RESOURCE_OVERLAY)) {
2831 struct bttv_buffer *new;
2833 new = videobuf_sg_alloc(sizeof(*new));
2834 new->crop = btv->crop[!!fh->do_crop].rect;
2835 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2836 retval = bttv_switch_overlay(btv, fh, new);
2842 static int bttv_reqbufs(struct file *file, void *priv,
2843 struct v4l2_requestbuffers *p)
2845 struct bttv_fh *fh = priv;
2846 return videobuf_reqbufs(bttv_queue(fh), p);
2849 static int bttv_querybuf(struct file *file, void *priv,
2850 struct v4l2_buffer *b)
2852 struct bttv_fh *fh = priv;
2853 return videobuf_querybuf(bttv_queue(fh), b);
2856 static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2858 struct bttv_fh *fh = priv;
2859 struct bttv *btv = fh->btv;
2860 int res = bttv_resource(fh);
2862 if (!check_alloc_btres_lock(btv, fh, res))
2865 return videobuf_qbuf(bttv_queue(fh), b);
2868 static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2870 struct bttv_fh *fh = priv;
2871 return videobuf_dqbuf(bttv_queue(fh), b,
2872 file->f_flags & O_NONBLOCK);
2875 static int bttv_streamon(struct file *file, void *priv,
2876 enum v4l2_buf_type type)
2878 struct bttv_fh *fh = priv;
2879 struct bttv *btv = fh->btv;
2880 int res = bttv_resource(fh);
2882 if (!check_alloc_btres_lock(btv, fh, res))
2884 return videobuf_streamon(bttv_queue(fh));
2888 static int bttv_streamoff(struct file *file, void *priv,
2889 enum v4l2_buf_type type)
2891 struct bttv_fh *fh = priv;
2892 struct bttv *btv = fh->btv;
2894 int res = bttv_resource(fh);
2897 retval = videobuf_streamoff(bttv_queue(fh));
2900 free_btres_lock(btv, fh, res);
2904 static int bttv_queryctrl(struct file *file, void *priv,
2905 struct v4l2_queryctrl *c)
2907 struct bttv_fh *fh = priv;
2908 struct bttv *btv = fh->btv;
2909 const struct v4l2_queryctrl *ctrl;
2911 if ((c->id < V4L2_CID_BASE ||
2912 c->id >= V4L2_CID_LASTP1) &&
2913 (c->id < V4L2_CID_PRIVATE_BASE ||
2914 c->id >= V4L2_CID_PRIVATE_LASTP1))
2917 if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
2920 ctrl = ctrl_by_id(c->id);
2922 *c = (NULL != ctrl) ? *ctrl : no_ctl;
2928 static int bttv_g_parm(struct file *file, void *f,
2929 struct v4l2_streamparm *parm)
2931 struct bttv_fh *fh = f;
2932 struct bttv *btv = fh->btv;
2934 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2935 &parm->parm.capture.timeperframe);
2940 static int bttv_g_tuner(struct file *file, void *priv,
2941 struct v4l2_tuner *t)
2943 struct bttv_fh *fh = priv;
2944 struct bttv *btv = fh->btv;
2946 if (btv->tuner_type == TUNER_ABSENT)
2951 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2952 bttv_call_all(btv, tuner, g_tuner, t);
2953 strcpy(t->name, "Television");
2954 t->capability = V4L2_TUNER_CAP_NORM;
2955 t->type = V4L2_TUNER_ANALOG_TV;
2956 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2959 if (btv->audio_mode_gpio)
2960 btv->audio_mode_gpio(btv, t, 0);
2965 static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
2967 struct bttv_fh *fh = f;
2968 struct bttv *btv = fh->btv;
2970 *p = v4l2_prio_max(&btv->prio);
2975 static int bttv_s_priority(struct file *file, void *f,
2976 enum v4l2_priority prio)
2978 struct bttv_fh *fh = f;
2979 struct bttv *btv = fh->btv;
2982 rc = v4l2_prio_change(&btv->prio, &fh->prio, prio);
2987 static int bttv_cropcap(struct file *file, void *priv,
2988 struct v4l2_cropcap *cap)
2990 struct bttv_fh *fh = priv;
2991 struct bttv *btv = fh->btv;
2993 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2994 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
2997 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3002 static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
3004 struct bttv_fh *fh = f;
3005 struct bttv *btv = fh->btv;
3007 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3008 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3011 /* No fh->do_crop = 1; because btv->crop[1] may be
3012 inconsistent with fh->width or fh->height and apps
3013 do not expect a change here. */
3015 crop->c = btv->crop[!!fh->do_crop].rect;
3020 static int bttv_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
3022 struct bttv_fh *fh = f;
3023 struct bttv *btv = fh->btv;
3024 const struct v4l2_rect *b;
3032 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3033 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3036 /* Make sure tvnorm, vbi_end and the current cropping
3037 parameters remain consistent until we're done. Note
3038 read() may change vbi_end in check_alloc_btres_lock(). */
3039 retval = v4l2_prio_check(&btv->prio, fh->prio);
3046 if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
3050 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3053 b_right = b_left + b->width;
3054 b_bottom = b->top + b->height;
3056 b_top = max(b->top, btv->vbi_end);
3057 if (b_top + 32 >= b_bottom) {
3061 /* Min. scaled size 48 x 32. */
3062 c.rect.left = clamp_t(s32, crop->c.left, b_left, b_right - 48);
3063 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3065 c.rect.width = clamp_t(s32, crop->c.width,
3066 48, b_right - c.rect.left);
3068 c.rect.top = clamp_t(s32, crop->c.top, b_top, b_bottom - 32);
3069 /* Top and height must be a multiple of two. */
3070 c.rect.top = (c.rect.top + 1) & ~1;
3072 c.rect.height = clamp_t(s32, crop->c.height,
3073 32, b_bottom - c.rect.top);
3074 c.rect.height = (c.rect.height + 1) & ~1;
3076 bttv_crop_calc_limits(&c);
3082 if (fh->width < c.min_scaled_width) {
3083 fh->width = c.min_scaled_width;
3084 btv->init.width = c.min_scaled_width;
3085 } else if (fh->width > c.max_scaled_width) {
3086 fh->width = c.max_scaled_width;
3087 btv->init.width = c.max_scaled_width;
3090 if (fh->height < c.min_scaled_height) {
3091 fh->height = c.min_scaled_height;
3092 btv->init.height = c.min_scaled_height;
3093 } else if (fh->height > c.max_scaled_height) {
3094 fh->height = c.max_scaled_height;
3095 btv->init.height = c.max_scaled_height;
3101 static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
3103 if (unlikely(a->index))
3106 strcpy(a->name, "audio");
3110 static int bttv_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
3112 if (unlikely(a->index))
3118 static ssize_t bttv_read(struct file *file, char __user *data,
3119 size_t count, loff_t *ppos)
3121 struct bttv_fh *fh = file->private_data;
3124 if (fh->btv->errors)
3125 bttv_reinit_bt848(fh->btv);
3126 dprintk("%d: read count=%d type=%s\n",
3127 fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
3130 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3131 if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3132 /* VIDEO_READ in use by another fh,
3133 or VIDEO_STREAM by any fh. */
3136 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3137 file->f_flags & O_NONBLOCK);
3138 free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
3140 case V4L2_BUF_TYPE_VBI_CAPTURE:
3141 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3143 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3144 file->f_flags & O_NONBLOCK);
3152 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3154 struct bttv_fh *fh = file->private_data;
3155 struct bttv_buffer *buf;
3156 enum v4l2_field field;
3157 unsigned int rc = POLLERR;
3159 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3160 if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
3162 return videobuf_poll_stream(file, &fh->vbi, wait);
3165 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3166 /* streaming capture */
3167 if (list_empty(&fh->cap.stream))
3169 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3171 /* read() capture */
3172 if (NULL == fh->cap.read_buf) {
3173 /* need to capture a new frame */
3174 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
3176 fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
3177 if (NULL == fh->cap.read_buf)
3179 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3180 field = videobuf_next_field(&fh->cap);
3181 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3182 kfree (fh->cap.read_buf);
3183 fh->cap.read_buf = NULL;
3186 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3187 fh->cap.read_off = 0;
3189 buf = (struct bttv_buffer*)fh->cap.read_buf;
3192 poll_wait(file, &buf->vb.done, wait);
3193 if (buf->vb.state == VIDEOBUF_DONE ||
3194 buf->vb.state == VIDEOBUF_ERROR)
3195 rc = POLLIN|POLLRDNORM;
3202 static int bttv_open(struct file *file)
3204 struct video_device *vdev = video_devdata(file);
3205 struct bttv *btv = video_drvdata(file);
3207 enum v4l2_buf_type type = 0;
3209 dprintk("open dev=%s\n", video_device_node_name(vdev));
3211 if (vdev->vfl_type == VFL_TYPE_GRABBER) {
3212 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3213 } else if (vdev->vfl_type == VFL_TYPE_VBI) {
3214 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3220 dprintk("%d: open called (type=%s)\n",
3221 btv->c.nr, v4l2_type_names[type]);
3223 /* allocate per filehandle data */
3224 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3227 file->private_data = fh;
3232 fh->ov.setup_ok = 0;
3234 v4l2_prio_open(&btv->prio, &fh->prio);
3236 videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
3237 &btv->c.pci->dev, &btv->s_lock,
3238 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3239 V4L2_FIELD_INTERLACED,
3240 sizeof(struct bttv_buffer),
3242 videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
3243 &btv->c.pci->dev, &btv->s_lock,
3244 V4L2_BUF_TYPE_VBI_CAPTURE,
3246 sizeof(struct bttv_buffer),
3248 set_tvnorm(btv,btv->tvnorm);
3249 set_input(btv, btv->input, btv->tvnorm);
3253 /* The V4L2 spec requires one global set of cropping parameters
3254 which only change on request. These are stored in btv->crop[1].
3255 However for compatibility with V4L apps and cropping unaware
3256 V4L2 apps we now reset the cropping parameters as seen through
3257 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3258 will use btv->crop[0], the default cropping parameters for the
3259 current video standard, and VIDIOC_S_FMT will not implicitely
3260 change the cropping parameters until VIDIOC_S_CROP has been
3262 fh->do_crop = !reset_crop; /* module parameter */
3264 /* Likewise there should be one global set of VBI capture
3265 parameters, but for compatibility with V4L apps and earlier
3266 driver versions each fh has its own parameters. */
3267 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3269 bttv_field_count(btv);
3273 static int bttv_release(struct file *file)
3275 struct bttv_fh *fh = file->private_data;
3276 struct bttv *btv = fh->btv;
3278 /* turn off overlay */
3279 if (check_btres(fh, RESOURCE_OVERLAY))
3280 bttv_switch_overlay(btv,fh,NULL);
3282 /* stop video capture */
3283 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3284 videobuf_streamoff(&fh->cap);
3285 free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
3287 if (fh->cap.read_buf) {
3288 buffer_release(&fh->cap,fh->cap.read_buf);
3289 kfree(fh->cap.read_buf);
3291 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3292 free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
3295 /* stop vbi capture */
3296 if (check_btres(fh, RESOURCE_VBI)) {
3297 videobuf_stop(&fh->vbi);
3298 free_btres_lock(btv,fh,RESOURCE_VBI);
3303 videobuf_mmap_free(&fh->cap);
3304 videobuf_mmap_free(&fh->vbi);
3305 v4l2_prio_close(&btv->prio, fh->prio);
3306 file->private_data = NULL;
3310 bttv_field_count(btv);
3319 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3321 struct bttv_fh *fh = file->private_data;
3323 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
3324 fh->btv->c.nr, v4l2_type_names[fh->type],
3325 vma->vm_start, vma->vm_end - vma->vm_start);
3326 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3329 static const struct v4l2_file_operations bttv_fops =
3331 .owner = THIS_MODULE,
3333 .release = bttv_release,
3334 .unlocked_ioctl = video_ioctl2,
3340 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
3341 .vidioc_querycap = bttv_querycap,
3342 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
3343 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
3344 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
3345 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
3346 .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
3347 .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
3348 .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
3349 .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
3350 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
3351 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
3352 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
3353 .vidioc_g_audio = bttv_g_audio,
3354 .vidioc_s_audio = bttv_s_audio,
3355 .vidioc_cropcap = bttv_cropcap,
3356 .vidioc_reqbufs = bttv_reqbufs,
3357 .vidioc_querybuf = bttv_querybuf,
3358 .vidioc_qbuf = bttv_qbuf,
3359 .vidioc_dqbuf = bttv_dqbuf,
3360 .vidioc_s_std = bttv_s_std,
3361 .vidioc_enum_input = bttv_enum_input,
3362 .vidioc_g_input = bttv_g_input,
3363 .vidioc_s_input = bttv_s_input,
3364 .vidioc_queryctrl = bttv_queryctrl,
3365 .vidioc_g_ctrl = bttv_g_ctrl,
3366 .vidioc_s_ctrl = bttv_s_ctrl,
3367 .vidioc_streamon = bttv_streamon,
3368 .vidioc_streamoff = bttv_streamoff,
3369 .vidioc_g_tuner = bttv_g_tuner,
3370 .vidioc_s_tuner = bttv_s_tuner,
3371 .vidioc_g_crop = bttv_g_crop,
3372 .vidioc_s_crop = bttv_s_crop,
3373 .vidioc_g_fbuf = bttv_g_fbuf,
3374 .vidioc_s_fbuf = bttv_s_fbuf,
3375 .vidioc_overlay = bttv_overlay,
3376 .vidioc_g_priority = bttv_g_priority,
3377 .vidioc_s_priority = bttv_s_priority,
3378 .vidioc_g_parm = bttv_g_parm,
3379 .vidioc_g_frequency = bttv_g_frequency,
3380 .vidioc_s_frequency = bttv_s_frequency,
3381 .vidioc_log_status = bttv_log_status,
3382 .vidioc_querystd = bttv_querystd,
3383 #ifdef CONFIG_VIDEO_ADV_DEBUG
3384 .vidioc_g_register = bttv_g_register,
3385 .vidioc_s_register = bttv_s_register,
3389 static struct video_device bttv_video_template = {
3391 .ioctl_ops = &bttv_ioctl_ops,
3392 .tvnorms = BTTV_NORMS,
3393 .current_norm = V4L2_STD_PAL,
3396 /* ----------------------------------------------------------------------- */
3397 /* radio interface */
3399 static int radio_open(struct file *file)
3401 struct video_device *vdev = video_devdata(file);
3402 struct bttv *btv = video_drvdata(file);
3405 dprintk("open dev=%s\n", video_device_node_name(vdev));
3407 dprintk("%d: open called (radio)\n", btv->c.nr);
3409 /* allocate per filehandle data */
3410 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
3413 file->private_data = fh;
3416 v4l2_prio_open(&btv->prio, &fh->prio);
3420 bttv_call_all(btv, tuner, s_radio);
3421 audio_input(btv,TVAUDIO_INPUT_RADIO);
3426 static int radio_release(struct file *file)
3428 struct bttv_fh *fh = file->private_data;
3429 struct bttv *btv = fh->btv;
3430 struct saa6588_command cmd;
3432 v4l2_prio_close(&btv->prio, fh->prio);
3433 file->private_data = NULL;
3438 bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
3443 static int radio_querycap(struct file *file, void *priv,
3444 struct v4l2_capability *cap)
3446 struct bttv_fh *fh = priv;
3447 struct bttv *btv = fh->btv;
3449 strcpy(cap->driver, "bttv");
3450 strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
3451 sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
3452 cap->capabilities = V4L2_CAP_TUNER;
3457 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
3459 struct bttv_fh *fh = priv;
3460 struct bttv *btv = fh->btv;
3462 if (btv->tuner_type == TUNER_ABSENT)
3466 strcpy(t->name, "Radio");
3467 t->type = V4L2_TUNER_RADIO;
3469 bttv_call_all(btv, tuner, g_tuner, t);
3471 if (btv->audio_mode_gpio)
3472 btv->audio_mode_gpio(btv, t, 0);
3477 static int radio_enum_input(struct file *file, void *priv,
3478 struct v4l2_input *i)
3483 strcpy(i->name, "Radio");
3484 i->type = V4L2_INPUT_TYPE_TUNER;
3489 static int radio_g_audio(struct file *file, void *priv,
3490 struct v4l2_audio *a)
3492 if (unlikely(a->index))
3495 strcpy(a->name, "Radio");
3500 static int radio_s_tuner(struct file *file, void *priv,
3501 struct v4l2_tuner *t)
3503 struct bttv_fh *fh = priv;
3504 struct bttv *btv = fh->btv;
3509 bttv_call_all(btv, tuner, s_tuner, t);
3513 static int radio_s_audio(struct file *file, void *priv,
3514 const struct v4l2_audio *a)
3516 if (unlikely(a->index))
3522 static int radio_s_input(struct file *filp, void *priv, unsigned int i)
3530 static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
3535 static int radio_queryctrl(struct file *file, void *priv,
3536 struct v4l2_queryctrl *c)
3538 const struct v4l2_queryctrl *ctrl;
3540 if (c->id < V4L2_CID_BASE ||
3541 c->id >= V4L2_CID_LASTP1)
3544 if (c->id == V4L2_CID_AUDIO_MUTE) {
3545 ctrl = ctrl_by_id(c->id);
3553 static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
3559 static ssize_t radio_read(struct file *file, char __user *data,
3560 size_t count, loff_t *ppos)
3562 struct bttv_fh *fh = file->private_data;
3563 struct bttv *btv = fh->btv;
3564 struct saa6588_command cmd;
3565 cmd.block_count = count/3;
3567 cmd.instance = file;
3568 cmd.result = -ENODEV;
3570 bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
3575 static unsigned int radio_poll(struct file *file, poll_table *wait)
3577 struct bttv_fh *fh = file->private_data;
3578 struct bttv *btv = fh->btv;
3579 struct saa6588_command cmd;
3580 cmd.instance = file;
3581 cmd.event_list = wait;
3582 cmd.result = -ENODEV;
3583 bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
3588 static const struct v4l2_file_operations radio_fops =
3590 .owner = THIS_MODULE,
3593 .release = radio_release,
3594 .unlocked_ioctl = video_ioctl2,
3598 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
3599 .vidioc_querycap = radio_querycap,
3600 .vidioc_g_tuner = radio_g_tuner,
3601 .vidioc_enum_input = radio_enum_input,
3602 .vidioc_g_audio = radio_g_audio,
3603 .vidioc_s_tuner = radio_s_tuner,
3604 .vidioc_s_audio = radio_s_audio,
3605 .vidioc_s_input = radio_s_input,
3606 .vidioc_s_std = radio_s_std,
3607 .vidioc_queryctrl = radio_queryctrl,
3608 .vidioc_g_input = radio_g_input,
3609 .vidioc_g_ctrl = bttv_g_ctrl,
3610 .vidioc_s_ctrl = bttv_s_ctrl,
3611 .vidioc_g_frequency = bttv_g_frequency,
3612 .vidioc_s_frequency = bttv_s_frequency,
3615 static struct video_device radio_template = {
3616 .fops = &radio_fops,
3617 .ioctl_ops = &radio_ioctl_ops,
3620 /* ----------------------------------------------------------------------- */
3621 /* some debug code */
3623 static int bttv_risc_decode(u32 risc)
3625 static char *instr[16] = {
3626 [ BT848_RISC_WRITE >> 28 ] = "write",
3627 [ BT848_RISC_SKIP >> 28 ] = "skip",
3628 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3629 [ BT848_RISC_JUMP >> 28 ] = "jump",
3630 [ BT848_RISC_SYNC >> 28 ] = "sync",
3631 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3632 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3633 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3635 static int incr[16] = {
3636 [ BT848_RISC_WRITE >> 28 ] = 2,
3637 [ BT848_RISC_JUMP >> 28 ] = 2,
3638 [ BT848_RISC_SYNC >> 28 ] = 2,
3639 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3640 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3641 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3643 static char *bits[] = {
3644 "be0", "be1", "be2", "be3/resync",
3645 "set0", "set1", "set2", "set3",
3646 "clr0", "clr1", "clr2", "clr3",
3647 "irq", "res", "eol", "sol",
3651 pr_cont("0x%08x [ %s", risc,
3652 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3653 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3654 if (risc & (1 << (i + 12)))
3655 pr_cont(" %s", bits[i]);
3656 pr_cont(" count=%d ]\n", risc & 0xfff);
3657 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3660 static void bttv_risc_disasm(struct bttv *btv,
3661 struct btcx_riscmem *risc)
3665 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3666 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
3667 for (i = 0; i < (risc->size >> 2); i += n) {
3668 pr_info("%s: 0x%lx: ",
3669 btv->c.v4l2_dev.name,
3670 (unsigned long)(risc->dma + (i<<2)));
3671 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
3672 for (j = 1; j < n; j++)
3673 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3674 btv->c.v4l2_dev.name,
3675 (unsigned long)(risc->dma + ((i+j)<<2)),
3677 if (0 == risc->cpu[i])
3682 static void bttv_print_riscaddr(struct bttv *btv)
3684 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
3685 pr_info(" vbi : o=%08llx e=%08llx\n",
3686 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3687 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3688 pr_info(" cap : o=%08llx e=%08llx\n",
3690 ? (unsigned long long)btv->curr.top->top.dma : 0,
3692 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3693 pr_info(" scr : o=%08llx e=%08llx\n",
3694 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3695 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3696 bttv_risc_disasm(btv, &btv->main);
3699 /* ----------------------------------------------------------------------- */
3702 static char *irq_name[] = {
3703 "FMTCHG", // format change detected (525 vs. 625)
3704 "VSYNC", // vertical sync (new field)
3705 "HSYNC", // horizontal sync
3706 "OFLOW", // chroma/luma AGC overflow
3707 "HLOCK", // horizontal lock changed
3708 "VPRES", // video presence changed
3710 "I2CDONE", // hw irc operation finished
3711 "GPINT", // gpio port triggered irq
3713 "RISCI", // risc instruction triggered irq
3714 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3715 "FTRGT", // pixel data fifo overrun
3716 "FDSR", // fifo data stream resyncronisation
3717 "PPERR", // parity error (data transfer)
3718 "RIPERR", // parity error (read risc instructions)
3719 "PABORT", // pci abort
3720 "OCERR", // risc instruction error
3721 "SCERR", // syncronisation error
3724 static void bttv_print_irqbits(u32 print, u32 mark)
3729 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3730 if (print & (1 << i))
3731 pr_cont(" %s", irq_name[i]);
3732 if (mark & (1 << i))
3737 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3739 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3741 (unsigned long)btv->main.dma,
3742 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
3743 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
3746 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3747 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3748 "Ok, then this is harmless, don't worry ;)\n",
3752 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3754 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3760 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3762 struct bttv_buffer *item;
3764 memset(set,0,sizeof(*set));
3766 /* capture request ? */
3767 if (!list_empty(&btv->capture)) {
3769 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3770 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3772 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3775 /* capture request for other field ? */
3776 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3777 (item->vb.queue.next != &btv->capture)) {
3778 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3779 /* Mike Isely <isely@pobox.com> - Only check
3780 * and set up the bottom field in the logic
3781 * below. Don't ever do the top field. This
3782 * of course means that if we set up the
3783 * bottom field in the above code that we'll
3784 * actually skip a field. But that's OK.
3785 * Having processed only a single buffer this
3786 * time, then the next time around the first
3787 * available buffer should be for a top field.
3788 * That will then cause us here to set up a
3789 * top then a bottom field in the normal way.
3790 * The alternative to this understanding is
3791 * that we set up the second available buffer
3792 * as a top field, but that's out of order
3793 * since this driver always processes the top
3794 * field first - the effect will be the two
3795 * buffers being returned in the wrong order,
3796 * with the second buffer also being delayed
3797 * by one field time (owing to the fifo nature
3798 * of videobuf). Worse still, we'll be stuck
3799 * doing fields out of order now every time
3800 * until something else causes a field to be
3801 * dropped. By effectively forcing a field to
3802 * drop this way then we always get back into
3803 * sync within a single frame time. (Out of
3804 * order fields can screw up deinterlacing
3806 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3807 if (NULL == set->bottom &&
3808 V4L2_FIELD_BOTTOM == item->vb.field) {
3811 if (NULL != set->top && NULL != set->bottom)
3817 /* screen overlay ? */
3818 if (NULL != btv->screen) {
3819 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3820 if (NULL == set->top && NULL == set->bottom) {
3821 set->top = btv->screen;
3822 set->bottom = btv->screen;
3825 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3827 set->top = btv->screen;
3829 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3830 NULL == set->bottom) {
3831 set->bottom = btv->screen;
3836 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3837 btv->c.nr, set->top, set->bottom,
3838 btv->screen, set->frame_irq, set->top_irq);
3843 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3844 struct bttv_buffer_set *curr, unsigned int state)
3848 v4l2_get_timestamp(&ts);
3850 if (wakeup->top == wakeup->bottom) {
3851 if (NULL != wakeup->top && curr->top != wakeup->top) {
3853 pr_debug("%d: wakeup: both=%p\n",
3854 btv->c.nr, wakeup->top);
3855 wakeup->top->vb.ts = ts;
3856 wakeup->top->vb.field_count = btv->field_count;
3857 wakeup->top->vb.state = state;
3858 wake_up(&wakeup->top->vb.done);
3861 if (NULL != wakeup->top && curr->top != wakeup->top) {
3863 pr_debug("%d: wakeup: top=%p\n",
3864 btv->c.nr, wakeup->top);
3865 wakeup->top->vb.ts = ts;
3866 wakeup->top->vb.field_count = btv->field_count;
3867 wakeup->top->vb.state = state;
3868 wake_up(&wakeup->top->vb.done);
3870 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3872 pr_debug("%d: wakeup: bottom=%p\n",
3873 btv->c.nr, wakeup->bottom);
3874 wakeup->bottom->vb.ts = ts;
3875 wakeup->bottom->vb.field_count = btv->field_count;
3876 wakeup->bottom->vb.state = state;
3877 wake_up(&wakeup->bottom->vb.done);
3883 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3891 v4l2_get_timestamp(&ts);
3893 wakeup->vb.field_count = btv->field_count;
3894 wakeup->vb.state = state;
3895 wake_up(&wakeup->vb.done);
3898 static void bttv_irq_timeout(unsigned long data)
3900 struct bttv *btv = (struct bttv *)data;
3901 struct bttv_buffer_set old,new;
3902 struct bttv_buffer *ovbi;
3903 struct bttv_buffer *item;
3904 unsigned long flags;
3907 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3908 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3909 btread(BT848_RISC_COUNT));
3910 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3914 spin_lock_irqsave(&btv->s_lock,flags);
3916 /* deactivate stuff */
3917 memset(&new,0,sizeof(new));
3923 bttv_buffer_activate_video(btv, &new);
3924 bttv_buffer_activate_vbi(btv, NULL);
3925 bttv_set_dma(btv, 0);
3928 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
3929 bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
3931 /* cancel all outstanding capture / vbi requests */
3932 while (!list_empty(&btv->capture)) {
3933 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3934 list_del(&item->vb.queue);
3935 item->vb.state = VIDEOBUF_ERROR;
3936 wake_up(&item->vb.done);
3938 while (!list_empty(&btv->vcapture)) {
3939 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3940 list_del(&item->vb.queue);
3941 item->vb.state = VIDEOBUF_ERROR;
3942 wake_up(&item->vb.done);
3946 spin_unlock_irqrestore(&btv->s_lock,flags);
3950 bttv_irq_wakeup_top(struct bttv *btv)
3952 struct bttv_buffer *wakeup = btv->curr.top;
3957 spin_lock(&btv->s_lock);
3958 btv->curr.top_irq = 0;
3959 btv->curr.top = NULL;
3960 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3962 v4l2_get_timestamp(&wakeup->vb.ts);
3963 wakeup->vb.field_count = btv->field_count;
3964 wakeup->vb.state = VIDEOBUF_DONE;
3965 wake_up(&wakeup->vb.done);
3966 spin_unlock(&btv->s_lock);
3969 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3973 if (rc > risc->dma + risc->size)
3979 bttv_irq_switch_video(struct bttv *btv)
3981 struct bttv_buffer_set new;
3982 struct bttv_buffer_set old;
3985 spin_lock(&btv->s_lock);
3987 /* new buffer set */
3988 bttv_irq_next_video(btv, &new);
3989 rc = btread(BT848_RISC_COUNT);
3990 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3991 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3994 bttv_irq_debug_low_latency(btv, rc);
3995 spin_unlock(&btv->s_lock);
4002 btv->loop_irq &= ~1;
4003 bttv_buffer_activate_video(btv, &new);
4004 bttv_set_dma(btv, 0);
4007 if (UNSET != btv->new_input) {
4008 video_mux(btv,btv->new_input);
4009 btv->new_input = UNSET;
4012 /* wake up finished buffers */
4013 bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
4014 spin_unlock(&btv->s_lock);
4018 bttv_irq_switch_vbi(struct bttv *btv)
4020 struct bttv_buffer *new = NULL;
4021 struct bttv_buffer *old;
4024 spin_lock(&btv->s_lock);
4026 if (!list_empty(&btv->vcapture))
4027 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4030 rc = btread(BT848_RISC_COUNT);
4031 if (NULL != old && (is_active(&old->top, rc) ||
4032 is_active(&old->bottom, rc))) {
4035 bttv_irq_debug_low_latency(btv, rc);
4036 spin_unlock(&btv->s_lock);
4042 btv->loop_irq &= ~4;
4043 bttv_buffer_activate_vbi(btv, new);
4044 bttv_set_dma(btv, 0);
4046 bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
4047 spin_unlock(&btv->s_lock);
4050 static irqreturn_t bttv_irq(int irq, void *dev_id)
4058 btv=(struct bttv *)dev_id;
4062 /* get/clear interrupt status bits */
4063 stat=btread(BT848_INT_STAT);
4064 astat=stat&btread(BT848_INT_MASK);
4068 btwrite(stat,BT848_INT_STAT);
4070 /* get device status bits */
4071 dstat=btread(BT848_DSTATUS);
4074 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
4075 btv->c.nr, count, btv->field_count,
4076 stat>>28, btread(BT848_RISC_COUNT));
4077 bttv_print_irqbits(stat,astat);
4078 if (stat & BT848_INT_HLOCK)
4079 pr_cont(" HLOC => %s",
4080 dstat & BT848_DSTATUS_HLOC
4082 if (stat & BT848_INT_VPRES)
4083 pr_cont(" PRES => %s",
4084 dstat & BT848_DSTATUS_PRES
4086 if (stat & BT848_INT_FMTCHG)
4087 pr_cont(" NUML => %s",
4088 dstat & BT848_DSTATUS_NUML
4093 if (astat&BT848_INT_VSYNC)
4096 if ((astat & BT848_INT_GPINT) && btv->remote) {
4097 bttv_input_irq(btv);
4100 if (astat & BT848_INT_I2CDONE) {
4101 btv->i2c_done = stat;
4102 wake_up(&btv->i2c_queue);
4105 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
4106 bttv_irq_switch_vbi(btv);
4108 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
4109 bttv_irq_wakeup_top(btv);
4111 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
4112 bttv_irq_switch_video(btv);
4114 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
4115 audio_mute(btv, btv->mute); /* trigger automute */
4117 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4118 pr_info("%d: %s%s @ %08x,",
4120 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4121 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4122 btread(BT848_RISC_COUNT));
4123 bttv_print_irqbits(stat,astat);
4126 bttv_print_riscaddr(btv);
4128 if (fdsr && astat & BT848_INT_FDSR) {
4129 pr_info("%d: FDSR @ %08x\n",
4130 btv->c.nr, btread(BT848_RISC_COUNT));
4132 bttv_print_riscaddr(btv);
4138 if (count > 8 || !(astat & BT848_INT_GPINT)) {
4139 btwrite(0, BT848_INT_MASK);
4141 pr_err("%d: IRQ lockup, cleared int mask [",
4144 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
4147 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4151 bttv_print_irqbits(stat,astat);
4159 return IRQ_RETVAL(handled);
4163 /* ----------------------------------------------------------------------- */
4164 /* initialitation */
4166 static struct video_device *vdev_init(struct bttv *btv,
4167 const struct video_device *template,
4168 const char *type_name)
4170 struct video_device *vfd;
4172 vfd = video_device_alloc();
4176 vfd->v4l2_dev = &btv->c.v4l2_dev;
4177 vfd->release = video_device_release;
4178 vfd->debug = bttv_debug;
4179 video_set_drvdata(vfd, btv);
4180 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4181 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4182 type_name, bttv_tvcards[btv->c.type].name);
4186 static void bttv_unregister_video(struct bttv *btv)
4188 if (btv->video_dev) {
4189 if (video_is_registered(btv->video_dev))
4190 video_unregister_device(btv->video_dev);
4192 video_device_release(btv->video_dev);
4193 btv->video_dev = NULL;
4196 if (video_is_registered(btv->vbi_dev))
4197 video_unregister_device(btv->vbi_dev);
4199 video_device_release(btv->vbi_dev);
4200 btv->vbi_dev = NULL;
4202 if (btv->radio_dev) {
4203 if (video_is_registered(btv->radio_dev))
4204 video_unregister_device(btv->radio_dev);
4206 video_device_release(btv->radio_dev);
4207 btv->radio_dev = NULL;
4211 /* register video4linux devices */
4212 static int bttv_register_video(struct bttv *btv)
4215 pr_notice("Overlay support disabled\n");
4218 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4220 if (NULL == btv->video_dev)
4222 if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
4223 video_nr[btv->c.nr]) < 0)
4225 pr_info("%d: registered device %s\n",
4226 btv->c.nr, video_device_node_name(btv->video_dev));
4227 if (device_create_file(&btv->video_dev->dev,
4228 &dev_attr_card)<0) {
4229 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
4234 btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
4236 if (NULL == btv->vbi_dev)
4238 if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
4239 vbi_nr[btv->c.nr]) < 0)
4241 pr_info("%d: registered device %s\n",
4242 btv->c.nr, video_device_node_name(btv->vbi_dev));
4244 if (!btv->has_radio)
4247 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4248 if (NULL == btv->radio_dev)
4250 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
4251 radio_nr[btv->c.nr]) < 0)
4253 pr_info("%d: registered device %s\n",
4254 btv->c.nr, video_device_node_name(btv->radio_dev));
4260 bttv_unregister_video(btv);
4265 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4266 /* response on cards with no firmware is not enabled by OF */
4267 static void pci_set_command(struct pci_dev *dev)
4269 #if defined(__powerpc__)
4272 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4273 cmd = (cmd | PCI_COMMAND_MEMORY );
4274 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4278 static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
4284 if (bttv_num == BTTV_MAX)
4286 pr_info("Bt8xx card found (%d)\n", bttv_num);
4287 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
4289 pr_err("out of memory\n");
4292 btv->c.nr = bttv_num;
4293 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
4294 "bttv%d", btv->c.nr);
4296 /* initialize structs / fill in defaults */
4297 mutex_init(&btv->lock);
4298 spin_lock_init(&btv->s_lock);
4299 spin_lock_init(&btv->gpio_lock);
4300 init_waitqueue_head(&btv->i2c_queue);
4301 INIT_LIST_HEAD(&btv->c.subs);
4302 INIT_LIST_HEAD(&btv->capture);
4303 INIT_LIST_HEAD(&btv->vcapture);
4304 v4l2_prio_init(&btv->prio);
4306 init_timer(&btv->timeout);
4307 btv->timeout.function = bttv_irq_timeout;
4308 btv->timeout.data = (unsigned long)btv;
4311 btv->tuner_type = UNSET;
4312 btv->new_input = UNSET;
4313 btv->has_radio=radio[btv->c.nr];
4315 /* pci stuff (init, get irq/mmio, ... */
4317 btv->id = dev->device;
4318 if (pci_enable_device(dev)) {
4319 pr_warn("%d: Can't enable device\n", btv->c.nr);
4322 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
4323 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
4326 if (!request_mem_region(pci_resource_start(dev,0),
4327 pci_resource_len(dev,0),
4328 btv->c.v4l2_dev.name)) {
4329 pr_warn("%d: can't request iomem (0x%llx)\n",
4331 (unsigned long long)pci_resource_start(dev, 0));
4334 pci_set_master(dev);
4335 pci_set_command(dev);
4337 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
4339 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
4343 btv->revision = dev->revision;
4344 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4345 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4346 bttv_num, btv->id, btv->revision, pci_name(dev),
4347 btv->c.pci->irq, lat,
4348 (unsigned long long)pci_resource_start(dev, 0));
4351 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4352 if (NULL == btv->bt848_mmio) {
4353 pr_err("%d: ioremap() failed\n", btv->c.nr);
4361 /* disable irqs, register irq handler */
4362 btwrite(0, BT848_INT_MASK);
4363 result = request_irq(btv->c.pci->irq, bttv_irq,
4364 IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
4366 pr_err("%d: can't get IRQ %d\n",
4367 bttv_num, btv->c.pci->irq);
4371 if (0 != bttv_handle_chipset(btv)) {
4376 /* init options from insmod args */
4377 btv->opt_combfilter = combfilter;
4378 btv->opt_lumafilter = lumafilter;
4379 btv->opt_automute = automute;
4380 btv->opt_chroma_agc = chroma_agc;
4381 btv->opt_adc_crush = adc_crush;
4382 btv->opt_vcr_hack = vcr_hack;
4383 btv->opt_whitecrush_upper = whitecrush_upper;
4384 btv->opt_whitecrush_lower = whitecrush_lower;
4385 btv->opt_uv_ratio = uv_ratio;
4386 btv->opt_full_luma_range = full_luma_range;
4387 btv->opt_coring = coring;
4389 /* fill struct bttv with some useful defaults */
4390 btv->init.btv = btv;
4391 btv->init.ov.w.width = 320;
4392 btv->init.ov.w.height = 240;
4393 btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
4394 btv->init.width = 320;
4395 btv->init.height = 240;
4398 /* initialize hardware */
4400 bttv_gpio_tracking(btv,"pre-init");
4402 bttv_risc_init_main(btv);
4406 btwrite(0x00, BT848_GPIO_REG_INP);
4407 btwrite(0x00, BT848_GPIO_OUT_EN);
4409 bttv_gpio_tracking(btv,"init");
4411 /* needs to be done before i2c is registered */
4412 bttv_init_card1(btv);
4414 /* register i2c + gpio */
4417 /* some card-specific stuff (needs working i2c) */
4418 bttv_init_card2(btv);
4419 bttv_init_tuner(btv);
4422 /* register video4linux + input */
4423 if (!bttv_tvcards[btv->c.type].no_video) {
4424 bttv_register_video(btv);
4425 bt848_bright(btv,32768);
4426 bt848_contrast(btv, 27648);
4427 bt848_hue(btv,32768);
4428 bt848_sat(btv,32768);
4430 set_input(btv, 0, btv->tvnorm);
4431 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4432 btv->crop[1] = btv->crop[0]; /* current = default */
4433 disclaim_vbi_lines(btv);
4434 disclaim_video_lines(btv);
4437 /* add subdevices and autoload dvb-bt8xx if needed */
4438 if (bttv_tvcards[btv->c.type].has_dvb) {
4439 bttv_sub_add_device(&btv->c, "dvb");
4440 request_modules(btv);
4444 init_bttv_i2c_ir(btv);
4445 bttv_input_init(btv);
4448 /* everything is fine */
4453 free_irq(btv->c.pci->irq,btv);
4456 v4l2_device_unregister(&btv->c.v4l2_dev);
4459 if (btv->bt848_mmio)
4460 iounmap(btv->bt848_mmio);
4461 release_mem_region(pci_resource_start(btv->c.pci,0),
4462 pci_resource_len(btv->c.pci,0));
4466 static void bttv_remove(struct pci_dev *pci_dev)
4468 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4469 struct bttv *btv = to_bttv(v4l2_dev);
4472 pr_info("%d: unloading\n", btv->c.nr);
4474 if (bttv_tvcards[btv->c.type].has_dvb)
4475 flush_request_modules(btv);
4477 /* shutdown everything (DMA+IRQs) */
4478 btand(~15, BT848_GPIO_DMA_CTL);
4479 btwrite(0, BT848_INT_MASK);
4480 btwrite(~0x0, BT848_INT_STAT);
4481 btwrite(0x0, BT848_GPIO_OUT_EN);
4483 bttv_gpio_tracking(btv,"cleanup");
4485 /* tell gpio modules we are leaving ... */
4487 bttv_input_fini(btv);
4488 bttv_sub_del_devices(&btv->c);
4490 /* unregister i2c_bus + input */
4493 /* unregister video4linux */
4494 bttv_unregister_video(btv);
4496 /* free allocated memory */
4497 btcx_riscmem_free(btv->c.pci,&btv->main);
4499 /* free ressources */
4500 free_irq(btv->c.pci->irq,btv);
4501 iounmap(btv->bt848_mmio);
4502 release_mem_region(pci_resource_start(btv->c.pci,0),
4503 pci_resource_len(btv->c.pci,0));
4505 v4l2_device_unregister(&btv->c.v4l2_dev);
4506 bttvs[btv->c.nr] = NULL;
4513 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4515 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4516 struct bttv *btv = to_bttv(v4l2_dev);
4517 struct bttv_buffer_set idle;
4518 unsigned long flags;
4520 dprintk("%d: suspend %d\n", btv->c.nr, state.event);
4522 /* stop dma + irqs */
4523 spin_lock_irqsave(&btv->s_lock,flags);
4524 memset(&idle, 0, sizeof(idle));
4525 btv->state.video = btv->curr;
4526 btv->state.vbi = btv->cvbi;
4527 btv->state.loop_irq = btv->loop_irq;
4530 bttv_buffer_activate_video(btv, &idle);
4531 bttv_buffer_activate_vbi(btv, NULL);
4532 bttv_set_dma(btv, 0);
4533 btwrite(0, BT848_INT_MASK);
4534 spin_unlock_irqrestore(&btv->s_lock,flags);
4536 /* save bt878 state */
4537 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4538 btv->state.gpio_data = gpio_read();
4540 /* save pci state */
4541 pci_save_state(pci_dev);
4542 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4543 pci_disable_device(pci_dev);
4544 btv->state.disabled = 1;
4549 static int bttv_resume(struct pci_dev *pci_dev)
4551 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
4552 struct bttv *btv = to_bttv(v4l2_dev);
4553 unsigned long flags;
4556 dprintk("%d: resume\n", btv->c.nr);
4558 /* restore pci state */
4559 if (btv->state.disabled) {
4560 err=pci_enable_device(pci_dev);
4562 pr_warn("%d: Can't enable device\n", btv->c.nr);
4565 btv->state.disabled = 0;
4567 err=pci_set_power_state(pci_dev, PCI_D0);
4569 pci_disable_device(pci_dev);
4570 pr_warn("%d: Can't enable device\n", btv->c.nr);
4571 btv->state.disabled = 1;
4575 pci_restore_state(pci_dev);
4577 /* restore bt878 state */
4578 bttv_reinit_bt848(btv);
4579 gpio_inout(0xffffff, btv->state.gpio_enable);
4580 gpio_write(btv->state.gpio_data);
4583 spin_lock_irqsave(&btv->s_lock,flags);
4584 btv->curr = btv->state.video;
4585 btv->cvbi = btv->state.vbi;
4586 btv->loop_irq = btv->state.loop_irq;
4587 bttv_buffer_activate_video(btv, &btv->curr);
4588 bttv_buffer_activate_vbi(btv, btv->cvbi);
4589 bttv_set_dma(btv, 0);
4590 spin_unlock_irqrestore(&btv->s_lock,flags);
4595 static struct pci_device_id bttv_pci_tbl[] = {
4596 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
4597 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
4598 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
4599 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
4600 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
4604 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4606 static struct pci_driver bttv_pci_driver = {
4608 .id_table = bttv_pci_tbl,
4609 .probe = bttv_probe,
4610 .remove = bttv_remove,
4612 .suspend = bttv_suspend,
4613 .resume = bttv_resume,
4617 static int __init bttv_init_module(void)
4623 pr_info("driver version %s loaded\n", BTTV_VERSION);
4624 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4626 if (gbufsize > BTTV_MAX_FBUF)
4627 gbufsize = BTTV_MAX_FBUF;
4628 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4630 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4631 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4633 bttv_check_chipset();
4635 ret = bus_register(&bttv_sub_bus_type);
4637 pr_warn("bus_register error: %d\n", ret);
4640 ret = pci_register_driver(&bttv_pci_driver);
4642 bus_unregister(&bttv_sub_bus_type);
4647 static void __exit bttv_cleanup_module(void)
4649 pci_unregister_driver(&bttv_pci_driver);
4650 bus_unregister(&bttv_sub_bus_type);
4653 module_init(bttv_init_module);
4654 module_exit(bttv_cleanup_module);