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/v4l2-chip-ident.h>
54 #include <media/tvaudio.h>
55 #include <media/msp3400.h>
57 #include <linux/dma-mapping.h>
60 #include <asm/byteorder.h>
62 #include <media/saa6588.h>
64 #define BTTV_VERSION "0.9.19"
66 unsigned int bttv_num
; /* number of Bt848s in use */
67 struct bttv
*bttvs
[BTTV_MAX
];
69 unsigned int bttv_debug
;
70 unsigned int bttv_verbose
= 1;
71 unsigned int bttv_gpio
;
73 /* config variables */
75 static unsigned int bigendian
=1;
77 static unsigned int bigendian
;
79 static unsigned int radio
[BTTV_MAX
];
80 static unsigned int irq_debug
;
81 static unsigned int gbuffers
= 8;
82 static unsigned int gbufsize
= 0x208000;
83 static unsigned int reset_crop
= 1;
85 static int video_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
86 static int radio_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
87 static int vbi_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
88 static int debug_latency
;
89 static int disable_ir
;
91 static unsigned int fdsr
;
94 static unsigned int combfilter
;
95 static unsigned int lumafilter
;
96 static unsigned int automute
= 1;
97 static unsigned int chroma_agc
;
98 static unsigned int agc_crush
= 1;
99 static unsigned int whitecrush_upper
= 0xCF;
100 static unsigned int whitecrush_lower
= 0x7F;
101 static unsigned int vcr_hack
;
102 static unsigned int irq_iswitch
;
103 static unsigned int uv_ratio
= 50;
104 static unsigned int full_luma_range
;
105 static unsigned int coring
;
107 /* API features (turn on/off stuff for testing) */
108 static unsigned int v4l2
= 1;
111 module_param(bttv_verbose
, int, 0644);
112 module_param(bttv_gpio
, int, 0644);
113 module_param(bttv_debug
, int, 0644);
114 module_param(irq_debug
, int, 0644);
115 module_param(debug_latency
, int, 0644);
116 module_param(disable_ir
, int, 0444);
118 module_param(fdsr
, int, 0444);
119 module_param(gbuffers
, int, 0444);
120 module_param(gbufsize
, int, 0444);
121 module_param(reset_crop
, int, 0444);
123 module_param(v4l2
, int, 0644);
124 module_param(bigendian
, int, 0644);
125 module_param(irq_iswitch
, int, 0644);
126 module_param(combfilter
, int, 0444);
127 module_param(lumafilter
, int, 0444);
128 module_param(automute
, int, 0444);
129 module_param(chroma_agc
, int, 0444);
130 module_param(agc_crush
, int, 0444);
131 module_param(whitecrush_upper
, int, 0444);
132 module_param(whitecrush_lower
, int, 0444);
133 module_param(vcr_hack
, int, 0444);
134 module_param(uv_ratio
, int, 0444);
135 module_param(full_luma_range
, int, 0444);
136 module_param(coring
, int, 0444);
138 module_param_array(radio
, int, NULL
, 0444);
139 module_param_array(video_nr
, int, NULL
, 0444);
140 module_param_array(radio_nr
, int, NULL
, 0444);
141 module_param_array(vbi_nr
, int, NULL
, 0444);
143 MODULE_PARM_DESC(radio
, "The TV card supports radio, default is 0 (no)");
144 MODULE_PARM_DESC(bigendian
, "byte order of the framebuffer, default is native endian");
145 MODULE_PARM_DESC(bttv_verbose
, "verbose startup messages, default is 1 (yes)");
146 MODULE_PARM_DESC(bttv_gpio
, "log gpio changes, default is 0 (no)");
147 MODULE_PARM_DESC(bttv_debug
, "debug messages, default is 0 (no)");
148 MODULE_PARM_DESC(irq_debug
, "irq handler debug messages, default is 0 (no)");
149 MODULE_PARM_DESC(disable_ir
, "disable infrared remote support");
150 MODULE_PARM_DESC(gbuffers
, "number of capture buffers. range 2-32, default 8");
151 MODULE_PARM_DESC(gbufsize
, "size of the capture buffers, default is 0x208000");
152 MODULE_PARM_DESC(reset_crop
, "reset cropping parameters at open(), default "
153 "is 1 (yes) for compatibility with older applications");
154 MODULE_PARM_DESC(automute
, "mute audio on bad/missing video signal, default is 1 (yes)");
155 MODULE_PARM_DESC(chroma_agc
, "enables the AGC of chroma signal, default is 0 (no)");
156 MODULE_PARM_DESC(agc_crush
, "enables the luminance AGC crush, default is 1 (yes)");
157 MODULE_PARM_DESC(whitecrush_upper
, "sets the white crush upper value, default is 207");
158 MODULE_PARM_DESC(whitecrush_lower
, "sets the white crush lower value, default is 127");
159 MODULE_PARM_DESC(vcr_hack
, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
160 MODULE_PARM_DESC(irq_iswitch
, "switch inputs in irq handler");
161 MODULE_PARM_DESC(uv_ratio
, "ratio between u and v gains, default is 50");
162 MODULE_PARM_DESC(full_luma_range
, "use the full luma range, default is 0 (no)");
163 MODULE_PARM_DESC(coring
, "set the luma coring level, default is 0 (no)");
164 MODULE_PARM_DESC(video_nr
, "video device numbers");
165 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
166 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
168 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
169 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
170 MODULE_LICENSE("GPL");
171 MODULE_VERSION(BTTV_VERSION
);
173 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
174 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
175 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
176 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
177 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
178 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
179 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
180 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
181 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
182 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
184 /* ----------------------------------------------------------------------- */
187 static ssize_t
show_card(struct device
*cd
,
188 struct device_attribute
*attr
, char *buf
)
190 struct video_device
*vfd
= container_of(cd
, struct video_device
, dev
);
191 struct bttv
*btv
= video_get_drvdata(vfd
);
192 return sprintf(buf
, "%d\n", btv
? btv
->c
.type
: UNSET
);
194 static DEVICE_ATTR(card
, S_IRUGO
, show_card
, NULL
);
196 /* ----------------------------------------------------------------------- */
197 /* dvb auto-load setup */
198 #if defined(CONFIG_MODULES) && defined(MODULE)
199 static void request_module_async(struct work_struct
*work
)
201 request_module("dvb-bt8xx");
204 static void request_modules(struct bttv
*dev
)
206 INIT_WORK(&dev
->request_module_wk
, request_module_async
);
207 schedule_work(&dev
->request_module_wk
);
210 static void flush_request_modules(struct bttv
*dev
)
212 flush_work(&dev
->request_module_wk
);
215 #define request_modules(dev)
216 #define flush_request_modules(dev) do {} while(0)
217 #endif /* CONFIG_MODULES */
220 /* ----------------------------------------------------------------------- */
223 /* special timing tables from conexant... */
224 static u8 SRAM_Table
[][60] =
226 /* PAL digital input over GPIO[7:0] */
228 45, // 45 bytes following
229 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
230 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
231 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
232 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
233 0x37,0x00,0xAF,0x21,0x00
235 /* NTSC digital input over GPIO[7:0] */
237 51, // 51 bytes following
238 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
239 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
240 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
241 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
242 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
245 // TGB_NTSC392 // quartzsight
246 // This table has been modified to be used for Fusion Rev D
248 0x2A, // size of table = 42
249 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
250 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
251 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
252 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
257 /* minhdelayx1 first video pixel we can capture on a line and
258 hdelayx1 start of active video, both relative to rising edge of
259 /HRESET pulse (0H) in 1 / fCLKx1.
260 swidth width of active video and
261 totalwidth total line width, both in 1 / fCLKx1.
262 sqwidth total line width in square pixels.
263 vdelay start of active video in 2 * field lines relative to
264 trailing edge of /VRESET pulse (VDELAY register).
265 sheight height of active video in 2 * field lines.
266 extraheight Added to sheight for cropcap.bounds.height only
267 videostart0 ITU-R frame line number of the line corresponding
268 to vdelay in the first field. */
269 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
270 vdelay, sheight, extraheight, videostart0) \
271 .cropcap.bounds.left = minhdelayx1, \
272 /* * 2 because vertically we count field lines times two, */ \
273 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
274 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
275 /* 4 is a safety margin at the end of the line. */ \
276 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
277 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
279 .cropcap.defrect.left = hdelayx1, \
280 .cropcap.defrect.top = (videostart0) * 2, \
281 .cropcap.defrect.width = swidth, \
282 .cropcap.defrect.height = sheight, \
283 .cropcap.pixelaspect.numerator = totalwidth, \
284 .cropcap.pixelaspect.denominator = sqwidth,
286 const struct bttv_tvnorm bttv_tvnorms
[] = {
288 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
289 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
291 .v4l2_id
= V4L2_STD_PAL
,
299 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
300 .scaledtwidth
= 1135,
304 .vbipack
= 255, /* min (2048 / 4, 0x1ff) & 0xff */
306 /* ITU-R frame line number of the first VBI line
307 we can capture, of the first and second field.
308 The last line is determined by cropcap.bounds. */
309 .vbistart
= { 7, 320 },
310 CROPCAP(/* minhdelayx1 */ 68,
312 /* Should be (768 * 1135 + 944 / 2) / 944.
313 cropcap.defrect is used for image width
314 checks, so we keep the old value 924. */
316 /* totalwidth */ 1135,
320 /* bt878 (and bt848?) can capture another
321 line below active video. */
323 /* videostart0 */ 23)
325 .v4l2_id
= V4L2_STD_NTSC_M
| V4L2_STD_NTSC_M_KR
,
333 .iform
= (BT848_IFORM_NTSC
|BT848_IFORM_XT0
),
338 .vbipack
= 144, /* min (1600 / 4, 0x1ff) & 0xff */
340 .vbistart
= { 10, 273 },
341 CROPCAP(/* minhdelayx1 */ 68,
343 /* Should be (640 * 910 + 780 / 2) / 780? */
345 /* totalwidth */ 910,
350 /* videostart0 */ 23)
352 .v4l2_id
= V4L2_STD_SECAM
,
360 .iform
= (BT848_IFORM_SECAM
|BT848_IFORM_XT1
),
361 .scaledtwidth
= 1135,
366 .sram
= 0, /* like PAL, correct? */
367 .vbistart
= { 7, 320 },
368 CROPCAP(/* minhdelayx1 */ 68,
371 /* totalwidth */ 1135,
376 /* videostart0 */ 23)
378 .v4l2_id
= V4L2_STD_PAL_Nc
,
386 .iform
= (BT848_IFORM_PAL_NC
|BT848_IFORM_XT0
),
393 .vbistart
= { 7, 320 },
394 CROPCAP(/* minhdelayx1 */ 68,
396 /* swidth */ (640 * 910 + 780 / 2) / 780,
397 /* totalwidth */ 910,
402 /* videostart0 */ 23)
404 .v4l2_id
= V4L2_STD_PAL_M
,
412 .iform
= (BT848_IFORM_PAL_M
|BT848_IFORM_XT0
),
419 .vbistart
= { 10, 273 },
420 CROPCAP(/* minhdelayx1 */ 68,
422 /* swidth */ (640 * 910 + 780 / 2) / 780,
423 /* totalwidth */ 910,
428 /* videostart0 */ 23)
430 .v4l2_id
= V4L2_STD_PAL_N
,
438 .iform
= (BT848_IFORM_PAL_N
|BT848_IFORM_XT1
),
445 .vbistart
= { 7, 320 },
446 CROPCAP(/* minhdelayx1 */ 68,
448 /* swidth */ (768 * 1135 + 944 / 2) / 944,
449 /* totalwidth */ 1135,
454 /* videostart0 */ 23)
456 .v4l2_id
= V4L2_STD_NTSC_M_JP
,
464 .iform
= (BT848_IFORM_NTSC_J
|BT848_IFORM_XT0
),
471 .vbistart
= { 10, 273 },
472 CROPCAP(/* minhdelayx1 */ 68,
474 /* swidth */ (640 * 910 + 780 / 2) / 780,
475 /* totalwidth */ 910,
480 /* videostart0 */ 23)
482 /* that one hopefully works with the strange timing
483 * which video recorders produce when playing a NTSC
484 * tape on a PAL TV ... */
485 .v4l2_id
= V4L2_STD_PAL_60
,
493 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
494 .scaledtwidth
= 1135,
501 .vbistart
= { 10, 273 },
502 CROPCAP(/* minhdelayx1 */ 68,
505 /* totalwidth */ 1135,
510 /* videostart0 */ 23)
513 static const unsigned int BTTV_TVNORMS
= ARRAY_SIZE(bttv_tvnorms
);
515 /* ----------------------------------------------------------------------- */
517 packed pixel formats must come first */
518 static const struct bttv_format formats
[] = {
520 .name
= "8 bpp, gray",
521 .fourcc
= V4L2_PIX_FMT_GREY
,
522 .btformat
= BT848_COLOR_FMT_Y8
,
524 .flags
= FORMAT_FLAGS_PACKED
,
526 .name
= "8 bpp, dithered color",
527 .fourcc
= V4L2_PIX_FMT_HI240
,
528 .btformat
= BT848_COLOR_FMT_RGB8
,
530 .flags
= FORMAT_FLAGS_PACKED
| FORMAT_FLAGS_DITHER
,
532 .name
= "15 bpp RGB, le",
533 .fourcc
= V4L2_PIX_FMT_RGB555
,
534 .btformat
= BT848_COLOR_FMT_RGB15
,
536 .flags
= FORMAT_FLAGS_PACKED
,
538 .name
= "15 bpp RGB, be",
539 .fourcc
= V4L2_PIX_FMT_RGB555X
,
540 .btformat
= BT848_COLOR_FMT_RGB15
,
541 .btswap
= 0x03, /* byteswap */
543 .flags
= FORMAT_FLAGS_PACKED
,
545 .name
= "16 bpp RGB, le",
546 .fourcc
= V4L2_PIX_FMT_RGB565
,
547 .btformat
= BT848_COLOR_FMT_RGB16
,
549 .flags
= FORMAT_FLAGS_PACKED
,
551 .name
= "16 bpp RGB, be",
552 .fourcc
= V4L2_PIX_FMT_RGB565X
,
553 .btformat
= BT848_COLOR_FMT_RGB16
,
554 .btswap
= 0x03, /* byteswap */
556 .flags
= FORMAT_FLAGS_PACKED
,
558 .name
= "24 bpp RGB, le",
559 .fourcc
= V4L2_PIX_FMT_BGR24
,
560 .btformat
= BT848_COLOR_FMT_RGB24
,
562 .flags
= FORMAT_FLAGS_PACKED
,
564 .name
= "32 bpp RGB, le",
565 .fourcc
= V4L2_PIX_FMT_BGR32
,
566 .btformat
= BT848_COLOR_FMT_RGB32
,
568 .flags
= FORMAT_FLAGS_PACKED
,
570 .name
= "32 bpp RGB, be",
571 .fourcc
= V4L2_PIX_FMT_RGB32
,
572 .btformat
= BT848_COLOR_FMT_RGB32
,
573 .btswap
= 0x0f, /* byte+word swap */
575 .flags
= FORMAT_FLAGS_PACKED
,
577 .name
= "4:2:2, packed, YUYV",
578 .fourcc
= V4L2_PIX_FMT_YUYV
,
579 .btformat
= BT848_COLOR_FMT_YUY2
,
581 .flags
= FORMAT_FLAGS_PACKED
,
583 .name
= "4:2:2, packed, UYVY",
584 .fourcc
= V4L2_PIX_FMT_UYVY
,
585 .btformat
= BT848_COLOR_FMT_YUY2
,
586 .btswap
= 0x03, /* byteswap */
588 .flags
= FORMAT_FLAGS_PACKED
,
590 .name
= "4:2:2, planar, Y-Cb-Cr",
591 .fourcc
= V4L2_PIX_FMT_YUV422P
,
592 .btformat
= BT848_COLOR_FMT_YCrCb422
,
594 .flags
= FORMAT_FLAGS_PLANAR
,
598 .name
= "4:2:0, planar, Y-Cb-Cr",
599 .fourcc
= V4L2_PIX_FMT_YUV420
,
600 .btformat
= BT848_COLOR_FMT_YCrCb422
,
602 .flags
= FORMAT_FLAGS_PLANAR
,
606 .name
= "4:2:0, planar, Y-Cr-Cb",
607 .fourcc
= V4L2_PIX_FMT_YVU420
,
608 .btformat
= BT848_COLOR_FMT_YCrCb422
,
610 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
614 .name
= "4:1:1, planar, Y-Cb-Cr",
615 .fourcc
= V4L2_PIX_FMT_YUV411P
,
616 .btformat
= BT848_COLOR_FMT_YCrCb411
,
618 .flags
= FORMAT_FLAGS_PLANAR
,
622 .name
= "4:1:0, planar, Y-Cb-Cr",
623 .fourcc
= V4L2_PIX_FMT_YUV410
,
624 .btformat
= BT848_COLOR_FMT_YCrCb411
,
626 .flags
= FORMAT_FLAGS_PLANAR
,
630 .name
= "4:1:0, planar, Y-Cr-Cb",
631 .fourcc
= V4L2_PIX_FMT_YVU410
,
632 .btformat
= BT848_COLOR_FMT_YCrCb411
,
634 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
638 .name
= "raw scanlines",
640 .btformat
= BT848_COLOR_FMT_RAW
,
642 .flags
= FORMAT_FLAGS_RAW
,
645 static const unsigned int FORMATS
= ARRAY_SIZE(formats
);
647 /* ----------------------------------------------------------------------- */
648 /* resource management */
651 RESOURCE_ allocated by freed by
653 VIDEO_READ bttv_read 1) bttv_read 2)
655 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
656 VIDIOC_QBUF 1) bttv_release
659 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
660 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
663 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
664 VIDIOC_QBUF 1) bttv_release
665 bttv_read, bttv_poll 1) 4)
667 1) The resource must be allocated when we enter buffer prepare functions
668 and remain allocated while buffers are in the DMA queue.
669 2) This is a single frame read.
670 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
671 RESOURCE_OVERLAY is allocated.
672 4) This is a continuous read, implies VIDIOC_STREAMON.
674 Note this driver permits video input and standard changes regardless if
675 resources are allocated.
678 #define VBI_RESOURCES (RESOURCE_VBI)
679 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
680 RESOURCE_VIDEO_STREAM | \
684 int check_alloc_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bit
)
686 int xbits
; /* mutual exclusive resources */
688 if (fh
->resources
& bit
)
689 /* have it already allocated */
693 if (bit
& (RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
))
694 xbits
|= RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
;
697 if (btv
->resources
& xbits
) {
698 /* no, someone else uses it */
702 if ((bit
& VIDEO_RESOURCES
)
703 && 0 == (btv
->resources
& VIDEO_RESOURCES
)) {
704 /* Do crop - use current, don't - use default parameters. */
705 __s32 top
= btv
->crop
[!!fh
->do_crop
].rect
.top
;
707 if (btv
->vbi_end
> top
)
710 /* We cannot capture the same line as video and VBI data.
711 Claim scan lines crop[].rect.top to bottom. */
712 btv
->crop_start
= top
;
713 } else if (bit
& VBI_RESOURCES
) {
714 __s32 end
= fh
->vbi_fmt
.end
;
716 if (end
> btv
->crop_start
)
719 /* Claim scan lines above fh->vbi_fmt.end. */
723 /* it's free, grab it */
724 fh
->resources
|= bit
;
725 btv
->resources
|= bit
;
733 int check_btres(struct bttv_fh
*fh
, int bit
)
735 return (fh
->resources
& bit
);
739 int locked_btres(struct bttv
*btv
, int bit
)
741 return (btv
->resources
& bit
);
744 /* Call with btv->lock down. */
746 disclaim_vbi_lines(struct bttv
*btv
)
751 /* Call with btv->lock down. */
753 disclaim_video_lines(struct bttv
*btv
)
755 const struct bttv_tvnorm
*tvnorm
;
758 tvnorm
= &bttv_tvnorms
[btv
->tvnorm
];
759 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
760 + tvnorm
->cropcap
.bounds
.height
;
762 /* VBI capturing ends at VDELAY, start of video capturing, no
763 matter how many lines the VBI RISC program expects. When video
764 capturing is off, it shall no longer "preempt" VBI capturing,
765 so we set VDELAY to maximum. */
766 crop
= btread(BT848_E_CROP
) | 0xc0;
767 btwrite(crop
, BT848_E_CROP
);
768 btwrite(0xfe, BT848_E_VDELAY_LO
);
769 btwrite(crop
, BT848_O_CROP
);
770 btwrite(0xfe, BT848_O_VDELAY_LO
);
774 void free_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bits
)
776 if ((fh
->resources
& bits
) != bits
) {
777 /* trying to free resources not allocated by us ... */
778 pr_err("BUG! (btres)\n");
780 fh
->resources
&= ~bits
;
781 btv
->resources
&= ~bits
;
783 bits
= btv
->resources
;
785 if (0 == (bits
& VIDEO_RESOURCES
))
786 disclaim_video_lines(btv
);
788 if (0 == (bits
& VBI_RESOURCES
))
789 disclaim_vbi_lines(btv
);
792 /* ----------------------------------------------------------------------- */
793 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
795 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
796 PLL_X = Reference pre-divider (0=1, 1=2)
797 PLL_C = Post divider (0=6, 1=4)
798 PLL_I = Integer input
799 PLL_F = Fractional input
801 F_input = 28.636363 MHz:
802 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
805 static void set_pll_freq(struct bttv
*btv
, unsigned int fin
, unsigned int fout
)
807 unsigned char fl
, fh
, fi
;
809 /* prevent overflows */
822 btwrite(fl
, BT848_PLL_F_LO
);
823 btwrite(fh
, BT848_PLL_F_HI
);
824 btwrite(fi
|BT848_PLL_X
, BT848_PLL_XCI
);
827 static void set_pll(struct bttv
*btv
)
831 if (!btv
->pll
.pll_crystal
)
834 if (btv
->pll
.pll_ofreq
== btv
->pll
.pll_current
) {
835 dprintk("%d: PLL: no change required\n", btv
->c
.nr
);
839 if (btv
->pll
.pll_ifreq
== btv
->pll
.pll_ofreq
) {
841 if (btv
->pll
.pll_current
== 0)
844 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
845 btv
->c
.nr
, btv
->pll
.pll_ifreq
);
846 btwrite(0x00,BT848_TGCTRL
);
847 btwrite(0x00,BT848_PLL_XCI
);
848 btv
->pll
.pll_current
= 0;
853 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
855 btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
856 set_pll_freq(btv
, btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
858 for (i
=0; i
<10; i
++) {
859 /* Let other people run while the PLL stabilizes */
862 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_PLOCK
) {
863 btwrite(0,BT848_DSTATUS
);
865 btwrite(0x08,BT848_TGCTRL
);
866 btv
->pll
.pll_current
= btv
->pll
.pll_ofreq
;
868 pr_info("PLL set ok\n");
872 btv
->pll
.pll_current
= -1;
874 pr_info("Setting PLL failed\n");
878 /* used to switch between the bt848's analog/digital video capture modes */
879 static void bt848A_set_timing(struct bttv
*btv
)
882 int table_idx
= bttv_tvnorms
[btv
->tvnorm
].sram
;
883 int fsc
= bttv_tvnorms
[btv
->tvnorm
].Fsc
;
885 if (btv
->input
== btv
->dig
) {
886 dprintk("%d: load digital timing table (table_idx=%d)\n",
887 btv
->c
.nr
,table_idx
);
889 /* timing change...reset timing generator address */
890 btwrite(0x00, BT848_TGCTRL
);
891 btwrite(0x02, BT848_TGCTRL
);
892 btwrite(0x00, BT848_TGCTRL
);
894 len
=SRAM_Table
[table_idx
][0];
895 for(i
= 1; i
<= len
; i
++)
896 btwrite(SRAM_Table
[table_idx
][i
],BT848_TGLB
);
897 btv
->pll
.pll_ofreq
= 27000000;
900 btwrite(0x11, BT848_TGCTRL
);
901 btwrite(0x41, BT848_DVSIF
);
903 btv
->pll
.pll_ofreq
= fsc
;
905 btwrite(0x0, BT848_DVSIF
);
909 /* ----------------------------------------------------------------------- */
911 static void bt848_bright(struct bttv
*btv
, int bright
)
915 // printk("set bright: %d\n", bright); // DEBUG
916 btv
->bright
= bright
;
918 /* We want -128 to 127 we get 0-65535 */
919 value
= (bright
>> 8) - 128;
920 btwrite(value
& 0xff, BT848_BRIGHT
);
923 static void bt848_hue(struct bttv
*btv
, int hue
)
930 value
= (hue
>> 8) - 128;
931 btwrite(value
& 0xff, BT848_HUE
);
934 static void bt848_contrast(struct bttv
*btv
, int cont
)
938 btv
->contrast
= cont
;
942 hibit
= (value
>> 6) & 4;
943 btwrite(value
& 0xff, BT848_CONTRAST_LO
);
944 btaor(hibit
, ~4, BT848_E_CONTROL
);
945 btaor(hibit
, ~4, BT848_O_CONTROL
);
948 static void bt848_sat(struct bttv
*btv
, int color
)
950 int val_u
,val_v
,hibits
;
952 btv
->saturation
= color
;
954 /* 0-511 for the color */
955 val_u
= ((color
* btv
->opt_uv_ratio
) / 50) >> 7;
956 val_v
= (((color
* (100 - btv
->opt_uv_ratio
) / 50) >>7)*180L)/254;
957 hibits
= (val_u
>> 7) & 2;
958 hibits
|= (val_v
>> 8) & 1;
959 btwrite(val_u
& 0xff, BT848_SAT_U_LO
);
960 btwrite(val_v
& 0xff, BT848_SAT_V_LO
);
961 btaor(hibits
, ~3, BT848_E_CONTROL
);
962 btaor(hibits
, ~3, BT848_O_CONTROL
);
965 /* ----------------------------------------------------------------------- */
968 video_mux(struct bttv
*btv
, unsigned int input
)
972 if (input
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
975 /* needed by RemoteVideo MX */
976 mask2
= bttv_tvcards
[btv
->c
.type
].gpiomask2
;
978 gpio_inout(mask2
,mask2
);
980 if (input
== btv
->svhs
) {
981 btor(BT848_CONTROL_COMP
, BT848_E_CONTROL
);
982 btor(BT848_CONTROL_COMP
, BT848_O_CONTROL
);
984 btand(~BT848_CONTROL_COMP
, BT848_E_CONTROL
);
985 btand(~BT848_CONTROL_COMP
, BT848_O_CONTROL
);
987 mux
= bttv_muxsel(btv
, input
);
988 btaor(mux
<<5, ~(3<<5), BT848_IFORM
);
989 dprintk("%d: video mux: input=%d mux=%d\n", btv
->c
.nr
, input
, mux
);
991 /* card specific hook */
992 if(bttv_tvcards
[btv
->c
.type
].muxsel_hook
)
993 bttv_tvcards
[btv
->c
.type
].muxsel_hook (btv
, input
);
997 static char *audio_modes
[] = {
998 "audio: tuner", "audio: radio", "audio: extern",
999 "audio: intern", "audio: mute"
1003 audio_mux_gpio(struct bttv
*btv
, int input
, int mute
)
1005 int gpio_val
, signal
, mute_gpio
;
1007 gpio_inout(bttv_tvcards
[btv
->c
.type
].gpiomask
,
1008 bttv_tvcards
[btv
->c
.type
].gpiomask
);
1009 signal
= btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
;
1012 mute_gpio
= mute
|| (btv
->opt_automute
&& (!signal
|| !btv
->users
)
1013 && !btv
->has_radio_tuner
);
1016 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomute
;
1018 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomux
[input
];
1020 switch (btv
->c
.type
) {
1021 case BTTV_BOARD_VOODOOTV_FM
:
1022 case BTTV_BOARD_VOODOOTV_200
:
1023 gpio_val
= bttv_tda9880_setnorm(btv
, gpio_val
);
1027 gpio_bits(bttv_tvcards
[btv
->c
.type
].gpiomask
, gpio_val
);
1031 bttv_gpio_tracking(btv
, audio_modes
[mute_gpio
? 4 : input
]);
1035 audio_mute(struct bttv
*btv
, int mute
)
1037 struct v4l2_ctrl
*ctrl
;
1039 audio_mux_gpio(btv
, btv
->audio_input
, mute
);
1041 if (btv
->sd_msp34xx
) {
1042 ctrl
= v4l2_ctrl_find(btv
->sd_msp34xx
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1044 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1046 if (btv
->sd_tvaudio
) {
1047 ctrl
= v4l2_ctrl_find(btv
->sd_tvaudio
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1049 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1051 if (btv
->sd_tda7432
) {
1052 ctrl
= v4l2_ctrl_find(btv
->sd_tda7432
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1054 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1060 audio_input(struct bttv
*btv
, int input
)
1062 audio_mux_gpio(btv
, input
, btv
->mute
);
1064 if (btv
->sd_msp34xx
) {
1067 /* Note: the inputs tuner/radio/extern/intern are translated
1068 to msp routings. This assumes common behavior for all msp3400
1069 based TV cards. When this assumption fails, then the
1070 specific MSP routing must be added to the card table.
1071 For now this is sufficient. */
1073 case TVAUDIO_INPUT_RADIO
:
1074 /* Some boards need the msp do to the radio demod */
1075 if (btv
->radio_uses_msp_demodulator
) {
1076 in
= MSP_INPUT_DEFAULT
;
1079 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1080 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1082 case TVAUDIO_INPUT_EXTERN
:
1083 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER1
,
1084 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1086 case TVAUDIO_INPUT_INTERN
:
1087 /* Yes, this is the same input as for RADIO. I doubt
1088 if this is ever used. The only board with an INTERN
1089 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1090 that was tested. My guess is that the whole INTERN
1091 input does not work. */
1092 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1093 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1095 case TVAUDIO_INPUT_TUNER
:
1097 /* This is the only card that uses TUNER2, and afaik,
1098 is the only difference between the VOODOOTV_FM
1100 if (btv
->c
.type
== BTTV_BOARD_VOODOOTV_200
)
1101 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER2
, \
1102 MSP_DSP_IN_TUNER
, MSP_DSP_IN_TUNER
);
1104 in
= MSP_INPUT_DEFAULT
;
1107 v4l2_subdev_call(btv
->sd_msp34xx
, audio
, s_routing
,
1108 in
, MSP_OUTPUT_DEFAULT
, 0);
1110 if (btv
->sd_tvaudio
) {
1111 v4l2_subdev_call(btv
->sd_tvaudio
, audio
, s_routing
,
1118 bttv_crop_calc_limits(struct bttv_crop
*c
)
1120 /* Scale factor min. 1:1, max. 16:1. Min. image size
1121 48 x 32. Scaled width must be a multiple of 4. */
1124 /* For bug compatibility with VIDIOCGCAP and image
1125 size checks in earlier driver versions. */
1126 c
->min_scaled_width
= 48;
1127 c
->min_scaled_height
= 32;
1129 c
->min_scaled_width
=
1130 (max(48, c
->rect
.width
>> 4) + 3) & ~3;
1131 c
->min_scaled_height
=
1132 max(32, c
->rect
.height
>> 4);
1135 c
->max_scaled_width
= c
->rect
.width
& ~3;
1136 c
->max_scaled_height
= c
->rect
.height
;
1140 bttv_crop_reset(struct bttv_crop
*c
, unsigned int norm
)
1142 c
->rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1143 bttv_crop_calc_limits(c
);
1146 /* Call with btv->lock down. */
1148 set_tvnorm(struct bttv
*btv
, unsigned int norm
)
1150 const struct bttv_tvnorm
*tvnorm
;
1153 BUG_ON(norm
>= BTTV_TVNORMS
);
1154 BUG_ON(btv
->tvnorm
>= BTTV_TVNORMS
);
1156 tvnorm
= &bttv_tvnorms
[norm
];
1158 if (memcmp(&bttv_tvnorms
[btv
->tvnorm
].cropcap
, &tvnorm
->cropcap
,
1159 sizeof (tvnorm
->cropcap
))) {
1160 bttv_crop_reset(&btv
->crop
[0], norm
);
1161 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
1163 if (0 == (btv
->resources
& VIDEO_RESOURCES
)) {
1164 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
1165 + tvnorm
->cropcap
.bounds
.height
;
1171 btwrite(tvnorm
->adelay
, BT848_ADELAY
);
1172 btwrite(tvnorm
->bdelay
, BT848_BDELAY
);
1173 btaor(tvnorm
->iform
,~(BT848_IFORM_NORM
|BT848_IFORM_XTBOTH
),
1175 btwrite(tvnorm
->vbipack
, BT848_VBI_PACK_SIZE
);
1176 btwrite(1, BT848_VBI_PACK_DEL
);
1177 bt848A_set_timing(btv
);
1179 switch (btv
->c
.type
) {
1180 case BTTV_BOARD_VOODOOTV_FM
:
1181 case BTTV_BOARD_VOODOOTV_200
:
1182 bttv_tda9880_setnorm(btv
, gpio_read());
1185 id
= tvnorm
->v4l2_id
;
1186 bttv_call_all(btv
, core
, s_std
, id
);
1191 /* Call with btv->lock down. */
1193 set_input(struct bttv
*btv
, unsigned int input
, unsigned int norm
)
1195 unsigned long flags
;
1199 spin_lock_irqsave(&btv
->s_lock
,flags
);
1200 if (btv
->curr
.frame_irq
) {
1201 /* active capture -> delayed input switch */
1202 btv
->new_input
= input
;
1204 video_mux(btv
,input
);
1206 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1208 video_mux(btv
,input
);
1210 btv
->audio_input
= (btv
->tuner_type
!= TUNER_ABSENT
&& input
== 0) ?
1211 TVAUDIO_INPUT_TUNER
: TVAUDIO_INPUT_EXTERN
;
1212 audio_input(btv
, btv
->audio_input
);
1213 set_tvnorm(btv
, norm
);
1216 static void init_irqreg(struct bttv
*btv
)
1219 btwrite(0xfffffUL
, BT848_INT_STAT
);
1221 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1223 btwrite(BT848_INT_I2CDONE
,
1227 btwrite((btv
->triton1
) |
1228 (btv
->gpioirq
? BT848_INT_GPINT
: 0) |
1230 (fdsr
? BT848_INT_FDSR
: 0) |
1231 BT848_INT_RISCI
| BT848_INT_OCERR
|
1232 BT848_INT_FMTCHG
|BT848_INT_HLOCK
|
1238 static void init_bt848(struct bttv
*btv
)
1240 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1241 /* very basic init only */
1246 btwrite(0x00, BT848_CAP_CTL
);
1247 btwrite(BT848_COLOR_CTL_GAMMA
, BT848_COLOR_CTL
);
1248 btwrite(BT848_IFORM_XTAUTO
| BT848_IFORM_AUTO
, BT848_IFORM
);
1250 /* set planar and packed mode trigger points and */
1251 /* set rising edge of inverted GPINTR pin as irq trigger */
1252 btwrite(BT848_GPIO_DMA_CTL_PKTP_32
|
1253 BT848_GPIO_DMA_CTL_PLTP1_16
|
1254 BT848_GPIO_DMA_CTL_PLTP23_16
|
1255 BT848_GPIO_DMA_CTL_GPINTC
|
1256 BT848_GPIO_DMA_CTL_GPINTI
,
1257 BT848_GPIO_DMA_CTL
);
1259 btwrite(0x20, BT848_E_VSCALE_HI
);
1260 btwrite(0x20, BT848_O_VSCALE_HI
);
1262 v4l2_ctrl_handler_setup(&btv
->ctrl_handler
);
1268 static void bttv_reinit_bt848(struct bttv
*btv
)
1270 unsigned long flags
;
1273 pr_info("%d: reset, reinitialize\n", btv
->c
.nr
);
1274 spin_lock_irqsave(&btv
->s_lock
,flags
);
1276 bttv_set_dma(btv
,0);
1277 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1280 btv
->pll
.pll_current
= -1;
1281 set_input(btv
, btv
->input
, btv
->tvnorm
);
1284 static int bttv_s_ctrl(struct v4l2_ctrl
*c
)
1286 struct bttv
*btv
= container_of(c
->handler
, struct bttv
, ctrl_handler
);
1290 case V4L2_CID_BRIGHTNESS
:
1291 bt848_bright(btv
, c
->val
);
1294 bt848_hue(btv
, c
->val
);
1296 case V4L2_CID_CONTRAST
:
1297 bt848_contrast(btv
, c
->val
);
1299 case V4L2_CID_SATURATION
:
1300 bt848_sat(btv
, c
->val
);
1302 case V4L2_CID_COLOR_KILLER
:
1304 btor(BT848_SCLOOP_CKILL
, BT848_E_SCLOOP
);
1305 btor(BT848_SCLOOP_CKILL
, BT848_O_SCLOOP
);
1307 btand(~BT848_SCLOOP_CKILL
, BT848_E_SCLOOP
);
1308 btand(~BT848_SCLOOP_CKILL
, BT848_O_SCLOOP
);
1311 case V4L2_CID_AUDIO_MUTE
:
1312 audio_mute(btv
, c
->val
);
1315 case V4L2_CID_AUDIO_VOLUME
:
1316 btv
->volume_gpio(btv
, c
->val
);
1319 case V4L2_CID_CHROMA_AGC
:
1320 val
= c
->val
? BT848_SCLOOP_CAGC
: 0;
1321 btwrite(val
, BT848_E_SCLOOP
);
1322 btwrite(val
, BT848_O_SCLOOP
);
1324 case V4L2_CID_PRIVATE_COMBFILTER
:
1325 btv
->opt_combfilter
= c
->val
;
1327 case V4L2_CID_PRIVATE_LUMAFILTER
:
1329 btand(~BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1330 btand(~BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1332 btor(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1333 btor(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1336 case V4L2_CID_PRIVATE_AUTOMUTE
:
1337 btv
->opt_automute
= c
->val
;
1339 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1340 btwrite(BT848_ADC_RESERVED
|
1341 (c
->val
? BT848_ADC_CRUSH
: 0),
1344 case V4L2_CID_PRIVATE_VCR_HACK
:
1345 btv
->opt_vcr_hack
= c
->val
;
1347 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1348 btwrite(c
->val
, BT848_WC_UP
);
1350 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1351 btwrite(c
->val
, BT848_WC_DOWN
);
1353 case V4L2_CID_PRIVATE_UV_RATIO
:
1354 btv
->opt_uv_ratio
= c
->val
;
1355 bt848_sat(btv
, btv
->saturation
);
1357 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1358 btaor((c
->val
<< 7), ~BT848_OFORM_RANGE
, BT848_OFORM
);
1360 case V4L2_CID_PRIVATE_CORING
:
1361 btaor((c
->val
<< 5), ~BT848_OFORM_CORE32
, BT848_OFORM
);
1369 /* ----------------------------------------------------------------------- */
1371 static const struct v4l2_ctrl_ops bttv_ctrl_ops
= {
1372 .s_ctrl
= bttv_s_ctrl
,
1375 static struct v4l2_ctrl_config bttv_ctrl_combfilter
= {
1376 .ops
= &bttv_ctrl_ops
,
1377 .id
= V4L2_CID_PRIVATE_COMBFILTER
,
1378 .name
= "Comb Filter",
1379 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1386 static struct v4l2_ctrl_config bttv_ctrl_automute
= {
1387 .ops
= &bttv_ctrl_ops
,
1388 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
1389 .name
= "Auto Mute",
1390 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1397 static struct v4l2_ctrl_config bttv_ctrl_lumafilter
= {
1398 .ops
= &bttv_ctrl_ops
,
1399 .id
= V4L2_CID_PRIVATE_LUMAFILTER
,
1400 .name
= "Luma Decimation Filter",
1401 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1408 static struct v4l2_ctrl_config bttv_ctrl_agc_crush
= {
1409 .ops
= &bttv_ctrl_ops
,
1410 .id
= V4L2_CID_PRIVATE_AGC_CRUSH
,
1411 .name
= "AGC Crush",
1412 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1419 static struct v4l2_ctrl_config bttv_ctrl_vcr_hack
= {
1420 .ops
= &bttv_ctrl_ops
,
1421 .id
= V4L2_CID_PRIVATE_VCR_HACK
,
1423 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1430 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower
= {
1431 .ops
= &bttv_ctrl_ops
,
1432 .id
= V4L2_CID_PRIVATE_WHITECRUSH_LOWER
,
1433 .name
= "Whitecrush Lower",
1434 .type
= V4L2_CTRL_TYPE_INTEGER
,
1441 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper
= {
1442 .ops
= &bttv_ctrl_ops
,
1443 .id
= V4L2_CID_PRIVATE_WHITECRUSH_UPPER
,
1444 .name
= "Whitecrush Upper",
1445 .type
= V4L2_CTRL_TYPE_INTEGER
,
1452 static struct v4l2_ctrl_config bttv_ctrl_uv_ratio
= {
1453 .ops
= &bttv_ctrl_ops
,
1454 .id
= V4L2_CID_PRIVATE_UV_RATIO
,
1456 .type
= V4L2_CTRL_TYPE_INTEGER
,
1463 static struct v4l2_ctrl_config bttv_ctrl_full_luma
= {
1464 .ops
= &bttv_ctrl_ops
,
1465 .id
= V4L2_CID_PRIVATE_FULL_LUMA_RANGE
,
1466 .name
= "Full Luma Range",
1467 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1473 static struct v4l2_ctrl_config bttv_ctrl_coring
= {
1474 .ops
= &bttv_ctrl_ops
,
1475 .id
= V4L2_CID_PRIVATE_CORING
,
1477 .type
= V4L2_CTRL_TYPE_INTEGER
,
1484 /* ----------------------------------------------------------------------- */
1486 void bttv_gpio_tracking(struct bttv
*btv
, char *comment
)
1488 unsigned int outbits
, data
;
1489 outbits
= btread(BT848_GPIO_OUT_EN
);
1490 data
= btread(BT848_GPIO_DATA
);
1491 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1492 btv
->c
.nr
, outbits
, data
& outbits
, data
& ~outbits
, comment
);
1495 static void bttv_field_count(struct bttv
*btv
)
1503 /* start field counter */
1504 btor(BT848_INT_VSYNC
,BT848_INT_MASK
);
1506 /* stop field counter */
1507 btand(~BT848_INT_VSYNC
,BT848_INT_MASK
);
1508 btv
->field_count
= 0;
1512 static const struct bttv_format
*
1513 format_by_fourcc(int fourcc
)
1517 for (i
= 0; i
< FORMATS
; i
++) {
1518 if (-1 == formats
[i
].fourcc
)
1520 if (formats
[i
].fourcc
== fourcc
)
1526 /* ----------------------------------------------------------------------- */
1530 bttv_switch_overlay(struct bttv
*btv
, struct bttv_fh
*fh
,
1531 struct bttv_buffer
*new)
1533 struct bttv_buffer
*old
;
1534 unsigned long flags
;
1537 dprintk("switch_overlay: enter [new=%p]\n", new);
1539 new->vb
.state
= VIDEOBUF_DONE
;
1540 spin_lock_irqsave(&btv
->s_lock
,flags
);
1544 bttv_set_dma(btv
, 0x03);
1545 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1547 dprintk("switch_overlay: old=%p state is %d\n",
1548 old
, old
->vb
.state
);
1549 bttv_dma_free(&fh
->cap
,btv
, old
);
1553 free_btres_lock(btv
,fh
,RESOURCE_OVERLAY
);
1554 dprintk("switch_overlay: done\n");
1558 /* ----------------------------------------------------------------------- */
1559 /* video4linux (1) interface */
1561 static int bttv_prepare_buffer(struct videobuf_queue
*q
,struct bttv
*btv
,
1562 struct bttv_buffer
*buf
,
1563 const struct bttv_format
*fmt
,
1564 unsigned int width
, unsigned int height
,
1565 enum v4l2_field field
)
1567 struct bttv_fh
*fh
= q
->priv_data
;
1568 int redo_dma_risc
= 0;
1573 /* check settings */
1576 if (fmt
->btformat
== BT848_COLOR_FMT_RAW
) {
1578 height
= RAW_LINES
*2;
1579 if (width
*height
> buf
->vb
.bsize
)
1581 buf
->vb
.size
= buf
->vb
.bsize
;
1583 /* Make sure tvnorm and vbi_end remain consistent
1584 until we're done. */
1588 /* In this mode capturing always starts at defrect.top
1589 (default VDELAY), ignoring cropping parameters. */
1590 if (btv
->vbi_end
> bttv_tvnorms
[norm
].cropcap
.defrect
.top
) {
1594 c
.rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1597 c
= btv
->crop
[!!fh
->do_crop
];
1599 if (width
< c
.min_scaled_width
||
1600 width
> c
.max_scaled_width
||
1601 height
< c
.min_scaled_height
)
1605 case V4L2_FIELD_TOP
:
1606 case V4L2_FIELD_BOTTOM
:
1607 case V4L2_FIELD_ALTERNATE
:
1608 /* btv->crop counts frame lines. Max. scale
1609 factor is 16:1 for frames, 8:1 for fields. */
1610 if (height
* 2 > c
.max_scaled_height
)
1615 if (height
> c
.max_scaled_height
)
1620 buf
->vb
.size
= (width
* height
* fmt
->depth
) >> 3;
1621 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
1625 /* alloc + fill struct bttv_buffer (if changed) */
1626 if (buf
->vb
.width
!= width
|| buf
->vb
.height
!= height
||
1627 buf
->vb
.field
!= field
||
1628 buf
->tvnorm
!= norm
|| buf
->fmt
!= fmt
||
1629 buf
->crop
.top
!= c
.rect
.top
||
1630 buf
->crop
.left
!= c
.rect
.left
||
1631 buf
->crop
.width
!= c
.rect
.width
||
1632 buf
->crop
.height
!= c
.rect
.height
) {
1633 buf
->vb
.width
= width
;
1634 buf
->vb
.height
= height
;
1635 buf
->vb
.field
= field
;
1642 /* alloc risc memory */
1643 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1645 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,&btv
->fbuf
)))
1650 if (0 != (rc
= bttv_buffer_risc(btv
,buf
)))
1653 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1657 bttv_dma_free(q
,btv
,buf
);
1662 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1664 struct bttv_fh
*fh
= q
->priv_data
;
1666 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
1669 if (*size
* *count
> gbuffers
* gbufsize
)
1670 *count
= (gbuffers
* gbufsize
) / *size
;
1675 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
1676 enum v4l2_field field
)
1678 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1679 struct bttv_fh
*fh
= q
->priv_data
;
1681 return bttv_prepare_buffer(q
,fh
->btv
, buf
, fh
->fmt
,
1682 fh
->width
, fh
->height
, field
);
1686 buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1688 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1689 struct bttv_fh
*fh
= q
->priv_data
;
1690 struct bttv
*btv
= fh
->btv
;
1692 buf
->vb
.state
= VIDEOBUF_QUEUED
;
1693 list_add_tail(&buf
->vb
.queue
,&btv
->capture
);
1694 if (!btv
->curr
.frame_irq
) {
1696 bttv_set_dma(btv
, 0x03);
1700 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1702 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1703 struct bttv_fh
*fh
= q
->priv_data
;
1705 bttv_dma_free(q
,fh
->btv
,buf
);
1708 static struct videobuf_queue_ops bttv_video_qops
= {
1709 .buf_setup
= buffer_setup
,
1710 .buf_prepare
= buffer_prepare
,
1711 .buf_queue
= buffer_queue
,
1712 .buf_release
= buffer_release
,
1715 static void radio_enable(struct bttv
*btv
)
1717 /* Switch to the radio tuner */
1718 if (!btv
->has_radio_tuner
) {
1719 btv
->has_radio_tuner
= 1;
1720 bttv_call_all(btv
, tuner
, s_radio
);
1721 btv
->audio_input
= TVAUDIO_INPUT_RADIO
;
1722 audio_input(btv
, btv
->audio_input
);
1726 static int bttv_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1728 struct bttv_fh
*fh
= priv
;
1729 struct bttv
*btv
= fh
->btv
;
1733 for (i
= 0; i
< BTTV_TVNORMS
; i
++)
1734 if (id
& bttv_tvnorms
[i
].v4l2_id
)
1736 if (i
== BTTV_TVNORMS
) {
1749 static int bttv_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1751 struct bttv_fh
*fh
= priv
;
1752 struct bttv
*btv
= fh
->btv
;
1758 static int bttv_querystd(struct file
*file
, void *f
, v4l2_std_id
*id
)
1760 struct bttv_fh
*fh
= f
;
1761 struct bttv
*btv
= fh
->btv
;
1763 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_NUML
)
1764 *id
= V4L2_STD_625_50
;
1766 *id
= V4L2_STD_525_60
;
1770 static int bttv_enum_input(struct file
*file
, void *priv
,
1771 struct v4l2_input
*i
)
1773 struct bttv_fh
*fh
= priv
;
1774 struct bttv
*btv
= fh
->btv
;
1777 if (i
->index
>= bttv_tvcards
[btv
->c
.type
].video_inputs
) {
1782 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1785 if (btv
->tuner_type
!= TUNER_ABSENT
&& i
->index
== 0) {
1786 sprintf(i
->name
, "Television");
1787 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1789 } else if (i
->index
== btv
->svhs
) {
1790 sprintf(i
->name
, "S-Video");
1792 sprintf(i
->name
, "Composite%d", i
->index
);
1795 if (i
->index
== btv
->input
) {
1796 __u32 dstatus
= btread(BT848_DSTATUS
);
1797 if (0 == (dstatus
& BT848_DSTATUS_PRES
))
1798 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1799 if (0 == (dstatus
& BT848_DSTATUS_HLOC
))
1800 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1803 i
->std
= BTTV_NORMS
;
1810 static int bttv_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1812 struct bttv_fh
*fh
= priv
;
1813 struct bttv
*btv
= fh
->btv
;
1820 static int bttv_s_input(struct file
*file
, void *priv
, unsigned int i
)
1822 struct bttv_fh
*fh
= priv
;
1823 struct bttv
*btv
= fh
->btv
;
1825 if (i
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1828 set_input(btv
, i
, btv
->tvnorm
);
1832 static int bttv_s_tuner(struct file
*file
, void *priv
,
1833 const struct v4l2_tuner
*t
)
1835 struct bttv_fh
*fh
= priv
;
1836 struct bttv
*btv
= fh
->btv
;
1841 bttv_call_all(btv
, tuner
, s_tuner
, t
);
1843 if (btv
->audio_mode_gpio
) {
1844 struct v4l2_tuner copy
= *t
;
1846 btv
->audio_mode_gpio(btv
, ©
, 1);
1851 static int bttv_g_frequency(struct file
*file
, void *priv
,
1852 struct v4l2_frequency
*f
)
1854 struct bttv_fh
*fh
= priv
;
1855 struct bttv
*btv
= fh
->btv
;
1860 if (f
->type
== V4L2_TUNER_RADIO
)
1862 f
->frequency
= f
->type
== V4L2_TUNER_RADIO
?
1863 btv
->radio_freq
: btv
->tv_freq
;
1868 static void bttv_set_frequency(struct bttv
*btv
, const struct v4l2_frequency
*f
)
1870 struct v4l2_frequency new_freq
= *f
;
1872 bttv_call_all(btv
, tuner
, s_frequency
, f
);
1873 /* s_frequency may clamp the frequency, so get the actual
1874 frequency before assigning radio/tv_freq. */
1875 bttv_call_all(btv
, tuner
, g_frequency
, &new_freq
);
1876 if (new_freq
.type
== V4L2_TUNER_RADIO
) {
1878 btv
->radio_freq
= new_freq
.frequency
;
1879 if (btv
->has_matchbox
)
1880 tea5757_set_freq(btv
, btv
->radio_freq
);
1882 btv
->tv_freq
= new_freq
.frequency
;
1886 static int bttv_s_frequency(struct file
*file
, void *priv
,
1887 const struct v4l2_frequency
*f
)
1889 struct bttv_fh
*fh
= priv
;
1890 struct bttv
*btv
= fh
->btv
;
1895 bttv_set_frequency(btv
, f
);
1899 static int bttv_log_status(struct file
*file
, void *f
)
1901 struct video_device
*vdev
= video_devdata(file
);
1902 struct bttv_fh
*fh
= f
;
1903 struct bttv
*btv
= fh
->btv
;
1905 v4l2_ctrl_handler_log_status(vdev
->ctrl_handler
, btv
->c
.v4l2_dev
.name
);
1906 bttv_call_all(btv
, core
, log_status
);
1910 static int bttv_g_chip_ident(struct file
*file
, void *f
, struct v4l2_dbg_chip_ident
*chip
)
1912 struct bttv_fh
*fh
= f
;
1913 struct bttv
*btv
= fh
->btv
;
1915 chip
->ident
= V4L2_IDENT_NONE
;
1917 if (chip
->match
.type
== V4L2_CHIP_MATCH_HOST
) {
1918 if (v4l2_chip_match_host(&chip
->match
)) {
1919 chip
->ident
= btv
->id
;
1920 if (chip
->ident
== PCI_DEVICE_ID_FUSION879
)
1921 chip
->ident
= V4L2_IDENT_BT879
;
1925 if (chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_DRIVER
&&
1926 chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
1928 /* TODO: is this correct? */
1929 return bttv_call_all_err(btv
, core
, g_chip_ident
, chip
);
1932 #ifdef CONFIG_VIDEO_ADV_DEBUG
1933 static int bttv_g_register(struct file
*file
, void *f
,
1934 struct v4l2_dbg_register
*reg
)
1936 struct bttv_fh
*fh
= f
;
1937 struct bttv
*btv
= fh
->btv
;
1939 if (!capable(CAP_SYS_ADMIN
))
1942 if (!v4l2_chip_match_host(®
->match
)) {
1943 /* TODO: subdev errors should not be ignored, this should become a
1944 subdev helper function. */
1945 bttv_call_all(btv
, core
, g_register
, reg
);
1949 /* bt848 has a 12-bit register space */
1951 reg
->val
= btread(reg
->reg
);
1957 static int bttv_s_register(struct file
*file
, void *f
,
1958 const struct v4l2_dbg_register
*reg
)
1960 struct bttv_fh
*fh
= f
;
1961 struct bttv
*btv
= fh
->btv
;
1963 if (!capable(CAP_SYS_ADMIN
))
1966 if (!v4l2_chip_match_host(®
->match
)) {
1967 /* TODO: subdev errors should not be ignored, this should become a
1968 subdev helper function. */
1969 bttv_call_all(btv
, core
, s_register
, reg
);
1973 /* bt848 has a 12-bit register space */
1974 btwrite(reg
->val
, reg
->reg
& 0xfff);
1980 /* Given cropping boundaries b and the scaled width and height of a
1981 single field or frame, which must not exceed hardware limits, this
1982 function adjusts the cropping parameters c. */
1984 bttv_crop_adjust (struct bttv_crop
* c
,
1985 const struct v4l2_rect
* b
,
1988 enum v4l2_field field
)
1990 __s32 frame_height
= height
<< !V4L2_FIELD_HAS_BOTH(field
);
1994 if (width
< c
->min_scaled_width
) {
1995 /* Max. hor. scale factor 16:1. */
1996 c
->rect
.width
= width
* 16;
1997 } else if (width
> c
->max_scaled_width
) {
1998 /* Min. hor. scale factor 1:1. */
1999 c
->rect
.width
= width
;
2001 max_left
= b
->left
+ b
->width
- width
;
2002 max_left
= min(max_left
, (__s32
) MAX_HDELAY
);
2003 if (c
->rect
.left
> max_left
)
2004 c
->rect
.left
= max_left
;
2007 if (height
< c
->min_scaled_height
) {
2008 /* Max. vert. scale factor 16:1, single fields 8:1. */
2009 c
->rect
.height
= height
* 16;
2010 } else if (frame_height
> c
->max_scaled_height
) {
2011 /* Min. vert. scale factor 1:1.
2012 Top and height count field lines times two. */
2013 c
->rect
.height
= (frame_height
+ 1) & ~1;
2015 max_top
= b
->top
+ b
->height
- c
->rect
.height
;
2016 if (c
->rect
.top
> max_top
)
2017 c
->rect
.top
= max_top
;
2020 bttv_crop_calc_limits(c
);
2023 /* Returns an error if scaling to a frame or single field with the given
2024 width and height is not possible with the current cropping parameters
2025 and width aligned according to width_mask. If adjust_size is TRUE the
2026 function may adjust the width and/or height instead, rounding width
2027 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2028 also adjust the current cropping parameters to get closer to the
2029 desired image size. */
2031 limit_scaled_size_lock (struct bttv_fh
* fh
,
2034 enum v4l2_field field
,
2035 unsigned int width_mask
,
2036 unsigned int width_bias
,
2040 struct bttv
*btv
= fh
->btv
;
2041 const struct v4l2_rect
*b
;
2042 struct bttv_crop
*c
;
2049 BUG_ON((int) width_mask
>= 0 ||
2050 width_bias
>= (unsigned int) -width_mask
);
2052 /* Make sure tvnorm, vbi_end and the current cropping parameters
2053 remain consistent until we're done. */
2055 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2057 /* Do crop - use current, don't - use default parameters. */
2058 c
= &btv
->crop
[!!fh
->do_crop
];
2063 && !locked_btres(btv
, VIDEO_RESOURCES
)) {
2067 /* We cannot scale up. When the scaled image is larger
2068 than crop.rect we adjust the crop.rect as required
2069 by the V4L2 spec, hence cropcap.bounds are our limit. */
2070 max_width
= min(b
->width
, (__s32
) MAX_HACTIVE
);
2071 max_height
= b
->height
;
2073 /* We cannot capture the same line as video and VBI data.
2074 Note btv->vbi_end is really a minimum, see
2075 bttv_vbi_try_fmt(). */
2076 if (btv
->vbi_end
> b
->top
) {
2077 max_height
-= btv
->vbi_end
- b
->top
;
2079 if (min_height
> max_height
)
2084 if (btv
->vbi_end
> c
->rect
.top
)
2087 min_width
= c
->min_scaled_width
;
2088 min_height
= c
->min_scaled_height
;
2089 max_width
= c
->max_scaled_width
;
2090 max_height
= c
->max_scaled_height
;
2095 min_width
= (min_width
- width_mask
- 1) & width_mask
;
2096 max_width
= max_width
& width_mask
;
2098 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2099 min_height
= min_height
;
2100 /* Min. scale factor is 1:1. */
2101 max_height
>>= !V4L2_FIELD_HAS_BOTH(field
);
2104 *width
= clamp(*width
, min_width
, max_width
);
2105 *height
= clamp(*height
, min_height
, max_height
);
2107 /* Round after clamping to avoid overflow. */
2108 *width
= (*width
+ width_bias
) & width_mask
;
2111 bttv_crop_adjust(c
, b
, *width
, *height
, field
);
2113 if (btv
->vbi_end
> c
->rect
.top
) {
2114 /* Move the crop window out of the way. */
2115 c
->rect
.top
= btv
->vbi_end
;
2120 if (*width
< min_width
||
2121 *height
< min_height
||
2122 *width
> max_width
||
2123 *height
> max_height
||
2124 0 != (*width
& ~width_mask
))
2128 rc
= 0; /* success */
2135 /* Returns an error if the given overlay window dimensions are not
2136 possible with the current cropping parameters. If adjust_size is
2137 TRUE the function may adjust the window width and/or height
2138 instead, however it always rounds the horizontal position and
2139 width as btcx_align() does. If adjust_crop is TRUE the function
2140 may also adjust the current cropping parameters to get closer
2141 to the desired window size. */
2143 verify_window_lock(struct bttv_fh
*fh
, struct v4l2_window
*win
,
2144 int adjust_size
, int adjust_crop
)
2146 enum v4l2_field field
;
2147 unsigned int width_mask
;
2150 if (win
->w
.width
< 48)
2152 if (win
->w
.height
< 32)
2154 if (win
->clipcount
> 2048)
2155 win
->clipcount
= 2048;
2158 win
->global_alpha
= 0;
2162 case V4L2_FIELD_TOP
:
2163 case V4L2_FIELD_BOTTOM
:
2164 case V4L2_FIELD_INTERLACED
:
2167 field
= V4L2_FIELD_ANY
;
2170 if (V4L2_FIELD_ANY
== field
) {
2173 height2
= fh
->btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2174 field
= (win
->w
.height
> height2
)
2175 ? V4L2_FIELD_INTERLACED
2180 if (NULL
== fh
->ovfmt
)
2182 /* 4-byte alignment. */
2184 switch (fh
->ovfmt
->depth
) {
2198 win
->w
.width
-= win
->w
.left
& ~width_mask
;
2199 win
->w
.left
= (win
->w
.left
- width_mask
- 1) & width_mask
;
2201 rc
= limit_scaled_size_lock(fh
, &win
->w
.width
, &win
->w
.height
,
2203 /* width_bias: round down */ 0,
2204 adjust_size
, adjust_crop
);
2210 static int setup_window_lock(struct bttv_fh
*fh
, struct bttv
*btv
,
2211 struct v4l2_window
*win
, int fixup
)
2213 struct v4l2_clip
*clips
= NULL
;
2214 int n
,size
,retval
= 0;
2216 if (NULL
== fh
->ovfmt
)
2218 if (!(fh
->ovfmt
->flags
& FORMAT_FLAGS_PACKED
))
2220 retval
= verify_window_lock(fh
, win
,
2221 /* adjust_size */ fixup
,
2222 /* adjust_crop */ fixup
);
2226 /* copy clips -- luckily v4l1 + v4l2 are binary
2227 compatible here ...*/
2229 size
= sizeof(*clips
)*(n
+4);
2230 clips
= kmalloc(size
,GFP_KERNEL
);
2234 if (copy_from_user(clips
,win
->clips
,sizeof(struct v4l2_clip
)*n
)) {
2240 /* clip against screen */
2241 if (NULL
!= btv
->fbuf
.base
)
2242 n
= btcx_screen_clips(btv
->fbuf
.fmt
.width
, btv
->fbuf
.fmt
.height
,
2244 btcx_sort_clips(clips
,n
);
2246 /* 4-byte alignments */
2247 switch (fh
->ovfmt
->depth
) {
2250 btcx_align(&win
->w
, clips
, n
, 3);
2253 btcx_align(&win
->w
, clips
, n
, 1);
2256 /* no alignment fixups needed */
2262 kfree(fh
->ov
.clips
);
2263 fh
->ov
.clips
= clips
;
2267 fh
->ov
.field
= win
->field
;
2268 fh
->ov
.setup_ok
= 1;
2270 btv
->init
.ov
.w
.width
= win
->w
.width
;
2271 btv
->init
.ov
.w
.height
= win
->w
.height
;
2272 btv
->init
.ov
.field
= win
->field
;
2274 /* update overlay if needed */
2276 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2277 struct bttv_buffer
*new;
2279 new = videobuf_sg_alloc(sizeof(*new));
2280 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2281 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2282 retval
= bttv_switch_overlay(btv
,fh
,new);
2287 /* ----------------------------------------------------------------------- */
2289 static struct videobuf_queue
* bttv_queue(struct bttv_fh
*fh
)
2291 struct videobuf_queue
* q
= NULL
;
2294 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2297 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2306 static int bttv_resource(struct bttv_fh
*fh
)
2311 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2312 res
= RESOURCE_VIDEO_STREAM
;
2314 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2323 static int bttv_switch_type(struct bttv_fh
*fh
, enum v4l2_buf_type type
)
2325 struct videobuf_queue
*q
= bttv_queue(fh
);
2326 int res
= bttv_resource(fh
);
2328 if (check_btres(fh
,res
))
2330 if (videobuf_queue_is_busy(q
))
2337 pix_format_set_size (struct v4l2_pix_format
* f
,
2338 const struct bttv_format
* fmt
,
2340 unsigned int height
)
2345 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2346 f
->bytesperline
= width
; /* Y plane */
2347 f
->sizeimage
= (width
* height
* fmt
->depth
) >> 3;
2349 f
->bytesperline
= (width
* fmt
->depth
) >> 3;
2350 f
->sizeimage
= height
* f
->bytesperline
;
2354 static int bttv_g_fmt_vid_cap(struct file
*file
, void *priv
,
2355 struct v4l2_format
*f
)
2357 struct bttv_fh
*fh
= priv
;
2359 pix_format_set_size(&f
->fmt
.pix
, fh
->fmt
,
2360 fh
->width
, fh
->height
);
2361 f
->fmt
.pix
.field
= fh
->cap
.field
;
2362 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
2363 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2368 static int bttv_g_fmt_vid_overlay(struct file
*file
, void *priv
,
2369 struct v4l2_format
*f
)
2371 struct bttv_fh
*fh
= priv
;
2373 f
->fmt
.win
.w
= fh
->ov
.w
;
2374 f
->fmt
.win
.field
= fh
->ov
.field
;
2379 static void bttv_get_width_mask_vid_cap(const struct bttv_format
*fmt
,
2380 unsigned int *width_mask
,
2381 unsigned int *width_bias
)
2383 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2384 *width_mask
= ~15; /* width must be a multiple of 16 pixels */
2385 *width_bias
= 8; /* nearest */
2387 *width_mask
= ~3; /* width must be a multiple of 4 pixels */
2388 *width_bias
= 2; /* nearest */
2392 static int bttv_try_fmt_vid_cap(struct file
*file
, void *priv
,
2393 struct v4l2_format
*f
)
2395 const struct bttv_format
*fmt
;
2396 struct bttv_fh
*fh
= priv
;
2397 struct bttv
*btv
= fh
->btv
;
2398 enum v4l2_field field
;
2399 __s32 width
, height
;
2401 unsigned int width_mask
, width_bias
;
2404 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2408 field
= f
->fmt
.pix
.field
;
2411 case V4L2_FIELD_TOP
:
2412 case V4L2_FIELD_BOTTOM
:
2413 case V4L2_FIELD_ALTERNATE
:
2414 case V4L2_FIELD_INTERLACED
:
2416 case V4L2_FIELD_SEQ_BT
:
2417 case V4L2_FIELD_SEQ_TB
:
2418 if (!(fmt
->flags
& FORMAT_FLAGS_PLANAR
)) {
2419 field
= V4L2_FIELD_SEQ_TB
;
2423 default: /* FIELD_ANY case */
2424 height2
= btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2425 field
= (f
->fmt
.pix
.height
> height2
)
2426 ? V4L2_FIELD_INTERLACED
2427 : V4L2_FIELD_BOTTOM
;
2431 width
= f
->fmt
.pix
.width
;
2432 height
= f
->fmt
.pix
.height
;
2434 bttv_get_width_mask_vid_cap(fmt
, &width_mask
, &width_bias
);
2435 rc
= limit_scaled_size_lock(fh
, &width
, &height
, field
,
2436 width_mask
, width_bias
,
2437 /* adjust_size */ 1,
2438 /* adjust_crop */ 0);
2442 /* update data for the application */
2443 f
->fmt
.pix
.field
= field
;
2444 pix_format_set_size(&f
->fmt
.pix
, fmt
, width
, height
);
2445 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2450 static int bttv_try_fmt_vid_overlay(struct file
*file
, void *priv
,
2451 struct v4l2_format
*f
)
2453 struct bttv_fh
*fh
= priv
;
2455 verify_window_lock(fh
, &f
->fmt
.win
,
2456 /* adjust_size */ 1,
2457 /* adjust_crop */ 0);
2461 static int bttv_s_fmt_vid_cap(struct file
*file
, void *priv
,
2462 struct v4l2_format
*f
)
2465 const struct bttv_format
*fmt
;
2466 struct bttv_fh
*fh
= priv
;
2467 struct bttv
*btv
= fh
->btv
;
2468 __s32 width
, height
;
2469 unsigned int width_mask
, width_bias
;
2470 enum v4l2_field field
;
2472 retval
= bttv_switch_type(fh
, f
->type
);
2476 retval
= bttv_try_fmt_vid_cap(file
, priv
, f
);
2480 width
= f
->fmt
.pix
.width
;
2481 height
= f
->fmt
.pix
.height
;
2482 field
= f
->fmt
.pix
.field
;
2484 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2485 bttv_get_width_mask_vid_cap(fmt
, &width_mask
, &width_bias
);
2486 retval
= limit_scaled_size_lock(fh
, &width
, &height
, f
->fmt
.pix
.field
,
2487 width_mask
, width_bias
,
2488 /* adjust_size */ 1,
2489 /* adjust_crop */ 1);
2493 f
->fmt
.pix
.field
= field
;
2495 /* update our state informations */
2497 fh
->cap
.field
= f
->fmt
.pix
.field
;
2498 fh
->cap
.last
= V4L2_FIELD_NONE
;
2499 fh
->width
= f
->fmt
.pix
.width
;
2500 fh
->height
= f
->fmt
.pix
.height
;
2501 btv
->init
.fmt
= fmt
;
2502 btv
->init
.width
= f
->fmt
.pix
.width
;
2503 btv
->init
.height
= f
->fmt
.pix
.height
;
2508 static int bttv_s_fmt_vid_overlay(struct file
*file
, void *priv
,
2509 struct v4l2_format
*f
)
2511 struct bttv_fh
*fh
= priv
;
2512 struct bttv
*btv
= fh
->btv
;
2514 if (no_overlay
> 0) {
2515 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2519 return setup_window_lock(fh
, btv
, &f
->fmt
.win
, 1);
2522 static int bttv_querycap(struct file
*file
, void *priv
,
2523 struct v4l2_capability
*cap
)
2525 struct video_device
*vdev
= video_devdata(file
);
2526 struct bttv_fh
*fh
= priv
;
2527 struct bttv
*btv
= fh
->btv
;
2532 strlcpy(cap
->driver
, "bttv", sizeof(cap
->driver
));
2533 strlcpy(cap
->card
, btv
->video_dev
->name
, sizeof(cap
->card
));
2534 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2535 "PCI:%s", pci_name(btv
->c
.pci
));
2537 V4L2_CAP_VIDEO_CAPTURE
|
2538 V4L2_CAP_READWRITE
|
2539 V4L2_CAP_STREAMING
|
2540 V4L2_CAP_DEVICE_CAPS
;
2541 if (no_overlay
<= 0)
2542 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
2544 cap
->capabilities
|= V4L2_CAP_VBI_CAPTURE
;
2546 cap
->capabilities
|= V4L2_CAP_RADIO
;
2549 * No need to lock here: those vars are initialized during board
2550 * probe and remains untouched during the rest of the driver lifecycle
2552 if (btv
->has_saa6588
)
2553 cap
->capabilities
|= V4L2_CAP_RDS_CAPTURE
;
2554 if (btv
->tuner_type
!= TUNER_ABSENT
)
2555 cap
->capabilities
|= V4L2_CAP_TUNER
;
2556 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
2557 cap
->device_caps
= cap
->capabilities
&
2558 (V4L2_CAP_VIDEO_CAPTURE
|
2559 V4L2_CAP_READWRITE
|
2560 V4L2_CAP_STREAMING
|
2561 V4L2_CAP_VIDEO_OVERLAY
|
2563 else if (vdev
->vfl_type
== VFL_TYPE_VBI
)
2564 cap
->device_caps
= cap
->capabilities
&
2565 (V4L2_CAP_VBI_CAPTURE
|
2566 V4L2_CAP_READWRITE
|
2567 V4L2_CAP_STREAMING
|
2570 cap
->device_caps
= V4L2_CAP_RADIO
| V4L2_CAP_TUNER
;
2571 if (btv
->has_saa6588
)
2572 cap
->device_caps
|= V4L2_CAP_READWRITE
|
2573 V4L2_CAP_RDS_CAPTURE
;
2578 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc
*f
)
2582 for (i
= 0; i
< FORMATS
; i
++) {
2583 if (formats
[i
].fourcc
!= -1)
2585 if ((unsigned int)index
== f
->index
)
2591 f
->pixelformat
= formats
[i
].fourcc
;
2592 strlcpy(f
->description
, formats
[i
].name
, sizeof(f
->description
));
2597 static int bttv_enum_fmt_vid_cap(struct file
*file
, void *priv
,
2598 struct v4l2_fmtdesc
*f
)
2600 int rc
= bttv_enum_fmt_cap_ovr(f
);
2608 static int bttv_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
2609 struct v4l2_fmtdesc
*f
)
2613 if (no_overlay
> 0) {
2614 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2618 rc
= bttv_enum_fmt_cap_ovr(f
);
2623 if (!(formats
[rc
].flags
& FORMAT_FLAGS_PACKED
))
2629 static int bttv_g_fbuf(struct file
*file
, void *f
,
2630 struct v4l2_framebuffer
*fb
)
2632 struct bttv_fh
*fh
= f
;
2633 struct bttv
*btv
= fh
->btv
;
2636 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2637 fb
->flags
= V4L2_FBUF_FLAG_PRIMARY
;
2639 fb
->fmt
.pixelformat
= fh
->ovfmt
->fourcc
;
2643 static int bttv_overlay(struct file
*file
, void *f
, unsigned int on
)
2645 struct bttv_fh
*fh
= f
;
2646 struct bttv
*btv
= fh
->btv
;
2647 struct bttv_buffer
*new;
2652 if (unlikely(!btv
->fbuf
.base
)) {
2655 if (unlikely(!fh
->ov
.setup_ok
)) {
2656 dprintk("%d: overlay: !setup_ok\n", btv
->c
.nr
);
2663 if (!check_alloc_btres_lock(btv
, fh
, RESOURCE_OVERLAY
))
2667 fh
->ov
.tvnorm
= btv
->tvnorm
;
2668 new = videobuf_sg_alloc(sizeof(*new));
2669 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2670 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2676 retval
= bttv_switch_overlay(btv
, fh
, new);
2680 static int bttv_s_fbuf(struct file
*file
, void *f
,
2681 const struct v4l2_framebuffer
*fb
)
2683 struct bttv_fh
*fh
= f
;
2684 struct bttv
*btv
= fh
->btv
;
2685 const struct bttv_format
*fmt
;
2688 if (!capable(CAP_SYS_ADMIN
) &&
2689 !capable(CAP_SYS_RAWIO
))
2693 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2696 if (0 == (fmt
->flags
& FORMAT_FLAGS_PACKED
))
2700 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2701 __s32 width
= fb
->fmt
.width
;
2702 __s32 height
= fb
->fmt
.height
;
2704 retval
= limit_scaled_size_lock(fh
, &width
, &height
,
2705 V4L2_FIELD_INTERLACED
,
2706 /* width_mask */ ~3,
2708 /* adjust_size */ 0,
2709 /* adjust_crop */ 0);
2715 btv
->fbuf
.base
= fb
->base
;
2716 btv
->fbuf
.fmt
.width
= fb
->fmt
.width
;
2717 btv
->fbuf
.fmt
.height
= fb
->fmt
.height
;
2718 if (0 != fb
->fmt
.bytesperline
)
2719 btv
->fbuf
.fmt
.bytesperline
= fb
->fmt
.bytesperline
;
2721 btv
->fbuf
.fmt
.bytesperline
= btv
->fbuf
.fmt
.width
*fmt
->depth
/8;
2725 btv
->init
.ovfmt
= fmt
;
2726 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2729 fh
->ov
.w
.width
= fb
->fmt
.width
;
2730 fh
->ov
.w
.height
= fb
->fmt
.height
;
2731 btv
->init
.ov
.w
.width
= fb
->fmt
.width
;
2732 btv
->init
.ov
.w
.height
= fb
->fmt
.height
;
2733 kfree(fh
->ov
.clips
);
2734 fh
->ov
.clips
= NULL
;
2737 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2738 struct bttv_buffer
*new;
2740 new = videobuf_sg_alloc(sizeof(*new));
2741 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2742 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2743 retval
= bttv_switch_overlay(btv
, fh
, new);
2749 static int bttv_reqbufs(struct file
*file
, void *priv
,
2750 struct v4l2_requestbuffers
*p
)
2752 struct bttv_fh
*fh
= priv
;
2753 return videobuf_reqbufs(bttv_queue(fh
), p
);
2756 static int bttv_querybuf(struct file
*file
, void *priv
,
2757 struct v4l2_buffer
*b
)
2759 struct bttv_fh
*fh
= priv
;
2760 return videobuf_querybuf(bttv_queue(fh
), b
);
2763 static int bttv_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2765 struct bttv_fh
*fh
= priv
;
2766 struct bttv
*btv
= fh
->btv
;
2767 int res
= bttv_resource(fh
);
2769 if (!check_alloc_btres_lock(btv
, fh
, res
))
2772 return videobuf_qbuf(bttv_queue(fh
), b
);
2775 static int bttv_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2777 struct bttv_fh
*fh
= priv
;
2778 return videobuf_dqbuf(bttv_queue(fh
), b
,
2779 file
->f_flags
& O_NONBLOCK
);
2782 static int bttv_streamon(struct file
*file
, void *priv
,
2783 enum v4l2_buf_type type
)
2785 struct bttv_fh
*fh
= priv
;
2786 struct bttv
*btv
= fh
->btv
;
2787 int res
= bttv_resource(fh
);
2789 if (!check_alloc_btres_lock(btv
, fh
, res
))
2791 return videobuf_streamon(bttv_queue(fh
));
2795 static int bttv_streamoff(struct file
*file
, void *priv
,
2796 enum v4l2_buf_type type
)
2798 struct bttv_fh
*fh
= priv
;
2799 struct bttv
*btv
= fh
->btv
;
2801 int res
= bttv_resource(fh
);
2804 retval
= videobuf_streamoff(bttv_queue(fh
));
2807 free_btres_lock(btv
, fh
, res
);
2811 static int bttv_g_parm(struct file
*file
, void *f
,
2812 struct v4l2_streamparm
*parm
)
2814 struct bttv_fh
*fh
= f
;
2815 struct bttv
*btv
= fh
->btv
;
2817 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2819 parm
->parm
.capture
.readbuffers
= gbuffers
;
2820 v4l2_video_std_frame_period(bttv_tvnorms
[btv
->tvnorm
].v4l2_id
,
2821 &parm
->parm
.capture
.timeperframe
);
2826 static int bttv_g_tuner(struct file
*file
, void *priv
,
2827 struct v4l2_tuner
*t
)
2829 struct bttv_fh
*fh
= priv
;
2830 struct bttv
*btv
= fh
->btv
;
2835 t
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
2836 t
->capability
= V4L2_TUNER_CAP_NORM
;
2837 bttv_call_all(btv
, tuner
, g_tuner
, t
);
2838 strcpy(t
->name
, "Television");
2839 t
->type
= V4L2_TUNER_ANALOG_TV
;
2840 if (btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
)
2843 if (btv
->audio_mode_gpio
)
2844 btv
->audio_mode_gpio(btv
, t
, 0);
2849 static int bttv_cropcap(struct file
*file
, void *priv
,
2850 struct v4l2_cropcap
*cap
)
2852 struct bttv_fh
*fh
= priv
;
2853 struct bttv
*btv
= fh
->btv
;
2855 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2856 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2859 *cap
= bttv_tvnorms
[btv
->tvnorm
].cropcap
;
2864 static int bttv_g_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
2866 struct bttv_fh
*fh
= f
;
2867 struct bttv
*btv
= fh
->btv
;
2869 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2870 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2873 /* No fh->do_crop = 1; because btv->crop[1] may be
2874 inconsistent with fh->width or fh->height and apps
2875 do not expect a change here. */
2877 crop
->c
= btv
->crop
[!!fh
->do_crop
].rect
;
2882 static int bttv_s_crop(struct file
*file
, void *f
, const struct v4l2_crop
*crop
)
2884 struct bttv_fh
*fh
= f
;
2885 struct bttv
*btv
= fh
->btv
;
2886 const struct v4l2_rect
*b
;
2894 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2895 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2898 /* Make sure tvnorm, vbi_end and the current cropping
2899 parameters remain consistent until we're done. Note
2900 read() may change vbi_end in check_alloc_btres_lock(). */
2903 if (locked_btres(fh
->btv
, VIDEO_RESOURCES
)) {
2907 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2910 b_right
= b_left
+ b
->width
;
2911 b_bottom
= b
->top
+ b
->height
;
2913 b_top
= max(b
->top
, btv
->vbi_end
);
2914 if (b_top
+ 32 >= b_bottom
) {
2918 /* Min. scaled size 48 x 32. */
2919 c
.rect
.left
= clamp_t(s32
, crop
->c
.left
, b_left
, b_right
- 48);
2920 c
.rect
.left
= min(c
.rect
.left
, (__s32
) MAX_HDELAY
);
2922 c
.rect
.width
= clamp_t(s32
, crop
->c
.width
,
2923 48, b_right
- c
.rect
.left
);
2925 c
.rect
.top
= clamp_t(s32
, crop
->c
.top
, b_top
, b_bottom
- 32);
2926 /* Top and height must be a multiple of two. */
2927 c
.rect
.top
= (c
.rect
.top
+ 1) & ~1;
2929 c
.rect
.height
= clamp_t(s32
, crop
->c
.height
,
2930 32, b_bottom
- c
.rect
.top
);
2931 c
.rect
.height
= (c
.rect
.height
+ 1) & ~1;
2933 bttv_crop_calc_limits(&c
);
2939 if (fh
->width
< c
.min_scaled_width
) {
2940 fh
->width
= c
.min_scaled_width
;
2941 btv
->init
.width
= c
.min_scaled_width
;
2942 } else if (fh
->width
> c
.max_scaled_width
) {
2943 fh
->width
= c
.max_scaled_width
;
2944 btv
->init
.width
= c
.max_scaled_width
;
2947 if (fh
->height
< c
.min_scaled_height
) {
2948 fh
->height
= c
.min_scaled_height
;
2949 btv
->init
.height
= c
.min_scaled_height
;
2950 } else if (fh
->height
> c
.max_scaled_height
) {
2951 fh
->height
= c
.max_scaled_height
;
2952 btv
->init
.height
= c
.max_scaled_height
;
2958 static ssize_t
bttv_read(struct file
*file
, char __user
*data
,
2959 size_t count
, loff_t
*ppos
)
2961 struct bttv_fh
*fh
= file
->private_data
;
2964 if (fh
->btv
->errors
)
2965 bttv_reinit_bt848(fh
->btv
);
2966 dprintk("%d: read count=%d type=%s\n",
2967 fh
->btv
->c
.nr
, (int)count
, v4l2_type_names
[fh
->type
]);
2970 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2971 if (!check_alloc_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
)) {
2972 /* VIDEO_READ in use by another fh,
2973 or VIDEO_STREAM by any fh. */
2976 retval
= videobuf_read_one(&fh
->cap
, data
, count
, ppos
,
2977 file
->f_flags
& O_NONBLOCK
);
2978 free_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
);
2980 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2981 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
2983 retval
= videobuf_read_stream(&fh
->vbi
, data
, count
, ppos
, 1,
2984 file
->f_flags
& O_NONBLOCK
);
2992 static unsigned int bttv_poll(struct file
*file
, poll_table
*wait
)
2994 struct bttv_fh
*fh
= file
->private_data
;
2995 struct bttv_buffer
*buf
;
2996 enum v4l2_field field
;
2997 unsigned int rc
= 0;
2998 unsigned long req_events
= poll_requested_events(wait
);
3000 if (v4l2_event_pending(&fh
->fh
))
3002 else if (req_events
& POLLPRI
)
3003 poll_wait(file
, &fh
->fh
.wait
, wait
);
3005 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
3008 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
3009 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
3010 return rc
| POLLERR
;
3011 return rc
| videobuf_poll_stream(file
, &fh
->vbi
, wait
);
3014 if (check_btres(fh
,RESOURCE_VIDEO_STREAM
)) {
3015 /* streaming capture */
3016 if (list_empty(&fh
->cap
.stream
))
3017 return rc
| POLLERR
;
3018 buf
= list_entry(fh
->cap
.stream
.next
,struct bttv_buffer
,vb
.stream
);
3020 /* read() capture */
3021 if (NULL
== fh
->cap
.read_buf
) {
3022 /* need to capture a new frame */
3023 if (locked_btres(fh
->btv
,RESOURCE_VIDEO_STREAM
))
3024 return rc
| POLLERR
;
3025 fh
->cap
.read_buf
= videobuf_sg_alloc(fh
->cap
.msize
);
3026 if (NULL
== fh
->cap
.read_buf
)
3027 return rc
| POLLERR
;
3028 fh
->cap
.read_buf
->memory
= V4L2_MEMORY_USERPTR
;
3029 field
= videobuf_next_field(&fh
->cap
);
3030 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,field
)) {
3031 kfree (fh
->cap
.read_buf
);
3032 fh
->cap
.read_buf
= NULL
;
3033 return rc
| POLLERR
;
3035 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
3036 fh
->cap
.read_off
= 0;
3038 buf
= (struct bttv_buffer
*)fh
->cap
.read_buf
;
3041 poll_wait(file
, &buf
->vb
.done
, wait
);
3042 if (buf
->vb
.state
== VIDEOBUF_DONE
||
3043 buf
->vb
.state
== VIDEOBUF_ERROR
)
3044 rc
= rc
| POLLIN
|POLLRDNORM
;
3048 static int bttv_open(struct file
*file
)
3050 struct video_device
*vdev
= video_devdata(file
);
3051 struct bttv
*btv
= video_drvdata(file
);
3053 enum v4l2_buf_type type
= 0;
3055 dprintk("open dev=%s\n", video_device_node_name(vdev
));
3057 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
) {
3058 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
3059 } else if (vdev
->vfl_type
== VFL_TYPE_VBI
) {
3060 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
3066 dprintk("%d: open called (type=%s)\n",
3067 btv
->c
.nr
, v4l2_type_names
[type
]);
3069 /* allocate per filehandle data */
3070 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3074 file
->private_data
= fh
;
3077 v4l2_fh_init(&fh
->fh
, vdev
);
3080 fh
->ov
.setup_ok
= 0;
3082 videobuf_queue_sg_init(&fh
->cap
, &bttv_video_qops
,
3083 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3084 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
3085 V4L2_FIELD_INTERLACED
,
3086 sizeof(struct bttv_buffer
),
3088 videobuf_queue_sg_init(&fh
->vbi
, &bttv_vbi_qops
,
3089 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3090 V4L2_BUF_TYPE_VBI_CAPTURE
,
3092 sizeof(struct bttv_buffer
),
3094 set_tvnorm(btv
,btv
->tvnorm
);
3095 set_input(btv
, btv
->input
, btv
->tvnorm
);
3096 audio_mute(btv
, btv
->mute
);
3098 /* The V4L2 spec requires one global set of cropping parameters
3099 which only change on request. These are stored in btv->crop[1].
3100 However for compatibility with V4L apps and cropping unaware
3101 V4L2 apps we now reset the cropping parameters as seen through
3102 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3103 will use btv->crop[0], the default cropping parameters for the
3104 current video standard, and VIDIOC_S_FMT will not implicitely
3105 change the cropping parameters until VIDIOC_S_CROP has been
3107 fh
->do_crop
= !reset_crop
; /* module parameter */
3109 /* Likewise there should be one global set of VBI capture
3110 parameters, but for compatibility with V4L apps and earlier
3111 driver versions each fh has its own parameters. */
3112 bttv_vbi_fmt_reset(&fh
->vbi_fmt
, btv
->tvnorm
);
3114 bttv_field_count(btv
);
3115 v4l2_fh_add(&fh
->fh
);
3119 static int bttv_release(struct file
*file
)
3121 struct bttv_fh
*fh
= file
->private_data
;
3122 struct bttv
*btv
= fh
->btv
;
3124 /* turn off overlay */
3125 if (check_btres(fh
, RESOURCE_OVERLAY
))
3126 bttv_switch_overlay(btv
,fh
,NULL
);
3128 /* stop video capture */
3129 if (check_btres(fh
, RESOURCE_VIDEO_STREAM
)) {
3130 videobuf_streamoff(&fh
->cap
);
3131 free_btres_lock(btv
,fh
,RESOURCE_VIDEO_STREAM
);
3133 if (fh
->cap
.read_buf
) {
3134 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
3135 kfree(fh
->cap
.read_buf
);
3137 if (check_btres(fh
, RESOURCE_VIDEO_READ
)) {
3138 free_btres_lock(btv
, fh
, RESOURCE_VIDEO_READ
);
3141 /* stop vbi capture */
3142 if (check_btres(fh
, RESOURCE_VBI
)) {
3143 videobuf_stop(&fh
->vbi
);
3144 free_btres_lock(btv
,fh
,RESOURCE_VBI
);
3149 videobuf_mmap_free(&fh
->cap
);
3150 videobuf_mmap_free(&fh
->vbi
);
3151 file
->private_data
= NULL
;
3154 bttv_field_count(btv
);
3157 audio_mute(btv
, btv
->mute
);
3159 v4l2_fh_del(&fh
->fh
);
3160 v4l2_fh_exit(&fh
->fh
);
3166 bttv_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3168 struct bttv_fh
*fh
= file
->private_data
;
3170 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
3171 fh
->btv
->c
.nr
, v4l2_type_names
[fh
->type
],
3172 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
3173 return videobuf_mmap_mapper(bttv_queue(fh
),vma
);
3176 static const struct v4l2_file_operations bttv_fops
=
3178 .owner
= THIS_MODULE
,
3180 .release
= bttv_release
,
3181 .unlocked_ioctl
= video_ioctl2
,
3187 static const struct v4l2_ioctl_ops bttv_ioctl_ops
= {
3188 .vidioc_querycap
= bttv_querycap
,
3189 .vidioc_enum_fmt_vid_cap
= bttv_enum_fmt_vid_cap
,
3190 .vidioc_g_fmt_vid_cap
= bttv_g_fmt_vid_cap
,
3191 .vidioc_try_fmt_vid_cap
= bttv_try_fmt_vid_cap
,
3192 .vidioc_s_fmt_vid_cap
= bttv_s_fmt_vid_cap
,
3193 .vidioc_enum_fmt_vid_overlay
= bttv_enum_fmt_vid_overlay
,
3194 .vidioc_g_fmt_vid_overlay
= bttv_g_fmt_vid_overlay
,
3195 .vidioc_try_fmt_vid_overlay
= bttv_try_fmt_vid_overlay
,
3196 .vidioc_s_fmt_vid_overlay
= bttv_s_fmt_vid_overlay
,
3197 .vidioc_g_fmt_vbi_cap
= bttv_g_fmt_vbi_cap
,
3198 .vidioc_try_fmt_vbi_cap
= bttv_try_fmt_vbi_cap
,
3199 .vidioc_s_fmt_vbi_cap
= bttv_s_fmt_vbi_cap
,
3200 .vidioc_cropcap
= bttv_cropcap
,
3201 .vidioc_reqbufs
= bttv_reqbufs
,
3202 .vidioc_querybuf
= bttv_querybuf
,
3203 .vidioc_qbuf
= bttv_qbuf
,
3204 .vidioc_dqbuf
= bttv_dqbuf
,
3205 .vidioc_s_std
= bttv_s_std
,
3206 .vidioc_g_std
= bttv_g_std
,
3207 .vidioc_enum_input
= bttv_enum_input
,
3208 .vidioc_g_input
= bttv_g_input
,
3209 .vidioc_s_input
= bttv_s_input
,
3210 .vidioc_streamon
= bttv_streamon
,
3211 .vidioc_streamoff
= bttv_streamoff
,
3212 .vidioc_g_tuner
= bttv_g_tuner
,
3213 .vidioc_s_tuner
= bttv_s_tuner
,
3214 .vidioc_g_crop
= bttv_g_crop
,
3215 .vidioc_s_crop
= bttv_s_crop
,
3216 .vidioc_g_fbuf
= bttv_g_fbuf
,
3217 .vidioc_s_fbuf
= bttv_s_fbuf
,
3218 .vidioc_overlay
= bttv_overlay
,
3219 .vidioc_g_parm
= bttv_g_parm
,
3220 .vidioc_g_frequency
= bttv_g_frequency
,
3221 .vidioc_s_frequency
= bttv_s_frequency
,
3222 .vidioc_log_status
= bttv_log_status
,
3223 .vidioc_querystd
= bttv_querystd
,
3224 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
3225 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
3226 .vidioc_g_chip_ident
= bttv_g_chip_ident
,
3227 #ifdef CONFIG_VIDEO_ADV_DEBUG
3228 .vidioc_g_register
= bttv_g_register
,
3229 .vidioc_s_register
= bttv_s_register
,
3233 static struct video_device bttv_video_template
= {
3235 .ioctl_ops
= &bttv_ioctl_ops
,
3236 .tvnorms
= BTTV_NORMS
,
3239 /* ----------------------------------------------------------------------- */
3240 /* radio interface */
3242 static int radio_open(struct file
*file
)
3244 struct video_device
*vdev
= video_devdata(file
);
3245 struct bttv
*btv
= video_drvdata(file
);
3248 dprintk("open dev=%s\n", video_device_node_name(vdev
));
3250 dprintk("%d: open called (radio)\n", btv
->c
.nr
);
3252 /* allocate per filehandle data */
3253 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3256 file
->private_data
= fh
;
3258 v4l2_fh_init(&fh
->fh
, vdev
);
3261 audio_mute(btv
, btv
->mute
);
3263 v4l2_fh_add(&fh
->fh
);
3268 static int radio_release(struct file
*file
)
3270 struct bttv_fh
*fh
= file
->private_data
;
3271 struct bttv
*btv
= fh
->btv
;
3272 struct saa6588_command cmd
;
3274 file
->private_data
= NULL
;
3275 v4l2_fh_del(&fh
->fh
);
3276 v4l2_fh_exit(&fh
->fh
);
3281 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_CLOSE
, &cmd
);
3283 if (btv
->radio_user
== 0)
3284 btv
->has_radio_tuner
= 0;
3288 static int radio_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
3290 struct bttv_fh
*fh
= priv
;
3291 struct bttv
*btv
= fh
->btv
;
3295 strcpy(t
->name
, "Radio");
3296 t
->type
= V4L2_TUNER_RADIO
;
3299 bttv_call_all(btv
, tuner
, g_tuner
, t
);
3301 if (btv
->audio_mode_gpio
)
3302 btv
->audio_mode_gpio(btv
, t
, 0);
3307 static int radio_s_tuner(struct file
*file
, void *priv
,
3308 const struct v4l2_tuner
*t
)
3310 struct bttv_fh
*fh
= priv
;
3311 struct bttv
*btv
= fh
->btv
;
3317 bttv_call_all(btv
, tuner
, s_tuner
, t
);
3321 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
3322 size_t count
, loff_t
*ppos
)
3324 struct bttv_fh
*fh
= file
->private_data
;
3325 struct bttv
*btv
= fh
->btv
;
3326 struct saa6588_command cmd
;
3327 cmd
.block_count
= count
/3;
3329 cmd
.instance
= file
;
3330 cmd
.result
= -ENODEV
;
3333 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_READ
, &cmd
);
3338 static unsigned int radio_poll(struct file
*file
, poll_table
*wait
)
3340 struct bttv_fh
*fh
= file
->private_data
;
3341 struct bttv
*btv
= fh
->btv
;
3342 unsigned long req_events
= poll_requested_events(wait
);
3343 struct saa6588_command cmd
;
3344 unsigned int res
= 0;
3346 if (v4l2_event_pending(&fh
->fh
))
3348 else if (req_events
& POLLPRI
)
3349 poll_wait(file
, &fh
->fh
.wait
, wait
);
3351 cmd
.instance
= file
;
3352 cmd
.event_list
= wait
;
3354 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_POLL
, &cmd
);
3359 static const struct v4l2_file_operations radio_fops
=
3361 .owner
= THIS_MODULE
,
3364 .release
= radio_release
,
3365 .unlocked_ioctl
= video_ioctl2
,
3369 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
3370 .vidioc_querycap
= bttv_querycap
,
3371 .vidioc_log_status
= bttv_log_status
,
3372 .vidioc_g_tuner
= radio_g_tuner
,
3373 .vidioc_s_tuner
= radio_s_tuner
,
3374 .vidioc_g_frequency
= bttv_g_frequency
,
3375 .vidioc_s_frequency
= bttv_s_frequency
,
3376 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
3377 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
3380 static struct video_device radio_template
= {
3381 .fops
= &radio_fops
,
3382 .ioctl_ops
= &radio_ioctl_ops
,
3385 /* ----------------------------------------------------------------------- */
3386 /* some debug code */
3388 static int bttv_risc_decode(u32 risc
)
3390 static char *instr
[16] = {
3391 [ BT848_RISC_WRITE
>> 28 ] = "write",
3392 [ BT848_RISC_SKIP
>> 28 ] = "skip",
3393 [ BT848_RISC_WRITEC
>> 28 ] = "writec",
3394 [ BT848_RISC_JUMP
>> 28 ] = "jump",
3395 [ BT848_RISC_SYNC
>> 28 ] = "sync",
3396 [ BT848_RISC_WRITE123
>> 28 ] = "write123",
3397 [ BT848_RISC_SKIP123
>> 28 ] = "skip123",
3398 [ BT848_RISC_WRITE1S23
>> 28 ] = "write1s23",
3400 static int incr
[16] = {
3401 [ BT848_RISC_WRITE
>> 28 ] = 2,
3402 [ BT848_RISC_JUMP
>> 28 ] = 2,
3403 [ BT848_RISC_SYNC
>> 28 ] = 2,
3404 [ BT848_RISC_WRITE123
>> 28 ] = 5,
3405 [ BT848_RISC_SKIP123
>> 28 ] = 2,
3406 [ BT848_RISC_WRITE1S23
>> 28 ] = 3,
3408 static char *bits
[] = {
3409 "be0", "be1", "be2", "be3/resync",
3410 "set0", "set1", "set2", "set3",
3411 "clr0", "clr1", "clr2", "clr3",
3412 "irq", "res", "eol", "sol",
3416 pr_cont("0x%08x [ %s", risc
,
3417 instr
[risc
>> 28] ? instr
[risc
>> 28] : "INVALID");
3418 for (i
= ARRAY_SIZE(bits
)-1; i
>= 0; i
--)
3419 if (risc
& (1 << (i
+ 12)))
3420 pr_cont(" %s", bits
[i
]);
3421 pr_cont(" count=%d ]\n", risc
& 0xfff);
3422 return incr
[risc
>> 28] ? incr
[risc
>> 28] : 1;
3425 static void bttv_risc_disasm(struct bttv
*btv
,
3426 struct btcx_riscmem
*risc
)
3430 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3431 btv
->c
.v4l2_dev
.name
, risc
->cpu
, (unsigned long)risc
->dma
);
3432 for (i
= 0; i
< (risc
->size
>> 2); i
+= n
) {
3433 pr_info("%s: 0x%lx: ",
3434 btv
->c
.v4l2_dev
.name
,
3435 (unsigned long)(risc
->dma
+ (i
<<2)));
3436 n
= bttv_risc_decode(le32_to_cpu(risc
->cpu
[i
]));
3437 for (j
= 1; j
< n
; j
++)
3438 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3439 btv
->c
.v4l2_dev
.name
,
3440 (unsigned long)(risc
->dma
+ ((i
+j
)<<2)),
3442 if (0 == risc
->cpu
[i
])
3447 static void bttv_print_riscaddr(struct bttv
*btv
)
3449 pr_info(" main: %08llx\n", (unsigned long long)btv
->main
.dma
);
3450 pr_info(" vbi : o=%08llx e=%08llx\n",
3451 btv
->cvbi
? (unsigned long long)btv
->cvbi
->top
.dma
: 0,
3452 btv
->cvbi
? (unsigned long long)btv
->cvbi
->bottom
.dma
: 0);
3453 pr_info(" cap : o=%08llx e=%08llx\n",
3455 ? (unsigned long long)btv
->curr
.top
->top
.dma
: 0,
3457 ? (unsigned long long)btv
->curr
.bottom
->bottom
.dma
: 0);
3458 pr_info(" scr : o=%08llx e=%08llx\n",
3459 btv
->screen
? (unsigned long long)btv
->screen
->top
.dma
: 0,
3460 btv
->screen
? (unsigned long long)btv
->screen
->bottom
.dma
: 0);
3461 bttv_risc_disasm(btv
, &btv
->main
);
3464 /* ----------------------------------------------------------------------- */
3467 static char *irq_name
[] = {
3468 "FMTCHG", // format change detected (525 vs. 625)
3469 "VSYNC", // vertical sync (new field)
3470 "HSYNC", // horizontal sync
3471 "OFLOW", // chroma/luma AGC overflow
3472 "HLOCK", // horizontal lock changed
3473 "VPRES", // video presence changed
3475 "I2CDONE", // hw irc operation finished
3476 "GPINT", // gpio port triggered irq
3478 "RISCI", // risc instruction triggered irq
3479 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3480 "FTRGT", // pixel data fifo overrun
3481 "FDSR", // fifo data stream resyncronisation
3482 "PPERR", // parity error (data transfer)
3483 "RIPERR", // parity error (read risc instructions)
3484 "PABORT", // pci abort
3485 "OCERR", // risc instruction error
3486 "SCERR", // syncronisation error
3489 static void bttv_print_irqbits(u32 print
, u32 mark
)
3494 for (i
= 0; i
< ARRAY_SIZE(irq_name
); i
++) {
3495 if (print
& (1 << i
))
3496 pr_cont(" %s", irq_name
[i
]);
3497 if (mark
& (1 << i
))
3502 static void bttv_irq_debug_low_latency(struct bttv
*btv
, u32 rc
)
3504 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3506 (unsigned long)btv
->main
.dma
,
3507 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_VBI
+1]),
3508 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_FIELD
+1]),
3511 if (0 == (btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
)) {
3512 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3513 "Ok, then this is harmless, don't worry ;)\n",
3517 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3519 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3525 bttv_irq_next_video(struct bttv
*btv
, struct bttv_buffer_set
*set
)
3527 struct bttv_buffer
*item
;
3529 memset(set
,0,sizeof(*set
));
3531 /* capture request ? */
3532 if (!list_empty(&btv
->capture
)) {
3534 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3535 if (V4L2_FIELD_HAS_TOP(item
->vb
.field
))
3537 if (V4L2_FIELD_HAS_BOTTOM(item
->vb
.field
))
3540 /* capture request for other field ? */
3541 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
) &&
3542 (item
->vb
.queue
.next
!= &btv
->capture
)) {
3543 item
= list_entry(item
->vb
.queue
.next
, struct bttv_buffer
, vb
.queue
);
3544 /* Mike Isely <isely@pobox.com> - Only check
3545 * and set up the bottom field in the logic
3546 * below. Don't ever do the top field. This
3547 * of course means that if we set up the
3548 * bottom field in the above code that we'll
3549 * actually skip a field. But that's OK.
3550 * Having processed only a single buffer this
3551 * time, then the next time around the first
3552 * available buffer should be for a top field.
3553 * That will then cause us here to set up a
3554 * top then a bottom field in the normal way.
3555 * The alternative to this understanding is
3556 * that we set up the second available buffer
3557 * as a top field, but that's out of order
3558 * since this driver always processes the top
3559 * field first - the effect will be the two
3560 * buffers being returned in the wrong order,
3561 * with the second buffer also being delayed
3562 * by one field time (owing to the fifo nature
3563 * of videobuf). Worse still, we'll be stuck
3564 * doing fields out of order now every time
3565 * until something else causes a field to be
3566 * dropped. By effectively forcing a field to
3567 * drop this way then we always get back into
3568 * sync within a single frame time. (Out of
3569 * order fields can screw up deinterlacing
3571 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
)) {
3572 if (NULL
== set
->bottom
&&
3573 V4L2_FIELD_BOTTOM
== item
->vb
.field
) {
3576 if (NULL
!= set
->top
&& NULL
!= set
->bottom
)
3582 /* screen overlay ? */
3583 if (NULL
!= btv
->screen
) {
3584 if (V4L2_FIELD_HAS_BOTH(btv
->screen
->vb
.field
)) {
3585 if (NULL
== set
->top
&& NULL
== set
->bottom
) {
3586 set
->top
= btv
->screen
;
3587 set
->bottom
= btv
->screen
;
3590 if (V4L2_FIELD_TOP
== btv
->screen
->vb
.field
&&
3592 set
->top
= btv
->screen
;
3594 if (V4L2_FIELD_BOTTOM
== btv
->screen
->vb
.field
&&
3595 NULL
== set
->bottom
) {
3596 set
->bottom
= btv
->screen
;
3601 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3602 btv
->c
.nr
, set
->top
, set
->bottom
,
3603 btv
->screen
, set
->frame_irq
, set
->top_irq
);
3608 bttv_irq_wakeup_video(struct bttv
*btv
, struct bttv_buffer_set
*wakeup
,
3609 struct bttv_buffer_set
*curr
, unsigned int state
)
3613 v4l2_get_timestamp(&ts
);
3615 if (wakeup
->top
== wakeup
->bottom
) {
3616 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3618 pr_debug("%d: wakeup: both=%p\n",
3619 btv
->c
.nr
, wakeup
->top
);
3620 wakeup
->top
->vb
.ts
= ts
;
3621 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3622 wakeup
->top
->vb
.state
= state
;
3623 wake_up(&wakeup
->top
->vb
.done
);
3626 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3628 pr_debug("%d: wakeup: top=%p\n",
3629 btv
->c
.nr
, wakeup
->top
);
3630 wakeup
->top
->vb
.ts
= ts
;
3631 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3632 wakeup
->top
->vb
.state
= state
;
3633 wake_up(&wakeup
->top
->vb
.done
);
3635 if (NULL
!= wakeup
->bottom
&& curr
->bottom
!= wakeup
->bottom
) {
3637 pr_debug("%d: wakeup: bottom=%p\n",
3638 btv
->c
.nr
, wakeup
->bottom
);
3639 wakeup
->bottom
->vb
.ts
= ts
;
3640 wakeup
->bottom
->vb
.field_count
= btv
->field_count
;
3641 wakeup
->bottom
->vb
.state
= state
;
3642 wake_up(&wakeup
->bottom
->vb
.done
);
3648 bttv_irq_wakeup_vbi(struct bttv
*btv
, struct bttv_buffer
*wakeup
,
3656 v4l2_get_timestamp(&ts
);
3658 wakeup
->vb
.field_count
= btv
->field_count
;
3659 wakeup
->vb
.state
= state
;
3660 wake_up(&wakeup
->vb
.done
);
3663 static void bttv_irq_timeout(unsigned long data
)
3665 struct bttv
*btv
= (struct bttv
*)data
;
3666 struct bttv_buffer_set old
,new;
3667 struct bttv_buffer
*ovbi
;
3668 struct bttv_buffer
*item
;
3669 unsigned long flags
;
3672 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3673 btv
->c
.nr
, btv
->framedrop
, btv
->irq_me
, btv
->irq_total
,
3674 btread(BT848_RISC_COUNT
));
3675 bttv_print_irqbits(btread(BT848_INT_STAT
),0);
3679 spin_lock_irqsave(&btv
->s_lock
,flags
);
3681 /* deactivate stuff */
3682 memset(&new,0,sizeof(new));
3688 bttv_buffer_activate_video(btv
, &new);
3689 bttv_buffer_activate_vbi(btv
, NULL
);
3690 bttv_set_dma(btv
, 0);
3693 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_ERROR
);
3694 bttv_irq_wakeup_vbi(btv
, ovbi
, VIDEOBUF_ERROR
);
3696 /* cancel all outstanding capture / vbi requests */
3697 while (!list_empty(&btv
->capture
)) {
3698 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3699 list_del(&item
->vb
.queue
);
3700 item
->vb
.state
= VIDEOBUF_ERROR
;
3701 wake_up(&item
->vb
.done
);
3703 while (!list_empty(&btv
->vcapture
)) {
3704 item
= list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3705 list_del(&item
->vb
.queue
);
3706 item
->vb
.state
= VIDEOBUF_ERROR
;
3707 wake_up(&item
->vb
.done
);
3711 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
3715 bttv_irq_wakeup_top(struct bttv
*btv
)
3717 struct bttv_buffer
*wakeup
= btv
->curr
.top
;
3722 spin_lock(&btv
->s_lock
);
3723 btv
->curr
.top_irq
= 0;
3724 btv
->curr
.top
= NULL
;
3725 bttv_risc_hook(btv
, RISC_SLOT_O_FIELD
, NULL
, 0);
3727 v4l2_get_timestamp(&wakeup
->vb
.ts
);
3728 wakeup
->vb
.field_count
= btv
->field_count
;
3729 wakeup
->vb
.state
= VIDEOBUF_DONE
;
3730 wake_up(&wakeup
->vb
.done
);
3731 spin_unlock(&btv
->s_lock
);
3734 static inline int is_active(struct btcx_riscmem
*risc
, u32 rc
)
3738 if (rc
> risc
->dma
+ risc
->size
)
3744 bttv_irq_switch_video(struct bttv
*btv
)
3746 struct bttv_buffer_set
new;
3747 struct bttv_buffer_set old
;
3750 spin_lock(&btv
->s_lock
);
3752 /* new buffer set */
3753 bttv_irq_next_video(btv
, &new);
3754 rc
= btread(BT848_RISC_COUNT
);
3755 if ((btv
->curr
.top
&& is_active(&btv
->curr
.top
->top
, rc
)) ||
3756 (btv
->curr
.bottom
&& is_active(&btv
->curr
.bottom
->bottom
, rc
))) {
3759 bttv_irq_debug_low_latency(btv
, rc
);
3760 spin_unlock(&btv
->s_lock
);
3767 btv
->loop_irq
&= ~1;
3768 bttv_buffer_activate_video(btv
, &new);
3769 bttv_set_dma(btv
, 0);
3772 if (UNSET
!= btv
->new_input
) {
3773 video_mux(btv
,btv
->new_input
);
3774 btv
->new_input
= UNSET
;
3777 /* wake up finished buffers */
3778 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_DONE
);
3779 spin_unlock(&btv
->s_lock
);
3783 bttv_irq_switch_vbi(struct bttv
*btv
)
3785 struct bttv_buffer
*new = NULL
;
3786 struct bttv_buffer
*old
;
3789 spin_lock(&btv
->s_lock
);
3791 if (!list_empty(&btv
->vcapture
))
3792 new = list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3795 rc
= btread(BT848_RISC_COUNT
);
3796 if (NULL
!= old
&& (is_active(&old
->top
, rc
) ||
3797 is_active(&old
->bottom
, rc
))) {
3800 bttv_irq_debug_low_latency(btv
, rc
);
3801 spin_unlock(&btv
->s_lock
);
3807 btv
->loop_irq
&= ~4;
3808 bttv_buffer_activate_vbi(btv
, new);
3809 bttv_set_dma(btv
, 0);
3811 bttv_irq_wakeup_vbi(btv
, old
, VIDEOBUF_DONE
);
3812 spin_unlock(&btv
->s_lock
);
3815 static irqreturn_t
bttv_irq(int irq
, void *dev_id
)
3823 btv
=(struct bttv
*)dev_id
;
3827 /* get/clear interrupt status bits */
3828 stat
=btread(BT848_INT_STAT
);
3829 astat
=stat
&btread(BT848_INT_MASK
);
3833 btwrite(stat
,BT848_INT_STAT
);
3835 /* get device status bits */
3836 dstat
=btread(BT848_DSTATUS
);
3839 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
3840 btv
->c
.nr
, count
, btv
->field_count
,
3841 stat
>>28, btread(BT848_RISC_COUNT
));
3842 bttv_print_irqbits(stat
,astat
);
3843 if (stat
& BT848_INT_HLOCK
)
3844 pr_cont(" HLOC => %s",
3845 dstat
& BT848_DSTATUS_HLOC
3847 if (stat
& BT848_INT_VPRES
)
3848 pr_cont(" PRES => %s",
3849 dstat
& BT848_DSTATUS_PRES
3851 if (stat
& BT848_INT_FMTCHG
)
3852 pr_cont(" NUML => %s",
3853 dstat
& BT848_DSTATUS_NUML
3858 if (astat
&BT848_INT_VSYNC
)
3861 if ((astat
& BT848_INT_GPINT
) && btv
->remote
) {
3862 bttv_input_irq(btv
);
3865 if (astat
& BT848_INT_I2CDONE
) {
3866 btv
->i2c_done
= stat
;
3867 wake_up(&btv
->i2c_queue
);
3870 if ((astat
& BT848_INT_RISCI
) && (stat
& (4<<28)))
3871 bttv_irq_switch_vbi(btv
);
3873 if ((astat
& BT848_INT_RISCI
) && (stat
& (2<<28)))
3874 bttv_irq_wakeup_top(btv
);
3876 if ((astat
& BT848_INT_RISCI
) && (stat
& (1<<28)))
3877 bttv_irq_switch_video(btv
);
3879 if ((astat
& BT848_INT_HLOCK
) && btv
->opt_automute
)
3880 /* trigger automute */
3881 audio_mux_gpio(btv
, btv
->audio_input
, btv
->mute
);
3883 if (astat
& (BT848_INT_SCERR
|BT848_INT_OCERR
)) {
3884 pr_info("%d: %s%s @ %08x,",
3886 (astat
& BT848_INT_SCERR
) ? "SCERR" : "",
3887 (astat
& BT848_INT_OCERR
) ? "OCERR" : "",
3888 btread(BT848_RISC_COUNT
));
3889 bttv_print_irqbits(stat
,astat
);
3892 bttv_print_riscaddr(btv
);
3894 if (fdsr
&& astat
& BT848_INT_FDSR
) {
3895 pr_info("%d: FDSR @ %08x\n",
3896 btv
->c
.nr
, btread(BT848_RISC_COUNT
));
3898 bttv_print_riscaddr(btv
);
3904 if (count
> 8 || !(astat
& BT848_INT_GPINT
)) {
3905 btwrite(0, BT848_INT_MASK
);
3907 pr_err("%d: IRQ lockup, cleared int mask [",
3910 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3913 btwrite(btread(BT848_INT_MASK
) & (-1 ^ BT848_INT_GPINT
),
3917 bttv_print_irqbits(stat
,astat
);
3925 return IRQ_RETVAL(handled
);
3929 /* ----------------------------------------------------------------------- */
3930 /* initialization */
3932 static struct video_device
*vdev_init(struct bttv
*btv
,
3933 const struct video_device
*template,
3934 const char *type_name
)
3936 struct video_device
*vfd
;
3938 vfd
= video_device_alloc();
3942 vfd
->v4l2_dev
= &btv
->c
.v4l2_dev
;
3943 vfd
->release
= video_device_release
;
3944 vfd
->debug
= bttv_debug
;
3945 set_bit(V4L2_FL_USE_FH_PRIO
, &vfd
->flags
);
3946 video_set_drvdata(vfd
, btv
);
3947 snprintf(vfd
->name
, sizeof(vfd
->name
), "BT%d%s %s (%s)",
3948 btv
->id
, (btv
->id
==848 && btv
->revision
==0x12) ? "A" : "",
3949 type_name
, bttv_tvcards
[btv
->c
.type
].name
);
3950 if (btv
->tuner_type
== TUNER_ABSENT
) {
3951 v4l2_disable_ioctl(vfd
, VIDIOC_G_FREQUENCY
);
3952 v4l2_disable_ioctl(vfd
, VIDIOC_S_FREQUENCY
);
3953 v4l2_disable_ioctl(vfd
, VIDIOC_G_TUNER
);
3954 v4l2_disable_ioctl(vfd
, VIDIOC_S_TUNER
);
3959 static void bttv_unregister_video(struct bttv
*btv
)
3961 if (btv
->video_dev
) {
3962 if (video_is_registered(btv
->video_dev
))
3963 video_unregister_device(btv
->video_dev
);
3965 video_device_release(btv
->video_dev
);
3966 btv
->video_dev
= NULL
;
3969 if (video_is_registered(btv
->vbi_dev
))
3970 video_unregister_device(btv
->vbi_dev
);
3972 video_device_release(btv
->vbi_dev
);
3973 btv
->vbi_dev
= NULL
;
3975 if (btv
->radio_dev
) {
3976 if (video_is_registered(btv
->radio_dev
))
3977 video_unregister_device(btv
->radio_dev
);
3979 video_device_release(btv
->radio_dev
);
3980 btv
->radio_dev
= NULL
;
3984 /* register video4linux devices */
3985 static int bttv_register_video(struct bttv
*btv
)
3988 pr_notice("Overlay support disabled\n");
3991 btv
->video_dev
= vdev_init(btv
, &bttv_video_template
, "video");
3993 if (NULL
== btv
->video_dev
)
3995 if (video_register_device(btv
->video_dev
, VFL_TYPE_GRABBER
,
3996 video_nr
[btv
->c
.nr
]) < 0)
3998 pr_info("%d: registered device %s\n",
3999 btv
->c
.nr
, video_device_node_name(btv
->video_dev
));
4000 if (device_create_file(&btv
->video_dev
->dev
,
4001 &dev_attr_card
)<0) {
4002 pr_err("%d: device_create_file 'card' failed\n", btv
->c
.nr
);
4007 btv
->vbi_dev
= vdev_init(btv
, &bttv_video_template
, "vbi");
4009 if (NULL
== btv
->vbi_dev
)
4011 if (video_register_device(btv
->vbi_dev
, VFL_TYPE_VBI
,
4012 vbi_nr
[btv
->c
.nr
]) < 0)
4014 pr_info("%d: registered device %s\n",
4015 btv
->c
.nr
, video_device_node_name(btv
->vbi_dev
));
4017 if (!btv
->has_radio
)
4020 btv
->radio_dev
= vdev_init(btv
, &radio_template
, "radio");
4021 if (NULL
== btv
->radio_dev
)
4023 btv
->radio_dev
->ctrl_handler
= &btv
->radio_ctrl_handler
;
4024 if (video_register_device(btv
->radio_dev
, VFL_TYPE_RADIO
,
4025 radio_nr
[btv
->c
.nr
]) < 0)
4027 pr_info("%d: registered device %s\n",
4028 btv
->c
.nr
, video_device_node_name(btv
->radio_dev
));
4034 bttv_unregister_video(btv
);
4039 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4040 /* response on cards with no firmware is not enabled by OF */
4041 static void pci_set_command(struct pci_dev
*dev
)
4043 #if defined(__powerpc__)
4046 pci_read_config_dword(dev
, PCI_COMMAND
, &cmd
);
4047 cmd
= (cmd
| PCI_COMMAND_MEMORY
);
4048 pci_write_config_dword(dev
, PCI_COMMAND
, cmd
);
4052 static int bttv_probe(struct pci_dev
*dev
, const struct pci_device_id
*pci_id
)
4054 struct v4l2_frequency init_freq
= {
4056 .type
= V4L2_TUNER_ANALOG_TV
,
4062 struct v4l2_ctrl_handler
*hdl
;
4064 if (bttv_num
== BTTV_MAX
)
4066 pr_info("Bt8xx card found (%d)\n", bttv_num
);
4067 bttvs
[bttv_num
] = btv
= kzalloc(sizeof(*btv
), GFP_KERNEL
);
4069 pr_err("out of memory\n");
4072 btv
->c
.nr
= bttv_num
;
4073 snprintf(btv
->c
.v4l2_dev
.name
, sizeof(btv
->c
.v4l2_dev
.name
),
4074 "bttv%d", btv
->c
.nr
);
4076 /* initialize structs / fill in defaults */
4077 mutex_init(&btv
->lock
);
4078 spin_lock_init(&btv
->s_lock
);
4079 spin_lock_init(&btv
->gpio_lock
);
4080 init_waitqueue_head(&btv
->i2c_queue
);
4081 INIT_LIST_HEAD(&btv
->c
.subs
);
4082 INIT_LIST_HEAD(&btv
->capture
);
4083 INIT_LIST_HEAD(&btv
->vcapture
);
4085 init_timer(&btv
->timeout
);
4086 btv
->timeout
.function
= bttv_irq_timeout
;
4087 btv
->timeout
.data
= (unsigned long)btv
;
4090 btv
->tuner_type
= UNSET
;
4091 btv
->new_input
= UNSET
;
4092 btv
->has_radio
=radio
[btv
->c
.nr
];
4094 /* pci stuff (init, get irq/mmio, ... */
4096 btv
->id
= dev
->device
;
4097 if (pci_enable_device(dev
)) {
4098 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4101 if (pci_set_dma_mask(dev
, DMA_BIT_MASK(32))) {
4102 pr_warn("%d: No suitable DMA available\n", btv
->c
.nr
);
4105 if (!request_mem_region(pci_resource_start(dev
,0),
4106 pci_resource_len(dev
,0),
4107 btv
->c
.v4l2_dev
.name
)) {
4108 pr_warn("%d: can't request iomem (0x%llx)\n",
4110 (unsigned long long)pci_resource_start(dev
, 0));
4113 pci_set_master(dev
);
4114 pci_set_command(dev
);
4116 result
= v4l2_device_register(&dev
->dev
, &btv
->c
.v4l2_dev
);
4118 pr_warn("%d: v4l2_device_register() failed\n", btv
->c
.nr
);
4121 hdl
= &btv
->ctrl_handler
;
4122 v4l2_ctrl_handler_init(hdl
, 20);
4123 btv
->c
.v4l2_dev
.ctrl_handler
= hdl
;
4124 v4l2_ctrl_handler_init(&btv
->radio_ctrl_handler
, 6);
4126 btv
->revision
= dev
->revision
;
4127 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
4128 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4129 bttv_num
, btv
->id
, btv
->revision
, pci_name(dev
),
4130 btv
->c
.pci
->irq
, lat
,
4131 (unsigned long long)pci_resource_start(dev
, 0));
4134 btv
->bt848_mmio
= ioremap(pci_resource_start(dev
, 0), 0x1000);
4135 if (NULL
== btv
->bt848_mmio
) {
4136 pr_err("%d: ioremap() failed\n", btv
->c
.nr
);
4144 /* disable irqs, register irq handler */
4145 btwrite(0, BT848_INT_MASK
);
4146 result
= request_irq(btv
->c
.pci
->irq
, bttv_irq
,
4147 IRQF_SHARED
| IRQF_DISABLED
, btv
->c
.v4l2_dev
.name
, (void *)btv
);
4149 pr_err("%d: can't get IRQ %d\n",
4150 bttv_num
, btv
->c
.pci
->irq
);
4154 if (0 != bttv_handle_chipset(btv
)) {
4159 /* init options from insmod args */
4160 btv
->opt_combfilter
= combfilter
;
4161 bttv_ctrl_combfilter
.def
= combfilter
;
4162 bttv_ctrl_lumafilter
.def
= lumafilter
;
4163 btv
->opt_automute
= automute
;
4164 bttv_ctrl_automute
.def
= automute
;
4165 bttv_ctrl_agc_crush
.def
= agc_crush
;
4166 btv
->opt_vcr_hack
= vcr_hack
;
4167 bttv_ctrl_vcr_hack
.def
= vcr_hack
;
4168 bttv_ctrl_whitecrush_upper
.def
= whitecrush_upper
;
4169 bttv_ctrl_whitecrush_lower
.def
= whitecrush_lower
;
4170 btv
->opt_uv_ratio
= uv_ratio
;
4171 bttv_ctrl_uv_ratio
.def
= uv_ratio
;
4172 bttv_ctrl_full_luma
.def
= full_luma_range
;
4173 bttv_ctrl_coring
.def
= coring
;
4175 /* fill struct bttv with some useful defaults */
4176 btv
->init
.btv
= btv
;
4177 btv
->init
.ov
.w
.width
= 320;
4178 btv
->init
.ov
.w
.height
= 240;
4179 btv
->init
.fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
4180 btv
->init
.width
= 320;
4181 btv
->init
.height
= 240;
4182 btv
->init
.ov
.w
.width
= 320;
4183 btv
->init
.ov
.w
.height
= 240;
4184 btv
->init
.ov
.field
= V4L2_FIELD_INTERLACED
;
4187 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4188 V4L2_CID_BRIGHTNESS
, 0, 0xff00, 0x100, 32768);
4189 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4190 V4L2_CID_CONTRAST
, 0, 0xff80, 0x80, 0x6c00);
4191 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4192 V4L2_CID_SATURATION
, 0, 0xff80, 0x80, 32768);
4193 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4194 V4L2_CID_COLOR_KILLER
, 0, 1, 1, 0);
4195 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4196 V4L2_CID_HUE
, 0, 0xff00, 0x100, 32768);
4197 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4198 V4L2_CID_CHROMA_AGC
, 0, 1, 1, !!chroma_agc
);
4199 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4200 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
4201 if (btv
->volume_gpio
)
4202 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4203 V4L2_CID_AUDIO_VOLUME
, 0, 0xff00, 0x100, 0xff00);
4204 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_combfilter
, NULL
);
4205 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_automute
, NULL
);
4206 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_lumafilter
, NULL
);
4207 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_agc_crush
, NULL
);
4208 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_vcr_hack
, NULL
);
4209 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_whitecrush_lower
, NULL
);
4210 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_whitecrush_upper
, NULL
);
4211 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_uv_ratio
, NULL
);
4212 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_full_luma
, NULL
);
4213 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_coring
, NULL
);
4215 /* initialize hardware */
4217 bttv_gpio_tracking(btv
,"pre-init");
4219 bttv_risc_init_main(btv
);
4223 btwrite(0x00, BT848_GPIO_REG_INP
);
4224 btwrite(0x00, BT848_GPIO_OUT_EN
);
4226 bttv_gpio_tracking(btv
,"init");
4228 /* needs to be done before i2c is registered */
4229 bttv_init_card1(btv
);
4231 /* register i2c + gpio */
4234 /* some card-specific stuff (needs working i2c) */
4235 bttv_init_card2(btv
);
4236 bttv_init_tuner(btv
);
4237 if (btv
->tuner_type
!= TUNER_ABSENT
) {
4238 bttv_set_frequency(btv
, &init_freq
);
4239 btv
->radio_freq
= 90500 * 16; /* 90.5Mhz default */
4241 btv
->std
= V4L2_STD_PAL
;
4243 if (!bttv_tvcards
[btv
->c
.type
].no_video
)
4244 v4l2_ctrl_handler_setup(hdl
);
4246 result
= hdl
->error
;
4252 /* register video4linux + input */
4253 if (!bttv_tvcards
[btv
->c
.type
].no_video
) {
4254 v4l2_ctrl_add_handler(&btv
->radio_ctrl_handler
, hdl
,
4255 v4l2_ctrl_radio_filter
);
4256 if (btv
->radio_ctrl_handler
.error
) {
4257 result
= btv
->radio_ctrl_handler
.error
;
4260 set_input(btv
, 0, btv
->tvnorm
);
4261 bttv_crop_reset(&btv
->crop
[0], btv
->tvnorm
);
4262 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
4263 disclaim_vbi_lines(btv
);
4264 disclaim_video_lines(btv
);
4265 bttv_register_video(btv
);
4268 /* add subdevices and autoload dvb-bt8xx if needed */
4269 if (bttv_tvcards
[btv
->c
.type
].has_dvb
) {
4270 bttv_sub_add_device(&btv
->c
, "dvb");
4271 request_modules(btv
);
4275 init_bttv_i2c_ir(btv
);
4276 bttv_input_init(btv
);
4279 /* everything is fine */
4284 free_irq(btv
->c
.pci
->irq
,btv
);
4287 v4l2_ctrl_handler_free(&btv
->ctrl_handler
);
4288 v4l2_ctrl_handler_free(&btv
->radio_ctrl_handler
);
4289 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4292 if (btv
->bt848_mmio
)
4293 iounmap(btv
->bt848_mmio
);
4294 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4295 pci_resource_len(btv
->c
.pci
,0));
4299 static void bttv_remove(struct pci_dev
*pci_dev
)
4301 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4302 struct bttv
*btv
= to_bttv(v4l2_dev
);
4305 pr_info("%d: unloading\n", btv
->c
.nr
);
4307 if (bttv_tvcards
[btv
->c
.type
].has_dvb
)
4308 flush_request_modules(btv
);
4310 /* shutdown everything (DMA+IRQs) */
4311 btand(~15, BT848_GPIO_DMA_CTL
);
4312 btwrite(0, BT848_INT_MASK
);
4313 btwrite(~0x0, BT848_INT_STAT
);
4314 btwrite(0x0, BT848_GPIO_OUT_EN
);
4316 bttv_gpio_tracking(btv
,"cleanup");
4318 /* tell gpio modules we are leaving ... */
4320 bttv_input_fini(btv
);
4321 bttv_sub_del_devices(&btv
->c
);
4323 /* unregister i2c_bus + input */
4326 /* unregister video4linux */
4327 bttv_unregister_video(btv
);
4329 /* free allocated memory */
4330 v4l2_ctrl_handler_free(&btv
->ctrl_handler
);
4331 v4l2_ctrl_handler_free(&btv
->radio_ctrl_handler
);
4332 btcx_riscmem_free(btv
->c
.pci
,&btv
->main
);
4334 /* free resources */
4335 free_irq(btv
->c
.pci
->irq
,btv
);
4336 iounmap(btv
->bt848_mmio
);
4337 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4338 pci_resource_len(btv
->c
.pci
,0));
4340 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4341 bttvs
[btv
->c
.nr
] = NULL
;
4348 static int bttv_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
4350 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4351 struct bttv
*btv
= to_bttv(v4l2_dev
);
4352 struct bttv_buffer_set idle
;
4353 unsigned long flags
;
4355 dprintk("%d: suspend %d\n", btv
->c
.nr
, state
.event
);
4357 /* stop dma + irqs */
4358 spin_lock_irqsave(&btv
->s_lock
,flags
);
4359 memset(&idle
, 0, sizeof(idle
));
4360 btv
->state
.video
= btv
->curr
;
4361 btv
->state
.vbi
= btv
->cvbi
;
4362 btv
->state
.loop_irq
= btv
->loop_irq
;
4365 bttv_buffer_activate_video(btv
, &idle
);
4366 bttv_buffer_activate_vbi(btv
, NULL
);
4367 bttv_set_dma(btv
, 0);
4368 btwrite(0, BT848_INT_MASK
);
4369 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4371 /* save bt878 state */
4372 btv
->state
.gpio_enable
= btread(BT848_GPIO_OUT_EN
);
4373 btv
->state
.gpio_data
= gpio_read();
4375 /* save pci state */
4376 pci_save_state(pci_dev
);
4377 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
4378 pci_disable_device(pci_dev
);
4379 btv
->state
.disabled
= 1;
4384 static int bttv_resume(struct pci_dev
*pci_dev
)
4386 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4387 struct bttv
*btv
= to_bttv(v4l2_dev
);
4388 unsigned long flags
;
4391 dprintk("%d: resume\n", btv
->c
.nr
);
4393 /* restore pci state */
4394 if (btv
->state
.disabled
) {
4395 err
=pci_enable_device(pci_dev
);
4397 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4400 btv
->state
.disabled
= 0;
4402 err
=pci_set_power_state(pci_dev
, PCI_D0
);
4404 pci_disable_device(pci_dev
);
4405 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4406 btv
->state
.disabled
= 1;
4410 pci_restore_state(pci_dev
);
4412 /* restore bt878 state */
4413 bttv_reinit_bt848(btv
);
4414 gpio_inout(0xffffff, btv
->state
.gpio_enable
);
4415 gpio_write(btv
->state
.gpio_data
);
4418 spin_lock_irqsave(&btv
->s_lock
,flags
);
4419 btv
->curr
= btv
->state
.video
;
4420 btv
->cvbi
= btv
->state
.vbi
;
4421 btv
->loop_irq
= btv
->state
.loop_irq
;
4422 bttv_buffer_activate_video(btv
, &btv
->curr
);
4423 bttv_buffer_activate_vbi(btv
, btv
->cvbi
);
4424 bttv_set_dma(btv
, 0);
4425 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4430 static struct pci_device_id bttv_pci_tbl
[] = {
4431 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT848
), 0},
4432 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT849
), 0},
4433 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT878
), 0},
4434 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT879
), 0},
4435 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_FUSION879
), 0},
4439 MODULE_DEVICE_TABLE(pci
, bttv_pci_tbl
);
4441 static struct pci_driver bttv_pci_driver
= {
4443 .id_table
= bttv_pci_tbl
,
4444 .probe
= bttv_probe
,
4445 .remove
= bttv_remove
,
4447 .suspend
= bttv_suspend
,
4448 .resume
= bttv_resume
,
4452 static int __init
bttv_init_module(void)
4458 pr_info("driver version %s loaded\n", BTTV_VERSION
);
4459 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
4461 if (gbufsize
> BTTV_MAX_FBUF
)
4462 gbufsize
= BTTV_MAX_FBUF
;
4463 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
4465 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4466 gbuffers
, gbufsize
>> 10, gbufsize
>> PAGE_SHIFT
);
4468 bttv_check_chipset();
4470 ret
= bus_register(&bttv_sub_bus_type
);
4472 pr_warn("bus_register error: %d\n", ret
);
4475 ret
= pci_register_driver(&bttv_pci_driver
);
4477 bus_unregister(&bttv_sub_bus_type
);
4482 static void __exit
bttv_cleanup_module(void)
4484 pci_unregister_driver(&bttv_pci_driver
);
4485 bus_unregister(&bttv_sub_bus_type
);
4488 module_init(bttv_init_module
);
4489 module_exit(bttv_cleanup_module
);