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/smp_lock.h>
46 #include <linux/interrupt.h>
47 #include <linux/kdev_t.h>
49 #include <media/v4l2-common.h>
50 #include <media/v4l2-ioctl.h>
51 #include <media/tvaudio.h>
52 #include <media/msp3400.h>
54 #include <linux/dma-mapping.h>
57 #include <asm/byteorder.h>
59 #include <media/rds.h>
62 unsigned int bttv_num
; /* number of Bt848s in use */
63 struct bttv
*bttvs
[BTTV_MAX
];
65 unsigned int bttv_debug
;
66 unsigned int bttv_verbose
= 1;
67 unsigned int bttv_gpio
;
69 /* config variables */
71 static unsigned int bigendian
=1;
73 static unsigned int bigendian
;
75 static unsigned int radio
[BTTV_MAX
];
76 static unsigned int irq_debug
;
77 static unsigned int gbuffers
= 8;
78 static unsigned int gbufsize
= 0x208000;
79 static unsigned int reset_crop
= 1;
81 static int video_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
82 static int radio_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
83 static int vbi_nr
[BTTV_MAX
] = { [0 ... (BTTV_MAX
-1)] = -1 };
84 static int debug_latency
;
85 static int disable_ir
;
87 static unsigned int fdsr
;
90 static unsigned int combfilter
;
91 static unsigned int lumafilter
;
92 static unsigned int automute
= 1;
93 static unsigned int chroma_agc
;
94 static unsigned int adc_crush
= 1;
95 static unsigned int whitecrush_upper
= 0xCF;
96 static unsigned int whitecrush_lower
= 0x7F;
97 static unsigned int vcr_hack
;
98 static unsigned int irq_iswitch
;
99 static unsigned int uv_ratio
= 50;
100 static unsigned int full_luma_range
;
101 static unsigned int coring
;
103 /* API features (turn on/off stuff for testing) */
104 static unsigned int v4l2
= 1;
107 module_param(bttv_verbose
, int, 0644);
108 module_param(bttv_gpio
, int, 0644);
109 module_param(bttv_debug
, int, 0644);
110 module_param(irq_debug
, int, 0644);
111 module_param(debug_latency
, int, 0644);
112 module_param(disable_ir
, int, 0444);
114 module_param(fdsr
, int, 0444);
115 module_param(gbuffers
, int, 0444);
116 module_param(gbufsize
, int, 0444);
117 module_param(reset_crop
, int, 0444);
119 module_param(v4l2
, int, 0644);
120 module_param(bigendian
, int, 0644);
121 module_param(irq_iswitch
, int, 0644);
122 module_param(combfilter
, int, 0444);
123 module_param(lumafilter
, int, 0444);
124 module_param(automute
, int, 0444);
125 module_param(chroma_agc
, int, 0444);
126 module_param(adc_crush
, int, 0444);
127 module_param(whitecrush_upper
, int, 0444);
128 module_param(whitecrush_lower
, int, 0444);
129 module_param(vcr_hack
, int, 0444);
130 module_param(uv_ratio
, int, 0444);
131 module_param(full_luma_range
, int, 0444);
132 module_param(coring
, int, 0444);
134 module_param_array(radio
, int, NULL
, 0444);
135 module_param_array(video_nr
, int, NULL
, 0444);
136 module_param_array(radio_nr
, int, NULL
, 0444);
137 module_param_array(vbi_nr
, int, NULL
, 0444);
139 MODULE_PARM_DESC(radio
,"The TV card supports radio, default is 0 (no)");
140 MODULE_PARM_DESC(bigendian
,"byte order of the framebuffer, default is native endian");
141 MODULE_PARM_DESC(bttv_verbose
,"verbose startup messages, default is 1 (yes)");
142 MODULE_PARM_DESC(bttv_gpio
,"log gpio changes, default is 0 (no)");
143 MODULE_PARM_DESC(bttv_debug
,"debug messages, default is 0 (no)");
144 MODULE_PARM_DESC(irq_debug
,"irq handler debug messages, default is 0 (no)");
145 MODULE_PARM_DESC(disable_ir
, "disable infrared remote support");
146 MODULE_PARM_DESC(gbuffers
,"number of capture buffers. range 2-32, default 8");
147 MODULE_PARM_DESC(gbufsize
,"size of the capture buffers, default is 0x208000");
148 MODULE_PARM_DESC(reset_crop
,"reset cropping parameters at open(), default "
149 "is 1 (yes) for compatibility with older applications");
150 MODULE_PARM_DESC(automute
,"mute audio on bad/missing video signal, default is 1 (yes)");
151 MODULE_PARM_DESC(chroma_agc
,"enables the AGC of chroma signal, default is 0 (no)");
152 MODULE_PARM_DESC(adc_crush
,"enables the luminance ADC crush, default is 1 (yes)");
153 MODULE_PARM_DESC(whitecrush_upper
,"sets the white crush upper value, default is 207");
154 MODULE_PARM_DESC(whitecrush_lower
,"sets the white crush lower value, default is 127");
155 MODULE_PARM_DESC(vcr_hack
,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
156 MODULE_PARM_DESC(irq_iswitch
,"switch inputs in irq handler");
157 MODULE_PARM_DESC(uv_ratio
,"ratio between u and v gains, default is 50");
158 MODULE_PARM_DESC(full_luma_range
,"use the full luma range, default is 0 (no)");
159 MODULE_PARM_DESC(coring
,"set the luma coring level, default is 0 (no)");
160 MODULE_PARM_DESC(video_nr
, "video device numbers");
161 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
162 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
164 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
165 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
166 MODULE_LICENSE("GPL");
168 /* ----------------------------------------------------------------------- */
171 static ssize_t
show_card(struct device
*cd
,
172 struct device_attribute
*attr
, char *buf
)
174 struct video_device
*vfd
= container_of(cd
, struct video_device
, dev
);
175 struct bttv
*btv
= video_get_drvdata(vfd
);
176 return sprintf(buf
, "%d\n", btv
? btv
->c
.type
: UNSET
);
178 static DEVICE_ATTR(card
, S_IRUGO
, show_card
, NULL
);
180 /* ----------------------------------------------------------------------- */
181 /* dvb auto-load setup */
182 #if defined(CONFIG_MODULES) && defined(MODULE)
183 static void request_module_async(struct work_struct
*work
)
185 request_module("dvb-bt8xx");
188 static void request_modules(struct bttv
*dev
)
190 INIT_WORK(&dev
->request_module_wk
, request_module_async
);
191 schedule_work(&dev
->request_module_wk
);
194 #define request_modules(dev)
195 #endif /* CONFIG_MODULES */
198 /* ----------------------------------------------------------------------- */
201 /* special timing tables from conexant... */
202 static u8 SRAM_Table
[][60] =
204 /* PAL digital input over GPIO[7:0] */
206 45, // 45 bytes following
207 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
208 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
209 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
210 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
211 0x37,0x00,0xAF,0x21,0x00
213 /* NTSC digital input over GPIO[7:0] */
215 51, // 51 bytes following
216 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
217 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
218 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
219 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
220 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
223 // TGB_NTSC392 // quartzsight
224 // This table has been modified to be used for Fusion Rev D
226 0x2A, // size of table = 42
227 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
228 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
229 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
230 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
235 /* minhdelayx1 first video pixel we can capture on a line and
236 hdelayx1 start of active video, both relative to rising edge of
237 /HRESET pulse (0H) in 1 / fCLKx1.
238 swidth width of active video and
239 totalwidth total line width, both in 1 / fCLKx1.
240 sqwidth total line width in square pixels.
241 vdelay start of active video in 2 * field lines relative to
242 trailing edge of /VRESET pulse (VDELAY register).
243 sheight height of active video in 2 * field lines.
244 videostart0 ITU-R frame line number of the line corresponding
245 to vdelay in the first field. */
246 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
247 vdelay, sheight, videostart0) \
248 .cropcap.bounds.left = minhdelayx1, \
249 /* * 2 because vertically we count field lines times two, */ \
250 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
251 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
252 /* 4 is a safety margin at the end of the line. */ \
253 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
254 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
255 .cropcap.defrect.left = hdelayx1, \
256 .cropcap.defrect.top = (videostart0) * 2, \
257 .cropcap.defrect.width = swidth, \
258 .cropcap.defrect.height = sheight, \
259 .cropcap.pixelaspect.numerator = totalwidth, \
260 .cropcap.pixelaspect.denominator = sqwidth,
262 const struct bttv_tvnorm bttv_tvnorms
[] = {
264 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
265 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
267 .v4l2_id
= V4L2_STD_PAL
,
275 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
276 .scaledtwidth
= 1135,
280 .vbipack
= 255, /* min (2048 / 4, 0x1ff) & 0xff */
282 /* ITU-R frame line number of the first VBI line
283 we can capture, of the first and second field.
284 The last line is determined by cropcap.bounds. */
285 .vbistart
= { 7, 320 },
286 CROPCAP(/* minhdelayx1 */ 68,
288 /* Should be (768 * 1135 + 944 / 2) / 944.
289 cropcap.defrect is used for image width
290 checks, so we keep the old value 924. */
292 /* totalwidth */ 1135,
296 /* videostart0 */ 23)
297 /* bt878 (and bt848?) can capture another
298 line below active video. */
299 .cropcap
.bounds
.height
= (576 + 2) + 0x20 - 2,
301 .v4l2_id
= V4L2_STD_NTSC_M
| V4L2_STD_NTSC_M_KR
,
309 .iform
= (BT848_IFORM_NTSC
|BT848_IFORM_XT0
),
314 .vbipack
= 144, /* min (1600 / 4, 0x1ff) & 0xff */
316 .vbistart
= { 10, 273 },
317 CROPCAP(/* minhdelayx1 */ 68,
319 /* Should be (640 * 910 + 780 / 2) / 780? */
321 /* totalwidth */ 910,
325 /* videostart0 */ 23)
327 .v4l2_id
= V4L2_STD_SECAM
,
335 .iform
= (BT848_IFORM_SECAM
|BT848_IFORM_XT1
),
336 .scaledtwidth
= 1135,
341 .sram
= 0, /* like PAL, correct? */
342 .vbistart
= { 7, 320 },
343 CROPCAP(/* minhdelayx1 */ 68,
346 /* totalwidth */ 1135,
350 /* videostart0 */ 23)
352 .v4l2_id
= V4L2_STD_PAL_Nc
,
360 .iform
= (BT848_IFORM_PAL_NC
|BT848_IFORM_XT0
),
367 .vbistart
= { 7, 320 },
368 CROPCAP(/* minhdelayx1 */ 68,
370 /* swidth */ (640 * 910 + 780 / 2) / 780,
371 /* totalwidth */ 910,
375 /* videostart0 */ 23)
377 .v4l2_id
= V4L2_STD_PAL_M
,
385 .iform
= (BT848_IFORM_PAL_M
|BT848_IFORM_XT0
),
392 .vbistart
= { 10, 273 },
393 CROPCAP(/* minhdelayx1 */ 68,
395 /* swidth */ (640 * 910 + 780 / 2) / 780,
396 /* totalwidth */ 910,
400 /* videostart0 */ 23)
402 .v4l2_id
= V4L2_STD_PAL_N
,
410 .iform
= (BT848_IFORM_PAL_N
|BT848_IFORM_XT1
),
417 .vbistart
= { 7, 320 },
418 CROPCAP(/* minhdelayx1 */ 68,
420 /* swidth */ (768 * 1135 + 944 / 2) / 944,
421 /* totalwidth */ 1135,
425 /* videostart0 */ 23)
427 .v4l2_id
= V4L2_STD_NTSC_M_JP
,
435 .iform
= (BT848_IFORM_NTSC_J
|BT848_IFORM_XT0
),
442 .vbistart
= { 10, 273 },
443 CROPCAP(/* minhdelayx1 */ 68,
445 /* swidth */ (640 * 910 + 780 / 2) / 780,
446 /* totalwidth */ 910,
450 /* videostart0 */ 23)
452 /* that one hopefully works with the strange timing
453 * which video recorders produce when playing a NTSC
454 * tape on a PAL TV ... */
455 .v4l2_id
= V4L2_STD_PAL_60
,
463 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
464 .scaledtwidth
= 1135,
471 .vbistart
= { 10, 273 },
472 CROPCAP(/* minhdelayx1 */ 68,
475 /* totalwidth */ 1135,
479 /* videostart0 */ 23)
482 static const unsigned int BTTV_TVNORMS
= ARRAY_SIZE(bttv_tvnorms
);
484 /* ----------------------------------------------------------------------- */
486 packed pixel formats must come first */
487 static const struct bttv_format formats
[] = {
489 .name
= "8 bpp, gray",
490 .fourcc
= V4L2_PIX_FMT_GREY
,
491 .btformat
= BT848_COLOR_FMT_Y8
,
493 .flags
= FORMAT_FLAGS_PACKED
,
495 .name
= "8 bpp, dithered color",
496 .fourcc
= V4L2_PIX_FMT_HI240
,
497 .btformat
= BT848_COLOR_FMT_RGB8
,
499 .flags
= FORMAT_FLAGS_PACKED
| FORMAT_FLAGS_DITHER
,
501 .name
= "15 bpp RGB, le",
502 .fourcc
= V4L2_PIX_FMT_RGB555
,
503 .btformat
= BT848_COLOR_FMT_RGB15
,
505 .flags
= FORMAT_FLAGS_PACKED
,
507 .name
= "15 bpp RGB, be",
508 .fourcc
= V4L2_PIX_FMT_RGB555X
,
509 .btformat
= BT848_COLOR_FMT_RGB15
,
510 .btswap
= 0x03, /* byteswap */
512 .flags
= FORMAT_FLAGS_PACKED
,
514 .name
= "16 bpp RGB, le",
515 .fourcc
= V4L2_PIX_FMT_RGB565
,
516 .btformat
= BT848_COLOR_FMT_RGB16
,
518 .flags
= FORMAT_FLAGS_PACKED
,
520 .name
= "16 bpp RGB, be",
521 .fourcc
= V4L2_PIX_FMT_RGB565X
,
522 .btformat
= BT848_COLOR_FMT_RGB16
,
523 .btswap
= 0x03, /* byteswap */
525 .flags
= FORMAT_FLAGS_PACKED
,
527 .name
= "24 bpp RGB, le",
528 .fourcc
= V4L2_PIX_FMT_BGR24
,
529 .btformat
= BT848_COLOR_FMT_RGB24
,
531 .flags
= FORMAT_FLAGS_PACKED
,
533 .name
= "32 bpp RGB, le",
534 .fourcc
= V4L2_PIX_FMT_BGR32
,
535 .btformat
= BT848_COLOR_FMT_RGB32
,
537 .flags
= FORMAT_FLAGS_PACKED
,
539 .name
= "32 bpp RGB, be",
540 .fourcc
= V4L2_PIX_FMT_RGB32
,
541 .btformat
= BT848_COLOR_FMT_RGB32
,
542 .btswap
= 0x0f, /* byte+word swap */
544 .flags
= FORMAT_FLAGS_PACKED
,
546 .name
= "4:2:2, packed, YUYV",
547 .fourcc
= V4L2_PIX_FMT_YUYV
,
548 .btformat
= BT848_COLOR_FMT_YUY2
,
550 .flags
= FORMAT_FLAGS_PACKED
,
552 .name
= "4:2:2, packed, YUYV",
553 .fourcc
= V4L2_PIX_FMT_YUYV
,
554 .btformat
= BT848_COLOR_FMT_YUY2
,
556 .flags
= FORMAT_FLAGS_PACKED
,
558 .name
= "4:2:2, packed, UYVY",
559 .fourcc
= V4L2_PIX_FMT_UYVY
,
560 .btformat
= BT848_COLOR_FMT_YUY2
,
561 .btswap
= 0x03, /* byteswap */
563 .flags
= FORMAT_FLAGS_PACKED
,
565 .name
= "4:2:2, planar, Y-Cb-Cr",
566 .fourcc
= V4L2_PIX_FMT_YUV422P
,
567 .btformat
= BT848_COLOR_FMT_YCrCb422
,
569 .flags
= FORMAT_FLAGS_PLANAR
,
573 .name
= "4:2:0, planar, Y-Cb-Cr",
574 .fourcc
= V4L2_PIX_FMT_YUV420
,
575 .btformat
= BT848_COLOR_FMT_YCrCb422
,
577 .flags
= FORMAT_FLAGS_PLANAR
,
581 .name
= "4:2:0, planar, Y-Cr-Cb",
582 .fourcc
= V4L2_PIX_FMT_YVU420
,
583 .btformat
= BT848_COLOR_FMT_YCrCb422
,
585 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
589 .name
= "4:1:1, planar, Y-Cb-Cr",
590 .fourcc
= V4L2_PIX_FMT_YUV411P
,
591 .btformat
= BT848_COLOR_FMT_YCrCb411
,
593 .flags
= FORMAT_FLAGS_PLANAR
,
597 .name
= "4:1:0, planar, Y-Cb-Cr",
598 .fourcc
= V4L2_PIX_FMT_YUV410
,
599 .btformat
= BT848_COLOR_FMT_YCrCb411
,
601 .flags
= FORMAT_FLAGS_PLANAR
,
605 .name
= "4:1:0, planar, Y-Cr-Cb",
606 .fourcc
= V4L2_PIX_FMT_YVU410
,
607 .btformat
= BT848_COLOR_FMT_YCrCb411
,
609 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
613 .name
= "raw scanlines",
615 .btformat
= BT848_COLOR_FMT_RAW
,
617 .flags
= FORMAT_FLAGS_RAW
,
620 static const unsigned int FORMATS
= ARRAY_SIZE(formats
);
622 /* ----------------------------------------------------------------------- */
624 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
625 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
626 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
627 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
628 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
629 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
630 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
631 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
632 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
633 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
634 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
635 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
637 static const struct v4l2_queryctrl no_ctl
= {
639 .flags
= V4L2_CTRL_FLAG_DISABLED
,
641 static const struct v4l2_queryctrl bttv_ctls
[] = {
644 .id
= V4L2_CID_BRIGHTNESS
,
645 .name
= "Brightness",
649 .default_value
= 32768,
650 .type
= V4L2_CTRL_TYPE_INTEGER
,
652 .id
= V4L2_CID_CONTRAST
,
657 .default_value
= 32768,
658 .type
= V4L2_CTRL_TYPE_INTEGER
,
660 .id
= V4L2_CID_SATURATION
,
661 .name
= "Saturation",
665 .default_value
= 32768,
666 .type
= V4L2_CTRL_TYPE_INTEGER
,
673 .default_value
= 32768,
674 .type
= V4L2_CTRL_TYPE_INTEGER
,
678 .id
= V4L2_CID_AUDIO_MUTE
,
682 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
684 .id
= V4L2_CID_AUDIO_VOLUME
,
689 .default_value
= 65535,
690 .type
= V4L2_CTRL_TYPE_INTEGER
,
692 .id
= V4L2_CID_AUDIO_BALANCE
,
697 .default_value
= 32768,
698 .type
= V4L2_CTRL_TYPE_INTEGER
,
700 .id
= V4L2_CID_AUDIO_BASS
,
705 .default_value
= 32768,
706 .type
= V4L2_CTRL_TYPE_INTEGER
,
708 .id
= V4L2_CID_AUDIO_TREBLE
,
713 .default_value
= 32768,
714 .type
= V4L2_CTRL_TYPE_INTEGER
,
716 /* --- private --- */
718 .id
= V4L2_CID_PRIVATE_CHROMA_AGC
,
719 .name
= "chroma agc",
722 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
724 .id
= V4L2_CID_PRIVATE_COMBFILTER
,
725 .name
= "combfilter",
728 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
730 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
734 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
736 .id
= V4L2_CID_PRIVATE_LUMAFILTER
,
737 .name
= "luma decimation filter",
740 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
742 .id
= V4L2_CID_PRIVATE_AGC_CRUSH
,
746 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
748 .id
= V4L2_CID_PRIVATE_VCR_HACK
,
752 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
754 .id
= V4L2_CID_PRIVATE_WHITECRUSH_UPPER
,
755 .name
= "whitecrush upper",
759 .default_value
= 0xCF,
760 .type
= V4L2_CTRL_TYPE_INTEGER
,
762 .id
= V4L2_CID_PRIVATE_WHITECRUSH_LOWER
,
763 .name
= "whitecrush lower",
767 .default_value
= 0x7F,
768 .type
= V4L2_CTRL_TYPE_INTEGER
,
770 .id
= V4L2_CID_PRIVATE_UV_RATIO
,
776 .type
= V4L2_CTRL_TYPE_INTEGER
,
778 .id
= V4L2_CID_PRIVATE_FULL_LUMA_RANGE
,
779 .name
= "full luma range",
782 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
784 .id
= V4L2_CID_PRIVATE_CORING
,
790 .type
= V4L2_CTRL_TYPE_INTEGER
,
797 static const struct v4l2_queryctrl
*ctrl_by_id(int id
)
801 for (i
= 0; i
< ARRAY_SIZE(bttv_ctls
); i
++)
802 if (bttv_ctls
[i
].id
== id
)
808 /* ----------------------------------------------------------------------- */
809 /* resource management */
812 RESOURCE_ allocated by freed by
814 VIDEO_READ bttv_read 1) bttv_read 2)
816 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
817 VIDIOC_QBUF 1) bttv_release
820 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
821 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
824 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
825 VIDIOC_QBUF 1) bttv_release
826 bttv_read, bttv_poll 1) 4)
828 1) The resource must be allocated when we enter buffer prepare functions
829 and remain allocated while buffers are in the DMA queue.
830 2) This is a single frame read.
831 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
832 RESOURCE_OVERLAY is allocated.
833 4) This is a continuous read, implies VIDIOC_STREAMON.
835 Note this driver permits video input and standard changes regardless if
836 resources are allocated.
839 #define VBI_RESOURCES (RESOURCE_VBI)
840 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
841 RESOURCE_VIDEO_STREAM | \
845 int check_alloc_btres(struct bttv
*btv
, struct bttv_fh
*fh
, int bit
)
847 int xbits
; /* mutual exclusive resources */
849 if (fh
->resources
& bit
)
850 /* have it already allocated */
854 if (bit
& (RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
))
855 xbits
|= RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
;
858 mutex_lock(&btv
->lock
);
859 if (btv
->resources
& xbits
) {
860 /* no, someone else uses it */
864 if ((bit
& VIDEO_RESOURCES
)
865 && 0 == (btv
->resources
& VIDEO_RESOURCES
)) {
866 /* Do crop - use current, don't - use default parameters. */
867 __s32 top
= btv
->crop
[!!fh
->do_crop
].rect
.top
;
869 if (btv
->vbi_end
> top
)
872 /* We cannot capture the same line as video and VBI data.
873 Claim scan lines crop[].rect.top to bottom. */
874 btv
->crop_start
= top
;
875 } else if (bit
& VBI_RESOURCES
) {
876 __s32 end
= fh
->vbi_fmt
.end
;
878 if (end
> btv
->crop_start
)
881 /* Claim scan lines above fh->vbi_fmt.end. */
885 /* it's free, grab it */
886 fh
->resources
|= bit
;
887 btv
->resources
|= bit
;
888 mutex_unlock(&btv
->lock
);
892 mutex_unlock(&btv
->lock
);
897 int check_btres(struct bttv_fh
*fh
, int bit
)
899 return (fh
->resources
& bit
);
903 int locked_btres(struct bttv
*btv
, int bit
)
905 return (btv
->resources
& bit
);
908 /* Call with btv->lock down. */
910 disclaim_vbi_lines(struct bttv
*btv
)
915 /* Call with btv->lock down. */
917 disclaim_video_lines(struct bttv
*btv
)
919 const struct bttv_tvnorm
*tvnorm
;
922 tvnorm
= &bttv_tvnorms
[btv
->tvnorm
];
923 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
924 + tvnorm
->cropcap
.bounds
.height
;
926 /* VBI capturing ends at VDELAY, start of video capturing, no
927 matter how many lines the VBI RISC program expects. When video
928 capturing is off, it shall no longer "preempt" VBI capturing,
929 so we set VDELAY to maximum. */
930 crop
= btread(BT848_E_CROP
) | 0xc0;
931 btwrite(crop
, BT848_E_CROP
);
932 btwrite(0xfe, BT848_E_VDELAY_LO
);
933 btwrite(crop
, BT848_O_CROP
);
934 btwrite(0xfe, BT848_O_VDELAY_LO
);
938 void free_btres(struct bttv
*btv
, struct bttv_fh
*fh
, int bits
)
940 if ((fh
->resources
& bits
) != bits
) {
941 /* trying to free ressources not allocated by us ... */
942 printk("bttv: BUG! (btres)\n");
944 mutex_lock(&btv
->lock
);
945 fh
->resources
&= ~bits
;
946 btv
->resources
&= ~bits
;
948 bits
= btv
->resources
;
950 if (0 == (bits
& VIDEO_RESOURCES
))
951 disclaim_video_lines(btv
);
953 if (0 == (bits
& VBI_RESOURCES
))
954 disclaim_vbi_lines(btv
);
956 mutex_unlock(&btv
->lock
);
959 /* ----------------------------------------------------------------------- */
960 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
962 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
963 PLL_X = Reference pre-divider (0=1, 1=2)
964 PLL_C = Post divider (0=6, 1=4)
965 PLL_I = Integer input
966 PLL_F = Fractional input
968 F_input = 28.636363 MHz:
969 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
972 static void set_pll_freq(struct bttv
*btv
, unsigned int fin
, unsigned int fout
)
974 unsigned char fl
, fh
, fi
;
976 /* prevent overflows */
989 btwrite(fl
, BT848_PLL_F_LO
);
990 btwrite(fh
, BT848_PLL_F_HI
);
991 btwrite(fi
|BT848_PLL_X
, BT848_PLL_XCI
);
994 static void set_pll(struct bttv
*btv
)
998 if (!btv
->pll
.pll_crystal
)
1001 if (btv
->pll
.pll_ofreq
== btv
->pll
.pll_current
) {
1002 dprintk("bttv%d: PLL: no change required\n",btv
->c
.nr
);
1006 if (btv
->pll
.pll_ifreq
== btv
->pll
.pll_ofreq
) {
1008 if (btv
->pll
.pll_current
== 0)
1010 bttv_printk(KERN_INFO
"bttv%d: PLL can sleep, using XTAL (%d).\n",
1011 btv
->c
.nr
,btv
->pll
.pll_ifreq
);
1012 btwrite(0x00,BT848_TGCTRL
);
1013 btwrite(0x00,BT848_PLL_XCI
);
1014 btv
->pll
.pll_current
= 0;
1018 bttv_printk(KERN_INFO
"bttv%d: PLL: %d => %d ",btv
->c
.nr
,
1019 btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
1020 set_pll_freq(btv
, btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
1022 for (i
=0; i
<10; i
++) {
1023 /* Let other people run while the PLL stabilizes */
1027 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_PLOCK
) {
1028 btwrite(0,BT848_DSTATUS
);
1030 btwrite(0x08,BT848_TGCTRL
);
1031 btv
->pll
.pll_current
= btv
->pll
.pll_ofreq
;
1032 bttv_printk(" ok\n");
1036 btv
->pll
.pll_current
= -1;
1037 bttv_printk("failed\n");
1041 /* used to switch between the bt848's analog/digital video capture modes */
1042 static void bt848A_set_timing(struct bttv
*btv
)
1045 int table_idx
= bttv_tvnorms
[btv
->tvnorm
].sram
;
1046 int fsc
= bttv_tvnorms
[btv
->tvnorm
].Fsc
;
1048 if (btv
->input
== btv
->dig
) {
1049 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1050 btv
->c
.nr
,table_idx
);
1052 /* timing change...reset timing generator address */
1053 btwrite(0x00, BT848_TGCTRL
);
1054 btwrite(0x02, BT848_TGCTRL
);
1055 btwrite(0x00, BT848_TGCTRL
);
1057 len
=SRAM_Table
[table_idx
][0];
1058 for(i
= 1; i
<= len
; i
++)
1059 btwrite(SRAM_Table
[table_idx
][i
],BT848_TGLB
);
1060 btv
->pll
.pll_ofreq
= 27000000;
1063 btwrite(0x11, BT848_TGCTRL
);
1064 btwrite(0x41, BT848_DVSIF
);
1066 btv
->pll
.pll_ofreq
= fsc
;
1068 btwrite(0x0, BT848_DVSIF
);
1072 /* ----------------------------------------------------------------------- */
1074 static void bt848_bright(struct bttv
*btv
, int bright
)
1078 // printk("bttv: set bright: %d\n",bright); // DEBUG
1079 btv
->bright
= bright
;
1081 /* We want -128 to 127 we get 0-65535 */
1082 value
= (bright
>> 8) - 128;
1083 btwrite(value
& 0xff, BT848_BRIGHT
);
1086 static void bt848_hue(struct bttv
*btv
, int hue
)
1093 value
= (hue
>> 8) - 128;
1094 btwrite(value
& 0xff, BT848_HUE
);
1097 static void bt848_contrast(struct bttv
*btv
, int cont
)
1101 btv
->contrast
= cont
;
1104 value
= (cont
>> 7);
1105 hibit
= (value
>> 6) & 4;
1106 btwrite(value
& 0xff, BT848_CONTRAST_LO
);
1107 btaor(hibit
, ~4, BT848_E_CONTROL
);
1108 btaor(hibit
, ~4, BT848_O_CONTROL
);
1111 static void bt848_sat(struct bttv
*btv
, int color
)
1113 int val_u
,val_v
,hibits
;
1115 btv
->saturation
= color
;
1117 /* 0-511 for the color */
1118 val_u
= ((color
* btv
->opt_uv_ratio
) / 50) >> 7;
1119 val_v
= (((color
* (100 - btv
->opt_uv_ratio
) / 50) >>7)*180L)/254;
1120 hibits
= (val_u
>> 7) & 2;
1121 hibits
|= (val_v
>> 8) & 1;
1122 btwrite(val_u
& 0xff, BT848_SAT_U_LO
);
1123 btwrite(val_v
& 0xff, BT848_SAT_V_LO
);
1124 btaor(hibits
, ~3, BT848_E_CONTROL
);
1125 btaor(hibits
, ~3, BT848_O_CONTROL
);
1128 /* ----------------------------------------------------------------------- */
1131 video_mux(struct bttv
*btv
, unsigned int input
)
1135 if (input
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1138 /* needed by RemoteVideo MX */
1139 mask2
= bttv_tvcards
[btv
->c
.type
].gpiomask2
;
1141 gpio_inout(mask2
,mask2
);
1143 if (input
== btv
->svhs
) {
1144 btor(BT848_CONTROL_COMP
, BT848_E_CONTROL
);
1145 btor(BT848_CONTROL_COMP
, BT848_O_CONTROL
);
1147 btand(~BT848_CONTROL_COMP
, BT848_E_CONTROL
);
1148 btand(~BT848_CONTROL_COMP
, BT848_O_CONTROL
);
1150 mux
= bttv_muxsel(btv
, input
);
1151 btaor(mux
<<5, ~(3<<5), BT848_IFORM
);
1152 dprintk(KERN_DEBUG
"bttv%d: video mux: input=%d mux=%d\n",
1153 btv
->c
.nr
,input
,mux
);
1155 /* card specific hook */
1156 if(bttv_tvcards
[btv
->c
.type
].muxsel_hook
)
1157 bttv_tvcards
[btv
->c
.type
].muxsel_hook (btv
, input
);
1161 static char *audio_modes
[] = {
1162 "audio: tuner", "audio: radio", "audio: extern",
1163 "audio: intern", "audio: mute"
1167 audio_mux(struct bttv
*btv
, int input
, int mute
)
1169 int gpio_val
, signal
;
1170 struct v4l2_control ctrl
;
1172 gpio_inout(bttv_tvcards
[btv
->c
.type
].gpiomask
,
1173 bttv_tvcards
[btv
->c
.type
].gpiomask
);
1174 signal
= btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
;
1180 mute
= mute
|| (btv
->opt_automute
&& !signal
&& !btv
->radio_user
);
1183 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomute
;
1185 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomux
[input
];
1187 switch (btv
->c
.type
) {
1188 case BTTV_BOARD_VOODOOTV_FM
:
1189 case BTTV_BOARD_VOODOOTV_200
:
1190 gpio_val
= bttv_tda9880_setnorm(btv
, gpio_val
);
1194 gpio_bits(bttv_tvcards
[btv
->c
.type
].gpiomask
, gpio_val
);
1198 bttv_gpio_tracking(btv
, audio_modes
[mute
? 4 : input
]);
1202 ctrl
.id
= V4L2_CID_AUDIO_MUTE
;
1203 ctrl
.value
= btv
->mute
;
1204 bttv_call_all(btv
, core
, s_ctrl
, &ctrl
);
1205 if (btv
->sd_msp34xx
) {
1208 /* Note: the inputs tuner/radio/extern/intern are translated
1209 to msp routings. This assumes common behavior for all msp3400
1210 based TV cards. When this assumption fails, then the
1211 specific MSP routing must be added to the card table.
1212 For now this is sufficient. */
1214 case TVAUDIO_INPUT_RADIO
:
1215 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1216 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1218 case TVAUDIO_INPUT_EXTERN
:
1219 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER1
,
1220 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1222 case TVAUDIO_INPUT_INTERN
:
1223 /* Yes, this is the same input as for RADIO. I doubt
1224 if this is ever used. The only board with an INTERN
1225 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1226 that was tested. My guess is that the whole INTERN
1227 input does not work. */
1228 in
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1229 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1231 case TVAUDIO_INPUT_TUNER
:
1233 /* This is the only card that uses TUNER2, and afaik,
1234 is the only difference between the VOODOOTV_FM
1236 if (btv
->c
.type
== BTTV_BOARD_VOODOOTV_200
)
1237 in
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER2
, \
1238 MSP_DSP_IN_TUNER
, MSP_DSP_IN_TUNER
);
1240 in
= MSP_INPUT_DEFAULT
;
1243 v4l2_subdev_call(btv
->sd_msp34xx
, audio
, s_routing
,
1244 in
, MSP_OUTPUT_DEFAULT
, 0);
1246 if (btv
->sd_tvaudio
) {
1247 v4l2_subdev_call(btv
->sd_tvaudio
, audio
, s_routing
,
1254 audio_mute(struct bttv
*btv
, int mute
)
1256 return audio_mux(btv
, btv
->audio
, mute
);
1260 audio_input(struct bttv
*btv
, int input
)
1262 return audio_mux(btv
, input
, btv
->mute
);
1266 bttv_crop_calc_limits(struct bttv_crop
*c
)
1268 /* Scale factor min. 1:1, max. 16:1. Min. image size
1269 48 x 32. Scaled width must be a multiple of 4. */
1272 /* For bug compatibility with VIDIOCGCAP and image
1273 size checks in earlier driver versions. */
1274 c
->min_scaled_width
= 48;
1275 c
->min_scaled_height
= 32;
1277 c
->min_scaled_width
=
1278 (max(48, c
->rect
.width
>> 4) + 3) & ~3;
1279 c
->min_scaled_height
=
1280 max(32, c
->rect
.height
>> 4);
1283 c
->max_scaled_width
= c
->rect
.width
& ~3;
1284 c
->max_scaled_height
= c
->rect
.height
;
1288 bttv_crop_reset(struct bttv_crop
*c
, unsigned int norm
)
1290 c
->rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1291 bttv_crop_calc_limits(c
);
1294 /* Call with btv->lock down. */
1296 set_tvnorm(struct bttv
*btv
, unsigned int norm
)
1298 const struct bttv_tvnorm
*tvnorm
;
1301 BUG_ON(norm
>= BTTV_TVNORMS
);
1302 BUG_ON(btv
->tvnorm
>= BTTV_TVNORMS
);
1304 tvnorm
= &bttv_tvnorms
[norm
];
1306 if (memcmp(&bttv_tvnorms
[btv
->tvnorm
].cropcap
, &tvnorm
->cropcap
,
1307 sizeof (tvnorm
->cropcap
))) {
1308 bttv_crop_reset(&btv
->crop
[0], norm
);
1309 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
1311 if (0 == (btv
->resources
& VIDEO_RESOURCES
)) {
1312 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
1313 + tvnorm
->cropcap
.bounds
.height
;
1319 btwrite(tvnorm
->adelay
, BT848_ADELAY
);
1320 btwrite(tvnorm
->bdelay
, BT848_BDELAY
);
1321 btaor(tvnorm
->iform
,~(BT848_IFORM_NORM
|BT848_IFORM_XTBOTH
),
1323 btwrite(tvnorm
->vbipack
, BT848_VBI_PACK_SIZE
);
1324 btwrite(1, BT848_VBI_PACK_DEL
);
1325 bt848A_set_timing(btv
);
1327 switch (btv
->c
.type
) {
1328 case BTTV_BOARD_VOODOOTV_FM
:
1329 case BTTV_BOARD_VOODOOTV_200
:
1330 bttv_tda9880_setnorm(btv
, gpio_read());
1333 id
= tvnorm
->v4l2_id
;
1334 bttv_call_all(btv
, core
, s_std
, id
);
1339 /* Call with btv->lock down. */
1341 set_input(struct bttv
*btv
, unsigned int input
, unsigned int norm
)
1343 unsigned long flags
;
1347 spin_lock_irqsave(&btv
->s_lock
,flags
);
1348 if (btv
->curr
.frame_irq
) {
1349 /* active capture -> delayed input switch */
1350 btv
->new_input
= input
;
1352 video_mux(btv
,input
);
1354 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1356 video_mux(btv
,input
);
1358 audio_input(btv
, (btv
->tuner_type
!= TUNER_ABSENT
&& input
== 0) ?
1359 TVAUDIO_INPUT_TUNER
: TVAUDIO_INPUT_EXTERN
);
1360 set_tvnorm(btv
, norm
);
1363 static void init_irqreg(struct bttv
*btv
)
1366 btwrite(0xfffffUL
, BT848_INT_STAT
);
1368 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1370 btwrite(BT848_INT_I2CDONE
,
1374 btwrite((btv
->triton1
) |
1375 (btv
->gpioirq
? BT848_INT_GPINT
: 0) |
1377 (fdsr
? BT848_INT_FDSR
: 0) |
1378 BT848_INT_RISCI
| BT848_INT_OCERR
|
1379 BT848_INT_FMTCHG
|BT848_INT_HLOCK
|
1385 static void init_bt848(struct bttv
*btv
)
1389 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1390 /* very basic init only */
1395 btwrite(0x00, BT848_CAP_CTL
);
1396 btwrite(BT848_COLOR_CTL_GAMMA
, BT848_COLOR_CTL
);
1397 btwrite(BT848_IFORM_XTAUTO
| BT848_IFORM_AUTO
, BT848_IFORM
);
1399 /* set planar and packed mode trigger points and */
1400 /* set rising edge of inverted GPINTR pin as irq trigger */
1401 btwrite(BT848_GPIO_DMA_CTL_PKTP_32
|
1402 BT848_GPIO_DMA_CTL_PLTP1_16
|
1403 BT848_GPIO_DMA_CTL_PLTP23_16
|
1404 BT848_GPIO_DMA_CTL_GPINTC
|
1405 BT848_GPIO_DMA_CTL_GPINTI
,
1406 BT848_GPIO_DMA_CTL
);
1408 val
= btv
->opt_chroma_agc
? BT848_SCLOOP_CAGC
: 0;
1409 btwrite(val
, BT848_E_SCLOOP
);
1410 btwrite(val
, BT848_O_SCLOOP
);
1412 btwrite(0x20, BT848_E_VSCALE_HI
);
1413 btwrite(0x20, BT848_O_VSCALE_HI
);
1414 btwrite(BT848_ADC_RESERVED
| (btv
->opt_adc_crush
? BT848_ADC_CRUSH
: 0),
1417 btwrite(whitecrush_upper
, BT848_WC_UP
);
1418 btwrite(whitecrush_lower
, BT848_WC_DOWN
);
1420 if (btv
->opt_lumafilter
) {
1421 btwrite(0, BT848_E_CONTROL
);
1422 btwrite(0, BT848_O_CONTROL
);
1424 btwrite(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1425 btwrite(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1428 bt848_bright(btv
, btv
->bright
);
1429 bt848_hue(btv
, btv
->hue
);
1430 bt848_contrast(btv
, btv
->contrast
);
1431 bt848_sat(btv
, btv
->saturation
);
1437 static void bttv_reinit_bt848(struct bttv
*btv
)
1439 unsigned long flags
;
1442 printk(KERN_INFO
"bttv%d: reset, reinitialize\n",btv
->c
.nr
);
1443 spin_lock_irqsave(&btv
->s_lock
,flags
);
1445 bttv_set_dma(btv
,0);
1446 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1449 btv
->pll
.pll_current
= -1;
1450 set_input(btv
, btv
->input
, btv
->tvnorm
);
1453 static int bttv_g_ctrl(struct file
*file
, void *priv
,
1454 struct v4l2_control
*c
)
1456 struct bttv_fh
*fh
= priv
;
1457 struct bttv
*btv
= fh
->btv
;
1460 case V4L2_CID_BRIGHTNESS
:
1461 c
->value
= btv
->bright
;
1464 c
->value
= btv
->hue
;
1466 case V4L2_CID_CONTRAST
:
1467 c
->value
= btv
->contrast
;
1469 case V4L2_CID_SATURATION
:
1470 c
->value
= btv
->saturation
;
1473 case V4L2_CID_AUDIO_MUTE
:
1474 case V4L2_CID_AUDIO_VOLUME
:
1475 case V4L2_CID_AUDIO_BALANCE
:
1476 case V4L2_CID_AUDIO_BASS
:
1477 case V4L2_CID_AUDIO_TREBLE
:
1478 bttv_call_all(btv
, core
, g_ctrl
, c
);
1481 case V4L2_CID_PRIVATE_CHROMA_AGC
:
1482 c
->value
= btv
->opt_chroma_agc
;
1484 case V4L2_CID_PRIVATE_COMBFILTER
:
1485 c
->value
= btv
->opt_combfilter
;
1487 case V4L2_CID_PRIVATE_LUMAFILTER
:
1488 c
->value
= btv
->opt_lumafilter
;
1490 case V4L2_CID_PRIVATE_AUTOMUTE
:
1491 c
->value
= btv
->opt_automute
;
1493 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1494 c
->value
= btv
->opt_adc_crush
;
1496 case V4L2_CID_PRIVATE_VCR_HACK
:
1497 c
->value
= btv
->opt_vcr_hack
;
1499 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1500 c
->value
= btv
->opt_whitecrush_upper
;
1502 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1503 c
->value
= btv
->opt_whitecrush_lower
;
1505 case V4L2_CID_PRIVATE_UV_RATIO
:
1506 c
->value
= btv
->opt_uv_ratio
;
1508 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1509 c
->value
= btv
->opt_full_luma_range
;
1511 case V4L2_CID_PRIVATE_CORING
:
1512 c
->value
= btv
->opt_coring
;
1520 static int bttv_s_ctrl(struct file
*file
, void *f
,
1521 struct v4l2_control
*c
)
1525 struct bttv_fh
*fh
= f
;
1526 struct bttv
*btv
= fh
->btv
;
1528 err
= v4l2_prio_check(&btv
->prio
, &fh
->prio
);
1533 case V4L2_CID_BRIGHTNESS
:
1534 bt848_bright(btv
, c
->value
);
1537 bt848_hue(btv
, c
->value
);
1539 case V4L2_CID_CONTRAST
:
1540 bt848_contrast(btv
, c
->value
);
1542 case V4L2_CID_SATURATION
:
1543 bt848_sat(btv
, c
->value
);
1545 case V4L2_CID_AUDIO_MUTE
:
1546 audio_mute(btv
, c
->value
);
1548 case V4L2_CID_AUDIO_VOLUME
:
1549 if (btv
->volume_gpio
)
1550 btv
->volume_gpio(btv
, c
->value
);
1552 bttv_call_all(btv
, core
, s_ctrl
, c
);
1554 case V4L2_CID_AUDIO_BALANCE
:
1555 case V4L2_CID_AUDIO_BASS
:
1556 case V4L2_CID_AUDIO_TREBLE
:
1557 bttv_call_all(btv
, core
, s_ctrl
, c
);
1560 case V4L2_CID_PRIVATE_CHROMA_AGC
:
1561 btv
->opt_chroma_agc
= c
->value
;
1562 val
= btv
->opt_chroma_agc
? BT848_SCLOOP_CAGC
: 0;
1563 btwrite(val
, BT848_E_SCLOOP
);
1564 btwrite(val
, BT848_O_SCLOOP
);
1566 case V4L2_CID_PRIVATE_COMBFILTER
:
1567 btv
->opt_combfilter
= c
->value
;
1569 case V4L2_CID_PRIVATE_LUMAFILTER
:
1570 btv
->opt_lumafilter
= c
->value
;
1571 if (btv
->opt_lumafilter
) {
1572 btand(~BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1573 btand(~BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1575 btor(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1576 btor(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1579 case V4L2_CID_PRIVATE_AUTOMUTE
:
1580 btv
->opt_automute
= c
->value
;
1582 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1583 btv
->opt_adc_crush
= c
->value
;
1584 btwrite(BT848_ADC_RESERVED
|
1585 (btv
->opt_adc_crush
? BT848_ADC_CRUSH
: 0),
1588 case V4L2_CID_PRIVATE_VCR_HACK
:
1589 btv
->opt_vcr_hack
= c
->value
;
1591 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1592 btv
->opt_whitecrush_upper
= c
->value
;
1593 btwrite(c
->value
, BT848_WC_UP
);
1595 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1596 btv
->opt_whitecrush_lower
= c
->value
;
1597 btwrite(c
->value
, BT848_WC_DOWN
);
1599 case V4L2_CID_PRIVATE_UV_RATIO
:
1600 btv
->opt_uv_ratio
= c
->value
;
1601 bt848_sat(btv
, btv
->saturation
);
1603 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1604 btv
->opt_full_luma_range
= c
->value
;
1605 btaor((c
->value
<<7), ~BT848_OFORM_RANGE
, BT848_OFORM
);
1607 case V4L2_CID_PRIVATE_CORING
:
1608 btv
->opt_coring
= c
->value
;
1609 btaor((c
->value
<<5), ~BT848_OFORM_CORE32
, BT848_OFORM
);
1617 /* ----------------------------------------------------------------------- */
1619 void bttv_gpio_tracking(struct bttv
*btv
, char *comment
)
1621 unsigned int outbits
, data
;
1622 outbits
= btread(BT848_GPIO_OUT_EN
);
1623 data
= btread(BT848_GPIO_DATA
);
1624 printk(KERN_DEBUG
"bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1625 btv
->c
.nr
,outbits
,data
& outbits
, data
& ~outbits
, comment
);
1628 static void bttv_field_count(struct bttv
*btv
)
1636 /* start field counter */
1637 btor(BT848_INT_VSYNC
,BT848_INT_MASK
);
1639 /* stop field counter */
1640 btand(~BT848_INT_VSYNC
,BT848_INT_MASK
);
1641 btv
->field_count
= 0;
1645 static const struct bttv_format
*
1646 format_by_fourcc(int fourcc
)
1650 for (i
= 0; i
< FORMATS
; i
++) {
1651 if (-1 == formats
[i
].fourcc
)
1653 if (formats
[i
].fourcc
== fourcc
)
1659 /* ----------------------------------------------------------------------- */
1663 bttv_switch_overlay(struct bttv
*btv
, struct bttv_fh
*fh
,
1664 struct bttv_buffer
*new)
1666 struct bttv_buffer
*old
;
1667 unsigned long flags
;
1670 dprintk("switch_overlay: enter [new=%p]\n",new);
1672 new->vb
.state
= VIDEOBUF_DONE
;
1673 spin_lock_irqsave(&btv
->s_lock
,flags
);
1677 bttv_set_dma(btv
, 0x03);
1678 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1680 dprintk("switch_overlay: old=%p state is %d\n",old
,old
->vb
.state
);
1681 bttv_dma_free(&fh
->cap
,btv
, old
);
1685 free_btres(btv
,fh
,RESOURCE_OVERLAY
);
1686 dprintk("switch_overlay: done\n");
1690 /* ----------------------------------------------------------------------- */
1691 /* video4linux (1) interface */
1693 static int bttv_prepare_buffer(struct videobuf_queue
*q
,struct bttv
*btv
,
1694 struct bttv_buffer
*buf
,
1695 const struct bttv_format
*fmt
,
1696 unsigned int width
, unsigned int height
,
1697 enum v4l2_field field
)
1699 struct bttv_fh
*fh
= q
->priv_data
;
1700 int redo_dma_risc
= 0;
1705 /* check settings */
1708 if (fmt
->btformat
== BT848_COLOR_FMT_RAW
) {
1710 height
= RAW_LINES
*2;
1711 if (width
*height
> buf
->vb
.bsize
)
1713 buf
->vb
.size
= buf
->vb
.bsize
;
1715 /* Make sure tvnorm and vbi_end remain consistent
1716 until we're done. */
1717 mutex_lock(&btv
->lock
);
1721 /* In this mode capturing always starts at defrect.top
1722 (default VDELAY), ignoring cropping parameters. */
1723 if (btv
->vbi_end
> bttv_tvnorms
[norm
].cropcap
.defrect
.top
) {
1724 mutex_unlock(&btv
->lock
);
1728 mutex_unlock(&btv
->lock
);
1730 c
.rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1732 mutex_lock(&btv
->lock
);
1735 c
= btv
->crop
[!!fh
->do_crop
];
1737 mutex_unlock(&btv
->lock
);
1739 if (width
< c
.min_scaled_width
||
1740 width
> c
.max_scaled_width
||
1741 height
< c
.min_scaled_height
)
1745 case V4L2_FIELD_TOP
:
1746 case V4L2_FIELD_BOTTOM
:
1747 case V4L2_FIELD_ALTERNATE
:
1748 /* btv->crop counts frame lines. Max. scale
1749 factor is 16:1 for frames, 8:1 for fields. */
1750 if (height
* 2 > c
.max_scaled_height
)
1755 if (height
> c
.max_scaled_height
)
1760 buf
->vb
.size
= (width
* height
* fmt
->depth
) >> 3;
1761 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
1765 /* alloc + fill struct bttv_buffer (if changed) */
1766 if (buf
->vb
.width
!= width
|| buf
->vb
.height
!= height
||
1767 buf
->vb
.field
!= field
||
1768 buf
->tvnorm
!= norm
|| buf
->fmt
!= fmt
||
1769 buf
->crop
.top
!= c
.rect
.top
||
1770 buf
->crop
.left
!= c
.rect
.left
||
1771 buf
->crop
.width
!= c
.rect
.width
||
1772 buf
->crop
.height
!= c
.rect
.height
) {
1773 buf
->vb
.width
= width
;
1774 buf
->vb
.height
= height
;
1775 buf
->vb
.field
= field
;
1782 /* alloc risc memory */
1783 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
1785 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,&btv
->fbuf
)))
1790 if (0 != (rc
= bttv_buffer_risc(btv
,buf
)))
1793 buf
->vb
.state
= VIDEOBUF_PREPARED
;
1797 bttv_dma_free(q
,btv
,buf
);
1802 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1804 struct bttv_fh
*fh
= q
->priv_data
;
1806 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
1809 while (*size
* *count
> gbuffers
* gbufsize
)
1815 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
1816 enum v4l2_field field
)
1818 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1819 struct bttv_fh
*fh
= q
->priv_data
;
1821 return bttv_prepare_buffer(q
,fh
->btv
, buf
, fh
->fmt
,
1822 fh
->width
, fh
->height
, field
);
1826 buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1828 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1829 struct bttv_fh
*fh
= q
->priv_data
;
1830 struct bttv
*btv
= fh
->btv
;
1832 buf
->vb
.state
= VIDEOBUF_QUEUED
;
1833 list_add_tail(&buf
->vb
.queue
,&btv
->capture
);
1834 if (!btv
->curr
.frame_irq
) {
1836 bttv_set_dma(btv
, 0x03);
1840 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1842 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1843 struct bttv_fh
*fh
= q
->priv_data
;
1845 bttv_dma_free(q
,fh
->btv
,buf
);
1848 static struct videobuf_queue_ops bttv_video_qops
= {
1849 .buf_setup
= buffer_setup
,
1850 .buf_prepare
= buffer_prepare
,
1851 .buf_queue
= buffer_queue
,
1852 .buf_release
= buffer_release
,
1855 static int bttv_s_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1857 struct bttv_fh
*fh
= priv
;
1858 struct bttv
*btv
= fh
->btv
;
1862 err
= v4l2_prio_check(&btv
->prio
, &fh
->prio
);
1866 for (i
= 0; i
< BTTV_TVNORMS
; i
++)
1867 if (*id
& bttv_tvnorms
[i
].v4l2_id
)
1869 if (i
== BTTV_TVNORMS
)
1872 mutex_lock(&btv
->lock
);
1874 mutex_unlock(&btv
->lock
);
1879 static int bttv_querystd(struct file
*file
, void *f
, v4l2_std_id
*id
)
1881 struct bttv_fh
*fh
= f
;
1882 struct bttv
*btv
= fh
->btv
;
1884 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_NUML
)
1885 *id
= V4L2_STD_625_50
;
1887 *id
= V4L2_STD_525_60
;
1891 static int bttv_enum_input(struct file
*file
, void *priv
,
1892 struct v4l2_input
*i
)
1894 struct bttv_fh
*fh
= priv
;
1895 struct bttv
*btv
= fh
->btv
;
1898 if (i
->index
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1901 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1904 if (btv
->tuner_type
!= TUNER_ABSENT
&& i
->index
== 0) {
1905 sprintf(i
->name
, "Television");
1906 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1908 } else if (i
->index
== btv
->svhs
) {
1909 sprintf(i
->name
, "S-Video");
1911 sprintf(i
->name
, "Composite%d", i
->index
);
1914 if (i
->index
== btv
->input
) {
1915 __u32 dstatus
= btread(BT848_DSTATUS
);
1916 if (0 == (dstatus
& BT848_DSTATUS_PRES
))
1917 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1918 if (0 == (dstatus
& BT848_DSTATUS_HLOC
))
1919 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1922 for (n
= 0; n
< BTTV_TVNORMS
; n
++)
1923 i
->std
|= bttv_tvnorms
[n
].v4l2_id
;
1928 static int bttv_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1930 struct bttv_fh
*fh
= priv
;
1931 struct bttv
*btv
= fh
->btv
;
1937 static int bttv_s_input(struct file
*file
, void *priv
, unsigned int i
)
1939 struct bttv_fh
*fh
= priv
;
1940 struct bttv
*btv
= fh
->btv
;
1944 err
= v4l2_prio_check(&btv
->prio
, &fh
->prio
);
1948 if (i
> bttv_tvcards
[btv
->c
.type
].video_inputs
)
1951 mutex_lock(&btv
->lock
);
1952 set_input(btv
, i
, btv
->tvnorm
);
1953 mutex_unlock(&btv
->lock
);
1957 static int bttv_s_tuner(struct file
*file
, void *priv
,
1958 struct v4l2_tuner
*t
)
1960 struct bttv_fh
*fh
= priv
;
1961 struct bttv
*btv
= fh
->btv
;
1964 err
= v4l2_prio_check(&btv
->prio
, &fh
->prio
);
1968 if (btv
->tuner_type
== TUNER_ABSENT
)
1974 mutex_lock(&btv
->lock
);
1975 bttv_call_all(btv
, tuner
, s_tuner
, t
);
1977 if (btv
->audio_mode_gpio
)
1978 btv
->audio_mode_gpio(btv
, t
, 1);
1980 mutex_unlock(&btv
->lock
);
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
;
1992 err
= v4l2_prio_check(&btv
->prio
, &fh
->prio
);
1996 f
->type
= btv
->radio_user
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1997 f
->frequency
= btv
->freq
;
2002 static int bttv_s_frequency(struct file
*file
, void *priv
,
2003 struct v4l2_frequency
*f
)
2005 struct bttv_fh
*fh
= priv
;
2006 struct bttv
*btv
= fh
->btv
;
2009 err
= v4l2_prio_check(&btv
->prio
, &fh
->prio
);
2013 if (unlikely(f
->tuner
!= 0))
2015 if (unlikely(f
->type
!= (btv
->radio_user
2016 ? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
)))
2018 mutex_lock(&btv
->lock
);
2019 btv
->freq
= f
->frequency
;
2020 bttv_call_all(btv
, tuner
, s_frequency
, f
);
2021 if (btv
->has_matchbox
&& btv
->radio_user
)
2022 tea5757_set_freq(btv
, btv
->freq
);
2023 mutex_unlock(&btv
->lock
);
2027 static int bttv_log_status(struct file
*file
, void *f
)
2029 struct bttv_fh
*fh
= f
;
2030 struct bttv
*btv
= fh
->btv
;
2032 printk(KERN_INFO
"bttv%d: ======== START STATUS CARD #%d ========\n",
2033 btv
->c
.nr
, btv
->c
.nr
);
2034 bttv_call_all(btv
, core
, log_status
);
2035 printk(KERN_INFO
"bttv%d: ======== END STATUS CARD #%d ========\n",
2036 btv
->c
.nr
, btv
->c
.nr
);
2040 #ifdef CONFIG_VIDEO_ADV_DEBUG
2041 static int bttv_g_register(struct file
*file
, void *f
,
2042 struct v4l2_dbg_register
*reg
)
2044 struct bttv_fh
*fh
= f
;
2045 struct bttv
*btv
= fh
->btv
;
2047 if (!capable(CAP_SYS_ADMIN
))
2050 if (!v4l2_chip_match_host(®
->match
))
2053 /* bt848 has a 12-bit register space */
2055 reg
->val
= btread(reg
->reg
);
2061 static int bttv_s_register(struct file
*file
, void *f
,
2062 struct v4l2_dbg_register
*reg
)
2064 struct bttv_fh
*fh
= f
;
2065 struct bttv
*btv
= fh
->btv
;
2067 if (!capable(CAP_SYS_ADMIN
))
2070 if (!v4l2_chip_match_host(®
->match
))
2073 /* bt848 has a 12-bit register space */
2075 btwrite(reg
->val
, reg
->reg
);
2081 /* Given cropping boundaries b and the scaled width and height of a
2082 single field or frame, which must not exceed hardware limits, this
2083 function adjusts the cropping parameters c. */
2085 bttv_crop_adjust (struct bttv_crop
* c
,
2086 const struct v4l2_rect
* b
,
2089 enum v4l2_field field
)
2091 __s32 frame_height
= height
<< !V4L2_FIELD_HAS_BOTH(field
);
2095 if (width
< c
->min_scaled_width
) {
2096 /* Max. hor. scale factor 16:1. */
2097 c
->rect
.width
= width
* 16;
2098 } else if (width
> c
->max_scaled_width
) {
2099 /* Min. hor. scale factor 1:1. */
2100 c
->rect
.width
= width
;
2102 max_left
= b
->left
+ b
->width
- width
;
2103 max_left
= min(max_left
, (__s32
) MAX_HDELAY
);
2104 if (c
->rect
.left
> max_left
)
2105 c
->rect
.left
= max_left
;
2108 if (height
< c
->min_scaled_height
) {
2109 /* Max. vert. scale factor 16:1, single fields 8:1. */
2110 c
->rect
.height
= height
* 16;
2111 } else if (frame_height
> c
->max_scaled_height
) {
2112 /* Min. vert. scale factor 1:1.
2113 Top and height count field lines times two. */
2114 c
->rect
.height
= (frame_height
+ 1) & ~1;
2116 max_top
= b
->top
+ b
->height
- c
->rect
.height
;
2117 if (c
->rect
.top
> max_top
)
2118 c
->rect
.top
= max_top
;
2121 bttv_crop_calc_limits(c
);
2124 /* Returns an error if scaling to a frame or single field with the given
2125 width and height is not possible with the current cropping parameters
2126 and width aligned according to width_mask. If adjust_size is TRUE the
2127 function may adjust the width and/or height instead, rounding width
2128 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2129 also adjust the current cropping parameters to get closer to the
2130 desired image size. */
2132 limit_scaled_size (struct bttv_fh
* fh
,
2135 enum v4l2_field field
,
2136 unsigned int width_mask
,
2137 unsigned int width_bias
,
2141 struct bttv
*btv
= fh
->btv
;
2142 const struct v4l2_rect
*b
;
2143 struct bttv_crop
*c
;
2150 BUG_ON((int) width_mask
>= 0 ||
2151 width_bias
>= (unsigned int) -width_mask
);
2153 /* Make sure tvnorm, vbi_end and the current cropping parameters
2154 remain consistent until we're done. */
2155 mutex_lock(&btv
->lock
);
2157 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2159 /* Do crop - use current, don't - use default parameters. */
2160 c
= &btv
->crop
[!!fh
->do_crop
];
2165 && !locked_btres(btv
, VIDEO_RESOURCES
)) {
2169 /* We cannot scale up. When the scaled image is larger
2170 than crop.rect we adjust the crop.rect as required
2171 by the V4L2 spec, hence cropcap.bounds are our limit. */
2172 max_width
= min(b
->width
, (__s32
) MAX_HACTIVE
);
2173 max_height
= b
->height
;
2175 /* We cannot capture the same line as video and VBI data.
2176 Note btv->vbi_end is really a minimum, see
2177 bttv_vbi_try_fmt(). */
2178 if (btv
->vbi_end
> b
->top
) {
2179 max_height
-= btv
->vbi_end
- b
->top
;
2181 if (min_height
> max_height
)
2186 if (btv
->vbi_end
> c
->rect
.top
)
2189 min_width
= c
->min_scaled_width
;
2190 min_height
= c
->min_scaled_height
;
2191 max_width
= c
->max_scaled_width
;
2192 max_height
= c
->max_scaled_height
;
2197 min_width
= (min_width
- width_mask
- 1) & width_mask
;
2198 max_width
= max_width
& width_mask
;
2200 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2201 min_height
= min_height
;
2202 /* Min. scale factor is 1:1. */
2203 max_height
>>= !V4L2_FIELD_HAS_BOTH(field
);
2206 *width
= clamp(*width
, min_width
, max_width
);
2207 *height
= clamp(*height
, min_height
, max_height
);
2209 /* Round after clamping to avoid overflow. */
2210 *width
= (*width
+ width_bias
) & width_mask
;
2213 bttv_crop_adjust(c
, b
, *width
, *height
, field
);
2215 if (btv
->vbi_end
> c
->rect
.top
) {
2216 /* Move the crop window out of the way. */
2217 c
->rect
.top
= btv
->vbi_end
;
2222 if (*width
< min_width
||
2223 *height
< min_height
||
2224 *width
> max_width
||
2225 *height
> max_height
||
2226 0 != (*width
& ~width_mask
))
2230 rc
= 0; /* success */
2233 mutex_unlock(&btv
->lock
);
2238 /* Returns an error if the given overlay window dimensions are not
2239 possible with the current cropping parameters. If adjust_size is
2240 TRUE the function may adjust the window width and/or height
2241 instead, however it always rounds the horizontal position and
2242 width as btcx_align() does. If adjust_crop is TRUE the function
2243 may also adjust the current cropping parameters to get closer
2244 to the desired window size. */
2246 verify_window (struct bttv_fh
* fh
,
2247 struct v4l2_window
* win
,
2251 enum v4l2_field field
;
2252 unsigned int width_mask
;
2255 if (win
->w
.width
< 48 || win
->w
.height
< 32)
2257 if (win
->clipcount
> 2048)
2262 if (V4L2_FIELD_ANY
== field
) {
2265 height2
= fh
->btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2266 field
= (win
->w
.height
> height2
)
2267 ? V4L2_FIELD_INTERLACED
2271 case V4L2_FIELD_TOP
:
2272 case V4L2_FIELD_BOTTOM
:
2273 case V4L2_FIELD_INTERLACED
:
2279 /* 4-byte alignment. */
2280 if (NULL
== fh
->ovfmt
)
2283 switch (fh
->ovfmt
->depth
) {
2297 win
->w
.width
-= win
->w
.left
& ~width_mask
;
2298 win
->w
.left
= (win
->w
.left
- width_mask
- 1) & width_mask
;
2300 rc
= limit_scaled_size(fh
, &win
->w
.width
, &win
->w
.height
,
2302 /* width_bias: round down */ 0,
2303 adjust_size
, adjust_crop
);
2311 static int setup_window(struct bttv_fh
*fh
, struct bttv
*btv
,
2312 struct v4l2_window
*win
, int fixup
)
2314 struct v4l2_clip
*clips
= NULL
;
2315 int n
,size
,retval
= 0;
2317 if (NULL
== fh
->ovfmt
)
2319 if (!(fh
->ovfmt
->flags
& FORMAT_FLAGS_PACKED
))
2321 retval
= verify_window(fh
, win
,
2322 /* adjust_size */ fixup
,
2323 /* adjust_crop */ fixup
);
2327 /* copy clips -- luckily v4l1 + v4l2 are binary
2328 compatible here ...*/
2330 size
= sizeof(*clips
)*(n
+4);
2331 clips
= kmalloc(size
,GFP_KERNEL
);
2335 if (copy_from_user(clips
,win
->clips
,sizeof(struct v4l2_clip
)*n
)) {
2340 /* clip against screen */
2341 if (NULL
!= btv
->fbuf
.base
)
2342 n
= btcx_screen_clips(btv
->fbuf
.fmt
.width
, btv
->fbuf
.fmt
.height
,
2344 btcx_sort_clips(clips
,n
);
2346 /* 4-byte alignments */
2347 switch (fh
->ovfmt
->depth
) {
2350 btcx_align(&win
->w
, clips
, n
, 3);
2353 btcx_align(&win
->w
, clips
, n
, 1);
2356 /* no alignment fixups needed */
2362 mutex_lock(&fh
->cap
.vb_lock
);
2363 kfree(fh
->ov
.clips
);
2364 fh
->ov
.clips
= clips
;
2368 fh
->ov
.field
= win
->field
;
2369 fh
->ov
.setup_ok
= 1;
2370 btv
->init
.ov
.w
.width
= win
->w
.width
;
2371 btv
->init
.ov
.w
.height
= win
->w
.height
;
2372 btv
->init
.ov
.field
= win
->field
;
2374 /* update overlay if needed */
2376 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2377 struct bttv_buffer
*new;
2379 new = videobuf_sg_alloc(sizeof(*new));
2380 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2381 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2382 retval
= bttv_switch_overlay(btv
,fh
,new);
2384 mutex_unlock(&fh
->cap
.vb_lock
);
2388 /* ----------------------------------------------------------------------- */
2390 static struct videobuf_queue
* bttv_queue(struct bttv_fh
*fh
)
2392 struct videobuf_queue
* q
= NULL
;
2395 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2398 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2407 static int bttv_resource(struct bttv_fh
*fh
)
2412 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2413 res
= RESOURCE_VIDEO_STREAM
;
2415 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2424 static int bttv_switch_type(struct bttv_fh
*fh
, enum v4l2_buf_type type
)
2426 struct videobuf_queue
*q
= bttv_queue(fh
);
2427 int res
= bttv_resource(fh
);
2429 if (check_btres(fh
,res
))
2431 if (videobuf_queue_is_busy(q
))
2438 pix_format_set_size (struct v4l2_pix_format
* f
,
2439 const struct bttv_format
* fmt
,
2441 unsigned int height
)
2446 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2447 f
->bytesperline
= width
; /* Y plane */
2448 f
->sizeimage
= (width
* height
* fmt
->depth
) >> 3;
2450 f
->bytesperline
= (width
* fmt
->depth
) >> 3;
2451 f
->sizeimage
= height
* f
->bytesperline
;
2455 static int bttv_g_fmt_vid_cap(struct file
*file
, void *priv
,
2456 struct v4l2_format
*f
)
2458 struct bttv_fh
*fh
= priv
;
2460 pix_format_set_size(&f
->fmt
.pix
, fh
->fmt
,
2461 fh
->width
, fh
->height
);
2462 f
->fmt
.pix
.field
= fh
->cap
.field
;
2463 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
2468 static int bttv_g_fmt_vid_overlay(struct file
*file
, void *priv
,
2469 struct v4l2_format
*f
)
2471 struct bttv_fh
*fh
= priv
;
2473 f
->fmt
.win
.w
= fh
->ov
.w
;
2474 f
->fmt
.win
.field
= fh
->ov
.field
;
2479 static int bttv_try_fmt_vid_cap(struct file
*file
, void *priv
,
2480 struct v4l2_format
*f
)
2482 const struct bttv_format
*fmt
;
2483 struct bttv_fh
*fh
= priv
;
2484 struct bttv
*btv
= fh
->btv
;
2485 enum v4l2_field field
;
2486 __s32 width
, height
;
2489 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2493 field
= f
->fmt
.pix
.field
;
2495 if (V4L2_FIELD_ANY
== field
) {
2498 height2
= btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2499 field
= (f
->fmt
.pix
.height
> height2
)
2500 ? V4L2_FIELD_INTERLACED
2501 : V4L2_FIELD_BOTTOM
;
2504 if (V4L2_FIELD_SEQ_BT
== field
)
2505 field
= V4L2_FIELD_SEQ_TB
;
2508 case V4L2_FIELD_TOP
:
2509 case V4L2_FIELD_BOTTOM
:
2510 case V4L2_FIELD_ALTERNATE
:
2511 case V4L2_FIELD_INTERLACED
:
2513 case V4L2_FIELD_SEQ_TB
:
2514 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
)
2521 width
= f
->fmt
.pix
.width
;
2522 height
= f
->fmt
.pix
.height
;
2524 rc
= limit_scaled_size(fh
, &width
, &height
, field
,
2525 /* width_mask: 4 pixels */ ~3,
2526 /* width_bias: nearest */ 2,
2527 /* adjust_size */ 1,
2528 /* adjust_crop */ 0);
2532 /* update data for the application */
2533 f
->fmt
.pix
.field
= field
;
2534 pix_format_set_size(&f
->fmt
.pix
, fmt
, width
, height
);
2539 static int bttv_try_fmt_vid_overlay(struct file
*file
, void *priv
,
2540 struct v4l2_format
*f
)
2542 struct bttv_fh
*fh
= priv
;
2544 return verify_window(fh
, &f
->fmt
.win
,
2545 /* adjust_size */ 1,
2546 /* adjust_crop */ 0);
2549 static int bttv_s_fmt_vid_cap(struct file
*file
, void *priv
,
2550 struct v4l2_format
*f
)
2553 const struct bttv_format
*fmt
;
2554 struct bttv_fh
*fh
= priv
;
2555 struct bttv
*btv
= fh
->btv
;
2556 __s32 width
, height
;
2557 enum v4l2_field field
;
2559 retval
= bttv_switch_type(fh
, f
->type
);
2563 retval
= bttv_try_fmt_vid_cap(file
, priv
, f
);
2567 width
= f
->fmt
.pix
.width
;
2568 height
= f
->fmt
.pix
.height
;
2569 field
= f
->fmt
.pix
.field
;
2571 retval
= limit_scaled_size(fh
, &width
, &height
, f
->fmt
.pix
.field
,
2572 /* width_mask: 4 pixels */ ~3,
2573 /* width_bias: nearest */ 2,
2574 /* adjust_size */ 1,
2575 /* adjust_crop */ 1);
2579 f
->fmt
.pix
.field
= field
;
2581 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2583 /* update our state informations */
2584 mutex_lock(&fh
->cap
.vb_lock
);
2586 fh
->cap
.field
= f
->fmt
.pix
.field
;
2587 fh
->cap
.last
= V4L2_FIELD_NONE
;
2588 fh
->width
= f
->fmt
.pix
.width
;
2589 fh
->height
= f
->fmt
.pix
.height
;
2590 btv
->init
.fmt
= fmt
;
2591 btv
->init
.width
= f
->fmt
.pix
.width
;
2592 btv
->init
.height
= f
->fmt
.pix
.height
;
2593 mutex_unlock(&fh
->cap
.vb_lock
);
2598 static int bttv_s_fmt_vid_overlay(struct file
*file
, void *priv
,
2599 struct v4l2_format
*f
)
2601 struct bttv_fh
*fh
= priv
;
2602 struct bttv
*btv
= fh
->btv
;
2604 if (no_overlay
> 0) {
2605 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2609 return setup_window(fh
, btv
, &f
->fmt
.win
, 1);
2612 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2613 static int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
2617 struct bttv_fh
*fh
= priv
;
2619 mutex_lock(&fh
->cap
.vb_lock
);
2620 retval
= __videobuf_mmap_setup(&fh
->cap
, gbuffers
, gbufsize
,
2623 mutex_unlock(&fh
->cap
.vb_lock
);
2628 memset(mbuf
, 0, sizeof(*mbuf
));
2629 mbuf
->frames
= gbuffers
;
2630 mbuf
->size
= gbuffers
* gbufsize
;
2632 for (i
= 0; i
< gbuffers
; i
++)
2633 mbuf
->offsets
[i
] = i
* gbufsize
;
2635 mutex_unlock(&fh
->cap
.vb_lock
);
2640 static int bttv_querycap(struct file
*file
, void *priv
,
2641 struct v4l2_capability
*cap
)
2643 struct bttv_fh
*fh
= priv
;
2644 struct bttv
*btv
= fh
->btv
;
2649 strlcpy(cap
->driver
, "bttv", sizeof(cap
->driver
));
2650 strlcpy(cap
->card
, btv
->video_dev
->name
, sizeof(cap
->card
));
2651 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2652 "PCI:%s", pci_name(btv
->c
.pci
));
2653 cap
->version
= BTTV_VERSION_CODE
;
2655 V4L2_CAP_VIDEO_CAPTURE
|
2656 V4L2_CAP_VBI_CAPTURE
|
2657 V4L2_CAP_READWRITE
|
2659 if (btv
->has_saa6588
)
2660 cap
->capabilities
|= V4L2_CAP_RDS_CAPTURE
;
2661 if (no_overlay
<= 0)
2662 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
2664 if (btv
->tuner_type
!= TUNER_ABSENT
)
2665 cap
->capabilities
|= V4L2_CAP_TUNER
;
2669 static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc
*f
)
2673 for (i
= 0; i
< FORMATS
; i
++) {
2674 if (formats
[i
].fourcc
!= -1)
2676 if ((unsigned int)index
== f
->index
)
2682 f
->pixelformat
= formats
[i
].fourcc
;
2683 strlcpy(f
->description
, formats
[i
].name
, sizeof(f
->description
));
2688 static int bttv_enum_fmt_vid_cap(struct file
*file
, void *priv
,
2689 struct v4l2_fmtdesc
*f
)
2691 int rc
= bttv_enum_fmt_cap_ovr(f
);
2699 static int bttv_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
2700 struct v4l2_fmtdesc
*f
)
2704 if (no_overlay
> 0) {
2705 printk(KERN_ERR
"V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2709 rc
= bttv_enum_fmt_cap_ovr(f
);
2714 if (!(formats
[rc
].flags
& FORMAT_FLAGS_PACKED
))
2720 static int bttv_g_fbuf(struct file
*file
, void *f
,
2721 struct v4l2_framebuffer
*fb
)
2723 struct bttv_fh
*fh
= f
;
2724 struct bttv
*btv
= fh
->btv
;
2727 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
2729 fb
->fmt
.pixelformat
= fh
->ovfmt
->fourcc
;
2733 static int bttv_overlay(struct file
*file
, void *f
, unsigned int on
)
2735 struct bttv_fh
*fh
= f
;
2736 struct bttv
*btv
= fh
->btv
;
2737 struct bttv_buffer
*new;
2742 if (NULL
== btv
->fbuf
.base
)
2744 if (!fh
->ov
.setup_ok
) {
2745 dprintk("bttv%d: overlay: !setup_ok\n", btv
->c
.nr
);
2750 if (!check_alloc_btres(btv
, fh
, RESOURCE_OVERLAY
))
2753 mutex_lock(&fh
->cap
.vb_lock
);
2755 fh
->ov
.tvnorm
= btv
->tvnorm
;
2756 new = videobuf_sg_alloc(sizeof(*new));
2757 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2758 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2764 retval
= bttv_switch_overlay(btv
, fh
, new);
2765 mutex_unlock(&fh
->cap
.vb_lock
);
2769 static int bttv_s_fbuf(struct file
*file
, void *f
,
2770 struct v4l2_framebuffer
*fb
)
2772 struct bttv_fh
*fh
= f
;
2773 struct bttv
*btv
= fh
->btv
;
2774 const struct bttv_format
*fmt
;
2777 if (!capable(CAP_SYS_ADMIN
) &&
2778 !capable(CAP_SYS_RAWIO
))
2782 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
2785 if (0 == (fmt
->flags
& FORMAT_FLAGS_PACKED
))
2789 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2790 __s32 width
= fb
->fmt
.width
;
2791 __s32 height
= fb
->fmt
.height
;
2793 retval
= limit_scaled_size(fh
, &width
, &height
,
2794 V4L2_FIELD_INTERLACED
,
2795 /* width_mask */ ~3,
2797 /* adjust_size */ 0,
2798 /* adjust_crop */ 0);
2804 mutex_lock(&fh
->cap
.vb_lock
);
2805 btv
->fbuf
.base
= fb
->base
;
2806 btv
->fbuf
.fmt
.width
= fb
->fmt
.width
;
2807 btv
->fbuf
.fmt
.height
= fb
->fmt
.height
;
2808 if (0 != fb
->fmt
.bytesperline
)
2809 btv
->fbuf
.fmt
.bytesperline
= fb
->fmt
.bytesperline
;
2811 btv
->fbuf
.fmt
.bytesperline
= btv
->fbuf
.fmt
.width
*fmt
->depth
/8;
2815 btv
->init
.ovfmt
= fmt
;
2816 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
2819 fh
->ov
.w
.width
= fb
->fmt
.width
;
2820 fh
->ov
.w
.height
= fb
->fmt
.height
;
2821 btv
->init
.ov
.w
.width
= fb
->fmt
.width
;
2822 btv
->init
.ov
.w
.height
= fb
->fmt
.height
;
2823 kfree(fh
->ov
.clips
);
2824 fh
->ov
.clips
= NULL
;
2827 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2828 struct bttv_buffer
*new;
2830 new = videobuf_sg_alloc(sizeof(*new));
2831 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2832 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2833 retval
= bttv_switch_overlay(btv
, fh
, new);
2836 mutex_unlock(&fh
->cap
.vb_lock
);
2840 static int bttv_reqbufs(struct file
*file
, void *priv
,
2841 struct v4l2_requestbuffers
*p
)
2843 struct bttv_fh
*fh
= priv
;
2844 return videobuf_reqbufs(bttv_queue(fh
), p
);
2847 static int bttv_querybuf(struct file
*file
, void *priv
,
2848 struct v4l2_buffer
*b
)
2850 struct bttv_fh
*fh
= priv
;
2851 return videobuf_querybuf(bttv_queue(fh
), b
);
2854 static int bttv_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2856 struct bttv_fh
*fh
= priv
;
2857 struct bttv
*btv
= fh
->btv
;
2858 int res
= bttv_resource(fh
);
2860 if (!check_alloc_btres(btv
, fh
, res
))
2863 return videobuf_qbuf(bttv_queue(fh
), b
);
2866 static int bttv_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
2868 struct bttv_fh
*fh
= priv
;
2869 return videobuf_dqbuf(bttv_queue(fh
), b
,
2870 file
->f_flags
& O_NONBLOCK
);
2873 static int bttv_streamon(struct file
*file
, void *priv
,
2874 enum v4l2_buf_type type
)
2876 struct bttv_fh
*fh
= priv
;
2877 struct bttv
*btv
= fh
->btv
;
2878 int res
= bttv_resource(fh
);
2880 if (!check_alloc_btres(btv
, fh
, res
))
2882 return videobuf_streamon(bttv_queue(fh
));
2886 static int bttv_streamoff(struct file
*file
, void *priv
,
2887 enum v4l2_buf_type type
)
2889 struct bttv_fh
*fh
= priv
;
2890 struct bttv
*btv
= fh
->btv
;
2892 int res
= bttv_resource(fh
);
2895 retval
= videobuf_streamoff(bttv_queue(fh
));
2898 free_btres(btv
, fh
, res
);
2902 static int bttv_queryctrl(struct file
*file
, void *priv
,
2903 struct v4l2_queryctrl
*c
)
2905 struct bttv_fh
*fh
= priv
;
2906 struct bttv
*btv
= fh
->btv
;
2907 const struct v4l2_queryctrl
*ctrl
;
2909 if ((c
->id
< V4L2_CID_BASE
||
2910 c
->id
>= V4L2_CID_LASTP1
) &&
2911 (c
->id
< V4L2_CID_PRIVATE_BASE
||
2912 c
->id
>= V4L2_CID_PRIVATE_LASTP1
))
2915 if (!btv
->volume_gpio
&& (c
->id
== V4L2_CID_AUDIO_VOLUME
))
2918 ctrl
= ctrl_by_id(c
->id
);
2920 *c
= (NULL
!= ctrl
) ? *ctrl
: no_ctl
;
2926 static int bttv_g_parm(struct file
*file
, void *f
,
2927 struct v4l2_streamparm
*parm
)
2929 struct bttv_fh
*fh
= f
;
2930 struct bttv
*btv
= fh
->btv
;
2932 v4l2_video_std_frame_period(bttv_tvnorms
[btv
->tvnorm
].v4l2_id
,
2933 &parm
->parm
.capture
.timeperframe
);
2937 static int bttv_g_tuner(struct file
*file
, void *priv
,
2938 struct v4l2_tuner
*t
)
2940 struct bttv_fh
*fh
= priv
;
2941 struct bttv
*btv
= fh
->btv
;
2943 if (btv
->tuner_type
== TUNER_ABSENT
)
2948 mutex_lock(&btv
->lock
);
2949 t
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
2950 bttv_call_all(btv
, tuner
, g_tuner
, t
);
2951 strcpy(t
->name
, "Television");
2952 t
->capability
= V4L2_TUNER_CAP_NORM
;
2953 t
->type
= V4L2_TUNER_ANALOG_TV
;
2954 if (btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
)
2957 if (btv
->audio_mode_gpio
)
2958 btv
->audio_mode_gpio(btv
, t
, 0);
2960 mutex_unlock(&btv
->lock
);
2964 static int bttv_g_priority(struct file
*file
, void *f
, enum v4l2_priority
*p
)
2966 struct bttv_fh
*fh
= f
;
2967 struct bttv
*btv
= fh
->btv
;
2969 *p
= v4l2_prio_max(&btv
->prio
);
2974 static int bttv_s_priority(struct file
*file
, void *f
,
2975 enum v4l2_priority prio
)
2977 struct bttv_fh
*fh
= f
;
2978 struct bttv
*btv
= fh
->btv
;
2980 return v4l2_prio_change(&btv
->prio
, &fh
->prio
, prio
);
2983 static int bttv_cropcap(struct file
*file
, void *priv
,
2984 struct v4l2_cropcap
*cap
)
2986 struct bttv_fh
*fh
= priv
;
2987 struct bttv
*btv
= fh
->btv
;
2989 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
2990 cap
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
2993 *cap
= bttv_tvnorms
[btv
->tvnorm
].cropcap
;
2998 static int bttv_g_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
3000 struct bttv_fh
*fh
= f
;
3001 struct bttv
*btv
= fh
->btv
;
3003 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
3004 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
3007 /* No fh->do_crop = 1; because btv->crop[1] may be
3008 inconsistent with fh->width or fh->height and apps
3009 do not expect a change here. */
3011 crop
->c
= btv
->crop
[!!fh
->do_crop
].rect
;
3016 static int bttv_s_crop(struct file
*file
, void *f
, struct v4l2_crop
*crop
)
3018 struct bttv_fh
*fh
= f
;
3019 struct bttv
*btv
= fh
->btv
;
3020 const struct v4l2_rect
*b
;
3028 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
3029 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
3032 retval
= v4l2_prio_check(&btv
->prio
, &fh
->prio
);
3036 /* Make sure tvnorm, vbi_end and the current cropping
3037 parameters remain consistent until we're done. Note
3038 read() may change vbi_end in check_alloc_btres(). */
3039 mutex_lock(&btv
->lock
);
3043 if (locked_btres(fh
->btv
, VIDEO_RESOURCES
)) {
3044 mutex_unlock(&btv
->lock
);
3048 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
3051 b_right
= b_left
+ b
->width
;
3052 b_bottom
= b
->top
+ b
->height
;
3054 b_top
= max(b
->top
, btv
->vbi_end
);
3055 if (b_top
+ 32 >= b_bottom
) {
3056 mutex_unlock(&btv
->lock
);
3060 /* Min. scaled size 48 x 32. */
3061 c
.rect
.left
= clamp(crop
->c
.left
, b_left
, b_right
- 48);
3062 c
.rect
.left
= min(c
.rect
.left
, (__s32
) MAX_HDELAY
);
3064 c
.rect
.width
= clamp(crop
->c
.width
,
3065 48, b_right
- c
.rect
.left
);
3067 c
.rect
.top
= clamp(crop
->c
.top
, b_top
, b_bottom
- 32);
3068 /* Top and height must be a multiple of two. */
3069 c
.rect
.top
= (c
.rect
.top
+ 1) & ~1;
3071 c
.rect
.height
= clamp(crop
->c
.height
,
3072 32, b_bottom
- c
.rect
.top
);
3073 c
.rect
.height
= (c
.rect
.height
+ 1) & ~1;
3075 bttv_crop_calc_limits(&c
);
3079 mutex_unlock(&btv
->lock
);
3083 mutex_lock(&fh
->cap
.vb_lock
);
3085 if (fh
->width
< c
.min_scaled_width
) {
3086 fh
->width
= c
.min_scaled_width
;
3087 btv
->init
.width
= c
.min_scaled_width
;
3088 } else if (fh
->width
> c
.max_scaled_width
) {
3089 fh
->width
= c
.max_scaled_width
;
3090 btv
->init
.width
= c
.max_scaled_width
;
3093 if (fh
->height
< c
.min_scaled_height
) {
3094 fh
->height
= c
.min_scaled_height
;
3095 btv
->init
.height
= c
.min_scaled_height
;
3096 } else if (fh
->height
> c
.max_scaled_height
) {
3097 fh
->height
= c
.max_scaled_height
;
3098 btv
->init
.height
= c
.max_scaled_height
;
3101 mutex_unlock(&fh
->cap
.vb_lock
);
3106 static int bttv_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
3108 if (unlikely(a
->index
))
3111 strcpy(a
->name
, "audio");
3115 static int bttv_s_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
3117 if (unlikely(a
->index
))
3123 static ssize_t
bttv_read(struct file
*file
, char __user
*data
,
3124 size_t count
, loff_t
*ppos
)
3126 struct bttv_fh
*fh
= file
->private_data
;
3129 if (fh
->btv
->errors
)
3130 bttv_reinit_bt848(fh
->btv
);
3131 dprintk("bttv%d: read count=%d type=%s\n",
3132 fh
->btv
->c
.nr
,(int)count
,v4l2_type_names
[fh
->type
]);
3135 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
3136 if (!check_alloc_btres(fh
->btv
, fh
, RESOURCE_VIDEO_READ
)) {
3137 /* VIDEO_READ in use by another fh,
3138 or VIDEO_STREAM by any fh. */
3141 retval
= videobuf_read_one(&fh
->cap
, data
, count
, ppos
,
3142 file
->f_flags
& O_NONBLOCK
);
3143 free_btres(fh
->btv
, fh
, RESOURCE_VIDEO_READ
);
3145 case V4L2_BUF_TYPE_VBI_CAPTURE
:
3146 if (!check_alloc_btres(fh
->btv
,fh
,RESOURCE_VBI
))
3148 retval
= videobuf_read_stream(&fh
->vbi
, data
, count
, ppos
, 1,
3149 file
->f_flags
& O_NONBLOCK
);
3157 static unsigned int bttv_poll(struct file
*file
, poll_table
*wait
)
3159 struct bttv_fh
*fh
= file
->private_data
;
3160 struct bttv_buffer
*buf
;
3161 enum v4l2_field field
;
3162 unsigned int rc
= POLLERR
;
3164 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
3165 if (!check_alloc_btres(fh
->btv
,fh
,RESOURCE_VBI
))
3167 return videobuf_poll_stream(file
, &fh
->vbi
, wait
);
3170 if (check_btres(fh
,RESOURCE_VIDEO_STREAM
)) {
3171 mutex_lock(&fh
->cap
.vb_lock
);
3172 /* streaming capture */
3173 if (list_empty(&fh
->cap
.stream
))
3175 buf
= list_entry(fh
->cap
.stream
.next
,struct bttv_buffer
,vb
.stream
);
3177 /* read() capture */
3178 mutex_lock(&fh
->cap
.vb_lock
);
3179 if (NULL
== fh
->cap
.read_buf
) {
3180 /* need to capture a new frame */
3181 if (locked_btres(fh
->btv
,RESOURCE_VIDEO_STREAM
))
3183 fh
->cap
.read_buf
= videobuf_sg_alloc(fh
->cap
.msize
);
3184 if (NULL
== fh
->cap
.read_buf
)
3186 fh
->cap
.read_buf
->memory
= V4L2_MEMORY_USERPTR
;
3187 field
= videobuf_next_field(&fh
->cap
);
3188 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,field
)) {
3189 kfree (fh
->cap
.read_buf
);
3190 fh
->cap
.read_buf
= NULL
;
3193 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
3194 fh
->cap
.read_off
= 0;
3196 mutex_unlock(&fh
->cap
.vb_lock
);
3197 buf
= (struct bttv_buffer
*)fh
->cap
.read_buf
;
3200 poll_wait(file
, &buf
->vb
.done
, wait
);
3201 if (buf
->vb
.state
== VIDEOBUF_DONE
||
3202 buf
->vb
.state
== VIDEOBUF_ERROR
)
3203 rc
= POLLIN
|POLLRDNORM
;
3207 mutex_unlock(&fh
->cap
.vb_lock
);
3211 static int bttv_open(struct file
*file
)
3213 struct video_device
*vdev
= video_devdata(file
);
3214 struct bttv
*btv
= video_drvdata(file
);
3216 enum v4l2_buf_type type
= 0;
3218 dprintk(KERN_DEBUG
"bttv: open dev=%s\n", video_device_node_name(vdev
));
3220 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
) {
3221 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
3222 } else if (vdev
->vfl_type
== VFL_TYPE_VBI
) {
3223 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
3231 dprintk(KERN_DEBUG
"bttv%d: open called (type=%s)\n",
3232 btv
->c
.nr
,v4l2_type_names
[type
]);
3234 /* allocate per filehandle data */
3235 fh
= kmalloc(sizeof(*fh
),GFP_KERNEL
);
3240 file
->private_data
= fh
;
3243 fh
->ov
.setup_ok
= 0;
3244 v4l2_prio_open(&btv
->prio
,&fh
->prio
);
3246 videobuf_queue_sg_init(&fh
->cap
, &bttv_video_qops
,
3247 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3248 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
3249 V4L2_FIELD_INTERLACED
,
3250 sizeof(struct bttv_buffer
),
3252 videobuf_queue_sg_init(&fh
->vbi
, &bttv_vbi_qops
,
3253 &btv
->c
.pci
->dev
, &btv
->s_lock
,
3254 V4L2_BUF_TYPE_VBI_CAPTURE
,
3256 sizeof(struct bttv_buffer
),
3258 set_tvnorm(btv
,btv
->tvnorm
);
3259 set_input(btv
, btv
->input
, btv
->tvnorm
);
3263 /* The V4L2 spec requires one global set of cropping parameters
3264 which only change on request. These are stored in btv->crop[1].
3265 However for compatibility with V4L apps and cropping unaware
3266 V4L2 apps we now reset the cropping parameters as seen through
3267 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3268 will use btv->crop[0], the default cropping parameters for the
3269 current video standard, and VIDIOC_S_FMT will not implicitely
3270 change the cropping parameters until VIDIOC_S_CROP has been
3272 fh
->do_crop
= !reset_crop
; /* module parameter */
3274 /* Likewise there should be one global set of VBI capture
3275 parameters, but for compatibility with V4L apps and earlier
3276 driver versions each fh has its own parameters. */
3277 bttv_vbi_fmt_reset(&fh
->vbi_fmt
, btv
->tvnorm
);
3279 bttv_field_count(btv
);
3284 static int bttv_release(struct file
*file
)
3286 struct bttv_fh
*fh
= file
->private_data
;
3287 struct bttv
*btv
= fh
->btv
;
3289 /* turn off overlay */
3290 if (check_btres(fh
, RESOURCE_OVERLAY
))
3291 bttv_switch_overlay(btv
,fh
,NULL
);
3293 /* stop video capture */
3294 if (check_btres(fh
, RESOURCE_VIDEO_STREAM
)) {
3295 videobuf_streamoff(&fh
->cap
);
3296 free_btres(btv
,fh
,RESOURCE_VIDEO_STREAM
);
3298 if (fh
->cap
.read_buf
) {
3299 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
3300 kfree(fh
->cap
.read_buf
);
3302 if (check_btres(fh
, RESOURCE_VIDEO_READ
)) {
3303 free_btres(btv
, fh
, RESOURCE_VIDEO_READ
);
3306 /* stop vbi capture */
3307 if (check_btres(fh
, RESOURCE_VBI
)) {
3308 videobuf_stop(&fh
->vbi
);
3309 free_btres(btv
,fh
,RESOURCE_VBI
);
3313 videobuf_mmap_free(&fh
->cap
);
3314 videobuf_mmap_free(&fh
->vbi
);
3315 v4l2_prio_close(&btv
->prio
,&fh
->prio
);
3316 file
->private_data
= NULL
;
3320 bttv_field_count(btv
);
3329 bttv_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3331 struct bttv_fh
*fh
= file
->private_data
;
3333 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3334 fh
->btv
->c
.nr
, v4l2_type_names
[fh
->type
],
3335 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
3336 return videobuf_mmap_mapper(bttv_queue(fh
),vma
);
3339 static const struct v4l2_file_operations bttv_fops
=
3341 .owner
= THIS_MODULE
,
3343 .release
= bttv_release
,
3344 .ioctl
= video_ioctl2
,
3350 static const struct v4l2_ioctl_ops bttv_ioctl_ops
= {
3351 .vidioc_querycap
= bttv_querycap
,
3352 .vidioc_enum_fmt_vid_cap
= bttv_enum_fmt_vid_cap
,
3353 .vidioc_g_fmt_vid_cap
= bttv_g_fmt_vid_cap
,
3354 .vidioc_try_fmt_vid_cap
= bttv_try_fmt_vid_cap
,
3355 .vidioc_s_fmt_vid_cap
= bttv_s_fmt_vid_cap
,
3356 .vidioc_enum_fmt_vid_overlay
= bttv_enum_fmt_vid_overlay
,
3357 .vidioc_g_fmt_vid_overlay
= bttv_g_fmt_vid_overlay
,
3358 .vidioc_try_fmt_vid_overlay
= bttv_try_fmt_vid_overlay
,
3359 .vidioc_s_fmt_vid_overlay
= bttv_s_fmt_vid_overlay
,
3360 .vidioc_g_fmt_vbi_cap
= bttv_g_fmt_vbi_cap
,
3361 .vidioc_try_fmt_vbi_cap
= bttv_try_fmt_vbi_cap
,
3362 .vidioc_s_fmt_vbi_cap
= bttv_s_fmt_vbi_cap
,
3363 .vidioc_g_audio
= bttv_g_audio
,
3364 .vidioc_s_audio
= bttv_s_audio
,
3365 .vidioc_cropcap
= bttv_cropcap
,
3366 .vidioc_reqbufs
= bttv_reqbufs
,
3367 .vidioc_querybuf
= bttv_querybuf
,
3368 .vidioc_qbuf
= bttv_qbuf
,
3369 .vidioc_dqbuf
= bttv_dqbuf
,
3370 .vidioc_s_std
= bttv_s_std
,
3371 .vidioc_enum_input
= bttv_enum_input
,
3372 .vidioc_g_input
= bttv_g_input
,
3373 .vidioc_s_input
= bttv_s_input
,
3374 .vidioc_queryctrl
= bttv_queryctrl
,
3375 .vidioc_g_ctrl
= bttv_g_ctrl
,
3376 .vidioc_s_ctrl
= bttv_s_ctrl
,
3377 .vidioc_streamon
= bttv_streamon
,
3378 .vidioc_streamoff
= bttv_streamoff
,
3379 .vidioc_g_tuner
= bttv_g_tuner
,
3380 .vidioc_s_tuner
= bttv_s_tuner
,
3381 #ifdef CONFIG_VIDEO_V4L1_COMPAT
3382 .vidiocgmbuf
= vidiocgmbuf
,
3384 .vidioc_g_crop
= bttv_g_crop
,
3385 .vidioc_s_crop
= bttv_s_crop
,
3386 .vidioc_g_fbuf
= bttv_g_fbuf
,
3387 .vidioc_s_fbuf
= bttv_s_fbuf
,
3388 .vidioc_overlay
= bttv_overlay
,
3389 .vidioc_g_priority
= bttv_g_priority
,
3390 .vidioc_s_priority
= bttv_s_priority
,
3391 .vidioc_g_parm
= bttv_g_parm
,
3392 .vidioc_g_frequency
= bttv_g_frequency
,
3393 .vidioc_s_frequency
= bttv_s_frequency
,
3394 .vidioc_log_status
= bttv_log_status
,
3395 .vidioc_querystd
= bttv_querystd
,
3396 #ifdef CONFIG_VIDEO_ADV_DEBUG
3397 .vidioc_g_register
= bttv_g_register
,
3398 .vidioc_s_register
= bttv_s_register
,
3402 static struct video_device bttv_video_template
= {
3404 .ioctl_ops
= &bttv_ioctl_ops
,
3405 .tvnorms
= BTTV_NORMS
,
3406 .current_norm
= V4L2_STD_PAL
,
3409 /* ----------------------------------------------------------------------- */
3410 /* radio interface */
3412 static int radio_open(struct file
*file
)
3414 struct video_device
*vdev
= video_devdata(file
);
3415 struct bttv
*btv
= video_drvdata(file
);
3418 dprintk("bttv: open dev=%s\n", video_device_node_name(vdev
));
3422 dprintk("bttv%d: open called (radio)\n",btv
->c
.nr
);
3424 /* allocate per filehandle data */
3425 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
3430 file
->private_data
= fh
;
3432 v4l2_prio_open(&btv
->prio
, &fh
->prio
);
3434 mutex_lock(&btv
->lock
);
3438 bttv_call_all(btv
, tuner
, s_radio
);
3439 audio_input(btv
,TVAUDIO_INPUT_RADIO
);
3441 mutex_unlock(&btv
->lock
);
3446 static int radio_release(struct file
*file
)
3448 struct bttv_fh
*fh
= file
->private_data
;
3449 struct bttv
*btv
= fh
->btv
;
3450 struct rds_command cmd
;
3452 v4l2_prio_close(&btv
->prio
,&fh
->prio
);
3453 file
->private_data
= NULL
;
3458 bttv_call_all(btv
, core
, ioctl
, RDS_CMD_CLOSE
, &cmd
);
3463 static int radio_querycap(struct file
*file
, void *priv
,
3464 struct v4l2_capability
*cap
)
3466 struct bttv_fh
*fh
= priv
;
3467 struct bttv
*btv
= fh
->btv
;
3469 strcpy(cap
->driver
, "bttv");
3470 strlcpy(cap
->card
, btv
->radio_dev
->name
, sizeof(cap
->card
));
3471 sprintf(cap
->bus_info
, "PCI:%s", pci_name(btv
->c
.pci
));
3472 cap
->version
= BTTV_VERSION_CODE
;
3473 cap
->capabilities
= V4L2_CAP_TUNER
;
3478 static int radio_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
3480 struct bttv_fh
*fh
= priv
;
3481 struct bttv
*btv
= fh
->btv
;
3483 if (btv
->tuner_type
== TUNER_ABSENT
)
3487 mutex_lock(&btv
->lock
);
3488 strcpy(t
->name
, "Radio");
3489 t
->type
= V4L2_TUNER_RADIO
;
3491 bttv_call_all(btv
, tuner
, g_tuner
, t
);
3493 if (btv
->audio_mode_gpio
)
3494 btv
->audio_mode_gpio(btv
, t
, 0);
3496 mutex_unlock(&btv
->lock
);
3501 static int radio_enum_input(struct file
*file
, void *priv
,
3502 struct v4l2_input
*i
)
3507 strcpy(i
->name
, "Radio");
3508 i
->type
= V4L2_INPUT_TYPE_TUNER
;
3513 static int radio_g_audio(struct file
*file
, void *priv
,
3514 struct v4l2_audio
*a
)
3516 if (unlikely(a
->index
))
3519 strcpy(a
->name
, "Radio");
3524 static int radio_s_tuner(struct file
*file
, void *priv
,
3525 struct v4l2_tuner
*t
)
3527 struct bttv_fh
*fh
= priv
;
3528 struct bttv
*btv
= fh
->btv
;
3533 bttv_call_all(btv
, tuner
, g_tuner
, t
);
3537 static int radio_s_audio(struct file
*file
, void *priv
,
3538 struct v4l2_audio
*a
)
3540 if (unlikely(a
->index
))
3546 static int radio_s_input(struct file
*filp
, void *priv
, unsigned int i
)
3554 static int radio_s_std(struct file
*file
, void *fh
, v4l2_std_id
*norm
)
3559 static int radio_queryctrl(struct file
*file
, void *priv
,
3560 struct v4l2_queryctrl
*c
)
3562 const struct v4l2_queryctrl
*ctrl
;
3564 if (c
->id
< V4L2_CID_BASE
||
3565 c
->id
>= V4L2_CID_LASTP1
)
3568 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
3569 ctrl
= ctrl_by_id(c
->id
);
3577 static int radio_g_input(struct file
*filp
, void *priv
, unsigned int *i
)
3583 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
3584 size_t count
, loff_t
*ppos
)
3586 struct bttv_fh
*fh
= file
->private_data
;
3587 struct bttv
*btv
= fh
->btv
;
3588 struct rds_command cmd
;
3589 cmd
.block_count
= count
/3;
3591 cmd
.instance
= file
;
3592 cmd
.result
= -ENODEV
;
3594 bttv_call_all(btv
, core
, ioctl
, RDS_CMD_READ
, &cmd
);
3599 static unsigned int radio_poll(struct file
*file
, poll_table
*wait
)
3601 struct bttv_fh
*fh
= file
->private_data
;
3602 struct bttv
*btv
= fh
->btv
;
3603 struct rds_command cmd
;
3604 cmd
.instance
= file
;
3605 cmd
.event_list
= wait
;
3606 cmd
.result
= -ENODEV
;
3607 bttv_call_all(btv
, core
, ioctl
, RDS_CMD_POLL
, &cmd
);
3612 static const struct v4l2_file_operations radio_fops
=
3614 .owner
= THIS_MODULE
,
3617 .release
= radio_release
,
3618 .ioctl
= video_ioctl2
,
3622 static const struct v4l2_ioctl_ops radio_ioctl_ops
= {
3623 .vidioc_querycap
= radio_querycap
,
3624 .vidioc_g_tuner
= radio_g_tuner
,
3625 .vidioc_enum_input
= radio_enum_input
,
3626 .vidioc_g_audio
= radio_g_audio
,
3627 .vidioc_s_tuner
= radio_s_tuner
,
3628 .vidioc_s_audio
= radio_s_audio
,
3629 .vidioc_s_input
= radio_s_input
,
3630 .vidioc_s_std
= radio_s_std
,
3631 .vidioc_queryctrl
= radio_queryctrl
,
3632 .vidioc_g_input
= radio_g_input
,
3633 .vidioc_g_ctrl
= bttv_g_ctrl
,
3634 .vidioc_s_ctrl
= bttv_s_ctrl
,
3635 .vidioc_g_frequency
= bttv_g_frequency
,
3636 .vidioc_s_frequency
= bttv_s_frequency
,
3639 static struct video_device radio_template
= {
3640 .fops
= &radio_fops
,
3641 .ioctl_ops
= &radio_ioctl_ops
,
3644 /* ----------------------------------------------------------------------- */
3645 /* some debug code */
3647 static int bttv_risc_decode(u32 risc
)
3649 static char *instr
[16] = {
3650 [ BT848_RISC_WRITE
>> 28 ] = "write",
3651 [ BT848_RISC_SKIP
>> 28 ] = "skip",
3652 [ BT848_RISC_WRITEC
>> 28 ] = "writec",
3653 [ BT848_RISC_JUMP
>> 28 ] = "jump",
3654 [ BT848_RISC_SYNC
>> 28 ] = "sync",
3655 [ BT848_RISC_WRITE123
>> 28 ] = "write123",
3656 [ BT848_RISC_SKIP123
>> 28 ] = "skip123",
3657 [ BT848_RISC_WRITE1S23
>> 28 ] = "write1s23",
3659 static int incr
[16] = {
3660 [ BT848_RISC_WRITE
>> 28 ] = 2,
3661 [ BT848_RISC_JUMP
>> 28 ] = 2,
3662 [ BT848_RISC_SYNC
>> 28 ] = 2,
3663 [ BT848_RISC_WRITE123
>> 28 ] = 5,
3664 [ BT848_RISC_SKIP123
>> 28 ] = 2,
3665 [ BT848_RISC_WRITE1S23
>> 28 ] = 3,
3667 static char *bits
[] = {
3668 "be0", "be1", "be2", "be3/resync",
3669 "set0", "set1", "set2", "set3",
3670 "clr0", "clr1", "clr2", "clr3",
3671 "irq", "res", "eol", "sol",
3675 printk("0x%08x [ %s", risc
,
3676 instr
[risc
>> 28] ? instr
[risc
>> 28] : "INVALID");
3677 for (i
= ARRAY_SIZE(bits
)-1; i
>= 0; i
--)
3678 if (risc
& (1 << (i
+ 12)))
3679 printk(" %s",bits
[i
]);
3680 printk(" count=%d ]\n", risc
& 0xfff);
3681 return incr
[risc
>> 28] ? incr
[risc
>> 28] : 1;
3684 static void bttv_risc_disasm(struct bttv
*btv
,
3685 struct btcx_riscmem
*risc
)
3689 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3690 btv
->c
.v4l2_dev
.name
, risc
->cpu
, (unsigned long)risc
->dma
);
3691 for (i
= 0; i
< (risc
->size
>> 2); i
+= n
) {
3692 printk("%s: 0x%lx: ", btv
->c
.v4l2_dev
.name
,
3693 (unsigned long)(risc
->dma
+ (i
<<2)));
3694 n
= bttv_risc_decode(le32_to_cpu(risc
->cpu
[i
]));
3695 for (j
= 1; j
< n
; j
++)
3696 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3697 btv
->c
.v4l2_dev
.name
, (unsigned long)(risc
->dma
+ ((i
+j
)<<2)),
3699 if (0 == risc
->cpu
[i
])
3704 static void bttv_print_riscaddr(struct bttv
*btv
)
3706 printk(" main: %08Lx\n",
3707 (unsigned long long)btv
->main
.dma
);
3708 printk(" vbi : o=%08Lx e=%08Lx\n",
3709 btv
->cvbi
? (unsigned long long)btv
->cvbi
->top
.dma
: 0,
3710 btv
->cvbi
? (unsigned long long)btv
->cvbi
->bottom
.dma
: 0);
3711 printk(" cap : o=%08Lx e=%08Lx\n",
3712 btv
->curr
.top
? (unsigned long long)btv
->curr
.top
->top
.dma
: 0,
3713 btv
->curr
.bottom
? (unsigned long long)btv
->curr
.bottom
->bottom
.dma
: 0);
3714 printk(" scr : o=%08Lx e=%08Lx\n",
3715 btv
->screen
? (unsigned long long)btv
->screen
->top
.dma
: 0,
3716 btv
->screen
? (unsigned long long)btv
->screen
->bottom
.dma
: 0);
3717 bttv_risc_disasm(btv
, &btv
->main
);
3720 /* ----------------------------------------------------------------------- */
3723 static char *irq_name
[] = {
3724 "FMTCHG", // format change detected (525 vs. 625)
3725 "VSYNC", // vertical sync (new field)
3726 "HSYNC", // horizontal sync
3727 "OFLOW", // chroma/luma AGC overflow
3728 "HLOCK", // horizontal lock changed
3729 "VPRES", // video presence changed
3731 "I2CDONE", // hw irc operation finished
3732 "GPINT", // gpio port triggered irq
3734 "RISCI", // risc instruction triggered irq
3735 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3736 "FTRGT", // pixel data fifo overrun
3737 "FDSR", // fifo data stream resyncronisation
3738 "PPERR", // parity error (data transfer)
3739 "RIPERR", // parity error (read risc instructions)
3740 "PABORT", // pci abort
3741 "OCERR", // risc instruction error
3742 "SCERR", // syncronisation error
3745 static void bttv_print_irqbits(u32 print
, u32 mark
)
3750 for (i
= 0; i
< ARRAY_SIZE(irq_name
); i
++) {
3751 if (print
& (1 << i
))
3752 printk(" %s",irq_name
[i
]);
3753 if (mark
& (1 << i
))
3758 static void bttv_irq_debug_low_latency(struct bttv
*btv
, u32 rc
)
3760 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3762 (unsigned long)btv
->main
.dma
,
3763 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_VBI
+1]),
3764 (unsigned long)le32_to_cpu(btv
->main
.cpu
[RISC_SLOT_O_FIELD
+1]),
3767 if (0 == (btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
)) {
3768 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3769 "Ok, then this is harmless, don't worry ;)\n",
3773 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3775 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3781 bttv_irq_next_video(struct bttv
*btv
, struct bttv_buffer_set
*set
)
3783 struct bttv_buffer
*item
;
3785 memset(set
,0,sizeof(*set
));
3787 /* capture request ? */
3788 if (!list_empty(&btv
->capture
)) {
3790 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3791 if (V4L2_FIELD_HAS_TOP(item
->vb
.field
))
3793 if (V4L2_FIELD_HAS_BOTTOM(item
->vb
.field
))
3796 /* capture request for other field ? */
3797 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
) &&
3798 (item
->vb
.queue
.next
!= &btv
->capture
)) {
3799 item
= list_entry(item
->vb
.queue
.next
, struct bttv_buffer
, vb
.queue
);
3800 /* Mike Isely <isely@pobox.com> - Only check
3801 * and set up the bottom field in the logic
3802 * below. Don't ever do the top field. This
3803 * of course means that if we set up the
3804 * bottom field in the above code that we'll
3805 * actually skip a field. But that's OK.
3806 * Having processed only a single buffer this
3807 * time, then the next time around the first
3808 * available buffer should be for a top field.
3809 * That will then cause us here to set up a
3810 * top then a bottom field in the normal way.
3811 * The alternative to this understanding is
3812 * that we set up the second available buffer
3813 * as a top field, but that's out of order
3814 * since this driver always processes the top
3815 * field first - the effect will be the two
3816 * buffers being returned in the wrong order,
3817 * with the second buffer also being delayed
3818 * by one field time (owing to the fifo nature
3819 * of videobuf). Worse still, we'll be stuck
3820 * doing fields out of order now every time
3821 * until something else causes a field to be
3822 * dropped. By effectively forcing a field to
3823 * drop this way then we always get back into
3824 * sync within a single frame time. (Out of
3825 * order fields can screw up deinterlacing
3827 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
)) {
3828 if (NULL
== set
->bottom
&&
3829 V4L2_FIELD_BOTTOM
== item
->vb
.field
) {
3832 if (NULL
!= set
->top
&& NULL
!= set
->bottom
)
3838 /* screen overlay ? */
3839 if (NULL
!= btv
->screen
) {
3840 if (V4L2_FIELD_HAS_BOTH(btv
->screen
->vb
.field
)) {
3841 if (NULL
== set
->top
&& NULL
== set
->bottom
) {
3842 set
->top
= btv
->screen
;
3843 set
->bottom
= btv
->screen
;
3846 if (V4L2_FIELD_TOP
== btv
->screen
->vb
.field
&&
3848 set
->top
= btv
->screen
;
3850 if (V4L2_FIELD_BOTTOM
== btv
->screen
->vb
.field
&&
3851 NULL
== set
->bottom
) {
3852 set
->bottom
= btv
->screen
;
3857 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3858 btv
->c
.nr
,set
->top
, set
->bottom
,
3859 btv
->screen
,set
->frame_irq
,set
->top_irq
);
3864 bttv_irq_wakeup_video(struct bttv
*btv
, struct bttv_buffer_set
*wakeup
,
3865 struct bttv_buffer_set
*curr
, unsigned int state
)
3869 do_gettimeofday(&ts
);
3871 if (wakeup
->top
== wakeup
->bottom
) {
3872 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3874 printk("bttv%d: wakeup: both=%p\n",btv
->c
.nr
,wakeup
->top
);
3875 wakeup
->top
->vb
.ts
= ts
;
3876 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3877 wakeup
->top
->vb
.state
= state
;
3878 wake_up(&wakeup
->top
->vb
.done
);
3881 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
3883 printk("bttv%d: wakeup: top=%p\n",btv
->c
.nr
,wakeup
->top
);
3884 wakeup
->top
->vb
.ts
= ts
;
3885 wakeup
->top
->vb
.field_count
= btv
->field_count
;
3886 wakeup
->top
->vb
.state
= state
;
3887 wake_up(&wakeup
->top
->vb
.done
);
3889 if (NULL
!= wakeup
->bottom
&& curr
->bottom
!= wakeup
->bottom
) {
3891 printk("bttv%d: wakeup: bottom=%p\n",btv
->c
.nr
,wakeup
->bottom
);
3892 wakeup
->bottom
->vb
.ts
= ts
;
3893 wakeup
->bottom
->vb
.field_count
= btv
->field_count
;
3894 wakeup
->bottom
->vb
.state
= state
;
3895 wake_up(&wakeup
->bottom
->vb
.done
);
3901 bttv_irq_wakeup_vbi(struct bttv
*btv
, struct bttv_buffer
*wakeup
,
3909 do_gettimeofday(&ts
);
3911 wakeup
->vb
.field_count
= btv
->field_count
;
3912 wakeup
->vb
.state
= state
;
3913 wake_up(&wakeup
->vb
.done
);
3916 static void bttv_irq_timeout(unsigned long data
)
3918 struct bttv
*btv
= (struct bttv
*)data
;
3919 struct bttv_buffer_set old
,new;
3920 struct bttv_buffer
*ovbi
;
3921 struct bttv_buffer
*item
;
3922 unsigned long flags
;
3925 printk(KERN_INFO
"bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3926 btv
->c
.nr
, btv
->framedrop
, btv
->irq_me
, btv
->irq_total
,
3927 btread(BT848_RISC_COUNT
));
3928 bttv_print_irqbits(btread(BT848_INT_STAT
),0);
3932 spin_lock_irqsave(&btv
->s_lock
,flags
);
3934 /* deactivate stuff */
3935 memset(&new,0,sizeof(new));
3941 bttv_buffer_activate_video(btv
, &new);
3942 bttv_buffer_activate_vbi(btv
, NULL
);
3943 bttv_set_dma(btv
, 0);
3946 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_ERROR
);
3947 bttv_irq_wakeup_vbi(btv
, ovbi
, VIDEOBUF_ERROR
);
3949 /* cancel all outstanding capture / vbi requests */
3950 while (!list_empty(&btv
->capture
)) {
3951 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
3952 list_del(&item
->vb
.queue
);
3953 item
->vb
.state
= VIDEOBUF_ERROR
;
3954 wake_up(&item
->vb
.done
);
3956 while (!list_empty(&btv
->vcapture
)) {
3957 item
= list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
3958 list_del(&item
->vb
.queue
);
3959 item
->vb
.state
= VIDEOBUF_ERROR
;
3960 wake_up(&item
->vb
.done
);
3964 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
3968 bttv_irq_wakeup_top(struct bttv
*btv
)
3970 struct bttv_buffer
*wakeup
= btv
->curr
.top
;
3975 spin_lock(&btv
->s_lock
);
3976 btv
->curr
.top_irq
= 0;
3977 btv
->curr
.top
= NULL
;
3978 bttv_risc_hook(btv
, RISC_SLOT_O_FIELD
, NULL
, 0);
3980 do_gettimeofday(&wakeup
->vb
.ts
);
3981 wakeup
->vb
.field_count
= btv
->field_count
;
3982 wakeup
->vb
.state
= VIDEOBUF_DONE
;
3983 wake_up(&wakeup
->vb
.done
);
3984 spin_unlock(&btv
->s_lock
);
3987 static inline int is_active(struct btcx_riscmem
*risc
, u32 rc
)
3991 if (rc
> risc
->dma
+ risc
->size
)
3997 bttv_irq_switch_video(struct bttv
*btv
)
3999 struct bttv_buffer_set
new;
4000 struct bttv_buffer_set old
;
4003 spin_lock(&btv
->s_lock
);
4005 /* new buffer set */
4006 bttv_irq_next_video(btv
, &new);
4007 rc
= btread(BT848_RISC_COUNT
);
4008 if ((btv
->curr
.top
&& is_active(&btv
->curr
.top
->top
, rc
)) ||
4009 (btv
->curr
.bottom
&& is_active(&btv
->curr
.bottom
->bottom
, rc
))) {
4012 bttv_irq_debug_low_latency(btv
, rc
);
4013 spin_unlock(&btv
->s_lock
);
4020 btv
->loop_irq
&= ~1;
4021 bttv_buffer_activate_video(btv
, &new);
4022 bttv_set_dma(btv
, 0);
4025 if (UNSET
!= btv
->new_input
) {
4026 video_mux(btv
,btv
->new_input
);
4027 btv
->new_input
= UNSET
;
4030 /* wake up finished buffers */
4031 bttv_irq_wakeup_video(btv
, &old
, &new, VIDEOBUF_DONE
);
4032 spin_unlock(&btv
->s_lock
);
4036 bttv_irq_switch_vbi(struct bttv
*btv
)
4038 struct bttv_buffer
*new = NULL
;
4039 struct bttv_buffer
*old
;
4042 spin_lock(&btv
->s_lock
);
4044 if (!list_empty(&btv
->vcapture
))
4045 new = list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
4048 rc
= btread(BT848_RISC_COUNT
);
4049 if (NULL
!= old
&& (is_active(&old
->top
, rc
) ||
4050 is_active(&old
->bottom
, rc
))) {
4053 bttv_irq_debug_low_latency(btv
, rc
);
4054 spin_unlock(&btv
->s_lock
);
4060 btv
->loop_irq
&= ~4;
4061 bttv_buffer_activate_vbi(btv
, new);
4062 bttv_set_dma(btv
, 0);
4064 bttv_irq_wakeup_vbi(btv
, old
, VIDEOBUF_DONE
);
4065 spin_unlock(&btv
->s_lock
);
4068 static irqreturn_t
bttv_irq(int irq
, void *dev_id
)
4076 btv
=(struct bttv
*)dev_id
;
4078 if (btv
->custom_irq
)
4079 handled
= btv
->custom_irq(btv
);
4083 /* get/clear interrupt status bits */
4084 stat
=btread(BT848_INT_STAT
);
4085 astat
=stat
&btread(BT848_INT_MASK
);
4089 btwrite(stat
,BT848_INT_STAT
);
4091 /* get device status bits */
4092 dstat
=btread(BT848_DSTATUS
);
4095 printk(KERN_DEBUG
"bttv%d: irq loop=%d fc=%d "
4096 "riscs=%x, riscc=%08x, ",
4097 btv
->c
.nr
, count
, btv
->field_count
,
4098 stat
>>28, btread(BT848_RISC_COUNT
));
4099 bttv_print_irqbits(stat
,astat
);
4100 if (stat
& BT848_INT_HLOCK
)
4101 printk(" HLOC => %s", (dstat
& BT848_DSTATUS_HLOC
)
4103 if (stat
& BT848_INT_VPRES
)
4104 printk(" PRES => %s", (dstat
& BT848_DSTATUS_PRES
)
4106 if (stat
& BT848_INT_FMTCHG
)
4107 printk(" NUML => %s", (dstat
& BT848_DSTATUS_NUML
)
4112 if (astat
&BT848_INT_VSYNC
)
4115 if ((astat
& BT848_INT_GPINT
) && btv
->remote
) {
4116 wake_up(&btv
->gpioq
);
4117 bttv_input_irq(btv
);
4120 if (astat
& BT848_INT_I2CDONE
) {
4121 btv
->i2c_done
= stat
;
4122 wake_up(&btv
->i2c_queue
);
4125 if ((astat
& BT848_INT_RISCI
) && (stat
& (4<<28)))
4126 bttv_irq_switch_vbi(btv
);
4128 if ((astat
& BT848_INT_RISCI
) && (stat
& (2<<28)))
4129 bttv_irq_wakeup_top(btv
);
4131 if ((astat
& BT848_INT_RISCI
) && (stat
& (1<<28)))
4132 bttv_irq_switch_video(btv
);
4134 if ((astat
& BT848_INT_HLOCK
) && btv
->opt_automute
)
4135 audio_mute(btv
, btv
->mute
); /* trigger automute */
4137 if (astat
& (BT848_INT_SCERR
|BT848_INT_OCERR
)) {
4138 printk(KERN_INFO
"bttv%d: %s%s @ %08x,",btv
->c
.nr
,
4139 (astat
& BT848_INT_SCERR
) ? "SCERR" : "",
4140 (astat
& BT848_INT_OCERR
) ? "OCERR" : "",
4141 btread(BT848_RISC_COUNT
));
4142 bttv_print_irqbits(stat
,astat
);
4145 bttv_print_riscaddr(btv
);
4147 if (fdsr
&& astat
& BT848_INT_FDSR
) {
4148 printk(KERN_INFO
"bttv%d: FDSR @ %08x\n",
4149 btv
->c
.nr
,btread(BT848_RISC_COUNT
));
4151 bttv_print_riscaddr(btv
);
4157 if (count
> 8 || !(astat
& BT848_INT_GPINT
)) {
4158 btwrite(0, BT848_INT_MASK
);
4161 "bttv%d: IRQ lockup, cleared int mask [", btv
->c
.nr
);
4164 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv
->c
.nr
);
4166 btwrite(btread(BT848_INT_MASK
) & (-1 ^ BT848_INT_GPINT
),
4170 bttv_print_irqbits(stat
,astat
);
4178 return IRQ_RETVAL(handled
);
4182 /* ----------------------------------------------------------------------- */
4183 /* initialitation */
4185 static struct video_device
*vdev_init(struct bttv
*btv
,
4186 const struct video_device
*template,
4187 const char *type_name
)
4189 struct video_device
*vfd
;
4191 vfd
= video_device_alloc();
4195 vfd
->v4l2_dev
= &btv
->c
.v4l2_dev
;
4196 vfd
->release
= video_device_release
;
4197 vfd
->debug
= bttv_debug
;
4198 video_set_drvdata(vfd
, btv
);
4199 snprintf(vfd
->name
, sizeof(vfd
->name
), "BT%d%s %s (%s)",
4200 btv
->id
, (btv
->id
==848 && btv
->revision
==0x12) ? "A" : "",
4201 type_name
, bttv_tvcards
[btv
->c
.type
].name
);
4205 static void bttv_unregister_video(struct bttv
*btv
)
4207 if (btv
->video_dev
) {
4208 if (video_is_registered(btv
->video_dev
))
4209 video_unregister_device(btv
->video_dev
);
4211 video_device_release(btv
->video_dev
);
4212 btv
->video_dev
= NULL
;
4215 if (video_is_registered(btv
->vbi_dev
))
4216 video_unregister_device(btv
->vbi_dev
);
4218 video_device_release(btv
->vbi_dev
);
4219 btv
->vbi_dev
= NULL
;
4221 if (btv
->radio_dev
) {
4222 if (video_is_registered(btv
->radio_dev
))
4223 video_unregister_device(btv
->radio_dev
);
4225 video_device_release(btv
->radio_dev
);
4226 btv
->radio_dev
= NULL
;
4230 /* register video4linux devices */
4231 static int __devinit
bttv_register_video(struct bttv
*btv
)
4234 printk("bttv: Overlay support disabled.\n");
4237 btv
->video_dev
= vdev_init(btv
, &bttv_video_template
, "video");
4239 if (NULL
== btv
->video_dev
)
4241 if (video_register_device(btv
->video_dev
, VFL_TYPE_GRABBER
,
4242 video_nr
[btv
->c
.nr
]) < 0)
4244 printk(KERN_INFO
"bttv%d: registered device %s\n",
4245 btv
->c
.nr
, video_device_node_name(btv
->video_dev
));
4246 if (device_create_file(&btv
->video_dev
->dev
,
4247 &dev_attr_card
)<0) {
4248 printk(KERN_ERR
"bttv%d: device_create_file 'card' "
4249 "failed\n", btv
->c
.nr
);
4254 btv
->vbi_dev
= vdev_init(btv
, &bttv_video_template
, "vbi");
4256 if (NULL
== btv
->vbi_dev
)
4258 if (video_register_device(btv
->vbi_dev
, VFL_TYPE_VBI
,
4259 vbi_nr
[btv
->c
.nr
]) < 0)
4261 printk(KERN_INFO
"bttv%d: registered device %s\n",
4262 btv
->c
.nr
, video_device_node_name(btv
->vbi_dev
));
4264 if (!btv
->has_radio
)
4267 btv
->radio_dev
= vdev_init(btv
, &radio_template
, "radio");
4268 if (NULL
== btv
->radio_dev
)
4270 if (video_register_device(btv
->radio_dev
, VFL_TYPE_RADIO
,
4271 radio_nr
[btv
->c
.nr
]) < 0)
4273 printk(KERN_INFO
"bttv%d: registered device %s\n",
4274 btv
->c
.nr
, video_device_node_name(btv
->radio_dev
));
4280 bttv_unregister_video(btv
);
4285 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4286 /* response on cards with no firmware is not enabled by OF */
4287 static void pci_set_command(struct pci_dev
*dev
)
4289 #if defined(__powerpc__)
4292 pci_read_config_dword(dev
, PCI_COMMAND
, &cmd
);
4293 cmd
= (cmd
| PCI_COMMAND_MEMORY
);
4294 pci_write_config_dword(dev
, PCI_COMMAND
, cmd
);
4298 static int __devinit
bttv_probe(struct pci_dev
*dev
,
4299 const struct pci_device_id
*pci_id
)
4305 if (bttv_num
== BTTV_MAX
)
4307 printk(KERN_INFO
"bttv: Bt8xx card found (%d).\n", bttv_num
);
4308 bttvs
[bttv_num
] = btv
= kzalloc(sizeof(*btv
), GFP_KERNEL
);
4310 printk(KERN_ERR
"bttv: out of memory.\n");
4313 btv
->c
.nr
= bttv_num
;
4314 snprintf(btv
->c
.v4l2_dev
.name
, sizeof(btv
->c
.v4l2_dev
.name
),
4315 "bttv%d", btv
->c
.nr
);
4317 /* initialize structs / fill in defaults */
4318 mutex_init(&btv
->lock
);
4319 spin_lock_init(&btv
->s_lock
);
4320 spin_lock_init(&btv
->gpio_lock
);
4321 init_waitqueue_head(&btv
->gpioq
);
4322 init_waitqueue_head(&btv
->i2c_queue
);
4323 INIT_LIST_HEAD(&btv
->c
.subs
);
4324 INIT_LIST_HEAD(&btv
->capture
);
4325 INIT_LIST_HEAD(&btv
->vcapture
);
4326 v4l2_prio_init(&btv
->prio
);
4328 init_timer(&btv
->timeout
);
4329 btv
->timeout
.function
= bttv_irq_timeout
;
4330 btv
->timeout
.data
= (unsigned long)btv
;
4333 btv
->tuner_type
= UNSET
;
4334 btv
->new_input
= UNSET
;
4335 btv
->has_radio
=radio
[btv
->c
.nr
];
4337 /* pci stuff (init, get irq/mmio, ... */
4339 btv
->id
= dev
->device
;
4340 if (pci_enable_device(dev
)) {
4341 printk(KERN_WARNING
"bttv%d: Can't enable device.\n",
4345 if (pci_set_dma_mask(dev
, DMA_BIT_MASK(32))) {
4346 printk(KERN_WARNING
"bttv%d: No suitable DMA available.\n",
4350 if (!request_mem_region(pci_resource_start(dev
,0),
4351 pci_resource_len(dev
,0),
4352 btv
->c
.v4l2_dev
.name
)) {
4353 printk(KERN_WARNING
"bttv%d: can't request iomem (0x%llx).\n",
4355 (unsigned long long)pci_resource_start(dev
,0));
4358 pci_set_master(dev
);
4359 pci_set_command(dev
);
4361 result
= v4l2_device_register(&dev
->dev
, &btv
->c
.v4l2_dev
);
4363 printk(KERN_WARNING
"bttv%d: v4l2_device_register() failed\n", btv
->c
.nr
);
4367 pci_read_config_byte(dev
, PCI_CLASS_REVISION
, &btv
->revision
);
4368 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
4369 printk(KERN_INFO
"bttv%d: Bt%d (rev %d) at %s, ",
4370 bttv_num
,btv
->id
, btv
->revision
, pci_name(dev
));
4371 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4372 btv
->c
.pci
->irq
, lat
,
4373 (unsigned long long)pci_resource_start(dev
,0));
4376 btv
->bt848_mmio
= ioremap(pci_resource_start(dev
, 0), 0x1000);
4377 if (NULL
== btv
->bt848_mmio
) {
4378 printk("bttv%d: ioremap() failed\n", btv
->c
.nr
);
4386 /* disable irqs, register irq handler */
4387 btwrite(0, BT848_INT_MASK
);
4388 result
= request_irq(btv
->c
.pci
->irq
, bttv_irq
,
4389 IRQF_SHARED
| IRQF_DISABLED
, btv
->c
.v4l2_dev
.name
, (void *)btv
);
4391 printk(KERN_ERR
"bttv%d: can't get IRQ %d\n",
4392 bttv_num
,btv
->c
.pci
->irq
);
4396 if (0 != bttv_handle_chipset(btv
)) {
4401 /* init options from insmod args */
4402 btv
->opt_combfilter
= combfilter
;
4403 btv
->opt_lumafilter
= lumafilter
;
4404 btv
->opt_automute
= automute
;
4405 btv
->opt_chroma_agc
= chroma_agc
;
4406 btv
->opt_adc_crush
= adc_crush
;
4407 btv
->opt_vcr_hack
= vcr_hack
;
4408 btv
->opt_whitecrush_upper
= whitecrush_upper
;
4409 btv
->opt_whitecrush_lower
= whitecrush_lower
;
4410 btv
->opt_uv_ratio
= uv_ratio
;
4411 btv
->opt_full_luma_range
= full_luma_range
;
4412 btv
->opt_coring
= coring
;
4414 /* fill struct bttv with some useful defaults */
4415 btv
->init
.btv
= btv
;
4416 btv
->init
.ov
.w
.width
= 320;
4417 btv
->init
.ov
.w
.height
= 240;
4418 btv
->init
.fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
4419 btv
->init
.width
= 320;
4420 btv
->init
.height
= 240;
4423 /* initialize hardware */
4425 bttv_gpio_tracking(btv
,"pre-init");
4427 bttv_risc_init_main(btv
);
4431 btwrite(0x00, BT848_GPIO_REG_INP
);
4432 btwrite(0x00, BT848_GPIO_OUT_EN
);
4434 bttv_gpio_tracking(btv
,"init");
4436 /* needs to be done before i2c is registered */
4437 bttv_init_card1(btv
);
4439 /* register i2c + gpio */
4442 /* some card-specific stuff (needs working i2c) */
4443 bttv_init_card2(btv
);
4444 bttv_init_tuner(btv
);
4447 /* register video4linux + input */
4448 if (!bttv_tvcards
[btv
->c
.type
].no_video
) {
4449 bttv_register_video(btv
);
4450 bt848_bright(btv
,32768);
4451 bt848_contrast(btv
,32768);
4452 bt848_hue(btv
,32768);
4453 bt848_sat(btv
,32768);
4455 set_input(btv
, 0, btv
->tvnorm
);
4456 bttv_crop_reset(&btv
->crop
[0], btv
->tvnorm
);
4457 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
4458 disclaim_vbi_lines(btv
);
4459 disclaim_video_lines(btv
);
4462 /* add subdevices and autoload dvb-bt8xx if needed */
4463 if (bttv_tvcards
[btv
->c
.type
].has_dvb
) {
4464 bttv_sub_add_device(&btv
->c
, "dvb");
4465 request_modules(btv
);
4469 init_bttv_i2c_ir(btv
);
4470 bttv_input_init(btv
);
4473 /* everything is fine */
4478 free_irq(btv
->c
.pci
->irq
,btv
);
4481 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4484 if (btv
->bt848_mmio
)
4485 iounmap(btv
->bt848_mmio
);
4486 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4487 pci_resource_len(btv
->c
.pci
,0));
4491 static void __devexit
bttv_remove(struct pci_dev
*pci_dev
)
4493 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4494 struct bttv
*btv
= to_bttv(v4l2_dev
);
4497 printk("bttv%d: unloading\n",btv
->c
.nr
);
4499 /* shutdown everything (DMA+IRQs) */
4500 btand(~15, BT848_GPIO_DMA_CTL
);
4501 btwrite(0, BT848_INT_MASK
);
4502 btwrite(~0x0, BT848_INT_STAT
);
4503 btwrite(0x0, BT848_GPIO_OUT_EN
);
4505 bttv_gpio_tracking(btv
,"cleanup");
4507 /* tell gpio modules we are leaving ... */
4509 wake_up(&btv
->gpioq
);
4510 bttv_input_fini(btv
);
4511 bttv_sub_del_devices(&btv
->c
);
4513 /* unregister i2c_bus + input */
4516 /* unregister video4linux */
4517 bttv_unregister_video(btv
);
4519 /* free allocated memory */
4520 btcx_riscmem_free(btv
->c
.pci
,&btv
->main
);
4522 /* free ressources */
4523 free_irq(btv
->c
.pci
->irq
,btv
);
4524 iounmap(btv
->bt848_mmio
);
4525 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4526 pci_resource_len(btv
->c
.pci
,0));
4528 v4l2_device_unregister(&btv
->c
.v4l2_dev
);
4529 bttvs
[btv
->c
.nr
] = NULL
;
4536 static int bttv_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
4538 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4539 struct bttv
*btv
= to_bttv(v4l2_dev
);
4540 struct bttv_buffer_set idle
;
4541 unsigned long flags
;
4543 dprintk("bttv%d: suspend %d\n", btv
->c
.nr
, state
.event
);
4545 /* stop dma + irqs */
4546 spin_lock_irqsave(&btv
->s_lock
,flags
);
4547 memset(&idle
, 0, sizeof(idle
));
4548 btv
->state
.video
= btv
->curr
;
4549 btv
->state
.vbi
= btv
->cvbi
;
4550 btv
->state
.loop_irq
= btv
->loop_irq
;
4553 bttv_buffer_activate_video(btv
, &idle
);
4554 bttv_buffer_activate_vbi(btv
, NULL
);
4555 bttv_set_dma(btv
, 0);
4556 btwrite(0, BT848_INT_MASK
);
4557 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4559 /* save bt878 state */
4560 btv
->state
.gpio_enable
= btread(BT848_GPIO_OUT_EN
);
4561 btv
->state
.gpio_data
= gpio_read();
4563 /* save pci state */
4564 pci_save_state(pci_dev
);
4565 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
4566 pci_disable_device(pci_dev
);
4567 btv
->state
.disabled
= 1;
4572 static int bttv_resume(struct pci_dev
*pci_dev
)
4574 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
4575 struct bttv
*btv
= to_bttv(v4l2_dev
);
4576 unsigned long flags
;
4579 dprintk("bttv%d: resume\n", btv
->c
.nr
);
4581 /* restore pci state */
4582 if (btv
->state
.disabled
) {
4583 err
=pci_enable_device(pci_dev
);
4585 printk(KERN_WARNING
"bttv%d: Can't enable device.\n",
4589 btv
->state
.disabled
= 0;
4591 err
=pci_set_power_state(pci_dev
, PCI_D0
);
4593 pci_disable_device(pci_dev
);
4594 printk(KERN_WARNING
"bttv%d: Can't enable device.\n",
4596 btv
->state
.disabled
= 1;
4600 pci_restore_state(pci_dev
);
4602 /* restore bt878 state */
4603 bttv_reinit_bt848(btv
);
4604 gpio_inout(0xffffff, btv
->state
.gpio_enable
);
4605 gpio_write(btv
->state
.gpio_data
);
4608 spin_lock_irqsave(&btv
->s_lock
,flags
);
4609 btv
->curr
= btv
->state
.video
;
4610 btv
->cvbi
= btv
->state
.vbi
;
4611 btv
->loop_irq
= btv
->state
.loop_irq
;
4612 bttv_buffer_activate_video(btv
, &btv
->curr
);
4613 bttv_buffer_activate_vbi(btv
, btv
->cvbi
);
4614 bttv_set_dma(btv
, 0);
4615 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4620 static struct pci_device_id bttv_pci_tbl
[] = {
4621 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT848
), 0},
4622 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT849
), 0},
4623 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT878
), 0},
4624 {PCI_VDEVICE(BROOKTREE
, PCI_DEVICE_ID_BT879
), 0},
4628 MODULE_DEVICE_TABLE(pci
, bttv_pci_tbl
);
4630 static struct pci_driver bttv_pci_driver
= {
4632 .id_table
= bttv_pci_tbl
,
4633 .probe
= bttv_probe
,
4634 .remove
= __devexit_p(bttv_remove
),
4636 .suspend
= bttv_suspend
,
4637 .resume
= bttv_resume
,
4641 static int __init
bttv_init_module(void)
4647 printk(KERN_INFO
"bttv: driver version %d.%d.%d loaded\n",
4648 (BTTV_VERSION_CODE
>> 16) & 0xff,
4649 (BTTV_VERSION_CODE
>> 8) & 0xff,
4650 BTTV_VERSION_CODE
& 0xff);
4652 printk(KERN_INFO
"bttv: snapshot date %04d-%02d-%02d\n",
4653 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
4655 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
4657 if (gbufsize
> BTTV_MAX_FBUF
)
4658 gbufsize
= BTTV_MAX_FBUF
;
4659 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
4661 printk(KERN_INFO
"bttv: using %d buffers with %dk (%d pages) each for capture\n",
4662 gbuffers
, gbufsize
>> 10, gbufsize
>> PAGE_SHIFT
);
4664 bttv_check_chipset();
4666 ret
= bus_register(&bttv_sub_bus_type
);
4668 printk(KERN_WARNING
"bttv: bus_register error: %d\n", ret
);
4671 ret
= pci_register_driver(&bttv_pci_driver
);
4673 bus_unregister(&bttv_sub_bus_type
);
4678 static void __exit
bttv_cleanup_module(void)
4680 pci_unregister_driver(&bttv_pci_driver
);
4681 bus_unregister(&bttv_sub_bus_type
);
4684 module_init(bttv_init_module
);
4685 module_exit(bttv_cleanup_module
);