3 bttv - Bt848 frame grabber driver
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
6 & Marcus Metzler <mocm@thp.uni-koeln.de>
7 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
12 Cropping and overscan support
13 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
14 Sponsored by OPQ Systems AB
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/interrupt.h>
39 #include <linux/kdev_t.h>
41 #include <media/v4l2-common.h>
42 #include <media/tvaudio.h>
43 #include <media/msp3400.h>
45 #include <linux/dma-mapping.h>
48 #include <asm/byteorder.h>
50 #include <media/rds.h>
53 unsigned int bttv_num; /* number of Bt848s in use */
54 struct bttv bttvs[BTTV_MAX];
56 unsigned int bttv_debug;
57 unsigned int bttv_verbose = 1;
58 unsigned int bttv_gpio;
60 /* config variables */
62 static unsigned int bigendian=1;
64 static unsigned int bigendian;
66 static unsigned int radio[BTTV_MAX];
67 static unsigned int irq_debug;
68 static unsigned int gbuffers = 8;
69 static unsigned int gbufsize = 0x208000;
70 static unsigned int reset_crop = 1;
72 static int video_nr = -1;
73 static int radio_nr = -1;
74 static int vbi_nr = -1;
75 static int debug_latency;
77 static unsigned int fdsr;
80 static unsigned int combfilter;
81 static unsigned int lumafilter;
82 static unsigned int automute = 1;
83 static unsigned int chroma_agc;
84 static unsigned int adc_crush = 1;
85 static unsigned int whitecrush_upper = 0xCF;
86 static unsigned int whitecrush_lower = 0x7F;
87 static unsigned int vcr_hack;
88 static unsigned int irq_iswitch;
89 static unsigned int uv_ratio = 50;
90 static unsigned int full_luma_range;
91 static unsigned int coring;
92 extern int no_overlay;
94 /* API features (turn on/off stuff for testing) */
95 static unsigned int v4l2 = 1;
98 module_param(bttv_verbose, int, 0644);
99 module_param(bttv_gpio, int, 0644);
100 module_param(bttv_debug, int, 0644);
101 module_param(irq_debug, int, 0644);
102 module_param(debug_latency, int, 0644);
104 module_param(fdsr, int, 0444);
105 module_param(video_nr, int, 0444);
106 module_param(radio_nr, int, 0444);
107 module_param(vbi_nr, int, 0444);
108 module_param(gbuffers, int, 0444);
109 module_param(gbufsize, int, 0444);
110 module_param(reset_crop, int, 0444);
112 module_param(v4l2, int, 0644);
113 module_param(bigendian, int, 0644);
114 module_param(irq_iswitch, int, 0644);
115 module_param(combfilter, int, 0444);
116 module_param(lumafilter, int, 0444);
117 module_param(automute, int, 0444);
118 module_param(chroma_agc, int, 0444);
119 module_param(adc_crush, int, 0444);
120 module_param(whitecrush_upper, int, 0444);
121 module_param(whitecrush_lower, int, 0444);
122 module_param(vcr_hack, int, 0444);
123 module_param(uv_ratio, int, 0444);
124 module_param(full_luma_range, int, 0444);
125 module_param(coring, int, 0444);
127 module_param_array(radio, int, NULL, 0444);
129 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
130 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
131 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
132 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
133 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
134 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
135 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
136 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
137 MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
138 "is 1 (yes) for compatibility with older applications");
139 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
140 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
141 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
142 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
143 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
144 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
145 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
146 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
147 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
148 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
150 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
151 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
152 MODULE_LICENSE("GPL");
154 /* ----------------------------------------------------------------------- */
157 static ssize_t show_card(struct device *cd,
158 struct device_attribute *attr, 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 DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
166 /* ----------------------------------------------------------------------- */
167 /* dvb auto-load setup */
168 #if defined(CONFIG_MODULES) && defined(MODULE)
169 static void request_module_async(struct work_struct *work)
171 request_module("dvb-bt8xx");
174 static void request_modules(struct bttv *dev)
176 INIT_WORK(&dev->request_module_wk, request_module_async);
177 schedule_work(&dev->request_module_wk);
180 #define request_modules(dev)
181 #endif /* CONFIG_MODULES */
184 /* ----------------------------------------------------------------------- */
187 /* special timing tables from conexant... */
188 static u8 SRAM_Table[][60] =
190 /* PAL digital input over GPIO[7:0] */
192 45, // 45 bytes following
193 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
194 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
195 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
196 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
197 0x37,0x00,0xAF,0x21,0x00
199 /* NTSC digital input over GPIO[7:0] */
201 51, // 51 bytes following
202 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
203 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
204 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
205 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
206 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
209 // TGB_NTSC392 // quartzsight
210 // This table has been modified to be used for Fusion Rev D
212 0x2A, // size of table = 42
213 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
214 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
215 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
216 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
221 /* minhdelayx1 first video pixel we can capture on a line and
222 hdelayx1 start of active video, both relative to rising edge of
223 /HRESET pulse (0H) in 1 / fCLKx1.
224 swidth width of active video and
225 totalwidth total line width, both in 1 / fCLKx1.
226 sqwidth total line width in square pixels.
227 vdelay start of active video in 2 * field lines relative to
228 trailing edge of /VRESET pulse (VDELAY register).
229 sheight height of active video in 2 * field lines.
230 videostart0 ITU-R frame line number of the line corresponding
231 to vdelay in the first field. */
232 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
233 vdelay, sheight, videostart0) \
234 .cropcap.bounds.left = minhdelayx1, \
235 /* * 2 because vertically we count field lines times two, */ \
236 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
237 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
238 /* 4 is a safety margin at the end of the line. */ \
239 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
240 .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
241 .cropcap.defrect.left = hdelayx1, \
242 .cropcap.defrect.top = (videostart0) * 2, \
243 .cropcap.defrect.width = swidth, \
244 .cropcap.defrect.height = sheight, \
245 .cropcap.pixelaspect.numerator = totalwidth, \
246 .cropcap.pixelaspect.denominator = sqwidth,
248 const struct bttv_tvnorm bttv_tvnorms[] = {
250 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
251 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
253 .v4l2_id = V4L2_STD_PAL,
261 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
262 .scaledtwidth = 1135,
266 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
268 /* ITU-R frame line number of the first VBI line
269 we can capture, of the first and second field.
270 The last line is determined by cropcap.bounds. */
271 .vbistart = { 7, 320 },
272 CROPCAP(/* minhdelayx1 */ 68,
274 /* Should be (768 * 1135 + 944 / 2) / 944.
275 cropcap.defrect is used for image width
276 checks, so we keep the old value 924. */
278 /* totalwidth */ 1135,
282 /* videostart0 */ 23)
283 /* bt878 (and bt848?) can capture another
284 line below active video. */
285 .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
287 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
295 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
300 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
302 .vbistart = { 10, 273 },
303 CROPCAP(/* minhdelayx1 */ 68,
305 /* Should be (640 * 910 + 780 / 2) / 780? */
307 /* totalwidth */ 910,
311 /* videostart0 */ 23)
313 .v4l2_id = V4L2_STD_SECAM,
321 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
322 .scaledtwidth = 1135,
327 .sram = 0, /* like PAL, correct? */
328 .vbistart = { 7, 320 },
329 CROPCAP(/* minhdelayx1 */ 68,
332 /* totalwidth */ 1135,
336 /* videostart0 */ 23)
338 .v4l2_id = V4L2_STD_PAL_Nc,
346 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
353 .vbistart = { 7, 320 },
354 CROPCAP(/* minhdelayx1 */ 68,
356 /* swidth */ (640 * 910 + 780 / 2) / 780,
357 /* totalwidth */ 910,
361 /* videostart0 */ 23)
363 .v4l2_id = V4L2_STD_PAL_M,
371 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
378 .vbistart = { 10, 273 },
379 CROPCAP(/* minhdelayx1 */ 68,
381 /* swidth */ (640 * 910 + 780 / 2) / 780,
382 /* totalwidth */ 910,
386 /* videostart0 */ 23)
388 .v4l2_id = V4L2_STD_PAL_N,
396 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
403 .vbistart = { 7, 320 },
404 CROPCAP(/* minhdelayx1 */ 68,
406 /* swidth */ (768 * 1135 + 944 / 2) / 944,
407 /* totalwidth */ 1135,
411 /* videostart0 */ 23)
413 .v4l2_id = V4L2_STD_NTSC_M_JP,
421 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
428 .vbistart = { 10, 273 },
429 CROPCAP(/* minhdelayx1 */ 68,
431 /* swidth */ (640 * 910 + 780 / 2) / 780,
432 /* totalwidth */ 910,
436 /* videostart0 */ 23)
438 /* that one hopefully works with the strange timing
439 * which video recorders produce when playing a NTSC
440 * tape on a PAL TV ... */
441 .v4l2_id = V4L2_STD_PAL_60,
449 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
450 .scaledtwidth = 1135,
457 .vbistart = { 10, 273 },
458 CROPCAP(/* minhdelayx1 */ 68,
461 /* totalwidth */ 1135,
465 /* videostart0 */ 23)
468 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
470 /* ----------------------------------------------------------------------- */
472 packed pixel formats must come first */
473 static const struct bttv_format bttv_formats[] = {
475 .name = "8 bpp, gray",
476 .palette = VIDEO_PALETTE_GREY,
477 .fourcc = V4L2_PIX_FMT_GREY,
478 .btformat = BT848_COLOR_FMT_Y8,
480 .flags = FORMAT_FLAGS_PACKED,
482 .name = "8 bpp, dithered color",
483 .palette = VIDEO_PALETTE_HI240,
484 .fourcc = V4L2_PIX_FMT_HI240,
485 .btformat = BT848_COLOR_FMT_RGB8,
487 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
489 .name = "15 bpp RGB, le",
490 .palette = VIDEO_PALETTE_RGB555,
491 .fourcc = V4L2_PIX_FMT_RGB555,
492 .btformat = BT848_COLOR_FMT_RGB15,
494 .flags = FORMAT_FLAGS_PACKED,
496 .name = "15 bpp RGB, be",
498 .fourcc = V4L2_PIX_FMT_RGB555X,
499 .btformat = BT848_COLOR_FMT_RGB15,
500 .btswap = 0x03, /* byteswap */
502 .flags = FORMAT_FLAGS_PACKED,
504 .name = "16 bpp RGB, le",
505 .palette = VIDEO_PALETTE_RGB565,
506 .fourcc = V4L2_PIX_FMT_RGB565,
507 .btformat = BT848_COLOR_FMT_RGB16,
509 .flags = FORMAT_FLAGS_PACKED,
511 .name = "16 bpp RGB, be",
513 .fourcc = V4L2_PIX_FMT_RGB565X,
514 .btformat = BT848_COLOR_FMT_RGB16,
515 .btswap = 0x03, /* byteswap */
517 .flags = FORMAT_FLAGS_PACKED,
519 .name = "24 bpp RGB, le",
520 .palette = VIDEO_PALETTE_RGB24,
521 .fourcc = V4L2_PIX_FMT_BGR24,
522 .btformat = BT848_COLOR_FMT_RGB24,
524 .flags = FORMAT_FLAGS_PACKED,
526 .name = "32 bpp RGB, le",
527 .palette = VIDEO_PALETTE_RGB32,
528 .fourcc = V4L2_PIX_FMT_BGR32,
529 .btformat = BT848_COLOR_FMT_RGB32,
531 .flags = FORMAT_FLAGS_PACKED,
533 .name = "32 bpp RGB, be",
535 .fourcc = V4L2_PIX_FMT_RGB32,
536 .btformat = BT848_COLOR_FMT_RGB32,
537 .btswap = 0x0f, /* byte+word swap */
539 .flags = FORMAT_FLAGS_PACKED,
541 .name = "4:2:2, packed, YUYV",
542 .palette = VIDEO_PALETTE_YUV422,
543 .fourcc = V4L2_PIX_FMT_YUYV,
544 .btformat = BT848_COLOR_FMT_YUY2,
546 .flags = FORMAT_FLAGS_PACKED,
548 .name = "4:2:2, packed, YUYV",
549 .palette = VIDEO_PALETTE_YUYV,
550 .fourcc = V4L2_PIX_FMT_YUYV,
551 .btformat = BT848_COLOR_FMT_YUY2,
553 .flags = FORMAT_FLAGS_PACKED,
555 .name = "4:2:2, packed, UYVY",
556 .palette = VIDEO_PALETTE_UYVY,
557 .fourcc = V4L2_PIX_FMT_UYVY,
558 .btformat = BT848_COLOR_FMT_YUY2,
559 .btswap = 0x03, /* byteswap */
561 .flags = FORMAT_FLAGS_PACKED,
563 .name = "4:2:2, planar, Y-Cb-Cr",
564 .palette = VIDEO_PALETTE_YUV422P,
565 .fourcc = V4L2_PIX_FMT_YUV422P,
566 .btformat = BT848_COLOR_FMT_YCrCb422,
568 .flags = FORMAT_FLAGS_PLANAR,
572 .name = "4:2:0, planar, Y-Cb-Cr",
573 .palette = VIDEO_PALETTE_YUV420P,
574 .fourcc = V4L2_PIX_FMT_YUV420,
575 .btformat = BT848_COLOR_FMT_YCrCb422,
577 .flags = FORMAT_FLAGS_PLANAR,
581 .name = "4:2:0, planar, Y-Cr-Cb",
583 .fourcc = V4L2_PIX_FMT_YVU420,
584 .btformat = BT848_COLOR_FMT_YCrCb422,
586 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
590 .name = "4:1:1, planar, Y-Cb-Cr",
591 .palette = VIDEO_PALETTE_YUV411P,
592 .fourcc = V4L2_PIX_FMT_YUV411P,
593 .btformat = BT848_COLOR_FMT_YCrCb411,
595 .flags = FORMAT_FLAGS_PLANAR,
599 .name = "4:1:0, planar, Y-Cb-Cr",
600 .palette = VIDEO_PALETTE_YUV410P,
601 .fourcc = V4L2_PIX_FMT_YUV410,
602 .btformat = BT848_COLOR_FMT_YCrCb411,
604 .flags = FORMAT_FLAGS_PLANAR,
608 .name = "4:1:0, planar, Y-Cr-Cb",
610 .fourcc = V4L2_PIX_FMT_YVU410,
611 .btformat = BT848_COLOR_FMT_YCrCb411,
613 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
617 .name = "raw scanlines",
618 .palette = VIDEO_PALETTE_RAW,
620 .btformat = BT848_COLOR_FMT_RAW,
622 .flags = FORMAT_FLAGS_RAW,
625 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
627 /* ----------------------------------------------------------------------- */
629 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
630 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
631 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
632 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
633 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
634 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
635 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
636 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
637 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
638 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
639 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
640 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
642 static const struct v4l2_queryctrl no_ctl = {
644 .flags = V4L2_CTRL_FLAG_DISABLED,
646 static const struct v4l2_queryctrl bttv_ctls[] = {
649 .id = V4L2_CID_BRIGHTNESS,
650 .name = "Brightness",
654 .default_value = 32768,
655 .type = V4L2_CTRL_TYPE_INTEGER,
657 .id = V4L2_CID_CONTRAST,
662 .default_value = 32768,
663 .type = V4L2_CTRL_TYPE_INTEGER,
665 .id = V4L2_CID_SATURATION,
666 .name = "Saturation",
670 .default_value = 32768,
671 .type = V4L2_CTRL_TYPE_INTEGER,
678 .default_value = 32768,
679 .type = V4L2_CTRL_TYPE_INTEGER,
683 .id = V4L2_CID_AUDIO_MUTE,
687 .type = V4L2_CTRL_TYPE_BOOLEAN,
689 .id = V4L2_CID_AUDIO_VOLUME,
694 .default_value = 65535,
695 .type = V4L2_CTRL_TYPE_INTEGER,
697 .id = V4L2_CID_AUDIO_BALANCE,
702 .default_value = 32768,
703 .type = V4L2_CTRL_TYPE_INTEGER,
705 .id = V4L2_CID_AUDIO_BASS,
710 .default_value = 32768,
711 .type = V4L2_CTRL_TYPE_INTEGER,
713 .id = V4L2_CID_AUDIO_TREBLE,
718 .default_value = 32768,
719 .type = V4L2_CTRL_TYPE_INTEGER,
721 /* --- private --- */
723 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
724 .name = "chroma agc",
727 .type = V4L2_CTRL_TYPE_BOOLEAN,
729 .id = V4L2_CID_PRIVATE_COMBFILTER,
730 .name = "combfilter",
733 .type = V4L2_CTRL_TYPE_BOOLEAN,
735 .id = V4L2_CID_PRIVATE_AUTOMUTE,
739 .type = V4L2_CTRL_TYPE_BOOLEAN,
741 .id = V4L2_CID_PRIVATE_LUMAFILTER,
742 .name = "luma decimation filter",
745 .type = V4L2_CTRL_TYPE_BOOLEAN,
747 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
751 .type = V4L2_CTRL_TYPE_BOOLEAN,
753 .id = V4L2_CID_PRIVATE_VCR_HACK,
757 .type = V4L2_CTRL_TYPE_BOOLEAN,
759 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
760 .name = "whitecrush upper",
764 .default_value = 0xCF,
765 .type = V4L2_CTRL_TYPE_INTEGER,
767 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
768 .name = "whitecrush lower",
772 .default_value = 0x7F,
773 .type = V4L2_CTRL_TYPE_INTEGER,
775 .id = V4L2_CID_PRIVATE_UV_RATIO,
781 .type = V4L2_CTRL_TYPE_INTEGER,
783 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
784 .name = "full luma range",
787 .type = V4L2_CTRL_TYPE_BOOLEAN,
789 .id = V4L2_CID_PRIVATE_CORING,
795 .type = V4L2_CTRL_TYPE_INTEGER,
801 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
803 /* ----------------------------------------------------------------------- */
804 /* resource management */
807 RESOURCE_ allocated by freed by
809 VIDEO_READ bttv_read 1) bttv_read 2)
811 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
812 VIDIOC_QBUF 1) bttv_release
815 OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
816 VIDIOC_OVERLAY on VIDIOC_OVERLAY off
819 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
820 VIDIOC_QBUF 1) bttv_release
821 bttv_read, bttv_poll 1) 4)
823 1) The resource must be allocated when we enter buffer prepare functions
824 and remain allocated while buffers are in the DMA queue.
825 2) This is a single frame read.
826 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
827 RESOURCE_OVERLAY is allocated.
828 4) This is a continuous read, implies VIDIOC_STREAMON.
830 Note this driver permits video input and standard changes regardless if
831 resources are allocated.
834 #define VBI_RESOURCES (RESOURCE_VBI)
835 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
836 RESOURCE_VIDEO_STREAM | \
840 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
842 int xbits; /* mutual exclusive resources */
844 if (fh->resources & bit)
845 /* have it already allocated */
849 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
850 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
853 mutex_lock(&btv->lock);
854 if (btv->resources & xbits) {
855 /* no, someone else uses it */
859 if ((bit & VIDEO_RESOURCES)
860 && 0 == (btv->resources & VIDEO_RESOURCES)) {
861 /* Do crop - use current, don't - use default parameters. */
862 __s32 top = btv->crop[!!fh->do_crop].rect.top;
864 if (btv->vbi_end > top)
867 /* We cannot capture the same line as video and VBI data.
868 Claim scan lines crop[].rect.top to bottom. */
869 btv->crop_start = top;
870 } else if (bit & VBI_RESOURCES) {
871 __s32 end = fh->vbi_fmt.end;
873 if (end > btv->crop_start)
876 /* Claim scan lines above fh->vbi_fmt.end. */
880 /* it's free, grab it */
881 fh->resources |= bit;
882 btv->resources |= bit;
883 mutex_unlock(&btv->lock);
887 mutex_unlock(&btv->lock);
892 int check_btres(struct bttv_fh *fh, int bit)
894 return (fh->resources & bit);
898 int locked_btres(struct bttv *btv, int bit)
900 return (btv->resources & bit);
903 /* Call with btv->lock down. */
905 disclaim_vbi_lines(struct bttv *btv)
910 /* Call with btv->lock down. */
912 disclaim_video_lines(struct bttv *btv)
914 const struct bttv_tvnorm *tvnorm;
917 tvnorm = &bttv_tvnorms[btv->tvnorm];
918 btv->crop_start = tvnorm->cropcap.bounds.top
919 + tvnorm->cropcap.bounds.height;
921 /* VBI capturing ends at VDELAY, start of video capturing, no
922 matter how many lines the VBI RISC program expects. When video
923 capturing is off, it shall no longer "preempt" VBI capturing,
924 so we set VDELAY to maximum. */
925 crop = btread(BT848_E_CROP) | 0xc0;
926 btwrite(crop, BT848_E_CROP);
927 btwrite(0xfe, BT848_E_VDELAY_LO);
928 btwrite(crop, BT848_O_CROP);
929 btwrite(0xfe, BT848_O_VDELAY_LO);
933 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
935 if ((fh->resources & bits) != bits) {
936 /* trying to free ressources not allocated by us ... */
937 printk("bttv: BUG! (btres)\n");
939 mutex_lock(&btv->lock);
940 fh->resources &= ~bits;
941 btv->resources &= ~bits;
943 bits = btv->resources;
945 if (0 == (bits & VIDEO_RESOURCES))
946 disclaim_video_lines(btv);
948 if (0 == (bits & VBI_RESOURCES))
949 disclaim_vbi_lines(btv);
951 mutex_unlock(&btv->lock);
954 /* ----------------------------------------------------------------------- */
955 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
957 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
958 PLL_X = Reference pre-divider (0=1, 1=2)
959 PLL_C = Post divider (0=6, 1=4)
960 PLL_I = Integer input
961 PLL_F = Fractional input
963 F_input = 28.636363 MHz:
964 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
967 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
969 unsigned char fl, fh, fi;
971 /* prevent overflows */
984 btwrite(fl, BT848_PLL_F_LO);
985 btwrite(fh, BT848_PLL_F_HI);
986 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
989 static void set_pll(struct bttv *btv)
993 if (!btv->pll.pll_crystal)
996 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
997 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
1001 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1003 if (btv->pll.pll_current == 0)
1005 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
1006 btv->c.nr,btv->pll.pll_ifreq);
1007 btwrite(0x00,BT848_TGCTRL);
1008 btwrite(0x00,BT848_PLL_XCI);
1009 btv->pll.pll_current = 0;
1013 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
1014 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1015 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1017 for (i=0; i<10; i++) {
1018 /* Let other people run while the PLL stabilizes */
1022 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
1023 btwrite(0,BT848_DSTATUS);
1025 btwrite(0x08,BT848_TGCTRL);
1026 btv->pll.pll_current = btv->pll.pll_ofreq;
1027 bttv_printk(" ok\n");
1031 btv->pll.pll_current = -1;
1032 bttv_printk("failed\n");
1036 /* used to switch between the bt848's analog/digital video capture modes */
1037 static void bt848A_set_timing(struct bttv *btv)
1040 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
1041 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
1043 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
1044 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
1045 btv->c.nr,table_idx);
1047 /* timing change...reset timing generator address */
1048 btwrite(0x00, BT848_TGCTRL);
1049 btwrite(0x02, BT848_TGCTRL);
1050 btwrite(0x00, BT848_TGCTRL);
1052 len=SRAM_Table[table_idx][0];
1053 for(i = 1; i <= len; i++)
1054 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
1055 btv->pll.pll_ofreq = 27000000;
1058 btwrite(0x11, BT848_TGCTRL);
1059 btwrite(0x41, BT848_DVSIF);
1061 btv->pll.pll_ofreq = fsc;
1063 btwrite(0x0, BT848_DVSIF);
1067 /* ----------------------------------------------------------------------- */
1069 static void bt848_bright(struct bttv *btv, int bright)
1073 // printk("bttv: set bright: %d\n",bright); // DEBUG
1074 btv->bright = bright;
1076 /* We want -128 to 127 we get 0-65535 */
1077 value = (bright >> 8) - 128;
1078 btwrite(value & 0xff, BT848_BRIGHT);
1081 static void bt848_hue(struct bttv *btv, int hue)
1088 value = (hue >> 8) - 128;
1089 btwrite(value & 0xff, BT848_HUE);
1092 static void bt848_contrast(struct bttv *btv, int cont)
1096 btv->contrast = cont;
1099 value = (cont >> 7);
1100 hibit = (value >> 6) & 4;
1101 btwrite(value & 0xff, BT848_CONTRAST_LO);
1102 btaor(hibit, ~4, BT848_E_CONTROL);
1103 btaor(hibit, ~4, BT848_O_CONTROL);
1106 static void bt848_sat(struct bttv *btv, int color)
1108 int val_u,val_v,hibits;
1110 btv->saturation = color;
1112 /* 0-511 for the color */
1113 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
1114 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
1115 hibits = (val_u >> 7) & 2;
1116 hibits |= (val_v >> 8) & 1;
1117 btwrite(val_u & 0xff, BT848_SAT_U_LO);
1118 btwrite(val_v & 0xff, BT848_SAT_V_LO);
1119 btaor(hibits, ~3, BT848_E_CONTROL);
1120 btaor(hibits, ~3, BT848_O_CONTROL);
1123 /* ----------------------------------------------------------------------- */
1126 video_mux(struct bttv *btv, unsigned int input)
1130 if (input >= bttv_tvcards[btv->c.type].video_inputs)
1133 /* needed by RemoteVideo MX */
1134 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
1136 gpio_inout(mask2,mask2);
1138 if (input == btv->svhs) {
1139 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1140 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1142 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1143 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1145 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
1146 btaor(mux<<5, ~(3<<5), BT848_IFORM);
1147 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
1148 btv->c.nr,input,mux);
1150 /* card specific hook */
1151 if(bttv_tvcards[btv->c.type].muxsel_hook)
1152 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
1156 static char *audio_modes[] = {
1157 "audio: tuner", "audio: radio", "audio: extern",
1158 "audio: intern", "audio: mute"
1162 audio_mux(struct bttv *btv, int input, int mute)
1164 int gpio_val, signal;
1165 struct v4l2_control ctrl;
1166 struct i2c_client *c;
1168 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
1169 bttv_tvcards[btv->c.type].gpiomask);
1170 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
1176 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
1179 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
1181 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
1183 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
1185 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
1189 ctrl.id = V4L2_CID_AUDIO_MUTE;
1190 ctrl.value = btv->mute;
1191 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
1192 c = btv->i2c_msp34xx_client;
1194 struct v4l2_routing route;
1196 /* Note: the inputs tuner/radio/extern/intern are translated
1197 to msp routings. This assumes common behavior for all msp3400
1198 based TV cards. When this assumption fails, then the
1199 specific MSP routing must be added to the card table.
1200 For now this is sufficient. */
1202 case TVAUDIO_INPUT_RADIO:
1203 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1204 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1206 case TVAUDIO_INPUT_EXTERN:
1207 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1208 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1210 case TVAUDIO_INPUT_INTERN:
1211 /* Yes, this is the same input as for RADIO. I doubt
1212 if this is ever used. The only board with an INTERN
1213 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1214 that was tested. My guess is that the whole INTERN
1215 input does not work. */
1216 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1217 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1219 case TVAUDIO_INPUT_TUNER:
1221 /* This is the only card that uses TUNER2, and afaik,
1222 is the only difference between the VOODOOTV_FM
1224 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1225 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1226 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1228 route.input = MSP_INPUT_DEFAULT;
1231 route.output = MSP_OUTPUT_DEFAULT;
1232 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1234 c = btv->i2c_tvaudio_client;
1236 struct v4l2_routing route;
1238 route.input = input;
1240 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1246 audio_mute(struct bttv *btv, int mute)
1248 return audio_mux(btv, btv->audio, mute);
1252 audio_input(struct bttv *btv, int input)
1254 return audio_mux(btv, input, btv->mute);
1258 i2c_vidiocschan(struct bttv *btv)
1260 v4l2_std_id std = bttv_tvnorms[btv->tvnorm].v4l2_id;
1262 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &std);
1263 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM || btv->c.type == BTTV_BOARD_VOODOOTV_200)
1264 bttv_tda9880_setnorm(btv,btv->tvnorm);
1268 bttv_crop_calc_limits(struct bttv_crop *c)
1270 /* Scale factor min. 1:1, max. 16:1. Min. image size
1271 48 x 32. Scaled width must be a multiple of 4. */
1274 /* For bug compatibility with VIDIOCGCAP and image
1275 size checks in earlier driver versions. */
1276 c->min_scaled_width = 48;
1277 c->min_scaled_height = 32;
1279 c->min_scaled_width =
1280 (max(48, c->rect.width >> 4) + 3) & ~3;
1281 c->min_scaled_height =
1282 max(32, c->rect.height >> 4);
1285 c->max_scaled_width = c->rect.width & ~3;
1286 c->max_scaled_height = c->rect.height;
1290 bttv_crop_reset(struct bttv_crop *c, int norm)
1292 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1293 bttv_crop_calc_limits(c);
1296 /* Call with btv->lock down. */
1298 set_tvnorm(struct bttv *btv, unsigned int norm)
1300 const struct bttv_tvnorm *tvnorm;
1302 if (norm < 0 || norm >= BTTV_TVNORMS)
1305 tvnorm = &bttv_tvnorms[norm];
1307 if (btv->tvnorm < 0 ||
1308 btv->tvnorm >= BTTV_TVNORMS ||
1309 0 != memcmp(&bttv_tvnorms[btv->tvnorm].cropcap,
1311 sizeof (tvnorm->cropcap))) {
1312 bttv_crop_reset(&btv->crop[0], norm);
1313 btv->crop[1] = btv->crop[0]; /* current = default */
1315 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1316 btv->crop_start = tvnorm->cropcap.bounds.top
1317 + tvnorm->cropcap.bounds.height;
1323 btwrite(tvnorm->adelay, BT848_ADELAY);
1324 btwrite(tvnorm->bdelay, BT848_BDELAY);
1325 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1327 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1328 btwrite(1, BT848_VBI_PACK_DEL);
1329 bt848A_set_timing(btv);
1331 switch (btv->c.type) {
1332 case BTTV_BOARD_VOODOOTV_FM:
1333 case BTTV_BOARD_VOODOOTV_200:
1334 bttv_tda9880_setnorm(btv,norm);
1340 /* Call with btv->lock down. */
1342 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1344 unsigned long flags;
1348 spin_lock_irqsave(&btv->s_lock,flags);
1349 if (btv->curr.frame_irq) {
1350 /* active capture -> delayed input switch */
1351 btv->new_input = input;
1353 video_mux(btv,input);
1355 spin_unlock_irqrestore(&btv->s_lock,flags);
1357 video_mux(btv,input);
1359 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1360 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
1361 set_tvnorm(btv, norm);
1362 i2c_vidiocschan(btv);
1365 static void init_irqreg(struct bttv *btv)
1368 btwrite(0xfffffUL, BT848_INT_STAT);
1370 if (bttv_tvcards[btv->c.type].no_video) {
1372 btwrite(BT848_INT_I2CDONE,
1376 btwrite((btv->triton1) |
1377 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1379 (fdsr ? BT848_INT_FDSR : 0) |
1380 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1381 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1387 static void init_bt848(struct bttv *btv)
1391 if (bttv_tvcards[btv->c.type].no_video) {
1392 /* very basic init only */
1397 btwrite(0x00, BT848_CAP_CTL);
1398 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1399 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1401 /* set planar and packed mode trigger points and */
1402 /* set rising edge of inverted GPINTR pin as irq trigger */
1403 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1404 BT848_GPIO_DMA_CTL_PLTP1_16|
1405 BT848_GPIO_DMA_CTL_PLTP23_16|
1406 BT848_GPIO_DMA_CTL_GPINTC|
1407 BT848_GPIO_DMA_CTL_GPINTI,
1408 BT848_GPIO_DMA_CTL);
1410 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1411 btwrite(val, BT848_E_SCLOOP);
1412 btwrite(val, BT848_O_SCLOOP);
1414 btwrite(0x20, BT848_E_VSCALE_HI);
1415 btwrite(0x20, BT848_O_VSCALE_HI);
1416 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1419 btwrite(whitecrush_upper, BT848_WC_UP);
1420 btwrite(whitecrush_lower, BT848_WC_DOWN);
1422 if (btv->opt_lumafilter) {
1423 btwrite(0, BT848_E_CONTROL);
1424 btwrite(0, BT848_O_CONTROL);
1426 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1427 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1430 bt848_bright(btv, btv->bright);
1431 bt848_hue(btv, btv->hue);
1432 bt848_contrast(btv, btv->contrast);
1433 bt848_sat(btv, btv->saturation);
1439 static void bttv_reinit_bt848(struct bttv *btv)
1441 unsigned long flags;
1444 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1445 spin_lock_irqsave(&btv->s_lock,flags);
1447 bttv_set_dma(btv,0);
1448 spin_unlock_irqrestore(&btv->s_lock,flags);
1451 btv->pll.pll_current = -1;
1452 set_input(btv, btv->input, btv->tvnorm);
1455 static int get_control(struct bttv *btv, struct v4l2_control *c)
1457 struct video_audio va;
1460 for (i = 0; i < BTTV_CTLS; i++)
1461 if (bttv_ctls[i].id == c->id)
1465 if (btv->audio_hook && i >= 4 && i <= 8) {
1466 memset(&va,0,sizeof(va));
1467 btv->audio_hook(btv,&va,0);
1469 case V4L2_CID_AUDIO_MUTE:
1470 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1472 case V4L2_CID_AUDIO_VOLUME:
1473 c->value = va.volume;
1475 case V4L2_CID_AUDIO_BALANCE:
1476 c->value = va.balance;
1478 case V4L2_CID_AUDIO_BASS:
1481 case V4L2_CID_AUDIO_TREBLE:
1482 c->value = va.treble;
1488 case V4L2_CID_BRIGHTNESS:
1489 c->value = btv->bright;
1492 c->value = btv->hue;
1494 case V4L2_CID_CONTRAST:
1495 c->value = btv->contrast;
1497 case V4L2_CID_SATURATION:
1498 c->value = btv->saturation;
1501 case V4L2_CID_AUDIO_MUTE:
1502 case V4L2_CID_AUDIO_VOLUME:
1503 case V4L2_CID_AUDIO_BALANCE:
1504 case V4L2_CID_AUDIO_BASS:
1505 case V4L2_CID_AUDIO_TREBLE:
1506 bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c);
1509 case V4L2_CID_PRIVATE_CHROMA_AGC:
1510 c->value = btv->opt_chroma_agc;
1512 case V4L2_CID_PRIVATE_COMBFILTER:
1513 c->value = btv->opt_combfilter;
1515 case V4L2_CID_PRIVATE_LUMAFILTER:
1516 c->value = btv->opt_lumafilter;
1518 case V4L2_CID_PRIVATE_AUTOMUTE:
1519 c->value = btv->opt_automute;
1521 case V4L2_CID_PRIVATE_AGC_CRUSH:
1522 c->value = btv->opt_adc_crush;
1524 case V4L2_CID_PRIVATE_VCR_HACK:
1525 c->value = btv->opt_vcr_hack;
1527 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1528 c->value = btv->opt_whitecrush_upper;
1530 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1531 c->value = btv->opt_whitecrush_lower;
1533 case V4L2_CID_PRIVATE_UV_RATIO:
1534 c->value = btv->opt_uv_ratio;
1536 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1537 c->value = btv->opt_full_luma_range;
1539 case V4L2_CID_PRIVATE_CORING:
1540 c->value = btv->opt_coring;
1548 static int set_control(struct bttv *btv, struct v4l2_control *c)
1550 struct video_audio va;
1553 for (i = 0; i < BTTV_CTLS; i++)
1554 if (bttv_ctls[i].id == c->id)
1558 if (btv->audio_hook && i >= 4 && i <= 8) {
1559 memset(&va,0,sizeof(va));
1560 btv->audio_hook(btv,&va,0);
1562 case V4L2_CID_AUDIO_MUTE:
1564 va.flags |= VIDEO_AUDIO_MUTE;
1567 va.flags &= ~VIDEO_AUDIO_MUTE;
1572 case V4L2_CID_AUDIO_VOLUME:
1573 va.volume = c->value;
1575 case V4L2_CID_AUDIO_BALANCE:
1576 va.balance = c->value;
1578 case V4L2_CID_AUDIO_BASS:
1581 case V4L2_CID_AUDIO_TREBLE:
1582 va.treble = c->value;
1585 btv->audio_hook(btv,&va,1);
1589 case V4L2_CID_BRIGHTNESS:
1590 bt848_bright(btv,c->value);
1593 bt848_hue(btv,c->value);
1595 case V4L2_CID_CONTRAST:
1596 bt848_contrast(btv,c->value);
1598 case V4L2_CID_SATURATION:
1599 bt848_sat(btv,c->value);
1601 case V4L2_CID_AUDIO_MUTE:
1602 audio_mute(btv, c->value);
1604 case V4L2_CID_AUDIO_VOLUME:
1605 case V4L2_CID_AUDIO_BALANCE:
1606 case V4L2_CID_AUDIO_BASS:
1607 case V4L2_CID_AUDIO_TREBLE:
1608 bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c);
1611 case V4L2_CID_PRIVATE_CHROMA_AGC:
1612 btv->opt_chroma_agc = c->value;
1613 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1614 btwrite(val, BT848_E_SCLOOP);
1615 btwrite(val, BT848_O_SCLOOP);
1617 case V4L2_CID_PRIVATE_COMBFILTER:
1618 btv->opt_combfilter = c->value;
1620 case V4L2_CID_PRIVATE_LUMAFILTER:
1621 btv->opt_lumafilter = c->value;
1622 if (btv->opt_lumafilter) {
1623 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1624 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1626 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1627 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1630 case V4L2_CID_PRIVATE_AUTOMUTE:
1631 btv->opt_automute = c->value;
1633 case V4L2_CID_PRIVATE_AGC_CRUSH:
1634 btv->opt_adc_crush = c->value;
1635 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1638 case V4L2_CID_PRIVATE_VCR_HACK:
1639 btv->opt_vcr_hack = c->value;
1641 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1642 btv->opt_whitecrush_upper = c->value;
1643 btwrite(c->value, BT848_WC_UP);
1645 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1646 btv->opt_whitecrush_lower = c->value;
1647 btwrite(c->value, BT848_WC_DOWN);
1649 case V4L2_CID_PRIVATE_UV_RATIO:
1650 btv->opt_uv_ratio = c->value;
1651 bt848_sat(btv, btv->saturation);
1653 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1654 btv->opt_full_luma_range = c->value;
1655 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1657 case V4L2_CID_PRIVATE_CORING:
1658 btv->opt_coring = c->value;
1659 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1667 /* ----------------------------------------------------------------------- */
1669 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1671 unsigned int outbits, data;
1672 outbits = btread(BT848_GPIO_OUT_EN);
1673 data = btread(BT848_GPIO_DATA);
1674 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1675 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1678 static void bttv_field_count(struct bttv *btv)
1686 /* start field counter */
1687 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1689 /* stop field counter */
1690 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1691 btv->field_count = 0;
1695 static const struct bttv_format*
1696 format_by_palette(int palette)
1700 for (i = 0; i < BTTV_FORMATS; i++) {
1701 if (-1 == bttv_formats[i].palette)
1703 if (bttv_formats[i].palette == palette)
1704 return bttv_formats+i;
1709 static const struct bttv_format*
1710 format_by_fourcc(int fourcc)
1714 for (i = 0; i < BTTV_FORMATS; i++) {
1715 if (-1 == bttv_formats[i].fourcc)
1717 if (bttv_formats[i].fourcc == fourcc)
1718 return bttv_formats+i;
1723 /* ----------------------------------------------------------------------- */
1727 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1728 struct bttv_buffer *new)
1730 struct bttv_buffer *old;
1731 unsigned long flags;
1734 dprintk("switch_overlay: enter [new=%p]\n",new);
1736 new->vb.state = STATE_DONE;
1737 spin_lock_irqsave(&btv->s_lock,flags);
1741 bttv_set_dma(btv, 0x03);
1742 spin_unlock_irqrestore(&btv->s_lock,flags);
1744 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1745 bttv_dma_free(&fh->cap,btv, old);
1749 free_btres(btv,fh,RESOURCE_OVERLAY);
1750 dprintk("switch_overlay: done\n");
1754 /* ----------------------------------------------------------------------- */
1755 /* video4linux (1) interface */
1757 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1758 struct bttv_buffer *buf,
1759 const struct bttv_format *fmt,
1760 unsigned int width, unsigned int height,
1761 enum v4l2_field field)
1763 struct bttv_fh *fh = q->priv_data;
1764 int redo_dma_risc = 0;
1769 /* check settings */
1772 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1774 height = RAW_LINES*2;
1775 if (width*height > buf->vb.bsize)
1777 buf->vb.size = buf->vb.bsize;
1779 /* Make sure tvnorm and vbi_end remain consistent
1780 until we're done. */
1781 mutex_lock(&btv->lock);
1785 /* In this mode capturing always starts at defrect.top
1786 (default VDELAY), ignoring cropping parameters. */
1787 if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
1788 mutex_unlock(&btv->lock);
1792 mutex_unlock(&btv->lock);
1794 c.rect = bttv_tvnorms[norm].cropcap.defrect;
1796 mutex_lock(&btv->lock);
1799 c = btv->crop[!!fh->do_crop];
1801 mutex_unlock(&btv->lock);
1803 if (width < c.min_scaled_width ||
1804 width > c.max_scaled_width ||
1805 height < c.min_scaled_height)
1809 case V4L2_FIELD_TOP:
1810 case V4L2_FIELD_BOTTOM:
1811 case V4L2_FIELD_ALTERNATE:
1812 /* btv->crop counts frame lines. Max. scale
1813 factor is 16:1 for frames, 8:1 for fields. */
1814 if (height * 2 > c.max_scaled_height)
1819 if (height > c.max_scaled_height)
1824 buf->vb.size = (width * height * fmt->depth) >> 3;
1825 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1829 /* alloc + fill struct bttv_buffer (if changed) */
1830 if (buf->vb.width != width || buf->vb.height != height ||
1831 buf->vb.field != field ||
1832 buf->tvnorm != norm || buf->fmt != fmt ||
1833 buf->crop.top != c.rect.top ||
1834 buf->crop.left != c.rect.left ||
1835 buf->crop.width != c.rect.width ||
1836 buf->crop.height != c.rect.height) {
1837 buf->vb.width = width;
1838 buf->vb.height = height;
1839 buf->vb.field = field;
1846 /* alloc risc memory */
1847 if (STATE_NEEDS_INIT == buf->vb.state) {
1849 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1854 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1857 buf->vb.state = STATE_PREPARED;
1861 bttv_dma_free(q,btv,buf);
1866 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1868 struct bttv_fh *fh = q->priv_data;
1870 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1873 while (*size * *count > gbuffers * gbufsize)
1879 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1880 enum v4l2_field field)
1882 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1883 struct bttv_fh *fh = q->priv_data;
1885 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1886 fh->width, fh->height, field);
1890 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1892 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1893 struct bttv_fh *fh = q->priv_data;
1894 struct bttv *btv = fh->btv;
1896 buf->vb.state = STATE_QUEUED;
1897 list_add_tail(&buf->vb.queue,&btv->capture);
1898 if (!btv->curr.frame_irq) {
1900 bttv_set_dma(btv, 0x03);
1904 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1906 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1907 struct bttv_fh *fh = q->priv_data;
1909 bttv_dma_free(q,fh->btv,buf);
1912 static struct videobuf_queue_ops bttv_video_qops = {
1913 .buf_setup = buffer_setup,
1914 .buf_prepare = buffer_prepare,
1915 .buf_queue = buffer_queue,
1916 .buf_release = buffer_release,
1919 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1923 return BTTV_VERSION_CODE;
1925 /* *** v4l1 *** ************************************************ */
1928 unsigned long *freq = arg;
1934 struct v4l2_frequency freq;
1936 memset(&freq, 0, sizeof(freq));
1937 freq.frequency = *(unsigned long *)arg;
1938 mutex_lock(&btv->lock);
1939 freq.type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1940 btv->freq = *(unsigned long *)arg;
1941 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,&freq);
1942 if (btv->has_matchbox && btv->radio_user)
1943 tea5757_set_freq(btv,*(unsigned long *)arg);
1944 mutex_unlock(&btv->lock);
1950 struct video_tuner *v = arg;
1952 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1954 if (v->tuner) /* Only tuner 0 */
1956 strcpy(v->name, "Television");
1958 v->rangehigh = 0x7FFFFFFF;
1959 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1960 v->mode = btv->tvnorm;
1961 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1962 bttv_call_i2c_clients(btv,cmd,v);
1967 struct video_tuner *v = arg;
1969 if (v->tuner) /* Only tuner 0 */
1971 if (v->mode >= BTTV_TVNORMS)
1974 mutex_lock(&btv->lock);
1975 set_tvnorm(btv,v->mode);
1976 bttv_call_i2c_clients(btv,cmd,v);
1977 mutex_unlock(&btv->lock);
1983 struct video_channel *v = arg;
1984 unsigned int channel = v->channel;
1986 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1989 v->flags = VIDEO_VC_AUDIO;
1990 v->type = VIDEO_TYPE_CAMERA;
1991 v->norm = btv->tvnorm;
1992 if (channel == bttv_tvcards[btv->c.type].tuner) {
1993 strcpy(v->name,"Television");
1994 v->flags|=VIDEO_VC_TUNER;
1995 v->type=VIDEO_TYPE_TV;
1997 } else if (channel == btv->svhs) {
1998 strcpy(v->name,"S-Video");
2000 sprintf(v->name,"Composite%d",channel);
2006 struct video_channel *v = arg;
2007 unsigned int channel = v->channel;
2009 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
2011 if (v->norm >= BTTV_TVNORMS)
2014 mutex_lock(&btv->lock);
2015 if (channel == btv->input &&
2016 v->norm == btv->tvnorm) {
2018 mutex_unlock(&btv->lock);
2022 set_input(btv, v->channel, v->norm);
2023 mutex_unlock(&btv->lock);
2029 struct video_audio *v = arg;
2031 memset(v,0,sizeof(*v));
2032 strcpy(v->name,"Television");
2033 v->flags |= VIDEO_AUDIO_MUTABLE;
2034 v->mode = VIDEO_SOUND_MONO;
2036 mutex_lock(&btv->lock);
2037 bttv_call_i2c_clients(btv,cmd,v);
2039 /* card specific hooks */
2040 if (btv->audio_hook)
2041 btv->audio_hook(btv,v,0);
2043 mutex_unlock(&btv->lock);
2048 struct video_audio *v = arg;
2049 unsigned int audio = v->audio;
2051 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
2054 mutex_lock(&btv->lock);
2055 audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
2056 bttv_call_i2c_clients(btv,cmd,v);
2058 /* card specific hooks */
2059 if (btv->audio_hook)
2060 btv->audio_hook(btv,v,1);
2062 mutex_unlock(&btv->lock);
2066 /* *** v4l2 *** ************************************************ */
2067 case VIDIOC_ENUMSTD:
2069 struct v4l2_standard *e = arg;
2070 unsigned int index = e->index;
2072 if (index >= BTTV_TVNORMS)
2074 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
2075 bttv_tvnorms[e->index].name);
2081 v4l2_std_id *id = arg;
2082 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
2087 v4l2_std_id *id = arg;
2090 for (i = 0; i < BTTV_TVNORMS; i++)
2091 if (*id & bttv_tvnorms[i].v4l2_id)
2093 if (i == BTTV_TVNORMS)
2096 mutex_lock(&btv->lock);
2098 i2c_vidiocschan(btv);
2099 mutex_unlock(&btv->lock);
2102 case VIDIOC_QUERYSTD:
2104 v4l2_std_id *id = arg;
2106 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
2107 *id = V4L2_STD_625_50;
2109 *id = V4L2_STD_525_60;
2113 case VIDIOC_ENUMINPUT:
2115 struct v4l2_input *i = arg;
2119 if (n >= bttv_tvcards[btv->c.type].video_inputs)
2121 memset(i,0,sizeof(*i));
2123 i->type = V4L2_INPUT_TYPE_CAMERA;
2125 if (i->index == bttv_tvcards[btv->c.type].tuner) {
2126 sprintf(i->name, "Television");
2127 i->type = V4L2_INPUT_TYPE_TUNER;
2129 } else if (i->index == btv->svhs) {
2130 sprintf(i->name, "S-Video");
2132 sprintf(i->name,"Composite%d",i->index);
2134 if (i->index == btv->input) {
2135 __u32 dstatus = btread(BT848_DSTATUS);
2136 if (0 == (dstatus & BT848_DSTATUS_PRES))
2137 i->status |= V4L2_IN_ST_NO_SIGNAL;
2138 if (0 == (dstatus & BT848_DSTATUS_HLOC))
2139 i->status |= V4L2_IN_ST_NO_H_LOCK;
2141 for (n = 0; n < BTTV_TVNORMS; n++)
2142 i->std |= bttv_tvnorms[n].v4l2_id;
2145 case VIDIOC_G_INPUT:
2151 case VIDIOC_S_INPUT:
2153 unsigned int *i = arg;
2155 if (*i > bttv_tvcards[btv->c.type].video_inputs)
2157 mutex_lock(&btv->lock);
2158 set_input(btv, *i, btv->tvnorm);
2159 mutex_unlock(&btv->lock);
2163 case VIDIOC_G_TUNER:
2165 struct v4l2_tuner *t = arg;
2167 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2171 mutex_lock(&btv->lock);
2172 memset(t,0,sizeof(*t));
2173 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2174 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
2175 strcpy(t->name, "Television");
2176 t->capability = V4L2_TUNER_CAP_NORM;
2177 t->type = V4L2_TUNER_ANALOG_TV;
2178 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2181 if (btv->audio_hook) {
2183 struct video_audio va;
2184 memset(&va, 0, sizeof(struct video_audio));
2185 btv->audio_hook(btv,&va,0);
2186 t->audmode = V4L2_TUNER_MODE_MONO;
2187 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2188 if(va.mode & VIDEO_SOUND_STEREO) {
2189 t->audmode = V4L2_TUNER_MODE_STEREO;
2190 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
2192 if(va.mode & VIDEO_SOUND_LANG2) {
2193 t->audmode = V4L2_TUNER_MODE_LANG1;
2194 t->rxsubchans = V4L2_TUNER_SUB_LANG1
2195 | V4L2_TUNER_SUB_LANG2;
2198 /* FIXME: fill capability+audmode */
2199 mutex_unlock(&btv->lock);
2202 case VIDIOC_S_TUNER:
2204 struct v4l2_tuner *t = arg;
2206 if (UNSET == bttv_tvcards[btv->c.type].tuner)
2210 mutex_lock(&btv->lock);
2211 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
2212 if (btv->audio_hook) {
2213 struct video_audio va;
2214 memset(&va, 0, sizeof(struct video_audio));
2215 if (t->audmode == V4L2_TUNER_MODE_MONO)
2216 va.mode = VIDEO_SOUND_MONO;
2217 else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
2218 t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
2219 va.mode = VIDEO_SOUND_STEREO;
2220 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
2221 va.mode = VIDEO_SOUND_LANG1;
2222 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
2223 va.mode = VIDEO_SOUND_LANG2;
2224 btv->audio_hook(btv,&va,1);
2226 mutex_unlock(&btv->lock);
2230 case VIDIOC_G_FREQUENCY:
2232 struct v4l2_frequency *f = arg;
2234 memset(f,0,sizeof(*f));
2235 f->type = V4L2_TUNER_ANALOG_TV;
2236 f->frequency = btv->freq;
2239 case VIDIOC_S_FREQUENCY:
2241 struct v4l2_frequency *f = arg;
2243 if (unlikely(f->tuner != 0))
2245 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
2247 mutex_lock(&btv->lock);
2248 btv->freq = f->frequency;
2249 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
2250 if (btv->has_matchbox && btv->radio_user)
2251 tea5757_set_freq(btv,btv->freq);
2252 mutex_unlock(&btv->lock);
2255 case VIDIOC_LOG_STATUS:
2257 printk(KERN_INFO "bttv%d: ================= START STATUS CARD #%d =================\n", btv->c.nr, btv->c.nr);
2258 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
2259 printk(KERN_INFO "bttv%d: ================== END STATUS CARD #%d ==================\n", btv->c.nr, btv->c.nr);
2262 #ifdef CONFIG_VIDEO_ADV_DEBUG
2263 case VIDIOC_DBG_G_REGISTER:
2264 case VIDIOC_DBG_S_REGISTER:
2266 struct v4l2_register *reg = arg;
2267 if (!capable(CAP_SYS_ADMIN))
2269 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
2271 /* bt848 has a 12-bit register space */
2273 if (cmd == VIDIOC_DBG_G_REGISTER)
2274 reg->val = btread(reg->reg);
2276 btwrite(reg->val, reg->reg);
2282 return -ENOIOCTLCMD;
2288 /* Given cropping boundaries b and the scaled width and height of a
2289 single field or frame, which must not exceed hardware limits, this
2290 function adjusts the cropping parameters c. */
2292 bttv_crop_adjust (struct bttv_crop * c,
2293 const struct v4l2_rect * b,
2296 enum v4l2_field field)
2298 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
2302 if (width < c->min_scaled_width) {
2303 /* Max. hor. scale factor 16:1. */
2304 c->rect.width = width * 16;
2305 } else if (width > c->max_scaled_width) {
2306 /* Min. hor. scale factor 1:1. */
2307 c->rect.width = width;
2309 max_left = b->left + b->width - width;
2310 max_left = min(max_left, (__s32) MAX_HDELAY);
2311 if (c->rect.left > max_left)
2312 c->rect.left = max_left;
2315 if (height < c->min_scaled_height) {
2316 /* Max. vert. scale factor 16:1, single fields 8:1. */
2317 c->rect.height = height * 16;
2318 } else if (frame_height > c->max_scaled_height) {
2319 /* Min. vert. scale factor 1:1.
2320 Top and height count field lines times two. */
2321 c->rect.height = (frame_height + 1) & ~1;
2323 max_top = b->top + b->height - c->rect.height;
2324 if (c->rect.top > max_top)
2325 c->rect.top = max_top;
2328 bttv_crop_calc_limits(c);
2331 /* Returns an error if scaling to a frame or single field with the given
2332 width and height is not possible with the current cropping parameters
2333 and width aligned according to width_mask. If adjust_size is TRUE the
2334 function may adjust the width and/or height instead, rounding width
2335 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
2336 also adjust the current cropping parameters to get closer to the
2337 desired image size. */
2339 limit_scaled_size (struct bttv_fh * fh,
2342 enum v4l2_field field,
2343 unsigned int width_mask,
2344 unsigned int width_bias,
2348 struct bttv *btv = fh->btv;
2349 const struct v4l2_rect *b;
2350 struct bttv_crop *c;
2357 BUG_ON((int) width_mask >= 0 ||
2358 width_bias >= (unsigned int) -width_mask);
2360 /* Make sure tvnorm, vbi_end and the current cropping parameters
2361 remain consistent until we're done. */
2362 mutex_lock(&btv->lock);
2364 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2366 /* Do crop - use current, don't - use default parameters. */
2367 c = &btv->crop[!!fh->do_crop];
2372 && !locked_btres(btv, VIDEO_RESOURCES)) {
2376 /* We cannot scale up. When the scaled image is larger
2377 than crop.rect we adjust the crop.rect as required
2378 by the V4L2 spec, hence cropcap.bounds are our limit. */
2379 max_width = min(b->width, (__s32) MAX_HACTIVE);
2380 max_height = b->height;
2382 /* We cannot capture the same line as video and VBI data.
2383 Note btv->vbi_end is really a minimum, see
2384 bttv_vbi_try_fmt(). */
2385 if (btv->vbi_end > b->top) {
2386 max_height -= btv->vbi_end - b->top;
2388 if (min_height > max_height)
2393 if (btv->vbi_end > c->rect.top)
2396 min_width = c->min_scaled_width;
2397 min_height = c->min_scaled_height;
2398 max_width = c->max_scaled_width;
2399 max_height = c->max_scaled_height;
2404 min_width = (min_width - width_mask - 1) & width_mask;
2405 max_width = max_width & width_mask;
2407 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
2408 min_height = min_height;
2409 /* Min. scale factor is 1:1. */
2410 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
2413 *width = clamp(*width, min_width, max_width);
2414 *height = clamp(*height, min_height, max_height);
2416 /* Round after clamping to avoid overflow. */
2417 *width = (*width + width_bias) & width_mask;
2420 bttv_crop_adjust(c, b, *width, *height, field);
2422 if (btv->vbi_end > c->rect.top) {
2423 /* Move the crop window out of the way. */
2424 c->rect.top = btv->vbi_end;
2429 if (*width < min_width ||
2430 *height < min_height ||
2431 *width > max_width ||
2432 *height > max_height ||
2433 0 != (*width & ~width_mask))
2437 rc = 0; /* success */
2440 mutex_unlock(&btv->lock);
2445 /* Returns an error if the given overlay window dimensions are not
2446 possible with the current cropping parameters. If adjust_size is
2447 TRUE the function may adjust the window width and/or height
2448 instead, however it always rounds the horizontal position and
2449 width as btcx_align() does. If adjust_crop is TRUE the function
2450 may also adjust the current cropping parameters to get closer
2451 to the desired window size. */
2453 verify_window (struct bttv_fh * fh,
2454 struct v4l2_window * win,
2458 enum v4l2_field field;
2459 unsigned int width_mask;
2462 if (win->w.width < 48 || win->w.height < 32)
2464 if (win->clipcount > 2048)
2469 if (V4L2_FIELD_ANY == field) {
2472 height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
2473 field = (win->w.height > height2)
2474 ? V4L2_FIELD_INTERLACED
2478 case V4L2_FIELD_TOP:
2479 case V4L2_FIELD_BOTTOM:
2480 case V4L2_FIELD_INTERLACED:
2486 /* 4-byte alignment. */
2487 if (NULL == fh->ovfmt)
2490 switch (fh->ovfmt->depth) {
2504 win->w.width -= win->w.left & ~width_mask;
2505 win->w.left = (win->w.left - width_mask - 1) & width_mask;
2507 rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
2509 /* width_bias: round down */ 0,
2510 adjust_size, adjust_crop);
2518 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2519 struct v4l2_window *win, int fixup)
2521 struct v4l2_clip *clips = NULL;
2522 int n,size,retval = 0;
2524 if (NULL == fh->ovfmt)
2526 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2528 retval = verify_window(fh, win,
2529 /* adjust_size */ fixup,
2530 /* adjust_crop */ fixup);
2534 /* copy clips -- luckily v4l1 + v4l2 are binary
2535 compatible here ...*/
2537 size = sizeof(*clips)*(n+4);
2538 clips = kmalloc(size,GFP_KERNEL);
2542 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2547 /* clip against screen */
2548 if (NULL != btv->fbuf.base)
2549 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2551 btcx_sort_clips(clips,n);
2553 /* 4-byte alignments */
2554 switch (fh->ovfmt->depth) {
2557 btcx_align(&win->w, clips, n, 3);
2560 btcx_align(&win->w, clips, n, 1);
2563 /* no alignment fixups needed */
2569 mutex_lock(&fh->cap.lock);
2570 kfree(fh->ov.clips);
2571 fh->ov.clips = clips;
2575 fh->ov.field = win->field;
2576 fh->ov.setup_ok = 1;
2577 btv->init.ov.w.width = win->w.width;
2578 btv->init.ov.w.height = win->w.height;
2579 btv->init.ov.field = win->field;
2581 /* update overlay if needed */
2583 if (check_btres(fh, RESOURCE_OVERLAY)) {
2584 struct bttv_buffer *new;
2586 new = videobuf_pci_alloc(sizeof(*new));
2587 new->crop = btv->crop[!!fh->do_crop].rect;
2588 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2589 retval = bttv_switch_overlay(btv,fh,new);
2591 mutex_unlock(&fh->cap.lock);
2595 /* ----------------------------------------------------------------------- */
2597 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2599 struct videobuf_queue* q = NULL;
2602 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2605 case V4L2_BUF_TYPE_VBI_CAPTURE:
2614 static int bttv_resource(struct bttv_fh *fh)
2619 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2620 res = RESOURCE_VIDEO_STREAM;
2622 case V4L2_BUF_TYPE_VBI_CAPTURE:
2631 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2633 struct videobuf_queue *q = bttv_queue(fh);
2634 int res = bttv_resource(fh);
2636 if (check_btres(fh,res))
2638 if (videobuf_queue_is_busy(q))
2645 pix_format_set_size (struct v4l2_pix_format * f,
2646 const struct bttv_format * fmt,
2648 unsigned int height)
2653 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2654 f->bytesperline = width; /* Y plane */
2655 f->sizeimage = (width * height * fmt->depth) >> 3;
2657 f->bytesperline = (width * fmt->depth) >> 3;
2658 f->sizeimage = height * f->bytesperline;
2662 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2665 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2666 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2667 pix_format_set_size (&f->fmt.pix, fh->fmt,
2668 fh->width, fh->height);
2669 f->fmt.pix.field = fh->cap.field;
2670 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2672 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2673 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2674 f->fmt.win.w = fh->ov.w;
2675 f->fmt.win.field = fh->ov.field;
2677 case V4L2_BUF_TYPE_VBI_CAPTURE:
2678 bttv_vbi_get_fmt(fh, &f->fmt.vbi);
2685 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2686 struct v4l2_format *f, int adjust_crop)
2689 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2691 const struct bttv_format *fmt;
2692 enum v4l2_field field;
2693 __s32 width, height;
2696 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2700 field = f->fmt.pix.field;
2701 if (V4L2_FIELD_ANY == field) {
2704 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
2705 field = (f->fmt.pix.height > height2)
2706 ? V4L2_FIELD_INTERLACED
2707 : V4L2_FIELD_BOTTOM;
2709 if (V4L2_FIELD_SEQ_BT == field)
2710 field = V4L2_FIELD_SEQ_TB;
2712 case V4L2_FIELD_TOP:
2713 case V4L2_FIELD_BOTTOM:
2714 case V4L2_FIELD_ALTERNATE:
2715 case V4L2_FIELD_INTERLACED:
2717 case V4L2_FIELD_SEQ_TB:
2718 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2725 width = f->fmt.pix.width;
2726 height = f->fmt.pix.height;
2728 rc = limit_scaled_size(fh, &width, &height, field,
2729 /* width_mask: 4 pixels */ ~3,
2730 /* width_bias: nearest */ 2,
2731 /* adjust_size */ 1,
2736 /* update data for the application */
2737 f->fmt.pix.field = field;
2738 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2742 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2743 return verify_window(fh, &f->fmt.win,
2744 /* adjust_size */ 1,
2745 /* adjust_crop */ 0);
2746 case V4L2_BUF_TYPE_VBI_CAPTURE:
2747 return bttv_vbi_try_fmt(fh, &f->fmt.vbi);
2753 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2754 struct v4l2_format *f)
2759 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2761 const struct bttv_format *fmt;
2763 retval = bttv_switch_type(fh,f->type);
2766 retval = bttv_try_fmt(fh,btv,f, /* adjust_crop */ 1);
2769 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2771 /* update our state informations */
2772 mutex_lock(&fh->cap.lock);
2774 fh->cap.field = f->fmt.pix.field;
2775 fh->cap.last = V4L2_FIELD_NONE;
2776 fh->width = f->fmt.pix.width;
2777 fh->height = f->fmt.pix.height;
2778 btv->init.fmt = fmt;
2779 btv->init.width = f->fmt.pix.width;
2780 btv->init.height = f->fmt.pix.height;
2781 mutex_unlock(&fh->cap.lock);
2785 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2786 if (no_overlay > 0) {
2787 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2790 return setup_window(fh, btv, &f->fmt.win, 1);
2791 case V4L2_BUF_TYPE_VBI_CAPTURE:
2792 retval = bttv_switch_type(fh,f->type);
2795 return bttv_vbi_set_fmt(fh, &f->fmt.vbi);
2801 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2802 unsigned int cmd, void *arg)
2804 struct bttv_fh *fh = file->private_data;
2805 struct bttv *btv = fh->btv;
2806 unsigned long flags;
2810 v4l_print_ioctl(btv->c.name, cmd);
2813 bttv_reinit_bt848(btv);
2821 case VIDIOC_S_INPUT:
2822 case VIDIOC_S_TUNER:
2823 case VIDIOC_S_FREQUENCY:
2824 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2831 /* *** v4l1 *** ************************************************ */
2834 struct video_capability *cap = arg;
2836 memset(cap,0,sizeof(*cap));
2837 strcpy(cap->name,btv->video_dev->name);
2838 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2840 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2843 cap->type = VID_TYPE_CAPTURE|
2847 if (no_overlay <= 0)
2848 cap->type |= VID_TYPE_OVERLAY;
2850 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2851 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2853 cap->minheight = 32;
2855 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2856 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2862 struct video_picture *pic = arg;
2864 memset(pic,0,sizeof(*pic));
2865 pic->brightness = btv->bright;
2866 pic->contrast = btv->contrast;
2867 pic->hue = btv->hue;
2868 pic->colour = btv->saturation;
2870 pic->depth = fh->fmt->depth;
2871 pic->palette = fh->fmt->palette;
2877 struct video_picture *pic = arg;
2878 const struct bttv_format *fmt;
2880 fmt = format_by_palette(pic->palette);
2883 mutex_lock(&fh->cap.lock);
2884 if (fmt->flags & FORMAT_FLAGS_RAW) {
2885 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2886 RAW_LINES * 2. F1 is stored at offset 0, F2
2887 at buffer size / 2. */
2888 fh->width = RAW_BPL;
2889 fh->height = gbufsize / RAW_BPL;
2890 btv->init.width = RAW_BPL;
2891 btv->init.height = gbufsize / RAW_BPL;
2895 btv->init.ovfmt = fmt;
2896 btv->init.fmt = fmt;
2898 /* dirty hack time: swap bytes for overlay if the
2899 display adaptor is big endian (insmod option) */
2900 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2901 fmt->palette == VIDEO_PALETTE_RGB565 ||
2902 fmt->palette == VIDEO_PALETTE_RGB32) {
2906 bt848_bright(btv,pic->brightness);
2907 bt848_contrast(btv,pic->contrast);
2908 bt848_hue(btv,pic->hue);
2909 bt848_sat(btv,pic->colour);
2910 mutex_unlock(&fh->cap.lock);
2916 struct video_window *win = arg;
2918 memset(win,0,sizeof(*win));
2919 win->x = fh->ov.w.left;
2920 win->y = fh->ov.w.top;
2921 win->width = fh->ov.w.width;
2922 win->height = fh->ov.w.height;
2927 struct video_window *win = arg;
2928 struct v4l2_window w2;
2930 if (no_overlay > 0) {
2931 printk ("VIDIOCSWIN: no_overlay\n");
2935 w2.field = V4L2_FIELD_ANY;
2938 w2.w.width = win->width;
2939 w2.w.height = win->height;
2940 w2.clipcount = win->clipcount;
2941 w2.clips = (struct v4l2_clip __user *)win->clips;
2942 retval = setup_window(fh, btv, &w2, 0);
2944 /* on v4l1 this ioctl affects the read() size too */
2945 fh->width = fh->ov.w.width;
2946 fh->height = fh->ov.w.height;
2947 btv->init.width = fh->ov.w.width;
2948 btv->init.height = fh->ov.w.height;
2955 struct video_buffer *fbuf = arg;
2957 fbuf->base = btv->fbuf.base;
2958 fbuf->width = btv->fbuf.fmt.width;
2959 fbuf->height = btv->fbuf.fmt.height;
2960 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2962 fbuf->depth = fh->ovfmt->depth;
2965 fbuf->depth = ((fbuf->bytesperline<<3)
2975 struct video_buffer *fbuf = arg;
2976 const struct bttv_format *fmt;
2979 if(!capable(CAP_SYS_ADMIN) &&
2980 !capable(CAP_SYS_RAWIO))
2982 end = (unsigned long)fbuf->base +
2983 fbuf->height * fbuf->bytesperline;
2984 mutex_lock(&fh->cap.lock);
2987 switch (fbuf->depth) {
2989 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2992 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2995 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2998 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
3002 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
3009 goto fh_unlock_and_return;
3013 btv->init.ovfmt = fmt;
3014 btv->init.fmt = fmt;
3015 btv->fbuf.base = fbuf->base;
3016 btv->fbuf.fmt.width = fbuf->width;
3017 btv->fbuf.fmt.height = fbuf->height;
3018 if (fbuf->bytesperline)
3019 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
3021 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
3022 mutex_unlock(&fh->cap.lock);
3027 case VIDIOC_OVERLAY:
3029 struct bttv_buffer *new;
3034 if (NULL == btv->fbuf.base)
3036 if (!fh->ov.setup_ok) {
3037 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
3042 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
3045 mutex_lock(&fh->cap.lock);
3047 fh->ov.tvnorm = btv->tvnorm;
3048 new = videobuf_pci_alloc(sizeof(*new));
3049 new->crop = btv->crop[!!fh->do_crop].rect;
3050 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
3056 retval = bttv_switch_overlay(btv,fh,new);
3057 mutex_unlock(&fh->cap.lock);
3063 struct video_mbuf *mbuf = arg;
3066 mutex_lock(&fh->cap.lock);
3067 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
3070 goto fh_unlock_and_return;
3073 memset(mbuf,0,sizeof(*mbuf));
3074 mbuf->frames = gbuffers;
3075 mbuf->size = gbuffers * gbufsize;
3076 for (i = 0; i < gbuffers; i++)
3077 mbuf->offsets[i] = i * gbufsize;
3078 mutex_unlock(&fh->cap.lock);
3081 case VIDIOCMCAPTURE:
3083 struct video_mmap *vm = arg;
3084 struct bttv_buffer *buf;
3085 enum v4l2_field field;
3089 if (vm->frame >= VIDEO_MAX_FRAME)
3092 res = bttv_resource(fh);
3093 if (!check_alloc_btres(btv, fh, res))
3096 mutex_lock(&fh->cap.lock);
3098 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
3100 goto fh_unlock_and_return;
3101 if (0 == buf->vb.baddr)
3102 goto fh_unlock_and_return;
3103 if (buf->vb.state == STATE_QUEUED ||
3104 buf->vb.state == STATE_ACTIVE)
3105 goto fh_unlock_and_return;
3107 height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
3108 field = (vm->height > height2)
3109 ? V4L2_FIELD_INTERLACED
3110 : V4L2_FIELD_BOTTOM;
3111 retval = bttv_prepare_buffer(&fh->cap,btv,buf,
3112 format_by_palette(vm->format),
3113 vm->width,vm->height,field);
3115 goto fh_unlock_and_return;
3116 btv->init.width = vm->width;
3117 btv->init.height = vm->height;
3118 spin_lock_irqsave(&btv->s_lock,flags);
3119 buffer_queue(&fh->cap,&buf->vb);
3120 spin_unlock_irqrestore(&btv->s_lock,flags);
3121 mutex_unlock(&fh->cap.lock);
3127 struct bttv_buffer *buf;
3129 if (*frame >= VIDEO_MAX_FRAME)
3132 mutex_lock(&fh->cap.lock);
3134 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
3136 goto fh_unlock_and_return;
3137 retval = videobuf_waiton(&buf->vb,0,1);
3139 goto fh_unlock_and_return;
3140 switch (buf->vb.state) {
3146 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
3147 videobuf_dma_sync(&fh->cap,dma);
3148 bttv_dma_free(&fh->cap,btv,buf);
3155 mutex_unlock(&fh->cap.lock);
3160 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
3161 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
3169 return v4l_compat_translate_ioctl(inode, file, cmd,
3170 arg, bttv_do_ioctl);
3181 return bttv_common_ioctls(btv,cmd,arg);
3183 /* *** v4l2 *** ************************************************ */
3184 case VIDIOC_QUERYCAP:
3186 struct v4l2_capability *cap = arg;
3190 memset(cap, 0, sizeof (*cap));
3191 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
3192 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
3193 snprintf(cap->bus_info, sizeof (cap->bus_info),
3194 "PCI:%s", pci_name(btv->c.pci));
3195 cap->version = BTTV_VERSION_CODE;
3197 V4L2_CAP_VIDEO_CAPTURE |
3198 V4L2_CAP_VBI_CAPTURE |
3199 V4L2_CAP_READWRITE |
3201 if (no_overlay <= 0)
3202 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
3204 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
3205 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
3206 cap->capabilities |= V4L2_CAP_TUNER;
3210 case VIDIOC_ENUM_FMT:
3212 struct v4l2_fmtdesc *f = arg;
3213 enum v4l2_buf_type type;
3218 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
3223 memset(f,0,sizeof(*f));
3226 f->pixelformat = V4L2_PIX_FMT_GREY;
3227 strcpy(f->description,"vbi data");
3231 /* video capture + overlay */
3233 for (i = 0; i < BTTV_FORMATS; i++) {
3234 if (bttv_formats[i].fourcc != -1)
3236 if ((unsigned int)index == f->index)
3239 if (BTTV_FORMATS == i)
3243 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3245 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
3246 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
3252 memset(f,0,sizeof(*f));
3255 f->pixelformat = bttv_formats[i].fourcc;
3256 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
3260 case VIDIOC_TRY_FMT:
3262 struct v4l2_format *f = arg;
3263 return bttv_try_fmt(fh,btv,f, /* adjust_crop */ 0);
3267 struct v4l2_format *f = arg;
3268 return bttv_g_fmt(fh,f);
3272 struct v4l2_format *f = arg;
3273 return bttv_s_fmt(fh,btv,f);
3278 struct v4l2_framebuffer *fb = arg;
3281 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
3283 fb->fmt.pixelformat = fh->ovfmt->fourcc;
3288 struct v4l2_framebuffer *fb = arg;
3289 const struct bttv_format *fmt;
3291 if(!capable(CAP_SYS_ADMIN) &&
3292 !capable(CAP_SYS_RAWIO))
3296 fmt = format_by_fourcc(fb->fmt.pixelformat);
3299 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
3303 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
3304 __s32 width = fb->fmt.width;
3305 __s32 height = fb->fmt.height;
3307 retval = limit_scaled_size(fh, &width, &height,
3308 V4L2_FIELD_INTERLACED,
3309 /* width_mask */ ~3,
3311 /* adjust_size */ 0,
3312 /* adjust_crop */ 0);
3318 mutex_lock(&fh->cap.lock);
3319 btv->fbuf.base = fb->base;
3320 btv->fbuf.fmt.width = fb->fmt.width;
3321 btv->fbuf.fmt.height = fb->fmt.height;
3322 if (0 != fb->fmt.bytesperline)
3323 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
3325 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
3329 btv->init.ovfmt = fmt;
3330 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
3333 fh->ov.w.width = fb->fmt.width;
3334 fh->ov.w.height = fb->fmt.height;
3335 btv->init.ov.w.width = fb->fmt.width;
3336 btv->init.ov.w.height = fb->fmt.height;
3337 kfree(fh->ov.clips);
3338 fh->ov.clips = NULL;
3341 if (check_btres(fh, RESOURCE_OVERLAY)) {
3342 struct bttv_buffer *new;
3344 new = videobuf_pci_alloc(sizeof(*new));
3345 new->crop = btv->crop[!!fh->do_crop].rect;
3346 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
3347 retval = bttv_switch_overlay(btv,fh,new);
3350 mutex_unlock(&fh->cap.lock);
3354 case VIDIOC_REQBUFS:
3355 return videobuf_reqbufs(bttv_queue(fh),arg);
3357 case VIDIOC_QUERYBUF:
3358 return videobuf_querybuf(bttv_queue(fh),arg);
3362 int res = bttv_resource(fh);
3364 if (!check_alloc_btres(btv, fh, res))
3366 return videobuf_qbuf(bttv_queue(fh),arg);
3370 return videobuf_dqbuf(bttv_queue(fh),arg,
3371 file->f_flags & O_NONBLOCK);
3373 case VIDIOC_STREAMON:
3375 int res = bttv_resource(fh);
3377 if (!check_alloc_btres(btv,fh,res))
3379 return videobuf_streamon(bttv_queue(fh));
3381 case VIDIOC_STREAMOFF:
3383 int res = bttv_resource(fh);
3385 retval = videobuf_streamoff(bttv_queue(fh));
3388 free_btres(btv,fh,res);
3392 case VIDIOC_QUERYCTRL:
3394 struct v4l2_queryctrl *c = arg;
3397 if ((c->id < V4L2_CID_BASE ||
3398 c->id >= V4L2_CID_LASTP1) &&
3399 (c->id < V4L2_CID_PRIVATE_BASE ||
3400 c->id >= V4L2_CID_PRIVATE_LASTP1))
3402 for (i = 0; i < BTTV_CTLS; i++)
3403 if (bttv_ctls[i].id == c->id)
3405 if (i == BTTV_CTLS) {
3410 if (btv->audio_hook && i >= 4 && i <= 8) {
3411 struct video_audio va;
3412 memset(&va,0,sizeof(va));
3413 btv->audio_hook(btv,&va,0);
3414 switch (bttv_ctls[i].id) {
3415 case V4L2_CID_AUDIO_VOLUME:
3416 if (!(va.flags & VIDEO_AUDIO_VOLUME))
3419 case V4L2_CID_AUDIO_BALANCE:
3420 if (!(va.flags & VIDEO_AUDIO_BALANCE))
3423 case V4L2_CID_AUDIO_BASS:
3424 if (!(va.flags & VIDEO_AUDIO_BASS))
3427 case V4L2_CID_AUDIO_TREBLE:
3428 if (!(va.flags & VIDEO_AUDIO_TREBLE))
3436 return get_control(btv,arg);
3438 return set_control(btv,arg);
3441 struct v4l2_streamparm *parm = arg;
3442 struct v4l2_standard s;
3443 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
3445 memset(parm,0,sizeof(*parm));
3446 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
3447 bttv_tvnorms[btv->tvnorm].name);
3448 parm->parm.capture.timeperframe = s.frameperiod;
3452 case VIDIOC_G_PRIORITY:
3454 enum v4l2_priority *p = arg;
3456 *p = v4l2_prio_max(&btv->prio);
3459 case VIDIOC_S_PRIORITY:
3461 enum v4l2_priority *prio = arg;
3463 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
3466 case VIDIOC_CROPCAP:
3468 struct v4l2_cropcap *cap = arg;
3469 enum v4l2_buf_type type;
3473 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3474 type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3477 *cap = bttv_tvnorms[btv->tvnorm].cropcap;
3484 struct v4l2_crop * crop = arg;
3486 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3487 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3490 /* No fh->do_crop = 1; because btv->crop[1] may be
3491 inconsistent with fh->width or fh->height and apps
3492 do not expect a change here. */
3494 crop->c = btv->crop[!!fh->do_crop].rect;
3500 struct v4l2_crop *crop = arg;
3501 const struct v4l2_rect *b;
3508 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
3509 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
3512 retval = v4l2_prio_check(&btv->prio,&fh->prio);
3516 /* Make sure tvnorm, vbi_end and the current cropping
3517 parameters remain consistent until we're done. Note
3518 read() may change vbi_end in check_alloc_btres(). */
3519 mutex_lock(&btv->lock);
3523 if (locked_btres(fh->btv, VIDEO_RESOURCES))
3524 goto btv_unlock_and_return;
3526 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
3529 b_right = b_left + b->width;
3530 b_bottom = b->top + b->height;
3532 b_top = max(b->top, btv->vbi_end);
3533 if (b_top + 32 >= b_bottom)
3534 goto btv_unlock_and_return;
3536 /* Min. scaled size 48 x 32. */
3537 c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
3538 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
3540 c.rect.width = clamp(crop->c.width,
3541 48, b_right - c.rect.left);
3543 c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
3544 /* Top and height must be a multiple of two. */
3545 c.rect.top = (c.rect.top + 1) & ~1;
3547 c.rect.height = clamp(crop->c.height,
3548 32, b_bottom - c.rect.top);
3549 c.rect.height = (c.rect.height + 1) & ~1;
3551 bttv_crop_calc_limits(&c);
3555 mutex_unlock(&btv->lock);
3559 mutex_lock(&fh->cap.lock);
3561 if (fh->width < c.min_scaled_width) {
3562 fh->width = c.min_scaled_width;
3563 btv->init.width = c.min_scaled_width;
3564 } else if (fh->width > c.max_scaled_width) {
3565 fh->width = c.max_scaled_width;
3566 btv->init.width = c.max_scaled_width;
3569 if (fh->height < c.min_scaled_height) {
3570 fh->height = c.min_scaled_height;
3571 btv->init.height = c.min_scaled_height;
3572 } else if (fh->height > c.max_scaled_height) {
3573 fh->height = c.max_scaled_height;
3574 btv->init.height = c.max_scaled_height;
3577 mutex_unlock(&fh->cap.lock);
3582 case VIDIOC_ENUMSTD:
3585 case VIDIOC_ENUMINPUT:
3586 case VIDIOC_G_INPUT:
3587 case VIDIOC_S_INPUT:
3588 case VIDIOC_G_TUNER:
3589 case VIDIOC_S_TUNER:
3590 case VIDIOC_G_FREQUENCY:
3591 case VIDIOC_S_FREQUENCY:
3592 case VIDIOC_LOG_STATUS:
3593 case VIDIOC_DBG_G_REGISTER:
3594 case VIDIOC_DBG_S_REGISTER:
3595 return bttv_common_ioctls(btv,cmd,arg);
3598 return -ENOIOCTLCMD;
3602 fh_unlock_and_return:
3603 mutex_unlock(&fh->cap.lock);
3606 btv_unlock_and_return:
3607 mutex_unlock(&btv->lock);
3611 static int bttv_ioctl(struct inode *inode, struct file *file,
3612 unsigned int cmd, unsigned long arg)
3614 struct bttv_fh *fh = file->private_data;
3619 const struct bttv_tvnorm *tvnorm;
3621 tvnorm = fh->vbi_fmt.tvnorm;
3623 if (fh->vbi_fmt.fmt.start[0] != tvnorm->vbistart[0] ||
3624 fh->vbi_fmt.fmt.start[1] != tvnorm->vbistart[1] ||
3625 fh->vbi_fmt.fmt.count[0] != fh->vbi_fmt.fmt.count[1]) {
3626 /* BTTV_VBISIZE cannot express these parameters,
3627 however open() resets the paramters to defaults
3628 and apps shouldn't call BTTV_VBISIZE after
3633 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
3634 return (fh->vbi_fmt.fmt.count[0] * 2
3635 * fh->vbi_fmt.fmt.samples_per_line);
3639 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
3643 static ssize_t bttv_read(struct file *file, char __user *data,
3644 size_t count, loff_t *ppos)
3646 struct bttv_fh *fh = file->private_data;
3649 if (fh->btv->errors)
3650 bttv_reinit_bt848(fh->btv);
3651 dprintk("bttv%d: read count=%d type=%s\n",
3652 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3655 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3656 if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
3657 /* VIDEO_READ in use by another fh,
3658 or VIDEO_STREAM by any fh. */
3661 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3662 file->f_flags & O_NONBLOCK);
3663 free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
3665 case V4L2_BUF_TYPE_VBI_CAPTURE:
3666 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3668 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3669 file->f_flags & O_NONBLOCK);
3677 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3679 struct bttv_fh *fh = file->private_data;
3680 struct bttv_buffer *buf;
3681 enum v4l2_field field;
3683 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3684 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3686 return videobuf_poll_stream(file, &fh->vbi, wait);
3689 if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
3690 /* streaming capture */
3691 if (list_empty(&fh->cap.stream))
3693 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3695 /* read() capture */
3696 mutex_lock(&fh->cap.lock);
3697 if (NULL == fh->cap.read_buf) {
3698 /* need to capture a new frame */
3699 if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM)) {
3700 mutex_unlock(&fh->cap.lock);
3703 fh->cap.read_buf = videobuf_pci_alloc(fh->cap.msize);
3704 if (NULL == fh->cap.read_buf) {
3705 mutex_unlock(&fh->cap.lock);
3708 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3709 field = videobuf_next_field(&fh->cap);
3710 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3711 kfree (fh->cap.read_buf);
3712 fh->cap.read_buf = NULL;
3713 mutex_unlock(&fh->cap.lock);
3716 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3717 fh->cap.read_off = 0;
3719 mutex_unlock(&fh->cap.lock);
3720 buf = (struct bttv_buffer*)fh->cap.read_buf;
3723 poll_wait(file, &buf->vb.done, wait);
3724 if (buf->vb.state == STATE_DONE ||
3725 buf->vb.state == STATE_ERROR)
3726 return POLLIN|POLLRDNORM;
3730 static int bttv_open(struct inode *inode, struct file *file)
3732 int minor = iminor(inode);
3733 struct bttv *btv = NULL;
3735 enum v4l2_buf_type type = 0;
3738 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3740 for (i = 0; i < bttv_num; i++) {
3741 if (bttvs[i].video_dev &&
3742 bttvs[i].video_dev->minor == minor) {
3744 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3747 if (bttvs[i].vbi_dev &&
3748 bttvs[i].vbi_dev->minor == minor) {
3750 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3757 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3758 btv->c.nr,v4l2_type_names[type]);
3760 /* allocate per filehandle data */
3761 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3764 file->private_data = fh;
3767 fh->ov.setup_ok = 0;
3768 v4l2_prio_open(&btv->prio,&fh->prio);
3770 videobuf_queue_pci_init(&fh->cap, &bttv_video_qops,
3771 btv->c.pci, &btv->s_lock,
3772 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3773 V4L2_FIELD_INTERLACED,
3774 sizeof(struct bttv_buffer),
3776 videobuf_queue_pci_init(&fh->vbi, &bttv_vbi_qops,
3777 btv->c.pci, &btv->s_lock,
3778 V4L2_BUF_TYPE_VBI_CAPTURE,
3780 sizeof(struct bttv_buffer),
3782 i2c_vidiocschan(btv);
3786 /* The V4L2 spec requires one global set of cropping parameters
3787 which only change on request. These are stored in btv->crop[1].
3788 However for compatibility with V4L apps and cropping unaware
3789 V4L2 apps we now reset the cropping parameters as seen through
3790 this fh, which is to say VIDIOC_G_CROP and scaling limit checks
3791 will use btv->crop[0], the default cropping parameters for the
3792 current video standard, and VIDIOC_S_FMT will not implicitely
3793 change the cropping parameters until VIDIOC_S_CROP has been
3795 fh->do_crop = !reset_crop; /* module parameter */
3797 /* Likewise there should be one global set of VBI capture
3798 parameters, but for compatibility with V4L apps and earlier
3799 driver versions each fh has its own parameters. */
3800 bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
3802 bttv_field_count(btv);
3806 static int bttv_release(struct inode *inode, struct file *file)
3808 struct bttv_fh *fh = file->private_data;
3809 struct bttv *btv = fh->btv;
3811 /* turn off overlay */
3812 if (check_btres(fh, RESOURCE_OVERLAY))
3813 bttv_switch_overlay(btv,fh,NULL);
3815 /* stop video capture */
3816 if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
3817 videobuf_streamoff(&fh->cap);
3818 free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
3820 if (fh->cap.read_buf) {
3821 buffer_release(&fh->cap,fh->cap.read_buf);
3822 kfree(fh->cap.read_buf);
3824 if (check_btres(fh, RESOURCE_VIDEO_READ)) {
3825 free_btres(btv, fh, RESOURCE_VIDEO_READ);
3828 /* stop vbi capture */
3829 if (check_btres(fh, RESOURCE_VBI)) {
3830 videobuf_stop(&fh->vbi);
3831 free_btres(btv,fh,RESOURCE_VBI);
3835 videobuf_mmap_free(&fh->cap);
3836 videobuf_mmap_free(&fh->vbi);
3837 v4l2_prio_close(&btv->prio,&fh->prio);
3838 file->private_data = NULL;
3842 bttv_field_count(btv);
3847 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3849 struct bttv_fh *fh = file->private_data;
3851 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3852 fh->btv->c.nr, v4l2_type_names[fh->type],
3853 vma->vm_start, vma->vm_end - vma->vm_start);
3854 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3857 static const struct file_operations bttv_fops =
3859 .owner = THIS_MODULE,
3861 .release = bttv_release,
3862 .ioctl = bttv_ioctl,
3863 .compat_ioctl = v4l_compat_ioctl32,
3864 .llseek = no_llseek,
3870 static struct video_device bttv_video_template =
3873 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3874 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3879 static struct video_device bttv_vbi_template =
3881 .name = "bt848/878 vbi",
3882 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3887 /* ----------------------------------------------------------------------- */
3888 /* radio interface */
3890 static int radio_open(struct inode *inode, struct file *file)
3892 int minor = iminor(inode);
3893 struct bttv *btv = NULL;
3896 dprintk("bttv: open minor=%d\n",minor);
3898 for (i = 0; i < bttv_num; i++) {
3899 if (bttvs[i].radio_dev->minor == minor) {
3907 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3908 mutex_lock(&btv->lock);
3912 file->private_data = btv;
3914 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3915 audio_input(btv,TVAUDIO_INPUT_RADIO);
3917 mutex_unlock(&btv->lock);
3921 static int radio_release(struct inode *inode, struct file *file)
3923 struct bttv *btv = file->private_data;
3924 struct rds_command cmd;
3928 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3933 static int radio_do_ioctl(struct inode *inode, struct file *file,
3934 unsigned int cmd, void *arg)
3936 struct bttv *btv = file->private_data;
3941 struct video_capability *cap = arg;
3943 memset(cap,0,sizeof(*cap));
3944 strcpy(cap->name,btv->radio_dev->name);
3945 cap->type = VID_TYPE_TUNER;
3953 struct video_tuner *v = arg;
3957 memset(v,0,sizeof(*v));
3958 strcpy(v->name, "Radio");
3959 bttv_call_i2c_clients(btv,cmd,v);
3971 case VIDIOC_LOG_STATUS:
3972 case VIDIOC_DBG_G_REGISTER:
3973 case VIDIOC_DBG_S_REGISTER:
3974 return bttv_common_ioctls(btv,cmd,arg);
3977 return -ENOIOCTLCMD;
3982 static int radio_ioctl(struct inode *inode, struct file *file,
3983 unsigned int cmd, unsigned long arg)
3985 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3988 static ssize_t radio_read(struct file *file, char __user *data,
3989 size_t count, loff_t *ppos)
3991 struct bttv *btv = file->private_data;
3992 struct rds_command cmd;
3993 cmd.block_count = count/3;
3995 cmd.instance = file;
3996 cmd.result = -ENODEV;
3998 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
4003 static unsigned int radio_poll(struct file *file, poll_table *wait)
4005 struct bttv *btv = file->private_data;
4006 struct rds_command cmd;
4007 cmd.instance = file;
4008 cmd.event_list = wait;
4009 cmd.result = -ENODEV;
4010 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
4015 static const struct file_operations radio_fops =
4017 .owner = THIS_MODULE,
4020 .release = radio_release,
4021 .ioctl = radio_ioctl,
4022 .llseek = no_llseek,
4026 static struct video_device radio_template =
4028 .name = "bt848/878 radio",
4029 .type = VID_TYPE_TUNER,
4030 .fops = &radio_fops,
4034 /* ----------------------------------------------------------------------- */
4035 /* some debug code */
4037 static int bttv_risc_decode(u32 risc)
4039 static char *instr[16] = {
4040 [ BT848_RISC_WRITE >> 28 ] = "write",
4041 [ BT848_RISC_SKIP >> 28 ] = "skip",
4042 [ BT848_RISC_WRITEC >> 28 ] = "writec",
4043 [ BT848_RISC_JUMP >> 28 ] = "jump",
4044 [ BT848_RISC_SYNC >> 28 ] = "sync",
4045 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
4046 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
4047 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
4049 static int incr[16] = {
4050 [ BT848_RISC_WRITE >> 28 ] = 2,
4051 [ BT848_RISC_JUMP >> 28 ] = 2,
4052 [ BT848_RISC_SYNC >> 28 ] = 2,
4053 [ BT848_RISC_WRITE123 >> 28 ] = 5,
4054 [ BT848_RISC_SKIP123 >> 28 ] = 2,
4055 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
4057 static char *bits[] = {
4058 "be0", "be1", "be2", "be3/resync",
4059 "set0", "set1", "set2", "set3",
4060 "clr0", "clr1", "clr2", "clr3",
4061 "irq", "res", "eol", "sol",
4065 printk("0x%08x [ %s", risc,
4066 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
4067 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
4068 if (risc & (1 << (i + 12)))
4069 printk(" %s",bits[i]);
4070 printk(" count=%d ]\n", risc & 0xfff);
4071 return incr[risc >> 28] ? incr[risc >> 28] : 1;
4074 static void bttv_risc_disasm(struct bttv *btv,
4075 struct btcx_riscmem *risc)
4079 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
4080 btv->c.name, risc->cpu, (unsigned long)risc->dma);
4081 for (i = 0; i < (risc->size >> 2); i += n) {
4082 printk("%s: 0x%lx: ", btv->c.name,
4083 (unsigned long)(risc->dma + (i<<2)));
4084 n = bttv_risc_decode(risc->cpu[i]);
4085 for (j = 1; j < n; j++)
4086 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
4087 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
4089 if (0 == risc->cpu[i])
4094 static void bttv_print_riscaddr(struct bttv *btv)
4096 printk(" main: %08Lx\n",
4097 (unsigned long long)btv->main.dma);
4098 printk(" vbi : o=%08Lx e=%08Lx\n",
4099 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
4100 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
4101 printk(" cap : o=%08Lx e=%08Lx\n",
4102 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
4103 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
4104 printk(" scr : o=%08Lx e=%08Lx\n",
4105 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
4106 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
4107 bttv_risc_disasm(btv, &btv->main);
4110 /* ----------------------------------------------------------------------- */
4113 static char *irq_name[] = {
4114 "FMTCHG", // format change detected (525 vs. 625)
4115 "VSYNC", // vertical sync (new field)
4116 "HSYNC", // horizontal sync
4117 "OFLOW", // chroma/luma AGC overflow
4118 "HLOCK", // horizontal lock changed
4119 "VPRES", // video presence changed
4121 "I2CDONE", // hw irc operation finished
4122 "GPINT", // gpio port triggered irq
4124 "RISCI", // risc instruction triggered irq
4125 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
4126 "FTRGT", // pixel data fifo overrun
4127 "FDSR", // fifo data stream resyncronisation
4128 "PPERR", // parity error (data transfer)
4129 "RIPERR", // parity error (read risc instructions)
4130 "PABORT", // pci abort
4131 "OCERR", // risc instruction error
4132 "SCERR", // syncronisation error
4135 static void bttv_print_irqbits(u32 print, u32 mark)
4140 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
4141 if (print & (1 << i))
4142 printk(" %s",irq_name[i]);
4143 if (mark & (1 << i))
4148 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
4150 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
4152 (unsigned long)btv->main.dma,
4153 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
4154 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
4157 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
4158 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
4159 "Ok, then this is harmless, don't worry ;)\n",
4163 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
4165 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
4171 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
4173 struct bttv_buffer *item;
4175 memset(set,0,sizeof(*set));
4177 /* capture request ? */
4178 if (!list_empty(&btv->capture)) {
4180 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4181 if (V4L2_FIELD_HAS_TOP(item->vb.field))
4183 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
4186 /* capture request for other field ? */
4187 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
4188 (item->vb.queue.next != &btv->capture)) {
4189 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
4190 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
4191 if (NULL == set->top &&
4192 V4L2_FIELD_TOP == item->vb.field) {
4195 if (NULL == set->bottom &&
4196 V4L2_FIELD_BOTTOM == item->vb.field) {
4199 if (NULL != set->top && NULL != set->bottom)
4205 /* screen overlay ? */
4206 if (NULL != btv->screen) {
4207 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
4208 if (NULL == set->top && NULL == set->bottom) {
4209 set->top = btv->screen;
4210 set->bottom = btv->screen;
4213 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
4215 set->top = btv->screen;
4217 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
4218 NULL == set->bottom) {
4219 set->bottom = btv->screen;
4224 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
4225 btv->c.nr,set->top, set->bottom,
4226 btv->screen,set->frame_irq,set->top_irq);
4231 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
4232 struct bttv_buffer_set *curr, unsigned int state)
4236 do_gettimeofday(&ts);
4238 if (wakeup->top == wakeup->bottom) {
4239 if (NULL != wakeup->top && curr->top != wakeup->top) {
4241 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
4242 wakeup->top->vb.ts = ts;
4243 wakeup->top->vb.field_count = btv->field_count;
4244 wakeup->top->vb.state = state;
4245 wake_up(&wakeup->top->vb.done);
4248 if (NULL != wakeup->top && curr->top != wakeup->top) {
4250 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
4251 wakeup->top->vb.ts = ts;
4252 wakeup->top->vb.field_count = btv->field_count;
4253 wakeup->top->vb.state = state;
4254 wake_up(&wakeup->top->vb.done);
4256 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
4258 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
4259 wakeup->bottom->vb.ts = ts;
4260 wakeup->bottom->vb.field_count = btv->field_count;
4261 wakeup->bottom->vb.state = state;
4262 wake_up(&wakeup->bottom->vb.done);
4268 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
4276 do_gettimeofday(&ts);
4278 wakeup->vb.field_count = btv->field_count;
4279 wakeup->vb.state = state;
4280 wake_up(&wakeup->vb.done);
4283 static void bttv_irq_timeout(unsigned long data)
4285 struct bttv *btv = (struct bttv *)data;
4286 struct bttv_buffer_set old,new;
4287 struct bttv_buffer *ovbi;
4288 struct bttv_buffer *item;
4289 unsigned long flags;
4292 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
4293 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
4294 btread(BT848_RISC_COUNT));
4295 bttv_print_irqbits(btread(BT848_INT_STAT),0);
4299 spin_lock_irqsave(&btv->s_lock,flags);
4301 /* deactivate stuff */
4302 memset(&new,0,sizeof(new));
4308 bttv_buffer_activate_video(btv, &new);
4309 bttv_buffer_activate_vbi(btv, NULL);
4310 bttv_set_dma(btv, 0);
4313 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
4314 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
4316 /* cancel all outstanding capture / vbi requests */
4317 while (!list_empty(&btv->capture)) {
4318 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
4319 list_del(&item->vb.queue);
4320 item->vb.state = STATE_ERROR;
4321 wake_up(&item->vb.done);
4323 while (!list_empty(&btv->vcapture)) {
4324 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4325 list_del(&item->vb.queue);
4326 item->vb.state = STATE_ERROR;
4327 wake_up(&item->vb.done);
4331 spin_unlock_irqrestore(&btv->s_lock,flags);
4335 bttv_irq_wakeup_top(struct bttv *btv)
4337 struct bttv_buffer *wakeup = btv->curr.top;
4342 spin_lock(&btv->s_lock);
4343 btv->curr.top_irq = 0;
4344 btv->curr.top = NULL;
4345 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
4347 do_gettimeofday(&wakeup->vb.ts);
4348 wakeup->vb.field_count = btv->field_count;
4349 wakeup->vb.state = STATE_DONE;
4350 wake_up(&wakeup->vb.done);
4351 spin_unlock(&btv->s_lock);
4354 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
4358 if (rc > risc->dma + risc->size)
4364 bttv_irq_switch_video(struct bttv *btv)
4366 struct bttv_buffer_set new;
4367 struct bttv_buffer_set old;
4370 spin_lock(&btv->s_lock);
4372 /* new buffer set */
4373 bttv_irq_next_video(btv, &new);
4374 rc = btread(BT848_RISC_COUNT);
4375 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
4376 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
4379 bttv_irq_debug_low_latency(btv, rc);
4380 spin_unlock(&btv->s_lock);
4387 btv->loop_irq &= ~1;
4388 bttv_buffer_activate_video(btv, &new);
4389 bttv_set_dma(btv, 0);
4392 if (UNSET != btv->new_input) {
4393 video_mux(btv,btv->new_input);
4394 btv->new_input = UNSET;
4397 /* wake up finished buffers */
4398 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
4399 spin_unlock(&btv->s_lock);
4403 bttv_irq_switch_vbi(struct bttv *btv)
4405 struct bttv_buffer *new = NULL;
4406 struct bttv_buffer *old;
4409 spin_lock(&btv->s_lock);
4411 if (!list_empty(&btv->vcapture))
4412 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
4415 rc = btread(BT848_RISC_COUNT);
4416 if (NULL != old && (is_active(&old->top, rc) ||
4417 is_active(&old->bottom, rc))) {
4420 bttv_irq_debug_low_latency(btv, rc);
4421 spin_unlock(&btv->s_lock);
4427 btv->loop_irq &= ~4;
4428 bttv_buffer_activate_vbi(btv, new);
4429 bttv_set_dma(btv, 0);
4431 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
4432 spin_unlock(&btv->s_lock);
4435 static irqreturn_t bttv_irq(int irq, void *dev_id)
4443 btv=(struct bttv *)dev_id;
4445 if (btv->custom_irq)
4446 handled = btv->custom_irq(btv);
4450 /* get/clear interrupt status bits */
4451 stat=btread(BT848_INT_STAT);
4452 astat=stat&btread(BT848_INT_MASK);
4456 btwrite(stat,BT848_INT_STAT);
4458 /* get device status bits */
4459 dstat=btread(BT848_DSTATUS);
4462 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
4463 "riscs=%x, riscc=%08x, ",
4464 btv->c.nr, count, btv->field_count,
4465 stat>>28, btread(BT848_RISC_COUNT));
4466 bttv_print_irqbits(stat,astat);
4467 if (stat & BT848_INT_HLOCK)
4468 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
4470 if (stat & BT848_INT_VPRES)
4471 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
4473 if (stat & BT848_INT_FMTCHG)
4474 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
4479 if (astat&BT848_INT_VSYNC)
4482 if ((astat & BT848_INT_GPINT) && btv->remote) {
4483 wake_up(&btv->gpioq);
4484 bttv_input_irq(btv);
4487 if (astat & BT848_INT_I2CDONE) {
4488 btv->i2c_done = stat;
4489 wake_up(&btv->i2c_queue);
4492 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
4493 bttv_irq_switch_vbi(btv);
4495 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
4496 bttv_irq_wakeup_top(btv);
4498 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
4499 bttv_irq_switch_video(btv);
4501 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
4502 audio_mute(btv, btv->mute); /* trigger automute */
4504 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
4505 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
4506 (astat & BT848_INT_SCERR) ? "SCERR" : "",
4507 (astat & BT848_INT_OCERR) ? "OCERR" : "",
4508 btread(BT848_RISC_COUNT));
4509 bttv_print_irqbits(stat,astat);
4512 bttv_print_riscaddr(btv);
4514 if (fdsr && astat & BT848_INT_FDSR) {
4515 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
4516 btv->c.nr,btread(BT848_RISC_COUNT));
4518 bttv_print_riscaddr(btv);
4524 if (count > 8 || !(astat & BT848_INT_GPINT)) {
4525 btwrite(0, BT848_INT_MASK);
4528 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
4531 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
4533 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
4537 bttv_print_irqbits(stat,astat);
4545 return IRQ_RETVAL(handled);
4549 /* ----------------------------------------------------------------------- */
4550 /* initialitation */
4552 static struct video_device *vdev_init(struct bttv *btv,
4553 struct video_device *template,
4556 struct video_device *vfd;
4558 vfd = video_device_alloc();
4563 vfd->dev = &btv->c.pci->dev;
4564 vfd->release = video_device_release;
4565 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
4566 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
4567 type, bttv_tvcards[btv->c.type].name);
4571 static void bttv_unregister_video(struct bttv *btv)
4573 if (btv->video_dev) {
4574 if (-1 != btv->video_dev->minor)
4575 video_unregister_device(btv->video_dev);
4577 video_device_release(btv->video_dev);
4578 btv->video_dev = NULL;
4581 if (-1 != btv->vbi_dev->minor)
4582 video_unregister_device(btv->vbi_dev);
4584 video_device_release(btv->vbi_dev);
4585 btv->vbi_dev = NULL;
4587 if (btv->radio_dev) {
4588 if (-1 != btv->radio_dev->minor)
4589 video_unregister_device(btv->radio_dev);
4591 video_device_release(btv->radio_dev);
4592 btv->radio_dev = NULL;
4596 /* register video4linux devices */
4597 static int __devinit bttv_register_video(struct bttv *btv)
4599 if (no_overlay <= 0) {
4600 bttv_video_template.type |= VID_TYPE_OVERLAY;
4602 printk("bttv: Overlay support disabled.\n");
4606 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
4607 if (NULL == btv->video_dev)
4609 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
4611 printk(KERN_INFO "bttv%d: registered device video%d\n",
4612 btv->c.nr,btv->video_dev->minor & 0x1f);
4613 if (device_create_file(&btv->video_dev->class_dev,
4614 &dev_attr_card)<0) {
4615 printk(KERN_ERR "bttv%d: device_create_file 'card' "
4616 "failed\n", btv->c.nr);
4621 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
4622 if (NULL == btv->vbi_dev)
4624 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
4626 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
4627 btv->c.nr,btv->vbi_dev->minor & 0x1f);
4629 if (!btv->has_radio)
4632 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
4633 if (NULL == btv->radio_dev)
4635 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
4637 printk(KERN_INFO "bttv%d: registered device radio%d\n",
4638 btv->c.nr,btv->radio_dev->minor & 0x1f);
4644 bttv_unregister_video(btv);
4649 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4650 /* response on cards with no firmware is not enabled by OF */
4651 static void pci_set_command(struct pci_dev *dev)
4653 #if defined(__powerpc__)
4656 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4657 cmd = (cmd | PCI_COMMAND_MEMORY );
4658 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4662 static int __devinit bttv_probe(struct pci_dev *dev,
4663 const struct pci_device_id *pci_id)
4669 if (bttv_num == BTTV_MAX)
4671 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
4672 btv=&bttvs[bttv_num];
4673 memset(btv,0,sizeof(*btv));
4674 btv->c.nr = bttv_num;
4675 sprintf(btv->c.name,"bttv%d",btv->c.nr);
4677 /* initialize structs / fill in defaults */
4678 mutex_init(&btv->lock);
4679 spin_lock_init(&btv->s_lock);
4680 spin_lock_init(&btv->gpio_lock);
4681 init_waitqueue_head(&btv->gpioq);
4682 init_waitqueue_head(&btv->i2c_queue);
4683 INIT_LIST_HEAD(&btv->c.subs);
4684 INIT_LIST_HEAD(&btv->capture);
4685 INIT_LIST_HEAD(&btv->vcapture);
4686 v4l2_prio_init(&btv->prio);
4688 init_timer(&btv->timeout);
4689 btv->timeout.function = bttv_irq_timeout;
4690 btv->timeout.data = (unsigned long)btv;
4693 btv->tuner_type = UNSET;
4694 btv->new_input = UNSET;
4695 btv->has_radio=radio[btv->c.nr];
4697 /* pci stuff (init, get irq/mmio, ... */
4699 btv->id = dev->device;
4700 if (pci_enable_device(dev)) {
4701 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4705 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
4706 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4710 if (!request_mem_region(pci_resource_start(dev,0),
4711 pci_resource_len(dev,0),
4713 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4715 (unsigned long long)pci_resource_start(dev,0));
4718 pci_set_master(dev);
4719 pci_set_command(dev);
4720 pci_set_drvdata(dev,btv);
4722 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4723 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4724 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4725 bttv_num,btv->id, btv->revision, pci_name(dev));
4726 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4727 btv->c.pci->irq, lat,
4728 (unsigned long long)pci_resource_start(dev,0));
4731 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4732 if (NULL == btv->bt848_mmio) {
4733 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4741 /* disable irqs, register irq handler */
4742 btwrite(0, BT848_INT_MASK);
4743 result = request_irq(btv->c.pci->irq, bttv_irq,
4744 IRQF_SHARED | IRQF_DISABLED,btv->c.name,(void *)btv);
4746 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4747 bttv_num,btv->c.pci->irq);
4751 if (0 != bttv_handle_chipset(btv)) {
4756 /* init options from insmod args */
4757 btv->opt_combfilter = combfilter;
4758 btv->opt_lumafilter = lumafilter;
4759 btv->opt_automute = automute;
4760 btv->opt_chroma_agc = chroma_agc;
4761 btv->opt_adc_crush = adc_crush;
4762 btv->opt_vcr_hack = vcr_hack;
4763 btv->opt_whitecrush_upper = whitecrush_upper;
4764 btv->opt_whitecrush_lower = whitecrush_lower;
4765 btv->opt_uv_ratio = uv_ratio;
4766 btv->opt_full_luma_range = full_luma_range;
4767 btv->opt_coring = coring;
4769 /* fill struct bttv with some useful defaults */
4770 btv->init.btv = btv;
4771 btv->init.ov.w.width = 320;
4772 btv->init.ov.w.height = 240;
4773 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4774 btv->init.width = 320;
4775 btv->init.height = 240;
4778 /* initialize hardware */
4780 bttv_gpio_tracking(btv,"pre-init");
4782 bttv_risc_init_main(btv);
4786 btwrite(0x00, BT848_GPIO_REG_INP);
4787 btwrite(0x00, BT848_GPIO_OUT_EN);
4789 bttv_gpio_tracking(btv,"init");
4791 /* needs to be done before i2c is registered */
4792 bttv_init_card1(btv);
4794 /* register i2c + gpio */
4797 /* some card-specific stuff (needs working i2c) */
4798 bttv_init_card2(btv);
4801 /* register video4linux + input */
4802 if (!bttv_tvcards[btv->c.type].no_video) {
4803 bttv_register_video(btv);
4804 bt848_bright(btv,32768);
4805 bt848_contrast(btv,32768);
4806 bt848_hue(btv,32768);
4807 bt848_sat(btv,32768);
4809 set_input(btv, 0, btv->tvnorm);
4810 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
4811 btv->crop[1] = btv->crop[0]; /* current = default */
4812 disclaim_vbi_lines(btv);
4813 disclaim_video_lines(btv);
4816 /* add subdevices and autoload dvb-bt8xx if needed */
4817 if (bttv_tvcards[btv->c.type].has_dvb) {
4818 bttv_sub_add_device(&btv->c, "dvb");
4819 request_modules(btv);
4822 bttv_input_init(btv);
4824 /* everything is fine */
4829 free_irq(btv->c.pci->irq,btv);
4832 if (btv->bt848_mmio)
4833 iounmap(btv->bt848_mmio);
4834 release_mem_region(pci_resource_start(btv->c.pci,0),
4835 pci_resource_len(btv->c.pci,0));
4836 pci_set_drvdata(dev,NULL);
4840 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4842 struct bttv *btv = pci_get_drvdata(pci_dev);
4845 printk("bttv%d: unloading\n",btv->c.nr);
4847 /* shutdown everything (DMA+IRQs) */
4848 btand(~15, BT848_GPIO_DMA_CTL);
4849 btwrite(0, BT848_INT_MASK);
4850 btwrite(~0x0, BT848_INT_STAT);
4851 btwrite(0x0, BT848_GPIO_OUT_EN);
4853 bttv_gpio_tracking(btv,"cleanup");
4855 /* tell gpio modules we are leaving ... */
4857 wake_up(&btv->gpioq);
4858 bttv_input_fini(btv);
4859 bttv_sub_del_devices(&btv->c);
4861 /* unregister i2c_bus + input */
4864 /* unregister video4linux */
4865 bttv_unregister_video(btv);
4867 /* free allocated memory */
4868 btcx_riscmem_free(btv->c.pci,&btv->main);
4870 /* free ressources */
4871 free_irq(btv->c.pci->irq,btv);
4872 iounmap(btv->bt848_mmio);
4873 release_mem_region(pci_resource_start(btv->c.pci,0),
4874 pci_resource_len(btv->c.pci,0));
4876 pci_set_drvdata(pci_dev, NULL);
4881 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4883 struct bttv *btv = pci_get_drvdata(pci_dev);
4884 struct bttv_buffer_set idle;
4885 unsigned long flags;
4887 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4889 /* stop dma + irqs */
4890 spin_lock_irqsave(&btv->s_lock,flags);
4891 memset(&idle, 0, sizeof(idle));
4892 btv->state.video = btv->curr;
4893 btv->state.vbi = btv->cvbi;
4894 btv->state.loop_irq = btv->loop_irq;
4897 bttv_buffer_activate_video(btv, &idle);
4898 bttv_buffer_activate_vbi(btv, NULL);
4899 bttv_set_dma(btv, 0);
4900 btwrite(0, BT848_INT_MASK);
4901 spin_unlock_irqrestore(&btv->s_lock,flags);
4903 /* save bt878 state */
4904 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4905 btv->state.gpio_data = gpio_read();
4907 /* save pci state */
4908 pci_save_state(pci_dev);
4909 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4910 pci_disable_device(pci_dev);
4911 btv->state.disabled = 1;
4916 static int bttv_resume(struct pci_dev *pci_dev)
4918 struct bttv *btv = pci_get_drvdata(pci_dev);
4919 unsigned long flags;
4922 dprintk("bttv%d: resume\n", btv->c.nr);
4924 /* restore pci state */
4925 if (btv->state.disabled) {
4926 err=pci_enable_device(pci_dev);
4928 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4932 btv->state.disabled = 0;
4934 err=pci_set_power_state(pci_dev, PCI_D0);
4936 pci_disable_device(pci_dev);
4937 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4939 btv->state.disabled = 1;
4943 pci_restore_state(pci_dev);
4945 /* restore bt878 state */
4946 bttv_reinit_bt848(btv);
4947 gpio_inout(0xffffff, btv->state.gpio_enable);
4948 gpio_write(btv->state.gpio_data);
4951 spin_lock_irqsave(&btv->s_lock,flags);
4952 btv->curr = btv->state.video;
4953 btv->cvbi = btv->state.vbi;
4954 btv->loop_irq = btv->state.loop_irq;
4955 bttv_buffer_activate_video(btv, &btv->curr);
4956 bttv_buffer_activate_vbi(btv, btv->cvbi);
4957 bttv_set_dma(btv, 0);
4958 spin_unlock_irqrestore(&btv->s_lock,flags);
4963 static struct pci_device_id bttv_pci_tbl[] = {
4964 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4965 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4966 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4967 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4968 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4969 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4970 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4971 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4975 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4977 static struct pci_driver bttv_pci_driver = {
4979 .id_table = bttv_pci_tbl,
4980 .probe = bttv_probe,
4981 .remove = __devexit_p(bttv_remove),
4983 .suspend = bttv_suspend,
4984 .resume = bttv_resume,
4988 static int __init bttv_init_module(void)
4994 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4995 (BTTV_VERSION_CODE >> 16) & 0xff,
4996 (BTTV_VERSION_CODE >> 8) & 0xff,
4997 BTTV_VERSION_CODE & 0xff);
4999 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
5000 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
5002 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
5004 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
5005 gbufsize = BTTV_MAX_FBUF;
5006 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
5008 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
5009 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
5011 bttv_check_chipset();
5013 ret = bus_register(&bttv_sub_bus_type);
5015 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
5018 return pci_register_driver(&bttv_pci_driver);
5021 static void __exit bttv_cleanup_module(void)
5023 pci_unregister_driver(&bttv_pci_driver);
5024 bus_unregister(&bttv_sub_bus_type);
5028 module_init(bttv_init_module);
5029 module_exit(bttv_cleanup_module);