1 // SPDX-License-Identifier: GPL-2.0-or-later
4 bttv - Bt848 frame grabber driver
6 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
7 & Marcus Metzler <mocm@thp.uni-koeln.de>
8 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
10 some v4l2 code lines are taken from Justin's bttv2 driver which is
11 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
14 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
16 Fixes to be fully V4L2 compliant by
17 (c) 2006 Mauro Carvalho Chehab <mchehab@kernel.org>
19 Cropping and overscan support
20 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
21 Sponsored by OPQ Systems AB
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-event.h>
41 #include <media/i2c/tvaudio.h>
42 #include <media/drv-intf/msp3400.h>
44 #include <linux/dma-mapping.h>
47 #include <asm/byteorder.h>
49 #include <media/i2c/saa6588.h>
51 #define BTTV_VERSION "0.9.19"
53 unsigned int bttv_num; /* number of Bt848s in use */
54 struct bttv *bttvs[BTTV_MAX];
56 unsigned int bttv_debug;
57 unsigned int bttv_verbose = 1;
58 unsigned int bttv_gpio;
60 /* config variables */
62 static unsigned int bigendian=1;
64 static unsigned int bigendian;
66 static unsigned int radio[BTTV_MAX];
67 static unsigned int irq_debug;
68 static unsigned int gbuffers = 8;
69 static unsigned int gbufsize = 0x208000;
70 static unsigned int reset_crop = 1;
72 static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
73 static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
74 static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
75 static int debug_latency;
76 static int disable_ir;
78 static unsigned int fdsr;
81 static unsigned int combfilter;
82 static unsigned int lumafilter;
83 static unsigned int automute = 1;
84 static unsigned int chroma_agc;
85 static unsigned int agc_crush = 1;
86 static unsigned int whitecrush_upper = 0xCF;
87 static unsigned int whitecrush_lower = 0x7F;
88 static unsigned int vcr_hack;
89 static unsigned int irq_iswitch;
90 static unsigned int uv_ratio = 50;
91 static unsigned int full_luma_range;
92 static unsigned int coring;
94 /* API features (turn on/off stuff for testing) */
95 static unsigned int v4l2 = 1;
98 module_param(bttv_verbose, int, 0644);
99 module_param(bttv_gpio, int, 0644);
100 module_param(bttv_debug, int, 0644);
101 module_param(irq_debug, int, 0644);
102 module_param(debug_latency, int, 0644);
103 module_param(disable_ir, int, 0444);
105 module_param(fdsr, int, 0444);
106 module_param(gbuffers, int, 0444);
107 module_param(gbufsize, int, 0444);
108 module_param(reset_crop, int, 0444);
110 module_param(v4l2, int, 0644);
111 module_param(bigendian, int, 0644);
112 module_param(irq_iswitch, int, 0644);
113 module_param(combfilter, int, 0444);
114 module_param(lumafilter, int, 0444);
115 module_param(automute, int, 0444);
116 module_param(chroma_agc, int, 0444);
117 module_param(agc_crush, int, 0444);
118 module_param(whitecrush_upper, int, 0444);
119 module_param(whitecrush_lower, int, 0444);
120 module_param(vcr_hack, int, 0444);
121 module_param(uv_ratio, int, 0444);
122 module_param(full_luma_range, int, 0444);
123 module_param(coring, int, 0444);
125 module_param_array(radio, int, NULL, 0444);
126 module_param_array(video_nr, int, NULL, 0444);
127 module_param_array(radio_nr, int, NULL, 0444);
128 module_param_array(vbi_nr, int, NULL, 0444);
130 MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
131 MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
132 MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
133 MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
134 MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
135 MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
136 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
137 MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
138 MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
139 MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default is 1 (yes) for compatibility with older applications");
140 MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
141 MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
142 MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
143 MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
144 MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
145 MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
146 MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
147 MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
148 MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
149 MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
150 MODULE_PARM_DESC(video_nr, "video device numbers");
151 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
152 MODULE_PARM_DESC(radio_nr, "radio device numbers");
154 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
155 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
156 MODULE_LICENSE("GPL");
157 MODULE_VERSION(BTTV_VERSION);
159 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
160 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
161 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
162 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
163 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
164 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
165 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
166 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
167 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
168 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
170 /* ----------------------------------------------------------------------- */
173 static ssize_t card_show(struct device *cd,
174 struct device_attribute *attr, char *buf)
176 struct video_device *vfd = to_video_device(cd);
177 struct bttv *btv = video_get_drvdata(vfd);
178 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
180 static DEVICE_ATTR_RO(card);
182 /* ----------------------------------------------------------------------- */
183 /* dvb auto-load setup */
184 #if defined(CONFIG_MODULES) && defined(MODULE)
185 static void request_module_async(struct work_struct *work)
187 request_module("dvb-bt8xx");
190 static void request_modules(struct bttv *dev)
192 INIT_WORK(&dev->request_module_wk, request_module_async);
193 schedule_work(&dev->request_module_wk);
196 static void flush_request_modules(struct bttv *dev)
198 flush_work(&dev->request_module_wk);
201 #define request_modules(dev)
202 #define flush_request_modules(dev) do {} while(0)
203 #endif /* CONFIG_MODULES */
206 /* ----------------------------------------------------------------------- */
209 /* special timing tables from conexant... */
210 static u8 SRAM_Table[][60] =
212 /* PAL digital input over GPIO[7:0] */
214 45, // 45 bytes following
215 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
216 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
217 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
218 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
219 0x37,0x00,0xAF,0x21,0x00
221 /* NTSC digital input over GPIO[7:0] */
223 51, // 51 bytes following
224 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
225 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
226 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
227 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
228 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
231 // TGB_NTSC392 // quartzsight
232 // This table has been modified to be used for Fusion Rev D
234 0x2A, // size of table = 42
235 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
236 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
237 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
238 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
243 /* minhdelayx1 first video pixel we can capture on a line and
244 hdelayx1 start of active video, both relative to rising edge of
245 /HRESET pulse (0H) in 1 / fCLKx1.
246 swidth width of active video and
247 totalwidth total line width, both in 1 / fCLKx1.
248 sqwidth total line width in square pixels.
249 vdelay start of active video in 2 * field lines relative to
250 trailing edge of /VRESET pulse (VDELAY register).
251 sheight height of active video in 2 * field lines.
252 extraheight Added to sheight for cropcap.bounds.height only
253 videostart0 ITU-R frame line number of the line corresponding
254 to vdelay in the first field. */
255 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
256 vdelay, sheight, extraheight, videostart0) \
257 .cropcap.bounds.left = minhdelayx1, \
258 /* * 2 because vertically we count field lines times two, */ \
259 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
260 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
261 /* 4 is a safety margin at the end of the line. */ \
262 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
263 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
265 .cropcap.defrect.left = hdelayx1, \
266 .cropcap.defrect.top = (videostart0) * 2, \
267 .cropcap.defrect.width = swidth, \
268 .cropcap.defrect.height = sheight, \
269 .cropcap.pixelaspect.numerator = totalwidth, \
270 .cropcap.pixelaspect.denominator = sqwidth,
272 const struct bttv_tvnorm bttv_tvnorms[] = {
274 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
275 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
277 .v4l2_id = V4L2_STD_PAL,
285 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
286 .scaledtwidth = 1135,
290 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
292 /* ITU-R frame line number of the first VBI line
293 we can capture, of the first and second field.
294 The last line is determined by cropcap.bounds. */
295 .vbistart = { 7, 320 },
296 CROPCAP(/* minhdelayx1 */ 68,
298 /* Should be (768 * 1135 + 944 / 2) / 944.
299 cropcap.defrect is used for image width
300 checks, so we keep the old value 924. */
302 /* totalwidth */ 1135,
306 /* bt878 (and bt848?) can capture another
307 line below active video. */
309 /* videostart0 */ 23)
311 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
319 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
324 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
326 .vbistart = { 10, 273 },
327 CROPCAP(/* minhdelayx1 */ 68,
329 /* Should be (640 * 910 + 780 / 2) / 780? */
331 /* totalwidth */ 910,
336 /* videostart0 */ 23)
338 .v4l2_id = V4L2_STD_SECAM,
346 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
347 .scaledtwidth = 1135,
352 .sram = 0, /* like PAL, correct? */
353 .vbistart = { 7, 320 },
354 CROPCAP(/* minhdelayx1 */ 68,
357 /* totalwidth */ 1135,
362 /* videostart0 */ 23)
364 .v4l2_id = V4L2_STD_PAL_Nc,
372 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
379 .vbistart = { 7, 320 },
380 CROPCAP(/* minhdelayx1 */ 68,
382 /* swidth */ (640 * 910 + 780 / 2) / 780,
383 /* totalwidth */ 910,
388 /* videostart0 */ 23)
390 .v4l2_id = V4L2_STD_PAL_M,
398 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
405 .vbistart = { 10, 273 },
406 CROPCAP(/* minhdelayx1 */ 68,
408 /* swidth */ (640 * 910 + 780 / 2) / 780,
409 /* totalwidth */ 910,
414 /* videostart0 */ 23)
416 .v4l2_id = V4L2_STD_PAL_N,
424 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
431 .vbistart = { 7, 320 },
432 CROPCAP(/* minhdelayx1 */ 68,
434 /* swidth */ (768 * 1135 + 944 / 2) / 944,
435 /* totalwidth */ 1135,
440 /* videostart0 */ 23)
442 .v4l2_id = V4L2_STD_NTSC_M_JP,
450 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
457 .vbistart = { 10, 273 },
458 CROPCAP(/* minhdelayx1 */ 68,
460 /* swidth */ (640 * 910 + 780 / 2) / 780,
461 /* totalwidth */ 910,
466 /* videostart0 */ 23)
468 /* that one hopefully works with the strange timing
469 * which video recorders produce when playing a NTSC
470 * tape on a PAL TV ... */
471 .v4l2_id = V4L2_STD_PAL_60,
479 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
480 .scaledtwidth = 1135,
487 .vbistart = { 10, 273 },
488 CROPCAP(/* minhdelayx1 */ 68,
491 /* totalwidth */ 1135,
496 /* videostart0 */ 23)
499 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
501 /* ----------------------------------------------------------------------- */
503 packed pixel formats must come first */
504 static const struct bttv_format formats[] = {
506 .fourcc = V4L2_PIX_FMT_GREY,
507 .btformat = BT848_COLOR_FMT_Y8,
509 .flags = FORMAT_FLAGS_PACKED,
511 .fourcc = V4L2_PIX_FMT_HI240,
512 .btformat = BT848_COLOR_FMT_RGB8,
514 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
516 .fourcc = V4L2_PIX_FMT_RGB555,
517 .btformat = BT848_COLOR_FMT_RGB15,
519 .flags = FORMAT_FLAGS_PACKED,
521 .fourcc = V4L2_PIX_FMT_RGB555X,
522 .btformat = BT848_COLOR_FMT_RGB15,
523 .btswap = 0x03, /* byteswap */
525 .flags = FORMAT_FLAGS_PACKED,
527 .fourcc = V4L2_PIX_FMT_RGB565,
528 .btformat = BT848_COLOR_FMT_RGB16,
530 .flags = FORMAT_FLAGS_PACKED,
532 .fourcc = V4L2_PIX_FMT_RGB565X,
533 .btformat = BT848_COLOR_FMT_RGB16,
534 .btswap = 0x03, /* byteswap */
536 .flags = FORMAT_FLAGS_PACKED,
538 .fourcc = V4L2_PIX_FMT_BGR24,
539 .btformat = BT848_COLOR_FMT_RGB24,
541 .flags = FORMAT_FLAGS_PACKED,
543 .fourcc = V4L2_PIX_FMT_BGR32,
544 .btformat = BT848_COLOR_FMT_RGB32,
546 .flags = FORMAT_FLAGS_PACKED,
548 .fourcc = V4L2_PIX_FMT_RGB32,
549 .btformat = BT848_COLOR_FMT_RGB32,
550 .btswap = 0x0f, /* byte+word swap */
552 .flags = FORMAT_FLAGS_PACKED,
554 .fourcc = V4L2_PIX_FMT_YUYV,
555 .btformat = BT848_COLOR_FMT_YUY2,
557 .flags = FORMAT_FLAGS_PACKED,
559 .fourcc = V4L2_PIX_FMT_UYVY,
560 .btformat = BT848_COLOR_FMT_YUY2,
561 .btswap = 0x03, /* byteswap */
563 .flags = FORMAT_FLAGS_PACKED,
565 .fourcc = V4L2_PIX_FMT_YUV422P,
566 .btformat = BT848_COLOR_FMT_YCrCb422,
568 .flags = FORMAT_FLAGS_PLANAR,
572 .fourcc = V4L2_PIX_FMT_YUV420,
573 .btformat = BT848_COLOR_FMT_YCrCb422,
575 .flags = FORMAT_FLAGS_PLANAR,
579 .fourcc = V4L2_PIX_FMT_YVU420,
580 .btformat = BT848_COLOR_FMT_YCrCb422,
582 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
586 .fourcc = V4L2_PIX_FMT_YUV411P,
587 .btformat = BT848_COLOR_FMT_YCrCb411,
589 .flags = FORMAT_FLAGS_PLANAR,
593 .fourcc = V4L2_PIX_FMT_YUV410,
594 .btformat = BT848_COLOR_FMT_YCrCb411,
596 .flags = FORMAT_FLAGS_PLANAR,
600 .fourcc = V4L2_PIX_FMT_YVU410,
601 .btformat = BT848_COLOR_FMT_YCrCb411,
603 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
608 .btformat = BT848_COLOR_FMT_RAW,
610 .flags = FORMAT_FLAGS_RAW,
613 static const unsigned int FORMATS = ARRAY_SIZE(formats);
615 /* ----------------------------------------------------------------------- */
616 /* resource management */
619 RESOURCE_ allocated by freed by
621 VIDEO_READ bttv_read 1) bttv_read 2)
623 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
624 VIDIOC_QBUF 1) bttv_release
627 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
628 VIDIOC_QBUF 1) bttv_release
629 bttv_read, bttv_poll 1) 3)
631 1) The resource must be allocated when we enter buffer prepare functions
632 and remain allocated while buffers are in the DMA queue.
633 2) This is a single frame read.
634 3) This is a continuous read, implies VIDIOC_STREAMON.
636 Note this driver permits video input and standard changes regardless if
637 resources are allocated.
640 #define VBI_RESOURCES (RESOURCE_VBI)
641 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
642 RESOURCE_VIDEO_STREAM)
644 int check_alloc_btres_lock(struct bttv *btv, int bit)
646 int xbits; /* mutual exclusive resources */
649 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
650 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
653 if (btv->resources & xbits) {
654 /* no, someone else uses it */
658 if ((bit & VIDEO_RESOURCES)
659 && 0 == (btv->resources & VIDEO_RESOURCES)) {
660 /* Do crop - use current, don't - use default parameters. */
661 __s32 top = btv->crop[!!btv->do_crop].rect.top;
663 if (btv->vbi_end > top)
666 /* We cannot capture the same line as video and VBI data.
667 Claim scan lines crop[].rect.top to bottom. */
668 btv->crop_start = top;
669 } else if (bit & VBI_RESOURCES) {
670 __s32 end = btv->vbi_fmt.end;
672 if (end > btv->crop_start)
675 /* Claim scan lines above btv->vbi_fmt.end. */
679 /* it's free, grab it */
680 btv->resources |= bit;
688 int check_btres(struct bttv *btv, int bit)
690 return (btv->resources & bit);
694 int locked_btres(struct bttv *btv, int bit)
696 return (btv->resources & bit);
699 /* Call with btv->lock down. */
701 disclaim_vbi_lines(struct bttv *btv)
706 /* Call with btv->lock down. */
708 disclaim_video_lines(struct bttv *btv)
710 const struct bttv_tvnorm *tvnorm;
713 tvnorm = &bttv_tvnorms[btv->tvnorm];
714 btv->crop_start = tvnorm->cropcap.bounds.top
715 + tvnorm->cropcap.bounds.height;
717 /* VBI capturing ends at VDELAY, start of video capturing, no
718 matter how many lines the VBI RISC program expects. When video
719 capturing is off, it shall no longer "preempt" VBI capturing,
720 so we set VDELAY to maximum. */
721 crop = btread(BT848_E_CROP) | 0xc0;
722 btwrite(crop, BT848_E_CROP);
723 btwrite(0xfe, BT848_E_VDELAY_LO);
724 btwrite(crop, BT848_O_CROP);
725 btwrite(0xfe, BT848_O_VDELAY_LO);
728 void free_btres_lock(struct bttv *btv, int bits)
730 if ((btv->resources & bits) != bits) {
731 /* trying to free resources not allocated by us ... */
732 pr_err("BUG! (btres)\n");
734 btv->resources &= ~bits;
736 bits = btv->resources;
738 if (0 == (bits & VIDEO_RESOURCES))
739 disclaim_video_lines(btv);
741 if (0 == (bits & VBI_RESOURCES))
742 disclaim_vbi_lines(btv);
745 /* ----------------------------------------------------------------------- */
746 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
748 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
749 PLL_X = Reference pre-divider (0=1, 1=2)
750 PLL_C = Post divider (0=6, 1=4)
751 PLL_I = Integer input
752 PLL_F = Fractional input
754 F_input = 28.636363 MHz:
755 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
758 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
760 unsigned char fl, fh, fi;
762 /* prevent overflows */
775 btwrite(fl, BT848_PLL_F_LO);
776 btwrite(fh, BT848_PLL_F_HI);
777 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
780 static void set_pll(struct bttv *btv)
784 if (!btv->pll.pll_crystal)
787 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
788 dprintk("%d: PLL: no change required\n", btv->c.nr);
792 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
794 if (btv->pll.pll_current == 0)
797 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
798 btv->c.nr, btv->pll.pll_ifreq);
799 btwrite(0x00,BT848_TGCTRL);
800 btwrite(0x00,BT848_PLL_XCI);
801 btv->pll.pll_current = 0;
806 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
808 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
809 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
811 for (i=0; i<10; i++) {
812 /* Let other people run while the PLL stabilizes */
815 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
816 btwrite(0,BT848_DSTATUS);
818 btwrite(0x08,BT848_TGCTRL);
819 btv->pll.pll_current = btv->pll.pll_ofreq;
821 pr_info("PLL set ok\n");
825 btv->pll.pll_current = -1;
827 pr_info("Setting PLL failed\n");
831 /* used to switch between the bt848's analog/digital video capture modes */
832 static void bt848A_set_timing(struct bttv *btv)
835 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
836 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
838 if (btv->input == btv->dig) {
839 dprintk("%d: load digital timing table (table_idx=%d)\n",
840 btv->c.nr,table_idx);
842 /* timing change...reset timing generator address */
843 btwrite(0x00, BT848_TGCTRL);
844 btwrite(0x02, BT848_TGCTRL);
845 btwrite(0x00, BT848_TGCTRL);
847 len=SRAM_Table[table_idx][0];
848 for(i = 1; i <= len; i++)
849 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
850 btv->pll.pll_ofreq = 27000000;
853 btwrite(0x11, BT848_TGCTRL);
854 btwrite(0x41, BT848_DVSIF);
856 btv->pll.pll_ofreq = fsc;
858 btwrite(0x0, BT848_DVSIF);
862 /* ----------------------------------------------------------------------- */
864 static void bt848_bright(struct bttv *btv, int bright)
868 // printk("set bright: %d\n", bright); // DEBUG
869 btv->bright = bright;
871 /* We want -128 to 127 we get 0-65535 */
872 value = (bright >> 8) - 128;
873 btwrite(value & 0xff, BT848_BRIGHT);
876 static void bt848_hue(struct bttv *btv, int hue)
883 value = (hue >> 8) - 128;
884 btwrite(value & 0xff, BT848_HUE);
887 static void bt848_contrast(struct bttv *btv, int cont)
891 btv->contrast = cont;
895 hibit = (value >> 6) & 4;
896 btwrite(value & 0xff, BT848_CONTRAST_LO);
897 btaor(hibit, ~4, BT848_E_CONTROL);
898 btaor(hibit, ~4, BT848_O_CONTROL);
901 static void bt848_sat(struct bttv *btv, int color)
903 int val_u,val_v,hibits;
905 btv->saturation = color;
907 /* 0-511 for the color */
908 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
909 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
910 hibits = (val_u >> 7) & 2;
911 hibits |= (val_v >> 8) & 1;
912 btwrite(val_u & 0xff, BT848_SAT_U_LO);
913 btwrite(val_v & 0xff, BT848_SAT_V_LO);
914 btaor(hibits, ~3, BT848_E_CONTROL);
915 btaor(hibits, ~3, BT848_O_CONTROL);
918 /* ----------------------------------------------------------------------- */
921 video_mux(struct bttv *btv, unsigned int input)
925 if (input >= bttv_tvcards[btv->c.type].video_inputs)
928 /* needed by RemoteVideo MX */
929 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
931 gpio_inout(mask2,mask2);
933 if (input == btv->svhs) {
934 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
935 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
937 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
938 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
940 mux = bttv_muxsel(btv, input);
941 btaor(mux<<5, ~(3<<5), BT848_IFORM);
942 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
944 /* card specific hook */
945 if(bttv_tvcards[btv->c.type].muxsel_hook)
946 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
950 static char *audio_modes[] = {
951 "audio: tuner", "audio: radio", "audio: extern",
952 "audio: intern", "audio: mute"
956 audio_mux_gpio(struct bttv *btv, int input, int mute)
958 int gpio_val, signal, mute_gpio;
960 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
961 bttv_tvcards[btv->c.type].gpiomask);
962 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
965 mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
966 && !btv->has_radio_tuner);
969 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
971 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
973 switch (btv->c.type) {
974 case BTTV_BOARD_VOODOOTV_FM:
975 case BTTV_BOARD_VOODOOTV_200:
976 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
980 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
984 bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
988 audio_mute(struct bttv *btv, int mute)
990 struct v4l2_ctrl *ctrl;
992 audio_mux_gpio(btv, btv->audio_input, mute);
994 if (btv->sd_msp34xx) {
995 ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
997 v4l2_ctrl_s_ctrl(ctrl, mute);
999 if (btv->sd_tvaudio) {
1000 ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1002 v4l2_ctrl_s_ctrl(ctrl, mute);
1004 if (btv->sd_tda7432) {
1005 ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1007 v4l2_ctrl_s_ctrl(ctrl, mute);
1013 audio_input(struct bttv *btv, int input)
1015 audio_mux_gpio(btv, input, btv->mute);
1017 if (btv->sd_msp34xx) {
1020 /* Note: the inputs tuner/radio/extern/intern are translated
1021 to msp routings. This assumes common behavior for all msp3400
1022 based TV cards. When this assumption fails, then the
1023 specific MSP routing must be added to the card table.
1024 For now this is sufficient. */
1026 case TVAUDIO_INPUT_RADIO:
1027 /* Some boards need the msp do to the radio demod */
1028 if (btv->radio_uses_msp_demodulator) {
1029 in = MSP_INPUT_DEFAULT;
1032 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1033 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1035 case TVAUDIO_INPUT_EXTERN:
1036 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1037 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1039 case TVAUDIO_INPUT_INTERN:
1040 /* Yes, this is the same input as for RADIO. I doubt
1041 if this is ever used. The only board with an INTERN
1042 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1043 that was tested. My guess is that the whole INTERN
1044 input does not work. */
1045 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1046 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1048 case TVAUDIO_INPUT_TUNER:
1050 /* This is the only card that uses TUNER2, and afaik,
1051 is the only difference between the VOODOOTV_FM
1053 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1054 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1055 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1057 in = MSP_INPUT_DEFAULT;
1060 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1061 in, MSP_OUTPUT_DEFAULT, 0);
1063 if (btv->sd_tvaudio) {
1064 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1071 bttv_crop_calc_limits(struct bttv_crop *c)
1073 /* Scale factor min. 1:1, max. 16:1. Min. image size
1074 48 x 32. Scaled width must be a multiple of 4. */
1077 /* For bug compatibility with VIDIOCGCAP and image
1078 size checks in earlier driver versions. */
1079 c->min_scaled_width = 48;
1080 c->min_scaled_height = 32;
1082 c->min_scaled_width =
1083 (max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
1084 c->min_scaled_height =
1085 max_t(unsigned int, 32, c->rect.height >> 4);
1088 c->max_scaled_width = c->rect.width & ~3;
1089 c->max_scaled_height = c->rect.height;
1093 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1095 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1096 bttv_crop_calc_limits(c);
1099 /* Call with btv->lock down. */
1101 set_tvnorm(struct bttv *btv, unsigned int norm)
1103 const struct bttv_tvnorm *tvnorm;
1106 WARN_ON(norm >= BTTV_TVNORMS);
1107 WARN_ON(btv->tvnorm >= BTTV_TVNORMS);
1109 tvnorm = &bttv_tvnorms[norm];
1111 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1112 sizeof (tvnorm->cropcap))) {
1113 bttv_crop_reset(&btv->crop[0], norm);
1114 btv->crop[1] = btv->crop[0]; /* current = default */
1116 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1117 btv->crop_start = tvnorm->cropcap.bounds.top
1118 + tvnorm->cropcap.bounds.height;
1124 btwrite(tvnorm->adelay, BT848_ADELAY);
1125 btwrite(tvnorm->bdelay, BT848_BDELAY);
1126 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1128 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1129 btwrite(1, BT848_VBI_PACK_DEL);
1130 bt848A_set_timing(btv);
1132 switch (btv->c.type) {
1133 case BTTV_BOARD_VOODOOTV_FM:
1134 case BTTV_BOARD_VOODOOTV_200:
1135 bttv_tda9880_setnorm(btv, gpio_read());
1138 id = tvnorm->v4l2_id;
1139 bttv_call_all(btv, video, s_std, id);
1144 /* Call with btv->lock down. */
1146 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1148 unsigned long flags;
1152 spin_lock_irqsave(&btv->s_lock,flags);
1153 if (btv->curr.frame_irq) {
1154 /* active capture -> delayed input switch */
1155 btv->new_input = input;
1157 video_mux(btv,input);
1159 spin_unlock_irqrestore(&btv->s_lock,flags);
1161 video_mux(btv,input);
1163 btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1164 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
1165 audio_input(btv, btv->audio_input);
1166 set_tvnorm(btv, norm);
1169 void init_irqreg(struct bttv *btv)
1172 btwrite(0xfffffUL, BT848_INT_STAT);
1174 if (bttv_tvcards[btv->c.type].no_video) {
1176 btwrite(BT848_INT_I2CDONE,
1180 btwrite((btv->triton1) |
1181 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1183 (fdsr ? BT848_INT_FDSR : 0) |
1184 BT848_INT_RISCI | BT848_INT_OCERR |
1185 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1191 static void init_bt848(struct bttv *btv)
1193 if (bttv_tvcards[btv->c.type].no_video) {
1194 /* very basic init only */
1199 btwrite(0x00, BT848_CAP_CTL);
1200 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1201 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1203 /* set planar and packed mode trigger points and */
1204 /* set rising edge of inverted GPINTR pin as irq trigger */
1205 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1206 BT848_GPIO_DMA_CTL_PLTP1_16|
1207 BT848_GPIO_DMA_CTL_PLTP23_16|
1208 BT848_GPIO_DMA_CTL_GPINTC|
1209 BT848_GPIO_DMA_CTL_GPINTI,
1210 BT848_GPIO_DMA_CTL);
1212 btwrite(0x20, BT848_E_VSCALE_HI);
1213 btwrite(0x20, BT848_O_VSCALE_HI);
1215 v4l2_ctrl_handler_setup(&btv->ctrl_handler);
1221 static void bttv_reinit_bt848(struct bttv *btv)
1223 unsigned long flags;
1226 pr_info("%d: reset, reinitialize\n", btv->c.nr);
1227 spin_lock_irqsave(&btv->s_lock,flags);
1229 bttv_set_dma(btv,0);
1230 spin_unlock_irqrestore(&btv->s_lock,flags);
1233 btv->pll.pll_current = -1;
1234 set_input(btv, btv->input, btv->tvnorm);
1237 static int bttv_s_ctrl(struct v4l2_ctrl *c)
1239 struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
1243 case V4L2_CID_BRIGHTNESS:
1244 bt848_bright(btv, c->val);
1247 bt848_hue(btv, c->val);
1249 case V4L2_CID_CONTRAST:
1250 bt848_contrast(btv, c->val);
1252 case V4L2_CID_SATURATION:
1253 bt848_sat(btv, c->val);
1255 case V4L2_CID_COLOR_KILLER:
1257 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1258 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1260 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1261 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1264 case V4L2_CID_AUDIO_MUTE:
1265 audio_mute(btv, c->val);
1268 case V4L2_CID_AUDIO_VOLUME:
1269 btv->volume_gpio(btv, c->val);
1272 case V4L2_CID_CHROMA_AGC:
1273 val = c->val ? BT848_SCLOOP_CAGC : 0;
1274 btwrite(val, BT848_E_SCLOOP);
1275 btwrite(val, BT848_O_SCLOOP);
1277 case V4L2_CID_PRIVATE_COMBFILTER:
1278 btv->opt_combfilter = c->val;
1280 case V4L2_CID_PRIVATE_LUMAFILTER:
1282 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1283 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1285 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1286 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1289 case V4L2_CID_PRIVATE_AUTOMUTE:
1290 btv->opt_automute = c->val;
1292 case V4L2_CID_PRIVATE_AGC_CRUSH:
1293 btwrite(BT848_ADC_RESERVED |
1294 (c->val ? BT848_ADC_CRUSH : 0),
1297 case V4L2_CID_PRIVATE_VCR_HACK:
1298 btv->opt_vcr_hack = c->val;
1300 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1301 btwrite(c->val, BT848_WC_UP);
1303 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1304 btwrite(c->val, BT848_WC_DOWN);
1306 case V4L2_CID_PRIVATE_UV_RATIO:
1307 btv->opt_uv_ratio = c->val;
1308 bt848_sat(btv, btv->saturation);
1310 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1311 btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
1313 case V4L2_CID_PRIVATE_CORING:
1314 btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
1322 /* ----------------------------------------------------------------------- */
1324 static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
1325 .s_ctrl = bttv_s_ctrl,
1328 static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
1329 .ops = &bttv_ctrl_ops,
1330 .id = V4L2_CID_PRIVATE_COMBFILTER,
1331 .name = "Comb Filter",
1332 .type = V4L2_CTRL_TYPE_BOOLEAN,
1339 static struct v4l2_ctrl_config bttv_ctrl_automute = {
1340 .ops = &bttv_ctrl_ops,
1341 .id = V4L2_CID_PRIVATE_AUTOMUTE,
1342 .name = "Auto Mute",
1343 .type = V4L2_CTRL_TYPE_BOOLEAN,
1350 static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
1351 .ops = &bttv_ctrl_ops,
1352 .id = V4L2_CID_PRIVATE_LUMAFILTER,
1353 .name = "Luma Decimation Filter",
1354 .type = V4L2_CTRL_TYPE_BOOLEAN,
1361 static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
1362 .ops = &bttv_ctrl_ops,
1363 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
1364 .name = "AGC Crush",
1365 .type = V4L2_CTRL_TYPE_BOOLEAN,
1372 static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
1373 .ops = &bttv_ctrl_ops,
1374 .id = V4L2_CID_PRIVATE_VCR_HACK,
1376 .type = V4L2_CTRL_TYPE_BOOLEAN,
1383 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
1384 .ops = &bttv_ctrl_ops,
1385 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
1386 .name = "Whitecrush Lower",
1387 .type = V4L2_CTRL_TYPE_INTEGER,
1394 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
1395 .ops = &bttv_ctrl_ops,
1396 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
1397 .name = "Whitecrush Upper",
1398 .type = V4L2_CTRL_TYPE_INTEGER,
1405 static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
1406 .ops = &bttv_ctrl_ops,
1407 .id = V4L2_CID_PRIVATE_UV_RATIO,
1409 .type = V4L2_CTRL_TYPE_INTEGER,
1416 static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
1417 .ops = &bttv_ctrl_ops,
1418 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
1419 .name = "Full Luma Range",
1420 .type = V4L2_CTRL_TYPE_BOOLEAN,
1426 static struct v4l2_ctrl_config bttv_ctrl_coring = {
1427 .ops = &bttv_ctrl_ops,
1428 .id = V4L2_CID_PRIVATE_CORING,
1430 .type = V4L2_CTRL_TYPE_INTEGER,
1437 /* ----------------------------------------------------------------------- */
1439 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1441 unsigned int outbits, data;
1442 outbits = btread(BT848_GPIO_OUT_EN);
1443 data = btread(BT848_GPIO_DATA);
1444 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1445 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
1448 static const struct bttv_format*
1449 format_by_fourcc(int fourcc)
1453 for (i = 0; i < FORMATS; i++) {
1454 if (-1 == formats[i].fourcc)
1456 if (formats[i].fourcc == fourcc)
1462 /* ----------------------------------------------------------------------- */
1463 /* video4linux (1) interface */
1465 static int queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
1466 unsigned int *num_planes, unsigned int sizes[],
1467 struct device *alloc_devs[])
1469 struct bttv *btv = vb2_get_drv_priv(q);
1470 unsigned int size = btv->fmt->depth * btv->width * btv->height >> 3;
1473 return sizes[0] < size ? -EINVAL : 0;
1480 static void buf_queue(struct vb2_buffer *vb)
1482 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1483 struct vb2_queue *vq = vb->vb2_queue;
1484 struct bttv *btv = vb2_get_drv_priv(vq);
1485 struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
1486 unsigned long flags;
1488 spin_lock_irqsave(&btv->s_lock, flags);
1489 if (list_empty(&btv->capture)) {
1490 btv->loop_irq = BT848_RISC_VIDEO;
1491 if (vb2_is_streaming(&btv->vbiq))
1492 btv->loop_irq |= BT848_RISC_VBI;
1493 bttv_set_dma(btv, BT848_CAP_CTL_CAPTURE_ODD |
1494 BT848_CAP_CTL_CAPTURE_EVEN);
1496 list_add_tail(&buf->list, &btv->capture);
1497 spin_unlock_irqrestore(&btv->s_lock, flags);
1500 static int buf_prepare(struct vb2_buffer *vb)
1502 struct vb2_queue *vq = vb->vb2_queue;
1503 struct bttv *btv = vb2_get_drv_priv(vq);
1504 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1505 struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
1506 unsigned int size = (btv->fmt->depth * btv->width * btv->height) >> 3;
1508 if (vb2_plane_size(vb, 0) < size)
1510 vb2_set_plane_payload(vb, 0, size);
1512 if (btv->field != V4L2_FIELD_ALTERNATE) {
1513 buf->vbuf.field = btv->field;
1514 } else if (btv->field_last == V4L2_FIELD_TOP) {
1515 buf->vbuf.field = V4L2_FIELD_BOTTOM;
1516 btv->field_last = V4L2_FIELD_BOTTOM;
1518 buf->vbuf.field = V4L2_FIELD_TOP;
1519 btv->field_last = V4L2_FIELD_TOP;
1522 /* Allocate memory for risc struct and create the risc program. */
1523 return bttv_buffer_risc(btv, buf);
1526 static void buf_cleanup(struct vb2_buffer *vb)
1528 struct vb2_queue *vq = vb->vb2_queue;
1529 struct bttv *btv = vb2_get_drv_priv(vq);
1530 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1531 struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
1533 btcx_riscmem_free(btv->c.pci, &buf->top);
1534 btcx_riscmem_free(btv->c.pci, &buf->bottom);
1537 static int start_streaming(struct vb2_queue *q, unsigned int count)
1540 struct bttv_buffer *buf;
1541 struct bttv *btv = vb2_get_drv_priv(q);
1543 if (!check_alloc_btres_lock(btv, RESOURCE_VIDEO_STREAM)) {
1544 if (btv->field_count)
1546 while (!list_empty(&btv->capture)) {
1547 buf = list_entry(btv->capture.next,
1548 struct bttv_buffer, list);
1549 list_del(&buf->list);
1550 buf->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
1551 vb2_buffer_done(&buf->vbuf.vb2_buf,
1552 VB2_BUF_STATE_QUEUED);
1556 if (!vb2_is_streaming(&btv->vbiq)) {
1558 btv->field_count = 0;
1565 static void stop_streaming(struct vb2_queue *q)
1567 unsigned long flags;
1568 struct bttv *btv = vb2_get_drv_priv(q);
1570 vb2_wait_for_all_buffers(q);
1571 spin_lock_irqsave(&btv->s_lock, flags);
1572 free_btres_lock(btv, RESOURCE_VIDEO_STREAM);
1573 if (!vb2_is_streaming(&btv->vbiq)) {
1574 /* stop field counter */
1575 btand(~BT848_INT_VSYNC, BT848_INT_MASK);
1577 spin_unlock_irqrestore(&btv->s_lock, flags);
1580 static const struct vb2_ops bttv_video_qops = {
1581 .queue_setup = queue_setup,
1582 .buf_queue = buf_queue,
1583 .buf_prepare = buf_prepare,
1584 .buf_cleanup = buf_cleanup,
1585 .start_streaming = start_streaming,
1586 .stop_streaming = stop_streaming,
1587 .wait_prepare = vb2_ops_wait_prepare,
1588 .wait_finish = vb2_ops_wait_finish,
1591 static void radio_enable(struct bttv *btv)
1593 /* Switch to the radio tuner */
1594 if (!btv->has_radio_tuner) {
1595 btv->has_radio_tuner = 1;
1596 bttv_call_all(btv, tuner, s_radio);
1597 btv->audio_input = TVAUDIO_INPUT_RADIO;
1598 audio_input(btv, btv->audio_input);
1602 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
1604 struct bttv *btv = video_drvdata(file);
1607 for (i = 0; i < BTTV_TVNORMS; i++)
1608 if (id & bttv_tvnorms[i].v4l2_id)
1610 if (i == BTTV_TVNORMS)
1617 static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
1619 struct bttv *btv = video_drvdata(file);
1625 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1627 struct bttv *btv = video_drvdata(file);
1629 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1630 *id &= V4L2_STD_625_50;
1632 *id &= V4L2_STD_525_60;
1636 static int bttv_enum_input(struct file *file, void *priv,
1637 struct v4l2_input *i)
1639 struct bttv *btv = video_drvdata(file);
1641 if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
1644 i->type = V4L2_INPUT_TYPE_CAMERA;
1647 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1648 sprintf(i->name, "Television");
1649 i->type = V4L2_INPUT_TYPE_TUNER;
1651 } else if (i->index == btv->svhs) {
1652 sprintf(i->name, "S-Video");
1654 sprintf(i->name, "Composite%d", i->index);
1657 if (i->index == btv->input) {
1658 __u32 dstatus = btread(BT848_DSTATUS);
1659 if (0 == (dstatus & BT848_DSTATUS_PRES))
1660 i->status |= V4L2_IN_ST_NO_SIGNAL;
1661 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1662 i->status |= V4L2_IN_ST_NO_H_LOCK;
1665 i->std = BTTV_NORMS;
1669 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1671 struct bttv *btv = video_drvdata(file);
1678 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1680 struct bttv *btv = video_drvdata(file);
1682 if (i >= bttv_tvcards[btv->c.type].video_inputs)
1685 set_input(btv, i, btv->tvnorm);
1689 static int bttv_s_tuner(struct file *file, void *priv,
1690 const struct v4l2_tuner *t)
1692 struct bttv *btv = video_drvdata(file);
1697 bttv_call_all(btv, tuner, s_tuner, t);
1699 if (btv->audio_mode_gpio) {
1700 struct v4l2_tuner copy = *t;
1702 btv->audio_mode_gpio(btv, ©, 1);
1707 static int bttv_g_frequency(struct file *file, void *priv,
1708 struct v4l2_frequency *f)
1710 struct bttv *btv = video_drvdata(file);
1715 if (f->type == V4L2_TUNER_RADIO)
1717 f->frequency = f->type == V4L2_TUNER_RADIO ?
1718 btv->radio_freq : btv->tv_freq;
1723 static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
1725 struct v4l2_frequency new_freq = *f;
1727 bttv_call_all(btv, tuner, s_frequency, f);
1728 /* s_frequency may clamp the frequency, so get the actual
1729 frequency before assigning radio/tv_freq. */
1730 bttv_call_all(btv, tuner, g_frequency, &new_freq);
1731 if (new_freq.type == V4L2_TUNER_RADIO) {
1733 btv->radio_freq = new_freq.frequency;
1734 if (btv->has_tea575x) {
1735 btv->tea.freq = btv->radio_freq;
1736 snd_tea575x_set_freq(&btv->tea);
1739 btv->tv_freq = new_freq.frequency;
1743 static int bttv_s_frequency(struct file *file, void *priv,
1744 const struct v4l2_frequency *f)
1746 struct bttv *btv = video_drvdata(file);
1751 bttv_set_frequency(btv, f);
1755 static int bttv_log_status(struct file *file, void *f)
1757 struct video_device *vdev = video_devdata(file);
1758 struct bttv *btv = video_drvdata(file);
1760 v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
1761 bttv_call_all(btv, core, log_status);
1765 #ifdef CONFIG_VIDEO_ADV_DEBUG
1766 static int bttv_g_register(struct file *file, void *f,
1767 struct v4l2_dbg_register *reg)
1769 struct bttv *btv = video_drvdata(file);
1771 /* bt848 has a 12-bit register space */
1773 reg->val = btread(reg->reg);
1779 static int bttv_s_register(struct file *file, void *f,
1780 const struct v4l2_dbg_register *reg)
1782 struct bttv *btv = video_drvdata(file);
1784 /* bt848 has a 12-bit register space */
1785 btwrite(reg->val, reg->reg & 0xfff);
1791 /* Given cropping boundaries b and the scaled width and height of a
1792 single field or frame, which must not exceed hardware limits, this
1793 function adjusts the cropping parameters c. */
1795 bttv_crop_adjust (struct bttv_crop * c,
1796 const struct v4l2_rect * b,
1799 enum v4l2_field field)
1801 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
1805 if (width < c->min_scaled_width) {
1806 /* Max. hor. scale factor 16:1. */
1807 c->rect.width = width * 16;
1808 } else if (width > c->max_scaled_width) {
1809 /* Min. hor. scale factor 1:1. */
1810 c->rect.width = width;
1812 max_left = b->left + b->width - width;
1813 max_left = min(max_left, (__s32) MAX_HDELAY);
1814 if (c->rect.left > max_left)
1815 c->rect.left = max_left;
1818 if (height < c->min_scaled_height) {
1819 /* Max. vert. scale factor 16:1, single fields 8:1. */
1820 c->rect.height = height * 16;
1821 } else if (frame_height > c->max_scaled_height) {
1822 /* Min. vert. scale factor 1:1.
1823 Top and height count field lines times two. */
1824 c->rect.height = (frame_height + 1) & ~1;
1826 max_top = b->top + b->height - c->rect.height;
1827 if (c->rect.top > max_top)
1828 c->rect.top = max_top;
1831 bttv_crop_calc_limits(c);
1834 /* Returns an error if scaling to a frame or single field with the given
1835 width and height is not possible with the current cropping parameters
1836 and width aligned according to width_mask. If adjust_size is TRUE the
1837 function may adjust the width and/or height instead, rounding width
1838 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
1839 also adjust the current cropping parameters to get closer to the
1840 desired image size. */
1842 limit_scaled_size_lock(struct bttv *btv, __s32 *width, __s32 *height,
1843 enum v4l2_field field, unsigned int width_mask,
1844 unsigned int width_bias, int adjust_size,
1847 const struct v4l2_rect *b;
1848 struct bttv_crop *c;
1855 WARN_ON((int)width_mask >= 0 ||
1856 width_bias >= (unsigned int)(-width_mask));
1858 /* Make sure tvnorm, vbi_end and the current cropping parameters
1859 remain consistent until we're done. */
1861 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
1863 /* Do crop - use current, don't - use default parameters. */
1864 c = &btv->crop[!!btv->do_crop];
1869 && !locked_btres(btv, VIDEO_RESOURCES)) {
1873 /* We cannot scale up. When the scaled image is larger
1874 than crop.rect we adjust the crop.rect as required
1875 by the V4L2 spec, hence cropcap.bounds are our limit. */
1876 max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
1877 max_height = b->height;
1879 /* We cannot capture the same line as video and VBI data.
1880 Note btv->vbi_end is really a minimum, see
1881 bttv_vbi_try_fmt(). */
1882 if (btv->vbi_end > b->top) {
1883 max_height -= btv->vbi_end - b->top;
1885 if (min_height > max_height)
1890 if (btv->vbi_end > c->rect.top)
1893 min_width = c->min_scaled_width;
1894 min_height = c->min_scaled_height;
1895 max_width = c->max_scaled_width;
1896 max_height = c->max_scaled_height;
1901 min_width = (min_width - width_mask - 1) & width_mask;
1902 max_width = max_width & width_mask;
1904 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
1905 /* Min. scale factor is 1:1. */
1906 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
1909 *width = clamp(*width, min_width, max_width);
1910 *height = clamp(*height, min_height, max_height);
1912 /* Round after clamping to avoid overflow. */
1913 *width = (*width + width_bias) & width_mask;
1916 bttv_crop_adjust(c, b, *width, *height, field);
1918 if (btv->vbi_end > c->rect.top) {
1919 /* Move the crop window out of the way. */
1920 c->rect.top = btv->vbi_end;
1925 if (*width < min_width ||
1926 *height < min_height ||
1927 *width > max_width ||
1928 *height > max_height ||
1929 0 != (*width & ~width_mask))
1933 rc = 0; /* success */
1940 static int bttv_switch_type(struct bttv *btv, enum v4l2_buf_type type)
1943 struct vb2_queue *q;
1946 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1948 res = RESOURCE_VIDEO_STREAM;
1950 case V4L2_BUF_TYPE_VBI_CAPTURE:
1959 if (check_btres(btv, res))
1969 pix_format_set_size (struct v4l2_pix_format * f,
1970 const struct bttv_format * fmt,
1972 unsigned int height)
1977 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
1978 f->bytesperline = width; /* Y plane */
1979 f->sizeimage = (width * height * fmt->depth) >> 3;
1981 f->bytesperline = (width * fmt->depth) >> 3;
1982 f->sizeimage = height * f->bytesperline;
1986 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
1987 struct v4l2_format *f)
1989 struct bttv *btv = video_drvdata(file);
1991 pix_format_set_size(&f->fmt.pix, btv->fmt, btv->width, btv->height);
1992 f->fmt.pix.field = btv->field;
1993 f->fmt.pix.pixelformat = btv->fmt->fourcc;
1994 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1999 static void bttv_get_width_mask_vid_cap(const struct bttv_format *fmt,
2000 unsigned int *width_mask,
2001 unsigned int *width_bias)
2003 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2004 *width_mask = ~15; /* width must be a multiple of 16 pixels */
2005 *width_bias = 8; /* nearest */
2007 *width_mask = ~3; /* width must be a multiple of 4 pixels */
2008 *width_bias = 2; /* nearest */
2012 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2013 struct v4l2_format *f)
2015 const struct bttv_format *fmt;
2016 struct bttv *btv = video_drvdata(file);
2017 enum v4l2_field field;
2018 __s32 width, height;
2020 unsigned int width_mask, width_bias;
2023 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2027 field = f->fmt.pix.field;
2030 case V4L2_FIELD_TOP:
2031 case V4L2_FIELD_BOTTOM:
2032 case V4L2_FIELD_ALTERNATE:
2033 case V4L2_FIELD_INTERLACED:
2035 case V4L2_FIELD_SEQ_BT:
2036 case V4L2_FIELD_SEQ_TB:
2037 if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
2038 field = V4L2_FIELD_SEQ_TB;
2042 default: /* FIELD_ANY case */
2043 height2 = btv->crop[!!btv->do_crop].rect.height >> 1;
2044 field = (f->fmt.pix.height > height2)
2045 ? V4L2_FIELD_INTERLACED
2046 : V4L2_FIELD_BOTTOM;
2050 width = f->fmt.pix.width;
2051 height = f->fmt.pix.height;
2053 bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2054 rc = limit_scaled_size_lock(btv, &width, &height, field, width_mask,
2059 /* update data for the application */
2060 f->fmt.pix.field = field;
2061 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2062 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2067 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2068 struct v4l2_format *f)
2071 const struct bttv_format *fmt;
2072 struct bttv *btv = video_drvdata(file);
2073 __s32 width, height;
2074 unsigned int width_mask, width_bias;
2075 enum v4l2_field field;
2077 retval = bttv_switch_type(btv, f->type);
2081 retval = bttv_try_fmt_vid_cap(file, priv, f);
2085 width = f->fmt.pix.width;
2086 height = f->fmt.pix.height;
2087 field = f->fmt.pix.field;
2089 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2090 bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2091 retval = limit_scaled_size_lock(btv, &width, &height, f->fmt.pix.field,
2092 width_mask, width_bias, 1, 1);
2096 f->fmt.pix.field = field;
2098 /* update our state information */
2100 btv->width = f->fmt.pix.width;
2101 btv->height = f->fmt.pix.height;
2102 btv->field = f->fmt.pix.field;
2104 * When field is V4L2_FIELD_ALTERNATE, buffers will be either
2105 * V4L2_FIELD_TOP or V4L2_FIELD_BOTTOM depending on the value of
2106 * field_last. Initialize field_last to V4L2_FIELD_BOTTOM so that
2107 * streaming starts with a V4L2_FIELD_TOP buffer.
2109 btv->field_last = V4L2_FIELD_BOTTOM;
2114 static int bttv_querycap(struct file *file, void *priv,
2115 struct v4l2_capability *cap)
2117 struct bttv *btv = video_drvdata(file);
2122 strscpy(cap->driver, "bttv", sizeof(cap->driver));
2123 strscpy(cap->card, btv->video_dev.name, sizeof(cap->card));
2124 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
2125 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
2126 if (video_is_registered(&btv->vbi_dev))
2127 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2128 if (video_is_registered(&btv->radio_dev)) {
2129 cap->capabilities |= V4L2_CAP_RADIO;
2130 if (btv->has_tea575x)
2131 cap->capabilities |= V4L2_CAP_HW_FREQ_SEEK;
2135 * No need to lock here: those vars are initialized during board
2136 * probe and remains untouched during the rest of the driver lifecycle
2138 if (btv->has_saa6588)
2139 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2140 if (btv->tuner_type != TUNER_ABSENT)
2141 cap->capabilities |= V4L2_CAP_TUNER;
2145 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2146 struct v4l2_fmtdesc *f)
2150 for (i = 0; i < FORMATS; i++) {
2151 if (formats[i].fourcc != -1)
2153 if ((unsigned int)index == f->index)
2159 f->pixelformat = formats[i].fourcc;
2164 static int bttv_g_parm(struct file *file, void *f,
2165 struct v4l2_streamparm *parm)
2167 struct bttv *btv = video_drvdata(file);
2169 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2171 parm->parm.capture.readbuffers = gbuffers;
2172 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2173 &parm->parm.capture.timeperframe);
2178 static int bttv_g_tuner(struct file *file, void *priv,
2179 struct v4l2_tuner *t)
2181 struct bttv *btv = video_drvdata(file);
2186 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2187 t->capability = V4L2_TUNER_CAP_NORM;
2188 bttv_call_all(btv, tuner, g_tuner, t);
2189 strscpy(t->name, "Television", sizeof(t->name));
2190 t->type = V4L2_TUNER_ANALOG_TV;
2191 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2194 if (btv->audio_mode_gpio)
2195 btv->audio_mode_gpio(btv, t, 0);
2200 static int bttv_g_pixelaspect(struct file *file, void *priv,
2201 int type, struct v4l2_fract *f)
2203 struct bttv *btv = video_drvdata(file);
2205 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2208 /* defrect and bounds are set via g_selection */
2209 *f = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
2213 static int bttv_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
2215 struct bttv *btv = video_drvdata(file);
2217 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2220 switch (sel->target) {
2221 case V4L2_SEL_TGT_CROP:
2222 sel->r = btv->crop[!!btv->do_crop].rect;
2224 case V4L2_SEL_TGT_CROP_DEFAULT:
2225 sel->r = bttv_tvnorms[btv->tvnorm].cropcap.defrect;
2227 case V4L2_SEL_TGT_CROP_BOUNDS:
2228 sel->r = bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2237 static int bttv_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
2239 struct bttv *btv = video_drvdata(file);
2240 const struct v4l2_rect *b;
2248 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2251 if (sel->target != V4L2_SEL_TGT_CROP)
2254 /* Make sure tvnorm, vbi_end and the current cropping
2255 parameters remain consistent until we're done. Note
2256 read() may change vbi_end in check_alloc_btres_lock(). */
2259 if (locked_btres(btv, VIDEO_RESOURCES))
2262 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2265 b_right = b_left + b->width;
2266 b_bottom = b->top + b->height;
2268 b_top = max(b->top, btv->vbi_end);
2269 if (b_top + 32 >= b_bottom) {
2273 /* Min. scaled size 48 x 32. */
2274 c.rect.left = clamp_t(s32, sel->r.left, b_left, b_right - 48);
2275 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
2277 c.rect.width = clamp_t(s32, sel->r.width,
2278 48, b_right - c.rect.left);
2280 c.rect.top = clamp_t(s32, sel->r.top, b_top, b_bottom - 32);
2281 /* Top and height must be a multiple of two. */
2282 c.rect.top = (c.rect.top + 1) & ~1;
2284 c.rect.height = clamp_t(s32, sel->r.height,
2285 32, b_bottom - c.rect.top);
2286 c.rect.height = (c.rect.height + 1) & ~1;
2288 bttv_crop_calc_limits(&c);
2296 if (btv->width < c.min_scaled_width)
2297 btv->width = c.min_scaled_width;
2298 else if (btv->width > c.max_scaled_width)
2299 btv->width = c.max_scaled_width;
2301 if (btv->height < c.min_scaled_height)
2302 btv->height = c.min_scaled_height;
2303 else if (btv->height > c.max_scaled_height)
2304 btv->height = c.max_scaled_height;
2309 static const struct v4l2_file_operations bttv_fops =
2311 .owner = THIS_MODULE,
2312 .open = v4l2_fh_open,
2313 .release = vb2_fop_release,
2314 .unlocked_ioctl = video_ioctl2,
2315 .read = vb2_fop_read,
2316 .mmap = vb2_fop_mmap,
2317 .poll = vb2_fop_poll,
2320 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
2321 .vidioc_querycap = bttv_querycap,
2322 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
2323 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
2324 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
2325 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
2326 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
2327 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
2328 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
2329 .vidioc_g_pixelaspect = bttv_g_pixelaspect,
2330 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2331 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2332 .vidioc_querybuf = vb2_ioctl_querybuf,
2333 .vidioc_qbuf = vb2_ioctl_qbuf,
2334 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2335 .vidioc_streamon = vb2_ioctl_streamon,
2336 .vidioc_streamoff = vb2_ioctl_streamoff,
2337 .vidioc_s_std = bttv_s_std,
2338 .vidioc_g_std = bttv_g_std,
2339 .vidioc_enum_input = bttv_enum_input,
2340 .vidioc_g_input = bttv_g_input,
2341 .vidioc_s_input = bttv_s_input,
2342 .vidioc_g_tuner = bttv_g_tuner,
2343 .vidioc_s_tuner = bttv_s_tuner,
2344 .vidioc_g_selection = bttv_g_selection,
2345 .vidioc_s_selection = bttv_s_selection,
2346 .vidioc_g_parm = bttv_g_parm,
2347 .vidioc_g_frequency = bttv_g_frequency,
2348 .vidioc_s_frequency = bttv_s_frequency,
2349 .vidioc_log_status = bttv_log_status,
2350 .vidioc_querystd = bttv_querystd,
2351 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2352 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2353 #ifdef CONFIG_VIDEO_ADV_DEBUG
2354 .vidioc_g_register = bttv_g_register,
2355 .vidioc_s_register = bttv_s_register,
2359 static struct video_device bttv_video_template = {
2361 .ioctl_ops = &bttv_ioctl_ops,
2362 .tvnorms = BTTV_NORMS,
2365 /* ----------------------------------------------------------------------- */
2366 /* radio interface */
2368 static int radio_open(struct file *file)
2370 struct video_device *vdev = video_devdata(file);
2371 struct bttv *btv = video_drvdata(file);
2372 int ret = v4l2_fh_open(file);
2377 dprintk("open dev=%s\n", video_device_node_name(vdev));
2378 dprintk("%d: open called (radio)\n", btv->c.nr);
2381 audio_mute(btv, btv->mute);
2386 static int radio_release(struct file *file)
2388 struct bttv *btv = video_drvdata(file);
2389 struct saa6588_command cmd;
2393 bttv_call_all(btv, core, command, SAA6588_CMD_CLOSE, &cmd);
2395 if (btv->radio_user == 0)
2396 btv->has_radio_tuner = 0;
2398 v4l2_fh_release(file);
2403 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
2405 struct bttv *btv = video_drvdata(file);
2409 strscpy(t->name, "Radio", sizeof(t->name));
2410 t->type = V4L2_TUNER_RADIO;
2413 bttv_call_all(btv, tuner, g_tuner, t);
2415 if (btv->audio_mode_gpio)
2416 btv->audio_mode_gpio(btv, t, 0);
2418 if (btv->has_tea575x)
2419 return snd_tea575x_g_tuner(&btv->tea, t);
2424 static int radio_s_tuner(struct file *file, void *priv,
2425 const struct v4l2_tuner *t)
2427 struct bttv *btv = video_drvdata(file);
2433 bttv_call_all(btv, tuner, s_tuner, t);
2437 static int radio_s_hw_freq_seek(struct file *file, void *priv,
2438 const struct v4l2_hw_freq_seek *a)
2440 struct bttv *btv = video_drvdata(file);
2442 if (btv->has_tea575x)
2443 return snd_tea575x_s_hw_freq_seek(file, &btv->tea, a);
2448 static int radio_enum_freq_bands(struct file *file, void *priv,
2449 struct v4l2_frequency_band *band)
2451 struct bttv *btv = video_drvdata(file);
2453 if (btv->has_tea575x)
2454 return snd_tea575x_enum_freq_bands(&btv->tea, band);
2459 static ssize_t radio_read(struct file *file, char __user *data,
2460 size_t count, loff_t *ppos)
2462 struct bttv *btv = video_drvdata(file);
2463 struct saa6588_command cmd;
2465 cmd.block_count = count / 3;
2466 cmd.nonblocking = file->f_flags & O_NONBLOCK;
2468 cmd.instance = file;
2469 cmd.result = -ENODEV;
2472 bttv_call_all(btv, core, command, SAA6588_CMD_READ, &cmd);
2477 static __poll_t radio_poll(struct file *file, poll_table *wait)
2479 struct bttv *btv = video_drvdata(file);
2480 struct saa6588_command cmd;
2481 __poll_t rc = v4l2_ctrl_poll(file, wait);
2484 cmd.instance = file;
2485 cmd.event_list = wait;
2487 bttv_call_all(btv, core, command, SAA6588_CMD_POLL, &cmd);
2489 return rc | cmd.poll_mask;
2492 static const struct v4l2_file_operations radio_fops =
2494 .owner = THIS_MODULE,
2497 .release = radio_release,
2498 .unlocked_ioctl = video_ioctl2,
2502 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2503 .vidioc_querycap = bttv_querycap,
2504 .vidioc_log_status = bttv_log_status,
2505 .vidioc_g_tuner = radio_g_tuner,
2506 .vidioc_s_tuner = radio_s_tuner,
2507 .vidioc_g_frequency = bttv_g_frequency,
2508 .vidioc_s_frequency = bttv_s_frequency,
2509 .vidioc_s_hw_freq_seek = radio_s_hw_freq_seek,
2510 .vidioc_enum_freq_bands = radio_enum_freq_bands,
2511 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2512 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2515 static struct video_device radio_template = {
2516 .fops = &radio_fops,
2517 .ioctl_ops = &radio_ioctl_ops,
2520 /* ----------------------------------------------------------------------- */
2521 /* some debug code */
2523 static int bttv_risc_decode(u32 risc)
2525 static char *instr[16] = {
2526 [ BT848_RISC_WRITE >> 28 ] = "write",
2527 [ BT848_RISC_SKIP >> 28 ] = "skip",
2528 [ BT848_RISC_WRITEC >> 28 ] = "writec",
2529 [ BT848_RISC_JUMP >> 28 ] = "jump",
2530 [ BT848_RISC_SYNC >> 28 ] = "sync",
2531 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
2532 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
2533 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
2535 static int incr[16] = {
2536 [ BT848_RISC_WRITE >> 28 ] = 2,
2537 [ BT848_RISC_JUMP >> 28 ] = 2,
2538 [ BT848_RISC_SYNC >> 28 ] = 2,
2539 [ BT848_RISC_WRITE123 >> 28 ] = 5,
2540 [ BT848_RISC_SKIP123 >> 28 ] = 2,
2541 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
2543 static char *bits[] = {
2544 "be0", "be1", "be2", "be3/resync",
2545 "set0", "set1", "set2", "set3",
2546 "clr0", "clr1", "clr2", "clr3",
2547 "irq", "res", "eol", "sol",
2551 pr_cont("0x%08x [ %s", risc,
2552 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
2553 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
2554 if (risc & (1 << (i + 12)))
2555 pr_cont(" %s", bits[i]);
2556 pr_cont(" count=%d ]\n", risc & 0xfff);
2557 return incr[risc >> 28] ? incr[risc >> 28] : 1;
2560 static void bttv_risc_disasm(struct bttv *btv,
2561 struct btcx_riscmem *risc)
2565 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
2566 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
2567 for (i = 0; i < (risc->size >> 2); i += n) {
2568 pr_info("%s: 0x%lx: ",
2569 btv->c.v4l2_dev.name,
2570 (unsigned long)(risc->dma + (i<<2)));
2571 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
2572 for (j = 1; j < n; j++)
2573 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
2574 btv->c.v4l2_dev.name,
2575 (unsigned long)(risc->dma + ((i+j)<<2)),
2577 if (0 == risc->cpu[i])
2582 static void bttv_print_riscaddr(struct bttv *btv)
2584 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
2585 pr_info(" vbi : o=%08llx e=%08llx\n",
2586 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
2587 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
2588 pr_info(" cap : o=%08llx e=%08llx\n",
2590 ? (unsigned long long)btv->curr.top->top.dma : 0,
2592 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
2593 bttv_risc_disasm(btv, &btv->main);
2596 /* ----------------------------------------------------------------------- */
2599 static char *irq_name[] = {
2600 "FMTCHG", // format change detected (525 vs. 625)
2601 "VSYNC", // vertical sync (new field)
2602 "HSYNC", // horizontal sync
2603 "OFLOW", // chroma/luma AGC overflow
2604 "HLOCK", // horizontal lock changed
2605 "VPRES", // video presence changed
2607 "I2CDONE", // hw irc operation finished
2608 "GPINT", // gpio port triggered irq
2610 "RISCI", // risc instruction triggered irq
2611 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
2612 "FTRGT", // pixel data fifo overrun
2613 "FDSR", // fifo data stream resyncronisation
2614 "PPERR", // parity error (data transfer)
2615 "RIPERR", // parity error (read risc instructions)
2616 "PABORT", // pci abort
2617 "OCERR", // risc instruction error
2618 "SCERR", // syncronisation error
2621 static void bttv_print_irqbits(u32 print, u32 mark)
2626 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
2627 if (print & (1 << i))
2628 pr_cont(" %s", irq_name[i]);
2629 if (mark & (1 << i))
2634 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
2636 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
2638 (unsigned long)btv->main.dma,
2639 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
2640 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
2643 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
2644 pr_notice("%d: Oh, there (temporarily?) is no input signal. Ok, then this is harmless, don't worry ;)\n",
2648 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
2650 pr_notice("%d: Lets try to catch the culprit red-handed ...\n",
2656 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
2658 struct bttv_buffer *item;
2660 memset(set,0,sizeof(*set));
2662 /* capture request ? */
2663 if (!list_empty(&btv->capture)) {
2664 set->frame_irq = BT848_RISC_VIDEO;
2665 item = list_entry(btv->capture.next, struct bttv_buffer, list);
2667 if (V4L2_FIELD_HAS_TOP(item->vbuf.field))
2669 if (V4L2_FIELD_HAS_BOTTOM(item->vbuf.field))
2672 /* capture request for other field ? */
2673 if (!V4L2_FIELD_HAS_BOTH(item->vbuf.field) &&
2674 item->list.next != &btv->capture) {
2675 item = list_entry(item->list.next,
2676 struct bttv_buffer, list);
2677 /* Mike Isely <isely@pobox.com> - Only check
2678 * and set up the bottom field in the logic
2679 * below. Don't ever do the top field. This
2680 * of course means that if we set up the
2681 * bottom field in the above code that we'll
2682 * actually skip a field. But that's OK.
2683 * Having processed only a single buffer this
2684 * time, then the next time around the first
2685 * available buffer should be for a top field.
2686 * That will then cause us here to set up a
2687 * top then a bottom field in the normal way.
2688 * The alternative to this understanding is
2689 * that we set up the second available buffer
2690 * as a top field, but that's out of order
2691 * since this driver always processes the top
2692 * field first - the effect will be the two
2693 * buffers being returned in the wrong order,
2694 * with the second buffer also being delayed
2695 * by one field time (owing to the fifo nature
2696 * of videobuf). Worse still, we'll be stuck
2697 * doing fields out of order now every time
2698 * until something else causes a field to be
2699 * dropped. By effectively forcing a field to
2700 * drop this way then we always get back into
2701 * sync within a single frame time. (Out of
2702 * order fields can screw up deinterlacing
2704 if (!V4L2_FIELD_HAS_BOTH(item->vbuf.field)) {
2706 item->vbuf.field == V4L2_FIELD_BOTTOM)
2708 if (set->top && set->bottom) {
2710 * The buffer set has a top buffer and
2711 * a bottom buffer and they are not
2712 * copies of each other.
2714 set->top_irq = BT848_RISC_TOP;
2720 dprintk("%d: next set: top=%p bottom=%p [irq=%d,%d]\n",
2721 btv->c.nr, set->top, set->bottom,
2722 set->frame_irq, set->top_irq);
2727 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
2728 struct bttv_buffer_set *curr, unsigned int state)
2730 u64 ts = ktime_get_ns();
2732 if (wakeup->top == wakeup->bottom) {
2733 if (NULL != wakeup->top && curr->top != wakeup->top) {
2735 pr_debug("%d: wakeup: both=%p\n",
2736 btv->c.nr, wakeup->top);
2737 wakeup->top->vbuf.vb2_buf.timestamp = ts;
2738 wakeup->top->vbuf.sequence = btv->field_count >> 1;
2739 vb2_buffer_done(&wakeup->top->vbuf.vb2_buf, state);
2740 if (btv->field_count == 0)
2741 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2744 if (NULL != wakeup->top && curr->top != wakeup->top) {
2746 pr_debug("%d: wakeup: top=%p\n",
2747 btv->c.nr, wakeup->top);
2748 wakeup->top->vbuf.vb2_buf.timestamp = ts;
2749 wakeup->top->vbuf.sequence = btv->field_count >> 1;
2750 vb2_buffer_done(&wakeup->top->vbuf.vb2_buf, state);
2751 if (btv->field_count == 0)
2752 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2754 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
2756 pr_debug("%d: wakeup: bottom=%p\n",
2757 btv->c.nr, wakeup->bottom);
2758 wakeup->bottom->vbuf.vb2_buf.timestamp = ts;
2759 wakeup->bottom->vbuf.sequence = btv->field_count >> 1;
2760 vb2_buffer_done(&wakeup->bottom->vbuf.vb2_buf, state);
2761 if (btv->field_count == 0)
2762 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2768 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
2773 wakeup->vbuf.vb2_buf.timestamp = ktime_get_ns();
2774 wakeup->vbuf.sequence = btv->field_count >> 1;
2777 * Ugly hack for backwards compatibility.
2778 * Some applications expect that the last 4 bytes of
2779 * the VBI data contains the sequence number.
2781 * This makes it possible to associate the VBI data
2782 * with the video frame if you use read() to get the
2785 if (vb2_fileio_is_active(wakeup->vbuf.vb2_buf.vb2_queue)) {
2786 u32 *vaddr = vb2_plane_vaddr(&wakeup->vbuf.vb2_buf, 0);
2787 unsigned long size =
2788 vb2_get_plane_payload(&wakeup->vbuf.vb2_buf, 0) / 4;
2790 if (vaddr && size) {
2792 *vaddr = wakeup->vbuf.sequence;
2796 vb2_buffer_done(&wakeup->vbuf.vb2_buf, state);
2797 if (btv->field_count == 0)
2798 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2801 static void bttv_irq_timeout(struct timer_list *t)
2803 struct bttv *btv = from_timer(btv, t, timeout);
2804 struct bttv_buffer_set old,new;
2805 struct bttv_buffer *ovbi;
2806 struct bttv_buffer *item;
2807 unsigned long flags;
2811 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
2812 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
2813 btread(BT848_RISC_COUNT));
2814 bttv_print_irqbits(btread(BT848_INT_STAT),0);
2818 spin_lock_irqsave(&btv->s_lock,flags);
2820 /* deactivate stuff */
2821 memset(&new,0,sizeof(new));
2827 bttv_buffer_activate_video(btv, &new);
2828 bttv_buffer_activate_vbi(btv, NULL);
2829 bttv_set_dma(btv, 0);
2832 bttv_irq_wakeup_video(btv, &old, &new, VB2_BUF_STATE_DONE);
2833 bttv_irq_wakeup_vbi(btv, ovbi, VB2_BUF_STATE_DONE);
2835 /* cancel all outstanding capture / vbi requests */
2836 if (btv->field_count)
2838 while (!list_empty(&btv->capture)) {
2839 item = list_entry(btv->capture.next, struct bttv_buffer, list);
2840 list_del(&item->list);
2841 item->vbuf.vb2_buf.timestamp = ktime_get_ns();
2842 item->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
2843 vb2_buffer_done(&item->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
2845 while (!list_empty(&btv->vcapture)) {
2846 item = list_entry(btv->vcapture.next, struct bttv_buffer, list);
2847 list_del(&item->list);
2848 item->vbuf.vb2_buf.timestamp = ktime_get_ns();
2849 item->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
2850 vb2_buffer_done(&item->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
2854 spin_unlock_irqrestore(&btv->s_lock,flags);
2858 bttv_irq_wakeup_top(struct bttv *btv)
2860 struct bttv_buffer *wakeup = btv->curr.top;
2865 spin_lock(&btv->s_lock);
2866 btv->curr.top_irq = 0;
2867 btv->curr.top = NULL;
2868 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
2869 wakeup->vbuf.vb2_buf.timestamp = ktime_get_ns();
2870 wakeup->vbuf.sequence = btv->field_count >> 1;
2871 vb2_buffer_done(&wakeup->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
2872 if (btv->field_count == 0)
2873 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2874 spin_unlock(&btv->s_lock);
2877 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
2881 if (rc > risc->dma + risc->size)
2887 bttv_irq_switch_video(struct bttv *btv)
2889 struct bttv_buffer_set new;
2890 struct bttv_buffer_set old;
2893 spin_lock(&btv->s_lock);
2895 /* new buffer set */
2896 bttv_irq_next_video(btv, &new);
2897 rc = btread(BT848_RISC_COUNT);
2898 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
2899 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
2902 bttv_irq_debug_low_latency(btv, rc);
2903 spin_unlock(&btv->s_lock);
2910 btv->loop_irq &= ~BT848_RISC_VIDEO;
2911 bttv_buffer_activate_video(btv, &new);
2912 bttv_set_dma(btv, 0);
2915 if (UNSET != btv->new_input) {
2916 video_mux(btv,btv->new_input);
2917 btv->new_input = UNSET;
2920 /* wake up finished buffers */
2921 bttv_irq_wakeup_video(btv, &old, &new, VB2_BUF_STATE_DONE);
2922 spin_unlock(&btv->s_lock);
2926 bttv_irq_switch_vbi(struct bttv *btv)
2928 struct bttv_buffer *new = NULL;
2929 struct bttv_buffer *old;
2932 spin_lock(&btv->s_lock);
2934 if (!list_empty(&btv->vcapture))
2935 new = list_entry(btv->vcapture.next, struct bttv_buffer, list);
2938 rc = btread(BT848_RISC_COUNT);
2939 if (NULL != old && (is_active(&old->top, rc) ||
2940 is_active(&old->bottom, rc))) {
2943 bttv_irq_debug_low_latency(btv, rc);
2944 spin_unlock(&btv->s_lock);
2950 btv->loop_irq &= ~BT848_RISC_VBI;
2951 bttv_buffer_activate_vbi(btv, new);
2952 bttv_set_dma(btv, 0);
2954 bttv_irq_wakeup_vbi(btv, old, VB2_BUF_STATE_DONE);
2955 spin_unlock(&btv->s_lock);
2958 static irqreturn_t bttv_irq(int irq, void *dev_id)
2966 btv=(struct bttv *)dev_id;
2970 /* get/clear interrupt status bits */
2971 stat=btread(BT848_INT_STAT);
2972 astat=stat&btread(BT848_INT_MASK);
2976 btwrite(stat,BT848_INT_STAT);
2978 /* get device status bits */
2979 dstat=btread(BT848_DSTATUS);
2982 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
2983 btv->c.nr, count, btv->field_count,
2984 stat>>28, btread(BT848_RISC_COUNT));
2985 bttv_print_irqbits(stat,astat);
2986 if (stat & BT848_INT_HLOCK)
2987 pr_cont(" HLOC => %s",
2988 dstat & BT848_DSTATUS_HLOC
2990 if (stat & BT848_INT_VPRES)
2991 pr_cont(" PRES => %s",
2992 dstat & BT848_DSTATUS_PRES
2994 if (stat & BT848_INT_FMTCHG)
2995 pr_cont(" NUML => %s",
2996 dstat & BT848_DSTATUS_NUML
3001 if (astat&BT848_INT_VSYNC)
3004 if ((astat & BT848_INT_GPINT) && btv->remote) {
3005 bttv_input_irq(btv);
3008 if (astat & BT848_INT_I2CDONE) {
3009 btv->i2c_done = stat;
3010 wake_up(&btv->i2c_queue);
3013 if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_VBI))
3014 bttv_irq_switch_vbi(btv);
3016 if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_TOP))
3017 bttv_irq_wakeup_top(btv);
3019 if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_VIDEO))
3020 bttv_irq_switch_video(btv);
3022 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3023 /* trigger automute */
3024 audio_mux_gpio(btv, btv->audio_input, btv->mute);
3026 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3027 pr_info("%d: %s%s @ %08x,",
3029 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3030 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3031 btread(BT848_RISC_COUNT));
3032 bttv_print_irqbits(stat,astat);
3035 bttv_print_riscaddr(btv);
3037 if (fdsr && astat & BT848_INT_FDSR) {
3038 pr_info("%d: FDSR @ %08x\n",
3039 btv->c.nr, btread(BT848_RISC_COUNT));
3041 bttv_print_riscaddr(btv);
3047 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3048 btwrite(0, BT848_INT_MASK);
3050 pr_err("%d: IRQ lockup, cleared int mask [",
3053 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3056 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3060 bttv_print_irqbits(stat,astat);
3068 return IRQ_RETVAL(handled);
3072 /* ----------------------------------------------------------------------- */
3073 /* initialization */
3075 static int vdev_init(struct bttv *btv, struct video_device *vfd,
3076 const struct video_device *template,
3077 const char *type_name)
3080 struct vb2_queue *q;
3082 vfd->v4l2_dev = &btv->c.v4l2_dev;
3083 vfd->release = video_device_release_empty;
3084 video_set_drvdata(vfd, btv);
3085 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3086 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3087 type_name, bttv_tvcards[btv->c.type].name);
3088 if (btv->tuner_type == TUNER_ABSENT) {
3089 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
3090 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
3091 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
3092 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
3095 if (strcmp(type_name, "radio") == 0)
3098 if (strcmp(type_name, "video") == 0) {
3100 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3101 q->ops = &bttv_video_qops;
3102 } else if (strcmp(type_name, "vbi") == 0) {
3104 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
3105 q->ops = &bttv_vbi_qops;
3109 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
3110 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ | VB2_DMABUF;
3111 q->mem_ops = &vb2_dma_sg_memops;
3113 q->gfp_flags = __GFP_DMA32;
3114 q->buf_struct_size = sizeof(struct bttv_buffer);
3115 q->lock = &btv->lock;
3116 q->min_buffers_needed = 2;
3117 q->dev = &btv->c.pci->dev;
3118 err = vb2_queue_init(q);
3126 static void bttv_unregister_video(struct bttv *btv)
3128 video_unregister_device(&btv->video_dev);
3129 video_unregister_device(&btv->vbi_dev);
3130 video_unregister_device(&btv->radio_dev);
3133 /* register video4linux devices */
3134 static int bttv_register_video(struct bttv *btv)
3137 vdev_init(btv, &btv->video_dev, &bttv_video_template, "video");
3138 btv->video_dev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
3139 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
3140 if (btv->tuner_type != TUNER_ABSENT)
3141 btv->video_dev.device_caps |= V4L2_CAP_TUNER;
3143 if (video_register_device(&btv->video_dev, VFL_TYPE_VIDEO,
3144 video_nr[btv->c.nr]) < 0)
3146 pr_info("%d: registered device %s\n",
3147 btv->c.nr, video_device_node_name(&btv->video_dev));
3148 if (device_create_file(&btv->video_dev.dev,
3149 &dev_attr_card)<0) {
3150 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
3155 vdev_init(btv, &btv->vbi_dev, &bttv_video_template, "vbi");
3156 btv->vbi_dev.device_caps = V4L2_CAP_VBI_CAPTURE | V4L2_CAP_READWRITE |
3158 if (btv->tuner_type != TUNER_ABSENT)
3159 btv->vbi_dev.device_caps |= V4L2_CAP_TUNER;
3161 if (video_register_device(&btv->vbi_dev, VFL_TYPE_VBI,
3162 vbi_nr[btv->c.nr]) < 0)
3164 pr_info("%d: registered device %s\n",
3165 btv->c.nr, video_device_node_name(&btv->vbi_dev));
3167 if (!btv->has_radio)
3170 vdev_init(btv, &btv->radio_dev, &radio_template, "radio");
3171 btv->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
3172 if (btv->has_saa6588)
3173 btv->radio_dev.device_caps |= V4L2_CAP_READWRITE |
3174 V4L2_CAP_RDS_CAPTURE;
3175 if (btv->has_tea575x)
3176 btv->radio_dev.device_caps |= V4L2_CAP_HW_FREQ_SEEK;
3177 btv->radio_dev.ctrl_handler = &btv->radio_ctrl_handler;
3178 if (video_register_device(&btv->radio_dev, VFL_TYPE_RADIO,
3179 radio_nr[btv->c.nr]) < 0)
3181 pr_info("%d: registered device %s\n",
3182 btv->c.nr, video_device_node_name(&btv->radio_dev));
3188 bttv_unregister_video(btv);
3193 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3194 /* response on cards with no firmware is not enabled by OF */
3195 static void pci_set_command(struct pci_dev *dev)
3197 #if defined(__powerpc__)
3200 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3201 cmd = (cmd | PCI_COMMAND_MEMORY );
3202 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3206 static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
3208 struct v4l2_frequency init_freq = {
3210 .type = V4L2_TUNER_ANALOG_TV,
3216 struct v4l2_ctrl_handler *hdl;
3218 if (bttv_num == BTTV_MAX)
3220 pr_info("Bt8xx card found (%d)\n", bttv_num);
3221 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
3223 pr_err("out of memory\n");
3226 btv->c.nr = bttv_num;
3227 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
3228 "bttv%d", btv->c.nr);
3230 /* initialize structs / fill in defaults */
3231 mutex_init(&btv->lock);
3232 spin_lock_init(&btv->s_lock);
3233 spin_lock_init(&btv->gpio_lock);
3234 init_waitqueue_head(&btv->i2c_queue);
3235 INIT_LIST_HEAD(&btv->c.subs);
3236 INIT_LIST_HEAD(&btv->capture);
3237 INIT_LIST_HEAD(&btv->vcapture);
3239 timer_setup(&btv->timeout, bttv_irq_timeout, 0);
3242 btv->tuner_type = UNSET;
3243 btv->new_input = UNSET;
3244 btv->has_radio=radio[btv->c.nr];
3246 /* pci stuff (init, get irq/mmio, ... */
3248 btv->id = dev->device;
3249 if (pci_enable_device(dev)) {
3250 pr_warn("%d: Can't enable device\n", btv->c.nr);
3254 if (dma_set_mask(&dev->dev, DMA_BIT_MASK(32))) {
3255 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
3259 if (!request_mem_region(pci_resource_start(dev,0),
3260 pci_resource_len(dev,0),
3261 btv->c.v4l2_dev.name)) {
3262 pr_warn("%d: can't request iomem (0x%llx)\n",
3264 (unsigned long long)pci_resource_start(dev, 0));
3268 pci_set_master(dev);
3269 pci_set_command(dev);
3271 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
3273 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
3276 hdl = &btv->ctrl_handler;
3277 v4l2_ctrl_handler_init(hdl, 20);
3278 btv->c.v4l2_dev.ctrl_handler = hdl;
3279 v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
3281 btv->revision = dev->revision;
3282 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3283 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
3284 bttv_num, btv->id, btv->revision, pci_name(dev),
3285 btv->c.pci->irq, lat,
3286 (unsigned long long)pci_resource_start(dev, 0));
3289 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
3290 if (NULL == btv->bt848_mmio) {
3291 pr_err("%d: ioremap() failed\n", btv->c.nr);
3299 /* disable irqs, register irq handler */
3300 btwrite(0, BT848_INT_MASK);
3301 result = request_irq(btv->c.pci->irq, bttv_irq,
3302 IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
3304 pr_err("%d: can't get IRQ %d\n",
3305 bttv_num, btv->c.pci->irq);
3309 if (0 != bttv_handle_chipset(btv)) {
3314 /* init options from insmod args */
3315 btv->opt_combfilter = combfilter;
3316 bttv_ctrl_combfilter.def = combfilter;
3317 bttv_ctrl_lumafilter.def = lumafilter;
3318 btv->opt_automute = automute;
3319 bttv_ctrl_automute.def = automute;
3320 bttv_ctrl_agc_crush.def = agc_crush;
3321 btv->opt_vcr_hack = vcr_hack;
3322 bttv_ctrl_vcr_hack.def = vcr_hack;
3323 bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
3324 bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
3325 btv->opt_uv_ratio = uv_ratio;
3326 bttv_ctrl_uv_ratio.def = uv_ratio;
3327 bttv_ctrl_full_luma.def = full_luma_range;
3328 bttv_ctrl_coring.def = coring;
3330 /* fill struct bttv with some useful defaults */
3331 btv->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
3334 btv->field = V4L2_FIELD_INTERLACED;
3336 btv->tvnorm = 0; /* Index into bttv_tvnorms[] i.e. PAL. */
3337 bttv_vbi_fmt_reset(&btv->vbi_fmt, btv->tvnorm);
3338 btv->vbi_count[0] = VBI_DEFLINES;
3339 btv->vbi_count[1] = VBI_DEFLINES;
3342 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3343 V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
3344 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3345 V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
3346 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3347 V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
3348 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3349 V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
3350 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3351 V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
3352 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3353 V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
3354 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3355 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
3356 if (btv->volume_gpio)
3357 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3358 V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
3359 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
3360 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
3361 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
3362 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
3363 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
3364 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
3365 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
3366 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
3367 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
3368 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
3370 /* initialize hardware */
3372 bttv_gpio_tracking(btv,"pre-init");
3374 bttv_risc_init_main(btv);
3378 btwrite(0x00, BT848_GPIO_REG_INP);
3379 btwrite(0x00, BT848_GPIO_OUT_EN);
3381 bttv_gpio_tracking(btv,"init");
3383 /* needs to be done before i2c is registered */
3384 bttv_init_card1(btv);
3386 /* register i2c + gpio */
3389 /* some card-specific stuff (needs working i2c) */
3390 bttv_init_card2(btv);
3391 bttv_init_tuner(btv);
3392 if (btv->tuner_type != TUNER_ABSENT) {
3393 bttv_set_frequency(btv, &init_freq);
3394 btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
3396 btv->std = V4L2_STD_PAL;
3398 if (!bttv_tvcards[btv->c.type].no_video)
3399 v4l2_ctrl_handler_setup(hdl);
3401 result = hdl->error;
3407 /* register video4linux + input */
3408 if (!bttv_tvcards[btv->c.type].no_video) {
3409 v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
3410 v4l2_ctrl_radio_filter, false);
3411 if (btv->radio_ctrl_handler.error) {
3412 result = btv->radio_ctrl_handler.error;
3415 set_input(btv, btv->input, btv->tvnorm);
3416 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
3417 btv->crop[1] = btv->crop[0]; /* current = default */
3418 disclaim_vbi_lines(btv);
3419 disclaim_video_lines(btv);
3420 bttv_register_video(btv);
3423 /* add subdevices and autoload dvb-bt8xx if needed */
3424 if (bttv_tvcards[btv->c.type].has_dvb) {
3425 bttv_sub_add_device(&btv->c, "dvb");
3426 request_modules(btv);
3430 init_bttv_i2c_ir(btv);
3431 bttv_input_init(btv);
3434 /* everything is fine */
3439 free_irq(btv->c.pci->irq,btv);
3442 v4l2_ctrl_handler_free(&btv->ctrl_handler);
3443 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
3444 v4l2_device_unregister(&btv->c.v4l2_dev);
3447 if (btv->bt848_mmio)
3448 iounmap(btv->bt848_mmio);
3449 release_mem_region(pci_resource_start(btv->c.pci,0),
3450 pci_resource_len(btv->c.pci,0));
3451 pci_disable_device(btv->c.pci);
3454 bttvs[btv->c.nr] = NULL;
3459 static void bttv_remove(struct pci_dev *pci_dev)
3461 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
3462 struct bttv *btv = to_bttv(v4l2_dev);
3465 pr_info("%d: unloading\n", btv->c.nr);
3467 if (bttv_tvcards[btv->c.type].has_dvb)
3468 flush_request_modules(btv);
3470 /* shutdown everything (DMA+IRQs) */
3471 btand(~15, BT848_GPIO_DMA_CTL);
3472 btwrite(0, BT848_INT_MASK);
3473 btwrite(~0x0, BT848_INT_STAT);
3474 btwrite(0x0, BT848_GPIO_OUT_EN);
3476 bttv_gpio_tracking(btv,"cleanup");
3478 /* tell gpio modules we are leaving ... */
3480 bttv_input_fini(btv);
3481 bttv_sub_del_devices(&btv->c);
3483 /* unregister i2c_bus + input */
3486 /* unregister video4linux */
3487 bttv_unregister_video(btv);
3489 /* free allocated memory */
3490 v4l2_ctrl_handler_free(&btv->ctrl_handler);
3491 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
3492 btcx_riscmem_free(btv->c.pci,&btv->main);
3494 /* free resources */
3495 free_irq(btv->c.pci->irq,btv);
3496 del_timer_sync(&btv->timeout);
3497 iounmap(btv->bt848_mmio);
3498 release_mem_region(pci_resource_start(btv->c.pci,0),
3499 pci_resource_len(btv->c.pci,0));
3500 pci_disable_device(btv->c.pci);
3502 v4l2_device_unregister(&btv->c.v4l2_dev);
3503 bttvs[btv->c.nr] = NULL;
3509 static int __maybe_unused bttv_suspend(struct device *dev)
3511 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
3512 struct bttv *btv = to_bttv(v4l2_dev);
3513 struct bttv_buffer_set idle;
3514 unsigned long flags;
3516 dprintk("%d: suspend\n", btv->c.nr);
3518 /* stop dma + irqs */
3519 spin_lock_irqsave(&btv->s_lock,flags);
3520 memset(&idle, 0, sizeof(idle));
3521 btv->state.video = btv->curr;
3522 btv->state.vbi = btv->cvbi;
3523 btv->state.loop_irq = btv->loop_irq;
3526 bttv_buffer_activate_video(btv, &idle);
3527 bttv_buffer_activate_vbi(btv, NULL);
3528 bttv_set_dma(btv, 0);
3529 btwrite(0, BT848_INT_MASK);
3530 spin_unlock_irqrestore(&btv->s_lock,flags);
3532 /* save bt878 state */
3533 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
3534 btv->state.gpio_data = gpio_read();
3536 btv->state.disabled = 1;
3540 static int __maybe_unused bttv_resume(struct device *dev)
3542 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
3543 struct bttv *btv = to_bttv(v4l2_dev);
3544 unsigned long flags;
3546 dprintk("%d: resume\n", btv->c.nr);
3548 btv->state.disabled = 0;
3550 /* restore bt878 state */
3551 bttv_reinit_bt848(btv);
3552 gpio_inout(0xffffff, btv->state.gpio_enable);
3553 gpio_write(btv->state.gpio_data);
3556 spin_lock_irqsave(&btv->s_lock,flags);
3557 btv->curr = btv->state.video;
3558 btv->cvbi = btv->state.vbi;
3559 btv->loop_irq = btv->state.loop_irq;
3560 bttv_buffer_activate_video(btv, &btv->curr);
3561 bttv_buffer_activate_vbi(btv, btv->cvbi);
3562 bttv_set_dma(btv, 0);
3563 spin_unlock_irqrestore(&btv->s_lock,flags);
3567 static const struct pci_device_id bttv_pci_tbl[] = {
3568 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
3569 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
3570 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
3571 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
3572 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
3576 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
3578 static SIMPLE_DEV_PM_OPS(bttv_pm_ops,
3582 static struct pci_driver bttv_pci_driver = {
3584 .id_table = bttv_pci_tbl,
3585 .probe = bttv_probe,
3586 .remove = bttv_remove,
3587 .driver.pm = &bttv_pm_ops,
3590 static int __init bttv_init_module(void)
3596 pr_info("driver version %s loaded\n", BTTV_VERSION);
3597 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
3599 if (gbufsize > BTTV_MAX_FBUF)
3600 gbufsize = BTTV_MAX_FBUF;
3601 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
3603 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
3604 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
3606 bttv_check_chipset();
3608 ret = bus_register(&bttv_sub_bus_type);
3610 pr_warn("bus_register error: %d\n", ret);
3613 ret = pci_register_driver(&bttv_pci_driver);
3615 bus_unregister(&bttv_sub_bus_type);
3620 static void __exit bttv_cleanup_module(void)
3622 pci_unregister_driver(&bttv_pci_driver);
3623 bus_unregister(&bttv_sub_bus_type);
3626 module_init(bttv_init_module);
3627 module_exit(bttv_cleanup_module);