2 $Id: bttv-driver.c,v 1.27 2004/11/07 14:44:59 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/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>
39 #include <asm/byteorder.h>
43 unsigned int bttv_num; /* number of Bt848s in use */
44 struct bttv bttvs[BTTV_MAX];
46 unsigned int bttv_debug = 0;
47 unsigned int bttv_verbose = 1;
48 unsigned int bttv_gpio = 0;
50 /* config variables */
52 static unsigned int bigendian=1;
54 static unsigned int bigendian=0;
56 static unsigned int radio[BTTV_MAX];
57 static unsigned int irq_debug = 0;
58 static unsigned int gbuffers = 8;
59 static unsigned int gbufsize = 0x208000;
61 static int video_nr = -1;
62 static int radio_nr = -1;
63 static int vbi_nr = -1;
64 static int debug_latency = 0;
66 static unsigned int fdsr = 0;
69 static unsigned int combfilter = 0;
70 static unsigned int lumafilter = 0;
71 static unsigned int automute = 1;
72 static unsigned int chroma_agc = 0;
73 static unsigned int adc_crush = 1;
74 static unsigned int whitecrush_upper = 0xCF;
75 static unsigned int whitecrush_lower = 0x7F;
76 static unsigned int vcr_hack = 0;
77 static unsigned int irq_iswitch = 0;
79 /* API features (turn on/off stuff for testing) */
80 static unsigned int v4l2 = 1;
84 module_param(bttv_verbose, int, 0644);
85 module_param(bttv_gpio, int, 0644);
86 module_param(bttv_debug, int, 0644);
87 module_param(irq_debug, int, 0644);
88 module_param(debug_latency, int, 0644);
90 module_param(fdsr, int, 0444);
91 module_param(video_nr, int, 0444);
92 module_param(radio_nr, int, 0444);
93 module_param(vbi_nr, int, 0444);
94 module_param(gbuffers, int, 0444);
95 module_param(gbufsize, int, 0444);
97 module_param(v4l2, int, 0644);
98 module_param(bigendian, int, 0644);
99 module_param(irq_iswitch, int, 0644);
100 module_param(combfilter, int, 0444);
101 module_param(lumafilter, int, 0444);
102 module_param(automute, int, 0444);
103 module_param(chroma_agc, int, 0444);
104 module_param(adc_crush, int, 0444);
105 module_param(whitecrush_upper, int, 0444);
106 module_param(whitecrush_lower, int, 0444);
107 module_param(vcr_hack, int, 0444);
108 module_param_array(radio, int, NULL, 0444);
110 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
111 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
112 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
113 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
114 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
115 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
116 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
117 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
118 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
119 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
120 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
121 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
122 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
123 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
124 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
126 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
127 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
128 MODULE_LICENSE("GPL");
130 /* ----------------------------------------------------------------------- */
133 static ssize_t show_card(struct class_device *cd, char *buf)
135 struct video_device *vfd = to_video_device(cd);
136 struct bttv *btv = dev_get_drvdata(vfd->dev);
137 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
139 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
141 /* ----------------------------------------------------------------------- */
144 /* special timing tables from conexant... */
145 static u8 SRAM_Table[][60] =
147 /* PAL digital input over GPIO[7:0] */
149 45, // 45 bytes following
150 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
151 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
152 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
153 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
154 0x37,0x00,0xAF,0x21,0x00
156 /* NTSC digital input over GPIO[7:0] */
158 51, // 51 bytes following
159 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
160 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
161 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
162 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
163 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
166 // TGB_NTSC392 // quartzsight
167 // This table has been modified to be used for Fusion Rev D
169 0x2A, // size of table = 42
170 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
171 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
172 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
173 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
178 const struct bttv_tvnorm bttv_tvnorms[] = {
180 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
181 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
183 .v4l2_id = V4L2_STD_PAL,
191 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
192 .scaledtwidth = 1135,
199 .v4l2_id = V4L2_STD_NTSC_M,
207 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
215 .v4l2_id = V4L2_STD_SECAM,
223 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
224 .scaledtwidth = 1135,
229 .sram = 0, /* like PAL, correct? */
231 .v4l2_id = V4L2_STD_PAL_Nc,
239 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
247 .v4l2_id = V4L2_STD_PAL_M,
255 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
263 .v4l2_id = V4L2_STD_PAL_N,
271 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
279 .v4l2_id = V4L2_STD_NTSC_M_JP,
287 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
295 /* that one hopefully works with the strange timing
296 * which video recorders produce when playing a NTSC
297 * tape on a PAL TV ... */
298 .v4l2_id = V4L2_STD_PAL_60,
306 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
307 .scaledtwidth = 1135,
316 const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
318 /* ----------------------------------------------------------------------- */
320 packed pixel formats must come first */
321 const struct bttv_format bttv_formats[] = {
323 .name = "8 bpp, gray",
324 .palette = VIDEO_PALETTE_GREY,
325 .fourcc = V4L2_PIX_FMT_GREY,
326 .btformat = BT848_COLOR_FMT_Y8,
328 .flags = FORMAT_FLAGS_PACKED,
330 .name = "8 bpp, dithered color",
331 .palette = VIDEO_PALETTE_HI240,
332 .fourcc = V4L2_PIX_FMT_HI240,
333 .btformat = BT848_COLOR_FMT_RGB8,
335 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
337 .name = "15 bpp RGB, le",
338 .palette = VIDEO_PALETTE_RGB555,
339 .fourcc = V4L2_PIX_FMT_RGB555,
340 .btformat = BT848_COLOR_FMT_RGB15,
342 .flags = FORMAT_FLAGS_PACKED,
344 .name = "15 bpp RGB, be",
346 .fourcc = V4L2_PIX_FMT_RGB555X,
347 .btformat = BT848_COLOR_FMT_RGB15,
348 .btswap = 0x03, /* byteswap */
350 .flags = FORMAT_FLAGS_PACKED,
352 .name = "16 bpp RGB, le",
353 .palette = VIDEO_PALETTE_RGB565,
354 .fourcc = V4L2_PIX_FMT_RGB565,
355 .btformat = BT848_COLOR_FMT_RGB16,
357 .flags = FORMAT_FLAGS_PACKED,
359 .name = "16 bpp RGB, be",
361 .fourcc = V4L2_PIX_FMT_RGB565X,
362 .btformat = BT848_COLOR_FMT_RGB16,
363 .btswap = 0x03, /* byteswap */
365 .flags = FORMAT_FLAGS_PACKED,
367 .name = "24 bpp RGB, le",
368 .palette = VIDEO_PALETTE_RGB24,
369 .fourcc = V4L2_PIX_FMT_BGR24,
370 .btformat = BT848_COLOR_FMT_RGB24,
372 .flags = FORMAT_FLAGS_PACKED,
374 .name = "32 bpp RGB, le",
375 .palette = VIDEO_PALETTE_RGB32,
376 .fourcc = V4L2_PIX_FMT_BGR32,
377 .btformat = BT848_COLOR_FMT_RGB32,
379 .flags = FORMAT_FLAGS_PACKED,
381 .name = "32 bpp RGB, be",
383 .fourcc = V4L2_PIX_FMT_RGB32,
384 .btformat = BT848_COLOR_FMT_RGB32,
385 .btswap = 0x0f, /* byte+word swap */
387 .flags = FORMAT_FLAGS_PACKED,
389 .name = "4:2:2, packed, YUYV",
390 .palette = VIDEO_PALETTE_YUV422,
391 .fourcc = V4L2_PIX_FMT_YUYV,
392 .btformat = BT848_COLOR_FMT_YUY2,
394 .flags = FORMAT_FLAGS_PACKED,
396 .name = "4:2:2, packed, YUYV",
397 .palette = VIDEO_PALETTE_YUYV,
398 .fourcc = V4L2_PIX_FMT_YUYV,
399 .btformat = BT848_COLOR_FMT_YUY2,
401 .flags = FORMAT_FLAGS_PACKED,
403 .name = "4:2:2, packed, UYVY",
404 .palette = VIDEO_PALETTE_UYVY,
405 .fourcc = V4L2_PIX_FMT_UYVY,
406 .btformat = BT848_COLOR_FMT_YUY2,
407 .btswap = 0x03, /* byteswap */
409 .flags = FORMAT_FLAGS_PACKED,
411 .name = "4:2:2, planar, Y-Cb-Cr",
412 .palette = VIDEO_PALETTE_YUV422P,
413 .fourcc = V4L2_PIX_FMT_YUV422P,
414 .btformat = BT848_COLOR_FMT_YCrCb422,
416 .flags = FORMAT_FLAGS_PLANAR,
420 .name = "4:2:0, planar, Y-Cb-Cr",
421 .palette = VIDEO_PALETTE_YUV420P,
422 .fourcc = V4L2_PIX_FMT_YUV420,
423 .btformat = BT848_COLOR_FMT_YCrCb422,
425 .flags = FORMAT_FLAGS_PLANAR,
429 .name = "4:2:0, planar, Y-Cr-Cb",
431 .fourcc = V4L2_PIX_FMT_YVU420,
432 .btformat = BT848_COLOR_FMT_YCrCb422,
434 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
438 .name = "4:1:1, planar, Y-Cb-Cr",
439 .palette = VIDEO_PALETTE_YUV411P,
440 .fourcc = V4L2_PIX_FMT_YUV411P,
441 .btformat = BT848_COLOR_FMT_YCrCb411,
443 .flags = FORMAT_FLAGS_PLANAR,
447 .name = "4:1:0, planar, Y-Cb-Cr",
448 .palette = VIDEO_PALETTE_YUV410P,
449 .fourcc = V4L2_PIX_FMT_YUV410,
450 .btformat = BT848_COLOR_FMT_YCrCb411,
452 .flags = FORMAT_FLAGS_PLANAR,
456 .name = "4:1:0, planar, Y-Cr-Cb",
458 .fourcc = V4L2_PIX_FMT_YVU410,
459 .btformat = BT848_COLOR_FMT_YCrCb411,
461 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
465 .name = "raw scanlines",
466 .palette = VIDEO_PALETTE_RAW,
468 .btformat = BT848_COLOR_FMT_RAW,
470 .flags = FORMAT_FLAGS_RAW,
473 const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
475 /* ----------------------------------------------------------------------- */
477 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
478 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
479 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
480 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
481 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
482 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
483 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
484 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
485 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 8)
487 static const struct v4l2_queryctrl no_ctl = {
489 .flags = V4L2_CTRL_FLAG_DISABLED,
491 static const struct v4l2_queryctrl bttv_ctls[] = {
494 .id = V4L2_CID_BRIGHTNESS,
495 .name = "Brightness",
499 .default_value = 32768,
500 .type = V4L2_CTRL_TYPE_INTEGER,
502 .id = V4L2_CID_CONTRAST,
507 .default_value = 32768,
508 .type = V4L2_CTRL_TYPE_INTEGER,
510 .id = V4L2_CID_SATURATION,
511 .name = "Saturation",
515 .default_value = 32768,
516 .type = V4L2_CTRL_TYPE_INTEGER,
523 .default_value = 32768,
524 .type = V4L2_CTRL_TYPE_INTEGER,
528 .id = V4L2_CID_AUDIO_MUTE,
532 .type = V4L2_CTRL_TYPE_BOOLEAN,
534 .id = V4L2_CID_AUDIO_VOLUME,
539 .default_value = 65535,
540 .type = V4L2_CTRL_TYPE_INTEGER,
542 .id = V4L2_CID_AUDIO_BALANCE,
547 .default_value = 32768,
548 .type = V4L2_CTRL_TYPE_INTEGER,
550 .id = V4L2_CID_AUDIO_BASS,
555 .default_value = 32768,
556 .type = V4L2_CTRL_TYPE_INTEGER,
558 .id = V4L2_CID_AUDIO_TREBLE,
563 .default_value = 32768,
564 .type = V4L2_CTRL_TYPE_INTEGER,
566 /* --- private --- */
568 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
569 .name = "chroma agc",
572 .type = V4L2_CTRL_TYPE_BOOLEAN,
574 .id = V4L2_CID_PRIVATE_COMBFILTER,
575 .name = "combfilter",
578 .type = V4L2_CTRL_TYPE_BOOLEAN,
580 .id = V4L2_CID_PRIVATE_AUTOMUTE,
584 .type = V4L2_CTRL_TYPE_BOOLEAN,
586 .id = V4L2_CID_PRIVATE_LUMAFILTER,
587 .name = "luma decimation filter",
590 .type = V4L2_CTRL_TYPE_BOOLEAN,
592 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
596 .type = V4L2_CTRL_TYPE_BOOLEAN,
598 .id = V4L2_CID_PRIVATE_VCR_HACK,
602 .type = V4L2_CTRL_TYPE_BOOLEAN,
604 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
605 .name = "whitecrush upper",
609 .default_value = 0xCF,
610 .type = V4L2_CTRL_TYPE_INTEGER,
612 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
613 .name = "whitecrush lower",
617 .default_value = 0x7F,
618 .type = V4L2_CTRL_TYPE_INTEGER,
622 const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
624 /* ----------------------------------------------------------------------- */
625 /* resource management */
628 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
630 if (fh->resources & bit)
631 /* have it already allocated */
636 if (btv->resources & bit) {
637 /* no, someone else uses it */
641 /* it's free, grab it */
642 fh->resources |= bit;
643 btv->resources |= bit;
649 int check_btres(struct bttv_fh *fh, int bit)
651 return (fh->resources & bit);
655 int locked_btres(struct bttv *btv, int bit)
657 return (btv->resources & bit);
661 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
664 if ((fh->resources & bits) != bits) {
665 /* trying to free ressources not allocated by us ... */
666 printk("bttv: BUG! (btres)\n");
670 fh->resources &= ~bits;
671 btv->resources &= ~bits;
675 /* ----------------------------------------------------------------------- */
676 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
678 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
679 PLL_X = Reference pre-divider (0=1, 1=2)
680 PLL_C = Post divider (0=6, 1=4)
681 PLL_I = Integer input
682 PLL_F = Fractional input
684 F_input = 28.636363 MHz:
685 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
688 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
690 unsigned char fl, fh, fi;
692 /* prevent overflows */
705 btwrite(fl, BT848_PLL_F_LO);
706 btwrite(fh, BT848_PLL_F_HI);
707 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
710 static void set_pll(struct bttv *btv)
714 if (!btv->pll.pll_crystal)
717 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
718 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
722 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
724 if (btv->pll.pll_current == 0)
726 vprintk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
727 btv->c.nr,btv->pll.pll_ifreq);
728 btwrite(0x00,BT848_TGCTRL);
729 btwrite(0x00,BT848_PLL_XCI);
730 btv->pll.pll_current = 0;
734 vprintk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
735 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
736 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
738 for (i=0; i<10; i++) {
739 /* Let other people run while the PLL stabilizes */
743 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
744 btwrite(0,BT848_DSTATUS);
746 btwrite(0x08,BT848_TGCTRL);
747 btv->pll.pll_current = btv->pll.pll_ofreq;
752 btv->pll.pll_current = -1;
757 /* used to switch between the bt848's analog/digital video capture modes */
758 void bt848A_set_timing(struct bttv *btv)
761 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
762 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
764 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
765 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
766 btv->c.nr,table_idx);
768 /* timing change...reset timing generator address */
769 btwrite(0x00, BT848_TGCTRL);
770 btwrite(0x02, BT848_TGCTRL);
771 btwrite(0x00, BT848_TGCTRL);
773 len=SRAM_Table[table_idx][0];
774 for(i = 1; i <= len; i++)
775 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
776 btv->pll.pll_ofreq = 27000000;
779 btwrite(0x11, BT848_TGCTRL);
780 btwrite(0x41, BT848_DVSIF);
782 btv->pll.pll_ofreq = fsc;
784 btwrite(0x0, BT848_DVSIF);
788 /* ----------------------------------------------------------------------- */
790 static void bt848_bright(struct bttv *btv, int bright)
794 // printk("bttv: set bright: %d\n",bright); // DEBUG
795 btv->bright = bright;
797 /* We want -128 to 127 we get 0-65535 */
798 value = (bright >> 8) - 128;
799 btwrite(value & 0xff, BT848_BRIGHT);
802 static void bt848_hue(struct bttv *btv, int hue)
809 value = (hue >> 8) - 128;
810 btwrite(value & 0xff, BT848_HUE);
813 static void bt848_contrast(struct bttv *btv, int cont)
817 btv->contrast = cont;
821 hibit = (value >> 6) & 4;
822 btwrite(value & 0xff, BT848_CONTRAST_LO);
823 btaor(hibit, ~4, BT848_E_CONTROL);
824 btaor(hibit, ~4, BT848_O_CONTROL);
827 static void bt848_sat(struct bttv *btv, int color)
829 int val_u,val_v,hibits;
831 btv->saturation = color;
833 /* 0-511 for the color */
835 val_v = ((color>>7)*180L)/254;
836 hibits = (val_u >> 7) & 2;
837 hibits |= (val_v >> 8) & 1;
838 btwrite(val_u & 0xff, BT848_SAT_U_LO);
839 btwrite(val_v & 0xff, BT848_SAT_V_LO);
840 btaor(hibits, ~3, BT848_E_CONTROL);
841 btaor(hibits, ~3, BT848_O_CONTROL);
844 /* ----------------------------------------------------------------------- */
847 video_mux(struct bttv *btv, unsigned int input)
851 if (input >= bttv_tvcards[btv->c.type].video_inputs)
854 /* needed by RemoteVideo MX */
855 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
857 gpio_inout(mask2,mask2);
859 if (input == btv->svhs) {
860 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
861 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
863 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
864 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
866 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
867 btaor(mux<<5, ~(3<<5), BT848_IFORM);
868 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
869 btv->c.nr,input,mux);
871 /* card specific hook */
872 if(bttv_tvcards[btv->c.type].muxsel_hook)
873 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
877 static char *audio_modes[] = {
878 "audio: tuner", "audio: radio", "audio: extern",
879 "audio: intern", "audio: off"
883 audio_mux(struct bttv *btv, int mode)
885 int val,mux,i2c_mux,signal;
887 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
888 bttv_tvcards[btv->c.type].gpiomask);
889 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
893 btv->audio |= AUDIO_MUTE;
896 btv->audio &= ~AUDIO_MUTE;
902 btv->audio &= AUDIO_MUTE;
905 i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
906 if (btv->opt_automute && !signal && !btv->radio_user)
909 printk("bttv%d: amux: mode=%d audio=%d signal=%s mux=%d/%d irq=%s\n",
910 btv->c.nr, mode, btv->audio, signal ? "yes" : "no",
911 mux, i2c_mux, in_interrupt() ? "yes" : "no");
914 val = bttv_tvcards[btv->c.type].audiomux[mux];
915 gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
917 bttv_gpio_tracking(btv,audio_modes[mux]);
919 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
924 i2c_vidiocschan(struct bttv *btv)
926 struct video_channel c;
928 memset(&c,0,sizeof(c));
929 c.norm = btv->tvnorm;
930 c.channel = btv->input;
931 bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
932 if (btv->c.type == BTTV_VOODOOTV_FM)
933 bttv_tda9880_setnorm(btv,c.norm);
937 set_tvnorm(struct bttv *btv, unsigned int norm)
939 const struct bttv_tvnorm *tvnorm;
941 if (norm < 0 || norm >= BTTV_TVNORMS)
945 tvnorm = &bttv_tvnorms[norm];
947 btwrite(tvnorm->adelay, BT848_ADELAY);
948 btwrite(tvnorm->bdelay, BT848_BDELAY);
949 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
951 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
952 btwrite(1, BT848_VBI_PACK_DEL);
953 bt848A_set_timing(btv);
955 switch (btv->c.type) {
956 case BTTV_VOODOOTV_FM:
957 bttv_tda9880_setnorm(btv,norm);
961 osprey_540_set_norm(btv,norm);
969 set_input(struct bttv *btv, unsigned int input)
975 spin_lock_irqsave(&btv->s_lock,flags);
976 if (btv->curr.frame_irq) {
977 /* active capture -> delayed input switch */
978 btv->new_input = input;
980 video_mux(btv,input);
982 spin_unlock_irqrestore(&btv->s_lock,flags);
984 video_mux(btv,input);
986 audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
987 AUDIO_TUNER : AUDIO_EXTERN));
988 set_tvnorm(btv,btv->tvnorm);
989 i2c_vidiocschan(btv);
992 static void init_irqreg(struct bttv *btv)
995 btwrite(0xfffffUL, BT848_INT_STAT);
997 if (bttv_tvcards[btv->c.type].no_video) {
999 btwrite(BT848_INT_I2CDONE,
1003 btwrite((btv->triton1) |
1004 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1006 (fdsr ? BT848_INT_FDSR : 0) |
1007 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1008 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1014 static void init_bt848(struct bttv *btv)
1018 if (bttv_tvcards[btv->c.type].no_video) {
1019 /* very basic init only */
1024 btwrite(0x00, BT848_CAP_CTL);
1025 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1026 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1028 /* set planar and packed mode trigger points and */
1029 /* set rising edge of inverted GPINTR pin as irq trigger */
1030 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1031 BT848_GPIO_DMA_CTL_PLTP1_16|
1032 BT848_GPIO_DMA_CTL_PLTP23_16|
1033 BT848_GPIO_DMA_CTL_GPINTC|
1034 BT848_GPIO_DMA_CTL_GPINTI,
1035 BT848_GPIO_DMA_CTL);
1037 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1038 btwrite(val, BT848_E_SCLOOP);
1039 btwrite(val, BT848_O_SCLOOP);
1041 btwrite(0x20, BT848_E_VSCALE_HI);
1042 btwrite(0x20, BT848_O_VSCALE_HI);
1043 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1046 btwrite(whitecrush_upper, BT848_WC_UP);
1047 btwrite(whitecrush_lower, BT848_WC_DOWN);
1049 if (btv->opt_lumafilter) {
1050 btwrite(0, BT848_E_CONTROL);
1051 btwrite(0, BT848_O_CONTROL);
1053 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1054 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1057 bt848_bright(btv, btv->bright);
1058 bt848_hue(btv, btv->hue);
1059 bt848_contrast(btv, btv->contrast);
1060 bt848_sat(btv, btv->saturation);
1066 void bttv_reinit_bt848(struct bttv *btv)
1068 unsigned long flags;
1071 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1072 spin_lock_irqsave(&btv->s_lock,flags);
1074 bttv_set_dma(btv,0);
1075 spin_unlock_irqrestore(&btv->s_lock,flags);
1078 btv->pll.pll_current = -1;
1079 set_input(btv,btv->input);
1082 static int get_control(struct bttv *btv, struct v4l2_control *c)
1084 struct video_audio va;
1087 for (i = 0; i < BTTV_CTLS; i++)
1088 if (bttv_ctls[i].id == c->id)
1092 if (i >= 4 && i <= 8) {
1093 memset(&va,0,sizeof(va));
1094 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1095 if (btv->audio_hook)
1096 btv->audio_hook(btv,&va,0);
1099 case V4L2_CID_BRIGHTNESS:
1100 c->value = btv->bright;
1103 c->value = btv->hue;
1105 case V4L2_CID_CONTRAST:
1106 c->value = btv->contrast;
1108 case V4L2_CID_SATURATION:
1109 c->value = btv->saturation;
1112 case V4L2_CID_AUDIO_MUTE:
1113 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1115 case V4L2_CID_AUDIO_VOLUME:
1116 c->value = va.volume;
1118 case V4L2_CID_AUDIO_BALANCE:
1119 c->value = va.balance;
1121 case V4L2_CID_AUDIO_BASS:
1124 case V4L2_CID_AUDIO_TREBLE:
1125 c->value = va.treble;
1128 case V4L2_CID_PRIVATE_CHROMA_AGC:
1129 c->value = btv->opt_chroma_agc;
1131 case V4L2_CID_PRIVATE_COMBFILTER:
1132 c->value = btv->opt_combfilter;
1134 case V4L2_CID_PRIVATE_LUMAFILTER:
1135 c->value = btv->opt_lumafilter;
1137 case V4L2_CID_PRIVATE_AUTOMUTE:
1138 c->value = btv->opt_automute;
1140 case V4L2_CID_PRIVATE_AGC_CRUSH:
1141 c->value = btv->opt_adc_crush;
1143 case V4L2_CID_PRIVATE_VCR_HACK:
1144 c->value = btv->opt_vcr_hack;
1146 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1147 c->value = btv->opt_whitecrush_upper;
1149 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1150 c->value = btv->opt_whitecrush_lower;
1158 static int set_control(struct bttv *btv, struct v4l2_control *c)
1160 struct video_audio va;
1163 for (i = 0; i < BTTV_CTLS; i++)
1164 if (bttv_ctls[i].id == c->id)
1168 if (i >= 4 && i <= 8) {
1169 memset(&va,0,sizeof(va));
1170 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1171 if (btv->audio_hook)
1172 btv->audio_hook(btv,&va,0);
1175 case V4L2_CID_BRIGHTNESS:
1176 bt848_bright(btv,c->value);
1179 bt848_hue(btv,c->value);
1181 case V4L2_CID_CONTRAST:
1182 bt848_contrast(btv,c->value);
1184 case V4L2_CID_SATURATION:
1185 bt848_sat(btv,c->value);
1187 case V4L2_CID_AUDIO_MUTE:
1189 va.flags |= VIDEO_AUDIO_MUTE;
1190 audio_mux(btv, AUDIO_MUTE);
1192 va.flags &= ~VIDEO_AUDIO_MUTE;
1193 audio_mux(btv, AUDIO_UNMUTE);
1197 case V4L2_CID_AUDIO_VOLUME:
1198 va.volume = c->value;
1200 case V4L2_CID_AUDIO_BALANCE:
1201 va.balance = c->value;
1203 case V4L2_CID_AUDIO_BASS:
1206 case V4L2_CID_AUDIO_TREBLE:
1207 va.treble = c->value;
1210 case V4L2_CID_PRIVATE_CHROMA_AGC:
1211 btv->opt_chroma_agc = c->value;
1212 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1213 btwrite(val, BT848_E_SCLOOP);
1214 btwrite(val, BT848_O_SCLOOP);
1216 case V4L2_CID_PRIVATE_COMBFILTER:
1217 btv->opt_combfilter = c->value;
1219 case V4L2_CID_PRIVATE_LUMAFILTER:
1220 btv->opt_lumafilter = c->value;
1221 if (btv->opt_lumafilter) {
1222 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1223 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1225 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1226 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1229 case V4L2_CID_PRIVATE_AUTOMUTE:
1230 btv->opt_automute = c->value;
1232 case V4L2_CID_PRIVATE_AGC_CRUSH:
1233 btv->opt_adc_crush = c->value;
1234 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1237 case V4L2_CID_PRIVATE_VCR_HACK:
1238 btv->opt_vcr_hack = c->value;
1240 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1241 btv->opt_whitecrush_upper = c->value;
1242 btwrite(c->value, BT848_WC_UP);
1244 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1245 btv->opt_whitecrush_lower = c->value;
1246 btwrite(c->value, BT848_WC_DOWN);
1251 if (i >= 4 && i <= 8) {
1252 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1253 if (btv->audio_hook)
1254 btv->audio_hook(btv,&va,1);
1259 /* ----------------------------------------------------------------------- */
1261 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1263 unsigned int outbits, data;
1264 outbits = btread(BT848_GPIO_OUT_EN);
1265 data = btread(BT848_GPIO_DATA);
1266 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1267 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1270 void bttv_field_count(struct bttv *btv)
1278 /* start field counter */
1279 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1281 /* stop field counter */
1282 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1283 btv->field_count = 0;
1287 static const struct bttv_format*
1288 format_by_palette(int palette)
1292 for (i = 0; i < BTTV_FORMATS; i++) {
1293 if (-1 == bttv_formats[i].palette)
1295 if (bttv_formats[i].palette == palette)
1296 return bttv_formats+i;
1301 static const struct bttv_format*
1302 format_by_fourcc(int fourcc)
1306 for (i = 0; i < BTTV_FORMATS; i++) {
1307 if (-1 == bttv_formats[i].fourcc)
1309 if (bttv_formats[i].fourcc == fourcc)
1310 return bttv_formats+i;
1315 /* ----------------------------------------------------------------------- */
1319 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1320 struct bttv_buffer *new)
1322 struct bttv_buffer *old;
1323 unsigned long flags;
1326 dprintk("switch_overlay: enter [new=%p]\n",new);
1328 new->vb.state = STATE_DONE;
1329 spin_lock_irqsave(&btv->s_lock,flags);
1333 bttv_set_dma(btv, 0x03);
1334 spin_unlock_irqrestore(&btv->s_lock,flags);
1336 free_btres(btv,fh,RESOURCE_OVERLAY);
1338 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1339 bttv_dma_free(btv, old);
1342 dprintk("switch_overlay: done\n");
1346 /* ----------------------------------------------------------------------- */
1347 /* video4linux (1) interface */
1349 static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
1350 const struct bttv_format *fmt,
1351 unsigned int width, unsigned int height,
1352 enum v4l2_field field)
1354 int redo_dma_risc = 0;
1357 /* check settings */
1360 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1362 height = RAW_LINES*2;
1363 if (width*height > buf->vb.bsize)
1365 buf->vb.size = buf->vb.bsize;
1369 width > bttv_tvnorms[btv->tvnorm].swidth ||
1370 height > bttv_tvnorms[btv->tvnorm].sheight)
1372 buf->vb.size = (width * height * fmt->depth) >> 3;
1373 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1377 /* alloc + fill struct bttv_buffer (if changed) */
1378 if (buf->vb.width != width || buf->vb.height != height ||
1379 buf->vb.field != field ||
1380 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1381 buf->vb.width = width;
1382 buf->vb.height = height;
1383 buf->vb.field = field;
1384 buf->tvnorm = btv->tvnorm;
1389 /* alloc risc memory */
1390 if (STATE_NEEDS_INIT == buf->vb.state) {
1392 if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
1397 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1400 buf->vb.state = STATE_PREPARED;
1404 bttv_dma_free(btv,buf);
1409 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1411 struct bttv_fh *fh = q->priv_data;
1413 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1416 while (*size * *count > gbuffers * gbufsize)
1422 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1423 enum v4l2_field field)
1425 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1426 struct bttv_fh *fh = q->priv_data;
1428 return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
1429 fh->width, fh->height, field);
1433 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1435 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1436 struct bttv_fh *fh = q->priv_data;
1437 struct bttv *btv = fh->btv;
1439 buf->vb.state = STATE_QUEUED;
1440 list_add_tail(&buf->vb.queue,&btv->capture);
1441 if (!btv->curr.frame_irq) {
1443 bttv_set_dma(btv, 0x03);
1447 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1449 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1450 struct bttv_fh *fh = q->priv_data;
1452 bttv_dma_free(fh->btv,buf);
1455 static struct videobuf_queue_ops bttv_video_qops = {
1456 .buf_setup = buffer_setup,
1457 .buf_prepare = buffer_prepare,
1458 .buf_queue = buffer_queue,
1459 .buf_release = buffer_release,
1462 static const char *v4l1_ioctls[] = {
1463 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
1464 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
1465 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
1466 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
1467 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
1468 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
1470 int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1474 return BTTV_VERSION_CODE;
1476 /* *** v4l1 *** ************************************************ */
1479 unsigned long *freq = arg;
1485 unsigned long *freq = arg;
1488 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1489 if (btv->has_matchbox && btv->radio_user)
1490 tea5757_set_freq(btv,*freq);
1497 struct video_tuner *v = arg;
1499 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1501 if (v->tuner) /* Only tuner 0 */
1503 strcpy(v->name, "Television");
1505 v->rangehigh = 0x7FFFFFFF;
1506 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1507 v->mode = btv->tvnorm;
1508 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1509 bttv_call_i2c_clients(btv,cmd,v);
1514 struct video_tuner *v = arg;
1516 if (v->tuner) /* Only tuner 0 */
1518 if (v->mode >= BTTV_TVNORMS)
1522 set_tvnorm(btv,v->mode);
1523 bttv_call_i2c_clients(btv,cmd,v);
1530 struct video_channel *v = arg;
1531 unsigned int channel = v->channel;
1533 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1536 v->flags = VIDEO_VC_AUDIO;
1537 v->type = VIDEO_TYPE_CAMERA;
1538 v->norm = btv->tvnorm;
1539 if (channel == bttv_tvcards[btv->c.type].tuner) {
1540 strcpy(v->name,"Television");
1541 v->flags|=VIDEO_VC_TUNER;
1542 v->type=VIDEO_TYPE_TV;
1544 } else if (channel == btv->svhs) {
1545 strcpy(v->name,"S-Video");
1547 sprintf(v->name,"Composite%d",channel);
1553 struct video_channel *v = arg;
1554 unsigned int channel = v->channel;
1556 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1558 if (v->norm >= BTTV_TVNORMS)
1562 if (channel == btv->input &&
1563 v->norm == btv->tvnorm) {
1569 btv->tvnorm = v->norm;
1570 set_input(btv,v->channel);
1577 struct video_audio *v = arg;
1579 memset(v,0,sizeof(*v));
1580 strcpy(v->name,"Television");
1581 v->flags |= VIDEO_AUDIO_MUTABLE;
1582 v->mode = VIDEO_SOUND_MONO;
1585 bttv_call_i2c_clients(btv,cmd,v);
1587 /* card specific hooks */
1588 if (btv->audio_hook)
1589 btv->audio_hook(btv,v,0);
1596 struct video_audio *v = arg;
1597 unsigned int audio = v->audio;
1599 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1603 audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
1604 bttv_call_i2c_clients(btv,cmd,v);
1606 /* card specific hooks */
1607 if (btv->audio_hook)
1608 btv->audio_hook(btv,v,1);
1614 /* *** v4l2 *** ************************************************ */
1615 case VIDIOC_ENUMSTD:
1617 struct v4l2_standard *e = arg;
1618 unsigned int index = e->index;
1620 if (index >= BTTV_TVNORMS)
1622 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1623 bttv_tvnorms[e->index].name);
1629 v4l2_std_id *id = arg;
1630 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1635 v4l2_std_id *id = arg;
1638 for (i = 0; i < BTTV_TVNORMS; i++)
1639 if (*id & bttv_tvnorms[i].v4l2_id)
1641 if (i == BTTV_TVNORMS)
1646 i2c_vidiocschan(btv);
1650 case VIDIOC_QUERYSTD:
1652 v4l2_std_id *id = arg;
1654 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1655 *id = V4L2_STD_625_50;
1657 *id = V4L2_STD_525_60;
1661 case VIDIOC_ENUMINPUT:
1663 struct v4l2_input *i = arg;
1667 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1669 memset(i,0,sizeof(*i));
1671 i->type = V4L2_INPUT_TYPE_CAMERA;
1673 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1674 sprintf(i->name, "Television");
1675 i->type = V4L2_INPUT_TYPE_TUNER;
1677 } else if (i->index == btv->svhs) {
1678 sprintf(i->name, "S-Video");
1680 sprintf(i->name,"Composite%d",i->index);
1682 if (i->index == btv->input) {
1683 __u32 dstatus = btread(BT848_DSTATUS);
1684 if (0 == (dstatus & BT848_DSTATUS_PRES))
1685 i->status |= V4L2_IN_ST_NO_SIGNAL;
1686 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1687 i->status |= V4L2_IN_ST_NO_H_LOCK;
1689 for (n = 0; n < BTTV_TVNORMS; n++)
1690 i->std |= bttv_tvnorms[n].v4l2_id;
1693 case VIDIOC_G_INPUT:
1699 case VIDIOC_S_INPUT:
1701 unsigned int *i = arg;
1703 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1711 case VIDIOC_G_TUNER:
1713 struct v4l2_tuner *t = arg;
1715 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1720 memset(t,0,sizeof(*t));
1721 strcpy(t->name, "Television");
1722 t->type = V4L2_TUNER_ANALOG_TV;
1723 t->rangehigh = 0xffffffffUL;
1724 t->capability = V4L2_TUNER_CAP_NORM;
1725 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1726 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1730 struct video_audio va;
1731 memset(&va, 0, sizeof(struct video_audio));
1732 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1733 if (btv->audio_hook)
1734 btv->audio_hook(btv,&va,0);
1735 if(va.mode & VIDEO_SOUND_STEREO) {
1736 t->audmode = V4L2_TUNER_MODE_STEREO;
1737 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1739 if(va.mode & VIDEO_SOUND_LANG1) {
1740 t->audmode = V4L2_TUNER_MODE_LANG1;
1741 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1742 | V4L2_TUNER_SUB_LANG2;
1745 /* FIXME: fill capability+audmode */
1749 case VIDIOC_S_TUNER:
1751 struct v4l2_tuner *t = arg;
1753 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1759 struct video_audio va;
1760 memset(&va, 0, sizeof(struct video_audio));
1761 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1762 if (t->audmode == V4L2_TUNER_MODE_MONO)
1763 va.mode = VIDEO_SOUND_MONO;
1764 else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1765 va.mode = VIDEO_SOUND_STEREO;
1766 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1767 va.mode = VIDEO_SOUND_LANG1;
1768 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1769 va.mode = VIDEO_SOUND_LANG2;
1770 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1771 if (btv->audio_hook)
1772 btv->audio_hook(btv,&va,1);
1778 case VIDIOC_G_FREQUENCY:
1780 struct v4l2_frequency *f = arg;
1782 memset(f,0,sizeof(*f));
1783 f->type = V4L2_TUNER_ANALOG_TV;
1784 f->frequency = btv->freq;
1787 case VIDIOC_S_FREQUENCY:
1789 struct v4l2_frequency *f = arg;
1791 if (unlikely(f->tuner != 0))
1793 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1796 btv->freq = f->frequency;
1797 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1798 if (btv->has_matchbox && btv->radio_user)
1799 tea5757_set_freq(btv,btv->freq);
1805 return -ENOIOCTLCMD;
1811 static int verify_window(const struct bttv_tvnorm *tvn,
1812 struct v4l2_window *win, int fixup)
1814 enum v4l2_field field;
1817 if (win->w.width < 48 || win->w.height < 32)
1819 if (win->clipcount > 2048)
1824 maxh = tvn->sheight;
1826 if (V4L2_FIELD_ANY == field) {
1827 field = (win->w.height > maxh/2)
1828 ? V4L2_FIELD_INTERLACED
1832 case V4L2_FIELD_TOP:
1833 case V4L2_FIELD_BOTTOM:
1836 case V4L2_FIELD_INTERLACED:
1842 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1845 if (win->w.width > maxw)
1846 win->w.width = maxw;
1847 if (win->w.height > maxh)
1848 win->w.height = maxh;
1853 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1854 struct v4l2_window *win, int fixup)
1856 struct v4l2_clip *clips = NULL;
1857 int n,size,retval = 0;
1859 if (NULL == fh->ovfmt)
1861 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1863 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1867 /* copy clips -- luckily v4l1 + v4l2 are binary
1868 compatible here ...*/
1870 size = sizeof(*clips)*(n+4);
1871 clips = kmalloc(size,GFP_KERNEL);
1875 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1880 /* clip against screen */
1881 if (NULL != btv->fbuf.base)
1882 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1884 btcx_sort_clips(clips,n);
1886 /* 4-byte alignments */
1887 switch (fh->ovfmt->depth) {
1890 btcx_align(&win->w, clips, n, 3);
1893 btcx_align(&win->w, clips, n, 1);
1896 /* no alignment fixups needed */
1902 down(&fh->cap.lock);
1904 kfree(fh->ov.clips);
1905 fh->ov.clips = clips;
1909 fh->ov.field = win->field;
1910 fh->ov.setup_ok = 1;
1911 btv->init.ov.w.width = win->w.width;
1912 btv->init.ov.w.height = win->w.height;
1913 btv->init.ov.field = win->field;
1915 /* update overlay if needed */
1917 if (check_btres(fh, RESOURCE_OVERLAY)) {
1918 struct bttv_buffer *new;
1920 new = videobuf_alloc(sizeof(*new));
1921 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1922 retval = bttv_switch_overlay(btv,fh,new);
1928 /* ----------------------------------------------------------------------- */
1930 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
1932 struct videobuf_queue* q = NULL;
1935 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1938 case V4L2_BUF_TYPE_VBI_CAPTURE:
1947 static int bttv_resource(struct bttv_fh *fh)
1952 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1953 res = RESOURCE_VIDEO;
1955 case V4L2_BUF_TYPE_VBI_CAPTURE:
1964 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
1966 struct videobuf_queue *q = bttv_queue(fh);
1967 int res = bttv_resource(fh);
1969 if (check_btres(fh,res))
1971 if (videobuf_queue_is_busy(q))
1977 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
1980 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1981 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
1982 f->fmt.pix.width = fh->width;
1983 f->fmt.pix.height = fh->height;
1984 f->fmt.pix.field = fh->cap.field;
1985 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1986 f->fmt.pix.bytesperline =
1987 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1988 f->fmt.pix.sizeimage =
1989 f->fmt.pix.height * f->fmt.pix.bytesperline;
1991 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1992 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
1993 f->fmt.win.w = fh->ov.w;
1994 f->fmt.win.field = fh->ov.field;
1996 case V4L2_BUF_TYPE_VBI_CAPTURE:
1997 bttv_vbi_get_fmt(fh,f);
2004 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2005 struct v4l2_format *f)
2008 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2010 const struct bttv_format *fmt;
2011 enum v4l2_field field;
2012 unsigned int maxw,maxh;
2014 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2019 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2020 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2021 field = f->fmt.pix.field;
2022 if (V4L2_FIELD_ANY == field)
2023 field = (f->fmt.pix.height > maxh/2)
2024 ? V4L2_FIELD_INTERLACED
2025 : V4L2_FIELD_BOTTOM;
2026 if (V4L2_FIELD_SEQ_BT == field)
2027 field = V4L2_FIELD_SEQ_TB;
2029 case V4L2_FIELD_TOP:
2030 case V4L2_FIELD_BOTTOM:
2031 case V4L2_FIELD_ALTERNATE:
2034 case V4L2_FIELD_INTERLACED:
2036 case V4L2_FIELD_SEQ_TB:
2037 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2044 /* update data for the application */
2045 f->fmt.pix.field = field;
2046 if (f->fmt.pix.width < 48)
2047 f->fmt.pix.width = 48;
2048 if (f->fmt.pix.height < 32)
2049 f->fmt.pix.height = 32;
2050 if (f->fmt.pix.width > maxw)
2051 f->fmt.pix.width = maxw;
2052 if (f->fmt.pix.height > maxh)
2053 f->fmt.pix.height = maxh;
2054 f->fmt.pix.width &= ~0x03;
2055 f->fmt.pix.bytesperline =
2056 (f->fmt.pix.width * fmt->depth) >> 3;
2057 f->fmt.pix.sizeimage =
2058 f->fmt.pix.height * f->fmt.pix.bytesperline;
2062 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2063 return verify_window(&bttv_tvnorms[btv->tvnorm],
2065 case V4L2_BUF_TYPE_VBI_CAPTURE:
2066 bttv_vbi_try_fmt(fh,f);
2073 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2074 struct v4l2_format *f)
2079 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2081 const struct bttv_format *fmt;
2083 retval = bttv_switch_type(fh,f->type);
2086 retval = bttv_try_fmt(fh,btv,f);
2089 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2091 /* update our state informations */
2092 down(&fh->cap.lock);
2094 fh->cap.field = f->fmt.pix.field;
2095 fh->cap.last = V4L2_FIELD_NONE;
2096 fh->width = f->fmt.pix.width;
2097 fh->height = f->fmt.pix.height;
2098 btv->init.fmt = fmt;
2099 btv->init.width = f->fmt.pix.width;
2100 btv->init.height = f->fmt.pix.height;
2105 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2106 return setup_window(fh, btv, &f->fmt.win, 1);
2107 case V4L2_BUF_TYPE_VBI_CAPTURE:
2108 retval = bttv_switch_type(fh,f->type);
2111 if (locked_btres(fh->btv, RESOURCE_VBI))
2113 bttv_vbi_try_fmt(fh,f);
2114 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2115 bttv_vbi_get_fmt(fh,f);
2122 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2123 unsigned int cmd, void *arg)
2125 struct bttv_fh *fh = file->private_data;
2126 struct bttv *btv = fh->btv;
2127 unsigned long flags;
2130 if (bttv_debug > 1) {
2131 switch (_IOC_TYPE(cmd)) {
2133 printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
2134 btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
2135 v4l1_ioctls[_IOC_NR(cmd)] : "???");
2138 printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
2139 btv->c.nr, cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
2142 printk("bttv%d: ioctl 0x%x (???)\n",
2147 bttv_reinit_bt848(btv);
2155 case VIDIOC_S_INPUT:
2156 case VIDIOC_S_TUNER:
2157 case VIDIOC_S_FREQUENCY:
2158 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2165 /* *** v4l1 *** ************************************************ */
2168 struct video_capability *cap = arg;
2170 memset(cap,0,sizeof(*cap));
2171 strcpy(cap->name,btv->video_dev->name);
2172 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2174 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2177 cap->type = VID_TYPE_CAPTURE|
2182 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2183 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2185 cap->minheight = 32;
2187 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2188 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2194 struct video_picture *pic = arg;
2196 memset(pic,0,sizeof(*pic));
2197 pic->brightness = btv->bright;
2198 pic->contrast = btv->contrast;
2199 pic->hue = btv->hue;
2200 pic->colour = btv->saturation;
2202 pic->depth = fh->fmt->depth;
2203 pic->palette = fh->fmt->palette;
2209 struct video_picture *pic = arg;
2210 const struct bttv_format *fmt;
2212 fmt = format_by_palette(pic->palette);
2215 down(&fh->cap.lock);
2216 if (fmt->depth != pic->depth) {
2218 goto fh_unlock_and_return;
2222 btv->init.ovfmt = fmt;
2223 btv->init.fmt = fmt;
2225 /* dirty hack time: swap bytes for overlay if the
2226 display adaptor is big endian (insmod option) */
2227 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2228 fmt->palette == VIDEO_PALETTE_RGB565 ||
2229 fmt->palette == VIDEO_PALETTE_RGB32) {
2233 bt848_bright(btv,pic->brightness);
2234 bt848_contrast(btv,pic->contrast);
2235 bt848_hue(btv,pic->hue);
2236 bt848_sat(btv,pic->colour);
2243 struct video_window *win = arg;
2245 memset(win,0,sizeof(*win));
2246 win->x = fh->ov.w.left;
2247 win->y = fh->ov.w.top;
2248 win->width = fh->ov.w.width;
2249 win->height = fh->ov.w.height;
2254 struct video_window *win = arg;
2255 struct v4l2_window w2;
2257 w2.field = V4L2_FIELD_ANY;
2260 w2.w.width = win->width;
2261 w2.w.height = win->height;
2262 w2.clipcount = win->clipcount;
2263 w2.clips = (struct v4l2_clip __user *)win->clips;
2264 retval = setup_window(fh, btv, &w2, 0);
2266 /* on v4l1 this ioctl affects the read() size too */
2267 fh->width = fh->ov.w.width;
2268 fh->height = fh->ov.w.height;
2269 btv->init.width = fh->ov.w.width;
2270 btv->init.height = fh->ov.w.height;
2277 struct video_buffer *fbuf = arg;
2279 fbuf->base = btv->fbuf.base;
2280 fbuf->width = btv->fbuf.fmt.width;
2281 fbuf->height = btv->fbuf.fmt.height;
2282 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2284 fbuf->depth = fh->ovfmt->depth;
2289 struct video_buffer *fbuf = arg;
2290 const struct bttv_format *fmt;
2293 if(!capable(CAP_SYS_ADMIN) &&
2294 !capable(CAP_SYS_RAWIO))
2296 end = (unsigned long)fbuf->base +
2297 fbuf->height * fbuf->bytesperline;
2298 down(&fh->cap.lock);
2301 switch (fbuf->depth) {
2303 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2306 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2309 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2312 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2316 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2323 goto fh_unlock_and_return;
2327 btv->init.ovfmt = fmt;
2328 btv->init.fmt = fmt;
2329 btv->fbuf.base = fbuf->base;
2330 btv->fbuf.fmt.width = fbuf->width;
2331 btv->fbuf.fmt.height = fbuf->height;
2332 if (fbuf->bytesperline)
2333 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2335 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2341 case VIDIOC_OVERLAY:
2343 struct bttv_buffer *new;
2348 if (NULL == btv->fbuf.base)
2350 if (!fh->ov.setup_ok) {
2351 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2356 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2359 down(&fh->cap.lock);
2361 fh->ov.tvnorm = btv->tvnorm;
2362 new = videobuf_alloc(sizeof(*new));
2363 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2369 retval = bttv_switch_overlay(btv,fh,new);
2376 struct video_mbuf *mbuf = arg;
2379 down(&fh->cap.lock);
2380 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2383 goto fh_unlock_and_return;
2384 memset(mbuf,0,sizeof(*mbuf));
2385 mbuf->frames = gbuffers;
2386 mbuf->size = gbuffers * gbufsize;
2387 for (i = 0; i < gbuffers; i++)
2388 mbuf->offsets[i] = i * gbufsize;
2392 case VIDIOCMCAPTURE:
2394 struct video_mmap *vm = arg;
2395 struct bttv_buffer *buf;
2396 enum v4l2_field field;
2398 if (vm->frame >= VIDEO_MAX_FRAME)
2401 down(&fh->cap.lock);
2403 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2405 goto fh_unlock_and_return;
2406 if (0 == buf->vb.baddr)
2407 goto fh_unlock_and_return;
2408 if (buf->vb.state == STATE_QUEUED ||
2409 buf->vb.state == STATE_ACTIVE)
2410 goto fh_unlock_and_return;
2412 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2413 ? V4L2_FIELD_INTERLACED
2414 : V4L2_FIELD_BOTTOM;
2415 retval = bttv_prepare_buffer(btv,buf,
2416 format_by_palette(vm->format),
2417 vm->width,vm->height,field);
2419 goto fh_unlock_and_return;
2420 spin_lock_irqsave(&btv->s_lock,flags);
2421 buffer_queue(&fh->cap,&buf->vb);
2422 spin_unlock_irqrestore(&btv->s_lock,flags);
2429 struct bttv_buffer *buf;
2431 if (*frame >= VIDEO_MAX_FRAME)
2434 down(&fh->cap.lock);
2436 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2438 goto fh_unlock_and_return;
2439 retval = videobuf_waiton(&buf->vb,0,1);
2441 goto fh_unlock_and_return;
2442 switch (buf->vb.state) {
2447 videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
2448 bttv_dma_free(btv,buf);
2460 struct vbi_format *fmt = (void *) arg;
2461 struct v4l2_format fmt2;
2463 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2464 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2468 bttv_vbi_get_fmt(fh, &fmt2);
2470 memset(fmt,0,sizeof(*fmt));
2471 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2472 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2473 fmt->sample_format = VIDEO_PALETTE_RAW;
2474 fmt->start[0] = fmt2.fmt.vbi.start[0];
2475 fmt->count[0] = fmt2.fmt.vbi.count[0];
2476 fmt->start[1] = fmt2.fmt.vbi.start[1];
2477 fmt->count[1] = fmt2.fmt.vbi.count[1];
2478 if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
2479 fmt->flags |= V4L2_VBI_UNSYNC;
2480 if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
2481 fmt->flags |= V4L2_VBI_INTERLACED;
2486 struct vbi_format *fmt = (void *) arg;
2487 struct v4l2_format fmt2;
2489 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2492 bttv_vbi_get_fmt(fh, &fmt2);
2494 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2495 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2496 fmt->sample_format != VIDEO_PALETTE_RAW ||
2497 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2498 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2499 fmt->count[0] != fmt->count[1] ||
2500 fmt->count[0] < 1 ||
2501 fmt->count[0] > 32 /* VBI_MAXLINES */)
2504 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2517 return bttv_common_ioctls(btv,cmd,arg);
2519 /* *** v4l2 *** ************************************************ */
2520 case VIDIOC_QUERYCAP:
2522 struct v4l2_capability *cap = arg;
2526 strcpy(cap->driver,"bttv");
2527 strlcpy(cap->card,btv->video_dev->name,sizeof(cap->card));
2528 sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
2529 cap->version = BTTV_VERSION_CODE;
2531 V4L2_CAP_VIDEO_CAPTURE |
2532 V4L2_CAP_VIDEO_OVERLAY |
2533 V4L2_CAP_VBI_CAPTURE |
2535 V4L2_CAP_READWRITE |
2540 case VIDIOC_ENUM_FMT:
2542 struct v4l2_fmtdesc *f = arg;
2543 enum v4l2_buf_type type;
2548 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2553 memset(f,0,sizeof(*f));
2556 f->pixelformat = V4L2_PIX_FMT_GREY;
2557 strcpy(f->description,"vbi data");
2561 /* video capture + overlay */
2563 for (i = 0; i < BTTV_FORMATS; i++) {
2564 if (bttv_formats[i].fourcc != -1)
2566 if ((unsigned int)index == f->index)
2569 if (BTTV_FORMATS == i)
2573 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2575 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2576 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2582 memset(f,0,sizeof(*f));
2585 f->pixelformat = bttv_formats[i].fourcc;
2586 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2590 case VIDIOC_TRY_FMT:
2592 struct v4l2_format *f = arg;
2593 return bttv_try_fmt(fh,btv,f);
2597 struct v4l2_format *f = arg;
2598 return bttv_g_fmt(fh,f);
2602 struct v4l2_format *f = arg;
2603 return bttv_s_fmt(fh,btv,f);
2608 struct v4l2_framebuffer *fb = arg;
2611 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2613 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2618 struct v4l2_framebuffer *fb = arg;
2619 const struct bttv_format *fmt;
2621 if(!capable(CAP_SYS_ADMIN) &&
2622 !capable(CAP_SYS_RAWIO))
2626 fmt = format_by_fourcc(fb->fmt.pixelformat);
2629 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2632 down(&fh->cap.lock);
2634 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2635 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2636 goto fh_unlock_and_return;
2637 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2638 goto fh_unlock_and_return;
2642 btv->fbuf.base = fb->base;
2643 btv->fbuf.fmt.width = fb->fmt.width;
2644 btv->fbuf.fmt.height = fb->fmt.height;
2645 if (0 != fb->fmt.bytesperline)
2646 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2648 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2652 btv->init.ovfmt = fmt;
2653 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2656 fh->ov.w.width = fb->fmt.width;
2657 fh->ov.w.height = fb->fmt.height;
2658 btv->init.ov.w.width = fb->fmt.width;
2659 btv->init.ov.w.height = fb->fmt.height;
2661 kfree(fh->ov.clips);
2662 fh->ov.clips = NULL;
2665 if (check_btres(fh, RESOURCE_OVERLAY)) {
2666 struct bttv_buffer *new;
2668 new = videobuf_alloc(sizeof(*new));
2669 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2670 retval = bttv_switch_overlay(btv,fh,new);
2677 case VIDIOC_REQBUFS:
2678 return videobuf_reqbufs(bttv_queue(fh),arg);
2680 case VIDIOC_QUERYBUF:
2681 return videobuf_querybuf(bttv_queue(fh),arg);
2684 return videobuf_qbuf(bttv_queue(fh),arg);
2687 return videobuf_dqbuf(bttv_queue(fh),arg,
2688 file->f_flags & O_NONBLOCK);
2690 case VIDIOC_STREAMON:
2692 int res = bttv_resource(fh);
2694 if (!check_alloc_btres(btv,fh,res))
2696 return videobuf_streamon(bttv_queue(fh));
2698 case VIDIOC_STREAMOFF:
2700 int res = bttv_resource(fh);
2702 retval = videobuf_streamoff(bttv_queue(fh));
2705 free_btres(btv,fh,res);
2709 case VIDIOC_QUERYCTRL:
2711 struct v4l2_queryctrl *c = arg;
2714 if ((c->id < V4L2_CID_BASE ||
2715 c->id >= V4L2_CID_LASTP1) &&
2716 (c->id < V4L2_CID_PRIVATE_BASE ||
2717 c->id >= V4L2_CID_PRIVATE_LASTP1))
2719 for (i = 0; i < BTTV_CTLS; i++)
2720 if (bttv_ctls[i].id == c->id)
2722 if (i == BTTV_CTLS) {
2727 if (i >= 4 && i <= 8) {
2728 struct video_audio va;
2729 memset(&va,0,sizeof(va));
2730 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2731 if (btv->audio_hook)
2732 btv->audio_hook(btv,&va,0);
2733 switch (bttv_ctls[i].id) {
2734 case V4L2_CID_AUDIO_VOLUME:
2735 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2738 case V4L2_CID_AUDIO_BALANCE:
2739 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2742 case V4L2_CID_AUDIO_BASS:
2743 if (!(va.flags & VIDEO_AUDIO_BASS))
2746 case V4L2_CID_AUDIO_TREBLE:
2747 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2755 return get_control(btv,arg);
2757 return set_control(btv,arg);
2760 struct v4l2_streamparm *parm = arg;
2761 struct v4l2_standard s;
2762 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2764 memset(parm,0,sizeof(*parm));
2765 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2766 bttv_tvnorms[btv->tvnorm].name);
2767 parm->parm.capture.timeperframe = s.frameperiod;
2771 case VIDIOC_G_PRIORITY:
2773 enum v4l2_priority *p = arg;
2775 *p = v4l2_prio_max(&btv->prio);
2778 case VIDIOC_S_PRIORITY:
2780 enum v4l2_priority *prio = arg;
2782 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2785 case VIDIOC_ENUMSTD:
2788 case VIDIOC_ENUMINPUT:
2789 case VIDIOC_G_INPUT:
2790 case VIDIOC_S_INPUT:
2791 case VIDIOC_G_TUNER:
2792 case VIDIOC_S_TUNER:
2793 case VIDIOC_G_FREQUENCY:
2794 case VIDIOC_S_FREQUENCY:
2795 return bttv_common_ioctls(btv,cmd,arg);
2798 return -ENOIOCTLCMD;
2802 fh_unlock_and_return:
2807 static int bttv_ioctl(struct inode *inode, struct file *file,
2808 unsigned int cmd, unsigned long arg)
2810 struct bttv_fh *fh = file->private_data;
2814 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2815 return fh->lines * 2 * 2048;
2817 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2821 static ssize_t bttv_read(struct file *file, char __user *data,
2822 size_t count, loff_t *ppos)
2824 struct bttv_fh *fh = file->private_data;
2827 if (fh->btv->errors)
2828 bttv_reinit_bt848(fh->btv);
2829 dprintk("bttv%d: read count=%d type=%s\n",
2830 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2833 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2834 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2836 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2837 file->f_flags & O_NONBLOCK);
2839 case V4L2_BUF_TYPE_VBI_CAPTURE:
2840 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2842 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2843 file->f_flags & O_NONBLOCK);
2851 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2853 struct bttv_fh *fh = file->private_data;
2854 struct bttv_buffer *buf;
2855 enum v4l2_field field;
2857 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2858 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2860 return videobuf_poll_stream(file, &fh->vbi, wait);
2863 if (check_btres(fh,RESOURCE_VIDEO)) {
2864 /* streaming capture */
2865 if (list_empty(&fh->cap.stream))
2867 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2869 /* read() capture */
2870 down(&fh->cap.lock);
2871 if (NULL == fh->cap.read_buf) {
2872 /* need to capture a new frame */
2873 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2877 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2878 if (NULL == fh->cap.read_buf) {
2882 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2883 field = videobuf_next_field(&fh->cap);
2884 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2888 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2889 fh->cap.read_off = 0;
2892 buf = (struct bttv_buffer*)fh->cap.read_buf;
2895 poll_wait(file, &buf->vb.done, wait);
2896 if (buf->vb.state == STATE_DONE ||
2897 buf->vb.state == STATE_ERROR)
2898 return POLLIN|POLLRDNORM;
2902 static int bttv_open(struct inode *inode, struct file *file)
2904 int minor = iminor(inode);
2905 struct bttv *btv = NULL;
2907 enum v4l2_buf_type type = 0;
2910 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
2912 for (i = 0; i < bttv_num; i++) {
2913 if (bttvs[i].video_dev &&
2914 bttvs[i].video_dev->minor == minor) {
2916 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2919 if (bttvs[i].vbi_dev &&
2920 bttvs[i].vbi_dev->minor == minor) {
2922 type = V4L2_BUF_TYPE_VBI_CAPTURE;
2929 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
2930 btv->c.nr,v4l2_type_names[type]);
2932 /* allocate per filehandle data */
2933 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
2936 file->private_data = fh;
2939 fh->ov.setup_ok = 0;
2940 v4l2_prio_open(&btv->prio,&fh->prio);
2942 videobuf_queue_init(&fh->cap, &bttv_video_qops,
2943 btv->c.pci, &btv->s_lock,
2944 V4L2_BUF_TYPE_VIDEO_CAPTURE,
2945 V4L2_FIELD_INTERLACED,
2946 sizeof(struct bttv_buffer),
2948 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
2949 btv->c.pci, &btv->s_lock,
2950 V4L2_BUF_TYPE_VBI_CAPTURE,
2952 sizeof(struct bttv_buffer),
2954 i2c_vidiocschan(btv);
2957 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
2958 bttv_vbi_setlines(fh,btv,16);
2959 bttv_field_count(btv);
2963 static int bttv_release(struct inode *inode, struct file *file)
2965 struct bttv_fh *fh = file->private_data;
2966 struct bttv *btv = fh->btv;
2968 /* turn off overlay */
2969 if (check_btres(fh, RESOURCE_OVERLAY))
2970 bttv_switch_overlay(btv,fh,NULL);
2972 /* stop video capture */
2973 if (check_btres(fh, RESOURCE_VIDEO)) {
2974 videobuf_streamoff(&fh->cap);
2975 free_btres(btv,fh,RESOURCE_VIDEO);
2977 if (fh->cap.read_buf) {
2978 buffer_release(&fh->cap,fh->cap.read_buf);
2979 kfree(fh->cap.read_buf);
2982 /* stop vbi capture */
2983 if (check_btres(fh, RESOURCE_VBI)) {
2984 if (fh->vbi.streaming)
2985 videobuf_streamoff(&fh->vbi);
2986 if (fh->vbi.reading)
2987 videobuf_read_stop(&fh->vbi);
2988 free_btres(btv,fh,RESOURCE_VBI);
2991 v4l2_prio_close(&btv->prio,&fh->prio);
2992 file->private_data = NULL;
2996 bttv_field_count(btv);
3001 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3003 struct bttv_fh *fh = file->private_data;
3005 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3006 fh->btv->c.nr, v4l2_type_names[fh->type],
3007 vma->vm_start, vma->vm_end - vma->vm_start);
3008 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3011 static struct file_operations bttv_fops =
3013 .owner = THIS_MODULE,
3015 .release = bttv_release,
3016 .ioctl = bttv_ioctl,
3017 .llseek = no_llseek,
3023 static struct video_device bttv_video_template =
3026 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
3027 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3028 .hardware = VID_HARDWARE_BT848,
3033 struct video_device bttv_vbi_template =
3035 .name = "bt848/878 vbi",
3036 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3037 .hardware = VID_HARDWARE_BT848,
3042 /* ----------------------------------------------------------------------- */
3043 /* radio interface */
3045 static int radio_open(struct inode *inode, struct file *file)
3047 int minor = iminor(inode);
3048 struct bttv *btv = NULL;
3051 dprintk("bttv: open minor=%d\n",minor);
3053 for (i = 0; i < bttv_num; i++) {
3054 if (bttvs[i].radio_dev->minor == minor) {
3062 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3064 if (btv->radio_user) {
3069 file->private_data = btv;
3071 i2c_vidiocschan(btv);
3072 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
3073 audio_mux(btv,AUDIO_RADIO);
3079 static int radio_release(struct inode *inode, struct file *file)
3081 struct bttv *btv = file->private_data;
3087 static int radio_do_ioctl(struct inode *inode, struct file *file,
3088 unsigned int cmd, void *arg)
3090 struct bttv *btv = file->private_data;
3095 struct video_capability *cap = arg;
3097 memset(cap,0,sizeof(*cap));
3098 strcpy(cap->name,btv->radio_dev->name);
3099 cap->type = VID_TYPE_TUNER;
3107 struct video_tuner *v = arg;
3111 memset(v,0,sizeof(*v));
3112 strcpy(v->name, "Radio");
3113 /* japan: 76.0 MHz - 89.9 MHz
3114 western europe: 87.5 MHz - 108.0 MHz
3115 russia: 65.0 MHz - 108.0 MHz */
3116 v->rangelow=(int)(65*16);
3117 v->rangehigh=(int)(108*16);
3118 bttv_call_i2c_clients(btv,cmd,v);
3130 return bttv_common_ioctls(btv,cmd,arg);
3133 return -ENOIOCTLCMD;
3138 static int radio_ioctl(struct inode *inode, struct file *file,
3139 unsigned int cmd, unsigned long arg)
3141 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3144 static struct file_operations radio_fops =
3146 .owner = THIS_MODULE,
3148 .release = radio_release,
3149 .ioctl = radio_ioctl,
3150 .llseek = no_llseek,
3153 static struct video_device radio_template =
3155 .name = "bt848/878 radio",
3156 .type = VID_TYPE_TUNER,
3157 .hardware = VID_HARDWARE_BT848,
3158 .fops = &radio_fops,
3162 /* ----------------------------------------------------------------------- */
3165 static char *irq_name[] = {
3166 "FMTCHG", // format change detected (525 vs. 625)
3167 "VSYNC", // vertical sync (new field)
3168 "HSYNC", // horizontal sync
3169 "OFLOW", // chroma/luma AGC overflow
3170 "HLOCK", // horizontal lock changed
3171 "VPRES", // video presence changed
3173 "I2CDONE", // hw irc operation finished
3174 "GPINT", // gpio port triggered irq
3176 "RISCI", // risc instruction triggered irq
3177 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3178 "FTRGT", // pixel data fifo overrun
3179 "FDSR", // fifo data stream resyncronisation
3180 "PPERR", // parity error (data transfer)
3181 "RIPERR", // parity error (read risc instructions)
3182 "PABORT", // pci abort
3183 "OCERR", // risc instruction error
3184 "SCERR", // syncronisation error
3187 static void bttv_print_irqbits(u32 print, u32 mark)
3192 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3193 if (print & (1 << i))
3194 printk(" %s",irq_name[i]);
3195 if (mark & (1 << i))
3200 static void bttv_print_riscaddr(struct bttv *btv)
3202 printk(" main: %08Lx\n",
3203 (unsigned long long)btv->main.dma);
3204 printk(" vbi : o=%08Lx e=%08Lx\n",
3205 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3206 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3207 printk(" cap : o=%08Lx e=%08Lx\n",
3208 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3209 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3210 printk(" scr : o=%08Lx e=%08Lx\n",
3211 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3212 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3215 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3217 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3219 (unsigned long)btv->main.dma,
3220 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3221 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3224 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3225 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3226 "Ok, then this is harmless, don't worry ;)\n",
3230 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3232 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3238 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3240 struct bttv_buffer *item;
3242 memset(set,0,sizeof(*set));
3244 /* capture request ? */
3245 if (!list_empty(&btv->capture)) {
3247 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3248 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3250 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3253 /* capture request for other field ? */
3254 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3255 (item->vb.queue.next != &btv->capture)) {
3256 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3257 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3258 if (NULL == set->top &&
3259 V4L2_FIELD_TOP == item->vb.field) {
3262 if (NULL == set->bottom &&
3263 V4L2_FIELD_BOTTOM == item->vb.field) {
3266 if (NULL != set->top && NULL != set->bottom)
3272 /* screen overlay ? */
3273 if (NULL != btv->screen) {
3274 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3275 if (NULL == set->top && NULL == set->bottom) {
3276 set->top = btv->screen;
3277 set->bottom = btv->screen;
3280 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3282 set->top = btv->screen;
3284 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3285 NULL == set->bottom) {
3286 set->bottom = btv->screen;
3291 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3292 btv->c.nr,set->top, set->bottom,
3293 btv->screen,set->frame_irq,set->top_irq);
3298 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3299 struct bttv_buffer_set *curr, unsigned int state)
3303 do_gettimeofday(&ts);
3305 if (wakeup->top == wakeup->bottom) {
3306 if (NULL != wakeup->top && curr->top != wakeup->top) {
3308 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3309 wakeup->top->vb.ts = ts;
3310 wakeup->top->vb.field_count = btv->field_count;
3311 wakeup->top->vb.state = state;
3312 wake_up(&wakeup->top->vb.done);
3315 if (NULL != wakeup->top && curr->top != wakeup->top) {
3317 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3318 wakeup->top->vb.ts = ts;
3319 wakeup->top->vb.field_count = btv->field_count;
3320 wakeup->top->vb.state = state;
3321 wake_up(&wakeup->top->vb.done);
3323 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3325 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3326 wakeup->bottom->vb.ts = ts;
3327 wakeup->bottom->vb.field_count = btv->field_count;
3328 wakeup->bottom->vb.state = state;
3329 wake_up(&wakeup->bottom->vb.done);
3335 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3343 do_gettimeofday(&ts);
3345 wakeup->vb.field_count = btv->field_count;
3346 wakeup->vb.state = state;
3347 wake_up(&wakeup->vb.done);
3350 static void bttv_irq_timeout(unsigned long data)
3352 struct bttv *btv = (struct bttv *)data;
3353 struct bttv_buffer_set old,new;
3354 struct bttv_buffer *ovbi;
3355 struct bttv_buffer *item;
3356 unsigned long flags;
3359 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3360 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3361 btread(BT848_RISC_COUNT));
3362 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3366 spin_lock_irqsave(&btv->s_lock,flags);
3368 /* deactivate stuff */
3369 memset(&new,0,sizeof(new));
3375 bttv_buffer_activate_video(btv, &new);
3376 bttv_buffer_activate_vbi(btv, NULL);
3377 bttv_set_dma(btv, 0);
3380 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3381 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3383 /* cancel all outstanding capture / vbi requests */
3384 while (!list_empty(&btv->capture)) {
3385 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3386 list_del(&item->vb.queue);
3387 item->vb.state = STATE_ERROR;
3388 wake_up(&item->vb.done);
3390 while (!list_empty(&btv->vcapture)) {
3391 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3392 list_del(&item->vb.queue);
3393 item->vb.state = STATE_ERROR;
3394 wake_up(&item->vb.done);
3398 spin_unlock_irqrestore(&btv->s_lock,flags);
3402 bttv_irq_wakeup_top(struct bttv *btv)
3404 struct bttv_buffer *wakeup = btv->curr.top;
3409 spin_lock(&btv->s_lock);
3410 btv->curr.top_irq = 0;
3411 btv->curr.top = NULL;
3412 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3414 do_gettimeofday(&wakeup->vb.ts);
3415 wakeup->vb.field_count = btv->field_count;
3416 wakeup->vb.state = STATE_DONE;
3417 wake_up(&wakeup->vb.done);
3418 spin_unlock(&btv->s_lock);
3421 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3425 if (rc > risc->dma + risc->size)
3431 bttv_irq_switch_video(struct bttv *btv)
3433 struct bttv_buffer_set new;
3434 struct bttv_buffer_set old;
3437 spin_lock(&btv->s_lock);
3439 /* new buffer set */
3440 bttv_irq_next_video(btv, &new);
3441 rc = btread(BT848_RISC_COUNT);
3442 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3443 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3446 bttv_irq_debug_low_latency(btv, rc);
3447 spin_unlock(&btv->s_lock);
3454 btv->loop_irq &= ~1;
3455 bttv_buffer_activate_video(btv, &new);
3456 bttv_set_dma(btv, 0);
3459 if (UNSET != btv->new_input) {
3460 video_mux(btv,btv->new_input);
3461 btv->new_input = UNSET;
3464 /* wake up finished buffers */
3465 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3466 spin_unlock(&btv->s_lock);
3470 bttv_irq_switch_vbi(struct bttv *btv)
3472 struct bttv_buffer *new = NULL;
3473 struct bttv_buffer *old;
3476 spin_lock(&btv->s_lock);
3478 if (!list_empty(&btv->vcapture))
3479 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3482 rc = btread(BT848_RISC_COUNT);
3483 if (NULL != old && (is_active(&old->top, rc) ||
3484 is_active(&old->bottom, rc))) {
3487 bttv_irq_debug_low_latency(btv, rc);
3488 spin_unlock(&btv->s_lock);
3494 btv->loop_irq &= ~4;
3495 bttv_buffer_activate_vbi(btv, new);
3496 bttv_set_dma(btv, 0);
3498 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3499 spin_unlock(&btv->s_lock);
3502 static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3510 btv=(struct bttv *)dev_id;
3513 /* get/clear interrupt status bits */
3514 stat=btread(BT848_INT_STAT);
3515 astat=stat&btread(BT848_INT_MASK);
3519 btwrite(stat,BT848_INT_STAT);
3521 /* get device status bits */
3522 dstat=btread(BT848_DSTATUS);
3525 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3526 "riscs=%x, riscc=%08x, ",
3527 btv->c.nr, count, btv->field_count,
3528 stat>>28, btread(BT848_RISC_COUNT));
3529 bttv_print_irqbits(stat,astat);
3530 if (stat & BT848_INT_HLOCK)
3531 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3533 if (stat & BT848_INT_VPRES)
3534 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3536 if (stat & BT848_INT_FMTCHG)
3537 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3542 if (astat&BT848_INT_VSYNC)
3545 if (astat & BT848_INT_GPINT) {
3546 wake_up(&btv->gpioq);
3547 bttv_gpio_irq(&btv->c);
3550 if (astat & BT848_INT_I2CDONE) {
3551 btv->i2c_done = stat;
3552 wake_up(&btv->i2c_queue);
3555 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3556 bttv_irq_switch_vbi(btv);
3558 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3559 bttv_irq_wakeup_top(btv);
3561 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3562 bttv_irq_switch_video(btv);
3564 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3567 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3568 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3569 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3570 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3571 btread(BT848_RISC_COUNT));
3572 bttv_print_irqbits(stat,astat);
3575 bttv_print_riscaddr(btv);
3577 if (fdsr && astat & BT848_INT_FDSR) {
3578 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3579 btv->c.nr,btread(BT848_RISC_COUNT));
3581 bttv_print_riscaddr(btv);
3586 btwrite(0, BT848_INT_MASK);
3588 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3589 bttv_print_irqbits(stat,astat);
3596 return IRQ_RETVAL(handled);
3600 /* ----------------------------------------------------------------------- */
3601 /* initialitation */
3603 static struct video_device *vdev_init(struct bttv *btv,
3604 struct video_device *template,
3607 struct video_device *vfd;
3609 vfd = video_device_alloc();
3614 vfd->dev = &btv->c.pci->dev;
3615 vfd->release = video_device_release;
3616 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3617 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3618 type, bttv_tvcards[btv->c.type].name);
3622 static void bttv_unregister_video(struct bttv *btv)
3624 if (btv->video_dev) {
3625 if (-1 != btv->video_dev->minor)
3626 video_unregister_device(btv->video_dev);
3628 video_device_release(btv->video_dev);
3629 btv->video_dev = NULL;
3632 if (-1 != btv->vbi_dev->minor)
3633 video_unregister_device(btv->vbi_dev);
3635 video_device_release(btv->vbi_dev);
3636 btv->vbi_dev = NULL;
3638 if (btv->radio_dev) {
3639 if (-1 != btv->radio_dev->minor)
3640 video_unregister_device(btv->radio_dev);
3642 video_device_release(btv->radio_dev);
3643 btv->radio_dev = NULL;
3647 /* register video4linux devices */
3648 static int __devinit bttv_register_video(struct bttv *btv)
3651 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3652 if (NULL == btv->video_dev)
3654 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3656 printk(KERN_INFO "bttv%d: registered device video%d\n",
3657 btv->c.nr,btv->video_dev->minor & 0x1f);
3658 video_device_create_file(btv->video_dev, &class_device_attr_card);
3661 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3662 if (NULL == btv->vbi_dev)
3664 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3666 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3667 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3669 if (!btv->has_radio)
3672 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3673 if (NULL == btv->radio_dev)
3675 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3677 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3678 btv->c.nr,btv->radio_dev->minor & 0x1f);
3684 bttv_unregister_video(btv);
3689 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3690 /* response on cards with no firmware is not enabled by OF */
3691 static void pci_set_command(struct pci_dev *dev)
3693 #if defined(__powerpc__)
3696 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3697 cmd = (cmd | PCI_COMMAND_MEMORY );
3698 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3702 static int __devinit bttv_probe(struct pci_dev *dev,
3703 const struct pci_device_id *pci_id)
3709 if (bttv_num == BTTV_MAX)
3711 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3712 btv=&bttvs[bttv_num];
3713 memset(btv,0,sizeof(*btv));
3714 btv->c.nr = bttv_num;
3715 sprintf(btv->c.name,"bttv%d",btv->c.nr);
3717 /* initialize structs / fill in defaults */
3718 init_MUTEX(&btv->lock);
3719 init_MUTEX(&btv->reslock);
3720 btv->s_lock = SPIN_LOCK_UNLOCKED;
3721 btv->gpio_lock = SPIN_LOCK_UNLOCKED;
3722 init_waitqueue_head(&btv->gpioq);
3723 init_waitqueue_head(&btv->i2c_queue);
3724 INIT_LIST_HEAD(&btv->c.subs);
3725 INIT_LIST_HEAD(&btv->capture);
3726 INIT_LIST_HEAD(&btv->vcapture);
3727 v4l2_prio_init(&btv->prio);
3729 init_timer(&btv->timeout);
3730 btv->timeout.function = bttv_irq_timeout;
3731 btv->timeout.data = (unsigned long)btv;
3734 btv->tuner_type = UNSET;
3735 btv->pinnacle_id = UNSET;
3736 btv->new_input = UNSET;
3738 btv->has_radio=radio[btv->c.nr];
3740 /* pci stuff (init, get irq/mmio, ... */
3742 btv->id = dev->device;
3743 if (pci_enable_device(dev)) {
3744 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3748 if (pci_set_dma_mask(dev, 0xffffffff)) {
3749 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3753 if (!request_mem_region(pci_resource_start(dev,0),
3754 pci_resource_len(dev,0),
3756 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3757 btv->c.nr, pci_resource_start(dev,0));
3760 pci_set_master(dev);
3761 pci_set_command(dev);
3762 pci_set_drvdata(dev,btv);
3763 if (!pci_dma_supported(dev,0xffffffff)) {
3764 printk("bttv%d: Oops: no 32bit PCI DMA ???\n", btv->c.nr);
3769 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3770 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3771 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3772 bttv_num,btv->id, btv->revision, pci_name(dev));
3773 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3774 btv->c.pci->irq, lat, pci_resource_start(dev,0));
3777 btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3778 if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3779 printk("bttv%d: ioremap() failed\n", btv->c.nr);
3787 /* disable irqs, register irq handler */
3788 btwrite(0, BT848_INT_MASK);
3789 result = request_irq(btv->c.pci->irq, bttv_irq,
3790 SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
3792 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
3793 bttv_num,btv->c.pci->irq);
3797 if (0 != bttv_handle_chipset(btv)) {
3802 /* init options from insmod args */
3803 btv->opt_combfilter = combfilter;
3804 btv->opt_lumafilter = lumafilter;
3805 btv->opt_automute = automute;
3806 btv->opt_chroma_agc = chroma_agc;
3807 btv->opt_adc_crush = adc_crush;
3808 btv->opt_vcr_hack = vcr_hack;
3809 btv->opt_whitecrush_upper = whitecrush_upper;
3810 btv->opt_whitecrush_lower = whitecrush_lower;
3812 /* fill struct bttv with some useful defaults */
3813 btv->init.btv = btv;
3814 btv->init.ov.w.width = 320;
3815 btv->init.ov.w.height = 240;
3816 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
3817 btv->init.width = 320;
3818 btv->init.height = 240;
3819 btv->init.lines = 16;
3822 /* initialize hardware */
3824 bttv_gpio_tracking(btv,"pre-init");
3826 bttv_risc_init_main(btv);
3830 btwrite(0x00, BT848_GPIO_REG_INP);
3831 btwrite(0x00, BT848_GPIO_OUT_EN);
3833 bttv_gpio_tracking(btv,"init");
3835 /* needs to be done before i2c is registered */
3836 bttv_init_card1(btv);
3838 /* register i2c + gpio */
3841 /* some card-specific stuff (needs working i2c) */
3842 bttv_init_card2(btv);
3845 /* register video4linux + input */
3846 if (!bttv_tvcards[btv->c.type].no_video) {
3847 bttv_register_video(btv);
3848 bt848_bright(btv,32768);
3849 bt848_contrast(btv,32768);
3850 bt848_hue(btv,32768);
3851 bt848_sat(btv,32768);
3852 audio_mux(btv,AUDIO_MUTE);
3856 /* add subdevices */
3857 if (btv->has_remote)
3858 bttv_sub_add_device(&btv->c, "remote");
3859 if (bttv_tvcards[btv->c.type].has_dvb)
3860 bttv_sub_add_device(&btv->c, "dvb");
3862 /* everything is fine */
3867 free_irq(btv->c.pci->irq,btv);
3870 if (btv->bt848_mmio)
3871 iounmap(btv->bt848_mmio);
3872 release_mem_region(pci_resource_start(btv->c.pci,0),
3873 pci_resource_len(btv->c.pci,0));
3874 pci_set_drvdata(dev,NULL);
3878 static void __devexit bttv_remove(struct pci_dev *pci_dev)
3880 struct bttv *btv = pci_get_drvdata(pci_dev);
3883 printk("bttv%d: unloading\n",btv->c.nr);
3885 /* shutdown everything (DMA+IRQs) */
3886 btand(~15, BT848_GPIO_DMA_CTL);
3887 btwrite(0, BT848_INT_MASK);
3888 btwrite(~0x0, BT848_INT_STAT);
3889 btwrite(0x0, BT848_GPIO_OUT_EN);
3891 bttv_gpio_tracking(btv,"cleanup");
3893 /* tell gpio modules we are leaving ... */
3895 wake_up(&btv->gpioq);
3896 bttv_sub_del_devices(&btv->c);
3898 /* unregister i2c_bus + input */
3901 /* unregister video4linux */
3902 bttv_unregister_video(btv);
3904 /* free allocated memory */
3905 btcx_riscmem_free(btv->c.pci,&btv->main);
3907 /* free ressources */
3908 free_irq(btv->c.pci->irq,btv);
3909 iounmap(btv->bt848_mmio);
3910 release_mem_region(pci_resource_start(btv->c.pci,0),
3911 pci_resource_len(btv->c.pci,0));
3913 pci_set_drvdata(pci_dev, NULL);
3917 static int bttv_suspend(struct pci_dev *pci_dev, u32 state)
3919 struct bttv *btv = pci_get_drvdata(pci_dev);
3920 struct bttv_buffer_set idle;
3921 unsigned long flags;
3923 dprintk("bttv%d: suspend %d\n", btv->c.nr, state);
3925 /* stop dma + irqs */
3926 spin_lock_irqsave(&btv->s_lock,flags);
3927 memset(&idle, 0, sizeof(idle));
3928 btv->state.video = btv->curr;
3929 btv->state.vbi = btv->cvbi;
3930 btv->state.loop_irq = btv->loop_irq;
3933 bttv_buffer_activate_video(btv, &idle);
3934 bttv_buffer_activate_vbi(btv, NULL);
3935 bttv_set_dma(btv, 0);
3936 btwrite(0, BT848_INT_MASK);
3937 spin_unlock_irqrestore(&btv->s_lock,flags);
3939 /* save bt878 state */
3940 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
3941 btv->state.gpio_data = gpio_read();
3943 /* save pci state */
3944 pci_save_state(pci_dev);
3945 if (0 != pci_set_power_state(pci_dev, state)) {
3946 pci_disable_device(pci_dev);
3947 btv->state.disabled = 1;
3952 static int bttv_resume(struct pci_dev *pci_dev)
3954 struct bttv *btv = pci_get_drvdata(pci_dev);
3955 unsigned long flags;
3957 dprintk("bttv%d: resume\n", btv->c.nr);
3959 /* restore pci state */
3960 if (btv->state.disabled) {
3961 pci_enable_device(pci_dev);
3962 btv->state.disabled = 0;
3964 pci_set_power_state(pci_dev, 0);
3965 pci_restore_state(pci_dev);
3967 /* restore bt878 state */
3968 bttv_reinit_bt848(btv);
3969 gpio_inout(0xffffff, btv->state.gpio_enable);
3970 gpio_write(btv->state.gpio_data);
3973 spin_lock_irqsave(&btv->s_lock,flags);
3974 btv->curr = btv->state.video;
3975 btv->cvbi = btv->state.vbi;
3976 btv->loop_irq = btv->state.loop_irq;
3977 bttv_buffer_activate_video(btv, &btv->curr);
3978 bttv_buffer_activate_vbi(btv, btv->cvbi);
3979 bttv_set_dma(btv, 0);
3980 spin_unlock_irqrestore(&btv->s_lock,flags);
3984 static struct pci_device_id bttv_pci_tbl[] = {
3985 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
3986 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3987 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
3988 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3989 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
3990 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3991 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
3992 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3996 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
3998 static struct pci_driver bttv_pci_driver = {
4000 .id_table = bttv_pci_tbl,
4001 .probe = bttv_probe,
4002 .remove = __devexit_p(bttv_remove),
4003 .suspend = bttv_suspend,
4004 .resume = bttv_resume,
4007 static int bttv_init_module(void)
4011 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4012 (BTTV_VERSION_CODE >> 16) & 0xff,
4013 (BTTV_VERSION_CODE >> 8) & 0xff,
4014 BTTV_VERSION_CODE & 0xff);
4016 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4017 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4019 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4021 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4022 gbufsize = BTTV_MAX_FBUF;
4023 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4025 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4026 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4028 bttv_check_chipset();
4030 bus_register(&bttv_sub_bus_type);
4031 return pci_module_init(&bttv_pci_driver);
4034 static void bttv_cleanup_module(void)
4036 pci_unregister_driver(&bttv_pci_driver);
4037 bus_unregister(&bttv_sub_bus_type);
4041 module_init(bttv_init_module);
4042 module_exit(bttv_cleanup_module);