2 bttv - Bt848 frame grabber driver
4 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
5 & Marcus Metzler <mocm@thp.uni-koeln.de>
6 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
8 some v4l2 code lines are taken from Justin's bttv2 driver which is
9 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/delay.h>
29 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/interrupt.h>
34 #include <linux/kdev_t.h>
37 #include <asm/byteorder.h>
41 unsigned int bttv_num; /* number of Bt848s in use */
42 struct bttv bttvs[BTTV_MAX];
44 unsigned int bttv_debug = 0;
45 unsigned int bttv_verbose = 1;
46 unsigned int bttv_gpio = 0;
48 /* config variables */
50 static unsigned int bigendian=1;
52 static unsigned int bigendian=0;
54 static unsigned int radio[BTTV_MAX];
55 static unsigned int irq_debug = 0;
56 static unsigned int gbuffers = 8;
57 static unsigned int gbufsize = 0x208000;
59 static int video_nr = -1;
60 static int radio_nr = -1;
61 static int vbi_nr = -1;
62 static int debug_latency = 0;
64 static unsigned int fdsr = 0;
67 static unsigned int combfilter = 0;
68 static unsigned int lumafilter = 0;
69 static unsigned int automute = 1;
70 static unsigned int chroma_agc = 0;
71 static unsigned int adc_crush = 1;
72 static unsigned int whitecrush_upper = 0xCF;
73 static unsigned int whitecrush_lower = 0x7F;
74 static unsigned int vcr_hack = 0;
75 static unsigned int irq_iswitch = 0;
77 /* API features (turn on/off stuff for testing) */
78 static unsigned int v4l2 = 1;
82 MODULE_PARM(radio,"1-" __stringify(BTTV_MAX) "i");
83 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
84 MODULE_PARM(bigendian,"i");
85 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
86 MODULE_PARM(bttv_verbose,"i");
87 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
88 MODULE_PARM(bttv_gpio,"i");
89 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
90 MODULE_PARM(bttv_debug,"i");
91 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
92 MODULE_PARM(irq_debug,"i");
93 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
94 MODULE_PARM(gbuffers,"i");
95 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
96 MODULE_PARM(gbufsize,"i");
97 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
99 MODULE_PARM(video_nr,"i");
100 MODULE_PARM(radio_nr,"i");
101 MODULE_PARM(vbi_nr,"i");
102 MODULE_PARM(debug_latency,"i");
104 MODULE_PARM(fdsr,"i");
106 MODULE_PARM(combfilter,"i");
107 MODULE_PARM(lumafilter,"i");
108 MODULE_PARM(automute,"i");
109 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
110 MODULE_PARM(chroma_agc,"i");
111 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
112 MODULE_PARM(adc_crush,"i");
113 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
114 MODULE_PARM(whitecrush_upper,"i");
115 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
116 MODULE_PARM(whitecrush_lower,"i");
117 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
118 MODULE_PARM(vcr_hack,"i");
119 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
120 MODULE_PARM(irq_iswitch,"i");
121 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
123 MODULE_PARM(v4l2,"i");
125 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
126 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
127 MODULE_LICENSE("GPL");
131 static int __init p_radio(char *str) { return bttv_parse(str,BTTV_MAX,radio); }
132 __setup("bttv.radio=", p_radio);
135 /* ----------------------------------------------------------------------- */
138 static ssize_t show_card(struct class_device *cd, char *buf)
140 struct video_device *vfd = to_video_device(cd);
141 struct bttv *btv = dev_get_drvdata(vfd->dev);
142 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
144 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
146 /* ----------------------------------------------------------------------- */
149 /* special timing tables from conexant... */
150 static u8 SRAM_Table[][60] =
152 /* PAL digital input over GPIO[7:0] */
154 45, // 45 bytes following
155 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
156 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
157 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
158 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
159 0x37,0x00,0xAF,0x21,0x00
161 /* NTSC digital input over GPIO[7:0] */
163 51, // 51 bytes following
164 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
165 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
166 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
167 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
168 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
171 // TGB_NTSC392 // quartzsight
172 // This table has been modified to be used for Fusion Rev D
174 0x2A, // size of table = 42
175 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
176 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
177 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
178 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
183 const struct bttv_tvnorm bttv_tvnorms[] = {
185 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
186 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
188 .v4l2_id = V4L2_STD_PAL,
196 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
197 .scaledtwidth = 1135,
204 .v4l2_id = V4L2_STD_NTSC_M,
212 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
220 .v4l2_id = V4L2_STD_SECAM,
228 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
229 .scaledtwidth = 1135,
234 .sram = 0, /* like PAL, correct? */
236 .v4l2_id = V4L2_STD_PAL_Nc,
244 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
252 .v4l2_id = V4L2_STD_PAL_M,
260 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
268 .v4l2_id = V4L2_STD_PAL_N,
276 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
284 .v4l2_id = V4L2_STD_NTSC_M_JP,
292 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
300 /* that one hopefully works with the strange timing
301 * which video recorders produce when playing a NTSC
302 * tape on a PAL TV ... */
303 .v4l2_id = V4L2_STD_PAL_60,
311 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
312 .scaledtwidth = 1135,
321 const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
323 /* ----------------------------------------------------------------------- */
325 packed pixel formats must come first */
326 const struct bttv_format bttv_formats[] = {
328 .name = "8 bpp, gray",
329 .palette = VIDEO_PALETTE_GREY,
330 .fourcc = V4L2_PIX_FMT_GREY,
331 .btformat = BT848_COLOR_FMT_Y8,
333 .flags = FORMAT_FLAGS_PACKED,
335 .name = "8 bpp, dithered color",
336 .palette = VIDEO_PALETTE_HI240,
337 .fourcc = V4L2_PIX_FMT_HI240,
338 .btformat = BT848_COLOR_FMT_RGB8,
340 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
342 .name = "15 bpp RGB, le",
343 .palette = VIDEO_PALETTE_RGB555,
344 .fourcc = V4L2_PIX_FMT_RGB555,
345 .btformat = BT848_COLOR_FMT_RGB15,
347 .flags = FORMAT_FLAGS_PACKED,
349 .name = "15 bpp RGB, be",
351 .fourcc = V4L2_PIX_FMT_RGB555X,
352 .btformat = BT848_COLOR_FMT_RGB15,
353 .btswap = 0x03, /* byteswap */
355 .flags = FORMAT_FLAGS_PACKED,
357 .name = "16 bpp RGB, le",
358 .palette = VIDEO_PALETTE_RGB565,
359 .fourcc = V4L2_PIX_FMT_RGB565,
360 .btformat = BT848_COLOR_FMT_RGB16,
362 .flags = FORMAT_FLAGS_PACKED,
364 .name = "16 bpp RGB, be",
366 .fourcc = V4L2_PIX_FMT_RGB565X,
367 .btformat = BT848_COLOR_FMT_RGB16,
368 .btswap = 0x03, /* byteswap */
370 .flags = FORMAT_FLAGS_PACKED,
372 .name = "24 bpp RGB, le",
373 .palette = VIDEO_PALETTE_RGB24,
374 .fourcc = V4L2_PIX_FMT_BGR24,
375 .btformat = BT848_COLOR_FMT_RGB24,
377 .flags = FORMAT_FLAGS_PACKED,
379 .name = "32 bpp RGB, le",
380 .palette = VIDEO_PALETTE_RGB32,
381 .fourcc = V4L2_PIX_FMT_BGR32,
382 .btformat = BT848_COLOR_FMT_RGB32,
384 .flags = FORMAT_FLAGS_PACKED,
386 .name = "32 bpp RGB, be",
388 .fourcc = V4L2_PIX_FMT_RGB32,
389 .btformat = BT848_COLOR_FMT_RGB32,
390 .btswap = 0x0f, /* byte+word swap */
392 .flags = FORMAT_FLAGS_PACKED,
394 .name = "4:2:2, packed, YUYV",
395 .palette = VIDEO_PALETTE_YUV422,
396 .fourcc = V4L2_PIX_FMT_YUYV,
397 .btformat = BT848_COLOR_FMT_YUY2,
399 .flags = FORMAT_FLAGS_PACKED,
401 .name = "4:2:2, packed, YUYV",
402 .palette = VIDEO_PALETTE_YUYV,
403 .fourcc = V4L2_PIX_FMT_YUYV,
404 .btformat = BT848_COLOR_FMT_YUY2,
406 .flags = FORMAT_FLAGS_PACKED,
408 .name = "4:2:2, packed, UYVY",
409 .palette = VIDEO_PALETTE_UYVY,
410 .fourcc = V4L2_PIX_FMT_UYVY,
411 .btformat = BT848_COLOR_FMT_YUY2,
412 .btswap = 0x03, /* byteswap */
414 .flags = FORMAT_FLAGS_PACKED,
416 .name = "4:2:2, planar, Y-Cb-Cr",
417 .palette = VIDEO_PALETTE_YUV422P,
418 .fourcc = V4L2_PIX_FMT_YUV422P,
419 .btformat = BT848_COLOR_FMT_YCrCb422,
421 .flags = FORMAT_FLAGS_PLANAR,
425 .name = "4:2:0, planar, Y-Cb-Cr",
426 .palette = VIDEO_PALETTE_YUV420P,
427 .fourcc = V4L2_PIX_FMT_YUV420,
428 .btformat = BT848_COLOR_FMT_YCrCb422,
430 .flags = FORMAT_FLAGS_PLANAR,
434 .name = "4:2:0, planar, Y-Cr-Cb",
436 .fourcc = V4L2_PIX_FMT_YVU420,
437 .btformat = BT848_COLOR_FMT_YCrCb422,
439 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
443 .name = "4:1:1, planar, Y-Cb-Cr",
444 .palette = VIDEO_PALETTE_YUV411P,
445 .fourcc = V4L2_PIX_FMT_YUV411P,
446 .btformat = BT848_COLOR_FMT_YCrCb411,
448 .flags = FORMAT_FLAGS_PLANAR,
452 .name = "4:1:0, planar, Y-Cb-Cr",
453 .palette = VIDEO_PALETTE_YUV410P,
454 .fourcc = V4L2_PIX_FMT_YUV410,
455 .btformat = BT848_COLOR_FMT_YCrCb411,
457 .flags = FORMAT_FLAGS_PLANAR,
461 .name = "4:1:0, planar, Y-Cr-Cb",
463 .fourcc = V4L2_PIX_FMT_YVU410,
464 .btformat = BT848_COLOR_FMT_YCrCb411,
466 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
470 .name = "raw scanlines",
471 .palette = VIDEO_PALETTE_RAW,
473 .btformat = BT848_COLOR_FMT_RAW,
475 .flags = FORMAT_FLAGS_RAW,
478 const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
480 /* ----------------------------------------------------------------------- */
482 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
483 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
484 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
485 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
486 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
487 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
488 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
489 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
490 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 8)
492 static const struct v4l2_queryctrl no_ctl = {
494 .flags = V4L2_CTRL_FLAG_DISABLED,
496 static const struct v4l2_queryctrl bttv_ctls[] = {
499 .id = V4L2_CID_BRIGHTNESS,
500 .name = "Brightness",
504 .default_value = 32768,
505 .type = V4L2_CTRL_TYPE_INTEGER,
507 .id = V4L2_CID_CONTRAST,
512 .default_value = 32768,
513 .type = V4L2_CTRL_TYPE_INTEGER,
515 .id = V4L2_CID_SATURATION,
516 .name = "Saturation",
520 .default_value = 32768,
521 .type = V4L2_CTRL_TYPE_INTEGER,
528 .default_value = 32768,
529 .type = V4L2_CTRL_TYPE_INTEGER,
533 .id = V4L2_CID_AUDIO_MUTE,
537 .type = V4L2_CTRL_TYPE_BOOLEAN,
539 .id = V4L2_CID_AUDIO_VOLUME,
544 .default_value = 65535,
545 .type = V4L2_CTRL_TYPE_INTEGER,
547 .id = V4L2_CID_AUDIO_BALANCE,
552 .default_value = 32768,
553 .type = V4L2_CTRL_TYPE_INTEGER,
555 .id = V4L2_CID_AUDIO_BASS,
560 .default_value = 32768,
561 .type = V4L2_CTRL_TYPE_INTEGER,
563 .id = V4L2_CID_AUDIO_TREBLE,
568 .default_value = 32768,
569 .type = V4L2_CTRL_TYPE_INTEGER,
571 /* --- private --- */
573 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
574 .name = "chroma agc",
577 .type = V4L2_CTRL_TYPE_BOOLEAN,
579 .id = V4L2_CID_PRIVATE_COMBFILTER,
580 .name = "combfilter",
583 .type = V4L2_CTRL_TYPE_BOOLEAN,
585 .id = V4L2_CID_PRIVATE_AUTOMUTE,
589 .type = V4L2_CTRL_TYPE_BOOLEAN,
591 .id = V4L2_CID_PRIVATE_LUMAFILTER,
592 .name = "luma decimation filter",
595 .type = V4L2_CTRL_TYPE_BOOLEAN,
597 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
601 .type = V4L2_CTRL_TYPE_BOOLEAN,
603 .id = V4L2_CID_PRIVATE_VCR_HACK,
607 .type = V4L2_CTRL_TYPE_BOOLEAN,
609 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
610 .name = "whitecrush upper",
614 .default_value = 0xCF,
615 .type = V4L2_CTRL_TYPE_INTEGER,
617 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
618 .name = "whitecrush lower",
622 .default_value = 0x7F,
623 .type = V4L2_CTRL_TYPE_INTEGER,
627 const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
629 /* ----------------------------------------------------------------------- */
630 /* resource management */
633 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
635 if (fh->resources & bit)
636 /* have it already allocated */
641 if (btv->resources & bit) {
642 /* no, someone else uses it */
646 /* it's free, grab it */
647 fh->resources |= bit;
648 btv->resources |= bit;
654 int check_btres(struct bttv_fh *fh, int bit)
656 return (fh->resources & bit);
660 int locked_btres(struct bttv *btv, int bit)
662 return (btv->resources & bit);
666 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
669 if ((fh->resources & bits) != bits) {
670 /* trying to free ressources not allocated by us ... */
671 printk("bttv: BUG! (btres)\n");
675 fh->resources &= ~bits;
676 btv->resources &= ~bits;
680 /* ----------------------------------------------------------------------- */
681 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
683 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
684 PLL_X = Reference pre-divider (0=1, 1=2)
685 PLL_C = Post divider (0=6, 1=4)
686 PLL_I = Integer input
687 PLL_F = Fractional input
689 F_input = 28.636363 MHz:
690 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
693 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
695 unsigned char fl, fh, fi;
697 /* prevent overflows */
710 btwrite(fl, BT848_PLL_F_LO);
711 btwrite(fh, BT848_PLL_F_HI);
712 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
715 static void set_pll(struct bttv *btv)
719 if (!btv->pll.pll_crystal)
722 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
723 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
727 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
729 if (btv->pll.pll_current == 0)
731 vprintk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
732 btv->c.nr,btv->pll.pll_ifreq);
733 btwrite(0x00,BT848_TGCTRL);
734 btwrite(0x00,BT848_PLL_XCI);
735 btv->pll.pll_current = 0;
739 vprintk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
740 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
741 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
743 for (i=0; i<10; i++) {
744 /* Let other people run while the PLL stabilizes */
746 set_current_state(TASK_INTERRUPTIBLE);
747 schedule_timeout(HZ/50);
749 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
750 btwrite(0,BT848_DSTATUS);
752 btwrite(0x08,BT848_TGCTRL);
753 btv->pll.pll_current = btv->pll.pll_ofreq;
758 btv->pll.pll_current = -1;
763 /* used to switch between the bt848's analog/digital video capture modes */
764 void bt848A_set_timing(struct bttv *btv)
767 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
768 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
770 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
771 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
772 btv->c.nr,table_idx);
774 /* timing change...reset timing generator address */
775 btwrite(0x00, BT848_TGCTRL);
776 btwrite(0x02, BT848_TGCTRL);
777 btwrite(0x00, BT848_TGCTRL);
779 len=SRAM_Table[table_idx][0];
780 for(i = 1; i <= len; i++)
781 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
782 btv->pll.pll_ofreq = 27000000;
785 btwrite(0x11, BT848_TGCTRL);
786 btwrite(0x41, BT848_DVSIF);
788 btv->pll.pll_ofreq = fsc;
790 btwrite(0x0, BT848_DVSIF);
794 /* ----------------------------------------------------------------------- */
796 static void bt848_bright(struct bttv *btv, int bright)
800 // printk("bttv: set bright: %d\n",bright); // DEBUG
801 btv->bright = bright;
803 /* We want -128 to 127 we get 0-65535 */
804 value = (bright >> 8) - 128;
805 btwrite(value & 0xff, BT848_BRIGHT);
808 static void bt848_hue(struct bttv *btv, int hue)
815 value = (hue >> 8) - 128;
816 btwrite(value & 0xff, BT848_HUE);
819 static void bt848_contrast(struct bttv *btv, int cont)
823 btv->contrast = cont;
827 hibit = (value >> 6) & 4;
828 btwrite(value & 0xff, BT848_CONTRAST_LO);
829 btaor(hibit, ~4, BT848_E_CONTROL);
830 btaor(hibit, ~4, BT848_O_CONTROL);
833 static void bt848_sat(struct bttv *btv, int color)
835 int val_u,val_v,hibits;
837 btv->saturation = color;
839 /* 0-511 for the color */
841 val_v = ((color>>7)*180L)/254;
842 hibits = (val_u >> 7) & 2;
843 hibits |= (val_v >> 8) & 1;
844 btwrite(val_u & 0xff, BT848_SAT_U_LO);
845 btwrite(val_v & 0xff, BT848_SAT_V_LO);
846 btaor(hibits, ~3, BT848_E_CONTROL);
847 btaor(hibits, ~3, BT848_O_CONTROL);
850 /* ----------------------------------------------------------------------- */
853 video_mux(struct bttv *btv, unsigned int input)
857 if (input >= bttv_tvcards[btv->c.type].video_inputs)
860 /* needed by RemoteVideo MX */
861 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
863 gpio_inout(mask2,mask2);
865 if (input == btv->svhs) {
866 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
867 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
869 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
870 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
872 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
873 btaor(mux<<5, ~(3<<5), BT848_IFORM);
874 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
875 btv->c.nr,input,mux);
877 /* card specific hook */
878 if(bttv_tvcards[btv->c.type].muxsel_hook)
879 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
883 static char *audio_modes[] = {
884 "audio: tuner", "audio: radio", "audio: extern",
885 "audio: intern", "audio: off"
889 audio_mux(struct bttv *btv, int mode)
891 int val,mux,i2c_mux,signal;
893 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
894 bttv_tvcards[btv->c.type].gpiomask);
895 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
899 btv->audio |= AUDIO_MUTE;
902 btv->audio &= ~AUDIO_MUTE;
908 btv->audio &= AUDIO_MUTE;
911 i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
912 if (btv->opt_automute && !signal && !btv->radio_user)
915 printk("bttv%d: amux: mode=%d audio=%d signal=%s mux=%d/%d irq=%s\n",
916 btv->c.nr, mode, btv->audio, signal ? "yes" : "no",
917 mux, i2c_mux, in_interrupt() ? "yes" : "no");
920 val = bttv_tvcards[btv->c.type].audiomux[mux];
921 gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
923 bttv_gpio_tracking(btv,audio_modes[mux]);
925 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
930 i2c_vidiocschan(struct bttv *btv)
932 struct video_channel c;
934 memset(&c,0,sizeof(c));
935 c.norm = btv->tvnorm;
936 c.channel = btv->input;
937 bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
938 if (btv->c.type == BTTV_VOODOOTV_FM)
939 bttv_tda9880_setnorm(btv,c.norm);
943 set_tvnorm(struct bttv *btv, unsigned int norm)
945 const struct bttv_tvnorm *tvnorm;
947 if (norm < 0 || norm >= BTTV_TVNORMS)
951 tvnorm = &bttv_tvnorms[norm];
953 btwrite(tvnorm->adelay, BT848_ADELAY);
954 btwrite(tvnorm->bdelay, BT848_BDELAY);
955 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
957 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
958 btwrite(1, BT848_VBI_PACK_DEL);
959 bt848A_set_timing(btv);
961 switch (btv->c.type) {
962 case BTTV_VOODOOTV_FM:
963 bttv_tda9880_setnorm(btv,norm);
967 osprey_540_set_norm(btv,norm);
975 set_input(struct bttv *btv, unsigned int input)
981 spin_lock_irqsave(&btv->s_lock,flags);
982 if (btv->curr.irqflags) {
983 /* active capture -> delayed input switch */
984 btv->new_input = input;
986 video_mux(btv,input);
988 spin_unlock_irqrestore(&btv->s_lock,flags);
990 video_mux(btv,input);
992 audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
993 AUDIO_TUNER : AUDIO_EXTERN));
994 set_tvnorm(btv,btv->tvnorm);
997 static void init_irqreg(struct bttv *btv)
1000 btwrite(0xfffffUL, BT848_INT_STAT);
1002 if (bttv_tvcards[btv->c.type].no_video) {
1004 btwrite(BT848_INT_I2CDONE,
1008 btwrite((btv->triton1) |
1009 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1011 (fdsr ? BT848_INT_FDSR : 0) |
1012 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1013 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1019 static void init_bt848(struct bttv *btv)
1023 if (bttv_tvcards[btv->c.type].no_video) {
1024 /* very basic init only */
1029 btwrite(0x00, BT848_CAP_CTL);
1030 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1031 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1033 /* set planar and packed mode trigger points and */
1034 /* set rising edge of inverted GPINTR pin as irq trigger */
1035 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1036 BT848_GPIO_DMA_CTL_PLTP1_16|
1037 BT848_GPIO_DMA_CTL_PLTP23_16|
1038 BT848_GPIO_DMA_CTL_GPINTC|
1039 BT848_GPIO_DMA_CTL_GPINTI,
1040 BT848_GPIO_DMA_CTL);
1042 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1043 btwrite(val, BT848_E_SCLOOP);
1044 btwrite(val, BT848_O_SCLOOP);
1046 btwrite(0x20, BT848_E_VSCALE_HI);
1047 btwrite(0x20, BT848_O_VSCALE_HI);
1048 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1051 btwrite(whitecrush_upper, BT848_WC_UP);
1052 btwrite(whitecrush_lower, BT848_WC_DOWN);
1054 bt848_bright(btv, btv->bright);
1055 bt848_hue(btv, btv->hue);
1056 bt848_contrast(btv, btv->contrast);
1057 bt848_sat(btv, btv->saturation);
1059 if (btv->opt_lumafilter) {
1060 btwrite(0, BT848_E_CONTROL);
1061 btwrite(0, BT848_O_CONTROL);
1063 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1064 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1071 extern void bttv_reinit_bt848(struct bttv *btv)
1073 unsigned long flags;
1076 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1077 spin_lock_irqsave(&btv->s_lock,flags);
1079 bttv_set_dma(btv,0,0);
1080 spin_unlock_irqrestore(&btv->s_lock,flags);
1083 btv->pll.pll_current = -1;
1084 set_input(btv,btv->input);
1087 static int get_control(struct bttv *btv, struct v4l2_control *c)
1089 struct video_audio va;
1092 for (i = 0; i < BTTV_CTLS; i++)
1093 if (bttv_ctls[i].id == c->id)
1097 if (i >= 4 && i <= 8) {
1098 memset(&va,0,sizeof(va));
1099 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1100 if (btv->audio_hook)
1101 btv->audio_hook(btv,&va,0);
1104 case V4L2_CID_BRIGHTNESS:
1105 c->value = btv->bright;
1108 c->value = btv->hue;
1110 case V4L2_CID_CONTRAST:
1111 c->value = btv->contrast;
1113 case V4L2_CID_SATURATION:
1114 c->value = btv->saturation;
1117 case V4L2_CID_AUDIO_MUTE:
1118 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1120 case V4L2_CID_AUDIO_VOLUME:
1121 c->value = va.volume;
1123 case V4L2_CID_AUDIO_BALANCE:
1124 c->value = va.balance;
1126 case V4L2_CID_AUDIO_BASS:
1129 case V4L2_CID_AUDIO_TREBLE:
1130 c->value = va.treble;
1133 case V4L2_CID_PRIVATE_CHROMA_AGC:
1134 c->value = btv->opt_chroma_agc;
1136 case V4L2_CID_PRIVATE_COMBFILTER:
1137 c->value = btv->opt_combfilter;
1139 case V4L2_CID_PRIVATE_LUMAFILTER:
1140 c->value = btv->opt_lumafilter;
1142 case V4L2_CID_PRIVATE_AUTOMUTE:
1143 c->value = btv->opt_automute;
1145 case V4L2_CID_PRIVATE_AGC_CRUSH:
1146 c->value = btv->opt_adc_crush;
1148 case V4L2_CID_PRIVATE_VCR_HACK:
1149 c->value = btv->opt_vcr_hack;
1151 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1152 c->value = btv->opt_whitecrush_upper;
1154 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1155 c->value = btv->opt_whitecrush_lower;
1163 static int set_control(struct bttv *btv, struct v4l2_control *c)
1165 struct video_audio va;
1168 for (i = 0; i < BTTV_CTLS; i++)
1169 if (bttv_ctls[i].id == c->id)
1173 if (i >= 4 && i <= 8) {
1174 memset(&va,0,sizeof(va));
1175 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1176 if (btv->audio_hook)
1177 btv->audio_hook(btv,&va,0);
1180 case V4L2_CID_BRIGHTNESS:
1181 bt848_bright(btv,c->value);
1184 bt848_hue(btv,c->value);
1186 case V4L2_CID_CONTRAST:
1187 bt848_contrast(btv,c->value);
1189 case V4L2_CID_SATURATION:
1190 bt848_sat(btv,c->value);
1192 case V4L2_CID_AUDIO_MUTE:
1194 va.flags |= VIDEO_AUDIO_MUTE;
1195 audio_mux(btv, AUDIO_MUTE);
1197 va.flags &= ~VIDEO_AUDIO_MUTE;
1198 audio_mux(btv, AUDIO_UNMUTE);
1202 case V4L2_CID_AUDIO_VOLUME:
1203 va.volume = c->value;
1205 case V4L2_CID_AUDIO_BALANCE:
1206 va.balance = c->value;
1208 case V4L2_CID_AUDIO_BASS:
1211 case V4L2_CID_AUDIO_TREBLE:
1212 va.treble = c->value;
1215 case V4L2_CID_PRIVATE_CHROMA_AGC:
1216 btv->opt_chroma_agc = c->value;
1217 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1218 btwrite(val, BT848_E_SCLOOP);
1219 btwrite(val, BT848_O_SCLOOP);
1221 case V4L2_CID_PRIVATE_COMBFILTER:
1222 btv->opt_combfilter = c->value;
1224 case V4L2_CID_PRIVATE_LUMAFILTER:
1225 btv->opt_lumafilter = c->value;
1226 if (btv->opt_lumafilter) {
1227 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1228 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1230 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1231 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1234 case V4L2_CID_PRIVATE_AUTOMUTE:
1235 btv->opt_automute = c->value;
1237 case V4L2_CID_PRIVATE_AGC_CRUSH:
1238 btv->opt_adc_crush = c->value;
1239 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1242 case V4L2_CID_PRIVATE_VCR_HACK:
1243 btv->opt_vcr_hack = c->value;
1245 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1246 btv->opt_whitecrush_upper = c->value;
1247 btwrite(c->value, BT848_WC_UP);
1249 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1250 btv->opt_whitecrush_lower = c->value;
1251 btwrite(c->value, BT848_WC_DOWN);
1256 if (i >= 4 && i <= 8) {
1257 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1258 if (btv->audio_hook)
1259 btv->audio_hook(btv,&va,1);
1264 /* ----------------------------------------------------------------------- */
1266 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1268 unsigned int outbits, data;
1269 outbits = btread(BT848_GPIO_OUT_EN);
1270 data = btread(BT848_GPIO_DATA);
1271 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1272 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1275 void bttv_field_count(struct bttv *btv)
1283 /* start field counter */
1284 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1286 /* stop field counter */
1287 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1288 btv->field_count = 0;
1292 static const struct bttv_format*
1293 format_by_palette(int palette)
1297 for (i = 0; i < BTTV_FORMATS; i++) {
1298 if (-1 == bttv_formats[i].palette)
1300 if (bttv_formats[i].palette == palette)
1301 return bttv_formats+i;
1306 static const struct bttv_format*
1307 format_by_fourcc(int fourcc)
1311 for (i = 0; i < BTTV_FORMATS; i++) {
1312 if (-1 == bttv_formats[i].fourcc)
1314 if (bttv_formats[i].fourcc == fourcc)
1315 return bttv_formats+i;
1320 /* ----------------------------------------------------------------------- */
1324 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1325 struct bttv_buffer *new)
1327 struct bttv_buffer *old;
1328 unsigned long flags;
1331 dprintk("switch_overlay: enter [new=%p]\n",new);
1333 new->vb.state = STATE_DONE;
1334 spin_lock_irqsave(&btv->s_lock,flags);
1337 bttv_set_dma(btv, 0x03, 1);
1338 spin_unlock_irqrestore(&btv->s_lock,flags);
1340 free_btres(btv,fh,RESOURCE_OVERLAY);
1342 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1343 bttv_dma_free(btv, old);
1346 dprintk("switch_overlay: done\n");
1350 /* ----------------------------------------------------------------------- */
1351 /* video4linux (1) interface */
1353 static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
1354 const struct bttv_format *fmt,
1355 unsigned int width, unsigned int height,
1356 enum v4l2_field field)
1358 int redo_dma_risc = 0;
1361 /* check settings */
1364 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1366 height = RAW_LINES*2;
1367 if (width*height > buf->vb.bsize)
1369 buf->vb.size = buf->vb.bsize;
1373 width > bttv_tvnorms[btv->tvnorm].swidth ||
1374 height > bttv_tvnorms[btv->tvnorm].sheight)
1376 buf->vb.size = (width * height * fmt->depth) >> 3;
1377 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1381 /* alloc + fill struct bttv_buffer (if changed) */
1382 if (buf->vb.width != width || buf->vb.height != height ||
1383 buf->vb.field != field ||
1384 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1385 buf->vb.width = width;
1386 buf->vb.height = height;
1387 buf->vb.field = field;
1388 buf->tvnorm = btv->tvnorm;
1393 /* alloc risc memory */
1394 if (STATE_NEEDS_INIT == buf->vb.state) {
1396 if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
1401 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1404 buf->vb.state = STATE_PREPARED;
1408 bttv_dma_free(btv,buf);
1413 buffer_setup(struct file *file, unsigned int *count, unsigned int *size)
1415 struct bttv_fh *fh = file->private_data;
1417 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1420 while (*size * *count > gbuffers * gbufsize)
1426 buffer_prepare(struct file *file, struct videobuf_buffer *vb,
1427 enum v4l2_field field)
1429 struct bttv_buffer *buf = (struct bttv_buffer*)vb;
1430 struct bttv_fh *fh = file->private_data;
1432 return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
1433 fh->width, fh->height, field);
1437 buffer_queue(struct file *file, struct videobuf_buffer *vb)
1439 struct bttv_buffer *buf = (struct bttv_buffer*)vb;
1440 struct bttv_fh *fh = file->private_data;
1442 buf->vb.state = STATE_QUEUED;
1443 list_add_tail(&buf->vb.queue,&fh->btv->capture);
1444 bttv_set_dma(fh->btv, 0x03, 1);
1447 static void buffer_release(struct file *file, struct videobuf_buffer *vb)
1449 struct bttv_buffer *buf = (struct bttv_buffer*)vb;
1450 struct bttv_fh *fh = file->private_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)
1707 i2c_vidiocschan(btv);
1712 case VIDIOC_G_TUNER:
1714 struct v4l2_tuner *t = arg;
1716 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1721 memset(t,0,sizeof(*t));
1722 strcpy(t->name, "Television");
1723 t->type = V4L2_TUNER_ANALOG_TV;
1724 t->rangehigh = 0xffffffffUL;
1725 t->capability = V4L2_TUNER_CAP_NORM;
1726 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1727 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1731 struct video_audio va;
1732 memset(&va, 0, sizeof(struct video_audio));
1733 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1734 if (btv->audio_hook)
1735 btv->audio_hook(btv,&va,0);
1736 if(va.mode & VIDEO_SOUND_STEREO) {
1737 t->audmode = V4L2_TUNER_MODE_STEREO;
1738 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1740 if(va.mode & VIDEO_SOUND_LANG1) {
1741 t->audmode = V4L2_TUNER_MODE_LANG1;
1742 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1743 | V4L2_TUNER_SUB_LANG2;
1746 /* FIXME: fill capability+audmode */
1750 case VIDIOC_S_TUNER:
1752 struct v4l2_tuner *t = arg;
1754 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1760 struct video_audio va;
1761 memset(&va, 0, sizeof(struct video_audio));
1762 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1763 if (t->audmode == V4L2_TUNER_MODE_MONO)
1764 va.mode = VIDEO_SOUND_MONO;
1765 else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1766 va.mode = VIDEO_SOUND_STEREO;
1767 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1768 va.mode = VIDEO_SOUND_LANG1;
1769 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1770 va.mode = VIDEO_SOUND_LANG2;
1771 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1772 if (btv->audio_hook)
1773 btv->audio_hook(btv,&va,1);
1779 case VIDIOC_G_FREQUENCY:
1781 struct v4l2_frequency *f = arg;
1783 memset(f,0,sizeof(*f));
1784 f->type = V4L2_TUNER_ANALOG_TV;
1785 f->frequency = btv->freq;
1788 case VIDIOC_S_FREQUENCY:
1790 struct v4l2_frequency *f = arg;
1792 if (unlikely(f->tuner != 0))
1794 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1797 btv->freq = f->frequency;
1798 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1799 if (btv->has_matchbox && btv->radio_user)
1800 tea5757_set_freq(btv,btv->freq);
1806 return -ENOIOCTLCMD;
1812 static int verify_window(const struct bttv_tvnorm *tvn,
1813 struct v4l2_window *win, int fixup)
1815 enum v4l2_field field;
1818 if (win->w.width < 48 || win->w.height < 32)
1820 if (win->clipcount > 2048)
1825 maxh = tvn->sheight;
1827 if (V4L2_FIELD_ANY == field) {
1828 field = (win->w.height > maxh/2)
1829 ? V4L2_FIELD_INTERLACED
1833 case V4L2_FIELD_TOP:
1834 case V4L2_FIELD_BOTTOM:
1837 case V4L2_FIELD_INTERLACED:
1843 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1846 if (win->w.width > maxw)
1847 win->w.width = maxw;
1848 if (win->w.height > maxh)
1849 win->w.height = maxh;
1854 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1855 struct v4l2_window *win, int fixup)
1857 struct v4l2_clip *clips = NULL;
1858 int n,size,retval = 0;
1860 if (NULL == fh->ovfmt)
1862 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1866 /* copy clips -- luckily v4l1 + v4l2 are binary
1867 compatible here ...*/
1869 size = sizeof(*clips)*(n+4);
1870 clips = kmalloc(size,GFP_KERNEL);
1874 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1879 /* clip against screen */
1880 if (NULL != btv->fbuf.base)
1881 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1883 btcx_sort_clips(clips,n);
1885 /* 4-byte alignments */
1886 switch (fh->ovfmt->depth) {
1889 btcx_align(&win->w, clips, n, 3);
1892 btcx_align(&win->w, clips, n, 1);
1895 /* no alignment fixups needed */
1901 down(&fh->cap.lock);
1903 kfree(fh->ov.clips);
1904 fh->ov.clips = clips;
1908 fh->ov.field = win->field;
1909 fh->ov.setup_ok = 1;
1910 btv->init.ov.w.width = win->w.width;
1911 btv->init.ov.w.height = win->w.height;
1912 btv->init.ov.field = win->field;
1914 /* update overlay if needed */
1916 if (check_btres(fh, RESOURCE_OVERLAY)) {
1917 struct bttv_buffer *new;
1919 new = videobuf_alloc(sizeof(*new));
1920 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1921 retval = bttv_switch_overlay(btv,fh,new);
1927 /* ----------------------------------------------------------------------- */
1929 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
1931 struct videobuf_queue* q = NULL;
1934 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1937 case V4L2_BUF_TYPE_VBI_CAPTURE:
1946 static int bttv_resource(struct bttv_fh *fh)
1951 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1952 res = RESOURCE_VIDEO;
1954 case V4L2_BUF_TYPE_VBI_CAPTURE:
1963 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
1965 struct videobuf_queue *q = bttv_queue(fh);
1966 int res = bttv_resource(fh);
1968 if (check_btres(fh,res))
1970 if (videobuf_queue_is_busy(q))
1976 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
1979 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1980 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
1981 f->fmt.pix.width = fh->width;
1982 f->fmt.pix.height = fh->height;
1983 f->fmt.pix.field = fh->cap.field;
1984 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1985 f->fmt.pix.bytesperline =
1986 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1987 f->fmt.pix.sizeimage =
1988 f->fmt.pix.height * f->fmt.pix.bytesperline;
1990 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1991 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
1992 f->fmt.win.w = fh->ov.w;
1993 f->fmt.win.field = fh->ov.field;
1995 case V4L2_BUF_TYPE_VBI_CAPTURE:
1996 bttv_vbi_get_fmt(fh,f);
2003 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2004 struct v4l2_format *f)
2007 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2009 const struct bttv_format *fmt;
2010 enum v4l2_field field;
2011 unsigned int maxw,maxh;
2013 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2018 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2019 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2020 field = f->fmt.pix.field;
2021 if (V4L2_FIELD_ANY == field)
2022 field = (f->fmt.pix.height > maxh/2)
2023 ? V4L2_FIELD_INTERLACED
2024 : V4L2_FIELD_BOTTOM;
2025 if (V4L2_FIELD_SEQ_BT == field)
2026 field = V4L2_FIELD_SEQ_TB;
2028 case V4L2_FIELD_TOP:
2029 case V4L2_FIELD_BOTTOM:
2030 case V4L2_FIELD_ALTERNATE:
2033 case V4L2_FIELD_INTERLACED:
2035 case V4L2_FIELD_SEQ_TB:
2036 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2043 /* update data for the application */
2044 f->fmt.pix.field = field;
2045 if (f->fmt.pix.width < 48)
2046 f->fmt.pix.width = 48;
2047 if (f->fmt.pix.height < 32)
2048 f->fmt.pix.height = 32;
2049 if (f->fmt.pix.width > maxw)
2050 f->fmt.pix.width = maxw;
2051 if (f->fmt.pix.height > maxh)
2052 f->fmt.pix.height = maxh;
2053 f->fmt.pix.bytesperline =
2054 (f->fmt.pix.width * fmt->depth) >> 3;
2055 f->fmt.pix.sizeimage =
2056 f->fmt.pix.height * f->fmt.pix.bytesperline;
2060 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2061 return verify_window(&bttv_tvnorms[btv->tvnorm],
2063 case V4L2_BUF_TYPE_VBI_CAPTURE:
2064 bttv_vbi_try_fmt(fh,f);
2071 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2072 struct v4l2_format *f)
2077 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2079 const struct bttv_format *fmt;
2081 retval = bttv_switch_type(fh,f->type);
2084 retval = bttv_try_fmt(fh,btv,f);
2087 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2089 /* update our state informations */
2090 down(&fh->cap.lock);
2092 fh->cap.field = f->fmt.pix.field;
2093 fh->cap.last = V4L2_FIELD_NONE;
2094 fh->width = f->fmt.pix.width;
2095 fh->height = f->fmt.pix.height;
2096 btv->init.fmt = fmt;
2097 btv->init.width = f->fmt.pix.width;
2098 btv->init.height = f->fmt.pix.height;
2103 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2104 return setup_window(fh, btv, &f->fmt.win, 1);
2105 case V4L2_BUF_TYPE_VBI_CAPTURE:
2106 retval = bttv_switch_type(fh,f->type);
2109 if (locked_btres(fh->btv, RESOURCE_VBI))
2111 bttv_vbi_try_fmt(fh,f);
2112 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2113 bttv_vbi_get_fmt(fh,f);
2120 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2121 unsigned int cmd, void *arg)
2123 struct bttv_fh *fh = file->private_data;
2124 struct bttv *btv = fh->btv;
2125 unsigned long flags;
2128 if (bttv_debug > 1) {
2129 switch (_IOC_TYPE(cmd)) {
2131 printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
2132 btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
2133 v4l1_ioctls[_IOC_NR(cmd)] : "???");
2136 printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
2137 btv->c.nr, cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
2140 printk("bttv%d: ioctl 0x%x (???)\n",
2145 bttv_reinit_bt848(btv);
2147 #ifdef VIDIOC_G_PRIORITY
2154 case VIDIOC_S_INPUT:
2155 case VIDIOC_S_TUNER:
2156 case VIDIOC_S_FREQUENCY:
2157 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2164 /* *** v4l1 *** ************************************************ */
2167 struct video_capability *cap = arg;
2169 memset(cap,0,sizeof(*cap));
2170 strcpy(cap->name,btv->video_dev->name);
2171 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2173 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2176 cap->type = VID_TYPE_CAPTURE|
2181 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2182 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2184 cap->minheight = 32;
2186 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2187 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2193 struct video_picture *pic = arg;
2195 memset(pic,0,sizeof(*pic));
2196 pic->brightness = btv->bright;
2197 pic->contrast = btv->contrast;
2198 pic->hue = btv->hue;
2199 pic->colour = btv->saturation;
2201 pic->depth = fh->fmt->depth;
2202 pic->palette = fh->fmt->palette;
2208 struct video_picture *pic = arg;
2209 const struct bttv_format *fmt;
2211 fmt = format_by_palette(pic->palette);
2214 down(&fh->cap.lock);
2215 if (fmt->depth != pic->depth) {
2217 goto fh_unlock_and_return;
2221 btv->init.ovfmt = fmt;
2222 btv->init.fmt = fmt;
2224 /* dirty hack time: swap bytes for overlay if the
2225 display adaptor is big endian (insmod option) */
2226 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2227 fmt->palette == VIDEO_PALETTE_RGB565 ||
2228 fmt->palette == VIDEO_PALETTE_RGB32) {
2232 bt848_bright(btv,pic->brightness);
2233 bt848_contrast(btv,pic->contrast);
2234 bt848_hue(btv,pic->hue);
2235 bt848_sat(btv,pic->colour);
2242 struct video_window *win = arg;
2244 memset(win,0,sizeof(*win));
2245 win->x = fh->ov.w.left;
2246 win->y = fh->ov.w.top;
2247 win->width = fh->ov.w.width;
2248 win->height = fh->ov.w.height;
2253 struct video_window *win = arg;
2254 struct v4l2_window w2;
2256 w2.field = V4L2_FIELD_ANY;
2259 w2.w.width = win->width;
2260 w2.w.height = win->height;
2261 w2.clipcount = win->clipcount;
2262 w2.clips = (struct v4l2_clip*)win->clips;
2263 retval = setup_window(fh, btv, &w2, 0);
2265 /* on v4l1 this ioctl affects the read() size too */
2266 fh->width = fh->ov.w.width;
2267 fh->height = fh->ov.w.height;
2268 btv->init.width = fh->ov.w.width;
2269 btv->init.height = fh->ov.w.height;
2276 struct video_buffer *fbuf = arg;
2278 fbuf->base = btv->fbuf.base;
2279 fbuf->width = btv->fbuf.fmt.width;
2280 fbuf->height = btv->fbuf.fmt.height;
2281 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2283 fbuf->depth = fh->ovfmt->depth;
2288 struct video_buffer *fbuf = arg;
2289 const struct bttv_format *fmt;
2292 if(!capable(CAP_SYS_ADMIN) &&
2293 !capable(CAP_SYS_RAWIO))
2295 end = (unsigned long)fbuf->base +
2296 fbuf->height * fbuf->bytesperline;
2297 down(&fh->cap.lock);
2300 switch (fbuf->depth) {
2302 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2305 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2308 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2311 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2315 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2322 goto fh_unlock_and_return;
2326 btv->init.ovfmt = fmt;
2327 btv->init.fmt = fmt;
2328 btv->fbuf.base = fbuf->base;
2329 btv->fbuf.fmt.width = fbuf->width;
2330 btv->fbuf.fmt.height = fbuf->height;
2331 if (fbuf->bytesperline)
2332 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2334 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2340 case VIDIOC_OVERLAY:
2342 struct bttv_buffer *new;
2347 if (NULL == btv->fbuf.base)
2349 if (!fh->ov.setup_ok) {
2350 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2355 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2358 down(&fh->cap.lock);
2360 fh->ov.tvnorm = btv->tvnorm;
2361 new = videobuf_alloc(sizeof(*new));
2362 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2368 retval = bttv_switch_overlay(btv,fh,new);
2375 struct video_mbuf *mbuf = arg;
2378 down(&fh->cap.lock);
2379 retval = videobuf_mmap_setup(file,&fh->cap,gbuffers,gbufsize,
2382 goto fh_unlock_and_return;
2383 memset(mbuf,0,sizeof(*mbuf));
2384 mbuf->frames = gbuffers;
2385 mbuf->size = gbuffers * gbufsize;
2386 for (i = 0; i < gbuffers; i++)
2387 mbuf->offsets[i] = i * gbufsize;
2391 case VIDIOCMCAPTURE:
2393 struct video_mmap *vm = arg;
2394 struct bttv_buffer *buf;
2395 enum v4l2_field field;
2397 if (vm->frame >= VIDEO_MAX_FRAME)
2400 down(&fh->cap.lock);
2402 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2404 goto fh_unlock_and_return;
2405 if (0 == buf->vb.baddr)
2406 goto fh_unlock_and_return;
2407 if (buf->vb.state == STATE_QUEUED ||
2408 buf->vb.state == STATE_ACTIVE)
2409 goto fh_unlock_and_return;
2411 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2412 ? V4L2_FIELD_INTERLACED
2413 : V4L2_FIELD_BOTTOM;
2414 retval = bttv_prepare_buffer(btv,buf,
2415 format_by_palette(vm->format),
2416 vm->width,vm->height,field);
2418 goto fh_unlock_and_return;
2419 spin_lock_irqsave(&btv->s_lock,flags);
2420 buffer_queue(file,&buf->vb);
2421 spin_unlock_irqrestore(&btv->s_lock,flags);
2428 struct bttv_buffer *buf;
2430 if (*frame >= VIDEO_MAX_FRAME)
2433 down(&fh->cap.lock);
2435 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2437 goto fh_unlock_and_return;
2438 retval = videobuf_waiton(&buf->vb,0,1);
2440 goto fh_unlock_and_return;
2441 switch (buf->vb.state) {
2446 videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
2447 bttv_dma_free(btv,buf);
2459 struct vbi_format *fmt = (void *) arg;
2460 struct v4l2_format fmt2;
2462 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2463 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2467 bttv_vbi_get_fmt(fh, &fmt2);
2469 memset(fmt,0,sizeof(*fmt));
2470 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2471 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2472 fmt->sample_format = VIDEO_PALETTE_RAW;
2473 fmt->start[0] = fmt2.fmt.vbi.start[0];
2474 fmt->count[0] = fmt2.fmt.vbi.count[0];
2475 fmt->start[1] = fmt2.fmt.vbi.start[1];
2476 fmt->count[1] = fmt2.fmt.vbi.count[1];
2477 if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
2478 fmt->flags |= V4L2_VBI_UNSYNC;
2479 if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
2480 fmt->flags |= V4L2_VBI_INTERLACED;
2485 struct vbi_format *fmt = (void *) arg;
2486 struct v4l2_format fmt2;
2488 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2491 bttv_vbi_get_fmt(fh, &fmt2);
2493 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2494 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2495 fmt->sample_format != VIDEO_PALETTE_RAW ||
2496 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2497 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2498 fmt->count[0] != fmt->count[1] ||
2499 fmt->count[0] < 1 ||
2500 fmt->count[0] > 32 /* VBI_MAXLINES */)
2503 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2516 return bttv_common_ioctls(btv,cmd,arg);
2518 /* *** v4l2 *** ************************************************ */
2519 case VIDIOC_QUERYCAP:
2521 struct v4l2_capability *cap = arg;
2525 strcpy(cap->driver,"bttv");
2526 strlcpy(cap->card,btv->video_dev->name,sizeof(cap->card));
2527 sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
2528 cap->version = BTTV_VERSION_CODE;
2530 V4L2_CAP_VIDEO_CAPTURE |
2531 V4L2_CAP_VIDEO_OVERLAY |
2532 V4L2_CAP_VBI_CAPTURE |
2534 V4L2_CAP_READWRITE |
2539 case VIDIOC_ENUM_FMT:
2541 struct v4l2_fmtdesc *f = arg;
2542 enum v4l2_buf_type type;
2547 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2552 memset(f,0,sizeof(*f));
2555 f->pixelformat = V4L2_PIX_FMT_GREY;
2556 strcpy(f->description,"vbi data");
2560 /* video capture + overlay */
2562 for (i = 0; i < BTTV_FORMATS; i++) {
2563 if (bttv_formats[i].fourcc != -1)
2565 if ((unsigned int)index == f->index)
2568 if (BTTV_FORMATS == i)
2572 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2574 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2575 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2581 memset(f,0,sizeof(*f));
2584 f->pixelformat = bttv_formats[i].fourcc;
2585 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2589 case VIDIOC_TRY_FMT:
2591 struct v4l2_format *f = arg;
2592 return bttv_try_fmt(fh,btv,f);
2596 struct v4l2_format *f = arg;
2597 return bttv_g_fmt(fh,f);
2601 struct v4l2_format *f = arg;
2602 return bttv_s_fmt(fh,btv,f);
2607 struct v4l2_framebuffer *fb = arg;
2610 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2612 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2617 struct v4l2_framebuffer *fb = arg;
2618 const struct bttv_format *fmt;
2620 if(!capable(CAP_SYS_ADMIN) &&
2621 !capable(CAP_SYS_RAWIO))
2625 fmt = format_by_fourcc(fb->fmt.pixelformat);
2628 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2631 down(&fh->cap.lock);
2633 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2634 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2635 goto fh_unlock_and_return;
2636 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2637 goto fh_unlock_and_return;
2641 btv->fbuf.base = fb->base;
2642 btv->fbuf.fmt.width = fb->fmt.width;
2643 btv->fbuf.fmt.height = fb->fmt.height;
2644 if (0 != fb->fmt.bytesperline)
2645 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2647 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2651 btv->init.ovfmt = fmt;
2652 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2655 fh->ov.w.width = fb->fmt.width;
2656 fh->ov.w.height = fb->fmt.height;
2657 btv->init.ov.w.width = fb->fmt.width;
2658 btv->init.ov.w.height = fb->fmt.height;
2660 kfree(fh->ov.clips);
2661 fh->ov.clips = NULL;
2664 if (check_btres(fh, RESOURCE_OVERLAY)) {
2665 struct bttv_buffer *new;
2667 new = videobuf_alloc(sizeof(*new));
2668 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2669 retval = bttv_switch_overlay(btv,fh,new);
2676 case VIDIOC_REQBUFS:
2677 return videobuf_reqbufs(file,bttv_queue(fh),arg);
2679 case VIDIOC_QUERYBUF:
2680 return videobuf_querybuf(bttv_queue(fh),arg);
2683 return videobuf_qbuf(file,bttv_queue(fh),arg);
2686 return videobuf_dqbuf(file,bttv_queue(fh),arg);
2688 case VIDIOC_STREAMON:
2690 int res = bttv_resource(fh);
2692 if (!check_alloc_btres(btv,fh,res))
2694 return videobuf_streamon(file,bttv_queue(fh));
2696 case VIDIOC_STREAMOFF:
2698 int res = bttv_resource(fh);
2700 retval = videobuf_streamoff(file,bttv_queue(fh));
2703 free_btres(btv,fh,res);
2707 case VIDIOC_QUERYCTRL:
2709 struct v4l2_queryctrl *c = arg;
2712 if ((c->id < V4L2_CID_BASE ||
2713 c->id >= V4L2_CID_LASTP1) &&
2714 (c->id < V4L2_CID_PRIVATE_BASE ||
2715 c->id >= V4L2_CID_PRIVATE_LASTP1))
2717 for (i = 0; i < BTTV_CTLS; i++)
2718 if (bttv_ctls[i].id == c->id)
2720 if (i == BTTV_CTLS) {
2725 if (i >= 4 && i <= 8) {
2726 struct video_audio va;
2727 memset(&va,0,sizeof(va));
2728 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2729 if (btv->audio_hook)
2730 btv->audio_hook(btv,&va,0);
2731 switch (bttv_ctls[i].id) {
2732 case V4L2_CID_AUDIO_VOLUME:
2733 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2736 case V4L2_CID_AUDIO_BALANCE:
2737 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2740 case V4L2_CID_AUDIO_BASS:
2741 if (!(va.flags & VIDEO_AUDIO_BASS))
2744 case V4L2_CID_AUDIO_TREBLE:
2745 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2753 return get_control(btv,arg);
2755 return set_control(btv,arg);
2758 struct v4l2_streamparm *parm = arg;
2759 struct v4l2_standard s;
2760 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2762 memset(parm,0,sizeof(*parm));
2763 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2764 bttv_tvnorms[btv->tvnorm].name);
2765 parm->parm.capture.timeperframe = s.frameperiod;
2769 #ifdef VIDIOC_G_PRIORITY
2770 case VIDIOC_G_PRIORITY:
2772 enum v4l2_priority *p = arg;
2774 *p = v4l2_prio_max(&btv->prio);
2777 case VIDIOC_S_PRIORITY:
2779 enum v4l2_priority *prio = arg;
2781 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2786 case VIDIOC_ENUMSTD:
2789 case VIDIOC_ENUMINPUT:
2790 case VIDIOC_G_INPUT:
2791 case VIDIOC_S_INPUT:
2792 case VIDIOC_G_TUNER:
2793 case VIDIOC_S_TUNER:
2794 case VIDIOC_G_FREQUENCY:
2795 case VIDIOC_S_FREQUENCY:
2796 return bttv_common_ioctls(btv,cmd,arg);
2799 return -ENOIOCTLCMD;
2803 fh_unlock_and_return:
2808 static int bttv_ioctl(struct inode *inode, struct file *file,
2809 unsigned int cmd, unsigned long arg)
2811 struct bttv_fh *fh = file->private_data;
2815 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2816 return fh->lines * 2 * 2048;
2818 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2822 static ssize_t bttv_read(struct file *file, char *data,
2823 size_t count, loff_t *ppos)
2825 struct bttv_fh *fh = file->private_data;
2828 if (fh->btv->errors)
2829 bttv_reinit_bt848(fh->btv);
2830 dprintk("bttv%d: read count=%d type=%s\n",
2831 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2834 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2835 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2837 retval = videobuf_read_one(file, &fh->cap, data, count, ppos);
2839 case V4L2_BUF_TYPE_VBI_CAPTURE:
2840 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2842 retval = videobuf_read_stream(file, &fh->vbi, data, count, ppos, 1);
2850 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2852 struct bttv_fh *fh = file->private_data;
2853 struct bttv_buffer *buf;
2854 enum v4l2_field field;
2856 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2857 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2859 return videobuf_poll_stream(file, &fh->vbi, wait);
2862 if (check_btres(fh,RESOURCE_VIDEO)) {
2863 /* streaming capture */
2864 if (list_empty(&fh->cap.stream))
2866 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2868 /* read() capture */
2869 down(&fh->cap.lock);
2870 if (NULL == fh->cap.read_buf) {
2871 /* need to capture a new frame */
2872 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2876 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2877 if (NULL == fh->cap.read_buf) {
2881 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2882 field = videobuf_next_field(&fh->cap);
2883 if (0 != fh->cap.ops->buf_prepare(file,fh->cap.read_buf,field)) {
2887 fh->cap.ops->buf_queue(file,fh->cap.read_buf);
2888 fh->cap.read_off = 0;
2891 buf = (struct bttv_buffer*)fh->cap.read_buf;
2894 poll_wait(file, &buf->vb.done, wait);
2895 if (buf->vb.state == STATE_DONE ||
2896 buf->vb.state == STATE_ERROR)
2897 return POLLIN|POLLRDNORM;
2901 static int bttv_open(struct inode *inode, struct file *file)
2903 int minor = iminor(inode);
2904 struct bttv *btv = NULL;
2906 enum v4l2_buf_type type = 0;
2909 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
2911 for (i = 0; i < bttv_num; i++) {
2912 if (bttvs[i].video_dev &&
2913 bttvs[i].video_dev->minor == minor) {
2915 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2918 if (bttvs[i].vbi_dev &&
2919 bttvs[i].vbi_dev->minor == minor) {
2921 type = V4L2_BUF_TYPE_VBI_CAPTURE;
2928 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
2929 btv->c.nr,v4l2_type_names[type]);
2931 /* allocate per filehandle data */
2932 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
2935 file->private_data = fh;
2938 fh->ov.setup_ok = 0;
2939 #ifdef VIDIOC_G_PRIORITY
2940 v4l2_prio_open(&btv->prio,&fh->prio);
2943 videobuf_queue_init(&fh->cap, &bttv_video_qops,
2944 btv->c.pci, &btv->s_lock,
2945 V4L2_BUF_TYPE_VIDEO_CAPTURE,
2946 V4L2_FIELD_INTERLACED,
2947 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));
2953 i2c_vidiocschan(btv);
2956 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
2957 bttv_vbi_setlines(fh,btv,16);
2958 bttv_field_count(btv);
2962 static int bttv_release(struct inode *inode, struct file *file)
2964 struct bttv_fh *fh = file->private_data;
2965 struct bttv *btv = fh->btv;
2967 /* turn off overlay */
2968 if (check_btres(fh, RESOURCE_OVERLAY))
2969 bttv_switch_overlay(btv,fh,NULL);
2971 /* stop video capture */
2972 if (check_btres(fh, RESOURCE_VIDEO)) {
2973 videobuf_streamoff(file,&fh->cap);
2974 free_btres(btv,fh,RESOURCE_VIDEO);
2976 if (fh->cap.read_buf) {
2977 buffer_release(file,fh->cap.read_buf);
2978 kfree(fh->cap.read_buf);
2981 /* stop vbi capture */
2982 if (check_btres(fh, RESOURCE_VBI)) {
2983 if (fh->vbi.streaming)
2984 videobuf_streamoff(file,&fh->vbi);
2985 if (fh->vbi.reading)
2986 videobuf_read_stop(file,&fh->vbi);
2987 free_btres(btv,fh,RESOURCE_VBI);
2990 #ifdef VIDIOC_G_PRIORITY
2991 v4l2_prio_close(&btv->prio,&fh->prio);
2993 file->private_data = NULL;
2997 bttv_field_count(btv);
3002 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3004 struct bttv_fh *fh = file->private_data;
3006 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3007 fh->btv->c.nr, v4l2_type_names[fh->type],
3008 vma->vm_start, vma->vm_end - vma->vm_start);
3009 return videobuf_mmap_mapper(vma,bttv_queue(fh));
3012 static struct file_operations bttv_fops =
3014 .owner = THIS_MODULE,
3016 .release = bttv_release,
3017 .ioctl = bttv_ioctl,
3018 .llseek = no_llseek,
3024 static struct video_device bttv_video_template =
3027 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
3028 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3029 .hardware = VID_HARDWARE_BT848,
3034 struct video_device bttv_vbi_template =
3036 .name = "bt848/878 vbi",
3037 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3038 .hardware = VID_HARDWARE_BT848,
3043 /* ----------------------------------------------------------------------- */
3044 /* radio interface */
3046 static int radio_open(struct inode *inode, struct file *file)
3048 int minor = iminor(inode);
3049 struct bttv *btv = NULL;
3052 dprintk("bttv: open minor=%d\n",minor);
3054 for (i = 0; i < bttv_num; i++) {
3055 if (bttvs[i].radio_dev->minor == minor) {
3063 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3065 if (btv->radio_user) {
3070 file->private_data = btv;
3072 i2c_vidiocschan(btv);
3073 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
3074 audio_mux(btv,AUDIO_RADIO);
3080 static int radio_release(struct inode *inode, struct file *file)
3082 struct bttv *btv = file->private_data;
3088 static int radio_do_ioctl(struct inode *inode, struct file *file,
3089 unsigned int cmd, void *arg)
3091 struct bttv *btv = file->private_data;
3096 struct video_capability *cap = arg;
3098 memset(cap,0,sizeof(*cap));
3099 strcpy(cap->name,btv->radio_dev->name);
3100 cap->type = VID_TYPE_TUNER;
3108 struct video_tuner *v = arg;
3112 memset(v,0,sizeof(*v));
3113 strcpy(v->name, "Radio");
3114 /* japan: 76.0 MHz - 89.9 MHz
3115 western europe: 87.5 MHz - 108.0 MHz
3116 russia: 65.0 MHz - 108.0 MHz */
3117 v->rangelow=(int)(65*16);
3118 v->rangehigh=(int)(108*16);
3119 bttv_call_i2c_clients(btv,cmd,v);
3131 return bttv_common_ioctls(btv,cmd,arg);
3134 return -ENOIOCTLCMD;
3139 static int radio_ioctl(struct inode *inode, struct file *file,
3140 unsigned int cmd, unsigned long arg)
3142 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3145 static struct file_operations radio_fops =
3147 .owner = THIS_MODULE,
3149 .release = radio_release,
3150 .ioctl = radio_ioctl,
3151 .llseek = no_llseek,
3154 static struct video_device radio_template =
3156 .name = "bt848/878 radio",
3157 .type = VID_TYPE_TUNER,
3158 .hardware = VID_HARDWARE_BT848,
3159 .fops = &radio_fops,
3163 /* ----------------------------------------------------------------------- */
3166 static char *irq_name[] = {
3167 "FMTCHG", // format change detected (525 vs. 625)
3168 "VSYNC", // vertical sync (new field)
3169 "HSYNC", // horizontal sync
3170 "OFLOW", // chroma/luma AGC overflow
3171 "HLOCK", // horizontal lock changed
3172 "VPRES", // video presence changed
3174 "I2CDONE", // hw irc operation finished
3175 "GPINT", // gpio port triggered irq
3177 "RISCI", // risc instruction triggered irq
3178 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3179 "FTRGT", // pixel data fifo overrun
3180 "FDSR", // fifo data stream resyncronisation
3181 "PPERR", // parity error (data transfer)
3182 "RIPERR", // parity error (read risc instructions)
3183 "PABORT", // pci abort
3184 "OCERR", // risc instruction error
3185 "SCERR", // syncronisation error
3188 static void bttv_print_irqbits(u32 print, u32 mark)
3193 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3194 if (print & (1 << i))
3195 printk(" %s",irq_name[i]);
3196 if (mark & (1 << i))
3201 static void bttv_print_riscaddr(struct bttv *btv)
3203 printk(" main: %08Lx\n",
3204 (unsigned long long)btv->main.dma);
3205 printk(" vbi : o=%08Lx e=%08Lx\n",
3206 btv->curr.vbi ? (unsigned long long)btv->curr.vbi->top.dma : 0,
3207 btv->curr.vbi ? (unsigned long long)btv->curr.vbi->bottom.dma : 0);
3208 printk(" cap : o=%08Lx e=%08Lx\n",
3209 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3210 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3211 printk(" scr : o=%08Lx e=%08Lx\n",
3212 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3213 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3216 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3218 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3220 (unsigned long)btv->main.dma,
3221 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3222 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3225 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3226 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3227 "Ok, then this is harmless, don't worry ;)",
3231 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3233 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3239 bttv_irq_next_set(struct bttv *btv, struct bttv_buffer_set *set)
3241 struct bttv_buffer *item;
3243 memset(set,0,sizeof(*set));
3246 if (!list_empty(&btv->vcapture)) {
3248 set->vbi = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
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 vbi=%p "
3299 "[screen=%p,irq=%d,%d]\n",
3300 btv->c.nr,set->top, set->bottom, set->vbi,
3301 btv->screen,set->irqflags,set->topirq);
3306 bttv_irq_wakeup_set(struct bttv *btv, struct bttv_buffer_set *wakeup,
3307 struct bttv_buffer_set *curr, unsigned int state)
3311 do_gettimeofday(&ts);
3313 if (NULL != wakeup->vbi) {
3314 wakeup->vbi->vb.ts = ts;
3315 wakeup->vbi->vb.field_count = btv->field_count;
3316 wakeup->vbi->vb.state = state;
3317 wake_up(&wakeup->vbi->vb.done);
3319 if (wakeup->top == wakeup->bottom) {
3320 if (NULL != wakeup->top && curr->top != wakeup->top) {
3322 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3323 wakeup->top->vb.ts = ts;
3324 wakeup->top->vb.field_count = btv->field_count;
3325 wakeup->top->vb.state = state;
3326 wake_up(&wakeup->top->vb.done);
3329 if (NULL != wakeup->top && curr->top != wakeup->top) {
3331 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3332 wakeup->top->vb.ts = ts;
3333 wakeup->top->vb.field_count = btv->field_count;
3334 wakeup->top->vb.state = state;
3335 wake_up(&wakeup->top->vb.done);
3337 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3339 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3340 wakeup->bottom->vb.ts = ts;
3341 wakeup->bottom->vb.field_count = btv->field_count;
3342 wakeup->bottom->vb.state = state;
3343 wake_up(&wakeup->bottom->vb.done);
3348 static void bttv_irq_timeout(unsigned long data)
3350 struct bttv *btv = (struct bttv *)data;
3351 struct bttv_buffer_set old,new;
3352 struct bttv_buffer *item;
3353 unsigned long flags;
3356 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3357 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3358 btread(BT848_RISC_COUNT));
3359 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3363 spin_lock_irqsave(&btv->s_lock,flags);
3365 /* deactivate stuff */
3366 memset(&new,0,sizeof(new));
3369 bttv_buffer_set_activate(btv, &new);
3370 bttv_set_dma(btv, 0, 0);
3373 bttv_irq_wakeup_set(btv, &old, &new, STATE_ERROR);
3375 /* cancel all outstanding capture / vbi requests */
3376 while (!list_empty(&btv->capture)) {
3377 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3378 list_del(&item->vb.queue);
3379 item->vb.state = STATE_ERROR;
3380 wake_up(&item->vb.done);
3382 while (!list_empty(&btv->vcapture)) {
3383 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3384 list_del(&item->vb.queue);
3385 item->vb.state = STATE_ERROR;
3386 wake_up(&item->vb.done);
3390 spin_unlock_irqrestore(&btv->s_lock,flags);
3394 bttv_irq_wakeup_top(struct bttv *btv)
3396 struct bttv_buffer *wakeup = btv->curr.top;
3401 spin_lock(&btv->s_lock);
3402 btv->curr.topirq = 0;
3403 btv->curr.top = NULL;
3404 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3406 do_gettimeofday(&wakeup->vb.ts);
3407 wakeup->vb.field_count = btv->field_count;
3408 wakeup->vb.state = STATE_DONE;
3409 wake_up(&wakeup->vb.done);
3410 spin_unlock(&btv->s_lock);
3414 bttv_irq_switch_fields(struct bttv *btv)
3416 struct bttv_buffer_set new;
3417 struct bttv_buffer_set old;
3420 spin_lock(&btv->s_lock);
3422 /* new buffer set */
3423 bttv_irq_next_set(btv, &new);
3424 rc = btread(BT848_RISC_COUNT);
3425 if (rc < btv->main.dma || rc > btv->main.dma + 0x100) {
3428 bttv_irq_debug_low_latency(btv, rc);
3429 spin_unlock(&btv->s_lock);
3436 bttv_buffer_set_activate(btv, &new);
3437 bttv_set_dma(btv, 0, new.irqflags);
3440 if (UNSET != btv->new_input) {
3441 video_mux(btv,btv->new_input);
3442 btv->new_input = UNSET;
3445 /* wake up finished buffers */
3446 bttv_irq_wakeup_set(btv, &old, &new, STATE_DONE);
3447 spin_unlock(&btv->s_lock);
3450 static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3458 btv=(struct bttv *)dev_id;
3461 /* get/clear interrupt status bits */
3462 stat=btread(BT848_INT_STAT);
3463 astat=stat&btread(BT848_INT_MASK);
3467 btwrite(stat,BT848_INT_STAT);
3469 /* get device status bits */
3470 dstat=btread(BT848_DSTATUS);
3473 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3474 "riscs=%x, riscc=%08x, ",
3475 btv->c.nr, count, btv->field_count,
3476 stat>>28, btread(BT848_RISC_COUNT));
3477 bttv_print_irqbits(stat,astat);
3478 if (stat & BT848_INT_HLOCK)
3479 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3481 if (stat & BT848_INT_VPRES)
3482 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3484 if (stat & BT848_INT_FMTCHG)
3485 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3490 if (astat&BT848_INT_VSYNC)
3493 if (astat & BT848_INT_GPINT) {
3494 wake_up(&btv->gpioq);
3495 bttv_gpio_irq(&btv->c);
3498 if (astat & BT848_INT_I2CDONE) {
3499 btv->i2c_done = stat;
3500 wake_up(&btv->i2c_queue);
3503 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3504 bttv_irq_wakeup_top(btv);
3506 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3507 bttv_irq_switch_fields(btv);
3509 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3512 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3513 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3514 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3515 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3516 btread(BT848_RISC_COUNT));
3517 bttv_print_irqbits(stat,astat);
3520 bttv_print_riscaddr(btv);
3522 if (fdsr && astat & BT848_INT_FDSR) {
3523 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3524 btv->c.nr,btread(BT848_RISC_COUNT));
3526 bttv_print_riscaddr(btv);
3531 btwrite(0, BT848_INT_MASK);
3533 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3534 bttv_print_irqbits(stat,astat);
3541 return IRQ_RETVAL(handled);
3545 /* ----------------------------------------------------------------------- */
3546 /* initialitation */
3548 static struct video_device *vdev_init(struct bttv *btv,
3549 struct video_device *template,
3552 struct video_device *vfd;
3554 vfd = video_device_alloc();
3559 vfd->dev = &btv->c.pci->dev;
3560 vfd->release = video_device_release;
3561 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3562 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3563 type, bttv_tvcards[btv->c.type].name);
3567 static void bttv_unregister_video(struct bttv *btv)
3569 if (btv->video_dev) {
3570 if (-1 != btv->video_dev->minor)
3571 video_unregister_device(btv->video_dev);
3573 video_device_release(btv->video_dev);
3574 btv->video_dev = NULL;
3577 if (-1 != btv->vbi_dev->minor)
3578 video_unregister_device(btv->vbi_dev);
3580 video_device_release(btv->vbi_dev);
3581 btv->vbi_dev = NULL;
3583 if (btv->radio_dev) {
3584 if (-1 != btv->radio_dev->minor)
3585 video_unregister_device(btv->radio_dev);
3587 video_device_release(btv->radio_dev);
3588 btv->radio_dev = NULL;
3592 /* register video4linux devices */
3593 static int __devinit bttv_register_video(struct bttv *btv)
3596 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3597 if (NULL == btv->video_dev)
3599 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3601 printk(KERN_INFO "bttv%d: registered device video%d\n",
3602 btv->c.nr,btv->video_dev->minor & 0x1f);
3603 video_device_create_file(btv->video_dev, &class_device_attr_card);
3606 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3607 if (NULL == btv->vbi_dev)
3609 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3611 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3612 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3614 if (!btv->has_radio)
3617 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3618 if (NULL == btv->radio_dev)
3620 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3622 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3623 btv->c.nr,btv->radio_dev->minor & 0x1f);
3629 bttv_unregister_video(btv);
3634 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3635 /* response on cards with no firmware is not enabled by OF */
3636 static void pci_set_command(struct pci_dev *dev)
3638 #if defined(__powerpc__)
3641 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3642 cmd = (cmd | PCI_COMMAND_MEMORY );
3643 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3647 static int __devinit bttv_probe(struct pci_dev *dev,
3648 const struct pci_device_id *pci_id)
3654 if (bttv_num == BTTV_MAX)
3656 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3657 btv=&bttvs[bttv_num];
3658 memset(btv,0,sizeof(*btv));
3659 btv->c.nr = bttv_num;
3660 sprintf(btv->c.name,"bttv%d",btv->c.nr);
3662 /* initialize structs / fill in defaults */
3663 init_MUTEX(&btv->lock);
3664 init_MUTEX(&btv->reslock);
3665 btv->s_lock = SPIN_LOCK_UNLOCKED;
3666 btv->gpio_lock = SPIN_LOCK_UNLOCKED;
3667 init_waitqueue_head(&btv->gpioq);
3668 init_waitqueue_head(&btv->i2c_queue);
3669 INIT_LIST_HEAD(&btv->c.subs);
3670 INIT_LIST_HEAD(&btv->capture);
3671 INIT_LIST_HEAD(&btv->vcapture);
3672 #ifdef VIDIOC_G_PRIORITY
3673 v4l2_prio_init(&btv->prio);
3676 init_timer(&btv->timeout);
3677 btv->timeout.function = bttv_irq_timeout;
3678 btv->timeout.data = (unsigned long)btv;
3681 btv->tuner_type = UNSET;
3682 btv->pinnacle_id = UNSET;
3683 btv->new_input = UNSET;
3685 btv->has_radio=radio[btv->c.nr];
3687 /* pci stuff (init, get irq/mmio, ... */
3689 btv->id = dev->device;
3690 if (pci_enable_device(dev)) {
3691 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3695 if (pci_set_dma_mask(dev, 0xffffffff)) {
3696 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3700 if (!request_mem_region(pci_resource_start(dev,0),
3701 pci_resource_len(dev,0),
3703 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3704 btv->c.nr, pci_resource_start(dev,0));
3707 pci_set_master(dev);
3708 pci_set_command(dev);
3709 pci_set_drvdata(dev,btv);
3710 if (!pci_dma_supported(dev,0xffffffff)) {
3711 printk("bttv%d: Oops: no 32bit PCI DMA ???\n", btv->c.nr);
3716 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3717 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3718 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3719 bttv_num,btv->id, btv->revision, pci_name(dev));
3720 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3721 btv->c.pci->irq, lat, pci_resource_start(dev,0));
3724 btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3725 if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3726 printk("bttv%d: ioremap() failed\n", btv->c.nr);
3734 /* disable irqs, register irq handler */
3735 btwrite(0, BT848_INT_MASK);
3736 result = request_irq(btv->c.pci->irq, bttv_irq,
3737 SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
3739 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
3740 bttv_num,btv->c.pci->irq);
3744 if (0 != bttv_handle_chipset(btv)) {
3749 /* init options from insmod args */
3750 btv->opt_combfilter = combfilter;
3751 btv->opt_lumafilter = lumafilter;
3752 btv->opt_automute = automute;
3753 btv->opt_chroma_agc = chroma_agc;
3754 btv->opt_adc_crush = adc_crush;
3755 btv->opt_vcr_hack = vcr_hack;
3756 btv->opt_whitecrush_upper = whitecrush_upper;
3757 btv->opt_whitecrush_lower = whitecrush_lower;
3759 /* fill struct bttv with some useful defaults */
3760 btv->init.btv = btv;
3761 btv->init.ov.w.width = 320;
3762 btv->init.ov.w.height = 240;
3763 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
3764 btv->init.width = 320;
3765 btv->init.height = 240;
3766 btv->init.lines = 16;
3769 /* initialize hardware */
3771 bttv_gpio_tracking(btv,"pre-init");
3773 bttv_risc_init_main(btv);
3777 btwrite(0x00, BT848_GPIO_REG_INP);
3778 btwrite(0x00, BT848_GPIO_OUT_EN);
3780 bttv_gpio_tracking(btv,"init");
3782 /* needs to be done before i2c is registered */
3783 bttv_init_card1(btv);
3785 /* register i2c + gpio */
3788 /* some card-specific stuff (needs working i2c) */
3789 bttv_init_card2(btv);
3792 /* register video4linux + input */
3793 if (!bttv_tvcards[btv->c.type].no_video) {
3794 bttv_register_video(btv);
3795 bt848_bright(btv,32768);
3796 bt848_contrast(btv,32768);
3797 bt848_hue(btv,32768);
3798 bt848_sat(btv,32768);
3799 audio_mux(btv,AUDIO_MUTE);
3803 /* add subdevices */
3804 if (btv->has_remote)
3805 bttv_sub_add_device(&btv->c, "remote");
3806 if (bttv_tvcards[btv->c.type].has_dvb)
3807 bttv_sub_add_device(&btv->c, "dvb");
3809 /* everything is fine */
3814 free_irq(btv->c.pci->irq,btv);
3817 if (btv->bt848_mmio)
3818 iounmap(btv->bt848_mmio);
3819 release_mem_region(pci_resource_start(btv->c.pci,0),
3820 pci_resource_len(btv->c.pci,0));
3821 pci_set_drvdata(dev,NULL);
3825 static void __devexit bttv_remove(struct pci_dev *pci_dev)
3827 struct bttv *btv = pci_get_drvdata(pci_dev);
3830 printk("bttv%d: unloading\n",btv->c.nr);
3832 /* shutdown everything (DMA+IRQs) */
3833 btand(~15, BT848_GPIO_DMA_CTL);
3834 btwrite(0, BT848_INT_MASK);
3835 btwrite(~0x0, BT848_INT_STAT);
3836 btwrite(0x0, BT848_GPIO_OUT_EN);
3838 bttv_gpio_tracking(btv,"cleanup");
3840 /* tell gpio modules we are leaving ... */
3842 wake_up(&btv->gpioq);
3843 bttv_sub_del_devices(&btv->c);
3845 /* unregister i2c_bus + input */
3848 /* unregister video4linux */
3849 bttv_unregister_video(btv);
3851 /* free allocated memory */
3852 btcx_riscmem_free(btv->c.pci,&btv->main);
3854 /* free ressources */
3855 free_irq(btv->c.pci->irq,btv);
3856 iounmap(btv->bt848_mmio);
3857 release_mem_region(pci_resource_start(btv->c.pci,0),
3858 pci_resource_len(btv->c.pci,0));
3860 pci_set_drvdata(pci_dev, NULL);
3864 static int bttv_suspend(struct pci_dev *pci_dev, u32 state)
3866 struct bttv *btv = pci_get_drvdata(pci_dev);
3867 struct bttv_buffer_set idle;
3868 unsigned long flags;
3870 printk("bttv%d: suspend %d\n", btv->c.nr, state);
3872 /* stop dma + irqs */
3873 spin_lock_irqsave(&btv->s_lock,flags);
3874 memset(&idle, 0, sizeof(idle));
3875 btv->state.set = btv->curr;
3877 bttv_buffer_set_activate(btv, &idle);
3878 bttv_set_dma(btv, 0, 0);
3879 btwrite(0, BT848_INT_MASK);
3880 spin_unlock_irqrestore(&btv->s_lock,flags);
3882 /* save bt878 state */
3883 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
3884 btv->state.gpio_data = gpio_read();
3886 /* save pci state */
3887 pci_save_state(pci_dev, btv->state.pci_cfg);
3888 if (0 != pci_set_power_state(pci_dev, state)) {
3889 pci_disable_device(pci_dev);
3890 btv->state.disabled = 1;
3895 static int bttv_resume(struct pci_dev *pci_dev)
3897 struct bttv *btv = pci_get_drvdata(pci_dev);
3898 unsigned long flags;
3900 printk("bttv%d: resume\n", btv->c.nr);
3902 /* restore pci state */
3903 if (btv->state.disabled) {
3904 pci_enable_device(pci_dev);
3905 btv->state.disabled = 0;
3907 pci_set_power_state(pci_dev, 0);
3908 pci_restore_state(pci_dev, btv->state.pci_cfg);
3910 /* restore bt878 state */
3911 bttv_reinit_bt848(btv);
3912 gpio_inout(0xffffff, btv->state.gpio_enable);
3913 gpio_write(btv->state.gpio_data);
3916 spin_lock_irqsave(&btv->s_lock,flags);
3917 btv->curr = btv->state.set;
3918 bttv_buffer_set_activate(btv, &btv->curr);
3919 bttv_set_dma(btv, 0, btv->curr.irqflags);
3920 spin_unlock_irqrestore(&btv->s_lock,flags);
3924 static struct pci_device_id bttv_pci_tbl[] = {
3925 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
3926 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3927 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
3928 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3929 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
3930 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3931 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
3932 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3936 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
3938 static struct pci_driver bttv_pci_driver = {
3940 .id_table = bttv_pci_tbl,
3941 .probe = bttv_probe,
3942 .remove = __devexit_p(bttv_remove),
3944 .suspend = bttv_suspend,
3945 .resume = bttv_resume,
3948 static int bttv_init_module(void)
3953 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
3954 (BTTV_VERSION_CODE >> 16) & 0xff,
3955 (BTTV_VERSION_CODE >> 8) & 0xff,
3956 BTTV_VERSION_CODE & 0xff);
3958 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
3959 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
3961 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
3963 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
3964 gbufsize = BTTV_MAX_FBUF;
3965 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
3967 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
3968 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
3970 bttv_check_chipset();
3972 bus_register(&bttv_sub_bus_type);
3973 rc = pci_module_init(&bttv_pci_driver);
3974 if (-ENODEV == rc) {
3975 /* plenty of people trying to use bttv for the cx2388x ... */
3976 if (NULL != pci_find_device(0x14f1, 0x8800, NULL))
3977 printk("bttv doesn't support your Conexant 2388x card.\n");
3982 static void bttv_cleanup_module(void)
3984 pci_unregister_driver(&bttv_pci_driver);
3985 bus_unregister(&bttv_sub_bus_type);
3989 module_init(bttv_init_module);
3990 module_exit(bttv_cleanup_module);