2 $Id: bttv-driver.c,v 1.34 2005/01/07 13:11:19 kraxel Exp $
4 bttv - Bt848 frame grabber driver
6 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
7 & Marcus Metzler <mocm@thp.uni-koeln.de>
8 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
10 some v4l2 code lines are taken from Justin's bttv2 driver which is
11 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/delay.h>
32 #include <linux/errno.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
37 #include <linux/kdev_t.h>
40 #include <asm/byteorder.h>
44 unsigned int bttv_num; /* number of Bt848s in use */
45 struct bttv bttvs[BTTV_MAX];
47 unsigned int bttv_debug = 0;
48 unsigned int bttv_verbose = 1;
49 unsigned int bttv_gpio = 0;
51 /* config variables */
53 static unsigned int bigendian=1;
55 static unsigned int bigendian=0;
57 static unsigned int radio[BTTV_MAX];
58 static unsigned int irq_debug = 0;
59 static unsigned int gbuffers = 8;
60 static unsigned int gbufsize = 0x208000;
62 static int video_nr = -1;
63 static int radio_nr = -1;
64 static int vbi_nr = -1;
65 static int debug_latency = 0;
67 static unsigned int fdsr = 0;
70 static unsigned int combfilter = 0;
71 static unsigned int lumafilter = 0;
72 static unsigned int automute = 1;
73 static unsigned int chroma_agc = 0;
74 static unsigned int adc_crush = 1;
75 static unsigned int whitecrush_upper = 0xCF;
76 static unsigned int whitecrush_lower = 0x7F;
77 static unsigned int vcr_hack = 0;
78 static unsigned int irq_iswitch = 0;
80 /* API features (turn on/off stuff for testing) */
81 static unsigned int v4l2 = 1;
85 module_param(bttv_verbose, int, 0644);
86 module_param(bttv_gpio, int, 0644);
87 module_param(bttv_debug, int, 0644);
88 module_param(irq_debug, int, 0644);
89 module_param(debug_latency, int, 0644);
91 module_param(fdsr, int, 0444);
92 module_param(video_nr, int, 0444);
93 module_param(radio_nr, int, 0444);
94 module_param(vbi_nr, int, 0444);
95 module_param(gbuffers, int, 0444);
96 module_param(gbufsize, int, 0444);
98 module_param(v4l2, int, 0644);
99 module_param(bigendian, int, 0644);
100 module_param(irq_iswitch, int, 0644);
101 module_param(combfilter, int, 0444);
102 module_param(lumafilter, int, 0444);
103 module_param(automute, int, 0444);
104 module_param(chroma_agc, int, 0444);
105 module_param(adc_crush, int, 0444);
106 module_param(whitecrush_upper, int, 0444);
107 module_param(whitecrush_lower, int, 0444);
108 module_param(vcr_hack, int, 0444);
110 module_param_array(radio, int, NULL, 0444);
112 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
113 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
114 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
115 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
116 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
117 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
118 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
119 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
120 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
121 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
122 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
123 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
124 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
125 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
126 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
128 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
129 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
130 MODULE_LICENSE("GPL");
132 /* ----------------------------------------------------------------------- */
135 static ssize_t show_card(struct class_device *cd, char *buf)
137 struct video_device *vfd = to_video_device(cd);
138 struct bttv *btv = dev_get_drvdata(vfd->dev);
139 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
141 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
143 /* ----------------------------------------------------------------------- */
146 /* special timing tables from conexant... */
147 static u8 SRAM_Table[][60] =
149 /* PAL digital input over GPIO[7:0] */
151 45, // 45 bytes following
152 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
153 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
154 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
155 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
156 0x37,0x00,0xAF,0x21,0x00
158 /* NTSC digital input over GPIO[7:0] */
160 51, // 51 bytes following
161 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
162 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
163 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
164 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
165 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
168 // TGB_NTSC392 // quartzsight
169 // This table has been modified to be used for Fusion Rev D
171 0x2A, // size of table = 42
172 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
173 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
174 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
175 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
180 const struct bttv_tvnorm bttv_tvnorms[] = {
182 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
183 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
185 .v4l2_id = V4L2_STD_PAL,
193 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
194 .scaledtwidth = 1135,
201 .v4l2_id = V4L2_STD_NTSC_M,
209 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
217 .v4l2_id = V4L2_STD_SECAM,
225 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
226 .scaledtwidth = 1135,
231 .sram = 0, /* like PAL, correct? */
233 .v4l2_id = V4L2_STD_PAL_Nc,
241 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
249 .v4l2_id = V4L2_STD_PAL_M,
257 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
265 .v4l2_id = V4L2_STD_PAL_N,
273 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
281 .v4l2_id = V4L2_STD_NTSC_M_JP,
289 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
297 /* that one hopefully works with the strange timing
298 * which video recorders produce when playing a NTSC
299 * tape on a PAL TV ... */
300 .v4l2_id = V4L2_STD_PAL_60,
308 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
309 .scaledtwidth = 1135,
318 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
320 /* ----------------------------------------------------------------------- */
322 packed pixel formats must come first */
323 static const struct bttv_format bttv_formats[] = {
325 .name = "8 bpp, gray",
326 .palette = VIDEO_PALETTE_GREY,
327 .fourcc = V4L2_PIX_FMT_GREY,
328 .btformat = BT848_COLOR_FMT_Y8,
330 .flags = FORMAT_FLAGS_PACKED,
332 .name = "8 bpp, dithered color",
333 .palette = VIDEO_PALETTE_HI240,
334 .fourcc = V4L2_PIX_FMT_HI240,
335 .btformat = BT848_COLOR_FMT_RGB8,
337 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
339 .name = "15 bpp RGB, le",
340 .palette = VIDEO_PALETTE_RGB555,
341 .fourcc = V4L2_PIX_FMT_RGB555,
342 .btformat = BT848_COLOR_FMT_RGB15,
344 .flags = FORMAT_FLAGS_PACKED,
346 .name = "15 bpp RGB, be",
348 .fourcc = V4L2_PIX_FMT_RGB555X,
349 .btformat = BT848_COLOR_FMT_RGB15,
350 .btswap = 0x03, /* byteswap */
352 .flags = FORMAT_FLAGS_PACKED,
354 .name = "16 bpp RGB, le",
355 .palette = VIDEO_PALETTE_RGB565,
356 .fourcc = V4L2_PIX_FMT_RGB565,
357 .btformat = BT848_COLOR_FMT_RGB16,
359 .flags = FORMAT_FLAGS_PACKED,
361 .name = "16 bpp RGB, be",
363 .fourcc = V4L2_PIX_FMT_RGB565X,
364 .btformat = BT848_COLOR_FMT_RGB16,
365 .btswap = 0x03, /* byteswap */
367 .flags = FORMAT_FLAGS_PACKED,
369 .name = "24 bpp RGB, le",
370 .palette = VIDEO_PALETTE_RGB24,
371 .fourcc = V4L2_PIX_FMT_BGR24,
372 .btformat = BT848_COLOR_FMT_RGB24,
374 .flags = FORMAT_FLAGS_PACKED,
376 .name = "32 bpp RGB, le",
377 .palette = VIDEO_PALETTE_RGB32,
378 .fourcc = V4L2_PIX_FMT_BGR32,
379 .btformat = BT848_COLOR_FMT_RGB32,
381 .flags = FORMAT_FLAGS_PACKED,
383 .name = "32 bpp RGB, be",
385 .fourcc = V4L2_PIX_FMT_RGB32,
386 .btformat = BT848_COLOR_FMT_RGB32,
387 .btswap = 0x0f, /* byte+word swap */
389 .flags = FORMAT_FLAGS_PACKED,
391 .name = "4:2:2, packed, YUYV",
392 .palette = VIDEO_PALETTE_YUV422,
393 .fourcc = V4L2_PIX_FMT_YUYV,
394 .btformat = BT848_COLOR_FMT_YUY2,
396 .flags = FORMAT_FLAGS_PACKED,
398 .name = "4:2:2, packed, YUYV",
399 .palette = VIDEO_PALETTE_YUYV,
400 .fourcc = V4L2_PIX_FMT_YUYV,
401 .btformat = BT848_COLOR_FMT_YUY2,
403 .flags = FORMAT_FLAGS_PACKED,
405 .name = "4:2:2, packed, UYVY",
406 .palette = VIDEO_PALETTE_UYVY,
407 .fourcc = V4L2_PIX_FMT_UYVY,
408 .btformat = BT848_COLOR_FMT_YUY2,
409 .btswap = 0x03, /* byteswap */
411 .flags = FORMAT_FLAGS_PACKED,
413 .name = "4:2:2, planar, Y-Cb-Cr",
414 .palette = VIDEO_PALETTE_YUV422P,
415 .fourcc = V4L2_PIX_FMT_YUV422P,
416 .btformat = BT848_COLOR_FMT_YCrCb422,
418 .flags = FORMAT_FLAGS_PLANAR,
422 .name = "4:2:0, planar, Y-Cb-Cr",
423 .palette = VIDEO_PALETTE_YUV420P,
424 .fourcc = V4L2_PIX_FMT_YUV420,
425 .btformat = BT848_COLOR_FMT_YCrCb422,
427 .flags = FORMAT_FLAGS_PLANAR,
431 .name = "4:2:0, planar, Y-Cr-Cb",
433 .fourcc = V4L2_PIX_FMT_YVU420,
434 .btformat = BT848_COLOR_FMT_YCrCb422,
436 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
440 .name = "4:1:1, planar, Y-Cb-Cr",
441 .palette = VIDEO_PALETTE_YUV411P,
442 .fourcc = V4L2_PIX_FMT_YUV411P,
443 .btformat = BT848_COLOR_FMT_YCrCb411,
445 .flags = FORMAT_FLAGS_PLANAR,
449 .name = "4:1:0, planar, Y-Cb-Cr",
450 .palette = VIDEO_PALETTE_YUV410P,
451 .fourcc = V4L2_PIX_FMT_YUV410,
452 .btformat = BT848_COLOR_FMT_YCrCb411,
454 .flags = FORMAT_FLAGS_PLANAR,
458 .name = "4:1:0, planar, Y-Cr-Cb",
460 .fourcc = V4L2_PIX_FMT_YVU410,
461 .btformat = BT848_COLOR_FMT_YCrCb411,
463 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
467 .name = "raw scanlines",
468 .palette = VIDEO_PALETTE_RAW,
470 .btformat = BT848_COLOR_FMT_RAW,
472 .flags = FORMAT_FLAGS_RAW,
475 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
477 /* ----------------------------------------------------------------------- */
479 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
480 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
481 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
482 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
483 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
484 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
485 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
486 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
487 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 8)
489 static const struct v4l2_queryctrl no_ctl = {
491 .flags = V4L2_CTRL_FLAG_DISABLED,
493 static const struct v4l2_queryctrl bttv_ctls[] = {
496 .id = V4L2_CID_BRIGHTNESS,
497 .name = "Brightness",
501 .default_value = 32768,
502 .type = V4L2_CTRL_TYPE_INTEGER,
504 .id = V4L2_CID_CONTRAST,
509 .default_value = 32768,
510 .type = V4L2_CTRL_TYPE_INTEGER,
512 .id = V4L2_CID_SATURATION,
513 .name = "Saturation",
517 .default_value = 32768,
518 .type = V4L2_CTRL_TYPE_INTEGER,
525 .default_value = 32768,
526 .type = V4L2_CTRL_TYPE_INTEGER,
530 .id = V4L2_CID_AUDIO_MUTE,
534 .type = V4L2_CTRL_TYPE_BOOLEAN,
536 .id = V4L2_CID_AUDIO_VOLUME,
541 .default_value = 65535,
542 .type = V4L2_CTRL_TYPE_INTEGER,
544 .id = V4L2_CID_AUDIO_BALANCE,
549 .default_value = 32768,
550 .type = V4L2_CTRL_TYPE_INTEGER,
552 .id = V4L2_CID_AUDIO_BASS,
557 .default_value = 32768,
558 .type = V4L2_CTRL_TYPE_INTEGER,
560 .id = V4L2_CID_AUDIO_TREBLE,
565 .default_value = 32768,
566 .type = V4L2_CTRL_TYPE_INTEGER,
568 /* --- private --- */
570 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
571 .name = "chroma agc",
574 .type = V4L2_CTRL_TYPE_BOOLEAN,
576 .id = V4L2_CID_PRIVATE_COMBFILTER,
577 .name = "combfilter",
580 .type = V4L2_CTRL_TYPE_BOOLEAN,
582 .id = V4L2_CID_PRIVATE_AUTOMUTE,
586 .type = V4L2_CTRL_TYPE_BOOLEAN,
588 .id = V4L2_CID_PRIVATE_LUMAFILTER,
589 .name = "luma decimation filter",
592 .type = V4L2_CTRL_TYPE_BOOLEAN,
594 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
598 .type = V4L2_CTRL_TYPE_BOOLEAN,
600 .id = V4L2_CID_PRIVATE_VCR_HACK,
604 .type = V4L2_CTRL_TYPE_BOOLEAN,
606 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
607 .name = "whitecrush upper",
611 .default_value = 0xCF,
612 .type = V4L2_CTRL_TYPE_INTEGER,
614 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
615 .name = "whitecrush lower",
619 .default_value = 0x7F,
620 .type = V4L2_CTRL_TYPE_INTEGER,
624 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
626 /* ----------------------------------------------------------------------- */
627 /* resource management */
630 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
632 if (fh->resources & bit)
633 /* have it already allocated */
638 if (btv->resources & bit) {
639 /* no, someone else uses it */
643 /* it's free, grab it */
644 fh->resources |= bit;
645 btv->resources |= bit;
651 int check_btres(struct bttv_fh *fh, int bit)
653 return (fh->resources & bit);
657 int locked_btres(struct bttv *btv, int bit)
659 return (btv->resources & bit);
663 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
666 if ((fh->resources & bits) != bits) {
667 /* trying to free ressources not allocated by us ... */
668 printk("bttv: BUG! (btres)\n");
672 fh->resources &= ~bits;
673 btv->resources &= ~bits;
677 /* ----------------------------------------------------------------------- */
678 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
680 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
681 PLL_X = Reference pre-divider (0=1, 1=2)
682 PLL_C = Post divider (0=6, 1=4)
683 PLL_I = Integer input
684 PLL_F = Fractional input
686 F_input = 28.636363 MHz:
687 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
690 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
692 unsigned char fl, fh, fi;
694 /* prevent overflows */
707 btwrite(fl, BT848_PLL_F_LO);
708 btwrite(fh, BT848_PLL_F_HI);
709 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
712 static void set_pll(struct bttv *btv)
716 if (!btv->pll.pll_crystal)
719 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
720 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
724 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
726 if (btv->pll.pll_current == 0)
728 vprintk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
729 btv->c.nr,btv->pll.pll_ifreq);
730 btwrite(0x00,BT848_TGCTRL);
731 btwrite(0x00,BT848_PLL_XCI);
732 btv->pll.pll_current = 0;
736 vprintk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
737 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
738 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
740 for (i=0; i<10; i++) {
741 /* Let other people run while the PLL stabilizes */
745 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
746 btwrite(0,BT848_DSTATUS);
748 btwrite(0x08,BT848_TGCTRL);
749 btv->pll.pll_current = btv->pll.pll_ofreq;
754 btv->pll.pll_current = -1;
759 /* used to switch between the bt848's analog/digital video capture modes */
760 static void bt848A_set_timing(struct bttv *btv)
763 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
764 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
766 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
767 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
768 btv->c.nr,table_idx);
770 /* timing change...reset timing generator address */
771 btwrite(0x00, BT848_TGCTRL);
772 btwrite(0x02, BT848_TGCTRL);
773 btwrite(0x00, BT848_TGCTRL);
775 len=SRAM_Table[table_idx][0];
776 for(i = 1; i <= len; i++)
777 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
778 btv->pll.pll_ofreq = 27000000;
781 btwrite(0x11, BT848_TGCTRL);
782 btwrite(0x41, BT848_DVSIF);
784 btv->pll.pll_ofreq = fsc;
786 btwrite(0x0, BT848_DVSIF);
790 /* ----------------------------------------------------------------------- */
792 static void bt848_bright(struct bttv *btv, int bright)
796 // printk("bttv: set bright: %d\n",bright); // DEBUG
797 btv->bright = bright;
799 /* We want -128 to 127 we get 0-65535 */
800 value = (bright >> 8) - 128;
801 btwrite(value & 0xff, BT848_BRIGHT);
804 static void bt848_hue(struct bttv *btv, int hue)
811 value = (hue >> 8) - 128;
812 btwrite(value & 0xff, BT848_HUE);
815 static void bt848_contrast(struct bttv *btv, int cont)
819 btv->contrast = cont;
823 hibit = (value >> 6) & 4;
824 btwrite(value & 0xff, BT848_CONTRAST_LO);
825 btaor(hibit, ~4, BT848_E_CONTROL);
826 btaor(hibit, ~4, BT848_O_CONTROL);
829 static void bt848_sat(struct bttv *btv, int color)
831 int val_u,val_v,hibits;
833 btv->saturation = color;
835 /* 0-511 for the color */
837 val_v = ((color>>7)*180L)/254;
838 hibits = (val_u >> 7) & 2;
839 hibits |= (val_v >> 8) & 1;
840 btwrite(val_u & 0xff, BT848_SAT_U_LO);
841 btwrite(val_v & 0xff, BT848_SAT_V_LO);
842 btaor(hibits, ~3, BT848_E_CONTROL);
843 btaor(hibits, ~3, BT848_O_CONTROL);
846 /* ----------------------------------------------------------------------- */
849 video_mux(struct bttv *btv, unsigned int input)
853 if (input >= bttv_tvcards[btv->c.type].video_inputs)
856 /* needed by RemoteVideo MX */
857 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
859 gpio_inout(mask2,mask2);
861 if (input == btv->svhs) {
862 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
863 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
865 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
866 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
868 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
869 btaor(mux<<5, ~(3<<5), BT848_IFORM);
870 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
871 btv->c.nr,input,mux);
873 /* card specific hook */
874 if(bttv_tvcards[btv->c.type].muxsel_hook)
875 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
879 static char *audio_modes[] = {
880 "audio: tuner", "audio: radio", "audio: extern",
881 "audio: intern", "audio: off"
885 audio_mux(struct bttv *btv, int mode)
887 int val,mux,i2c_mux,signal;
889 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
890 bttv_tvcards[btv->c.type].gpiomask);
891 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
895 btv->audio |= AUDIO_MUTE;
898 btv->audio &= ~AUDIO_MUTE;
904 btv->audio &= AUDIO_MUTE;
907 i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
908 if (btv->opt_automute && !signal && !btv->radio_user)
911 printk("bttv%d: amux: mode=%d audio=%d signal=%s mux=%d/%d irq=%s\n",
912 btv->c.nr, mode, btv->audio, signal ? "yes" : "no",
913 mux, i2c_mux, in_interrupt() ? "yes" : "no");
916 val = bttv_tvcards[btv->c.type].audiomux[mux];
917 gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
919 bttv_gpio_tracking(btv,audio_modes[mux]);
921 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
926 i2c_vidiocschan(struct bttv *btv)
928 struct video_channel c;
930 memset(&c,0,sizeof(c));
931 c.norm = btv->tvnorm;
932 c.channel = btv->input;
933 bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
934 if (btv->c.type == BTTV_VOODOOTV_FM)
935 bttv_tda9880_setnorm(btv,c.norm);
939 set_tvnorm(struct bttv *btv, unsigned int norm)
941 const struct bttv_tvnorm *tvnorm;
943 if (norm < 0 || norm >= BTTV_TVNORMS)
947 tvnorm = &bttv_tvnorms[norm];
949 btwrite(tvnorm->adelay, BT848_ADELAY);
950 btwrite(tvnorm->bdelay, BT848_BDELAY);
951 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
953 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
954 btwrite(1, BT848_VBI_PACK_DEL);
955 bt848A_set_timing(btv);
957 switch (btv->c.type) {
958 case BTTV_VOODOOTV_FM:
959 bttv_tda9880_setnorm(btv,norm);
963 osprey_540_set_norm(btv,norm);
971 set_input(struct bttv *btv, unsigned int input)
977 spin_lock_irqsave(&btv->s_lock,flags);
978 if (btv->curr.frame_irq) {
979 /* active capture -> delayed input switch */
980 btv->new_input = input;
982 video_mux(btv,input);
984 spin_unlock_irqrestore(&btv->s_lock,flags);
986 video_mux(btv,input);
988 audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
989 AUDIO_TUNER : AUDIO_EXTERN));
990 set_tvnorm(btv,btv->tvnorm);
991 i2c_vidiocschan(btv);
994 static void init_irqreg(struct bttv *btv)
997 btwrite(0xfffffUL, BT848_INT_STAT);
999 if (bttv_tvcards[btv->c.type].no_video) {
1001 btwrite(BT848_INT_I2CDONE,
1005 btwrite((btv->triton1) |
1006 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1008 (fdsr ? BT848_INT_FDSR : 0) |
1009 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1010 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1016 static void init_bt848(struct bttv *btv)
1020 if (bttv_tvcards[btv->c.type].no_video) {
1021 /* very basic init only */
1026 btwrite(0x00, BT848_CAP_CTL);
1027 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1028 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1030 /* set planar and packed mode trigger points and */
1031 /* set rising edge of inverted GPINTR pin as irq trigger */
1032 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1033 BT848_GPIO_DMA_CTL_PLTP1_16|
1034 BT848_GPIO_DMA_CTL_PLTP23_16|
1035 BT848_GPIO_DMA_CTL_GPINTC|
1036 BT848_GPIO_DMA_CTL_GPINTI,
1037 BT848_GPIO_DMA_CTL);
1039 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1040 btwrite(val, BT848_E_SCLOOP);
1041 btwrite(val, BT848_O_SCLOOP);
1043 btwrite(0x20, BT848_E_VSCALE_HI);
1044 btwrite(0x20, BT848_O_VSCALE_HI);
1045 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1048 btwrite(whitecrush_upper, BT848_WC_UP);
1049 btwrite(whitecrush_lower, BT848_WC_DOWN);
1051 if (btv->opt_lumafilter) {
1052 btwrite(0, BT848_E_CONTROL);
1053 btwrite(0, BT848_O_CONTROL);
1055 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1056 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1059 bt848_bright(btv, btv->bright);
1060 bt848_hue(btv, btv->hue);
1061 bt848_contrast(btv, btv->contrast);
1062 bt848_sat(btv, btv->saturation);
1068 static void bttv_reinit_bt848(struct bttv *btv)
1070 unsigned long flags;
1073 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1074 spin_lock_irqsave(&btv->s_lock,flags);
1076 bttv_set_dma(btv,0);
1077 spin_unlock_irqrestore(&btv->s_lock,flags);
1080 btv->pll.pll_current = -1;
1081 set_input(btv,btv->input);
1084 static int get_control(struct bttv *btv, struct v4l2_control *c)
1086 struct video_audio va;
1089 for (i = 0; i < BTTV_CTLS; i++)
1090 if (bttv_ctls[i].id == c->id)
1094 if (i >= 4 && i <= 8) {
1095 memset(&va,0,sizeof(va));
1096 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1097 if (btv->audio_hook)
1098 btv->audio_hook(btv,&va,0);
1101 case V4L2_CID_BRIGHTNESS:
1102 c->value = btv->bright;
1105 c->value = btv->hue;
1107 case V4L2_CID_CONTRAST:
1108 c->value = btv->contrast;
1110 case V4L2_CID_SATURATION:
1111 c->value = btv->saturation;
1114 case V4L2_CID_AUDIO_MUTE:
1115 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1117 case V4L2_CID_AUDIO_VOLUME:
1118 c->value = va.volume;
1120 case V4L2_CID_AUDIO_BALANCE:
1121 c->value = va.balance;
1123 case V4L2_CID_AUDIO_BASS:
1126 case V4L2_CID_AUDIO_TREBLE:
1127 c->value = va.treble;
1130 case V4L2_CID_PRIVATE_CHROMA_AGC:
1131 c->value = btv->opt_chroma_agc;
1133 case V4L2_CID_PRIVATE_COMBFILTER:
1134 c->value = btv->opt_combfilter;
1136 case V4L2_CID_PRIVATE_LUMAFILTER:
1137 c->value = btv->opt_lumafilter;
1139 case V4L2_CID_PRIVATE_AUTOMUTE:
1140 c->value = btv->opt_automute;
1142 case V4L2_CID_PRIVATE_AGC_CRUSH:
1143 c->value = btv->opt_adc_crush;
1145 case V4L2_CID_PRIVATE_VCR_HACK:
1146 c->value = btv->opt_vcr_hack;
1148 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1149 c->value = btv->opt_whitecrush_upper;
1151 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1152 c->value = btv->opt_whitecrush_lower;
1160 static int set_control(struct bttv *btv, struct v4l2_control *c)
1162 struct video_audio va;
1165 for (i = 0; i < BTTV_CTLS; i++)
1166 if (bttv_ctls[i].id == c->id)
1170 if (i >= 4 && i <= 8) {
1171 memset(&va,0,sizeof(va));
1172 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1173 if (btv->audio_hook)
1174 btv->audio_hook(btv,&va,0);
1177 case V4L2_CID_BRIGHTNESS:
1178 bt848_bright(btv,c->value);
1181 bt848_hue(btv,c->value);
1183 case V4L2_CID_CONTRAST:
1184 bt848_contrast(btv,c->value);
1186 case V4L2_CID_SATURATION:
1187 bt848_sat(btv,c->value);
1189 case V4L2_CID_AUDIO_MUTE:
1191 va.flags |= VIDEO_AUDIO_MUTE;
1192 audio_mux(btv, AUDIO_MUTE);
1194 va.flags &= ~VIDEO_AUDIO_MUTE;
1195 audio_mux(btv, AUDIO_UNMUTE);
1199 case V4L2_CID_AUDIO_VOLUME:
1200 va.volume = c->value;
1202 case V4L2_CID_AUDIO_BALANCE:
1203 va.balance = c->value;
1205 case V4L2_CID_AUDIO_BASS:
1208 case V4L2_CID_AUDIO_TREBLE:
1209 va.treble = c->value;
1212 case V4L2_CID_PRIVATE_CHROMA_AGC:
1213 btv->opt_chroma_agc = c->value;
1214 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1215 btwrite(val, BT848_E_SCLOOP);
1216 btwrite(val, BT848_O_SCLOOP);
1218 case V4L2_CID_PRIVATE_COMBFILTER:
1219 btv->opt_combfilter = c->value;
1221 case V4L2_CID_PRIVATE_LUMAFILTER:
1222 btv->opt_lumafilter = c->value;
1223 if (btv->opt_lumafilter) {
1224 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1225 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1227 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1228 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1231 case V4L2_CID_PRIVATE_AUTOMUTE:
1232 btv->opt_automute = c->value;
1234 case V4L2_CID_PRIVATE_AGC_CRUSH:
1235 btv->opt_adc_crush = c->value;
1236 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1239 case V4L2_CID_PRIVATE_VCR_HACK:
1240 btv->opt_vcr_hack = c->value;
1242 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1243 btv->opt_whitecrush_upper = c->value;
1244 btwrite(c->value, BT848_WC_UP);
1246 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1247 btv->opt_whitecrush_lower = c->value;
1248 btwrite(c->value, BT848_WC_DOWN);
1253 if (i >= 4 && i <= 8) {
1254 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1255 if (btv->audio_hook)
1256 btv->audio_hook(btv,&va,1);
1261 /* ----------------------------------------------------------------------- */
1263 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1265 unsigned int outbits, data;
1266 outbits = btread(BT848_GPIO_OUT_EN);
1267 data = btread(BT848_GPIO_DATA);
1268 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1269 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1272 static void bttv_field_count(struct bttv *btv)
1280 /* start field counter */
1281 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1283 /* stop field counter */
1284 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1285 btv->field_count = 0;
1289 static const struct bttv_format*
1290 format_by_palette(int palette)
1294 for (i = 0; i < BTTV_FORMATS; i++) {
1295 if (-1 == bttv_formats[i].palette)
1297 if (bttv_formats[i].palette == palette)
1298 return bttv_formats+i;
1303 static const struct bttv_format*
1304 format_by_fourcc(int fourcc)
1308 for (i = 0; i < BTTV_FORMATS; i++) {
1309 if (-1 == bttv_formats[i].fourcc)
1311 if (bttv_formats[i].fourcc == fourcc)
1312 return bttv_formats+i;
1317 /* ----------------------------------------------------------------------- */
1321 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1322 struct bttv_buffer *new)
1324 struct bttv_buffer *old;
1325 unsigned long flags;
1328 dprintk("switch_overlay: enter [new=%p]\n",new);
1330 new->vb.state = STATE_DONE;
1331 spin_lock_irqsave(&btv->s_lock,flags);
1335 bttv_set_dma(btv, 0x03);
1336 spin_unlock_irqrestore(&btv->s_lock,flags);
1338 free_btres(btv,fh,RESOURCE_OVERLAY);
1340 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1341 bttv_dma_free(btv, old);
1344 dprintk("switch_overlay: done\n");
1348 /* ----------------------------------------------------------------------- */
1349 /* video4linux (1) interface */
1351 static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
1352 const struct bttv_format *fmt,
1353 unsigned int width, unsigned int height,
1354 enum v4l2_field field)
1356 int redo_dma_risc = 0;
1359 /* check settings */
1362 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1364 height = RAW_LINES*2;
1365 if (width*height > buf->vb.bsize)
1367 buf->vb.size = buf->vb.bsize;
1371 width > bttv_tvnorms[btv->tvnorm].swidth ||
1372 height > bttv_tvnorms[btv->tvnorm].sheight)
1374 buf->vb.size = (width * height * fmt->depth) >> 3;
1375 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1379 /* alloc + fill struct bttv_buffer (if changed) */
1380 if (buf->vb.width != width || buf->vb.height != height ||
1381 buf->vb.field != field ||
1382 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1383 buf->vb.width = width;
1384 buf->vb.height = height;
1385 buf->vb.field = field;
1386 buf->tvnorm = btv->tvnorm;
1391 /* alloc risc memory */
1392 if (STATE_NEEDS_INIT == buf->vb.state) {
1394 if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
1399 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1402 buf->vb.state = STATE_PREPARED;
1406 bttv_dma_free(btv,buf);
1411 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1413 struct bttv_fh *fh = q->priv_data;
1415 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1418 while (*size * *count > gbuffers * gbufsize)
1424 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1425 enum v4l2_field field)
1427 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1428 struct bttv_fh *fh = q->priv_data;
1430 return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
1431 fh->width, fh->height, field);
1435 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1437 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1438 struct bttv_fh *fh = q->priv_data;
1439 struct bttv *btv = fh->btv;
1441 buf->vb.state = STATE_QUEUED;
1442 list_add_tail(&buf->vb.queue,&btv->capture);
1443 if (!btv->curr.frame_irq) {
1445 bttv_set_dma(btv, 0x03);
1449 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1451 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1452 struct bttv_fh *fh = q->priv_data;
1454 bttv_dma_free(fh->btv,buf);
1457 static struct videobuf_queue_ops bttv_video_qops = {
1458 .buf_setup = buffer_setup,
1459 .buf_prepare = buffer_prepare,
1460 .buf_queue = buffer_queue,
1461 .buf_release = buffer_release,
1464 static const char *v4l1_ioctls[] = {
1465 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
1466 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
1467 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
1468 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
1469 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
1470 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
1472 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1476 return BTTV_VERSION_CODE;
1478 /* *** v4l1 *** ************************************************ */
1481 unsigned long *freq = arg;
1487 unsigned long *freq = arg;
1490 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1491 if (btv->has_matchbox && btv->radio_user)
1492 tea5757_set_freq(btv,*freq);
1499 struct video_tuner *v = arg;
1501 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1503 if (v->tuner) /* Only tuner 0 */
1505 strcpy(v->name, "Television");
1507 v->rangehigh = 0x7FFFFFFF;
1508 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1509 v->mode = btv->tvnorm;
1510 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1511 bttv_call_i2c_clients(btv,cmd,v);
1516 struct video_tuner *v = arg;
1518 if (v->tuner) /* Only tuner 0 */
1520 if (v->mode >= BTTV_TVNORMS)
1524 set_tvnorm(btv,v->mode);
1525 bttv_call_i2c_clients(btv,cmd,v);
1532 struct video_channel *v = arg;
1533 unsigned int channel = v->channel;
1535 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1538 v->flags = VIDEO_VC_AUDIO;
1539 v->type = VIDEO_TYPE_CAMERA;
1540 v->norm = btv->tvnorm;
1541 if (channel == bttv_tvcards[btv->c.type].tuner) {
1542 strcpy(v->name,"Television");
1543 v->flags|=VIDEO_VC_TUNER;
1544 v->type=VIDEO_TYPE_TV;
1546 } else if (channel == btv->svhs) {
1547 strcpy(v->name,"S-Video");
1549 sprintf(v->name,"Composite%d",channel);
1555 struct video_channel *v = arg;
1556 unsigned int channel = v->channel;
1558 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1560 if (v->norm >= BTTV_TVNORMS)
1564 if (channel == btv->input &&
1565 v->norm == btv->tvnorm) {
1571 btv->tvnorm = v->norm;
1572 set_input(btv,v->channel);
1579 struct video_audio *v = arg;
1581 memset(v,0,sizeof(*v));
1582 strcpy(v->name,"Television");
1583 v->flags |= VIDEO_AUDIO_MUTABLE;
1584 v->mode = VIDEO_SOUND_MONO;
1587 bttv_call_i2c_clients(btv,cmd,v);
1589 /* card specific hooks */
1590 if (btv->audio_hook)
1591 btv->audio_hook(btv,v,0);
1598 struct video_audio *v = arg;
1599 unsigned int audio = v->audio;
1601 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1605 audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
1606 bttv_call_i2c_clients(btv,cmd,v);
1608 /* card specific hooks */
1609 if (btv->audio_hook)
1610 btv->audio_hook(btv,v,1);
1616 /* *** v4l2 *** ************************************************ */
1617 case VIDIOC_ENUMSTD:
1619 struct v4l2_standard *e = arg;
1620 unsigned int index = e->index;
1622 if (index >= BTTV_TVNORMS)
1624 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1625 bttv_tvnorms[e->index].name);
1631 v4l2_std_id *id = arg;
1632 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1637 v4l2_std_id *id = arg;
1640 for (i = 0; i < BTTV_TVNORMS; i++)
1641 if (*id & bttv_tvnorms[i].v4l2_id)
1643 if (i == BTTV_TVNORMS)
1648 i2c_vidiocschan(btv);
1652 case VIDIOC_QUERYSTD:
1654 v4l2_std_id *id = arg;
1656 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1657 *id = V4L2_STD_625_50;
1659 *id = V4L2_STD_525_60;
1663 case VIDIOC_ENUMINPUT:
1665 struct v4l2_input *i = arg;
1669 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1671 memset(i,0,sizeof(*i));
1673 i->type = V4L2_INPUT_TYPE_CAMERA;
1675 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1676 sprintf(i->name, "Television");
1677 i->type = V4L2_INPUT_TYPE_TUNER;
1679 } else if (i->index == btv->svhs) {
1680 sprintf(i->name, "S-Video");
1682 sprintf(i->name,"Composite%d",i->index);
1684 if (i->index == btv->input) {
1685 __u32 dstatus = btread(BT848_DSTATUS);
1686 if (0 == (dstatus & BT848_DSTATUS_PRES))
1687 i->status |= V4L2_IN_ST_NO_SIGNAL;
1688 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1689 i->status |= V4L2_IN_ST_NO_H_LOCK;
1691 for (n = 0; n < BTTV_TVNORMS; n++)
1692 i->std |= bttv_tvnorms[n].v4l2_id;
1695 case VIDIOC_G_INPUT:
1701 case VIDIOC_S_INPUT:
1703 unsigned int *i = arg;
1705 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1713 case VIDIOC_G_TUNER:
1715 struct v4l2_tuner *t = arg;
1717 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1722 memset(t,0,sizeof(*t));
1723 strcpy(t->name, "Television");
1724 t->type = V4L2_TUNER_ANALOG_TV;
1725 t->rangehigh = 0xffffffffUL;
1726 t->capability = V4L2_TUNER_CAP_NORM;
1727 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1728 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1732 struct video_audio va;
1733 memset(&va, 0, sizeof(struct video_audio));
1734 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1735 if (btv->audio_hook)
1736 btv->audio_hook(btv,&va,0);
1737 if(va.mode & VIDEO_SOUND_STEREO) {
1738 t->audmode = V4L2_TUNER_MODE_STEREO;
1739 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1741 if(va.mode & VIDEO_SOUND_LANG1) {
1742 t->audmode = V4L2_TUNER_MODE_LANG1;
1743 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1744 | V4L2_TUNER_SUB_LANG2;
1747 /* FIXME: fill capability+audmode */
1751 case VIDIOC_S_TUNER:
1753 struct v4l2_tuner *t = arg;
1755 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1761 struct video_audio va;
1762 memset(&va, 0, sizeof(struct video_audio));
1763 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1764 if (t->audmode == V4L2_TUNER_MODE_MONO)
1765 va.mode = VIDEO_SOUND_MONO;
1766 else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1767 va.mode = VIDEO_SOUND_STEREO;
1768 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1769 va.mode = VIDEO_SOUND_LANG1;
1770 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1771 va.mode = VIDEO_SOUND_LANG2;
1772 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1773 if (btv->audio_hook)
1774 btv->audio_hook(btv,&va,1);
1780 case VIDIOC_G_FREQUENCY:
1782 struct v4l2_frequency *f = arg;
1784 memset(f,0,sizeof(*f));
1785 f->type = V4L2_TUNER_ANALOG_TV;
1786 f->frequency = btv->freq;
1789 case VIDIOC_S_FREQUENCY:
1791 struct v4l2_frequency *f = arg;
1793 if (unlikely(f->tuner != 0))
1795 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1798 btv->freq = f->frequency;
1799 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1800 if (btv->has_matchbox && btv->radio_user)
1801 tea5757_set_freq(btv,btv->freq);
1807 return -ENOIOCTLCMD;
1813 static int verify_window(const struct bttv_tvnorm *tvn,
1814 struct v4l2_window *win, int fixup)
1816 enum v4l2_field field;
1819 if (win->w.width < 48 || win->w.height < 32)
1821 if (win->clipcount > 2048)
1826 maxh = tvn->sheight;
1828 if (V4L2_FIELD_ANY == field) {
1829 field = (win->w.height > maxh/2)
1830 ? V4L2_FIELD_INTERLACED
1834 case V4L2_FIELD_TOP:
1835 case V4L2_FIELD_BOTTOM:
1838 case V4L2_FIELD_INTERLACED:
1844 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1847 if (win->w.width > maxw)
1848 win->w.width = maxw;
1849 if (win->w.height > maxh)
1850 win->w.height = maxh;
1855 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1856 struct v4l2_window *win, int fixup)
1858 struct v4l2_clip *clips = NULL;
1859 int n,size,retval = 0;
1861 if (NULL == fh->ovfmt)
1863 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1865 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1869 /* copy clips -- luckily v4l1 + v4l2 are binary
1870 compatible here ...*/
1872 size = sizeof(*clips)*(n+4);
1873 clips = kmalloc(size,GFP_KERNEL);
1877 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1882 /* clip against screen */
1883 if (NULL != btv->fbuf.base)
1884 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1886 btcx_sort_clips(clips,n);
1888 /* 4-byte alignments */
1889 switch (fh->ovfmt->depth) {
1892 btcx_align(&win->w, clips, n, 3);
1895 btcx_align(&win->w, clips, n, 1);
1898 /* no alignment fixups needed */
1904 down(&fh->cap.lock);
1906 kfree(fh->ov.clips);
1907 fh->ov.clips = clips;
1911 fh->ov.field = win->field;
1912 fh->ov.setup_ok = 1;
1913 btv->init.ov.w.width = win->w.width;
1914 btv->init.ov.w.height = win->w.height;
1915 btv->init.ov.field = win->field;
1917 /* update overlay if needed */
1919 if (check_btres(fh, RESOURCE_OVERLAY)) {
1920 struct bttv_buffer *new;
1922 new = videobuf_alloc(sizeof(*new));
1923 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1924 retval = bttv_switch_overlay(btv,fh,new);
1930 /* ----------------------------------------------------------------------- */
1932 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
1934 struct videobuf_queue* q = NULL;
1937 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1940 case V4L2_BUF_TYPE_VBI_CAPTURE:
1949 static int bttv_resource(struct bttv_fh *fh)
1954 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1955 res = RESOURCE_VIDEO;
1957 case V4L2_BUF_TYPE_VBI_CAPTURE:
1966 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
1968 struct videobuf_queue *q = bttv_queue(fh);
1969 int res = bttv_resource(fh);
1971 if (check_btres(fh,res))
1973 if (videobuf_queue_is_busy(q))
1979 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
1982 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1983 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
1984 f->fmt.pix.width = fh->width;
1985 f->fmt.pix.height = fh->height;
1986 f->fmt.pix.field = fh->cap.field;
1987 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1988 f->fmt.pix.bytesperline =
1989 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1990 f->fmt.pix.sizeimage =
1991 f->fmt.pix.height * f->fmt.pix.bytesperline;
1993 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1994 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
1995 f->fmt.win.w = fh->ov.w;
1996 f->fmt.win.field = fh->ov.field;
1998 case V4L2_BUF_TYPE_VBI_CAPTURE:
1999 bttv_vbi_get_fmt(fh,f);
2006 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2007 struct v4l2_format *f)
2010 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2012 const struct bttv_format *fmt;
2013 enum v4l2_field field;
2014 unsigned int maxw,maxh;
2016 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2021 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2022 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2023 field = f->fmt.pix.field;
2024 if (V4L2_FIELD_ANY == field)
2025 field = (f->fmt.pix.height > maxh/2)
2026 ? V4L2_FIELD_INTERLACED
2027 : V4L2_FIELD_BOTTOM;
2028 if (V4L2_FIELD_SEQ_BT == field)
2029 field = V4L2_FIELD_SEQ_TB;
2031 case V4L2_FIELD_TOP:
2032 case V4L2_FIELD_BOTTOM:
2033 case V4L2_FIELD_ALTERNATE:
2036 case V4L2_FIELD_INTERLACED:
2038 case V4L2_FIELD_SEQ_TB:
2039 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2046 /* update data for the application */
2047 f->fmt.pix.field = field;
2048 if (f->fmt.pix.width < 48)
2049 f->fmt.pix.width = 48;
2050 if (f->fmt.pix.height < 32)
2051 f->fmt.pix.height = 32;
2052 if (f->fmt.pix.width > maxw)
2053 f->fmt.pix.width = maxw;
2054 if (f->fmt.pix.height > maxh)
2055 f->fmt.pix.height = maxh;
2056 f->fmt.pix.width &= ~0x03;
2057 f->fmt.pix.bytesperline =
2058 (f->fmt.pix.width * fmt->depth) >> 3;
2059 f->fmt.pix.sizeimage =
2060 f->fmt.pix.height * f->fmt.pix.bytesperline;
2064 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2065 return verify_window(&bttv_tvnorms[btv->tvnorm],
2067 case V4L2_BUF_TYPE_VBI_CAPTURE:
2068 bttv_vbi_try_fmt(fh,f);
2075 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2076 struct v4l2_format *f)
2081 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2083 const struct bttv_format *fmt;
2085 retval = bttv_switch_type(fh,f->type);
2088 retval = bttv_try_fmt(fh,btv,f);
2091 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2093 /* update our state informations */
2094 down(&fh->cap.lock);
2096 fh->cap.field = f->fmt.pix.field;
2097 fh->cap.last = V4L2_FIELD_NONE;
2098 fh->width = f->fmt.pix.width;
2099 fh->height = f->fmt.pix.height;
2100 btv->init.fmt = fmt;
2101 btv->init.width = f->fmt.pix.width;
2102 btv->init.height = f->fmt.pix.height;
2107 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2108 return setup_window(fh, btv, &f->fmt.win, 1);
2109 case V4L2_BUF_TYPE_VBI_CAPTURE:
2110 retval = bttv_switch_type(fh,f->type);
2113 if (locked_btres(fh->btv, RESOURCE_VBI))
2115 bttv_vbi_try_fmt(fh,f);
2116 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2117 bttv_vbi_get_fmt(fh,f);
2124 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2125 unsigned int cmd, void *arg)
2127 struct bttv_fh *fh = file->private_data;
2128 struct bttv *btv = fh->btv;
2129 unsigned long flags;
2132 if (bttv_debug > 1) {
2133 switch (_IOC_TYPE(cmd)) {
2135 printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
2136 btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
2137 v4l1_ioctls[_IOC_NR(cmd)] : "???");
2140 printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
2141 btv->c.nr, cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
2144 printk("bttv%d: ioctl 0x%x (???)\n",
2149 bttv_reinit_bt848(btv);
2157 case VIDIOC_S_INPUT:
2158 case VIDIOC_S_TUNER:
2159 case VIDIOC_S_FREQUENCY:
2160 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2167 /* *** v4l1 *** ************************************************ */
2170 struct video_capability *cap = arg;
2172 memset(cap,0,sizeof(*cap));
2173 strcpy(cap->name,btv->video_dev->name);
2174 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2176 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2179 cap->type = VID_TYPE_CAPTURE|
2184 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2185 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2187 cap->minheight = 32;
2189 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2190 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2196 struct video_picture *pic = arg;
2198 memset(pic,0,sizeof(*pic));
2199 pic->brightness = btv->bright;
2200 pic->contrast = btv->contrast;
2201 pic->hue = btv->hue;
2202 pic->colour = btv->saturation;
2204 pic->depth = fh->fmt->depth;
2205 pic->palette = fh->fmt->palette;
2211 struct video_picture *pic = arg;
2212 const struct bttv_format *fmt;
2214 fmt = format_by_palette(pic->palette);
2217 down(&fh->cap.lock);
2218 if (fmt->depth != pic->depth) {
2220 goto fh_unlock_and_return;
2224 btv->init.ovfmt = fmt;
2225 btv->init.fmt = fmt;
2227 /* dirty hack time: swap bytes for overlay if the
2228 display adaptor is big endian (insmod option) */
2229 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2230 fmt->palette == VIDEO_PALETTE_RGB565 ||
2231 fmt->palette == VIDEO_PALETTE_RGB32) {
2235 bt848_bright(btv,pic->brightness);
2236 bt848_contrast(btv,pic->contrast);
2237 bt848_hue(btv,pic->hue);
2238 bt848_sat(btv,pic->colour);
2245 struct video_window *win = arg;
2247 memset(win,0,sizeof(*win));
2248 win->x = fh->ov.w.left;
2249 win->y = fh->ov.w.top;
2250 win->width = fh->ov.w.width;
2251 win->height = fh->ov.w.height;
2256 struct video_window *win = arg;
2257 struct v4l2_window w2;
2259 w2.field = V4L2_FIELD_ANY;
2262 w2.w.width = win->width;
2263 w2.w.height = win->height;
2264 w2.clipcount = win->clipcount;
2265 w2.clips = (struct v4l2_clip __user *)win->clips;
2266 retval = setup_window(fh, btv, &w2, 0);
2268 /* on v4l1 this ioctl affects the read() size too */
2269 fh->width = fh->ov.w.width;
2270 fh->height = fh->ov.w.height;
2271 btv->init.width = fh->ov.w.width;
2272 btv->init.height = fh->ov.w.height;
2279 struct video_buffer *fbuf = arg;
2281 fbuf->base = btv->fbuf.base;
2282 fbuf->width = btv->fbuf.fmt.width;
2283 fbuf->height = btv->fbuf.fmt.height;
2284 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2286 fbuf->depth = fh->ovfmt->depth;
2291 struct video_buffer *fbuf = arg;
2292 const struct bttv_format *fmt;
2295 if(!capable(CAP_SYS_ADMIN) &&
2296 !capable(CAP_SYS_RAWIO))
2298 end = (unsigned long)fbuf->base +
2299 fbuf->height * fbuf->bytesperline;
2300 down(&fh->cap.lock);
2303 switch (fbuf->depth) {
2305 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2308 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2311 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2314 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2318 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2325 goto fh_unlock_and_return;
2329 btv->init.ovfmt = fmt;
2330 btv->init.fmt = fmt;
2331 btv->fbuf.base = fbuf->base;
2332 btv->fbuf.fmt.width = fbuf->width;
2333 btv->fbuf.fmt.height = fbuf->height;
2334 if (fbuf->bytesperline)
2335 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2337 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2343 case VIDIOC_OVERLAY:
2345 struct bttv_buffer *new;
2350 if (NULL == btv->fbuf.base)
2352 if (!fh->ov.setup_ok) {
2353 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2358 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2361 down(&fh->cap.lock);
2363 fh->ov.tvnorm = btv->tvnorm;
2364 new = videobuf_alloc(sizeof(*new));
2365 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2371 retval = bttv_switch_overlay(btv,fh,new);
2378 struct video_mbuf *mbuf = arg;
2381 down(&fh->cap.lock);
2382 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2385 goto fh_unlock_and_return;
2386 memset(mbuf,0,sizeof(*mbuf));
2387 mbuf->frames = gbuffers;
2388 mbuf->size = gbuffers * gbufsize;
2389 for (i = 0; i < gbuffers; i++)
2390 mbuf->offsets[i] = i * gbufsize;
2394 case VIDIOCMCAPTURE:
2396 struct video_mmap *vm = arg;
2397 struct bttv_buffer *buf;
2398 enum v4l2_field field;
2400 if (vm->frame >= VIDEO_MAX_FRAME)
2403 down(&fh->cap.lock);
2405 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2407 goto fh_unlock_and_return;
2408 if (0 == buf->vb.baddr)
2409 goto fh_unlock_and_return;
2410 if (buf->vb.state == STATE_QUEUED ||
2411 buf->vb.state == STATE_ACTIVE)
2412 goto fh_unlock_and_return;
2414 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2415 ? V4L2_FIELD_INTERLACED
2416 : V4L2_FIELD_BOTTOM;
2417 retval = bttv_prepare_buffer(btv,buf,
2418 format_by_palette(vm->format),
2419 vm->width,vm->height,field);
2421 goto fh_unlock_and_return;
2422 spin_lock_irqsave(&btv->s_lock,flags);
2423 buffer_queue(&fh->cap,&buf->vb);
2424 spin_unlock_irqrestore(&btv->s_lock,flags);
2431 struct bttv_buffer *buf;
2433 if (*frame >= VIDEO_MAX_FRAME)
2436 down(&fh->cap.lock);
2438 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2440 goto fh_unlock_and_return;
2441 retval = videobuf_waiton(&buf->vb,0,1);
2443 goto fh_unlock_and_return;
2444 switch (buf->vb.state) {
2449 videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
2450 bttv_dma_free(btv,buf);
2462 struct vbi_format *fmt = (void *) arg;
2463 struct v4l2_format fmt2;
2465 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2466 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2470 bttv_vbi_get_fmt(fh, &fmt2);
2472 memset(fmt,0,sizeof(*fmt));
2473 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2474 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2475 fmt->sample_format = VIDEO_PALETTE_RAW;
2476 fmt->start[0] = fmt2.fmt.vbi.start[0];
2477 fmt->count[0] = fmt2.fmt.vbi.count[0];
2478 fmt->start[1] = fmt2.fmt.vbi.start[1];
2479 fmt->count[1] = fmt2.fmt.vbi.count[1];
2480 if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
2481 fmt->flags |= V4L2_VBI_UNSYNC;
2482 if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
2483 fmt->flags |= V4L2_VBI_INTERLACED;
2488 struct vbi_format *fmt = (void *) arg;
2489 struct v4l2_format fmt2;
2491 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2494 bttv_vbi_get_fmt(fh, &fmt2);
2496 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2497 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2498 fmt->sample_format != VIDEO_PALETTE_RAW ||
2499 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2500 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2501 fmt->count[0] != fmt->count[1] ||
2502 fmt->count[0] < 1 ||
2503 fmt->count[0] > 32 /* VBI_MAXLINES */)
2506 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2519 return bttv_common_ioctls(btv,cmd,arg);
2521 /* *** v4l2 *** ************************************************ */
2522 case VIDIOC_QUERYCAP:
2524 struct v4l2_capability *cap = arg;
2528 strcpy(cap->driver,"bttv");
2529 strlcpy(cap->card,btv->video_dev->name,sizeof(cap->card));
2530 sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
2531 cap->version = BTTV_VERSION_CODE;
2533 V4L2_CAP_VIDEO_CAPTURE |
2534 V4L2_CAP_VIDEO_OVERLAY |
2535 V4L2_CAP_VBI_CAPTURE |
2536 V4L2_CAP_READWRITE |
2538 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2539 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2540 cap->capabilities |= V4L2_CAP_TUNER;
2544 case VIDIOC_ENUM_FMT:
2546 struct v4l2_fmtdesc *f = arg;
2547 enum v4l2_buf_type type;
2552 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2557 memset(f,0,sizeof(*f));
2560 f->pixelformat = V4L2_PIX_FMT_GREY;
2561 strcpy(f->description,"vbi data");
2565 /* video capture + overlay */
2567 for (i = 0; i < BTTV_FORMATS; i++) {
2568 if (bttv_formats[i].fourcc != -1)
2570 if ((unsigned int)index == f->index)
2573 if (BTTV_FORMATS == i)
2577 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2579 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2580 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2586 memset(f,0,sizeof(*f));
2589 f->pixelformat = bttv_formats[i].fourcc;
2590 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2594 case VIDIOC_TRY_FMT:
2596 struct v4l2_format *f = arg;
2597 return bttv_try_fmt(fh,btv,f);
2601 struct v4l2_format *f = arg;
2602 return bttv_g_fmt(fh,f);
2606 struct v4l2_format *f = arg;
2607 return bttv_s_fmt(fh,btv,f);
2612 struct v4l2_framebuffer *fb = arg;
2615 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2617 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2622 struct v4l2_framebuffer *fb = arg;
2623 const struct bttv_format *fmt;
2625 if(!capable(CAP_SYS_ADMIN) &&
2626 !capable(CAP_SYS_RAWIO))
2630 fmt = format_by_fourcc(fb->fmt.pixelformat);
2633 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2636 down(&fh->cap.lock);
2638 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2639 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2640 goto fh_unlock_and_return;
2641 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2642 goto fh_unlock_and_return;
2646 btv->fbuf.base = fb->base;
2647 btv->fbuf.fmt.width = fb->fmt.width;
2648 btv->fbuf.fmt.height = fb->fmt.height;
2649 if (0 != fb->fmt.bytesperline)
2650 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2652 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2656 btv->init.ovfmt = fmt;
2657 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2660 fh->ov.w.width = fb->fmt.width;
2661 fh->ov.w.height = fb->fmt.height;
2662 btv->init.ov.w.width = fb->fmt.width;
2663 btv->init.ov.w.height = fb->fmt.height;
2665 kfree(fh->ov.clips);
2666 fh->ov.clips = NULL;
2669 if (check_btres(fh, RESOURCE_OVERLAY)) {
2670 struct bttv_buffer *new;
2672 new = videobuf_alloc(sizeof(*new));
2673 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2674 retval = bttv_switch_overlay(btv,fh,new);
2681 case VIDIOC_REQBUFS:
2682 return videobuf_reqbufs(bttv_queue(fh),arg);
2684 case VIDIOC_QUERYBUF:
2685 return videobuf_querybuf(bttv_queue(fh),arg);
2688 return videobuf_qbuf(bttv_queue(fh),arg);
2691 return videobuf_dqbuf(bttv_queue(fh),arg,
2692 file->f_flags & O_NONBLOCK);
2694 case VIDIOC_STREAMON:
2696 int res = bttv_resource(fh);
2698 if (!check_alloc_btres(btv,fh,res))
2700 return videobuf_streamon(bttv_queue(fh));
2702 case VIDIOC_STREAMOFF:
2704 int res = bttv_resource(fh);
2706 retval = videobuf_streamoff(bttv_queue(fh));
2709 free_btres(btv,fh,res);
2713 case VIDIOC_QUERYCTRL:
2715 struct v4l2_queryctrl *c = arg;
2718 if ((c->id < V4L2_CID_BASE ||
2719 c->id >= V4L2_CID_LASTP1) &&
2720 (c->id < V4L2_CID_PRIVATE_BASE ||
2721 c->id >= V4L2_CID_PRIVATE_LASTP1))
2723 for (i = 0; i < BTTV_CTLS; i++)
2724 if (bttv_ctls[i].id == c->id)
2726 if (i == BTTV_CTLS) {
2731 if (i >= 4 && i <= 8) {
2732 struct video_audio va;
2733 memset(&va,0,sizeof(va));
2734 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2735 if (btv->audio_hook)
2736 btv->audio_hook(btv,&va,0);
2737 switch (bttv_ctls[i].id) {
2738 case V4L2_CID_AUDIO_VOLUME:
2739 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2742 case V4L2_CID_AUDIO_BALANCE:
2743 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2746 case V4L2_CID_AUDIO_BASS:
2747 if (!(va.flags & VIDEO_AUDIO_BASS))
2750 case V4L2_CID_AUDIO_TREBLE:
2751 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2759 return get_control(btv,arg);
2761 return set_control(btv,arg);
2764 struct v4l2_streamparm *parm = arg;
2765 struct v4l2_standard s;
2766 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2768 memset(parm,0,sizeof(*parm));
2769 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2770 bttv_tvnorms[btv->tvnorm].name);
2771 parm->parm.capture.timeperframe = s.frameperiod;
2775 case VIDIOC_G_PRIORITY:
2777 enum v4l2_priority *p = arg;
2779 *p = v4l2_prio_max(&btv->prio);
2782 case VIDIOC_S_PRIORITY:
2784 enum v4l2_priority *prio = arg;
2786 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2789 case VIDIOC_ENUMSTD:
2792 case VIDIOC_ENUMINPUT:
2793 case VIDIOC_G_INPUT:
2794 case VIDIOC_S_INPUT:
2795 case VIDIOC_G_TUNER:
2796 case VIDIOC_S_TUNER:
2797 case VIDIOC_G_FREQUENCY:
2798 case VIDIOC_S_FREQUENCY:
2799 return bttv_common_ioctls(btv,cmd,arg);
2802 return -ENOIOCTLCMD;
2806 fh_unlock_and_return:
2811 static int bttv_ioctl(struct inode *inode, struct file *file,
2812 unsigned int cmd, unsigned long arg)
2814 struct bttv_fh *fh = file->private_data;
2818 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2819 return fh->lines * 2 * 2048;
2821 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2825 static ssize_t bttv_read(struct file *file, char __user *data,
2826 size_t count, loff_t *ppos)
2828 struct bttv_fh *fh = file->private_data;
2831 if (fh->btv->errors)
2832 bttv_reinit_bt848(fh->btv);
2833 dprintk("bttv%d: read count=%d type=%s\n",
2834 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2837 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2838 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2840 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2841 file->f_flags & O_NONBLOCK);
2843 case V4L2_BUF_TYPE_VBI_CAPTURE:
2844 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2846 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2847 file->f_flags & O_NONBLOCK);
2855 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2857 struct bttv_fh *fh = file->private_data;
2858 struct bttv_buffer *buf;
2859 enum v4l2_field field;
2861 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2862 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2864 return videobuf_poll_stream(file, &fh->vbi, wait);
2867 if (check_btres(fh,RESOURCE_VIDEO)) {
2868 /* streaming capture */
2869 if (list_empty(&fh->cap.stream))
2871 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2873 /* read() capture */
2874 down(&fh->cap.lock);
2875 if (NULL == fh->cap.read_buf) {
2876 /* need to capture a new frame */
2877 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2881 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2882 if (NULL == fh->cap.read_buf) {
2886 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2887 field = videobuf_next_field(&fh->cap);
2888 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2892 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2893 fh->cap.read_off = 0;
2896 buf = (struct bttv_buffer*)fh->cap.read_buf;
2899 poll_wait(file, &buf->vb.done, wait);
2900 if (buf->vb.state == STATE_DONE ||
2901 buf->vb.state == STATE_ERROR)
2902 return POLLIN|POLLRDNORM;
2906 static int bttv_open(struct inode *inode, struct file *file)
2908 int minor = iminor(inode);
2909 struct bttv *btv = NULL;
2911 enum v4l2_buf_type type = 0;
2914 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
2916 for (i = 0; i < bttv_num; i++) {
2917 if (bttvs[i].video_dev &&
2918 bttvs[i].video_dev->minor == minor) {
2920 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2923 if (bttvs[i].vbi_dev &&
2924 bttvs[i].vbi_dev->minor == minor) {
2926 type = V4L2_BUF_TYPE_VBI_CAPTURE;
2933 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
2934 btv->c.nr,v4l2_type_names[type]);
2936 /* allocate per filehandle data */
2937 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
2940 file->private_data = fh;
2943 fh->ov.setup_ok = 0;
2944 v4l2_prio_open(&btv->prio,&fh->prio);
2946 videobuf_queue_init(&fh->cap, &bttv_video_qops,
2947 btv->c.pci, &btv->s_lock,
2948 V4L2_BUF_TYPE_VIDEO_CAPTURE,
2949 V4L2_FIELD_INTERLACED,
2950 sizeof(struct bttv_buffer),
2952 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
2953 btv->c.pci, &btv->s_lock,
2954 V4L2_BUF_TYPE_VBI_CAPTURE,
2956 sizeof(struct bttv_buffer),
2958 i2c_vidiocschan(btv);
2961 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
2962 bttv_vbi_setlines(fh,btv,16);
2963 bttv_field_count(btv);
2967 static int bttv_release(struct inode *inode, struct file *file)
2969 struct bttv_fh *fh = file->private_data;
2970 struct bttv *btv = fh->btv;
2972 /* turn off overlay */
2973 if (check_btres(fh, RESOURCE_OVERLAY))
2974 bttv_switch_overlay(btv,fh,NULL);
2976 /* stop video capture */
2977 if (check_btres(fh, RESOURCE_VIDEO)) {
2978 videobuf_streamoff(&fh->cap);
2979 free_btres(btv,fh,RESOURCE_VIDEO);
2981 if (fh->cap.read_buf) {
2982 buffer_release(&fh->cap,fh->cap.read_buf);
2983 kfree(fh->cap.read_buf);
2986 /* stop vbi capture */
2987 if (check_btres(fh, RESOURCE_VBI)) {
2988 if (fh->vbi.streaming)
2989 videobuf_streamoff(&fh->vbi);
2990 if (fh->vbi.reading)
2991 videobuf_read_stop(&fh->vbi);
2992 free_btres(btv,fh,RESOURCE_VBI);
2996 videobuf_mmap_free(&fh->cap);
2997 videobuf_mmap_free(&fh->vbi);
2998 v4l2_prio_close(&btv->prio,&fh->prio);
2999 file->private_data = NULL;
3003 bttv_field_count(btv);
3008 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3010 struct bttv_fh *fh = file->private_data;
3012 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3013 fh->btv->c.nr, v4l2_type_names[fh->type],
3014 vma->vm_start, vma->vm_end - vma->vm_start);
3015 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3018 static struct file_operations bttv_fops =
3020 .owner = THIS_MODULE,
3022 .release = bttv_release,
3023 .ioctl = bttv_ioctl,
3024 .llseek = no_llseek,
3030 static struct video_device bttv_video_template =
3033 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
3034 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3035 .hardware = VID_HARDWARE_BT848,
3040 static struct video_device bttv_vbi_template =
3042 .name = "bt848/878 vbi",
3043 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3044 .hardware = VID_HARDWARE_BT848,
3049 /* ----------------------------------------------------------------------- */
3050 /* radio interface */
3052 static int radio_open(struct inode *inode, struct file *file)
3054 int minor = iminor(inode);
3055 struct bttv *btv = NULL;
3058 dprintk("bttv: open minor=%d\n",minor);
3060 for (i = 0; i < bttv_num; i++) {
3061 if (bttvs[i].radio_dev->minor == minor) {
3069 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3071 if (btv->radio_user) {
3076 file->private_data = btv;
3078 i2c_vidiocschan(btv);
3079 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
3080 audio_mux(btv,AUDIO_RADIO);
3086 static int radio_release(struct inode *inode, struct file *file)
3088 struct bttv *btv = file->private_data;
3094 static int radio_do_ioctl(struct inode *inode, struct file *file,
3095 unsigned int cmd, void *arg)
3097 struct bttv *btv = file->private_data;
3102 struct video_capability *cap = arg;
3104 memset(cap,0,sizeof(*cap));
3105 strcpy(cap->name,btv->radio_dev->name);
3106 cap->type = VID_TYPE_TUNER;
3114 struct video_tuner *v = arg;
3118 memset(v,0,sizeof(*v));
3119 strcpy(v->name, "Radio");
3120 /* japan: 76.0 MHz - 89.9 MHz
3121 western europe: 87.5 MHz - 108.0 MHz
3122 russia: 65.0 MHz - 108.0 MHz */
3123 v->rangelow=(int)(65*16);
3124 v->rangehigh=(int)(108*16);
3125 bttv_call_i2c_clients(btv,cmd,v);
3137 return bttv_common_ioctls(btv,cmd,arg);
3140 return -ENOIOCTLCMD;
3145 static int radio_ioctl(struct inode *inode, struct file *file,
3146 unsigned int cmd, unsigned long arg)
3148 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3151 static struct file_operations radio_fops =
3153 .owner = THIS_MODULE,
3155 .release = radio_release,
3156 .ioctl = radio_ioctl,
3157 .llseek = no_llseek,
3160 static struct video_device radio_template =
3162 .name = "bt848/878 radio",
3163 .type = VID_TYPE_TUNER,
3164 .hardware = VID_HARDWARE_BT848,
3165 .fops = &radio_fops,
3169 /* ----------------------------------------------------------------------- */
3172 static char *irq_name[] = {
3173 "FMTCHG", // format change detected (525 vs. 625)
3174 "VSYNC", // vertical sync (new field)
3175 "HSYNC", // horizontal sync
3176 "OFLOW", // chroma/luma AGC overflow
3177 "HLOCK", // horizontal lock changed
3178 "VPRES", // video presence changed
3180 "I2CDONE", // hw irc operation finished
3181 "GPINT", // gpio port triggered irq
3183 "RISCI", // risc instruction triggered irq
3184 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3185 "FTRGT", // pixel data fifo overrun
3186 "FDSR", // fifo data stream resyncronisation
3187 "PPERR", // parity error (data transfer)
3188 "RIPERR", // parity error (read risc instructions)
3189 "PABORT", // pci abort
3190 "OCERR", // risc instruction error
3191 "SCERR", // syncronisation error
3194 static void bttv_print_irqbits(u32 print, u32 mark)
3199 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3200 if (print & (1 << i))
3201 printk(" %s",irq_name[i]);
3202 if (mark & (1 << i))
3207 static void bttv_print_riscaddr(struct bttv *btv)
3209 printk(" main: %08Lx\n",
3210 (unsigned long long)btv->main.dma);
3211 printk(" vbi : o=%08Lx e=%08Lx\n",
3212 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3213 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3214 printk(" cap : o=%08Lx e=%08Lx\n",
3215 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3216 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3217 printk(" scr : o=%08Lx e=%08Lx\n",
3218 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3219 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3222 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3224 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3226 (unsigned long)btv->main.dma,
3227 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3228 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3231 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3232 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3233 "Ok, then this is harmless, don't worry ;)\n",
3237 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3239 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3245 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3247 struct bttv_buffer *item;
3249 memset(set,0,sizeof(*set));
3251 /* capture request ? */
3252 if (!list_empty(&btv->capture)) {
3254 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3255 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3257 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3260 /* capture request for other field ? */
3261 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3262 (item->vb.queue.next != &btv->capture)) {
3263 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3264 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3265 if (NULL == set->top &&
3266 V4L2_FIELD_TOP == item->vb.field) {
3269 if (NULL == set->bottom &&
3270 V4L2_FIELD_BOTTOM == item->vb.field) {
3273 if (NULL != set->top && NULL != set->bottom)
3279 /* screen overlay ? */
3280 if (NULL != btv->screen) {
3281 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3282 if (NULL == set->top && NULL == set->bottom) {
3283 set->top = btv->screen;
3284 set->bottom = btv->screen;
3287 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3289 set->top = btv->screen;
3291 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3292 NULL == set->bottom) {
3293 set->bottom = btv->screen;
3298 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3299 btv->c.nr,set->top, set->bottom,
3300 btv->screen,set->frame_irq,set->top_irq);
3305 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3306 struct bttv_buffer_set *curr, unsigned int state)
3310 do_gettimeofday(&ts);
3312 if (wakeup->top == wakeup->bottom) {
3313 if (NULL != wakeup->top && curr->top != wakeup->top) {
3315 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3316 wakeup->top->vb.ts = ts;
3317 wakeup->top->vb.field_count = btv->field_count;
3318 wakeup->top->vb.state = state;
3319 wake_up(&wakeup->top->vb.done);
3322 if (NULL != wakeup->top && curr->top != wakeup->top) {
3324 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3325 wakeup->top->vb.ts = ts;
3326 wakeup->top->vb.field_count = btv->field_count;
3327 wakeup->top->vb.state = state;
3328 wake_up(&wakeup->top->vb.done);
3330 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3332 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3333 wakeup->bottom->vb.ts = ts;
3334 wakeup->bottom->vb.field_count = btv->field_count;
3335 wakeup->bottom->vb.state = state;
3336 wake_up(&wakeup->bottom->vb.done);
3342 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3350 do_gettimeofday(&ts);
3352 wakeup->vb.field_count = btv->field_count;
3353 wakeup->vb.state = state;
3354 wake_up(&wakeup->vb.done);
3357 static void bttv_irq_timeout(unsigned long data)
3359 struct bttv *btv = (struct bttv *)data;
3360 struct bttv_buffer_set old,new;
3361 struct bttv_buffer *ovbi;
3362 struct bttv_buffer *item;
3363 unsigned long flags;
3366 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3367 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3368 btread(BT848_RISC_COUNT));
3369 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3373 spin_lock_irqsave(&btv->s_lock,flags);
3375 /* deactivate stuff */
3376 memset(&new,0,sizeof(new));
3382 bttv_buffer_activate_video(btv, &new);
3383 bttv_buffer_activate_vbi(btv, NULL);
3384 bttv_set_dma(btv, 0);
3387 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3388 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3390 /* cancel all outstanding capture / vbi requests */
3391 while (!list_empty(&btv->capture)) {
3392 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3393 list_del(&item->vb.queue);
3394 item->vb.state = STATE_ERROR;
3395 wake_up(&item->vb.done);
3397 while (!list_empty(&btv->vcapture)) {
3398 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3399 list_del(&item->vb.queue);
3400 item->vb.state = STATE_ERROR;
3401 wake_up(&item->vb.done);
3405 spin_unlock_irqrestore(&btv->s_lock,flags);
3409 bttv_irq_wakeup_top(struct bttv *btv)
3411 struct bttv_buffer *wakeup = btv->curr.top;
3416 spin_lock(&btv->s_lock);
3417 btv->curr.top_irq = 0;
3418 btv->curr.top = NULL;
3419 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3421 do_gettimeofday(&wakeup->vb.ts);
3422 wakeup->vb.field_count = btv->field_count;
3423 wakeup->vb.state = STATE_DONE;
3424 wake_up(&wakeup->vb.done);
3425 spin_unlock(&btv->s_lock);
3428 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3432 if (rc > risc->dma + risc->size)
3438 bttv_irq_switch_video(struct bttv *btv)
3440 struct bttv_buffer_set new;
3441 struct bttv_buffer_set old;
3444 spin_lock(&btv->s_lock);
3446 /* new buffer set */
3447 bttv_irq_next_video(btv, &new);
3448 rc = btread(BT848_RISC_COUNT);
3449 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3450 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3453 bttv_irq_debug_low_latency(btv, rc);
3454 spin_unlock(&btv->s_lock);
3461 btv->loop_irq &= ~1;
3462 bttv_buffer_activate_video(btv, &new);
3463 bttv_set_dma(btv, 0);
3466 if (UNSET != btv->new_input) {
3467 video_mux(btv,btv->new_input);
3468 btv->new_input = UNSET;
3471 /* wake up finished buffers */
3472 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3473 spin_unlock(&btv->s_lock);
3477 bttv_irq_switch_vbi(struct bttv *btv)
3479 struct bttv_buffer *new = NULL;
3480 struct bttv_buffer *old;
3483 spin_lock(&btv->s_lock);
3485 if (!list_empty(&btv->vcapture))
3486 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3489 rc = btread(BT848_RISC_COUNT);
3490 if (NULL != old && (is_active(&old->top, rc) ||
3491 is_active(&old->bottom, rc))) {
3494 bttv_irq_debug_low_latency(btv, rc);
3495 spin_unlock(&btv->s_lock);
3501 btv->loop_irq &= ~4;
3502 bttv_buffer_activate_vbi(btv, new);
3503 bttv_set_dma(btv, 0);
3505 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3506 spin_unlock(&btv->s_lock);
3509 static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3517 btv=(struct bttv *)dev_id;
3520 /* get/clear interrupt status bits */
3521 stat=btread(BT848_INT_STAT);
3522 astat=stat&btread(BT848_INT_MASK);
3526 btwrite(stat,BT848_INT_STAT);
3528 /* get device status bits */
3529 dstat=btread(BT848_DSTATUS);
3532 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3533 "riscs=%x, riscc=%08x, ",
3534 btv->c.nr, count, btv->field_count,
3535 stat>>28, btread(BT848_RISC_COUNT));
3536 bttv_print_irqbits(stat,astat);
3537 if (stat & BT848_INT_HLOCK)
3538 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3540 if (stat & BT848_INT_VPRES)
3541 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3543 if (stat & BT848_INT_FMTCHG)
3544 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3549 if (astat&BT848_INT_VSYNC)
3552 if (astat & BT848_INT_GPINT) {
3553 wake_up(&btv->gpioq);
3554 bttv_gpio_irq(&btv->c);
3557 if (astat & BT848_INT_I2CDONE) {
3558 btv->i2c_done = stat;
3559 wake_up(&btv->i2c_queue);
3562 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3563 bttv_irq_switch_vbi(btv);
3565 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3566 bttv_irq_wakeup_top(btv);
3568 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3569 bttv_irq_switch_video(btv);
3571 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3574 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3575 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3576 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3577 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3578 btread(BT848_RISC_COUNT));
3579 bttv_print_irqbits(stat,astat);
3582 bttv_print_riscaddr(btv);
3584 if (fdsr && astat & BT848_INT_FDSR) {
3585 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3586 btv->c.nr,btread(BT848_RISC_COUNT));
3588 bttv_print_riscaddr(btv);
3593 btwrite(0, BT848_INT_MASK);
3595 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3596 bttv_print_irqbits(stat,astat);
3603 return IRQ_RETVAL(handled);
3607 /* ----------------------------------------------------------------------- */
3608 /* initialitation */
3610 static struct video_device *vdev_init(struct bttv *btv,
3611 struct video_device *template,
3614 struct video_device *vfd;
3616 vfd = video_device_alloc();
3621 vfd->dev = &btv->c.pci->dev;
3622 vfd->release = video_device_release;
3623 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3624 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3625 type, bttv_tvcards[btv->c.type].name);
3629 static void bttv_unregister_video(struct bttv *btv)
3631 if (btv->video_dev) {
3632 if (-1 != btv->video_dev->minor)
3633 video_unregister_device(btv->video_dev);
3635 video_device_release(btv->video_dev);
3636 btv->video_dev = NULL;
3639 if (-1 != btv->vbi_dev->minor)
3640 video_unregister_device(btv->vbi_dev);
3642 video_device_release(btv->vbi_dev);
3643 btv->vbi_dev = NULL;
3645 if (btv->radio_dev) {
3646 if (-1 != btv->radio_dev->minor)
3647 video_unregister_device(btv->radio_dev);
3649 video_device_release(btv->radio_dev);
3650 btv->radio_dev = NULL;
3654 /* register video4linux devices */
3655 static int __devinit bttv_register_video(struct bttv *btv)
3658 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3659 if (NULL == btv->video_dev)
3661 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3663 printk(KERN_INFO "bttv%d: registered device video%d\n",
3664 btv->c.nr,btv->video_dev->minor & 0x1f);
3665 video_device_create_file(btv->video_dev, &class_device_attr_card);
3668 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3669 if (NULL == btv->vbi_dev)
3671 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3673 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3674 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3676 if (!btv->has_radio)
3679 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3680 if (NULL == btv->radio_dev)
3682 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3684 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3685 btv->c.nr,btv->radio_dev->minor & 0x1f);
3691 bttv_unregister_video(btv);
3696 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3697 /* response on cards with no firmware is not enabled by OF */
3698 static void pci_set_command(struct pci_dev *dev)
3700 #if defined(__powerpc__)
3703 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3704 cmd = (cmd | PCI_COMMAND_MEMORY );
3705 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3709 static int __devinit bttv_probe(struct pci_dev *dev,
3710 const struct pci_device_id *pci_id)
3716 if (bttv_num == BTTV_MAX)
3718 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3719 btv=&bttvs[bttv_num];
3720 memset(btv,0,sizeof(*btv));
3721 btv->c.nr = bttv_num;
3722 sprintf(btv->c.name,"bttv%d",btv->c.nr);
3724 /* initialize structs / fill in defaults */
3725 init_MUTEX(&btv->lock);
3726 init_MUTEX(&btv->reslock);
3727 spin_lock_init(&btv->s_lock);
3728 spin_lock_init(&btv->gpio_lock);
3729 init_waitqueue_head(&btv->gpioq);
3730 init_waitqueue_head(&btv->i2c_queue);
3731 INIT_LIST_HEAD(&btv->c.subs);
3732 INIT_LIST_HEAD(&btv->capture);
3733 INIT_LIST_HEAD(&btv->vcapture);
3734 v4l2_prio_init(&btv->prio);
3736 init_timer(&btv->timeout);
3737 btv->timeout.function = bttv_irq_timeout;
3738 btv->timeout.data = (unsigned long)btv;
3741 btv->tuner_type = UNSET;
3742 btv->pinnacle_id = UNSET;
3743 btv->new_input = UNSET;
3745 btv->has_radio=radio[btv->c.nr];
3747 /* pci stuff (init, get irq/mmio, ... */
3749 btv->id = dev->device;
3750 if (pci_enable_device(dev)) {
3751 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3755 if (pci_set_dma_mask(dev, 0xffffffff)) {
3756 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3760 if (!request_mem_region(pci_resource_start(dev,0),
3761 pci_resource_len(dev,0),
3763 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3764 btv->c.nr, pci_resource_start(dev,0));
3767 pci_set_master(dev);
3768 pci_set_command(dev);
3769 pci_set_drvdata(dev,btv);
3770 if (!pci_dma_supported(dev,0xffffffff)) {
3771 printk("bttv%d: Oops: no 32bit PCI DMA ???\n", btv->c.nr);
3776 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3777 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3778 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3779 bttv_num,btv->id, btv->revision, pci_name(dev));
3780 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3781 btv->c.pci->irq, lat, pci_resource_start(dev,0));
3784 btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3785 if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3786 printk("bttv%d: ioremap() failed\n", btv->c.nr);
3794 /* disable irqs, register irq handler */
3795 btwrite(0, BT848_INT_MASK);
3796 result = request_irq(btv->c.pci->irq, bttv_irq,
3797 SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
3799 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
3800 bttv_num,btv->c.pci->irq);
3804 if (0 != bttv_handle_chipset(btv)) {
3809 /* init options from insmod args */
3810 btv->opt_combfilter = combfilter;
3811 btv->opt_lumafilter = lumafilter;
3812 btv->opt_automute = automute;
3813 btv->opt_chroma_agc = chroma_agc;
3814 btv->opt_adc_crush = adc_crush;
3815 btv->opt_vcr_hack = vcr_hack;
3816 btv->opt_whitecrush_upper = whitecrush_upper;
3817 btv->opt_whitecrush_lower = whitecrush_lower;
3819 /* fill struct bttv with some useful defaults */
3820 btv->init.btv = btv;
3821 btv->init.ov.w.width = 320;
3822 btv->init.ov.w.height = 240;
3823 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
3824 btv->init.width = 320;
3825 btv->init.height = 240;
3826 btv->init.lines = 16;
3829 /* initialize hardware */
3831 bttv_gpio_tracking(btv,"pre-init");
3833 bttv_risc_init_main(btv);
3837 btwrite(0x00, BT848_GPIO_REG_INP);
3838 btwrite(0x00, BT848_GPIO_OUT_EN);
3840 bttv_gpio_tracking(btv,"init");
3842 /* needs to be done before i2c is registered */
3843 bttv_init_card1(btv);
3845 /* register i2c + gpio */
3848 /* some card-specific stuff (needs working i2c) */
3849 bttv_init_card2(btv);
3852 /* register video4linux + input */
3853 if (!bttv_tvcards[btv->c.type].no_video) {
3854 bttv_register_video(btv);
3855 bt848_bright(btv,32768);
3856 bt848_contrast(btv,32768);
3857 bt848_hue(btv,32768);
3858 bt848_sat(btv,32768);
3859 audio_mux(btv,AUDIO_MUTE);
3863 /* add subdevices */
3864 if (btv->has_remote)
3865 bttv_sub_add_device(&btv->c, "remote");
3866 if (bttv_tvcards[btv->c.type].has_dvb)
3867 bttv_sub_add_device(&btv->c, "dvb");
3869 /* everything is fine */
3874 free_irq(btv->c.pci->irq,btv);
3877 if (btv->bt848_mmio)
3878 iounmap(btv->bt848_mmio);
3879 release_mem_region(pci_resource_start(btv->c.pci,0),
3880 pci_resource_len(btv->c.pci,0));
3881 pci_set_drvdata(dev,NULL);
3885 static void __devexit bttv_remove(struct pci_dev *pci_dev)
3887 struct bttv *btv = pci_get_drvdata(pci_dev);
3890 printk("bttv%d: unloading\n",btv->c.nr);
3892 /* shutdown everything (DMA+IRQs) */
3893 btand(~15, BT848_GPIO_DMA_CTL);
3894 btwrite(0, BT848_INT_MASK);
3895 btwrite(~0x0, BT848_INT_STAT);
3896 btwrite(0x0, BT848_GPIO_OUT_EN);
3898 bttv_gpio_tracking(btv,"cleanup");
3900 /* tell gpio modules we are leaving ... */
3902 wake_up(&btv->gpioq);
3903 bttv_sub_del_devices(&btv->c);
3905 /* unregister i2c_bus + input */
3908 /* unregister video4linux */
3909 bttv_unregister_video(btv);
3911 /* free allocated memory */
3912 btcx_riscmem_free(btv->c.pci,&btv->main);
3914 /* free ressources */
3915 free_irq(btv->c.pci->irq,btv);
3916 iounmap(btv->bt848_mmio);
3917 release_mem_region(pci_resource_start(btv->c.pci,0),
3918 pci_resource_len(btv->c.pci,0));
3920 pci_set_drvdata(pci_dev, NULL);
3924 static int bttv_suspend(struct pci_dev *pci_dev, u32 state)
3926 struct bttv *btv = pci_get_drvdata(pci_dev);
3927 struct bttv_buffer_set idle;
3928 unsigned long flags;
3930 dprintk("bttv%d: suspend %d\n", btv->c.nr, state);
3932 /* stop dma + irqs */
3933 spin_lock_irqsave(&btv->s_lock,flags);
3934 memset(&idle, 0, sizeof(idle));
3935 btv->state.video = btv->curr;
3936 btv->state.vbi = btv->cvbi;
3937 btv->state.loop_irq = btv->loop_irq;
3940 bttv_buffer_activate_video(btv, &idle);
3941 bttv_buffer_activate_vbi(btv, NULL);
3942 bttv_set_dma(btv, 0);
3943 btwrite(0, BT848_INT_MASK);
3944 spin_unlock_irqrestore(&btv->s_lock,flags);
3946 /* save bt878 state */
3947 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
3948 btv->state.gpio_data = gpio_read();
3950 /* save pci state */
3951 pci_save_state(pci_dev);
3952 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
3953 pci_disable_device(pci_dev);
3954 btv->state.disabled = 1;
3959 static int bttv_resume(struct pci_dev *pci_dev)
3961 struct bttv *btv = pci_get_drvdata(pci_dev);
3962 unsigned long flags;
3964 dprintk("bttv%d: resume\n", btv->c.nr);
3966 /* restore pci state */
3967 if (btv->state.disabled) {
3968 pci_enable_device(pci_dev);
3969 btv->state.disabled = 0;
3971 pci_set_power_state(pci_dev, PCI_D0);
3972 pci_restore_state(pci_dev);
3974 /* restore bt878 state */
3975 bttv_reinit_bt848(btv);
3976 gpio_inout(0xffffff, btv->state.gpio_enable);
3977 gpio_write(btv->state.gpio_data);
3980 spin_lock_irqsave(&btv->s_lock,flags);
3981 btv->curr = btv->state.video;
3982 btv->cvbi = btv->state.vbi;
3983 btv->loop_irq = btv->state.loop_irq;
3984 bttv_buffer_activate_video(btv, &btv->curr);
3985 bttv_buffer_activate_vbi(btv, btv->cvbi);
3986 bttv_set_dma(btv, 0);
3987 spin_unlock_irqrestore(&btv->s_lock,flags);
3991 static struct pci_device_id bttv_pci_tbl[] = {
3992 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
3993 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3994 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
3995 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3996 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
3997 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3998 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
3999 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4003 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4005 static struct pci_driver bttv_pci_driver = {
4007 .id_table = bttv_pci_tbl,
4008 .probe = bttv_probe,
4009 .remove = __devexit_p(bttv_remove),
4010 .suspend = bttv_suspend,
4011 .resume = bttv_resume,
4014 static int bttv_init_module(void)
4018 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4019 (BTTV_VERSION_CODE >> 16) & 0xff,
4020 (BTTV_VERSION_CODE >> 8) & 0xff,
4021 BTTV_VERSION_CODE & 0xff);
4023 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4024 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4026 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4028 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4029 gbufsize = BTTV_MAX_FBUF;
4030 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4032 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4033 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4035 bttv_check_chipset();
4037 bus_register(&bttv_sub_bus_type);
4038 return pci_module_init(&bttv_pci_driver);
4041 static void bttv_cleanup_module(void)
4043 pci_unregister_driver(&bttv_pci_driver);
4044 bus_unregister(&bttv_sub_bus_type);
4048 module_init(bttv_init_module);
4049 module_exit(bttv_cleanup_module);