2 $Id: bttv-driver.c,v 1.52 2005/08/04 00:55:16 mchehab Exp $
4 bttv - Bt848 frame grabber driver
6 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
7 & Marcus Metzler <mocm@thp.uni-koeln.de>
8 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
10 some v4l2 code lines are taken from Justin's bttv2 driver which is
11 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/delay.h>
32 #include <linux/errno.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
37 #include <linux/kdev_t.h>
38 #include <linux/dma-mapping.h>
41 #include <asm/byteorder.h>
45 unsigned int bttv_num; /* number of Bt848s in use */
46 struct bttv bttvs[BTTV_MAX];
48 unsigned int bttv_debug = 0;
49 unsigned int bttv_verbose = 1;
50 unsigned int bttv_gpio = 0;
52 /* config variables */
54 static unsigned int bigendian=1;
56 static unsigned int bigendian=0;
58 static unsigned int radio[BTTV_MAX];
59 static unsigned int irq_debug = 0;
60 static unsigned int gbuffers = 8;
61 static unsigned int gbufsize = 0x208000;
63 static int video_nr = -1;
64 static int radio_nr = -1;
65 static int vbi_nr = -1;
66 static int debug_latency = 0;
68 static unsigned int fdsr = 0;
71 static unsigned int combfilter = 0;
72 static unsigned int lumafilter = 0;
73 static unsigned int automute = 1;
74 static unsigned int chroma_agc = 0;
75 static unsigned int adc_crush = 1;
76 static unsigned int whitecrush_upper = 0xCF;
77 static unsigned int whitecrush_lower = 0x7F;
78 static unsigned int vcr_hack = 0;
79 static unsigned int irq_iswitch = 0;
80 static unsigned int uv_ratio = 50;
81 static unsigned int full_luma_range = 0;
82 static unsigned int coring = 0;
83 extern int no_overlay;
85 /* API features (turn on/off stuff for testing) */
86 static unsigned int v4l2 = 1;
90 module_param(bttv_verbose, int, 0644);
91 module_param(bttv_gpio, int, 0644);
92 module_param(bttv_debug, int, 0644);
93 module_param(irq_debug, int, 0644);
94 module_param(debug_latency, int, 0644);
96 module_param(fdsr, int, 0444);
97 module_param(video_nr, int, 0444);
98 module_param(radio_nr, int, 0444);
99 module_param(vbi_nr, int, 0444);
100 module_param(gbuffers, int, 0444);
101 module_param(gbufsize, int, 0444);
103 module_param(v4l2, int, 0644);
104 module_param(bigendian, int, 0644);
105 module_param(irq_iswitch, int, 0644);
106 module_param(combfilter, int, 0444);
107 module_param(lumafilter, int, 0444);
108 module_param(automute, int, 0444);
109 module_param(chroma_agc, int, 0444);
110 module_param(adc_crush, int, 0444);
111 module_param(whitecrush_upper, int, 0444);
112 module_param(whitecrush_lower, int, 0444);
113 module_param(vcr_hack, int, 0444);
114 module_param(uv_ratio, int, 0444);
115 module_param(full_luma_range, int, 0444);
116 module_param(coring, int, 0444);
118 module_param_array(radio, int, NULL, 0444);
120 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
121 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
122 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
123 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
124 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
125 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
126 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
127 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
128 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
129 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
130 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
131 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
132 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
133 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
134 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
135 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
136 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
137 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
139 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
140 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
141 MODULE_LICENSE("GPL");
143 /* ----------------------------------------------------------------------- */
146 static ssize_t show_card(struct class_device *cd, char *buf)
148 struct video_device *vfd = to_video_device(cd);
149 struct bttv *btv = dev_get_drvdata(vfd->dev);
150 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
152 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
154 /* ----------------------------------------------------------------------- */
157 /* special timing tables from conexant... */
158 static u8 SRAM_Table[][60] =
160 /* PAL digital input over GPIO[7:0] */
162 45, // 45 bytes following
163 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
164 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
165 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
166 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
167 0x37,0x00,0xAF,0x21,0x00
169 /* NTSC digital input over GPIO[7:0] */
171 51, // 51 bytes following
172 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
173 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
174 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
175 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
176 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
179 // TGB_NTSC392 // quartzsight
180 // This table has been modified to be used for Fusion Rev D
182 0x2A, // size of table = 42
183 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
184 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
185 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
186 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
191 const struct bttv_tvnorm bttv_tvnorms[] = {
193 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
194 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
196 .v4l2_id = V4L2_STD_PAL,
204 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
205 .scaledtwidth = 1135,
212 .v4l2_id = V4L2_STD_NTSC_M,
220 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
228 .v4l2_id = V4L2_STD_SECAM,
236 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
237 .scaledtwidth = 1135,
242 .sram = 0, /* like PAL, correct? */
244 .v4l2_id = V4L2_STD_PAL_Nc,
252 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
260 .v4l2_id = V4L2_STD_PAL_M,
268 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
276 .v4l2_id = V4L2_STD_PAL_N,
284 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
292 .v4l2_id = V4L2_STD_NTSC_M_JP,
300 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
308 /* that one hopefully works with the strange timing
309 * which video recorders produce when playing a NTSC
310 * tape on a PAL TV ... */
311 .v4l2_id = V4L2_STD_PAL_60,
319 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
320 .scaledtwidth = 1135,
329 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
331 /* ----------------------------------------------------------------------- */
333 packed pixel formats must come first */
334 static const struct bttv_format bttv_formats[] = {
336 .name = "8 bpp, gray",
337 .palette = VIDEO_PALETTE_GREY,
338 .fourcc = V4L2_PIX_FMT_GREY,
339 .btformat = BT848_COLOR_FMT_Y8,
341 .flags = FORMAT_FLAGS_PACKED,
343 .name = "8 bpp, dithered color",
344 .palette = VIDEO_PALETTE_HI240,
345 .fourcc = V4L2_PIX_FMT_HI240,
346 .btformat = BT848_COLOR_FMT_RGB8,
348 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
350 .name = "15 bpp RGB, le",
351 .palette = VIDEO_PALETTE_RGB555,
352 .fourcc = V4L2_PIX_FMT_RGB555,
353 .btformat = BT848_COLOR_FMT_RGB15,
355 .flags = FORMAT_FLAGS_PACKED,
357 .name = "15 bpp RGB, be",
359 .fourcc = V4L2_PIX_FMT_RGB555X,
360 .btformat = BT848_COLOR_FMT_RGB15,
361 .btswap = 0x03, /* byteswap */
363 .flags = FORMAT_FLAGS_PACKED,
365 .name = "16 bpp RGB, le",
366 .palette = VIDEO_PALETTE_RGB565,
367 .fourcc = V4L2_PIX_FMT_RGB565,
368 .btformat = BT848_COLOR_FMT_RGB16,
370 .flags = FORMAT_FLAGS_PACKED,
372 .name = "16 bpp RGB, be",
374 .fourcc = V4L2_PIX_FMT_RGB565X,
375 .btformat = BT848_COLOR_FMT_RGB16,
376 .btswap = 0x03, /* byteswap */
378 .flags = FORMAT_FLAGS_PACKED,
380 .name = "24 bpp RGB, le",
381 .palette = VIDEO_PALETTE_RGB24,
382 .fourcc = V4L2_PIX_FMT_BGR24,
383 .btformat = BT848_COLOR_FMT_RGB24,
385 .flags = FORMAT_FLAGS_PACKED,
387 .name = "32 bpp RGB, le",
388 .palette = VIDEO_PALETTE_RGB32,
389 .fourcc = V4L2_PIX_FMT_BGR32,
390 .btformat = BT848_COLOR_FMT_RGB32,
392 .flags = FORMAT_FLAGS_PACKED,
394 .name = "32 bpp RGB, be",
396 .fourcc = V4L2_PIX_FMT_RGB32,
397 .btformat = BT848_COLOR_FMT_RGB32,
398 .btswap = 0x0f, /* byte+word swap */
400 .flags = FORMAT_FLAGS_PACKED,
402 .name = "4:2:2, packed, YUYV",
403 .palette = VIDEO_PALETTE_YUV422,
404 .fourcc = V4L2_PIX_FMT_YUYV,
405 .btformat = BT848_COLOR_FMT_YUY2,
407 .flags = FORMAT_FLAGS_PACKED,
409 .name = "4:2:2, packed, YUYV",
410 .palette = VIDEO_PALETTE_YUYV,
411 .fourcc = V4L2_PIX_FMT_YUYV,
412 .btformat = BT848_COLOR_FMT_YUY2,
414 .flags = FORMAT_FLAGS_PACKED,
416 .name = "4:2:2, packed, UYVY",
417 .palette = VIDEO_PALETTE_UYVY,
418 .fourcc = V4L2_PIX_FMT_UYVY,
419 .btformat = BT848_COLOR_FMT_YUY2,
420 .btswap = 0x03, /* byteswap */
422 .flags = FORMAT_FLAGS_PACKED,
424 .name = "4:2:2, planar, Y-Cb-Cr",
425 .palette = VIDEO_PALETTE_YUV422P,
426 .fourcc = V4L2_PIX_FMT_YUV422P,
427 .btformat = BT848_COLOR_FMT_YCrCb422,
429 .flags = FORMAT_FLAGS_PLANAR,
433 .name = "4:2:0, planar, Y-Cb-Cr",
434 .palette = VIDEO_PALETTE_YUV420P,
435 .fourcc = V4L2_PIX_FMT_YUV420,
436 .btformat = BT848_COLOR_FMT_YCrCb422,
438 .flags = FORMAT_FLAGS_PLANAR,
442 .name = "4:2:0, planar, Y-Cr-Cb",
444 .fourcc = V4L2_PIX_FMT_YVU420,
445 .btformat = BT848_COLOR_FMT_YCrCb422,
447 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
451 .name = "4:1:1, planar, Y-Cb-Cr",
452 .palette = VIDEO_PALETTE_YUV411P,
453 .fourcc = V4L2_PIX_FMT_YUV411P,
454 .btformat = BT848_COLOR_FMT_YCrCb411,
456 .flags = FORMAT_FLAGS_PLANAR,
460 .name = "4:1:0, planar, Y-Cb-Cr",
461 .palette = VIDEO_PALETTE_YUV410P,
462 .fourcc = V4L2_PIX_FMT_YUV410,
463 .btformat = BT848_COLOR_FMT_YCrCb411,
465 .flags = FORMAT_FLAGS_PLANAR,
469 .name = "4:1:0, planar, Y-Cr-Cb",
471 .fourcc = V4L2_PIX_FMT_YVU410,
472 .btformat = BT848_COLOR_FMT_YCrCb411,
474 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
478 .name = "raw scanlines",
479 .palette = VIDEO_PALETTE_RAW,
481 .btformat = BT848_COLOR_FMT_RAW,
483 .flags = FORMAT_FLAGS_RAW,
486 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
488 /* ----------------------------------------------------------------------- */
490 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
491 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
492 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
493 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
494 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
495 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
496 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
497 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
498 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
499 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
500 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
501 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
503 static const struct v4l2_queryctrl no_ctl = {
505 .flags = V4L2_CTRL_FLAG_DISABLED,
507 static const struct v4l2_queryctrl bttv_ctls[] = {
510 .id = V4L2_CID_BRIGHTNESS,
511 .name = "Brightness",
515 .default_value = 32768,
516 .type = V4L2_CTRL_TYPE_INTEGER,
518 .id = V4L2_CID_CONTRAST,
523 .default_value = 32768,
524 .type = V4L2_CTRL_TYPE_INTEGER,
526 .id = V4L2_CID_SATURATION,
527 .name = "Saturation",
531 .default_value = 32768,
532 .type = V4L2_CTRL_TYPE_INTEGER,
539 .default_value = 32768,
540 .type = V4L2_CTRL_TYPE_INTEGER,
544 .id = V4L2_CID_AUDIO_MUTE,
548 .type = V4L2_CTRL_TYPE_BOOLEAN,
550 .id = V4L2_CID_AUDIO_VOLUME,
555 .default_value = 65535,
556 .type = V4L2_CTRL_TYPE_INTEGER,
558 .id = V4L2_CID_AUDIO_BALANCE,
563 .default_value = 32768,
564 .type = V4L2_CTRL_TYPE_INTEGER,
566 .id = V4L2_CID_AUDIO_BASS,
571 .default_value = 32768,
572 .type = V4L2_CTRL_TYPE_INTEGER,
574 .id = V4L2_CID_AUDIO_TREBLE,
579 .default_value = 32768,
580 .type = V4L2_CTRL_TYPE_INTEGER,
582 /* --- private --- */
584 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
585 .name = "chroma agc",
588 .type = V4L2_CTRL_TYPE_BOOLEAN,
590 .id = V4L2_CID_PRIVATE_COMBFILTER,
591 .name = "combfilter",
594 .type = V4L2_CTRL_TYPE_BOOLEAN,
596 .id = V4L2_CID_PRIVATE_AUTOMUTE,
600 .type = V4L2_CTRL_TYPE_BOOLEAN,
602 .id = V4L2_CID_PRIVATE_LUMAFILTER,
603 .name = "luma decimation filter",
606 .type = V4L2_CTRL_TYPE_BOOLEAN,
608 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
612 .type = V4L2_CTRL_TYPE_BOOLEAN,
614 .id = V4L2_CID_PRIVATE_VCR_HACK,
618 .type = V4L2_CTRL_TYPE_BOOLEAN,
620 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
621 .name = "whitecrush upper",
625 .default_value = 0xCF,
626 .type = V4L2_CTRL_TYPE_INTEGER,
628 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
629 .name = "whitecrush lower",
633 .default_value = 0x7F,
634 .type = V4L2_CTRL_TYPE_INTEGER,
636 .id = V4L2_CID_PRIVATE_UV_RATIO,
642 .type = V4L2_CTRL_TYPE_INTEGER,
644 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
645 .name = "full luma range",
648 .type = V4L2_CTRL_TYPE_BOOLEAN,
650 .id = V4L2_CID_PRIVATE_CORING,
656 .type = V4L2_CTRL_TYPE_INTEGER,
662 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
664 /* ----------------------------------------------------------------------- */
665 /* resource management */
668 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
670 if (fh->resources & bit)
671 /* have it already allocated */
676 if (btv->resources & bit) {
677 /* no, someone else uses it */
681 /* it's free, grab it */
682 fh->resources |= bit;
683 btv->resources |= bit;
689 int check_btres(struct bttv_fh *fh, int bit)
691 return (fh->resources & bit);
695 int locked_btres(struct bttv *btv, int bit)
697 return (btv->resources & bit);
701 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
703 if ((fh->resources & bits) != bits) {
704 /* trying to free ressources not allocated by us ... */
705 printk("bttv: BUG! (btres)\n");
708 fh->resources &= ~bits;
709 btv->resources &= ~bits;
713 /* ----------------------------------------------------------------------- */
714 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
716 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
717 PLL_X = Reference pre-divider (0=1, 1=2)
718 PLL_C = Post divider (0=6, 1=4)
719 PLL_I = Integer input
720 PLL_F = Fractional input
722 F_input = 28.636363 MHz:
723 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
726 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
728 unsigned char fl, fh, fi;
730 /* prevent overflows */
743 btwrite(fl, BT848_PLL_F_LO);
744 btwrite(fh, BT848_PLL_F_HI);
745 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
748 static void set_pll(struct bttv *btv)
752 if (!btv->pll.pll_crystal)
755 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
756 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
760 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
762 if (btv->pll.pll_current == 0)
764 vprintk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
765 btv->c.nr,btv->pll.pll_ifreq);
766 btwrite(0x00,BT848_TGCTRL);
767 btwrite(0x00,BT848_PLL_XCI);
768 btv->pll.pll_current = 0;
772 vprintk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
773 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
774 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
776 for (i=0; i<10; i++) {
777 /* Let other people run while the PLL stabilizes */
781 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
782 btwrite(0,BT848_DSTATUS);
784 btwrite(0x08,BT848_TGCTRL);
785 btv->pll.pll_current = btv->pll.pll_ofreq;
790 btv->pll.pll_current = -1;
795 /* used to switch between the bt848's analog/digital video capture modes */
796 static void bt848A_set_timing(struct bttv *btv)
799 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
800 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
802 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
803 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
804 btv->c.nr,table_idx);
806 /* timing change...reset timing generator address */
807 btwrite(0x00, BT848_TGCTRL);
808 btwrite(0x02, BT848_TGCTRL);
809 btwrite(0x00, BT848_TGCTRL);
811 len=SRAM_Table[table_idx][0];
812 for(i = 1; i <= len; i++)
813 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
814 btv->pll.pll_ofreq = 27000000;
817 btwrite(0x11, BT848_TGCTRL);
818 btwrite(0x41, BT848_DVSIF);
820 btv->pll.pll_ofreq = fsc;
822 btwrite(0x0, BT848_DVSIF);
826 /* ----------------------------------------------------------------------- */
828 static void bt848_bright(struct bttv *btv, int bright)
832 // printk("bttv: set bright: %d\n",bright); // DEBUG
833 btv->bright = bright;
835 /* We want -128 to 127 we get 0-65535 */
836 value = (bright >> 8) - 128;
837 btwrite(value & 0xff, BT848_BRIGHT);
840 static void bt848_hue(struct bttv *btv, int hue)
847 value = (hue >> 8) - 128;
848 btwrite(value & 0xff, BT848_HUE);
851 static void bt848_contrast(struct bttv *btv, int cont)
855 btv->contrast = cont;
859 hibit = (value >> 6) & 4;
860 btwrite(value & 0xff, BT848_CONTRAST_LO);
861 btaor(hibit, ~4, BT848_E_CONTROL);
862 btaor(hibit, ~4, BT848_O_CONTROL);
865 static void bt848_sat(struct bttv *btv, int color)
867 int val_u,val_v,hibits;
869 btv->saturation = color;
871 /* 0-511 for the color */
872 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
873 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
874 hibits = (val_u >> 7) & 2;
875 hibits |= (val_v >> 8) & 1;
876 btwrite(val_u & 0xff, BT848_SAT_U_LO);
877 btwrite(val_v & 0xff, BT848_SAT_V_LO);
878 btaor(hibits, ~3, BT848_E_CONTROL);
879 btaor(hibits, ~3, BT848_O_CONTROL);
882 /* ----------------------------------------------------------------------- */
885 video_mux(struct bttv *btv, unsigned int input)
889 if (input >= bttv_tvcards[btv->c.type].video_inputs)
892 /* needed by RemoteVideo MX */
893 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
895 gpio_inout(mask2,mask2);
897 if (input == btv->svhs) {
898 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
899 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
901 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
902 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
904 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
905 btaor(mux<<5, ~(3<<5), BT848_IFORM);
906 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
907 btv->c.nr,input,mux);
909 /* card specific hook */
910 if(bttv_tvcards[btv->c.type].muxsel_hook)
911 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
915 static char *audio_modes[] = {
916 "audio: tuner", "audio: radio", "audio: extern",
917 "audio: intern", "audio: off"
921 audio_mux(struct bttv *btv, int mode)
923 int val,mux,i2c_mux,signal;
925 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
926 bttv_tvcards[btv->c.type].gpiomask);
927 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
931 btv->audio |= AUDIO_MUTE;
934 btv->audio &= ~AUDIO_MUTE;
940 btv->audio &= AUDIO_MUTE;
943 i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
944 if (btv->opt_automute && !signal && !btv->radio_user)
947 val = bttv_tvcards[btv->c.type].audiomux[mux];
948 gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
950 bttv_gpio_tracking(btv,audio_modes[mux]);
952 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
957 i2c_vidiocschan(struct bttv *btv)
959 struct video_channel c;
961 memset(&c,0,sizeof(c));
962 c.norm = btv->tvnorm;
963 c.channel = btv->input;
964 bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
965 if (btv->c.type == BTTV_VOODOOTV_FM)
966 bttv_tda9880_setnorm(btv,c.norm);
970 set_tvnorm(struct bttv *btv, unsigned int norm)
972 const struct bttv_tvnorm *tvnorm;
974 if (norm < 0 || norm >= BTTV_TVNORMS)
978 tvnorm = &bttv_tvnorms[norm];
980 btwrite(tvnorm->adelay, BT848_ADELAY);
981 btwrite(tvnorm->bdelay, BT848_BDELAY);
982 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
984 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
985 btwrite(1, BT848_VBI_PACK_DEL);
986 bt848A_set_timing(btv);
988 switch (btv->c.type) {
989 case BTTV_VOODOOTV_FM:
990 bttv_tda9880_setnorm(btv,norm);
997 set_input(struct bttv *btv, unsigned int input)
1003 spin_lock_irqsave(&btv->s_lock,flags);
1004 if (btv->curr.frame_irq) {
1005 /* active capture -> delayed input switch */
1006 btv->new_input = input;
1008 video_mux(btv,input);
1010 spin_unlock_irqrestore(&btv->s_lock,flags);
1012 video_mux(btv,input);
1014 audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1015 AUDIO_TUNER : AUDIO_EXTERN));
1016 set_tvnorm(btv,btv->tvnorm);
1017 i2c_vidiocschan(btv);
1020 static void init_irqreg(struct bttv *btv)
1023 btwrite(0xfffffUL, BT848_INT_STAT);
1025 if (bttv_tvcards[btv->c.type].no_video) {
1027 btwrite(BT848_INT_I2CDONE,
1031 btwrite((btv->triton1) |
1032 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1034 (fdsr ? BT848_INT_FDSR : 0) |
1035 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1036 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1042 static void init_bt848(struct bttv *btv)
1046 if (bttv_tvcards[btv->c.type].no_video) {
1047 /* very basic init only */
1052 btwrite(0x00, BT848_CAP_CTL);
1053 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1054 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1056 /* set planar and packed mode trigger points and */
1057 /* set rising edge of inverted GPINTR pin as irq trigger */
1058 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1059 BT848_GPIO_DMA_CTL_PLTP1_16|
1060 BT848_GPIO_DMA_CTL_PLTP23_16|
1061 BT848_GPIO_DMA_CTL_GPINTC|
1062 BT848_GPIO_DMA_CTL_GPINTI,
1063 BT848_GPIO_DMA_CTL);
1065 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1066 btwrite(val, BT848_E_SCLOOP);
1067 btwrite(val, BT848_O_SCLOOP);
1069 btwrite(0x20, BT848_E_VSCALE_HI);
1070 btwrite(0x20, BT848_O_VSCALE_HI);
1071 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1074 btwrite(whitecrush_upper, BT848_WC_UP);
1075 btwrite(whitecrush_lower, BT848_WC_DOWN);
1077 if (btv->opt_lumafilter) {
1078 btwrite(0, BT848_E_CONTROL);
1079 btwrite(0, BT848_O_CONTROL);
1081 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1082 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1085 bt848_bright(btv, btv->bright);
1086 bt848_hue(btv, btv->hue);
1087 bt848_contrast(btv, btv->contrast);
1088 bt848_sat(btv, btv->saturation);
1094 static void bttv_reinit_bt848(struct bttv *btv)
1096 unsigned long flags;
1099 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1100 spin_lock_irqsave(&btv->s_lock,flags);
1102 bttv_set_dma(btv,0);
1103 spin_unlock_irqrestore(&btv->s_lock,flags);
1106 btv->pll.pll_current = -1;
1107 set_input(btv,btv->input);
1110 static int get_control(struct bttv *btv, struct v4l2_control *c)
1112 struct video_audio va;
1115 for (i = 0; i < BTTV_CTLS; i++)
1116 if (bttv_ctls[i].id == c->id)
1120 if (i >= 4 && i <= 8) {
1121 memset(&va,0,sizeof(va));
1122 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1123 if (btv->audio_hook)
1124 btv->audio_hook(btv,&va,0);
1127 case V4L2_CID_BRIGHTNESS:
1128 c->value = btv->bright;
1131 c->value = btv->hue;
1133 case V4L2_CID_CONTRAST:
1134 c->value = btv->contrast;
1136 case V4L2_CID_SATURATION:
1137 c->value = btv->saturation;
1140 case V4L2_CID_AUDIO_MUTE:
1141 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1143 case V4L2_CID_AUDIO_VOLUME:
1144 c->value = va.volume;
1146 case V4L2_CID_AUDIO_BALANCE:
1147 c->value = va.balance;
1149 case V4L2_CID_AUDIO_BASS:
1152 case V4L2_CID_AUDIO_TREBLE:
1153 c->value = va.treble;
1156 case V4L2_CID_PRIVATE_CHROMA_AGC:
1157 c->value = btv->opt_chroma_agc;
1159 case V4L2_CID_PRIVATE_COMBFILTER:
1160 c->value = btv->opt_combfilter;
1162 case V4L2_CID_PRIVATE_LUMAFILTER:
1163 c->value = btv->opt_lumafilter;
1165 case V4L2_CID_PRIVATE_AUTOMUTE:
1166 c->value = btv->opt_automute;
1168 case V4L2_CID_PRIVATE_AGC_CRUSH:
1169 c->value = btv->opt_adc_crush;
1171 case V4L2_CID_PRIVATE_VCR_HACK:
1172 c->value = btv->opt_vcr_hack;
1174 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1175 c->value = btv->opt_whitecrush_upper;
1177 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1178 c->value = btv->opt_whitecrush_lower;
1180 case V4L2_CID_PRIVATE_UV_RATIO:
1181 c->value = btv->opt_uv_ratio;
1183 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1184 c->value = btv->opt_full_luma_range;
1186 case V4L2_CID_PRIVATE_CORING:
1187 c->value = btv->opt_coring;
1195 static int set_control(struct bttv *btv, struct v4l2_control *c)
1197 struct video_audio va;
1200 for (i = 0; i < BTTV_CTLS; i++)
1201 if (bttv_ctls[i].id == c->id)
1205 if (i >= 4 && i <= 8) {
1206 memset(&va,0,sizeof(va));
1207 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1208 if (btv->audio_hook)
1209 btv->audio_hook(btv,&va,0);
1212 case V4L2_CID_BRIGHTNESS:
1213 bt848_bright(btv,c->value);
1216 bt848_hue(btv,c->value);
1218 case V4L2_CID_CONTRAST:
1219 bt848_contrast(btv,c->value);
1221 case V4L2_CID_SATURATION:
1222 bt848_sat(btv,c->value);
1224 case V4L2_CID_AUDIO_MUTE:
1226 va.flags |= VIDEO_AUDIO_MUTE;
1227 audio_mux(btv, AUDIO_MUTE);
1229 va.flags &= ~VIDEO_AUDIO_MUTE;
1230 audio_mux(btv, AUDIO_UNMUTE);
1234 case V4L2_CID_AUDIO_VOLUME:
1235 va.volume = c->value;
1237 case V4L2_CID_AUDIO_BALANCE:
1238 va.balance = c->value;
1240 case V4L2_CID_AUDIO_BASS:
1243 case V4L2_CID_AUDIO_TREBLE:
1244 va.treble = c->value;
1247 case V4L2_CID_PRIVATE_CHROMA_AGC:
1248 btv->opt_chroma_agc = c->value;
1249 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1250 btwrite(val, BT848_E_SCLOOP);
1251 btwrite(val, BT848_O_SCLOOP);
1253 case V4L2_CID_PRIVATE_COMBFILTER:
1254 btv->opt_combfilter = c->value;
1256 case V4L2_CID_PRIVATE_LUMAFILTER:
1257 btv->opt_lumafilter = c->value;
1258 if (btv->opt_lumafilter) {
1259 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1260 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1262 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1263 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1266 case V4L2_CID_PRIVATE_AUTOMUTE:
1267 btv->opt_automute = c->value;
1269 case V4L2_CID_PRIVATE_AGC_CRUSH:
1270 btv->opt_adc_crush = c->value;
1271 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1274 case V4L2_CID_PRIVATE_VCR_HACK:
1275 btv->opt_vcr_hack = c->value;
1277 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1278 btv->opt_whitecrush_upper = c->value;
1279 btwrite(c->value, BT848_WC_UP);
1281 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1282 btv->opt_whitecrush_lower = c->value;
1283 btwrite(c->value, BT848_WC_DOWN);
1285 case V4L2_CID_PRIVATE_UV_RATIO:
1286 btv->opt_uv_ratio = c->value;
1287 bt848_sat(btv, btv->saturation);
1289 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1290 btv->opt_full_luma_range = c->value;
1291 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1293 case V4L2_CID_PRIVATE_CORING:
1294 btv->opt_coring = c->value;
1295 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1300 if (i >= 4 && i <= 8) {
1301 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1302 if (btv->audio_hook)
1303 btv->audio_hook(btv,&va,1);
1308 /* ----------------------------------------------------------------------- */
1310 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1312 unsigned int outbits, data;
1313 outbits = btread(BT848_GPIO_OUT_EN);
1314 data = btread(BT848_GPIO_DATA);
1315 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1316 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1319 static void bttv_field_count(struct bttv *btv)
1327 /* start field counter */
1328 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1330 /* stop field counter */
1331 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1332 btv->field_count = 0;
1336 static const struct bttv_format*
1337 format_by_palette(int palette)
1341 for (i = 0; i < BTTV_FORMATS; i++) {
1342 if (-1 == bttv_formats[i].palette)
1344 if (bttv_formats[i].palette == palette)
1345 return bttv_formats+i;
1350 static const struct bttv_format*
1351 format_by_fourcc(int fourcc)
1355 for (i = 0; i < BTTV_FORMATS; i++) {
1356 if (-1 == bttv_formats[i].fourcc)
1358 if (bttv_formats[i].fourcc == fourcc)
1359 return bttv_formats+i;
1364 /* ----------------------------------------------------------------------- */
1368 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1369 struct bttv_buffer *new)
1371 struct bttv_buffer *old;
1372 unsigned long flags;
1375 dprintk("switch_overlay: enter [new=%p]\n",new);
1377 new->vb.state = STATE_DONE;
1378 spin_lock_irqsave(&btv->s_lock,flags);
1382 bttv_set_dma(btv, 0x03);
1383 spin_unlock_irqrestore(&btv->s_lock,flags);
1385 free_btres(btv,fh,RESOURCE_OVERLAY);
1387 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1388 bttv_dma_free(btv, old);
1391 dprintk("switch_overlay: done\n");
1395 /* ----------------------------------------------------------------------- */
1396 /* video4linux (1) interface */
1398 static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
1399 const struct bttv_format *fmt,
1400 unsigned int width, unsigned int height,
1401 enum v4l2_field field)
1403 int redo_dma_risc = 0;
1406 /* check settings */
1409 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1411 height = RAW_LINES*2;
1412 if (width*height > buf->vb.bsize)
1414 buf->vb.size = buf->vb.bsize;
1418 width > bttv_tvnorms[btv->tvnorm].swidth ||
1419 height > bttv_tvnorms[btv->tvnorm].sheight)
1421 buf->vb.size = (width * height * fmt->depth) >> 3;
1422 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1426 /* alloc + fill struct bttv_buffer (if changed) */
1427 if (buf->vb.width != width || buf->vb.height != height ||
1428 buf->vb.field != field ||
1429 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1430 buf->vb.width = width;
1431 buf->vb.height = height;
1432 buf->vb.field = field;
1433 buf->tvnorm = btv->tvnorm;
1438 /* alloc risc memory */
1439 if (STATE_NEEDS_INIT == buf->vb.state) {
1441 if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
1446 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1449 buf->vb.state = STATE_PREPARED;
1453 bttv_dma_free(btv,buf);
1458 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1460 struct bttv_fh *fh = q->priv_data;
1462 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1465 while (*size * *count > gbuffers * gbufsize)
1471 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1472 enum v4l2_field field)
1474 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1475 struct bttv_fh *fh = q->priv_data;
1477 return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
1478 fh->width, fh->height, field);
1482 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1484 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1485 struct bttv_fh *fh = q->priv_data;
1486 struct bttv *btv = fh->btv;
1488 buf->vb.state = STATE_QUEUED;
1489 list_add_tail(&buf->vb.queue,&btv->capture);
1490 if (!btv->curr.frame_irq) {
1492 bttv_set_dma(btv, 0x03);
1496 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1498 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1499 struct bttv_fh *fh = q->priv_data;
1501 bttv_dma_free(fh->btv,buf);
1504 static struct videobuf_queue_ops bttv_video_qops = {
1505 .buf_setup = buffer_setup,
1506 .buf_prepare = buffer_prepare,
1507 .buf_queue = buffer_queue,
1508 .buf_release = buffer_release,
1511 static const char *v4l1_ioctls[] = {
1512 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
1513 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
1514 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
1515 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
1516 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
1517 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
1519 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1523 return BTTV_VERSION_CODE;
1525 /* *** v4l1 *** ************************************************ */
1528 unsigned long *freq = arg;
1534 unsigned long *freq = arg;
1537 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1538 if (btv->has_matchbox && btv->radio_user)
1539 tea5757_set_freq(btv,*freq);
1546 struct video_tuner *v = arg;
1548 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1550 if (v->tuner) /* Only tuner 0 */
1552 strcpy(v->name, "Television");
1554 v->rangehigh = 0x7FFFFFFF;
1555 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1556 v->mode = btv->tvnorm;
1557 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1558 bttv_call_i2c_clients(btv,cmd,v);
1563 struct video_tuner *v = arg;
1565 if (v->tuner) /* Only tuner 0 */
1567 if (v->mode >= BTTV_TVNORMS)
1571 set_tvnorm(btv,v->mode);
1572 bttv_call_i2c_clients(btv,cmd,v);
1579 struct video_channel *v = arg;
1580 unsigned int channel = v->channel;
1582 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1585 v->flags = VIDEO_VC_AUDIO;
1586 v->type = VIDEO_TYPE_CAMERA;
1587 v->norm = btv->tvnorm;
1588 if (channel == bttv_tvcards[btv->c.type].tuner) {
1589 strcpy(v->name,"Television");
1590 v->flags|=VIDEO_VC_TUNER;
1591 v->type=VIDEO_TYPE_TV;
1593 } else if (channel == btv->svhs) {
1594 strcpy(v->name,"S-Video");
1596 sprintf(v->name,"Composite%d",channel);
1602 struct video_channel *v = arg;
1603 unsigned int channel = v->channel;
1605 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1607 if (v->norm >= BTTV_TVNORMS)
1611 if (channel == btv->input &&
1612 v->norm == btv->tvnorm) {
1618 btv->tvnorm = v->norm;
1619 set_input(btv,v->channel);
1626 struct video_audio *v = arg;
1628 memset(v,0,sizeof(*v));
1629 strcpy(v->name,"Television");
1630 v->flags |= VIDEO_AUDIO_MUTABLE;
1631 v->mode = VIDEO_SOUND_MONO;
1634 bttv_call_i2c_clients(btv,cmd,v);
1636 /* card specific hooks */
1637 if (btv->audio_hook)
1638 btv->audio_hook(btv,v,0);
1645 struct video_audio *v = arg;
1646 unsigned int audio = v->audio;
1648 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1652 audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
1653 bttv_call_i2c_clients(btv,cmd,v);
1655 /* card specific hooks */
1656 if (btv->audio_hook)
1657 btv->audio_hook(btv,v,1);
1663 /* *** v4l2 *** ************************************************ */
1664 case VIDIOC_ENUMSTD:
1666 struct v4l2_standard *e = arg;
1667 unsigned int index = e->index;
1669 if (index >= BTTV_TVNORMS)
1671 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1672 bttv_tvnorms[e->index].name);
1678 v4l2_std_id *id = arg;
1679 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1684 v4l2_std_id *id = arg;
1687 for (i = 0; i < BTTV_TVNORMS; i++)
1688 if (*id & bttv_tvnorms[i].v4l2_id)
1690 if (i == BTTV_TVNORMS)
1695 i2c_vidiocschan(btv);
1699 case VIDIOC_QUERYSTD:
1701 v4l2_std_id *id = arg;
1703 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1704 *id = V4L2_STD_625_50;
1706 *id = V4L2_STD_525_60;
1710 case VIDIOC_ENUMINPUT:
1712 struct v4l2_input *i = arg;
1716 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1718 memset(i,0,sizeof(*i));
1720 i->type = V4L2_INPUT_TYPE_CAMERA;
1722 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1723 sprintf(i->name, "Television");
1724 i->type = V4L2_INPUT_TYPE_TUNER;
1726 } else if (i->index == btv->svhs) {
1727 sprintf(i->name, "S-Video");
1729 sprintf(i->name,"Composite%d",i->index);
1731 if (i->index == btv->input) {
1732 __u32 dstatus = btread(BT848_DSTATUS);
1733 if (0 == (dstatus & BT848_DSTATUS_PRES))
1734 i->status |= V4L2_IN_ST_NO_SIGNAL;
1735 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1736 i->status |= V4L2_IN_ST_NO_H_LOCK;
1738 for (n = 0; n < BTTV_TVNORMS; n++)
1739 i->std |= bttv_tvnorms[n].v4l2_id;
1742 case VIDIOC_G_INPUT:
1748 case VIDIOC_S_INPUT:
1750 unsigned int *i = arg;
1752 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1760 case VIDIOC_G_TUNER:
1762 struct v4l2_tuner *t = arg;
1764 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1769 memset(t,0,sizeof(*t));
1770 strcpy(t->name, "Television");
1771 t->type = V4L2_TUNER_ANALOG_TV;
1772 t->rangehigh = 0xffffffffUL;
1773 t->capability = V4L2_TUNER_CAP_NORM;
1774 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1775 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1779 struct video_audio va;
1780 memset(&va, 0, sizeof(struct video_audio));
1781 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1782 if (btv->audio_hook)
1783 btv->audio_hook(btv,&va,0);
1784 if(va.mode & VIDEO_SOUND_STEREO) {
1785 t->audmode = V4L2_TUNER_MODE_STEREO;
1786 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1788 if(va.mode & VIDEO_SOUND_LANG1) {
1789 t->audmode = V4L2_TUNER_MODE_LANG1;
1790 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1791 | V4L2_TUNER_SUB_LANG2;
1794 /* FIXME: fill capability+audmode */
1798 case VIDIOC_S_TUNER:
1800 struct v4l2_tuner *t = arg;
1802 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1808 struct video_audio va;
1809 memset(&va, 0, sizeof(struct video_audio));
1810 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1811 if (t->audmode == V4L2_TUNER_MODE_MONO)
1812 va.mode = VIDEO_SOUND_MONO;
1813 else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1814 va.mode = VIDEO_SOUND_STEREO;
1815 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1816 va.mode = VIDEO_SOUND_LANG1;
1817 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1818 va.mode = VIDEO_SOUND_LANG2;
1819 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1820 if (btv->audio_hook)
1821 btv->audio_hook(btv,&va,1);
1827 case VIDIOC_G_FREQUENCY:
1829 struct v4l2_frequency *f = arg;
1831 memset(f,0,sizeof(*f));
1832 f->type = V4L2_TUNER_ANALOG_TV;
1833 f->frequency = btv->freq;
1836 case VIDIOC_S_FREQUENCY:
1838 struct v4l2_frequency *f = arg;
1840 if (unlikely(f->tuner != 0))
1842 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
1845 btv->freq = f->frequency;
1846 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1847 if (btv->has_matchbox && btv->radio_user)
1848 tea5757_set_freq(btv,btv->freq);
1854 return -ENOIOCTLCMD;
1860 static int verify_window(const struct bttv_tvnorm *tvn,
1861 struct v4l2_window *win, int fixup)
1863 enum v4l2_field field;
1866 if (win->w.width < 48 || win->w.height < 32)
1868 if (win->clipcount > 2048)
1873 maxh = tvn->sheight;
1875 if (V4L2_FIELD_ANY == field) {
1876 field = (win->w.height > maxh/2)
1877 ? V4L2_FIELD_INTERLACED
1881 case V4L2_FIELD_TOP:
1882 case V4L2_FIELD_BOTTOM:
1885 case V4L2_FIELD_INTERLACED:
1891 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1894 if (win->w.width > maxw)
1895 win->w.width = maxw;
1896 if (win->w.height > maxh)
1897 win->w.height = maxh;
1902 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1903 struct v4l2_window *win, int fixup)
1905 struct v4l2_clip *clips = NULL;
1906 int n,size,retval = 0;
1908 if (NULL == fh->ovfmt)
1910 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1912 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1916 /* copy clips -- luckily v4l1 + v4l2 are binary
1917 compatible here ...*/
1919 size = sizeof(*clips)*(n+4);
1920 clips = kmalloc(size,GFP_KERNEL);
1924 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1929 /* clip against screen */
1930 if (NULL != btv->fbuf.base)
1931 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1933 btcx_sort_clips(clips,n);
1935 /* 4-byte alignments */
1936 switch (fh->ovfmt->depth) {
1939 btcx_align(&win->w, clips, n, 3);
1942 btcx_align(&win->w, clips, n, 1);
1945 /* no alignment fixups needed */
1951 down(&fh->cap.lock);
1953 kfree(fh->ov.clips);
1954 fh->ov.clips = clips;
1958 fh->ov.field = win->field;
1959 fh->ov.setup_ok = 1;
1960 btv->init.ov.w.width = win->w.width;
1961 btv->init.ov.w.height = win->w.height;
1962 btv->init.ov.field = win->field;
1964 /* update overlay if needed */
1966 if (check_btres(fh, RESOURCE_OVERLAY)) {
1967 struct bttv_buffer *new;
1969 new = videobuf_alloc(sizeof(*new));
1970 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1971 retval = bttv_switch_overlay(btv,fh,new);
1977 /* ----------------------------------------------------------------------- */
1979 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
1981 struct videobuf_queue* q = NULL;
1984 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1987 case V4L2_BUF_TYPE_VBI_CAPTURE:
1996 static int bttv_resource(struct bttv_fh *fh)
2001 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2002 res = RESOURCE_VIDEO;
2004 case V4L2_BUF_TYPE_VBI_CAPTURE:
2013 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2015 struct videobuf_queue *q = bttv_queue(fh);
2016 int res = bttv_resource(fh);
2018 if (check_btres(fh,res))
2020 if (videobuf_queue_is_busy(q))
2026 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2029 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2030 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2031 f->fmt.pix.width = fh->width;
2032 f->fmt.pix.height = fh->height;
2033 f->fmt.pix.field = fh->cap.field;
2034 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2035 f->fmt.pix.bytesperline =
2036 (f->fmt.pix.width * fh->fmt->depth) >> 3;
2037 f->fmt.pix.sizeimage =
2038 f->fmt.pix.height * f->fmt.pix.bytesperline;
2040 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2041 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2042 f->fmt.win.w = fh->ov.w;
2043 f->fmt.win.field = fh->ov.field;
2045 case V4L2_BUF_TYPE_VBI_CAPTURE:
2046 bttv_vbi_get_fmt(fh,f);
2053 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2054 struct v4l2_format *f)
2057 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2059 const struct bttv_format *fmt;
2060 enum v4l2_field field;
2061 unsigned int maxw,maxh;
2063 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2068 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2069 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2070 field = f->fmt.pix.field;
2071 if (V4L2_FIELD_ANY == field)
2072 field = (f->fmt.pix.height > maxh/2)
2073 ? V4L2_FIELD_INTERLACED
2074 : V4L2_FIELD_BOTTOM;
2075 if (V4L2_FIELD_SEQ_BT == field)
2076 field = V4L2_FIELD_SEQ_TB;
2078 case V4L2_FIELD_TOP:
2079 case V4L2_FIELD_BOTTOM:
2080 case V4L2_FIELD_ALTERNATE:
2083 case V4L2_FIELD_INTERLACED:
2085 case V4L2_FIELD_SEQ_TB:
2086 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2093 /* update data for the application */
2094 f->fmt.pix.field = field;
2095 if (f->fmt.pix.width < 48)
2096 f->fmt.pix.width = 48;
2097 if (f->fmt.pix.height < 32)
2098 f->fmt.pix.height = 32;
2099 if (f->fmt.pix.width > maxw)
2100 f->fmt.pix.width = maxw;
2101 if (f->fmt.pix.height > maxh)
2102 f->fmt.pix.height = maxh;
2103 f->fmt.pix.width &= ~0x03;
2104 f->fmt.pix.bytesperline =
2105 (f->fmt.pix.width * fmt->depth) >> 3;
2106 f->fmt.pix.sizeimage =
2107 f->fmt.pix.height * f->fmt.pix.bytesperline;
2111 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2112 return verify_window(&bttv_tvnorms[btv->tvnorm],
2114 case V4L2_BUF_TYPE_VBI_CAPTURE:
2115 bttv_vbi_try_fmt(fh,f);
2122 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2123 struct v4l2_format *f)
2128 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2130 const struct bttv_format *fmt;
2132 retval = bttv_switch_type(fh,f->type);
2135 retval = bttv_try_fmt(fh,btv,f);
2138 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2140 /* update our state informations */
2141 down(&fh->cap.lock);
2143 fh->cap.field = f->fmt.pix.field;
2144 fh->cap.last = V4L2_FIELD_NONE;
2145 fh->width = f->fmt.pix.width;
2146 fh->height = f->fmt.pix.height;
2147 btv->init.fmt = fmt;
2148 btv->init.width = f->fmt.pix.width;
2149 btv->init.height = f->fmt.pix.height;
2154 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2155 if (no_overlay > 0) {
2156 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2159 return setup_window(fh, btv, &f->fmt.win, 1);
2160 case V4L2_BUF_TYPE_VBI_CAPTURE:
2161 retval = bttv_switch_type(fh,f->type);
2164 if (locked_btres(fh->btv, RESOURCE_VBI))
2166 bttv_vbi_try_fmt(fh,f);
2167 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2168 bttv_vbi_get_fmt(fh,f);
2175 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2176 unsigned int cmd, void *arg)
2178 struct bttv_fh *fh = file->private_data;
2179 struct bttv *btv = fh->btv;
2180 unsigned long flags;
2183 if (bttv_debug > 1) {
2184 switch (_IOC_TYPE(cmd)) {
2186 printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
2187 btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
2188 v4l1_ioctls[_IOC_NR(cmd)] : "???");
2191 printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
2192 btv->c.nr, cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
2195 printk("bttv%d: ioctl 0x%x (???)\n",
2200 bttv_reinit_bt848(btv);
2208 case VIDIOC_S_INPUT:
2209 case VIDIOC_S_TUNER:
2210 case VIDIOC_S_FREQUENCY:
2211 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2218 /* *** v4l1 *** ************************************************ */
2221 struct video_capability *cap = arg;
2223 memset(cap,0,sizeof(*cap));
2224 strcpy(cap->name,btv->video_dev->name);
2225 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2227 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2230 cap->type = VID_TYPE_CAPTURE|
2234 if (no_overlay <= 0)
2235 cap->type |= VID_TYPE_OVERLAY;
2237 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2238 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2240 cap->minheight = 32;
2242 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2243 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2249 struct video_picture *pic = arg;
2251 memset(pic,0,sizeof(*pic));
2252 pic->brightness = btv->bright;
2253 pic->contrast = btv->contrast;
2254 pic->hue = btv->hue;
2255 pic->colour = btv->saturation;
2257 pic->depth = fh->fmt->depth;
2258 pic->palette = fh->fmt->palette;
2264 struct video_picture *pic = arg;
2265 const struct bttv_format *fmt;
2267 fmt = format_by_palette(pic->palette);
2270 down(&fh->cap.lock);
2271 if (fmt->depth != pic->depth) {
2273 goto fh_unlock_and_return;
2277 btv->init.ovfmt = fmt;
2278 btv->init.fmt = fmt;
2280 /* dirty hack time: swap bytes for overlay if the
2281 display adaptor is big endian (insmod option) */
2282 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2283 fmt->palette == VIDEO_PALETTE_RGB565 ||
2284 fmt->palette == VIDEO_PALETTE_RGB32) {
2288 bt848_bright(btv,pic->brightness);
2289 bt848_contrast(btv,pic->contrast);
2290 bt848_hue(btv,pic->hue);
2291 bt848_sat(btv,pic->colour);
2298 struct video_window *win = arg;
2300 memset(win,0,sizeof(*win));
2301 win->x = fh->ov.w.left;
2302 win->y = fh->ov.w.top;
2303 win->width = fh->ov.w.width;
2304 win->height = fh->ov.w.height;
2309 struct video_window *win = arg;
2310 struct v4l2_window w2;
2312 if (no_overlay > 0) {
2313 printk ("VIDIOCSWIN: no_overlay\n");
2317 w2.field = V4L2_FIELD_ANY;
2320 w2.w.width = win->width;
2321 w2.w.height = win->height;
2322 w2.clipcount = win->clipcount;
2323 w2.clips = (struct v4l2_clip __user *)win->clips;
2324 retval = setup_window(fh, btv, &w2, 0);
2326 /* on v4l1 this ioctl affects the read() size too */
2327 fh->width = fh->ov.w.width;
2328 fh->height = fh->ov.w.height;
2329 btv->init.width = fh->ov.w.width;
2330 btv->init.height = fh->ov.w.height;
2337 struct video_buffer *fbuf = arg;
2339 fbuf->base = btv->fbuf.base;
2340 fbuf->width = btv->fbuf.fmt.width;
2341 fbuf->height = btv->fbuf.fmt.height;
2342 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2344 fbuf->depth = fh->ovfmt->depth;
2349 struct video_buffer *fbuf = arg;
2350 const struct bttv_format *fmt;
2353 if(!capable(CAP_SYS_ADMIN) &&
2354 !capable(CAP_SYS_RAWIO))
2356 end = (unsigned long)fbuf->base +
2357 fbuf->height * fbuf->bytesperline;
2358 down(&fh->cap.lock);
2361 switch (fbuf->depth) {
2363 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2366 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2369 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2372 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2376 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2383 goto fh_unlock_and_return;
2387 btv->init.ovfmt = fmt;
2388 btv->init.fmt = fmt;
2389 btv->fbuf.base = fbuf->base;
2390 btv->fbuf.fmt.width = fbuf->width;
2391 btv->fbuf.fmt.height = fbuf->height;
2392 if (fbuf->bytesperline)
2393 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2395 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2401 case VIDIOC_OVERLAY:
2403 struct bttv_buffer *new;
2408 if (NULL == btv->fbuf.base)
2410 if (!fh->ov.setup_ok) {
2411 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2416 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2419 down(&fh->cap.lock);
2421 fh->ov.tvnorm = btv->tvnorm;
2422 new = videobuf_alloc(sizeof(*new));
2423 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2429 retval = bttv_switch_overlay(btv,fh,new);
2436 struct video_mbuf *mbuf = arg;
2439 down(&fh->cap.lock);
2440 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2443 goto fh_unlock_and_return;
2444 memset(mbuf,0,sizeof(*mbuf));
2445 mbuf->frames = gbuffers;
2446 mbuf->size = gbuffers * gbufsize;
2447 for (i = 0; i < gbuffers; i++)
2448 mbuf->offsets[i] = i * gbufsize;
2452 case VIDIOCMCAPTURE:
2454 struct video_mmap *vm = arg;
2455 struct bttv_buffer *buf;
2456 enum v4l2_field field;
2458 if (vm->frame >= VIDEO_MAX_FRAME)
2461 down(&fh->cap.lock);
2463 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2465 goto fh_unlock_and_return;
2466 if (0 == buf->vb.baddr)
2467 goto fh_unlock_and_return;
2468 if (buf->vb.state == STATE_QUEUED ||
2469 buf->vb.state == STATE_ACTIVE)
2470 goto fh_unlock_and_return;
2472 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2473 ? V4L2_FIELD_INTERLACED
2474 : V4L2_FIELD_BOTTOM;
2475 retval = bttv_prepare_buffer(btv,buf,
2476 format_by_palette(vm->format),
2477 vm->width,vm->height,field);
2479 goto fh_unlock_and_return;
2480 spin_lock_irqsave(&btv->s_lock,flags);
2481 buffer_queue(&fh->cap,&buf->vb);
2482 spin_unlock_irqrestore(&btv->s_lock,flags);
2489 struct bttv_buffer *buf;
2491 if (*frame >= VIDEO_MAX_FRAME)
2494 down(&fh->cap.lock);
2496 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2498 goto fh_unlock_and_return;
2499 retval = videobuf_waiton(&buf->vb,0,1);
2501 goto fh_unlock_and_return;
2502 switch (buf->vb.state) {
2507 videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
2508 bttv_dma_free(btv,buf);
2520 struct vbi_format *fmt = (void *) arg;
2521 struct v4l2_format fmt2;
2523 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2524 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2528 bttv_vbi_get_fmt(fh, &fmt2);
2530 memset(fmt,0,sizeof(*fmt));
2531 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2532 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2533 fmt->sample_format = VIDEO_PALETTE_RAW;
2534 fmt->start[0] = fmt2.fmt.vbi.start[0];
2535 fmt->count[0] = fmt2.fmt.vbi.count[0];
2536 fmt->start[1] = fmt2.fmt.vbi.start[1];
2537 fmt->count[1] = fmt2.fmt.vbi.count[1];
2538 if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
2539 fmt->flags |= V4L2_VBI_UNSYNC;
2540 if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
2541 fmt->flags |= V4L2_VBI_INTERLACED;
2546 struct vbi_format *fmt = (void *) arg;
2547 struct v4l2_format fmt2;
2549 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2552 bttv_vbi_get_fmt(fh, &fmt2);
2554 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2555 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2556 fmt->sample_format != VIDEO_PALETTE_RAW ||
2557 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2558 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2559 fmt->count[0] != fmt->count[1] ||
2560 fmt->count[0] < 1 ||
2561 fmt->count[0] > 32 /* VBI_MAXLINES */)
2564 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2577 return bttv_common_ioctls(btv,cmd,arg);
2579 /* *** v4l2 *** ************************************************ */
2580 case VIDIOC_QUERYCAP:
2582 struct v4l2_capability *cap = arg;
2586 strcpy(cap->driver,"bttv");
2587 strlcpy(cap->card,btv->video_dev->name,sizeof(cap->card));
2588 sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
2589 cap->version = BTTV_VERSION_CODE;
2591 V4L2_CAP_VIDEO_CAPTURE |
2592 V4L2_CAP_VBI_CAPTURE |
2593 V4L2_CAP_READWRITE |
2595 if (no_overlay <= 0)
2596 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2598 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2599 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2600 cap->capabilities |= V4L2_CAP_TUNER;
2604 case VIDIOC_ENUM_FMT:
2606 struct v4l2_fmtdesc *f = arg;
2607 enum v4l2_buf_type type;
2612 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2617 memset(f,0,sizeof(*f));
2620 f->pixelformat = V4L2_PIX_FMT_GREY;
2621 strcpy(f->description,"vbi data");
2625 /* video capture + overlay */
2627 for (i = 0; i < BTTV_FORMATS; i++) {
2628 if (bttv_formats[i].fourcc != -1)
2630 if ((unsigned int)index == f->index)
2633 if (BTTV_FORMATS == i)
2637 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2639 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2640 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2646 memset(f,0,sizeof(*f));
2649 f->pixelformat = bttv_formats[i].fourcc;
2650 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2654 case VIDIOC_TRY_FMT:
2656 struct v4l2_format *f = arg;
2657 return bttv_try_fmt(fh,btv,f);
2661 struct v4l2_format *f = arg;
2662 return bttv_g_fmt(fh,f);
2666 struct v4l2_format *f = arg;
2667 return bttv_s_fmt(fh,btv,f);
2672 struct v4l2_framebuffer *fb = arg;
2675 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2677 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2682 struct v4l2_framebuffer *fb = arg;
2683 const struct bttv_format *fmt;
2685 if(!capable(CAP_SYS_ADMIN) &&
2686 !capable(CAP_SYS_RAWIO))
2690 fmt = format_by_fourcc(fb->fmt.pixelformat);
2693 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2696 down(&fh->cap.lock);
2698 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2699 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2700 goto fh_unlock_and_return;
2701 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2702 goto fh_unlock_and_return;
2706 btv->fbuf.base = fb->base;
2707 btv->fbuf.fmt.width = fb->fmt.width;
2708 btv->fbuf.fmt.height = fb->fmt.height;
2709 if (0 != fb->fmt.bytesperline)
2710 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2712 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2716 btv->init.ovfmt = fmt;
2717 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2720 fh->ov.w.width = fb->fmt.width;
2721 fh->ov.w.height = fb->fmt.height;
2722 btv->init.ov.w.width = fb->fmt.width;
2723 btv->init.ov.w.height = fb->fmt.height;
2725 kfree(fh->ov.clips);
2726 fh->ov.clips = NULL;
2729 if (check_btres(fh, RESOURCE_OVERLAY)) {
2730 struct bttv_buffer *new;
2732 new = videobuf_alloc(sizeof(*new));
2733 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2734 retval = bttv_switch_overlay(btv,fh,new);
2741 case VIDIOC_REQBUFS:
2742 return videobuf_reqbufs(bttv_queue(fh),arg);
2744 case VIDIOC_QUERYBUF:
2745 return videobuf_querybuf(bttv_queue(fh),arg);
2748 return videobuf_qbuf(bttv_queue(fh),arg);
2751 return videobuf_dqbuf(bttv_queue(fh),arg,
2752 file->f_flags & O_NONBLOCK);
2754 case VIDIOC_STREAMON:
2756 int res = bttv_resource(fh);
2758 if (!check_alloc_btres(btv,fh,res))
2760 return videobuf_streamon(bttv_queue(fh));
2762 case VIDIOC_STREAMOFF:
2764 int res = bttv_resource(fh);
2766 retval = videobuf_streamoff(bttv_queue(fh));
2769 free_btres(btv,fh,res);
2773 case VIDIOC_QUERYCTRL:
2775 struct v4l2_queryctrl *c = arg;
2778 if ((c->id < V4L2_CID_BASE ||
2779 c->id >= V4L2_CID_LASTP1) &&
2780 (c->id < V4L2_CID_PRIVATE_BASE ||
2781 c->id >= V4L2_CID_PRIVATE_LASTP1))
2783 for (i = 0; i < BTTV_CTLS; i++)
2784 if (bttv_ctls[i].id == c->id)
2786 if (i == BTTV_CTLS) {
2791 if (i >= 4 && i <= 8) {
2792 struct video_audio va;
2793 memset(&va,0,sizeof(va));
2794 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2795 if (btv->audio_hook)
2796 btv->audio_hook(btv,&va,0);
2797 switch (bttv_ctls[i].id) {
2798 case V4L2_CID_AUDIO_VOLUME:
2799 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2802 case V4L2_CID_AUDIO_BALANCE:
2803 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2806 case V4L2_CID_AUDIO_BASS:
2807 if (!(va.flags & VIDEO_AUDIO_BASS))
2810 case V4L2_CID_AUDIO_TREBLE:
2811 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2819 return get_control(btv,arg);
2821 return set_control(btv,arg);
2824 struct v4l2_streamparm *parm = arg;
2825 struct v4l2_standard s;
2826 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2828 memset(parm,0,sizeof(*parm));
2829 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2830 bttv_tvnorms[btv->tvnorm].name);
2831 parm->parm.capture.timeperframe = s.frameperiod;
2835 case VIDIOC_G_PRIORITY:
2837 enum v4l2_priority *p = arg;
2839 *p = v4l2_prio_max(&btv->prio);
2842 case VIDIOC_S_PRIORITY:
2844 enum v4l2_priority *prio = arg;
2846 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2849 case VIDIOC_ENUMSTD:
2852 case VIDIOC_ENUMINPUT:
2853 case VIDIOC_G_INPUT:
2854 case VIDIOC_S_INPUT:
2855 case VIDIOC_G_TUNER:
2856 case VIDIOC_S_TUNER:
2857 case VIDIOC_G_FREQUENCY:
2858 case VIDIOC_S_FREQUENCY:
2859 return bttv_common_ioctls(btv,cmd,arg);
2862 return -ENOIOCTLCMD;
2866 fh_unlock_and_return:
2871 static int bttv_ioctl(struct inode *inode, struct file *file,
2872 unsigned int cmd, unsigned long arg)
2874 struct bttv_fh *fh = file->private_data;
2878 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2879 return fh->lines * 2 * 2048;
2881 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2885 static ssize_t bttv_read(struct file *file, char __user *data,
2886 size_t count, loff_t *ppos)
2888 struct bttv_fh *fh = file->private_data;
2891 if (fh->btv->errors)
2892 bttv_reinit_bt848(fh->btv);
2893 dprintk("bttv%d: read count=%d type=%s\n",
2894 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2897 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2898 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2900 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2901 file->f_flags & O_NONBLOCK);
2903 case V4L2_BUF_TYPE_VBI_CAPTURE:
2904 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2906 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2907 file->f_flags & O_NONBLOCK);
2915 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2917 struct bttv_fh *fh = file->private_data;
2918 struct bttv_buffer *buf;
2919 enum v4l2_field field;
2921 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2922 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2924 return videobuf_poll_stream(file, &fh->vbi, wait);
2927 if (check_btres(fh,RESOURCE_VIDEO)) {
2928 /* streaming capture */
2929 if (list_empty(&fh->cap.stream))
2931 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2933 /* read() capture */
2934 down(&fh->cap.lock);
2935 if (NULL == fh->cap.read_buf) {
2936 /* need to capture a new frame */
2937 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2941 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2942 if (NULL == fh->cap.read_buf) {
2946 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2947 field = videobuf_next_field(&fh->cap);
2948 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2952 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2953 fh->cap.read_off = 0;
2956 buf = (struct bttv_buffer*)fh->cap.read_buf;
2959 poll_wait(file, &buf->vb.done, wait);
2960 if (buf->vb.state == STATE_DONE ||
2961 buf->vb.state == STATE_ERROR)
2962 return POLLIN|POLLRDNORM;
2966 static int bttv_open(struct inode *inode, struct file *file)
2968 int minor = iminor(inode);
2969 struct bttv *btv = NULL;
2971 enum v4l2_buf_type type = 0;
2974 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
2976 for (i = 0; i < bttv_num; i++) {
2977 if (bttvs[i].video_dev &&
2978 bttvs[i].video_dev->minor == minor) {
2980 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2983 if (bttvs[i].vbi_dev &&
2984 bttvs[i].vbi_dev->minor == minor) {
2986 type = V4L2_BUF_TYPE_VBI_CAPTURE;
2993 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
2994 btv->c.nr,v4l2_type_names[type]);
2996 /* allocate per filehandle data */
2997 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3000 file->private_data = fh;
3003 fh->ov.setup_ok = 0;
3004 v4l2_prio_open(&btv->prio,&fh->prio);
3006 videobuf_queue_init(&fh->cap, &bttv_video_qops,
3007 btv->c.pci, &btv->s_lock,
3008 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3009 V4L2_FIELD_INTERLACED,
3010 sizeof(struct bttv_buffer),
3012 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3013 btv->c.pci, &btv->s_lock,
3014 V4L2_BUF_TYPE_VBI_CAPTURE,
3016 sizeof(struct bttv_buffer),
3018 i2c_vidiocschan(btv);
3021 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
3022 bttv_vbi_setlines(fh,btv,16);
3023 bttv_field_count(btv);
3027 static int bttv_release(struct inode *inode, struct file *file)
3029 struct bttv_fh *fh = file->private_data;
3030 struct bttv *btv = fh->btv;
3032 /* turn off overlay */
3033 if (check_btres(fh, RESOURCE_OVERLAY))
3034 bttv_switch_overlay(btv,fh,NULL);
3036 /* stop video capture */
3037 if (check_btres(fh, RESOURCE_VIDEO)) {
3038 videobuf_streamoff(&fh->cap);
3039 free_btres(btv,fh,RESOURCE_VIDEO);
3041 if (fh->cap.read_buf) {
3042 buffer_release(&fh->cap,fh->cap.read_buf);
3043 kfree(fh->cap.read_buf);
3046 /* stop vbi capture */
3047 if (check_btres(fh, RESOURCE_VBI)) {
3048 if (fh->vbi.streaming)
3049 videobuf_streamoff(&fh->vbi);
3050 if (fh->vbi.reading)
3051 videobuf_read_stop(&fh->vbi);
3052 free_btres(btv,fh,RESOURCE_VBI);
3056 videobuf_mmap_free(&fh->cap);