3 bttv - Bt848 frame grabber driver
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
6 & Marcus Metzler <mocm@thp.uni-koeln.de>
7 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
13 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
15 Fixes to be fully V4L2 compliant by
16 (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
18 Cropping and overscan support
19 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
20 Sponsored by OPQ Systems AB
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/delay.h>
42 #include <linux/slab.h>
43 #include <linux/errno.h>
45 #include <linux/kernel.h>
46 #include <linux/sched.h>
47 #include <linux/interrupt.h>
48 #include <linux/kdev_t.h>
50 #include <media/v4l2-common.h>
51 #include <media/v4l2-ioctl.h>
52 #include <media/v4l2-event.h>
53 #include <media/i2c/tvaudio.h>
54 #include <media/drv-intf/msp3400.h>
56 #include <linux/dma-mapping.h>
59 #include <asm/byteorder.h>
61 #include <media/i2c/saa6588.h>
63 #define BTTV_VERSION "0.9.19"
65 unsigned int bttv_num
; /* number of Bt848s in use */
66 struct bttv
*bttvs
[BTTV_MAX
];
68 unsigned int bttv_debug
;
69 unsigned int bttv_verbose
= 1;
70 unsigned int bttv_gpio
;
72 /* config variables */
74 static unsigned int bigendian
=1;
76 static unsigned int bigendian
;
78 static unsigned int radio
[BTTV_MAX
];
79 static unsigned int irq_debug
;
80 static unsigned int gbuffers
= 8;
81 static unsigned int gbufsize
= 0x208000;
82 static unsigned int reset_crop
= 1;
84 static int video_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
85 static int radio_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
86 static int vbi_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
87 static int debug_latency
;
88 static int disable_ir
;
90 static unsigned int fdsr
;
93 static unsigned int combfilter
;
94 static unsigned int lumafilter
;
95 static unsigned int automute
= 1;
96 static unsigned int chroma_agc
;
97 static unsigned int agc_crush
= 1;
98 static unsigned int whitecrush_upper
= 0xCF;
99 static unsigned int whitecrush_lower
= 0x7F;
100 static unsigned int vcr_hack
;
101 static unsigned int irq_iswitch
;
102 static unsigned int uv_ratio
= 50;
103 static unsigned int full_luma_range
;
104 static unsigned int coring
;
106 /* API features (turn on/off stuff for testing) */
107 static unsigned int v4l2
= 1;
110 module_param(bttv_verbose
, int, 0644);
111 module_param(bttv_gpio
, int, 0644);
112 module_param(bttv_debug
, int, 0644);
113 module_param(irq_debug
, int, 0644);
114 module_param(debug_latency
, int, 0644);
115 module_param(disable_ir
, int, 0444);
117 module_param(fdsr
, int, 0444);
118 module_param(gbuffers
, int, 0444);
119 module_param(gbufsize
, int, 0444);
120 module_param(reset_crop
, int, 0444);
122 module_param(v4l2
, int, 0644);
123 module_param(bigendian
, int, 0644);
124 module_param(irq_iswitch
, int, 0644);
125 module_param(combfilter
, int, 0444);
126 module_param(lumafilter
, int, 0444);
127 module_param(automute
, int, 0444);
128 module_param(chroma_agc
, int, 0444);
129 module_param(agc_crush
, int, 0444);
130 module_param(whitecrush_upper
, int, 0444);
131 module_param(whitecrush_lower
, int, 0444);
132 module_param(vcr_hack
, int, 0444);
133 module_param(uv_ratio
, int, 0444);
134 module_param(full_luma_range
, int, 0444);
135 module_param(coring
, int, 0444);
137 module_param_array(radio
, int, NULL
, 0444);
138 module_param_array(video_nr
, int, NULL
, 0444);
139 module_param_array(radio_nr
, int, NULL
, 0444);
140 module_param_array(vbi_nr
, int, NULL
, 0444);
142 MODULE_PARM_DESC(radio
, "The TV card supports radio, default is 0 (no)");
143 MODULE_PARM_DESC(bigendian
, "byte order of the framebuffer, default is native endian");
144 MODULE_PARM_DESC(bttv_verbose
, "verbose startup messages, default is 1 (yes)");
145 MODULE_PARM_DESC(bttv_gpio
, "log gpio changes, default is 0 (no)");
146 MODULE_PARM_DESC(bttv_debug
, "debug messages, default is 0 (no)");
147 MODULE_PARM_DESC(irq_debug
, "irq handler debug messages, default is 0 (no)");
148 MODULE_PARM_DESC(disable_ir
, "disable infrared remote support");
149 MODULE_PARM_DESC(gbuffers
, "number of capture buffers. range 2-32, default 8");
150 MODULE_PARM_DESC(gbufsize
, "size of the capture buffers, default is 0x208000");
151 MODULE_PARM_DESC(reset_crop
, "reset cropping parameters at open(), default is 1 (yes) for compatibility with older applications");
152 MODULE_PARM_DESC(automute
, "mute audio on bad/missing video signal, default is 1 (yes)");
153 MODULE_PARM_DESC(chroma_agc
, "enables the AGC of chroma signal, default is 0 (no)");
154 MODULE_PARM_DESC(agc_crush
, "enables the luminance AGC crush, default is 1 (yes)");
155 MODULE_PARM_DESC(whitecrush_upper
, "sets the white crush upper value, default is 207");
156 MODULE_PARM_DESC(whitecrush_lower
, "sets the white crush lower value, default is 127");
157 MODULE_PARM_DESC(vcr_hack
, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
158 MODULE_PARM_DESC(irq_iswitch
, "switch inputs in irq handler");
159 MODULE_PARM_DESC(uv_ratio
, "ratio between u and v gains, default is 50");
160 MODULE_PARM_DESC(full_luma_range
, "use the full luma range, default is 0 (no)");
161 MODULE_PARM_DESC(coring
, "set the luma coring level, default is 0 (no)");
162 MODULE_PARM_DESC(video_nr
, "video device numbers");
163 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
164 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
166 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
167 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
168 MODULE_LICENSE("GPL");
169 MODULE_VERSION(BTTV_VERSION
);
171 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
172 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
173 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
174 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
175 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
176 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
177 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
178 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
179 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
180 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
182 /* ----------------------------------------------------------------------- */
185 static ssize_t
show_card(struct device
*cd
,
186 struct device_attribute
*attr
, char *buf
)
188 struct video_device
*vfd
= to_video_device(cd
);
189 struct bttv
*btv
= video_get_drvdata(vfd
);
190 return sprintf(buf
, "%d\n", btv
? btv
->c
.type
: UNSET
);
192 static DEVICE_ATTR(card
, S_IRUGO
, show_card
, NULL
);
194 /* ----------------------------------------------------------------------- */
195 /* dvb auto-load setup */
196 #if defined(CONFIG_MODULES) && defined(MODULE)
197 static void request_module_async(struct work_struct
*work
)
199 request_module("dvb-bt8xx");
202 static void request_modules(struct bttv
*dev
)
204 INIT_WORK(&dev
->request_module_wk
, request_module_async
);
205 schedule_work(&dev
->request_module_wk
);
208 static void flush_request_modules(struct bttv
*dev
)
210 flush_work(&dev
->request_module_wk
);
213 #define request_modules(dev)
214 #define flush_request_modules(dev) do {} while(0)
215 #endif /* CONFIG_MODULES */
218 /* ----------------------------------------------------------------------- */
221 /* special timing tables from conexant... */
222 static u8 SRAM_Table
[][60] =
224 /* PAL digital input over GPIO[7:0] */
226 45, // 45 bytes following
227 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
228 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
229 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
230 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
231 0x37,0x00,0xAF,0x21,0x00
233 /* NTSC digital input over GPIO[7:0] */
235 51, // 51 bytes following
236 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
237 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
238 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
239 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
240 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
243 // TGB_NTSC392 // quartzsight
244 // This table has been modified to be used for Fusion Rev D
246 0x2A, // size of table = 42
247 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
248 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
249 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
250 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
255 /* minhdelayx1 first video pixel we can capture on a line and
256 hdelayx1 start of active video, both relative to rising edge of
257 /HRESET pulse (0H) in 1 / fCLKx1.
258 swidth width of active video and
259 totalwidth total line width, both in 1 / fCLKx1.
260 sqwidth total line width in square pixels.
261 vdelay start of active video in 2 * field lines relative to
262 trailing edge of /VRESET pulse (VDELAY register).
263 sheight height of active video in 2 * field lines.
264 extraheight Added to sheight for cropcap.bounds.height only
265 videostart0 ITU-R frame line number of the line corresponding
266 to vdelay in the first field. */
267 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
268 vdelay, sheight, extraheight, videostart0) \
269 .cropcap.bounds.left = minhdelayx1, \
270 /* * 2 because vertically we count field lines times two, */ \
271 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
272 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
273 /* 4 is a safety margin at the end of the line. */ \
274 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
275 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
277 .cropcap.defrect.left = hdelayx1, \
278 .cropcap.defrect.top = (videostart0) * 2, \
279 .cropcap.defrect.width = swidth, \
280 .cropcap.defrect.height = sheight, \
281 .cropcap.pixelaspect.numerator = totalwidth, \
282 .cropcap.pixelaspect.denominator = sqwidth,
284 const struct bttv_tvnorm bttv_tvnorms
[] = {
286 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
287 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
289 .v4l2_id
= V4L2_STD_PAL
,
297 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
298 .scaledtwidth
= 1135,
302 .vbipack
= 255, /* min (2048 / 4, 0x1ff) & 0xff */
304 /* ITU-R frame line number of the first VBI line
305 we can capture, of the first and second field.
306 The last line is determined by cropcap.bounds. */
307 .vbistart
= { 7, 320 },
308 CROPCAP(/* minhdelayx1 */ 68,
310 /* Should be (768 * 1135 + 944 / 2) / 944.
311 cropcap.defrect is used for image width
312 checks, so we keep the old value 924. */
314 /* totalwidth */ 1135,
318 /* bt878 (and bt848?) can capture another
319 line below active video. */
321 /* videostart0 */ 23)
323 .v4l2_id
= V4L2_STD_NTSC_M
| V4L2_STD_NTSC_M_KR
,
331 .iform
= (BT848_IFORM_NTSC
|BT848_IFORM_XT0
),
336 .vbipack
= 144, /* min (1600 / 4, 0x1ff) & 0xff */
338 .vbistart
= { 10, 273 },
339 CROPCAP(/* minhdelayx1 */ 68,
341 /* Should be (640 * 910 + 780 / 2) / 780? */
343 /* totalwidth */ 910,
348 /* videostart0 */ 23)
350 .v4l2_id
= V4L2_STD_SECAM
,
358 .iform
= (BT848_IFORM_SECAM
|BT848_IFORM_XT1
),
359 .scaledtwidth
= 1135,
364 .sram
= 0, /* like PAL, correct? */
365 .vbistart
= { 7, 320 },
366 CROPCAP(/* minhdelayx1 */ 68,
369 /* totalwidth */ 1135,
374 /* videostart0 */ 23)
376 .v4l2_id
= V4L2_STD_PAL_Nc
,
384 .iform
= (BT848_IFORM_PAL_NC
|BT848_IFORM_XT0
),
391 .vbistart
= { 7, 320 },
392 CROPCAP(/* minhdelayx1 */ 68,
394 /* swidth */ (640 * 910 + 780 / 2) / 780,
395 /* totalwidth */ 910,
400 /* videostart0 */ 23)
402 .v4l2_id
= V4L2_STD_PAL_M
,
410 .iform
= (BT848_IFORM_PAL_M
|BT848_IFORM_XT0
),
417 .vbistart
= { 10, 273 },
418 CROPCAP(/* minhdelayx1 */ 68,
420 /* swidth */ (640 * 910 + 780 / 2) / 780,
421 /* totalwidth */ 910,
426 /* videostart0 */ 23)
428 .v4l2_id
= V4L2_STD_PAL_N
,
436 .iform
= (BT848_IFORM_PAL_N
|BT848_IFORM_XT1
),
443 .vbistart
= { 7, 320 },
444 CROPCAP(/* minhdelayx1 */ 68,
446 /* swidth */ (768 * 1135 + 944 / 2) / 944,
447 /* totalwidth */ 1135,
452 /* videostart0 */ 23)
454 .v4l2_id
= V4L2_STD_NTSC_M_JP
,
462 .iform
= (BT848_IFORM_NTSC_J
|BT848_IFORM_XT0
),
469 .vbistart
= { 10, 273 },
470 CROPCAP(/* minhdelayx1 */ 68,
472 /* swidth */ (640 * 910 + 780 / 2) / 780,
473 /* totalwidth */ 910,
478 /* videostart0 */ 23)
480 /* that one hopefully works with the strange timing
481 * which video recorders produce when playing a NTSC
482 * tape on a PAL TV ... */
483 .v4l2_id
= V4L2_STD_PAL_60
,
491 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
492 .scaledtwidth
= 1135,
499 .vbistart
= { 10, 273 },
500 CROPCAP(/* minhdelayx1 */ 68,
503 /* totalwidth */ 1135,
508 /* videostart0 */ 23)
511 static const unsigned int BTTV_TVNORMS
= ARRAY_SIZE(bttv_tvnorms
);
513 /* ----------------------------------------------------------------------- */
515 packed pixel formats must come first */
516 static const struct bttv_format formats
[] = {
518 .name
= "8 bpp, gray",
519 .fourcc
= V4L2_PIX_FMT_GREY
,
520 .btformat
= BT848_COLOR_FMT_Y8
,
522 .flags
= FORMAT_FLAGS_PACKED
,
524 .name
= "8 bpp, dithered color",
525 .fourcc
= V4L2_PIX_FMT_HI240
,
526 .btformat
= BT848_COLOR_FMT_RGB8
,
528 .flags
= FORMAT_FLAGS_PACKED
| FORMAT_FLAGS_DITHER
,
530 .name
= "15 bpp RGB, le",
531 .fourcc
= V4L2_PIX_FMT_RGB555
,
532 .btformat
= BT848_COLOR_FMT_RGB15
,
534 .flags
= FORMAT_FLAGS_PACKED
,
536 .name
= "15 bpp RGB, be",
537 .fourcc
= V4L2_PIX_FMT_RGB555X
,
538 .btformat
= BT848_COLOR_FMT_RGB15
,
539 .btswap
= 0x03, /* byteswap */
541 .flags
= FORMAT_FLAGS_PACKED
,
543 .name
= "16 bpp RGB, le",
544 .fourcc
= V4L2_PIX_FMT_RGB565
,
545 .btformat
= BT848_COLOR_FMT_RGB16
,
547 .flags
= FORMAT_FLAGS_PACKED
,
549 .name
= "16 bpp RGB, be",
550 .fourcc
= V4L2_PIX_FMT_RGB565X
,
551 .btformat
= BT848_COLOR_FMT_RGB16
,
552 .btswap
= 0x03, /* byteswap */
554 .flags
= FORMAT_FLAGS_PACKED
,
556 .name
= "24 bpp RGB, le",
557 .fourcc
= V4L2_PIX_FMT_BGR24
,
558 .btformat
= BT848_COLOR_FMT_RGB24
,
560 .flags
= FORMAT_FLAGS_PACKED
,
562 .name
= "32 bpp RGB, le",
563 .fourcc
= V4L2_PIX_FMT_BGR32
,
564 .btformat
= BT848_COLOR_FMT_RGB32
,
566 .flags
= FORMAT_FLAGS_PACKED
,
568 .name
= "32 bpp RGB, be",
569 .fourcc
= V4L2_PIX_FMT_RGB32
,
570 .btformat
= BT848_COLOR_FMT_RGB32
,
571 .btswap
= 0x0f, /* byte+word swap */
573 .flags
= FORMAT_FLAGS_PACKED
,
575 .name
= "4:2:2, packed, YUYV",
576 .fourcc
= V4L2_PIX_FMT_YUYV
,
577 .btformat
= BT848_COLOR_FMT_YUY2
,
579 .flags
= FORMAT_FLAGS_PACKED
,
581 .name
= "4:2:2, packed, UYVY",
582 .fourcc
= V4L2_PIX_FMT_UYVY
,
583 .btformat
= BT848_COLOR_FMT_YUY2
,
584 .btswap
= 0x03, /* byteswap */
586 .flags
= FORMAT_FLAGS_PACKED
,
588 .name
= "4:2:2, planar, Y-Cb-Cr",
589 .fourcc
= V4L2_PIX_FMT_YUV422P
,
590 .btformat
= BT848_COLOR_FMT_YCrCb422
,
592 .flags
= FORMAT_FLAGS_PLANAR
,
596 .name
= "4:2:0, planar, Y-Cb-Cr",
597 .fourcc
= V4L2_PIX_FMT_YUV420
,
598 .btformat
= BT848_COLOR_FMT_YCrCb422
,
600 .flags
= FORMAT_FLAGS_PLANAR
,
604 .name
= "4:2:0, planar, Y-Cr-Cb",
605 .fourcc
= V4L2_PIX_FMT_YVU420
,
606 .btformat
= BT848_COLOR_FMT_YCrCb422
,
608 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
612 .name
= "4:1:1, planar, Y-Cb-Cr",
613 .fourcc
= V4L2_PIX_FMT_YUV411P
,
614 .btformat
= BT848_COLOR_FMT_YCrCb411
,
616 .flags
= FORMAT_FLAGS_PLANAR
,
620 .name
= "4:1:0, planar, Y-Cb-Cr",
621 .fourcc
= V4L2_PIX_FMT_YUV410
,
622 .btformat
= BT848_COLOR_FMT_YCrCb411
,
624 .flags
= FORMAT_FLAGS_PLANAR
,
628 .name
= "4:1:0, planar, Y-Cr-Cb",
629 .fourcc
= V4L2_PIX_FMT_YVU410
,
630 .btformat
= BT848_COLOR_FMT_YCrCb411
,
632 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
636 .name
= "raw scanlines",
638 .btformat
= BT848_COLOR_FMT_RAW
,
640 .flags
= FORMAT_FLAGS_RAW
,
643 static const unsigned int FORMATS
= ARRAY_SIZE(formats
);
645 /* ----------------------------------------------------------------------- */
646 /* resource management */
649 RESOURCE_ allocated by freed by
651 VIDEO_READ bttv_read 1) bttv_read 2)
653 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
654 VIDIOC_QBUF 1) bttv_release
657 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
658 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
661 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
662 VIDIOC_QBUF 1) bttv_release
663 bttv_read, bttv_poll 1) 4)
665 1) The resource must be allocated when we enter buffer prepare functions
666 and remain allocated while buffers are in the DMA queue.
667 2) This is a single frame read.
668 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
669 RESOURCE_OVERLAY is allocated.
670 4) This is a continuous read, implies VIDIOC_STREAMON.
672 Note this driver permits video input and standard changes regardless if
673 resources are allocated.
676 #define VBI_RESOURCES (RESOURCE_VBI)
677 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
678 RESOURCE_VIDEO_STREAM | \
682 int check_alloc_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bit
)
684 int xbits
; /* mutual exclusive resources */
686 if (fh
->resources
& bit
)
687 /* have it already allocated */
691 if (bit
& (RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
))
692 xbits
|= RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
;
695 if (btv
->resources
& xbits
) {
696 /* no, someone else uses it */
700 if ((bit
& VIDEO_RESOURCES
)
701 && 0 == (btv
->resources
& VIDEO_RESOURCES
)) {
702 /* Do crop - use current, don't - use default parameters. */
703 __s32 top
= btv
->crop
[!!fh
->do_crop
].rect
.top
;
705 if (btv
->vbi_end
> top
)
708 /* We cannot capture the same line as video and VBI data.
709 Claim scan lines crop[].rect.top to bottom. */
710 btv
->crop_start
= top
;
711 } else if (bit
& VBI_RESOURCES
) {
712 __s32 end
= fh
->vbi_fmt
.end
;
714 if (end
> btv
->crop_start
)
717 /* Claim scan lines above fh->vbi_fmt.end. */
721 /* it's free, grab it */
722 fh
->resources
|= bit
;
723 btv
->resources
|= bit
;
731 int check_btres(struct bttv_fh
*fh
, int bit
)
733 return (fh
->resources
& bit
);
737 int locked_btres(struct bttv
*btv
, int bit
)
739 return (btv
->resources
& bit
);
742 /* Call with btv->lock down. */
744 disclaim_vbi_lines(struct bttv
*btv
)
749 /* Call with btv->lock down. */
751 disclaim_video_lines(struct bttv
*btv
)
753 const struct bttv_tvnorm
*tvnorm
;
756 tvnorm
= &bttv_tvnorms
[btv
->tvnorm
];
757 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
758 + tvnorm
->cropcap
.bounds
.height
;
760 /* VBI capturing ends at VDELAY, start of video capturing, no
761 matter how many lines the VBI RISC program expects. When video
762 capturing is off, it shall no longer "preempt" VBI capturing,
763 so we set VDELAY to maximum. */
764 crop
= btread(BT848_E_CROP
) | 0xc0;
765 btwrite(crop
, BT848_E_CROP
);
766 btwrite(0xfe, BT848_E_VDELAY_LO
);
767 btwrite(crop
, BT848_O_CROP
);
768 btwrite(0xfe, BT848_O_VDELAY_LO
);
772 void free_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bits
)
774 if ((fh
->resources
& bits
) != bits
) {
775 /* trying to free resources not allocated by us ... */
776 pr_err("BUG! (btres)\n");
778 fh
->resources
&= ~bits
;
779 btv
->resources
&= ~bits
;
781 bits
= btv
->resources
;
783 if (0 == (bits
& VIDEO_RESOURCES
))
784 disclaim_video_lines(btv
);
786 if (0 == (bits
& VBI_RESOURCES
))
787 disclaim_vbi_lines(btv
);
790 /* ----------------------------------------------------------------------- */
791 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
793 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
794 PLL_X = Reference pre-divider (0=1, 1=2)
795 PLL_C = Post divider (0=6, 1=4)
796 PLL_I = Integer input
797 PLL_F = Fractional input
799 F_input = 28.636363 MHz:
800 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
803 static void set_pll_freq(struct bttv
*btv
, unsigned int fin
, unsigned int fout
)
805 unsigned char fl
, fh
, fi
;
807 /* prevent overflows */
820 btwrite(fl
, BT848_PLL_F_LO
);
821 btwrite(fh
, BT848_PLL_F_HI
);
822 btwrite(fi
|BT848_PLL_X
, BT848_PLL_XCI
);
825 static void set_pll(struct bttv
*btv
)
829 if (!btv
->pll
.pll_crystal
)
832 if (btv
->pll
.pll_ofreq
== btv
->pll
.pll_current
) {
833 dprintk("%d: PLL: no change required\n", btv
->c
.nr
);
837 if (btv
->pll
.pll_ifreq
== btv
->pll
.pll_ofreq
) {
839 if (btv
->pll
.pll_current
== 0)
842 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
843 btv
->c
.nr
, btv
->pll
.pll_ifreq
);
844 btwrite(0x00,BT848_TGCTRL
);
845 btwrite(0x00,BT848_PLL_XCI
);
846 btv
->pll
.pll_current
= 0;
851 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
853 btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
854 set_pll_freq(btv
, btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
856 for (i
=0; i
<10; i
++) {
857 /* Let other people run while the PLL stabilizes */
860 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_PLOCK
) {
861 btwrite(0,BT848_DSTATUS
);
863 btwrite(0x08,BT848_TGCTRL
);
864 btv
->pll
.pll_current
= btv
->pll
.pll_ofreq
;
866 pr_info("PLL set ok\n");
870 btv
->pll
.pll_current
= -1;
872 pr_info("Setting PLL failed\n");
876 /* used to switch between the bt848's analog/digital video capture modes */
877 static void bt848A_set_timing(struct bttv
*btv
)
880 int table_idx
= bttv_tvnorms
[btv
->tvnorm
].sram
;
881 int fsc
= bttv_tvnorms
[btv
->tvnorm
].Fsc
;
883 if (btv
->input
== btv
->dig
) {
884 dprintk("%d: load digital timing table (table_idx=%d)\n",
885 btv
->c
.nr
,table_idx
);
887 /* timing change...reset timing generator address */
888 btwrite(0x00, BT848_TGCTRL
);
889 btwrite(0x02, BT848_TGCTRL
);
890 btwrite(0x00, BT848_TGCTRL
);
892 len
=SRAM_Table
[table_idx
][0];
893 for(i
= 1; i
<= len
; i
++)
894 btwrite(SRAM_Table
[table_idx
][i
],BT848_TGLB
);
895 btv
->pll
.pll_ofreq
= 27000000;
898 btwrite(0x11, BT848_TGCTRL
);
899 btwrite(0x41, BT848_DVSIF
);
901 btv
->pll
.pll_ofreq
= fsc
;
903 btwrite(0x0, BT848_DVSIF
);
907 /* ----------------------------------------------------------------------- */
909 static void bt848_bright(struct bttv
*btv
, int bright
)
913 // printk("set bright: %d\n", bright); // DEBUG
914 btv
->bright
= bright
;
916 /* We want -128 to 127 we get 0-65535 */
917 value
= (bright
>> 8) - 128;
918 btwrite(value
& 0xff, BT848_BRIGHT
);
921 static void bt848_hue(struct bttv
*btv
, int hue
)
928 value
= (hue
>> 8) - 128;
929 btwrite(value
& 0xff, BT848_HUE
);
932 static void bt848_contrast(struct bttv
*btv
, int cont
)
936 btv
->contrast
= cont
;
940 hibit
= (value
>> 6) & 4;
941 btwrite(value
& 0xff, BT848_CONTRAST_LO
);
942 btaor(hibit
, ~4, BT848_E_CONTROL
);
943 btaor(hibit
, ~4, BT848_O_CONTROL
);
946 static void bt848_sat(struct bttv
*btv
, int color
)
948 int val_u
,val_v
,hibits
;
950 btv
->saturation
= color
;
952 /* 0-511 for the color */
953 val_u
= ((color
* btv
->opt_uv_ratio
) / 50) >> 7;
954 val_v
= (((color
* (100 - btv
->opt_uv_ratio
) / 50) >>7)*180L)/254;
955 hibits
= (val_u
>> 7) & 2;
956 hibits
|= (val_v
>> 8) & 1;
957 btwrite(val_u
& 0xff, BT848_SAT_U_LO
);
958 btwrite(val_v
& 0xff, BT848_SAT_V_LO
);
959 btaor(hibits
, ~3, BT848_E_CONTROL
);
960 btaor(hibits
, ~3, BT848_O_CONTROL
);
963 /* ----------------------------------------------------------------------- */
966 video_mux(struct bttv
*btv
, unsigned int input
)
970 if (input
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
973 /* needed by RemoteVideo MX */
974 mask2
= bttv_tvcards
[btv
->c
.type
].gpiomask2
;
976 gpio_inout(mask2
,mask2
);
978 if (input
== btv
->svhs
) {
979 btor(BT848_CONTROL_COMP
, BT848_E_CONTROL
);
980 btor(BT848_CONTROL_COMP
, BT848_O_CONTROL
);
982 btand(~BT848_CONTROL_COMP
, BT848_E_CONTROL
);
983 btand(~BT848_CONTROL_COMP
, BT848_O_CONTROL
);
985 mux
= bttv_muxsel(btv
, input
);
986 btaor(mux
<<5, ~(3<<5), BT848_IFORM
);
987 dprintk("%d: video mux: input=%d mux=%d\n", btv
->c
.nr
, input
, mux
);
989 /* card specific hook */
990 if(bttv_tvcards
[btv
->c
.type
].muxsel_hook
)
991 bttv_tvcards
[btv
->c
.type
].muxsel_hook (btv
, input
);
995 static char *audio_modes
[] = {
996 "audio: tuner", "audio: radio", "audio: extern",
997 "audio: intern", "audio: mute"
1001 audio_mux_gpio(struct bttv
*btv
, int input
, int mute
)
1003 int gpio_val
, signal
, mute_gpio
;
1005 gpio_inout(bttv_tvcards
[btv
->c
.type
].gpiomask
,
1006 bttv_tvcards
[btv
->c
.type
].gpiomask
);
1007 signal
= btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
;
1010 mute_gpio
= mute
|| (btv
->opt_automute
&& (!signal
|| !btv
->users
)
1011 && !btv
->has_radio_tuner
);
1014 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomute
;
1016 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomux
[input
];
1018 switch (btv
->c
.type
) {
1019 case BTTV_BOARD_VOODOOTV_FM
:
1020 case BTTV_BOARD_VOODOOTV_200
:
1021 gpio_val
= bttv_tda9880_setnorm(btv
, gpio_val
);
1025 gpio_bits(bttv_tvcards
[btv
->c
.type
].gpiomask
, gpio_val
);
1029 bttv_gpio_tracking(btv
, audio_modes
[mute_gpio
? 4 : input
]);
1033 audio_mute(struct bttv
*btv
, int mute
)
1035 struct v4l2_ctrl
*ctrl
;
1037 audio_mux_gpio(btv
, btv
->audio_input
, mute
);
1039 if (btv
->sd_msp34xx
) {
1040 ctrl
= v4l2_ctrl_find(btv
->sd_msp34xx
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1042 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1044 if (btv
->sd_tvaudio
) {
1045 ctrl
= v4l2_ctrl_find(btv
->sd_tvaudio
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1047 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1049 if (btv
->sd_tda7432
) {
1050 ctrl
= v4l2_ctrl_find(btv
->sd_tda7432
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1052 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1058 audio_input(struct bttv
*btv
, int input
)
1060 audio_mux_gpio(btv
, input
, btv
->mute
);
1062 if (btv
->sd_msp34xx
) {
1065 /* Note: the inputs tuner/radio/extern/intern are translated
1066 to msp routings. This assumes common behavior for all msp3400
1067 based TV cards. When this assumption fails, then the
1068 specific MSP routing must be added to the card table.
1069 For now this is sufficient. */
1071 case TVAUDIO_INPUT_RADIO
:
1072 /* Some boards need the msp do to the radio demod */
1073 if (btv
->radio_uses_msp_demodulator
) {
1074 in
= MSP_INPUT_DEFAULT
;
1077 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1078 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1080 case TVAUDIO_INPUT_EXTERN
:
1081 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER1
,
1082 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1084 case TVAUDIO_INPUT_INTERN
:
1085 /* Yes, this is the same input as for RADIO. I doubt
1086 if this is ever used. The only board with an INTERN
1087 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1088 that was tested. My guess is that the whole INTERN
1089 input does not work. */
1090 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1091 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1093 case TVAUDIO_INPUT_TUNER
:
1095 /* This is the only card that uses TUNER2, and afaik,
1096 is the only difference between the VOODOOTV_FM
1098 if (btv
->c
.type
== BTTV_BOARD_VOODOOTV_200
)
1099 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER2
, \
1100 MSP_DSP_IN_TUNER
, MSP_DSP_IN_TUNER
);
1102 in
= MSP_INPUT_DEFAULT
;
1105 v4l2_subdev_call(btv
->sd_msp34xx
, audio
, s_routing
,
1106 in
, MSP_OUTPUT_DEFAULT
, 0);
1108 if (btv
->sd_tvaudio
) {
1109 v4l2_subdev_call(btv
->sd_tvaudio
, audio
, s_routing
,
1116 bttv_crop_calc_limits(struct bttv_crop
*c
)
1118 /* Scale factor min. 1:1, max. 16:1. Min. image size
1119 48 x 32. Scaled width must be a multiple of 4. */
1122 /* For bug compatibility with VIDIOCGCAP and image
1123 size checks in earlier driver versions. */
1124 c
->min_scaled_width
= 48;
1125 c
->min_scaled_height
= 32;
1127 c
->min_scaled_width
=
1128 (max_t(unsigned int, 48, c
->rect
.width
>> 4) + 3) & ~3;
1129 c
->min_scaled_height
=
1130 max_t(unsigned int, 32, c
->rect
.height
>> 4);
1133 c
->max_scaled_width
= c
->rect
.width
& ~3;
1134 c
->max_scaled_height
= c
->rect
.height
;
1138 bttv_crop_reset(struct bttv_crop
*c
, unsigned int norm
)
1140 c
->rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1141 bttv_crop_calc_limits(c
);
1144 /* Call with btv->lock down. */
1146 set_tvnorm(struct bttv
*btv
, unsigned int norm
)
1148 const struct bttv_tvnorm
*tvnorm
;
1151 BUG_ON(norm
>= BTTV_TVNORMS
);
1152 BUG_ON(btv
->tvnorm
>= BTTV_TVNORMS
);
1154 tvnorm
= &bttv_tvnorms
[norm
];
1156 if (memcmp(&bttv_tvnorms
[btv
->tvnorm
].cropcap
, &tvnorm
->cropcap
,
1157 sizeof (tvnorm
->cropcap
))) {
1158 bttv_crop_reset(&btv
->crop
[0], norm
);
1159 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
1161 if (0 == (btv
->resources
& VIDEO_RESOURCES
)) {
1162 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
1163 + tvnorm
->cropcap
.bounds
.height
;
1169 btwrite(tvnorm
->adelay
, BT848_ADELAY
);
1170 btwrite(tvnorm
->bdelay
, BT848_BDELAY
);
1171 btaor(tvnorm
->iform
,~(BT848_IFORM_NORM
|BT848_IFORM_XTBOTH
),
1173 btwrite(tvnorm
->vbipack
, BT848_VBI_PACK_SIZE
);
1174 btwrite(1, BT848_VBI_PACK_DEL
);
1175 bt848A_set_timing(btv
);
1177 switch (btv
->c
.type
) {
1178 case BTTV_BOARD_VOODOOTV_FM
:
1179 case BTTV_BOARD_VOODOOTV_200
:
1180 bttv_tda9880_setnorm(btv
, gpio_read());
1183 id
= tvnorm
->v4l2_id
;
1184 bttv_call_all(btv
, video
, s_std
, id
);
1189 /* Call with btv->lock down. */
1191 set_input(struct bttv
*btv
, unsigned int input
, unsigned int norm
)
1193 unsigned long flags
;
1197 spin_lock_irqsave(&btv
->s_lock
,flags
);
1198 if (btv
->curr
.frame_irq
) {
1199 /* active capture -> delayed input switch */
1200 btv
->new_input
= input
;
1202 video_mux(btv
,input
);
1204 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1206 video_mux(btv
,input
);
1208 btv
->audio_input
= (btv
->tuner_type
!= TUNER_ABSENT
&& input
== 0) ?
1209 TVAUDIO_INPUT_TUNER
: TVAUDIO_INPUT_EXTERN
;
1210 audio_input(btv
, btv
->audio_input
);
1211 set_tvnorm(btv
, norm
);
1214 static void init_irqreg(struct bttv
*btv
)
1217 btwrite(0xfffffUL
, BT848_INT_STAT
);
1219 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1221 btwrite(BT848_INT_I2CDONE
,
1225 btwrite((btv
->triton1
) |
1226 (btv
->gpioirq
? BT848_INT_GPINT
: 0) |
1228 (fdsr
? BT848_INT_FDSR
: 0) |
1229 BT848_INT_RISCI
| BT848_INT_OCERR
|
1230 BT848_INT_FMTCHG
|BT848_INT_HLOCK
|
1236 static void init_bt848(struct bttv
*btv
)
1238 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1239 /* very basic init only */
1244 btwrite(0x00, BT848_CAP_CTL
);
1245 btwrite(BT848_COLOR_CTL_GAMMA
, BT848_COLOR_CTL
);
1246 btwrite(BT848_IFORM_XTAUTO
| BT848_IFORM_AUTO
, BT848_IFORM
);
1248 /* set planar and packed mode trigger points and */
1249 /* set rising edge of inverted GPINTR pin as irq trigger */
1250 btwrite(BT848_GPIO_DMA_CTL_PKTP_32
|
1251 BT848_GPIO_DMA_CTL_PLTP1_16
|
1252 BT848_GPIO_DMA_CTL_PLTP23_16
|
1253 BT848_GPIO_DMA_CTL_GPINTC
|
1254 BT848_GPIO_DMA_CTL_GPINTI
,
1255 BT848_GPIO_DMA_CTL
);
1257 btwrite(0x20, BT848_E_VSCALE_HI
);
1258 btwrite(0x20, BT848_O_VSCALE_HI
);
1260 v4l2_ctrl_handler_setup(&btv
->ctrl_handler
);
1266 static void bttv_reinit_bt848(struct bttv
*btv
)
1268 unsigned long flags
;
1271 pr_info("%d: reset, reinitialize\n", btv
->c
.nr
);
1272 spin_lock_irqsave(&btv
->s_lock
,flags
);
1274 bttv_set_dma(btv
,0);
1275 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1278 btv
->pll
.pll_current
= -1;
1279 set_input(btv
, btv
->input
, btv
->tvnorm
);
1282 static int bttv_s_ctrl(struct v4l2_ctrl
*c
)
1284 struct bttv
*btv
= container_of(c
->handler
, struct bttv
, ctrl_handler
);
1288 case V4L2_CID_BRIGHTNESS
:
1289 bt848_bright(btv
, c
->val
);
1292 bt848_hue(btv
, c
->val
);
1294 case V4L2_CID_CONTRAST
:
1295 bt848_contrast(btv
, c
->val
);
1297 case V4L2_CID_SATURATION
:
1298 bt848_sat(btv
, c
->val
);
1300 case V4L2_CID_COLOR_KILLER
:
1302 btor(BT848_SCLOOP_CKILL
, BT848_E_SCLOOP
);
1303 btor(BT848_SCLOOP_CKILL
, BT848_O_SCLOOP
);
1305 btand(~BT848_SCLOOP_CKILL
, BT848_E_SCLOOP
);
1306 btand(~BT848_SCLOOP_CKILL
, BT848_O_SCLOOP
);
1309 case V4L2_CID_AUDIO_MUTE
:
1310 audio_mute(btv
, c
->val
);
1313 case V4L2_CID_AUDIO_VOLUME
:
1314 btv
->volume_gpio(btv
, c
->val
);
1317 case V4L2_CID_CHROMA_AGC
:
1318 val
= c
->val
? BT848_SCLOOP_CAGC
: 0;
1319 btwrite(val
, BT848_E_SCLOOP
);
1320 btwrite(val
, BT848_O_SCLOOP
);
1322 case V4L2_CID_PRIVATE_COMBFILTER
:
1323 btv
->opt_combfilter
= c
->val
;
1325 case V4L2_CID_PRIVATE_LUMAFILTER
:
1327 btand(~BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1328 btand(~BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1330 btor(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1331 btor(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1334 case V4L2_CID_PRIVATE_AUTOMUTE
:
1335 btv
->opt_automute
= c
->val
;
1337 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1338 btwrite(BT848_ADC_RESERVED
|
1339 (c
->val
? BT848_ADC_CRUSH
: 0),
1342 case V4L2_CID_PRIVATE_VCR_HACK
:
1343 btv
->opt_vcr_hack
= c
->val
;
1345 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1346 btwrite(c
->val
, BT848_WC_UP
);
1348 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1349 btwrite(c
->val
, BT848_WC_DOWN
);
1351 case V4L2_CID_PRIVATE_UV_RATIO
:
1352 btv
->opt_uv_ratio
= c
->val
;
1353 bt848_sat(btv
, btv
->saturation
);
1355 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1356 btaor((c
->val
<< 7), ~BT848_OFORM_RANGE
, BT848_OFORM
);
1358 case V4L2_CID_PRIVATE_CORING
:
1359 btaor((c
->val
<< 5), ~BT848_OFORM_CORE32
, BT848_OFORM
);
1367 /* ----------------------------------------------------------------------- */
1369 static const struct v4l2_ctrl_ops bttv_ctrl_ops
= {
1370 .s_ctrl
= bttv_s_ctrl
,
1373 static struct v4l2_ctrl_config bttv_ctrl_combfilter
= {
1374 .ops
= &bttv_ctrl_ops
,
1375 .id
= V4L2_CID_PRIVATE_COMBFILTER
,
1376 .name
= "Comb Filter",
1377 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1384 static struct v4l2_ctrl_config bttv_ctrl_automute
= {
1385 .ops
= &bttv_ctrl_ops
,
1386 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
1387 .name
= "Auto Mute",
1388 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1395 static struct v4l2_ctrl_config bttv_ctrl_lumafilter
= {
1396 .ops
= &bttv_ctrl_ops
,
1397 .id
= V4L2_CID_PRIVATE_LUMAFILTER
,
1398 .name
= "Luma Decimation Filter",
1399 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1406 static struct v4l2_ctrl_config bttv_ctrl_agc_crush
= {
1407 .ops
= &bttv_ctrl_ops
,
1408 .id
= V4L2_CID_PRIVATE_AGC_CRUSH
,
1409 .name
= "AGC Crush",
1410 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1417 static struct v4l2_ctrl_config bttv_ctrl_vcr_hack
= {
1418 .ops
= &bttv_ctrl_ops
,
1419 .id
= V4L2_CID_PRIVATE_VCR_HACK
,
1421 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1428 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower
= {
1429 .ops
= &bttv_ctrl_ops
,
1430 .id
= V4L2_CID_PRIVATE_WHITECRUSH_LOWER
,
1431 .name
= "Whitecrush Lower",
1432 .type
= V4L2_CTRL_TYPE_INTEGER
,
1439 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper
= {
1440 .ops
= &bttv_ctrl_ops
,
1441 .id
= V4L2_CID_PRIVATE_WHITECRUSH_UPPER
,
1442 .name
= "Whitecrush Upper",
1443 .type
= V4L2_CTRL_TYPE_INTEGER
,
1450 static struct v4l2_ctrl_config bttv_ctrl_uv_ratio
= {
1451 .ops
= &bttv_ctrl_ops
,
1452 .id
= V4L2_CID_PRIVATE_UV_RATIO
,
1454 .type
= V4L2_CTRL_TYPE_INTEGER
,
1461 static struct v4l2_ctrl_config bttv_ctrl_full_luma
= {
1462 .ops
= &bttv_ctrl_ops
,
1463 .id
= V4L2_CID_PRIVATE_FULL_LUMA_RANGE
,
1464 .name
= "Full Luma Range",
1465 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1471 static struct v4l2_ctrl_config bttv_ctrl_coring
= {
1472 .ops
= &bttv_ctrl_ops
,
1473 .id
= V4L2_CID_PRIVATE_CORING
,
1475 .type
= V4L2_CTRL_TYPE_INTEGER
,
1482 /* ----------------------------------------------------------------------- */
1484 void bttv_gpio_tracking(struct bttv
*btv
, char *comment
)
1486 unsigned int outbits
, data
;
1487 outbits
= btread(BT848_GPIO_OUT_EN
);
1488 data
= btread(BT848_GPIO_DATA
);
1489 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1490 btv
->c
.nr
, outbits
, data
& outbits
, data
& ~outbits
, comment
);
1493 static void bttv_field_count(struct bttv
*btv
)
1501 /* start field counter */
1502 btor(BT848_INT_VSYNC
,BT848_INT_MASK
);
1504 /* stop field counter */
1505 btand(~BT848_INT_VSYNC
,BT848_INT_MASK
);
1506 btv
->field_count
= 0;
1510 static const struct bttv_format
*
1511 format_by_fourcc(int fourcc
)
1515 for (i
= 0; i
< FORMATS
; i
++) {
1516 if (-1 == formats
[i
].fourcc
)
1518 if (formats
[i
].fourcc
== fourcc
)
1524 /* ----------------------------------------------------------------------- */
1528 bttv_switch_overlay(struct bttv
*btv
, struct bttv_fh
*fh
,
1529 struct bttv_buffer
*new)
1531 struct bttv_buffer
*old
;
1532 unsigned long flags
;
1534 dprintk("switch_overlay: enter [new=%p]\n", new);
1536 new->vb
.state
= VIDEOBUF_DONE
;
1537 spin_lock_irqsave(&btv
->s_lock
,flags
);
1541 bttv_set_dma(btv
, 0x03);
1542 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1544 dprintk("switch_overlay: old=%p state is %d\n",
1545 old
, old
->vb
.state
);
1546 bttv_dma_free(&fh
->cap
,btv
, old
);
1550 free_btres_lock(btv
,fh
,RESOURCE_OVERLAY
);
1551 dprintk("switch_overlay: done\n");
1555 /* ----------------------------------------------------------------------- */
1556 /* video4linux (1) interface */
1558 static int bttv_prepare_buffer(struct videobuf_queue
*q
,struct bttv
*btv
,
1559 struct bttv_buffer
*buf
,
1560 const struct bttv_format
*fmt
,
1561 unsigned int width
, unsigned int height
,
1562 enum v4l2_field field
)
1564 struct bttv_fh
*fh
= q
->priv_data
;
1565 int redo_dma_risc
= 0;
1570 /* check settings */
1573 if (fmt
->btformat
== BT848_COLOR_FMT_RAW
) {
1575 height
= RAW_LINES
*2;
1576 if (width
*height
> buf
->vb
.bsize
)
1578 buf
->vb
.size
= buf
->vb
.bsize
;
1580 /* Make sure tvnorm and vbi_end remain consistent
1581 until we're done. */
1585 /* In this mode capturing always starts at defrect.top
1586 (default VDELAY), ignoring cropping parameters. */
1587 if (btv
->vbi_end
> bttv_tvnorms
[norm
].cropcap
.defrect
.top
) {
1591 c
.rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1594 c
= btv
->crop
[!!fh
->do_crop
];
1596 if (width
< c
.min_scaled_width
||
1597 width
> c
.max_scaled_width
||
1598 height
< c
.min_scaled_height
)
1602 case V4L2_FIELD_TOP
:
1603 case V4L2_FIELD_BOTTOM
:
1604 case V4L2_FIELD_ALTERNATE
:
1605 /* btv->crop counts frame lines. Max. scale
1606 factor is 16:1 for frames, 8:1 for fields. */
1607 if (height
* 2 > c
.max_scaled_height
)
1612 if (height
> c
.max_scaled_height
)
1617 buf
->vb
.size
= (width
* height
* fmt
->depth
) >> 3;
1618 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
1622 /* alloc + fill struct bttv_buffer (if changed) */
1623 if (buf
->vb
.width
!= width
|| buf
->vb
.height
!= height
||
1624 buf
->vb
.field
!= field
||
1625 buf
->tvnorm
!= norm
|| buf
->fmt
!= fmt
||
1626 buf
->crop
.top
!= c
.rect
.top
||
1627 buf
->crop
.left
!= c
.rect
.left
||
1628 buf
->crop
.width
!= c
.rect
.width
||
1629 buf
->crop
.height
!= c
.rect
.height
) {
1630 buf
->vb
.width
= width
;
1631 buf
->vb
.height
= height
;
1632 buf
->vb
.field
= field
;
1639 /* alloc risc memory */
1640 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1642 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,&btv
->fbuf
)))
1647 if (0 != (rc
= bttv_buffer_risc(btv
,buf
)))
1650 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1654 bttv_dma_free(q
,btv
,buf
);
1659 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1661 struct bttv_fh
*fh
= q
->priv_data
;
1663 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
1666 if (*size
* *count
> gbuffers
* gbufsize
)
1667 *count
= (gbuffers
* gbufsize
) / *size
;
1672 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
1673 enum v4l2_field field
)
1675 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1676 struct bttv_fh
*fh
= q
->priv_data
;
1678 return bttv_prepare_buffer(q
,fh
->btv
, buf
, fh
->fmt
,
1679 fh
->width
, fh
->height
, field
);
1683 buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1685 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1686 struct bttv_fh
*fh
= q
->priv_data
;
1687 struct bttv
*btv
= fh
->btv
;
1689 buf
->vb
.state
= VIDEOBUF_QUEUED
;
1690 list_add_tail(&buf
->vb
.queue
,&btv
->capture
);
1691 if (!btv
->curr
.frame_irq
) {
1693 bttv_set_dma(btv
, 0x03);
1697 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1699 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1700 struct bttv_fh
*fh
= q
->priv_data
;
1702 bttv_dma_free(q
,fh
->btv
,buf
);
1705 static struct videobuf_queue_ops bttv_video_qops
= {
1706 .buf_setup
= buffer_setup
,
1707 .buf_prepare
= buffer_prepare
,
1708 .buf_queue
= buffer_queue
,
1709 .buf_release
= buffer_release
,
1712 static void radio_enable(struct bttv
*btv
)
1714 /* Switch to the radio tuner */
1715 if (!btv
->has_radio_tuner
) {
1716 btv
->has_radio_tuner
= 1;
1717 bttv_call_all(btv
, tuner
, s_radio
);
1718 btv
->audio_input
= TVAUDIO_INPUT_RADIO
;
1719 audio_input(btv
, btv
->audio_input
);
1723 static int bttv_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1725 struct bttv_fh
*fh
= priv
;
1726 struct bttv
*btv
= fh
->btv
;
1729 for (i
= 0; i
< BTTV_TVNORMS
; i
++)
1730 if (id
& bttv_tvnorms
[i
].v4l2_id
)
1732 if (i
== BTTV_TVNORMS
)
1739 static int bttv_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1741 struct bttv_fh
*fh
= priv
;
1742 struct bttv
*btv
= fh
->btv
;
1748 static int bttv_querystd(struct file
*file
, void *f
, v4l2_std_id
*id
)
1750 struct bttv_fh
*fh
= f
;
1751 struct bttv
*btv
= fh
->btv
;
1753 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_NUML
)
1754 *id
&= V4L2_STD_625_50
;
1756 *id
&= V4L2_STD_525_60
;
1760 static int bttv_enum_input(struct file
*file
, void *priv
,
1761 struct v4l2_input
*i
)
1763 struct bttv_fh
*fh
= priv
;
1764 struct bttv
*btv
= fh
->btv
;
1766 if (i
->index
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1769 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1772 if (btv
->tuner_type
!= TUNER_ABSENT
&& i
->index
== 0) {
1773 sprintf(i
->name
, "Television");
1774 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1776 } else if (i
->index
== btv
->svhs
) {
1777 sprintf(i
->name
, "S-Video");
1779 sprintf(i
->name
, "Composite%d", i
->index
);
1782 if (i
->index
== btv
->input
) {
1783 __u32 dstatus
= btread(BT848_DSTATUS
);
1784 if (0 == (dstatus
& BT848_DSTATUS_PRES
))
1785 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1786 if (0 == (dstatus
& BT848_DSTATUS_HLOC
))
1787 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1790 i
->std
= BTTV_NORMS
;
1794 static int bttv_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1796 struct bttv_fh
*fh
= priv
;
1797 struct bttv
*btv
= fh
->btv
;
1804 static int bttv_s_input(struct file
*file
, void *priv
, unsigned int i
)
1806 struct bttv_fh
*fh
= priv
;
1807 struct bttv
*btv
= fh
->btv
;
1809 if (i
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1812 set_input(btv
, i
, btv
->tvnorm
);
1816 static int bttv_s_tuner(struct file
*file
, void *priv
,
1817 const struct v4l2_tuner
*t
)
1819 struct bttv_fh
*fh
= priv
;
1820 struct bttv
*btv
= fh
->btv
;
1825 bttv_call_all(btv
, tuner
, s_tuner
, t
);
1827 if (btv
->audio_mode_gpio
) {
1828 struct v4l2_tuner copy
= *t
;
1830 btv
->audio_mode_gpio(btv
, ©
, 1);
1835 static int bttv_g_frequency(struct file
*file
, void *priv
,
1836 struct v4l2_frequency
*f
)
1838 struct bttv_fh
*fh
= priv
;
1839 struct bttv
*btv
= fh
->btv
;
1844 if (f
->type
== V4L2_TUNER_RADIO
)
1846 f
->frequency
= f
->type
== V4L2_TUNER_RADIO
?
1847 btv
->radio_freq
: btv
->tv_freq
;
1852 static void bttv_set_frequency(struct bttv
*btv
, const struct v4l2_frequency
*f
)
1854 struct v4l2_frequency new_freq
= *f
;
1856 bttv_call_all(btv
, tuner
, s_frequency
, f
);
1857 /* s_frequency may clamp the frequency, so get the actual
1858 frequency before assigning radio/tv_freq. */
1859 bttv_call_all(btv
, tuner
, g_frequency
, &new_freq
);
1860 if (new_freq
.type
== V4L2_TUNER_RADIO
) {
1862 btv
->radio_freq
= new_freq
.frequency
;
1863 if (btv
->has_tea575x
) {
1864 btv
->tea
.freq
= btv
->radio_freq
;
1865 snd_tea575x_set_freq(&btv
->tea
);
1868 btv
->tv_freq
= new_freq
.frequency
;
1872 static int bttv_s_frequency(struct file
*file
, void *priv
,
1873 const struct v4l2_frequency
*f
)
1875 struct bttv_fh
*fh
= priv
;
1876 struct bttv
*btv
= fh
->btv
;
1881 bttv_set_frequency(btv
, f
);
1885 static int bttv_log_status(struct file
*file
, void *f
)
1887 struct video_device
*vdev
= video_devdata(file
);
1888 struct bttv_fh
*fh
= f
;
1889 struct bttv
*btv
= fh
->btv
;
1891 v4l2_ctrl_handler_log_status(vdev
->ctrl_handler
, btv
->c
.v4l2_dev
.name
);
1892 bttv_call_all(btv
, core
, log_status
);
1896 #ifdef CONFIG_VIDEO_ADV_DEBUG
1897 static int bttv_g_register(struct file
*file
, void *f
,
1898 struct v4l2_dbg_register
*reg
)
1900 struct bttv_fh
*fh
= f
;
1901 struct bttv
*btv
= fh
->btv
;
1903 /* bt848 has a 12-bit register space */
1905 reg
->val
= btread(reg
->reg
);
1911 static int bttv_s_register(struct file
*file
, void *f
,
1912 const struct v4l2_dbg_register
*reg
)
1914 struct bttv_fh
*fh
= f
;
1915 struct bttv
*btv
= fh
->btv
;
1917 /* bt848 has a 12-bit register space */
1918 btwrite(reg
->val
, reg
->reg
& 0xfff);
1924 /* Given cropping boundaries b and the scaled width and height of a
1925 single field or frame, which must not exceed hardware limits, this
1926 function adjusts the cropping parameters c. */
1928 bttv_crop_adjust (struct bttv_crop
* c
,
1929 const struct v4l2_rect
* b
,
1932 enum v4l2_field field
)
1934 __s32 frame_height
= height
<< !V4L2_FIELD_HAS_BOTH(field
);
1938 if (width
< c
->min_scaled_width
) {
1939 /* Max. hor. scale factor 16:1. */
1940 c
->rect
.width
= width
* 16;
1941 } else if (width
> c
->max_scaled_width
) {
1942 /* Min. hor. scale factor 1:1. */
1943 c
->rect
.width
= width
;
1945 max_left
= b
->left
+ b
->width
- width
;
1946 max_left
= min(max_left
, (__s32
) MAX_HDELAY
);
1947 if (c
->rect
.left
> max_left
)
1948 c
->rect
.left
= max_left
;
1951 if (height
< c
->min_scaled_height
) {
1952 /* Max. vert. scale factor 16:1, single fields 8:1. */
1953 c
->rect
.height
= height
* 16;
1954 } else if (frame_height
> c
->max_scaled_height
) {
1955 /* Min. vert. scale factor 1:1.
1956 Top and height count field lines times two. */
1957 c
->rect
.height
= (frame_height
+ 1) & ~1;
1959 max_top
= b
->top
+ b
->height
- c
->rect
.height
;
1960 if (c
->rect
.top
> max_top
)
1961 c
->rect
.top
= max_top
;
1964 bttv_crop_calc_limits(c
);
1967 /* Returns an error if scaling to a frame or single field with the given
1968 width and height is not possible with the current cropping parameters
1969 and width aligned according to width_mask. If adjust_size is TRUE the
1970 function may adjust the width and/or height instead, rounding width
1971 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
1972 also adjust the current cropping parameters to get closer to the
1973 desired image size. */
1975 limit_scaled_size_lock (struct bttv_fh
* fh
,
1978 enum v4l2_field field
,
1979 unsigned int width_mask
,
1980 unsigned int width_bias
,
1984 struct bttv
*btv
= fh
->btv
;
1985 const struct v4l2_rect
*b
;
1986 struct bttv_crop
*c
;
1993 BUG_ON((int) width_mask
>= 0 ||
1994 width_bias
>= (unsigned int) -width_mask
);
1996 /* Make sure tvnorm, vbi_end and the current cropping parameters
1997 remain consistent until we're done. */
1999 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2001 /* Do crop - use current, don't - use default parameters. */
2002 c
= &btv
->crop
[!!fh
->do_crop
];
2007 && !locked_btres(btv
, VIDEO_RESOURCES
)) {
2011 /* We cannot scale up. When the scaled image is larger
2012 than crop.rect we adjust the crop.rect as required
2013 by the V4L2 spec, hence cropcap.bounds are our limit. */
2014 max_width
= min_t(unsigned int, b
->width
, MAX_HACTIVE
);
2015 max_height
= b
->height
;
2017 /* We cannot capture the same line as video and VBI data.
2018 Note btv->vbi_end is really a minimum, see
2019 bttv_vbi_try_fmt(). */
2020 if (btv
->vbi_end
> b
->top
) {
2021 max_height
-= btv
->vbi_end
- b
->top
;
2023 if (min_height
> max_height
)
2028 if (btv
->vbi_end
> c
->rect
.top
)
2031 min_width
= c
->min_scaled_width
;
2032 min_height
= c
->min_scaled_height
;
2033 max_width
= c
->max_scaled_width
;
2034 max_height
= c
->max_scaled_height
;
2039 min_width
= (min_width
- width_mask
- 1) & width_mask
;
2040 max_width
= max_width
& width_mask
;
2042 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2043 min_height
= min_height
;
2044 /* Min. scale factor is 1:1. */
2045 max_height
>>= !V4L2_FIELD_HAS_BOTH(field
);
2048 *width
= clamp(*width
, min_width
, max_width
);
2049 *height
= clamp(*height
, min_height
, max_height
);
2051 /* Round after clamping to avoid overflow. */
2052 *width
= (*width
+ width_bias
) & width_mask
;
2055 bttv_crop_adjust(c
, b
, *width
, *height
, field
);
2057 if (btv
->vbi_end
> c
->rect
.top
) {
2058 /* Move the crop window out of the way. */
2059 c
->rect
.top
= btv
->vbi_end
;
2064 if (*width
< min_width
||
2065 *height
< min_height
||
2066 *width
> max_width
||
2067 *height
> max_height
||
2068 0 != (*width
& ~width_mask
))
2072 rc
= 0; /* success */
2079 /* Returns an error if the given overlay window dimensions are not
2080 possible with the current cropping parameters. If adjust_size is
2081 TRUE the function may adjust the window width and/or height
2082 instead, however it always rounds the horizontal position and
2083 width as btcx_align() does. If adjust_crop is TRUE the function
2084 may also adjust the current cropping parameters to get closer
2085 to the desired window size. */
2087 verify_window_lock(struct bttv_fh
*fh
, struct v4l2_window
*win
,
2088 int adjust_size
, int adjust_crop
)
2090 enum v4l2_field field
;
2091 unsigned int width_mask
;
2094 if (win
->w
.width
< 48)
2096 if (win
->w
.height
< 32)
2098 if (win
->clipcount
> 2048)
2099 win
->clipcount
= 2048;
2102 win
->global_alpha
= 0;
2106 case V4L2_FIELD_TOP
:
2107 case V4L2_FIELD_BOTTOM
:
2108 case V4L2_FIELD_INTERLACED
:
2111 field
= V4L2_FIELD_ANY
;
2114 if (V4L2_FIELD_ANY
== field
) {
2117 height2
= fh
->btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2118 field
= (win
->w
.height
> height2
)
2119 ? V4L2_FIELD_INTERLACED
2124 if (NULL
== fh
->ovfmt
)
2126 /* 4-byte alignment. */
2128 switch (fh
->ovfmt
->depth
) {
2142 win
->w
.width
-= win
->w
.left
& ~width_mask
;
2143 win
->w
.left
= (win
->w
.left
- width_mask
- 1) & width_mask
;
2145 rc
= limit_scaled_size_lock(fh
, &win
->w
.width
, &win
->w
.height
,
2147 /* width_bias: round down */ 0,
2148 adjust_size
, adjust_crop
);
2154 static int setup_window_lock(struct bttv_fh
*fh
, struct bttv
*btv
,
2155 struct v4l2_window
*win
, int fixup
)
2157 struct v4l2_clip
*clips
= NULL
;
2158 int n
,size
,retval
= 0;
2160 if (NULL
== fh
->ovfmt
)
2162 if (!(fh
->ovfmt
->flags
& FORMAT_FLAGS_PACKED
))
2164 retval
= verify_window_lock(fh
, win
,
2165 /* adjust_size */ fixup
,
2166 /* adjust_crop */ fixup
);
2170 /* copy clips -- luckily v4l1 + v4l2 are binary
2171 compatible here ...*/
2173 size
= sizeof(*clips
)*(n
+4);
2174 clips
= kmalloc(size
,GFP_KERNEL
);
2178 if (copy_from_user(clips
,win
->clips
,sizeof(struct v4l2_clip
)*n
)) {
2184 /* clip against screen */
2185 if (NULL
!= btv
->fbuf
.base
)
2186 n
= btcx_screen_clips(btv
->fbuf
.fmt
.width
, btv
->fbuf
.fmt
.height
,
2188 btcx_sort_clips(clips
,n
);
2190 /* 4-byte alignments */
2191 switch (fh
->ovfmt
->depth
) {
2194 btcx_align(&win
->w
, clips
, n
, 3);
2197 btcx_align(&win
->w
, clips
, n
, 1);
2200 /* no alignment fixups needed */
2206 kfree(fh
->ov
.clips
);
2207 fh
->ov
.clips
= clips
;
2211 fh
->ov
.field
= win
->field
;
2212 fh
->ov
.setup_ok
= 1;
2214 btv
->init
.ov
.w
.width
= win
->w
.width
;
2215 btv
->init
.ov
.w
.height
= win
->w
.height
;
2216 btv
->init
.ov
.field
= win
->field
;
2218 /* update overlay if needed */
2220 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2221 struct bttv_buffer
*new;
2223 new = videobuf_sg_alloc(sizeof(*new));
2224 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2225 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2226 retval
= bttv_switch_overlay(btv
,fh
,new);
2231 /* ----------------------------------------------------------------------- */
2233 static struct videobuf_queue
* bttv_queue(struct bttv_fh
*fh
)
2235 struct videobuf_queue
* q
= NULL
;
2238 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2241 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2250 static int bttv_resource(struct bttv_fh
*fh
)
2255 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2256 res
= RESOURCE_VIDEO_STREAM
;
2258 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2267 static int bttv_switch_type(struct bttv_fh
*fh
, enum v4l2_buf_type type
)
2269 struct videobuf_queue
*q
= bttv_queue(fh
);
2270 int res
= bttv_resource(fh
);
2272 if (check_btres(fh
,res
))
2274 if (videobuf_queue_is_busy(q
))
2281 pix_format_set_size (struct v4l2_pix_format
* f
,
2282 const struct bttv_format
* fmt
,
2284 unsigned int height
)
2289 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2290 f
->bytesperline
= width
; /* Y plane */
2291 f
->sizeimage
= (width
* height
* fmt
->depth
) >> 3;
2293 f
->bytesperline
= (width
* fmt
->depth
) >> 3;
2294 f
->sizeimage
= height
* f
->bytesperline
;
2298 static int bttv_g_fmt_vid_cap(struct file
*file
, void *priv
,
2299 struct v4l2_format
*f
)
2301 struct bttv_fh
*fh
= priv
;
2303 pix_format_set_size(&f
->fmt
.pix
, fh
->fmt
,
2304 fh
->width
, fh
->height
);
2305 f
->fmt
.pix
.field
= fh
->cap
.field
;
2306 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
2307 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2312 static int bttv_g_fmt_vid_overlay(struct file
*file
, void *priv
,
2313 struct v4l2_format
*f
)
2315 struct bttv_fh
*fh
= priv
;
2317 f
->fmt
.win
.w
= fh
->ov
.w
;
2318 f
->fmt
.win
.field
= fh
->ov
.field
;
2323 static void bttv_get_width_mask_vid_cap(const struct bttv_format
*fmt
,
2324 unsigned int *width_mask
,
2325 unsigned int *width_bias
)
2327 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2328 *width_mask
= ~15; /* width must be a multiple of 16 pixels */
2329 *width_bias
= 8; /* nearest */
2331 *width_mask
= ~3; /* width must be a multiple of 4 pixels */
2332 *width_bias
= 2; /* nearest */
2336 static int bttv_try_fmt_vid_cap(struct file
*file
, void *priv
,
2337 struct v4l2_format
*f
)
2339 const struct bttv_format
*fmt
;
2340 struct bttv_fh
*fh
= priv
;
2341 struct bttv
*btv
= fh
->btv
;
2342 enum v4l2_field field
;
2343 __s32 width
, height
;
2345 unsigned int width_mask
, width_bias
;
2348 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2352 field
= f
->fmt
.pix
.field
;
2355 case V4L2_FIELD_TOP
:
2356 case V4L2_FIELD_BOTTOM
:
2357 case V4L2_FIELD_ALTERNATE
:
2358 case V4L2_FIELD_INTERLACED
:
2360 case V4L2_FIELD_SEQ_BT
:
2361 case V4L2_FIELD_SEQ_TB
:
2362 if (!(fmt
->flags
& FORMAT_FLAGS_PLANAR
)) {
2363 field
= V4L2_FIELD_SEQ_TB
;
2367 default: /* FIELD_ANY case */
2368 height2
= btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2369 field
= (f
->fmt
.pix
.height
> height2
)
2370 ? V4L2_FIELD_INTERLACED
2371 : V4L2_FIELD_BOTTOM
;
2375 width
= f
->fmt
.pix
.width
;
2376 height
= f
->fmt
.pix
.height
;
2378 bttv_get_width_mask_vid_cap(fmt
, &width_mask
, &width_bias
);
2379 rc
= limit_scaled_size_lock(fh
, &width
, &height
, field
,
2380 width_mask
, width_bias
,
2381 /* adjust_size */ 1,
2382 /* adjust_crop */ 0);
2386 /* update data for the application */
2387 f
->fmt
.pix
.field
= field
;
2388 pix_format_set_size(&f
->fmt
.pix
, fmt
, width
, height
);
2389 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2394 static int bttv_try_fmt_vid_overlay(struct file
*file
, void *priv
,
2395 struct v4l2_format
*f
)
2397 struct bttv_fh
*fh
= priv
;
2399 verify_window_lock(fh
, &f
->fmt
.win
,
2400 /* adjust_size */ 1,
2401 /* adjust_crop */ 0);
2405 static int bttv_s_fmt_vid_cap(struct file
*file
, void *priv
,
2406 struct v4l2_format
*f
)
2409 const struct bttv_format
*fmt
;
2410 struct bttv_fh
*fh
= priv
;
2411 struct bttv
*btv
= fh
->btv
;
2412 __s32 width
, height
;
2413 unsigned int width_mask
, width_bias
;
2414 enum v4l2_field field
;
2416 retval
= bttv_switch_type(fh
, f
->type
);
2420 retval
= bttv_try_fmt_vid_cap(file
, priv
, f
);
2424 width
= f
->fmt
.pix
.width
;
2425 height
= f
->fmt
.pix
.height
;
2426 field
= f
->fmt
.pix
.field
;
2428 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2429 bttv_get_width_mask_vid_cap(fmt
, &width_mask
, &width_bias
);
2430 retval
= limit_scaled_size_lock(fh
, &width
, &height
, f
->fmt
.pix
.field
,
2431 width_mask
, width_bias
,
2432 /* adjust_size */ 1,
2433 /* adjust_crop */ 1);
2437 f
->fmt
.pix
.field
= field
;
2439 /* update our state informations */
2441 fh
->cap
.field
= f
->fmt
.pix
.field
;
2442 fh
->cap
.last
= V4L2_FIELD_NONE
;
2443 fh
->width
= f
->fmt
.pix
.width
;
2444 fh
->height
= f
->fmt
.pix
.height
;
2445 btv
->init
.fmt
= fmt
;
2446 btv
->init
.width
= f
->fmt
.pix
.width
;
2447 btv
->init
.height
= f
->fmt
.pix
.height
;
2452 static int bttv_s_fmt_vid_overlay(struct file
*file
, void *priv
,
2453 struct v4l2_format
*f
)
2455 struct bttv_fh
*fh
= priv
;
2456 struct bttv
*btv
= fh
->btv
;
2458 if (no_overlay
> 0) {
2459 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2463 return setup_window_lock(fh
, btv
, &f
->fmt
.win
, 1);
2466 static int bttv_querycap(struct file
*file
, void *priv
,
2467 struct v4l2_capability
*cap
)
2469 struct video_device
*vdev
= video_devdata(file
);
2470 struct bttv_fh
*fh
= priv
;
2471 struct bttv
*btv
= fh
->btv
;
2476 strlcpy(cap
->driver
, "bttv", sizeof(cap
->driver
));
2477 strlcpy(cap
->card
, btv
->video_dev
.name
, sizeof(cap
->card
));
2478 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2479 "PCI:%s", pci_name(btv
->c
.pci
));
2481 V4L2_CAP_VIDEO_CAPTURE
|
2482 V4L2_CAP_READWRITE
|
2483 V4L2_CAP_STREAMING
|
2484 V4L2_CAP_DEVICE_CAPS
;
2485 if (no_overlay
<= 0)
2486 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
2487 if (video_is_registered(&btv
->vbi_dev
))
2488 cap
->capabilities
|= V4L2_CAP_VBI_CAPTURE
;
2489 if (video_is_registered(&btv
->radio_dev
))
2490 cap
->capabilities
|= V4L2_CAP_RADIO
;
2493 * No need to lock here: those vars are initialized during board
2494 * probe and remains untouched during the rest of the driver lifecycle
2496 if (btv
->has_saa6588
)
2497 cap
->capabilities
|= V4L2_CAP_RDS_CAPTURE
;
2498 if (btv
->tuner_type
!= TUNER_ABSENT
)
2499 cap
->capabilities
|= V4L2_CAP_TUNER
;
2500 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
2501 cap
->device_caps
= cap
->capabilities
&
2502 (V4L2_CAP_VIDEO_CAPTURE
|
2503 V4L2_CAP_READWRITE
|
2504 V4L2_CAP_STREAMING
|
2505 V4L2_CAP_VIDEO_OVERLAY
|
2507 else if (vdev
->vfl_type
== VFL_TYPE_VBI
)
2508 cap
->device_caps
= cap
->capabilities
&
2509 (V4L2_CAP_VBI_CAPTURE
|
2510 V4L2_CAP_READWRITE
|
2511 V4L2_CAP_STREAMING
|
2514 cap
->device_caps
= V4L2_CAP_RADIO
| V4L2_CAP_TUNER
;
2515 if (btv
->has_saa6588
)
2516 cap
->device_caps
|= V4L2_CAP_READWRITE
|
2517 V4L2_CAP_RDS_CAPTURE
;
2518 if (btv
->has_tea575x
)
2519 cap
->device_caps
|= V4L2_CAP_HW_FREQ_SEEK
;
2524 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc
*f
)
2528 for (i
= 0; i
< FORMATS
; i
++) {
2529 if (formats
[i
].fourcc
!= -1)
2531 if ((unsigned int)index
== f
->index
)
2537 f
->pixelformat
= formats
[i
].fourcc
;
2538 strlcpy(f
->description
, formats
[i
].name
, sizeof(f
->description
));
2543 static int bttv_enum_fmt_vid_cap(struct file
*file
, void *priv
,
2544 struct v4l2_fmtdesc
*f
)
2546 int rc
= bttv_enum_fmt_cap_ovr(f
);
2554 static int bttv_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
2555 struct v4l2_fmtdesc
*f
)
2559 if (no_overlay
> 0) {
2560 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2564 rc
= bttv_enum_fmt_cap_ovr(f
);
2569 if (!(formats
[rc
].flags
& FORMAT_FLAGS_PACKED
))
2575 static int bttv_g_fbuf(struct file
*file
, void *f
,
2576 struct v4l2_framebuffer
*fb
)
2578 struct bttv_fh
*fh
= f
;
2579 struct bttv
*btv
= fh
->btv
;
2582 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2583 fb
->flags
= V4L2_FBUF_FLAG_PRIMARY
;
2585 fb
->fmt
.pixelformat
= fh
->ovfmt
->fourcc
;
2589 static int bttv_overlay(struct file
*file
, void *f
, unsigned int on
)
2591 struct bttv_fh
*fh
= f
;
2592 struct bttv
*btv
= fh
->btv
;
2593 struct bttv_buffer
*new;
2598 if (unlikely(!btv
->fbuf
.base
)) {
2601 if (unlikely(!fh
->ov
.setup_ok
)) {
2602 dprintk("%d: overlay: !setup_ok\n", btv
->c
.nr
);
2609 if (!check_alloc_btres_lock(btv
, fh
, RESOURCE_OVERLAY
))
2613 fh
->ov
.tvnorm
= btv
->tvnorm
;
2614 new = videobuf_sg_alloc(sizeof(*new));
2615 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2616 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2622 retval
= bttv_switch_overlay(btv
, fh
, new);
2626 static int bttv_s_fbuf(struct file
*file
, void *f
,
2627 const struct v4l2_framebuffer
*fb
)
2629 struct bttv_fh
*fh
= f
;
2630 struct bttv
*btv
= fh
->btv
;
2631 const struct bttv_format
*fmt
;
2634 if (!capable(CAP_SYS_ADMIN
) &&
2635 !capable(CAP_SYS_RAWIO
))
2639 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2642 if (0 == (fmt
->flags
& FORMAT_FLAGS_PACKED
))
2646 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2647 __s32 width
= fb
->fmt
.width
;
2648 __s32 height
= fb
->fmt
.height
;
2650 retval
= limit_scaled_size_lock(fh
, &width
, &height
,
2651 V4L2_FIELD_INTERLACED
,
2652 /* width_mask */ ~3,
2654 /* adjust_size */ 0,
2655 /* adjust_crop */ 0);
2661 btv
->fbuf
.base
= fb
->base
;
2662 btv
->fbuf
.fmt
.width
= fb
->fmt
.width
;
2663 btv
->fbuf
.fmt
.height
= fb
->fmt
.height
;
2664 if (0 != fb
->fmt
.bytesperline
)
2665 btv
->fbuf
.fmt
.bytesperline
= fb
->fmt
.bytesperline
;
2667 btv
->fbuf
.fmt
.bytesperline
= btv
->fbuf
.fmt
.width
*fmt
->depth
/8;
2671 btv
->init
.ovfmt
= fmt
;
2672 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2675 fh
->ov
.w
.width
= fb
->fmt
.width
;
2676 fh
->ov
.w
.height
= fb
->fmt
.height
;
2677 btv
->init
.ov
.w
.width
= fb
->fmt
.width
;
2678 btv
->init
.ov
.w
.height
= fb
->fmt
.height
;
2680 kfree(fh
->ov
.clips
);
2681 fh
->ov
.clips
= NULL
;
2684 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2685 struct bttv_buffer
*new;
2687 new = videobuf_sg_alloc(sizeof(*new));
2688 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2689 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2690 retval
= bttv_switch_overlay(btv
, fh
, new);
2696 static int bttv_reqbufs(struct file
*file
, void *priv
,
2697 struct v4l2_requestbuffers
*p
)
2699 struct bttv_fh
*fh
= priv
;
2700 return videobuf_reqbufs(bttv_queue(fh
), p
);
2703 static int bttv_querybuf(struct file
*file
, void *priv
,
2704 struct v4l2_buffer
*b
)
2706 struct bttv_fh
*fh
= priv
;
2707 return videobuf_querybuf(bttv_queue(fh
), b
);
2710 static int bttv_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2712 struct bttv_fh
*fh
= priv
;
2713 struct bttv
*btv
= fh
->btv
;
2714 int res
= bttv_resource(fh
);
2716 if (!check_alloc_btres_lock(btv
, fh
, res
))
2719 return videobuf_qbuf(bttv_queue(fh
), b
);
2722 static int bttv_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2724 struct bttv_fh
*fh
= priv
;
2725 return videobuf_dqbuf(bttv_queue(fh
), b
,
2726 file
->f_flags
& O_NONBLOCK
);
2729 static int bttv_streamon(struct file
*file
, void *priv
,
2730 enum v4l2_buf_type type
)
2732 struct bttv_fh
*fh
= priv
;
2733 struct bttv
*btv
= fh
->btv
;
2734 int res
= bttv_resource(fh
);
2736 if (!check_alloc_btres_lock(btv
, fh
, res
))
2738 return videobuf_streamon(bttv_queue(fh
));
2742 static int bttv_streamoff(struct file
*file
, void *priv
,
2743 enum v4l2_buf_type type
)
2745 struct bttv_fh
*fh
= priv
;
2746 struct bttv
*btv
= fh
->btv
;
2748 int res
= bttv_resource(fh
);
2751 retval
= videobuf_streamoff(bttv_queue(fh
));
2754 free_btres_lock(btv
, fh
, res
);
2758 static int bttv_g_parm(struct file
*file
, void *f
,
2759 struct v4l2_streamparm
*parm
)
2761 struct bttv_fh
*fh
= f
;
2762 struct bttv
*btv
= fh
->btv
;
2764 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2766 parm
->parm
.capture
.readbuffers
= gbuffers
;
2767 v4l2_video_std_frame_period(bttv_tvnorms
[btv
->tvnorm
].v4l2_id
,
2768 &parm
->parm
.capture
.timeperframe
);
2773 static int bttv_g_tuner(struct file
*file
, void *priv
,
2774 struct v4l2_tuner
*t
)
2776 struct bttv_fh
*fh
= priv
;
2777 struct bttv
*btv
= fh
->btv
;
2782 t
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
2783 t
->capability
= V4L2_TUNER_CAP_NORM
;
2784 bttv_call_all(btv
, tuner
, g_tuner
, t
);
2785 strcpy(t
->name
, "Television");
2786 t
->type
= V4L2_TUNER_ANALOG_TV
;
2787 if (btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
)
2790 if (btv
->audio_mode_gpio
)
2791 btv
->audio_mode_gpio(btv
, t
, 0);
2796 static int bttv_cropcap(struct file
*file
, void *priv
,
2797 struct v4l2_cropcap
*cap
)
2799 struct bttv_fh
*fh
= priv
;
2800 struct bttv
*btv
= fh
->btv
;
2802 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2803 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2806 /* defrect and bounds are set via g_selection */
2807 cap
->pixelaspect
= bttv_tvnorms
[btv
->tvnorm
].cropcap
.pixelaspect
;
2812 static int bttv_g_selection(struct file
*file
, void *f
, struct v4l2_selection
*sel
)
2814 struct bttv_fh
*fh
= f
;
2815 struct bttv
*btv
= fh
->btv
;
2817 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2818 sel
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2821 switch (sel
->target
) {
2822 case V4L2_SEL_TGT_CROP
:
2824 * No fh->do_crop = 1; because btv->crop[1] may be
2825 * inconsistent with fh->width or fh->height and apps
2826 * do not expect a change here.
2828 sel
->r
= btv
->crop
[!!fh
->do_crop
].rect
;
2830 case V4L2_SEL_TGT_CROP_DEFAULT
:
2831 sel
->r
= bttv_tvnorms
[btv
->tvnorm
].cropcap
.defrect
;
2833 case V4L2_SEL_TGT_CROP_BOUNDS
:
2834 sel
->r
= bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2843 static int bttv_s_selection(struct file
*file
, void *f
, struct v4l2_selection
*sel
)
2845 struct bttv_fh
*fh
= f
;
2846 struct bttv
*btv
= fh
->btv
;
2847 const struct v4l2_rect
*b
;
2855 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2856 sel
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2859 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
2862 /* Make sure tvnorm, vbi_end and the current cropping
2863 parameters remain consistent until we're done. Note
2864 read() may change vbi_end in check_alloc_btres_lock(). */
2867 if (locked_btres(fh
->btv
, VIDEO_RESOURCES
)) {
2871 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2874 b_right
= b_left
+ b
->width
;
2875 b_bottom
= b
->top
+ b
->height
;
2877 b_top
= max(b
->top
, btv
->vbi_end
);
2878 if (b_top
+ 32 >= b_bottom
) {
2882 /* Min. scaled size 48 x 32. */
2883 c
.rect
.left
= clamp_t(s32
, sel
->r
.left
, b_left
, b_right
- 48);
2884 c
.rect
.left
= min(c
.rect
.left
, (__s32
) MAX_HDELAY
);
2886 c
.rect
.width
= clamp_t(s32
, sel
->r
.width
,
2887 48, b_right
- c
.rect
.left
);
2889 c
.rect
.top
= clamp_t(s32
, sel
->r
.top
, b_top
, b_bottom
- 32);
2890 /* Top and height must be a multiple of two. */
2891 c
.rect
.top
= (c
.rect
.top
+ 1) & ~1;
2893 c
.rect
.height
= clamp_t(s32
, sel
->r
.height
,
2894 32, b_bottom
- c
.rect
.top
);
2895 c
.rect
.height
= (c
.rect
.height
+ 1) & ~1;
2897 bttv_crop_calc_limits(&c
);
2905 if (fh
->width
< c
.min_scaled_width
) {
2906 fh
->width
= c
.min_scaled_width
;
2907 btv
->init
.width
= c
.min_scaled_width
;
2908 } else if (fh
->width
> c
.max_scaled_width
) {
2909 fh
->width
= c
.max_scaled_width
;
2910 btv
->init
.width
= c
.max_scaled_width
;
2913 if (fh
->height
< c
.min_scaled_height
) {
2914 fh
->height
= c
.min_scaled_height
;
2915 btv
->init
.height
= c
.min_scaled_height
;
2916 } else if (fh
->height
> c
.max_scaled_height
) {
2917 fh
->height
= c
.max_scaled_height
;
2918 btv
->init
.height
= c
.max_scaled_height
;
2924 static ssize_t
bttv_read(struct file
*file
, char __user
*data
,
2925 size_t count
, loff_t
*ppos
)
2927 struct bttv_fh
*fh
= file
->private_data
;
2930 if (fh
->btv
->errors
)
2931 bttv_reinit_bt848(fh
->btv
);
2932 dprintk("%d: read count=%d type=%s\n",
2933 fh
->btv
->c
.nr
, (int)count
, v4l2_type_names
[fh
->type
]);
2936 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2937 if (!check_alloc_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
)) {
2938 /* VIDEO_READ in use by another fh,
2939 or VIDEO_STREAM by any fh. */
2942 retval
= videobuf_read_one(&fh
->cap
, data
, count
, ppos
,
2943 file
->f_flags
& O_NONBLOCK
);
2944 free_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
);
2946 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2947 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
2949 retval
= videobuf_read_stream(&fh
->vbi
, data
, count
, ppos
, 1,
2950 file
->f_flags
& O_NONBLOCK
);
2958 static unsigned int bttv_poll(struct file
*file
, poll_table
*wait
)
2960 struct bttv_fh
*fh
= file
->private_data
;
2961 struct bttv_buffer
*buf
;
2962 enum v4l2_field field
;
2963 unsigned int rc
= 0;
2964 unsigned long req_events
= poll_requested_events(wait
);
2966 if (v4l2_event_pending(&fh
->fh
))
2968 else if (req_events
& POLLPRI
)
2969 poll_wait(file
, &fh
->fh
.wait
, wait
);
2971 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
2974 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
2975 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
2976 return rc
| POLLERR
;
2977 return rc
| videobuf_poll_stream(file
, &fh
->vbi
, wait
);
2980 if (check_btres(fh
,RESOURCE_VIDEO_STREAM
)) {
2981 /* streaming capture */
2982 if (list_empty(&fh
->cap
.stream
))
2983 return rc
| POLLERR
;
2984 buf
= list_entry(fh
->cap
.stream
.next
,struct bttv_buffer
,vb
.stream
);
2986 /* read() capture */
2987 if (NULL
== fh
->cap
.read_buf
) {
2988 /* need to capture a new frame */
2989 if (locked_btres(fh
->btv
,RESOURCE_VIDEO_STREAM
))
2990 return rc
| POLLERR
;
2991 fh
->cap
.read_buf
= videobuf_sg_alloc(fh
->cap
.msize
);
2992 if (NULL
== fh
->cap
.read_buf
)
2993 return rc
| POLLERR
;
2994 fh
->cap
.read_buf
->memory
= V4L2_MEMORY_USERPTR
;
2995 field
= videobuf_next_field(&fh
->cap
);
2996 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,field
)) {
2997 kfree (fh
->cap
.read_buf
);
2998 fh
->cap
.read_buf
= NULL
;
2999 return rc
| POLLERR
;
3001 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
3002 fh
->cap
.read_off
= 0;
3004 buf
= (struct bttv_buffer
*)fh
->cap
.read_buf
;
3007 poll_wait(file
, &buf
->vb
.done
, wait
);
3008 if (buf
->vb
.state
== VIDEOBUF_DONE
||
3009 buf
->vb
.state
== VIDEOBUF_ERROR
)
3010 rc
= rc
| POLLIN
|POLLRDNORM
;
3014 static int bttv_open(struct file
*file
)
3016 struct video_device
*vdev
= video_devdata(file
);
3017 struct bttv
*btv
= video_drvdata(file
);
3019 enum v4l2_buf_type type
= 0;
3021 dprintk("open dev=%s\n", video_device_node_name(vdev
));
3023 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
) {
3024 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
3025 } else if (vdev
->vfl_type
== VFL_TYPE_VBI
) {
3026 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
3032 dprintk("%d: open called (type=%s)\n",
3033 btv
->c
.nr
, v4l2_type_names
[type
]);
3035 /* allocate per filehandle data */
3036 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3040 file
->private_data
= fh
;
3043 v4l2_fh_init(&fh
->fh
, vdev
);
3046 fh
->ov
.setup_ok
= 0;
3048 videobuf_queue_sg_init(&fh
->cap
, &bttv_video_qops
,
3049 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3050 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
3051 V4L2_FIELD_INTERLACED
,
3052 sizeof(struct bttv_buffer
),
3054 videobuf_queue_sg_init(&fh
->vbi
, &bttv_vbi_qops
,
3055 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3056 V4L2_BUF_TYPE_VBI_CAPTURE
,
3058 sizeof(struct bttv_buffer
),
3060 set_tvnorm(btv
,btv
->tvnorm
);
3061 set_input(btv
, btv
->input
, btv
->tvnorm
);
3062 audio_mute(btv
, btv
->mute
);
3064 /* The V4L2 spec requires one global set of cropping parameters
3065 which only change on request. These are stored in btv->crop[1].
3066 However for compatibility with V4L apps and cropping unaware
3067 V4L2 apps we now reset the cropping parameters as seen through
3068 this fh, which is to say VIDIOC_G_SELECTION and scaling limit checks
3069 will use btv->crop[0], the default cropping parameters for the
3070 current video standard, and VIDIOC_S_FMT will not implicitely
3071 change the cropping parameters until VIDIOC_S_SELECTION has been
3073 fh
->do_crop
= !reset_crop
; /* module parameter */
3075 /* Likewise there should be one global set of VBI capture
3076 parameters, but for compatibility with V4L apps and earlier
3077 driver versions each fh has its own parameters. */
3078 bttv_vbi_fmt_reset(&fh
->vbi_fmt
, btv
->tvnorm
);
3080 bttv_field_count(btv
);
3081 v4l2_fh_add(&fh
->fh
);
3085 static int bttv_release(struct file
*file
)
3087 struct bttv_fh
*fh
= file
->private_data
;
3088 struct bttv
*btv
= fh
->btv
;
3090 /* turn off overlay */
3091 if (check_btres(fh
, RESOURCE_OVERLAY
))
3092 bttv_switch_overlay(btv
,fh
,NULL
);
3094 /* stop video capture */
3095 if (check_btres(fh
, RESOURCE_VIDEO_STREAM
)) {
3096 videobuf_streamoff(&fh
->cap
);
3097 free_btres_lock(btv
,fh
,RESOURCE_VIDEO_STREAM
);
3099 if (fh
->cap
.read_buf
) {
3100 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
3101 kfree(fh
->cap
.read_buf
);
3103 if (check_btres(fh
, RESOURCE_VIDEO_READ
)) {
3104 free_btres_lock(btv
, fh
, RESOURCE_VIDEO_READ
);
3107 /* stop vbi capture */
3108 if (check_btres(fh
, RESOURCE_VBI
)) {
3109 videobuf_stop(&fh
->vbi
);
3110 free_btres_lock(btv
,fh
,RESOURCE_VBI
);
3115 videobuf_mmap_free(&fh
->cap
);
3116 videobuf_mmap_free(&fh
->vbi
);
3117 file
->private_data
= NULL
;
3120 bttv_field_count(btv
);
3123 audio_mute(btv
, btv
->mute
);
3125 v4l2_fh_del(&fh
->fh
);
3126 v4l2_fh_exit(&fh
->fh
);
3132 bttv_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3134 struct bttv_fh
*fh
= file
->private_data
;
3136 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
3137 fh
->btv
->c
.nr
, v4l2_type_names
[fh
->type
],
3138 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
3139 return videobuf_mmap_mapper(bttv_queue(fh
),vma
);
3142 static const struct v4l2_file_operations bttv_fops
=
3144 .owner
= THIS_MODULE
,
3146 .release
= bttv_release
,
3147 .unlocked_ioctl
= video_ioctl2
,
3153 static const struct v4l2_ioctl_ops bttv_ioctl_ops
= {
3154 .vidioc_querycap
= bttv_querycap
,
3155 .vidioc_enum_fmt_vid_cap
= bttv_enum_fmt_vid_cap
,
3156 .vidioc_g_fmt_vid_cap
= bttv_g_fmt_vid_cap
,
3157 .vidioc_try_fmt_vid_cap
= bttv_try_fmt_vid_cap
,
3158 .vidioc_s_fmt_vid_cap
= bttv_s_fmt_vid_cap
,
3159 .vidioc_enum_fmt_vid_overlay
= bttv_enum_fmt_vid_overlay
,
3160 .vidioc_g_fmt_vid_overlay
= bttv_g_fmt_vid_overlay
,
3161 .vidioc_try_fmt_vid_overlay
= bttv_try_fmt_vid_overlay
,
3162 .vidioc_s_fmt_vid_overlay
= bttv_s_fmt_vid_overlay
,
3163 .vidioc_g_fmt_vbi_cap
= bttv_g_fmt_vbi_cap
,
3164 .vidioc_try_fmt_vbi_cap
= bttv_try_fmt_vbi_cap
,
3165 .vidioc_s_fmt_vbi_cap
= bttv_s_fmt_vbi_cap
,
3166 .vidioc_cropcap
= bttv_cropcap
,
3167 .vidioc_reqbufs
= bttv_reqbufs
,
3168 .vidioc_querybuf
= bttv_querybuf
,
3169 .vidioc_qbuf
= bttv_qbuf
,
3170 .vidioc_dqbuf
= bttv_dqbuf
,
3171 .vidioc_s_std
= bttv_s_std
,
3172 .vidioc_g_std
= bttv_g_std
,
3173 .vidioc_enum_input
= bttv_enum_input
,
3174 .vidioc_g_input
= bttv_g_input
,
3175 .vidioc_s_input
= bttv_s_input
,
3176 .vidioc_streamon
= bttv_streamon
,
3177 .vidioc_streamoff
= bttv_streamoff
,
3178 .vidioc_g_tuner
= bttv_g_tuner
,
3179 .vidioc_s_tuner
= bttv_s_tuner
,
3180 .vidioc_g_selection
= bttv_g_selection
,
3181 .vidioc_s_selection
= bttv_s_selection
,
3182 .vidioc_g_fbuf
= bttv_g_fbuf
,
3183 .vidioc_s_fbuf
= bttv_s_fbuf
,
3184 .vidioc_overlay
= bttv_overlay
,
3185 .vidioc_g_parm
= bttv_g_parm
,
3186 .vidioc_g_frequency
= bttv_g_frequency
,
3187 .vidioc_s_frequency
= bttv_s_frequency
,
3188 .vidioc_log_status
= bttv_log_status
,
3189 .vidioc_querystd
= bttv_querystd
,
3190 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
3191 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
3192 #ifdef CONFIG_VIDEO_ADV_DEBUG
3193 .vidioc_g_register
= bttv_g_register
,
3194 .vidioc_s_register
= bttv_s_register
,
3198 static struct video_device bttv_video_template
= {
3200 .ioctl_ops
= &bttv_ioctl_ops
,
3201 .tvnorms
= BTTV_NORMS
,
3204 /* ----------------------------------------------------------------------- */
3205 /* radio interface */
3207 static int radio_open(struct file
*file
)
3209 struct video_device
*vdev
= video_devdata(file
);
3210 struct bttv
*btv
= video_drvdata(file
);
3213 dprintk("open dev=%s\n", video_device_node_name(vdev
));
3215 dprintk("%d: open called (radio)\n", btv
->c
.nr
);
3217 /* allocate per filehandle data */
3218 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3221 file
->private_data
= fh
;
3223 v4l2_fh_init(&fh
->fh
, vdev
);
3226 audio_mute(btv
, btv
->mute
);
3228 v4l2_fh_add(&fh
->fh
);
3233 static int radio_release(struct file
*file
)
3235 struct bttv_fh
*fh
= file
->private_data
;
3236 struct bttv
*btv
= fh
->btv
;
3237 struct saa6588_command cmd
;
3239 file
->private_data
= NULL
;
3240 v4l2_fh_del(&fh
->fh
);
3241 v4l2_fh_exit(&fh
->fh
);
3246 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_CLOSE
, &cmd
);
3248 if (btv
->radio_user
== 0)
3249 btv
->has_radio_tuner
= 0;
3253 static int radio_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
3255 struct bttv_fh
*fh
= priv
;
3256 struct bttv
*btv
= fh
->btv
;
3260 strcpy(t
->name
, "Radio");
3261 t
->type
= V4L2_TUNER_RADIO
;
3264 bttv_call_all(btv
, tuner
, g_tuner
, t
);
3266 if (btv
->audio_mode_gpio
)
3267 btv
->audio_mode_gpio(btv
, t
, 0);
3269 if (btv
->has_tea575x
)
3270 return snd_tea575x_g_tuner(&btv
->tea
, t
);
3275 static int radio_s_tuner(struct file
*file
, void *priv
,
3276 const struct v4l2_tuner
*t
)
3278 struct bttv_fh
*fh
= priv
;
3279 struct bttv
*btv
= fh
->btv
;
3285 bttv_call_all(btv
, tuner
, s_tuner
, t
);
3289 static int radio_s_hw_freq_seek(struct file
*file
, void *priv
,
3290 const struct v4l2_hw_freq_seek
*a
)
3292 struct bttv_fh
*fh
= priv
;
3293 struct bttv
*btv
= fh
->btv
;
3295 if (btv
->has_tea575x
)
3296 return snd_tea575x_s_hw_freq_seek(file
, &btv
->tea
, a
);
3301 static int radio_enum_freq_bands(struct file
*file
, void *priv
,
3302 struct v4l2_frequency_band
*band
)
3304 struct bttv_fh
*fh
= priv
;
3305 struct bttv
*btv
= fh
->btv
;
3307 if (btv
->has_tea575x
)
3308 return snd_tea575x_enum_freq_bands(&btv
->tea
, band
);
3313 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
3314 size_t count
, loff_t
*ppos
)
3316 struct bttv_fh
*fh
= file
->private_data
;
3317 struct bttv
*btv
= fh
->btv
;
3318 struct saa6588_command cmd
;
3320 cmd
.block_count
= count
/ 3;
3321 cmd
.nonblocking
= file
->f_flags
& O_NONBLOCK
;
3323 cmd
.instance
= file
;
3324 cmd
.result
= -ENODEV
;
3327 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_READ
, &cmd
);
3332 static unsigned int radio_poll(struct file
*file
, poll_table
*wait
)
3334 struct bttv_fh
*fh
= file
->private_data
;
3335 struct bttv
*btv
= fh
->btv
;
3336 unsigned long req_events
= poll_requested_events(wait
);
3337 struct saa6588_command cmd
;
3338 unsigned int res
= 0;
3340 if (v4l2_event_pending(&fh
->fh
))
3342 else if (req_events
& POLLPRI
)
3343 poll_wait(file
, &fh
->fh
.wait
, wait
);
3345 cmd
.instance
= file
;
3346 cmd
.event_list
= wait
;
3348 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_POLL
, &cmd
);
3353 static const struct v4l2_file_operations radio_fops
=
3355 .owner
= THIS_MODULE
,
3358 .release
= radio_release
,
3359 .unlocked_ioctl
= video_ioctl2
,
3363 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
3364 .vidioc_querycap
= bttv_querycap
,
3365 .vidioc_log_status
= bttv_log_status
,
3366 .vidioc_g_tuner
= radio_g_tuner
,
3367 .vidioc_s_tuner
= radio_s_tuner
,
3368 .vidioc_g_frequency
= bttv_g_frequency
,
3369 .vidioc_s_frequency
= bttv_s_frequency
,
3370 .vidioc_s_hw_freq_seek
= radio_s_hw_freq_seek
,
3371 .vidioc_enum_freq_bands
= radio_enum_freq_bands
,
3372 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
3373 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
3376 static struct video_device radio_template
= {
3377 .fops
= &radio_fops
,
3378 .ioctl_ops
= &radio_ioctl_ops
,
3381 /* ----------------------------------------------------------------------- */
3382 /* some debug code */
3384 static int bttv_risc_decode(u32 risc
)
3386 static char *instr
[16] = {
3387 [ BT848_RISC_WRITE
>> 28 ] = "write",
3388 [ BT848_RISC_SKIP
>> 28 ] = "skip",
3389 [ BT848_RISC_WRITEC
>> 28 ] = "writec",
3390 [ BT848_RISC_JUMP
>> 28 ] = "jump",
3391 [ BT848_RISC_SYNC
>> 28 ] = "sync",
3392 [ BT848_RISC_WRITE123
>> 28 ] = "write123",
3393 [ BT848_RISC_SKIP123
>> 28 ] = "skip123",
3394 [ BT848_RISC_WRITE1S23
>> 28 ] = "write1s23",
3396 static int incr
[16] = {
3397 [ BT848_RISC_WRITE
>> 28 ] = 2,
3398 [ BT848_RISC_JUMP
>> 28 ] = 2,
3399 [ BT848_RISC_SYNC
>> 28 ] = 2,
3400 [ BT848_RISC_WRITE123
>> 28 ] = 5,
3401 [ BT848_RISC_SKIP123
>> 28 ] = 2,
3402 [ BT848_RISC_WRITE1S23
>> 28 ] = 3,
3404 static char *bits
[] = {
3405 "be0", "be1", "be2", "be3/resync",
3406 "set0", "set1", "set2", "set3",
3407 "clr0", "clr1", "clr2", "clr3",
3408 "irq", "res", "eol", "sol",
3412 pr_cont("0x%08x [ %s", risc
,
3413 instr
[risc
>> 28] ? instr
[risc
>> 28] : "INVALID");
3414 for (i
= ARRAY_SIZE(bits
)-1; i
>= 0; i
--)
3415 if (risc
& (1 << (i
+ 12)))
3416 pr_cont(" %s", bits
[i
]);
3417 pr_cont(" count=%d ]\n", risc
& 0xfff);
3418 return incr
[risc
>> 28] ? incr
[risc
>> 28] : 1;
3421 static void bttv_risc_disasm(struct bttv
*btv
,
3422 struct btcx_riscmem
*risc
)
3426 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3427 btv
->c
.v4l2_dev
.name
, risc
->cpu
, (unsigned long)risc
->dma
);
3428 for (i
= 0; i
< (risc
->size
>> 2); i
+= n
) {
3429 pr_info("%s: 0x%lx: ",
3430 btv
->c
.v4l2_dev
.name
,
3431 (unsigned long)(risc
->dma
+ (i
<<2)));
3432 n
= bttv_risc_decode(le32_to_cpu(risc
->cpu
[i
]));
3433 for (j
= 1; j
< n
; j
++)
3434 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3435 btv
->c
.v4l2_dev
.name
,
3436 (unsigned long)(risc
->dma
+ ((i
+j
)<<2)),
3438 if (0 == risc
->cpu
[i
])
3443 static void bttv_print_riscaddr(struct bttv
*btv
)
3445 pr_info(" main: %08llx\n", (unsigned long long)btv
->main
.dma
);
3446 pr_info(" vbi : o=%08llx e=%08llx\n",
3447 btv
->cvbi
? (unsigned long long)btv
->cvbi
->top
.dma
: 0,
3448 btv
->cvbi
? (unsigned long long)btv
->cvbi
->bottom
.dma
: 0);
3449 pr_info(" cap : o=%08llx e=%08llx\n",
3451 ? (unsigned long long)btv
->curr
.top
->top
.dma
: 0,
3453 ? (unsigned long long)btv
->curr
.bottom
->bottom
.dma
: 0);
3454 pr_info(" scr : o=%08llx e=%08llx\n",
3455 btv
->screen
? (unsigned long long)btv
->screen
->top
.dma
: 0,
3456 btv
->screen
? (unsigned long long)btv
->screen
->bottom
.dma
: 0);
3457 bttv_risc_disasm(btv
, &btv
->main
);
3460 /* ----------------------------------------------------------------------- */
3463 static char *irq_name
[] = {
3464 "FMTCHG", // format change detected (525 vs. 625)
3465 "VSYNC", // vertical sync (new field)
3466 "HSYNC", // horizontal sync
3467 "OFLOW", // chroma/luma AGC overflow
3468 "HLOCK", // horizontal lock changed
3469 "VPRES", // video presence changed
3471 "I2CDONE", // hw irc operation finished
3472 "GPINT", // gpio port triggered irq
3474 "RISCI", // risc instruction triggered irq
3475 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3476 "FTRGT", // pixel data fifo overrun
3477 "FDSR", // fifo data stream resyncronisation
3478 "PPERR", // parity error (data transfer)
3479 "RIPERR", // parity error (read risc instructions)
3480 "PABORT", // pci abort
3481 "OCERR", // risc instruction error
3482 "SCERR", // syncronisation error
3485 static void bttv_print_irqbits(u32 print
, u32 mark
)
3490 for (i
= 0; i
< ARRAY_SIZE(irq_name
); i
++) {
3491 if (print
& (1 << i
))
3492 pr_cont(" %s", irq_name
[i
]);
3493 if (mark
& (1 << i
))
3498 static void bttv_irq_debug_low_latency(struct bttv
*btv
, u32 rc
)
3500 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3502 (unsigned long)btv
->main
.dma
,
3503 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_VBI
+1]),
3504 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_FIELD
+1]),
3507 if (0 == (btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
)) {
3508 pr_notice("%d: Oh, there (temporarily?) is no input signal. Ok, then this is harmless, don't worry ;)\n",
3512 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3514 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3520 bttv_irq_next_video(struct bttv
*btv
, struct bttv_buffer_set
*set
)
3522 struct bttv_buffer
*item
;
3524 memset(set
,0,sizeof(*set
));
3526 /* capture request ? */
3527 if (!list_empty(&btv
->capture
)) {
3529 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3530 if (V4L2_FIELD_HAS_TOP(item
->vb
.field
))
3532 if (V4L2_FIELD_HAS_BOTTOM(item
->vb
.field
))
3535 /* capture request for other field ? */
3536 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
) &&
3537 (item
->vb
.queue
.next
!= &btv
->capture
)) {
3538 item
= list_entry(item
->vb
.queue
.next
, struct bttv_buffer
, vb
.queue
);
3539 /* Mike Isely <isely@pobox.com> - Only check
3540 * and set up the bottom field in the logic
3541 * below. Don't ever do the top field. This
3542 * of course means that if we set up the
3543 * bottom field in the above code that we'll
3544 * actually skip a field. But that's OK.
3545 * Having processed only a single buffer this
3546 * time, then the next time around the first
3547 * available buffer should be for a top field.
3548 * That will then cause us here to set up a
3549 * top then a bottom field in the normal way.
3550 * The alternative to this understanding is
3551 * that we set up the second available buffer
3552 * as a top field, but that's out of order
3553 * since this driver always processes the top
3554 * field first - the effect will be the two
3555 * buffers being returned in the wrong order,
3556 * with the second buffer also being delayed
3557 * by one field time (owing to the fifo nature
3558 * of videobuf). Worse still, we'll be stuck
3559 * doing fields out of order now every time
3560 * until something else causes a field to be
3561 * dropped. By effectively forcing a field to
3562 * drop this way then we always get back into
3563 * sync within a single frame time. (Out of
3564 * order fields can screw up deinterlacing
3566 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
)) {
3567 if (NULL
== set
->bottom
&&
3568 V4L2_FIELD_BOTTOM
== item
->vb
.field
) {
3571 if (NULL
!= set
->top
&& NULL
!= set
->bottom
)
3577 /* screen overlay ? */
3578 if (NULL
!= btv
->screen
) {
3579 if (V4L2_FIELD_HAS_BOTH(btv
->screen
->vb
.field
)) {
3580 if (NULL
== set
->top
&& NULL
== set
->bottom
) {
3581 set
->top
= btv
->screen
;
3582 set
->bottom
= btv
->screen
;
3585 if (V4L2_FIELD_TOP
== btv
->screen
->vb
.field
&&
3587 set
->top
= btv
->screen
;
3589 if (V4L2_FIELD_BOTTOM
== btv
->screen
->vb
.field
&&
3590 NULL
== set
->bottom
) {
3591 set
->bottom
= btv
->screen
;
3596 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3597 btv
->c
.nr
, set
->top
, set
->bottom
,
3598 btv
->screen
, set
->frame_irq
, set
->top_irq
);
3603 bttv_irq_wakeup_video(struct bttv
*btv
, struct bttv_buffer_set
*wakeup
,
3604 struct bttv_buffer_set
*curr
, unsigned int state
)
3608 v4l2_get_timestamp(&ts
);
3610 if (wakeup
->top
== wakeup
->bottom
) {
3611 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3613 pr_debug("%d: wakeup: both=%p\n",
3614 btv
->c
.nr
, wakeup
->top
);
3615 wakeup
->top
->vb
.ts
= ts
;
3616 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3617 wakeup
->top
->vb
.state
= state
;
3618 wake_up(&wakeup
->top
->vb
.done
);
3621 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3623 pr_debug("%d: wakeup: top=%p\n",
3624 btv
->c
.nr
, wakeup
->top
);
3625 wakeup
->top
->vb
.ts
= ts
;
3626 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3627 wakeup
->top
->vb
.state
= state
;
3628 wake_up(&wakeup
->top
->vb
.done
);
3630 if (NULL
!= wakeup
->bottom
&& curr
->bottom
!= wakeup
->bottom
) {
3632 pr_debug("%d: wakeup: bottom=%p\n",
3633 btv
->c
.nr
, wakeup
->bottom
);
3634 wakeup
->bottom
->vb
.ts
= ts
;
3635 wakeup
->bottom
->vb
.field_count
= btv
->field_count
;
3636 wakeup
->bottom
->vb
.state
= state
;
3637 wake_up(&wakeup
->bottom
->vb
.done
);
3643 bttv_irq_wakeup_vbi(struct bttv
*btv
, struct bttv_buffer
*wakeup
,
3649 v4l2_get_timestamp(&wakeup
->vb
.ts
);
3650 wakeup
->vb
.field_count
= btv
->field_count
;
3651 wakeup
->vb
.state
= state
;
3652 wake_up(&wakeup
->vb
.done
);
3655 static void bttv_irq_timeout(unsigned long data
)
3657 struct bttv
*btv
= (struct bttv
*)data
;
3658 struct bttv_buffer_set old
,new;
3659 struct bttv_buffer
*ovbi
;
3660 struct bttv_buffer
*item
;
3661 unsigned long flags
;
3664 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3665 btv
->c
.nr
, btv
->framedrop
, btv
->irq_me
, btv
->irq_total
,
3666 btread(BT848_RISC_COUNT
));
3667 bttv_print_irqbits(btread(BT848_INT_STAT
),0);
3671 spin_lock_irqsave(&btv
->s_lock
,flags
);
3673 /* deactivate stuff */
3674 memset(&new,0,sizeof(new));
3680 bttv_buffer_activate_video(btv
, &new);
3681 bttv_buffer_activate_vbi(btv
, NULL
);
3682 bttv_set_dma(btv
, 0);
3685 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_ERROR
);
3686 bttv_irq_wakeup_vbi(btv
, ovbi
, VIDEOBUF_ERROR
);
3688 /* cancel all outstanding capture / vbi requests */
3689 while (!list_empty(&btv
->capture
)) {
3690 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3691 list_del(&item
->vb
.queue
);
3692 item
->vb
.state
= VIDEOBUF_ERROR
;
3693 wake_up(&item
->vb
.done
);
3695 while (!list_empty(&btv
->vcapture
)) {
3696 item
= list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3697 list_del(&item
->vb
.queue
);
3698 item
->vb
.state
= VIDEOBUF_ERROR
;
3699 wake_up(&item
->vb
.done
);
3703 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
3707 bttv_irq_wakeup_top(struct bttv
*btv
)
3709 struct bttv_buffer
*wakeup
= btv
->curr
.top
;
3714 spin_lock(&btv
->s_lock
);
3715 btv
->curr
.top_irq
= 0;
3716 btv
->curr
.top
= NULL
;
3717 bttv_risc_hook(btv
, RISC_SLOT_O_FIELD
, NULL
, 0);
3719 v4l2_get_timestamp(&wakeup
->vb
.ts
);
3720 wakeup
->vb
.field_count
= btv
->field_count
;
3721 wakeup
->vb
.state
= VIDEOBUF_DONE
;
3722 wake_up(&wakeup
->vb
.done
);
3723 spin_unlock(&btv
->s_lock
);
3726 static inline int is_active(struct btcx_riscmem
*risc
, u32 rc
)
3730 if (rc
> risc
->dma
+ risc
->size
)
3736 bttv_irq_switch_video(struct bttv
*btv
)
3738 struct bttv_buffer_set
new;
3739 struct bttv_buffer_set old
;
3742 spin_lock(&btv
->s_lock
);
3744 /* new buffer set */
3745 bttv_irq_next_video(btv
, &new);
3746 rc
= btread(BT848_RISC_COUNT
);
3747 if ((btv
->curr
.top
&& is_active(&btv
->curr
.top
->top
, rc
)) ||
3748 (btv
->curr
.bottom
&& is_active(&btv
->curr
.bottom
->bottom
, rc
))) {
3751 bttv_irq_debug_low_latency(btv
, rc
);
3752 spin_unlock(&btv
->s_lock
);
3759 btv
->loop_irq
&= ~1;
3760 bttv_buffer_activate_video(btv
, &new);
3761 bttv_set_dma(btv
, 0);
3764 if (UNSET
!= btv
->new_input
) {
3765 video_mux(btv
,btv
->new_input
);
3766 btv
->new_input
= UNSET
;
3769 /* wake up finished buffers */
3770 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_DONE
);
3771 spin_unlock(&btv
->s_lock
);
3775 bttv_irq_switch_vbi(struct bttv
*btv
)
3777 struct bttv_buffer
*new = NULL
;
3778 struct bttv_buffer
*old
;
3781 spin_lock(&btv
->s_lock
);
3783 if (!list_empty(&btv
->vcapture
))
3784 new = list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3787 rc
= btread(BT848_RISC_COUNT
);
3788 if (NULL
!= old
&& (is_active(&old
->top
, rc
) ||
3789 is_active(&old
->bottom
, rc
))) {
3792 bttv_irq_debug_low_latency(btv
, rc
);
3793 spin_unlock(&btv
->s_lock
);
3799 btv
->loop_irq
&= ~4;
3800 bttv_buffer_activate_vbi(btv
, new);
3801 bttv_set_dma(btv
, 0);
3803 bttv_irq_wakeup_vbi(btv
, old
, VIDEOBUF_DONE
);
3804 spin_unlock(&btv
->s_lock
);
3807 static irqreturn_t
bttv_irq(int irq
, void *dev_id
)
3815 btv
=(struct bttv
*)dev_id
;
3819 /* get/clear interrupt status bits */
3820 stat
=btread(BT848_INT_STAT
);
3821 astat
=stat
&btread(BT848_INT_MASK
);
3825 btwrite(stat
,BT848_INT_STAT
);
3827 /* get device status bits */
3828 dstat
=btread(BT848_DSTATUS
);
3831 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
3832 btv
->c
.nr
, count
, btv
->field_count
,
3833 stat
>>28, btread(BT848_RISC_COUNT
));
3834 bttv_print_irqbits(stat
,astat
);
3835 if (stat
& BT848_INT_HLOCK
)
3836 pr_cont(" HLOC => %s",
3837 dstat
& BT848_DSTATUS_HLOC
3839 if (stat
& BT848_INT_VPRES
)
3840 pr_cont(" PRES => %s",
3841 dstat
& BT848_DSTATUS_PRES
3843 if (stat
& BT848_INT_FMTCHG
)
3844 pr_cont(" NUML => %s",
3845 dstat
& BT848_DSTATUS_NUML
3850 if (astat
&BT848_INT_VSYNC
)
3853 if ((astat
& BT848_INT_GPINT
) && btv
->remote
) {
3854 bttv_input_irq(btv
);
3857 if (astat
& BT848_INT_I2CDONE
) {
3858 btv
->i2c_done
= stat
;
3859 wake_up(&btv
->i2c_queue
);
3862 if ((astat
& BT848_INT_RISCI
) && (stat
& (4<<28)))
3863 bttv_irq_switch_vbi(btv
);
3865 if ((astat
& BT848_INT_RISCI
) && (stat
& (2<<28)))
3866 bttv_irq_wakeup_top(btv
);
3868 if ((astat
& BT848_INT_RISCI
) && (stat
& (1<<28)))
3869 bttv_irq_switch_video(btv
);
3871 if ((astat
& BT848_INT_HLOCK
) && btv
->opt_automute
)
3872 /* trigger automute */
3873 audio_mux_gpio(btv
, btv
->audio_input
, btv
->mute
);
3875 if (astat
& (BT848_INT_SCERR
|BT848_INT_OCERR
)) {
3876 pr_info("%d: %s%s @ %08x,",
3878 (astat
& BT848_INT_SCERR
) ? "SCERR" : "",
3879 (astat
& BT848_INT_OCERR
) ? "OCERR" : "",
3880 btread(BT848_RISC_COUNT
));
3881 bttv_print_irqbits(stat
,astat
);
3884 bttv_print_riscaddr(btv
);
3886 if (fdsr
&& astat
& BT848_INT_FDSR
) {
3887 pr_info("%d: FDSR @ %08x\n",
3888 btv
->c
.nr
, btread(BT848_RISC_COUNT
));
3890 bttv_print_riscaddr(btv
);
3896 if (count
> 8 || !(astat
& BT848_INT_GPINT
)) {
3897 btwrite(0, BT848_INT_MASK
);
3899 pr_err("%d: IRQ lockup, cleared int mask [",
3902 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3905 btwrite(btread(BT848_INT_MASK
) & (-1 ^ BT848_INT_GPINT
),
3909 bttv_print_irqbits(stat
,astat
);
3917 return IRQ_RETVAL(handled
);
3921 /* ----------------------------------------------------------------------- */
3922 /* initialization */
3924 static void vdev_init(struct bttv
*btv
,
3925 struct video_device
*vfd
,
3926 const struct video_device
*template,
3927 const char *type_name
)
3930 vfd
->v4l2_dev
= &btv
->c
.v4l2_dev
;
3931 vfd
->release
= video_device_release_empty
;
3932 video_set_drvdata(vfd
, btv
);
3933 snprintf(vfd
->name
, sizeof(vfd
->name
), "BT%d%s %s (%s)",
3934 btv
->id
, (btv
->id
==848 && btv
->revision
==0x12) ? "A" : "",
3935 type_name
, bttv_tvcards
[btv
->c
.type
].name
);
3936 if (btv
->tuner_type
== TUNER_ABSENT
) {
3937 v4l2_disable_ioctl(vfd
, VIDIOC_G_FREQUENCY
);
3938 v4l2_disable_ioctl(vfd
, VIDIOC_S_FREQUENCY
);
3939 v4l2_disable_ioctl(vfd
, VIDIOC_G_TUNER
);
3940 v4l2_disable_ioctl(vfd
, VIDIOC_S_TUNER
);
3944 static void bttv_unregister_video(struct bttv
*btv
)
3946 video_unregister_device(&btv
->video_dev
);
3947 video_unregister_device(&btv
->vbi_dev
);
3948 video_unregister_device(&btv
->radio_dev
);
3951 /* register video4linux devices */
3952 static int bttv_register_video(struct bttv
*btv
)
3955 pr_notice("Overlay support disabled\n");
3958 vdev_init(btv
, &btv
->video_dev
, &bttv_video_template
, "video");
3960 if (video_register_device(&btv
->video_dev
, VFL_TYPE_GRABBER
,
3961 video_nr
[btv
->c
.nr
]) < 0)
3963 pr_info("%d: registered device %s\n",
3964 btv
->c
.nr
, video_device_node_name(&btv
->video_dev
));
3965 if (device_create_file(&btv
->video_dev
.dev
,
3966 &dev_attr_card
)<0) {
3967 pr_err("%d: device_create_file 'card' failed\n", btv
->c
.nr
);
3972 vdev_init(btv
, &btv
->vbi_dev
, &bttv_video_template
, "vbi");
3974 if (video_register_device(&btv
->vbi_dev
, VFL_TYPE_VBI
,
3975 vbi_nr
[btv
->c
.nr
]) < 0)
3977 pr_info("%d: registered device %s\n",
3978 btv
->c
.nr
, video_device_node_name(&btv
->vbi_dev
));
3980 if (!btv
->has_radio
)
3983 vdev_init(btv
, &btv
->radio_dev
, &radio_template
, "radio");
3984 btv
->radio_dev
.ctrl_handler
= &btv
->radio_ctrl_handler
;
3985 if (video_register_device(&btv
->radio_dev
, VFL_TYPE_RADIO
,
3986 radio_nr
[btv
->c
.nr
]) < 0)
3988 pr_info("%d: registered device %s\n",
3989 btv
->c
.nr
, video_device_node_name(&btv
->radio_dev
));
3995 bttv_unregister_video(btv
);
4000 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4001 /* response on cards with no firmware is not enabled by OF */
4002 static void pci_set_command(struct pci_dev
*dev
)
4004 #if defined(__powerpc__)
4007 pci_read_config_dword(dev
, PCI_COMMAND
, &cmd
);
4008 cmd
= (cmd
| PCI_COMMAND_MEMORY
);
4009 pci_write_config_dword(dev
, PCI_COMMAND
, cmd
);
4013 static int bttv_probe(struct pci_dev
*dev
, const struct pci_device_id
*pci_id
)
4015 struct v4l2_frequency init_freq
= {
4017 .type
= V4L2_TUNER_ANALOG_TV
,
4023 struct v4l2_ctrl_handler
*hdl
;
4025 if (bttv_num
== BTTV_MAX
)
4027 pr_info("Bt8xx card found (%d)\n", bttv_num
);
4028 bttvs
[bttv_num
] = btv
= kzalloc(sizeof(*btv
), GFP_KERNEL
);
4030 pr_err("out of memory\n");
4033 btv
->c
.nr
= bttv_num
;
4034 snprintf(btv
->c
.v4l2_dev
.name
, sizeof(btv
->c
.v4l2_dev
.name
),
4035 "bttv%d", btv
->c
.nr
);
4037 /* initialize structs / fill in defaults */
4038 mutex_init(&btv
->lock
);
4039 spin_lock_init(&btv
->s_lock
);
4040 spin_lock_init(&btv
->gpio_lock
);
4041 init_waitqueue_head(&btv
->i2c_queue
);
4042 INIT_LIST_HEAD(&btv
->c
.subs
);
4043 INIT_LIST_HEAD(&btv
->capture
);
4044 INIT_LIST_HEAD(&btv
->vcapture
);
4046 init_timer(&btv
->timeout
);
4047 btv
->timeout
.function
= bttv_irq_timeout
;
4048 btv
->timeout
.data
= (unsigned long)btv
;
4051 btv
->tuner_type
= UNSET
;
4052 btv
->new_input
= UNSET
;
4053 btv
->has_radio
=radio
[btv
->c
.nr
];
4055 /* pci stuff (init, get irq/mmio, ... */
4057 btv
->id
= dev
->device
;
4058 if (pci_enable_device(dev
)) {
4059 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4062 if (pci_set_dma_mask(dev
, DMA_BIT_MASK(32))) {
4063 pr_warn("%d: No suitable DMA available\n", btv
->c
.nr
);
4066 if (!request_mem_region(pci_resource_start(dev
,0),
4067 pci_resource_len(dev
,0),
4068 btv
->c
.v4l2_dev
.name
)) {
4069 pr_warn("%d: can't request iomem (0x%llx)\n",
4071 (unsigned long long)pci_resource_start(dev
, 0));
4074 pci_set_master(dev
);
4075 pci_set_command(dev
);
4077 result
= v4l2_device_register(&dev
->dev
, &btv
->c
.v4l2_dev
);
4079 pr_warn("%d: v4l2_device_register() failed\n", btv
->c
.nr
);
4082 hdl
= &btv
->ctrl_handler
;
4083 v4l2_ctrl_handler_init(hdl
, 20);
4084 btv
->c
.v4l2_dev
.ctrl_handler
= hdl
;
4085 v4l2_ctrl_handler_init(&btv
->radio_ctrl_handler
, 6);
4087 btv
->revision
= dev
->revision
;
4088 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
4089 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4090 bttv_num
, btv
->id
, btv
->revision
, pci_name(dev
),
4091 btv
->c
.pci
->irq
, lat
,
4092 (unsigned long long)pci_resource_start(dev
, 0));
4095 btv
->bt848_mmio
= ioremap(pci_resource_start(dev
, 0), 0x1000);
4096 if (NULL
== btv
->bt848_mmio
) {
4097 pr_err("%d: ioremap() failed\n", btv
->c
.nr
);
4105 /* disable irqs, register irq handler */
4106 btwrite(0, BT848_INT_MASK
);
4107 result
= request_irq(btv
->c
.pci
->irq
, bttv_irq
,
4108 IRQF_SHARED
, btv
->c
.v4l2_dev
.name
, (void *)btv
);
4110 pr_err("%d: can't get IRQ %d\n",
4111 bttv_num
, btv
->c
.pci
->irq
);
4115 if (0 != bttv_handle_chipset(btv
)) {
4120 /* init options from insmod args */
4121 btv
->opt_combfilter
= combfilter
;
4122 bttv_ctrl_combfilter
.def
= combfilter
;
4123 bttv_ctrl_lumafilter
.def
= lumafilter
;
4124 btv
->opt_automute
= automute
;
4125 bttv_ctrl_automute
.def
= automute
;
4126 bttv_ctrl_agc_crush
.def
= agc_crush
;
4127 btv
->opt_vcr_hack
= vcr_hack
;
4128 bttv_ctrl_vcr_hack
.def
= vcr_hack
;
4129 bttv_ctrl_whitecrush_upper
.def
= whitecrush_upper
;
4130 bttv_ctrl_whitecrush_lower
.def
= whitecrush_lower
;
4131 btv
->opt_uv_ratio
= uv_ratio
;
4132 bttv_ctrl_uv_ratio
.def
= uv_ratio
;
4133 bttv_ctrl_full_luma
.def
= full_luma_range
;
4134 bttv_ctrl_coring
.def
= coring
;
4136 /* fill struct bttv with some useful defaults */
4137 btv
->init
.btv
= btv
;
4138 btv
->init
.ov
.w
.width
= 320;
4139 btv
->init
.ov
.w
.height
= 240;
4140 btv
->init
.fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
4141 btv
->init
.width
= 320;
4142 btv
->init
.height
= 240;
4143 btv
->init
.ov
.w
.width
= 320;
4144 btv
->init
.ov
.w
.height
= 240;
4145 btv
->init
.ov
.field
= V4L2_FIELD_INTERLACED
;
4148 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4149 V4L2_CID_BRIGHTNESS
, 0, 0xff00, 0x100, 32768);
4150 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4151 V4L2_CID_CONTRAST
, 0, 0xff80, 0x80, 0x6c00);
4152 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4153 V4L2_CID_SATURATION
, 0, 0xff80, 0x80, 32768);
4154 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4155 V4L2_CID_COLOR_KILLER
, 0, 1, 1, 0);
4156 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4157 V4L2_CID_HUE
, 0, 0xff00, 0x100, 32768);
4158 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4159 V4L2_CID_CHROMA_AGC
, 0, 1, 1, !!chroma_agc
);
4160 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4161 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
4162 if (btv
->volume_gpio
)
4163 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4164 V4L2_CID_AUDIO_VOLUME
, 0, 0xff00, 0x100, 0xff00);
4165 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_combfilter
, NULL
);
4166 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_automute
, NULL
);
4167 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_lumafilter
, NULL
);
4168 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_agc_crush
, NULL
);
4169 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_vcr_hack
, NULL
);
4170 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_whitecrush_lower
, NULL
);
4171 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_whitecrush_upper
, NULL
);
4172 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_uv_ratio
, NULL
);
4173 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_full_luma
, NULL
);
4174 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_coring
, NULL
);
4176 /* initialize hardware */
4178 bttv_gpio_tracking(btv
,"pre-init");
4180 bttv_risc_init_main(btv
);
4184 btwrite(0x00, BT848_GPIO_REG_INP
);
4185 btwrite(0x00, BT848_GPIO_OUT_EN
);
4187 bttv_gpio_tracking(btv
,"init");
4189 /* needs to be done before i2c is registered */
4190 bttv_init_card1(btv
);
4192 /* register i2c + gpio */
4195 /* some card-specific stuff (needs working i2c) */
4196 bttv_init_card2(btv
);
4197 bttv_init_tuner(btv
);
4198 if (btv
->tuner_type
!= TUNER_ABSENT
) {
4199 bttv_set_frequency(btv
, &init_freq
);
4200 btv
->radio_freq
= 90500 * 16; /* 90.5Mhz default */
4202 btv
->std
= V4L2_STD_PAL
;
4204 if (!bttv_tvcards
[btv
->c
.type
].no_video
)
4205 v4l2_ctrl_handler_setup(hdl
);
4207 result
= hdl
->error
;
4213 /* register video4linux + input */
4214 if (!bttv_tvcards
[btv
->c
.type
].no_video
) {
4215 v4l2_ctrl_add_handler(&btv
->radio_ctrl_handler
, hdl
,
4216 v4l2_ctrl_radio_filter
);
4217 if (btv
->radio_ctrl_handler
.error
) {
4218 result
= btv
->radio_ctrl_handler
.error
;
4221 set_input(btv
, 0, btv
->tvnorm
);
4222 bttv_crop_reset(&btv
->crop
[0], btv
->tvnorm
);
4223 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
4224 disclaim_vbi_lines(btv
);
4225 disclaim_video_lines(btv
);
4226 bttv_register_video(btv
);
4229 /* add subdevices and autoload dvb-bt8xx if needed */
4230 if (bttv_tvcards
[btv
->c
.type
].has_dvb
) {
4231 bttv_sub_add_device(&btv
->c
, "dvb");
4232 request_modules(btv
);
4236 init_bttv_i2c_ir(btv
);
4237 bttv_input_init(btv
);
4240 /* everything is fine */
4245 free_irq(btv
->c
.pci
->irq
,btv
);
4248 v4l2_ctrl_handler_free(&btv
->ctrl_handler
);
4249 v4l2_ctrl_handler_free(&btv
->radio_ctrl_handler
);
4250 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4253 if (btv
->bt848_mmio
)
4254 iounmap(btv
->bt848_mmio
);
4255 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4256 pci_resource_len(btv
->c
.pci
,0));
4257 pci_disable_device(btv
->c
.pci
);
4261 static void bttv_remove(struct pci_dev
*pci_dev
)
4263 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4264 struct bttv
*btv
= to_bttv(v4l2_dev
);
4267 pr_info("%d: unloading\n", btv
->c
.nr
);
4269 if (bttv_tvcards
[btv
->c
.type
].has_dvb
)
4270 flush_request_modules(btv
);
4272 /* shutdown everything (DMA+IRQs) */
4273 btand(~15, BT848_GPIO_DMA_CTL
);
4274 btwrite(0, BT848_INT_MASK
);
4275 btwrite(~0x0, BT848_INT_STAT
);
4276 btwrite(0x0, BT848_GPIO_OUT_EN
);
4278 bttv_gpio_tracking(btv
,"cleanup");
4280 /* tell gpio modules we are leaving ... */
4282 bttv_input_fini(btv
);
4283 bttv_sub_del_devices(&btv
->c
);
4285 /* unregister i2c_bus + input */
4288 /* unregister video4linux */
4289 bttv_unregister_video(btv
);
4291 /* free allocated memory */
4292 v4l2_ctrl_handler_free(&btv
->ctrl_handler
);
4293 v4l2_ctrl_handler_free(&btv
->radio_ctrl_handler
);
4294 btcx_riscmem_free(btv
->c
.pci
,&btv
->main
);
4296 /* free resources */
4297 free_irq(btv
->c
.pci
->irq
,btv
);
4298 iounmap(btv
->bt848_mmio
);
4299 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4300 pci_resource_len(btv
->c
.pci
,0));
4301 pci_disable_device(btv
->c
.pci
);
4303 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4304 bttvs
[btv
->c
.nr
] = NULL
;
4311 static int bttv_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
4313 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4314 struct bttv
*btv
= to_bttv(v4l2_dev
);
4315 struct bttv_buffer_set idle
;
4316 unsigned long flags
;
4318 dprintk("%d: suspend %d\n", btv
->c
.nr
, state
.event
);
4320 /* stop dma + irqs */
4321 spin_lock_irqsave(&btv
->s_lock
,flags
);
4322 memset(&idle
, 0, sizeof(idle
));
4323 btv
->state
.video
= btv
->curr
;
4324 btv
->state
.vbi
= btv
->cvbi
;
4325 btv
->state
.loop_irq
= btv
->loop_irq
;
4328 bttv_buffer_activate_video(btv
, &idle
);
4329 bttv_buffer_activate_vbi(btv
, NULL
);
4330 bttv_set_dma(btv
, 0);
4331 btwrite(0, BT848_INT_MASK
);
4332 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4334 /* save bt878 state */
4335 btv
->state
.gpio_enable
= btread(BT848_GPIO_OUT_EN
);
4336 btv
->state
.gpio_data
= gpio_read();
4338 /* save pci state */
4339 pci_save_state(pci_dev
);
4340 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
4341 pci_disable_device(pci_dev
);
4342 btv
->state
.disabled
= 1;
4347 static int bttv_resume(struct pci_dev
*pci_dev
)
4349 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4350 struct bttv
*btv
= to_bttv(v4l2_dev
);
4351 unsigned long flags
;
4354 dprintk("%d: resume\n", btv
->c
.nr
);
4356 /* restore pci state */
4357 if (btv
->state
.disabled
) {
4358 err
=pci_enable_device(pci_dev
);
4360 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4363 btv
->state
.disabled
= 0;
4365 err
=pci_set_power_state(pci_dev
, PCI_D0
);
4367 pci_disable_device(pci_dev
);
4368 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4369 btv
->state
.disabled
= 1;
4373 pci_restore_state(pci_dev
);
4375 /* restore bt878 state */
4376 bttv_reinit_bt848(btv
);
4377 gpio_inout(0xffffff, btv
->state
.gpio_enable
);
4378 gpio_write(btv
->state
.gpio_data
);
4381 spin_lock_irqsave(&btv
->s_lock
,flags
);
4382 btv
->curr
= btv
->state
.video
;
4383 btv
->cvbi
= btv
->state
.vbi
;
4384 btv
->loop_irq
= btv
->state
.loop_irq
;
4385 bttv_buffer_activate_video(btv
, &btv
->curr
);
4386 bttv_buffer_activate_vbi(btv
, btv
->cvbi
);
4387 bttv_set_dma(btv
, 0);
4388 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4393 static struct pci_device_id bttv_pci_tbl
[] = {
4394 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT848
), 0},
4395 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT849
), 0},
4396 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT878
), 0},
4397 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT879
), 0},
4398 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_FUSION879
), 0},
4402 MODULE_DEVICE_TABLE(pci
, bttv_pci_tbl
);
4404 static struct pci_driver bttv_pci_driver
= {
4406 .id_table
= bttv_pci_tbl
,
4407 .probe
= bttv_probe
,
4408 .remove
= bttv_remove
,
4410 .suspend
= bttv_suspend
,
4411 .resume
= bttv_resume
,
4415 static int __init
bttv_init_module(void)
4421 pr_info("driver version %s loaded\n", BTTV_VERSION
);
4422 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
4424 if (gbufsize
> BTTV_MAX_FBUF
)
4425 gbufsize
= BTTV_MAX_FBUF
;
4426 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
4428 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4429 gbuffers
, gbufsize
>> 10, gbufsize
>> PAGE_SHIFT
);
4431 bttv_check_chipset();
4433 ret
= bus_register(&bttv_sub_bus_type
);
4435 pr_warn("bus_register error: %d\n", ret
);
4438 ret
= pci_register_driver(&bttv_pci_driver
);
4440 bus_unregister(&bttv_sub_bus_type
);
4445 static void __exit
bttv_cleanup_module(void)
4447 pci_unregister_driver(&bttv_pci_driver
);
4448 bus_unregister(&bttv_sub_bus_type
);
4451 module_init(bttv_init_module
);
4452 module_exit(bttv_cleanup_module
);