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>
12 Cropping and overscan support
13 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
14 Sponsored by OPQ Systems AB
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/interrupt.h>
39 #include <linux/kdev_t.h>
41 #include <media/v4l2-common.h>
42 #include <media/tvaudio.h>
43 #include <media/msp3400.h>
45 #include <linux/dma-mapping.h>
48 #include <asm/byteorder.h>
50 #include <media/rds.h>
53 unsigned int bttv_num
; /* number of Bt848s in use */
54 struct bttv bttvs
[BTTV_MAX
];
56 unsigned int bttv_debug
;
57 unsigned int bttv_verbose
= 1;
58 unsigned int bttv_gpio
;
60 /* config variables */
62 static unsigned int bigendian
=1;
64 static unsigned int bigendian
;
66 static unsigned int radio
[BTTV_MAX
];
67 static unsigned int irq_debug
;
68 static unsigned int gbuffers
= 8;
69 static unsigned int gbufsize
= 0x208000;
70 static unsigned int reset_crop
= 1;
72 static int video_nr
= -1;
73 static int radio_nr
= -1;
74 static int vbi_nr
= -1;
75 static int debug_latency
;
77 static unsigned int fdsr
;
80 static unsigned int combfilter
;
81 static unsigned int lumafilter
;
82 static unsigned int automute
= 1;
83 static unsigned int chroma_agc
;
84 static unsigned int adc_crush
= 1;
85 static unsigned int whitecrush_upper
= 0xCF;
86 static unsigned int whitecrush_lower
= 0x7F;
87 static unsigned int vcr_hack
;
88 static unsigned int irq_iswitch
;
89 static unsigned int uv_ratio
= 50;
90 static unsigned int full_luma_range
;
91 static unsigned int coring
;
92 extern int no_overlay
;
94 /* API features (turn on/off stuff for testing) */
95 static unsigned int v4l2
= 1;
98 module_param(bttv_verbose
, int, 0644);
99 module_param(bttv_gpio
, int, 0644);
100 module_param(bttv_debug
, int, 0644);
101 module_param(irq_debug
, int, 0644);
102 module_param(debug_latency
, int, 0644);
104 module_param(fdsr
, int, 0444);
105 module_param(video_nr
, int, 0444);
106 module_param(radio_nr
, int, 0444);
107 module_param(vbi_nr
, int, 0444);
108 module_param(gbuffers
, int, 0444);
109 module_param(gbufsize
, int, 0444);
110 module_param(reset_crop
, int, 0444);
112 module_param(v4l2
, int, 0644);
113 module_param(bigendian
, int, 0644);
114 module_param(irq_iswitch
, int, 0644);
115 module_param(combfilter
, int, 0444);
116 module_param(lumafilter
, int, 0444);
117 module_param(automute
, int, 0444);
118 module_param(chroma_agc
, int, 0444);
119 module_param(adc_crush
, int, 0444);
120 module_param(whitecrush_upper
, int, 0444);
121 module_param(whitecrush_lower
, int, 0444);
122 module_param(vcr_hack
, int, 0444);
123 module_param(uv_ratio
, int, 0444);
124 module_param(full_luma_range
, int, 0444);
125 module_param(coring
, int, 0444);
127 module_param_array(radio
, int, NULL
, 0444);
129 MODULE_PARM_DESC(radio
,"The TV card supports radio, default is 0 (no)");
130 MODULE_PARM_DESC(bigendian
,"byte order of the framebuffer, default is native endian");
131 MODULE_PARM_DESC(bttv_verbose
,"verbose startup messages, default is 1 (yes)");
132 MODULE_PARM_DESC(bttv_gpio
,"log gpio changes, default is 0 (no)");
133 MODULE_PARM_DESC(bttv_debug
,"debug messages, default is 0 (no)");
134 MODULE_PARM_DESC(irq_debug
,"irq handler debug messages, default is 0 (no)");
135 MODULE_PARM_DESC(gbuffers
,"number of capture buffers. range 2-32, default 8");
136 MODULE_PARM_DESC(gbufsize
,"size of the capture buffers, default is 0x208000");
137 MODULE_PARM_DESC(reset_crop
,"reset cropping parameters at open(), default "
138 "is 1 (yes) for compatibility with older applications");
139 MODULE_PARM_DESC(automute
,"mute audio on bad/missing video signal, default is 1 (yes)");
140 MODULE_PARM_DESC(chroma_agc
,"enables the AGC of chroma signal, default is 0 (no)");
141 MODULE_PARM_DESC(adc_crush
,"enables the luminance ADC crush, default is 1 (yes)");
142 MODULE_PARM_DESC(whitecrush_upper
,"sets the white crush upper value, default is 207");
143 MODULE_PARM_DESC(whitecrush_lower
,"sets the white crush lower value, default is 127");
144 MODULE_PARM_DESC(vcr_hack
,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
145 MODULE_PARM_DESC(irq_iswitch
,"switch inputs in irq handler");
146 MODULE_PARM_DESC(uv_ratio
,"ratio between u and v gains, default is 50");
147 MODULE_PARM_DESC(full_luma_range
,"use the full luma range, default is 0 (no)");
148 MODULE_PARM_DESC(coring
,"set the luma coring level, default is 0 (no)");
150 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
151 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
152 MODULE_LICENSE("GPL");
154 /* ----------------------------------------------------------------------- */
157 static ssize_t
show_card(struct class_device
*cd
, char *buf
)
159 struct video_device
*vfd
= to_video_device(cd
);
160 struct bttv
*btv
= dev_get_drvdata(vfd
->dev
);
161 return sprintf(buf
, "%d\n", btv
? btv
->c
.type
: UNSET
);
163 static CLASS_DEVICE_ATTR(card
, S_IRUGO
, show_card
, NULL
);
165 /* ----------------------------------------------------------------------- */
166 /* dvb auto-load setup */
167 #if defined(CONFIG_MODULES) && defined(MODULE)
168 static void request_module_async(struct work_struct
*work
)
170 request_module("dvb-bt8xx");
173 static void request_modules(struct bttv
*dev
)
175 INIT_WORK(&dev
->request_module_wk
, request_module_async
);
176 schedule_work(&dev
->request_module_wk
);
179 #define request_modules(dev)
180 #endif /* CONFIG_MODULES */
183 /* ----------------------------------------------------------------------- */
186 /* special timing tables from conexant... */
187 static u8 SRAM_Table
[][60] =
189 /* PAL digital input over GPIO[7:0] */
191 45, // 45 bytes following
192 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
193 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
194 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
195 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
196 0x37,0x00,0xAF,0x21,0x00
198 /* NTSC digital input over GPIO[7:0] */
200 51, // 51 bytes following
201 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
202 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
203 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
204 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
205 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
208 // TGB_NTSC392 // quartzsight
209 // This table has been modified to be used for Fusion Rev D
211 0x2A, // size of table = 42
212 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
213 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
214 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
215 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
220 /* minhdelayx1 first video pixel we can capture on a line and
221 hdelayx1 start of active video, both relative to rising edge of
222 /HRESET pulse (0H) in 1 / fCLKx1.
223 swidth width of active video and
224 totalwidth total line width, both in 1 / fCLKx1.
225 sqwidth total line width in square pixels.
226 vdelay start of active video in 2 * field lines relative to
227 trailing edge of /VRESET pulse (VDELAY register).
228 sheight height of active video in 2 * field lines.
229 videostart0 ITU-R frame line number of the line corresponding
230 to vdelay in the first field. */
231 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
232 vdelay, sheight, videostart0) \
233 .cropcap.bounds.left = minhdelayx1, \
234 /* * 2 because vertically we count field lines times two, */ \
235 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
236 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
237 /* 4 is a safety margin at the end of the line. */ \
238 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
239 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
240 .cropcap.defrect.left = hdelayx1, \
241 .cropcap.defrect.top = (videostart0) * 2, \
242 .cropcap.defrect.width = swidth, \
243 .cropcap.defrect.height = sheight, \
244 .cropcap.pixelaspect.numerator = totalwidth, \
245 .cropcap.pixelaspect.denominator = sqwidth,
247 const struct bttv_tvnorm bttv_tvnorms
[] = {
249 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
250 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
252 .v4l2_id
= V4L2_STD_PAL
,
260 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
261 .scaledtwidth
= 1135,
265 .vbipack
= 255, /* min (2048 / 4, 0x1ff) & 0xff */
267 /* ITU-R frame line number of the first VBI line
268 we can capture, of the first and second field.
269 The last line is determined by cropcap.bounds. */
270 .vbistart
= { 7, 320 },
271 CROPCAP(/* minhdelayx1 */ 68,
273 /* Should be (768 * 1135 + 944 / 2) / 944.
274 cropcap.defrect is used for image width
275 checks, so we keep the old value 924. */
277 /* totalwidth */ 1135,
281 /* videostart0 */ 23)
282 /* bt878 (and bt848?) can capture another
283 line below active video. */
284 .cropcap
.bounds
.height
= (576 + 2) + 0x20 - 2,
286 .v4l2_id
= V4L2_STD_NTSC_M
| V4L2_STD_NTSC_M_KR
,
294 .iform
= (BT848_IFORM_NTSC
|BT848_IFORM_XT0
),
299 .vbipack
= 144, /* min (1600 / 4, 0x1ff) & 0xff */
301 .vbistart
= { 10, 273 },
302 CROPCAP(/* minhdelayx1 */ 68,
304 /* Should be (640 * 910 + 780 / 2) / 780? */
306 /* totalwidth */ 910,
310 /* videostart0 */ 23)
312 .v4l2_id
= V4L2_STD_SECAM
,
320 .iform
= (BT848_IFORM_SECAM
|BT848_IFORM_XT1
),
321 .scaledtwidth
= 1135,
326 .sram
= 0, /* like PAL, correct? */
327 .vbistart
= { 7, 320 },
328 CROPCAP(/* minhdelayx1 */ 68,
331 /* totalwidth */ 1135,
335 /* videostart0 */ 23)
337 .v4l2_id
= V4L2_STD_PAL_Nc
,
345 .iform
= (BT848_IFORM_PAL_NC
|BT848_IFORM_XT0
),
352 .vbistart
= { 7, 320 },
353 CROPCAP(/* minhdelayx1 */ 68,
355 /* swidth */ (640 * 910 + 780 / 2) / 780,
356 /* totalwidth */ 910,
360 /* videostart0 */ 23)
362 .v4l2_id
= V4L2_STD_PAL_M
,
370 .iform
= (BT848_IFORM_PAL_M
|BT848_IFORM_XT0
),
377 .vbistart
= { 10, 273 },
378 CROPCAP(/* minhdelayx1 */ 68,
380 /* swidth */ (640 * 910 + 780 / 2) / 780,
381 /* totalwidth */ 910,
385 /* videostart0 */ 23)
387 .v4l2_id
= V4L2_STD_PAL_N
,
395 .iform
= (BT848_IFORM_PAL_N
|BT848_IFORM_XT1
),
402 .vbistart
= { 7, 320 },
403 CROPCAP(/* minhdelayx1 */ 68,
405 /* swidth */ (768 * 1135 + 944 / 2) / 944,
406 /* totalwidth */ 1135,
410 /* videostart0 */ 23)
412 .v4l2_id
= V4L2_STD_NTSC_M_JP
,
420 .iform
= (BT848_IFORM_NTSC_J
|BT848_IFORM_XT0
),
427 .vbistart
= { 10, 273 },
428 CROPCAP(/* minhdelayx1 */ 68,
430 /* swidth */ (640 * 910 + 780 / 2) / 780,
431 /* totalwidth */ 910,
435 /* videostart0 */ 23)
437 /* that one hopefully works with the strange timing
438 * which video recorders produce when playing a NTSC
439 * tape on a PAL TV ... */
440 .v4l2_id
= V4L2_STD_PAL_60
,
448 .iform
= (BT848_IFORM_PAL_BDGHI
|BT848_IFORM_XT1
),
449 .scaledtwidth
= 1135,
456 .vbistart
= { 10, 273 },
457 CROPCAP(/* minhdelayx1 */ 68,
460 /* totalwidth */ 1135,
464 /* videostart0 */ 23)
467 static const unsigned int BTTV_TVNORMS
= ARRAY_SIZE(bttv_tvnorms
);
469 /* ----------------------------------------------------------------------- */
471 packed pixel formats must come first */
472 static const struct bttv_format bttv_formats
[] = {
474 .name
= "8 bpp, gray",
475 .palette
= VIDEO_PALETTE_GREY
,
476 .fourcc
= V4L2_PIX_FMT_GREY
,
477 .btformat
= BT848_COLOR_FMT_Y8
,
479 .flags
= FORMAT_FLAGS_PACKED
,
481 .name
= "8 bpp, dithered color",
482 .palette
= VIDEO_PALETTE_HI240
,
483 .fourcc
= V4L2_PIX_FMT_HI240
,
484 .btformat
= BT848_COLOR_FMT_RGB8
,
486 .flags
= FORMAT_FLAGS_PACKED
| FORMAT_FLAGS_DITHER
,
488 .name
= "15 bpp RGB, le",
489 .palette
= VIDEO_PALETTE_RGB555
,
490 .fourcc
= V4L2_PIX_FMT_RGB555
,
491 .btformat
= BT848_COLOR_FMT_RGB15
,
493 .flags
= FORMAT_FLAGS_PACKED
,
495 .name
= "15 bpp RGB, be",
497 .fourcc
= V4L2_PIX_FMT_RGB555X
,
498 .btformat
= BT848_COLOR_FMT_RGB15
,
499 .btswap
= 0x03, /* byteswap */
501 .flags
= FORMAT_FLAGS_PACKED
,
503 .name
= "16 bpp RGB, le",
504 .palette
= VIDEO_PALETTE_RGB565
,
505 .fourcc
= V4L2_PIX_FMT_RGB565
,
506 .btformat
= BT848_COLOR_FMT_RGB16
,
508 .flags
= FORMAT_FLAGS_PACKED
,
510 .name
= "16 bpp RGB, be",
512 .fourcc
= V4L2_PIX_FMT_RGB565X
,
513 .btformat
= BT848_COLOR_FMT_RGB16
,
514 .btswap
= 0x03, /* byteswap */
516 .flags
= FORMAT_FLAGS_PACKED
,
518 .name
= "24 bpp RGB, le",
519 .palette
= VIDEO_PALETTE_RGB24
,
520 .fourcc
= V4L2_PIX_FMT_BGR24
,
521 .btformat
= BT848_COLOR_FMT_RGB24
,
523 .flags
= FORMAT_FLAGS_PACKED
,
525 .name
= "32 bpp RGB, le",
526 .palette
= VIDEO_PALETTE_RGB32
,
527 .fourcc
= V4L2_PIX_FMT_BGR32
,
528 .btformat
= BT848_COLOR_FMT_RGB32
,
530 .flags
= FORMAT_FLAGS_PACKED
,
532 .name
= "32 bpp RGB, be",
534 .fourcc
= V4L2_PIX_FMT_RGB32
,
535 .btformat
= BT848_COLOR_FMT_RGB32
,
536 .btswap
= 0x0f, /* byte+word swap */
538 .flags
= FORMAT_FLAGS_PACKED
,
540 .name
= "4:2:2, packed, YUYV",
541 .palette
= VIDEO_PALETTE_YUV422
,
542 .fourcc
= V4L2_PIX_FMT_YUYV
,
543 .btformat
= BT848_COLOR_FMT_YUY2
,
545 .flags
= FORMAT_FLAGS_PACKED
,
547 .name
= "4:2:2, packed, YUYV",
548 .palette
= VIDEO_PALETTE_YUYV
,
549 .fourcc
= V4L2_PIX_FMT_YUYV
,
550 .btformat
= BT848_COLOR_FMT_YUY2
,
552 .flags
= FORMAT_FLAGS_PACKED
,
554 .name
= "4:2:2, packed, UYVY",
555 .palette
= VIDEO_PALETTE_UYVY
,
556 .fourcc
= V4L2_PIX_FMT_UYVY
,
557 .btformat
= BT848_COLOR_FMT_YUY2
,
558 .btswap
= 0x03, /* byteswap */
560 .flags
= FORMAT_FLAGS_PACKED
,
562 .name
= "4:2:2, planar, Y-Cb-Cr",
563 .palette
= VIDEO_PALETTE_YUV422P
,
564 .fourcc
= V4L2_PIX_FMT_YUV422P
,
565 .btformat
= BT848_COLOR_FMT_YCrCb422
,
567 .flags
= FORMAT_FLAGS_PLANAR
,
571 .name
= "4:2:0, planar, Y-Cb-Cr",
572 .palette
= VIDEO_PALETTE_YUV420P
,
573 .fourcc
= V4L2_PIX_FMT_YUV420
,
574 .btformat
= BT848_COLOR_FMT_YCrCb422
,
576 .flags
= FORMAT_FLAGS_PLANAR
,
580 .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 .palette
= VIDEO_PALETTE_YUV411P
,
591 .fourcc
= V4L2_PIX_FMT_YUV411P
,
592 .btformat
= BT848_COLOR_FMT_YCrCb411
,
594 .flags
= FORMAT_FLAGS_PLANAR
,
598 .name
= "4:1:0, planar, Y-Cb-Cr",
599 .palette
= VIDEO_PALETTE_YUV410P
,
600 .fourcc
= V4L2_PIX_FMT_YUV410
,
601 .btformat
= BT848_COLOR_FMT_YCrCb411
,
603 .flags
= FORMAT_FLAGS_PLANAR
,
607 .name
= "4:1:0, planar, Y-Cr-Cb",
609 .fourcc
= V4L2_PIX_FMT_YVU410
,
610 .btformat
= BT848_COLOR_FMT_YCrCb411
,
612 .flags
= FORMAT_FLAGS_PLANAR
| FORMAT_FLAGS_CrCb
,
616 .name
= "raw scanlines",
617 .palette
= VIDEO_PALETTE_RAW
,
619 .btformat
= BT848_COLOR_FMT_RAW
,
621 .flags
= FORMAT_FLAGS_RAW
,
624 static const unsigned int BTTV_FORMATS
= ARRAY_SIZE(bttv_formats
);
626 /* ----------------------------------------------------------------------- */
628 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
629 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
630 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
631 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
632 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
633 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
634 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
635 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
636 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
637 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
638 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
639 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
641 static const struct v4l2_queryctrl no_ctl
= {
643 .flags
= V4L2_CTRL_FLAG_DISABLED
,
645 static const struct v4l2_queryctrl bttv_ctls
[] = {
648 .id
= V4L2_CID_BRIGHTNESS
,
649 .name
= "Brightness",
653 .default_value
= 32768,
654 .type
= V4L2_CTRL_TYPE_INTEGER
,
656 .id
= V4L2_CID_CONTRAST
,
661 .default_value
= 32768,
662 .type
= V4L2_CTRL_TYPE_INTEGER
,
664 .id
= V4L2_CID_SATURATION
,
665 .name
= "Saturation",
669 .default_value
= 32768,
670 .type
= V4L2_CTRL_TYPE_INTEGER
,
677 .default_value
= 32768,
678 .type
= V4L2_CTRL_TYPE_INTEGER
,
682 .id
= V4L2_CID_AUDIO_MUTE
,
686 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
688 .id
= V4L2_CID_AUDIO_VOLUME
,
693 .default_value
= 65535,
694 .type
= V4L2_CTRL_TYPE_INTEGER
,
696 .id
= V4L2_CID_AUDIO_BALANCE
,
701 .default_value
= 32768,
702 .type
= V4L2_CTRL_TYPE_INTEGER
,
704 .id
= V4L2_CID_AUDIO_BASS
,
709 .default_value
= 32768,
710 .type
= V4L2_CTRL_TYPE_INTEGER
,
712 .id
= V4L2_CID_AUDIO_TREBLE
,
717 .default_value
= 32768,
718 .type
= V4L2_CTRL_TYPE_INTEGER
,
720 /* --- private --- */
722 .id
= V4L2_CID_PRIVATE_CHROMA_AGC
,
723 .name
= "chroma agc",
726 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
728 .id
= V4L2_CID_PRIVATE_COMBFILTER
,
729 .name
= "combfilter",
732 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
734 .id
= V4L2_CID_PRIVATE_AUTOMUTE
,
738 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
740 .id
= V4L2_CID_PRIVATE_LUMAFILTER
,
741 .name
= "luma decimation filter",
744 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
746 .id
= V4L2_CID_PRIVATE_AGC_CRUSH
,
750 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
752 .id
= V4L2_CID_PRIVATE_VCR_HACK
,
756 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
758 .id
= V4L2_CID_PRIVATE_WHITECRUSH_UPPER
,
759 .name
= "whitecrush upper",
763 .default_value
= 0xCF,
764 .type
= V4L2_CTRL_TYPE_INTEGER
,
766 .id
= V4L2_CID_PRIVATE_WHITECRUSH_LOWER
,
767 .name
= "whitecrush lower",
771 .default_value
= 0x7F,
772 .type
= V4L2_CTRL_TYPE_INTEGER
,
774 .id
= V4L2_CID_PRIVATE_UV_RATIO
,
780 .type
= V4L2_CTRL_TYPE_INTEGER
,
782 .id
= V4L2_CID_PRIVATE_FULL_LUMA_RANGE
,
783 .name
= "full luma range",
786 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
788 .id
= V4L2_CID_PRIVATE_CORING
,
794 .type
= V4L2_CTRL_TYPE_INTEGER
,
800 static const int BTTV_CTLS
= ARRAY_SIZE(bttv_ctls
);
802 /* ----------------------------------------------------------------------- */
803 /* resource management */
806 RESOURCE_ allocated by freed by
808 VIDEO_READ bttv_read 1) bttv_read 2)
810 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
811 VIDIOC_QBUF 1) bttv_release
814 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
815 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
818 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
819 VIDIOC_QBUF 1) bttv_release
820 bttv_read, bttv_poll 1) 4)
822 1) The resource must be allocated when we enter buffer prepare functions
823 and remain allocated while buffers are in the DMA queue.
824 2) This is a single frame read.
825 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
826 RESOURCE_OVERLAY is allocated.
827 4) This is a continuous read, implies VIDIOC_STREAMON.
829 Note this driver permits video input and standard changes regardless if
830 resources are allocated.
833 #define VBI_RESOURCES (RESOURCE_VBI)
834 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
835 RESOURCE_VIDEO_STREAM | \
839 int check_alloc_btres(struct bttv
*btv
, struct bttv_fh
*fh
, int bit
)
841 int xbits
; /* mutual exclusive resources */
843 if (fh
->resources
& bit
)
844 /* have it already allocated */
848 if (bit
& (RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
))
849 xbits
|= RESOURCE_VIDEO_READ
| RESOURCE_VIDEO_STREAM
;
852 mutex_lock(&btv
->lock
);
853 if (btv
->resources
& xbits
) {
854 /* no, someone else uses it */
858 if ((bit
& VIDEO_RESOURCES
)
859 && 0 == (btv
->resources
& VIDEO_RESOURCES
)) {
860 /* Do crop - use current, don't - use default parameters. */
861 __s32 top
= btv
->crop
[!!fh
->do_crop
].rect
.top
;
863 if (btv
->vbi_end
> top
)
866 /* We cannot capture the same line as video and VBI data.
867 Claim scan lines crop[].rect.top to bottom. */
868 btv
->crop_start
= top
;
869 } else if (bit
& VBI_RESOURCES
) {
870 __s32 end
= fh
->vbi_fmt
.end
;
872 if (end
> btv
->crop_start
)
875 /* Claim scan lines above fh->vbi_fmt.end. */
879 /* it's free, grab it */
880 fh
->resources
|= bit
;
881 btv
->resources
|= bit
;
882 mutex_unlock(&btv
->lock
);
886 mutex_unlock(&btv
->lock
);
891 int check_btres(struct bttv_fh
*fh
, int bit
)
893 return (fh
->resources
& bit
);
897 int locked_btres(struct bttv
*btv
, int bit
)
899 return (btv
->resources
& bit
);
902 /* Call with btv->lock down. */
904 disclaim_vbi_lines(struct bttv
*btv
)
909 /* Call with btv->lock down. */
911 disclaim_video_lines(struct bttv
*btv
)
913 const struct bttv_tvnorm
*tvnorm
;
916 tvnorm
= &bttv_tvnorms
[btv
->tvnorm
];
917 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
918 + tvnorm
->cropcap
.bounds
.height
;
920 /* VBI capturing ends at VDELAY, start of video capturing, no
921 matter how many lines the VBI RISC program expects. When video
922 capturing is off, it shall no longer "preempt" VBI capturing,
923 so we set VDELAY to maximum. */
924 crop
= btread(BT848_E_CROP
) | 0xc0;
925 btwrite(crop
, BT848_E_CROP
);
926 btwrite(0xfe, BT848_E_VDELAY_LO
);
927 btwrite(crop
, BT848_O_CROP
);
928 btwrite(0xfe, BT848_O_VDELAY_LO
);
932 void free_btres(struct bttv
*btv
, struct bttv_fh
*fh
, int bits
)
934 if ((fh
->resources
& bits
) != bits
) {
935 /* trying to free ressources not allocated by us ... */
936 printk("bttv: BUG! (btres)\n");
938 mutex_lock(&btv
->lock
);
939 fh
->resources
&= ~bits
;
940 btv
->resources
&= ~bits
;
942 bits
= btv
->resources
;
944 if (0 == (bits
& VIDEO_RESOURCES
))
945 disclaim_video_lines(btv
);
947 if (0 == (bits
& VBI_RESOURCES
))
948 disclaim_vbi_lines(btv
);
950 mutex_unlock(&btv
->lock
);
953 /* ----------------------------------------------------------------------- */
954 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
956 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
957 PLL_X = Reference pre-divider (0=1, 1=2)
958 PLL_C = Post divider (0=6, 1=4)
959 PLL_I = Integer input
960 PLL_F = Fractional input
962 F_input = 28.636363 MHz:
963 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
966 static void set_pll_freq(struct bttv
*btv
, unsigned int fin
, unsigned int fout
)
968 unsigned char fl
, fh
, fi
;
970 /* prevent overflows */
983 btwrite(fl
, BT848_PLL_F_LO
);
984 btwrite(fh
, BT848_PLL_F_HI
);
985 btwrite(fi
|BT848_PLL_X
, BT848_PLL_XCI
);
988 static void set_pll(struct bttv
*btv
)
992 if (!btv
->pll
.pll_crystal
)
995 if (btv
->pll
.pll_ofreq
== btv
->pll
.pll_current
) {
996 dprintk("bttv%d: PLL: no change required\n",btv
->c
.nr
);
1000 if (btv
->pll
.pll_ifreq
== btv
->pll
.pll_ofreq
) {
1002 if (btv
->pll
.pll_current
== 0)
1004 bttv_printk(KERN_INFO
"bttv%d: PLL can sleep, using XTAL (%d).\n",
1005 btv
->c
.nr
,btv
->pll
.pll_ifreq
);
1006 btwrite(0x00,BT848_TGCTRL
);
1007 btwrite(0x00,BT848_PLL_XCI
);
1008 btv
->pll
.pll_current
= 0;
1012 bttv_printk(KERN_INFO
"bttv%d: PLL: %d => %d ",btv
->c
.nr
,
1013 btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
1014 set_pll_freq(btv
, btv
->pll
.pll_ifreq
, btv
->pll
.pll_ofreq
);
1016 for (i
=0; i
<10; i
++) {
1017 /* Let other people run while the PLL stabilizes */
1021 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_PLOCK
) {
1022 btwrite(0,BT848_DSTATUS
);
1024 btwrite(0x08,BT848_TGCTRL
);
1025 btv
->pll
.pll_current
= btv
->pll
.pll_ofreq
;
1026 bttv_printk(" ok\n");
1030 btv
->pll
.pll_current
= -1;
1031 bttv_printk("failed\n");
1035 /* used to switch between the bt848's analog/digital video capture modes */
1036 static void bt848A_set_timing(struct bttv
*btv
)
1039 int table_idx
= bttv_tvnorms
[btv
->tvnorm
].sram
;
1040 int fsc
= bttv_tvnorms
[btv
->tvnorm
].Fsc
;
1042 if (UNSET
== bttv_tvcards
[btv
->c
.type
].muxsel
[btv
->input
]) {
1043 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1044 btv
->c
.nr
,table_idx
);
1046 /* timing change...reset timing generator address */
1047 btwrite(0x00, BT848_TGCTRL
);
1048 btwrite(0x02, BT848_TGCTRL
);
1049 btwrite(0x00, BT848_TGCTRL
);
1051 len
=SRAM_Table
[table_idx
][0];
1052 for(i
= 1; i
<= len
; i
++)
1053 btwrite(SRAM_Table
[table_idx
][i
],BT848_TGLB
);
1054 btv
->pll
.pll_ofreq
= 27000000;
1057 btwrite(0x11, BT848_TGCTRL
);
1058 btwrite(0x41, BT848_DVSIF
);
1060 btv
->pll
.pll_ofreq
= fsc
;
1062 btwrite(0x0, BT848_DVSIF
);
1066 /* ----------------------------------------------------------------------- */
1068 static void bt848_bright(struct bttv
*btv
, int bright
)
1072 // printk("bttv: set bright: %d\n",bright); // DEBUG
1073 btv
->bright
= bright
;
1075 /* We want -128 to 127 we get 0-65535 */
1076 value
= (bright
>> 8) - 128;
1077 btwrite(value
& 0xff, BT848_BRIGHT
);
1080 static void bt848_hue(struct bttv
*btv
, int hue
)
1087 value
= (hue
>> 8) - 128;
1088 btwrite(value
& 0xff, BT848_HUE
);
1091 static void bt848_contrast(struct bttv
*btv
, int cont
)
1095 btv
->contrast
= cont
;
1098 value
= (cont
>> 7);
1099 hibit
= (value
>> 6) & 4;
1100 btwrite(value
& 0xff, BT848_CONTRAST_LO
);
1101 btaor(hibit
, ~4, BT848_E_CONTROL
);
1102 btaor(hibit
, ~4, BT848_O_CONTROL
);
1105 static void bt848_sat(struct bttv
*btv
, int color
)
1107 int val_u
,val_v
,hibits
;
1109 btv
->saturation
= color
;
1111 /* 0-511 for the color */
1112 val_u
= ((color
* btv
->opt_uv_ratio
) / 50) >> 7;
1113 val_v
= (((color
* (100 - btv
->opt_uv_ratio
) / 50) >>7)*180L)/254;
1114 hibits
= (val_u
>> 7) & 2;
1115 hibits
|= (val_v
>> 8) & 1;
1116 btwrite(val_u
& 0xff, BT848_SAT_U_LO
);
1117 btwrite(val_v
& 0xff, BT848_SAT_V_LO
);
1118 btaor(hibits
, ~3, BT848_E_CONTROL
);
1119 btaor(hibits
, ~3, BT848_O_CONTROL
);
1122 /* ----------------------------------------------------------------------- */
1125 video_mux(struct bttv
*btv
, unsigned int input
)
1129 if (input
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1132 /* needed by RemoteVideo MX */
1133 mask2
= bttv_tvcards
[btv
->c
.type
].gpiomask2
;
1135 gpio_inout(mask2
,mask2
);
1137 if (input
== btv
->svhs
) {
1138 btor(BT848_CONTROL_COMP
, BT848_E_CONTROL
);
1139 btor(BT848_CONTROL_COMP
, BT848_O_CONTROL
);
1141 btand(~BT848_CONTROL_COMP
, BT848_E_CONTROL
);
1142 btand(~BT848_CONTROL_COMP
, BT848_O_CONTROL
);
1144 mux
= bttv_tvcards
[btv
->c
.type
].muxsel
[input
] & 3;
1145 btaor(mux
<<5, ~(3<<5), BT848_IFORM
);
1146 dprintk(KERN_DEBUG
"bttv%d: video mux: input=%d mux=%d\n",
1147 btv
->c
.nr
,input
,mux
);
1149 /* card specific hook */
1150 if(bttv_tvcards
[btv
->c
.type
].muxsel_hook
)
1151 bttv_tvcards
[btv
->c
.type
].muxsel_hook (btv
, input
);
1155 static char *audio_modes
[] = {
1156 "audio: tuner", "audio: radio", "audio: extern",
1157 "audio: intern", "audio: mute"
1161 audio_mux(struct bttv
*btv
, int input
, int mute
)
1163 int gpio_val
, signal
;
1164 struct v4l2_control ctrl
;
1165 struct i2c_client
*c
;
1167 gpio_inout(bttv_tvcards
[btv
->c
.type
].gpiomask
,
1168 bttv_tvcards
[btv
->c
.type
].gpiomask
);
1169 signal
= btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
;
1175 mute
= mute
|| (btv
->opt_automute
&& !signal
&& !btv
->radio_user
);
1178 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomute
;
1180 gpio_val
= bttv_tvcards
[btv
->c
.type
].gpiomux
[input
];
1182 gpio_bits(bttv_tvcards
[btv
->c
.type
].gpiomask
, gpio_val
);
1184 bttv_gpio_tracking(btv
, audio_modes
[mute
? 4 : input
]);
1188 ctrl
.id
= V4L2_CID_AUDIO_MUTE
;
1189 ctrl
.value
= btv
->mute
;
1190 bttv_call_i2c_clients(btv
, VIDIOC_S_CTRL
, &ctrl
);
1191 c
= btv
->i2c_msp34xx_client
;
1193 struct v4l2_routing route
;
1195 /* Note: the inputs tuner/radio/extern/intern are translated
1196 to msp routings. This assumes common behavior for all msp3400
1197 based TV cards. When this assumption fails, then the
1198 specific MSP routing must be added to the card table.
1199 For now this is sufficient. */
1201 case TVAUDIO_INPUT_RADIO
:
1202 route
.input
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1203 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1205 case TVAUDIO_INPUT_EXTERN
:
1206 route
.input
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER1
,
1207 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1209 case TVAUDIO_INPUT_INTERN
:
1210 /* Yes, this is the same input as for RADIO. I doubt
1211 if this is ever used. The only board with an INTERN
1212 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1213 that was tested. My guess is that the whole INTERN
1214 input does not work. */
1215 route
.input
= MSP_INPUT(MSP_IN_SCART2
, MSP_IN_TUNER1
,
1216 MSP_DSP_IN_SCART
, MSP_DSP_IN_SCART
);
1218 case TVAUDIO_INPUT_TUNER
:
1220 /* This is the only card that uses TUNER2, and afaik,
1221 is the only difference between the VOODOOTV_FM
1223 if (btv
->c
.type
== BTTV_BOARD_VOODOOTV_200
)
1224 route
.input
= MSP_INPUT(MSP_IN_SCART1
, MSP_IN_TUNER2
, \
1225 MSP_DSP_IN_TUNER
, MSP_DSP_IN_TUNER
);
1227 route
.input
= MSP_INPUT_DEFAULT
;
1230 route
.output
= MSP_OUTPUT_DEFAULT
;
1231 c
->driver
->command(c
, VIDIOC_INT_S_AUDIO_ROUTING
, &route
);
1233 c
= btv
->i2c_tvaudio_client
;
1235 struct v4l2_routing route
;
1237 route
.input
= input
;
1239 c
->driver
->command(c
, VIDIOC_INT_S_AUDIO_ROUTING
, &route
);
1245 audio_mute(struct bttv
*btv
, int mute
)
1247 return audio_mux(btv
, btv
->audio
, mute
);
1251 audio_input(struct bttv
*btv
, int input
)
1253 return audio_mux(btv
, input
, btv
->mute
);
1257 i2c_vidiocschan(struct bttv
*btv
)
1259 v4l2_std_id std
= bttv_tvnorms
[btv
->tvnorm
].v4l2_id
;
1261 bttv_call_i2c_clients(btv
, VIDIOC_S_STD
, &std
);
1262 if (btv
->c
.type
== BTTV_BOARD_VOODOOTV_FM
|| btv
->c
.type
== BTTV_BOARD_VOODOOTV_200
)
1263 bttv_tda9880_setnorm(btv
,btv
->tvnorm
);
1267 bttv_crop_calc_limits(struct bttv_crop
*c
)
1269 /* Scale factor min. 1:1, max. 16:1. Min. image size
1270 48 x 32. Scaled width must be a multiple of 4. */
1273 /* For bug compatibility with VIDIOCGCAP and image
1274 size checks in earlier driver versions. */
1275 c
->min_scaled_width
= 48;
1276 c
->min_scaled_height
= 32;
1278 c
->min_scaled_width
=
1279 (max(48, c
->rect
.width
>> 4) + 3) & ~3;
1280 c
->min_scaled_height
=
1281 max(32, c
->rect
.height
>> 4);
1284 c
->max_scaled_width
= c
->rect
.width
& ~3;
1285 c
->max_scaled_height
= c
->rect
.height
;
1289 bttv_crop_reset(struct bttv_crop
*c
, int norm
)
1291 c
->rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1292 bttv_crop_calc_limits(c
);
1295 /* Call with btv->lock down. */
1297 set_tvnorm(struct bttv
*btv
, unsigned int norm
)
1299 const struct bttv_tvnorm
*tvnorm
;
1301 if (norm
< 0 || norm
>= BTTV_TVNORMS
)
1304 tvnorm
= &bttv_tvnorms
[norm
];
1306 if (btv
->tvnorm
< 0 ||
1307 btv
->tvnorm
>= BTTV_TVNORMS
||
1308 0 != memcmp(&bttv_tvnorms
[btv
->tvnorm
].cropcap
,
1310 sizeof (tvnorm
->cropcap
))) {
1311 bttv_crop_reset(&btv
->crop
[0], norm
);
1312 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
1314 if (0 == (btv
->resources
& VIDEO_RESOURCES
)) {
1315 btv
->crop_start
= tvnorm
->cropcap
.bounds
.top
1316 + tvnorm
->cropcap
.bounds
.height
;
1322 btwrite(tvnorm
->adelay
, BT848_ADELAY
);
1323 btwrite(tvnorm
->bdelay
, BT848_BDELAY
);
1324 btaor(tvnorm
->iform
,~(BT848_IFORM_NORM
|BT848_IFORM_XTBOTH
),
1326 btwrite(tvnorm
->vbipack
, BT848_VBI_PACK_SIZE
);
1327 btwrite(1, BT848_VBI_PACK_DEL
);
1328 bt848A_set_timing(btv
);
1330 switch (btv
->c
.type
) {
1331 case BTTV_BOARD_VOODOOTV_FM
:
1332 case BTTV_BOARD_VOODOOTV_200
:
1333 bttv_tda9880_setnorm(btv
,norm
);
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
,(input
== bttv_tvcards
[btv
->c
.type
].tuner
?
1359 TVAUDIO_INPUT_TUNER
: TVAUDIO_INPUT_EXTERN
));
1360 set_tvnorm(btv
, norm
);
1361 i2c_vidiocschan(btv
);
1364 static void init_irqreg(struct bttv
*btv
)
1367 btwrite(0xfffffUL
, BT848_INT_STAT
);
1369 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1371 btwrite(BT848_INT_I2CDONE
,
1375 btwrite((btv
->triton1
) |
1376 (btv
->gpioirq
? BT848_INT_GPINT
: 0) |
1378 (fdsr
? BT848_INT_FDSR
: 0) |
1379 BT848_INT_RISCI
|BT848_INT_OCERR
|BT848_INT_VPRES
|
1380 BT848_INT_FMTCHG
|BT848_INT_HLOCK
|
1386 static void init_bt848(struct bttv
*btv
)
1390 if (bttv_tvcards
[btv
->c
.type
].no_video
) {
1391 /* very basic init only */
1396 btwrite(0x00, BT848_CAP_CTL
);
1397 btwrite(BT848_COLOR_CTL_GAMMA
, BT848_COLOR_CTL
);
1398 btwrite(BT848_IFORM_XTAUTO
| BT848_IFORM_AUTO
, BT848_IFORM
);
1400 /* set planar and packed mode trigger points and */
1401 /* set rising edge of inverted GPINTR pin as irq trigger */
1402 btwrite(BT848_GPIO_DMA_CTL_PKTP_32
|
1403 BT848_GPIO_DMA_CTL_PLTP1_16
|
1404 BT848_GPIO_DMA_CTL_PLTP23_16
|
1405 BT848_GPIO_DMA_CTL_GPINTC
|
1406 BT848_GPIO_DMA_CTL_GPINTI
,
1407 BT848_GPIO_DMA_CTL
);
1409 val
= btv
->opt_chroma_agc
? BT848_SCLOOP_CAGC
: 0;
1410 btwrite(val
, BT848_E_SCLOOP
);
1411 btwrite(val
, BT848_O_SCLOOP
);
1413 btwrite(0x20, BT848_E_VSCALE_HI
);
1414 btwrite(0x20, BT848_O_VSCALE_HI
);
1415 btwrite(BT848_ADC_RESERVED
| (btv
->opt_adc_crush
? BT848_ADC_CRUSH
: 0),
1418 btwrite(whitecrush_upper
, BT848_WC_UP
);
1419 btwrite(whitecrush_lower
, BT848_WC_DOWN
);
1421 if (btv
->opt_lumafilter
) {
1422 btwrite(0, BT848_E_CONTROL
);
1423 btwrite(0, BT848_O_CONTROL
);
1425 btwrite(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1426 btwrite(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1429 bt848_bright(btv
, btv
->bright
);
1430 bt848_hue(btv
, btv
->hue
);
1431 bt848_contrast(btv
, btv
->contrast
);
1432 bt848_sat(btv
, btv
->saturation
);
1438 static void bttv_reinit_bt848(struct bttv
*btv
)
1440 unsigned long flags
;
1443 printk(KERN_INFO
"bttv%d: reset, reinitialize\n",btv
->c
.nr
);
1444 spin_lock_irqsave(&btv
->s_lock
,flags
);
1446 bttv_set_dma(btv
,0);
1447 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1450 btv
->pll
.pll_current
= -1;
1451 set_input(btv
, btv
->input
, btv
->tvnorm
);
1454 static int get_control(struct bttv
*btv
, struct v4l2_control
*c
)
1456 struct video_audio va
;
1459 for (i
= 0; i
< BTTV_CTLS
; i
++)
1460 if (bttv_ctls
[i
].id
== c
->id
)
1464 if (btv
->audio_hook
&& i
>= 4 && i
<= 8) {
1465 memset(&va
,0,sizeof(va
));
1466 btv
->audio_hook(btv
,&va
,0);
1468 case V4L2_CID_AUDIO_MUTE
:
1469 c
->value
= (VIDEO_AUDIO_MUTE
& va
.flags
) ? 1 : 0;
1471 case V4L2_CID_AUDIO_VOLUME
:
1472 c
->value
= va
.volume
;
1474 case V4L2_CID_AUDIO_BALANCE
:
1475 c
->value
= va
.balance
;
1477 case V4L2_CID_AUDIO_BASS
:
1480 case V4L2_CID_AUDIO_TREBLE
:
1481 c
->value
= va
.treble
;
1487 case V4L2_CID_BRIGHTNESS
:
1488 c
->value
= btv
->bright
;
1491 c
->value
= btv
->hue
;
1493 case V4L2_CID_CONTRAST
:
1494 c
->value
= btv
->contrast
;
1496 case V4L2_CID_SATURATION
:
1497 c
->value
= btv
->saturation
;
1500 case V4L2_CID_AUDIO_MUTE
:
1501 case V4L2_CID_AUDIO_VOLUME
:
1502 case V4L2_CID_AUDIO_BALANCE
:
1503 case V4L2_CID_AUDIO_BASS
:
1504 case V4L2_CID_AUDIO_TREBLE
:
1505 bttv_call_i2c_clients(btv
,VIDIOC_G_CTRL
,c
);
1508 case V4L2_CID_PRIVATE_CHROMA_AGC
:
1509 c
->value
= btv
->opt_chroma_agc
;
1511 case V4L2_CID_PRIVATE_COMBFILTER
:
1512 c
->value
= btv
->opt_combfilter
;
1514 case V4L2_CID_PRIVATE_LUMAFILTER
:
1515 c
->value
= btv
->opt_lumafilter
;
1517 case V4L2_CID_PRIVATE_AUTOMUTE
:
1518 c
->value
= btv
->opt_automute
;
1520 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1521 c
->value
= btv
->opt_adc_crush
;
1523 case V4L2_CID_PRIVATE_VCR_HACK
:
1524 c
->value
= btv
->opt_vcr_hack
;
1526 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1527 c
->value
= btv
->opt_whitecrush_upper
;
1529 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1530 c
->value
= btv
->opt_whitecrush_lower
;
1532 case V4L2_CID_PRIVATE_UV_RATIO
:
1533 c
->value
= btv
->opt_uv_ratio
;
1535 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1536 c
->value
= btv
->opt_full_luma_range
;
1538 case V4L2_CID_PRIVATE_CORING
:
1539 c
->value
= btv
->opt_coring
;
1547 static int set_control(struct bttv
*btv
, struct v4l2_control
*c
)
1549 struct video_audio va
;
1552 for (i
= 0; i
< BTTV_CTLS
; i
++)
1553 if (bttv_ctls
[i
].id
== c
->id
)
1557 if (btv
->audio_hook
&& i
>= 4 && i
<= 8) {
1558 memset(&va
,0,sizeof(va
));
1559 btv
->audio_hook(btv
,&va
,0);
1561 case V4L2_CID_AUDIO_MUTE
:
1563 va
.flags
|= VIDEO_AUDIO_MUTE
;
1566 va
.flags
&= ~VIDEO_AUDIO_MUTE
;
1571 case V4L2_CID_AUDIO_VOLUME
:
1572 va
.volume
= c
->value
;
1574 case V4L2_CID_AUDIO_BALANCE
:
1575 va
.balance
= c
->value
;
1577 case V4L2_CID_AUDIO_BASS
:
1580 case V4L2_CID_AUDIO_TREBLE
:
1581 va
.treble
= c
->value
;
1584 btv
->audio_hook(btv
,&va
,1);
1588 case V4L2_CID_BRIGHTNESS
:
1589 bt848_bright(btv
,c
->value
);
1592 bt848_hue(btv
,c
->value
);
1594 case V4L2_CID_CONTRAST
:
1595 bt848_contrast(btv
,c
->value
);
1597 case V4L2_CID_SATURATION
:
1598 bt848_sat(btv
,c
->value
);
1600 case V4L2_CID_AUDIO_MUTE
:
1601 audio_mute(btv
, c
->value
);
1603 case V4L2_CID_AUDIO_VOLUME
:
1604 case V4L2_CID_AUDIO_BALANCE
:
1605 case V4L2_CID_AUDIO_BASS
:
1606 case V4L2_CID_AUDIO_TREBLE
:
1607 bttv_call_i2c_clients(btv
,VIDIOC_S_CTRL
,c
);
1610 case V4L2_CID_PRIVATE_CHROMA_AGC
:
1611 btv
->opt_chroma_agc
= c
->value
;
1612 val
= btv
->opt_chroma_agc
? BT848_SCLOOP_CAGC
: 0;
1613 btwrite(val
, BT848_E_SCLOOP
);
1614 btwrite(val
, BT848_O_SCLOOP
);
1616 case V4L2_CID_PRIVATE_COMBFILTER
:
1617 btv
->opt_combfilter
= c
->value
;
1619 case V4L2_CID_PRIVATE_LUMAFILTER
:
1620 btv
->opt_lumafilter
= c
->value
;
1621 if (btv
->opt_lumafilter
) {
1622 btand(~BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1623 btand(~BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1625 btor(BT848_CONTROL_LDEC
, BT848_E_CONTROL
);
1626 btor(BT848_CONTROL_LDEC
, BT848_O_CONTROL
);
1629 case V4L2_CID_PRIVATE_AUTOMUTE
:
1630 btv
->opt_automute
= c
->value
;
1632 case V4L2_CID_PRIVATE_AGC_CRUSH
:
1633 btv
->opt_adc_crush
= c
->value
;
1634 btwrite(BT848_ADC_RESERVED
| (btv
->opt_adc_crush
? BT848_ADC_CRUSH
: 0),
1637 case V4L2_CID_PRIVATE_VCR_HACK
:
1638 btv
->opt_vcr_hack
= c
->value
;
1640 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER
:
1641 btv
->opt_whitecrush_upper
= c
->value
;
1642 btwrite(c
->value
, BT848_WC_UP
);
1644 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER
:
1645 btv
->opt_whitecrush_lower
= c
->value
;
1646 btwrite(c
->value
, BT848_WC_DOWN
);
1648 case V4L2_CID_PRIVATE_UV_RATIO
:
1649 btv
->opt_uv_ratio
= c
->value
;
1650 bt848_sat(btv
, btv
->saturation
);
1652 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE
:
1653 btv
->opt_full_luma_range
= c
->value
;
1654 btaor((c
->value
<<7), ~BT848_OFORM_RANGE
, BT848_OFORM
);
1656 case V4L2_CID_PRIVATE_CORING
:
1657 btv
->opt_coring
= c
->value
;
1658 btaor((c
->value
<<5), ~BT848_OFORM_CORE32
, BT848_OFORM
);
1666 /* ----------------------------------------------------------------------- */
1668 void bttv_gpio_tracking(struct bttv
*btv
, char *comment
)
1670 unsigned int outbits
, data
;
1671 outbits
= btread(BT848_GPIO_OUT_EN
);
1672 data
= btread(BT848_GPIO_DATA
);
1673 printk(KERN_DEBUG
"bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1674 btv
->c
.nr
,outbits
,data
& outbits
, data
& ~outbits
, comment
);
1677 static void bttv_field_count(struct bttv
*btv
)
1685 /* start field counter */
1686 btor(BT848_INT_VSYNC
,BT848_INT_MASK
);
1688 /* stop field counter */
1689 btand(~BT848_INT_VSYNC
,BT848_INT_MASK
);
1690 btv
->field_count
= 0;
1694 static const struct bttv_format
*
1695 format_by_palette(int palette
)
1699 for (i
= 0; i
< BTTV_FORMATS
; i
++) {
1700 if (-1 == bttv_formats
[i
].palette
)
1702 if (bttv_formats
[i
].palette
== palette
)
1703 return bttv_formats
+i
;
1708 static const struct bttv_format
*
1709 format_by_fourcc(int fourcc
)
1713 for (i
= 0; i
< BTTV_FORMATS
; i
++) {
1714 if (-1 == bttv_formats
[i
].fourcc
)
1716 if (bttv_formats
[i
].fourcc
== fourcc
)
1717 return bttv_formats
+i
;
1722 /* ----------------------------------------------------------------------- */
1726 bttv_switch_overlay(struct bttv
*btv
, struct bttv_fh
*fh
,
1727 struct bttv_buffer
*new)
1729 struct bttv_buffer
*old
;
1730 unsigned long flags
;
1733 dprintk("switch_overlay: enter [new=%p]\n",new);
1735 new->vb
.state
= STATE_DONE
;
1736 spin_lock_irqsave(&btv
->s_lock
,flags
);
1740 bttv_set_dma(btv
, 0x03);
1741 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
1743 dprintk("switch_overlay: old=%p state is %d\n",old
,old
->vb
.state
);
1744 bttv_dma_free(&fh
->cap
,btv
, old
);
1748 free_btres(btv
,fh
,RESOURCE_OVERLAY
);
1749 dprintk("switch_overlay: done\n");
1753 /* ----------------------------------------------------------------------- */
1754 /* video4linux (1) interface */
1756 static int bttv_prepare_buffer(struct videobuf_queue
*q
,struct bttv
*btv
,
1757 struct bttv_buffer
*buf
,
1758 const struct bttv_format
*fmt
,
1759 unsigned int width
, unsigned int height
,
1760 enum v4l2_field field
)
1762 struct bttv_fh
*fh
= q
->priv_data
;
1763 int redo_dma_risc
= 0;
1768 /* check settings */
1771 if (fmt
->btformat
== BT848_COLOR_FMT_RAW
) {
1773 height
= RAW_LINES
*2;
1774 if (width
*height
> buf
->vb
.bsize
)
1776 buf
->vb
.size
= buf
->vb
.bsize
;
1778 /* Make sure tvnorm and vbi_end remain consistent
1779 until we're done. */
1780 mutex_lock(&btv
->lock
);
1784 /* In this mode capturing always starts at defrect.top
1785 (default VDELAY), ignoring cropping parameters. */
1786 if (btv
->vbi_end
> bttv_tvnorms
[norm
].cropcap
.defrect
.top
) {
1787 mutex_unlock(&btv
->lock
);
1791 mutex_unlock(&btv
->lock
);
1793 c
.rect
= bttv_tvnorms
[norm
].cropcap
.defrect
;
1795 mutex_lock(&btv
->lock
);
1798 c
= btv
->crop
[!!fh
->do_crop
];
1800 mutex_unlock(&btv
->lock
);
1802 if (width
< c
.min_scaled_width
||
1803 width
> c
.max_scaled_width
||
1804 height
< c
.min_scaled_height
)
1808 case V4L2_FIELD_TOP
:
1809 case V4L2_FIELD_BOTTOM
:
1810 case V4L2_FIELD_ALTERNATE
:
1811 /* btv->crop counts frame lines. Max. scale
1812 factor is 16:1 for frames, 8:1 for fields. */
1813 if (height
* 2 > c
.max_scaled_height
)
1818 if (height
> c
.max_scaled_height
)
1823 buf
->vb
.size
= (width
* height
* fmt
->depth
) >> 3;
1824 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
1828 /* alloc + fill struct bttv_buffer (if changed) */
1829 if (buf
->vb
.width
!= width
|| buf
->vb
.height
!= height
||
1830 buf
->vb
.field
!= field
||
1831 buf
->tvnorm
!= norm
|| buf
->fmt
!= fmt
||
1832 buf
->crop
.top
!= c
.rect
.top
||
1833 buf
->crop
.left
!= c
.rect
.left
||
1834 buf
->crop
.width
!= c
.rect
.width
||
1835 buf
->crop
.height
!= c
.rect
.height
) {
1836 buf
->vb
.width
= width
;
1837 buf
->vb
.height
= height
;
1838 buf
->vb
.field
= field
;
1845 /* alloc risc memory */
1846 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
1848 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,&btv
->fbuf
)))
1853 if (0 != (rc
= bttv_buffer_risc(btv
,buf
)))
1856 buf
->vb
.state
= STATE_PREPARED
;
1860 bttv_dma_free(q
,btv
,buf
);
1865 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
1867 struct bttv_fh
*fh
= q
->priv_data
;
1869 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
1872 while (*size
* *count
> gbuffers
* gbufsize
)
1878 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
1879 enum v4l2_field field
)
1881 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1882 struct bttv_fh
*fh
= q
->priv_data
;
1884 return bttv_prepare_buffer(q
,fh
->btv
, buf
, fh
->fmt
,
1885 fh
->width
, fh
->height
, field
);
1889 buffer_queue(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1891 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1892 struct bttv_fh
*fh
= q
->priv_data
;
1893 struct bttv
*btv
= fh
->btv
;
1895 buf
->vb
.state
= STATE_QUEUED
;
1896 list_add_tail(&buf
->vb
.queue
,&btv
->capture
);
1897 if (!btv
->curr
.frame_irq
) {
1899 bttv_set_dma(btv
, 0x03);
1903 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
1905 struct bttv_buffer
*buf
= container_of(vb
,struct bttv_buffer
,vb
);
1906 struct bttv_fh
*fh
= q
->priv_data
;
1908 bttv_dma_free(q
,fh
->btv
,buf
);
1911 static struct videobuf_queue_ops bttv_video_qops
= {
1912 .buf_setup
= buffer_setup
,
1913 .buf_prepare
= buffer_prepare
,
1914 .buf_queue
= buffer_queue
,
1915 .buf_release
= buffer_release
,
1918 static int bttv_common_ioctls(struct bttv
*btv
, unsigned int cmd
, void *arg
)
1922 return BTTV_VERSION_CODE
;
1924 /* *** v4l1 *** ************************************************ */
1927 unsigned long *freq
= arg
;
1933 struct v4l2_frequency freq
;
1935 memset(&freq
, 0, sizeof(freq
));
1936 freq
.frequency
= *(unsigned long *)arg
;
1937 mutex_lock(&btv
->lock
);
1938 freq
.type
= btv
->radio_user
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1939 btv
->freq
= *(unsigned long *)arg
;
1940 bttv_call_i2c_clients(btv
,VIDIOC_S_FREQUENCY
,&freq
);
1941 if (btv
->has_matchbox
&& btv
->radio_user
)
1942 tea5757_set_freq(btv
,*(unsigned long *)arg
);
1943 mutex_unlock(&btv
->lock
);
1949 struct video_tuner
*v
= arg
;
1951 if (UNSET
== bttv_tvcards
[btv
->c
.type
].tuner
)
1953 if (v
->tuner
) /* Only tuner 0 */
1955 strcpy(v
->name
, "Television");
1957 v
->rangehigh
= 0x7FFFFFFF;
1958 v
->flags
= VIDEO_TUNER_PAL
|VIDEO_TUNER_NTSC
|VIDEO_TUNER_SECAM
;
1959 v
->mode
= btv
->tvnorm
;
1960 v
->signal
= (btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
) ? 0xFFFF : 0;
1961 bttv_call_i2c_clients(btv
,cmd
,v
);
1966 struct video_tuner
*v
= arg
;
1968 if (v
->tuner
) /* Only tuner 0 */
1970 if (v
->mode
>= BTTV_TVNORMS
)
1973 mutex_lock(&btv
->lock
);
1974 set_tvnorm(btv
,v
->mode
);
1975 bttv_call_i2c_clients(btv
,cmd
,v
);
1976 mutex_unlock(&btv
->lock
);
1982 struct video_channel
*v
= arg
;
1983 unsigned int channel
= v
->channel
;
1985 if (channel
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
1988 v
->flags
= VIDEO_VC_AUDIO
;
1989 v
->type
= VIDEO_TYPE_CAMERA
;
1990 v
->norm
= btv
->tvnorm
;
1991 if (channel
== bttv_tvcards
[btv
->c
.type
].tuner
) {
1992 strcpy(v
->name
,"Television");
1993 v
->flags
|=VIDEO_VC_TUNER
;
1994 v
->type
=VIDEO_TYPE_TV
;
1996 } else if (channel
== btv
->svhs
) {
1997 strcpy(v
->name
,"S-Video");
1999 sprintf(v
->name
,"Composite%d",channel
);
2005 struct video_channel
*v
= arg
;
2006 unsigned int channel
= v
->channel
;
2008 if (channel
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
2010 if (v
->norm
>= BTTV_TVNORMS
)
2013 mutex_lock(&btv
->lock
);
2014 if (channel
== btv
->input
&&
2015 v
->norm
== btv
->tvnorm
) {
2017 mutex_unlock(&btv
->lock
);
2021 set_input(btv
, v
->channel
, v
->norm
);
2022 mutex_unlock(&btv
->lock
);
2028 struct video_audio
*v
= arg
;
2030 memset(v
,0,sizeof(*v
));
2031 strcpy(v
->name
,"Television");
2032 v
->flags
|= VIDEO_AUDIO_MUTABLE
;
2033 v
->mode
= VIDEO_SOUND_MONO
;
2035 mutex_lock(&btv
->lock
);
2036 bttv_call_i2c_clients(btv
,cmd
,v
);
2038 /* card specific hooks */
2039 if (btv
->audio_hook
)
2040 btv
->audio_hook(btv
,v
,0);
2042 mutex_unlock(&btv
->lock
);
2047 struct video_audio
*v
= arg
;
2048 unsigned int audio
= v
->audio
;
2050 if (audio
>= bttv_tvcards
[btv
->c
.type
].audio_inputs
)
2053 mutex_lock(&btv
->lock
);
2054 audio_mute(btv
, (v
->flags
&VIDEO_AUDIO_MUTE
) ? 1 : 0);
2055 bttv_call_i2c_clients(btv
,cmd
,v
);
2057 /* card specific hooks */
2058 if (btv
->audio_hook
)
2059 btv
->audio_hook(btv
,v
,1);
2061 mutex_unlock(&btv
->lock
);
2065 /* *** v4l2 *** ************************************************ */
2066 case VIDIOC_ENUMSTD
:
2068 struct v4l2_standard
*e
= arg
;
2069 unsigned int index
= e
->index
;
2071 if (index
>= BTTV_TVNORMS
)
2073 v4l2_video_std_construct(e
, bttv_tvnorms
[e
->index
].v4l2_id
,
2074 bttv_tvnorms
[e
->index
].name
);
2080 v4l2_std_id
*id
= arg
;
2081 *id
= bttv_tvnorms
[btv
->tvnorm
].v4l2_id
;
2086 v4l2_std_id
*id
= arg
;
2089 for (i
= 0; i
< BTTV_TVNORMS
; i
++)
2090 if (*id
& bttv_tvnorms
[i
].v4l2_id
)
2092 if (i
== BTTV_TVNORMS
)
2095 mutex_lock(&btv
->lock
);
2097 i2c_vidiocschan(btv
);
2098 mutex_unlock(&btv
->lock
);
2101 case VIDIOC_QUERYSTD
:
2103 v4l2_std_id
*id
= arg
;
2105 if (btread(BT848_DSTATUS
) & BT848_DSTATUS_NUML
)
2106 *id
= V4L2_STD_625_50
;
2108 *id
= V4L2_STD_525_60
;
2112 case VIDIOC_ENUMINPUT
:
2114 struct v4l2_input
*i
= arg
;
2118 if (n
>= bttv_tvcards
[btv
->c
.type
].video_inputs
)
2120 memset(i
,0,sizeof(*i
));
2122 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
2124 if (i
->index
== bttv_tvcards
[btv
->c
.type
].tuner
) {
2125 sprintf(i
->name
, "Television");
2126 i
->type
= V4L2_INPUT_TYPE_TUNER
;
2128 } else if (i
->index
== btv
->svhs
) {
2129 sprintf(i
->name
, "S-Video");
2131 sprintf(i
->name
,"Composite%d",i
->index
);
2133 if (i
->index
== btv
->input
) {
2134 __u32 dstatus
= btread(BT848_DSTATUS
);
2135 if (0 == (dstatus
& BT848_DSTATUS_PRES
))
2136 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
2137 if (0 == (dstatus
& BT848_DSTATUS_HLOC
))
2138 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
2140 for (n
= 0; n
< BTTV_TVNORMS
; n
++)
2141 i
->std
|= bttv_tvnorms
[n
].v4l2_id
;
2144 case VIDIOC_G_INPUT
:
2150 case VIDIOC_S_INPUT
:
2152 unsigned int *i
= arg
;
2154 if (*i
> bttv_tvcards
[btv
->c
.type
].video_inputs
)
2156 mutex_lock(&btv
->lock
);
2157 set_input(btv
, *i
, btv
->tvnorm
);
2158 mutex_unlock(&btv
->lock
);
2162 case VIDIOC_G_TUNER
:
2164 struct v4l2_tuner
*t
= arg
;
2166 if (UNSET
== bttv_tvcards
[btv
->c
.type
].tuner
)
2170 mutex_lock(&btv
->lock
);
2171 memset(t
,0,sizeof(*t
));
2172 t
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
2173 bttv_call_i2c_clients(btv
, VIDIOC_G_TUNER
, t
);
2174 strcpy(t
->name
, "Television");
2175 t
->capability
= V4L2_TUNER_CAP_NORM
;
2176 t
->type
= V4L2_TUNER_ANALOG_TV
;
2177 if (btread(BT848_DSTATUS
)&BT848_DSTATUS_HLOC
)
2180 if (btv
->audio_hook
) {
2182 struct video_audio va
;
2183 memset(&va
, 0, sizeof(struct video_audio
));
2184 btv
->audio_hook(btv
,&va
,0);
2185 t
->audmode
= V4L2_TUNER_MODE_MONO
;
2186 t
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
2187 if(va
.mode
& VIDEO_SOUND_STEREO
) {
2188 t
->audmode
= V4L2_TUNER_MODE_STEREO
;
2189 t
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
2191 if(va
.mode
& VIDEO_SOUND_LANG2
) {
2192 t
->audmode
= V4L2_TUNER_MODE_LANG1
;
2193 t
->rxsubchans
= V4L2_TUNER_SUB_LANG1
2194 | V4L2_TUNER_SUB_LANG2
;
2197 /* FIXME: fill capability+audmode */
2198 mutex_unlock(&btv
->lock
);
2201 case VIDIOC_S_TUNER
:
2203 struct v4l2_tuner
*t
= arg
;
2205 if (UNSET
== bttv_tvcards
[btv
->c
.type
].tuner
)
2209 mutex_lock(&btv
->lock
);
2210 bttv_call_i2c_clients(btv
, VIDIOC_S_TUNER
, t
);
2211 if (btv
->audio_hook
) {
2212 struct video_audio va
;
2213 memset(&va
, 0, sizeof(struct video_audio
));
2214 if (t
->audmode
== V4L2_TUNER_MODE_MONO
)
2215 va
.mode
= VIDEO_SOUND_MONO
;
2216 else if (t
->audmode
== V4L2_TUNER_MODE_STEREO
||
2217 t
->audmode
== V4L2_TUNER_MODE_LANG1_LANG2
)
2218 va
.mode
= VIDEO_SOUND_STEREO
;
2219 else if (t
->audmode
== V4L2_TUNER_MODE_LANG1
)
2220 va
.mode
= VIDEO_SOUND_LANG1
;
2221 else if (t
->audmode
== V4L2_TUNER_MODE_LANG2
)
2222 va
.mode
= VIDEO_SOUND_LANG2
;
2223 btv
->audio_hook(btv
,&va
,1);
2225 mutex_unlock(&btv
->lock
);
2229 case VIDIOC_G_FREQUENCY
:
2231 struct v4l2_frequency
*f
= arg
;
2233 memset(f
,0,sizeof(*f
));
2234 f
->type
= V4L2_TUNER_ANALOG_TV
;
2235 f
->frequency
= btv
->freq
;
2238 case VIDIOC_S_FREQUENCY
:
2240 struct v4l2_frequency
*f
= arg
;
2242 if (unlikely(f
->tuner
!= 0))
2244 if (unlikely (f
->type
!= V4L2_TUNER_ANALOG_TV
))
2246 mutex_lock(&btv
->lock
);
2247 btv
->freq
= f
->frequency
;
2248 bttv_call_i2c_clients(btv
,VIDIOC_S_FREQUENCY
,f
);
2249 if (btv
->has_matchbox
&& btv
->radio_user
)
2250 tea5757_set_freq(btv
,btv
->freq
);
2251 mutex_unlock(&btv
->lock
);
2254 case VIDIOC_LOG_STATUS
:
2256 printk(KERN_INFO
"bttv%d: ================= START STATUS CARD #%d =================\n", btv
->c
.nr
, btv
->c
.nr
);
2257 bttv_call_i2c_clients(btv
, VIDIOC_LOG_STATUS
, NULL
);
2258 printk(KERN_INFO
"bttv%d: ================== END STATUS CARD #%d ==================\n", btv
->c
.nr
, btv
->c
.nr
);
2261 #ifdef CONFIG_VIDEO_ADV_DEBUG
2262 case VIDIOC_DBG_G_REGISTER
:
2263 case VIDIOC_DBG_S_REGISTER
:
2265 struct v4l2_register
*reg
= arg
;
2266 if (!capable(CAP_SYS_ADMIN
))
2268 if (!v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
))
2270 /* bt848 has a 12-bit register space */
2272 if (cmd
== VIDIOC_DBG_G_REGISTER
)
2273 reg
->val
= btread(reg
->reg
);
2275 btwrite(reg
->val
, reg
->reg
);
2281 return -ENOIOCTLCMD
;
2287 /* Given cropping boundaries b and the scaled width and height of a
2288 single field or frame, which must not exceed hardware limits, this
2289 function adjusts the cropping parameters c. */
2291 bttv_crop_adjust (struct bttv_crop
* c
,
2292 const struct v4l2_rect
* b
,
2295 enum v4l2_field field
)
2297 __s32 frame_height
= height
<< !V4L2_FIELD_HAS_BOTH(field
);
2301 if (width
< c
->min_scaled_width
) {
2302 /* Max. hor. scale factor 16:1. */
2303 c
->rect
.width
= width
* 16;
2304 } else if (width
> c
->max_scaled_width
) {
2305 /* Min. hor. scale factor 1:1. */
2306 c
->rect
.width
= width
;
2308 max_left
= b
->left
+ b
->width
- width
;
2309 max_left
= min(max_left
, (__s32
) MAX_HDELAY
);
2310 if (c
->rect
.left
> max_left
)
2311 c
->rect
.left
= max_left
;
2314 if (height
< c
->min_scaled_height
) {
2315 /* Max. vert. scale factor 16:1, single fields 8:1. */
2316 c
->rect
.height
= height
* 16;
2317 } else if (frame_height
> c
->max_scaled_height
) {
2318 /* Min. vert. scale factor 1:1.
2319 Top and height count field lines times two. */
2320 c
->rect
.height
= (frame_height
+ 1) & ~1;
2322 max_top
= b
->top
+ b
->height
- c
->rect
.height
;
2323 if (c
->rect
.top
> max_top
)
2324 c
->rect
.top
= max_top
;
2327 bttv_crop_calc_limits(c
);
2330 /* Returns an error if scaling to a frame or single field with the given
2331 width and height is not possible with the current cropping parameters
2332 and width aligned according to width_mask. If adjust_size is TRUE the
2333 function may adjust the width and/or height instead, rounding width
2334 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2335 also adjust the current cropping parameters to get closer to the
2336 desired image size. */
2338 limit_scaled_size (struct bttv_fh
* fh
,
2341 enum v4l2_field field
,
2342 unsigned int width_mask
,
2343 unsigned int width_bias
,
2347 struct bttv
*btv
= fh
->btv
;
2348 const struct v4l2_rect
*b
;
2349 struct bttv_crop
*c
;
2356 BUG_ON((int) width_mask
>= 0 ||
2357 width_bias
>= (unsigned int) -width_mask
);
2359 /* Make sure tvnorm, vbi_end and the current cropping parameters
2360 remain consistent until we're done. */
2361 mutex_lock(&btv
->lock
);
2363 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
2365 /* Do crop - use current, don't - use default parameters. */
2366 c
= &btv
->crop
[!!fh
->do_crop
];
2371 && !locked_btres(btv
, VIDEO_RESOURCES
)) {
2375 /* We cannot scale up. When the scaled image is larger
2376 than crop.rect we adjust the crop.rect as required
2377 by the V4L2 spec, hence cropcap.bounds are our limit. */
2378 max_width
= min(b
->width
, (__s32
) MAX_HACTIVE
);
2379 max_height
= b
->height
;
2381 /* We cannot capture the same line as video and VBI data.
2382 Note btv->vbi_end is really a minimum, see
2383 bttv_vbi_try_fmt(). */
2384 if (btv
->vbi_end
> b
->top
) {
2385 max_height
-= btv
->vbi_end
- b
->top
;
2387 if (min_height
> max_height
)
2392 if (btv
->vbi_end
> c
->rect
.top
)
2395 min_width
= c
->min_scaled_width
;
2396 min_height
= c
->min_scaled_height
;
2397 max_width
= c
->max_scaled_width
;
2398 max_height
= c
->max_scaled_height
;
2403 min_width
= (min_width
- width_mask
- 1) & width_mask
;
2404 max_width
= max_width
& width_mask
;
2406 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2407 min_height
= min_height
;
2408 /* Min. scale factor is 1:1. */
2409 max_height
>>= !V4L2_FIELD_HAS_BOTH(field
);
2412 *width
= clamp(*width
, min_width
, max_width
);
2413 *height
= clamp(*height
, min_height
, max_height
);
2415 /* Round after clamping to avoid overflow. */
2416 *width
= (*width
+ width_bias
) & width_mask
;
2419 bttv_crop_adjust(c
, b
, *width
, *height
, field
);
2421 if (btv
->vbi_end
> c
->rect
.top
) {
2422 /* Move the crop window out of the way. */
2423 c
->rect
.top
= btv
->vbi_end
;
2428 if (*width
< min_width
||
2429 *height
< min_height
||
2430 *width
> max_width
||
2431 *height
> max_height
||
2432 0 != (*width
& ~width_mask
))
2436 rc
= 0; /* success */
2439 mutex_unlock(&btv
->lock
);
2444 /* Returns an error if the given overlay window dimensions are not
2445 possible with the current cropping parameters. If adjust_size is
2446 TRUE the function may adjust the window width and/or height
2447 instead, however it always rounds the horizontal position and
2448 width as btcx_align() does. If adjust_crop is TRUE the function
2449 may also adjust the current cropping parameters to get closer
2450 to the desired window size. */
2452 verify_window (struct bttv_fh
* fh
,
2453 struct v4l2_window
* win
,
2457 enum v4l2_field field
;
2458 unsigned int width_mask
;
2461 if (win
->w
.width
< 48 || win
->w
.height
< 32)
2463 if (win
->clipcount
> 2048)
2468 if (V4L2_FIELD_ANY
== field
) {
2471 height2
= fh
->btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2472 field
= (win
->w
.height
> height2
)
2473 ? V4L2_FIELD_INTERLACED
2477 case V4L2_FIELD_TOP
:
2478 case V4L2_FIELD_BOTTOM
:
2479 case V4L2_FIELD_INTERLACED
:
2485 /* 4-byte alignment. */
2486 if (NULL
== fh
->ovfmt
)
2489 switch (fh
->ovfmt
->depth
) {
2503 win
->w
.width
-= win
->w
.left
& ~width_mask
;
2504 win
->w
.left
= (win
->w
.left
- width_mask
- 1) & width_mask
;
2506 rc
= limit_scaled_size(fh
, &win
->w
.width
, &win
->w
.height
,
2508 /* width_bias: round down */ 0,
2509 adjust_size
, adjust_crop
);
2517 static int setup_window(struct bttv_fh
*fh
, struct bttv
*btv
,
2518 struct v4l2_window
*win
, int fixup
)
2520 struct v4l2_clip
*clips
= NULL
;
2521 int n
,size
,retval
= 0;
2523 if (NULL
== fh
->ovfmt
)
2525 if (!(fh
->ovfmt
->flags
& FORMAT_FLAGS_PACKED
))
2527 retval
= verify_window(fh
, win
,
2528 /* adjust_size */ fixup
,
2529 /* adjust_crop */ fixup
);
2533 /* copy clips -- luckily v4l1 + v4l2 are binary
2534 compatible here ...*/
2536 size
= sizeof(*clips
)*(n
+4);
2537 clips
= kmalloc(size
,GFP_KERNEL
);
2541 if (copy_from_user(clips
,win
->clips
,sizeof(struct v4l2_clip
)*n
)) {
2546 /* clip against screen */
2547 if (NULL
!= btv
->fbuf
.base
)
2548 n
= btcx_screen_clips(btv
->fbuf
.fmt
.width
, btv
->fbuf
.fmt
.height
,
2550 btcx_sort_clips(clips
,n
);
2552 /* 4-byte alignments */
2553 switch (fh
->ovfmt
->depth
) {
2556 btcx_align(&win
->w
, clips
, n
, 3);
2559 btcx_align(&win
->w
, clips
, n
, 1);
2562 /* no alignment fixups needed */
2568 mutex_lock(&fh
->cap
.lock
);
2569 kfree(fh
->ov
.clips
);
2570 fh
->ov
.clips
= clips
;
2574 fh
->ov
.field
= win
->field
;
2575 fh
->ov
.setup_ok
= 1;
2576 btv
->init
.ov
.w
.width
= win
->w
.width
;
2577 btv
->init
.ov
.w
.height
= win
->w
.height
;
2578 btv
->init
.ov
.field
= win
->field
;
2580 /* update overlay if needed */
2582 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
2583 struct bttv_buffer
*new;
2585 new = videobuf_pci_alloc(sizeof(*new));
2586 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
2587 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
2588 retval
= bttv_switch_overlay(btv
,fh
,new);
2590 mutex_unlock(&fh
->cap
.lock
);
2594 /* ----------------------------------------------------------------------- */
2596 static struct videobuf_queue
* bttv_queue(struct bttv_fh
*fh
)
2598 struct videobuf_queue
* q
= NULL
;
2601 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2604 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2613 static int bttv_resource(struct bttv_fh
*fh
)
2618 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2619 res
= RESOURCE_VIDEO_STREAM
;
2621 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2630 static int bttv_switch_type(struct bttv_fh
*fh
, enum v4l2_buf_type type
)
2632 struct videobuf_queue
*q
= bttv_queue(fh
);
2633 int res
= bttv_resource(fh
);
2635 if (check_btres(fh
,res
))
2637 if (videobuf_queue_is_busy(q
))
2644 pix_format_set_size (struct v4l2_pix_format
* f
,
2645 const struct bttv_format
* fmt
,
2647 unsigned int height
)
2652 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
) {
2653 f
->bytesperline
= width
; /* Y plane */
2654 f
->sizeimage
= (width
* height
* fmt
->depth
) >> 3;
2656 f
->bytesperline
= (width
* fmt
->depth
) >> 3;
2657 f
->sizeimage
= height
* f
->bytesperline
;
2661 static int bttv_g_fmt(struct bttv_fh
*fh
, struct v4l2_format
*f
)
2664 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2665 memset(&f
->fmt
.pix
,0,sizeof(struct v4l2_pix_format
));
2666 pix_format_set_size (&f
->fmt
.pix
, fh
->fmt
,
2667 fh
->width
, fh
->height
);
2668 f
->fmt
.pix
.field
= fh
->cap
.field
;
2669 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
2671 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
2672 memset(&f
->fmt
.win
,0,sizeof(struct v4l2_window
));
2673 f
->fmt
.win
.w
= fh
->ov
.w
;
2674 f
->fmt
.win
.field
= fh
->ov
.field
;
2676 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2677 bttv_vbi_get_fmt(fh
, &f
->fmt
.vbi
);
2684 static int bttv_try_fmt(struct bttv_fh
*fh
, struct bttv
*btv
,
2685 struct v4l2_format
*f
, int adjust_crop
)
2688 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2690 const struct bttv_format
*fmt
;
2691 enum v4l2_field field
;
2692 __s32 width
, height
;
2695 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2699 field
= f
->fmt
.pix
.field
;
2700 if (V4L2_FIELD_ANY
== field
) {
2703 height2
= btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
2704 field
= (f
->fmt
.pix
.height
> height2
)
2705 ? V4L2_FIELD_INTERLACED
2706 : V4L2_FIELD_BOTTOM
;
2708 if (V4L2_FIELD_SEQ_BT
== field
)
2709 field
= V4L2_FIELD_SEQ_TB
;
2711 case V4L2_FIELD_TOP
:
2712 case V4L2_FIELD_BOTTOM
:
2713 case V4L2_FIELD_ALTERNATE
:
2714 case V4L2_FIELD_INTERLACED
:
2716 case V4L2_FIELD_SEQ_TB
:
2717 if (fmt
->flags
& FORMAT_FLAGS_PLANAR
)
2724 width
= f
->fmt
.pix
.width
;
2725 height
= f
->fmt
.pix
.height
;
2727 rc
= limit_scaled_size(fh
, &width
, &height
, field
,
2728 /* width_mask: 4 pixels */ ~3,
2729 /* width_bias: nearest */ 2,
2730 /* adjust_size */ 1,
2735 /* update data for the application */
2736 f
->fmt
.pix
.field
= field
;
2737 pix_format_set_size(&f
->fmt
.pix
, fmt
, width
, height
);
2741 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
2742 return verify_window(fh
, &f
->fmt
.win
,
2743 /* adjust_size */ 1,
2744 /* adjust_crop */ 0);
2745 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2746 return bttv_vbi_try_fmt(fh
, &f
->fmt
.vbi
);
2752 static int bttv_s_fmt(struct bttv_fh
*fh
, struct bttv
*btv
,
2753 struct v4l2_format
*f
)
2758 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
2760 const struct bttv_format
*fmt
;
2762 retval
= bttv_switch_type(fh
,f
->type
);
2765 retval
= bttv_try_fmt(fh
,btv
,f
, /* adjust_crop */ 1);
2768 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
2770 /* update our state informations */
2771 mutex_lock(&fh
->cap
.lock
);
2773 fh
->cap
.field
= f
->fmt
.pix
.field
;
2774 fh
->cap
.last
= V4L2_FIELD_NONE
;
2775 fh
->width
= f
->fmt
.pix
.width
;
2776 fh
->height
= f
->fmt
.pix
.height
;
2777 btv
->init
.fmt
= fmt
;
2778 btv
->init
.width
= f
->fmt
.pix
.width
;
2779 btv
->init
.height
= f
->fmt
.pix
.height
;
2780 mutex_unlock(&fh
->cap
.lock
);
2784 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
2785 if (no_overlay
> 0) {
2786 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2789 return setup_window(fh
, btv
, &f
->fmt
.win
, 1);
2790 case V4L2_BUF_TYPE_VBI_CAPTURE
:
2791 retval
= bttv_switch_type(fh
,f
->type
);
2794 return bttv_vbi_set_fmt(fh
, &f
->fmt
.vbi
);
2800 static int bttv_do_ioctl(struct inode
*inode
, struct file
*file
,
2801 unsigned int cmd
, void *arg
)
2803 struct bttv_fh
*fh
= file
->private_data
;
2804 struct bttv
*btv
= fh
->btv
;
2805 unsigned long flags
;
2809 v4l_print_ioctl(btv
->c
.name
, cmd
);
2812 bttv_reinit_bt848(btv
);
2820 case VIDIOC_S_INPUT
:
2821 case VIDIOC_S_TUNER
:
2822 case VIDIOC_S_FREQUENCY
:
2823 retval
= v4l2_prio_check(&btv
->prio
,&fh
->prio
);
2830 /* *** v4l1 *** ************************************************ */
2833 struct video_capability
*cap
= arg
;
2835 memset(cap
,0,sizeof(*cap
));
2836 strcpy(cap
->name
,btv
->video_dev
->name
);
2837 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
2839 cap
->type
= VID_TYPE_TUNER
|VID_TYPE_TELETEXT
;
2842 cap
->type
= VID_TYPE_CAPTURE
|
2846 if (no_overlay
<= 0)
2847 cap
->type
|= VID_TYPE_OVERLAY
;
2849 cap
->maxwidth
= bttv_tvnorms
[btv
->tvnorm
].swidth
;
2850 cap
->maxheight
= bttv_tvnorms
[btv
->tvnorm
].sheight
;
2852 cap
->minheight
= 32;
2854 cap
->channels
= bttv_tvcards
[btv
->c
.type
].video_inputs
;
2855 cap
->audios
= bttv_tvcards
[btv
->c
.type
].audio_inputs
;
2861 struct video_picture
*pic
= arg
;
2863 memset(pic
,0,sizeof(*pic
));
2864 pic
->brightness
= btv
->bright
;
2865 pic
->contrast
= btv
->contrast
;
2866 pic
->hue
= btv
->hue
;
2867 pic
->colour
= btv
->saturation
;
2869 pic
->depth
= fh
->fmt
->depth
;
2870 pic
->palette
= fh
->fmt
->palette
;
2876 struct video_picture
*pic
= arg
;
2877 const struct bttv_format
*fmt
;
2879 fmt
= format_by_palette(pic
->palette
);
2882 mutex_lock(&fh
->cap
.lock
);
2883 if (fmt
->depth
!= pic
->depth
) {
2885 goto fh_unlock_and_return
;
2887 if (fmt
->flags
& FORMAT_FLAGS_RAW
) {
2888 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2889 RAW_LINES * 2. F1 is stored at offset 0, F2
2890 at buffer size / 2. */
2891 fh
->width
= RAW_BPL
;
2892 fh
->height
= gbufsize
/ RAW_BPL
;
2893 btv
->init
.width
= RAW_BPL
;
2894 btv
->init
.height
= gbufsize
/ RAW_BPL
;
2898 btv
->init
.ovfmt
= fmt
;
2899 btv
->init
.fmt
= fmt
;
2901 /* dirty hack time: swap bytes for overlay if the
2902 display adaptor is big endian (insmod option) */
2903 if (fmt
->palette
== VIDEO_PALETTE_RGB555
||
2904 fmt
->palette
== VIDEO_PALETTE_RGB565
||
2905 fmt
->palette
== VIDEO_PALETTE_RGB32
) {
2909 bt848_bright(btv
,pic
->brightness
);
2910 bt848_contrast(btv
,pic
->contrast
);
2911 bt848_hue(btv
,pic
->hue
);
2912 bt848_sat(btv
,pic
->colour
);
2913 mutex_unlock(&fh
->cap
.lock
);
2919 struct video_window
*win
= arg
;
2921 memset(win
,0,sizeof(*win
));
2922 win
->x
= fh
->ov
.w
.left
;
2923 win
->y
= fh
->ov
.w
.top
;
2924 win
->width
= fh
->ov
.w
.width
;
2925 win
->height
= fh
->ov
.w
.height
;
2930 struct video_window
*win
= arg
;
2931 struct v4l2_window w2
;
2933 if (no_overlay
> 0) {
2934 printk ("VIDIOCSWIN: no_overlay\n");
2938 w2
.field
= V4L2_FIELD_ANY
;
2941 w2
.w
.width
= win
->width
;
2942 w2
.w
.height
= win
->height
;
2943 w2
.clipcount
= win
->clipcount
;
2944 w2
.clips
= (struct v4l2_clip __user
*)win
->clips
;
2945 retval
= setup_window(fh
, btv
, &w2
, 0);
2947 /* on v4l1 this ioctl affects the read() size too */
2948 fh
->width
= fh
->ov
.w
.width
;
2949 fh
->height
= fh
->ov
.w
.height
;
2950 btv
->init
.width
= fh
->ov
.w
.width
;
2951 btv
->init
.height
= fh
->ov
.w
.height
;
2958 struct video_buffer
*fbuf
= arg
;
2960 fbuf
->base
= btv
->fbuf
.base
;
2961 fbuf
->width
= btv
->fbuf
.fmt
.width
;
2962 fbuf
->height
= btv
->fbuf
.fmt
.height
;
2963 fbuf
->bytesperline
= btv
->fbuf
.fmt
.bytesperline
;
2965 fbuf
->depth
= fh
->ovfmt
->depth
;
2968 fbuf
->depth
= ((fbuf
->bytesperline
<<3)
2978 struct video_buffer
*fbuf
= arg
;
2979 const struct bttv_format
*fmt
;
2982 if(!capable(CAP_SYS_ADMIN
) &&
2983 !capable(CAP_SYS_RAWIO
))
2985 end
= (unsigned long)fbuf
->base
+
2986 fbuf
->height
* fbuf
->bytesperline
;
2987 mutex_lock(&fh
->cap
.lock
);
2990 switch (fbuf
->depth
) {
2992 fmt
= format_by_palette(VIDEO_PALETTE_HI240
);
2995 fmt
= format_by_palette(VIDEO_PALETTE_RGB565
);
2998 fmt
= format_by_palette(VIDEO_PALETTE_RGB24
);
3001 fmt
= format_by_palette(VIDEO_PALETTE_RGB32
);
3005 fmt
= format_by_palette(VIDEO_PALETTE_RGB555
);
3012 goto fh_unlock_and_return
;
3016 btv
->init
.ovfmt
= fmt
;
3017 btv
->init
.fmt
= fmt
;
3018 btv
->fbuf
.base
= fbuf
->base
;
3019 btv
->fbuf
.fmt
.width
= fbuf
->width
;
3020 btv
->fbuf
.fmt
.height
= fbuf
->height
;
3021 if (fbuf
->bytesperline
)
3022 btv
->fbuf
.fmt
.bytesperline
= fbuf
->bytesperline
;
3024 btv
->fbuf
.fmt
.bytesperline
= btv
->fbuf
.fmt
.width
*fbuf
->depth
/8;
3025 mutex_unlock(&fh
->cap
.lock
);
3030 case VIDIOC_OVERLAY
:
3032 struct bttv_buffer
*new;
3037 if (NULL
== btv
->fbuf
.base
)
3039 if (!fh
->ov
.setup_ok
) {
3040 dprintk("bttv%d: overlay: !setup_ok\n",btv
->c
.nr
);
3045 if (!check_alloc_btres(btv
,fh
,RESOURCE_OVERLAY
))
3048 mutex_lock(&fh
->cap
.lock
);
3050 fh
->ov
.tvnorm
= btv
->tvnorm
;
3051 new = videobuf_pci_alloc(sizeof(*new));
3052 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
3053 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
3059 retval
= bttv_switch_overlay(btv
,fh
,new);
3060 mutex_unlock(&fh
->cap
.lock
);
3066 struct video_mbuf
*mbuf
= arg
;
3069 mutex_lock(&fh
->cap
.lock
);
3070 retval
= videobuf_mmap_setup(&fh
->cap
,gbuffers
,gbufsize
,
3073 goto fh_unlock_and_return
;
3074 memset(mbuf
,0,sizeof(*mbuf
));
3075 mbuf
->frames
= gbuffers
;
3076 mbuf
->size
= gbuffers
* gbufsize
;
3077 for (i
= 0; i
< gbuffers
; i
++)
3078 mbuf
->offsets
[i
] = i
* gbufsize
;
3079 mutex_unlock(&fh
->cap
.lock
);
3082 case VIDIOCMCAPTURE
:
3084 struct video_mmap
*vm
= arg
;
3085 struct bttv_buffer
*buf
;
3086 enum v4l2_field field
;
3090 if (vm
->frame
>= VIDEO_MAX_FRAME
)
3093 res
= bttv_resource(fh
);
3094 if (!check_alloc_btres(btv
, fh
, res
))
3097 mutex_lock(&fh
->cap
.lock
);
3099 buf
= (struct bttv_buffer
*)fh
->cap
.bufs
[vm
->frame
];
3101 goto fh_unlock_and_return
;
3102 if (0 == buf
->vb
.baddr
)
3103 goto fh_unlock_and_return
;
3104 if (buf
->vb
.state
== STATE_QUEUED
||
3105 buf
->vb
.state
== STATE_ACTIVE
)
3106 goto fh_unlock_and_return
;
3108 height2
= btv
->crop
[!!fh
->do_crop
].rect
.height
>> 1;
3109 field
= (vm
->height
> height2
)
3110 ? V4L2_FIELD_INTERLACED
3111 : V4L2_FIELD_BOTTOM
;
3112 retval
= bttv_prepare_buffer(&fh
->cap
,btv
,buf
,
3113 format_by_palette(vm
->format
),
3114 vm
->width
,vm
->height
,field
);
3116 goto fh_unlock_and_return
;
3117 spin_lock_irqsave(&btv
->s_lock
,flags
);
3118 buffer_queue(&fh
->cap
,&buf
->vb
);
3119 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
3120 mutex_unlock(&fh
->cap
.lock
);
3126 struct bttv_buffer
*buf
;
3128 if (*frame
>= VIDEO_MAX_FRAME
)
3131 mutex_lock(&fh
->cap
.lock
);
3133 buf
= (struct bttv_buffer
*)fh
->cap
.bufs
[*frame
];
3135 goto fh_unlock_and_return
;
3136 retval
= videobuf_waiton(&buf
->vb
,0,1);
3138 goto fh_unlock_and_return
;
3139 switch (buf
->vb
.state
) {
3145 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
3146 videobuf_dma_sync(&fh
->cap
,dma
);
3147 bttv_dma_free(&fh
->cap
,btv
,buf
);
3154 mutex_unlock(&fh
->cap
.lock
);
3159 if (fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
) {
3160 retval
= bttv_switch_type(fh
,V4L2_BUF_TYPE_VBI_CAPTURE
);
3168 return v4l_compat_translate_ioctl(inode
, file
, cmd
,
3169 arg
, bttv_do_ioctl
);
3180 return bttv_common_ioctls(btv
,cmd
,arg
);
3182 /* *** v4l2 *** ************************************************ */
3183 case VIDIOC_QUERYCAP
:
3185 struct v4l2_capability
*cap
= arg
;
3189 memset(cap
, 0, sizeof (*cap
));
3190 strlcpy(cap
->driver
, "bttv", sizeof (cap
->driver
));
3191 strlcpy(cap
->card
, btv
->video_dev
->name
, sizeof (cap
->card
));
3192 snprintf(cap
->bus_info
, sizeof (cap
->bus_info
),
3193 "PCI:%s", pci_name(btv
->c
.pci
));
3194 cap
->version
= BTTV_VERSION_CODE
;
3196 V4L2_CAP_VIDEO_CAPTURE
|
3197 V4L2_CAP_VBI_CAPTURE
|
3198 V4L2_CAP_READWRITE
|
3200 if (no_overlay
<= 0)
3201 cap
->capabilities
|= V4L2_CAP_VIDEO_OVERLAY
;
3203 if (bttv_tvcards
[btv
->c
.type
].tuner
!= UNSET
&&
3204 bttv_tvcards
[btv
->c
.type
].tuner
!= TUNER_ABSENT
)
3205 cap
->capabilities
|= V4L2_CAP_TUNER
;
3209 case VIDIOC_ENUM_FMT
:
3211 struct v4l2_fmtdesc
*f
= arg
;
3212 enum v4l2_buf_type type
;
3217 if (V4L2_BUF_TYPE_VBI_CAPTURE
== type
) {
3222 memset(f
,0,sizeof(*f
));
3225 f
->pixelformat
= V4L2_PIX_FMT_GREY
;
3226 strcpy(f
->description
,"vbi data");
3230 /* video capture + overlay */
3232 for (i
= 0; i
< BTTV_FORMATS
; i
++) {
3233 if (bttv_formats
[i
].fourcc
!= -1)
3235 if ((unsigned int)index
== f
->index
)
3238 if (BTTV_FORMATS
== i
)
3242 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
3244 case V4L2_BUF_TYPE_VIDEO_OVERLAY
:
3245 if (!(bttv_formats
[i
].flags
& FORMAT_FLAGS_PACKED
))
3251 memset(f
,0,sizeof(*f
));
3254 f
->pixelformat
= bttv_formats
[i
].fourcc
;
3255 strlcpy(f
->description
,bttv_formats
[i
].name
,sizeof(f
->description
));
3259 case VIDIOC_TRY_FMT
:
3261 struct v4l2_format
*f
= arg
;
3262 return bttv_try_fmt(fh
,btv
,f
, /* adjust_crop */ 0);
3266 struct v4l2_format
*f
= arg
;
3267 return bttv_g_fmt(fh
,f
);
3271 struct v4l2_format
*f
= arg
;
3272 return bttv_s_fmt(fh
,btv
,f
);
3277 struct v4l2_framebuffer
*fb
= arg
;
3280 fb
->capability
= V4L2_FBUF_CAP_LIST_CLIPPING
;
3282 fb
->fmt
.pixelformat
= fh
->ovfmt
->fourcc
;
3287 struct v4l2_framebuffer
*fb
= arg
;
3288 const struct bttv_format
*fmt
;
3290 if(!capable(CAP_SYS_ADMIN
) &&
3291 !capable(CAP_SYS_RAWIO
))
3295 fmt
= format_by_fourcc(fb
->fmt
.pixelformat
);
3298 if (0 == (fmt
->flags
& FORMAT_FLAGS_PACKED
))
3302 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
3303 __s32 width
= fb
->fmt
.width
;
3304 __s32 height
= fb
->fmt
.height
;
3306 retval
= limit_scaled_size(fh
, &width
, &height
,
3307 V4L2_FIELD_INTERLACED
,
3308 /* width_mask */ ~3,
3310 /* adjust_size */ 0,
3311 /* adjust_crop */ 0);
3317 mutex_lock(&fh
->cap
.lock
);
3318 btv
->fbuf
.base
= fb
->base
;
3319 btv
->fbuf
.fmt
.width
= fb
->fmt
.width
;
3320 btv
->fbuf
.fmt
.height
= fb
->fmt
.height
;
3321 if (0 != fb
->fmt
.bytesperline
)
3322 btv
->fbuf
.fmt
.bytesperline
= fb
->fmt
.bytesperline
;
3324 btv
->fbuf
.fmt
.bytesperline
= btv
->fbuf
.fmt
.width
*fmt
->depth
/8;
3328 btv
->init
.ovfmt
= fmt
;
3329 if (fb
->flags
& V4L2_FBUF_FLAG_OVERLAY
) {
3332 fh
->ov
.w
.width
= fb
->fmt
.width
;
3333 fh
->ov
.w
.height
= fb
->fmt
.height
;
3334 btv
->init
.ov
.w
.width
= fb
->fmt
.width
;
3335 btv
->init
.ov
.w
.height
= fb
->fmt
.height
;
3336 kfree(fh
->ov
.clips
);
3337 fh
->ov
.clips
= NULL
;
3340 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
3341 struct bttv_buffer
*new;
3343 new = videobuf_pci_alloc(sizeof(*new));
3344 new->crop
= btv
->crop
[!!fh
->do_crop
].rect
;
3345 bttv_overlay_risc(btv
,&fh
->ov
,fh
->ovfmt
,new);
3346 retval
= bttv_switch_overlay(btv
,fh
,new);
3349 mutex_unlock(&fh
->cap
.lock
);
3353 case VIDIOC_REQBUFS
:
3354 return videobuf_reqbufs(bttv_queue(fh
),arg
);
3356 case VIDIOC_QUERYBUF
:
3357 return videobuf_querybuf(bttv_queue(fh
),arg
);
3361 int res
= bttv_resource(fh
);
3363 if (!check_alloc_btres(btv
, fh
, res
))
3365 return videobuf_qbuf(bttv_queue(fh
),arg
);
3369 return videobuf_dqbuf(bttv_queue(fh
),arg
,
3370 file
->f_flags
& O_NONBLOCK
);
3372 case VIDIOC_STREAMON
:
3374 int res
= bttv_resource(fh
);
3376 if (!check_alloc_btres(btv
,fh
,res
))
3378 return videobuf_streamon(bttv_queue(fh
));
3380 case VIDIOC_STREAMOFF
:
3382 int res
= bttv_resource(fh
);
3384 retval
= videobuf_streamoff(bttv_queue(fh
));
3387 free_btres(btv
,fh
,res
);
3391 case VIDIOC_QUERYCTRL
:
3393 struct v4l2_queryctrl
*c
= arg
;
3396 if ((c
->id
< V4L2_CID_BASE
||
3397 c
->id
>= V4L2_CID_LASTP1
) &&
3398 (c
->id
< V4L2_CID_PRIVATE_BASE
||
3399 c
->id
>= V4L2_CID_PRIVATE_LASTP1
))
3401 for (i
= 0; i
< BTTV_CTLS
; i
++)
3402 if (bttv_ctls
[i
].id
== c
->id
)
3404 if (i
== BTTV_CTLS
) {
3409 if (btv
->audio_hook
&& i
>= 4 && i
<= 8) {
3410 struct video_audio va
;
3411 memset(&va
,0,sizeof(va
));
3412 btv
->audio_hook(btv
,&va
,0);
3413 switch (bttv_ctls
[i
].id
) {
3414 case V4L2_CID_AUDIO_VOLUME
:
3415 if (!(va
.flags
& VIDEO_AUDIO_VOLUME
))
3418 case V4L2_CID_AUDIO_BALANCE
:
3419 if (!(va
.flags
& VIDEO_AUDIO_BALANCE
))
3422 case V4L2_CID_AUDIO_BASS
:
3423 if (!(va
.flags
& VIDEO_AUDIO_BASS
))
3426 case V4L2_CID_AUDIO_TREBLE
:
3427 if (!(va
.flags
& VIDEO_AUDIO_TREBLE
))
3435 return get_control(btv
,arg
);
3437 return set_control(btv
,arg
);
3440 struct v4l2_streamparm
*parm
= arg
;
3441 struct v4l2_standard s
;
3442 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
3444 memset(parm
,0,sizeof(*parm
));
3445 v4l2_video_std_construct(&s
, bttv_tvnorms
[btv
->tvnorm
].v4l2_id
,
3446 bttv_tvnorms
[btv
->tvnorm
].name
);
3447 parm
->parm
.capture
.timeperframe
= s
.frameperiod
;
3451 case VIDIOC_G_PRIORITY
:
3453 enum v4l2_priority
*p
= arg
;
3455 *p
= v4l2_prio_max(&btv
->prio
);
3458 case VIDIOC_S_PRIORITY
:
3460 enum v4l2_priority
*prio
= arg
;
3462 return v4l2_prio_change(&btv
->prio
, &fh
->prio
, *prio
);
3465 case VIDIOC_CROPCAP
:
3467 struct v4l2_cropcap
*cap
= arg
;
3468 enum v4l2_buf_type type
;
3472 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
3473 type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
3476 *cap
= bttv_tvnorms
[btv
->tvnorm
].cropcap
;
3483 struct v4l2_crop
* crop
= arg
;
3485 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
3486 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
3489 /* No fh->do_crop = 1; because btv->crop[1] may be
3490 inconsistent with fh->width or fh->height and apps
3491 do not expect a change here. */
3493 crop
->c
= btv
->crop
[!!fh
->do_crop
].rect
;
3499 struct v4l2_crop
*crop
= arg
;
3500 const struct v4l2_rect
*b
;
3507 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
3508 crop
->type
!= V4L2_BUF_TYPE_VIDEO_OVERLAY
)
3511 retval
= v4l2_prio_check(&btv
->prio
,&fh
->prio
);
3515 /* Make sure tvnorm, vbi_end and the current cropping
3516 parameters remain consistent until we're done. Note
3517 read() may change vbi_end in check_alloc_btres(). */
3518 mutex_lock(&btv
->lock
);
3522 if (locked_btres(fh
->btv
, VIDEO_RESOURCES
))
3523 goto btv_unlock_and_return
;
3525 b
= &bttv_tvnorms
[btv
->tvnorm
].cropcap
.bounds
;
3528 b_right
= b_left
+ b
->width
;
3529 b_bottom
= b
->top
+ b
->height
;
3531 b_top
= max(b
->top
, btv
->vbi_end
);
3532 if (b_top
+ 32 >= b_bottom
)
3533 goto btv_unlock_and_return
;
3535 /* Min. scaled size 48 x 32. */
3536 c
.rect
.left
= clamp(crop
->c
.left
, b_left
, b_right
- 48);
3537 c
.rect
.left
= min(c
.rect
.left
, (__s32
) MAX_HDELAY
);
3539 c
.rect
.width
= clamp(crop
->c
.width
,
3540 48, b_right
- c
.rect
.left
);
3542 c
.rect
.top
= clamp(crop
->c
.top
, b_top
, b_bottom
- 32);
3543 /* Top and height must be a multiple of two. */
3544 c
.rect
.top
= (c
.rect
.top
+ 1) & ~1;
3546 c
.rect
.height
= clamp(crop
->c
.height
,
3547 32, b_bottom
- c
.rect
.top
);
3548 c
.rect
.height
= (c
.rect
.height
+ 1) & ~1;
3550 bttv_crop_calc_limits(&c
);
3554 mutex_unlock(&btv
->lock
);
3558 mutex_lock(&fh
->cap
.lock
);
3560 if (fh
->width
< c
.min_scaled_width
) {
3561 fh
->width
= c
.min_scaled_width
;
3562 btv
->init
.width
= c
.min_scaled_width
;
3563 } else if (fh
->width
> c
.max_scaled_width
) {
3564 fh
->width
= c
.max_scaled_width
;
3565 btv
->init
.width
= c
.max_scaled_width
;
3568 if (fh
->height
< c
.min_scaled_height
) {
3569 fh
->height
= c
.min_scaled_height
;
3570 btv
->init
.height
= c
.min_scaled_height
;
3571 } else if (fh
->height
> c
.max_scaled_height
) {
3572 fh
->height
= c
.max_scaled_height
;
3573 btv
->init
.height
= c
.max_scaled_height
;
3576 mutex_unlock(&fh
->cap
.lock
);
3581 case VIDIOC_ENUMSTD
:
3584 case VIDIOC_ENUMINPUT
:
3585 case VIDIOC_G_INPUT
:
3586 case VIDIOC_S_INPUT
:
3587 case VIDIOC_G_TUNER
:
3588 case VIDIOC_S_TUNER
:
3589 case VIDIOC_G_FREQUENCY
:
3590 case VIDIOC_S_FREQUENCY
:
3591 case VIDIOC_LOG_STATUS
:
3592 case VIDIOC_DBG_G_REGISTER
:
3593 case VIDIOC_DBG_S_REGISTER
:
3594 return bttv_common_ioctls(btv
,cmd
,arg
);
3597 return -ENOIOCTLCMD
;
3601 fh_unlock_and_return
:
3602 mutex_unlock(&fh
->cap
.lock
);
3605 btv_unlock_and_return
:
3606 mutex_unlock(&btv
->lock
);
3610 static int bttv_ioctl(struct inode
*inode
, struct file
*file
,
3611 unsigned int cmd
, unsigned long arg
)
3613 struct bttv_fh
*fh
= file
->private_data
;
3618 const struct bttv_tvnorm
*tvnorm
;
3620 tvnorm
= fh
->vbi_fmt
.tvnorm
;
3622 if (fh
->vbi_fmt
.fmt
.start
[0] != tvnorm
->vbistart
[0] ||
3623 fh
->vbi_fmt
.fmt
.start
[1] != tvnorm
->vbistart
[1] ||
3624 fh
->vbi_fmt
.fmt
.count
[0] != fh
->vbi_fmt
.fmt
.count
[1]) {
3625 /* BTTV_VBISIZE cannot express these parameters,
3626 however open() resets the paramters to defaults
3627 and apps shouldn't call BTTV_VBISIZE after
3632 bttv_switch_type(fh
,V4L2_BUF_TYPE_VBI_CAPTURE
);
3633 return (fh
->vbi_fmt
.fmt
.count
[0] * 2
3634 * fh
->vbi_fmt
.fmt
.samples_per_line
);
3638 return video_usercopy(inode
, file
, cmd
, arg
, bttv_do_ioctl
);
3642 static ssize_t
bttv_read(struct file
*file
, char __user
*data
,
3643 size_t count
, loff_t
*ppos
)
3645 struct bttv_fh
*fh
= file
->private_data
;
3648 if (fh
->btv
->errors
)
3649 bttv_reinit_bt848(fh
->btv
);
3650 dprintk("bttv%d: read count=%d type=%s\n",
3651 fh
->btv
->c
.nr
,(int)count
,v4l2_type_names
[fh
->type
]);
3654 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
3655 if (!check_alloc_btres(fh
->btv
, fh
, RESOURCE_VIDEO_READ
)) {
3656 /* VIDEO_READ in use by another fh,
3657 or VIDEO_STREAM by any fh. */
3660 retval
= videobuf_read_one(&fh
->cap
, data
, count
, ppos
,
3661 file
->f_flags
& O_NONBLOCK
);
3662 free_btres(fh
->btv
, fh
, RESOURCE_VIDEO_READ
);
3664 case V4L2_BUF_TYPE_VBI_CAPTURE
:
3665 if (!check_alloc_btres(fh
->btv
,fh
,RESOURCE_VBI
))
3667 retval
= videobuf_read_stream(&fh
->vbi
, data
, count
, ppos
, 1,
3668 file
->f_flags
& O_NONBLOCK
);
3676 static unsigned int bttv_poll(struct file
*file
, poll_table
*wait
)
3678 struct bttv_fh
*fh
= file
->private_data
;
3679 struct bttv_buffer
*buf
;
3680 enum v4l2_field field
;
3682 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
3683 if (!check_alloc_btres(fh
->btv
,fh
,RESOURCE_VBI
))
3685 return videobuf_poll_stream(file
, &fh
->vbi
, wait
);
3688 if (check_btres(fh
,RESOURCE_VIDEO_STREAM
)) {
3689 /* streaming capture */
3690 if (list_empty(&fh
->cap
.stream
))
3692 buf
= list_entry(fh
->cap
.stream
.next
,struct bttv_buffer
,vb
.stream
);
3694 /* read() capture */
3695 mutex_lock(&fh
->cap
.lock
);
3696 if (NULL
== fh
->cap
.read_buf
) {
3697 /* need to capture a new frame */
3698 if (locked_btres(fh
->btv
,RESOURCE_VIDEO_STREAM
)) {
3699 mutex_unlock(&fh
->cap
.lock
);
3702 fh
->cap
.read_buf
= videobuf_pci_alloc(fh
->cap
.msize
);
3703 if (NULL
== fh
->cap
.read_buf
) {
3704 mutex_unlock(&fh
->cap
.lock
);
3707 fh
->cap
.read_buf
->memory
= V4L2_MEMORY_USERPTR
;
3708 field
= videobuf_next_field(&fh
->cap
);
3709 if (0 != fh
->cap
.ops
->buf_prepare(&fh
->cap
,fh
->cap
.read_buf
,field
)) {
3710 kfree (fh
->cap
.read_buf
);
3711 fh
->cap
.read_buf
= NULL
;
3712 mutex_unlock(&fh
->cap
.lock
);
3715 fh
->cap
.ops
->buf_queue(&fh
->cap
,fh
->cap
.read_buf
);
3716 fh
->cap
.read_off
= 0;
3718 mutex_unlock(&fh
->cap
.lock
);
3719 buf
= (struct bttv_buffer
*)fh
->cap
.read_buf
;
3722 poll_wait(file
, &buf
->vb
.done
, wait
);
3723 if (buf
->vb
.state
== STATE_DONE
||
3724 buf
->vb
.state
== STATE_ERROR
)
3725 return POLLIN
|POLLRDNORM
;
3729 static int bttv_open(struct inode
*inode
, struct file
*file
)
3731 int minor
= iminor(inode
);
3732 struct bttv
*btv
= NULL
;
3734 enum v4l2_buf_type type
= 0;
3737 dprintk(KERN_DEBUG
"bttv: open minor=%d\n",minor
);
3739 for (i
= 0; i
< bttv_num
; i
++) {
3740 if (bttvs
[i
].video_dev
&&
3741 bttvs
[i
].video_dev
->minor
== minor
) {
3743 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
3746 if (bttvs
[i
].vbi_dev
&&
3747 bttvs
[i
].vbi_dev
->minor
== minor
) {
3749 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
3756 dprintk(KERN_DEBUG
"bttv%d: open called (type=%s)\n",
3757 btv
->c
.nr
,v4l2_type_names
[type
]);
3759 /* allocate per filehandle data */
3760 fh
= kmalloc(sizeof(*fh
),GFP_KERNEL
);
3763 file
->private_data
= fh
;
3766 fh
->ov
.setup_ok
= 0;
3767 v4l2_prio_open(&btv
->prio
,&fh
->prio
);
3769 videobuf_queue_pci_init(&fh
->cap
, &bttv_video_qops
,
3770 btv
->c
.pci
, &btv
->s_lock
,
3771 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
3772 V4L2_FIELD_INTERLACED
,
3773 sizeof(struct bttv_buffer
),
3775 videobuf_queue_pci_init(&fh
->vbi
, &bttv_vbi_qops
,
3776 btv
->c
.pci
, &btv
->s_lock
,
3777 V4L2_BUF_TYPE_VBI_CAPTURE
,
3779 sizeof(struct bttv_buffer
),
3781 i2c_vidiocschan(btv
);
3785 /* The V4L2 spec requires one global set of cropping parameters
3786 which only change on request. These are stored in btv->crop[1].
3787 However for compatibility with V4L apps and cropping unaware
3788 V4L2 apps we now reset the cropping parameters as seen through
3789 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3790 will use btv->crop[0], the default cropping parameters for the
3791 current video standard, and VIDIOC_S_FMT will not implicitely
3792 change the cropping parameters until VIDIOC_S_CROP has been
3794 fh
->do_crop
= !reset_crop
; /* module parameter */
3796 /* Likewise there should be one global set of VBI capture
3797 parameters, but for compatibility with V4L apps and earlier
3798 driver versions each fh has its own parameters. */
3799 bttv_vbi_fmt_reset(&fh
->vbi_fmt
, btv
->tvnorm
);
3801 bttv_field_count(btv
);
3805 static int bttv_release(struct inode
*inode
, struct file
*file
)
3807 struct bttv_fh
*fh
= file
->private_data
;
3808 struct bttv
*btv
= fh
->btv
;
3810 /* turn off overlay */
3811 if (check_btres(fh
, RESOURCE_OVERLAY
))
3812 bttv_switch_overlay(btv
,fh
,NULL
);
3814 /* stop video capture */
3815 if (check_btres(fh
, RESOURCE_VIDEO_STREAM
)) {
3816 videobuf_streamoff(&fh
->cap
);
3817 free_btres(btv
,fh
,RESOURCE_VIDEO_STREAM
);
3819 if (fh
->cap
.read_buf
) {
3820 buffer_release(&fh
->cap
,fh
->cap
.read_buf
);
3821 kfree(fh
->cap
.read_buf
);
3823 if (check_btres(fh
, RESOURCE_VIDEO_READ
)) {
3824 free_btres(btv
, fh
, RESOURCE_VIDEO_READ
);
3827 /* stop vbi capture */
3828 if (check_btres(fh
, RESOURCE_VBI
)) {
3829 if (fh
->vbi
.streaming
)
3830 videobuf_streamoff(&fh
->vbi
);
3831 if (fh
->vbi
.reading
)
3832 videobuf_read_stop(&fh
->vbi
);
3833 free_btres(btv
,fh
,RESOURCE_VBI
);
3837 videobuf_mmap_free(&fh
->cap
);
3838 videobuf_mmap_free(&fh
->vbi
);
3839 v4l2_prio_close(&btv
->prio
,&fh
->prio
);
3840 file
->private_data
= NULL
;
3844 bttv_field_count(btv
);
3849 bttv_mmap(struct file
*file
, struct vm_area_struct
*vma
)
3851 struct bttv_fh
*fh
= file
->private_data
;
3853 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3854 fh
->btv
->c
.nr
, v4l2_type_names
[fh
->type
],
3855 vma
->vm_start
, vma
->vm_end
- vma
->vm_start
);
3856 return videobuf_mmap_mapper(bttv_queue(fh
),vma
);
3859 static const struct file_operations bttv_fops
=
3861 .owner
= THIS_MODULE
,
3863 .release
= bttv_release
,
3864 .ioctl
= bttv_ioctl
,
3865 .compat_ioctl
= v4l_compat_ioctl32
,
3866 .llseek
= no_llseek
,
3872 static struct video_device bttv_video_template
=
3875 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|
3876 VID_TYPE_CLIPPING
|VID_TYPE_SCALES
,
3877 .hardware
= VID_HARDWARE_BT848
,
3882 static struct video_device bttv_vbi_template
=
3884 .name
= "bt848/878 vbi",
3885 .type
= VID_TYPE_TUNER
|VID_TYPE_TELETEXT
,
3886 .hardware
= VID_HARDWARE_BT848
,
3891 /* ----------------------------------------------------------------------- */
3892 /* radio interface */
3894 static int radio_open(struct inode
*inode
, struct file
*file
)
3896 int minor
= iminor(inode
);
3897 struct bttv
*btv
= NULL
;
3900 dprintk("bttv: open minor=%d\n",minor
);
3902 for (i
= 0; i
< bttv_num
; i
++) {
3903 if (bttvs
[i
].radio_dev
->minor
== minor
) {
3911 dprintk("bttv%d: open called (radio)\n",btv
->c
.nr
);
3912 mutex_lock(&btv
->lock
);
3916 file
->private_data
= btv
;
3918 bttv_call_i2c_clients(btv
,AUDC_SET_RADIO
,NULL
);
3919 audio_input(btv
,TVAUDIO_INPUT_RADIO
);
3921 mutex_unlock(&btv
->lock
);
3925 static int radio_release(struct inode
*inode
, struct file
*file
)
3927 struct bttv
*btv
= file
->private_data
;
3928 struct rds_command cmd
;
3932 bttv_call_i2c_clients(btv
, RDS_CMD_CLOSE
, &cmd
);
3937 static int radio_do_ioctl(struct inode
*inode
, struct file
*file
,
3938 unsigned int cmd
, void *arg
)
3940 struct bttv
*btv
= file
->private_data
;
3945 struct video_capability
*cap
= arg
;
3947 memset(cap
,0,sizeof(*cap
));
3948 strcpy(cap
->name
,btv
->radio_dev
->name
);
3949 cap
->type
= VID_TYPE_TUNER
;
3957 struct video_tuner
*v
= arg
;
3961 memset(v
,0,sizeof(*v
));
3962 strcpy(v
->name
, "Radio");
3963 bttv_call_i2c_clients(btv
,cmd
,v
);
3975 case VIDIOC_LOG_STATUS
:
3976 case VIDIOC_DBG_G_REGISTER
:
3977 case VIDIOC_DBG_S_REGISTER
:
3978 return bttv_common_ioctls(btv
,cmd
,arg
);
3981 return -ENOIOCTLCMD
;
3986 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
3987 unsigned int cmd
, unsigned long arg
)
3989 return video_usercopy(inode
, file
, cmd
, arg
, radio_do_ioctl
);
3992 static ssize_t
radio_read(struct file
*file
, char __user
*data
,
3993 size_t count
, loff_t
*ppos
)
3995 struct bttv
*btv
= file
->private_data
;
3996 struct rds_command cmd
;
3997 cmd
.block_count
= count
/3;
3999 cmd
.instance
= file
;
4000 cmd
.result
= -ENODEV
;
4002 bttv_call_i2c_clients(btv
, RDS_CMD_READ
, &cmd
);
4007 static unsigned int radio_poll(struct file
*file
, poll_table
*wait
)
4009 struct bttv
*btv
= file
->private_data
;
4010 struct rds_command cmd
;
4011 cmd
.instance
= file
;
4012 cmd
.event_list
= wait
;
4013 cmd
.result
= -ENODEV
;
4014 bttv_call_i2c_clients(btv
, RDS_CMD_POLL
, &cmd
);
4019 static const struct file_operations radio_fops
=
4021 .owner
= THIS_MODULE
,
4024 .release
= radio_release
,
4025 .ioctl
= radio_ioctl
,
4026 .llseek
= no_llseek
,
4030 static struct video_device radio_template
=
4032 .name
= "bt848/878 radio",
4033 .type
= VID_TYPE_TUNER
,
4034 .hardware
= VID_HARDWARE_BT848
,
4035 .fops
= &radio_fops
,
4039 /* ----------------------------------------------------------------------- */
4040 /* some debug code */
4042 static int bttv_risc_decode(u32 risc
)
4044 static char *instr
[16] = {
4045 [ BT848_RISC_WRITE
>> 28 ] = "write",
4046 [ BT848_RISC_SKIP
>> 28 ] = "skip",
4047 [ BT848_RISC_WRITEC
>> 28 ] = "writec",
4048 [ BT848_RISC_JUMP
>> 28 ] = "jump",
4049 [ BT848_RISC_SYNC
>> 28 ] = "sync",
4050 [ BT848_RISC_WRITE123
>> 28 ] = "write123",
4051 [ BT848_RISC_SKIP123
>> 28 ] = "skip123",
4052 [ BT848_RISC_WRITE1S23
>> 28 ] = "write1s23",
4054 static int incr
[16] = {
4055 [ BT848_RISC_WRITE
>> 28 ] = 2,
4056 [ BT848_RISC_JUMP
>> 28 ] = 2,
4057 [ BT848_RISC_SYNC
>> 28 ] = 2,
4058 [ BT848_RISC_WRITE123
>> 28 ] = 5,
4059 [ BT848_RISC_SKIP123
>> 28 ] = 2,
4060 [ BT848_RISC_WRITE1S23
>> 28 ] = 3,
4062 static char *bits
[] = {
4063 "be0", "be1", "be2", "be3/resync",
4064 "set0", "set1", "set2", "set3",
4065 "clr0", "clr1", "clr2", "clr3",
4066 "irq", "res", "eol", "sol",
4070 printk("0x%08x [ %s", risc
,
4071 instr
[risc
>> 28] ? instr
[risc
>> 28] : "INVALID");
4072 for (i
= ARRAY_SIZE(bits
)-1; i
>= 0; i
--)
4073 if (risc
& (1 << (i
+ 12)))
4074 printk(" %s",bits
[i
]);
4075 printk(" count=%d ]\n", risc
& 0xfff);
4076 return incr
[risc
>> 28] ? incr
[risc
>> 28] : 1;
4079 static void bttv_risc_disasm(struct bttv
*btv
,
4080 struct btcx_riscmem
*risc
)
4084 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
4085 btv
->c
.name
, risc
->cpu
, (unsigned long)risc
->dma
);
4086 for (i
= 0; i
< (risc
->size
>> 2); i
+= n
) {
4087 printk("%s: 0x%lx: ", btv
->c
.name
,
4088 (unsigned long)(risc
->dma
+ (i
<<2)));
4089 n
= bttv_risc_decode(risc
->cpu
[i
]);
4090 for (j
= 1; j
< n
; j
++)
4091 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
4092 btv
->c
.name
, (unsigned long)(risc
->dma
+ ((i
+j
)<<2)),
4094 if (0 == risc
->cpu
[i
])
4099 static void bttv_print_riscaddr(struct bttv
*btv
)
4101 printk(" main: %08Lx\n",
4102 (unsigned long long)btv
->main
.dma
);
4103 printk(" vbi : o=%08Lx e=%08Lx\n",
4104 btv
->cvbi
? (unsigned long long)btv
->cvbi
->top
.dma
: 0,
4105 btv
->cvbi
? (unsigned long long)btv
->cvbi
->bottom
.dma
: 0);
4106 printk(" cap : o=%08Lx e=%08Lx\n",
4107 btv
->curr
.top
? (unsigned long long)btv
->curr
.top
->top
.dma
: 0,
4108 btv
->curr
.bottom
? (unsigned long long)btv
->curr
.bottom
->bottom
.dma
: 0);
4109 printk(" scr : o=%08Lx e=%08Lx\n",
4110 btv
->screen
? (unsigned long long)btv
->screen
->top
.dma
: 0,
4111 btv
->screen
? (unsigned long long)btv
->screen
->bottom
.dma
: 0);
4112 bttv_risc_disasm(btv
, &btv
->main
);
4115 /* ----------------------------------------------------------------------- */
4118 static char *irq_name
[] = {
4119 "FMTCHG", // format change detected (525 vs. 625)
4120 "VSYNC", // vertical sync (new field)
4121 "HSYNC", // horizontal sync
4122 "OFLOW", // chroma/luma AGC overflow
4123 "HLOCK", // horizontal lock changed
4124 "VPRES", // video presence changed
4126 "I2CDONE", // hw irc operation finished
4127 "GPINT", // gpio port triggered irq
4129 "RISCI", // risc instruction triggered irq
4130 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
4131 "FTRGT", // pixel data fifo overrun
4132 "FDSR", // fifo data stream resyncronisation
4133 "PPERR", // parity error (data transfer)
4134 "RIPERR", // parity error (read risc instructions)
4135 "PABORT", // pci abort
4136 "OCERR", // risc instruction error
4137 "SCERR", // syncronisation error
4140 static void bttv_print_irqbits(u32 print
, u32 mark
)
4145 for (i
= 0; i
< ARRAY_SIZE(irq_name
); i
++) {
4146 if (print
& (1 << i
))
4147 printk(" %s",irq_name
[i
]);
4148 if (mark
& (1 << i
))
4153 static void bttv_irq_debug_low_latency(struct bttv
*btv
, u32 rc
)
4155 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
4157 (unsigned long)btv
->main
.dma
,
4158 (unsigned long)btv
->main
.cpu
[RISC_SLOT_O_VBI
+1],
4159 (unsigned long)btv
->main
.cpu
[RISC_SLOT_O_FIELD
+1],
4162 if (0 == (btread(BT848_DSTATUS
) & BT848_DSTATUS_HLOC
)) {
4163 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
4164 "Ok, then this is harmless, don't worry ;)\n",
4168 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
4170 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
4176 bttv_irq_next_video(struct bttv
*btv
, struct bttv_buffer_set
*set
)
4178 struct bttv_buffer
*item
;
4180 memset(set
,0,sizeof(*set
));
4182 /* capture request ? */
4183 if (!list_empty(&btv
->capture
)) {
4185 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
4186 if (V4L2_FIELD_HAS_TOP(item
->vb
.field
))
4188 if (V4L2_FIELD_HAS_BOTTOM(item
->vb
.field
))
4191 /* capture request for other field ? */
4192 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
) &&
4193 (item
->vb
.queue
.next
!= &btv
->capture
)) {
4194 item
= list_entry(item
->vb
.queue
.next
, struct bttv_buffer
, vb
.queue
);
4195 if (!V4L2_FIELD_HAS_BOTH(item
->vb
.field
)) {
4196 if (NULL
== set
->top
&&
4197 V4L2_FIELD_TOP
== item
->vb
.field
) {
4200 if (NULL
== set
->bottom
&&
4201 V4L2_FIELD_BOTTOM
== item
->vb
.field
) {
4204 if (NULL
!= set
->top
&& NULL
!= set
->bottom
)
4210 /* screen overlay ? */
4211 if (NULL
!= btv
->screen
) {
4212 if (V4L2_FIELD_HAS_BOTH(btv
->screen
->vb
.field
)) {
4213 if (NULL
== set
->top
&& NULL
== set
->bottom
) {
4214 set
->top
= btv
->screen
;
4215 set
->bottom
= btv
->screen
;
4218 if (V4L2_FIELD_TOP
== btv
->screen
->vb
.field
&&
4220 set
->top
= btv
->screen
;
4222 if (V4L2_FIELD_BOTTOM
== btv
->screen
->vb
.field
&&
4223 NULL
== set
->bottom
) {
4224 set
->bottom
= btv
->screen
;
4229 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
4230 btv
->c
.nr
,set
->top
, set
->bottom
,
4231 btv
->screen
,set
->frame_irq
,set
->top_irq
);
4236 bttv_irq_wakeup_video(struct bttv
*btv
, struct bttv_buffer_set
*wakeup
,
4237 struct bttv_buffer_set
*curr
, unsigned int state
)
4241 do_gettimeofday(&ts
);
4243 if (wakeup
->top
== wakeup
->bottom
) {
4244 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
4246 printk("bttv%d: wakeup: both=%p\n",btv
->c
.nr
,wakeup
->top
);
4247 wakeup
->top
->vb
.ts
= ts
;
4248 wakeup
->top
->vb
.field_count
= btv
->field_count
;
4249 wakeup
->top
->vb
.state
= state
;
4250 wake_up(&wakeup
->top
->vb
.done
);
4253 if (NULL
!= wakeup
->top
&& curr
->top
!= wakeup
->top
) {
4255 printk("bttv%d: wakeup: top=%p\n",btv
->c
.nr
,wakeup
->top
);
4256 wakeup
->top
->vb
.ts
= ts
;
4257 wakeup
->top
->vb
.field_count
= btv
->field_count
;
4258 wakeup
->top
->vb
.state
= state
;
4259 wake_up(&wakeup
->top
->vb
.done
);
4261 if (NULL
!= wakeup
->bottom
&& curr
->bottom
!= wakeup
->bottom
) {
4263 printk("bttv%d: wakeup: bottom=%p\n",btv
->c
.nr
,wakeup
->bottom
);
4264 wakeup
->bottom
->vb
.ts
= ts
;
4265 wakeup
->bottom
->vb
.field_count
= btv
->field_count
;
4266 wakeup
->bottom
->vb
.state
= state
;
4267 wake_up(&wakeup
->bottom
->vb
.done
);
4273 bttv_irq_wakeup_vbi(struct bttv
*btv
, struct bttv_buffer
*wakeup
,
4281 do_gettimeofday(&ts
);
4283 wakeup
->vb
.field_count
= btv
->field_count
;
4284 wakeup
->vb
.state
= state
;
4285 wake_up(&wakeup
->vb
.done
);
4288 static void bttv_irq_timeout(unsigned long data
)
4290 struct bttv
*btv
= (struct bttv
*)data
;
4291 struct bttv_buffer_set old
,new;
4292 struct bttv_buffer
*ovbi
;
4293 struct bttv_buffer
*item
;
4294 unsigned long flags
;
4297 printk(KERN_INFO
"bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
4298 btv
->c
.nr
, btv
->framedrop
, btv
->irq_me
, btv
->irq_total
,
4299 btread(BT848_RISC_COUNT
));
4300 bttv_print_irqbits(btread(BT848_INT_STAT
),0);
4304 spin_lock_irqsave(&btv
->s_lock
,flags
);
4306 /* deactivate stuff */
4307 memset(&new,0,sizeof(new));
4313 bttv_buffer_activate_video(btv
, &new);
4314 bttv_buffer_activate_vbi(btv
, NULL
);
4315 bttv_set_dma(btv
, 0);
4318 bttv_irq_wakeup_video(btv
, &old
, &new, STATE_ERROR
);
4319 bttv_irq_wakeup_vbi(btv
, ovbi
, STATE_ERROR
);
4321 /* cancel all outstanding capture / vbi requests */
4322 while (!list_empty(&btv
->capture
)) {
4323 item
= list_entry(btv
->capture
.next
, struct bttv_buffer
, vb
.queue
);
4324 list_del(&item
->vb
.queue
);
4325 item
->vb
.state
= STATE_ERROR
;
4326 wake_up(&item
->vb
.done
);
4328 while (!list_empty(&btv
->vcapture
)) {
4329 item
= list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
4330 list_del(&item
->vb
.queue
);
4331 item
->vb
.state
= STATE_ERROR
;
4332 wake_up(&item
->vb
.done
);
4336 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4340 bttv_irq_wakeup_top(struct bttv
*btv
)
4342 struct bttv_buffer
*wakeup
= btv
->curr
.top
;
4347 spin_lock(&btv
->s_lock
);
4348 btv
->curr
.top_irq
= 0;
4349 btv
->curr
.top
= NULL
;
4350 bttv_risc_hook(btv
, RISC_SLOT_O_FIELD
, NULL
, 0);
4352 do_gettimeofday(&wakeup
->vb
.ts
);
4353 wakeup
->vb
.field_count
= btv
->field_count
;
4354 wakeup
->vb
.state
= STATE_DONE
;
4355 wake_up(&wakeup
->vb
.done
);
4356 spin_unlock(&btv
->s_lock
);
4359 static inline int is_active(struct btcx_riscmem
*risc
, u32 rc
)
4363 if (rc
> risc
->dma
+ risc
->size
)
4369 bttv_irq_switch_video(struct bttv
*btv
)
4371 struct bttv_buffer_set
new;
4372 struct bttv_buffer_set old
;
4375 spin_lock(&btv
->s_lock
);
4377 /* new buffer set */
4378 bttv_irq_next_video(btv
, &new);
4379 rc
= btread(BT848_RISC_COUNT
);
4380 if ((btv
->curr
.top
&& is_active(&btv
->curr
.top
->top
, rc
)) ||
4381 (btv
->curr
.bottom
&& is_active(&btv
->curr
.bottom
->bottom
, rc
))) {
4384 bttv_irq_debug_low_latency(btv
, rc
);
4385 spin_unlock(&btv
->s_lock
);
4392 btv
->loop_irq
&= ~1;
4393 bttv_buffer_activate_video(btv
, &new);
4394 bttv_set_dma(btv
, 0);
4397 if (UNSET
!= btv
->new_input
) {
4398 video_mux(btv
,btv
->new_input
);
4399 btv
->new_input
= UNSET
;
4402 /* wake up finished buffers */
4403 bttv_irq_wakeup_video(btv
, &old
, &new, STATE_DONE
);
4404 spin_unlock(&btv
->s_lock
);
4408 bttv_irq_switch_vbi(struct bttv
*btv
)
4410 struct bttv_buffer
*new = NULL
;
4411 struct bttv_buffer
*old
;
4414 spin_lock(&btv
->s_lock
);
4416 if (!list_empty(&btv
->vcapture
))
4417 new = list_entry(btv
->vcapture
.next
, struct bttv_buffer
, vb
.queue
);
4420 rc
= btread(BT848_RISC_COUNT
);
4421 if (NULL
!= old
&& (is_active(&old
->top
, rc
) ||
4422 is_active(&old
->bottom
, rc
))) {
4425 bttv_irq_debug_low_latency(btv
, rc
);
4426 spin_unlock(&btv
->s_lock
);
4432 btv
->loop_irq
&= ~4;
4433 bttv_buffer_activate_vbi(btv
, new);
4434 bttv_set_dma(btv
, 0);
4436 bttv_irq_wakeup_vbi(btv
, old
, STATE_DONE
);
4437 spin_unlock(&btv
->s_lock
);
4440 static irqreturn_t
bttv_irq(int irq
, void *dev_id
)
4448 btv
=(struct bttv
*)dev_id
;
4450 if (btv
->custom_irq
)
4451 handled
= btv
->custom_irq(btv
);
4455 /* get/clear interrupt status bits */
4456 stat
=btread(BT848_INT_STAT
);
4457 astat
=stat
&btread(BT848_INT_MASK
);
4461 btwrite(stat
,BT848_INT_STAT
);
4463 /* get device status bits */
4464 dstat
=btread(BT848_DSTATUS
);
4467 printk(KERN_DEBUG
"bttv%d: irq loop=%d fc=%d "
4468 "riscs=%x, riscc=%08x, ",
4469 btv
->c
.nr
, count
, btv
->field_count
,
4470 stat
>>28, btread(BT848_RISC_COUNT
));
4471 bttv_print_irqbits(stat
,astat
);
4472 if (stat
& BT848_INT_HLOCK
)
4473 printk(" HLOC => %s", (dstat
& BT848_DSTATUS_HLOC
)
4475 if (stat
& BT848_INT_VPRES
)
4476 printk(" PRES => %s", (dstat
& BT848_DSTATUS_PRES
)
4478 if (stat
& BT848_INT_FMTCHG
)
4479 printk(" NUML => %s", (dstat
& BT848_DSTATUS_NUML
)
4484 if (astat
&BT848_INT_VSYNC
)
4487 if ((astat
& BT848_INT_GPINT
) && btv
->remote
) {
4488 wake_up(&btv
->gpioq
);
4489 bttv_input_irq(btv
);
4492 if (astat
& BT848_INT_I2CDONE
) {
4493 btv
->i2c_done
= stat
;
4494 wake_up(&btv
->i2c_queue
);
4497 if ((astat
& BT848_INT_RISCI
) && (stat
& (4<<28)))
4498 bttv_irq_switch_vbi(btv
);
4500 if ((astat
& BT848_INT_RISCI
) && (stat
& (2<<28)))
4501 bttv_irq_wakeup_top(btv
);
4503 if ((astat
& BT848_INT_RISCI
) && (stat
& (1<<28)))
4504 bttv_irq_switch_video(btv
);
4506 if ((astat
& BT848_INT_HLOCK
) && btv
->opt_automute
)
4507 audio_mute(btv
, btv
->mute
); /* trigger automute */
4509 if (astat
& (BT848_INT_SCERR
|BT848_INT_OCERR
)) {
4510 printk(KERN_INFO
"bttv%d: %s%s @ %08x,",btv
->c
.nr
,
4511 (astat
& BT848_INT_SCERR
) ? "SCERR" : "",
4512 (astat
& BT848_INT_OCERR
) ? "OCERR" : "",
4513 btread(BT848_RISC_COUNT
));
4514 bttv_print_irqbits(stat
,astat
);
4517 bttv_print_riscaddr(btv
);
4519 if (fdsr
&& astat
& BT848_INT_FDSR
) {
4520 printk(KERN_INFO
"bttv%d: FDSR @ %08x\n",
4521 btv
->c
.nr
,btread(BT848_RISC_COUNT
));
4523 bttv_print_riscaddr(btv
);
4529 if (count
> 8 || !(astat
& BT848_INT_GPINT
)) {
4530 btwrite(0, BT848_INT_MASK
);
4533 "bttv%d: IRQ lockup, cleared int mask [", btv
->c
.nr
);
4536 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv
->c
.nr
);
4538 btwrite(btread(BT848_INT_MASK
) & (-1 ^ BT848_INT_GPINT
),
4542 bttv_print_irqbits(stat
,astat
);
4550 return IRQ_RETVAL(handled
);
4554 /* ----------------------------------------------------------------------- */
4555 /* initialitation */
4557 static struct video_device
*vdev_init(struct bttv
*btv
,
4558 struct video_device
*template,
4561 struct video_device
*vfd
;
4563 vfd
= video_device_alloc();
4568 vfd
->dev
= &btv
->c
.pci
->dev
;
4569 vfd
->release
= video_device_release
;
4570 snprintf(vfd
->name
, sizeof(vfd
->name
), "BT%d%s %s (%s)",
4571 btv
->id
, (btv
->id
==848 && btv
->revision
==0x12) ? "A" : "",
4572 type
, bttv_tvcards
[btv
->c
.type
].name
);
4576 static void bttv_unregister_video(struct bttv
*btv
)
4578 if (btv
->video_dev
) {
4579 if (-1 != btv
->video_dev
->minor
)
4580 video_unregister_device(btv
->video_dev
);
4582 video_device_release(btv
->video_dev
);
4583 btv
->video_dev
= NULL
;
4586 if (-1 != btv
->vbi_dev
->minor
)
4587 video_unregister_device(btv
->vbi_dev
);
4589 video_device_release(btv
->vbi_dev
);
4590 btv
->vbi_dev
= NULL
;
4592 if (btv
->radio_dev
) {
4593 if (-1 != btv
->radio_dev
->minor
)
4594 video_unregister_device(btv
->radio_dev
);
4596 video_device_release(btv
->radio_dev
);
4597 btv
->radio_dev
= NULL
;
4601 /* register video4linux devices */
4602 static int __devinit
bttv_register_video(struct bttv
*btv
)
4604 if (no_overlay
<= 0) {
4605 bttv_video_template
.type
|= VID_TYPE_OVERLAY
;
4607 printk("bttv: Overlay support disabled.\n");
4611 btv
->video_dev
= vdev_init(btv
, &bttv_video_template
, "video");
4612 if (NULL
== btv
->video_dev
)
4614 if (video_register_device(btv
->video_dev
,VFL_TYPE_GRABBER
,video_nr
)<0)
4616 printk(KERN_INFO
"bttv%d: registered device video%d\n",
4617 btv
->c
.nr
,btv
->video_dev
->minor
& 0x1f);
4618 if (class_device_create_file(&btv
->video_dev
->class_dev
,
4619 &class_device_attr_card
)<0) {
4620 printk(KERN_ERR
"bttv%d: class_device_create_file 'card' "
4621 "failed\n", btv
->c
.nr
);
4626 btv
->vbi_dev
= vdev_init(btv
, &bttv_vbi_template
, "vbi");
4627 if (NULL
== btv
->vbi_dev
)
4629 if (video_register_device(btv
->vbi_dev
,VFL_TYPE_VBI
,vbi_nr
)<0)
4631 printk(KERN_INFO
"bttv%d: registered device vbi%d\n",
4632 btv
->c
.nr
,btv
->vbi_dev
->minor
& 0x1f);
4634 if (!btv
->has_radio
)
4637 btv
->radio_dev
= vdev_init(btv
, &radio_template
, "radio");
4638 if (NULL
== btv
->radio_dev
)
4640 if (video_register_device(btv
->radio_dev
, VFL_TYPE_RADIO
,radio_nr
)<0)
4642 printk(KERN_INFO
"bttv%d: registered device radio%d\n",
4643 btv
->c
.nr
,btv
->radio_dev
->minor
& 0x1f);
4649 bttv_unregister_video(btv
);
4654 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4655 /* response on cards with no firmware is not enabled by OF */
4656 static void pci_set_command(struct pci_dev
*dev
)
4658 #if defined(__powerpc__)
4661 pci_read_config_dword(dev
, PCI_COMMAND
, &cmd
);
4662 cmd
= (cmd
| PCI_COMMAND_MEMORY
);
4663 pci_write_config_dword(dev
, PCI_COMMAND
, cmd
);
4667 static int __devinit
bttv_probe(struct pci_dev
*dev
,
4668 const struct pci_device_id
*pci_id
)
4674 if (bttv_num
== BTTV_MAX
)
4676 printk(KERN_INFO
"bttv: Bt8xx card found (%d).\n", bttv_num
);
4677 btv
=&bttvs
[bttv_num
];
4678 memset(btv
,0,sizeof(*btv
));
4679 btv
->c
.nr
= bttv_num
;
4680 sprintf(btv
->c
.name
,"bttv%d",btv
->c
.nr
);
4682 /* initialize structs / fill in defaults */
4683 mutex_init(&btv
->lock
);
4684 spin_lock_init(&btv
->s_lock
);
4685 spin_lock_init(&btv
->gpio_lock
);
4686 init_waitqueue_head(&btv
->gpioq
);
4687 init_waitqueue_head(&btv
->i2c_queue
);
4688 INIT_LIST_HEAD(&btv
->c
.subs
);
4689 INIT_LIST_HEAD(&btv
->capture
);
4690 INIT_LIST_HEAD(&btv
->vcapture
);
4691 v4l2_prio_init(&btv
->prio
);
4693 init_timer(&btv
->timeout
);
4694 btv
->timeout
.function
= bttv_irq_timeout
;
4695 btv
->timeout
.data
= (unsigned long)btv
;
4698 btv
->tuner_type
= UNSET
;
4699 btv
->new_input
= UNSET
;
4700 btv
->has_radio
=radio
[btv
->c
.nr
];
4702 /* pci stuff (init, get irq/mmio, ... */
4704 btv
->id
= dev
->device
;
4705 if (pci_enable_device(dev
)) {
4706 printk(KERN_WARNING
"bttv%d: Can't enable device.\n",
4710 if (pci_set_dma_mask(dev
, DMA_32BIT_MASK
)) {
4711 printk(KERN_WARNING
"bttv%d: No suitable DMA available.\n",
4715 if (!request_mem_region(pci_resource_start(dev
,0),
4716 pci_resource_len(dev
,0),
4718 printk(KERN_WARNING
"bttv%d: can't request iomem (0x%llx).\n",
4720 (unsigned long long)pci_resource_start(dev
,0));
4723 pci_set_master(dev
);
4724 pci_set_command(dev
);
4725 pci_set_drvdata(dev
,btv
);
4727 pci_read_config_byte(dev
, PCI_CLASS_REVISION
, &btv
->revision
);
4728 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
4729 printk(KERN_INFO
"bttv%d: Bt%d (rev %d) at %s, ",
4730 bttv_num
,btv
->id
, btv
->revision
, pci_name(dev
));
4731 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4732 btv
->c
.pci
->irq
, lat
,
4733 (unsigned long long)pci_resource_start(dev
,0));
4736 btv
->bt848_mmio
= ioremap(pci_resource_start(dev
, 0), 0x1000);
4737 if (NULL
== btv
->bt848_mmio
) {
4738 printk("bttv%d: ioremap() failed\n", btv
->c
.nr
);
4746 /* disable irqs, register irq handler */
4747 btwrite(0, BT848_INT_MASK
);
4748 result
= request_irq(btv
->c
.pci
->irq
, bttv_irq
,
4749 IRQF_SHARED
| IRQF_DISABLED
,btv
->c
.name
,(void *)btv
);
4751 printk(KERN_ERR
"bttv%d: can't get IRQ %d\n",
4752 bttv_num
,btv
->c
.pci
->irq
);
4756 if (0 != bttv_handle_chipset(btv
)) {
4761 /* init options from insmod args */
4762 btv
->opt_combfilter
= combfilter
;
4763 btv
->opt_lumafilter
= lumafilter
;
4764 btv
->opt_automute
= automute
;
4765 btv
->opt_chroma_agc
= chroma_agc
;
4766 btv
->opt_adc_crush
= adc_crush
;
4767 btv
->opt_vcr_hack
= vcr_hack
;
4768 btv
->opt_whitecrush_upper
= whitecrush_upper
;
4769 btv
->opt_whitecrush_lower
= whitecrush_lower
;
4770 btv
->opt_uv_ratio
= uv_ratio
;
4771 btv
->opt_full_luma_range
= full_luma_range
;
4772 btv
->opt_coring
= coring
;
4774 /* fill struct bttv with some useful defaults */
4775 btv
->init
.btv
= btv
;
4776 btv
->init
.ov
.w
.width
= 320;
4777 btv
->init
.ov
.w
.height
= 240;
4778 btv
->init
.fmt
= format_by_palette(VIDEO_PALETTE_RGB24
);
4779 btv
->init
.width
= 320;
4780 btv
->init
.height
= 240;
4783 /* initialize hardware */
4785 bttv_gpio_tracking(btv
,"pre-init");
4787 bttv_risc_init_main(btv
);
4791 btwrite(0x00, BT848_GPIO_REG_INP
);
4792 btwrite(0x00, BT848_GPIO_OUT_EN
);
4794 bttv_gpio_tracking(btv
,"init");
4796 /* needs to be done before i2c is registered */
4797 bttv_init_card1(btv
);
4799 /* register i2c + gpio */
4802 /* some card-specific stuff (needs working i2c) */
4803 bttv_init_card2(btv
);
4806 /* register video4linux + input */
4807 if (!bttv_tvcards
[btv
->c
.type
].no_video
) {
4808 bttv_register_video(btv
);
4809 bt848_bright(btv
,32768);
4810 bt848_contrast(btv
,32768);
4811 bt848_hue(btv
,32768);
4812 bt848_sat(btv
,32768);
4814 set_input(btv
, 0, btv
->tvnorm
);
4815 bttv_crop_reset(&btv
->crop
[0], btv
->tvnorm
);
4816 btv
->crop
[1] = btv
->crop
[0]; /* current = default */
4817 disclaim_vbi_lines(btv
);
4818 disclaim_video_lines(btv
);
4821 /* add subdevices and autoload dvb-bt8xx if needed */
4822 if (bttv_tvcards
[btv
->c
.type
].has_dvb
) {
4823 bttv_sub_add_device(&btv
->c
, "dvb");
4824 request_modules(btv
);
4827 bttv_input_init(btv
);
4829 /* everything is fine */
4834 free_irq(btv
->c
.pci
->irq
,btv
);
4837 if (btv
->bt848_mmio
)
4838 iounmap(btv
->bt848_mmio
);
4839 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4840 pci_resource_len(btv
->c
.pci
,0));
4841 pci_set_drvdata(dev
,NULL
);
4845 static void __devexit
bttv_remove(struct pci_dev
*pci_dev
)
4847 struct bttv
*btv
= pci_get_drvdata(pci_dev
);
4850 printk("bttv%d: unloading\n",btv
->c
.nr
);
4852 /* shutdown everything (DMA+IRQs) */
4853 btand(~15, BT848_GPIO_DMA_CTL
);
4854 btwrite(0, BT848_INT_MASK
);
4855 btwrite(~0x0, BT848_INT_STAT
);
4856 btwrite(0x0, BT848_GPIO_OUT_EN
);
4858 bttv_gpio_tracking(btv
,"cleanup");
4860 /* tell gpio modules we are leaving ... */
4862 wake_up(&btv
->gpioq
);
4863 bttv_input_fini(btv
);
4864 bttv_sub_del_devices(&btv
->c
);
4866 /* unregister i2c_bus + input */
4869 /* unregister video4linux */
4870 bttv_unregister_video(btv
);
4872 /* free allocated memory */
4873 btcx_riscmem_free(btv
->c
.pci
,&btv
->main
);
4875 /* free ressources */
4876 free_irq(btv
->c
.pci
->irq
,btv
);
4877 iounmap(btv
->bt848_mmio
);
4878 release_mem_region(pci_resource_start(btv
->c
.pci
,0),
4879 pci_resource_len(btv
->c
.pci
,0));
4881 pci_set_drvdata(pci_dev
, NULL
);
4886 static int bttv_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
4888 struct bttv
*btv
= pci_get_drvdata(pci_dev
);
4889 struct bttv_buffer_set idle
;
4890 unsigned long flags
;
4892 dprintk("bttv%d: suspend %d\n", btv
->c
.nr
, state
.event
);
4894 /* stop dma + irqs */
4895 spin_lock_irqsave(&btv
->s_lock
,flags
);
4896 memset(&idle
, 0, sizeof(idle
));
4897 btv
->state
.video
= btv
->curr
;
4898 btv
->state
.vbi
= btv
->cvbi
;
4899 btv
->state
.loop_irq
= btv
->loop_irq
;
4902 bttv_buffer_activate_video(btv
, &idle
);
4903 bttv_buffer_activate_vbi(btv
, NULL
);
4904 bttv_set_dma(btv
, 0);
4905 btwrite(0, BT848_INT_MASK
);
4906 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4908 /* save bt878 state */
4909 btv
->state
.gpio_enable
= btread(BT848_GPIO_OUT_EN
);
4910 btv
->state
.gpio_data
= gpio_read();
4912 /* save pci state */
4913 pci_save_state(pci_dev
);
4914 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
4915 pci_disable_device(pci_dev
);
4916 btv
->state
.disabled
= 1;
4921 static int bttv_resume(struct pci_dev
*pci_dev
)
4923 struct bttv
*btv
= pci_get_drvdata(pci_dev
);
4924 unsigned long flags
;
4927 dprintk("bttv%d: resume\n", btv
->c
.nr
);
4929 /* restore pci state */
4930 if (btv
->state
.disabled
) {
4931 err
=pci_enable_device(pci_dev
);
4933 printk(KERN_WARNING
"bttv%d: Can't enable device.\n",
4937 btv
->state
.disabled
= 0;
4939 err
=pci_set_power_state(pci_dev
, PCI_D0
);
4941 pci_disable_device(pci_dev
);
4942 printk(KERN_WARNING
"bttv%d: Can't enable device.\n",
4944 btv
->state
.disabled
= 1;
4948 pci_restore_state(pci_dev
);
4950 /* restore bt878 state */
4951 bttv_reinit_bt848(btv
);
4952 gpio_inout(0xffffff, btv
->state
.gpio_enable
);
4953 gpio_write(btv
->state
.gpio_data
);
4956 spin_lock_irqsave(&btv
->s_lock
,flags
);
4957 btv
->curr
= btv
->state
.video
;
4958 btv
->cvbi
= btv
->state
.vbi
;
4959 btv
->loop_irq
= btv
->state
.loop_irq
;
4960 bttv_buffer_activate_video(btv
, &btv
->curr
);
4961 bttv_buffer_activate_vbi(btv
, btv
->cvbi
);
4962 bttv_set_dma(btv
, 0);
4963 spin_unlock_irqrestore(&btv
->s_lock
,flags
);
4968 static struct pci_device_id bttv_pci_tbl
[] = {
4969 {PCI_VENDOR_ID_BROOKTREE
, PCI_DEVICE_ID_BT848
,
4970 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4971 {PCI_VENDOR_ID_BROOKTREE
, PCI_DEVICE_ID_BT849
,
4972 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4973 {PCI_VENDOR_ID_BROOKTREE
, PCI_DEVICE_ID_BT878
,
4974 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4975 {PCI_VENDOR_ID_BROOKTREE
, PCI_DEVICE_ID_BT879
,
4976 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
4980 MODULE_DEVICE_TABLE(pci
, bttv_pci_tbl
);
4982 static struct pci_driver bttv_pci_driver
= {
4984 .id_table
= bttv_pci_tbl
,
4985 .probe
= bttv_probe
,
4986 .remove
= __devexit_p(bttv_remove
),
4988 .suspend
= bttv_suspend
,
4989 .resume
= bttv_resume
,
4993 static int bttv_init_module(void)
4999 printk(KERN_INFO
"bttv: driver version %d.%d.%d loaded\n",
5000 (BTTV_VERSION_CODE
>> 16) & 0xff,
5001 (BTTV_VERSION_CODE
>> 8) & 0xff,
5002 BTTV_VERSION_CODE
& 0xff);
5004 printk(KERN_INFO
"bttv: snapshot date %04d-%02d-%02d\n",
5005 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
5007 if (gbuffers
< 2 || gbuffers
> VIDEO_MAX_FRAME
)
5009 if (gbufsize
< 0 || gbufsize
> BTTV_MAX_FBUF
)
5010 gbufsize
= BTTV_MAX_FBUF
;
5011 gbufsize
= (gbufsize
+ PAGE_SIZE
- 1) & PAGE_MASK
;
5013 printk(KERN_INFO
"bttv: using %d buffers with %dk (%d pages) each for capture\n",
5014 gbuffers
, gbufsize
>> 10, gbufsize
>> PAGE_SHIFT
);
5016 bttv_check_chipset();
5018 ret
= bus_register(&bttv_sub_bus_type
);
5020 printk(KERN_WARNING
"bttv: bus_register error: %d\n", ret
);
5023 return pci_register_driver(&bttv_pci_driver
);
5026 static void bttv_cleanup_module(void)
5028 pci_unregister_driver(&bttv_pci_driver
);
5029 bus_unregister(&bttv_sub_bus_type
);
5033 module_init(bttv_init_module
);
5034 module_exit(bttv_cleanup_module
);