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/moduleparam.h>
34 #include <linux/delay.h>
35 #include <linux/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/interrupt.h>
40 #include <linux/kdev_t.h>
42 #include <media/v4l2-common.h>
43 #include <media/tvaudio.h>
44 #include <media/msp3400.h>
46 #include <linux/dma-mapping.h>
49 #include <asm/byteorder.h>
51 #include <media/rds.h>
54 unsigned int bttv_num; /* number of Bt848s in use */
55 struct bttv bttvs[BTTV_MAX];
57 unsigned int bttv_debug;
58 unsigned int bttv_verbose = 1;
59 unsigned int bttv_gpio;
61 /* config variables */
63 static unsigned int bigendian=1;
65 static unsigned int bigendian;
67 static unsigned int radio[BTTV_MAX];
68 static unsigned int irq_debug;
69 static unsigned int gbuffers = 8;
70 static unsigned int gbufsize = 0x208000;
71 static unsigned int reset_crop = 1;
73 static int video_nr = -1;
74 static int radio_nr = -1;
75 static int vbi_nr = -1;
76 static int debug_latency;
78 static unsigned int fdsr;
81 static unsigned int combfilter;
82 static unsigned int lumafilter;
83 static unsigned int automute = 1;
84 static unsigned int chroma_agc;
85 static unsigned int adc_crush = 1;
86 static unsigned int whitecrush_upper = 0xCF;
87 static unsigned int whitecrush_lower = 0x7F;
88 static unsigned int vcr_hack;
89 static unsigned int irq_iswitch;
90 static unsigned int uv_ratio = 50;
91 static unsigned int full_luma_range;
92 static unsigned int coring;
93 extern int no_overlay;
95 /* API features (turn on/off stuff for testing) */
96 static unsigned int v4l2 = 1;
99 module_param(bttv_verbose, int, 0644);
100 module_param(bttv_gpio, int, 0644);
101 module_param(bttv_debug, int, 0644);
102 module_param(irq_debug, int, 0644);
103 module_param(debug_latency, int, 0644);
105 module_param(fdsr, int, 0444);
106 module_param(video_nr, int, 0444);
107 module_param(radio_nr, int, 0444);
108 module_param(vbi_nr, int, 0444);
109 module_param(gbuffers, int, 0444);
110 module_param(gbufsize, int, 0444);
111 module_param(reset_crop, int, 0444);
113 module_param(v4l2, int, 0644);
114 module_param(bigendian, int, 0644);
115 module_param(irq_iswitch, int, 0644);
116 module_param(combfilter, int, 0444);
117 module_param(lumafilter, int, 0444);
118 module_param(automute, int, 0444);
119 module_param(chroma_agc, int, 0444);
120 module_param(adc_crush, int, 0444);
121 module_param(whitecrush_upper, int, 0444);
122 module_param(whitecrush_lower, int, 0444);
123 module_param(vcr_hack, int, 0444);
124 module_param(uv_ratio, int, 0444);
125 module_param(full_luma_range, int, 0444);
126 module_param(coring, int, 0444);
128 module_param_array(radio, int, NULL, 0444);
130 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
131 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
132 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
133 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
134 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
135 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
136 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
137 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
138 MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
139 "is 1 (yes) for compatibility with older applications");
140 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
141 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
142 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
143 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
144 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
145 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
146 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
147 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
148 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
149 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
151 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
152 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
153 MODULE_LICENSE("GPL");
155 /* ----------------------------------------------------------------------- */
158 static ssize_t show_card(struct class_device *cd, char *buf)
160 struct video_device *vfd = to_video_device(cd);
161 struct bttv *btv = dev_get_drvdata(vfd->dev);
162 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
164 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
166 /* ----------------------------------------------------------------------- */
169 /* special timing tables from conexant... */
170 static u8 SRAM_Table[][60] =
172 /* PAL digital input over GPIO[7:0] */
174 45, // 45 bytes following
175 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
176 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
177 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
178 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
179 0x37,0x00,0xAF,0x21,0x00
181 /* NTSC digital input over GPIO[7:0] */
183 51, // 51 bytes following
184 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
185 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
186 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
187 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
188 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
191 // TGB_NTSC392 // quartzsight
192 // This table has been modified to be used for Fusion Rev D
194 0x2A, // size of table = 42
195 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
196 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
197 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
198 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
203 /* minhdelayx1 first video pixel we can capture on a line and
204 hdelayx1 start of active video, both relative to rising edge of
205 /HRESET pulse (0H) in 1 / fCLKx1.
206 swidth width of active video and
207 totalwidth total line width, both in 1 / fCLKx1.
208 sqwidth total line width in square pixels.
209 vdelay start of active video in 2 * field lines relative to
210 trailing edge of /VRESET pulse (VDELAY register).
211 sheight height of active video in 2 * field lines.
212 videostart0 ITU-R frame line number of the line corresponding
213 to vdelay in the first field. */
214 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
215 vdelay, sheight, videostart0) \
216 .cropcap.bounds.left = minhdelayx1, \
217 /* * 2 because vertically we count field lines times two, */ \
218 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
219 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
220 /* 4 is a safety margin at the end of the line. */ \
221 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
222 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
223 .cropcap.defrect.left = hdelayx1, \
224 .cropcap.defrect.top = (videostart0) * 2, \
225 .cropcap.defrect.width = swidth, \
226 .cropcap.defrect.height = sheight, \
227 .cropcap.pixelaspect.numerator = totalwidth, \
228 .cropcap.pixelaspect.denominator = sqwidth,
230 const struct bttv_tvnorm bttv_tvnorms[] = {
232 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
233 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
235 .v4l2_id = V4L2_STD_PAL,
243 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
244 .scaledtwidth = 1135,
248 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
250 /* ITU-R frame line number of the first VBI line
251 we can capture, of the first and second field.
252 The last line is determined by cropcap.bounds. */
253 .vbistart = { 7, 320 },
254 CROPCAP(/* minhdelayx1 */ 68,
256 /* Should be (768 * 1135 + 944 / 2) / 944.
257 cropcap.defrect is used for image width
258 checks, so we keep the old value 924. */
260 /* totalwidth */ 1135,
264 /* videostart0 */ 23)
265 /* bt878 (and bt848?) can capture another
266 line below active video. */
267 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
269 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
277 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
282 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
284 .vbistart = { 10, 273 },
285 CROPCAP(/* minhdelayx1 */ 68,
287 /* Should be (640 * 910 + 780 / 2) / 780? */
289 /* totalwidth */ 910,
293 /* videostart0 */ 23)
295 .v4l2_id = V4L2_STD_SECAM,
303 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
304 .scaledtwidth = 1135,
309 .sram = 0, /* like PAL, correct? */
310 .vbistart = { 7, 320 },
311 CROPCAP(/* minhdelayx1 */ 68,
314 /* totalwidth */ 1135,
318 /* videostart0 */ 23)
320 .v4l2_id = V4L2_STD_PAL_Nc,
328 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
335 .vbistart = { 7, 320 },
336 CROPCAP(/* minhdelayx1 */ 68,
338 /* swidth */ (640 * 910 + 780 / 2) / 780,
339 /* totalwidth */ 910,
343 /* videostart0 */ 23)
345 .v4l2_id = V4L2_STD_PAL_M,
353 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
360 .vbistart = { 10, 273 },
361 CROPCAP(/* minhdelayx1 */ 68,
363 /* swidth */ (640 * 910 + 780 / 2) / 780,
364 /* totalwidth */ 910,
368 /* videostart0 */ 23)
370 .v4l2_id = V4L2_STD_PAL_N,
378 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
385 .vbistart = { 7, 320 },
386 CROPCAP(/* minhdelayx1 */ 68,
388 /* swidth */ (768 * 1135 + 944 / 2) / 944,
389 /* totalwidth */ 1135,
393 /* videostart0 */ 23)
395 .v4l2_id = V4L2_STD_NTSC_M_JP,
403 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
410 .vbistart = { 10, 273 },
411 CROPCAP(/* minhdelayx1 */ 68,
413 /* swidth */ (640 * 910 + 780 / 2) / 780,
414 /* totalwidth */ 910,
418 /* videostart0 */ 23)
420 /* that one hopefully works with the strange timing
421 * which video recorders produce when playing a NTSC
422 * tape on a PAL TV ... */
423 .v4l2_id = V4L2_STD_PAL_60,
431 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
432 .scaledtwidth = 1135,
439 .vbistart = { 10, 273 },
440 CROPCAP(/* minhdelayx1 */ 68,
443 /* totalwidth */ 1135,
447 /* videostart0 */ 23)
450 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
452 /* ----------------------------------------------------------------------- */
454 packed pixel formats must come first */
455 static const struct bttv_format bttv_formats[] = {
457 .name = "8 bpp, gray",
458 .palette = VIDEO_PALETTE_GREY,
459 .fourcc = V4L2_PIX_FMT_GREY,
460 .btformat = BT848_COLOR_FMT_Y8,
462 .flags = FORMAT_FLAGS_PACKED,
464 .name = "8 bpp, dithered color",
465 .palette = VIDEO_PALETTE_HI240,
466 .fourcc = V4L2_PIX_FMT_HI240,
467 .btformat = BT848_COLOR_FMT_RGB8,
469 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
471 .name = "15 bpp RGB, le",
472 .palette = VIDEO_PALETTE_RGB555,
473 .fourcc = V4L2_PIX_FMT_RGB555,
474 .btformat = BT848_COLOR_FMT_RGB15,
476 .flags = FORMAT_FLAGS_PACKED,
478 .name = "15 bpp RGB, be",
480 .fourcc = V4L2_PIX_FMT_RGB555X,
481 .btformat = BT848_COLOR_FMT_RGB15,
482 .btswap = 0x03, /* byteswap */
484 .flags = FORMAT_FLAGS_PACKED,
486 .name = "16 bpp RGB, le",
487 .palette = VIDEO_PALETTE_RGB565,
488 .fourcc = V4L2_PIX_FMT_RGB565,
489 .btformat = BT848_COLOR_FMT_RGB16,
491 .flags = FORMAT_FLAGS_PACKED,
493 .name = "16 bpp RGB, be",
495 .fourcc = V4L2_PIX_FMT_RGB565X,
496 .btformat = BT848_COLOR_FMT_RGB16,
497 .btswap = 0x03, /* byteswap */
499 .flags = FORMAT_FLAGS_PACKED,
501 .name = "24 bpp RGB, le",
502 .palette = VIDEO_PALETTE_RGB24,
503 .fourcc = V4L2_PIX_FMT_BGR24,
504 .btformat = BT848_COLOR_FMT_RGB24,
506 .flags = FORMAT_FLAGS_PACKED,
508 .name = "32 bpp RGB, le",
509 .palette = VIDEO_PALETTE_RGB32,
510 .fourcc = V4L2_PIX_FMT_BGR32,
511 .btformat = BT848_COLOR_FMT_RGB32,
513 .flags = FORMAT_FLAGS_PACKED,
515 .name = "32 bpp RGB, be",
517 .fourcc = V4L2_PIX_FMT_RGB32,
518 .btformat = BT848_COLOR_FMT_RGB32,
519 .btswap = 0x0f, /* byte+word swap */
521 .flags = FORMAT_FLAGS_PACKED,
523 .name = "4:2:2, packed, YUYV",
524 .palette = VIDEO_PALETTE_YUV422,
525 .fourcc = V4L2_PIX_FMT_YUYV,
526 .btformat = BT848_COLOR_FMT_YUY2,
528 .flags = FORMAT_FLAGS_PACKED,
530 .name = "4:2:2, packed, YUYV",
531 .palette = VIDEO_PALETTE_YUYV,
532 .fourcc = V4L2_PIX_FMT_YUYV,
533 .btformat = BT848_COLOR_FMT_YUY2,
535 .flags = FORMAT_FLAGS_PACKED,
537 .name = "4:2:2, packed, UYVY",
538 .palette = VIDEO_PALETTE_UYVY,
539 .fourcc = V4L2_PIX_FMT_UYVY,
540 .btformat = BT848_COLOR_FMT_YUY2,
541 .btswap = 0x03, /* byteswap */
543 .flags = FORMAT_FLAGS_PACKED,
545 .name = "4:2:2, planar, Y-Cb-Cr",
546 .palette = VIDEO_PALETTE_YUV422P,
547 .fourcc = V4L2_PIX_FMT_YUV422P,
548 .btformat = BT848_COLOR_FMT_YCrCb422,
550 .flags = FORMAT_FLAGS_PLANAR,
554 .name = "4:2:0, planar, Y-Cb-Cr",
555 .palette = VIDEO_PALETTE_YUV420P,
556 .fourcc = V4L2_PIX_FMT_YUV420,
557 .btformat = BT848_COLOR_FMT_YCrCb422,
559 .flags = FORMAT_FLAGS_PLANAR,
563 .name = "4:2:0, planar, Y-Cr-Cb",
565 .fourcc = V4L2_PIX_FMT_YVU420,
566 .btformat = BT848_COLOR_FMT_YCrCb422,
568 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
572 .name = "4:1:1, planar, Y-Cb-Cr",
573 .palette = VIDEO_PALETTE_YUV411P,
574 .fourcc = V4L2_PIX_FMT_YUV411P,
575 .btformat = BT848_COLOR_FMT_YCrCb411,
577 .flags = FORMAT_FLAGS_PLANAR,
581 .name = "4:1:0, planar, Y-Cb-Cr",
582 .palette = VIDEO_PALETTE_YUV410P,
583 .fourcc = V4L2_PIX_FMT_YUV410,
584 .btformat = BT848_COLOR_FMT_YCrCb411,
586 .flags = FORMAT_FLAGS_PLANAR,
590 .name = "4:1:0, planar, Y-Cr-Cb",
592 .fourcc = V4L2_PIX_FMT_YVU410,
593 .btformat = BT848_COLOR_FMT_YCrCb411,
595 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
599 .name = "raw scanlines",
600 .palette = VIDEO_PALETTE_RAW,
602 .btformat = BT848_COLOR_FMT_RAW,
604 .flags = FORMAT_FLAGS_RAW,
607 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
609 /* ----------------------------------------------------------------------- */
611 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
612 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
613 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
614 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
615 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
616 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
617 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
618 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
619 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
620 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
621 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
622 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
624 static const struct v4l2_queryctrl no_ctl = {
626 .flags = V4L2_CTRL_FLAG_DISABLED,
628 static const struct v4l2_queryctrl bttv_ctls[] = {
631 .id = V4L2_CID_BRIGHTNESS,
632 .name = "Brightness",
636 .default_value = 32768,
637 .type = V4L2_CTRL_TYPE_INTEGER,
639 .id = V4L2_CID_CONTRAST,
644 .default_value = 32768,
645 .type = V4L2_CTRL_TYPE_INTEGER,
647 .id = V4L2_CID_SATURATION,
648 .name = "Saturation",
652 .default_value = 32768,
653 .type = V4L2_CTRL_TYPE_INTEGER,
660 .default_value = 32768,
661 .type = V4L2_CTRL_TYPE_INTEGER,
665 .id = V4L2_CID_AUDIO_MUTE,
669 .type = V4L2_CTRL_TYPE_BOOLEAN,
671 .id = V4L2_CID_AUDIO_VOLUME,
676 .default_value = 65535,
677 .type = V4L2_CTRL_TYPE_INTEGER,
679 .id = V4L2_CID_AUDIO_BALANCE,
684 .default_value = 32768,
685 .type = V4L2_CTRL_TYPE_INTEGER,
687 .id = V4L2_CID_AUDIO_BASS,
692 .default_value = 32768,
693 .type = V4L2_CTRL_TYPE_INTEGER,
695 .id = V4L2_CID_AUDIO_TREBLE,
700 .default_value = 32768,
701 .type = V4L2_CTRL_TYPE_INTEGER,
703 /* --- private --- */
705 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
706 .name = "chroma agc",
709 .type = V4L2_CTRL_TYPE_BOOLEAN,
711 .id = V4L2_CID_PRIVATE_COMBFILTER,
712 .name = "combfilter",
715 .type = V4L2_CTRL_TYPE_BOOLEAN,
717 .id = V4L2_CID_PRIVATE_AUTOMUTE,
721 .type = V4L2_CTRL_TYPE_BOOLEAN,
723 .id = V4L2_CID_PRIVATE_LUMAFILTER,
724 .name = "luma decimation filter",
727 .type = V4L2_CTRL_TYPE_BOOLEAN,
729 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
733 .type = V4L2_CTRL_TYPE_BOOLEAN,
735 .id = V4L2_CID_PRIVATE_VCR_HACK,
739 .type = V4L2_CTRL_TYPE_BOOLEAN,
741 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
742 .name = "whitecrush upper",
746 .default_value = 0xCF,
747 .type = V4L2_CTRL_TYPE_INTEGER,
749 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
750 .name = "whitecrush lower",
754 .default_value = 0x7F,
755 .type = V4L2_CTRL_TYPE_INTEGER,
757 .id = V4L2_CID_PRIVATE_UV_RATIO,
763 .type = V4L2_CTRL_TYPE_INTEGER,
765 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
766 .name = "full luma range",
769 .type = V4L2_CTRL_TYPE_BOOLEAN,
771 .id = V4L2_CID_PRIVATE_CORING,
777 .type = V4L2_CTRL_TYPE_INTEGER,
783 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
785 /* ----------------------------------------------------------------------- */
786 /* resource management */
789 RESOURCE_ allocated by freed by
791 VIDEO_READ bttv_read 1) bttv_read 2)
793 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
794 VIDIOC_QBUF 1) bttv_release
797 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
798 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
801 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
802 VIDIOC_QBUF 1) bttv_release
803 bttv_read, bttv_poll 1) 4)
805 1) The resource must be allocated when we enter buffer prepare functions
806 and remain allocated while buffers are in the DMA queue.
807 2) This is a single frame read.
808 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
809 RESOURCE_OVERLAY is allocated.
810 4) This is a continuous read, implies VIDIOC_STREAMON.
812 Note this driver permits video input and standard changes regardless if
813 resources are allocated.
816 #define VBI_RESOURCES (RESOURCE_VBI)
817 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
818 RESOURCE_VIDEO_STREAM | \
822 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
824 int xbits; /* mutual exclusive resources */
826 if (fh->resources & bit)
827 /* have it already allocated */
831 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
832 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
835 mutex_lock(&btv->lock);
836 if (btv->resources & xbits) {
837 /* no, someone else uses it */
841 if ((bit & VIDEO_RESOURCES)
842 && 0 == (btv->resources & VIDEO_RESOURCES)) {
843 /* Do crop - use current, don't - use default parameters. */
844 __s32 top = btv->crop[!!fh->do_crop].rect.top;
846 if (btv->vbi_end > top)
849 /* We cannot capture the same line as video and VBI data.
850 Claim scan lines crop[].rect.top to bottom. */
851 btv->crop_start = top;
852 } else if (bit & VBI_RESOURCES) {
853 __s32 end = fh->vbi_fmt.end;
855 if (end > btv->crop_start)
858 /* Claim scan lines above fh->vbi_fmt.end. */
862 /* it's free, grab it */
863 fh->resources |= bit;
864 btv->resources |= bit;
865 mutex_unlock(&btv->lock);
869 mutex_unlock(&btv->lock);
874 int check_btres(struct bttv_fh *fh, int bit)
876 return (fh->resources & bit);
880 int locked_btres(struct bttv *btv, int bit)
882 return (btv->resources & bit);
885 /* Call with btv->lock down. */
887 disclaim_vbi_lines(struct bttv *btv)
892 /* Call with btv->lock down. */
894 disclaim_video_lines(struct bttv *btv)
896 const struct bttv_tvnorm *tvnorm;
899 tvnorm = &bttv_tvnorms[btv->tvnorm];
900 btv->crop_start = tvnorm->cropcap.bounds.top
901 + tvnorm->cropcap.bounds.height;
903 /* VBI capturing ends at VDELAY, start of video capturing, no
904 matter how many lines the VBI RISC program expects. When video
905 capturing is off, it shall no longer "preempt" VBI capturing,
906 so we set VDELAY to maximum. */
907 crop = btread(BT848_E_CROP) | 0xc0;
908 btwrite(crop, BT848_E_CROP);
909 btwrite(0xfe, BT848_E_VDELAY_LO);
910 btwrite(crop, BT848_O_CROP);
911 btwrite(0xfe, BT848_O_VDELAY_LO);
915 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
917 if ((fh->resources & bits) != bits) {
918 /* trying to free ressources not allocated by us ... */
919 printk("bttv: BUG! (btres)\n");
921 mutex_lock(&btv->lock);
922 fh->resources &= ~bits;
923 btv->resources &= ~bits;
925 bits = btv->resources;
927 if (0 == (bits & VIDEO_RESOURCES))
928 disclaim_video_lines(btv);
930 if (0 == (bits & VBI_RESOURCES))
931 disclaim_vbi_lines(btv);
933 mutex_unlock(&btv->lock);
936 /* ----------------------------------------------------------------------- */
937 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
939 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
940 PLL_X = Reference pre-divider (0=1, 1=2)
941 PLL_C = Post divider (0=6, 1=4)
942 PLL_I = Integer input
943 PLL_F = Fractional input
945 F_input = 28.636363 MHz:
946 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
949 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
951 unsigned char fl, fh, fi;
953 /* prevent overflows */
966 btwrite(fl, BT848_PLL_F_LO);
967 btwrite(fh, BT848_PLL_F_HI);
968 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
971 static void set_pll(struct bttv *btv)
975 if (!btv->pll.pll_crystal)
978 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
979 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
983 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
985 if (btv->pll.pll_current == 0)
987 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
988 btv->c.nr,btv->pll.pll_ifreq);
989 btwrite(0x00,BT848_TGCTRL);
990 btwrite(0x00,BT848_PLL_XCI);
991 btv->pll.pll_current = 0;
995 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
996 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
997 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
999 for (i=0; i<10; i++) {
1000 /* Let other people run while the PLL stabilizes */
1004 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1005 btwrite(0,BT848_DSTATUS);
1007 btwrite(0x08,BT848_TGCTRL);
1008 btv->pll.pll_current = btv->pll.pll_ofreq;
1009 bttv_printk(" ok\n");
1013 btv->pll.pll_current = -1;
1014 bttv_printk("failed\n");
1018 /* used to switch between the bt848's analog/digital video capture modes */
1019 static void bt848A_set_timing(struct bttv *btv)
1022 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1023 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1025 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
1026 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1027 btv->c.nr,table_idx);
1029 /* timing change...reset timing generator address */
1030 btwrite(0x00, BT848_TGCTRL);
1031 btwrite(0x02, BT848_TGCTRL);
1032 btwrite(0x00, BT848_TGCTRL);
1034 len=SRAM_Table[table_idx][0];
1035 for(i = 1; i <= len; i++)
1036 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1037 btv->pll.pll_ofreq = 27000000;
1040 btwrite(0x11, BT848_TGCTRL);
1041 btwrite(0x41, BT848_DVSIF);
1043 btv->pll.pll_ofreq = fsc;
1045 btwrite(0x0, BT848_DVSIF);
1049 /* ----------------------------------------------------------------------- */
1051 static void bt848_bright(struct bttv *btv, int bright)
1055 // printk("bttv: set bright: %d\n",bright); // DEBUG
1056 btv->bright = bright;
1058 /* We want -128 to 127 we get 0-65535 */
1059 value = (bright >> 8) - 128;
1060 btwrite(value & 0xff, BT848_BRIGHT);
1063 static void bt848_hue(struct bttv *btv, int hue)
1070 value = (hue >> 8) - 128;
1071 btwrite(value & 0xff, BT848_HUE);
1074 static void bt848_contrast(struct bttv *btv, int cont)
1078 btv->contrast = cont;
1081 value = (cont >> 7);
1082 hibit = (value >> 6) & 4;
1083 btwrite(value & 0xff, BT848_CONTRAST_LO);
1084 btaor(hibit, ~4, BT848_E_CONTROL);
1085 btaor(hibit, ~4, BT848_O_CONTROL);
1088 static void bt848_sat(struct bttv *btv, int color)
1090 int val_u,val_v,hibits;
1092 btv->saturation = color;
1094 /* 0-511 for the color */
1095 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1096 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1097 hibits = (val_u >> 7) & 2;
1098 hibits |= (val_v >> 8) & 1;
1099 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1100 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1101 btaor(hibits, ~3, BT848_E_CONTROL);
1102 btaor(hibits, ~3, BT848_O_CONTROL);
1105 /* ----------------------------------------------------------------------- */
1108 video_mux(struct bttv *btv, unsigned int input)
1112 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1115 /* needed by RemoteVideo MX */
1116 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1118 gpio_inout(mask2,mask2);
1120 if (input == btv->svhs) {
1121 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1122 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1124 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1125 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1127 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
1128 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1129 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1130 btv->c.nr,input,mux);
1132 /* card specific hook */
1133 if(bttv_tvcards[btv->c.type].muxsel_hook)
1134 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1138 static char *audio_modes[] = {
1139 "audio: tuner", "audio: radio", "audio: extern",
1140 "audio: intern", "audio: mute"
1144 audio_mux(struct bttv *btv, int input, int mute)
1146 int gpio_val, signal;
1147 struct v4l2_control ctrl;
1148 struct i2c_client *c;
1150 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1151 bttv_tvcards[btv->c.type].gpiomask);
1152 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1158 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1161 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1163 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1165 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1167 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1171 ctrl.id = V4L2_CID_AUDIO_MUTE;
1172 ctrl.value = btv->mute;
1173 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
1174 c = btv->i2c_msp34xx_client;
1176 struct v4l2_routing route;
1178 /* Note: the inputs tuner/radio/extern/intern are translated
1179 to msp routings. This assumes common behavior for all msp3400
1180 based TV cards. When this assumption fails, then the
1181 specific MSP routing must be added to the card table.
1182 For now this is sufficient. */
1184 case TVAUDIO_INPUT_RADIO:
1185 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1186 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1188 case TVAUDIO_INPUT_EXTERN:
1189 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1190 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1192 case TVAUDIO_INPUT_INTERN:
1193 /* Yes, this is the same input as for RADIO. I doubt
1194 if this is ever used. The only board with an INTERN
1195 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1196 that was tested. My guess is that the whole INTERN
1197 input does not work. */
1198 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1199 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1201 case TVAUDIO_INPUT_TUNER:
1203 route.input = MSP_INPUT_DEFAULT;
1206 route.output = MSP_OUTPUT_DEFAULT;
1207 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1209 c = btv->i2c_tvaudio_client;
1211 struct v4l2_routing route;
1213 route.input = input;
1215 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1221 audio_mute(struct bttv *btv, int mute)
1223 return audio_mux(btv, btv->audio, mute);
1227 audio_input(struct bttv *btv, int input)
1229 return audio_mux(btv, input, btv->mute);
1233 i2c_vidiocschan(struct bttv *btv)
1235 v4l2_std_id std = bttv_tvnorms[btv->tvnorm].v4l2_id;
1237 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &std);
1238 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM)
1239 bttv_tda9880_setnorm(btv,btv->tvnorm);
1243 bttv_crop_calc_limits(struct bttv_crop *c)
1245 /* Scale factor min. 1:1, max. 16:1. Min. image size
1246 48 x 32. Scaled width must be a multiple of 4. */
1249 /* For bug compatibility with VIDIOCGCAP and image
1250 size checks in earlier driver versions. */
1251 c->min_scaled_width = 48;
1252 c->min_scaled_height = 32;
1254 c->min_scaled_width =
1255 (max(48, c->rect.width >> 4) + 3) & ~3;
1256 c->min_scaled_height =
1257 max(32, c->rect.height >> 4);
1260 c->max_scaled_width = c->rect.width & ~3;
1261 c->max_scaled_height = c->rect.height;
1265 bttv_crop_reset(struct bttv_crop *c, int norm)
1267 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1268 bttv_crop_calc_limits(c);
1271 /* Call with btv->lock down. */
1273 set_tvnorm(struct bttv *btv, unsigned int norm)
1275 const struct bttv_tvnorm *tvnorm;
1277 if (norm < 0 || norm >= BTTV_TVNORMS)
1280 tvnorm = &bttv_tvnorms[norm];
1282 if (btv->tvnorm < 0 ||
1283 btv->tvnorm >= BTTV_TVNORMS ||
1284 0 != memcmp(&bttv_tvnorms[btv->tvnorm].cropcap,
1286 sizeof (tvnorm->cropcap))) {
1287 bttv_crop_reset(&btv->crop[0], norm);
1288 btv->crop[1] = btv->crop[0]; /* current = default */
1290 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1291 btv->crop_start = tvnorm->cropcap.bounds.top
1292 + tvnorm->cropcap.bounds.height;
1298 btwrite(tvnorm->adelay, BT848_ADELAY);
1299 btwrite(tvnorm->bdelay, BT848_BDELAY);
1300 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1302 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1303 btwrite(1, BT848_VBI_PACK_DEL);
1304 bt848A_set_timing(btv);
1306 switch (btv->c.type) {
1307 case BTTV_BOARD_VOODOOTV_FM:
1308 bttv_tda9880_setnorm(btv,norm);
1314 /* Call with btv->lock down. */
1316 set_input(struct bttv *btv, unsigned int input)
1318 unsigned long flags;
1322 spin_lock_irqsave(&btv->s_lock,flags);
1323 if (btv->curr.frame_irq) {
1324 /* active capture -> delayed input switch */
1325 btv->new_input = input;
1327 video_mux(btv,input);
1329 spin_unlock_irqrestore(&btv->s_lock,flags);
1331 video_mux(btv,input);
1333 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1334 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
1335 set_tvnorm(btv,btv->tvnorm);
1336 i2c_vidiocschan(btv);
1339 static void init_irqreg(struct bttv *btv)
1342 btwrite(0xfffffUL, BT848_INT_STAT);
1344 if (bttv_tvcards[btv->c.type].no_video) {
1346 btwrite(BT848_INT_I2CDONE,
1350 btwrite((btv->triton1) |
1351 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1353 (fdsr ? BT848_INT_FDSR : 0) |
1354 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1355 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1361 static void init_bt848(struct bttv *btv)
1365 if (bttv_tvcards[btv->c.type].no_video) {
1366 /* very basic init only */
1371 btwrite(0x00, BT848_CAP_CTL);
1372 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1373 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1375 /* set planar and packed mode trigger points and */
1376 /* set rising edge of inverted GPINTR pin as irq trigger */
1377 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1378 BT848_GPIO_DMA_CTL_PLTP1_16|
1379 BT848_GPIO_DMA_CTL_PLTP23_16|
1380 BT848_GPIO_DMA_CTL_GPINTC|
1381 BT848_GPIO_DMA_CTL_GPINTI,
1382 BT848_GPIO_DMA_CTL);
1384 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1385 btwrite(val, BT848_E_SCLOOP);
1386 btwrite(val, BT848_O_SCLOOP);
1388 btwrite(0x20, BT848_E_VSCALE_HI);
1389 btwrite(0x20, BT848_O_VSCALE_HI);
1390 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1393 btwrite(whitecrush_upper, BT848_WC_UP);
1394 btwrite(whitecrush_lower, BT848_WC_DOWN);
1396 if (btv->opt_lumafilter) {
1397 btwrite(0, BT848_E_CONTROL);
1398 btwrite(0, BT848_O_CONTROL);
1400 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1401 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1404 bt848_bright(btv, btv->bright);
1405 bt848_hue(btv, btv->hue);
1406 bt848_contrast(btv, btv->contrast);
1407 bt848_sat(btv, btv->saturation);
1413 static void bttv_reinit_bt848(struct bttv *btv)
1415 unsigned long flags;
1418 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1419 spin_lock_irqsave(&btv->s_lock,flags);
1421 bttv_set_dma(btv,0);
1422 spin_unlock_irqrestore(&btv->s_lock,flags);
1425 btv->pll.pll_current = -1;
1426 set_input(btv,btv->input);
1429 static int get_control(struct bttv *btv, struct v4l2_control *c)
1431 struct video_audio va;
1434 for (i = 0; i < BTTV_CTLS; i++)
1435 if (bttv_ctls[i].id == c->id)
1439 if (btv->audio_hook && i >= 4 && i <= 8) {
1440 memset(&va,0,sizeof(va));
1441 btv->audio_hook(btv,&va,0);
1443 case V4L2_CID_AUDIO_MUTE:
1444 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1446 case V4L2_CID_AUDIO_VOLUME:
1447 c->value = va.volume;
1449 case V4L2_CID_AUDIO_BALANCE:
1450 c->value = va.balance;
1452 case V4L2_CID_AUDIO_BASS:
1455 case V4L2_CID_AUDIO_TREBLE:
1456 c->value = va.treble;
1462 case V4L2_CID_BRIGHTNESS:
1463 c->value = btv->bright;
1466 c->value = btv->hue;
1468 case V4L2_CID_CONTRAST:
1469 c->value = btv->contrast;
1471 case V4L2_CID_SATURATION:
1472 c->value = btv->saturation;
1475 case V4L2_CID_AUDIO_MUTE:
1476 case V4L2_CID_AUDIO_VOLUME:
1477 case V4L2_CID_AUDIO_BALANCE:
1478 case V4L2_CID_AUDIO_BASS:
1479 case V4L2_CID_AUDIO_TREBLE:
1480 bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c);
1483 case V4L2_CID_PRIVATE_CHROMA_AGC:
1484 c->value = btv->opt_chroma_agc;
1486 case V4L2_CID_PRIVATE_COMBFILTER:
1487 c->value = btv->opt_combfilter;
1489 case V4L2_CID_PRIVATE_LUMAFILTER:
1490 c->value = btv->opt_lumafilter;
1492 case V4L2_CID_PRIVATE_AUTOMUTE:
1493 c->value = btv->opt_automute;
1495 case V4L2_CID_PRIVATE_AGC_CRUSH:
1496 c->value = btv->opt_adc_crush;
1498 case V4L2_CID_PRIVATE_VCR_HACK:
1499 c->value = btv->opt_vcr_hack;
1501 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1502 c->value = btv->opt_whitecrush_upper;
1504 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1505 c->value = btv->opt_whitecrush_lower;
1507 case V4L2_CID_PRIVATE_UV_RATIO:
1508 c->value = btv->opt_uv_ratio;
1510 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1511 c->value = btv->opt_full_luma_range;
1513 case V4L2_CID_PRIVATE_CORING:
1514 c->value = btv->opt_coring;
1522 static int set_control(struct bttv *btv, struct v4l2_control *c)
1524 struct video_audio va;
1527 for (i = 0; i < BTTV_CTLS; i++)
1528 if (bttv_ctls[i].id == c->id)
1532 if (btv->audio_hook && i >= 4 && i <= 8) {
1533 memset(&va,0,sizeof(va));
1534 btv->audio_hook(btv,&va,0);
1536 case V4L2_CID_AUDIO_MUTE:
1538 va.flags |= VIDEO_AUDIO_MUTE;
1541 va.flags &= ~VIDEO_AUDIO_MUTE;
1546 case V4L2_CID_AUDIO_VOLUME:
1547 va.volume = c->value;
1549 case V4L2_CID_AUDIO_BALANCE:
1550 va.balance = c->value;
1552 case V4L2_CID_AUDIO_BASS:
1555 case V4L2_CID_AUDIO_TREBLE:
1556 va.treble = c->value;
1559 btv->audio_hook(btv,&va,1);
1563 case V4L2_CID_BRIGHTNESS:
1564 bt848_bright(btv,c->value);
1567 bt848_hue(btv,c->value);
1569 case V4L2_CID_CONTRAST:
1570 bt848_contrast(btv,c->value);
1572 case V4L2_CID_SATURATION:
1573 bt848_sat(btv,c->value);
1575 case V4L2_CID_AUDIO_MUTE:
1576 audio_mute(btv, c->value);
1578 case V4L2_CID_AUDIO_VOLUME:
1579 case V4L2_CID_AUDIO_BALANCE:
1580 case V4L2_CID_AUDIO_BASS:
1581 case V4L2_CID_AUDIO_TREBLE:
1582 bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c);
1585 case V4L2_CID_PRIVATE_CHROMA_AGC:
1586 btv->opt_chroma_agc = c->value;
1587 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1588 btwrite(val, BT848_E_SCLOOP);
1589 btwrite(val, BT848_O_SCLOOP);
1591 case V4L2_CID_PRIVATE_COMBFILTER:
1592 btv->opt_combfilter = c->value;
1594 case V4L2_CID_PRIVATE_LUMAFILTER:
1595 btv->opt_lumafilter = c->value;
1596 if (btv->opt_lumafilter) {
1597 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1598 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1600 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1601 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1604 case V4L2_CID_PRIVATE_AUTOMUTE:
1605 btv->opt_automute = c->value;
1607 case V4L2_CID_PRIVATE_AGC_CRUSH:
1608 btv->opt_adc_crush = c->value;
1609 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1612 case V4L2_CID_PRIVATE_VCR_HACK:
1613 btv->opt_vcr_hack = c->value;
1615 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1616 btv->opt_whitecrush_upper = c->value;
1617 btwrite(c->value, BT848_WC_UP);
1619 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1620 btv->opt_whitecrush_lower = c->value;
1621 btwrite(c->value, BT848_WC_DOWN);
1623 case V4L2_CID_PRIVATE_UV_RATIO:
1624 btv->opt_uv_ratio = c->value;
1625 bt848_sat(btv, btv->saturation);
1627 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1628 btv->opt_full_luma_range = c->value;
1629 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1631 case V4L2_CID_PRIVATE_CORING:
1632 btv->opt_coring = c->value;
1633 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1641 /* ----------------------------------------------------------------------- */
1643 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1645 unsigned int outbits, data;
1646 outbits = btread(BT848_GPIO_OUT_EN);
1647 data = btread(BT848_GPIO_DATA);
1648 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1649 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1652 static void bttv_field_count(struct bttv *btv)
1660 /* start field counter */
1661 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1663 /* stop field counter */
1664 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1665 btv->field_count = 0;
1669 static const struct bttv_format*
1670 format_by_palette(int palette)
1674 for (i = 0; i < BTTV_FORMATS; i++) {
1675 if (-1 == bttv_formats[i].palette)
1677 if (bttv_formats[i].palette == palette)
1678 return bttv_formats+i;
1683 static const struct bttv_format*
1684 format_by_fourcc(int fourcc)
1688 for (i = 0; i < BTTV_FORMATS; i++) {
1689 if (-1 == bttv_formats[i].fourcc)
1691 if (bttv_formats[i].fourcc == fourcc)
1692 return bttv_formats+i;
1697 /* ----------------------------------------------------------------------- */
1701 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1702 struct bttv_buffer *new)
1704 struct bttv_buffer *old;
1705 unsigned long flags;
1708 dprintk("switch_overlay: enter [new=%p]\n",new);
1710 new->vb.state = STATE_DONE;
1711 spin_lock_irqsave(&btv->s_lock,flags);
1715 bttv_set_dma(btv, 0x03);
1716 spin_unlock_irqrestore(&btv->s_lock,flags);
1718 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1719 bttv_dma_free(&fh->cap,btv, old);
1723 free_btres(btv,fh,RESOURCE_OVERLAY);
1724 dprintk("switch_overlay: done\n");
1728 /* ----------------------------------------------------------------------- */
1729 /* video4linux (1) interface */
1731 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1732 struct bttv_buffer *buf,
1733 const struct bttv_format *fmt,
1734 unsigned int width, unsigned int height,
1735 enum v4l2_field field)
1737 struct bttv_fh *fh = q->priv_data;
1738 int redo_dma_risc = 0;
1743 /* check settings */
1746 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1748 height = RAW_LINES*2;
1749 if (width*height > buf->vb.bsize)
1751 buf->vb.size = buf->vb.bsize;
1753 /* Make sure tvnorm and vbi_end remain consistent
1754 until we're done. */
1755 mutex_lock(&btv->lock);
1759 /* In this mode capturing always starts at defrect.top
1760 (default VDELAY), ignoring cropping parameters. */
1761 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1762 mutex_unlock(&btv->lock);
1766 mutex_unlock(&btv->lock);
1768 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1770 mutex_lock(&btv->lock);
1773 c = btv->crop[!!fh->do_crop];
1775 mutex_unlock(&btv->lock);
1777 if (width < c.min_scaled_width ||
1778 width > c.max_scaled_width ||
1779 height < c.min_scaled_height)
1783 case V4L2_FIELD_TOP:
1784 case V4L2_FIELD_BOTTOM:
1785 case V4L2_FIELD_ALTERNATE:
1786 /* btv->crop counts frame lines. Max. scale
1787 factor is 16:1 for frames, 8:1 for fields. */
1788 if (height * 2 > c.max_scaled_height)
1793 if (height > c.max_scaled_height)
1798 buf->vb.size = (width * height * fmt->depth) >> 3;
1799 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1803 /* alloc + fill struct bttv_buffer (if changed) */
1804 if (buf->vb.width != width || buf->vb.height != height ||
1805 buf->vb.field != field ||
1806 buf->tvnorm != norm || buf->fmt != fmt ||
1807 buf->crop.top != c.rect.top ||
1808 buf->crop.left != c.rect.left ||
1809 buf->crop.width != c.rect.width ||
1810 buf->crop.height != c.rect.height) {
1811 buf->vb.width = width;
1812 buf->vb.height = height;
1813 buf->vb.field = field;
1820 /* alloc risc memory */
1821 if (STATE_NEEDS_INIT == buf->vb.state) {
1823 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1828 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1831 buf->vb.state = STATE_PREPARED;
1835 bttv_dma_free(q,btv,buf);
1840 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1842 struct bttv_fh *fh = q->priv_data;
1844 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1847 while (*size * *count > gbuffers * gbufsize)
1853 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1854 enum v4l2_field field)
1856 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1857 struct bttv_fh *fh = q->priv_data;
1859 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1860 fh->width, fh->height, field);
1864 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1866 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1867 struct bttv_fh *fh = q->priv_data;
1868 struct bttv *btv = fh->btv;
1870 buf->vb.state = STATE_QUEUED;
1871 list_add_tail(&buf->vb.queue,&btv->capture);
1872 if (!btv->curr.frame_irq) {
1874 bttv_set_dma(btv, 0x03);
1878 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1880 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1881 struct bttv_fh *fh = q->priv_data;
1883 bttv_dma_free(q,fh->btv,buf);
1886 static struct videobuf_queue_ops bttv_video_qops = {
1887 .buf_setup = buffer_setup,
1888 .buf_prepare = buffer_prepare,
1889 .buf_queue = buffer_queue,
1890 .buf_release = buffer_release,
1893 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1897 return BTTV_VERSION_CODE;
1899 /* *** v4l1 *** ************************************************ */
1902 unsigned long *freq = arg;
1908 struct v4l2_frequency freq;
1910 memset(&freq, 0, sizeof(freq));
1911 freq.frequency = *(unsigned long *)arg;
1912 mutex_lock(&btv->lock);
1913 freq.type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1914 btv->freq = *(unsigned long *)arg;
1915 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,&freq);
1916 if (btv->has_matchbox && btv->radio_user)
1917 tea5757_set_freq(btv,*(unsigned long *)arg);
1918 mutex_unlock(&btv->lock);
1924 struct video_tuner *v = arg;
1926 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1928 if (v->tuner) /* Only tuner 0 */
1930 strcpy(v->name, "Television");
1932 v->rangehigh = 0x7FFFFFFF;
1933 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1934 v->mode = btv->tvnorm;
1935 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1936 bttv_call_i2c_clients(btv,cmd,v);
1941 struct video_tuner *v = arg;
1943 if (v->tuner) /* Only tuner 0 */
1945 if (v->mode >= BTTV_TVNORMS)
1948 mutex_lock(&btv->lock);
1949 set_tvnorm(btv,v->mode);
1950 bttv_call_i2c_clients(btv,cmd,v);
1951 mutex_unlock(&btv->lock);
1957 struct video_channel *v = arg;
1958 unsigned int channel = v->channel;
1960 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1963 v->flags = VIDEO_VC_AUDIO;
1964 v->type = VIDEO_TYPE_CAMERA;
1965 v->norm = btv->tvnorm;
1966 if (channel == bttv_tvcards[btv->c.type].tuner) {
1967 strcpy(v->name,"Television");
1968 v->flags|=VIDEO_VC_TUNER;
1969 v->type=VIDEO_TYPE_TV;
1971 } else if (channel == btv->svhs) {
1972 strcpy(v->name,"S-Video");
1974 sprintf(v->name,"Composite%d",channel);
1980 struct video_channel *v = arg;
1981 unsigned int channel = v->channel;
1983 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1985 if (v->norm >= BTTV_TVNORMS)
1988 mutex_lock(&btv->lock);
1989 if (channel == btv->input &&
1990 v->norm == btv->tvnorm) {
1992 mutex_unlock(&btv->lock);
1996 btv->tvnorm = v->norm;
1997 set_input(btv,v->channel);
1998 mutex_unlock(&btv->lock);
2004 struct video_audio *v = arg;
2006 memset(v,0,sizeof(*v));
2007 strcpy(v->name,"Television");
2008 v->flags |= VIDEO_AUDIO_MUTABLE;
2009 v->mode = VIDEO_SOUND_MONO;
2011 mutex_lock(&btv->lock);
2012 bttv_call_i2c_clients(btv,cmd,v);
2014 /* card specific hooks */
2015 if (btv->audio_hook)
2016 btv->audio_hook(btv,v,0);
2018 mutex_unlock(&btv->lock);
2023 struct video_audio *v = arg;
2024 unsigned int audio = v->audio;
2026 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
2029 mutex_lock(&btv->lock);
2030 audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
2031 bttv_call_i2c_clients(btv,cmd,v);
2033 /* card specific hooks */
2034 if (btv->audio_hook)
2035 btv->audio_hook(btv,v,1);
2037 mutex_unlock(&btv->lock);
2041 /* *** v4l2 *** ************************************************ */
2042 case VIDIOC_ENUMSTD:
2044 struct v4l2_standard *e = arg;
2045 unsigned int index = e->index;
2047 if (index >= BTTV_TVNORMS)
2049 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
2050 bttv_tvnorms[e->index].name);
2056 v4l2_std_id *id = arg;
2057 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
2062 v4l2_std_id *id = arg;
2065 for (i = 0; i < BTTV_TVNORMS; i++)
2066 if (*id & bttv_tvnorms[i].v4l2_id)
2068 if (i == BTTV_TVNORMS)
2071 mutex_lock(&btv->lock);
2073 i2c_vidiocschan(btv);
2074 mutex_unlock(&btv->lock);
2077 case VIDIOC_QUERYSTD:
2079 v4l2_std_id *id = arg;
2081 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
2082 *id = V4L2_STD_625_50;
2084 *id = V4L2_STD_525_60;
2088 case VIDIOC_ENUMINPUT:
2090 struct v4l2_input *i = arg;
2094 if (n >= bttv_tvcards[btv->c.type].video_inputs)
2096 memset(i,0,sizeof(*i));
2098 i->type = V4L2_INPUT_TYPE_CAMERA;
2100 if (i->index == bttv_tvcards[btv->c.type].tuner) {
2101 sprintf(i->name, "Television");
2102 i->type = V4L2_INPUT_TYPE_TUNER;
2104 } else if (i->index == btv->svhs) {
2105 sprintf(i->name, "S-Video");
2107 sprintf(i->name,"Composite%d",i->index);
2109 if (i->index == btv->input) {
2110 __u32 dstatus = btread(BT848_DSTATUS);
2111 if (0 == (dstatus & BT848_DSTATUS_PRES))
2112 i->status |= V4L2_IN_ST_NO_SIGNAL;
2113 if (0 == (dstatus & BT848_DSTATUS_HLOC))
2114 i->status |= V4L2_IN_ST_NO_H_LOCK;
2116 for (n = 0; n < BTTV_TVNORMS; n++)
2117 i->std |= bttv_tvnorms[n].v4l2_id;
2120 case VIDIOC_G_INPUT:
2126 case VIDIOC_S_INPUT:
2128 unsigned int *i = arg;
2130 if (*i > bttv_tvcards[btv->c.type].video_inputs)
2132 mutex_lock(&btv->lock);
2134 mutex_unlock(&btv->lock);
2138 case VIDIOC_G_TUNER:
2140 struct v4l2_tuner *t = arg;
2142 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2146 mutex_lock(&btv->lock);
2147 memset(t,0,sizeof(*t));
2148 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2149 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
2150 strcpy(t->name, "Television");
2151 t->capability = V4L2_TUNER_CAP_NORM;
2152 t->type = V4L2_TUNER_ANALOG_TV;
2153 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2156 if (btv->audio_hook) {
2158 struct video_audio va;
2159 memset(&va, 0, sizeof(struct video_audio));
2160 btv->audio_hook(btv,&va,0);
2161 t->audmode = V4L2_TUNER_MODE_MONO;
2162 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2163 if(va.mode & VIDEO_SOUND_STEREO) {
2164 t->audmode = V4L2_TUNER_MODE_STEREO;
2165 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
2167 if(va.mode & VIDEO_SOUND_LANG2) {
2168 t->audmode = V4L2_TUNER_MODE_LANG1;
2169 t->rxsubchans = V4L2_TUNER_SUB_LANG1
2170 | V4L2_TUNER_SUB_LANG2;
2173 /* FIXME: fill capability+audmode */
2174 mutex_unlock(&btv->lock);
2177 case VIDIOC_S_TUNER:
2179 struct v4l2_tuner *t = arg;
2181 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2185 mutex_lock(&btv->lock);
2186 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
2187 if (btv->audio_hook) {
2188 struct video_audio va;
2189 memset(&va, 0, sizeof(struct video_audio));
2190 if (t->audmode == V4L2_TUNER_MODE_MONO)
2191 va.mode = VIDEO_SOUND_MONO;
2192 else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
2193 t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
2194 va.mode = VIDEO_SOUND_STEREO;
2195 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
2196 va.mode = VIDEO_SOUND_LANG1;
2197 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
2198 va.mode = VIDEO_SOUND_LANG2;
2199 btv->audio_hook(btv,&va,1);
2201 mutex_unlock(&btv->lock);
2205 case VIDIOC_G_FREQUENCY:
2207 struct v4l2_frequency *f = arg;
2209 memset(f,0,sizeof(*f));
2210 f->type = V4L2_TUNER_ANALOG_TV;
2211 f->frequency = btv->freq;
2214 case VIDIOC_S_FREQUENCY:
2216 struct v4l2_frequency *f = arg;
2218 if (unlikely(f->tuner != 0))
2220 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
2222 mutex_lock(&btv->lock);
2223 btv->freq = f->frequency;
2224 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
2225 if (btv->has_matchbox && btv->radio_user)
2226 tea5757_set_freq(btv,btv->freq);
2227 mutex_unlock(&btv->lock);
2230 case VIDIOC_LOG_STATUS:
2232 printk(KERN_INFO "bttv%d: ================= START STATUS CARD #%d =================\n", btv->c.nr, btv->c.nr);
2233 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
2234 printk(KERN_INFO "bttv%d: ================== END STATUS CARD #%d ==================\n", btv->c.nr, btv->c.nr);
2239 return -ENOIOCTLCMD;
2245 /* Given cropping boundaries b and the scaled width and height of a
2246 single field or frame, which must not exceed hardware limits, this
2247 function adjusts the cropping parameters c. */
2249 bttv_crop_adjust (struct bttv_crop * c,
2250 const struct v4l2_rect * b,
2253 enum v4l2_field field)
2255 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2259 if (width < c->min_scaled_width) {
2260 /* Max. hor. scale factor 16:1. */
2261 c->rect.width = width * 16;
2262 } else if (width > c->max_scaled_width) {
2263 /* Min. hor. scale factor 1:1. */
2264 c->rect.width = width;
2266 max_left = b->left + b->width - width;
2267 max_left = min(max_left, (__s32) MAX_HDELAY);
2268 if (c->rect.left > max_left)
2269 c->rect.left = max_left;
2272 if (height < c->min_scaled_height) {
2273 /* Max. vert. scale factor 16:1, single fields 8:1. */
2274 c->rect.height = height * 16;
2275 } else if (frame_height > c->max_scaled_height) {
2276 /* Min. vert. scale factor 1:1.
2277 Top and height count field lines times two. */
2278 c->rect.height = (frame_height + 1) & ~1;
2280 max_top = b->top + b->height - c->rect.height;
2281 if (c->rect.top > max_top)
2282 c->rect.top = max_top;
2285 bttv_crop_calc_limits(c);
2288 /* Returns an error if scaling to a frame or single field with the given
2289 width and height is not possible with the current cropping parameters
2290 and width aligned according to width_mask. If adjust_size is TRUE the
2291 function may adjust the width and/or height instead, rounding width
2292 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2293 also adjust the current cropping parameters to get closer to the
2294 desired image size. */
2296 limit_scaled_size (struct bttv_fh * fh,
2299 enum v4l2_field field,
2300 unsigned int width_mask,
2301 unsigned int width_bias,
2305 struct bttv *btv = fh->btv;
2306 const struct v4l2_rect *b;
2307 struct bttv_crop *c;
2314 BUG_ON((int) width_mask >= 0 ||
2315 width_bias >= (unsigned int) -width_mask);
2317 /* Make sure tvnorm, vbi_end and the current cropping parameters
2318 remain consistent until we're done. */
2319 mutex_lock(&btv->lock);
2321 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2323 /* Do crop - use current, don't - use default parameters. */
2324 c = &btv->crop[!!fh->do_crop];
2329 && !locked_btres(btv, VIDEO_RESOURCES)) {
2333 /* We cannot scale up. When the scaled image is larger
2334 than crop.rect we adjust the crop.rect as required
2335 by the V4L2 spec, hence cropcap.bounds are our limit. */
2336 max_width = min(b->width, (__s32) MAX_HACTIVE);
2337 max_height = b->height;
2339 /* We cannot capture the same line as video and VBI data.
2340 Note btv->vbi_end is really a minimum, see
2341 bttv_vbi_try_fmt(). */
2342 if (btv->vbi_end > b->top) {
2343 max_height -= btv->vbi_end - b->top;
2345 if (min_height > max_height)
2350 if (btv->vbi_end > c->rect.top)
2353 min_width = c->min_scaled_width;
2354 min_height = c->min_scaled_height;
2355 max_width = c->max_scaled_width;
2356 max_height = c->max_scaled_height;
2361 min_width = (min_width - width_mask - 1) & width_mask;
2362 max_width = max_width & width_mask;
2364 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2365 min_height = min_height;
2366 /* Min. scale factor is 1:1. */
2367 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2370 *width = clamp(*width, min_width, max_width);
2371 *height = clamp(*height, min_height, max_height);
2373 /* Round after clamping to avoid overflow. */
2374 *width = (*width + width_bias) & width_mask;
2377 bttv_crop_adjust(c, b, *width, *height, field);
2379 if (btv->vbi_end > c->rect.top) {
2380 /* Move the crop window out of the way. */
2381 c->rect.top = btv->vbi_end;
2386 if (*width < min_width ||
2387 *height < min_height ||
2388 *width > max_width ||
2389 *height > max_height ||
2390 0 != (*width & ~width_mask))
2394 rc = 0; /* success */
2397 mutex_unlock(&btv->lock);
2402 /* Returns an error if the given overlay window dimensions are not
2403 possible with the current cropping parameters. If adjust_size is
2404 TRUE the function may adjust the window width and/or height
2405 instead, however it always rounds the horizontal position and
2406 width as btcx_align() does. If adjust_crop is TRUE the function
2407 may also adjust the current cropping parameters to get closer
2408 to the desired window size. */
2410 verify_window (struct bttv_fh * fh,
2411 struct v4l2_window * win,
2415 enum v4l2_field field;
2416 unsigned int width_mask;
2419 if (win->w.width < 48 || win->w.height < 32)
2421 if (win->clipcount > 2048)
2426 if (V4L2_FIELD_ANY == field) {
2429 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2430 field = (win->w.height > height2)
2431 ? V4L2_FIELD_INTERLACED
2435 case V4L2_FIELD_TOP:
2436 case V4L2_FIELD_BOTTOM:
2437 case V4L2_FIELD_INTERLACED:
2443 /* 4-byte alignment. */
2444 if (NULL == fh->ovfmt)
2447 switch (fh->ovfmt->depth) {
2461 win->w.width -= win->w.left & ~width_mask;
2462 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2464 rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
2466 /* width_bias: round down */ 0,
2467 adjust_size, adjust_crop);
2475 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2476 struct v4l2_window *win, int fixup)
2478 struct v4l2_clip *clips = NULL;
2479 int n,size,retval = 0;
2481 if (NULL == fh->ovfmt)
2483 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2485 retval = verify_window(fh, win,
2486 /* adjust_size */ fixup,
2487 /* adjust_crop */ fixup);
2491 /* copy clips -- luckily v4l1 + v4l2 are binary
2492 compatible here ...*/
2494 size = sizeof(*clips)*(n+4);
2495 clips = kmalloc(size,GFP_KERNEL);
2499 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2504 /* clip against screen */
2505 if (NULL != btv->fbuf.base)
2506 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2508 btcx_sort_clips(clips,n);
2510 /* 4-byte alignments */
2511 switch (fh->ovfmt->depth) {
2514 btcx_align(&win->w, clips, n, 3);
2517 btcx_align(&win->w, clips, n, 1);
2520 /* no alignment fixups needed */
2526 mutex_lock(&fh->cap.lock);
2527 kfree(fh->ov.clips);
2528 fh->ov.clips = clips;
2532 fh->ov.field = win->field;
2533 fh->ov.setup_ok = 1;
2534 btv->init.ov.w.width = win->w.width;
2535 btv->init.ov.w.height = win->w.height;
2536 btv->init.ov.field = win->field;
2538 /* update overlay if needed */
2540 if (check_btres(fh, RESOURCE_OVERLAY)) {
2541 struct bttv_buffer *new;
2543 new = videobuf_alloc(sizeof(*new));
2544 new->crop = btv->crop[!!fh->do_crop].rect;
2545 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2546 retval = bttv_switch_overlay(btv,fh,new);
2548 mutex_unlock(&fh->cap.lock);
2552 /* ----------------------------------------------------------------------- */
2554 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2556 struct videobuf_queue* q = NULL;
2559 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2562 case V4L2_BUF_TYPE_VBI_CAPTURE:
2571 static int bttv_resource(struct bttv_fh *fh)
2576 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2577 res = RESOURCE_VIDEO_STREAM;
2579 case V4L2_BUF_TYPE_VBI_CAPTURE:
2588 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2590 struct videobuf_queue *q = bttv_queue(fh);
2591 int res = bttv_resource(fh);
2593 if (check_btres(fh,res))
2595 if (videobuf_queue_is_busy(q))
2602 pix_format_set_size (struct v4l2_pix_format * f,
2603 const struct bttv_format * fmt,
2605 unsigned int height)
2610 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2611 f->bytesperline = width; /* Y plane */
2612 f->sizeimage = (width * height * fmt->depth) >> 3;
2614 f->bytesperline = (width * fmt->depth) >> 3;
2615 f->sizeimage = height * f->bytesperline;
2619 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2622 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2623 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2624 pix_format_set_size (&f->fmt.pix, fh->fmt,
2625 fh->width, fh->height);
2626 f->fmt.pix.field = fh->cap.field;
2627 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2629 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2630 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2631 f->fmt.win.w = fh->ov.w;
2632 f->fmt.win.field = fh->ov.field;
2634 case V4L2_BUF_TYPE_VBI_CAPTURE:
2635 bttv_vbi_get_fmt(fh, &f->fmt.vbi);
2642 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2643 struct v4l2_format *f, int adjust_crop)
2646 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2648 const struct bttv_format *fmt;
2649 enum v4l2_field field;
2650 __s32 width, height;
2653 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2657 field = f->fmt.pix.field;
2658 if (V4L2_FIELD_ANY == field) {
2661 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2662 field = (f->fmt.pix.height > height2)
2663 ? V4L2_FIELD_INTERLACED
2664 : V4L2_FIELD_BOTTOM;
2666 if (V4L2_FIELD_SEQ_BT == field)
2667 field = V4L2_FIELD_SEQ_TB;
2669 case V4L2_FIELD_TOP:
2670 case V4L2_FIELD_BOTTOM:
2671 case V4L2_FIELD_ALTERNATE:
2672 case V4L2_FIELD_INTERLACED:
2674 case V4L2_FIELD_SEQ_TB:
2675 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2682 width = f->fmt.pix.width;
2683 height = f->fmt.pix.height;
2685 rc = limit_scaled_size(fh, &width, &height, field,
2686 /* width_mask: 4 pixels */ ~3,
2687 /* width_bias: nearest */ 2,
2688 /* adjust_size */ 1,
2693 /* update data for the application */
2694 f->fmt.pix.field = field;
2695 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2699 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2700 return verify_window(fh, &f->fmt.win,
2701 /* adjust_size */ 1,
2702 /* adjust_crop */ 0);
2703 case V4L2_BUF_TYPE_VBI_CAPTURE:
2704 return bttv_vbi_try_fmt(fh, &f->fmt.vbi);
2710 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2711 struct v4l2_format *f)
2716 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2718 const struct bttv_format *fmt;
2720 retval = bttv_switch_type(fh,f->type);
2723 retval = bttv_try_fmt(fh,btv,f, /* adjust_crop */ 1);
2726 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2728 /* update our state informations */
2729 mutex_lock(&fh->cap.lock);
2731 fh->cap.field = f->fmt.pix.field;
2732 fh->cap.last = V4L2_FIELD_NONE;
2733 fh->width = f->fmt.pix.width;
2734 fh->height = f->fmt.pix.height;
2735 btv->init.fmt = fmt;
2736 btv->init.width = f->fmt.pix.width;
2737 btv->init.height = f->fmt.pix.height;
2738 mutex_unlock(&fh->cap.lock);
2742 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2743 if (no_overlay > 0) {
2744 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2747 return setup_window(fh, btv, &f->fmt.win, 1);
2748 case V4L2_BUF_TYPE_VBI_CAPTURE:
2749 retval = bttv_switch_type(fh,f->type);
2752 return bttv_vbi_set_fmt(fh, &f->fmt.vbi);
2758 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2759 unsigned int cmd, void *arg)
2761 struct bttv_fh *fh = file->private_data;
2762 struct bttv *btv = fh->btv;
2763 unsigned long flags;
2767 v4l_print_ioctl(btv->c.name, cmd);
2770 bttv_reinit_bt848(btv);
2778 case VIDIOC_S_INPUT:
2779 case VIDIOC_S_TUNER:
2780 case VIDIOC_S_FREQUENCY:
2781 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2788 /* *** v4l1 *** ************************************************ */
2791 struct video_capability *cap = arg;
2793 memset(cap,0,sizeof(*cap));
2794 strcpy(cap->name,btv->video_dev->name);
2795 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2797 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2800 cap->type = VID_TYPE_CAPTURE|
2804 if (no_overlay <= 0)
2805 cap->type |= VID_TYPE_OVERLAY;
2807 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2808 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2810 cap->minheight = 32;
2812 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2813 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2819 struct video_picture *pic = arg;
2821 memset(pic,0,sizeof(*pic));
2822 pic->brightness = btv->bright;
2823 pic->contrast = btv->contrast;
2824 pic->hue = btv->hue;
2825 pic->colour = btv->saturation;
2827 pic->depth = fh->fmt->depth;
2828 pic->palette = fh->fmt->palette;
2834 struct video_picture *pic = arg;
2835 const struct bttv_format *fmt;
2837 fmt = format_by_palette(pic->palette);
2840 mutex_lock(&fh->cap.lock);
2841 if (fmt->depth != pic->depth) {
2843 goto fh_unlock_and_return;
2845 if (fmt->flags & FORMAT_FLAGS_RAW) {
2846 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2847 RAW_LINES * 2. F1 is stored at offset 0, F2
2848 at buffer size / 2. */
2849 fh->width = RAW_BPL;
2850 fh->height = gbufsize / RAW_BPL;
2851 btv->init.width = RAW_BPL;
2852 btv->init.height = gbufsize / RAW_BPL;
2856 btv->init.ovfmt = fmt;
2857 btv->init.fmt = fmt;
2859 /* dirty hack time: swap bytes for overlay if the
2860 display adaptor is big endian (insmod option) */
2861 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2862 fmt->palette == VIDEO_PALETTE_RGB565 ||
2863 fmt->palette == VIDEO_PALETTE_RGB32) {
2867 bt848_bright(btv,pic->brightness);
2868 bt848_contrast(btv,pic->contrast);
2869 bt848_hue(btv,pic->hue);
2870 bt848_sat(btv,pic->colour);
2871 mutex_unlock(&fh->cap.lock);
2877 struct video_window *win = arg;
2879 memset(win,0,sizeof(*win));
2880 win->x = fh->ov.w.left;
2881 win->y = fh->ov.w.top;
2882 win->width = fh->ov.w.width;
2883 win->height = fh->ov.w.height;
2888 struct video_window *win = arg;
2889 struct v4l2_window w2;
2891 if (no_overlay > 0) {
2892 printk ("VIDIOCSWIN: no_overlay\n");
2896 w2.field = V4L2_FIELD_ANY;
2899 w2.w.width = win->width;
2900 w2.w.height = win->height;
2901 w2.clipcount = win->clipcount;
2902 w2.clips = (struct v4l2_clip __user *)win->clips;
2903 retval = setup_window(fh, btv, &w2, 0);
2905 /* on v4l1 this ioctl affects the read() size too */
2906 fh->width = fh->ov.w.width;
2907 fh->height = fh->ov.w.height;
2908 btv->init.width = fh->ov.w.width;
2909 btv->init.height = fh->ov.w.height;
2916 struct video_buffer *fbuf = arg;
2918 fbuf->base = btv->fbuf.base;
2919 fbuf->width = btv->fbuf.fmt.width;
2920 fbuf->height = btv->fbuf.fmt.height;
2921 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2923 fbuf->depth = fh->ovfmt->depth;
2926 fbuf->depth = ((fbuf->bytesperline<<3)
2936 struct video_buffer *fbuf = arg;
2937 const struct bttv_format *fmt;
2940 if(!capable(CAP_SYS_ADMIN) &&
2941 !capable(CAP_SYS_RAWIO))
2943 end = (unsigned long)fbuf->base +
2944 fbuf->height * fbuf->bytesperline;
2945 mutex_lock(&fh->cap.lock);
2948 switch (fbuf->depth) {
2950 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2953 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2956 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2959 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2963 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2970 goto fh_unlock_and_return;
2974 btv->init.ovfmt = fmt;
2975 btv->init.fmt = fmt;
2976 btv->fbuf.base = fbuf->base;
2977 btv->fbuf.fmt.width = fbuf->width;
2978 btv->fbuf.fmt.height = fbuf->height;
2979 if (fbuf->bytesperline)
2980 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2982 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2983 mutex_unlock(&fh->cap.lock);
2988 case VIDIOC_OVERLAY:
2990 struct bttv_buffer *new;
2995 if (NULL == btv->fbuf.base)
2997 if (!fh->ov.setup_ok) {
2998 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
3003 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
3006 mutex_lock(&fh->cap.lock);
3008 fh->ov.tvnorm = btv->tvnorm;
3009 new = videobuf_alloc(sizeof(*new));
3010 new->crop = btv->crop[!!fh->do_crop].rect;
3011 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
3017 retval = bttv_switch_overlay(btv,fh,new);
3018 mutex_unlock(&fh->cap.lock);
3024 struct video_mbuf *mbuf = arg;
3027 mutex_lock(&fh->cap.lock);
3028 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
3031 goto fh_unlock_and_return;
3032 memset(mbuf,0,sizeof(*mbuf));
3033 mbuf->frames = gbuffers;
3034 mbuf->size = gbuffers * gbufsize;
3035 for (i = 0; i < gbuffers; i++)
3036 mbuf->offsets[i] = i * gbufsize;
3037 mutex_unlock(&fh->cap.lock);
3040 case VIDIOCMCAPTURE:
3042 struct video_mmap *vm = arg;
3043 struct bttv_buffer *buf;
3044 enum v4l2_field field;
3048 if (vm->frame >= VIDEO_MAX_FRAME)
3051 res = bttv_resource(fh);
3052 if (!check_alloc_btres(btv, fh, res))
3055 mutex_lock(&fh->cap.lock);
3057 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
3059 goto fh_unlock_and_return;
3060 if (0 == buf->vb.baddr)
3061 goto fh_unlock_and_return;
3062 if (buf->vb.state == STATE_QUEUED ||
3063 buf->vb.state == STATE_ACTIVE)
3064 goto fh_unlock_and_return;
3066 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
3067 field = (vm->height > height2)
3068 ? V4L2_FIELD_INTERLACED
3069 : V4L2_FIELD_BOTTOM;
3070 retval = bttv_prepare_buffer(&fh->cap,btv,buf,
3071 format_by_palette(vm->format),
3072 vm->width,vm->height,field);
3074 goto fh_unlock_and_return;
3075 spin_lock_irqsave(&btv->s_lock,flags);
3076 buffer_queue(&fh->cap,&buf->vb);
3077 spin_unlock_irqrestore(&btv->s_lock,flags);
3078 mutex_unlock(&fh->cap.lock);
3084 struct bttv_buffer *buf;
3086 if (*frame >= VIDEO_MAX_FRAME)
3089 mutex_lock(&fh->cap.lock);
3091 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
3093 goto fh_unlock_and_return;
3094 retval = videobuf_waiton(&buf->vb,0,1);
3096 goto fh_unlock_and_return;
3097 switch (buf->vb.state) {
3102 videobuf_dma_sync(&fh->cap,&buf->vb.dma);
3103 bttv_dma_free(&fh->cap,btv,buf);
3109 mutex_unlock(&fh->cap.lock);
3114 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
3115 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
3123 return v4l_compat_translate_ioctl(inode, file, cmd,
3124 arg, bttv_do_ioctl);
3135 return bttv_common_ioctls(btv,cmd,arg);
3137 /* *** v4l2 *** ************************************************ */
3138 case VIDIOC_QUERYCAP:
3140 struct v4l2_capability *cap = arg;
3144 memset(cap, 0, sizeof (*cap));
3145 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
3146 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
3147 snprintf(cap->bus_info, sizeof (cap->bus_info),
3148 "PCI:%s", pci_name(btv->c.pci));
3149 cap->version = BTTV_VERSION_CODE;
3151 V4L2_CAP_VIDEO_CAPTURE |
3152 V4L2_CAP_VBI_CAPTURE |
3153 V4L2_CAP_READWRITE |
3155 if (no_overlay <= 0)
3156 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
3158 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
3159 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
3160 cap->capabilities |= V4L2_CAP_TUNER;
3164 case VIDIOC_ENUM_FMT:
3166 struct v4l2_fmtdesc *f = arg;
3167 enum v4l2_buf_type type;
3172 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
3177 memset(f,0,sizeof(*f));
3180 f->pixelformat = V4L2_PIX_FMT_GREY;
3181 strcpy(f->description,"vbi data");
3185 /* video capture + overlay */
3187 for (i = 0; i < BTTV_FORMATS; i++) {
3188 if (bttv_formats[i].fourcc != -1)
3190 if ((unsigned int)index == f->index)
3193 if (BTTV_FORMATS == i)
3197 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3199 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3200 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
3206 memset(f,0,sizeof(*f));
3209 f->pixelformat = bttv_formats[i].fourcc;
3210 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
3214 case VIDIOC_TRY_FMT:
3216 struct v4l2_format *f = arg;
3217 return bttv_try_fmt(fh,btv,f, /* adjust_crop */ 0);
3221 struct v4l2_format *f = arg;
3222 return bttv_g_fmt(fh,f);
3226 struct v4l2_format *f = arg;
3227 return bttv_s_fmt(fh,btv,f);
3232 struct v4l2_framebuffer *fb = arg;
3235 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3237 fb->fmt.pixelformat = fh->ovfmt->fourcc;
3242 struct v4l2_framebuffer *fb = arg;
3243 const struct bttv_format *fmt;
3245 if(!capable(CAP_SYS_ADMIN) &&
3246 !capable(CAP_SYS_RAWIO))
3250 fmt = format_by_fourcc(fb->fmt.pixelformat);
3253 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
3257 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
3258 __s32 width = fb->fmt.width;
3259 __s32 height = fb->fmt.height;
3261 retval = limit_scaled_size(fh, &width, &height,
3262 V4L2_FIELD_INTERLACED,
3263 /* width_mask */ ~3,
3265 /* adjust_size */ 0,
3266 /* adjust_crop */ 0);
3272 mutex_lock(&fh->cap.lock);
3273 btv->fbuf.base = fb->base;
3274 btv->fbuf.fmt.width = fb->fmt.width;
3275 btv->fbuf.fmt.height = fb->fmt.height;
3276 if (0 != fb->fmt.bytesperline)
3277 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
3279 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
3283 btv->init.ovfmt = fmt;
3284 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
3287 fh->ov.w.width = fb->fmt.width;
3288 fh->ov.w.height = fb->fmt.height;
3289 btv->init.ov.w.width = fb->fmt.width;
3290 btv->init.ov.w.height = fb->fmt.height;
3291 kfree(fh->ov.clips);
3292 fh->ov.clips = NULL;
3295 if (check_btres(fh, RESOURCE_OVERLAY)) {
3296 struct bttv_buffer *new;
3298 new = videobuf_alloc(sizeof(*new));
3299 new->crop = btv->crop[!!fh->do_crop].rect;
3300 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
3301 retval = bttv_switch_overlay(btv,fh,new);
3304 mutex_unlock(&fh->cap.lock);
3308 case VIDIOC_REQBUFS:
3309 return videobuf_reqbufs(bttv_queue(fh),arg);
3311 case VIDIOC_QUERYBUF:
3312 return videobuf_querybuf(bttv_queue(fh),arg);
3316 int res = bttv_resource(fh);
3318 if (!check_alloc_btres(btv, fh, res))
3320 return videobuf_qbuf(bttv_queue(fh),arg);
3324 return videobuf_dqbuf(bttv_queue(fh),arg,
3325 file->f_flags & O_NONBLOCK);
3327 case VIDIOC_STREAMON:
3329 int res = bttv_resource(fh);
3331 if (!check_alloc_btres(btv,fh,res))
3333 return videobuf_streamon(bttv_queue(fh));
3335 case VIDIOC_STREAMOFF:
3337 int res = bttv_resource(fh);
3339 retval = videobuf_streamoff(bttv_queue(fh));
3342 free_btres(btv,fh,res);
3346 case VIDIOC_QUERYCTRL:
3348 struct v4l2_queryctrl *c = arg;
3351 if ((c->id < V4L2_CID_BASE ||
3352 c->id >= V4L2_CID_LASTP1) &&
3353 (c->id < V4L2_CID_PRIVATE_BASE ||
3354 c->id >= V4L2_CID_PRIVATE_LASTP1))
3356 for (i = 0; i < BTTV_CTLS; i++)
3357 if (bttv_ctls[i].id == c->id)
3359 if (i == BTTV_CTLS) {
3364 if (btv->audio_hook && i >= 4 && i <= 8) {
3365 struct video_audio va;
3366 memset(&va,0,sizeof(va));
3367 btv->audio_hook(btv,&va,0);
3368 switch (bttv_ctls[i].id) {
3369 case V4L2_CID_AUDIO_VOLUME:
3370 if (!(va.flags & VIDEO_AUDIO_VOLUME))
3373 case V4L2_CID_AUDIO_BALANCE:
3374 if (!(va.flags & VIDEO_AUDIO_BALANCE))
3377 case V4L2_CID_AUDIO_BASS:
3378 if (!(va.flags & VIDEO_AUDIO_BASS))
3381 case V4L2_CID_AUDIO_TREBLE:
3382 if (!(va.flags & VIDEO_AUDIO_TREBLE))
3390 return get_control(btv,arg);
3392 return set_control(btv,arg);
3395 struct v4l2_streamparm *parm = arg;
3396 struct v4l2_standard s;
3397 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
3399 memset(parm,0,sizeof(*parm));
3400 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
3401 bttv_tvnorms[btv->tvnorm].name);
3402 parm->parm.capture.timeperframe = s.frameperiod;
3406 case VIDIOC_G_PRIORITY:
3408 enum v4l2_priority *p = arg;
3410 *p = v4l2_prio_max(&btv->prio);
3413 case VIDIOC_S_PRIORITY:
3415 enum v4l2_priority *prio = arg;
3417 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
3420 case VIDIOC_CROPCAP:
3422 struct v4l2_cropcap *cap = arg;
3423 enum v4l2_buf_type type;
3427 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3428 type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3431 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3438 struct v4l2_crop * crop = arg;
3440 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3441 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3444 /* No fh->do_crop = 1; because btv->crop[1] may be
3445 inconsistent with fh->width or fh->height and apps
3446 do not expect a change here. */
3448 crop->c = btv->crop[!!fh->do_crop].rect;
3454 struct v4l2_crop *crop = arg;
3455 const struct v4l2_rect *b;
3462 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3463 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3466 retval = v4l2_prio_check(&btv->prio,&fh->prio);
3470 /* Make sure tvnorm, vbi_end and the current cropping
3471 parameters remain consistent until we're done. Note
3472 read() may change vbi_end in check_alloc_btres(). */
3473 mutex_lock(&btv->lock);
3477 if (locked_btres(fh->btv, VIDEO_RESOURCES))
3478 goto btv_unlock_and_return;
3480 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3483 b_right = b_left + b->width;
3484 b_bottom = b->top + b->height;
3486 b_top = max(b->top, btv->vbi_end);
3487 if (b_top + 32 >= b_bottom)
3488 goto btv_unlock_and_return;
3490 /* Min. scaled size 48 x 32. */
3491 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3492 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3494 c.rect.width = clamp(crop->c.width,
3495 48, b_right - c.rect.left);
3497 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3498 /* Top and height must be a multiple of two. */
3499 c.rect.top = (c.rect.top + 1) & ~1;
3501 c.rect.height = clamp(crop->c.height,
3502 32, b_bottom - c.rect.top);
3503 c.rect.height = (c.rect.height + 1) & ~1;
3505 bttv_crop_calc_limits(&c);
3509 mutex_unlock(&btv->lock);
3513 mutex_lock(&fh->cap.lock);
3515 if (fh->width < c.min_scaled_width) {
3516 fh->width = c.min_scaled_width;
3517 btv->init.width = c.min_scaled_width;
3518 } else if (fh->width > c.max_scaled_width) {
3519 fh->width = c.max_scaled_width;
3520 btv->init.width = c.max_scaled_width;
3523 if (fh->height < c.min_scaled_height) {
3524 fh->height = c.min_scaled_height;
3525 btv->init.height = c.min_scaled_height;
3526 } else if (fh->height > c.max_scaled_height) {
3527 fh->height = c.max_scaled_height;
3528 btv->init.height = c.max_scaled_height;
3531 mutex_unlock(&fh->cap.lock);
3536 case VIDIOC_ENUMSTD:
3539 case VIDIOC_ENUMINPUT:
3540 case VIDIOC_G_INPUT:
3541 case VIDIOC_S_INPUT:
3542 case VIDIOC_G_TUNER:
3543 case VIDIOC_S_TUNER:
3544 case VIDIOC_G_FREQUENCY:
3545 case VIDIOC_S_FREQUENCY:
3546 case VIDIOC_LOG_STATUS:
3547 return bttv_common_ioctls(btv,cmd,arg);
3550 return -ENOIOCTLCMD;
3554 fh_unlock_and_return:
3555 mutex_unlock(&fh->cap.lock);
3558 btv_unlock_and_return:
3559 mutex_unlock(&btv->lock);
3563 static int bttv_ioctl(struct inode *inode, struct file *file,
3564 unsigned int cmd, unsigned long arg)
3566 struct bttv_fh *fh = file->private_data;
3571 const struct bttv_tvnorm *tvnorm;
3573 tvnorm = fh->vbi_fmt.tvnorm;
3575 if (fh->vbi_fmt.fmt.start[0] != tvnorm->vbistart[0] ||
3576 fh->vbi_fmt.fmt.start[1] != tvnorm->vbistart[1] ||
3577 fh->vbi_fmt.fmt.count[0] != fh->vbi_fmt.fmt.count[1]) {
3578 /* BTTV_VBISIZE cannot express these parameters,
3579 however open() resets the paramters to defaults
3580 and apps shouldn't call BTTV_VBISIZE after
3585 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
3586 return (fh->vbi_fmt.fmt.count[0] * 2
3587 * fh->vbi_fmt.fmt.samples_per_line);
3591 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
3595 static ssize_t bttv_read(struct file *file, char __user *data,
3596 size_t count, loff_t *ppos)
3598 struct bttv_fh *fh = file->private_data;
3601 if (fh->btv->errors)
3602 bttv_reinit_bt848(fh->btv);
3603 dprintk("bttv%d: read count=%d type=%s\n",
3604 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3607 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3608 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3609 /* VIDEO_READ in use by another fh,
3610 or VIDEO_STREAM by any fh. */
3613 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3614 file->f_flags & O_NONBLOCK);
3615 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
3617 case V4L2_BUF_TYPE_VBI_CAPTURE:
3618 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3620 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3621 file->f_flags & O_NONBLOCK);
3629 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3631 struct bttv_fh *fh = file->private_data;
3632 struct bttv_buffer *buf;
3633 enum v4l2_field field;
3635 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3636 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3638 return videobuf_poll_stream(file, &fh->vbi, wait);
3641 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3642 /* streaming capture */
3643 if (list_empty(&fh->cap.stream))
3645 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3647 /* read() capture */
3648 mutex_lock(&fh->cap.lock);
3649 if (NULL == fh->cap.read_buf) {
3650 /* need to capture a new frame */
3651 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM)) {
3652 mutex_unlock(&fh->cap.lock);
3655 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
3656 if (NULL == fh->cap.read_buf) {
3657 mutex_unlock(&fh->cap.lock);
3660 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3661 field = videobuf_next_field(&fh->cap);
3662 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3663 kfree (fh->cap.read_buf);
3664 fh->cap.read_buf = NULL;
3665 mutex_unlock(&fh->cap.lock);
3668 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3669 fh->cap.read_off = 0;
3671 mutex_unlock(&fh->cap.lock);
3672 buf = (struct bttv_buffer*)fh->cap.read_buf;
3675 poll_wait(file, &buf->vb.done, wait);
3676 if (buf->vb.state == STATE_DONE ||
3677 buf->vb.state == STATE_ERROR)
3678 return POLLIN|POLLRDNORM;
3682 static int bttv_open(struct inode *inode, struct file *file)
3684 int minor = iminor(inode);
3685 struct bttv *btv = NULL;
3687 enum v4l2_buf_type type = 0;
3690 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3692 for (i = 0; i < bttv_num; i++) {
3693 if (bttvs[i].video_dev &&
3694 bttvs[i].video_dev->minor == minor) {
3696 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3699 if (bttvs[i].vbi_dev &&
3700 bttvs[i].vbi_dev->minor == minor) {
3702 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3709 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3710 btv->c.nr,v4l2_type_names[type]);
3712 /* allocate per filehandle data */
3713 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3716 file->private_data = fh;
3719 fh->ov.setup_ok = 0;
3720 v4l2_prio_open(&btv->prio,&fh->prio);
3722 videobuf_queue_init(&fh->cap, &bttv_video_qops,
3723 btv->c.pci, &btv->s_lock,
3724 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3725 V4L2_FIELD_INTERLACED,
3726 sizeof(struct bttv_buffer),
3728 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3729 btv->c.pci, &btv->s_lock,
3730 V4L2_BUF_TYPE_VBI_CAPTURE,
3732 sizeof(struct bttv_buffer),
3734 i2c_vidiocschan(btv);
3738 /* The V4L2 spec requires one global set of cropping parameters
3739 which only change on request. These are stored in btv->crop[1].
3740 However for compatibility with V4L apps and cropping unaware
3741 V4L2 apps we now reset the cropping parameters as seen through
3742 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3743 will use btv->crop[0], the default cropping parameters for the
3744 current video standard, and VIDIOC_S_FMT will not implicitely
3745 change the cropping parameters until VIDIOC_S_CROP has been
3747 fh->do_crop = !reset_crop; /* module parameter */
3749 /* Likewise there should be one global set of VBI capture
3750 parameters, but for compatibility with V4L apps and earlier
3751 driver versions each fh has its own parameters. */
3752 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3754 bttv_field_count(btv);
3758 static int bttv_release(struct inode *inode, struct file *file)
3760 struct bttv_fh *fh = file->private_data;
3761 struct bttv *btv = fh->btv;
3763 /* turn off overlay */
3764 if (check_btres(fh, RESOURCE_OVERLAY))
3765 bttv_switch_overlay(btv,fh,NULL);
3767 /* stop video capture */
3768 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3769 videobuf_streamoff(&fh->cap);
3770 free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
3772 if (fh->cap.read_buf) {
3773 buffer_release(&fh->cap,fh->cap.read_buf);
3774 kfree(fh->cap.read_buf);
3776 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3777 free_btres(btv, fh, RESOURCE_VIDEO_READ);
3780 /* stop vbi capture */
3781 if (check_btres(fh, RESOURCE_VBI)) {
3782 if (fh->vbi.streaming)
3783 videobuf_streamoff(&fh->vbi);
3784 if (fh->vbi.reading)
3785 videobuf_read_stop(&fh->vbi);
3786 free_btres(btv,fh,RESOURCE_VBI);
3790 videobuf_mmap_free(&fh->cap);
3791 videobuf_mmap_free(&fh->vbi);
3792 v4l2_prio_close(&btv->prio,&fh->prio);
3793 file->private_data = NULL;
3797 bttv_field_count(btv);
3802 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3804 struct bttv_fh *fh = file->private_data;
3806 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3807 fh->btv->c.nr, v4l2_type_names[fh->type],
3808 vma->vm_start, vma->vm_end - vma->vm_start);
3809 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3812 static const struct file_operations bttv_fops =
3814 .owner = THIS_MODULE,
3816 .release = bttv_release,
3817 .ioctl = bttv_ioctl,
3818 .compat_ioctl = v4l_compat_ioctl32,
3819 .llseek = no_llseek,
3825 static struct video_device bttv_video_template =
3828 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3829 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3830 .hardware = VID_HARDWARE_BT848,
3835 static struct video_device bttv_vbi_template =
3837 .name = "bt848/878 vbi",
3838 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3839 .hardware = VID_HARDWARE_BT848,
3844 /* ----------------------------------------------------------------------- */
3845 /* radio interface */
3847 static int radio_open(struct inode *inode, struct file *file)
3849 int minor = iminor(inode);
3850 struct bttv *btv = NULL;
3853 dprintk("bttv: open minor=%d\n",minor);
3855 for (i = 0; i < bttv_num; i++) {
3856 if (bttvs[i].radio_dev->minor == minor) {
3864 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3865 mutex_lock(&btv->lock);
3869 file->private_data = btv;
3871 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3872 audio_input(btv,TVAUDIO_INPUT_RADIO);
3874 mutex_unlock(&btv->lock);
3878 static int radio_release(struct inode *inode, struct file *file)
3880 struct bttv *btv = file->private_data;
3881 struct rds_command cmd;
3885 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3890 static int radio_do_ioctl(struct inode *inode, struct file *file,
3891 unsigned int cmd, void *arg)
3893 struct bttv *btv = file->private_data;
3898 struct video_capability *cap = arg;
3900 memset(cap,0,sizeof(*cap));
3901 strcpy(cap->name,btv->radio_dev->name);
3902 cap->type = VID_TYPE_TUNER;
3910 struct video_tuner *v = arg;
3914 memset(v,0,sizeof(*v));
3915 strcpy(v->name, "Radio");
3916 bttv_call_i2c_clients(btv,cmd,v);
3928 case VIDIOC_LOG_STATUS:
3929 return bttv_common_ioctls(btv,cmd,arg);
3932 return -ENOIOCTLCMD;
3937 static int radio_ioctl(struct inode *inode, struct file *file,
3938 unsigned int cmd, unsigned long arg)
3940 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3943 static ssize_t radio_read(struct file *file, char __user *data,
3944 size_t count, loff_t *ppos)
3946 struct bttv *btv = file->private_data;
3947 struct rds_command cmd;
3948 cmd.block_count = count/3;
3950 cmd.instance = file;
3951 cmd.result = -ENODEV;
3953 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3958 static unsigned int radio_poll(struct file *file, poll_table *wait)
3960 struct bttv *btv = file->private_data;
3961 struct rds_command cmd;
3962 cmd.instance = file;
3963 cmd.event_list = wait;
3964 cmd.result = -ENODEV;
3965 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3970 static const struct file_operations radio_fops =
3972 .owner = THIS_MODULE,
3975 .release = radio_release,
3976 .ioctl = radio_ioctl,
3977 .llseek = no_llseek,
3981 static struct video_device radio_template =
3983 .name = "bt848/878 radio",
3984 .type = VID_TYPE_TUNER,
3985 .hardware = VID_HARDWARE_BT848,
3986 .fops = &radio_fops,
3990 /* ----------------------------------------------------------------------- */
3991 /* some debug code */
3993 static int bttv_risc_decode(u32 risc)
3995 static char *instr[16] = {
3996 [ BT848_RISC_WRITE >> 28 ] = "write",
3997 [ BT848_RISC_SKIP >> 28 ] = "skip",
3998 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3999 [ BT848_RISC_JUMP >> 28 ] = "jump",
4000 [ BT848_RISC_SYNC >> 28 ] = "sync",
4001 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
4002 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
4003 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
4005 static int incr[16] = {
4006 [ BT848_RISC_WRITE >> 28 ] = 2,
4007 [ BT848_RISC_JUMP >> 28 ] = 2,
4008 [ BT848_RISC_SYNC >> 28 ] = 2,
4009 [ BT848_RISC_WRITE123 >> 28 ] = 5,
4010 [ BT848_RISC_SKIP123 >> 28 ] = 2,
4011 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
4013 static char *bits[] = {
4014 "be0", "be1", "be2", "be3/resync",
4015 "set0", "set1", "set2", "set3",
4016 "clr0", "clr1", "clr2", "clr3",
4017 "irq", "res", "eol", "sol",
4021 printk("0x%08x [ %s", risc,
4022 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
4023 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
4024 if (risc & (1 << (i + 12)))
4025 printk(" %s",bits[i]);
4026 printk(" count=%d ]\n", risc & 0xfff);
4027 return incr[risc >> 28] ? incr[risc >> 28] : 1;
4030 static void bttv_risc_disasm(struct bttv *btv,
4031 struct btcx_riscmem *risc)
4035 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
4036 btv->c.name, risc->cpu, (unsigned long)risc->dma);
4037 for (i = 0; i < (risc->size >> 2); i += n) {
4038 printk("%s: 0x%lx: ", btv->c.name,
4039 (unsigned long)(risc->dma + (i<<2)));
4040 n = bttv_risc_decode(risc->cpu[i]);
4041 for (j = 1; j < n; j++)
4042 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
4043 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
4045 if (0 == risc->cpu[i])
4050 static void bttv_print_riscaddr(struct bttv *btv)
4052 printk(" main: %08Lx\n",
4053 (unsigned long long)btv->main.dma);
4054 printk(" vbi : o=%08Lx e=%08Lx\n",
4055 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
4056 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
4057 printk(" cap : o=%08Lx e=%08Lx\n",
4058 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
4059 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
4060 printk(" scr : o=%08Lx e=%08Lx\n",
4061 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
4062 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
4063 bttv_risc_disasm(btv, &btv->main);
4066 /* ----------------------------------------------------------------------- */
4069 static char *irq_name[] = {
4070 "FMTCHG", // format change detected (525 vs. 625)
4071 "VSYNC", // vertical sync (new field)
4072 "HSYNC", // horizontal sync
4073 "OFLOW", // chroma/luma AGC overflow
4074 "HLOCK", // horizontal lock changed
4075 "VPRES", // video presence changed
4077 "I2CDONE", // hw irc operation finished
4078 "GPINT", // gpio port triggered irq
4080 "RISCI", // risc instruction triggered irq
4081 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
4082 "FTRGT", // pixel data fifo overrun
4083 "FDSR", // fifo data stream resyncronisation
4084 "PPERR", // parity error (data transfer)
4085 "RIPERR", // parity error (read risc instructions)
4086 "PABORT", // pci abort
4087 "OCERR", // risc instruction error
4088 "SCERR", // syncronisation error
4091 static void bttv_print_irqbits(u32 print, u32 mark)
4096 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
4097 if (print & (1 << i))
4098 printk(" %s",irq_name[i]);
4099 if (mark & (1 << i))
4104 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
4106 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
4108 (unsigned long)btv->main.dma,
4109 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
4110 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
4113 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
4114 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
4115 "Ok, then this is harmless, don't worry ;)\n",
4119 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
4121 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
4127 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
4129 struct bttv_buffer *item;
4131 memset(set,0,sizeof(*set));
4133 /* capture request ? */
4134 if (!list_empty(&btv->capture)) {
4136 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4137 if (V4L2_FIELD_HAS_TOP(item->vb.field))
4139 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
4142 /* capture request for other field ? */
4143 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
4144 (item->vb.queue.next != &btv->capture)) {
4145 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
4146 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
4147 if (NULL == set->top &&
4148 V4L2_FIELD_TOP == item->vb.field) {
4151 if (NULL == set->bottom &&
4152 V4L2_FIELD_BOTTOM == item->vb.field) {
4155 if (NULL != set->top && NULL != set->bottom)
4161 /* screen overlay ? */
4162 if (NULL != btv->screen) {
4163 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
4164 if (NULL == set->top && NULL == set->bottom) {
4165 set->top = btv->screen;
4166 set->bottom = btv->screen;
4169 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
4171 set->top = btv->screen;
4173 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
4174 NULL == set->bottom) {
4175 set->bottom = btv->screen;
4180 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
4181 btv->c.nr,set->top, set->bottom,
4182 btv->screen,set->frame_irq,set->top_irq);
4187 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
4188 struct bttv_buffer_set *curr, unsigned int state)
4192 do_gettimeofday(&ts);
4194 if (wakeup->top == wakeup->bottom) {
4195 if (NULL != wakeup->top && curr->top != wakeup->top) {
4197 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
4198 wakeup->top->vb.ts = ts;
4199 wakeup->top->vb.field_count = btv->field_count;
4200 wakeup->top->vb.state = state;
4201 wake_up(&wakeup->top->vb.done);
4204 if (NULL != wakeup->top && curr->top != wakeup->top) {
4206 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
4207 wakeup->top->vb.ts = ts;
4208 wakeup->top->vb.field_count = btv->field_count;
4209 wakeup->top->vb.state = state;
4210 wake_up(&wakeup->top->vb.done);
4212 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
4214 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
4215 wakeup->bottom->vb.ts = ts;
4216 wakeup->bottom->vb.field_count = btv->field_count;
4217 wakeup->bottom->vb.state = state;
4218 wake_up(&wakeup->bottom->vb.done);
4224 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
4232 do_gettimeofday(&ts);
4234 wakeup->vb.field_count = btv->field_count;
4235 wakeup->vb.state = state;
4236 wake_up(&wakeup->vb.done);
4239 static void bttv_irq_timeout(unsigned long data)
4241 struct bttv *btv = (struct bttv *)data;
4242 struct bttv_buffer_set old,new;
4243 struct bttv_buffer *ovbi;
4244 struct bttv_buffer *item;
4245 unsigned long flags;
4248 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
4249 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
4250 btread(BT848_RISC_COUNT));
4251 bttv_print_irqbits(btread(BT848_INT_STAT),0);
4255 spin_lock_irqsave(&btv->s_lock,flags);
4257 /* deactivate stuff */
4258 memset(&new,0,sizeof(new));
4264 bttv_buffer_activate_video(btv, &new);
4265 bttv_buffer_activate_vbi(btv, NULL);
4266 bttv_set_dma(btv, 0);
4269 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
4270 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
4272 /* cancel all outstanding capture / vbi requests */
4273 while (!list_empty(&btv->capture)) {
4274 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4275 list_del(&item->vb.queue);
4276 item->vb.state = STATE_ERROR;
4277 wake_up(&item->vb.done);
4279 while (!list_empty(&btv->vcapture)) {
4280 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4281 list_del(&item->vb.queue);
4282 item->vb.state = STATE_ERROR;
4283 wake_up(&item->vb.done);
4287 spin_unlock_irqrestore(&btv->s_lock,flags);
4291 bttv_irq_wakeup_top(struct bttv *btv)
4293 struct bttv_buffer *wakeup = btv->curr.top;
4298 spin_lock(&btv->s_lock);
4299 btv->curr.top_irq = 0;
4300 btv->curr.top = NULL;
4301 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
4303 do_gettimeofday(&wakeup->vb.ts);
4304 wakeup->vb.field_count = btv->field_count;
4305 wakeup->vb.state = STATE_DONE;
4306 wake_up(&wakeup->vb.done);
4307 spin_unlock(&btv->s_lock);
4310 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
4314 if (rc > risc->dma + risc->size)
4320 bttv_irq_switch_video(struct bttv *btv)
4322 struct bttv_buffer_set new;
4323 struct bttv_buffer_set old;
4326 spin_lock(&btv->s_lock);
4328 /* new buffer set */
4329 bttv_irq_next_video(btv, &new);
4330 rc = btread(BT848_RISC_COUNT);
4331 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4332 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4335 bttv_irq_debug_low_latency(btv, rc);
4336 spin_unlock(&btv->s_lock);
4343 btv->loop_irq &= ~1;
4344 bttv_buffer_activate_video(btv, &new);
4345 bttv_set_dma(btv, 0);
4348 if (UNSET != btv->new_input) {
4349 video_mux(btv,btv->new_input);
4350 btv->new_input = UNSET;
4353 /* wake up finished buffers */
4354 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
4355 spin_unlock(&btv->s_lock);
4359 bttv_irq_switch_vbi(struct bttv *btv)
4361 struct bttv_buffer *new = NULL;
4362 struct bttv_buffer *old;
4365 spin_lock(&btv->s_lock);
4367 if (!list_empty(&btv->vcapture))
4368 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4371 rc = btread(BT848_RISC_COUNT);
4372 if (NULL != old && (is_active(&old->top, rc) ||
4373 is_active(&old->bottom, rc))) {
4376 bttv_irq_debug_low_latency(btv, rc);
4377 spin_unlock(&btv->s_lock);
4383 btv->loop_irq &= ~4;
4384 bttv_buffer_activate_vbi(btv, new);
4385 bttv_set_dma(btv, 0);
4387 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
4388 spin_unlock(&btv->s_lock);
4391 static irqreturn_t bttv_irq(int irq, void *dev_id)
4399 btv=(struct bttv *)dev_id;
4401 if (btv->custom_irq)
4402 handled = btv->custom_irq(btv);
4406 /* get/clear interrupt status bits */
4407 stat=btread(BT848_INT_STAT);
4408 astat=stat&btread(BT848_INT_MASK);
4412 btwrite(stat,BT848_INT_STAT);
4414 /* get device status bits */
4415 dstat=btread(BT848_DSTATUS);
4418 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4419 "riscs=%x, riscc=%08x, ",
4420 btv->c.nr, count, btv->field_count,
4421 stat>>28, btread(BT848_RISC_COUNT));
4422 bttv_print_irqbits(stat,astat);
4423 if (stat & BT848_INT_HLOCK)
4424 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4426 if (stat & BT848_INT_VPRES)
4427 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4429 if (stat & BT848_INT_FMTCHG)
4430 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4435 if (astat&BT848_INT_VSYNC)
4438 if ((astat & BT848_INT_GPINT) && btv->remote) {
4439 wake_up(&btv->gpioq);
4440 bttv_input_irq(btv);
4443 if (astat & BT848_INT_I2CDONE) {
4444 btv->i2c_done = stat;
4445 wake_up(&btv->i2c_queue);
4448 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
4449 bttv_irq_switch_vbi(btv);
4451 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
4452 bttv_irq_wakeup_top(btv);
4454 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
4455 bttv_irq_switch_video(btv);
4457 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
4458 audio_mute(btv, btv->mute); /* trigger automute */
4460 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4461 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4462 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4463 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4464 btread(BT848_RISC_COUNT));
4465 bttv_print_irqbits(stat,astat);
4468 bttv_print_riscaddr(btv);
4470 if (fdsr && astat & BT848_INT_FDSR) {
4471 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4472 btv->c.nr,btread(BT848_RISC_COUNT));
4474 bttv_print_riscaddr(btv);
4480 if (count > 8 || !(astat & BT848_INT_GPINT)) {
4481 btwrite(0, BT848_INT_MASK);
4484 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4487 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4489 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4493 bttv_print_irqbits(stat,astat);
4501 return IRQ_RETVAL(handled);
4505 /* ----------------------------------------------------------------------- */
4506 /* initialitation */
4508 static struct video_device *vdev_init(struct bttv *btv,
4509 struct video_device *template,
4512 struct video_device *vfd;
4514 vfd = video_device_alloc();
4519 vfd->dev = &btv->c.pci->dev;
4520 vfd->release = video_device_release;
4521 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4522 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4523 type, bttv_tvcards[btv->c.type].name);
4527 static void bttv_unregister_video(struct bttv *btv)
4529 if (btv->video_dev) {
4530 if (-1 != btv->video_dev->minor)
4531 video_unregister_device(btv->video_dev);
4533 video_device_release(btv->video_dev);
4534 btv->video_dev = NULL;
4537 if (-1 != btv->vbi_dev->minor)
4538 video_unregister_device(btv->vbi_dev);
4540 video_device_release(btv->vbi_dev);
4541 btv->vbi_dev = NULL;
4543 if (btv->radio_dev) {
4544 if (-1 != btv->radio_dev->minor)
4545 video_unregister_device(btv->radio_dev);
4547 video_device_release(btv->radio_dev);
4548 btv->radio_dev = NULL;
4552 /* register video4linux devices */
4553 static int __devinit bttv_register_video(struct bttv *btv)
4555 if (no_overlay <= 0) {
4556 bttv_video_template.type |= VID_TYPE_OVERLAY;
4558 printk("bttv: Overlay support disabled.\n");
4562 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4563 if (NULL == btv->video_dev)
4565 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
4567 printk(KERN_INFO "bttv%d: registered device video%d\n",
4568 btv->c.nr,btv->video_dev->minor & 0x1f);
4569 if (class_device_create_file(&btv->video_dev->class_dev,
4570 &class_device_attr_card)<0) {
4571 printk(KERN_ERR "bttv%d: class_device_create_file 'card' "
4572 "failed\n", btv->c.nr);
4577 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
4578 if (NULL == btv->vbi_dev)
4580 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
4582 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
4583 btv->c.nr,btv->vbi_dev->minor & 0x1f);
4585 if (!btv->has_radio)
4588 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4589 if (NULL == btv->radio_dev)
4591 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
4593 printk(KERN_INFO "bttv%d: registered device radio%d\n",
4594 btv->c.nr,btv->radio_dev->minor & 0x1f);
4600 bttv_unregister_video(btv);
4605 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4606 /* response on cards with no firmware is not enabled by OF */
4607 static void pci_set_command(struct pci_dev *dev)
4609 #if defined(__powerpc__)
4612 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4613 cmd = (cmd | PCI_COMMAND_MEMORY );
4614 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4618 static int __devinit bttv_probe(struct pci_dev *dev,
4619 const struct pci_device_id *pci_id)
4625 if (bttv_num == BTTV_MAX)
4627 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
4628 btv=&bttvs[bttv_num];
4629 memset(btv,0,sizeof(*btv));
4630 btv->c.nr = bttv_num;
4631 sprintf(btv->c.name,"bttv%d",btv->c.nr);
4633 /* initialize structs / fill in defaults */
4634 mutex_init(&btv->lock);
4635 spin_lock_init(&btv->s_lock);
4636 spin_lock_init(&btv->gpio_lock);
4637 init_waitqueue_head(&btv->gpioq);
4638 init_waitqueue_head(&btv->i2c_queue);
4639 INIT_LIST_HEAD(&btv->c.subs);
4640 INIT_LIST_HEAD(&btv->capture);
4641 INIT_LIST_HEAD(&btv->vcapture);
4642 v4l2_prio_init(&btv->prio);
4644 init_timer(&btv->timeout);
4645 btv->timeout.function = bttv_irq_timeout;
4646 btv->timeout.data = (unsigned long)btv;
4649 btv->tuner_type = UNSET;
4650 btv->new_input = UNSET;
4651 btv->has_radio=radio[btv->c.nr];
4653 /* pci stuff (init, get irq/mmio, ... */
4655 btv->id = dev->device;
4656 if (pci_enable_device(dev)) {
4657 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4661 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
4662 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4666 if (!request_mem_region(pci_resource_start(dev,0),
4667 pci_resource_len(dev,0),
4669 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4671 (unsigned long long)pci_resource_start(dev,0));
4674 pci_set_master(dev);
4675 pci_set_command(dev);
4676 pci_set_drvdata(dev,btv);
4678 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4679 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4680 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4681 bttv_num,btv->id, btv->revision, pci_name(dev));
4682 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4683 btv->c.pci->irq, lat,
4684 (unsigned long long)pci_resource_start(dev,0));
4687 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4688 if (NULL == btv->bt848_mmio) {
4689 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4697 /* disable irqs, register irq handler */
4698 btwrite(0, BT848_INT_MASK);
4699 result = request_irq(btv->c.pci->irq, bttv_irq,
4700 IRQF_SHARED | IRQF_DISABLED,btv->c.name,(void *)btv);
4702 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4703 bttv_num,btv->c.pci->irq);
4707 if (0 != bttv_handle_chipset(btv)) {
4712 /* init options from insmod args */
4713 btv->opt_combfilter = combfilter;
4714 btv->opt_lumafilter = lumafilter;
4715 btv->opt_automute = automute;
4716 btv->opt_chroma_agc = chroma_agc;
4717 btv->opt_adc_crush = adc_crush;
4718 btv->opt_vcr_hack = vcr_hack;
4719 btv->opt_whitecrush_upper = whitecrush_upper;
4720 btv->opt_whitecrush_lower = whitecrush_lower;
4721 btv->opt_uv_ratio = uv_ratio;
4722 btv->opt_full_luma_range = full_luma_range;
4723 btv->opt_coring = coring;
4725 /* fill struct bttv with some useful defaults */
4726 btv->init.btv = btv;
4727 btv->init.ov.w.width = 320;
4728 btv->init.ov.w.height = 240;
4729 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4730 btv->init.width = 320;
4731 btv->init.height = 240;
4734 /* initialize hardware */
4736 bttv_gpio_tracking(btv,"pre-init");
4738 bttv_risc_init_main(btv);
4742 btwrite(0x00, BT848_GPIO_REG_INP);
4743 btwrite(0x00, BT848_GPIO_OUT_EN);
4745 bttv_gpio_tracking(btv,"init");
4747 /* needs to be done before i2c is registered */
4748 bttv_init_card1(btv);
4750 /* register i2c + gpio */
4753 /* some card-specific stuff (needs working i2c) */
4754 bttv_init_card2(btv);
4757 /* register video4linux + input */
4758 if (!bttv_tvcards[btv->c.type].no_video) {
4759 bttv_register_video(btv);
4760 bt848_bright(btv,32768);
4761 bt848_contrast(btv,32768);
4762 bt848_hue(btv,32768);
4763 bt848_sat(btv,32768);
4766 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4767 btv->crop[1] = btv->crop[0]; /* current = default */
4768 disclaim_vbi_lines(btv);
4769 disclaim_video_lines(btv);
4772 /* add subdevices */
4773 if (bttv_tvcards[btv->c.type].has_dvb)
4774 bttv_sub_add_device(&btv->c, "dvb");
4776 bttv_input_init(btv);
4778 /* everything is fine */
4783 free_irq(btv->c.pci->irq,btv);
4786 if (btv->bt848_mmio)
4787 iounmap(btv->bt848_mmio);
4788 release_mem_region(pci_resource_start(btv->c.pci,0),
4789 pci_resource_len(btv->c.pci,0));
4790 pci_set_drvdata(dev,NULL);
4794 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4796 struct bttv *btv = pci_get_drvdata(pci_dev);
4799 printk("bttv%d: unloading\n",btv->c.nr);
4801 /* shutdown everything (DMA+IRQs) */
4802 btand(~15, BT848_GPIO_DMA_CTL);
4803 btwrite(0, BT848_INT_MASK);
4804 btwrite(~0x0, BT848_INT_STAT);
4805 btwrite(0x0, BT848_GPIO_OUT_EN);
4807 bttv_gpio_tracking(btv,"cleanup");
4809 /* tell gpio modules we are leaving ... */
4811 wake_up(&btv->gpioq);
4812 bttv_input_fini(btv);
4813 bttv_sub_del_devices(&btv->c);
4815 /* unregister i2c_bus + input */
4818 /* unregister video4linux */
4819 bttv_unregister_video(btv);
4821 /* free allocated memory */
4822 btcx_riscmem_free(btv->c.pci,&btv->main);
4824 /* free ressources */
4825 free_irq(btv->c.pci->irq,btv);
4826 iounmap(btv->bt848_mmio);
4827 release_mem_region(pci_resource_start(btv->c.pci,0),
4828 pci_resource_len(btv->c.pci,0));
4830 pci_set_drvdata(pci_dev, NULL);
4835 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4837 struct bttv *btv = pci_get_drvdata(pci_dev);
4838 struct bttv_buffer_set idle;
4839 unsigned long flags;
4841 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4843 /* stop dma + irqs */
4844 spin_lock_irqsave(&btv->s_lock,flags);
4845 memset(&idle, 0, sizeof(idle));
4846 btv->state.video = btv->curr;
4847 btv->state.vbi = btv->cvbi;
4848 btv->state.loop_irq = btv->loop_irq;
4851 bttv_buffer_activate_video(btv, &idle);
4852 bttv_buffer_activate_vbi(btv, NULL);
4853 bttv_set_dma(btv, 0);
4854 btwrite(0, BT848_INT_MASK);
4855 spin_unlock_irqrestore(&btv->s_lock,flags);
4857 /* save bt878 state */
4858 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4859 btv->state.gpio_data = gpio_read();
4861 /* save pci state */
4862 pci_save_state(pci_dev);
4863 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4864 pci_disable_device(pci_dev);
4865 btv->state.disabled = 1;
4870 static int bttv_resume(struct pci_dev *pci_dev)
4872 struct bttv *btv = pci_get_drvdata(pci_dev);
4873 unsigned long flags;
4876 dprintk("bttv%d: resume\n", btv->c.nr);
4878 /* restore pci state */
4879 if (btv->state.disabled) {
4880 err=pci_enable_device(pci_dev);
4882 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4886 btv->state.disabled = 0;
4888 err=pci_set_power_state(pci_dev, PCI_D0);
4890 pci_disable_device(pci_dev);
4891 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4893 btv->state.disabled = 1;
4897 pci_restore_state(pci_dev);
4899 /* restore bt878 state */
4900 bttv_reinit_bt848(btv);
4901 gpio_inout(0xffffff, btv->state.gpio_enable);
4902 gpio_write(btv->state.gpio_data);
4905 spin_lock_irqsave(&btv->s_lock,flags);
4906 btv->curr = btv->state.video;
4907 btv->cvbi = btv->state.vbi;
4908 btv->loop_irq = btv->state.loop_irq;
4909 bttv_buffer_activate_video(btv, &btv->curr);
4910 bttv_buffer_activate_vbi(btv, btv->cvbi);
4911 bttv_set_dma(btv, 0);
4912 spin_unlock_irqrestore(&btv->s_lock,flags);
4917 static struct pci_device_id bttv_pci_tbl[] = {
4918 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4919 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4920 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4921 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4922 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4923 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4924 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4925 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4929 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4931 static struct pci_driver bttv_pci_driver = {
4933 .id_table = bttv_pci_tbl,
4934 .probe = bttv_probe,
4935 .remove = __devexit_p(bttv_remove),
4937 .suspend = bttv_suspend,
4938 .resume = bttv_resume,
4942 static int bttv_init_module(void)
4948 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4949 (BTTV_VERSION_CODE >> 16) & 0xff,
4950 (BTTV_VERSION_CODE >> 8) & 0xff,
4951 BTTV_VERSION_CODE & 0xff);
4953 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4954 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4956 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4958 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4959 gbufsize = BTTV_MAX_FBUF;
4960 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4962 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4963 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4965 bttv_check_chipset();
4967 ret = bus_register(&bttv_sub_bus_type);
4969 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4972 return pci_register_driver(&bttv_pci_driver);
4975 static void bttv_cleanup_module(void)
4977 pci_unregister_driver(&bttv_pci_driver);
4978 bus_unregister(&bttv_sub_bus_type);
4982 module_init(bttv_init_module);
4983 module_exit(bttv_cleanup_module);