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 videostart0 ITU-R frame line number of the line corresponding
267 to vdelay in the first field. */
268 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
269 vdelay, sheight, videostart0) \
270 .cropcap.bounds.left = minhdelayx1, \
271 /* * 2 because vertically we count field lines times two, */ \
272 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
273 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
274 /* 4 is a safety margin at the end of the line. */ \
275 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
276 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
277 .cropcap.defrect.left = hdelayx1, \
278 .cropcap.defrect.top = (videostart0) * 2, \
279 .cropcap.defrect.width = swidth, \
280 .cropcap.defrect.height = sheight, \
281 .cropcap.pixelaspect.numerator = totalwidth, \
282 .cropcap.pixelaspect.denominator = sqwidth,
284 const struct bttv_tvnorm bttv_tvnorms
[] = {
286 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
287 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
289 .v4l2_id
= V4L2_STD_PAL
,
297 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
298 .scaledtwidth
= 1135,
302 .vbipack
= 255, /* min (2048 / 4, 0x1ff) & 0xff */
304 /* ITU-R frame line number of the first VBI line
305 we can capture, of the first and second field.
306 The last line is determined by cropcap.bounds. */
307 .vbistart
= { 7, 320 },
308 CROPCAP(/* minhdelayx1 */ 68,
310 /* Should be (768 * 1135 + 944 / 2) / 944.
311 cropcap.defrect is used for image width
312 checks, so we keep the old value 924. */
314 /* totalwidth */ 1135,
317 /* bt878 (and bt848?) can capture another
318 line below active video. */
319 /* sheight */ (576 + 2) + 0x20 - 2,
320 /* videostart0 */ 23)
322 .v4l2_id
= V4L2_STD_NTSC_M
| V4L2_STD_NTSC_M_KR
,
330 .iform
= (BT848_IFORM_NTSC
|BT848_IFORM_XT0
),
335 .vbipack
= 144, /* min (1600 / 4, 0x1ff) & 0xff */
337 .vbistart
= { 10, 273 },
338 CROPCAP(/* minhdelayx1 */ 68,
340 /* Should be (640 * 910 + 780 / 2) / 780? */
342 /* totalwidth */ 910,
346 /* videostart0 */ 23)
348 .v4l2_id
= V4L2_STD_SECAM
,
356 .iform
= (BT848_IFORM_SECAM
|BT848_IFORM_XT1
),
357 .scaledtwidth
= 1135,
362 .sram
= 0, /* like PAL, correct? */
363 .vbistart
= { 7, 320 },
364 CROPCAP(/* minhdelayx1 */ 68,
367 /* totalwidth */ 1135,
371 /* videostart0 */ 23)
373 .v4l2_id
= V4L2_STD_PAL_Nc
,
381 .iform
= (BT848_IFORM_PAL_NC
|BT848_IFORM_XT0
),
388 .vbistart
= { 7, 320 },
389 CROPCAP(/* minhdelayx1 */ 68,
391 /* swidth */ (640 * 910 + 780 / 2) / 780,
392 /* totalwidth */ 910,
396 /* videostart0 */ 23)
398 .v4l2_id
= V4L2_STD_PAL_M
,
406 .iform
= (BT848_IFORM_PAL_M
|BT848_IFORM_XT0
),
413 .vbistart
= { 10, 273 },
414 CROPCAP(/* minhdelayx1 */ 68,
416 /* swidth */ (640 * 910 + 780 / 2) / 780,
417 /* totalwidth */ 910,
421 /* videostart0 */ 23)
423 .v4l2_id
= V4L2_STD_PAL_N
,
431 .iform
= (BT848_IFORM_PAL_N
|BT848_IFORM_XT1
),
438 .vbistart
= { 7, 320 },
439 CROPCAP(/* minhdelayx1 */ 68,
441 /* swidth */ (768 * 1135 + 944 / 2) / 944,
442 /* totalwidth */ 1135,
446 /* videostart0 */ 23)
448 .v4l2_id
= V4L2_STD_NTSC_M_JP
,
456 .iform
= (BT848_IFORM_NTSC_J
|BT848_IFORM_XT0
),
463 .vbistart
= { 10, 273 },
464 CROPCAP(/* minhdelayx1 */ 68,
466 /* swidth */ (640 * 910 + 780 / 2) / 780,
467 /* totalwidth */ 910,
471 /* videostart0 */ 23)
473 /* that one hopefully works with the strange timing
474 * which video recorders produce when playing a NTSC
475 * tape on a PAL TV ... */
476 .v4l2_id
= V4L2_STD_PAL_60
,
484 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
485 .scaledtwidth
= 1135,
492 .vbistart
= { 10, 273 },
493 CROPCAP(/* minhdelayx1 */ 68,
496 /* totalwidth */ 1135,
500 /* videostart0 */ 23)
503 static const unsigned int BTTV_TVNORMS
= ARRAY_SIZE(bttv_tvnorms
);
505 /* ----------------------------------------------------------------------- */
507 packed pixel formats must come first */
508 static const struct bttv_format formats
[] = {
510 .name
= "8 bpp, gray",
511 .fourcc
= V4L2_PIX_FMT_GREY
,
512 .btformat
= BT848_COLOR_FMT_Y8
,
514 .flags
= FORMAT_FLAGS_PACKED
,
516 .name
= "8 bpp, dithered color",
517 .fourcc
= V4L2_PIX_FMT_HI240
,
518 .btformat
= BT848_COLOR_FMT_RGB8
,
520 .flags
= FORMAT_FLAGS_PACKED
| FORMAT_FLAGS_DITHER
,
522 .name
= "15 bpp RGB, le",
523 .fourcc
= V4L2_PIX_FMT_RGB555
,
524 .btformat
= BT848_COLOR_FMT_RGB15
,
526 .flags
= FORMAT_FLAGS_PACKED
,
528 .name
= "15 bpp RGB, be",
529 .fourcc
= V4L2_PIX_FMT_RGB555X
,
530 .btformat
= BT848_COLOR_FMT_RGB15
,
531 .btswap
= 0x03, /* byteswap */
533 .flags
= FORMAT_FLAGS_PACKED
,
535 .name
= "16 bpp RGB, le",
536 .fourcc
= V4L2_PIX_FMT_RGB565
,
537 .btformat
= BT848_COLOR_FMT_RGB16
,
539 .flags
= FORMAT_FLAGS_PACKED
,
541 .name
= "16 bpp RGB, be",
542 .fourcc
= V4L2_PIX_FMT_RGB565X
,
543 .btformat
= BT848_COLOR_FMT_RGB16
,
544 .btswap
= 0x03, /* byteswap */
546 .flags
= FORMAT_FLAGS_PACKED
,
548 .name
= "24 bpp RGB, le",
549 .fourcc
= V4L2_PIX_FMT_BGR24
,
550 .btformat
= BT848_COLOR_FMT_RGB24
,
552 .flags
= FORMAT_FLAGS_PACKED
,
554 .name
= "32 bpp RGB, le",
555 .fourcc
= V4L2_PIX_FMT_BGR32
,
556 .btformat
= BT848_COLOR_FMT_RGB32
,
558 .flags
= FORMAT_FLAGS_PACKED
,
560 .name
= "32 bpp RGB, be",
561 .fourcc
= V4L2_PIX_FMT_RGB32
,
562 .btformat
= BT848_COLOR_FMT_RGB32
,
563 .btswap
= 0x0f, /* byte+word swap */
565 .flags
= FORMAT_FLAGS_PACKED
,
567 .name
= "4:2:2, packed, YUYV",
568 .fourcc
= V4L2_PIX_FMT_YUYV
,
569 .btformat
= BT848_COLOR_FMT_YUY2
,
571 .flags
= FORMAT_FLAGS_PACKED
,
573 .name
= "4:2:2, packed, UYVY",
574 .fourcc
= V4L2_PIX_FMT_UYVY
,
575 .btformat
= BT848_COLOR_FMT_YUY2
,
576 .btswap
= 0x03, /* byteswap */
578 .flags
= FORMAT_FLAGS_PACKED
,
580 .name
= "4:2:2, planar, Y-Cb-Cr",
581 .fourcc
= V4L2_PIX_FMT_YUV422P
,
582 .btformat
= BT848_COLOR_FMT_YCrCb422
,
584 .flags
= FORMAT_FLAGS_PLANAR
,
588 .name
= "4:2:0, planar, Y-Cb-Cr",
589 .fourcc
= V4L2_PIX_FMT_YUV420
,
590 .btformat
= BT848_COLOR_FMT_YCrCb422
,
592 .flags
= FORMAT_FLAGS_PLANAR
,
596 .name
= "4:2:0, planar, Y-Cr-Cb",
597 .fourcc
= V4L2_PIX_FMT_YVU420
,
598 .btformat
= BT848_COLOR_FMT_YCrCb422
,
600 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
604 .name
= "4:1:1, planar, Y-Cb-Cr",
605 .fourcc
= V4L2_PIX_FMT_YUV411P
,
606 .btformat
= BT848_COLOR_FMT_YCrCb411
,
608 .flags
= FORMAT_FLAGS_PLANAR
,
612 .name
= "4:1:0, planar, Y-Cb-Cr",
613 .fourcc
= V4L2_PIX_FMT_YUV410
,
614 .btformat
= BT848_COLOR_FMT_YCrCb411
,
616 .flags
= FORMAT_FLAGS_PLANAR
,
620 .name
= "4:1:0, planar, Y-Cr-Cb",
621 .fourcc
= V4L2_PIX_FMT_YVU410
,
622 .btformat
= BT848_COLOR_FMT_YCrCb411
,
624 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
628 .name
= "raw scanlines",
630 .btformat
= BT848_COLOR_FMT_RAW
,
632 .flags
= FORMAT_FLAGS_RAW
,
635 static const unsigned int FORMATS
= ARRAY_SIZE(formats
);
637 /* ----------------------------------------------------------------------- */
638 /* resource management */
641 RESOURCE_ allocated by freed by
643 VIDEO_READ bttv_read 1) bttv_read 2)
645 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
646 VIDIOC_QBUF 1) bttv_release
649 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
650 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
653 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
654 VIDIOC_QBUF 1) bttv_release
655 bttv_read, bttv_poll 1) 4)
657 1) The resource must be allocated when we enter buffer prepare functions
658 and remain allocated while buffers are in the DMA queue.
659 2) This is a single frame read.
660 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
661 RESOURCE_OVERLAY is allocated.
662 4) This is a continuous read, implies VIDIOC_STREAMON.
664 Note this driver permits video input and standard changes regardless if
665 resources are allocated.
668 #define VBI_RESOURCES (RESOURCE_VBI)
669 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
670 RESOURCE_VIDEO_STREAM | \
674 int check_alloc_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bit
)
676 int xbits
; /* mutual exclusive resources */
678 if (fh
->resources
& bit
)
679 /* have it already allocated */
683 if (bit
& (RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
))
684 xbits
|= RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
;
687 if (btv
->resources
& xbits
) {
688 /* no, someone else uses it */
692 if ((bit
& VIDEO_RESOURCES
)
693 && 0 == (btv
->resources
& VIDEO_RESOURCES
)) {
694 /* Do crop - use current, don't - use default parameters. */
695 __s32 top
= btv
->crop
[!!fh
->do_crop
].rect
.top
;
697 if (btv
->vbi_end
> top
)
700 /* We cannot capture the same line as video and VBI data.
701 Claim scan lines crop[].rect.top to bottom. */
702 btv
->crop_start
= top
;
703 } else if (bit
& VBI_RESOURCES
) {
704 __s32 end
= fh
->vbi_fmt
.end
;
706 if (end
> btv
->crop_start
)
709 /* Claim scan lines above fh->vbi_fmt.end. */
713 /* it's free, grab it */
714 fh
->resources
|= bit
;
715 btv
->resources
|= bit
;
723 int check_btres(struct bttv_fh
*fh
, int bit
)
725 return (fh
->resources
& bit
);
729 int locked_btres(struct bttv
*btv
, int bit
)
731 return (btv
->resources
& bit
);
734 /* Call with btv->lock down. */
736 disclaim_vbi_lines(struct bttv
*btv
)
741 /* Call with btv->lock down. */
743 disclaim_video_lines(struct bttv
*btv
)
745 const struct bttv_tvnorm
*tvnorm
;
748 tvnorm
= &bttv_tvnorms
[btv
->tvnorm
];
749 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
750 + tvnorm
->cropcap
.bounds
.height
;
752 /* VBI capturing ends at VDELAY, start of video capturing, no
753 matter how many lines the VBI RISC program expects. When video
754 capturing is off, it shall no longer "preempt" VBI capturing,
755 so we set VDELAY to maximum. */
756 crop
= btread(BT848_E_CROP
) | 0xc0;
757 btwrite(crop
, BT848_E_CROP
);
758 btwrite(0xfe, BT848_E_VDELAY_LO
);
759 btwrite(crop
, BT848_O_CROP
);
760 btwrite(0xfe, BT848_O_VDELAY_LO
);
764 void free_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bits
)
766 if ((fh
->resources
& bits
) != bits
) {
767 /* trying to free resources not allocated by us ... */
768 pr_err("BUG! (btres)\n");
770 fh
->resources
&= ~bits
;
771 btv
->resources
&= ~bits
;
773 bits
= btv
->resources
;
775 if (0 == (bits
& VIDEO_RESOURCES
))
776 disclaim_video_lines(btv
);
778 if (0 == (bits
& VBI_RESOURCES
))
779 disclaim_vbi_lines(btv
);
782 /* ----------------------------------------------------------------------- */
783 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
785 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
786 PLL_X = Reference pre-divider (0=1, 1=2)
787 PLL_C = Post divider (0=6, 1=4)
788 PLL_I = Integer input
789 PLL_F = Fractional input
791 F_input = 28.636363 MHz:
792 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
795 static void set_pll_freq(struct bttv
*btv
, unsigned int fin
, unsigned int fout
)
797 unsigned char fl
, fh
, fi
;
799 /* prevent overflows */
812 btwrite(fl
, BT848_PLL_F_LO
);
813 btwrite(fh
, BT848_PLL_F_HI
);
814 btwrite(fi
|BT848_PLL_X
, BT848_PLL_XCI
);
817 static void set_pll(struct bttv
*btv
)
821 if (!btv
->pll
.pll_crystal
)
824 if (btv
->pll
.pll_ofreq
== btv
->pll
.pll_current
) {
825 dprintk("%d: PLL: no change required\n", btv
->c
.nr
);
829 if (btv
->pll
.pll_ifreq
== btv
->pll
.pll_ofreq
) {
831 if (btv
->pll
.pll_current
== 0)
834 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
835 btv
->c
.nr
, btv
->pll
.pll_ifreq
);
836 btwrite(0x00,BT848_TGCTRL
);
837 btwrite(0x00,BT848_PLL_XCI
);
838 btv
->pll
.pll_current
= 0;
843 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
845 btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
846 set_pll_freq(btv
, btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
848 for (i
=0; i
<10; i
++) {
849 /* Let other people run while the PLL stabilizes */
852 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_PLOCK
) {
853 btwrite(0,BT848_DSTATUS
);
855 btwrite(0x08,BT848_TGCTRL
);
856 btv
->pll
.pll_current
= btv
->pll
.pll_ofreq
;
858 pr_info("PLL set ok\n");
862 btv
->pll
.pll_current
= -1;
864 pr_info("Setting PLL failed\n");
868 /* used to switch between the bt848's analog/digital video capture modes */
869 static void bt848A_set_timing(struct bttv
*btv
)
872 int table_idx
= bttv_tvnorms
[btv
->tvnorm
].sram
;
873 int fsc
= bttv_tvnorms
[btv
->tvnorm
].Fsc
;
875 if (btv
->input
== btv
->dig
) {
876 dprintk("%d: load digital timing table (table_idx=%d)\n",
877 btv
->c
.nr
,table_idx
);
879 /* timing change...reset timing generator address */
880 btwrite(0x00, BT848_TGCTRL
);
881 btwrite(0x02, BT848_TGCTRL
);
882 btwrite(0x00, BT848_TGCTRL
);
884 len
=SRAM_Table
[table_idx
][0];
885 for(i
= 1; i
<= len
; i
++)
886 btwrite(SRAM_Table
[table_idx
][i
],BT848_TGLB
);
887 btv
->pll
.pll_ofreq
= 27000000;
890 btwrite(0x11, BT848_TGCTRL
);
891 btwrite(0x41, BT848_DVSIF
);
893 btv
->pll
.pll_ofreq
= fsc
;
895 btwrite(0x0, BT848_DVSIF
);
899 /* ----------------------------------------------------------------------- */
901 static void bt848_bright(struct bttv
*btv
, int bright
)
905 // printk("set bright: %d\n", bright); // DEBUG
906 btv
->bright
= bright
;
908 /* We want -128 to 127 we get 0-65535 */
909 value
= (bright
>> 8) - 128;
910 btwrite(value
& 0xff, BT848_BRIGHT
);
913 static void bt848_hue(struct bttv
*btv
, int hue
)
920 value
= (hue
>> 8) - 128;
921 btwrite(value
& 0xff, BT848_HUE
);
924 static void bt848_contrast(struct bttv
*btv
, int cont
)
928 btv
->contrast
= cont
;
932 hibit
= (value
>> 6) & 4;
933 btwrite(value
& 0xff, BT848_CONTRAST_LO
);
934 btaor(hibit
, ~4, BT848_E_CONTROL
);
935 btaor(hibit
, ~4, BT848_O_CONTROL
);
938 static void bt848_sat(struct bttv
*btv
, int color
)
940 int val_u
,val_v
,hibits
;
942 btv
->saturation
= color
;
944 /* 0-511 for the color */
945 val_u
= ((color
* btv
->opt_uv_ratio
) / 50) >> 7;
946 val_v
= (((color
* (100 - btv
->opt_uv_ratio
) / 50) >>7)*180L)/254;
947 hibits
= (val_u
>> 7) & 2;
948 hibits
|= (val_v
>> 8) & 1;
949 btwrite(val_u
& 0xff, BT848_SAT_U_LO
);
950 btwrite(val_v
& 0xff, BT848_SAT_V_LO
);
951 btaor(hibits
, ~3, BT848_E_CONTROL
);
952 btaor(hibits
, ~3, BT848_O_CONTROL
);
955 /* ----------------------------------------------------------------------- */
958 video_mux(struct bttv
*btv
, unsigned int input
)
962 if (input
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
965 /* needed by RemoteVideo MX */
966 mask2
= bttv_tvcards
[btv
->c
.type
].gpiomask2
;
968 gpio_inout(mask2
,mask2
);
970 if (input
== btv
->svhs
) {
971 btor(BT848_CONTROL_COMP
, BT848_E_CONTROL
);
972 btor(BT848_CONTROL_COMP
, BT848_O_CONTROL
);
974 btand(~BT848_CONTROL_COMP
, BT848_E_CONTROL
);
975 btand(~BT848_CONTROL_COMP
, BT848_O_CONTROL
);
977 mux
= bttv_muxsel(btv
, input
);
978 btaor(mux
<<5, ~(3<<5), BT848_IFORM
);
979 dprintk("%d: video mux: input=%d mux=%d\n", btv
->c
.nr
, input
, mux
);
981 /* card specific hook */
982 if(bttv_tvcards
[btv
->c
.type
].muxsel_hook
)
983 bttv_tvcards
[btv
->c
.type
].muxsel_hook (btv
, input
);
987 static char *audio_modes
[] = {
988 "audio: tuner", "audio: radio", "audio: extern",
989 "audio: intern", "audio: mute"
993 audio_mux_gpio(struct bttv
*btv
, int input
, int mute
)
995 int gpio_val
, signal
, mute_gpio
;
997 gpio_inout(bttv_tvcards
[btv
->c
.type
].gpiomask
,
998 bttv_tvcards
[btv
->c
.type
].gpiomask
);
999 signal
= btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
;
1002 mute_gpio
= mute
|| (btv
->opt_automute
&& (!signal
|| !btv
->users
)
1003 && !btv
->has_radio_tuner
);
1006 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomute
;
1008 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomux
[input
];
1010 switch (btv
->c
.type
) {
1011 case BTTV_BOARD_VOODOOTV_FM
:
1012 case BTTV_BOARD_VOODOOTV_200
:
1013 gpio_val
= bttv_tda9880_setnorm(btv
, gpio_val
);
1017 gpio_bits(bttv_tvcards
[btv
->c
.type
].gpiomask
, gpio_val
);
1021 bttv_gpio_tracking(btv
, audio_modes
[mute_gpio
? 4 : input
]);
1025 audio_mute(struct bttv
*btv
, int mute
)
1027 struct v4l2_ctrl
*ctrl
;
1029 audio_mux_gpio(btv
, btv
->audio_input
, mute
);
1031 if (btv
->sd_msp34xx
) {
1032 ctrl
= v4l2_ctrl_find(btv
->sd_msp34xx
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1034 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1036 if (btv
->sd_tvaudio
) {
1037 ctrl
= v4l2_ctrl_find(btv
->sd_tvaudio
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1039 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1041 if (btv
->sd_tda7432
) {
1042 ctrl
= v4l2_ctrl_find(btv
->sd_tda7432
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
1044 v4l2_ctrl_s_ctrl(ctrl
, mute
);
1050 audio_input(struct bttv
*btv
, int input
)
1052 audio_mux_gpio(btv
, input
, btv
->mute
);
1054 if (btv
->sd_msp34xx
) {
1057 /* Note: the inputs tuner/radio/extern/intern are translated
1058 to msp routings. This assumes common behavior for all msp3400
1059 based TV cards. When this assumption fails, then the
1060 specific MSP routing must be added to the card table.
1061 For now this is sufficient. */
1063 case TVAUDIO_INPUT_RADIO
:
1064 /* Some boards need the msp do to the radio demod */
1065 if (btv
->radio_uses_msp_demodulator
) {
1066 in
= MSP_INPUT_DEFAULT
;
1069 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1070 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1072 case TVAUDIO_INPUT_EXTERN
:
1073 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER1
,
1074 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1076 case TVAUDIO_INPUT_INTERN
:
1077 /* Yes, this is the same input as for RADIO. I doubt
1078 if this is ever used. The only board with an INTERN
1079 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1080 that was tested. My guess is that the whole INTERN
1081 input does not work. */
1082 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1083 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1085 case TVAUDIO_INPUT_TUNER
:
1087 /* This is the only card that uses TUNER2, and afaik,
1088 is the only difference between the VOODOOTV_FM
1090 if (btv
->c
.type
== BTTV_BOARD_VOODOOTV_200
)
1091 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER2
, \
1092 MSP_DSP_IN_TUNER
, MSP_DSP_IN_TUNER
);
1094 in
= MSP_INPUT_DEFAULT
;
1097 v4l2_subdev_call(btv
->sd_msp34xx
, audio
, s_routing
,
1098 in
, MSP_OUTPUT_DEFAULT
, 0);
1100 if (btv
->sd_tvaudio
) {
1101 v4l2_subdev_call(btv
->sd_tvaudio
, audio
, s_routing
,
1108 bttv_crop_calc_limits(struct bttv_crop
*c
)
1110 /* Scale factor min. 1:1, max. 16:1. Min. image size
1111 48 x 32. Scaled width must be a multiple of 4. */
1114 /* For bug compatibility with VIDIOCGCAP and image
1115 size checks in earlier driver versions. */
1116 c
->min_scaled_width
= 48;
1117 c
->min_scaled_height
= 32;
1119 c
->min_scaled_width
=
1120 (max(48, c
->rect
.width
>> 4) + 3) & ~3;
1121 c
->min_scaled_height
=
1122 max(32, c
->rect
.height
>> 4);
1125 c
->max_scaled_width
= c
->rect
.width
& ~3;
1126 c
->max_scaled_height
= c
->rect
.height
;
1130 bttv_crop_reset(struct bttv_crop
*c
, unsigned int norm
)
1132 c
->rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1133 bttv_crop_calc_limits(c
);
1136 /* Call with btv->lock down. */
1138 set_tvnorm(struct bttv
*btv
, unsigned int norm
)
1140 const struct bttv_tvnorm
*tvnorm
;
1143 BUG_ON(norm
>= BTTV_TVNORMS
);
1144 BUG_ON(btv
->tvnorm
>= BTTV_TVNORMS
);
1146 tvnorm
= &bttv_tvnorms
[norm
];
1148 if (memcmp(&bttv_tvnorms
[btv
->tvnorm
].cropcap
, &tvnorm
->cropcap
,
1149 sizeof (tvnorm
->cropcap
))) {
1150 bttv_crop_reset(&btv
->crop
[0], norm
);
1151 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
1153 if (0 == (btv
->resources
& VIDEO_RESOURCES
)) {
1154 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
1155 + tvnorm
->cropcap
.bounds
.height
;
1161 btwrite(tvnorm
->adelay
, BT848_ADELAY
);
1162 btwrite(tvnorm
->bdelay
, BT848_BDELAY
);
1163 btaor(tvnorm
->iform
,~(BT848_IFORM_NORM
|BT848_IFORM_XTBOTH
),
1165 btwrite(tvnorm
->vbipack
, BT848_VBI_PACK_SIZE
);
1166 btwrite(1, BT848_VBI_PACK_DEL
);
1167 bt848A_set_timing(btv
);
1169 switch (btv
->c
.type
) {
1170 case BTTV_BOARD_VOODOOTV_FM
:
1171 case BTTV_BOARD_VOODOOTV_200
:
1172 bttv_tda9880_setnorm(btv
, gpio_read());
1175 id
= tvnorm
->v4l2_id
;
1176 bttv_call_all(btv
, core
, s_std
, id
);
1181 /* Call with btv->lock down. */
1183 set_input(struct bttv
*btv
, unsigned int input
, unsigned int norm
)
1185 unsigned long flags
;
1189 spin_lock_irqsave(&btv
->s_lock
,flags
);
1190 if (btv
->curr
.frame_irq
) {
1191 /* active capture -> delayed input switch */
1192 btv
->new_input
= input
;
1194 video_mux(btv
,input
);
1196 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1198 video_mux(btv
,input
);
1200 btv
->audio_input
= (btv
->tuner_type
!= TUNER_ABSENT
&& input
== 0) ?
1201 TVAUDIO_INPUT_TUNER
: TVAUDIO_INPUT_EXTERN
;
1202 audio_input(btv
, btv
->audio_input
);
1203 set_tvnorm(btv
, norm
);
1206 static void init_irqreg(struct bttv
*btv
)
1209 btwrite(0xfffffUL
, BT848_INT_STAT
);
1211 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1213 btwrite(BT848_INT_I2CDONE
,
1217 btwrite((btv
->triton1
) |
1218 (btv
->gpioirq
? BT848_INT_GPINT
: 0) |
1220 (fdsr
? BT848_INT_FDSR
: 0) |
1221 BT848_INT_RISCI
| BT848_INT_OCERR
|
1222 BT848_INT_FMTCHG
|BT848_INT_HLOCK
|
1228 static void init_bt848(struct bttv
*btv
)
1230 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1231 /* very basic init only */
1236 btwrite(0x00, BT848_CAP_CTL
);
1237 btwrite(BT848_COLOR_CTL_GAMMA
, BT848_COLOR_CTL
);
1238 btwrite(BT848_IFORM_XTAUTO
| BT848_IFORM_AUTO
, BT848_IFORM
);
1240 /* set planar and packed mode trigger points and */
1241 /* set rising edge of inverted GPINTR pin as irq trigger */
1242 btwrite(BT848_GPIO_DMA_CTL_PKTP_32
|
1243 BT848_GPIO_DMA_CTL_PLTP1_16
|
1244 BT848_GPIO_DMA_CTL_PLTP23_16
|
1245 BT848_GPIO_DMA_CTL_GPINTC
|
1246 BT848_GPIO_DMA_CTL_GPINTI
,
1247 BT848_GPIO_DMA_CTL
);
1249 btwrite(0x20, BT848_E_VSCALE_HI
);
1250 btwrite(0x20, BT848_O_VSCALE_HI
);
1252 v4l2_ctrl_handler_setup(&btv
->ctrl_handler
);
1258 static void bttv_reinit_bt848(struct bttv
*btv
)
1260 unsigned long flags
;
1263 pr_info("%d: reset, reinitialize\n", btv
->c
.nr
);
1264 spin_lock_irqsave(&btv
->s_lock
,flags
);
1266 bttv_set_dma(btv
,0);
1267 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1270 btv
->pll
.pll_current
= -1;
1271 set_input(btv
, btv
->input
, btv
->tvnorm
);
1274 static int bttv_s_ctrl(struct v4l2_ctrl
*c
)
1276 struct bttv
*btv
= container_of(c
->handler
, struct bttv
, ctrl_handler
);
1280 case V4L2_CID_BRIGHTNESS
:
1281 bt848_bright(btv
, c
->val
);
1284 bt848_hue(btv
, c
->val
);
1286 case V4L2_CID_CONTRAST
:
1287 bt848_contrast(btv
, c
->val
);
1289 case V4L2_CID_SATURATION
:
1290 bt848_sat(btv
, c
->val
);
1292 case V4L2_CID_COLOR_KILLER
:
1294 btor(BT848_SCLOOP_CKILL
, BT848_E_SCLOOP
);
1295 btor(BT848_SCLOOP_CKILL
, BT848_O_SCLOOP
);
1297 btand(~BT848_SCLOOP_CKILL
, BT848_E_SCLOOP
);
1298 btand(~BT848_SCLOOP_CKILL
, BT848_O_SCLOOP
);
1301 case V4L2_CID_AUDIO_MUTE
:
1302 audio_mute(btv
, c
->val
);
1305 case V4L2_CID_AUDIO_VOLUME
:
1306 btv
->volume_gpio(btv
, c
->val
);
1309 case V4L2_CID_CHROMA_AGC
:
1310 val
= c
->val
? BT848_SCLOOP_CAGC
: 0;
1311 btwrite(val
, BT848_E_SCLOOP
);
1312 btwrite(val
, BT848_O_SCLOOP
);
1314 case V4L2_CID_PRIVATE_COMBFILTER
:
1315 btv
->opt_combfilter
= c
->val
;
1317 case V4L2_CID_PRIVATE_LUMAFILTER
:
1319 btand(~BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1320 btand(~BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1322 btor(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1323 btor(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1326 case V4L2_CID_PRIVATE_AUTOMUTE
:
1327 btv
->opt_automute
= c
->val
;
1329 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1330 btwrite(BT848_ADC_RESERVED
|
1331 (c
->val
? BT848_ADC_CRUSH
: 0),
1334 case V4L2_CID_PRIVATE_VCR_HACK
:
1335 btv
->opt_vcr_hack
= c
->val
;
1337 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1338 btwrite(c
->val
, BT848_WC_UP
);
1340 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1341 btwrite(c
->val
, BT848_WC_DOWN
);
1343 case V4L2_CID_PRIVATE_UV_RATIO
:
1344 btv
->opt_uv_ratio
= c
->val
;
1345 bt848_sat(btv
, btv
->saturation
);
1347 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1348 btaor((c
->val
<< 7), ~BT848_OFORM_RANGE
, BT848_OFORM
);
1350 case V4L2_CID_PRIVATE_CORING
:
1351 btaor((c
->val
<< 5), ~BT848_OFORM_CORE32
, BT848_OFORM
);
1359 /* ----------------------------------------------------------------------- */
1361 static const struct v4l2_ctrl_ops bttv_ctrl_ops
= {
1362 .s_ctrl
= bttv_s_ctrl
,
1365 static struct v4l2_ctrl_config bttv_ctrl_combfilter
= {
1366 .ops
= &bttv_ctrl_ops
,
1367 .id
= V4L2_CID_PRIVATE_COMBFILTER
,
1368 .name
= "Comb Filter",
1369 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1376 static struct v4l2_ctrl_config bttv_ctrl_automute
= {
1377 .ops
= &bttv_ctrl_ops
,
1378 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
1379 .name
= "Auto Mute",
1380 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1387 static struct v4l2_ctrl_config bttv_ctrl_lumafilter
= {
1388 .ops
= &bttv_ctrl_ops
,
1389 .id
= V4L2_CID_PRIVATE_LUMAFILTER
,
1390 .name
= "Luma Decimation Filter",
1391 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1398 static struct v4l2_ctrl_config bttv_ctrl_agc_crush
= {
1399 .ops
= &bttv_ctrl_ops
,
1400 .id
= V4L2_CID_PRIVATE_AGC_CRUSH
,
1401 .name
= "AGC Crush",
1402 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1409 static struct v4l2_ctrl_config bttv_ctrl_vcr_hack
= {
1410 .ops
= &bttv_ctrl_ops
,
1411 .id
= V4L2_CID_PRIVATE_VCR_HACK
,
1413 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1420 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower
= {
1421 .ops
= &bttv_ctrl_ops
,
1422 .id
= V4L2_CID_PRIVATE_WHITECRUSH_LOWER
,
1423 .name
= "Whitecrush Lower",
1424 .type
= V4L2_CTRL_TYPE_INTEGER
,
1431 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper
= {
1432 .ops
= &bttv_ctrl_ops
,
1433 .id
= V4L2_CID_PRIVATE_WHITECRUSH_UPPER
,
1434 .name
= "Whitecrush Upper",
1435 .type
= V4L2_CTRL_TYPE_INTEGER
,
1442 static struct v4l2_ctrl_config bttv_ctrl_uv_ratio
= {
1443 .ops
= &bttv_ctrl_ops
,
1444 .id
= V4L2_CID_PRIVATE_UV_RATIO
,
1446 .type
= V4L2_CTRL_TYPE_INTEGER
,
1453 static struct v4l2_ctrl_config bttv_ctrl_full_luma
= {
1454 .ops
= &bttv_ctrl_ops
,
1455 .id
= V4L2_CID_PRIVATE_FULL_LUMA_RANGE
,
1456 .name
= "Full Luma Range",
1457 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1463 static struct v4l2_ctrl_config bttv_ctrl_coring
= {
1464 .ops
= &bttv_ctrl_ops
,
1465 .id
= V4L2_CID_PRIVATE_CORING
,
1467 .type
= V4L2_CTRL_TYPE_INTEGER
,
1474 /* ----------------------------------------------------------------------- */
1476 void bttv_gpio_tracking(struct bttv
*btv
, char *comment
)
1478 unsigned int outbits
, data
;
1479 outbits
= btread(BT848_GPIO_OUT_EN
);
1480 data
= btread(BT848_GPIO_DATA
);
1481 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1482 btv
->c
.nr
, outbits
, data
& outbits
, data
& ~outbits
, comment
);
1485 static void bttv_field_count(struct bttv
*btv
)
1493 /* start field counter */
1494 btor(BT848_INT_VSYNC
,BT848_INT_MASK
);
1496 /* stop field counter */
1497 btand(~BT848_INT_VSYNC
,BT848_INT_MASK
);
1498 btv
->field_count
= 0;
1502 static const struct bttv_format
*
1503 format_by_fourcc(int fourcc
)
1507 for (i
= 0; i
< FORMATS
; i
++) {
1508 if (-1 == formats
[i
].fourcc
)
1510 if (formats
[i
].fourcc
== fourcc
)
1516 /* ----------------------------------------------------------------------- */
1520 bttv_switch_overlay(struct bttv
*btv
, struct bttv_fh
*fh
,
1521 struct bttv_buffer
*new)
1523 struct bttv_buffer
*old
;
1524 unsigned long flags
;
1527 dprintk("switch_overlay: enter [new=%p]\n", new);
1529 new->vb
.state
= VIDEOBUF_DONE
;
1530 spin_lock_irqsave(&btv
->s_lock
,flags
);
1534 bttv_set_dma(btv
, 0x03);
1535 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1537 dprintk("switch_overlay: old=%p state is %d\n",
1538 old
, old
->vb
.state
);
1539 bttv_dma_free(&fh
->cap
,btv
, old
);
1543 free_btres_lock(btv
,fh
,RESOURCE_OVERLAY
);
1544 dprintk("switch_overlay: done\n");
1548 /* ----------------------------------------------------------------------- */
1549 /* video4linux (1) interface */
1551 static int bttv_prepare_buffer(struct videobuf_queue
*q
,struct bttv
*btv
,
1552 struct bttv_buffer
*buf
,
1553 const struct bttv_format
*fmt
,
1554 unsigned int width
, unsigned int height
,
1555 enum v4l2_field field
)
1557 struct bttv_fh
*fh
= q
->priv_data
;
1558 int redo_dma_risc
= 0;
1563 /* check settings */
1566 if (fmt
->btformat
== BT848_COLOR_FMT_RAW
) {
1568 height
= RAW_LINES
*2;
1569 if (width
*height
> buf
->vb
.bsize
)
1571 buf
->vb
.size
= buf
->vb
.bsize
;
1573 /* Make sure tvnorm and vbi_end remain consistent
1574 until we're done. */
1578 /* In this mode capturing always starts at defrect.top
1579 (default VDELAY), ignoring cropping parameters. */
1580 if (btv
->vbi_end
> bttv_tvnorms
[norm
].cropcap
.defrect
.top
) {
1584 c
.rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1587 c
= btv
->crop
[!!fh
->do_crop
];
1589 if (width
< c
.min_scaled_width
||
1590 width
> c
.max_scaled_width
||
1591 height
< c
.min_scaled_height
)
1595 case V4L2_FIELD_TOP
:
1596 case V4L2_FIELD_BOTTOM
:
1597 case V4L2_FIELD_ALTERNATE
:
1598 /* btv->crop counts frame lines. Max. scale
1599 factor is 16:1 for frames, 8:1 for fields. */
1600 if (height
* 2 > c
.max_scaled_height
)
1605 if (height
> c
.max_scaled_height
)
1610 buf
->vb
.size
= (width
* height
* fmt
->depth
) >> 3;
1611 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
1615 /* alloc + fill struct bttv_buffer (if changed) */
1616 if (buf
->vb
.width
!= width
|| buf
->vb
.height
!= height
||
1617 buf
->vb
.field
!= field
||
1618 buf
->tvnorm
!= norm
|| buf
->fmt
!= fmt
||
1619 buf
->crop
.top
!= c
.rect
.top
||
1620 buf
->crop
.left
!= c
.rect
.left
||
1621 buf
->crop
.width
!= c
.rect
.width
||
1622 buf
->crop
.height
!= c
.rect
.height
) {
1623 buf
->vb
.width
= width
;
1624 buf
->vb
.height
= height
;
1625 buf
->vb
.field
= field
;
1632 /* alloc risc memory */
1633 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1635 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,&btv
->fbuf
)))
1640 if (0 != (rc
= bttv_buffer_risc(btv
,buf
)))
1643 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1647 bttv_dma_free(q
,btv
,buf
);
1652 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1654 struct bttv_fh
*fh
= q
->priv_data
;
1656 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
1659 if (*size
* *count
> gbuffers
* gbufsize
)
1660 *count
= (gbuffers
* gbufsize
) / *size
;
1665 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
1666 enum v4l2_field field
)
1668 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1669 struct bttv_fh
*fh
= q
->priv_data
;
1671 return bttv_prepare_buffer(q
,fh
->btv
, buf
, fh
->fmt
,
1672 fh
->width
, fh
->height
, field
);
1676 buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1678 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1679 struct bttv_fh
*fh
= q
->priv_data
;
1680 struct bttv
*btv
= fh
->btv
;
1682 buf
->vb
.state
= VIDEOBUF_QUEUED
;
1683 list_add_tail(&buf
->vb
.queue
,&btv
->capture
);
1684 if (!btv
->curr
.frame_irq
) {
1686 bttv_set_dma(btv
, 0x03);
1690 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1692 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1693 struct bttv_fh
*fh
= q
->priv_data
;
1695 bttv_dma_free(q
,fh
->btv
,buf
);
1698 static struct videobuf_queue_ops bttv_video_qops
= {
1699 .buf_setup
= buffer_setup
,
1700 .buf_prepare
= buffer_prepare
,
1701 .buf_queue
= buffer_queue
,
1702 .buf_release
= buffer_release
,
1705 static void radio_enable(struct bttv
*btv
)
1707 /* Switch to the radio tuner */
1708 if (!btv
->has_radio_tuner
) {
1709 btv
->has_radio_tuner
= 1;
1710 bttv_call_all(btv
, tuner
, s_radio
);
1711 btv
->audio_input
= TVAUDIO_INPUT_RADIO
;
1712 audio_input(btv
, btv
->audio_input
);
1716 static int bttv_s_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1718 struct bttv_fh
*fh
= priv
;
1719 struct bttv
*btv
= fh
->btv
;
1723 for (i
= 0; i
< BTTV_TVNORMS
; i
++)
1724 if (*id
& bttv_tvnorms
[i
].v4l2_id
)
1726 if (i
== BTTV_TVNORMS
) {
1739 static int bttv_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1741 struct bttv_fh
*fh
= priv
;
1742 struct bttv
*btv
= fh
->btv
;
1748 static int bttv_querystd(struct file
*file
, void *f
, v4l2_std_id
*id
)
1750 struct bttv_fh
*fh
= f
;
1751 struct bttv
*btv
= fh
->btv
;
1753 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_NUML
)
1754 *id
= V4L2_STD_625_50
;
1756 *id
= V4L2_STD_525_60
;
1760 static int bttv_enum_input(struct file
*file
, void *priv
,
1761 struct v4l2_input
*i
)
1763 struct bttv_fh
*fh
= priv
;
1764 struct bttv
*btv
= fh
->btv
;
1767 if (i
->index
>= bttv_tvcards
[btv
->c
.type
].video_inputs
) {
1772 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1775 if (btv
->tuner_type
!= TUNER_ABSENT
&& i
->index
== 0) {
1776 sprintf(i
->name
, "Television");
1777 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1779 } else if (i
->index
== btv
->svhs
) {
1780 sprintf(i
->name
, "S-Video");
1782 sprintf(i
->name
, "Composite%d", i
->index
);
1785 if (i
->index
== btv
->input
) {
1786 __u32 dstatus
= btread(BT848_DSTATUS
);
1787 if (0 == (dstatus
& BT848_DSTATUS_PRES
))
1788 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1789 if (0 == (dstatus
& BT848_DSTATUS_HLOC
))
1790 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1793 i
->std
= BTTV_NORMS
;
1800 static int bttv_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1802 struct bttv_fh
*fh
= priv
;
1803 struct bttv
*btv
= fh
->btv
;
1810 static int bttv_s_input(struct file
*file
, void *priv
, unsigned int i
)
1812 struct bttv_fh
*fh
= priv
;
1813 struct bttv
*btv
= fh
->btv
;
1815 if (i
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1818 set_input(btv
, i
, btv
->tvnorm
);
1822 static int bttv_s_tuner(struct file
*file
, void *priv
,
1823 const struct v4l2_tuner
*t
)
1825 struct bttv_fh
*fh
= priv
;
1826 struct bttv
*btv
= fh
->btv
;
1831 bttv_call_all(btv
, tuner
, s_tuner
, t
);
1833 if (btv
->audio_mode_gpio
) {
1834 struct v4l2_tuner copy
= *t
;
1836 btv
->audio_mode_gpio(btv
, ©
, 1);
1841 static int bttv_g_frequency(struct file
*file
, void *priv
,
1842 struct v4l2_frequency
*f
)
1844 struct bttv_fh
*fh
= priv
;
1845 struct bttv
*btv
= fh
->btv
;
1850 if (f
->type
== V4L2_TUNER_RADIO
)
1852 f
->frequency
= f
->type
== V4L2_TUNER_RADIO
?
1853 btv
->radio_freq
: btv
->tv_freq
;
1858 static void bttv_set_frequency(struct bttv
*btv
, const struct v4l2_frequency
*f
)
1860 struct v4l2_frequency new_freq
= *f
;
1862 bttv_call_all(btv
, tuner
, s_frequency
, f
);
1863 /* s_frequency may clamp the frequency, so get the actual
1864 frequency before assigning radio/tv_freq. */
1865 bttv_call_all(btv
, tuner
, g_frequency
, &new_freq
);
1866 if (new_freq
.type
== V4L2_TUNER_RADIO
) {
1868 btv
->radio_freq
= new_freq
.frequency
;
1869 if (btv
->has_matchbox
)
1870 tea5757_set_freq(btv
, btv
->radio_freq
);
1872 btv
->tv_freq
= new_freq
.frequency
;
1876 static int bttv_s_frequency(struct file
*file
, void *priv
,
1877 const struct v4l2_frequency
*f
)
1879 struct bttv_fh
*fh
= priv
;
1880 struct bttv
*btv
= fh
->btv
;
1885 bttv_set_frequency(btv
, f
);
1889 static int bttv_log_status(struct file
*file
, void *f
)
1891 struct video_device
*vdev
= video_devdata(file
);
1892 struct bttv_fh
*fh
= f
;
1893 struct bttv
*btv
= fh
->btv
;
1895 v4l2_ctrl_handler_log_status(vdev
->ctrl_handler
, btv
->c
.v4l2_dev
.name
);
1896 bttv_call_all(btv
, core
, log_status
);
1900 static int bttv_g_chip_ident(struct file
*file
, void *f
, struct v4l2_dbg_chip_ident
*chip
)
1902 struct bttv_fh
*fh
= f
;
1903 struct bttv
*btv
= fh
->btv
;
1905 chip
->ident
= V4L2_IDENT_NONE
;
1907 if (chip
->match
.type
== V4L2_CHIP_MATCH_HOST
) {
1908 if (v4l2_chip_match_host(&chip
->match
)) {
1909 chip
->ident
= btv
->id
;
1910 if (chip
->ident
== PCI_DEVICE_ID_FUSION879
)
1911 chip
->ident
= V4L2_IDENT_BT879
;
1915 if (chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_DRIVER
&&
1916 chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
1918 /* TODO: is this correct? */
1919 return bttv_call_all_err(btv
, core
, g_chip_ident
, chip
);
1922 #ifdef CONFIG_VIDEO_ADV_DEBUG
1923 static int bttv_g_register(struct file
*file
, void *f
,
1924 struct v4l2_dbg_register
*reg
)
1926 struct bttv_fh
*fh
= f
;
1927 struct bttv
*btv
= fh
->btv
;
1929 if (!capable(CAP_SYS_ADMIN
))
1932 if (!v4l2_chip_match_host(®
->match
)) {
1933 /* TODO: subdev errors should not be ignored, this should become a
1934 subdev helper function. */
1935 bttv_call_all(btv
, core
, g_register
, reg
);
1939 /* bt848 has a 12-bit register space */
1941 reg
->val
= btread(reg
->reg
);
1947 static int bttv_s_register(struct file
*file
, void *f
,
1948 struct v4l2_dbg_register
*reg
)
1950 struct bttv_fh
*fh
= f
;
1951 struct bttv
*btv
= fh
->btv
;
1953 if (!capable(CAP_SYS_ADMIN
))
1956 if (!v4l2_chip_match_host(®
->match
)) {
1957 /* TODO: subdev errors should not be ignored, this should become a
1958 subdev helper function. */
1959 bttv_call_all(btv
, core
, s_register
, reg
);
1963 /* bt848 has a 12-bit register space */
1965 btwrite(reg
->val
, reg
->reg
);
1971 /* Given cropping boundaries b and the scaled width and height of a
1972 single field or frame, which must not exceed hardware limits, this
1973 function adjusts the cropping parameters c. */
1975 bttv_crop_adjust (struct bttv_crop
* c
,
1976 const struct v4l2_rect
* b
,
1979 enum v4l2_field field
)
1981 __s32 frame_height
= height
<< !V4L2_FIELD_HAS_BOTH(field
);
1985 if (width
< c
->min_scaled_width
) {
1986 /* Max. hor. scale factor 16:1. */
1987 c
->rect
.width
= width
* 16;
1988 } else if (width
> c
->max_scaled_width
) {
1989 /* Min. hor. scale factor 1:1. */
1990 c
->rect
.width
= width
;
1992 max_left
= b
->left
+ b
->width
- width
;
1993 max_left
= min(max_left
, (__s32
) MAX_HDELAY
);
1994 if (c
->rect
.left
> max_left
)
1995 c
->rect
.left
= max_left
;
1998 if (height
< c
->min_scaled_height
) {
1999 /* Max. vert. scale factor 16:1, single fields 8:1. */
2000 c
->rect
.height
= height
* 16;
2001 } else if (frame_height
> c
->max_scaled_height
) {
2002 /* Min. vert. scale factor 1:1.
2003 Top and height count field lines times two. */
2004 c
->rect
.height
= (frame_height
+ 1) & ~1;
2006 max_top
= b
->top
+ b
->height
- c
->rect
.height
;
2007 if (c
->rect
.top
> max_top
)
2008 c
->rect
.top
= max_top
;
2011 bttv_crop_calc_limits(c
);
2014 /* Returns an error if scaling to a frame or single field with the given
2015 width and height is not possible with the current cropping parameters
2016 and width aligned according to width_mask. If adjust_size is TRUE the
2017 function may adjust the width and/or height instead, rounding width
2018 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2019 also adjust the current cropping parameters to get closer to the
2020 desired image size. */
2022 limit_scaled_size_lock (struct bttv_fh
* fh
,
2025 enum v4l2_field field
,
2026 unsigned int width_mask
,
2027 unsigned int width_bias
,
2031 struct bttv
*btv
= fh
->btv
;
2032 const struct v4l2_rect
*b
;
2033 struct bttv_crop
*c
;
2040 BUG_ON((int) width_mask
>= 0 ||
2041 width_bias
>= (unsigned int) -width_mask
);
2043 /* Make sure tvnorm, vbi_end and the current cropping parameters
2044 remain consistent until we're done. */
2046 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2048 /* Do crop - use current, don't - use default parameters. */
2049 c
= &btv
->crop
[!!fh
->do_crop
];
2054 && !locked_btres(btv
, VIDEO_RESOURCES
)) {
2058 /* We cannot scale up. When the scaled image is larger
2059 than crop.rect we adjust the crop.rect as required
2060 by the V4L2 spec, hence cropcap.bounds are our limit. */
2061 max_width
= min(b
->width
, (__s32
) MAX_HACTIVE
);
2062 max_height
= b
->height
;
2064 /* We cannot capture the same line as video and VBI data.
2065 Note btv->vbi_end is really a minimum, see
2066 bttv_vbi_try_fmt(). */
2067 if (btv
->vbi_end
> b
->top
) {
2068 max_height
-= btv
->vbi_end
- b
->top
;
2070 if (min_height
> max_height
)
2075 if (btv
->vbi_end
> c
->rect
.top
)
2078 min_width
= c
->min_scaled_width
;
2079 min_height
= c
->min_scaled_height
;
2080 max_width
= c
->max_scaled_width
;
2081 max_height
= c
->max_scaled_height
;
2086 min_width
= (min_width
- width_mask
- 1) & width_mask
;
2087 max_width
= max_width
& width_mask
;
2089 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2090 min_height
= min_height
;
2091 /* Min. scale factor is 1:1. */
2092 max_height
>>= !V4L2_FIELD_HAS_BOTH(field
);
2095 *width
= clamp(*width
, min_width
, max_width
);
2096 *height
= clamp(*height
, min_height
, max_height
);
2098 /* Round after clamping to avoid overflow. */
2099 *width
= (*width
+ width_bias
) & width_mask
;
2102 bttv_crop_adjust(c
, b
, *width
, *height
, field
);
2104 if (btv
->vbi_end
> c
->rect
.top
) {
2105 /* Move the crop window out of the way. */
2106 c
->rect
.top
= btv
->vbi_end
;
2111 if (*width
< min_width
||
2112 *height
< min_height
||
2113 *width
> max_width
||
2114 *height
> max_height
||
2115 0 != (*width
& ~width_mask
))
2119 rc
= 0; /* success */
2126 /* Returns an error if the given overlay window dimensions are not
2127 possible with the current cropping parameters. If adjust_size is
2128 TRUE the function may adjust the window width and/or height
2129 instead, however it always rounds the horizontal position and
2130 width as btcx_align() does. If adjust_crop is TRUE the function
2131 may also adjust the current cropping parameters to get closer
2132 to the desired window size. */
2134 verify_window_lock(struct bttv_fh
*fh
, struct v4l2_window
*win
,
2135 int adjust_size
, int adjust_crop
)
2137 enum v4l2_field field
;
2138 unsigned int width_mask
;
2141 if (win
->w
.width
< 48)
2143 if (win
->w
.height
< 32)
2145 if (win
->clipcount
> 2048)
2146 win
->clipcount
= 2048;
2149 win
->global_alpha
= 0;
2153 case V4L2_FIELD_TOP
:
2154 case V4L2_FIELD_BOTTOM
:
2155 case V4L2_FIELD_INTERLACED
:
2158 field
= V4L2_FIELD_ANY
;
2161 if (V4L2_FIELD_ANY
== field
) {
2164 height2
= fh
->btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2165 field
= (win
->w
.height
> height2
)
2166 ? V4L2_FIELD_INTERLACED
2171 if (NULL
== fh
->ovfmt
)
2173 /* 4-byte alignment. */
2175 switch (fh
->ovfmt
->depth
) {
2189 win
->w
.width
-= win
->w
.left
& ~width_mask
;
2190 win
->w
.left
= (win
->w
.left
- width_mask
- 1) & width_mask
;
2192 rc
= limit_scaled_size_lock(fh
, &win
->w
.width
, &win
->w
.height
,
2194 /* width_bias: round down */ 0,
2195 adjust_size
, adjust_crop
);
2201 static int setup_window_lock(struct bttv_fh
*fh
, struct bttv
*btv
,
2202 struct v4l2_window
*win
, int fixup
)
2204 struct v4l2_clip
*clips
= NULL
;
2205 int n
,size
,retval
= 0;
2207 if (NULL
== fh
->ovfmt
)
2209 if (!(fh
->ovfmt
->flags
& FORMAT_FLAGS_PACKED
))
2211 retval
= verify_window_lock(fh
, win
,
2212 /* adjust_size */ fixup
,
2213 /* adjust_crop */ fixup
);
2217 /* copy clips -- luckily v4l1 + v4l2 are binary
2218 compatible here ...*/
2220 size
= sizeof(*clips
)*(n
+4);
2221 clips
= kmalloc(size
,GFP_KERNEL
);
2225 if (copy_from_user(clips
,win
->clips
,sizeof(struct v4l2_clip
)*n
)) {
2231 /* clip against screen */
2232 if (NULL
!= btv
->fbuf
.base
)
2233 n
= btcx_screen_clips(btv
->fbuf
.fmt
.width
, btv
->fbuf
.fmt
.height
,
2235 btcx_sort_clips(clips
,n
);
2237 /* 4-byte alignments */
2238 switch (fh
->ovfmt
->depth
) {
2241 btcx_align(&win
->w
, clips
, n
, 3);
2244 btcx_align(&win
->w
, clips
, n
, 1);
2247 /* no alignment fixups needed */
2253 kfree(fh
->ov
.clips
);
2254 fh
->ov
.clips
= clips
;
2258 fh
->ov
.field
= win
->field
;
2259 fh
->ov
.setup_ok
= 1;
2261 btv
->init
.ov
.w
.width
= win
->w
.width
;
2262 btv
->init
.ov
.w
.height
= win
->w
.height
;
2263 btv
->init
.ov
.field
= win
->field
;
2265 /* update overlay if needed */
2267 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2268 struct bttv_buffer
*new;
2270 new = videobuf_sg_alloc(sizeof(*new));
2271 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2272 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2273 retval
= bttv_switch_overlay(btv
,fh
,new);
2278 /* ----------------------------------------------------------------------- */
2280 static struct videobuf_queue
* bttv_queue(struct bttv_fh
*fh
)
2282 struct videobuf_queue
* q
= NULL
;
2285 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2288 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2297 static int bttv_resource(struct bttv_fh
*fh
)
2302 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2303 res
= RESOURCE_VIDEO_STREAM
;
2305 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2314 static int bttv_switch_type(struct bttv_fh
*fh
, enum v4l2_buf_type type
)
2316 struct videobuf_queue
*q
= bttv_queue(fh
);
2317 int res
= bttv_resource(fh
);
2319 if (check_btres(fh
,res
))
2321 if (videobuf_queue_is_busy(q
))
2328 pix_format_set_size (struct v4l2_pix_format
* f
,
2329 const struct bttv_format
* fmt
,
2331 unsigned int height
)
2336 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2337 f
->bytesperline
= width
; /* Y plane */
2338 f
->sizeimage
= (width
* height
* fmt
->depth
) >> 3;
2340 f
->bytesperline
= (width
* fmt
->depth
) >> 3;
2341 f
->sizeimage
= height
* f
->bytesperline
;
2345 static int bttv_g_fmt_vid_cap(struct file
*file
, void *priv
,
2346 struct v4l2_format
*f
)
2348 struct bttv_fh
*fh
= priv
;
2350 pix_format_set_size(&f
->fmt
.pix
, fh
->fmt
,
2351 fh
->width
, fh
->height
);
2352 f
->fmt
.pix
.field
= fh
->cap
.field
;
2353 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
2354 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2359 static int bttv_g_fmt_vid_overlay(struct file
*file
, void *priv
,
2360 struct v4l2_format
*f
)
2362 struct bttv_fh
*fh
= priv
;
2364 f
->fmt
.win
.w
= fh
->ov
.w
;
2365 f
->fmt
.win
.field
= fh
->ov
.field
;
2370 static int bttv_try_fmt_vid_cap(struct file
*file
, void *priv
,
2371 struct v4l2_format
*f
)
2373 const struct bttv_format
*fmt
;
2374 struct bttv_fh
*fh
= priv
;
2375 struct bttv
*btv
= fh
->btv
;
2376 enum v4l2_field field
;
2377 __s32 width
, height
;
2381 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2385 field
= f
->fmt
.pix
.field
;
2388 case V4L2_FIELD_TOP
:
2389 case V4L2_FIELD_BOTTOM
:
2390 case V4L2_FIELD_ALTERNATE
:
2391 case V4L2_FIELD_INTERLACED
:
2393 case V4L2_FIELD_SEQ_BT
:
2394 case V4L2_FIELD_SEQ_TB
:
2395 if (!(fmt
->flags
& FORMAT_FLAGS_PLANAR
)) {
2396 field
= V4L2_FIELD_SEQ_TB
;
2400 default: /* FIELD_ANY case */
2401 height2
= btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2402 field
= (f
->fmt
.pix
.height
> height2
)
2403 ? V4L2_FIELD_INTERLACED
2404 : V4L2_FIELD_BOTTOM
;
2408 width
= f
->fmt
.pix
.width
;
2409 height
= f
->fmt
.pix
.height
;
2411 rc
= limit_scaled_size_lock(fh
, &width
, &height
, field
,
2412 /* width_mask: 4 pixels */ ~3,
2413 /* width_bias: nearest */ 2,
2414 /* adjust_size */ 1,
2415 /* adjust_crop */ 0);
2419 /* update data for the application */
2420 f
->fmt
.pix
.field
= field
;
2421 pix_format_set_size(&f
->fmt
.pix
, fmt
, width
, height
);
2422 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
2427 static int bttv_try_fmt_vid_overlay(struct file
*file
, void *priv
,
2428 struct v4l2_format
*f
)
2430 struct bttv_fh
*fh
= priv
;
2432 verify_window_lock(fh
, &f
->fmt
.win
,
2433 /* adjust_size */ 1,
2434 /* adjust_crop */ 0);
2438 static int bttv_s_fmt_vid_cap(struct file
*file
, void *priv
,
2439 struct v4l2_format
*f
)
2442 const struct bttv_format
*fmt
;
2443 struct bttv_fh
*fh
= priv
;
2444 struct bttv
*btv
= fh
->btv
;
2445 __s32 width
, height
;
2446 enum v4l2_field field
;
2448 retval
= bttv_switch_type(fh
, f
->type
);
2452 retval
= bttv_try_fmt_vid_cap(file
, priv
, f
);
2456 width
= f
->fmt
.pix
.width
;
2457 height
= f
->fmt
.pix
.height
;
2458 field
= f
->fmt
.pix
.field
;
2460 retval
= limit_scaled_size_lock(fh
, &width
, &height
, f
->fmt
.pix
.field
,
2461 /* width_mask: 4 pixels */ ~3,
2462 /* width_bias: nearest */ 2,
2463 /* adjust_size */ 1,
2464 /* adjust_crop */ 1);
2468 f
->fmt
.pix
.field
= field
;
2470 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2472 /* update our state informations */
2474 fh
->cap
.field
= f
->fmt
.pix
.field
;
2475 fh
->cap
.last
= V4L2_FIELD_NONE
;
2476 fh
->width
= f
->fmt
.pix
.width
;
2477 fh
->height
= f
->fmt
.pix
.height
;
2478 btv
->init
.fmt
= fmt
;
2479 btv
->init
.width
= f
->fmt
.pix
.width
;
2480 btv
->init
.height
= f
->fmt
.pix
.height
;
2485 static int bttv_s_fmt_vid_overlay(struct file
*file
, void *priv
,
2486 struct v4l2_format
*f
)
2488 struct bttv_fh
*fh
= priv
;
2489 struct bttv
*btv
= fh
->btv
;
2491 if (no_overlay
> 0) {
2492 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2496 return setup_window_lock(fh
, btv
, &f
->fmt
.win
, 1);
2499 static int bttv_querycap(struct file
*file
, void *priv
,
2500 struct v4l2_capability
*cap
)
2502 struct video_device
*vdev
= video_devdata(file
);
2503 struct bttv_fh
*fh
= priv
;
2504 struct bttv
*btv
= fh
->btv
;
2509 strlcpy(cap
->driver
, "bttv", sizeof(cap
->driver
));
2510 strlcpy(cap
->card
, btv
->video_dev
->name
, sizeof(cap
->card
));
2511 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2512 "PCI:%s", pci_name(btv
->c
.pci
));
2514 V4L2_CAP_VIDEO_CAPTURE
|
2515 V4L2_CAP_READWRITE
|
2516 V4L2_CAP_STREAMING
|
2517 V4L2_CAP_DEVICE_CAPS
;
2518 if (no_overlay
<= 0)
2519 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
2521 cap
->capabilities
|= V4L2_CAP_VBI_CAPTURE
;
2523 cap
->capabilities
|= V4L2_CAP_RADIO
;
2526 * No need to lock here: those vars are initialized during board
2527 * probe and remains untouched during the rest of the driver lifecycle
2529 if (btv
->has_saa6588
)
2530 cap
->capabilities
|= V4L2_CAP_RDS_CAPTURE
;
2531 if (btv
->tuner_type
!= TUNER_ABSENT
)
2532 cap
->capabilities
|= V4L2_CAP_TUNER
;
2533 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
2534 cap
->device_caps
= cap
->capabilities
&
2535 (V4L2_CAP_VIDEO_CAPTURE
|
2536 V4L2_CAP_READWRITE
|
2537 V4L2_CAP_STREAMING
|
2538 V4L2_CAP_VIDEO_OVERLAY
|
2540 else if (vdev
->vfl_type
== VFL_TYPE_VBI
)
2541 cap
->device_caps
= cap
->capabilities
&
2542 (V4L2_CAP_VBI_CAPTURE
|
2543 V4L2_CAP_READWRITE
|
2544 V4L2_CAP_STREAMING
|
2547 cap
->device_caps
= V4L2_CAP_RADIO
| V4L2_CAP_TUNER
;
2548 if (btv
->has_saa6588
)
2549 cap
->device_caps
|= V4L2_CAP_READWRITE
|
2550 V4L2_CAP_RDS_CAPTURE
;
2555 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc
*f
)
2559 for (i
= 0; i
< FORMATS
; i
++) {
2560 if (formats
[i
].fourcc
!= -1)
2562 if ((unsigned int)index
== f
->index
)
2568 f
->pixelformat
= formats
[i
].fourcc
;
2569 strlcpy(f
->description
, formats
[i
].name
, sizeof(f
->description
));
2574 static int bttv_enum_fmt_vid_cap(struct file
*file
, void *priv
,
2575 struct v4l2_fmtdesc
*f
)
2577 int rc
= bttv_enum_fmt_cap_ovr(f
);
2585 static int bttv_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
2586 struct v4l2_fmtdesc
*f
)
2590 if (no_overlay
> 0) {
2591 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2595 rc
= bttv_enum_fmt_cap_ovr(f
);
2600 if (!(formats
[rc
].flags
& FORMAT_FLAGS_PACKED
))
2606 static int bttv_g_fbuf(struct file
*file
, void *f
,
2607 struct v4l2_framebuffer
*fb
)
2609 struct bttv_fh
*fh
= f
;
2610 struct bttv
*btv
= fh
->btv
;
2613 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2614 fb
->flags
= V4L2_FBUF_FLAG_PRIMARY
;
2616 fb
->fmt
.pixelformat
= fh
->ovfmt
->fourcc
;
2620 static int bttv_overlay(struct file
*file
, void *f
, unsigned int on
)
2622 struct bttv_fh
*fh
= f
;
2623 struct bttv
*btv
= fh
->btv
;
2624 struct bttv_buffer
*new;
2629 if (unlikely(!btv
->fbuf
.base
)) {
2632 if (unlikely(!fh
->ov
.setup_ok
)) {
2633 dprintk("%d: overlay: !setup_ok\n", btv
->c
.nr
);
2640 if (!check_alloc_btres_lock(btv
, fh
, RESOURCE_OVERLAY
))
2644 fh
->ov
.tvnorm
= btv
->tvnorm
;
2645 new = videobuf_sg_alloc(sizeof(*new));
2646 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2647 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2653 retval
= bttv_switch_overlay(btv
, fh
, new);
2657 static int bttv_s_fbuf(struct file
*file
, void *f
,
2658 const struct v4l2_framebuffer
*fb
)
2660 struct bttv_fh
*fh
= f
;
2661 struct bttv
*btv
= fh
->btv
;
2662 const struct bttv_format
*fmt
;
2665 if (!capable(CAP_SYS_ADMIN
) &&
2666 !capable(CAP_SYS_RAWIO
))
2670 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2673 if (0 == (fmt
->flags
& FORMAT_FLAGS_PACKED
))
2677 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2678 __s32 width
= fb
->fmt
.width
;
2679 __s32 height
= fb
->fmt
.height
;
2681 retval
= limit_scaled_size_lock(fh
, &width
, &height
,
2682 V4L2_FIELD_INTERLACED
,
2683 /* width_mask */ ~3,
2685 /* adjust_size */ 0,
2686 /* adjust_crop */ 0);
2692 btv
->fbuf
.base
= fb
->base
;
2693 btv
->fbuf
.fmt
.width
= fb
->fmt
.width
;
2694 btv
->fbuf
.fmt
.height
= fb
->fmt
.height
;
2695 if (0 != fb
->fmt
.bytesperline
)
2696 btv
->fbuf
.fmt
.bytesperline
= fb
->fmt
.bytesperline
;
2698 btv
->fbuf
.fmt
.bytesperline
= btv
->fbuf
.fmt
.width
*fmt
->depth
/8;
2702 btv
->init
.ovfmt
= fmt
;
2703 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2706 fh
->ov
.w
.width
= fb
->fmt
.width
;
2707 fh
->ov
.w
.height
= fb
->fmt
.height
;
2708 btv
->init
.ov
.w
.width
= fb
->fmt
.width
;
2709 btv
->init
.ov
.w
.height
= fb
->fmt
.height
;
2710 kfree(fh
->ov
.clips
);
2711 fh
->ov
.clips
= NULL
;
2714 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2715 struct bttv_buffer
*new;
2717 new = videobuf_sg_alloc(sizeof(*new));
2718 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2719 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2720 retval
= bttv_switch_overlay(btv
, fh
, new);
2726 static int bttv_reqbufs(struct file
*file
, void *priv
,
2727 struct v4l2_requestbuffers
*p
)
2729 struct bttv_fh
*fh
= priv
;
2730 return videobuf_reqbufs(bttv_queue(fh
), p
);
2733 static int bttv_querybuf(struct file
*file
, void *priv
,
2734 struct v4l2_buffer
*b
)
2736 struct bttv_fh
*fh
= priv
;
2737 return videobuf_querybuf(bttv_queue(fh
), b
);
2740 static int bttv_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2742 struct bttv_fh
*fh
= priv
;
2743 struct bttv
*btv
= fh
->btv
;
2744 int res
= bttv_resource(fh
);
2746 if (!check_alloc_btres_lock(btv
, fh
, res
))
2749 return videobuf_qbuf(bttv_queue(fh
), b
);
2752 static int bttv_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2754 struct bttv_fh
*fh
= priv
;
2755 return videobuf_dqbuf(bttv_queue(fh
), b
,
2756 file
->f_flags
& O_NONBLOCK
);
2759 static int bttv_streamon(struct file
*file
, void *priv
,
2760 enum v4l2_buf_type type
)
2762 struct bttv_fh
*fh
= priv
;
2763 struct bttv
*btv
= fh
->btv
;
2764 int res
= bttv_resource(fh
);
2766 if (!check_alloc_btres_lock(btv
, fh
, res
))
2768 return videobuf_streamon(bttv_queue(fh
));
2772 static int bttv_streamoff(struct file
*file
, void *priv
,
2773 enum v4l2_buf_type type
)
2775 struct bttv_fh
*fh
= priv
;
2776 struct bttv
*btv
= fh
->btv
;
2778 int res
= bttv_resource(fh
);
2781 retval
= videobuf_streamoff(bttv_queue(fh
));
2784 free_btres_lock(btv
, fh
, res
);
2788 static int bttv_g_parm(struct file
*file
, void *f
,
2789 struct v4l2_streamparm
*parm
)
2791 struct bttv_fh
*fh
= f
;
2792 struct bttv
*btv
= fh
->btv
;
2794 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2796 parm
->parm
.capture
.readbuffers
= gbuffers
;
2797 v4l2_video_std_frame_period(bttv_tvnorms
[btv
->tvnorm
].v4l2_id
,
2798 &parm
->parm
.capture
.timeperframe
);
2803 static int bttv_g_tuner(struct file
*file
, void *priv
,
2804 struct v4l2_tuner
*t
)
2806 struct bttv_fh
*fh
= priv
;
2807 struct bttv
*btv
= fh
->btv
;
2812 t
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
2813 t
->capability
= V4L2_TUNER_CAP_NORM
;
2814 bttv_call_all(btv
, tuner
, g_tuner
, t
);
2815 strcpy(t
->name
, "Television");
2816 t
->type
= V4L2_TUNER_ANALOG_TV
;
2817 if (btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
)
2820 if (btv
->audio_mode_gpio
)
2821 btv
->audio_mode_gpio(btv
, t
, 0);
2826 static int bttv_cropcap(struct file
*file
, void *priv
,
2827 struct v4l2_cropcap
*cap
)
2829 struct bttv_fh
*fh
= priv
;
2830 struct bttv
*btv
= fh
->btv
;
2832 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2833 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2836 *cap
= bttv_tvnorms
[btv
->tvnorm
].cropcap
;
2841 static int bttv_g_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
2843 struct bttv_fh
*fh
= f
;
2844 struct bttv
*btv
= fh
->btv
;
2846 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2847 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2850 /* No fh->do_crop = 1; because btv->crop[1] may be
2851 inconsistent with fh->width or fh->height and apps
2852 do not expect a change here. */
2854 crop
->c
= btv
->crop
[!!fh
->do_crop
].rect
;
2859 static int bttv_s_crop(struct file
*file
, void *f
, const struct v4l2_crop
*crop
)
2861 struct bttv_fh
*fh
= f
;
2862 struct bttv
*btv
= fh
->btv
;
2863 const struct v4l2_rect
*b
;
2871 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2872 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2875 /* Make sure tvnorm, vbi_end and the current cropping
2876 parameters remain consistent until we're done. Note
2877 read() may change vbi_end in check_alloc_btres_lock(). */
2880 if (locked_btres(fh
->btv
, VIDEO_RESOURCES
)) {
2884 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2887 b_right
= b_left
+ b
->width
;
2888 b_bottom
= b
->top
+ b
->height
;
2890 b_top
= max(b
->top
, btv
->vbi_end
);
2891 if (b_top
+ 32 >= b_bottom
) {
2895 /* Min. scaled size 48 x 32. */
2896 c
.rect
.left
= clamp_t(s32
, crop
->c
.left
, b_left
, b_right
- 48);
2897 c
.rect
.left
= min(c
.rect
.left
, (__s32
) MAX_HDELAY
);
2899 c
.rect
.width
= clamp_t(s32
, crop
->c
.width
,
2900 48, b_right
- c
.rect
.left
);
2902 c
.rect
.top
= clamp_t(s32
, crop
->c
.top
, b_top
, b_bottom
- 32);
2903 /* Top and height must be a multiple of two. */
2904 c
.rect
.top
= (c
.rect
.top
+ 1) & ~1;
2906 c
.rect
.height
= clamp_t(s32
, crop
->c
.height
,
2907 32, b_bottom
- c
.rect
.top
);
2908 c
.rect
.height
= (c
.rect
.height
+ 1) & ~1;
2910 bttv_crop_calc_limits(&c
);
2916 if (fh
->width
< c
.min_scaled_width
) {
2917 fh
->width
= c
.min_scaled_width
;
2918 btv
->init
.width
= c
.min_scaled_width
;
2919 } else if (fh
->width
> c
.max_scaled_width
) {
2920 fh
->width
= c
.max_scaled_width
;
2921 btv
->init
.width
= c
.max_scaled_width
;
2924 if (fh
->height
< c
.min_scaled_height
) {
2925 fh
->height
= c
.min_scaled_height
;
2926 btv
->init
.height
= c
.min_scaled_height
;
2927 } else if (fh
->height
> c
.max_scaled_height
) {
2928 fh
->height
= c
.max_scaled_height
;
2929 btv
->init
.height
= c
.max_scaled_height
;
2935 static ssize_t
bttv_read(struct file
*file
, char __user
*data
,
2936 size_t count
, loff_t
*ppos
)
2938 struct bttv_fh
*fh
= file
->private_data
;
2941 if (fh
->btv
->errors
)
2942 bttv_reinit_bt848(fh
->btv
);
2943 dprintk("%d: read count=%d type=%s\n",
2944 fh
->btv
->c
.nr
, (int)count
, v4l2_type_names
[fh
->type
]);
2947 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2948 if (!check_alloc_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
)) {
2949 /* VIDEO_READ in use by another fh,
2950 or VIDEO_STREAM by any fh. */
2953 retval
= videobuf_read_one(&fh
->cap
, data
, count
, ppos
,
2954 file
->f_flags
& O_NONBLOCK
);
2955 free_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
);
2957 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2958 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
2960 retval
= videobuf_read_stream(&fh
->vbi
, data
, count
, ppos
, 1,
2961 file
->f_flags
& O_NONBLOCK
);
2969 static unsigned int bttv_poll(struct file
*file
, poll_table
*wait
)
2971 struct bttv_fh
*fh
= file
->private_data
;
2972 struct bttv_buffer
*buf
;
2973 enum v4l2_field field
;
2974 unsigned int rc
= 0;
2975 unsigned long req_events
= poll_requested_events(wait
);
2977 if (v4l2_event_pending(&fh
->fh
))
2979 else if (req_events
& POLLPRI
)
2980 poll_wait(file
, &fh
->fh
.wait
, wait
);
2982 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
2985 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
2986 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
2987 return rc
| POLLERR
;
2988 return rc
| videobuf_poll_stream(file
, &fh
->vbi
, wait
);
2991 if (check_btres(fh
,RESOURCE_VIDEO_STREAM
)) {
2992 /* streaming capture */
2993 if (list_empty(&fh
->cap
.stream
))
2994 return rc
| POLLERR
;
2995 buf
= list_entry(fh
->cap
.stream
.next
,struct bttv_buffer
,vb
.stream
);
2997 /* read() capture */
2998 if (NULL
== fh
->cap
.read_buf
) {
2999 /* need to capture a new frame */
3000 if (locked_btres(fh
->btv
,RESOURCE_VIDEO_STREAM
))
3001 return rc
| POLLERR
;
3002 fh
->cap
.read_buf
= videobuf_sg_alloc(fh
->cap
.msize
);
3003 if (NULL
== fh
->cap
.read_buf
)
3004 return rc
| POLLERR
;
3005 fh
->cap
.read_buf
->memory
= V4L2_MEMORY_USERPTR
;
3006 field
= videobuf_next_field(&fh
->cap
);
3007 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,field
)) {
3008 kfree (fh
->cap
.read_buf
);
3009 fh
->cap
.read_buf
= NULL
;
3010 return rc
| POLLERR
;
3012 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
3013 fh
->cap
.read_off
= 0;
3015 buf
= (struct bttv_buffer
*)fh
->cap
.read_buf
;
3018 poll_wait(file
, &buf
->vb
.done
, wait
);
3019 if (buf
->vb
.state
== VIDEOBUF_DONE
||
3020 buf
->vb
.state
== VIDEOBUF_ERROR
)
3021 rc
= rc
| POLLIN
|POLLRDNORM
;
3025 static int bttv_open(struct file
*file
)
3027 struct video_device
*vdev
= video_devdata(file
);
3028 struct bttv
*btv
= video_drvdata(file
);
3030 enum v4l2_buf_type type
= 0;
3032 dprintk("open dev=%s\n", video_device_node_name(vdev
));
3034 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
) {
3035 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
3036 } else if (vdev
->vfl_type
== VFL_TYPE_VBI
) {
3037 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
3043 dprintk("%d: open called (type=%s)\n",
3044 btv
->c
.nr
, v4l2_type_names
[type
]);
3046 /* allocate per filehandle data */
3047 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3051 file
->private_data
= fh
;
3054 v4l2_fh_init(&fh
->fh
, vdev
);
3057 fh
->ov
.setup_ok
= 0;
3059 videobuf_queue_sg_init(&fh
->cap
, &bttv_video_qops
,
3060 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3061 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
3062 V4L2_FIELD_INTERLACED
,
3063 sizeof(struct bttv_buffer
),
3065 videobuf_queue_sg_init(&fh
->vbi
, &bttv_vbi_qops
,
3066 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3067 V4L2_BUF_TYPE_VBI_CAPTURE
,
3069 sizeof(struct bttv_buffer
),
3071 set_tvnorm(btv
,btv
->tvnorm
);
3072 set_input(btv
, btv
->input
, btv
->tvnorm
);
3073 audio_mute(btv
, btv
->mute
);
3075 /* The V4L2 spec requires one global set of cropping parameters
3076 which only change on request. These are stored in btv->crop[1].
3077 However for compatibility with V4L apps and cropping unaware
3078 V4L2 apps we now reset the cropping parameters as seen through
3079 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3080 will use btv->crop[0], the default cropping parameters for the
3081 current video standard, and VIDIOC_S_FMT will not implicitely
3082 change the cropping parameters until VIDIOC_S_CROP has been
3084 fh
->do_crop
= !reset_crop
; /* module parameter */
3086 /* Likewise there should be one global set of VBI capture
3087 parameters, but for compatibility with V4L apps and earlier
3088 driver versions each fh has its own parameters. */
3089 bttv_vbi_fmt_reset(&fh
->vbi_fmt
, btv
->tvnorm
);
3091 bttv_field_count(btv
);
3092 v4l2_fh_add(&fh
->fh
);
3096 static int bttv_release(struct file
*file
)
3098 struct bttv_fh
*fh
= file
->private_data
;
3099 struct bttv
*btv
= fh
->btv
;
3101 /* turn off overlay */
3102 if (check_btres(fh
, RESOURCE_OVERLAY
))
3103 bttv_switch_overlay(btv
,fh
,NULL
);
3105 /* stop video capture */
3106 if (check_btres(fh
, RESOURCE_VIDEO_STREAM
)) {
3107 videobuf_streamoff(&fh
->cap
);
3108 free_btres_lock(btv
,fh
,RESOURCE_VIDEO_STREAM
);
3110 if (fh
->cap
.read_buf
) {
3111 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
3112 kfree(fh
->cap
.read_buf
);
3114 if (check_btres(fh
, RESOURCE_VIDEO_READ
)) {
3115 free_btres_lock(btv
, fh
, RESOURCE_VIDEO_READ
);
3118 /* stop vbi capture */
3119 if (check_btres(fh
, RESOURCE_VBI
)) {
3120 videobuf_stop(&fh
->vbi
);
3121 free_btres_lock(btv
,fh
,RESOURCE_VBI
);
3126 videobuf_mmap_free(&fh
->cap
);
3127 videobuf_mmap_free(&fh
->vbi
);
3128 file
->private_data
= NULL
;
3131 bttv_field_count(btv
);
3134 audio_mute(btv
, btv
->mute
);
3136 v4l2_fh_del(&fh
->fh
);
3137 v4l2_fh_exit(&fh
->fh
);
3143 bttv_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3145 struct bttv_fh
*fh
= file
->private_data
;
3147 dprintk("%d: mmap type=%s 0x%lx+%ld\n",
3148 fh
->btv
->c
.nr
, v4l2_type_names
[fh
->type
],
3149 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
3150 return videobuf_mmap_mapper(bttv_queue(fh
),vma
);
3153 static const struct v4l2_file_operations bttv_fops
=
3155 .owner
= THIS_MODULE
,
3157 .release
= bttv_release
,
3158 .unlocked_ioctl
= video_ioctl2
,
3164 static const struct v4l2_ioctl_ops bttv_ioctl_ops
= {
3165 .vidioc_querycap
= bttv_querycap
,
3166 .vidioc_enum_fmt_vid_cap
= bttv_enum_fmt_vid_cap
,
3167 .vidioc_g_fmt_vid_cap
= bttv_g_fmt_vid_cap
,
3168 .vidioc_try_fmt_vid_cap
= bttv_try_fmt_vid_cap
,
3169 .vidioc_s_fmt_vid_cap
= bttv_s_fmt_vid_cap
,
3170 .vidioc_enum_fmt_vid_overlay
= bttv_enum_fmt_vid_overlay
,
3171 .vidioc_g_fmt_vid_overlay
= bttv_g_fmt_vid_overlay
,
3172 .vidioc_try_fmt_vid_overlay
= bttv_try_fmt_vid_overlay
,
3173 .vidioc_s_fmt_vid_overlay
= bttv_s_fmt_vid_overlay
,
3174 .vidioc_g_fmt_vbi_cap
= bttv_g_fmt_vbi_cap
,
3175 .vidioc_try_fmt_vbi_cap
= bttv_try_fmt_vbi_cap
,
3176 .vidioc_s_fmt_vbi_cap
= bttv_s_fmt_vbi_cap
,
3177 .vidioc_cropcap
= bttv_cropcap
,
3178 .vidioc_reqbufs
= bttv_reqbufs
,
3179 .vidioc_querybuf
= bttv_querybuf
,
3180 .vidioc_qbuf
= bttv_qbuf
,
3181 .vidioc_dqbuf
= bttv_dqbuf
,
3182 .vidioc_s_std
= bttv_s_std
,
3183 .vidioc_g_std
= bttv_g_std
,
3184 .vidioc_enum_input
= bttv_enum_input
,
3185 .vidioc_g_input
= bttv_g_input
,
3186 .vidioc_s_input
= bttv_s_input
,
3187 .vidioc_streamon
= bttv_streamon
,
3188 .vidioc_streamoff
= bttv_streamoff
,
3189 .vidioc_g_tuner
= bttv_g_tuner
,
3190 .vidioc_s_tuner
= bttv_s_tuner
,
3191 .vidioc_g_crop
= bttv_g_crop
,
3192 .vidioc_s_crop
= bttv_s_crop
,
3193 .vidioc_g_fbuf
= bttv_g_fbuf
,
3194 .vidioc_s_fbuf
= bttv_s_fbuf
,
3195 .vidioc_overlay
= bttv_overlay
,
3196 .vidioc_g_parm
= bttv_g_parm
,
3197 .vidioc_g_frequency
= bttv_g_frequency
,
3198 .vidioc_s_frequency
= bttv_s_frequency
,
3199 .vidioc_log_status
= bttv_log_status
,
3200 .vidioc_querystd
= bttv_querystd
,
3201 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
3202 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
3203 .vidioc_g_chip_ident
= bttv_g_chip_ident
,
3204 #ifdef CONFIG_VIDEO_ADV_DEBUG
3205 .vidioc_g_register
= bttv_g_register
,
3206 .vidioc_s_register
= bttv_s_register
,
3210 static struct video_device bttv_video_template
= {
3212 .ioctl_ops
= &bttv_ioctl_ops
,
3213 .tvnorms
= BTTV_NORMS
,
3216 /* ----------------------------------------------------------------------- */
3217 /* radio interface */
3219 static int radio_open(struct file
*file
)
3221 struct video_device
*vdev
= video_devdata(file
);
3222 struct bttv
*btv
= video_drvdata(file
);
3225 dprintk("open dev=%s\n", video_device_node_name(vdev
));
3227 dprintk("%d: open called (radio)\n", btv
->c
.nr
);
3229 /* allocate per filehandle data */
3230 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3233 file
->private_data
= fh
;
3235 v4l2_fh_init(&fh
->fh
, vdev
);
3238 audio_mute(btv
, btv
->mute
);
3240 v4l2_fh_add(&fh
->fh
);
3245 static int radio_release(struct file
*file
)
3247 struct bttv_fh
*fh
= file
->private_data
;
3248 struct bttv
*btv
= fh
->btv
;
3249 struct saa6588_command cmd
;
3251 file
->private_data
= NULL
;
3252 v4l2_fh_del(&fh
->fh
);
3253 v4l2_fh_exit(&fh
->fh
);
3258 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_CLOSE
, &cmd
);
3260 if (btv
->radio_user
== 0)
3261 btv
->has_radio_tuner
= 0;
3265 static int radio_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
3267 struct bttv_fh
*fh
= priv
;
3268 struct bttv
*btv
= fh
->btv
;
3272 strcpy(t
->name
, "Radio");
3273 t
->type
= V4L2_TUNER_RADIO
;
3276 bttv_call_all(btv
, tuner
, g_tuner
, t
);
3278 if (btv
->audio_mode_gpio
)
3279 btv
->audio_mode_gpio(btv
, t
, 0);
3284 static int radio_s_tuner(struct file
*file
, void *priv
,
3285 const struct v4l2_tuner
*t
)
3287 struct bttv_fh
*fh
= priv
;
3288 struct bttv
*btv
= fh
->btv
;
3294 bttv_call_all(btv
, tuner
, s_tuner
, t
);
3298 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
3299 size_t count
, loff_t
*ppos
)
3301 struct bttv_fh
*fh
= file
->private_data
;
3302 struct bttv
*btv
= fh
->btv
;
3303 struct saa6588_command cmd
;
3304 cmd
.block_count
= count
/3;
3306 cmd
.instance
= file
;
3307 cmd
.result
= -ENODEV
;
3310 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_READ
, &cmd
);
3315 static unsigned int radio_poll(struct file
*file
, poll_table
*wait
)
3317 struct bttv_fh
*fh
= file
->private_data
;
3318 struct bttv
*btv
= fh
->btv
;
3319 unsigned long req_events
= poll_requested_events(wait
);
3320 struct saa6588_command cmd
;
3321 unsigned int res
= 0;
3323 if (v4l2_event_pending(&fh
->fh
))
3325 else if (req_events
& POLLPRI
)
3326 poll_wait(file
, &fh
->fh
.wait
, wait
);
3328 cmd
.instance
= file
;
3329 cmd
.event_list
= wait
;
3331 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_POLL
, &cmd
);
3336 static const struct v4l2_file_operations radio_fops
=
3338 .owner
= THIS_MODULE
,
3341 .release
= radio_release
,
3342 .unlocked_ioctl
= video_ioctl2
,
3346 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
3347 .vidioc_querycap
= bttv_querycap
,
3348 .vidioc_log_status
= bttv_log_status
,
3349 .vidioc_g_tuner
= radio_g_tuner
,
3350 .vidioc_s_tuner
= radio_s_tuner
,
3351 .vidioc_g_frequency
= bttv_g_frequency
,
3352 .vidioc_s_frequency
= bttv_s_frequency
,
3353 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
3354 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
3357 static struct video_device radio_template
= {
3358 .fops
= &radio_fops
,
3359 .ioctl_ops
= &radio_ioctl_ops
,
3362 /* ----------------------------------------------------------------------- */
3363 /* some debug code */
3365 static int bttv_risc_decode(u32 risc
)
3367 static char *instr
[16] = {
3368 [ BT848_RISC_WRITE
>> 28 ] = "write",
3369 [ BT848_RISC_SKIP
>> 28 ] = "skip",
3370 [ BT848_RISC_WRITEC
>> 28 ] = "writec",
3371 [ BT848_RISC_JUMP
>> 28 ] = "jump",
3372 [ BT848_RISC_SYNC
>> 28 ] = "sync",
3373 [ BT848_RISC_WRITE123
>> 28 ] = "write123",
3374 [ BT848_RISC_SKIP123
>> 28 ] = "skip123",
3375 [ BT848_RISC_WRITE1S23
>> 28 ] = "write1s23",
3377 static int incr
[16] = {
3378 [ BT848_RISC_WRITE
>> 28 ] = 2,
3379 [ BT848_RISC_JUMP
>> 28 ] = 2,
3380 [ BT848_RISC_SYNC
>> 28 ] = 2,
3381 [ BT848_RISC_WRITE123
>> 28 ] = 5,
3382 [ BT848_RISC_SKIP123
>> 28 ] = 2,
3383 [ BT848_RISC_WRITE1S23
>> 28 ] = 3,
3385 static char *bits
[] = {
3386 "be0", "be1", "be2", "be3/resync",
3387 "set0", "set1", "set2", "set3",
3388 "clr0", "clr1", "clr2", "clr3",
3389 "irq", "res", "eol", "sol",
3393 pr_cont("0x%08x [ %s", risc
,
3394 instr
[risc
>> 28] ? instr
[risc
>> 28] : "INVALID");
3395 for (i
= ARRAY_SIZE(bits
)-1; i
>= 0; i
--)
3396 if (risc
& (1 << (i
+ 12)))
3397 pr_cont(" %s", bits
[i
]);
3398 pr_cont(" count=%d ]\n", risc
& 0xfff);
3399 return incr
[risc
>> 28] ? incr
[risc
>> 28] : 1;
3402 static void bttv_risc_disasm(struct bttv
*btv
,
3403 struct btcx_riscmem
*risc
)
3407 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
3408 btv
->c
.v4l2_dev
.name
, risc
->cpu
, (unsigned long)risc
->dma
);
3409 for (i
= 0; i
< (risc
->size
>> 2); i
+= n
) {
3410 pr_info("%s: 0x%lx: ",
3411 btv
->c
.v4l2_dev
.name
,
3412 (unsigned long)(risc
->dma
+ (i
<<2)));
3413 n
= bttv_risc_decode(le32_to_cpu(risc
->cpu
[i
]));
3414 for (j
= 1; j
< n
; j
++)
3415 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3416 btv
->c
.v4l2_dev
.name
,
3417 (unsigned long)(risc
->dma
+ ((i
+j
)<<2)),
3419 if (0 == risc
->cpu
[i
])
3424 static void bttv_print_riscaddr(struct bttv
*btv
)
3426 pr_info(" main: %08llx\n", (unsigned long long)btv
->main
.dma
);
3427 pr_info(" vbi : o=%08llx e=%08llx\n",
3428 btv
->cvbi
? (unsigned long long)btv
->cvbi
->top
.dma
: 0,
3429 btv
->cvbi
? (unsigned long long)btv
->cvbi
->bottom
.dma
: 0);
3430 pr_info(" cap : o=%08llx e=%08llx\n",
3432 ? (unsigned long long)btv
->curr
.top
->top
.dma
: 0,
3434 ? (unsigned long long)btv
->curr
.bottom
->bottom
.dma
: 0);
3435 pr_info(" scr : o=%08llx e=%08llx\n",
3436 btv
->screen
? (unsigned long long)btv
->screen
->top
.dma
: 0,
3437 btv
->screen
? (unsigned long long)btv
->screen
->bottom
.dma
: 0);
3438 bttv_risc_disasm(btv
, &btv
->main
);
3441 /* ----------------------------------------------------------------------- */
3444 static char *irq_name
[] = {
3445 "FMTCHG", // format change detected (525 vs. 625)
3446 "VSYNC", // vertical sync (new field)
3447 "HSYNC", // horizontal sync
3448 "OFLOW", // chroma/luma AGC overflow
3449 "HLOCK", // horizontal lock changed
3450 "VPRES", // video presence changed
3452 "I2CDONE", // hw irc operation finished
3453 "GPINT", // gpio port triggered irq
3455 "RISCI", // risc instruction triggered irq
3456 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3457 "FTRGT", // pixel data fifo overrun
3458 "FDSR", // fifo data stream resyncronisation
3459 "PPERR", // parity error (data transfer)
3460 "RIPERR", // parity error (read risc instructions)
3461 "PABORT", // pci abort
3462 "OCERR", // risc instruction error
3463 "SCERR", // syncronisation error
3466 static void bttv_print_irqbits(u32 print
, u32 mark
)
3471 for (i
= 0; i
< ARRAY_SIZE(irq_name
); i
++) {
3472 if (print
& (1 << i
))
3473 pr_cont(" %s", irq_name
[i
]);
3474 if (mark
& (1 << i
))
3479 static void bttv_irq_debug_low_latency(struct bttv
*btv
, u32 rc
)
3481 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3483 (unsigned long)btv
->main
.dma
,
3484 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_VBI
+1]),
3485 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_FIELD
+1]),
3488 if (0 == (btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
)) {
3489 pr_notice("%d: Oh, there (temporarily?) is no input signal. "
3490 "Ok, then this is harmless, don't worry ;)\n",
3494 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
3496 pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
3502 bttv_irq_next_video(struct bttv
*btv
, struct bttv_buffer_set
*set
)
3504 struct bttv_buffer
*item
;
3506 memset(set
,0,sizeof(*set
));
3508 /* capture request ? */
3509 if (!list_empty(&btv
->capture
)) {
3511 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3512 if (V4L2_FIELD_HAS_TOP(item
->vb
.field
))
3514 if (V4L2_FIELD_HAS_BOTTOM(item
->vb
.field
))
3517 /* capture request for other field ? */
3518 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
) &&
3519 (item
->vb
.queue
.next
!= &btv
->capture
)) {
3520 item
= list_entry(item
->vb
.queue
.next
, struct bttv_buffer
, vb
.queue
);
3521 /* Mike Isely <isely@pobox.com> - Only check
3522 * and set up the bottom field in the logic
3523 * below. Don't ever do the top field. This
3524 * of course means that if we set up the
3525 * bottom field in the above code that we'll
3526 * actually skip a field. But that's OK.
3527 * Having processed only a single buffer this
3528 * time, then the next time around the first
3529 * available buffer should be for a top field.
3530 * That will then cause us here to set up a
3531 * top then a bottom field in the normal way.
3532 * The alternative to this understanding is
3533 * that we set up the second available buffer
3534 * as a top field, but that's out of order
3535 * since this driver always processes the top
3536 * field first - the effect will be the two
3537 * buffers being returned in the wrong order,
3538 * with the second buffer also being delayed
3539 * by one field time (owing to the fifo nature
3540 * of videobuf). Worse still, we'll be stuck
3541 * doing fields out of order now every time
3542 * until something else causes a field to be
3543 * dropped. By effectively forcing a field to
3544 * drop this way then we always get back into
3545 * sync within a single frame time. (Out of
3546 * order fields can screw up deinterlacing
3548 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
)) {
3549 if (NULL
== set
->bottom
&&
3550 V4L2_FIELD_BOTTOM
== item
->vb
.field
) {
3553 if (NULL
!= set
->top
&& NULL
!= set
->bottom
)
3559 /* screen overlay ? */
3560 if (NULL
!= btv
->screen
) {
3561 if (V4L2_FIELD_HAS_BOTH(btv
->screen
->vb
.field
)) {
3562 if (NULL
== set
->top
&& NULL
== set
->bottom
) {
3563 set
->top
= btv
->screen
;
3564 set
->bottom
= btv
->screen
;
3567 if (V4L2_FIELD_TOP
== btv
->screen
->vb
.field
&&
3569 set
->top
= btv
->screen
;
3571 if (V4L2_FIELD_BOTTOM
== btv
->screen
->vb
.field
&&
3572 NULL
== set
->bottom
) {
3573 set
->bottom
= btv
->screen
;
3578 dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3579 btv
->c
.nr
, set
->top
, set
->bottom
,
3580 btv
->screen
, set
->frame_irq
, set
->top_irq
);
3585 bttv_irq_wakeup_video(struct bttv
*btv
, struct bttv_buffer_set
*wakeup
,
3586 struct bttv_buffer_set
*curr
, unsigned int state
)
3590 v4l2_get_timestamp(&ts
);
3592 if (wakeup
->top
== wakeup
->bottom
) {
3593 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3595 pr_debug("%d: wakeup: both=%p\n",
3596 btv
->c
.nr
, wakeup
->top
);
3597 wakeup
->top
->vb
.ts
= ts
;
3598 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3599 wakeup
->top
->vb
.state
= state
;
3600 wake_up(&wakeup
->top
->vb
.done
);
3603 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3605 pr_debug("%d: wakeup: top=%p\n",
3606 btv
->c
.nr
, wakeup
->top
);
3607 wakeup
->top
->vb
.ts
= ts
;
3608 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3609 wakeup
->top
->vb
.state
= state
;
3610 wake_up(&wakeup
->top
->vb
.done
);
3612 if (NULL
!= wakeup
->bottom
&& curr
->bottom
!= wakeup
->bottom
) {
3614 pr_debug("%d: wakeup: bottom=%p\n",
3615 btv
->c
.nr
, wakeup
->bottom
);
3616 wakeup
->bottom
->vb
.ts
= ts
;
3617 wakeup
->bottom
->vb
.field_count
= btv
->field_count
;
3618 wakeup
->bottom
->vb
.state
= state
;
3619 wake_up(&wakeup
->bottom
->vb
.done
);
3625 bttv_irq_wakeup_vbi(struct bttv
*btv
, struct bttv_buffer
*wakeup
,
3633 v4l2_get_timestamp(&ts
);
3635 wakeup
->vb
.field_count
= btv
->field_count
;
3636 wakeup
->vb
.state
= state
;
3637 wake_up(&wakeup
->vb
.done
);
3640 static void bttv_irq_timeout(unsigned long data
)
3642 struct bttv
*btv
= (struct bttv
*)data
;
3643 struct bttv_buffer_set old
,new;
3644 struct bttv_buffer
*ovbi
;
3645 struct bttv_buffer
*item
;
3646 unsigned long flags
;
3649 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3650 btv
->c
.nr
, btv
->framedrop
, btv
->irq_me
, btv
->irq_total
,
3651 btread(BT848_RISC_COUNT
));
3652 bttv_print_irqbits(btread(BT848_INT_STAT
),0);
3656 spin_lock_irqsave(&btv
->s_lock
,flags
);
3658 /* deactivate stuff */
3659 memset(&new,0,sizeof(new));
3665 bttv_buffer_activate_video(btv
, &new);
3666 bttv_buffer_activate_vbi(btv
, NULL
);
3667 bttv_set_dma(btv
, 0);
3670 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_ERROR
);
3671 bttv_irq_wakeup_vbi(btv
, ovbi
, VIDEOBUF_ERROR
);
3673 /* cancel all outstanding capture / vbi requests */
3674 while (!list_empty(&btv
->capture
)) {
3675 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3676 list_del(&item
->vb
.queue
);
3677 item
->vb
.state
= VIDEOBUF_ERROR
;
3678 wake_up(&item
->vb
.done
);
3680 while (!list_empty(&btv
->vcapture
)) {
3681 item
= list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3682 list_del(&item
->vb
.queue
);
3683 item
->vb
.state
= VIDEOBUF_ERROR
;
3684 wake_up(&item
->vb
.done
);
3688 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
3692 bttv_irq_wakeup_top(struct bttv
*btv
)
3694 struct bttv_buffer
*wakeup
= btv
->curr
.top
;
3699 spin_lock(&btv
->s_lock
);
3700 btv
->curr
.top_irq
= 0;
3701 btv
->curr
.top
= NULL
;
3702 bttv_risc_hook(btv
, RISC_SLOT_O_FIELD
, NULL
, 0);
3704 v4l2_get_timestamp(&wakeup
->vb
.ts
);
3705 wakeup
->vb
.field_count
= btv
->field_count
;
3706 wakeup
->vb
.state
= VIDEOBUF_DONE
;
3707 wake_up(&wakeup
->vb
.done
);
3708 spin_unlock(&btv
->s_lock
);
3711 static inline int is_active(struct btcx_riscmem
*risc
, u32 rc
)
3715 if (rc
> risc
->dma
+ risc
->size
)
3721 bttv_irq_switch_video(struct bttv
*btv
)
3723 struct bttv_buffer_set
new;
3724 struct bttv_buffer_set old
;
3727 spin_lock(&btv
->s_lock
);
3729 /* new buffer set */
3730 bttv_irq_next_video(btv
, &new);
3731 rc
= btread(BT848_RISC_COUNT
);
3732 if ((btv
->curr
.top
&& is_active(&btv
->curr
.top
->top
, rc
)) ||
3733 (btv
->curr
.bottom
&& is_active(&btv
->curr
.bottom
->bottom
, rc
))) {
3736 bttv_irq_debug_low_latency(btv
, rc
);
3737 spin_unlock(&btv
->s_lock
);
3744 btv
->loop_irq
&= ~1;
3745 bttv_buffer_activate_video(btv
, &new);
3746 bttv_set_dma(btv
, 0);
3749 if (UNSET
!= btv
->new_input
) {
3750 video_mux(btv
,btv
->new_input
);
3751 btv
->new_input
= UNSET
;
3754 /* wake up finished buffers */
3755 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_DONE
);
3756 spin_unlock(&btv
->s_lock
);
3760 bttv_irq_switch_vbi(struct bttv
*btv
)
3762 struct bttv_buffer
*new = NULL
;
3763 struct bttv_buffer
*old
;
3766 spin_lock(&btv
->s_lock
);
3768 if (!list_empty(&btv
->vcapture
))
3769 new = list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3772 rc
= btread(BT848_RISC_COUNT
);
3773 if (NULL
!= old
&& (is_active(&old
->top
, rc
) ||
3774 is_active(&old
->bottom
, rc
))) {
3777 bttv_irq_debug_low_latency(btv
, rc
);
3778 spin_unlock(&btv
->s_lock
);
3784 btv
->loop_irq
&= ~4;
3785 bttv_buffer_activate_vbi(btv
, new);
3786 bttv_set_dma(btv
, 0);
3788 bttv_irq_wakeup_vbi(btv
, old
, VIDEOBUF_DONE
);
3789 spin_unlock(&btv
->s_lock
);
3792 static irqreturn_t
bttv_irq(int irq
, void *dev_id
)
3800 btv
=(struct bttv
*)dev_id
;
3804 /* get/clear interrupt status bits */
3805 stat
=btread(BT848_INT_STAT
);
3806 astat
=stat
&btread(BT848_INT_MASK
);
3810 btwrite(stat
,BT848_INT_STAT
);
3812 /* get device status bits */
3813 dstat
=btread(BT848_DSTATUS
);
3816 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
3817 btv
->c
.nr
, count
, btv
->field_count
,
3818 stat
>>28, btread(BT848_RISC_COUNT
));
3819 bttv_print_irqbits(stat
,astat
);
3820 if (stat
& BT848_INT_HLOCK
)
3821 pr_cont(" HLOC => %s",
3822 dstat
& BT848_DSTATUS_HLOC
3824 if (stat
& BT848_INT_VPRES
)
3825 pr_cont(" PRES => %s",
3826 dstat
& BT848_DSTATUS_PRES
3828 if (stat
& BT848_INT_FMTCHG
)
3829 pr_cont(" NUML => %s",
3830 dstat
& BT848_DSTATUS_NUML
3835 if (astat
&BT848_INT_VSYNC
)
3838 if ((astat
& BT848_INT_GPINT
) && btv
->remote
) {
3839 bttv_input_irq(btv
);
3842 if (astat
& BT848_INT_I2CDONE
) {
3843 btv
->i2c_done
= stat
;
3844 wake_up(&btv
->i2c_queue
);
3847 if ((astat
& BT848_INT_RISCI
) && (stat
& (4<<28)))
3848 bttv_irq_switch_vbi(btv
);
3850 if ((astat
& BT848_INT_RISCI
) && (stat
& (2<<28)))
3851 bttv_irq_wakeup_top(btv
);
3853 if ((astat
& BT848_INT_RISCI
) && (stat
& (1<<28)))
3854 bttv_irq_switch_video(btv
);
3856 if ((astat
& BT848_INT_HLOCK
) && btv
->opt_automute
)
3857 /* trigger automute */
3858 audio_mux_gpio(btv
, btv
->audio_input
, btv
->mute
);
3860 if (astat
& (BT848_INT_SCERR
|BT848_INT_OCERR
)) {
3861 pr_info("%d: %s%s @ %08x,",
3863 (astat
& BT848_INT_SCERR
) ? "SCERR" : "",
3864 (astat
& BT848_INT_OCERR
) ? "OCERR" : "",
3865 btread(BT848_RISC_COUNT
));
3866 bttv_print_irqbits(stat
,astat
);
3869 bttv_print_riscaddr(btv
);
3871 if (fdsr
&& astat
& BT848_INT_FDSR
) {
3872 pr_info("%d: FDSR @ %08x\n",
3873 btv
->c
.nr
, btread(BT848_RISC_COUNT
));
3875 bttv_print_riscaddr(btv
);
3881 if (count
> 8 || !(astat
& BT848_INT_GPINT
)) {
3882 btwrite(0, BT848_INT_MASK
);
3884 pr_err("%d: IRQ lockup, cleared int mask [",
3887 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3890 btwrite(btread(BT848_INT_MASK
) & (-1 ^ BT848_INT_GPINT
),
3894 bttv_print_irqbits(stat
,astat
);
3902 return IRQ_RETVAL(handled
);
3906 /* ----------------------------------------------------------------------- */
3907 /* initialization */
3909 static struct video_device
*vdev_init(struct bttv
*btv
,
3910 const struct video_device
*template,
3911 const char *type_name
)
3913 struct video_device
*vfd
;
3915 vfd
= video_device_alloc();
3919 vfd
->v4l2_dev
= &btv
->c
.v4l2_dev
;
3920 vfd
->release
= video_device_release
;
3921 vfd
->debug
= bttv_debug
;
3922 set_bit(V4L2_FL_USE_FH_PRIO
, &vfd
->flags
);
3923 video_set_drvdata(vfd
, btv
);
3924 snprintf(vfd
->name
, sizeof(vfd
->name
), "BT%d%s %s (%s)",
3925 btv
->id
, (btv
->id
==848 && btv
->revision
==0x12) ? "A" : "",
3926 type_name
, bttv_tvcards
[btv
->c
.type
].name
);
3927 if (btv
->tuner_type
== TUNER_ABSENT
) {
3928 v4l2_disable_ioctl(vfd
, VIDIOC_G_FREQUENCY
);
3929 v4l2_disable_ioctl(vfd
, VIDIOC_S_FREQUENCY
);
3930 v4l2_disable_ioctl(vfd
, VIDIOC_G_TUNER
);
3931 v4l2_disable_ioctl(vfd
, VIDIOC_S_TUNER
);
3936 static void bttv_unregister_video(struct bttv
*btv
)
3938 if (btv
->video_dev
) {
3939 if (video_is_registered(btv
->video_dev
))
3940 video_unregister_device(btv
->video_dev
);
3942 video_device_release(btv
->video_dev
);
3943 btv
->video_dev
= NULL
;
3946 if (video_is_registered(btv
->vbi_dev
))
3947 video_unregister_device(btv
->vbi_dev
);
3949 video_device_release(btv
->vbi_dev
);
3950 btv
->vbi_dev
= NULL
;
3952 if (btv
->radio_dev
) {
3953 if (video_is_registered(btv
->radio_dev
))
3954 video_unregister_device(btv
->radio_dev
);
3956 video_device_release(btv
->radio_dev
);
3957 btv
->radio_dev
= NULL
;
3961 /* register video4linux devices */
3962 static int bttv_register_video(struct bttv
*btv
)
3965 pr_notice("Overlay support disabled\n");
3968 btv
->video_dev
= vdev_init(btv
, &bttv_video_template
, "video");
3970 if (NULL
== btv
->video_dev
)
3972 if (video_register_device(btv
->video_dev
, VFL_TYPE_GRABBER
,
3973 video_nr
[btv
->c
.nr
]) < 0)
3975 pr_info("%d: registered device %s\n",
3976 btv
->c
.nr
, video_device_node_name(btv
->video_dev
));
3977 if (device_create_file(&btv
->video_dev
->dev
,
3978 &dev_attr_card
)<0) {
3979 pr_err("%d: device_create_file 'card' failed\n", btv
->c
.nr
);
3984 btv
->vbi_dev
= vdev_init(btv
, &bttv_video_template
, "vbi");
3986 if (NULL
== btv
->vbi_dev
)
3988 if (video_register_device(btv
->vbi_dev
, VFL_TYPE_VBI
,
3989 vbi_nr
[btv
->c
.nr
]) < 0)
3991 pr_info("%d: registered device %s\n",
3992 btv
->c
.nr
, video_device_node_name(btv
->vbi_dev
));
3994 if (!btv
->has_radio
)
3997 btv
->radio_dev
= vdev_init(btv
, &radio_template
, "radio");
3998 if (NULL
== btv
->radio_dev
)
4000 btv
->radio_dev
->ctrl_handler
= &btv
->radio_ctrl_handler
;
4001 if (video_register_device(btv
->radio_dev
, VFL_TYPE_RADIO
,
4002 radio_nr
[btv
->c
.nr
]) < 0)
4004 pr_info("%d: registered device %s\n",
4005 btv
->c
.nr
, video_device_node_name(btv
->radio_dev
));
4011 bttv_unregister_video(btv
);
4016 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4017 /* response on cards with no firmware is not enabled by OF */
4018 static void pci_set_command(struct pci_dev
*dev
)
4020 #if defined(__powerpc__)
4023 pci_read_config_dword(dev
, PCI_COMMAND
, &cmd
);
4024 cmd
= (cmd
| PCI_COMMAND_MEMORY
);
4025 pci_write_config_dword(dev
, PCI_COMMAND
, cmd
);
4029 static int bttv_probe(struct pci_dev
*dev
, const struct pci_device_id
*pci_id
)
4031 struct v4l2_frequency init_freq
= {
4033 .type
= V4L2_TUNER_ANALOG_TV
,
4039 struct v4l2_ctrl_handler
*hdl
;
4041 if (bttv_num
== BTTV_MAX
)
4043 pr_info("Bt8xx card found (%d)\n", bttv_num
);
4044 bttvs
[bttv_num
] = btv
= kzalloc(sizeof(*btv
), GFP_KERNEL
);
4046 pr_err("out of memory\n");
4049 btv
->c
.nr
= bttv_num
;
4050 snprintf(btv
->c
.v4l2_dev
.name
, sizeof(btv
->c
.v4l2_dev
.name
),
4051 "bttv%d", btv
->c
.nr
);
4053 /* initialize structs / fill in defaults */
4054 mutex_init(&btv
->lock
);
4055 spin_lock_init(&btv
->s_lock
);
4056 spin_lock_init(&btv
->gpio_lock
);
4057 init_waitqueue_head(&btv
->i2c_queue
);
4058 INIT_LIST_HEAD(&btv
->c
.subs
);
4059 INIT_LIST_HEAD(&btv
->capture
);
4060 INIT_LIST_HEAD(&btv
->vcapture
);
4062 init_timer(&btv
->timeout
);
4063 btv
->timeout
.function
= bttv_irq_timeout
;
4064 btv
->timeout
.data
= (unsigned long)btv
;
4067 btv
->tuner_type
= UNSET
;
4068 btv
->new_input
= UNSET
;
4069 btv
->has_radio
=radio
[btv
->c
.nr
];
4071 /* pci stuff (init, get irq/mmio, ... */
4073 btv
->id
= dev
->device
;
4074 if (pci_enable_device(dev
)) {
4075 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4078 if (pci_set_dma_mask(dev
, DMA_BIT_MASK(32))) {
4079 pr_warn("%d: No suitable DMA available\n", btv
->c
.nr
);
4082 if (!request_mem_region(pci_resource_start(dev
,0),
4083 pci_resource_len(dev
,0),
4084 btv
->c
.v4l2_dev
.name
)) {
4085 pr_warn("%d: can't request iomem (0x%llx)\n",
4087 (unsigned long long)pci_resource_start(dev
, 0));
4090 pci_set_master(dev
);
4091 pci_set_command(dev
);
4093 result
= v4l2_device_register(&dev
->dev
, &btv
->c
.v4l2_dev
);
4095 pr_warn("%d: v4l2_device_register() failed\n", btv
->c
.nr
);
4098 hdl
= &btv
->ctrl_handler
;
4099 v4l2_ctrl_handler_init(hdl
, 20);
4100 btv
->c
.v4l2_dev
.ctrl_handler
= hdl
;
4101 v4l2_ctrl_handler_init(&btv
->radio_ctrl_handler
, 6);
4103 btv
->revision
= dev
->revision
;
4104 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
4105 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
4106 bttv_num
, btv
->id
, btv
->revision
, pci_name(dev
),
4107 btv
->c
.pci
->irq
, lat
,
4108 (unsigned long long)pci_resource_start(dev
, 0));
4111 btv
->bt848_mmio
= ioremap(pci_resource_start(dev
, 0), 0x1000);
4112 if (NULL
== btv
->bt848_mmio
) {
4113 pr_err("%d: ioremap() failed\n", btv
->c
.nr
);
4121 /* disable irqs, register irq handler */
4122 btwrite(0, BT848_INT_MASK
);
4123 result
= request_irq(btv
->c
.pci
->irq
, bttv_irq
,
4124 IRQF_SHARED
| IRQF_DISABLED
, btv
->c
.v4l2_dev
.name
, (void *)btv
);
4126 pr_err("%d: can't get IRQ %d\n",
4127 bttv_num
, btv
->c
.pci
->irq
);
4131 if (0 != bttv_handle_chipset(btv
)) {
4136 /* init options from insmod args */
4137 btv
->opt_combfilter
= combfilter
;
4138 bttv_ctrl_combfilter
.def
= combfilter
;
4139 bttv_ctrl_lumafilter
.def
= lumafilter
;
4140 btv
->opt_automute
= automute
;
4141 bttv_ctrl_automute
.def
= automute
;
4142 bttv_ctrl_agc_crush
.def
= agc_crush
;
4143 btv
->opt_vcr_hack
= vcr_hack
;
4144 bttv_ctrl_vcr_hack
.def
= vcr_hack
;
4145 bttv_ctrl_whitecrush_upper
.def
= whitecrush_upper
;
4146 bttv_ctrl_whitecrush_lower
.def
= whitecrush_lower
;
4147 btv
->opt_uv_ratio
= uv_ratio
;
4148 bttv_ctrl_uv_ratio
.def
= uv_ratio
;
4149 bttv_ctrl_full_luma
.def
= full_luma_range
;
4150 bttv_ctrl_coring
.def
= coring
;
4152 /* fill struct bttv with some useful defaults */
4153 btv
->init
.btv
= btv
;
4154 btv
->init
.ov
.w
.width
= 320;
4155 btv
->init
.ov
.w
.height
= 240;
4156 btv
->init
.fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
4157 btv
->init
.width
= 320;
4158 btv
->init
.height
= 240;
4159 btv
->init
.ov
.w
.width
= 320;
4160 btv
->init
.ov
.w
.height
= 240;
4161 btv
->init
.ov
.field
= V4L2_FIELD_INTERLACED
;
4164 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4165 V4L2_CID_BRIGHTNESS
, 0, 0xff00, 0x100, 32768);
4166 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4167 V4L2_CID_CONTRAST
, 0, 0xff80, 0x80, 0x6c00);
4168 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4169 V4L2_CID_SATURATION
, 0, 0xff80, 0x80, 32768);
4170 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4171 V4L2_CID_COLOR_KILLER
, 0, 1, 1, 0);
4172 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4173 V4L2_CID_HUE
, 0, 0xff00, 0x100, 32768);
4174 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4175 V4L2_CID_CHROMA_AGC
, 0, 1, 1, !!chroma_agc
);
4176 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4177 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
4178 if (btv
->volume_gpio
)
4179 v4l2_ctrl_new_std(hdl
, &bttv_ctrl_ops
,
4180 V4L2_CID_AUDIO_VOLUME
, 0, 0xff00, 0x100, 0xff00);
4181 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_combfilter
, NULL
);
4182 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_automute
, NULL
);
4183 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_lumafilter
, NULL
);
4184 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_agc_crush
, NULL
);
4185 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_vcr_hack
, NULL
);
4186 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_whitecrush_lower
, NULL
);
4187 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_whitecrush_upper
, NULL
);
4188 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_uv_ratio
, NULL
);
4189 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_full_luma
, NULL
);
4190 v4l2_ctrl_new_custom(hdl
, &bttv_ctrl_coring
, NULL
);
4192 /* initialize hardware */
4194 bttv_gpio_tracking(btv
,"pre-init");
4196 bttv_risc_init_main(btv
);
4200 btwrite(0x00, BT848_GPIO_REG_INP
);
4201 btwrite(0x00, BT848_GPIO_OUT_EN
);
4203 bttv_gpio_tracking(btv
,"init");
4205 /* needs to be done before i2c is registered */
4206 bttv_init_card1(btv
);
4208 /* register i2c + gpio */
4211 /* some card-specific stuff (needs working i2c) */
4212 bttv_init_card2(btv
);
4213 bttv_init_tuner(btv
);
4214 if (btv
->tuner_type
!= TUNER_ABSENT
) {
4215 bttv_set_frequency(btv
, &init_freq
);
4216 btv
->radio_freq
= 90500 * 16; /* 90.5Mhz default */
4218 btv
->std
= V4L2_STD_PAL
;
4220 v4l2_ctrl_handler_setup(hdl
);
4222 result
= hdl
->error
;
4228 /* register video4linux + input */
4229 if (!bttv_tvcards
[btv
->c
.type
].no_video
) {
4230 v4l2_ctrl_add_handler(&btv
->radio_ctrl_handler
, hdl
,
4231 v4l2_ctrl_radio_filter
);
4232 if (btv
->radio_ctrl_handler
.error
) {
4233 result
= btv
->radio_ctrl_handler
.error
;
4236 set_input(btv
, 0, btv
->tvnorm
);
4237 bttv_crop_reset(&btv
->crop
[0], btv
->tvnorm
);
4238 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
4239 disclaim_vbi_lines(btv
);
4240 disclaim_video_lines(btv
);
4241 bttv_register_video(btv
);
4244 /* add subdevices and autoload dvb-bt8xx if needed */
4245 if (bttv_tvcards
[btv
->c
.type
].has_dvb
) {
4246 bttv_sub_add_device(&btv
->c
, "dvb");
4247 request_modules(btv
);
4251 init_bttv_i2c_ir(btv
);
4252 bttv_input_init(btv
);
4255 /* everything is fine */
4260 free_irq(btv
->c
.pci
->irq
,btv
);
4263 v4l2_ctrl_handler_free(&btv
->ctrl_handler
);
4264 v4l2_ctrl_handler_free(&btv
->radio_ctrl_handler
);
4265 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4268 if (btv
->bt848_mmio
)
4269 iounmap(btv
->bt848_mmio
);
4270 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4271 pci_resource_len(btv
->c
.pci
,0));
4275 static void bttv_remove(struct pci_dev
*pci_dev
)
4277 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4278 struct bttv
*btv
= to_bttv(v4l2_dev
);
4281 pr_info("%d: unloading\n", btv
->c
.nr
);
4283 if (bttv_tvcards
[btv
->c
.type
].has_dvb
)
4284 flush_request_modules(btv
);
4286 /* shutdown everything (DMA+IRQs) */
4287 btand(~15, BT848_GPIO_DMA_CTL
);
4288 btwrite(0, BT848_INT_MASK
);
4289 btwrite(~0x0, BT848_INT_STAT
);
4290 btwrite(0x0, BT848_GPIO_OUT_EN
);
4292 bttv_gpio_tracking(btv
,"cleanup");
4294 /* tell gpio modules we are leaving ... */
4296 bttv_input_fini(btv
);
4297 bttv_sub_del_devices(&btv
->c
);
4299 /* unregister i2c_bus + input */
4302 /* unregister video4linux */
4303 bttv_unregister_video(btv
);
4305 /* free allocated memory */
4306 v4l2_ctrl_handler_free(&btv
->ctrl_handler
);
4307 v4l2_ctrl_handler_free(&btv
->radio_ctrl_handler
);
4308 btcx_riscmem_free(btv
->c
.pci
,&btv
->main
);
4310 /* free resources */
4311 free_irq(btv
->c
.pci
->irq
,btv
);
4312 iounmap(btv
->bt848_mmio
);
4313 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4314 pci_resource_len(btv
->c
.pci
,0));
4316 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4317 bttvs
[btv
->c
.nr
] = NULL
;
4324 static int bttv_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
4326 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4327 struct bttv
*btv
= to_bttv(v4l2_dev
);
4328 struct bttv_buffer_set idle
;
4329 unsigned long flags
;
4331 dprintk("%d: suspend %d\n", btv
->c
.nr
, state
.event
);
4333 /* stop dma + irqs */
4334 spin_lock_irqsave(&btv
->s_lock
,flags
);
4335 memset(&idle
, 0, sizeof(idle
));
4336 btv
->state
.video
= btv
->curr
;
4337 btv
->state
.vbi
= btv
->cvbi
;
4338 btv
->state
.loop_irq
= btv
->loop_irq
;
4341 bttv_buffer_activate_video(btv
, &idle
);
4342 bttv_buffer_activate_vbi(btv
, NULL
);
4343 bttv_set_dma(btv
, 0);
4344 btwrite(0, BT848_INT_MASK
);
4345 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4347 /* save bt878 state */
4348 btv
->state
.gpio_enable
= btread(BT848_GPIO_OUT_EN
);
4349 btv
->state
.gpio_data
= gpio_read();
4351 /* save pci state */
4352 pci_save_state(pci_dev
);
4353 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
4354 pci_disable_device(pci_dev
);
4355 btv
->state
.disabled
= 1;
4360 static int bttv_resume(struct pci_dev
*pci_dev
)
4362 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4363 struct bttv
*btv
= to_bttv(v4l2_dev
);
4364 unsigned long flags
;
4367 dprintk("%d: resume\n", btv
->c
.nr
);
4369 /* restore pci state */
4370 if (btv
->state
.disabled
) {
4371 err
=pci_enable_device(pci_dev
);
4373 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4376 btv
->state
.disabled
= 0;
4378 err
=pci_set_power_state(pci_dev
, PCI_D0
);
4380 pci_disable_device(pci_dev
);
4381 pr_warn("%d: Can't enable device\n", btv
->c
.nr
);
4382 btv
->state
.disabled
= 1;
4386 pci_restore_state(pci_dev
);
4388 /* restore bt878 state */
4389 bttv_reinit_bt848(btv
);
4390 gpio_inout(0xffffff, btv
->state
.gpio_enable
);
4391 gpio_write(btv
->state
.gpio_data
);
4394 spin_lock_irqsave(&btv
->s_lock
,flags
);
4395 btv
->curr
= btv
->state
.video
;
4396 btv
->cvbi
= btv
->state
.vbi
;
4397 btv
->loop_irq
= btv
->state
.loop_irq
;
4398 bttv_buffer_activate_video(btv
, &btv
->curr
);
4399 bttv_buffer_activate_vbi(btv
, btv
->cvbi
);
4400 bttv_set_dma(btv
, 0);
4401 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4406 static struct pci_device_id bttv_pci_tbl
[] = {
4407 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT848
), 0},
4408 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT849
), 0},
4409 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT878
), 0},
4410 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT879
), 0},
4411 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_FUSION879
), 0},
4415 MODULE_DEVICE_TABLE(pci
, bttv_pci_tbl
);
4417 static struct pci_driver bttv_pci_driver
= {
4419 .id_table
= bttv_pci_tbl
,
4420 .probe
= bttv_probe
,
4421 .remove
= bttv_remove
,
4423 .suspend
= bttv_suspend
,
4424 .resume
= bttv_resume
,
4428 static int __init
bttv_init_module(void)
4434 pr_info("driver version %s loaded\n", BTTV_VERSION
);
4435 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
4437 if (gbufsize
> BTTV_MAX_FBUF
)
4438 gbufsize
= BTTV_MAX_FBUF
;
4439 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
4441 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
4442 gbuffers
, gbufsize
>> 10, gbufsize
>> PAGE_SHIFT
);
4444 bttv_check_chipset();
4446 ret
= bus_register(&bttv_sub_bus_type
);
4448 pr_warn("bus_register error: %d\n", ret
);
4451 ret
= pci_register_driver(&bttv_pci_driver
);
4453 bus_unregister(&bttv_sub_bus_type
);
4458 static void __exit
bttv_cleanup_module(void)
4460 pci_unregister_driver(&bttv_pci_driver
);
4461 bus_unregister(&bttv_sub_bus_type
);
4464 module_init(bttv_init_module
);
4465 module_exit(bttv_cleanup_module
);