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 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/slab.h>
41 #include <linux/errno.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
45 #include <linux/interrupt.h>
46 #include <linux/kdev_t.h>
48 #include <media/v4l2-common.h>
49 #include <media/v4l2-ioctl.h>
50 #include <media/tvaudio.h>
51 #include <media/msp3400.h>
53 #include <linux/dma-mapping.h>
56 #include <asm/byteorder.h>
58 #include <media/saa6588.h>
61 unsigned int bttv_num
; /* number of Bt848s in use */
62 struct bttv
*bttvs
[BTTV_MAX
];
64 unsigned int bttv_debug
;
65 unsigned int bttv_verbose
= 1;
66 unsigned int bttv_gpio
;
68 /* config variables */
70 static unsigned int bigendian
=1;
72 static unsigned int bigendian
;
74 static unsigned int radio
[BTTV_MAX
];
75 static unsigned int irq_debug
;
76 static unsigned int gbuffers
= 8;
77 static unsigned int gbufsize
= 0x208000;
78 static unsigned int reset_crop
= 1;
80 static int video_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
81 static int radio_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
82 static int vbi_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
83 static int debug_latency
;
84 static int disable_ir
;
86 static unsigned int fdsr
;
89 static unsigned int combfilter
;
90 static unsigned int lumafilter
;
91 static unsigned int automute
= 1;
92 static unsigned int chroma_agc
;
93 static unsigned int adc_crush
= 1;
94 static unsigned int whitecrush_upper
= 0xCF;
95 static unsigned int whitecrush_lower
= 0x7F;
96 static unsigned int vcr_hack
;
97 static unsigned int irq_iswitch
;
98 static unsigned int uv_ratio
= 50;
99 static unsigned int full_luma_range
;
100 static unsigned int coring
;
102 /* API features (turn on/off stuff for testing) */
103 static unsigned int v4l2
= 1;
106 module_param(bttv_verbose
, int, 0644);
107 module_param(bttv_gpio
, int, 0644);
108 module_param(bttv_debug
, int, 0644);
109 module_param(irq_debug
, int, 0644);
110 module_param(debug_latency
, int, 0644);
111 module_param(disable_ir
, int, 0444);
113 module_param(fdsr
, int, 0444);
114 module_param(gbuffers
, int, 0444);
115 module_param(gbufsize
, int, 0444);
116 module_param(reset_crop
, int, 0444);
118 module_param(v4l2
, int, 0644);
119 module_param(bigendian
, int, 0644);
120 module_param(irq_iswitch
, int, 0644);
121 module_param(combfilter
, int, 0444);
122 module_param(lumafilter
, int, 0444);
123 module_param(automute
, int, 0444);
124 module_param(chroma_agc
, int, 0444);
125 module_param(adc_crush
, int, 0444);
126 module_param(whitecrush_upper
, int, 0444);
127 module_param(whitecrush_lower
, int, 0444);
128 module_param(vcr_hack
, int, 0444);
129 module_param(uv_ratio
, int, 0444);
130 module_param(full_luma_range
, int, 0444);
131 module_param(coring
, int, 0444);
133 module_param_array(radio
, int, NULL
, 0444);
134 module_param_array(video_nr
, int, NULL
, 0444);
135 module_param_array(radio_nr
, int, NULL
, 0444);
136 module_param_array(vbi_nr
, int, NULL
, 0444);
138 MODULE_PARM_DESC(radio
,"The TV card supports radio, default is 0 (no)");
139 MODULE_PARM_DESC(bigendian
,"byte order of the framebuffer, default is native endian");
140 MODULE_PARM_DESC(bttv_verbose
,"verbose startup messages, default is 1 (yes)");
141 MODULE_PARM_DESC(bttv_gpio
,"log gpio changes, default is 0 (no)");
142 MODULE_PARM_DESC(bttv_debug
,"debug messages, default is 0 (no)");
143 MODULE_PARM_DESC(irq_debug
,"irq handler debug messages, default is 0 (no)");
144 MODULE_PARM_DESC(disable_ir
, "disable infrared remote support");
145 MODULE_PARM_DESC(gbuffers
,"number of capture buffers. range 2-32, default 8");
146 MODULE_PARM_DESC(gbufsize
,"size of the capture buffers, default is 0x208000");
147 MODULE_PARM_DESC(reset_crop
,"reset cropping parameters at open(), default "
148 "is 1 (yes) for compatibility with older applications");
149 MODULE_PARM_DESC(automute
,"mute audio on bad/missing video signal, default is 1 (yes)");
150 MODULE_PARM_DESC(chroma_agc
,"enables the AGC of chroma signal, default is 0 (no)");
151 MODULE_PARM_DESC(adc_crush
,"enables the luminance ADC crush, default is 1 (yes)");
152 MODULE_PARM_DESC(whitecrush_upper
,"sets the white crush upper value, default is 207");
153 MODULE_PARM_DESC(whitecrush_lower
,"sets the white crush lower value, default is 127");
154 MODULE_PARM_DESC(vcr_hack
,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
155 MODULE_PARM_DESC(irq_iswitch
,"switch inputs in irq handler");
156 MODULE_PARM_DESC(uv_ratio
,"ratio between u and v gains, default is 50");
157 MODULE_PARM_DESC(full_luma_range
,"use the full luma range, default is 0 (no)");
158 MODULE_PARM_DESC(coring
,"set the luma coring level, default is 0 (no)");
159 MODULE_PARM_DESC(video_nr
, "video device numbers");
160 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
161 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
163 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
164 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
165 MODULE_LICENSE("GPL");
167 /* ----------------------------------------------------------------------- */
170 static ssize_t
show_card(struct device
*cd
,
171 struct device_attribute
*attr
, char *buf
)
173 struct video_device
*vfd
= container_of(cd
, struct video_device
, dev
);
174 struct bttv
*btv
= video_get_drvdata(vfd
);
175 return sprintf(buf
, "%d\n", btv
? btv
->c
.type
: UNSET
);
177 static DEVICE_ATTR(card
, S_IRUGO
, show_card
, NULL
);
179 /* ----------------------------------------------------------------------- */
180 /* dvb auto-load setup */
181 #if defined(CONFIG_MODULES) && defined(MODULE)
182 static void request_module_async(struct work_struct
*work
)
184 request_module("dvb-bt8xx");
187 static void request_modules(struct bttv
*dev
)
189 INIT_WORK(&dev
->request_module_wk
, request_module_async
);
190 schedule_work(&dev
->request_module_wk
);
193 static void flush_request_modules(struct bttv
*dev
)
195 flush_work_sync(&dev
->request_module_wk
);
198 #define request_modules(dev)
199 #define flush_request_modules(dev)
200 #endif /* CONFIG_MODULES */
203 /* ----------------------------------------------------------------------- */
206 /* special timing tables from conexant... */
207 static u8 SRAM_Table
[][60] =
209 /* PAL digital input over GPIO[7:0] */
211 45, // 45 bytes following
212 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
213 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
214 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
215 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
216 0x37,0x00,0xAF,0x21,0x00
218 /* NTSC digital input over GPIO[7:0] */
220 51, // 51 bytes following
221 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
222 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
223 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
224 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
225 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
228 // TGB_NTSC392 // quartzsight
229 // This table has been modified to be used for Fusion Rev D
231 0x2A, // size of table = 42
232 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
233 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
234 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
235 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
240 /* minhdelayx1 first video pixel we can capture on a line and
241 hdelayx1 start of active video, both relative to rising edge of
242 /HRESET pulse (0H) in 1 / fCLKx1.
243 swidth width of active video and
244 totalwidth total line width, both in 1 / fCLKx1.
245 sqwidth total line width in square pixels.
246 vdelay start of active video in 2 * field lines relative to
247 trailing edge of /VRESET pulse (VDELAY register).
248 sheight height of active video in 2 * field lines.
249 videostart0 ITU-R frame line number of the line corresponding
250 to vdelay in the first field. */
251 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
252 vdelay, sheight, videostart0) \
253 .cropcap.bounds.left = minhdelayx1, \
254 /* * 2 because vertically we count field lines times two, */ \
255 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
256 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
257 /* 4 is a safety margin at the end of the line. */ \
258 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
259 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
260 .cropcap.defrect.left = hdelayx1, \
261 .cropcap.defrect.top = (videostart0) * 2, \
262 .cropcap.defrect.width = swidth, \
263 .cropcap.defrect.height = sheight, \
264 .cropcap.pixelaspect.numerator = totalwidth, \
265 .cropcap.pixelaspect.denominator = sqwidth,
267 const struct bttv_tvnorm bttv_tvnorms
[] = {
269 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
270 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
272 .v4l2_id
= V4L2_STD_PAL
,
280 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
281 .scaledtwidth
= 1135,
285 .vbipack
= 255, /* min (2048 / 4, 0x1ff) & 0xff */
287 /* ITU-R frame line number of the first VBI line
288 we can capture, of the first and second field.
289 The last line is determined by cropcap.bounds. */
290 .vbistart
= { 7, 320 },
291 CROPCAP(/* minhdelayx1 */ 68,
293 /* Should be (768 * 1135 + 944 / 2) / 944.
294 cropcap.defrect is used for image width
295 checks, so we keep the old value 924. */
297 /* totalwidth */ 1135,
301 /* videostart0 */ 23)
302 /* bt878 (and bt848?) can capture another
303 line below active video. */
304 .cropcap
.bounds
.height
= (576 + 2) + 0x20 - 2,
306 .v4l2_id
= V4L2_STD_NTSC_M
| V4L2_STD_NTSC_M_KR
,
314 .iform
= (BT848_IFORM_NTSC
|BT848_IFORM_XT0
),
319 .vbipack
= 144, /* min (1600 / 4, 0x1ff) & 0xff */
321 .vbistart
= { 10, 273 },
322 CROPCAP(/* minhdelayx1 */ 68,
324 /* Should be (640 * 910 + 780 / 2) / 780? */
326 /* totalwidth */ 910,
330 /* videostart0 */ 23)
332 .v4l2_id
= V4L2_STD_SECAM
,
340 .iform
= (BT848_IFORM_SECAM
|BT848_IFORM_XT1
),
341 .scaledtwidth
= 1135,
346 .sram
= 0, /* like PAL, correct? */
347 .vbistart
= { 7, 320 },
348 CROPCAP(/* minhdelayx1 */ 68,
351 /* totalwidth */ 1135,
355 /* videostart0 */ 23)
357 .v4l2_id
= V4L2_STD_PAL_Nc
,
365 .iform
= (BT848_IFORM_PAL_NC
|BT848_IFORM_XT0
),
372 .vbistart
= { 7, 320 },
373 CROPCAP(/* minhdelayx1 */ 68,
375 /* swidth */ (640 * 910 + 780 / 2) / 780,
376 /* totalwidth */ 910,
380 /* videostart0 */ 23)
382 .v4l2_id
= V4L2_STD_PAL_M
,
390 .iform
= (BT848_IFORM_PAL_M
|BT848_IFORM_XT0
),
397 .vbistart
= { 10, 273 },
398 CROPCAP(/* minhdelayx1 */ 68,
400 /* swidth */ (640 * 910 + 780 / 2) / 780,
401 /* totalwidth */ 910,
405 /* videostart0 */ 23)
407 .v4l2_id
= V4L2_STD_PAL_N
,
415 .iform
= (BT848_IFORM_PAL_N
|BT848_IFORM_XT1
),
422 .vbistart
= { 7, 320 },
423 CROPCAP(/* minhdelayx1 */ 68,
425 /* swidth */ (768 * 1135 + 944 / 2) / 944,
426 /* totalwidth */ 1135,
430 /* videostart0 */ 23)
432 .v4l2_id
= V4L2_STD_NTSC_M_JP
,
440 .iform
= (BT848_IFORM_NTSC_J
|BT848_IFORM_XT0
),
447 .vbistart
= { 10, 273 },
448 CROPCAP(/* minhdelayx1 */ 68,
450 /* swidth */ (640 * 910 + 780 / 2) / 780,
451 /* totalwidth */ 910,
455 /* videostart0 */ 23)
457 /* that one hopefully works with the strange timing
458 * which video recorders produce when playing a NTSC
459 * tape on a PAL TV ... */
460 .v4l2_id
= V4L2_STD_PAL_60
,
468 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
469 .scaledtwidth
= 1135,
476 .vbistart
= { 10, 273 },
477 CROPCAP(/* minhdelayx1 */ 68,
480 /* totalwidth */ 1135,
484 /* videostart0 */ 23)
487 static const unsigned int BTTV_TVNORMS
= ARRAY_SIZE(bttv_tvnorms
);
489 /* ----------------------------------------------------------------------- */
491 packed pixel formats must come first */
492 static const struct bttv_format formats
[] = {
494 .name
= "8 bpp, gray",
495 .fourcc
= V4L2_PIX_FMT_GREY
,
496 .btformat
= BT848_COLOR_FMT_Y8
,
498 .flags
= FORMAT_FLAGS_PACKED
,
500 .name
= "8 bpp, dithered color",
501 .fourcc
= V4L2_PIX_FMT_HI240
,
502 .btformat
= BT848_COLOR_FMT_RGB8
,
504 .flags
= FORMAT_FLAGS_PACKED
| FORMAT_FLAGS_DITHER
,
506 .name
= "15 bpp RGB, le",
507 .fourcc
= V4L2_PIX_FMT_RGB555
,
508 .btformat
= BT848_COLOR_FMT_RGB15
,
510 .flags
= FORMAT_FLAGS_PACKED
,
512 .name
= "15 bpp RGB, be",
513 .fourcc
= V4L2_PIX_FMT_RGB555X
,
514 .btformat
= BT848_COLOR_FMT_RGB15
,
515 .btswap
= 0x03, /* byteswap */
517 .flags
= FORMAT_FLAGS_PACKED
,
519 .name
= "16 bpp RGB, le",
520 .fourcc
= V4L2_PIX_FMT_RGB565
,
521 .btformat
= BT848_COLOR_FMT_RGB16
,
523 .flags
= FORMAT_FLAGS_PACKED
,
525 .name
= "16 bpp RGB, be",
526 .fourcc
= V4L2_PIX_FMT_RGB565X
,
527 .btformat
= BT848_COLOR_FMT_RGB16
,
528 .btswap
= 0x03, /* byteswap */
530 .flags
= FORMAT_FLAGS_PACKED
,
532 .name
= "24 bpp RGB, le",
533 .fourcc
= V4L2_PIX_FMT_BGR24
,
534 .btformat
= BT848_COLOR_FMT_RGB24
,
536 .flags
= FORMAT_FLAGS_PACKED
,
538 .name
= "32 bpp RGB, le",
539 .fourcc
= V4L2_PIX_FMT_BGR32
,
540 .btformat
= BT848_COLOR_FMT_RGB32
,
542 .flags
= FORMAT_FLAGS_PACKED
,
544 .name
= "32 bpp RGB, be",
545 .fourcc
= V4L2_PIX_FMT_RGB32
,
546 .btformat
= BT848_COLOR_FMT_RGB32
,
547 .btswap
= 0x0f, /* byte+word swap */
549 .flags
= FORMAT_FLAGS_PACKED
,
551 .name
= "4:2:2, packed, YUYV",
552 .fourcc
= V4L2_PIX_FMT_YUYV
,
553 .btformat
= BT848_COLOR_FMT_YUY2
,
555 .flags
= FORMAT_FLAGS_PACKED
,
557 .name
= "4:2:2, packed, YUYV",
558 .fourcc
= V4L2_PIX_FMT_YUYV
,
559 .btformat
= BT848_COLOR_FMT_YUY2
,
561 .flags
= FORMAT_FLAGS_PACKED
,
563 .name
= "4:2:2, packed, UYVY",
564 .fourcc
= V4L2_PIX_FMT_UYVY
,
565 .btformat
= BT848_COLOR_FMT_YUY2
,
566 .btswap
= 0x03, /* byteswap */
568 .flags
= FORMAT_FLAGS_PACKED
,
570 .name
= "4:2:2, planar, Y-Cb-Cr",
571 .fourcc
= V4L2_PIX_FMT_YUV422P
,
572 .btformat
= BT848_COLOR_FMT_YCrCb422
,
574 .flags
= FORMAT_FLAGS_PLANAR
,
578 .name
= "4:2:0, planar, Y-Cb-Cr",
579 .fourcc
= V4L2_PIX_FMT_YUV420
,
580 .btformat
= BT848_COLOR_FMT_YCrCb422
,
582 .flags
= FORMAT_FLAGS_PLANAR
,
586 .name
= "4:2:0, planar, Y-Cr-Cb",
587 .fourcc
= V4L2_PIX_FMT_YVU420
,
588 .btformat
= BT848_COLOR_FMT_YCrCb422
,
590 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
594 .name
= "4:1:1, planar, Y-Cb-Cr",
595 .fourcc
= V4L2_PIX_FMT_YUV411P
,
596 .btformat
= BT848_COLOR_FMT_YCrCb411
,
598 .flags
= FORMAT_FLAGS_PLANAR
,
602 .name
= "4:1:0, planar, Y-Cb-Cr",
603 .fourcc
= V4L2_PIX_FMT_YUV410
,
604 .btformat
= BT848_COLOR_FMT_YCrCb411
,
606 .flags
= FORMAT_FLAGS_PLANAR
,
610 .name
= "4:1:0, planar, Y-Cr-Cb",
611 .fourcc
= V4L2_PIX_FMT_YVU410
,
612 .btformat
= BT848_COLOR_FMT_YCrCb411
,
614 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
618 .name
= "raw scanlines",
620 .btformat
= BT848_COLOR_FMT_RAW
,
622 .flags
= FORMAT_FLAGS_RAW
,
625 static const unsigned int FORMATS
= ARRAY_SIZE(formats
);
627 /* ----------------------------------------------------------------------- */
629 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
630 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
631 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
632 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
633 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
634 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
635 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
636 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
637 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
638 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
639 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
640 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
642 static const struct v4l2_queryctrl no_ctl
= {
644 .flags
= V4L2_CTRL_FLAG_DISABLED
,
646 static const struct v4l2_queryctrl bttv_ctls
[] = {
649 .id
= V4L2_CID_BRIGHTNESS
,
650 .name
= "Brightness",
654 .default_value
= 32768,
655 .type
= V4L2_CTRL_TYPE_INTEGER
,
657 .id
= V4L2_CID_CONTRAST
,
662 .default_value
= 32768,
663 .type
= V4L2_CTRL_TYPE_INTEGER
,
665 .id
= V4L2_CID_SATURATION
,
666 .name
= "Saturation",
670 .default_value
= 32768,
671 .type
= V4L2_CTRL_TYPE_INTEGER
,
678 .default_value
= 32768,
679 .type
= V4L2_CTRL_TYPE_INTEGER
,
683 .id
= V4L2_CID_AUDIO_MUTE
,
687 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
689 .id
= V4L2_CID_AUDIO_VOLUME
,
694 .default_value
= 65535,
695 .type
= V4L2_CTRL_TYPE_INTEGER
,
697 .id
= V4L2_CID_AUDIO_BALANCE
,
702 .default_value
= 32768,
703 .type
= V4L2_CTRL_TYPE_INTEGER
,
705 .id
= V4L2_CID_AUDIO_BASS
,
710 .default_value
= 32768,
711 .type
= V4L2_CTRL_TYPE_INTEGER
,
713 .id
= V4L2_CID_AUDIO_TREBLE
,
718 .default_value
= 32768,
719 .type
= V4L2_CTRL_TYPE_INTEGER
,
721 /* --- private --- */
723 .id
= V4L2_CID_PRIVATE_CHROMA_AGC
,
724 .name
= "chroma agc",
727 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
729 .id
= V4L2_CID_PRIVATE_COMBFILTER
,
730 .name
= "combfilter",
733 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
735 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
739 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
741 .id
= V4L2_CID_PRIVATE_LUMAFILTER
,
742 .name
= "luma decimation filter",
745 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
747 .id
= V4L2_CID_PRIVATE_AGC_CRUSH
,
751 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
753 .id
= V4L2_CID_PRIVATE_VCR_HACK
,
757 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
759 .id
= V4L2_CID_PRIVATE_WHITECRUSH_UPPER
,
760 .name
= "whitecrush upper",
764 .default_value
= 0xCF,
765 .type
= V4L2_CTRL_TYPE_INTEGER
,
767 .id
= V4L2_CID_PRIVATE_WHITECRUSH_LOWER
,
768 .name
= "whitecrush lower",
772 .default_value
= 0x7F,
773 .type
= V4L2_CTRL_TYPE_INTEGER
,
775 .id
= V4L2_CID_PRIVATE_UV_RATIO
,
781 .type
= V4L2_CTRL_TYPE_INTEGER
,
783 .id
= V4L2_CID_PRIVATE_FULL_LUMA_RANGE
,
784 .name
= "full luma range",
787 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
789 .id
= V4L2_CID_PRIVATE_CORING
,
795 .type
= V4L2_CTRL_TYPE_INTEGER
,
802 static const struct v4l2_queryctrl
*ctrl_by_id(int id
)
806 for (i
= 0; i
< ARRAY_SIZE(bttv_ctls
); i
++)
807 if (bttv_ctls
[i
].id
== id
)
813 /* ----------------------------------------------------------------------- */
814 /* resource management */
817 RESOURCE_ allocated by freed by
819 VIDEO_READ bttv_read 1) bttv_read 2)
821 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
822 VIDIOC_QBUF 1) bttv_release
825 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
826 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
829 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
830 VIDIOC_QBUF 1) bttv_release
831 bttv_read, bttv_poll 1) 4)
833 1) The resource must be allocated when we enter buffer prepare functions
834 and remain allocated while buffers are in the DMA queue.
835 2) This is a single frame read.
836 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
837 RESOURCE_OVERLAY is allocated.
838 4) This is a continuous read, implies VIDIOC_STREAMON.
840 Note this driver permits video input and standard changes regardless if
841 resources are allocated.
844 #define VBI_RESOURCES (RESOURCE_VBI)
845 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
846 RESOURCE_VIDEO_STREAM | \
850 int check_alloc_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bit
)
852 int xbits
; /* mutual exclusive resources */
854 if (fh
->resources
& bit
)
855 /* have it already allocated */
859 if (bit
& (RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
))
860 xbits
|= RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
;
863 if (btv
->resources
& xbits
) {
864 /* no, someone else uses it */
868 if ((bit
& VIDEO_RESOURCES
)
869 && 0 == (btv
->resources
& VIDEO_RESOURCES
)) {
870 /* Do crop - use current, don't - use default parameters. */
871 __s32 top
= btv
->crop
[!!fh
->do_crop
].rect
.top
;
873 if (btv
->vbi_end
> top
)
876 /* We cannot capture the same line as video and VBI data.
877 Claim scan lines crop[].rect.top to bottom. */
878 btv
->crop_start
= top
;
879 } else if (bit
& VBI_RESOURCES
) {
880 __s32 end
= fh
->vbi_fmt
.end
;
882 if (end
> btv
->crop_start
)
885 /* Claim scan lines above fh->vbi_fmt.end. */
889 /* it's free, grab it */
890 fh
->resources
|= bit
;
891 btv
->resources
|= bit
;
899 int check_btres(struct bttv_fh
*fh
, int bit
)
901 return (fh
->resources
& bit
);
905 int locked_btres(struct bttv
*btv
, int bit
)
907 return (btv
->resources
& bit
);
910 /* Call with btv->lock down. */
912 disclaim_vbi_lines(struct bttv
*btv
)
917 /* Call with btv->lock down. */
919 disclaim_video_lines(struct bttv
*btv
)
921 const struct bttv_tvnorm
*tvnorm
;
924 tvnorm
= &bttv_tvnorms
[btv
->tvnorm
];
925 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
926 + tvnorm
->cropcap
.bounds
.height
;
928 /* VBI capturing ends at VDELAY, start of video capturing, no
929 matter how many lines the VBI RISC program expects. When video
930 capturing is off, it shall no longer "preempt" VBI capturing,
931 so we set VDELAY to maximum. */
932 crop
= btread(BT848_E_CROP
) | 0xc0;
933 btwrite(crop
, BT848_E_CROP
);
934 btwrite(0xfe, BT848_E_VDELAY_LO
);
935 btwrite(crop
, BT848_O_CROP
);
936 btwrite(0xfe, BT848_O_VDELAY_LO
);
940 void free_btres_lock(struct bttv
*btv
, struct bttv_fh
*fh
, int bits
)
942 if ((fh
->resources
& bits
) != bits
) {
943 /* trying to free ressources not allocated by us ... */
944 printk("bttv: BUG! (btres)\n");
946 fh
->resources
&= ~bits
;
947 btv
->resources
&= ~bits
;
949 bits
= btv
->resources
;
951 if (0 == (bits
& VIDEO_RESOURCES
))
952 disclaim_video_lines(btv
);
954 if (0 == (bits
& VBI_RESOURCES
))
955 disclaim_vbi_lines(btv
);
958 /* ----------------------------------------------------------------------- */
959 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
961 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
962 PLL_X = Reference pre-divider (0=1, 1=2)
963 PLL_C = Post divider (0=6, 1=4)
964 PLL_I = Integer input
965 PLL_F = Fractional input
967 F_input = 28.636363 MHz:
968 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
971 static void set_pll_freq(struct bttv
*btv
, unsigned int fin
, unsigned int fout
)
973 unsigned char fl
, fh
, fi
;
975 /* prevent overflows */
988 btwrite(fl
, BT848_PLL_F_LO
);
989 btwrite(fh
, BT848_PLL_F_HI
);
990 btwrite(fi
|BT848_PLL_X
, BT848_PLL_XCI
);
993 static void set_pll(struct bttv
*btv
)
997 if (!btv
->pll
.pll_crystal
)
1000 if (btv
->pll
.pll_ofreq
== btv
->pll
.pll_current
) {
1001 dprintk("bttv%d: PLL: no change required\n",btv
->c
.nr
);
1005 if (btv
->pll
.pll_ifreq
== btv
->pll
.pll_ofreq
) {
1007 if (btv
->pll
.pll_current
== 0)
1009 bttv_printk(KERN_INFO
"bttv%d: PLL can sleep, using XTAL (%d).\n",
1010 btv
->c
.nr
,btv
->pll
.pll_ifreq
);
1011 btwrite(0x00,BT848_TGCTRL
);
1012 btwrite(0x00,BT848_PLL_XCI
);
1013 btv
->pll
.pll_current
= 0;
1017 bttv_printk(KERN_INFO
"bttv%d: PLL: %d => %d ",btv
->c
.nr
,
1018 btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
1019 set_pll_freq(btv
, btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
1021 for (i
=0; i
<10; i
++) {
1022 /* Let other people run while the PLL stabilizes */
1026 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_PLOCK
) {
1027 btwrite(0,BT848_DSTATUS
);
1029 btwrite(0x08,BT848_TGCTRL
);
1030 btv
->pll
.pll_current
= btv
->pll
.pll_ofreq
;
1031 bttv_printk(" ok\n");
1035 btv
->pll
.pll_current
= -1;
1036 bttv_printk("failed\n");
1040 /* used to switch between the bt848's analog/digital video capture modes */
1041 static void bt848A_set_timing(struct bttv
*btv
)
1044 int table_idx
= bttv_tvnorms
[btv
->tvnorm
].sram
;
1045 int fsc
= bttv_tvnorms
[btv
->tvnorm
].Fsc
;
1047 if (btv
->input
== btv
->dig
) {
1048 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1049 btv
->c
.nr
,table_idx
);
1051 /* timing change...reset timing generator address */
1052 btwrite(0x00, BT848_TGCTRL
);
1053 btwrite(0x02, BT848_TGCTRL
);
1054 btwrite(0x00, BT848_TGCTRL
);
1056 len
=SRAM_Table
[table_idx
][0];
1057 for(i
= 1; i
<= len
; i
++)
1058 btwrite(SRAM_Table
[table_idx
][i
],BT848_TGLB
);
1059 btv
->pll
.pll_ofreq
= 27000000;
1062 btwrite(0x11, BT848_TGCTRL
);
1063 btwrite(0x41, BT848_DVSIF
);
1065 btv
->pll
.pll_ofreq
= fsc
;
1067 btwrite(0x0, BT848_DVSIF
);
1071 /* ----------------------------------------------------------------------- */
1073 static void bt848_bright(struct bttv
*btv
, int bright
)
1077 // printk("bttv: set bright: %d\n",bright); // DEBUG
1078 btv
->bright
= bright
;
1080 /* We want -128 to 127 we get 0-65535 */
1081 value
= (bright
>> 8) - 128;
1082 btwrite(value
& 0xff, BT848_BRIGHT
);
1085 static void bt848_hue(struct bttv
*btv
, int hue
)
1092 value
= (hue
>> 8) - 128;
1093 btwrite(value
& 0xff, BT848_HUE
);
1096 static void bt848_contrast(struct bttv
*btv
, int cont
)
1100 btv
->contrast
= cont
;
1103 value
= (cont
>> 7);
1104 hibit
= (value
>> 6) & 4;
1105 btwrite(value
& 0xff, BT848_CONTRAST_LO
);
1106 btaor(hibit
, ~4, BT848_E_CONTROL
);
1107 btaor(hibit
, ~4, BT848_O_CONTROL
);
1110 static void bt848_sat(struct bttv
*btv
, int color
)
1112 int val_u
,val_v
,hibits
;
1114 btv
->saturation
= color
;
1116 /* 0-511 for the color */
1117 val_u
= ((color
* btv
->opt_uv_ratio
) / 50) >> 7;
1118 val_v
= (((color
* (100 - btv
->opt_uv_ratio
) / 50) >>7)*180L)/254;
1119 hibits
= (val_u
>> 7) & 2;
1120 hibits
|= (val_v
>> 8) & 1;
1121 btwrite(val_u
& 0xff, BT848_SAT_U_LO
);
1122 btwrite(val_v
& 0xff, BT848_SAT_V_LO
);
1123 btaor(hibits
, ~3, BT848_E_CONTROL
);
1124 btaor(hibits
, ~3, BT848_O_CONTROL
);
1127 /* ----------------------------------------------------------------------- */
1130 video_mux(struct bttv
*btv
, unsigned int input
)
1134 if (input
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1137 /* needed by RemoteVideo MX */
1138 mask2
= bttv_tvcards
[btv
->c
.type
].gpiomask2
;
1140 gpio_inout(mask2
,mask2
);
1142 if (input
== btv
->svhs
) {
1143 btor(BT848_CONTROL_COMP
, BT848_E_CONTROL
);
1144 btor(BT848_CONTROL_COMP
, BT848_O_CONTROL
);
1146 btand(~BT848_CONTROL_COMP
, BT848_E_CONTROL
);
1147 btand(~BT848_CONTROL_COMP
, BT848_O_CONTROL
);
1149 mux
= bttv_muxsel(btv
, input
);
1150 btaor(mux
<<5, ~(3<<5), BT848_IFORM
);
1151 dprintk(KERN_DEBUG
"bttv%d: video mux: input=%d mux=%d\n",
1152 btv
->c
.nr
,input
,mux
);
1154 /* card specific hook */
1155 if(bttv_tvcards
[btv
->c
.type
].muxsel_hook
)
1156 bttv_tvcards
[btv
->c
.type
].muxsel_hook (btv
, input
);
1160 static char *audio_modes
[] = {
1161 "audio: tuner", "audio: radio", "audio: extern",
1162 "audio: intern", "audio: mute"
1166 audio_mux(struct bttv
*btv
, int input
, int mute
)
1168 int gpio_val
, signal
;
1169 struct v4l2_control ctrl
;
1171 gpio_inout(bttv_tvcards
[btv
->c
.type
].gpiomask
,
1172 bttv_tvcards
[btv
->c
.type
].gpiomask
);
1173 signal
= btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
;
1179 mute
= mute
|| (btv
->opt_automute
&& !signal
&& !btv
->radio_user
);
1182 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomute
;
1184 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomux
[input
];
1186 switch (btv
->c
.type
) {
1187 case BTTV_BOARD_VOODOOTV_FM
:
1188 case BTTV_BOARD_VOODOOTV_200
:
1189 gpio_val
= bttv_tda9880_setnorm(btv
, gpio_val
);
1193 gpio_bits(bttv_tvcards
[btv
->c
.type
].gpiomask
, gpio_val
);
1197 bttv_gpio_tracking(btv
, audio_modes
[mute
? 4 : input
]);
1201 ctrl
.id
= V4L2_CID_AUDIO_MUTE
;
1202 ctrl
.value
= btv
->mute
;
1203 bttv_call_all(btv
, core
, s_ctrl
, &ctrl
);
1204 if (btv
->sd_msp34xx
) {
1207 /* Note: the inputs tuner/radio/extern/intern are translated
1208 to msp routings. This assumes common behavior for all msp3400
1209 based TV cards. When this assumption fails, then the
1210 specific MSP routing must be added to the card table.
1211 For now this is sufficient. */
1213 case TVAUDIO_INPUT_RADIO
:
1214 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1215 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1217 case TVAUDIO_INPUT_EXTERN
:
1218 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER1
,
1219 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1221 case TVAUDIO_INPUT_INTERN
:
1222 /* Yes, this is the same input as for RADIO. I doubt
1223 if this is ever used. The only board with an INTERN
1224 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1225 that was tested. My guess is that the whole INTERN
1226 input does not work. */
1227 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1228 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1230 case TVAUDIO_INPUT_TUNER
:
1232 /* This is the only card that uses TUNER2, and afaik,
1233 is the only difference between the VOODOOTV_FM
1235 if (btv
->c
.type
== BTTV_BOARD_VOODOOTV_200
)
1236 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER2
, \
1237 MSP_DSP_IN_TUNER
, MSP_DSP_IN_TUNER
);
1239 in
= MSP_INPUT_DEFAULT
;
1242 v4l2_subdev_call(btv
->sd_msp34xx
, audio
, s_routing
,
1243 in
, MSP_OUTPUT_DEFAULT
, 0);
1245 if (btv
->sd_tvaudio
) {
1246 v4l2_subdev_call(btv
->sd_tvaudio
, audio
, s_routing
,
1253 audio_mute(struct bttv
*btv
, int mute
)
1255 return audio_mux(btv
, btv
->audio
, mute
);
1259 audio_input(struct bttv
*btv
, int input
)
1261 return audio_mux(btv
, input
, btv
->mute
);
1265 bttv_crop_calc_limits(struct bttv_crop
*c
)
1267 /* Scale factor min. 1:1, max. 16:1. Min. image size
1268 48 x 32. Scaled width must be a multiple of 4. */
1271 /* For bug compatibility with VIDIOCGCAP and image
1272 size checks in earlier driver versions. */
1273 c
->min_scaled_width
= 48;
1274 c
->min_scaled_height
= 32;
1276 c
->min_scaled_width
=
1277 (max(48, c
->rect
.width
>> 4) + 3) & ~3;
1278 c
->min_scaled_height
=
1279 max(32, c
->rect
.height
>> 4);
1282 c
->max_scaled_width
= c
->rect
.width
& ~3;
1283 c
->max_scaled_height
= c
->rect
.height
;
1287 bttv_crop_reset(struct bttv_crop
*c
, unsigned int norm
)
1289 c
->rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1290 bttv_crop_calc_limits(c
);
1293 /* Call with btv->lock down. */
1295 set_tvnorm(struct bttv
*btv
, unsigned int norm
)
1297 const struct bttv_tvnorm
*tvnorm
;
1300 BUG_ON(norm
>= BTTV_TVNORMS
);
1301 BUG_ON(btv
->tvnorm
>= BTTV_TVNORMS
);
1303 tvnorm
= &bttv_tvnorms
[norm
];
1305 if (memcmp(&bttv_tvnorms
[btv
->tvnorm
].cropcap
, &tvnorm
->cropcap
,
1306 sizeof (tvnorm
->cropcap
))) {
1307 bttv_crop_reset(&btv
->crop
[0], norm
);
1308 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
1310 if (0 == (btv
->resources
& VIDEO_RESOURCES
)) {
1311 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
1312 + tvnorm
->cropcap
.bounds
.height
;
1318 btwrite(tvnorm
->adelay
, BT848_ADELAY
);
1319 btwrite(tvnorm
->bdelay
, BT848_BDELAY
);
1320 btaor(tvnorm
->iform
,~(BT848_IFORM_NORM
|BT848_IFORM_XTBOTH
),
1322 btwrite(tvnorm
->vbipack
, BT848_VBI_PACK_SIZE
);
1323 btwrite(1, BT848_VBI_PACK_DEL
);
1324 bt848A_set_timing(btv
);
1326 switch (btv
->c
.type
) {
1327 case BTTV_BOARD_VOODOOTV_FM
:
1328 case BTTV_BOARD_VOODOOTV_200
:
1329 bttv_tda9880_setnorm(btv
, gpio_read());
1332 id
= tvnorm
->v4l2_id
;
1333 bttv_call_all(btv
, core
, s_std
, id
);
1338 /* Call with btv->lock down. */
1340 set_input(struct bttv
*btv
, unsigned int input
, unsigned int norm
)
1342 unsigned long flags
;
1346 spin_lock_irqsave(&btv
->s_lock
,flags
);
1347 if (btv
->curr
.frame_irq
) {
1348 /* active capture -> delayed input switch */
1349 btv
->new_input
= input
;
1351 video_mux(btv
,input
);
1353 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1355 video_mux(btv
,input
);
1357 audio_input(btv
, (btv
->tuner_type
!= TUNER_ABSENT
&& input
== 0) ?
1358 TVAUDIO_INPUT_TUNER
: TVAUDIO_INPUT_EXTERN
);
1359 set_tvnorm(btv
, norm
);
1362 static void init_irqreg(struct bttv
*btv
)
1365 btwrite(0xfffffUL
, BT848_INT_STAT
);
1367 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1369 btwrite(BT848_INT_I2CDONE
,
1373 btwrite((btv
->triton1
) |
1374 (btv
->gpioirq
? BT848_INT_GPINT
: 0) |
1376 (fdsr
? BT848_INT_FDSR
: 0) |
1377 BT848_INT_RISCI
| BT848_INT_OCERR
|
1378 BT848_INT_FMTCHG
|BT848_INT_HLOCK
|
1384 static void init_bt848(struct bttv
*btv
)
1388 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1389 /* very basic init only */
1394 btwrite(0x00, BT848_CAP_CTL
);
1395 btwrite(BT848_COLOR_CTL_GAMMA
, BT848_COLOR_CTL
);
1396 btwrite(BT848_IFORM_XTAUTO
| BT848_IFORM_AUTO
, BT848_IFORM
);
1398 /* set planar and packed mode trigger points and */
1399 /* set rising edge of inverted GPINTR pin as irq trigger */
1400 btwrite(BT848_GPIO_DMA_CTL_PKTP_32
|
1401 BT848_GPIO_DMA_CTL_PLTP1_16
|
1402 BT848_GPIO_DMA_CTL_PLTP23_16
|
1403 BT848_GPIO_DMA_CTL_GPINTC
|
1404 BT848_GPIO_DMA_CTL_GPINTI
,
1405 BT848_GPIO_DMA_CTL
);
1407 val
= btv
->opt_chroma_agc
? BT848_SCLOOP_CAGC
: 0;
1408 btwrite(val
, BT848_E_SCLOOP
);
1409 btwrite(val
, BT848_O_SCLOOP
);
1411 btwrite(0x20, BT848_E_VSCALE_HI
);
1412 btwrite(0x20, BT848_O_VSCALE_HI
);
1413 btwrite(BT848_ADC_RESERVED
| (btv
->opt_adc_crush
? BT848_ADC_CRUSH
: 0),
1416 btwrite(whitecrush_upper
, BT848_WC_UP
);
1417 btwrite(whitecrush_lower
, BT848_WC_DOWN
);
1419 if (btv
->opt_lumafilter
) {
1420 btwrite(0, BT848_E_CONTROL
);
1421 btwrite(0, BT848_O_CONTROL
);
1423 btwrite(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1424 btwrite(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1427 bt848_bright(btv
, btv
->bright
);
1428 bt848_hue(btv
, btv
->hue
);
1429 bt848_contrast(btv
, btv
->contrast
);
1430 bt848_sat(btv
, btv
->saturation
);
1436 static void bttv_reinit_bt848(struct bttv
*btv
)
1438 unsigned long flags
;
1441 printk(KERN_INFO
"bttv%d: reset, reinitialize\n",btv
->c
.nr
);
1442 spin_lock_irqsave(&btv
->s_lock
,flags
);
1444 bttv_set_dma(btv
,0);
1445 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1448 btv
->pll
.pll_current
= -1;
1449 set_input(btv
, btv
->input
, btv
->tvnorm
);
1452 static int bttv_g_ctrl(struct file
*file
, void *priv
,
1453 struct v4l2_control
*c
)
1455 struct bttv_fh
*fh
= priv
;
1456 struct bttv
*btv
= fh
->btv
;
1459 case V4L2_CID_BRIGHTNESS
:
1460 c
->value
= btv
->bright
;
1463 c
->value
= btv
->hue
;
1465 case V4L2_CID_CONTRAST
:
1466 c
->value
= btv
->contrast
;
1468 case V4L2_CID_SATURATION
:
1469 c
->value
= btv
->saturation
;
1472 case V4L2_CID_AUDIO_MUTE
:
1473 case V4L2_CID_AUDIO_VOLUME
:
1474 case V4L2_CID_AUDIO_BALANCE
:
1475 case V4L2_CID_AUDIO_BASS
:
1476 case V4L2_CID_AUDIO_TREBLE
:
1477 bttv_call_all(btv
, core
, g_ctrl
, c
);
1480 case V4L2_CID_PRIVATE_CHROMA_AGC
:
1481 c
->value
= btv
->opt_chroma_agc
;
1483 case V4L2_CID_PRIVATE_COMBFILTER
:
1484 c
->value
= btv
->opt_combfilter
;
1486 case V4L2_CID_PRIVATE_LUMAFILTER
:
1487 c
->value
= btv
->opt_lumafilter
;
1489 case V4L2_CID_PRIVATE_AUTOMUTE
:
1490 c
->value
= btv
->opt_automute
;
1492 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1493 c
->value
= btv
->opt_adc_crush
;
1495 case V4L2_CID_PRIVATE_VCR_HACK
:
1496 c
->value
= btv
->opt_vcr_hack
;
1498 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1499 c
->value
= btv
->opt_whitecrush_upper
;
1501 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1502 c
->value
= btv
->opt_whitecrush_lower
;
1504 case V4L2_CID_PRIVATE_UV_RATIO
:
1505 c
->value
= btv
->opt_uv_ratio
;
1507 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1508 c
->value
= btv
->opt_full_luma_range
;
1510 case V4L2_CID_PRIVATE_CORING
:
1511 c
->value
= btv
->opt_coring
;
1519 static int bttv_s_ctrl(struct file
*file
, void *f
,
1520 struct v4l2_control
*c
)
1524 struct bttv_fh
*fh
= f
;
1525 struct bttv
*btv
= fh
->btv
;
1527 err
= v4l2_prio_check(&btv
->prio
, fh
->prio
);
1532 case V4L2_CID_BRIGHTNESS
:
1533 bt848_bright(btv
, c
->value
);
1536 bt848_hue(btv
, c
->value
);
1538 case V4L2_CID_CONTRAST
:
1539 bt848_contrast(btv
, c
->value
);
1541 case V4L2_CID_SATURATION
:
1542 bt848_sat(btv
, c
->value
);
1544 case V4L2_CID_AUDIO_MUTE
:
1545 audio_mute(btv
, c
->value
);
1547 case V4L2_CID_AUDIO_VOLUME
:
1548 if (btv
->volume_gpio
)
1549 btv
->volume_gpio(btv
, c
->value
);
1551 bttv_call_all(btv
, core
, s_ctrl
, c
);
1553 case V4L2_CID_AUDIO_BALANCE
:
1554 case V4L2_CID_AUDIO_BASS
:
1555 case V4L2_CID_AUDIO_TREBLE
:
1556 bttv_call_all(btv
, core
, s_ctrl
, c
);
1559 case V4L2_CID_PRIVATE_CHROMA_AGC
:
1560 btv
->opt_chroma_agc
= c
->value
;
1561 val
= btv
->opt_chroma_agc
? BT848_SCLOOP_CAGC
: 0;
1562 btwrite(val
, BT848_E_SCLOOP
);
1563 btwrite(val
, BT848_O_SCLOOP
);
1565 case V4L2_CID_PRIVATE_COMBFILTER
:
1566 btv
->opt_combfilter
= c
->value
;
1568 case V4L2_CID_PRIVATE_LUMAFILTER
:
1569 btv
->opt_lumafilter
= c
->value
;
1570 if (btv
->opt_lumafilter
) {
1571 btand(~BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1572 btand(~BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1574 btor(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1575 btor(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1578 case V4L2_CID_PRIVATE_AUTOMUTE
:
1579 btv
->opt_automute
= c
->value
;
1581 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1582 btv
->opt_adc_crush
= c
->value
;
1583 btwrite(BT848_ADC_RESERVED
|
1584 (btv
->opt_adc_crush
? BT848_ADC_CRUSH
: 0),
1587 case V4L2_CID_PRIVATE_VCR_HACK
:
1588 btv
->opt_vcr_hack
= c
->value
;
1590 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1591 btv
->opt_whitecrush_upper
= c
->value
;
1592 btwrite(c
->value
, BT848_WC_UP
);
1594 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1595 btv
->opt_whitecrush_lower
= c
->value
;
1596 btwrite(c
->value
, BT848_WC_DOWN
);
1598 case V4L2_CID_PRIVATE_UV_RATIO
:
1599 btv
->opt_uv_ratio
= c
->value
;
1600 bt848_sat(btv
, btv
->saturation
);
1602 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1603 btv
->opt_full_luma_range
= c
->value
;
1604 btaor((c
->value
<<7), ~BT848_OFORM_RANGE
, BT848_OFORM
);
1606 case V4L2_CID_PRIVATE_CORING
:
1607 btv
->opt_coring
= c
->value
;
1608 btaor((c
->value
<<5), ~BT848_OFORM_CORE32
, BT848_OFORM
);
1616 /* ----------------------------------------------------------------------- */
1618 void bttv_gpio_tracking(struct bttv
*btv
, char *comment
)
1620 unsigned int outbits
, data
;
1621 outbits
= btread(BT848_GPIO_OUT_EN
);
1622 data
= btread(BT848_GPIO_DATA
);
1623 printk(KERN_DEBUG
"bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1624 btv
->c
.nr
,outbits
,data
& outbits
, data
& ~outbits
, comment
);
1627 static void bttv_field_count(struct bttv
*btv
)
1635 /* start field counter */
1636 btor(BT848_INT_VSYNC
,BT848_INT_MASK
);
1638 /* stop field counter */
1639 btand(~BT848_INT_VSYNC
,BT848_INT_MASK
);
1640 btv
->field_count
= 0;
1644 static const struct bttv_format
*
1645 format_by_fourcc(int fourcc
)
1649 for (i
= 0; i
< FORMATS
; i
++) {
1650 if (-1 == formats
[i
].fourcc
)
1652 if (formats
[i
].fourcc
== fourcc
)
1658 /* ----------------------------------------------------------------------- */
1662 bttv_switch_overlay(struct bttv
*btv
, struct bttv_fh
*fh
,
1663 struct bttv_buffer
*new)
1665 struct bttv_buffer
*old
;
1666 unsigned long flags
;
1669 dprintk("switch_overlay: enter [new=%p]\n",new);
1671 new->vb
.state
= VIDEOBUF_DONE
;
1672 spin_lock_irqsave(&btv
->s_lock
,flags
);
1676 bttv_set_dma(btv
, 0x03);
1677 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1679 dprintk("switch_overlay: old=%p state is %d\n",old
,old
->vb
.state
);
1680 bttv_dma_free(&fh
->cap
,btv
, old
);
1684 free_btres_lock(btv
,fh
,RESOURCE_OVERLAY
);
1685 dprintk("switch_overlay: done\n");
1689 /* ----------------------------------------------------------------------- */
1690 /* video4linux (1) interface */
1692 static int bttv_prepare_buffer(struct videobuf_queue
*q
,struct bttv
*btv
,
1693 struct bttv_buffer
*buf
,
1694 const struct bttv_format
*fmt
,
1695 unsigned int width
, unsigned int height
,
1696 enum v4l2_field field
)
1698 struct bttv_fh
*fh
= q
->priv_data
;
1699 int redo_dma_risc
= 0;
1704 /* check settings */
1707 if (fmt
->btformat
== BT848_COLOR_FMT_RAW
) {
1709 height
= RAW_LINES
*2;
1710 if (width
*height
> buf
->vb
.bsize
)
1712 buf
->vb
.size
= buf
->vb
.bsize
;
1714 /* Make sure tvnorm and vbi_end remain consistent
1715 until we're done. */
1719 /* In this mode capturing always starts at defrect.top
1720 (default VDELAY), ignoring cropping parameters. */
1721 if (btv
->vbi_end
> bttv_tvnorms
[norm
].cropcap
.defrect
.top
) {
1725 c
.rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1728 c
= btv
->crop
[!!fh
->do_crop
];
1730 if (width
< c
.min_scaled_width
||
1731 width
> c
.max_scaled_width
||
1732 height
< c
.min_scaled_height
)
1736 case V4L2_FIELD_TOP
:
1737 case V4L2_FIELD_BOTTOM
:
1738 case V4L2_FIELD_ALTERNATE
:
1739 /* btv->crop counts frame lines. Max. scale
1740 factor is 16:1 for frames, 8:1 for fields. */
1741 if (height
* 2 > c
.max_scaled_height
)
1746 if (height
> c
.max_scaled_height
)
1751 buf
->vb
.size
= (width
* height
* fmt
->depth
) >> 3;
1752 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
1756 /* alloc + fill struct bttv_buffer (if changed) */
1757 if (buf
->vb
.width
!= width
|| buf
->vb
.height
!= height
||
1758 buf
->vb
.field
!= field
||
1759 buf
->tvnorm
!= norm
|| buf
->fmt
!= fmt
||
1760 buf
->crop
.top
!= c
.rect
.top
||
1761 buf
->crop
.left
!= c
.rect
.left
||
1762 buf
->crop
.width
!= c
.rect
.width
||
1763 buf
->crop
.height
!= c
.rect
.height
) {
1764 buf
->vb
.width
= width
;
1765 buf
->vb
.height
= height
;
1766 buf
->vb
.field
= field
;
1773 /* alloc risc memory */
1774 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1776 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,&btv
->fbuf
)))
1781 if (0 != (rc
= bttv_buffer_risc(btv
,buf
)))
1784 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1788 bttv_dma_free(q
,btv
,buf
);
1793 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1795 struct bttv_fh
*fh
= q
->priv_data
;
1797 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
1800 if (*size
* *count
> gbuffers
* gbufsize
)
1801 *count
= (gbuffers
* gbufsize
) / *size
;
1806 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
1807 enum v4l2_field field
)
1809 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1810 struct bttv_fh
*fh
= q
->priv_data
;
1812 return bttv_prepare_buffer(q
,fh
->btv
, buf
, fh
->fmt
,
1813 fh
->width
, fh
->height
, field
);
1817 buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1819 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1820 struct bttv_fh
*fh
= q
->priv_data
;
1821 struct bttv
*btv
= fh
->btv
;
1823 buf
->vb
.state
= VIDEOBUF_QUEUED
;
1824 list_add_tail(&buf
->vb
.queue
,&btv
->capture
);
1825 if (!btv
->curr
.frame_irq
) {
1827 bttv_set_dma(btv
, 0x03);
1831 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1833 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1834 struct bttv_fh
*fh
= q
->priv_data
;
1836 bttv_dma_free(q
,fh
->btv
,buf
);
1839 static struct videobuf_queue_ops bttv_video_qops
= {
1840 .buf_setup
= buffer_setup
,
1841 .buf_prepare
= buffer_prepare
,
1842 .buf_queue
= buffer_queue
,
1843 .buf_release
= buffer_release
,
1846 static int bttv_s_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1848 struct bttv_fh
*fh
= priv
;
1849 struct bttv
*btv
= fh
->btv
;
1853 err
= v4l2_prio_check(&btv
->prio
, fh
->prio
);
1857 for (i
= 0; i
< BTTV_TVNORMS
; i
++)
1858 if (*id
& bttv_tvnorms
[i
].v4l2_id
)
1860 if (i
== BTTV_TVNORMS
) {
1872 static int bttv_querystd(struct file
*file
, void *f
, v4l2_std_id
*id
)
1874 struct bttv_fh
*fh
= f
;
1875 struct bttv
*btv
= fh
->btv
;
1877 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_NUML
)
1878 *id
= V4L2_STD_625_50
;
1880 *id
= V4L2_STD_525_60
;
1884 static int bttv_enum_input(struct file
*file
, void *priv
,
1885 struct v4l2_input
*i
)
1887 struct bttv_fh
*fh
= priv
;
1888 struct bttv
*btv
= fh
->btv
;
1891 if (i
->index
>= bttv_tvcards
[btv
->c
.type
].video_inputs
) {
1896 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1899 if (btv
->tuner_type
!= TUNER_ABSENT
&& i
->index
== 0) {
1900 sprintf(i
->name
, "Television");
1901 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1903 } else if (i
->index
== btv
->svhs
) {
1904 sprintf(i
->name
, "S-Video");
1906 sprintf(i
->name
, "Composite%d", i
->index
);
1909 if (i
->index
== btv
->input
) {
1910 __u32 dstatus
= btread(BT848_DSTATUS
);
1911 if (0 == (dstatus
& BT848_DSTATUS_PRES
))
1912 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1913 if (0 == (dstatus
& BT848_DSTATUS_HLOC
))
1914 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1917 i
->std
= BTTV_NORMS
;
1924 static int bttv_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1926 struct bttv_fh
*fh
= priv
;
1927 struct bttv
*btv
= fh
->btv
;
1934 static int bttv_s_input(struct file
*file
, void *priv
, unsigned int i
)
1936 struct bttv_fh
*fh
= priv
;
1937 struct bttv
*btv
= fh
->btv
;
1941 err
= v4l2_prio_check(&btv
->prio
, fh
->prio
);
1945 if (i
> bttv_tvcards
[btv
->c
.type
].video_inputs
) {
1950 set_input(btv
, i
, btv
->tvnorm
);
1956 static int bttv_s_tuner(struct file
*file
, void *priv
,
1957 struct v4l2_tuner
*t
)
1959 struct bttv_fh
*fh
= priv
;
1960 struct bttv
*btv
= fh
->btv
;
1963 if (unlikely(0 != t
->index
))
1966 if (unlikely(btv
->tuner_type
== TUNER_ABSENT
)) {
1971 err
= v4l2_prio_check(&btv
->prio
, fh
->prio
);
1975 bttv_call_all(btv
, tuner
, s_tuner
, t
);
1977 if (btv
->audio_mode_gpio
)
1978 btv
->audio_mode_gpio(btv
, t
, 1);
1985 static int bttv_g_frequency(struct file
*file
, void *priv
,
1986 struct v4l2_frequency
*f
)
1988 struct bttv_fh
*fh
= priv
;
1989 struct bttv
*btv
= fh
->btv
;
1991 f
->type
= btv
->radio_user
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1992 f
->frequency
= btv
->freq
;
1997 static int bttv_s_frequency(struct file
*file
, void *priv
,
1998 struct v4l2_frequency
*f
)
2000 struct bttv_fh
*fh
= priv
;
2001 struct bttv
*btv
= fh
->btv
;
2004 if (unlikely(f
->tuner
!= 0))
2007 err
= v4l2_prio_check(&btv
->prio
, fh
->prio
);
2011 if (unlikely(f
->type
!= (btv
->radio_user
2012 ? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
))) {
2016 btv
->freq
= f
->frequency
;
2017 bttv_call_all(btv
, tuner
, s_frequency
, f
);
2018 if (btv
->has_matchbox
&& btv
->radio_user
)
2019 tea5757_set_freq(btv
, btv
->freq
);
2025 static int bttv_log_status(struct file
*file
, void *f
)
2027 struct bttv_fh
*fh
= f
;
2028 struct bttv
*btv
= fh
->btv
;
2030 printk(KERN_INFO
"bttv%d: ======== START STATUS CARD #%d ========\n",
2031 btv
->c
.nr
, btv
->c
.nr
);
2032 bttv_call_all(btv
, core
, log_status
);
2033 printk(KERN_INFO
"bttv%d: ======== END STATUS CARD #%d ========\n",
2034 btv
->c
.nr
, btv
->c
.nr
);
2038 #ifdef CONFIG_VIDEO_ADV_DEBUG
2039 static int bttv_g_register(struct file
*file
, void *f
,
2040 struct v4l2_dbg_register
*reg
)
2042 struct bttv_fh
*fh
= f
;
2043 struct bttv
*btv
= fh
->btv
;
2045 if (!capable(CAP_SYS_ADMIN
))
2048 if (!v4l2_chip_match_host(®
->match
))
2051 /* bt848 has a 12-bit register space */
2053 reg
->val
= btread(reg
->reg
);
2059 static int bttv_s_register(struct file
*file
, void *f
,
2060 struct v4l2_dbg_register
*reg
)
2062 struct bttv_fh
*fh
= f
;
2063 struct bttv
*btv
= fh
->btv
;
2065 if (!capable(CAP_SYS_ADMIN
))
2068 if (!v4l2_chip_match_host(®
->match
))
2071 /* bt848 has a 12-bit register space */
2073 btwrite(reg
->val
, reg
->reg
);
2079 /* Given cropping boundaries b and the scaled width and height of a
2080 single field or frame, which must not exceed hardware limits, this
2081 function adjusts the cropping parameters c. */
2083 bttv_crop_adjust (struct bttv_crop
* c
,
2084 const struct v4l2_rect
* b
,
2087 enum v4l2_field field
)
2089 __s32 frame_height
= height
<< !V4L2_FIELD_HAS_BOTH(field
);
2093 if (width
< c
->min_scaled_width
) {
2094 /* Max. hor. scale factor 16:1. */
2095 c
->rect
.width
= width
* 16;
2096 } else if (width
> c
->max_scaled_width
) {
2097 /* Min. hor. scale factor 1:1. */
2098 c
->rect
.width
= width
;
2100 max_left
= b
->left
+ b
->width
- width
;
2101 max_left
= min(max_left
, (__s32
) MAX_HDELAY
);
2102 if (c
->rect
.left
> max_left
)
2103 c
->rect
.left
= max_left
;
2106 if (height
< c
->min_scaled_height
) {
2107 /* Max. vert. scale factor 16:1, single fields 8:1. */
2108 c
->rect
.height
= height
* 16;
2109 } else if (frame_height
> c
->max_scaled_height
) {
2110 /* Min. vert. scale factor 1:1.
2111 Top and height count field lines times two. */
2112 c
->rect
.height
= (frame_height
+ 1) & ~1;
2114 max_top
= b
->top
+ b
->height
- c
->rect
.height
;
2115 if (c
->rect
.top
> max_top
)
2116 c
->rect
.top
= max_top
;
2119 bttv_crop_calc_limits(c
);
2122 /* Returns an error if scaling to a frame or single field with the given
2123 width and height is not possible with the current cropping parameters
2124 and width aligned according to width_mask. If adjust_size is TRUE the
2125 function may adjust the width and/or height instead, rounding width
2126 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2127 also adjust the current cropping parameters to get closer to the
2128 desired image size. */
2130 limit_scaled_size_lock (struct bttv_fh
* fh
,
2133 enum v4l2_field field
,
2134 unsigned int width_mask
,
2135 unsigned int width_bias
,
2139 struct bttv
*btv
= fh
->btv
;
2140 const struct v4l2_rect
*b
;
2141 struct bttv_crop
*c
;
2148 BUG_ON((int) width_mask
>= 0 ||
2149 width_bias
>= (unsigned int) -width_mask
);
2151 /* Make sure tvnorm, vbi_end and the current cropping parameters
2152 remain consistent until we're done. */
2154 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2156 /* Do crop - use current, don't - use default parameters. */
2157 c
= &btv
->crop
[!!fh
->do_crop
];
2162 && !locked_btres(btv
, VIDEO_RESOURCES
)) {
2166 /* We cannot scale up. When the scaled image is larger
2167 than crop.rect we adjust the crop.rect as required
2168 by the V4L2 spec, hence cropcap.bounds are our limit. */
2169 max_width
= min(b
->width
, (__s32
) MAX_HACTIVE
);
2170 max_height
= b
->height
;
2172 /* We cannot capture the same line as video and VBI data.
2173 Note btv->vbi_end is really a minimum, see
2174 bttv_vbi_try_fmt(). */
2175 if (btv
->vbi_end
> b
->top
) {
2176 max_height
-= btv
->vbi_end
- b
->top
;
2178 if (min_height
> max_height
)
2183 if (btv
->vbi_end
> c
->rect
.top
)
2186 min_width
= c
->min_scaled_width
;
2187 min_height
= c
->min_scaled_height
;
2188 max_width
= c
->max_scaled_width
;
2189 max_height
= c
->max_scaled_height
;
2194 min_width
= (min_width
- width_mask
- 1) & width_mask
;
2195 max_width
= max_width
& width_mask
;
2197 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2198 min_height
= min_height
;
2199 /* Min. scale factor is 1:1. */
2200 max_height
>>= !V4L2_FIELD_HAS_BOTH(field
);
2203 *width
= clamp(*width
, min_width
, max_width
);
2204 *height
= clamp(*height
, min_height
, max_height
);
2206 /* Round after clamping to avoid overflow. */
2207 *width
= (*width
+ width_bias
) & width_mask
;
2210 bttv_crop_adjust(c
, b
, *width
, *height
, field
);
2212 if (btv
->vbi_end
> c
->rect
.top
) {
2213 /* Move the crop window out of the way. */
2214 c
->rect
.top
= btv
->vbi_end
;
2219 if (*width
< min_width
||
2220 *height
< min_height
||
2221 *width
> max_width
||
2222 *height
> max_height
||
2223 0 != (*width
& ~width_mask
))
2227 rc
= 0; /* success */
2234 /* Returns an error if the given overlay window dimensions are not
2235 possible with the current cropping parameters. If adjust_size is
2236 TRUE the function may adjust the window width and/or height
2237 instead, however it always rounds the horizontal position and
2238 width as btcx_align() does. If adjust_crop is TRUE the function
2239 may also adjust the current cropping parameters to get closer
2240 to the desired window size. */
2242 verify_window_lock (struct bttv_fh
* fh
,
2243 struct v4l2_window
* win
,
2247 enum v4l2_field field
;
2248 unsigned int width_mask
;
2251 if (win
->w
.width
< 48 || win
->w
.height
< 32)
2253 if (win
->clipcount
> 2048)
2258 if (V4L2_FIELD_ANY
== field
) {
2261 height2
= fh
->btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2262 field
= (win
->w
.height
> height2
)
2263 ? V4L2_FIELD_INTERLACED
2267 case V4L2_FIELD_TOP
:
2268 case V4L2_FIELD_BOTTOM
:
2269 case V4L2_FIELD_INTERLACED
:
2275 /* 4-byte alignment. */
2276 if (NULL
== fh
->ovfmt
)
2279 switch (fh
->ovfmt
->depth
) {
2293 win
->w
.width
-= win
->w
.left
& ~width_mask
;
2294 win
->w
.left
= (win
->w
.left
- width_mask
- 1) & width_mask
;
2296 rc
= limit_scaled_size_lock(fh
, &win
->w
.width
, &win
->w
.height
,
2298 /* width_bias: round down */ 0,
2299 adjust_size
, adjust_crop
);
2307 static int setup_window_lock(struct bttv_fh
*fh
, struct bttv
*btv
,
2308 struct v4l2_window
*win
, int fixup
)
2310 struct v4l2_clip
*clips
= NULL
;
2311 int n
,size
,retval
= 0;
2313 if (NULL
== fh
->ovfmt
)
2315 if (!(fh
->ovfmt
->flags
& FORMAT_FLAGS_PACKED
))
2317 retval
= verify_window_lock(fh
, win
,
2318 /* adjust_size */ fixup
,
2319 /* adjust_crop */ fixup
);
2323 /* copy clips -- luckily v4l1 + v4l2 are binary
2324 compatible here ...*/
2326 size
= sizeof(*clips
)*(n
+4);
2327 clips
= kmalloc(size
,GFP_KERNEL
);
2331 if (copy_from_user(clips
,win
->clips
,sizeof(struct v4l2_clip
)*n
)) {
2337 /* clip against screen */
2338 if (NULL
!= btv
->fbuf
.base
)
2339 n
= btcx_screen_clips(btv
->fbuf
.fmt
.width
, btv
->fbuf
.fmt
.height
,
2341 btcx_sort_clips(clips
,n
);
2343 /* 4-byte alignments */
2344 switch (fh
->ovfmt
->depth
) {
2347 btcx_align(&win
->w
, clips
, n
, 3);
2350 btcx_align(&win
->w
, clips
, n
, 1);
2353 /* no alignment fixups needed */
2359 kfree(fh
->ov
.clips
);
2360 fh
->ov
.clips
= clips
;
2364 fh
->ov
.field
= win
->field
;
2365 fh
->ov
.setup_ok
= 1;
2367 btv
->init
.ov
.w
.width
= win
->w
.width
;
2368 btv
->init
.ov
.w
.height
= win
->w
.height
;
2369 btv
->init
.ov
.field
= win
->field
;
2371 /* update overlay if needed */
2373 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2374 struct bttv_buffer
*new;
2376 new = videobuf_sg_alloc(sizeof(*new));
2377 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2378 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2379 retval
= bttv_switch_overlay(btv
,fh
,new);
2384 /* ----------------------------------------------------------------------- */
2386 static struct videobuf_queue
* bttv_queue(struct bttv_fh
*fh
)
2388 struct videobuf_queue
* q
= NULL
;
2391 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2394 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2403 static int bttv_resource(struct bttv_fh
*fh
)
2408 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2409 res
= RESOURCE_VIDEO_STREAM
;
2411 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2420 static int bttv_switch_type(struct bttv_fh
*fh
, enum v4l2_buf_type type
)
2422 struct videobuf_queue
*q
= bttv_queue(fh
);
2423 int res
= bttv_resource(fh
);
2425 if (check_btres(fh
,res
))
2427 if (videobuf_queue_is_busy(q
))
2434 pix_format_set_size (struct v4l2_pix_format
* f
,
2435 const struct bttv_format
* fmt
,
2437 unsigned int height
)
2442 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2443 f
->bytesperline
= width
; /* Y plane */
2444 f
->sizeimage
= (width
* height
* fmt
->depth
) >> 3;
2446 f
->bytesperline
= (width
* fmt
->depth
) >> 3;
2447 f
->sizeimage
= height
* f
->bytesperline
;
2451 static int bttv_g_fmt_vid_cap(struct file
*file
, void *priv
,
2452 struct v4l2_format
*f
)
2454 struct bttv_fh
*fh
= priv
;
2456 pix_format_set_size(&f
->fmt
.pix
, fh
->fmt
,
2457 fh
->width
, fh
->height
);
2458 f
->fmt
.pix
.field
= fh
->cap
.field
;
2459 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
2464 static int bttv_g_fmt_vid_overlay(struct file
*file
, void *priv
,
2465 struct v4l2_format
*f
)
2467 struct bttv_fh
*fh
= priv
;
2469 f
->fmt
.win
.w
= fh
->ov
.w
;
2470 f
->fmt
.win
.field
= fh
->ov
.field
;
2475 static int bttv_try_fmt_vid_cap(struct file
*file
, void *priv
,
2476 struct v4l2_format
*f
)
2478 const struct bttv_format
*fmt
;
2479 struct bttv_fh
*fh
= priv
;
2480 struct bttv
*btv
= fh
->btv
;
2481 enum v4l2_field field
;
2482 __s32 width
, height
;
2485 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2489 field
= f
->fmt
.pix
.field
;
2491 if (V4L2_FIELD_ANY
== field
) {
2494 height2
= btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2495 field
= (f
->fmt
.pix
.height
> height2
)
2496 ? V4L2_FIELD_INTERLACED
2497 : V4L2_FIELD_BOTTOM
;
2500 if (V4L2_FIELD_SEQ_BT
== field
)
2501 field
= V4L2_FIELD_SEQ_TB
;
2504 case V4L2_FIELD_TOP
:
2505 case V4L2_FIELD_BOTTOM
:
2506 case V4L2_FIELD_ALTERNATE
:
2507 case V4L2_FIELD_INTERLACED
:
2509 case V4L2_FIELD_SEQ_TB
:
2510 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
)
2517 width
= f
->fmt
.pix
.width
;
2518 height
= f
->fmt
.pix
.height
;
2520 rc
= limit_scaled_size_lock(fh
, &width
, &height
, field
,
2521 /* width_mask: 4 pixels */ ~3,
2522 /* width_bias: nearest */ 2,
2523 /* adjust_size */ 1,
2524 /* adjust_crop */ 0);
2528 /* update data for the application */
2529 f
->fmt
.pix
.field
= field
;
2530 pix_format_set_size(&f
->fmt
.pix
, fmt
, width
, height
);
2535 static int bttv_try_fmt_vid_overlay(struct file
*file
, void *priv
,
2536 struct v4l2_format
*f
)
2538 struct bttv_fh
*fh
= priv
;
2540 return verify_window_lock(fh
, &f
->fmt
.win
,
2541 /* adjust_size */ 1,
2542 /* adjust_crop */ 0);
2545 static int bttv_s_fmt_vid_cap(struct file
*file
, void *priv
,
2546 struct v4l2_format
*f
)
2549 const struct bttv_format
*fmt
;
2550 struct bttv_fh
*fh
= priv
;
2551 struct bttv
*btv
= fh
->btv
;
2552 __s32 width
, height
;
2553 enum v4l2_field field
;
2555 retval
= bttv_switch_type(fh
, f
->type
);
2559 retval
= bttv_try_fmt_vid_cap(file
, priv
, f
);
2563 width
= f
->fmt
.pix
.width
;
2564 height
= f
->fmt
.pix
.height
;
2565 field
= f
->fmt
.pix
.field
;
2567 retval
= limit_scaled_size_lock(fh
, &width
, &height
, f
->fmt
.pix
.field
,
2568 /* width_mask: 4 pixels */ ~3,
2569 /* width_bias: nearest */ 2,
2570 /* adjust_size */ 1,
2571 /* adjust_crop */ 1);
2575 f
->fmt
.pix
.field
= field
;
2577 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2579 /* update our state informations */
2581 fh
->cap
.field
= f
->fmt
.pix
.field
;
2582 fh
->cap
.last
= V4L2_FIELD_NONE
;
2583 fh
->width
= f
->fmt
.pix
.width
;
2584 fh
->height
= f
->fmt
.pix
.height
;
2585 btv
->init
.fmt
= fmt
;
2586 btv
->init
.width
= f
->fmt
.pix
.width
;
2587 btv
->init
.height
= f
->fmt
.pix
.height
;
2592 static int bttv_s_fmt_vid_overlay(struct file
*file
, void *priv
,
2593 struct v4l2_format
*f
)
2595 struct bttv_fh
*fh
= priv
;
2596 struct bttv
*btv
= fh
->btv
;
2598 if (no_overlay
> 0) {
2599 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2603 return setup_window_lock(fh
, btv
, &f
->fmt
.win
, 1);
2606 static int bttv_querycap(struct file
*file
, void *priv
,
2607 struct v4l2_capability
*cap
)
2609 struct bttv_fh
*fh
= priv
;
2610 struct bttv
*btv
= fh
->btv
;
2615 strlcpy(cap
->driver
, "bttv", sizeof(cap
->driver
));
2616 strlcpy(cap
->card
, btv
->video_dev
->name
, sizeof(cap
->card
));
2617 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2618 "PCI:%s", pci_name(btv
->c
.pci
));
2619 cap
->version
= BTTV_VERSION_CODE
;
2621 V4L2_CAP_VIDEO_CAPTURE
|
2622 V4L2_CAP_VBI_CAPTURE
|
2623 V4L2_CAP_READWRITE
|
2625 if (no_overlay
<= 0)
2626 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
2629 * No need to lock here: those vars are initialized during board
2630 * probe and remains untouched during the rest of the driver lifecycle
2632 if (btv
->has_saa6588
)
2633 cap
->capabilities
|= V4L2_CAP_RDS_CAPTURE
;
2634 if (btv
->tuner_type
!= TUNER_ABSENT
)
2635 cap
->capabilities
|= V4L2_CAP_TUNER
;
2639 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc
*f
)
2643 for (i
= 0; i
< FORMATS
; i
++) {
2644 if (formats
[i
].fourcc
!= -1)
2646 if ((unsigned int)index
== f
->index
)
2652 f
->pixelformat
= formats
[i
].fourcc
;
2653 strlcpy(f
->description
, formats
[i
].name
, sizeof(f
->description
));
2658 static int bttv_enum_fmt_vid_cap(struct file
*file
, void *priv
,
2659 struct v4l2_fmtdesc
*f
)
2661 int rc
= bttv_enum_fmt_cap_ovr(f
);
2669 static int bttv_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
2670 struct v4l2_fmtdesc
*f
)
2674 if (no_overlay
> 0) {
2675 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2679 rc
= bttv_enum_fmt_cap_ovr(f
);
2684 if (!(formats
[rc
].flags
& FORMAT_FLAGS_PACKED
))
2690 static int bttv_g_fbuf(struct file
*file
, void *f
,
2691 struct v4l2_framebuffer
*fb
)
2693 struct bttv_fh
*fh
= f
;
2694 struct bttv
*btv
= fh
->btv
;
2697 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2699 fb
->fmt
.pixelformat
= fh
->ovfmt
->fourcc
;
2703 static int bttv_overlay(struct file
*file
, void *f
, unsigned int on
)
2705 struct bttv_fh
*fh
= f
;
2706 struct bttv
*btv
= fh
->btv
;
2707 struct bttv_buffer
*new;
2712 if (unlikely(!btv
->fbuf
.base
)) {
2715 if (unlikely(!fh
->ov
.setup_ok
)) {
2716 dprintk("bttv%d: overlay: !setup_ok\n", btv
->c
.nr
);
2723 if (!check_alloc_btres_lock(btv
, fh
, RESOURCE_OVERLAY
))
2727 fh
->ov
.tvnorm
= btv
->tvnorm
;
2728 new = videobuf_sg_alloc(sizeof(*new));
2729 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2730 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2736 retval
= bttv_switch_overlay(btv
, fh
, new);
2740 static int bttv_s_fbuf(struct file
*file
, void *f
,
2741 struct v4l2_framebuffer
*fb
)
2743 struct bttv_fh
*fh
= f
;
2744 struct bttv
*btv
= fh
->btv
;
2745 const struct bttv_format
*fmt
;
2748 if (!capable(CAP_SYS_ADMIN
) &&
2749 !capable(CAP_SYS_RAWIO
))
2753 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2756 if (0 == (fmt
->flags
& FORMAT_FLAGS_PACKED
))
2760 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2761 __s32 width
= fb
->fmt
.width
;
2762 __s32 height
= fb
->fmt
.height
;
2764 retval
= limit_scaled_size_lock(fh
, &width
, &height
,
2765 V4L2_FIELD_INTERLACED
,
2766 /* width_mask */ ~3,
2768 /* adjust_size */ 0,
2769 /* adjust_crop */ 0);
2775 btv
->fbuf
.base
= fb
->base
;
2776 btv
->fbuf
.fmt
.width
= fb
->fmt
.width
;
2777 btv
->fbuf
.fmt
.height
= fb
->fmt
.height
;
2778 if (0 != fb
->fmt
.bytesperline
)
2779 btv
->fbuf
.fmt
.bytesperline
= fb
->fmt
.bytesperline
;
2781 btv
->fbuf
.fmt
.bytesperline
= btv
->fbuf
.fmt
.width
*fmt
->depth
/8;
2785 btv
->init
.ovfmt
= fmt
;
2786 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2789 fh
->ov
.w
.width
= fb
->fmt
.width
;
2790 fh
->ov
.w
.height
= fb
->fmt
.height
;
2791 btv
->init
.ov
.w
.width
= fb
->fmt
.width
;
2792 btv
->init
.ov
.w
.height
= fb
->fmt
.height
;
2793 kfree(fh
->ov
.clips
);
2794 fh
->ov
.clips
= NULL
;
2797 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2798 struct bttv_buffer
*new;
2800 new = videobuf_sg_alloc(sizeof(*new));
2801 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2802 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2803 retval
= bttv_switch_overlay(btv
, fh
, new);
2809 static int bttv_reqbufs(struct file
*file
, void *priv
,
2810 struct v4l2_requestbuffers
*p
)
2812 struct bttv_fh
*fh
= priv
;
2813 return videobuf_reqbufs(bttv_queue(fh
), p
);
2816 static int bttv_querybuf(struct file
*file
, void *priv
,
2817 struct v4l2_buffer
*b
)
2819 struct bttv_fh
*fh
= priv
;
2820 return videobuf_querybuf(bttv_queue(fh
), b
);
2823 static int bttv_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2825 struct bttv_fh
*fh
= priv
;
2826 struct bttv
*btv
= fh
->btv
;
2827 int res
= bttv_resource(fh
);
2829 if (!check_alloc_btres_lock(btv
, fh
, res
))
2832 return videobuf_qbuf(bttv_queue(fh
), b
);
2835 static int bttv_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2837 struct bttv_fh
*fh
= priv
;
2838 return videobuf_dqbuf(bttv_queue(fh
), b
,
2839 file
->f_flags
& O_NONBLOCK
);
2842 static int bttv_streamon(struct file
*file
, void *priv
,
2843 enum v4l2_buf_type type
)
2845 struct bttv_fh
*fh
= priv
;
2846 struct bttv
*btv
= fh
->btv
;
2847 int res
= bttv_resource(fh
);
2849 if (!check_alloc_btres_lock(btv
, fh
, res
))
2851 return videobuf_streamon(bttv_queue(fh
));
2855 static int bttv_streamoff(struct file
*file
, void *priv
,
2856 enum v4l2_buf_type type
)
2858 struct bttv_fh
*fh
= priv
;
2859 struct bttv
*btv
= fh
->btv
;
2861 int res
= bttv_resource(fh
);
2864 retval
= videobuf_streamoff(bttv_queue(fh
));
2867 free_btres_lock(btv
, fh
, res
);
2871 static int bttv_queryctrl(struct file
*file
, void *priv
,
2872 struct v4l2_queryctrl
*c
)
2874 struct bttv_fh
*fh
= priv
;
2875 struct bttv
*btv
= fh
->btv
;
2876 const struct v4l2_queryctrl
*ctrl
;
2878 if ((c
->id
< V4L2_CID_BASE
||
2879 c
->id
>= V4L2_CID_LASTP1
) &&
2880 (c
->id
< V4L2_CID_PRIVATE_BASE
||
2881 c
->id
>= V4L2_CID_PRIVATE_LASTP1
))
2884 if (!btv
->volume_gpio
&& (c
->id
== V4L2_CID_AUDIO_VOLUME
))
2887 ctrl
= ctrl_by_id(c
->id
);
2889 *c
= (NULL
!= ctrl
) ? *ctrl
: no_ctl
;
2895 static int bttv_g_parm(struct file
*file
, void *f
,
2896 struct v4l2_streamparm
*parm
)
2898 struct bttv_fh
*fh
= f
;
2899 struct bttv
*btv
= fh
->btv
;
2901 v4l2_video_std_frame_period(bttv_tvnorms
[btv
->tvnorm
].v4l2_id
,
2902 &parm
->parm
.capture
.timeperframe
);
2907 static int bttv_g_tuner(struct file
*file
, void *priv
,
2908 struct v4l2_tuner
*t
)
2910 struct bttv_fh
*fh
= priv
;
2911 struct bttv
*btv
= fh
->btv
;
2913 if (btv
->tuner_type
== TUNER_ABSENT
)
2918 t
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
2919 bttv_call_all(btv
, tuner
, g_tuner
, t
);
2920 strcpy(t
->name
, "Television");
2921 t
->capability
= V4L2_TUNER_CAP_NORM
;
2922 t
->type
= V4L2_TUNER_ANALOG_TV
;
2923 if (btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
)
2926 if (btv
->audio_mode_gpio
)
2927 btv
->audio_mode_gpio(btv
, t
, 0);
2932 static int bttv_g_priority(struct file
*file
, void *f
, enum v4l2_priority
*p
)
2934 struct bttv_fh
*fh
= f
;
2935 struct bttv
*btv
= fh
->btv
;
2937 *p
= v4l2_prio_max(&btv
->prio
);
2942 static int bttv_s_priority(struct file
*file
, void *f
,
2943 enum v4l2_priority prio
)
2945 struct bttv_fh
*fh
= f
;
2946 struct bttv
*btv
= fh
->btv
;
2949 rc
= v4l2_prio_change(&btv
->prio
, &fh
->prio
, prio
);
2954 static int bttv_cropcap(struct file
*file
, void *priv
,
2955 struct v4l2_cropcap
*cap
)
2957 struct bttv_fh
*fh
= priv
;
2958 struct bttv
*btv
= fh
->btv
;
2960 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2961 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2964 *cap
= bttv_tvnorms
[btv
->tvnorm
].cropcap
;
2969 static int bttv_g_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
2971 struct bttv_fh
*fh
= f
;
2972 struct bttv
*btv
= fh
->btv
;
2974 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2975 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2978 /* No fh->do_crop = 1; because btv->crop[1] may be
2979 inconsistent with fh->width or fh->height and apps
2980 do not expect a change here. */
2982 crop
->c
= btv
->crop
[!!fh
->do_crop
].rect
;
2987 static int bttv_s_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
2989 struct bttv_fh
*fh
= f
;
2990 struct bttv
*btv
= fh
->btv
;
2991 const struct v4l2_rect
*b
;
2999 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
3000 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
3003 /* Make sure tvnorm, vbi_end and the current cropping
3004 parameters remain consistent until we're done. Note
3005 read() may change vbi_end in check_alloc_btres_lock(). */
3006 retval
= v4l2_prio_check(&btv
->prio
, fh
->prio
);
3013 if (locked_btres(fh
->btv
, VIDEO_RESOURCES
)) {
3017 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
3020 b_right
= b_left
+ b
->width
;
3021 b_bottom
= b
->top
+ b
->height
;
3023 b_top
= max(b
->top
, btv
->vbi_end
);
3024 if (b_top
+ 32 >= b_bottom
) {
3028 /* Min. scaled size 48 x 32. */
3029 c
.rect
.left
= clamp(crop
->c
.left
, b_left
, b_right
- 48);
3030 c
.rect
.left
= min(c
.rect
.left
, (__s32
) MAX_HDELAY
);
3032 c
.rect
.width
= clamp(crop
->c
.width
,
3033 48, b_right
- c
.rect
.left
);
3035 c
.rect
.top
= clamp(crop
->c
.top
, b_top
, b_bottom
- 32);
3036 /* Top and height must be a multiple of two. */
3037 c
.rect
.top
= (c
.rect
.top
+ 1) & ~1;
3039 c
.rect
.height
= clamp(crop
->c
.height
,
3040 32, b_bottom
- c
.rect
.top
);
3041 c
.rect
.height
= (c
.rect
.height
+ 1) & ~1;
3043 bttv_crop_calc_limits(&c
);
3049 if (fh
->width
< c
.min_scaled_width
) {
3050 fh
->width
= c
.min_scaled_width
;
3051 btv
->init
.width
= c
.min_scaled_width
;
3052 } else if (fh
->width
> c
.max_scaled_width
) {
3053 fh
->width
= c
.max_scaled_width
;
3054 btv
->init
.width
= c
.max_scaled_width
;
3057 if (fh
->height
< c
.min_scaled_height
) {
3058 fh
->height
= c
.min_scaled_height
;
3059 btv
->init
.height
= c
.min_scaled_height
;
3060 } else if (fh
->height
> c
.max_scaled_height
) {
3061 fh
->height
= c
.max_scaled_height
;
3062 btv
->init
.height
= c
.max_scaled_height
;
3068 static int bttv_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
3070 if (unlikely(a
->index
))
3073 strcpy(a
->name
, "audio");
3077 static int bttv_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
3079 if (unlikely(a
->index
))
3085 static ssize_t
bttv_read(struct file
*file
, char __user
*data
,
3086 size_t count
, loff_t
*ppos
)
3088 struct bttv_fh
*fh
= file
->private_data
;
3091 if (fh
->btv
->errors
)
3092 bttv_reinit_bt848(fh
->btv
);
3093 dprintk("bttv%d: read count=%d type=%s\n",
3094 fh
->btv
->c
.nr
,(int)count
,v4l2_type_names
[fh
->type
]);
3097 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
3098 if (!check_alloc_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
)) {
3099 /* VIDEO_READ in use by another fh,
3100 or VIDEO_STREAM by any fh. */
3103 retval
= videobuf_read_one(&fh
->cap
, data
, count
, ppos
,
3104 file
->f_flags
& O_NONBLOCK
);
3105 free_btres_lock(fh
->btv
, fh
, RESOURCE_VIDEO_READ
);
3107 case V4L2_BUF_TYPE_VBI_CAPTURE
:
3108 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
3110 retval
= videobuf_read_stream(&fh
->vbi
, data
, count
, ppos
, 1,
3111 file
->f_flags
& O_NONBLOCK
);
3119 static unsigned int bttv_poll(struct file
*file
, poll_table
*wait
)
3121 struct bttv_fh
*fh
= file
->private_data
;
3122 struct bttv_buffer
*buf
;
3123 enum v4l2_field field
;
3124 unsigned int rc
= POLLERR
;
3126 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
3127 if (!check_alloc_btres_lock(fh
->btv
,fh
,RESOURCE_VBI
))
3129 return videobuf_poll_stream(file
, &fh
->vbi
, wait
);
3132 if (check_btres(fh
,RESOURCE_VIDEO_STREAM
)) {
3133 /* streaming capture */
3134 if (list_empty(&fh
->cap
.stream
))
3136 buf
= list_entry(fh
->cap
.stream
.next
,struct bttv_buffer
,vb
.stream
);
3138 /* read() capture */
3139 if (NULL
== fh
->cap
.read_buf
) {
3140 /* need to capture a new frame */
3141 if (locked_btres(fh
->btv
,RESOURCE_VIDEO_STREAM
))
3143 fh
->cap
.read_buf
= videobuf_sg_alloc(fh
->cap
.msize
);
3144 if (NULL
== fh
->cap
.read_buf
)
3146 fh
->cap
.read_buf
->memory
= V4L2_MEMORY_USERPTR
;
3147 field
= videobuf_next_field(&fh
->cap
);
3148 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,field
)) {
3149 kfree (fh
->cap
.read_buf
);
3150 fh
->cap
.read_buf
= NULL
;
3153 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
3154 fh
->cap
.read_off
= 0;
3156 buf
= (struct bttv_buffer
*)fh
->cap
.read_buf
;
3159 poll_wait(file
, &buf
->vb
.done
, wait
);
3160 if (buf
->vb
.state
== VIDEOBUF_DONE
||
3161 buf
->vb
.state
== VIDEOBUF_ERROR
)
3162 rc
= POLLIN
|POLLRDNORM
;
3169 static int bttv_open(struct file
*file
)
3171 struct video_device
*vdev
= video_devdata(file
);
3172 struct bttv
*btv
= video_drvdata(file
);
3174 enum v4l2_buf_type type
= 0;
3176 dprintk(KERN_DEBUG
"bttv: open dev=%s\n", video_device_node_name(vdev
));
3178 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
) {
3179 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
3180 } else if (vdev
->vfl_type
== VFL_TYPE_VBI
) {
3181 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
3187 dprintk(KERN_DEBUG
"bttv%d: open called (type=%s)\n",
3188 btv
->c
.nr
,v4l2_type_names
[type
]);
3190 /* allocate per filehandle data */
3191 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3194 file
->private_data
= fh
;
3199 fh
->ov
.setup_ok
= 0;
3201 v4l2_prio_open(&btv
->prio
, &fh
->prio
);
3203 videobuf_queue_sg_init(&fh
->cap
, &bttv_video_qops
,
3204 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3205 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
3206 V4L2_FIELD_INTERLACED
,
3207 sizeof(struct bttv_buffer
),
3209 videobuf_queue_sg_init(&fh
->vbi
, &bttv_vbi_qops
,
3210 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3211 V4L2_BUF_TYPE_VBI_CAPTURE
,
3213 sizeof(struct bttv_buffer
),
3215 set_tvnorm(btv
,btv
->tvnorm
);
3216 set_input(btv
, btv
->input
, btv
->tvnorm
);
3220 /* The V4L2 spec requires one global set of cropping parameters
3221 which only change on request. These are stored in btv->crop[1].
3222 However for compatibility with V4L apps and cropping unaware
3223 V4L2 apps we now reset the cropping parameters as seen through
3224 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3225 will use btv->crop[0], the default cropping parameters for the
3226 current video standard, and VIDIOC_S_FMT will not implicitely
3227 change the cropping parameters until VIDIOC_S_CROP has been
3229 fh
->do_crop
= !reset_crop
; /* module parameter */
3231 /* Likewise there should be one global set of VBI capture
3232 parameters, but for compatibility with V4L apps and earlier
3233 driver versions each fh has its own parameters. */
3234 bttv_vbi_fmt_reset(&fh
->vbi_fmt
, btv
->tvnorm
);
3236 bttv_field_count(btv
);
3240 static int bttv_release(struct file
*file
)
3242 struct bttv_fh
*fh
= file
->private_data
;
3243 struct bttv
*btv
= fh
->btv
;
3245 /* turn off overlay */
3246 if (check_btres(fh
, RESOURCE_OVERLAY
))
3247 bttv_switch_overlay(btv
,fh
,NULL
);
3249 /* stop video capture */
3250 if (check_btres(fh
, RESOURCE_VIDEO_STREAM
)) {
3251 videobuf_streamoff(&fh
->cap
);
3252 free_btres_lock(btv
,fh
,RESOURCE_VIDEO_STREAM
);
3254 if (fh
->cap
.read_buf
) {
3255 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
3256 kfree(fh
->cap
.read_buf
);
3258 if (check_btres(fh
, RESOURCE_VIDEO_READ
)) {
3259 free_btres_lock(btv
, fh
, RESOURCE_VIDEO_READ
);
3262 /* stop vbi capture */
3263 if (check_btres(fh
, RESOURCE_VBI
)) {
3264 videobuf_stop(&fh
->vbi
);
3265 free_btres_lock(btv
,fh
,RESOURCE_VBI
);
3270 videobuf_mmap_free(&fh
->cap
);
3271 videobuf_mmap_free(&fh
->vbi
);
3272 v4l2_prio_close(&btv
->prio
, fh
->prio
);
3273 file
->private_data
= NULL
;
3277 bttv_field_count(btv
);
3286 bttv_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3288 struct bttv_fh
*fh
= file
->private_data
;
3290 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3291 fh
->btv
->c
.nr
, v4l2_type_names
[fh
->type
],
3292 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
3293 return videobuf_mmap_mapper(bttv_queue(fh
),vma
);
3296 static const struct v4l2_file_operations bttv_fops
=
3298 .owner
= THIS_MODULE
,
3300 .release
= bttv_release
,
3301 .unlocked_ioctl
= video_ioctl2
,
3307 static const struct v4l2_ioctl_ops bttv_ioctl_ops
= {
3308 .vidioc_querycap
= bttv_querycap
,
3309 .vidioc_enum_fmt_vid_cap
= bttv_enum_fmt_vid_cap
,
3310 .vidioc_g_fmt_vid_cap
= bttv_g_fmt_vid_cap
,
3311 .vidioc_try_fmt_vid_cap
= bttv_try_fmt_vid_cap
,
3312 .vidioc_s_fmt_vid_cap
= bttv_s_fmt_vid_cap
,
3313 .vidioc_enum_fmt_vid_overlay
= bttv_enum_fmt_vid_overlay
,
3314 .vidioc_g_fmt_vid_overlay
= bttv_g_fmt_vid_overlay
,
3315 .vidioc_try_fmt_vid_overlay
= bttv_try_fmt_vid_overlay
,
3316 .vidioc_s_fmt_vid_overlay
= bttv_s_fmt_vid_overlay
,
3317 .vidioc_g_fmt_vbi_cap
= bttv_g_fmt_vbi_cap
,
3318 .vidioc_try_fmt_vbi_cap
= bttv_try_fmt_vbi_cap
,
3319 .vidioc_s_fmt_vbi_cap
= bttv_s_fmt_vbi_cap
,
3320 .vidioc_g_audio
= bttv_g_audio
,
3321 .vidioc_s_audio
= bttv_s_audio
,
3322 .vidioc_cropcap
= bttv_cropcap
,
3323 .vidioc_reqbufs
= bttv_reqbufs
,
3324 .vidioc_querybuf
= bttv_querybuf
,
3325 .vidioc_qbuf
= bttv_qbuf
,
3326 .vidioc_dqbuf
= bttv_dqbuf
,
3327 .vidioc_s_std
= bttv_s_std
,
3328 .vidioc_enum_input
= bttv_enum_input
,
3329 .vidioc_g_input
= bttv_g_input
,
3330 .vidioc_s_input
= bttv_s_input
,
3331 .vidioc_queryctrl
= bttv_queryctrl
,
3332 .vidioc_g_ctrl
= bttv_g_ctrl
,
3333 .vidioc_s_ctrl
= bttv_s_ctrl
,
3334 .vidioc_streamon
= bttv_streamon
,
3335 .vidioc_streamoff
= bttv_streamoff
,
3336 .vidioc_g_tuner
= bttv_g_tuner
,
3337 .vidioc_s_tuner
= bttv_s_tuner
,
3338 .vidioc_g_crop
= bttv_g_crop
,
3339 .vidioc_s_crop
= bttv_s_crop
,
3340 .vidioc_g_fbuf
= bttv_g_fbuf
,
3341 .vidioc_s_fbuf
= bttv_s_fbuf
,
3342 .vidioc_overlay
= bttv_overlay
,
3343 .vidioc_g_priority
= bttv_g_priority
,
3344 .vidioc_s_priority
= bttv_s_priority
,
3345 .vidioc_g_parm
= bttv_g_parm
,
3346 .vidioc_g_frequency
= bttv_g_frequency
,
3347 .vidioc_s_frequency
= bttv_s_frequency
,
3348 .vidioc_log_status
= bttv_log_status
,
3349 .vidioc_querystd
= bttv_querystd
,
3350 #ifdef CONFIG_VIDEO_ADV_DEBUG
3351 .vidioc_g_register
= bttv_g_register
,
3352 .vidioc_s_register
= bttv_s_register
,
3356 static struct video_device bttv_video_template
= {
3358 .ioctl_ops
= &bttv_ioctl_ops
,
3359 .tvnorms
= BTTV_NORMS
,
3360 .current_norm
= V4L2_STD_PAL
,
3363 /* ----------------------------------------------------------------------- */
3364 /* radio interface */
3366 static int radio_open(struct file
*file
)
3368 struct video_device
*vdev
= video_devdata(file
);
3369 struct bttv
*btv
= video_drvdata(file
);
3372 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev
));
3374 dprintk("bttv%d: open called (radio)\n",btv
->c
.nr
);
3376 /* allocate per filehandle data */
3377 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3380 file
->private_data
= fh
;
3383 v4l2_prio_open(&btv
->prio
, &fh
->prio
);
3387 bttv_call_all(btv
, tuner
, s_radio
);
3388 audio_input(btv
,TVAUDIO_INPUT_RADIO
);
3393 static int radio_release(struct file
*file
)
3395 struct bttv_fh
*fh
= file
->private_data
;
3396 struct bttv
*btv
= fh
->btv
;
3397 struct saa6588_command cmd
;
3399 v4l2_prio_close(&btv
->prio
, fh
->prio
);
3400 file
->private_data
= NULL
;
3405 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_CLOSE
, &cmd
);
3410 static int radio_querycap(struct file
*file
, void *priv
,
3411 struct v4l2_capability
*cap
)
3413 struct bttv_fh
*fh
= priv
;
3414 struct bttv
*btv
= fh
->btv
;
3416 strcpy(cap
->driver
, "bttv");
3417 strlcpy(cap
->card
, btv
->radio_dev
->name
, sizeof(cap
->card
));
3418 sprintf(cap
->bus_info
, "PCI:%s", pci_name(btv
->c
.pci
));
3419 cap
->version
= BTTV_VERSION_CODE
;
3420 cap
->capabilities
= V4L2_CAP_TUNER
;
3425 static int radio_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
3427 struct bttv_fh
*fh
= priv
;
3428 struct bttv
*btv
= fh
->btv
;
3430 if (btv
->tuner_type
== TUNER_ABSENT
)
3434 strcpy(t
->name
, "Radio");
3435 t
->type
= V4L2_TUNER_RADIO
;
3437 bttv_call_all(btv
, tuner
, g_tuner
, t
);
3439 if (btv
->audio_mode_gpio
)
3440 btv
->audio_mode_gpio(btv
, t
, 0);
3445 static int radio_enum_input(struct file
*file
, void *priv
,
3446 struct v4l2_input
*i
)
3451 strcpy(i
->name
, "Radio");
3452 i
->type
= V4L2_INPUT_TYPE_TUNER
;
3457 static int radio_g_audio(struct file
*file
, void *priv
,
3458 struct v4l2_audio
*a
)
3460 if (unlikely(a
->index
))
3463 strcpy(a
->name
, "Radio");
3468 static int radio_s_tuner(struct file
*file
, void *priv
,
3469 struct v4l2_tuner
*t
)
3471 struct bttv_fh
*fh
= priv
;
3472 struct bttv
*btv
= fh
->btv
;
3477 bttv_call_all(btv
, tuner
, g_tuner
, t
);
3481 static int radio_s_audio(struct file
*file
, void *priv
,
3482 struct v4l2_audio
*a
)
3484 if (unlikely(a
->index
))
3490 static int radio_s_input(struct file
*filp
, void *priv
, unsigned int i
)
3498 static int radio_s_std(struct file
*file
, void *fh
, v4l2_std_id
*norm
)
3503 static int radio_queryctrl(struct file
*file
, void *priv
,
3504 struct v4l2_queryctrl
*c
)
3506 const struct v4l2_queryctrl
*ctrl
;
3508 if (c
->id
< V4L2_CID_BASE
||
3509 c
->id
>= V4L2_CID_LASTP1
)
3512 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
3513 ctrl
= ctrl_by_id(c
->id
);
3521 static int radio_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
3527 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
3528 size_t count
, loff_t
*ppos
)
3530 struct bttv_fh
*fh
= file
->private_data
;
3531 struct bttv
*btv
= fh
->btv
;
3532 struct saa6588_command cmd
;
3533 cmd
.block_count
= count
/3;
3535 cmd
.instance
= file
;
3536 cmd
.result
= -ENODEV
;
3538 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_READ
, &cmd
);
3543 static unsigned int radio_poll(struct file
*file
, poll_table
*wait
)
3545 struct bttv_fh
*fh
= file
->private_data
;
3546 struct bttv
*btv
= fh
->btv
;
3547 struct saa6588_command cmd
;
3548 cmd
.instance
= file
;
3549 cmd
.event_list
= wait
;
3550 cmd
.result
= -ENODEV
;
3551 bttv_call_all(btv
, core
, ioctl
, SAA6588_CMD_POLL
, &cmd
);
3556 static const struct v4l2_file_operations radio_fops
=
3558 .owner
= THIS_MODULE
,
3561 .release
= radio_release
,
3562 .unlocked_ioctl
= video_ioctl2
,
3566 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
3567 .vidioc_querycap
= radio_querycap
,
3568 .vidioc_g_tuner
= radio_g_tuner
,
3569 .vidioc_enum_input
= radio_enum_input
,
3570 .vidioc_g_audio
= radio_g_audio
,
3571 .vidioc_s_tuner
= radio_s_tuner
,
3572 .vidioc_s_audio
= radio_s_audio
,
3573 .vidioc_s_input
= radio_s_input
,
3574 .vidioc_s_std
= radio_s_std
,
3575 .vidioc_queryctrl
= radio_queryctrl
,
3576 .vidioc_g_input
= radio_g_input
,
3577 .vidioc_g_ctrl
= bttv_g_ctrl
,
3578 .vidioc_s_ctrl
= bttv_s_ctrl
,
3579 .vidioc_g_frequency
= bttv_g_frequency
,
3580 .vidioc_s_frequency
= bttv_s_frequency
,
3583 static struct video_device radio_template
= {
3584 .fops
= &radio_fops
,
3585 .ioctl_ops
= &radio_ioctl_ops
,
3588 /* ----------------------------------------------------------------------- */
3589 /* some debug code */
3591 static int bttv_risc_decode(u32 risc
)
3593 static char *instr
[16] = {
3594 [ BT848_RISC_WRITE
>> 28 ] = "write",
3595 [ BT848_RISC_SKIP
>> 28 ] = "skip",
3596 [ BT848_RISC_WRITEC
>> 28 ] = "writec",
3597 [ BT848_RISC_JUMP
>> 28 ] = "jump",
3598 [ BT848_RISC_SYNC
>> 28 ] = "sync",
3599 [ BT848_RISC_WRITE123
>> 28 ] = "write123",
3600 [ BT848_RISC_SKIP123
>> 28 ] = "skip123",
3601 [ BT848_RISC_WRITE1S23
>> 28 ] = "write1s23",
3603 static int incr
[16] = {
3604 [ BT848_RISC_WRITE
>> 28 ] = 2,
3605 [ BT848_RISC_JUMP
>> 28 ] = 2,
3606 [ BT848_RISC_SYNC
>> 28 ] = 2,
3607 [ BT848_RISC_WRITE123
>> 28 ] = 5,
3608 [ BT848_RISC_SKIP123
>> 28 ] = 2,
3609 [ BT848_RISC_WRITE1S23
>> 28 ] = 3,
3611 static char *bits
[] = {
3612 "be0", "be1", "be2", "be3/resync",
3613 "set0", "set1", "set2", "set3",
3614 "clr0", "clr1", "clr2", "clr3",
3615 "irq", "res", "eol", "sol",
3619 printk("0x%08x [ %s", risc
,
3620 instr
[risc
>> 28] ? instr
[risc
>> 28] : "INVALID");
3621 for (i
= ARRAY_SIZE(bits
)-1; i
>= 0; i
--)
3622 if (risc
& (1 << (i
+ 12)))
3623 printk(" %s",bits
[i
]);
3624 printk(" count=%d ]\n", risc
& 0xfff);
3625 return incr
[risc
>> 28] ? incr
[risc
>> 28] : 1;
3628 static void bttv_risc_disasm(struct bttv
*btv
,
3629 struct btcx_riscmem
*risc
)
3633 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3634 btv
->c
.v4l2_dev
.name
, risc
->cpu
, (unsigned long)risc
->dma
);
3635 for (i
= 0; i
< (risc
->size
>> 2); i
+= n
) {
3636 printk("%s: 0x%lx: ", btv
->c
.v4l2_dev
.name
,
3637 (unsigned long)(risc
->dma
+ (i
<<2)));
3638 n
= bttv_risc_decode(le32_to_cpu(risc
->cpu
[i
]));
3639 for (j
= 1; j
< n
; j
++)
3640 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3641 btv
->c
.v4l2_dev
.name
, (unsigned long)(risc
->dma
+ ((i
+j
)<<2)),
3643 if (0 == risc
->cpu
[i
])
3648 static void bttv_print_riscaddr(struct bttv
*btv
)
3650 printk(" main: %08Lx\n",
3651 (unsigned long long)btv
->main
.dma
);
3652 printk(" vbi : o=%08Lx e=%08Lx\n",
3653 btv
->cvbi
? (unsigned long long)btv
->cvbi
->top
.dma
: 0,
3654 btv
->cvbi
? (unsigned long long)btv
->cvbi
->bottom
.dma
: 0);
3655 printk(" cap : o=%08Lx e=%08Lx\n",
3656 btv
->curr
.top
? (unsigned long long)btv
->curr
.top
->top
.dma
: 0,
3657 btv
->curr
.bottom
? (unsigned long long)btv
->curr
.bottom
->bottom
.dma
: 0);
3658 printk(" scr : o=%08Lx e=%08Lx\n",
3659 btv
->screen
? (unsigned long long)btv
->screen
->top
.dma
: 0,
3660 btv
->screen
? (unsigned long long)btv
->screen
->bottom
.dma
: 0);
3661 bttv_risc_disasm(btv
, &btv
->main
);
3664 /* ----------------------------------------------------------------------- */
3667 static char *irq_name
[] = {
3668 "FMTCHG", // format change detected (525 vs. 625)
3669 "VSYNC", // vertical sync (new field)
3670 "HSYNC", // horizontal sync
3671 "OFLOW", // chroma/luma AGC overflow
3672 "HLOCK", // horizontal lock changed
3673 "VPRES", // video presence changed
3675 "I2CDONE", // hw irc operation finished
3676 "GPINT", // gpio port triggered irq
3678 "RISCI", // risc instruction triggered irq
3679 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3680 "FTRGT", // pixel data fifo overrun
3681 "FDSR", // fifo data stream resyncronisation
3682 "PPERR", // parity error (data transfer)
3683 "RIPERR", // parity error (read risc instructions)
3684 "PABORT", // pci abort
3685 "OCERR", // risc instruction error
3686 "SCERR", // syncronisation error
3689 static void bttv_print_irqbits(u32 print
, u32 mark
)
3694 for (i
= 0; i
< ARRAY_SIZE(irq_name
); i
++) {
3695 if (print
& (1 << i
))
3696 printk(" %s",irq_name
[i
]);
3697 if (mark
& (1 << i
))
3702 static void bttv_irq_debug_low_latency(struct bttv
*btv
, u32 rc
)
3704 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3706 (unsigned long)btv
->main
.dma
,
3707 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_VBI
+1]),
3708 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_FIELD
+1]),
3711 if (0 == (btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
)) {
3712 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3713 "Ok, then this is harmless, don't worry ;)\n",
3717 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3719 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3725 bttv_irq_next_video(struct bttv
*btv
, struct bttv_buffer_set
*set
)
3727 struct bttv_buffer
*item
;
3729 memset(set
,0,sizeof(*set
));
3731 /* capture request ? */
3732 if (!list_empty(&btv
->capture
)) {
3734 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3735 if (V4L2_FIELD_HAS_TOP(item
->vb
.field
))
3737 if (V4L2_FIELD_HAS_BOTTOM(item
->vb
.field
))
3740 /* capture request for other field ? */
3741 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
) &&
3742 (item
->vb
.queue
.next
!= &btv
->capture
)) {
3743 item
= list_entry(item
->vb
.queue
.next
, struct bttv_buffer
, vb
.queue
);
3744 /* Mike Isely <isely@pobox.com> - Only check
3745 * and set up the bottom field in the logic
3746 * below. Don't ever do the top field. This
3747 * of course means that if we set up the
3748 * bottom field in the above code that we'll
3749 * actually skip a field. But that's OK.
3750 * Having processed only a single buffer this
3751 * time, then the next time around the first
3752 * available buffer should be for a top field.
3753 * That will then cause us here to set up a
3754 * top then a bottom field in the normal way.
3755 * The alternative to this understanding is
3756 * that we set up the second available buffer
3757 * as a top field, but that's out of order
3758 * since this driver always processes the top
3759 * field first - the effect will be the two
3760 * buffers being returned in the wrong order,
3761 * with the second buffer also being delayed
3762 * by one field time (owing to the fifo nature
3763 * of videobuf). Worse still, we'll be stuck
3764 * doing fields out of order now every time
3765 * until something else causes a field to be
3766 * dropped. By effectively forcing a field to
3767 * drop this way then we always get back into
3768 * sync within a single frame time. (Out of
3769 * order fields can screw up deinterlacing
3771 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
)) {
3772 if (NULL
== set
->bottom
&&
3773 V4L2_FIELD_BOTTOM
== item
->vb
.field
) {
3776 if (NULL
!= set
->top
&& NULL
!= set
->bottom
)
3782 /* screen overlay ? */
3783 if (NULL
!= btv
->screen
) {
3784 if (V4L2_FIELD_HAS_BOTH(btv
->screen
->vb
.field
)) {
3785 if (NULL
== set
->top
&& NULL
== set
->bottom
) {
3786 set
->top
= btv
->screen
;
3787 set
->bottom
= btv
->screen
;
3790 if (V4L2_FIELD_TOP
== btv
->screen
->vb
.field
&&
3792 set
->top
= btv
->screen
;
3794 if (V4L2_FIELD_BOTTOM
== btv
->screen
->vb
.field
&&
3795 NULL
== set
->bottom
) {
3796 set
->bottom
= btv
->screen
;
3801 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3802 btv
->c
.nr
,set
->top
, set
->bottom
,
3803 btv
->screen
,set
->frame_irq
,set
->top_irq
);
3808 bttv_irq_wakeup_video(struct bttv
*btv
, struct bttv_buffer_set
*wakeup
,
3809 struct bttv_buffer_set
*curr
, unsigned int state
)
3813 do_gettimeofday(&ts
);
3815 if (wakeup
->top
== wakeup
->bottom
) {
3816 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3818 printk("bttv%d: wakeup: both=%p\n",btv
->c
.nr
,wakeup
->top
);
3819 wakeup
->top
->vb
.ts
= ts
;
3820 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3821 wakeup
->top
->vb
.state
= state
;
3822 wake_up(&wakeup
->top
->vb
.done
);
3825 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3827 printk("bttv%d: wakeup: top=%p\n",btv
->c
.nr
,wakeup
->top
);
3828 wakeup
->top
->vb
.ts
= ts
;
3829 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3830 wakeup
->top
->vb
.state
= state
;
3831 wake_up(&wakeup
->top
->vb
.done
);
3833 if (NULL
!= wakeup
->bottom
&& curr
->bottom
!= wakeup
->bottom
) {
3835 printk("bttv%d: wakeup: bottom=%p\n",btv
->c
.nr
,wakeup
->bottom
);
3836 wakeup
->bottom
->vb
.ts
= ts
;
3837 wakeup
->bottom
->vb
.field_count
= btv
->field_count
;
3838 wakeup
->bottom
->vb
.state
= state
;
3839 wake_up(&wakeup
->bottom
->vb
.done
);
3845 bttv_irq_wakeup_vbi(struct bttv
*btv
, struct bttv_buffer
*wakeup
,
3853 do_gettimeofday(&ts
);
3855 wakeup
->vb
.field_count
= btv
->field_count
;
3856 wakeup
->vb
.state
= state
;
3857 wake_up(&wakeup
->vb
.done
);
3860 static void bttv_irq_timeout(unsigned long data
)
3862 struct bttv
*btv
= (struct bttv
*)data
;
3863 struct bttv_buffer_set old
,new;
3864 struct bttv_buffer
*ovbi
;
3865 struct bttv_buffer
*item
;
3866 unsigned long flags
;
3869 printk(KERN_INFO
"bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3870 btv
->c
.nr
, btv
->framedrop
, btv
->irq_me
, btv
->irq_total
,
3871 btread(BT848_RISC_COUNT
));
3872 bttv_print_irqbits(btread(BT848_INT_STAT
),0);
3876 spin_lock_irqsave(&btv
->s_lock
,flags
);
3878 /* deactivate stuff */
3879 memset(&new,0,sizeof(new));
3885 bttv_buffer_activate_video(btv
, &new);
3886 bttv_buffer_activate_vbi(btv
, NULL
);
3887 bttv_set_dma(btv
, 0);
3890 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_ERROR
);
3891 bttv_irq_wakeup_vbi(btv
, ovbi
, VIDEOBUF_ERROR
);
3893 /* cancel all outstanding capture / vbi requests */
3894 while (!list_empty(&btv
->capture
)) {
3895 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3896 list_del(&item
->vb
.queue
);
3897 item
->vb
.state
= VIDEOBUF_ERROR
;
3898 wake_up(&item
->vb
.done
);
3900 while (!list_empty(&btv
->vcapture
)) {
3901 item
= list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3902 list_del(&item
->vb
.queue
);
3903 item
->vb
.state
= VIDEOBUF_ERROR
;
3904 wake_up(&item
->vb
.done
);
3908 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
3912 bttv_irq_wakeup_top(struct bttv
*btv
)
3914 struct bttv_buffer
*wakeup
= btv
->curr
.top
;
3919 spin_lock(&btv
->s_lock
);
3920 btv
->curr
.top_irq
= 0;
3921 btv
->curr
.top
= NULL
;
3922 bttv_risc_hook(btv
, RISC_SLOT_O_FIELD
, NULL
, 0);
3924 do_gettimeofday(&wakeup
->vb
.ts
);
3925 wakeup
->vb
.field_count
= btv
->field_count
;
3926 wakeup
->vb
.state
= VIDEOBUF_DONE
;
3927 wake_up(&wakeup
->vb
.done
);
3928 spin_unlock(&btv
->s_lock
);
3931 static inline int is_active(struct btcx_riscmem
*risc
, u32 rc
)
3935 if (rc
> risc
->dma
+ risc
->size
)
3941 bttv_irq_switch_video(struct bttv
*btv
)
3943 struct bttv_buffer_set
new;
3944 struct bttv_buffer_set old
;
3947 spin_lock(&btv
->s_lock
);
3949 /* new buffer set */
3950 bttv_irq_next_video(btv
, &new);
3951 rc
= btread(BT848_RISC_COUNT
);
3952 if ((btv
->curr
.top
&& is_active(&btv
->curr
.top
->top
, rc
)) ||
3953 (btv
->curr
.bottom
&& is_active(&btv
->curr
.bottom
->bottom
, rc
))) {
3956 bttv_irq_debug_low_latency(btv
, rc
);
3957 spin_unlock(&btv
->s_lock
);
3964 btv
->loop_irq
&= ~1;
3965 bttv_buffer_activate_video(btv
, &new);
3966 bttv_set_dma(btv
, 0);
3969 if (UNSET
!= btv
->new_input
) {
3970 video_mux(btv
,btv
->new_input
);
3971 btv
->new_input
= UNSET
;
3974 /* wake up finished buffers */
3975 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_DONE
);
3976 spin_unlock(&btv
->s_lock
);
3980 bttv_irq_switch_vbi(struct bttv
*btv
)
3982 struct bttv_buffer
*new = NULL
;
3983 struct bttv_buffer
*old
;
3986 spin_lock(&btv
->s_lock
);
3988 if (!list_empty(&btv
->vcapture
))
3989 new = list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3992 rc
= btread(BT848_RISC_COUNT
);
3993 if (NULL
!= old
&& (is_active(&old
->top
, rc
) ||
3994 is_active(&old
->bottom
, rc
))) {
3997 bttv_irq_debug_low_latency(btv
, rc
);
3998 spin_unlock(&btv
->s_lock
);
4004 btv
->loop_irq
&= ~4;
4005 bttv_buffer_activate_vbi(btv
, new);
4006 bttv_set_dma(btv
, 0);
4008 bttv_irq_wakeup_vbi(btv
, old
, VIDEOBUF_DONE
);
4009 spin_unlock(&btv
->s_lock
);
4012 static irqreturn_t
bttv_irq(int irq
, void *dev_id
)
4020 btv
=(struct bttv
*)dev_id
;
4024 /* get/clear interrupt status bits */
4025 stat
=btread(BT848_INT_STAT
);
4026 astat
=stat
&btread(BT848_INT_MASK
);
4030 btwrite(stat
,BT848_INT_STAT
);
4032 /* get device status bits */
4033 dstat
=btread(BT848_DSTATUS
);
4036 printk(KERN_DEBUG
"bttv%d: irq loop=%d fc=%d "
4037 "riscs=%x, riscc=%08x, ",
4038 btv
->c
.nr
, count
, btv
->field_count
,
4039 stat
>>28, btread(BT848_RISC_COUNT
));
4040 bttv_print_irqbits(stat
,astat
);
4041 if (stat
& BT848_INT_HLOCK
)
4042 printk(" HLOC => %s", (dstat
& BT848_DSTATUS_HLOC
)
4044 if (stat
& BT848_INT_VPRES
)
4045 printk(" PRES => %s", (dstat
& BT848_DSTATUS_PRES
)
4047 if (stat
& BT848_INT_FMTCHG
)
4048 printk(" NUML => %s", (dstat
& BT848_DSTATUS_NUML
)
4053 if (astat
&BT848_INT_VSYNC
)
4056 if ((astat
& BT848_INT_GPINT
) && btv
->remote
) {
4057 bttv_input_irq(btv
);
4060 if (astat
& BT848_INT_I2CDONE
) {
4061 btv
->i2c_done
= stat
;
4062 wake_up(&btv
->i2c_queue
);
4065 if ((astat
& BT848_INT_RISCI
) && (stat
& (4<<28)))
4066 bttv_irq_switch_vbi(btv
);
4068 if ((astat
& BT848_INT_RISCI
) && (stat
& (2<<28)))
4069 bttv_irq_wakeup_top(btv
);
4071 if ((astat
& BT848_INT_RISCI
) && (stat
& (1<<28)))
4072 bttv_irq_switch_video(btv
);
4074 if ((astat
& BT848_INT_HLOCK
) && btv
->opt_automute
)
4075 audio_mute(btv
, btv
->mute
); /* trigger automute */
4077 if (astat
& (BT848_INT_SCERR
|BT848_INT_OCERR
)) {
4078 printk(KERN_INFO
"bttv%d: %s%s @ %08x,",btv
->c
.nr
,
4079 (astat
& BT848_INT_SCERR
) ? "SCERR" : "",
4080 (astat
& BT848_INT_OCERR
) ? "OCERR" : "",
4081 btread(BT848_RISC_COUNT
));
4082 bttv_print_irqbits(stat
,astat
);
4085 bttv_print_riscaddr(btv
);
4087 if (fdsr
&& astat
& BT848_INT_FDSR
) {
4088 printk(KERN_INFO
"bttv%d: FDSR @ %08x\n",
4089 btv
->c
.nr
,btread(BT848_RISC_COUNT
));
4091 bttv_print_riscaddr(btv
);
4097 if (count
> 8 || !(astat
& BT848_INT_GPINT
)) {
4098 btwrite(0, BT848_INT_MASK
);
4101 "bttv%d: IRQ lockup, cleared int mask [", btv
->c
.nr
);
4104 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv
->c
.nr
);
4106 btwrite(btread(BT848_INT_MASK
) & (-1 ^ BT848_INT_GPINT
),
4110 bttv_print_irqbits(stat
,astat
);
4118 return IRQ_RETVAL(handled
);
4122 /* ----------------------------------------------------------------------- */
4123 /* initialitation */
4125 static struct video_device
*vdev_init(struct bttv
*btv
,
4126 const struct video_device
*template,
4127 const char *type_name
)
4129 struct video_device
*vfd
;
4131 vfd
= video_device_alloc();
4135 vfd
->v4l2_dev
= &btv
->c
.v4l2_dev
;
4136 vfd
->release
= video_device_release
;
4137 vfd
->debug
= bttv_debug
;
4138 video_set_drvdata(vfd
, btv
);
4139 snprintf(vfd
->name
, sizeof(vfd
->name
), "BT%d%s %s (%s)",
4140 btv
->id
, (btv
->id
==848 && btv
->revision
==0x12) ? "A" : "",
4141 type_name
, bttv_tvcards
[btv
->c
.type
].name
);
4145 static void bttv_unregister_video(struct bttv
*btv
)
4147 if (btv
->video_dev
) {
4148 if (video_is_registered(btv
->video_dev
))
4149 video_unregister_device(btv
->video_dev
);
4151 video_device_release(btv
->video_dev
);
4152 btv
->video_dev
= NULL
;
4155 if (video_is_registered(btv
->vbi_dev
))
4156 video_unregister_device(btv
->vbi_dev
);
4158 video_device_release(btv
->vbi_dev
);
4159 btv
->vbi_dev
= NULL
;
4161 if (btv
->radio_dev
) {
4162 if (video_is_registered(btv
->radio_dev
))
4163 video_unregister_device(btv
->radio_dev
);
4165 video_device_release(btv
->radio_dev
);
4166 btv
->radio_dev
= NULL
;
4170 /* register video4linux devices */
4171 static int __devinit
bttv_register_video(struct bttv
*btv
)
4174 printk("bttv: Overlay support disabled.\n");
4177 btv
->video_dev
= vdev_init(btv
, &bttv_video_template
, "video");
4179 if (NULL
== btv
->video_dev
)
4181 if (video_register_device(btv
->video_dev
, VFL_TYPE_GRABBER
,
4182 video_nr
[btv
->c
.nr
]) < 0)
4184 printk(KERN_INFO
"bttv%d: registered device %s\n",
4185 btv
->c
.nr
, video_device_node_name(btv
->video_dev
));
4186 if (device_create_file(&btv
->video_dev
->dev
,
4187 &dev_attr_card
)<0) {
4188 printk(KERN_ERR
"bttv%d: device_create_file 'card' "
4189 "failed\n", btv
->c
.nr
);
4194 btv
->vbi_dev
= vdev_init(btv
, &bttv_video_template
, "vbi");
4196 if (NULL
== btv
->vbi_dev
)
4198 if (video_register_device(btv
->vbi_dev
, VFL_TYPE_VBI
,
4199 vbi_nr
[btv
->c
.nr
]) < 0)
4201 printk(KERN_INFO
"bttv%d: registered device %s\n",
4202 btv
->c
.nr
, video_device_node_name(btv
->vbi_dev
));
4204 if (!btv
->has_radio
)
4207 btv
->radio_dev
= vdev_init(btv
, &radio_template
, "radio");
4208 if (NULL
== btv
->radio_dev
)
4210 if (video_register_device(btv
->radio_dev
, VFL_TYPE_RADIO
,
4211 radio_nr
[btv
->c
.nr
]) < 0)
4213 printk(KERN_INFO
"bttv%d: registered device %s\n",
4214 btv
->c
.nr
, video_device_node_name(btv
->radio_dev
));
4220 bttv_unregister_video(btv
);
4225 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4226 /* response on cards with no firmware is not enabled by OF */
4227 static void pci_set_command(struct pci_dev
*dev
)
4229 #if defined(__powerpc__)
4232 pci_read_config_dword(dev
, PCI_COMMAND
, &cmd
);
4233 cmd
= (cmd
| PCI_COMMAND_MEMORY
);
4234 pci_write_config_dword(dev
, PCI_COMMAND
, cmd
);
4238 static int __devinit
bttv_probe(struct pci_dev
*dev
,
4239 const struct pci_device_id
*pci_id
)
4245 if (bttv_num
== BTTV_MAX
)
4247 printk(KERN_INFO
"bttv: Bt8xx card found (%d).\n", bttv_num
);
4248 bttvs
[bttv_num
] = btv
= kzalloc(sizeof(*btv
), GFP_KERNEL
);
4250 printk(KERN_ERR
"bttv: out of memory.\n");
4253 btv
->c
.nr
= bttv_num
;
4254 snprintf(btv
->c
.v4l2_dev
.name
, sizeof(btv
->c
.v4l2_dev
.name
),
4255 "bttv%d", btv
->c
.nr
);
4257 /* initialize structs / fill in defaults */
4258 mutex_init(&btv
->lock
);
4259 spin_lock_init(&btv
->s_lock
);
4260 spin_lock_init(&btv
->gpio_lock
);
4261 init_waitqueue_head(&btv
->i2c_queue
);
4262 INIT_LIST_HEAD(&btv
->c
.subs
);
4263 INIT_LIST_HEAD(&btv
->capture
);
4264 INIT_LIST_HEAD(&btv
->vcapture
);
4265 v4l2_prio_init(&btv
->prio
);
4267 init_timer(&btv
->timeout
);
4268 btv
->timeout
.function
= bttv_irq_timeout
;
4269 btv
->timeout
.data
= (unsigned long)btv
;
4272 btv
->tuner_type
= UNSET
;
4273 btv
->new_input
= UNSET
;
4274 btv
->has_radio
=radio
[btv
->c
.nr
];
4276 /* pci stuff (init, get irq/mmio, ... */
4278 btv
->id
= dev
->device
;
4279 if (pci_enable_device(dev
)) {
4280 printk(KERN_WARNING
"bttv%d: Can't enable device.\n",
4284 if (pci_set_dma_mask(dev
, DMA_BIT_MASK(32))) {
4285 printk(KERN_WARNING
"bttv%d: No suitable DMA available.\n",
4289 if (!request_mem_region(pci_resource_start(dev
,0),
4290 pci_resource_len(dev
,0),
4291 btv
->c
.v4l2_dev
.name
)) {
4292 printk(KERN_WARNING
"bttv%d: can't request iomem (0x%llx).\n",
4294 (unsigned long long)pci_resource_start(dev
,0));
4297 pci_set_master(dev
);
4298 pci_set_command(dev
);
4300 result
= v4l2_device_register(&dev
->dev
, &btv
->c
.v4l2_dev
);
4302 printk(KERN_WARNING
"bttv%d: v4l2_device_register() failed\n", btv
->c
.nr
);
4306 pci_read_config_byte(dev
, PCI_CLASS_REVISION
, &btv
->revision
);
4307 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
4308 printk(KERN_INFO
"bttv%d: Bt%d (rev %d) at %s, ",
4309 bttv_num
,btv
->id
, btv
->revision
, pci_name(dev
));
4310 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4311 btv
->c
.pci
->irq
, lat
,
4312 (unsigned long long)pci_resource_start(dev
,0));
4315 btv
->bt848_mmio
= ioremap(pci_resource_start(dev
, 0), 0x1000);
4316 if (NULL
== btv
->bt848_mmio
) {
4317 printk("bttv%d: ioremap() failed\n", btv
->c
.nr
);
4325 /* disable irqs, register irq handler */
4326 btwrite(0, BT848_INT_MASK
);
4327 result
= request_irq(btv
->c
.pci
->irq
, bttv_irq
,
4328 IRQF_SHARED
| IRQF_DISABLED
, btv
->c
.v4l2_dev
.name
, (void *)btv
);
4330 printk(KERN_ERR
"bttv%d: can't get IRQ %d\n",
4331 bttv_num
,btv
->c
.pci
->irq
);
4335 if (0 != bttv_handle_chipset(btv
)) {
4340 /* init options from insmod args */
4341 btv
->opt_combfilter
= combfilter
;
4342 btv
->opt_lumafilter
= lumafilter
;
4343 btv
->opt_automute
= automute
;
4344 btv
->opt_chroma_agc
= chroma_agc
;
4345 btv
->opt_adc_crush
= adc_crush
;
4346 btv
->opt_vcr_hack
= vcr_hack
;
4347 btv
->opt_whitecrush_upper
= whitecrush_upper
;
4348 btv
->opt_whitecrush_lower
= whitecrush_lower
;
4349 btv
->opt_uv_ratio
= uv_ratio
;
4350 btv
->opt_full_luma_range
= full_luma_range
;
4351 btv
->opt_coring
= coring
;
4353 /* fill struct bttv with some useful defaults */
4354 btv
->init
.btv
= btv
;
4355 btv
->init
.ov
.w
.width
= 320;
4356 btv
->init
.ov
.w
.height
= 240;
4357 btv
->init
.fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
4358 btv
->init
.width
= 320;
4359 btv
->init
.height
= 240;
4362 /* initialize hardware */
4364 bttv_gpio_tracking(btv
,"pre-init");
4366 bttv_risc_init_main(btv
);
4370 btwrite(0x00, BT848_GPIO_REG_INP
);
4371 btwrite(0x00, BT848_GPIO_OUT_EN
);
4373 bttv_gpio_tracking(btv
,"init");
4375 /* needs to be done before i2c is registered */
4376 bttv_init_card1(btv
);
4378 /* register i2c + gpio */
4381 /* some card-specific stuff (needs working i2c) */
4382 bttv_init_card2(btv
);
4383 bttv_init_tuner(btv
);
4386 /* register video4linux + input */
4387 if (!bttv_tvcards
[btv
->c
.type
].no_video
) {
4388 bttv_register_video(btv
);
4389 bt848_bright(btv
,32768);
4390 bt848_contrast(btv
,32768);
4391 bt848_hue(btv
,32768);
4392 bt848_sat(btv
,32768);
4394 set_input(btv
, 0, btv
->tvnorm
);
4395 bttv_crop_reset(&btv
->crop
[0], btv
->tvnorm
);
4396 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
4397 disclaim_vbi_lines(btv
);
4398 disclaim_video_lines(btv
);
4401 /* add subdevices and autoload dvb-bt8xx if needed */
4402 if (bttv_tvcards
[btv
->c
.type
].has_dvb
) {
4403 bttv_sub_add_device(&btv
->c
, "dvb");
4404 request_modules(btv
);
4408 init_bttv_i2c_ir(btv
);
4409 bttv_input_init(btv
);
4412 /* everything is fine */
4417 free_irq(btv
->c
.pci
->irq
,btv
);
4420 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4423 if (btv
->bt848_mmio
)
4424 iounmap(btv
->bt848_mmio
);
4425 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4426 pci_resource_len(btv
->c
.pci
,0));
4430 static void __devexit
bttv_remove(struct pci_dev
*pci_dev
)
4432 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4433 struct bttv
*btv
= to_bttv(v4l2_dev
);
4436 printk("bttv%d: unloading\n",btv
->c
.nr
);
4438 if (bttv_tvcards
[btv
->c
.type
].has_dvb
)
4439 flush_request_modules(btv
);
4441 /* shutdown everything (DMA+IRQs) */
4442 btand(~15, BT848_GPIO_DMA_CTL
);
4443 btwrite(0, BT848_INT_MASK
);
4444 btwrite(~0x0, BT848_INT_STAT
);
4445 btwrite(0x0, BT848_GPIO_OUT_EN
);
4447 bttv_gpio_tracking(btv
,"cleanup");
4449 /* tell gpio modules we are leaving ... */
4451 bttv_input_fini(btv
);
4452 bttv_sub_del_devices(&btv
->c
);
4454 /* unregister i2c_bus + input */
4457 /* unregister video4linux */
4458 bttv_unregister_video(btv
);
4460 /* free allocated memory */
4461 btcx_riscmem_free(btv
->c
.pci
,&btv
->main
);
4463 /* free ressources */
4464 free_irq(btv
->c
.pci
->irq
,btv
);
4465 iounmap(btv
->bt848_mmio
);
4466 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4467 pci_resource_len(btv
->c
.pci
,0));
4469 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4470 bttvs
[btv
->c
.nr
] = NULL
;
4477 static int bttv_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
4479 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4480 struct bttv
*btv
= to_bttv(v4l2_dev
);
4481 struct bttv_buffer_set idle
;
4482 unsigned long flags
;
4484 dprintk("bttv%d: suspend %d\n", btv
->c
.nr
, state
.event
);
4486 /* stop dma + irqs */
4487 spin_lock_irqsave(&btv
->s_lock
,flags
);
4488 memset(&idle
, 0, sizeof(idle
));
4489 btv
->state
.video
= btv
->curr
;
4490 btv
->state
.vbi
= btv
->cvbi
;
4491 btv
->state
.loop_irq
= btv
->loop_irq
;
4494 bttv_buffer_activate_video(btv
, &idle
);
4495 bttv_buffer_activate_vbi(btv
, NULL
);
4496 bttv_set_dma(btv
, 0);
4497 btwrite(0, BT848_INT_MASK
);
4498 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4500 /* save bt878 state */
4501 btv
->state
.gpio_enable
= btread(BT848_GPIO_OUT_EN
);
4502 btv
->state
.gpio_data
= gpio_read();
4504 /* save pci state */
4505 pci_save_state(pci_dev
);
4506 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
4507 pci_disable_device(pci_dev
);
4508 btv
->state
.disabled
= 1;
4513 static int bttv_resume(struct pci_dev
*pci_dev
)
4515 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4516 struct bttv
*btv
= to_bttv(v4l2_dev
);
4517 unsigned long flags
;
4520 dprintk("bttv%d: resume\n", btv
->c
.nr
);
4522 /* restore pci state */
4523 if (btv
->state
.disabled
) {
4524 err
=pci_enable_device(pci_dev
);
4526 printk(KERN_WARNING
"bttv%d: Can't enable device.\n",
4530 btv
->state
.disabled
= 0;
4532 err
=pci_set_power_state(pci_dev
, PCI_D0
);
4534 pci_disable_device(pci_dev
);
4535 printk(KERN_WARNING
"bttv%d: Can't enable device.\n",
4537 btv
->state
.disabled
= 1;
4541 pci_restore_state(pci_dev
);
4543 /* restore bt878 state */
4544 bttv_reinit_bt848(btv
);
4545 gpio_inout(0xffffff, btv
->state
.gpio_enable
);
4546 gpio_write(btv
->state
.gpio_data
);
4549 spin_lock_irqsave(&btv
->s_lock
,flags
);
4550 btv
->curr
= btv
->state
.video
;
4551 btv
->cvbi
= btv
->state
.vbi
;
4552 btv
->loop_irq
= btv
->state
.loop_irq
;
4553 bttv_buffer_activate_video(btv
, &btv
->curr
);
4554 bttv_buffer_activate_vbi(btv
, btv
->cvbi
);
4555 bttv_set_dma(btv
, 0);
4556 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4561 static struct pci_device_id bttv_pci_tbl
[] = {
4562 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT848
), 0},
4563 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT849
), 0},
4564 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT878
), 0},
4565 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT879
), 0},
4569 MODULE_DEVICE_TABLE(pci
, bttv_pci_tbl
);
4571 static struct pci_driver bttv_pci_driver
= {
4573 .id_table
= bttv_pci_tbl
,
4574 .probe
= bttv_probe
,
4575 .remove
= __devexit_p(bttv_remove
),
4577 .suspend
= bttv_suspend
,
4578 .resume
= bttv_resume
,
4582 static int __init
bttv_init_module(void)
4588 printk(KERN_INFO
"bttv: driver version %d.%d.%d loaded\n",
4589 (BTTV_VERSION_CODE
>> 16) & 0xff,
4590 (BTTV_VERSION_CODE
>> 8) & 0xff,
4591 BTTV_VERSION_CODE
& 0xff);
4593 printk(KERN_INFO
"bttv: snapshot date %04d-%02d-%02d\n",
4594 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
4596 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
4598 if (gbufsize
> BTTV_MAX_FBUF
)
4599 gbufsize
= BTTV_MAX_FBUF
;
4600 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
4602 printk(KERN_INFO
"bttv: using %d buffers with %dk (%d pages) each for capture\n",
4603 gbuffers
, gbufsize
>> 10, gbufsize
>> PAGE_SHIFT
);
4605 bttv_check_chipset();
4607 ret
= bus_register(&bttv_sub_bus_type
);
4609 printk(KERN_WARNING
"bttv: bus_register error: %d\n", ret
);
4612 ret
= pci_register_driver(&bttv_pci_driver
);
4614 bus_unregister(&bttv_sub_bus_type
);
4619 static void __exit
bttv_cleanup_module(void)
4621 pci_unregister_driver(&bttv_pci_driver
);
4622 bus_unregister(&bttv_sub_bus_type
);
4625 module_init(bttv_init_module
);
4626 module_exit(bttv_cleanup_module
);