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 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
37 #include <linux/dma-mapping.h>
40 #include <asm/byteorder.h>
47 unsigned int bttv_num; /* number of Bt848s in use */
48 struct bttv bttvs[BTTV_MAX];
50 unsigned int bttv_debug = 0;
51 unsigned int bttv_verbose = 1;
52 unsigned int bttv_gpio = 0;
54 /* config variables */
56 static unsigned int bigendian=1;
58 static unsigned int bigendian=0;
60 static unsigned int radio[BTTV_MAX];
61 static unsigned int irq_debug = 0;
62 static unsigned int gbuffers = 8;
63 static unsigned int gbufsize = 0x208000;
65 static int video_nr = -1;
66 static int radio_nr = -1;
67 static int vbi_nr = -1;
68 static int debug_latency = 0;
70 static unsigned int fdsr = 0;
73 static unsigned int combfilter = 0;
74 static unsigned int lumafilter = 0;
75 static unsigned int automute = 1;
76 static unsigned int chroma_agc = 0;
77 static unsigned int adc_crush = 1;
78 static unsigned int whitecrush_upper = 0xCF;
79 static unsigned int whitecrush_lower = 0x7F;
80 static unsigned int vcr_hack = 0;
81 static unsigned int irq_iswitch = 0;
82 static unsigned int uv_ratio = 50;
83 static unsigned int full_luma_range = 0;
84 static unsigned int coring = 0;
85 extern int no_overlay;
87 /* API features (turn on/off stuff for testing) */
88 static unsigned int v4l2 = 1;
92 module_param(bttv_verbose, int, 0644);
93 module_param(bttv_gpio, int, 0644);
94 module_param(bttv_debug, int, 0644);
95 module_param(irq_debug, int, 0644);
96 module_param(debug_latency, int, 0644);
98 module_param(fdsr, int, 0444);
99 module_param(video_nr, int, 0444);
100 module_param(radio_nr, int, 0444);
101 module_param(vbi_nr, int, 0444);
102 module_param(gbuffers, int, 0444);
103 module_param(gbufsize, int, 0444);
105 module_param(v4l2, int, 0644);
106 module_param(bigendian, int, 0644);
107 module_param(irq_iswitch, int, 0644);
108 module_param(combfilter, int, 0444);
109 module_param(lumafilter, int, 0444);
110 module_param(automute, int, 0444);
111 module_param(chroma_agc, int, 0444);
112 module_param(adc_crush, int, 0444);
113 module_param(whitecrush_upper, int, 0444);
114 module_param(whitecrush_lower, int, 0444);
115 module_param(vcr_hack, int, 0444);
116 module_param(uv_ratio, int, 0444);
117 module_param(full_luma_range, int, 0444);
118 module_param(coring, int, 0444);
120 module_param_array(radio, int, NULL, 0444);
122 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
123 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
124 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
125 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
126 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
127 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
128 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
129 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
130 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
131 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
132 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
133 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
134 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
135 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
136 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
137 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
138 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
139 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
141 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
142 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
143 MODULE_LICENSE("GPL");
145 /* ----------------------------------------------------------------------- */
148 static ssize_t show_card(struct class_device *cd, char *buf)
150 struct video_device *vfd = to_video_device(cd);
151 struct bttv *btv = dev_get_drvdata(vfd->dev);
152 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
154 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
156 /* ----------------------------------------------------------------------- */
159 /* special timing tables from conexant... */
160 static u8 SRAM_Table[][60] =
162 /* PAL digital input over GPIO[7:0] */
164 45, // 45 bytes following
165 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
166 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
167 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
168 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
169 0x37,0x00,0xAF,0x21,0x00
171 /* NTSC digital input over GPIO[7:0] */
173 51, // 51 bytes following
174 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
175 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
176 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
177 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
178 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
181 // TGB_NTSC392 // quartzsight
182 // This table has been modified to be used for Fusion Rev D
184 0x2A, // size of table = 42
185 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
186 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
187 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
188 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
193 const struct bttv_tvnorm bttv_tvnorms[] = {
195 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
196 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
198 .v4l2_id = V4L2_STD_PAL,
206 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
207 .scaledtwidth = 1135,
214 .v4l2_id = V4L2_STD_NTSC_M,
222 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
230 .v4l2_id = V4L2_STD_SECAM,
238 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
239 .scaledtwidth = 1135,
244 .sram = 0, /* like PAL, correct? */
246 .v4l2_id = V4L2_STD_PAL_Nc,
254 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
262 .v4l2_id = V4L2_STD_PAL_M,
270 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
278 .v4l2_id = V4L2_STD_PAL_N,
286 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
294 .v4l2_id = V4L2_STD_NTSC_M_JP,
302 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
310 /* that one hopefully works with the strange timing
311 * which video recorders produce when playing a NTSC
312 * tape on a PAL TV ... */
313 .v4l2_id = V4L2_STD_PAL_60,
321 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
322 .scaledtwidth = 1135,
331 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
333 /* ----------------------------------------------------------------------- */
335 packed pixel formats must come first */
336 static const struct bttv_format bttv_formats[] = {
338 .name = "8 bpp, gray",
339 .palette = VIDEO_PALETTE_GREY,
340 .fourcc = V4L2_PIX_FMT_GREY,
341 .btformat = BT848_COLOR_FMT_Y8,
343 .flags = FORMAT_FLAGS_PACKED,
345 .name = "8 bpp, dithered color",
346 .palette = VIDEO_PALETTE_HI240,
347 .fourcc = V4L2_PIX_FMT_HI240,
348 .btformat = BT848_COLOR_FMT_RGB8,
350 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
352 .name = "15 bpp RGB, le",
353 .palette = VIDEO_PALETTE_RGB555,
354 .fourcc = V4L2_PIX_FMT_RGB555,
355 .btformat = BT848_COLOR_FMT_RGB15,
357 .flags = FORMAT_FLAGS_PACKED,
359 .name = "15 bpp RGB, be",
361 .fourcc = V4L2_PIX_FMT_RGB555X,
362 .btformat = BT848_COLOR_FMT_RGB15,
363 .btswap = 0x03, /* byteswap */
365 .flags = FORMAT_FLAGS_PACKED,
367 .name = "16 bpp RGB, le",
368 .palette = VIDEO_PALETTE_RGB565,
369 .fourcc = V4L2_PIX_FMT_RGB565,
370 .btformat = BT848_COLOR_FMT_RGB16,
372 .flags = FORMAT_FLAGS_PACKED,
374 .name = "16 bpp RGB, be",
376 .fourcc = V4L2_PIX_FMT_RGB565X,
377 .btformat = BT848_COLOR_FMT_RGB16,
378 .btswap = 0x03, /* byteswap */
380 .flags = FORMAT_FLAGS_PACKED,
382 .name = "24 bpp RGB, le",
383 .palette = VIDEO_PALETTE_RGB24,
384 .fourcc = V4L2_PIX_FMT_BGR24,
385 .btformat = BT848_COLOR_FMT_RGB24,
387 .flags = FORMAT_FLAGS_PACKED,
389 .name = "32 bpp RGB, le",
390 .palette = VIDEO_PALETTE_RGB32,
391 .fourcc = V4L2_PIX_FMT_BGR32,
392 .btformat = BT848_COLOR_FMT_RGB32,
394 .flags = FORMAT_FLAGS_PACKED,
396 .name = "32 bpp RGB, be",
398 .fourcc = V4L2_PIX_FMT_RGB32,
399 .btformat = BT848_COLOR_FMT_RGB32,
400 .btswap = 0x0f, /* byte+word swap */
402 .flags = FORMAT_FLAGS_PACKED,
404 .name = "4:2:2, packed, YUYV",
405 .palette = VIDEO_PALETTE_YUV422,
406 .fourcc = V4L2_PIX_FMT_YUYV,
407 .btformat = BT848_COLOR_FMT_YUY2,
409 .flags = FORMAT_FLAGS_PACKED,
411 .name = "4:2:2, packed, YUYV",
412 .palette = VIDEO_PALETTE_YUYV,
413 .fourcc = V4L2_PIX_FMT_YUYV,
414 .btformat = BT848_COLOR_FMT_YUY2,
416 .flags = FORMAT_FLAGS_PACKED,
418 .name = "4:2:2, packed, UYVY",
419 .palette = VIDEO_PALETTE_UYVY,
420 .fourcc = V4L2_PIX_FMT_UYVY,
421 .btformat = BT848_COLOR_FMT_YUY2,
422 .btswap = 0x03, /* byteswap */
424 .flags = FORMAT_FLAGS_PACKED,
426 .name = "4:2:2, planar, Y-Cb-Cr",
427 .palette = VIDEO_PALETTE_YUV422P,
428 .fourcc = V4L2_PIX_FMT_YUV422P,
429 .btformat = BT848_COLOR_FMT_YCrCb422,
431 .flags = FORMAT_FLAGS_PLANAR,
435 .name = "4:2:0, planar, Y-Cb-Cr",
436 .palette = VIDEO_PALETTE_YUV420P,
437 .fourcc = V4L2_PIX_FMT_YUV420,
438 .btformat = BT848_COLOR_FMT_YCrCb422,
440 .flags = FORMAT_FLAGS_PLANAR,
444 .name = "4:2:0, planar, Y-Cr-Cb",
446 .fourcc = V4L2_PIX_FMT_YVU420,
447 .btformat = BT848_COLOR_FMT_YCrCb422,
449 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
453 .name = "4:1:1, planar, Y-Cb-Cr",
454 .palette = VIDEO_PALETTE_YUV411P,
455 .fourcc = V4L2_PIX_FMT_YUV411P,
456 .btformat = BT848_COLOR_FMT_YCrCb411,
458 .flags = FORMAT_FLAGS_PLANAR,
462 .name = "4:1:0, planar, Y-Cb-Cr",
463 .palette = VIDEO_PALETTE_YUV410P,
464 .fourcc = V4L2_PIX_FMT_YUV410,
465 .btformat = BT848_COLOR_FMT_YCrCb411,
467 .flags = FORMAT_FLAGS_PLANAR,
471 .name = "4:1:0, planar, Y-Cr-Cb",
473 .fourcc = V4L2_PIX_FMT_YVU410,
474 .btformat = BT848_COLOR_FMT_YCrCb411,
476 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
480 .name = "raw scanlines",
481 .palette = VIDEO_PALETTE_RAW,
483 .btformat = BT848_COLOR_FMT_RAW,
485 .flags = FORMAT_FLAGS_RAW,
488 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
490 /* ----------------------------------------------------------------------- */
492 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
493 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
494 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
495 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
496 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
497 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
498 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
499 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
500 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
501 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
502 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
503 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
505 static const struct v4l2_queryctrl no_ctl = {
507 .flags = V4L2_CTRL_FLAG_DISABLED,
509 static const struct v4l2_queryctrl bttv_ctls[] = {
512 .id = V4L2_CID_BRIGHTNESS,
513 .name = "Brightness",
517 .default_value = 32768,
518 .type = V4L2_CTRL_TYPE_INTEGER,
520 .id = V4L2_CID_CONTRAST,
525 .default_value = 32768,
526 .type = V4L2_CTRL_TYPE_INTEGER,
528 .id = V4L2_CID_SATURATION,
529 .name = "Saturation",
533 .default_value = 32768,
534 .type = V4L2_CTRL_TYPE_INTEGER,
541 .default_value = 32768,
542 .type = V4L2_CTRL_TYPE_INTEGER,
546 .id = V4L2_CID_AUDIO_MUTE,
550 .type = V4L2_CTRL_TYPE_BOOLEAN,
552 .id = V4L2_CID_AUDIO_VOLUME,
557 .default_value = 65535,
558 .type = V4L2_CTRL_TYPE_INTEGER,
560 .id = V4L2_CID_AUDIO_BALANCE,
565 .default_value = 32768,
566 .type = V4L2_CTRL_TYPE_INTEGER,
568 .id = V4L2_CID_AUDIO_BASS,
573 .default_value = 32768,
574 .type = V4L2_CTRL_TYPE_INTEGER,
576 .id = V4L2_CID_AUDIO_TREBLE,
581 .default_value = 32768,
582 .type = V4L2_CTRL_TYPE_INTEGER,
584 /* --- private --- */
586 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
587 .name = "chroma agc",
590 .type = V4L2_CTRL_TYPE_BOOLEAN,
592 .id = V4L2_CID_PRIVATE_COMBFILTER,
593 .name = "combfilter",
596 .type = V4L2_CTRL_TYPE_BOOLEAN,
598 .id = V4L2_CID_PRIVATE_AUTOMUTE,
602 .type = V4L2_CTRL_TYPE_BOOLEAN,
604 .id = V4L2_CID_PRIVATE_LUMAFILTER,
605 .name = "luma decimation filter",
608 .type = V4L2_CTRL_TYPE_BOOLEAN,
610 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
614 .type = V4L2_CTRL_TYPE_BOOLEAN,
616 .id = V4L2_CID_PRIVATE_VCR_HACK,
620 .type = V4L2_CTRL_TYPE_BOOLEAN,
622 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
623 .name = "whitecrush upper",
627 .default_value = 0xCF,
628 .type = V4L2_CTRL_TYPE_INTEGER,
630 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
631 .name = "whitecrush lower",
635 .default_value = 0x7F,
636 .type = V4L2_CTRL_TYPE_INTEGER,
638 .id = V4L2_CID_PRIVATE_UV_RATIO,
644 .type = V4L2_CTRL_TYPE_INTEGER,
646 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
647 .name = "full luma range",
650 .type = V4L2_CTRL_TYPE_BOOLEAN,
652 .id = V4L2_CID_PRIVATE_CORING,
658 .type = V4L2_CTRL_TYPE_INTEGER,
664 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
666 /* ----------------------------------------------------------------------- */
667 /* resource management */
670 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
672 if (fh->resources & bit)
673 /* have it already allocated */
678 if (btv->resources & bit) {
679 /* no, someone else uses it */
683 /* it's free, grab it */
684 fh->resources |= bit;
685 btv->resources |= bit;
691 int check_btres(struct bttv_fh *fh, int bit)
693 return (fh->resources & bit);
697 int locked_btres(struct bttv *btv, int bit)
699 return (btv->resources & bit);
703 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
705 if ((fh->resources & bits) != bits) {
706 /* trying to free ressources not allocated by us ... */
707 printk("bttv: BUG! (btres)\n");
710 fh->resources &= ~bits;
711 btv->resources &= ~bits;
715 /* ----------------------------------------------------------------------- */
716 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
718 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
719 PLL_X = Reference pre-divider (0=1, 1=2)
720 PLL_C = Post divider (0=6, 1=4)
721 PLL_I = Integer input
722 PLL_F = Fractional input
724 F_input = 28.636363 MHz:
725 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
728 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
730 unsigned char fl, fh, fi;
732 /* prevent overflows */
745 btwrite(fl, BT848_PLL_F_LO);
746 btwrite(fh, BT848_PLL_F_HI);
747 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
750 static void set_pll(struct bttv *btv)
754 if (!btv->pll.pll_crystal)
757 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
758 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
762 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
764 if (btv->pll.pll_current == 0)
766 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
767 btv->c.nr,btv->pll.pll_ifreq);
768 btwrite(0x00,BT848_TGCTRL);
769 btwrite(0x00,BT848_PLL_XCI);
770 btv->pll.pll_current = 0;
774 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
775 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
776 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
778 for (i=0; i<10; i++) {
779 /* Let other people run while the PLL stabilizes */
783 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
784 btwrite(0,BT848_DSTATUS);
786 btwrite(0x08,BT848_TGCTRL);
787 btv->pll.pll_current = btv->pll.pll_ofreq;
788 bttv_printk(" ok\n");
792 btv->pll.pll_current = -1;
793 bttv_printk("failed\n");
797 /* used to switch between the bt848's analog/digital video capture modes */
798 static void bt848A_set_timing(struct bttv *btv)
801 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
802 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
804 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
805 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
806 btv->c.nr,table_idx);
808 /* timing change...reset timing generator address */
809 btwrite(0x00, BT848_TGCTRL);
810 btwrite(0x02, BT848_TGCTRL);
811 btwrite(0x00, BT848_TGCTRL);
813 len=SRAM_Table[table_idx][0];
814 for(i = 1; i <= len; i++)
815 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
816 btv->pll.pll_ofreq = 27000000;
819 btwrite(0x11, BT848_TGCTRL);
820 btwrite(0x41, BT848_DVSIF);
822 btv->pll.pll_ofreq = fsc;
824 btwrite(0x0, BT848_DVSIF);
828 /* ----------------------------------------------------------------------- */
830 static void bt848_bright(struct bttv *btv, int bright)
834 // printk("bttv: set bright: %d\n",bright); // DEBUG
835 btv->bright = bright;
837 /* We want -128 to 127 we get 0-65535 */
838 value = (bright >> 8) - 128;
839 btwrite(value & 0xff, BT848_BRIGHT);
842 static void bt848_hue(struct bttv *btv, int hue)
849 value = (hue >> 8) - 128;
850 btwrite(value & 0xff, BT848_HUE);
853 static void bt848_contrast(struct bttv *btv, int cont)
857 btv->contrast = cont;
861 hibit = (value >> 6) & 4;
862 btwrite(value & 0xff, BT848_CONTRAST_LO);
863 btaor(hibit, ~4, BT848_E_CONTROL);
864 btaor(hibit, ~4, BT848_O_CONTROL);
867 static void bt848_sat(struct bttv *btv, int color)
869 int val_u,val_v,hibits;
871 btv->saturation = color;
873 /* 0-511 for the color */
874 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
875 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
876 hibits = (val_u >> 7) & 2;
877 hibits |= (val_v >> 8) & 1;
878 btwrite(val_u & 0xff, BT848_SAT_U_LO);
879 btwrite(val_v & 0xff, BT848_SAT_V_LO);
880 btaor(hibits, ~3, BT848_E_CONTROL);
881 btaor(hibits, ~3, BT848_O_CONTROL);
884 /* ----------------------------------------------------------------------- */
887 video_mux(struct bttv *btv, unsigned int input)
891 if (input >= bttv_tvcards[btv->c.type].video_inputs)
894 /* needed by RemoteVideo MX */
895 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
897 gpio_inout(mask2,mask2);
899 if (input == btv->svhs) {
900 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
901 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
903 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
904 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
906 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
907 btaor(mux<<5, ~(3<<5), BT848_IFORM);
908 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
909 btv->c.nr,input,mux);
911 /* card specific hook */
912 if(bttv_tvcards[btv->c.type].muxsel_hook)
913 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
917 static char *audio_modes[] = {
918 "audio: tuner", "audio: radio", "audio: extern",
919 "audio: intern", "audio: off"
923 audio_mux(struct bttv *btv, int mode)
925 int val,mux,i2c_mux,signal;
927 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
928 bttv_tvcards[btv->c.type].gpiomask);
929 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
933 btv->audio |= AUDIO_MUTE;
936 btv->audio &= ~AUDIO_MUTE;
942 btv->audio &= AUDIO_MUTE;
945 i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
946 if (btv->opt_automute && !signal && !btv->radio_user)
949 val = bttv_tvcards[btv->c.type].audiomux[mux];
950 gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
952 bttv_gpio_tracking(btv,audio_modes[mux]);
954 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
959 i2c_vidiocschan(struct bttv *btv)
961 struct video_channel c;
963 memset(&c,0,sizeof(c));
964 c.norm = btv->tvnorm;
965 c.channel = btv->input;
966 bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
967 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM)
968 bttv_tda9880_setnorm(btv,c.norm);
972 set_tvnorm(struct bttv *btv, unsigned int norm)
974 const struct bttv_tvnorm *tvnorm;
976 if (norm < 0 || norm >= BTTV_TVNORMS)
980 tvnorm = &bttv_tvnorms[norm];
982 btwrite(tvnorm->adelay, BT848_ADELAY);
983 btwrite(tvnorm->bdelay, BT848_BDELAY);
984 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
986 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
987 btwrite(1, BT848_VBI_PACK_DEL);
988 bt848A_set_timing(btv);
990 switch (btv->c.type) {
991 case BTTV_BOARD_VOODOOTV_FM:
992 bttv_tda9880_setnorm(btv,norm);
999 set_input(struct bttv *btv, unsigned int input)
1001 unsigned long flags;
1005 spin_lock_irqsave(&btv->s_lock,flags);
1006 if (btv->curr.frame_irq) {
1007 /* active capture -> delayed input switch */
1008 btv->new_input = input;
1010 video_mux(btv,input);
1012 spin_unlock_irqrestore(&btv->s_lock,flags);
1014 video_mux(btv,input);
1016 audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1017 AUDIO_TUNER : AUDIO_EXTERN));
1018 set_tvnorm(btv,btv->tvnorm);
1019 i2c_vidiocschan(btv);
1022 static void init_irqreg(struct bttv *btv)
1025 btwrite(0xfffffUL, BT848_INT_STAT);
1027 if (bttv_tvcards[btv->c.type].no_video) {
1029 btwrite(BT848_INT_I2CDONE,
1033 btwrite((btv->triton1) |
1034 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1036 (fdsr ? BT848_INT_FDSR : 0) |
1037 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1038 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1044 static void init_bt848(struct bttv *btv)
1048 if (bttv_tvcards[btv->c.type].no_video) {
1049 /* very basic init only */
1054 btwrite(0x00, BT848_CAP_CTL);
1055 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1056 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1058 /* set planar and packed mode trigger points and */
1059 /* set rising edge of inverted GPINTR pin as irq trigger */
1060 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1061 BT848_GPIO_DMA_CTL_PLTP1_16|
1062 BT848_GPIO_DMA_CTL_PLTP23_16|
1063 BT848_GPIO_DMA_CTL_GPINTC|
1064 BT848_GPIO_DMA_CTL_GPINTI,
1065 BT848_GPIO_DMA_CTL);
1067 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1068 btwrite(val, BT848_E_SCLOOP);
1069 btwrite(val, BT848_O_SCLOOP);
1071 btwrite(0x20, BT848_E_VSCALE_HI);
1072 btwrite(0x20, BT848_O_VSCALE_HI);
1073 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1076 btwrite(whitecrush_upper, BT848_WC_UP);
1077 btwrite(whitecrush_lower, BT848_WC_DOWN);
1079 if (btv->opt_lumafilter) {
1080 btwrite(0, BT848_E_CONTROL);
1081 btwrite(0, BT848_O_CONTROL);
1083 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1084 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1087 bt848_bright(btv, btv->bright);
1088 bt848_hue(btv, btv->hue);
1089 bt848_contrast(btv, btv->contrast);
1090 bt848_sat(btv, btv->saturation);
1096 static void bttv_reinit_bt848(struct bttv *btv)
1098 unsigned long flags;
1101 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1102 spin_lock_irqsave(&btv->s_lock,flags);
1104 bttv_set_dma(btv,0);
1105 spin_unlock_irqrestore(&btv->s_lock,flags);
1108 btv->pll.pll_current = -1;
1109 set_input(btv,btv->input);
1112 static int get_control(struct bttv *btv, struct v4l2_control *c)
1114 struct video_audio va;
1117 for (i = 0; i < BTTV_CTLS; i++)
1118 if (bttv_ctls[i].id == c->id)
1122 if (i >= 4 && i <= 8) {
1123 memset(&va,0,sizeof(va));
1124 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1125 if (btv->audio_hook)
1126 btv->audio_hook(btv,&va,0);
1129 case V4L2_CID_BRIGHTNESS:
1130 c->value = btv->bright;
1133 c->value = btv->hue;
1135 case V4L2_CID_CONTRAST:
1136 c->value = btv->contrast;
1138 case V4L2_CID_SATURATION:
1139 c->value = btv->saturation;
1142 case V4L2_CID_AUDIO_MUTE:
1143 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1145 case V4L2_CID_AUDIO_VOLUME:
1146 c->value = va.volume;
1148 case V4L2_CID_AUDIO_BALANCE:
1149 c->value = va.balance;
1151 case V4L2_CID_AUDIO_BASS:
1154 case V4L2_CID_AUDIO_TREBLE:
1155 c->value = va.treble;
1158 case V4L2_CID_PRIVATE_CHROMA_AGC:
1159 c->value = btv->opt_chroma_agc;
1161 case V4L2_CID_PRIVATE_COMBFILTER:
1162 c->value = btv->opt_combfilter;
1164 case V4L2_CID_PRIVATE_LUMAFILTER:
1165 c->value = btv->opt_lumafilter;
1167 case V4L2_CID_PRIVATE_AUTOMUTE:
1168 c->value = btv->opt_automute;
1170 case V4L2_CID_PRIVATE_AGC_CRUSH:
1171 c->value = btv->opt_adc_crush;
1173 case V4L2_CID_PRIVATE_VCR_HACK:
1174 c->value = btv->opt_vcr_hack;
1176 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1177 c->value = btv->opt_whitecrush_upper;
1179 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1180 c->value = btv->opt_whitecrush_lower;
1182 case V4L2_CID_PRIVATE_UV_RATIO:
1183 c->value = btv->opt_uv_ratio;
1185 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1186 c->value = btv->opt_full_luma_range;
1188 case V4L2_CID_PRIVATE_CORING:
1189 c->value = btv->opt_coring;
1197 static int set_control(struct bttv *btv, struct v4l2_control *c)
1199 struct video_audio va;
1202 for (i = 0; i < BTTV_CTLS; i++)
1203 if (bttv_ctls[i].id == c->id)
1207 if (i >= 4 && i <= 8) {
1208 memset(&va,0,sizeof(va));
1209 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1210 if (btv->audio_hook)
1211 btv->audio_hook(btv,&va,0);
1214 case V4L2_CID_BRIGHTNESS:
1215 bt848_bright(btv,c->value);
1218 bt848_hue(btv,c->value);
1220 case V4L2_CID_CONTRAST:
1221 bt848_contrast(btv,c->value);
1223 case V4L2_CID_SATURATION:
1224 bt848_sat(btv,c->value);
1226 case V4L2_CID_AUDIO_MUTE:
1228 va.flags |= VIDEO_AUDIO_MUTE;
1229 audio_mux(btv, AUDIO_MUTE);
1231 va.flags &= ~VIDEO_AUDIO_MUTE;
1232 audio_mux(btv, AUDIO_UNMUTE);
1236 case V4L2_CID_AUDIO_VOLUME:
1237 va.volume = c->value;
1239 case V4L2_CID_AUDIO_BALANCE:
1240 va.balance = c->value;
1242 case V4L2_CID_AUDIO_BASS:
1245 case V4L2_CID_AUDIO_TREBLE:
1246 va.treble = c->value;
1249 case V4L2_CID_PRIVATE_CHROMA_AGC:
1250 btv->opt_chroma_agc = c->value;
1251 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1252 btwrite(val, BT848_E_SCLOOP);
1253 btwrite(val, BT848_O_SCLOOP);
1255 case V4L2_CID_PRIVATE_COMBFILTER:
1256 btv->opt_combfilter = c->value;
1258 case V4L2_CID_PRIVATE_LUMAFILTER:
1259 btv->opt_lumafilter = c->value;
1260 if (btv->opt_lumafilter) {
1261 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1262 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1264 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1265 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1268 case V4L2_CID_PRIVATE_AUTOMUTE:
1269 btv->opt_automute = c->value;
1271 case V4L2_CID_PRIVATE_AGC_CRUSH:
1272 btv->opt_adc_crush = c->value;
1273 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1276 case V4L2_CID_PRIVATE_VCR_HACK:
1277 btv->opt_vcr_hack = c->value;
1279 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1280 btv->opt_whitecrush_upper = c->value;
1281 btwrite(c->value, BT848_WC_UP);
1283 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1284 btv->opt_whitecrush_lower = c->value;
1285 btwrite(c->value, BT848_WC_DOWN);
1287 case V4L2_CID_PRIVATE_UV_RATIO:
1288 btv->opt_uv_ratio = c->value;
1289 bt848_sat(btv, btv->saturation);
1291 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1292 btv->opt_full_luma_range = c->value;
1293 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1295 case V4L2_CID_PRIVATE_CORING:
1296 btv->opt_coring = c->value;
1297 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1302 if (i >= 4 && i <= 8) {
1303 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1304 if (btv->audio_hook)
1305 btv->audio_hook(btv,&va,1);
1310 /* ----------------------------------------------------------------------- */
1312 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1314 unsigned int outbits, data;
1315 outbits = btread(BT848_GPIO_OUT_EN);
1316 data = btread(BT848_GPIO_DATA);
1317 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1318 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1321 static void bttv_field_count(struct bttv *btv)
1329 /* start field counter */
1330 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1332 /* stop field counter */
1333 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1334 btv->field_count = 0;
1338 static const struct bttv_format*
1339 format_by_palette(int palette)
1343 for (i = 0; i < BTTV_FORMATS; i++) {
1344 if (-1 == bttv_formats[i].palette)
1346 if (bttv_formats[i].palette == palette)
1347 return bttv_formats+i;
1352 static const struct bttv_format*
1353 format_by_fourcc(int fourcc)
1357 for (i = 0; i < BTTV_FORMATS; i++) {
1358 if (-1 == bttv_formats[i].fourcc)
1360 if (bttv_formats[i].fourcc == fourcc)
1361 return bttv_formats+i;
1366 /* ----------------------------------------------------------------------- */
1370 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1371 struct bttv_buffer *new)
1373 struct bttv_buffer *old;
1374 unsigned long flags;
1377 dprintk("switch_overlay: enter [new=%p]\n",new);
1379 new->vb.state = STATE_DONE;
1380 spin_lock_irqsave(&btv->s_lock,flags);
1384 bttv_set_dma(btv, 0x03);
1385 spin_unlock_irqrestore(&btv->s_lock,flags);
1387 free_btres(btv,fh,RESOURCE_OVERLAY);
1389 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1390 bttv_dma_free(btv, old);
1393 dprintk("switch_overlay: done\n");
1397 /* ----------------------------------------------------------------------- */
1398 /* video4linux (1) interface */
1400 static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
1401 const struct bttv_format *fmt,
1402 unsigned int width, unsigned int height,
1403 enum v4l2_field field)
1405 int redo_dma_risc = 0;
1408 /* check settings */
1411 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1413 height = RAW_LINES*2;
1414 if (width*height > buf->vb.bsize)
1416 buf->vb.size = buf->vb.bsize;
1420 width > bttv_tvnorms[btv->tvnorm].swidth ||
1421 height > bttv_tvnorms[btv->tvnorm].sheight)
1423 buf->vb.size = (width * height * fmt->depth) >> 3;
1424 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1428 /* alloc + fill struct bttv_buffer (if changed) */
1429 if (buf->vb.width != width || buf->vb.height != height ||
1430 buf->vb.field != field ||
1431 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1432 buf->vb.width = width;
1433 buf->vb.height = height;
1434 buf->vb.field = field;
1435 buf->tvnorm = btv->tvnorm;
1440 /* alloc risc memory */
1441 if (STATE_NEEDS_INIT == buf->vb.state) {
1443 if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
1448 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1451 buf->vb.state = STATE_PREPARED;
1455 bttv_dma_free(btv,buf);
1460 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1462 struct bttv_fh *fh = q->priv_data;
1464 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1467 while (*size * *count > gbuffers * gbufsize)
1473 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1474 enum v4l2_field field)
1476 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1477 struct bttv_fh *fh = q->priv_data;
1479 return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
1480 fh->width, fh->height, field);
1484 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1486 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1487 struct bttv_fh *fh = q->priv_data;
1488 struct bttv *btv = fh->btv;
1490 buf->vb.state = STATE_QUEUED;
1491 list_add_tail(&buf->vb.queue,&btv->capture);
1492 if (!btv->curr.frame_irq) {
1494 bttv_set_dma(btv, 0x03);
1498 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1500 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1501 struct bttv_fh *fh = q->priv_data;
1503 bttv_dma_free(fh->btv,buf);
1506 static struct videobuf_queue_ops bttv_video_qops = {
1507 .buf_setup = buffer_setup,
1508 .buf_prepare = buffer_prepare,
1509 .buf_queue = buffer_queue,
1510 .buf_release = buffer_release,
1513 static const char *v4l1_ioctls[] = {
1514 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
1515 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
1516 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
1517 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
1518 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
1519 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
1521 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1525 return BTTV_VERSION_CODE;
1527 /* *** v4l1 *** ************************************************ */
1530 unsigned long *freq = arg;
1536 unsigned long *freq = arg;
1539 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1540 if (btv->has_matchbox && btv->radio_user)
1541 tea5757_set_freq(btv,*freq);
1548 struct video_tuner *v = arg;
1550 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1552 if (v->tuner) /* Only tuner 0 */
1554 strcpy(v->name, "Television");
1556 v->rangehigh = 0x7FFFFFFF;
1557 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1558 v->mode = btv->tvnorm;
1559 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1560 bttv_call_i2c_clients(btv,cmd,v);
1565 struct video_tuner *v = arg;
1567 if (v->tuner) /* Only tuner 0 */
1569 if (v->mode >= BTTV_TVNORMS)
1573 set_tvnorm(btv,v->mode);
1574 bttv_call_i2c_clients(btv,cmd,v);
1581 struct video_channel *v = arg;
1582 unsigned int channel = v->channel;
1584 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1587 v->flags = VIDEO_VC_AUDIO;
1588 v->type = VIDEO_TYPE_CAMERA;
1589 v->norm = btv->tvnorm;
1590 if (channel == bttv_tvcards[btv->c.type].tuner) {
1591 strcpy(v->name,"Television");
1592 v->flags|=VIDEO_VC_TUNER;
1593 v->type=VIDEO_TYPE_TV;
1595 } else if (channel == btv->svhs) {
1596 strcpy(v->name,"S-Video");
1598 sprintf(v->name,"Composite%d",channel);
1604 struct video_channel *v = arg;
1605 unsigned int channel = v->channel;
1607 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1609 if (v->norm >= BTTV_TVNORMS)
1613 if (channel == btv->input &&
1614 v->norm == btv->tvnorm) {
1620 btv->tvnorm = v->norm;
1621 set_input(btv,v->channel);
1628 struct video_audio *v = arg;
1630 memset(v,0,sizeof(*v));
1631 strcpy(v->name,"Television");
1632 v->flags |= VIDEO_AUDIO_MUTABLE;
1633 v->mode = VIDEO_SOUND_MONO;
1636 bttv_call_i2c_clients(btv,cmd,v);
1638 /* card specific hooks */
1639 if (btv->audio_hook)
1640 btv->audio_hook(btv,v,0);
1647 struct video_audio *v = arg;
1648 unsigned int audio = v->audio;
1650 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1654 audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
1655 bttv_call_i2c_clients(btv,cmd,v);
1657 /* card specific hooks */
1658 if (btv->audio_hook)
1659 btv->audio_hook(btv,v,1);
1665 /* *** v4l2 *** ************************************************ */
1666 case VIDIOC_ENUMSTD:
1668 struct v4l2_standard *e = arg;
1669 unsigned int index = e->index;
1671 if (index >= BTTV_TVNORMS)
1673 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1674 bttv_tvnorms[e->index].name);
1680 v4l2_std_id *id = arg;
1681 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1686 v4l2_std_id *id = arg;
1689 for (i = 0; i < BTTV_TVNORMS; i++)
1690 if (*id & bttv_tvnorms[i].v4l2_id)
1692 if (i == BTTV_TVNORMS)
1697 i2c_vidiocschan(btv);
1701 case VIDIOC_QUERYSTD:
1703 v4l2_std_id *id = arg;
1705 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1706 *id = V4L2_STD_625_50;
1708 *id = V4L2_STD_525_60;
1712 case VIDIOC_ENUMINPUT:
1714 struct v4l2_input *i = arg;
1718 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1720 memset(i,0,sizeof(*i));
1722 i->type = V4L2_INPUT_TYPE_CAMERA;
1724 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1725 sprintf(i->name, "Television");
1726 i->type = V4L2_INPUT_TYPE_TUNER;
1728 } else if (i->index == btv->svhs) {
1729 sprintf(i->name, "S-Video");
1731 sprintf(i->name,"Composite%d",i->index);
1733 if (i->index == btv->input) {
1734 __u32 dstatus = btread(BT848_DSTATUS);
1735 if (0 == (dstatus & BT848_DSTATUS_PRES))
1736 i->status |= V4L2_IN_ST_NO_SIGNAL;
1737 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1738 i->status |= V4L2_IN_ST_NO_H_LOCK;
1740 for (n = 0; n < BTTV_TVNORMS; n++)
1741 i->std |= bttv_tvnorms[n].v4l2_id;
1744 case VIDIOC_G_INPUT:
1750 case VIDIOC_S_INPUT:
1752 unsigned int *i = arg;
1754 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1762 case VIDIOC_G_TUNER:
1764 struct v4l2_tuner *t = arg;
1766 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1771 memset(t,0,sizeof(*t));
1772 strcpy(t->name, "Television");
1773 t->type = V4L2_TUNER_ANALOG_TV;
1774 t->rangehigh = 0xffffffffUL;
1775 t->capability = V4L2_TUNER_CAP_NORM;
1776 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1777 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1781 struct video_audio va;
1782 memset(&va, 0, sizeof(struct video_audio));
1783 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1784 if (btv->audio_hook)
1785 btv->audio_hook(btv,&va,0);
1786 if(va.mode & VIDEO_SOUND_STEREO) {
1787 t->audmode = V4L2_TUNER_MODE_STEREO;
1788 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1790 if(va.mode & VIDEO_SOUND_LANG1) {
1791 t->audmode = V4L2_TUNER_MODE_LANG1;
1792 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1793 | V4L2_TUNER_SUB_LANG2;
1796 /* FIXME: fill capability+audmode */
1800 case VIDIOC_S_TUNER:
1802 struct v4l2_tuner *t = arg;
1804 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1810 struct video_audio va;
1811 memset(&va, 0, sizeof(struct video_audio));
1812 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1813 if (t->audmode == V4L2_TUNER_MODE_MONO)
1814 va.mode = VIDEO_SOUND_MONO;
1815 else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1816 va.mode = VIDEO_SOUND_STEREO;
1817 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1818 va.mode = VIDEO_SOUND_LANG1;
1819 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1820 va.mode = VIDEO_SOUND_LANG2;
1821 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1822 if (btv->audio_hook)
1823 btv->audio_hook(btv,&va,1);
1829 case VIDIOC_G_FREQUENCY:
1831 struct v4l2_frequency *f = arg;
1833 memset(f,0,sizeof(*f));
1834 f->type = V4L2_TUNER_ANALOG_TV;
1835 f->frequency = btv->freq;
1838 case VIDIOC_S_FREQUENCY:
1840 struct v4l2_frequency *f = arg;
1842 if (unlikely(f->tuner != 0))
1844 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
1847 btv->freq = f->frequency;
1848 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1849 if (btv->has_matchbox && btv->radio_user)
1850 tea5757_set_freq(btv,btv->freq);
1854 case VIDIOC_LOG_STATUS:
1856 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
1861 return -ENOIOCTLCMD;
1867 static int verify_window(const struct bttv_tvnorm *tvn,
1868 struct v4l2_window *win, int fixup)
1870 enum v4l2_field field;
1873 if (win->w.width < 48 || win->w.height < 32)
1875 if (win->clipcount > 2048)
1880 maxh = tvn->sheight;
1882 if (V4L2_FIELD_ANY == field) {
1883 field = (win->w.height > maxh/2)
1884 ? V4L2_FIELD_INTERLACED
1888 case V4L2_FIELD_TOP:
1889 case V4L2_FIELD_BOTTOM:
1892 case V4L2_FIELD_INTERLACED:
1898 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1901 if (win->w.width > maxw)
1902 win->w.width = maxw;
1903 if (win->w.height > maxh)
1904 win->w.height = maxh;
1909 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1910 struct v4l2_window *win, int fixup)
1912 struct v4l2_clip *clips = NULL;
1913 int n,size,retval = 0;
1915 if (NULL == fh->ovfmt)
1917 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1919 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1923 /* copy clips -- luckily v4l1 + v4l2 are binary
1924 compatible here ...*/
1926 size = sizeof(*clips)*(n+4);
1927 clips = kmalloc(size,GFP_KERNEL);
1931 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1936 /* clip against screen */
1937 if (NULL != btv->fbuf.base)
1938 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1940 btcx_sort_clips(clips,n);
1942 /* 4-byte alignments */
1943 switch (fh->ovfmt->depth) {
1946 btcx_align(&win->w, clips, n, 3);
1949 btcx_align(&win->w, clips, n, 1);
1952 /* no alignment fixups needed */
1958 down(&fh->cap.lock);
1959 kfree(fh->ov.clips);
1960 fh->ov.clips = clips;
1964 fh->ov.field = win->field;
1965 fh->ov.setup_ok = 1;
1966 btv->init.ov.w.width = win->w.width;
1967 btv->init.ov.w.height = win->w.height;
1968 btv->init.ov.field = win->field;
1970 /* update overlay if needed */
1972 if (check_btres(fh, RESOURCE_OVERLAY)) {
1973 struct bttv_buffer *new;
1975 new = videobuf_alloc(sizeof(*new));
1976 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1977 retval = bttv_switch_overlay(btv,fh,new);
1983 /* ----------------------------------------------------------------------- */
1985 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
1987 struct videobuf_queue* q = NULL;
1990 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1993 case V4L2_BUF_TYPE_VBI_CAPTURE:
2002 static int bttv_resource(struct bttv_fh *fh)
2007 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2008 res = RESOURCE_VIDEO;
2010 case V4L2_BUF_TYPE_VBI_CAPTURE:
2019 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2021 struct videobuf_queue *q = bttv_queue(fh);
2022 int res = bttv_resource(fh);
2024 if (check_btres(fh,res))
2026 if (videobuf_queue_is_busy(q))
2033 pix_format_set_size (struct v4l2_pix_format * f,
2034 const struct bttv_format * fmt,
2036 unsigned int height)
2041 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2042 f->bytesperline = width; /* Y plane */
2043 f->sizeimage = (width * height * fmt->depth) >> 3;
2045 f->bytesperline = (width * fmt->depth) >> 3;
2046 f->sizeimage = height * f->bytesperline;
2050 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2053 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2054 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2055 pix_format_set_size (&f->fmt.pix, fh->fmt,
2056 fh->width, fh->height);
2057 f->fmt.pix.field = fh->cap.field;
2058 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2060 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2061 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2062 f->fmt.win.w = fh->ov.w;
2063 f->fmt.win.field = fh->ov.field;
2065 case V4L2_BUF_TYPE_VBI_CAPTURE:
2066 bttv_vbi_get_fmt(fh,f);
2073 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2074 struct v4l2_format *f)
2077 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2079 const struct bttv_format *fmt;
2080 enum v4l2_field field;
2081 unsigned int maxw,maxh;
2083 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2088 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2089 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2090 field = f->fmt.pix.field;
2091 if (V4L2_FIELD_ANY == field)
2092 field = (f->fmt.pix.height > maxh/2)
2093 ? V4L2_FIELD_INTERLACED
2094 : V4L2_FIELD_BOTTOM;
2095 if (V4L2_FIELD_SEQ_BT == field)
2096 field = V4L2_FIELD_SEQ_TB;
2098 case V4L2_FIELD_TOP:
2099 case V4L2_FIELD_BOTTOM:
2100 case V4L2_FIELD_ALTERNATE:
2103 case V4L2_FIELD_INTERLACED:
2105 case V4L2_FIELD_SEQ_TB:
2106 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2113 /* update data for the application */
2114 f->fmt.pix.field = field;
2115 if (f->fmt.pix.width < 48)
2116 f->fmt.pix.width = 48;
2117 if (f->fmt.pix.height < 32)
2118 f->fmt.pix.height = 32;
2119 if (f->fmt.pix.width > maxw)
2120 f->fmt.pix.width = maxw;
2121 if (f->fmt.pix.height > maxh)
2122 f->fmt.pix.height = maxh;
2123 pix_format_set_size (&f->fmt.pix, fmt,
2124 f->fmt.pix.width & ~3,
2129 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2130 return verify_window(&bttv_tvnorms[btv->tvnorm],
2132 case V4L2_BUF_TYPE_VBI_CAPTURE:
2133 bttv_vbi_try_fmt(fh,f);
2140 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2141 struct v4l2_format *f)
2146 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2148 const struct bttv_format *fmt;
2150 retval = bttv_switch_type(fh,f->type);
2153 retval = bttv_try_fmt(fh,btv,f);
2156 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2158 /* update our state informations */
2159 down(&fh->cap.lock);
2161 fh->cap.field = f->fmt.pix.field;
2162 fh->cap.last = V4L2_FIELD_NONE;
2163 fh->width = f->fmt.pix.width;
2164 fh->height = f->fmt.pix.height;
2165 btv->init.fmt = fmt;
2166 btv->init.width = f->fmt.pix.width;
2167 btv->init.height = f->fmt.pix.height;
2172 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2173 if (no_overlay > 0) {
2174 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2177 return setup_window(fh, btv, &f->fmt.win, 1);
2178 case V4L2_BUF_TYPE_VBI_CAPTURE:
2179 retval = bttv_switch_type(fh,f->type);
2182 if (locked_btres(fh->btv, RESOURCE_VBI))
2184 bttv_vbi_try_fmt(fh,f);
2185 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2186 bttv_vbi_get_fmt(fh,f);
2193 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2194 unsigned int cmd, void *arg)
2196 struct bttv_fh *fh = file->private_data;
2197 struct bttv *btv = fh->btv;
2198 unsigned long flags;
2201 if (bttv_debug > 1) {
2202 switch (_IOC_TYPE(cmd)) {
2204 printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
2205 btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
2206 v4l1_ioctls[_IOC_NR(cmd)] : "???");
2209 printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
2210 btv->c.nr, cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
2213 printk("bttv%d: ioctl 0x%x (???)\n",
2218 bttv_reinit_bt848(btv);
2226 case VIDIOC_S_INPUT:
2227 case VIDIOC_S_TUNER:
2228 case VIDIOC_S_FREQUENCY:
2229 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2236 /* *** v4l1 *** ************************************************ */
2239 struct video_capability *cap = arg;
2241 memset(cap,0,sizeof(*cap));
2242 strcpy(cap->name,btv->video_dev->name);
2243 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2245 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2248 cap->type = VID_TYPE_CAPTURE|
2252 if (no_overlay <= 0)
2253 cap->type |= VID_TYPE_OVERLAY;
2255 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2256 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2258 cap->minheight = 32;
2260 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2261 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2267 struct video_picture *pic = arg;
2269 memset(pic,0,sizeof(*pic));
2270 pic->brightness = btv->bright;
2271 pic->contrast = btv->contrast;
2272 pic->hue = btv->hue;
2273 pic->colour = btv->saturation;
2275 pic->depth = fh->fmt->depth;
2276 pic->palette = fh->fmt->palette;
2282 struct video_picture *pic = arg;
2283 const struct bttv_format *fmt;
2285 fmt = format_by_palette(pic->palette);
2288 down(&fh->cap.lock);
2289 if (fmt->depth != pic->depth) {
2291 goto fh_unlock_and_return;
2293 if (fmt->flags & FORMAT_FLAGS_RAW) {
2294 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2295 RAW_LINES * 2. F1 is stored at offset 0, F2
2296 at buffer size / 2. */
2297 fh->width = RAW_BPL;
2298 fh->height = gbufsize / RAW_BPL;
2299 btv->init.width = RAW_BPL;
2300 btv->init.height = gbufsize / RAW_BPL;
2304 btv->init.ovfmt = fmt;
2305 btv->init.fmt = fmt;
2307 /* dirty hack time: swap bytes for overlay if the
2308 display adaptor is big endian (insmod option) */
2309 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2310 fmt->palette == VIDEO_PALETTE_RGB565 ||
2311 fmt->palette == VIDEO_PALETTE_RGB32) {
2315 bt848_bright(btv,pic->brightness);
2316 bt848_contrast(btv,pic->contrast);
2317 bt848_hue(btv,pic->hue);
2318 bt848_sat(btv,pic->colour);
2325 struct video_window *win = arg;
2327 memset(win,0,sizeof(*win));
2328 win->x = fh->ov.w.left;
2329 win->y = fh->ov.w.top;
2330 win->width = fh->ov.w.width;
2331 win->height = fh->ov.w.height;
2336 struct video_window *win = arg;
2337 struct v4l2_window w2;
2339 if (no_overlay > 0) {
2340 printk ("VIDIOCSWIN: no_overlay\n");
2344 w2.field = V4L2_FIELD_ANY;
2347 w2.w.width = win->width;
2348 w2.w.height = win->height;
2349 w2.clipcount = win->clipcount;
2350 w2.clips = (struct v4l2_clip __user *)win->clips;
2351 retval = setup_window(fh, btv, &w2, 0);
2353 /* on v4l1 this ioctl affects the read() size too */
2354 fh->width = fh->ov.w.width;
2355 fh->height = fh->ov.w.height;
2356 btv->init.width = fh->ov.w.width;
2357 btv->init.height = fh->ov.w.height;
2364 struct video_buffer *fbuf = arg;
2366 fbuf->base = btv->fbuf.base;
2367 fbuf->width = btv->fbuf.fmt.width;
2368 fbuf->height = btv->fbuf.fmt.height;
2369 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2371 fbuf->depth = fh->ovfmt->depth;
2376 struct video_buffer *fbuf = arg;
2377 const struct bttv_format *fmt;
2380 if(!capable(CAP_SYS_ADMIN) &&
2381 !capable(CAP_SYS_RAWIO))
2383 end = (unsigned long)fbuf->base +
2384 fbuf->height * fbuf->bytesperline;
2385 down(&fh->cap.lock);
2388 switch (fbuf->depth) {
2390 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2393 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2396 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2399 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2403 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2410 goto fh_unlock_and_return;
2414 btv->init.ovfmt = fmt;
2415 btv->init.fmt = fmt;
2416 btv->fbuf.base = fbuf->base;
2417 btv->fbuf.fmt.width = fbuf->width;
2418 btv->fbuf.fmt.height = fbuf->height;
2419 if (fbuf->bytesperline)
2420 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2422 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2428 case VIDIOC_OVERLAY:
2430 struct bttv_buffer *new;
2435 if (NULL == btv->fbuf.base)
2437 if (!fh->ov.setup_ok) {
2438 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2443 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2446 down(&fh->cap.lock);
2448 fh->ov.tvnorm = btv->tvnorm;
2449 new = videobuf_alloc(sizeof(*new));
2450 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2456 retval = bttv_switch_overlay(btv,fh,new);
2463 struct video_mbuf *mbuf = arg;
2466 down(&fh->cap.lock);
2467 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2470 goto fh_unlock_and_return;
2471 memset(mbuf,0,sizeof(*mbuf));
2472 mbuf->frames = gbuffers;
2473 mbuf->size = gbuffers * gbufsize;
2474 for (i = 0; i < gbuffers; i++)
2475 mbuf->offsets[i] = i * gbufsize;
2479 case VIDIOCMCAPTURE:
2481 struct video_mmap *vm = arg;
2482 struct bttv_buffer *buf;
2483 enum v4l2_field field;
2485 if (vm->frame >= VIDEO_MAX_FRAME)
2488 down(&fh->cap.lock);
2490 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2492 goto fh_unlock_and_return;
2493 if (0 == buf->vb.baddr)
2494 goto fh_unlock_and_return;
2495 if (buf->vb.state == STATE_QUEUED ||
2496 buf->vb.state == STATE_ACTIVE)
2497 goto fh_unlock_and_return;
2499 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2500 ? V4L2_FIELD_INTERLACED
2501 : V4L2_FIELD_BOTTOM;
2502 retval = bttv_prepare_buffer(btv,buf,
2503 format_by_palette(vm->format),
2504 vm->width,vm->height,field);
2506 goto fh_unlock_and_return;
2507 spin_lock_irqsave(&btv->s_lock,flags);
2508 buffer_queue(&fh->cap,&buf->vb);
2509 spin_unlock_irqrestore(&btv->s_lock,flags);
2516 struct bttv_buffer *buf;
2518 if (*frame >= VIDEO_MAX_FRAME)
2521 down(&fh->cap.lock);
2523 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2525 goto fh_unlock_and_return;
2526 retval = videobuf_waiton(&buf->vb,0,1);
2528 goto fh_unlock_and_return;
2529 switch (buf->vb.state) {
2534 videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
2535 bttv_dma_free(btv,buf);
2547 struct vbi_format *fmt = (void *) arg;
2548 struct v4l2_format fmt2;
2550 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2551 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2555 bttv_vbi_get_fmt(fh, &fmt2);
2557 memset(fmt,0,sizeof(*fmt));
2558 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2559 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2560 fmt->sample_format = VIDEO_PALETTE_RAW;
2561 fmt->start[0] = fmt2.fmt.vbi.start[0];
2562 fmt->count[0] = fmt2.fmt.vbi.count[0];
2563 fmt->start[1] = fmt2.fmt.vbi.start[1];
2564 fmt->count[1] = fmt2.fmt.vbi.count[1];
2565 if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
2566 fmt->flags |= V4L2_VBI_UNSYNC;
2567 if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
2568 fmt->flags |= V4L2_VBI_INTERLACED;
2573 struct vbi_format *fmt = (void *) arg;
2574 struct v4l2_format fmt2;
2576 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2579 bttv_vbi_get_fmt(fh, &fmt2);
2581 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2582 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2583 fmt->sample_format != VIDEO_PALETTE_RAW ||
2584 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2585 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2586 fmt->count[0] != fmt->count[1] ||
2587 fmt->count[0] < 1 ||
2588 fmt->count[0] > 32 /* VBI_MAXLINES */)
2591 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2604 return bttv_common_ioctls(btv,cmd,arg);
2606 /* *** v4l2 *** ************************************************ */
2607 case VIDIOC_QUERYCAP:
2609 struct v4l2_capability *cap = arg;
2613 strcpy(cap->driver,"bttv");
2614 strlcpy(cap->card,btv->video_dev->name,sizeof(cap->card));
2615 sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
2616 cap->version = BTTV_VERSION_CODE;
2618 V4L2_CAP_VIDEO_CAPTURE |
2619 V4L2_CAP_VBI_CAPTURE |
2620 V4L2_CAP_READWRITE |
2622 if (no_overlay <= 0)
2623 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2625 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2626 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2627 cap->capabilities |= V4L2_CAP_TUNER;
2631 case VIDIOC_ENUM_FMT:
2633 struct v4l2_fmtdesc *f = arg;
2634 enum v4l2_buf_type type;
2639 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2644 memset(f,0,sizeof(*f));
2647 f->pixelformat = V4L2_PIX_FMT_GREY;
2648 strcpy(f->description,"vbi data");
2652 /* video capture + overlay */
2654 for (i = 0; i < BTTV_FORMATS; i++) {
2655 if (bttv_formats[i].fourcc != -1)
2657 if ((unsigned int)index == f->index)
2660 if (BTTV_FORMATS == i)
2664 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2666 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2667 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2673 memset(f,0,sizeof(*f));
2676 f->pixelformat = bttv_formats[i].fourcc;
2677 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2681 case VIDIOC_TRY_FMT:
2683 struct v4l2_format *f = arg;
2684 return bttv_try_fmt(fh,btv,f);
2688 struct v4l2_format *f = arg;
2689 return bttv_g_fmt(fh,f);
2693 struct v4l2_format *f = arg;
2694 return bttv_s_fmt(fh,btv,f);
2699 struct v4l2_framebuffer *fb = arg;
2702 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2704 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2709 struct v4l2_framebuffer *fb = arg;
2710 const struct bttv_format *fmt;
2712 if(!capable(CAP_SYS_ADMIN) &&
2713 !capable(CAP_SYS_RAWIO))
2717 fmt = format_by_fourcc(fb->fmt.pixelformat);
2720 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2723 down(&fh->cap.lock);
2725 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2726 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2727 goto fh_unlock_and_return;
2728 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2729 goto fh_unlock_and_return;
2733 btv->fbuf.base = fb->base;
2734 btv->fbuf.fmt.width = fb->fmt.width;
2735 btv->fbuf.fmt.height = fb->fmt.height;
2736 if (0 != fb->fmt.bytesperline)
2737 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2739 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2743 btv->init.ovfmt = fmt;
2744 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2747 fh->ov.w.width = fb->fmt.width;
2748 fh->ov.w.height = fb->fmt.height;
2749 btv->init.ov.w.width = fb->fmt.width;
2750 btv->init.ov.w.height = fb->fmt.height;
2751 kfree(fh->ov.clips);
2752 fh->ov.clips = NULL;
2755 if (check_btres(fh, RESOURCE_OVERLAY)) {
2756 struct bttv_buffer *new;
2758 new = videobuf_alloc(sizeof(*new));
2759 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2760 retval = bttv_switch_overlay(btv,fh,new);
2767 case VIDIOC_REQBUFS:
2768 return videobuf_reqbufs(bttv_queue(fh),arg);
2770 case VIDIOC_QUERYBUF:
2771 return videobuf_querybuf(bttv_queue(fh),arg);
2774 return videobuf_qbuf(bttv_queue(fh),arg);
2777 return videobuf_dqbuf(bttv_queue(fh),arg,
2778 file->f_flags & O_NONBLOCK);
2780 case VIDIOC_STREAMON:
2782 int res = bttv_resource(fh);
2784 if (!check_alloc_btres(btv,fh,res))
2786 return videobuf_streamon(bttv_queue(fh));
2788 case VIDIOC_STREAMOFF:
2790 int res = bttv_resource(fh);
2792 retval = videobuf_streamoff(bttv_queue(fh));
2795 free_btres(btv,fh,res);
2799 case VIDIOC_QUERYCTRL:
2801 struct v4l2_queryctrl *c = arg;
2804 if ((c->id < V4L2_CID_BASE ||
2805 c->id >= V4L2_CID_LASTP1) &&
2806 (c->id < V4L2_CID_PRIVATE_BASE ||
2807 c->id >= V4L2_CID_PRIVATE_LASTP1))
2809 for (i = 0; i < BTTV_CTLS; i++)
2810 if (bttv_ctls[i].id == c->id)
2812 if (i == BTTV_CTLS) {
2817 if (i >= 4 && i <= 8) {
2818 struct video_audio va;
2819 memset(&va,0,sizeof(va));
2820 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2821 if (btv->audio_hook)
2822 btv->audio_hook(btv,&va,0);
2823 switch (bttv_ctls[i].id) {
2824 case V4L2_CID_AUDIO_VOLUME:
2825 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2828 case V4L2_CID_AUDIO_BALANCE:
2829 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2832 case V4L2_CID_AUDIO_BASS:
2833 if (!(va.flags & VIDEO_AUDIO_BASS))
2836 case V4L2_CID_AUDIO_TREBLE:
2837 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2845 return get_control(btv,arg);
2847 return set_control(btv,arg);
2850 struct v4l2_streamparm *parm = arg;
2851 struct v4l2_standard s;
2852 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2854 memset(parm,0,sizeof(*parm));
2855 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2856 bttv_tvnorms[btv->tvnorm].name);
2857 parm->parm.capture.timeperframe = s.frameperiod;
2861 case VIDIOC_G_PRIORITY:
2863 enum v4l2_priority *p = arg;
2865 *p = v4l2_prio_max(&btv->prio);
2868 case VIDIOC_S_PRIORITY:
2870 enum v4l2_priority *prio = arg;
2872 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2875 case VIDIOC_ENUMSTD:
2878 case VIDIOC_ENUMINPUT:
2879 case VIDIOC_G_INPUT:
2880 case VIDIOC_S_INPUT:
2881 case VIDIOC_G_TUNER:
2882 case VIDIOC_S_TUNER:
2883 case VIDIOC_G_FREQUENCY:
2884 case VIDIOC_S_FREQUENCY:
2885 case VIDIOC_LOG_STATUS:
2886 return bttv_common_ioctls(btv,cmd,arg);
2889 return -ENOIOCTLCMD;
2893 fh_unlock_and_return:
2898 static int bttv_ioctl(struct inode *inode, struct file *file,
2899 unsigned int cmd, unsigned long arg)
2901 struct bttv_fh *fh = file->private_data;
2905 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2906 return fh->lines * 2 * 2048;
2908 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2912 static ssize_t bttv_read(struct file *file, char __user *data,
2913 size_t count, loff_t *ppos)
2915 struct bttv_fh *fh = file->private_data;
2918 if (fh->btv->errors)
2919 bttv_reinit_bt848(fh->btv);
2920 dprintk("bttv%d: read count=%d type=%s\n",
2921 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2924 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2925 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2927 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2928 file->f_flags & O_NONBLOCK);
2930 case V4L2_BUF_TYPE_VBI_CAPTURE:
2931 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2933 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2934 file->f_flags & O_NONBLOCK);
2942 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2944 struct bttv_fh *fh = file->private_data;
2945 struct bttv_buffer *buf;
2946 enum v4l2_field field;
2948 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2949 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2951 return videobuf_poll_stream(file, &fh->vbi, wait);
2954 if (check_btres(fh,RESOURCE_VIDEO)) {
2955 /* streaming capture */
2956 if (list_empty(&fh->cap.stream))
2958 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2960 /* read() capture */
2961 down(&fh->cap.lock);
2962 if (NULL == fh->cap.read_buf) {
2963 /* need to capture a new frame */
2964 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2968 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2969 if (NULL == fh->cap.read_buf) {
2973 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2974 field = videobuf_next_field(&fh->cap);
2975 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2976 kfree (fh->cap.read_buf);
2977 fh->cap.read_buf = NULL;
2981 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2982 fh->cap.read_off = 0;
2985 buf = (struct bttv_buffer*)fh->cap.read_buf;
2988 poll_wait(file, &buf->vb.done, wait);
2989 if (buf->vb.state == STATE_DONE ||
2990 buf->vb.state == STATE_ERROR)
2991 return POLLIN|POLLRDNORM;
2995 static int bttv_open(struct inode *inode, struct file *file)
2997 int minor = iminor(inode);
2998 struct bttv *btv = NULL;
3000 enum v4l2_buf_type type = 0;
3003 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3005 for (i = 0; i < bttv_num; i++) {
3006 if (bttvs[i].video_dev &&
3007 bttvs[i].video_dev->minor == minor) {
3009 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3012 if (bttvs[i].vbi_dev &&
3013 bttvs[i].vbi_dev->minor == minor) {
3015 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3022 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3023 btv->c.nr,v4l2_type_names[type]);
3025 /* allocate per filehandle data */
3026 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3029 file->private_data = fh;
3032 fh->ov.setup_ok = 0;
3033 v4l2_prio_open(&btv->prio,&fh->prio);
3035 videobuf_queue_init(&fh->cap, &bttv_video_qops,
3036 btv->c.pci, &btv->s_lock,
3037 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3038 V4L2_FIELD_INTERLACED,
3039 sizeof(struct bttv_buffer),
3041 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3042 btv->c.pci, &btv->s_lock,
3043 V4L2_BUF_TYPE_VBI_CAPTURE,
3045 sizeof(struct bttv_buffer),
3047 i2c_vidiocschan(btv);
3050 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
3051 bttv_vbi_setlines(fh,btv,16);
3052 bttv_field_count(btv);
3056 static int bttv_release(struct inode *inode, struct file *file)
3058 struct bttv_fh *fh = file->private_data;
3059 struct bttv *btv = fh->btv;
3061 /* turn off overlay */
3062 if (check_btres(fh, RESOURCE_OVERLAY))
3063 bttv_switch_overlay(btv,fh,NULL);
3065 /* stop video capture */
3066 if (check_btres(fh, RESOURCE_VIDEO)) {
3067 videobuf_streamoff(&fh->cap);
3068 free_btres(btv,fh,RESOURCE_VIDEO);
3070 if (fh->cap.read_buf) {
3071 buffer_release(&fh->cap,fh->cap.read_buf);
3072 kfree(fh->cap.read_buf);
3075 /* stop vbi capture */
3076 if (check_btres(fh, RESOURCE_VBI)) {
3077 if (fh->vbi.streaming)
3078 videobuf_streamoff(&fh->vbi);
3079 if (fh->vbi.reading)
3080 videobuf_read_stop(&fh->vbi);
3081 free_btres(btv,fh,RESOURCE_VBI);
3085 videobuf_mmap_free(&fh->cap);
3086 videobuf_mmap_free(&fh->vbi);
3087 v4l2_prio_close(&btv->prio,&fh->prio);
3088 file->private_data = NULL;
3092 bttv_field_count(btv);
3097 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3099 struct bttv_fh *fh = file->private_data;
3101 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3102 fh->btv->c.nr, v4l2_type_names[fh->type],
3103 vma->vm_start, vma->vm_end - vma->vm_start);
3104 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3107 static struct file_operations bttv_fops =
3109 .owner = THIS_MODULE,
3111 .release = bttv_release,
3112 .ioctl = bttv_ioctl,
3113 .llseek = no_llseek,
3119 static struct video_device bttv_video_template =
3122 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3123 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3124 .hardware = VID_HARDWARE_BT848,
3129 static struct video_device bttv_vbi_template =
3131 .name = "bt848/878 vbi",
3132 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3133 .hardware = VID_HARDWARE_BT848,
3138 /* ----------------------------------------------------------------------- */
3139 /* radio interface */
3141 static int radio_open(struct inode *inode, struct file *file)
3143 int minor = iminor(inode);
3144 struct bttv *btv = NULL;
3147 dprintk("bttv: open minor=%d\n",minor);
3149 for (i = 0; i < bttv_num; i++) {
3150 if (bttvs[i].radio_dev->minor == minor) {
3158 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3163 file->private_data = btv;
3165 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
3166 audio_mux(btv,AUDIO_RADIO);
3172 static int radio_release(struct inode *inode, struct file *file)
3174 struct bttv *btv = file->private_data;
3175 struct rds_command cmd;
3179 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3184 static int radio_do_ioctl(struct inode *inode, struct file *file,
3185 unsigned int cmd, void *arg)
3187 struct bttv *btv = file->private_data;
3192 struct video_capability *cap = arg;
3194 memset(cap,0,sizeof(*cap));
3195 strcpy(cap->name,btv->radio_dev->name);
3196 cap->type = VID_TYPE_TUNER;
3204 struct video_tuner *v = arg;
3208 memset(v,0,sizeof(*v));
3209 strcpy(v->name, "Radio");
3210 bttv_call_i2c_clients(btv,cmd,v);
3222 return bttv_common_ioctls(btv,cmd,arg);
3225 return -ENOIOCTLCMD;
3230 static int radio_ioctl(struct inode *inode, struct file *file,
3231 unsigned int cmd, unsigned long arg)
3233 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3236 static ssize_t radio_read(struct file *file, char __user *data,
3237 size_t count, loff_t *ppos)
3239 struct bttv *btv = file->private_data;
3240 struct rds_command cmd;
3241 cmd.block_count = count/3;
3243 cmd.instance = file;
3244 cmd.result = -ENODEV;
3246 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3251 static unsigned int radio_poll(struct file *file, poll_table *wait)
3253 struct bttv *btv = file->private_data;
3254 struct rds_command cmd;
3255 cmd.instance = file;
3256 cmd.event_list = wait;
3257 cmd.result = -ENODEV;
3258 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3263 static struct file_operations radio_fops =
3265 .owner = THIS_MODULE,
3268 .release = radio_release,
3269 .ioctl = radio_ioctl,
3270 .llseek = no_llseek,
3274 static struct video_device radio_template =
3276 .name = "bt848/878 radio",
3277 .type = VID_TYPE_TUNER,
3278 .hardware = VID_HARDWARE_BT848,
3279 .fops = &radio_fops,
3283 /* ----------------------------------------------------------------------- */
3284 /* some debug code */
3286 static int bttv_risc_decode(u32 risc)
3288 static char *instr[16] = {
3289 [ BT848_RISC_WRITE >> 28 ] = "write",
3290 [ BT848_RISC_SKIP >> 28 ] = "skip",
3291 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3292 [ BT848_RISC_JUMP >> 28 ] = "jump",
3293 [ BT848_RISC_SYNC >> 28 ] = "sync",
3294 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3295 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3296 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3298 static int incr[16] = {
3299 [ BT848_RISC_WRITE >> 28 ] = 2,
3300 [ BT848_RISC_JUMP >> 28 ] = 2,
3301 [ BT848_RISC_SYNC >> 28 ] = 2,
3302 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3303 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3304 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3306 static char *bits[] = {
3307 "be0", "be1", "be2", "be3/resync",
3308 "set0", "set1", "set2", "set3",
3309 "clr0", "clr1", "clr2", "clr3",
3310 "irq", "res", "eol", "sol",
3314 printk("0x%08x [ %s", risc,
3315 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3316 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3317 if (risc & (1 << (i + 12)))
3318 printk(" %s",bits[i]);
3319 printk(" count=%d ]\n", risc & 0xfff);
3320 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3323 static void bttv_risc_disasm(struct bttv *btv,
3324 struct btcx_riscmem *risc)
3328 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3329 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3330 for (i = 0; i < (risc->size >> 2); i += n) {
3331 printk("%s: 0x%lx: ", btv->c.name,
3332 (unsigned long)(risc->dma + (i<<2)));
3333 n = bttv_risc_decode(risc->cpu[i]);
3334 for (j = 1; j < n; j++)
3335 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3336 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3338 if (0 == risc->cpu[i])
3343 static void bttv_print_riscaddr(struct bttv *btv)
3345 printk(" main: %08Lx\n",
3346 (unsigned long long)btv->main.dma);
3347 printk(" vbi : o=%08Lx e=%08Lx\n",
3348 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3349 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3350 printk(" cap : o=%08Lx e=%08Lx\n",
3351 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3352 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3353 printk(" scr : o=%08Lx e=%08Lx\n",
3354 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3355 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3356 bttv_risc_disasm(btv, &btv->main);
3359 /* ----------------------------------------------------------------------- */
3362 static char *irq_name[] = {
3363 "FMTCHG", // format change detected (525 vs. 625)
3364 "VSYNC", // vertical sync (new field)
3365 "HSYNC", // horizontal sync
3366 "OFLOW", // chroma/luma AGC overflow
3367 "HLOCK", // horizontal lock changed
3368 "VPRES", // video presence changed
3370 "I2CDONE", // hw irc operation finished
3371 "GPINT", // gpio port triggered irq
3373 "RISCI", // risc instruction triggered irq
3374 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3375 "FTRGT", // pixel data fifo overrun
3376 "FDSR", // fifo data stream resyncronisation
3377 "PPERR", // parity error (data transfer)
3378 "RIPERR", // parity error (read risc instructions)
3379 "PABORT", // pci abort
3380 "OCERR", // risc instruction error
3381 "SCERR", // syncronisation error
3384 static void bttv_print_irqbits(u32 print, u32 mark)
3389 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3390 if (print & (1 << i))
3391 printk(" %s",irq_name[i]);
3392 if (mark & (1 << i))
3397 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3399 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3401 (unsigned long)btv->main.dma,
3402 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3403 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3406 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3407 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3408 "Ok, then this is harmless, don't worry ;)\n",
3412 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3414 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3420 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3422 struct bttv_buffer *item;
3424 memset(set,0,sizeof(*set));
3426 /* capture request ? */
3427 if (!list_empty(&btv->capture)) {
3429 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3430 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3432 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3435 /* capture request for other field ? */
3436 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3437 (item->vb.queue.next != &btv->capture)) {
3438 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3439 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3440 if (NULL == set->top &&
3441 V4L2_FIELD_TOP == item->vb.field) {
3444 if (NULL == set->bottom &&
3445 V4L2_FIELD_BOTTOM == item->vb.field) {
3448 if (NULL != set->top && NULL != set->bottom)
3454 /* screen overlay ? */
3455 if (NULL != btv->screen) {
3456 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3457 if (NULL == set->top && NULL == set->bottom) {
3458 set->top = btv->screen;
3459 set->bottom = btv->screen;
3462 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3464 set->top = btv->screen;
3466 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3467 NULL == set->bottom) {
3468 set->bottom = btv->screen;
3473 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3474 btv->c.nr,set->top, set->bottom,
3475 btv->screen,set->frame_irq,set->top_irq);
3480 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3481 struct bttv_buffer_set *curr, unsigned int state)
3485 do_gettimeofday(&ts);
3487 if (wakeup->top == wakeup->bottom) {
3488 if (NULL != wakeup->top && curr->top != wakeup->top) {
3490 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3491 wakeup->top->vb.ts = ts;
3492 wakeup->top->vb.field_count = btv->field_count;
3493 wakeup->top->vb.state = state;
3494 wake_up(&wakeup->top->vb.done);
3497 if (NULL != wakeup->top && curr->top != wakeup->top) {
3499 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3500 wakeup->top->vb.ts = ts;
3501 wakeup->top->vb.field_count = btv->field_count;
3502 wakeup->top->vb.state = state;
3503 wake_up(&wakeup->top->vb.done);
3505 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3507 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3508 wakeup->bottom->vb.ts = ts;
3509 wakeup->bottom->vb.field_count = btv->field_count;
3510 wakeup->bottom->vb.state = state;
3511 wake_up(&wakeup->bottom->vb.done);
3517 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3525 do_gettimeofday(&ts);
3527 wakeup->vb.field_count = btv->field_count;
3528 wakeup->vb.state = state;
3529 wake_up(&wakeup->vb.done);
3532 static void bttv_irq_timeout(unsigned long data)
3534 struct bttv *btv = (struct bttv *)data;
3535 struct bttv_buffer_set old,new;
3536 struct bttv_buffer *ovbi;
3537 struct bttv_buffer *item;
3538 unsigned long flags;
3541 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3542 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3543 btread(BT848_RISC_COUNT));
3544 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3548 spin_lock_irqsave(&btv->s_lock,flags);
3550 /* deactivate stuff */
3551 memset(&new,0,sizeof(new));
3557 bttv_buffer_activate_video(btv, &new);
3558 bttv_buffer_activate_vbi(btv, NULL);
3559 bttv_set_dma(btv, 0);
3562 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3563 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3565 /* cancel all outstanding capture / vbi requests */
3566 while (!list_empty(&btv->capture)) {
3567 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3568 list_del(&item->vb.queue);
3569 item->vb.state = STATE_ERROR;
3570 wake_up(&item->vb.done);
3572 while (!list_empty(&btv->vcapture)) {
3573 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3574 list_del(&item->vb.queue);
3575 item->vb.state = STATE_ERROR;
3576 wake_up(&item->vb.done);
3580 spin_unlock_irqrestore(&btv->s_lock,flags);
3584 bttv_irq_wakeup_top(struct bttv *btv)
3586 struct bttv_buffer *wakeup = btv->curr.top;
3591 spin_lock(&btv->s_lock);
3592 btv->curr.top_irq = 0;
3593 btv->curr.top = NULL;
3594 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3596 do_gettimeofday(&wakeup->vb.ts);
3597 wakeup->vb.field_count = btv->field_count;
3598 wakeup->vb.state = STATE_DONE;
3599 wake_up(&wakeup->vb.done);
3600 spin_unlock(&btv->s_lock);
3603 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3607 if (rc > risc->dma + risc->size)
3613 bttv_irq_switch_video(struct bttv *btv)
3615 struct bttv_buffer_set new;
3616 struct bttv_buffer_set old;
3619 spin_lock(&btv->s_lock);
3621 /* new buffer set */
3622 bttv_irq_next_video(btv, &new);
3623 rc = btread(BT848_RISC_COUNT);
3624 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3625 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3628 bttv_irq_debug_low_latency(btv, rc);
3629 spin_unlock(&btv->s_lock);
3636 btv->loop_irq &= ~1;
3637 bttv_buffer_activate_video(btv, &new);
3638 bttv_set_dma(btv, 0);
3641 if (UNSET != btv->new_input) {
3642 video_mux(btv,btv->new_input);
3643 btv->new_input = UNSET;
3646 /* wake up finished buffers */
3647 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3648 spin_unlock(&btv->s_lock);
3652 bttv_irq_switch_vbi(struct bttv *btv)
3654 struct bttv_buffer *new = NULL;
3655 struct bttv_buffer *old;
3658 spin_lock(&btv->s_lock);
3660 if (!list_empty(&btv->vcapture))
3661 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3664 rc = btread(BT848_RISC_COUNT);
3665 if (NULL != old && (is_active(&old->top, rc) ||
3666 is_active(&old->bottom, rc))) {
3669 bttv_irq_debug_low_latency(btv, rc);
3670 spin_unlock(&btv->s_lock);
3676 btv->loop_irq &= ~4;
3677 bttv_buffer_activate_vbi(btv, new);
3678 bttv_set_dma(btv, 0);
3680 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3681 spin_unlock(&btv->s_lock);
3684 static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3692 btv=(struct bttv *)dev_id;
3695 handled = bttv_any_irq(&btv->c);
3699 /* get/clear interrupt status bits */
3700 stat=btread(BT848_INT_STAT);
3701 astat=stat&btread(BT848_INT_MASK);
3705 btwrite(stat,BT848_INT_STAT);
3707 /* get device status bits */
3708 dstat=btread(BT848_DSTATUS);
3711 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3712 "riscs=%x, riscc=%08x, ",
3713 btv->c.nr, count, btv->field_count,
3714 stat>>28, btread(BT848_RISC_COUNT));
3715 bttv_print_irqbits(stat,astat);
3716 if (stat & BT848_INT_HLOCK)
3717 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3719 if (stat & BT848_INT_VPRES)
3720 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3722 if (stat & BT848_INT_FMTCHG)
3723 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3728 if (astat&BT848_INT_VSYNC)
3731 if (astat & BT848_INT_GPINT) {
3732 wake_up(&btv->gpioq);
3733 bttv_gpio_irq(&btv->c);
3736 if (astat & BT848_INT_I2CDONE) {
3737 btv->i2c_done = stat;
3738 wake_up(&btv->i2c_queue);
3741 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3742 bttv_irq_switch_vbi(btv);
3744 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3745 bttv_irq_wakeup_top(btv);
3747 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3748 bttv_irq_switch_video(btv);
3750 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3753 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3754 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3755 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3756 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3757 btread(BT848_RISC_COUNT));
3758 bttv_print_irqbits(stat,astat);
3761 bttv_print_riscaddr(btv);
3763 if (fdsr && astat & BT848_INT_FDSR) {
3764 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3765 btv->c.nr,btread(BT848_RISC_COUNT));
3767 bttv_print_riscaddr(btv);
3773 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3774 btwrite(0, BT848_INT_MASK);
3777 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3780 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
3782 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3786 bttv_print_irqbits(stat,astat);
3794 return IRQ_RETVAL(handled);
3798 /* ----------------------------------------------------------------------- */
3799 /* initialitation */
3801 static struct video_device *vdev_init(struct bttv *btv,
3802 struct video_device *template,
3805 struct video_device *vfd;
3807 vfd = video_device_alloc();
3812 vfd->dev = &btv->c.pci->dev;
3813 vfd->release = video_device_release;
3814 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3815 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3816 type, bttv_tvcards[btv->c.type].name);
3820 static void bttv_unregister_video(struct bttv *btv)
3822 if (btv->video_dev) {
3823 if (-1 != btv->video_dev->minor)
3824 video_unregister_device(btv->video_dev);
3826 video_device_release(btv->video_dev);
3827 btv->video_dev = NULL;
3830 if (-1 != btv->vbi_dev->minor)
3831 video_unregister_device(btv->vbi_dev);
3833 video_device_release(btv->vbi_dev);
3834 btv->vbi_dev = NULL;
3836 if (btv->radio_dev) {
3837 if (-1 != btv->radio_dev->minor)
3838 video_unregister_device(btv->radio_dev);
3840 video_device_release(btv->radio_dev);
3841 btv->radio_dev = NULL;
3845 /* register video4linux devices */
3846 static int __devinit bttv_register_video(struct bttv *btv)
3848 if (no_overlay <= 0) {
3849 bttv_video_template.type |= VID_TYPE_OVERLAY;
3851 printk("bttv: Overlay support disabled.\n");
3855 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3856 if (NULL == btv->video_dev)
3858 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3860 printk(KERN_INFO "bttv%d: registered device video%d\n",
3861 btv->c.nr,btv->video_dev->minor & 0x1f);
3862 video_device_create_file(btv->video_dev, &class_device_attr_card);
3865 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3866 if (NULL == btv->vbi_dev)
3868 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3870 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3871 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3873 if (!btv->has_radio)
3876 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3877 if (NULL == btv->radio_dev)
3879 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3881 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3882 btv->c.nr,btv->radio_dev->minor & 0x1f);
3888 bttv_unregister_video(btv);
3893 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3894 /* response on cards with no firmware is not enabled by OF */
3895 static void pci_set_command(struct pci_dev *dev)
3897 #if defined(__powerpc__)
3900 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3901 cmd = (cmd | PCI_COMMAND_MEMORY );
3902 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3906 static int __devinit bttv_probe(struct pci_dev *dev,
3907 const struct pci_device_id *pci_id)
3913 if (bttv_num == BTTV_MAX)
3915 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3916 btv=&bttvs[bttv_num];
3917 memset(btv,0,sizeof(*btv));
3918 btv->c.nr = bttv_num;
3919 sprintf(btv->c.name,"bttv%d",btv->c.nr);
3921 /* initialize structs / fill in defaults */
3922 init_MUTEX(&btv->lock);
3923 init_MUTEX(&btv->reslock);
3924 spin_lock_init(&btv->s_lock);
3925 spin_lock_init(&btv->gpio_lock);
3926 init_waitqueue_head(&btv->gpioq);
3927 init_waitqueue_head(&btv->i2c_queue);
3928 INIT_LIST_HEAD(&btv->c.subs);
3929 INIT_LIST_HEAD(&btv->capture);
3930 INIT_LIST_HEAD(&btv->vcapture);
3931 v4l2_prio_init(&btv->prio);
3933 init_timer(&btv->timeout);
3934 btv->timeout.function = bttv_irq_timeout;
3935 btv->timeout.data = (unsigned long)btv;
3938 btv->tuner_type = UNSET;
3939 btv->pinnacle_id = UNSET;
3940 btv->new_input = UNSET;
3941 btv->has_radio=radio[btv->c.nr];
3943 /* pci stuff (init, get irq/mmio, ... */
3945 btv->id = dev->device;
3946 if (pci_enable_device(dev)) {
3947 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3951 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
3952 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3956 if (!request_mem_region(pci_resource_start(dev,0),
3957 pci_resource_len(dev,0),
3959 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3960 btv->c.nr, pci_resource_start(dev,0));
3963 pci_set_master(dev);
3964 pci_set_command(dev);
3965 pci_set_drvdata(dev,btv);
3967 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3968 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3969 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3970 bttv_num,btv->id, btv->revision, pci_name(dev));
3971 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3972 btv->c.pci->irq, lat, pci_resource_start(dev,0));
3975 btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3976 if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3977 printk("bttv%d: ioremap() failed\n", btv->c.nr);
3985 /* disable irqs, register irq handler */
3986 btwrite(0, BT848_INT_MASK);
3987 result = request_irq(btv->c.pci->irq, bttv_irq,
3988 SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
3990 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
3991 bttv_num,btv->c.pci->irq);
3995 if (0 != bttv_handle_chipset(btv)) {
4000 /* init options from insmod args */
4001 btv->opt_combfilter = combfilter;
4002 btv->opt_lumafilter = lumafilter;
4003 btv->opt_automute = automute;
4004 btv->opt_chroma_agc = chroma_agc;
4005 btv->opt_adc_crush = adc_crush;
4006 btv->opt_vcr_hack = vcr_hack;
4007 btv->opt_whitecrush_upper = whitecrush_upper;
4008 btv->opt_whitecrush_lower = whitecrush_lower;
4009 btv->opt_uv_ratio = uv_ratio;
4010 btv->opt_full_luma_range = full_luma_range;
4011 btv->opt_coring = coring;
4013 /* fill struct bttv with some useful defaults */
4014 btv->init.btv = btv;
4015 btv->init.ov.w.width = 320;
4016 btv->init.ov.w.height = 240;
4017 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4018 btv->init.width = 320;
4019 btv->init.height = 240;
4020 btv->init.lines = 16;
4023 /* initialize hardware */
4025 bttv_gpio_tracking(btv,"pre-init");
4027 bttv_risc_init_main(btv);
4031 btwrite(0x00, BT848_GPIO_REG_INP);
4032 btwrite(0x00, BT848_GPIO_OUT_EN);
4034 bttv_gpio_tracking(btv,"init");
4036 /* needs to be done before i2c is registered */
4037 bttv_init_card1(btv);
4039 /* register i2c + gpio */
4042 /* some card-specific stuff (needs working i2c) */
4043 bttv_init_card2(btv);
4046 /* register video4linux + input */
4047 if (!bttv_tvcards[btv->c.type].no_video) {
4048 bttv_register_video(btv);
4049 bt848_bright(btv,32768);
4050 bt848_contrast(btv,32768);
4051 bt848_hue(btv,32768);
4052 bt848_sat(btv,32768);
4053 audio_mux(btv,AUDIO_MUTE);
4057 /* add subdevices */
4058 if (btv->has_remote)
4059 bttv_sub_add_device(&btv->c, "remote");
4060 if (bttv_tvcards[btv->c.type].has_dvb)
4061 bttv_sub_add_device(&btv->c, "dvb");
4063 /* everything is fine */
4068 free_irq(btv->c.pci->irq,btv);
4071 if (btv->bt848_mmio)
4072 iounmap(btv->bt848_mmio);
4073 release_mem_region(pci_resource_start(btv->c.pci,0),
4074 pci_resource_len(btv->c.pci,0));
4075 pci_set_drvdata(dev,NULL);
4079 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4081 struct bttv *btv = pci_get_drvdata(pci_dev);
4084 printk("bttv%d: unloading\n",btv->c.nr);
4086 /* shutdown everything (DMA+IRQs) */
4087 btand(~15, BT848_GPIO_DMA_CTL);
4088 btwrite(0, BT848_INT_MASK);
4089 btwrite(~0x0, BT848_INT_STAT);
4090 btwrite(0x0, BT848_GPIO_OUT_EN);
4092 bttv_gpio_tracking(btv,"cleanup");
4094 /* tell gpio modules we are leaving ... */
4096 wake_up(&btv->gpioq);
4097 bttv_sub_del_devices(&btv->c);
4099 /* unregister i2c_bus + input */
4102 /* unregister video4linux */
4103 bttv_unregister_video(btv);
4105 /* free allocated memory */
4106 btcx_riscmem_free(btv->c.pci,&btv->main);
4108 /* free ressources */
4109 free_irq(btv->c.pci->irq,btv);
4110 iounmap(btv->bt848_mmio);
4111 release_mem_region(pci_resource_start(btv->c.pci,0),
4112 pci_resource_len(btv->c.pci,0));
4114 pci_set_drvdata(pci_dev, NULL);
4118 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4120 struct bttv *btv = pci_get_drvdata(pci_dev);
4121 struct bttv_buffer_set idle;
4122 unsigned long flags;
4124 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4126 /* stop dma + irqs */
4127 spin_lock_irqsave(&btv->s_lock,flags);
4128 memset(&idle, 0, sizeof(idle));
4129 btv->state.video = btv->curr;
4130 btv->state.vbi = btv->cvbi;
4131 btv->state.loop_irq = btv->loop_irq;
4134 bttv_buffer_activate_video(btv, &idle);
4135 bttv_buffer_activate_vbi(btv, NULL);
4136 bttv_set_dma(btv, 0);
4137 btwrite(0, BT848_INT_MASK);
4138 spin_unlock_irqrestore(&btv->s_lock,flags);
4140 /* save bt878 state */
4141 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4142 btv->state.gpio_data = gpio_read();
4144 /* save pci state */
4145 pci_save_state(pci_dev);
4146 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4147 pci_disable_device(pci_dev);
4148 btv->state.disabled = 1;
4153 static int bttv_resume(struct pci_dev *pci_dev)
4155 struct bttv *btv = pci_get_drvdata(pci_dev);
4156 unsigned long flags;
4159 dprintk("bttv%d: resume\n", btv->c.nr);
4161 /* restore pci state */
4162 if (btv->state.disabled) {
4163 err=pci_enable_device(pci_dev);
4165 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4169 btv->state.disabled = 0;
4171 err=pci_set_power_state(pci_dev, PCI_D0);
4173 pci_disable_device(pci_dev);
4174 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4176 btv->state.disabled = 1;
4180 pci_restore_state(pci_dev);
4182 /* restore bt878 state */
4183 bttv_reinit_bt848(btv);
4184 gpio_inout(0xffffff, btv->state.gpio_enable);
4185 gpio_write(btv->state.gpio_data);
4188 spin_lock_irqsave(&btv->s_lock,flags);
4189 btv->curr = btv->state.video;
4190 btv->cvbi = btv->state.vbi;
4191 btv->loop_irq = btv->state.loop_irq;
4192 bttv_buffer_activate_video(btv, &btv->curr);
4193 bttv_buffer_activate_vbi(btv, btv->cvbi);
4194 bttv_set_dma(btv, 0);
4195 spin_unlock_irqrestore(&btv->s_lock,flags);
4199 static struct pci_device_id bttv_pci_tbl[] = {
4200 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4201 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4202 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4203 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4204 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4205 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4206 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4207 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4211 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4213 static struct pci_driver bttv_pci_driver = {
4215 .id_table = bttv_pci_tbl,
4216 .probe = bttv_probe,
4217 .remove = __devexit_p(bttv_remove),
4218 .suspend = bttv_suspend,
4219 .resume = bttv_resume,
4222 static int bttv_init_module(void)
4226 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4227 (BTTV_VERSION_CODE >> 16) & 0xff,
4228 (BTTV_VERSION_CODE >> 8) & 0xff,
4229 BTTV_VERSION_CODE & 0xff);
4231 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4232 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4234 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4236 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4237 gbufsize = BTTV_MAX_FBUF;
4238 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4240 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4241 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4243 bttv_check_chipset();
4245 bus_register(&bttv_sub_bus_type);
4246 return pci_module_init(&bttv_pci_driver);
4249 static void bttv_cleanup_module(void)
4251 pci_unregister_driver(&bttv_pci_driver);
4252 bus_unregister(&bttv_sub_bus_type);
4256 module_init(bttv_init_module);
4257 module_exit(bttv_cleanup_module);