3 bttv - Bt848 frame grabber driver
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
6 & Marcus Metzler <mocm@thp.uni-koeln.de>
7 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
38 #include <media/v4l2-common.h>
39 #include <media/tvaudio.h>
40 #include <media/msp3400.h>
42 #include <linux/dma-mapping.h>
45 #include <asm/byteorder.h>
47 #include <media/rds.h>
50 unsigned int bttv_num; /* number of Bt848s in use */
51 struct bttv bttvs[BTTV_MAX];
53 unsigned int bttv_debug;
54 unsigned int bttv_verbose = 1;
55 unsigned int bttv_gpio;
57 /* config variables */
59 static unsigned int bigendian=1;
61 static unsigned int bigendian;
63 static unsigned int radio[BTTV_MAX];
64 static unsigned int irq_debug;
65 static unsigned int gbuffers = 8;
66 static unsigned int gbufsize = 0x208000;
68 static int video_nr = -1;
69 static int radio_nr = -1;
70 static int vbi_nr = -1;
71 static int debug_latency;
73 static unsigned int fdsr;
76 static unsigned int combfilter;
77 static unsigned int lumafilter;
78 static unsigned int automute = 1;
79 static unsigned int chroma_agc;
80 static unsigned int adc_crush = 1;
81 static unsigned int whitecrush_upper = 0xCF;
82 static unsigned int whitecrush_lower = 0x7F;
83 static unsigned int vcr_hack;
84 static unsigned int irq_iswitch;
85 static unsigned int uv_ratio = 50;
86 static unsigned int full_luma_range;
87 static unsigned int coring;
88 extern int no_overlay;
90 /* API features (turn on/off stuff for testing) */
91 static unsigned int v4l2 = 1;
94 module_param(bttv_verbose, int, 0644);
95 module_param(bttv_gpio, int, 0644);
96 module_param(bttv_debug, int, 0644);
97 module_param(irq_debug, int, 0644);
98 module_param(debug_latency, int, 0644);
100 module_param(fdsr, int, 0444);
101 module_param(video_nr, int, 0444);
102 module_param(radio_nr, int, 0444);
103 module_param(vbi_nr, int, 0444);
104 module_param(gbuffers, int, 0444);
105 module_param(gbufsize, int, 0444);
107 module_param(v4l2, int, 0644);
108 module_param(bigendian, int, 0644);
109 module_param(irq_iswitch, int, 0644);
110 module_param(combfilter, int, 0444);
111 module_param(lumafilter, int, 0444);
112 module_param(automute, int, 0444);
113 module_param(chroma_agc, int, 0444);
114 module_param(adc_crush, int, 0444);
115 module_param(whitecrush_upper, int, 0444);
116 module_param(whitecrush_lower, int, 0444);
117 module_param(vcr_hack, int, 0444);
118 module_param(uv_ratio, int, 0444);
119 module_param(full_luma_range, int, 0444);
120 module_param(coring, int, 0444);
122 module_param_array(radio, int, NULL, 0444);
124 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
125 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
126 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
127 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
128 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
129 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
130 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
131 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
132 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
133 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
134 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
135 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
136 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
137 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
138 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
139 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
140 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
141 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
143 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
144 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
145 MODULE_LICENSE("GPL");
147 /* ----------------------------------------------------------------------- */
150 static ssize_t show_card(struct class_device *cd, char *buf)
152 struct video_device *vfd = to_video_device(cd);
153 struct bttv *btv = dev_get_drvdata(vfd->dev);
154 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
156 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
158 /* ----------------------------------------------------------------------- */
161 /* special timing tables from conexant... */
162 static u8 SRAM_Table[][60] =
164 /* PAL digital input over GPIO[7:0] */
166 45, // 45 bytes following
167 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
168 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
169 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
170 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
171 0x37,0x00,0xAF,0x21,0x00
173 /* NTSC digital input over GPIO[7:0] */
175 51, // 51 bytes following
176 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
177 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
178 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
179 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
180 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
183 // TGB_NTSC392 // quartzsight
184 // This table has been modified to be used for Fusion Rev D
186 0x2A, // size of table = 42
187 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
188 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
189 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
190 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
195 const struct bttv_tvnorm bttv_tvnorms[] = {
197 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
198 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
200 .v4l2_id = V4L2_STD_PAL,
208 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
209 .scaledtwidth = 1135,
215 /* ITU-R frame line number of the first VBI line
216 we can capture, of the first and second field. */
217 .vbistart = { 7,320 },
219 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
227 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
234 .vbistart = { 10, 273 },
236 .v4l2_id = V4L2_STD_SECAM,
244 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
245 .scaledtwidth = 1135,
250 .sram = 0, /* like PAL, correct? */
251 .vbistart = { 7, 320 },
253 .v4l2_id = V4L2_STD_PAL_Nc,
261 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
268 .vbistart = { 7, 320 },
270 .v4l2_id = V4L2_STD_PAL_M,
278 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
285 .vbistart = { 10, 273 },
287 .v4l2_id = V4L2_STD_PAL_N,
295 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
302 .vbistart = { 7, 320},
304 .v4l2_id = V4L2_STD_NTSC_M_JP,
312 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
319 .vbistart = {10, 273},
321 /* that one hopefully works with the strange timing
322 * which video recorders produce when playing a NTSC
323 * tape on a PAL TV ... */
324 .v4l2_id = V4L2_STD_PAL_60,
332 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
333 .scaledtwidth = 1135,
340 .vbistart = { 10, 273 },
343 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
345 /* ----------------------------------------------------------------------- */
347 packed pixel formats must come first */
348 static const struct bttv_format bttv_formats[] = {
350 .name = "8 bpp, gray",
351 .palette = VIDEO_PALETTE_GREY,
352 .fourcc = V4L2_PIX_FMT_GREY,
353 .btformat = BT848_COLOR_FMT_Y8,
355 .flags = FORMAT_FLAGS_PACKED,
357 .name = "8 bpp, dithered color",
358 .palette = VIDEO_PALETTE_HI240,
359 .fourcc = V4L2_PIX_FMT_HI240,
360 .btformat = BT848_COLOR_FMT_RGB8,
362 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
364 .name = "15 bpp RGB, le",
365 .palette = VIDEO_PALETTE_RGB555,
366 .fourcc = V4L2_PIX_FMT_RGB555,
367 .btformat = BT848_COLOR_FMT_RGB15,
369 .flags = FORMAT_FLAGS_PACKED,
371 .name = "15 bpp RGB, be",
373 .fourcc = V4L2_PIX_FMT_RGB555X,
374 .btformat = BT848_COLOR_FMT_RGB15,
375 .btswap = 0x03, /* byteswap */
377 .flags = FORMAT_FLAGS_PACKED,
379 .name = "16 bpp RGB, le",
380 .palette = VIDEO_PALETTE_RGB565,
381 .fourcc = V4L2_PIX_FMT_RGB565,
382 .btformat = BT848_COLOR_FMT_RGB16,
384 .flags = FORMAT_FLAGS_PACKED,
386 .name = "16 bpp RGB, be",
388 .fourcc = V4L2_PIX_FMT_RGB565X,
389 .btformat = BT848_COLOR_FMT_RGB16,
390 .btswap = 0x03, /* byteswap */
392 .flags = FORMAT_FLAGS_PACKED,
394 .name = "24 bpp RGB, le",
395 .palette = VIDEO_PALETTE_RGB24,
396 .fourcc = V4L2_PIX_FMT_BGR24,
397 .btformat = BT848_COLOR_FMT_RGB24,
399 .flags = FORMAT_FLAGS_PACKED,
401 .name = "32 bpp RGB, le",
402 .palette = VIDEO_PALETTE_RGB32,
403 .fourcc = V4L2_PIX_FMT_BGR32,
404 .btformat = BT848_COLOR_FMT_RGB32,
406 .flags = FORMAT_FLAGS_PACKED,
408 .name = "32 bpp RGB, be",
410 .fourcc = V4L2_PIX_FMT_RGB32,
411 .btformat = BT848_COLOR_FMT_RGB32,
412 .btswap = 0x0f, /* byte+word swap */
414 .flags = FORMAT_FLAGS_PACKED,
416 .name = "4:2:2, packed, YUYV",
417 .palette = VIDEO_PALETTE_YUV422,
418 .fourcc = V4L2_PIX_FMT_YUYV,
419 .btformat = BT848_COLOR_FMT_YUY2,
421 .flags = FORMAT_FLAGS_PACKED,
423 .name = "4:2:2, packed, YUYV",
424 .palette = VIDEO_PALETTE_YUYV,
425 .fourcc = V4L2_PIX_FMT_YUYV,
426 .btformat = BT848_COLOR_FMT_YUY2,
428 .flags = FORMAT_FLAGS_PACKED,
430 .name = "4:2:2, packed, UYVY",
431 .palette = VIDEO_PALETTE_UYVY,
432 .fourcc = V4L2_PIX_FMT_UYVY,
433 .btformat = BT848_COLOR_FMT_YUY2,
434 .btswap = 0x03, /* byteswap */
436 .flags = FORMAT_FLAGS_PACKED,
438 .name = "4:2:2, planar, Y-Cb-Cr",
439 .palette = VIDEO_PALETTE_YUV422P,
440 .fourcc = V4L2_PIX_FMT_YUV422P,
441 .btformat = BT848_COLOR_FMT_YCrCb422,
443 .flags = FORMAT_FLAGS_PLANAR,
447 .name = "4:2:0, planar, Y-Cb-Cr",
448 .palette = VIDEO_PALETTE_YUV420P,
449 .fourcc = V4L2_PIX_FMT_YUV420,
450 .btformat = BT848_COLOR_FMT_YCrCb422,
452 .flags = FORMAT_FLAGS_PLANAR,
456 .name = "4:2:0, planar, Y-Cr-Cb",
458 .fourcc = V4L2_PIX_FMT_YVU420,
459 .btformat = BT848_COLOR_FMT_YCrCb422,
461 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
465 .name = "4:1:1, planar, Y-Cb-Cr",
466 .palette = VIDEO_PALETTE_YUV411P,
467 .fourcc = V4L2_PIX_FMT_YUV411P,
468 .btformat = BT848_COLOR_FMT_YCrCb411,
470 .flags = FORMAT_FLAGS_PLANAR,
474 .name = "4:1:0, planar, Y-Cb-Cr",
475 .palette = VIDEO_PALETTE_YUV410P,
476 .fourcc = V4L2_PIX_FMT_YUV410,
477 .btformat = BT848_COLOR_FMT_YCrCb411,
479 .flags = FORMAT_FLAGS_PLANAR,
483 .name = "4:1:0, planar, Y-Cr-Cb",
485 .fourcc = V4L2_PIX_FMT_YVU410,
486 .btformat = BT848_COLOR_FMT_YCrCb411,
488 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
492 .name = "raw scanlines",
493 .palette = VIDEO_PALETTE_RAW,
495 .btformat = BT848_COLOR_FMT_RAW,
497 .flags = FORMAT_FLAGS_RAW,
500 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
502 /* ----------------------------------------------------------------------- */
504 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
505 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
506 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
507 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
508 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
509 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
510 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
511 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
512 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
513 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
514 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
515 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
517 static const struct v4l2_queryctrl no_ctl = {
519 .flags = V4L2_CTRL_FLAG_DISABLED,
521 static const struct v4l2_queryctrl bttv_ctls[] = {
524 .id = V4L2_CID_BRIGHTNESS,
525 .name = "Brightness",
529 .default_value = 32768,
530 .type = V4L2_CTRL_TYPE_INTEGER,
532 .id = V4L2_CID_CONTRAST,
537 .default_value = 32768,
538 .type = V4L2_CTRL_TYPE_INTEGER,
540 .id = V4L2_CID_SATURATION,
541 .name = "Saturation",
545 .default_value = 32768,
546 .type = V4L2_CTRL_TYPE_INTEGER,
553 .default_value = 32768,
554 .type = V4L2_CTRL_TYPE_INTEGER,
558 .id = V4L2_CID_AUDIO_MUTE,
562 .type = V4L2_CTRL_TYPE_BOOLEAN,
564 .id = V4L2_CID_AUDIO_VOLUME,
569 .default_value = 65535,
570 .type = V4L2_CTRL_TYPE_INTEGER,
572 .id = V4L2_CID_AUDIO_BALANCE,
577 .default_value = 32768,
578 .type = V4L2_CTRL_TYPE_INTEGER,
580 .id = V4L2_CID_AUDIO_BASS,
585 .default_value = 32768,
586 .type = V4L2_CTRL_TYPE_INTEGER,
588 .id = V4L2_CID_AUDIO_TREBLE,
593 .default_value = 32768,
594 .type = V4L2_CTRL_TYPE_INTEGER,
596 /* --- private --- */
598 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
599 .name = "chroma agc",
602 .type = V4L2_CTRL_TYPE_BOOLEAN,
604 .id = V4L2_CID_PRIVATE_COMBFILTER,
605 .name = "combfilter",
608 .type = V4L2_CTRL_TYPE_BOOLEAN,
610 .id = V4L2_CID_PRIVATE_AUTOMUTE,
614 .type = V4L2_CTRL_TYPE_BOOLEAN,
616 .id = V4L2_CID_PRIVATE_LUMAFILTER,
617 .name = "luma decimation filter",
620 .type = V4L2_CTRL_TYPE_BOOLEAN,
622 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
626 .type = V4L2_CTRL_TYPE_BOOLEAN,
628 .id = V4L2_CID_PRIVATE_VCR_HACK,
632 .type = V4L2_CTRL_TYPE_BOOLEAN,
634 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
635 .name = "whitecrush upper",
639 .default_value = 0xCF,
640 .type = V4L2_CTRL_TYPE_INTEGER,
642 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
643 .name = "whitecrush lower",
647 .default_value = 0x7F,
648 .type = V4L2_CTRL_TYPE_INTEGER,
650 .id = V4L2_CID_PRIVATE_UV_RATIO,
656 .type = V4L2_CTRL_TYPE_INTEGER,
658 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
659 .name = "full luma range",
662 .type = V4L2_CTRL_TYPE_BOOLEAN,
664 .id = V4L2_CID_PRIVATE_CORING,
670 .type = V4L2_CTRL_TYPE_INTEGER,
676 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
678 /* ----------------------------------------------------------------------- */
679 /* resource management */
682 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
684 if (fh->resources & bit)
685 /* have it already allocated */
689 mutex_lock(&btv->reslock);
690 if (btv->resources & bit) {
691 /* no, someone else uses it */
692 mutex_unlock(&btv->reslock);
695 /* it's free, grab it */
696 fh->resources |= bit;
697 btv->resources |= bit;
698 mutex_unlock(&btv->reslock);
703 int check_btres(struct bttv_fh *fh, int bit)
705 return (fh->resources & bit);
709 int locked_btres(struct bttv *btv, int bit)
711 return (btv->resources & bit);
715 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
717 if ((fh->resources & bits) != bits) {
718 /* trying to free ressources not allocated by us ... */
719 printk("bttv: BUG! (btres)\n");
721 mutex_lock(&btv->reslock);
722 fh->resources &= ~bits;
723 btv->resources &= ~bits;
724 mutex_unlock(&btv->reslock);
727 /* ----------------------------------------------------------------------- */
728 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
730 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
731 PLL_X = Reference pre-divider (0=1, 1=2)
732 PLL_C = Post divider (0=6, 1=4)
733 PLL_I = Integer input
734 PLL_F = Fractional input
736 F_input = 28.636363 MHz:
737 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
740 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
742 unsigned char fl, fh, fi;
744 /* prevent overflows */
757 btwrite(fl, BT848_PLL_F_LO);
758 btwrite(fh, BT848_PLL_F_HI);
759 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
762 static void set_pll(struct bttv *btv)
766 if (!btv->pll.pll_crystal)
769 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
770 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
774 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
776 if (btv->pll.pll_current == 0)
778 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
779 btv->c.nr,btv->pll.pll_ifreq);
780 btwrite(0x00,BT848_TGCTRL);
781 btwrite(0x00,BT848_PLL_XCI);
782 btv->pll.pll_current = 0;
786 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
787 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
788 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
790 for (i=0; i<10; i++) {
791 /* Let other people run while the PLL stabilizes */
795 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
796 btwrite(0,BT848_DSTATUS);
798 btwrite(0x08,BT848_TGCTRL);
799 btv->pll.pll_current = btv->pll.pll_ofreq;
800 bttv_printk(" ok\n");
804 btv->pll.pll_current = -1;
805 bttv_printk("failed\n");
809 /* used to switch between the bt848's analog/digital video capture modes */
810 static void bt848A_set_timing(struct bttv *btv)
813 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
814 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
816 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
817 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
818 btv->c.nr,table_idx);
820 /* timing change...reset timing generator address */
821 btwrite(0x00, BT848_TGCTRL);
822 btwrite(0x02, BT848_TGCTRL);
823 btwrite(0x00, BT848_TGCTRL);
825 len=SRAM_Table[table_idx][0];
826 for(i = 1; i <= len; i++)
827 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
828 btv->pll.pll_ofreq = 27000000;
831 btwrite(0x11, BT848_TGCTRL);
832 btwrite(0x41, BT848_DVSIF);
834 btv->pll.pll_ofreq = fsc;
836 btwrite(0x0, BT848_DVSIF);
840 /* ----------------------------------------------------------------------- */
842 static void bt848_bright(struct bttv *btv, int bright)
846 // printk("bttv: set bright: %d\n",bright); // DEBUG
847 btv->bright = bright;
849 /* We want -128 to 127 we get 0-65535 */
850 value = (bright >> 8) - 128;
851 btwrite(value & 0xff, BT848_BRIGHT);
854 static void bt848_hue(struct bttv *btv, int hue)
861 value = (hue >> 8) - 128;
862 btwrite(value & 0xff, BT848_HUE);
865 static void bt848_contrast(struct bttv *btv, int cont)
869 btv->contrast = cont;
873 hibit = (value >> 6) & 4;
874 btwrite(value & 0xff, BT848_CONTRAST_LO);
875 btaor(hibit, ~4, BT848_E_CONTROL);
876 btaor(hibit, ~4, BT848_O_CONTROL);
879 static void bt848_sat(struct bttv *btv, int color)
881 int val_u,val_v,hibits;
883 btv->saturation = color;
885 /* 0-511 for the color */
886 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
887 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
888 hibits = (val_u >> 7) & 2;
889 hibits |= (val_v >> 8) & 1;
890 btwrite(val_u & 0xff, BT848_SAT_U_LO);
891 btwrite(val_v & 0xff, BT848_SAT_V_LO);
892 btaor(hibits, ~3, BT848_E_CONTROL);
893 btaor(hibits, ~3, BT848_O_CONTROL);
896 /* ----------------------------------------------------------------------- */
899 video_mux(struct bttv *btv, unsigned int input)
903 if (input >= bttv_tvcards[btv->c.type].video_inputs)
906 /* needed by RemoteVideo MX */
907 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
909 gpio_inout(mask2,mask2);
911 if (input == btv->svhs) {
912 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
913 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
915 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
916 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
918 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
919 btaor(mux<<5, ~(3<<5), BT848_IFORM);
920 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
921 btv->c.nr,input,mux);
923 /* card specific hook */
924 if(bttv_tvcards[btv->c.type].muxsel_hook)
925 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
929 static char *audio_modes[] = {
930 "audio: tuner", "audio: radio", "audio: extern",
931 "audio: intern", "audio: mute"
935 audio_mux(struct bttv *btv, int input, int mute)
937 int gpio_val, signal;
938 struct v4l2_control ctrl;
939 struct i2c_client *c;
941 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
942 bttv_tvcards[btv->c.type].gpiomask);
943 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
949 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
952 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
954 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
956 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
958 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
962 ctrl.id = V4L2_CID_AUDIO_MUTE;
963 ctrl.value = btv->mute;
964 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
965 c = btv->i2c_msp34xx_client;
967 struct v4l2_routing route;
969 /* Note: the inputs tuner/radio/extern/intern are translated
970 to msp routings. This assumes common behavior for all msp3400
971 based TV cards. When this assumption fails, then the
972 specific MSP routing must be added to the card table.
973 For now this is sufficient. */
975 case TVAUDIO_INPUT_RADIO:
976 route.input = MSP_INPUT(MSP_IN_SCART_2, MSP_IN_TUNER_1,
977 MSP_DSP_OUT_SCART, MSP_DSP_OUT_SCART);
979 case TVAUDIO_INPUT_EXTERN:
980 route.input = MSP_INPUT(MSP_IN_SCART_1, MSP_IN_TUNER_1,
981 MSP_DSP_OUT_SCART, MSP_DSP_OUT_SCART);
983 case TVAUDIO_INPUT_INTERN:
984 /* Yes, this is the same input as for RADIO. I doubt
985 if this is ever used. The only board with an INTERN
986 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
987 that was tested. My guess is that the whole INTERN
988 input does not work. */
989 route.input = MSP_INPUT(MSP_IN_SCART_2, MSP_IN_TUNER_1,
990 MSP_DSP_OUT_SCART, MSP_DSP_OUT_SCART);
992 case TVAUDIO_INPUT_TUNER:
994 route.input = MSP_INPUT_DEFAULT;
997 route.output = MSP_OUTPUT_DEFAULT;
998 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1000 c = btv->i2c_tvaudio_client;
1002 struct v4l2_routing route;
1004 route.input = input;
1006 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1012 audio_mute(struct bttv *btv, int mute)
1014 return audio_mux(btv, btv->audio, mute);
1018 audio_input(struct bttv *btv, int input)
1020 return audio_mux(btv, input, btv->mute);
1024 i2c_vidiocschan(struct bttv *btv)
1026 v4l2_std_id std = bttv_tvnorms[btv->tvnorm].v4l2_id;
1028 bttv_call_i2c_clients(btv, VIDIOC_S_INPUT, &btv->input);
1029 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &std);
1030 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM)
1031 bttv_tda9880_setnorm(btv,btv->tvnorm);
1035 set_tvnorm(struct bttv *btv, unsigned int norm)
1037 const struct bttv_tvnorm *tvnorm;
1039 if (norm < 0 || norm >= BTTV_TVNORMS)
1043 tvnorm = &bttv_tvnorms[norm];
1045 btwrite(tvnorm->adelay, BT848_ADELAY);
1046 btwrite(tvnorm->bdelay, BT848_BDELAY);
1047 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1049 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1050 btwrite(1, BT848_VBI_PACK_DEL);
1051 bt848A_set_timing(btv);
1053 switch (btv->c.type) {
1054 case BTTV_BOARD_VOODOOTV_FM:
1055 bttv_tda9880_setnorm(btv,norm);
1062 set_input(struct bttv *btv, unsigned int input)
1064 unsigned long flags;
1068 spin_lock_irqsave(&btv->s_lock,flags);
1069 if (btv->curr.frame_irq) {
1070 /* active capture -> delayed input switch */
1071 btv->new_input = input;
1073 video_mux(btv,input);
1075 spin_unlock_irqrestore(&btv->s_lock,flags);
1077 video_mux(btv,input);
1079 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1080 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
1081 set_tvnorm(btv,btv->tvnorm);
1082 i2c_vidiocschan(btv);
1085 static void init_irqreg(struct bttv *btv)
1088 btwrite(0xfffffUL, BT848_INT_STAT);
1090 if (bttv_tvcards[btv->c.type].no_video) {
1092 btwrite(BT848_INT_I2CDONE,
1096 btwrite((btv->triton1) |
1097 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1099 (fdsr ? BT848_INT_FDSR : 0) |
1100 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1101 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1107 static void init_bt848(struct bttv *btv)
1111 if (bttv_tvcards[btv->c.type].no_video) {
1112 /* very basic init only */
1117 btwrite(0x00, BT848_CAP_CTL);
1118 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1119 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1121 /* set planar and packed mode trigger points and */
1122 /* set rising edge of inverted GPINTR pin as irq trigger */
1123 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1124 BT848_GPIO_DMA_CTL_PLTP1_16|
1125 BT848_GPIO_DMA_CTL_PLTP23_16|
1126 BT848_GPIO_DMA_CTL_GPINTC|
1127 BT848_GPIO_DMA_CTL_GPINTI,
1128 BT848_GPIO_DMA_CTL);
1130 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1131 btwrite(val, BT848_E_SCLOOP);
1132 btwrite(val, BT848_O_SCLOOP);
1134 btwrite(0x20, BT848_E_VSCALE_HI);
1135 btwrite(0x20, BT848_O_VSCALE_HI);
1136 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1139 btwrite(whitecrush_upper, BT848_WC_UP);
1140 btwrite(whitecrush_lower, BT848_WC_DOWN);
1142 if (btv->opt_lumafilter) {
1143 btwrite(0, BT848_E_CONTROL);
1144 btwrite(0, BT848_O_CONTROL);
1146 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1147 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1150 bt848_bright(btv, btv->bright);
1151 bt848_hue(btv, btv->hue);
1152 bt848_contrast(btv, btv->contrast);
1153 bt848_sat(btv, btv->saturation);
1159 static void bttv_reinit_bt848(struct bttv *btv)
1161 unsigned long flags;
1164 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1165 spin_lock_irqsave(&btv->s_lock,flags);
1167 bttv_set_dma(btv,0);
1168 spin_unlock_irqrestore(&btv->s_lock,flags);
1171 btv->pll.pll_current = -1;
1172 set_input(btv,btv->input);
1175 static int get_control(struct bttv *btv, struct v4l2_control *c)
1177 struct video_audio va;
1180 for (i = 0; i < BTTV_CTLS; i++)
1181 if (bttv_ctls[i].id == c->id)
1185 if (btv->audio_hook && i >= 4 && i <= 8) {
1186 memset(&va,0,sizeof(va));
1187 btv->audio_hook(btv,&va,0);
1189 case V4L2_CID_AUDIO_MUTE:
1190 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1192 case V4L2_CID_AUDIO_VOLUME:
1193 c->value = va.volume;
1195 case V4L2_CID_AUDIO_BALANCE:
1196 c->value = va.balance;
1198 case V4L2_CID_AUDIO_BASS:
1201 case V4L2_CID_AUDIO_TREBLE:
1202 c->value = va.treble;
1208 case V4L2_CID_BRIGHTNESS:
1209 c->value = btv->bright;
1212 c->value = btv->hue;
1214 case V4L2_CID_CONTRAST:
1215 c->value = btv->contrast;
1217 case V4L2_CID_SATURATION:
1218 c->value = btv->saturation;
1221 case V4L2_CID_AUDIO_MUTE:
1222 case V4L2_CID_AUDIO_VOLUME:
1223 case V4L2_CID_AUDIO_BALANCE:
1224 case V4L2_CID_AUDIO_BASS:
1225 case V4L2_CID_AUDIO_TREBLE:
1226 bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c);
1229 case V4L2_CID_PRIVATE_CHROMA_AGC:
1230 c->value = btv->opt_chroma_agc;
1232 case V4L2_CID_PRIVATE_COMBFILTER:
1233 c->value = btv->opt_combfilter;
1235 case V4L2_CID_PRIVATE_LUMAFILTER:
1236 c->value = btv->opt_lumafilter;
1238 case V4L2_CID_PRIVATE_AUTOMUTE:
1239 c->value = btv->opt_automute;
1241 case V4L2_CID_PRIVATE_AGC_CRUSH:
1242 c->value = btv->opt_adc_crush;
1244 case V4L2_CID_PRIVATE_VCR_HACK:
1245 c->value = btv->opt_vcr_hack;
1247 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1248 c->value = btv->opt_whitecrush_upper;
1250 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1251 c->value = btv->opt_whitecrush_lower;
1253 case V4L2_CID_PRIVATE_UV_RATIO:
1254 c->value = btv->opt_uv_ratio;
1256 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1257 c->value = btv->opt_full_luma_range;
1259 case V4L2_CID_PRIVATE_CORING:
1260 c->value = btv->opt_coring;
1268 static int set_control(struct bttv *btv, struct v4l2_control *c)
1270 struct video_audio va;
1273 for (i = 0; i < BTTV_CTLS; i++)
1274 if (bttv_ctls[i].id == c->id)
1278 if (btv->audio_hook && i >= 4 && i <= 8) {
1279 memset(&va,0,sizeof(va));
1280 btv->audio_hook(btv,&va,0);
1282 case V4L2_CID_AUDIO_MUTE:
1284 va.flags |= VIDEO_AUDIO_MUTE;
1287 va.flags &= ~VIDEO_AUDIO_MUTE;
1292 case V4L2_CID_AUDIO_VOLUME:
1293 va.volume = c->value;
1295 case V4L2_CID_AUDIO_BALANCE:
1296 va.balance = c->value;
1298 case V4L2_CID_AUDIO_BASS:
1301 case V4L2_CID_AUDIO_TREBLE:
1302 va.treble = c->value;
1305 btv->audio_hook(btv,&va,1);
1309 case V4L2_CID_BRIGHTNESS:
1310 bt848_bright(btv,c->value);
1313 bt848_hue(btv,c->value);
1315 case V4L2_CID_CONTRAST:
1316 bt848_contrast(btv,c->value);
1318 case V4L2_CID_SATURATION:
1319 bt848_sat(btv,c->value);
1321 case V4L2_CID_AUDIO_MUTE:
1322 audio_mute(btv, c->value);
1324 case V4L2_CID_AUDIO_VOLUME:
1325 case V4L2_CID_AUDIO_BALANCE:
1326 case V4L2_CID_AUDIO_BASS:
1327 case V4L2_CID_AUDIO_TREBLE:
1328 bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c);
1331 case V4L2_CID_PRIVATE_CHROMA_AGC:
1332 btv->opt_chroma_agc = c->value;
1333 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1334 btwrite(val, BT848_E_SCLOOP);
1335 btwrite(val, BT848_O_SCLOOP);
1337 case V4L2_CID_PRIVATE_COMBFILTER:
1338 btv->opt_combfilter = c->value;
1340 case V4L2_CID_PRIVATE_LUMAFILTER:
1341 btv->opt_lumafilter = c->value;
1342 if (btv->opt_lumafilter) {
1343 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1344 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1346 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1347 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1350 case V4L2_CID_PRIVATE_AUTOMUTE:
1351 btv->opt_automute = c->value;
1353 case V4L2_CID_PRIVATE_AGC_CRUSH:
1354 btv->opt_adc_crush = c->value;
1355 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1358 case V4L2_CID_PRIVATE_VCR_HACK:
1359 btv->opt_vcr_hack = c->value;
1361 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1362 btv->opt_whitecrush_upper = c->value;
1363 btwrite(c->value, BT848_WC_UP);
1365 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1366 btv->opt_whitecrush_lower = c->value;
1367 btwrite(c->value, BT848_WC_DOWN);
1369 case V4L2_CID_PRIVATE_UV_RATIO:
1370 btv->opt_uv_ratio = c->value;
1371 bt848_sat(btv, btv->saturation);
1373 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1374 btv->opt_full_luma_range = c->value;
1375 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1377 case V4L2_CID_PRIVATE_CORING:
1378 btv->opt_coring = c->value;
1379 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1387 /* ----------------------------------------------------------------------- */
1389 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1391 unsigned int outbits, data;
1392 outbits = btread(BT848_GPIO_OUT_EN);
1393 data = btread(BT848_GPIO_DATA);
1394 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1395 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1398 static void bttv_field_count(struct bttv *btv)
1406 /* start field counter */
1407 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1409 /* stop field counter */
1410 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1411 btv->field_count = 0;
1415 static const struct bttv_format*
1416 format_by_palette(int palette)
1420 for (i = 0; i < BTTV_FORMATS; i++) {
1421 if (-1 == bttv_formats[i].palette)
1423 if (bttv_formats[i].palette == palette)
1424 return bttv_formats+i;
1429 static const struct bttv_format*
1430 format_by_fourcc(int fourcc)
1434 for (i = 0; i < BTTV_FORMATS; i++) {
1435 if (-1 == bttv_formats[i].fourcc)
1437 if (bttv_formats[i].fourcc == fourcc)
1438 return bttv_formats+i;
1443 /* ----------------------------------------------------------------------- */
1447 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1448 struct bttv_buffer *new)
1450 struct bttv_buffer *old;
1451 unsigned long flags;
1454 dprintk("switch_overlay: enter [new=%p]\n",new);
1456 new->vb.state = STATE_DONE;
1457 spin_lock_irqsave(&btv->s_lock,flags);
1461 bttv_set_dma(btv, 0x03);
1462 spin_unlock_irqrestore(&btv->s_lock,flags);
1464 free_btres(btv,fh,RESOURCE_OVERLAY);
1466 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1467 bttv_dma_free(&fh->cap,btv, old);
1470 dprintk("switch_overlay: done\n");
1474 /* ----------------------------------------------------------------------- */
1475 /* video4linux (1) interface */
1477 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1478 struct bttv_buffer *buf,
1479 const struct bttv_format *fmt,
1480 unsigned int width, unsigned int height,
1481 enum v4l2_field field)
1483 int redo_dma_risc = 0;
1486 /* check settings */
1489 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1491 height = RAW_LINES*2;
1492 if (width*height > buf->vb.bsize)
1494 buf->vb.size = buf->vb.bsize;
1498 width > bttv_tvnorms[btv->tvnorm].swidth ||
1499 height > bttv_tvnorms[btv->tvnorm].sheight)
1501 buf->vb.size = (width * height * fmt->depth) >> 3;
1502 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1506 /* alloc + fill struct bttv_buffer (if changed) */
1507 if (buf->vb.width != width || buf->vb.height != height ||
1508 buf->vb.field != field ||
1509 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1510 buf->vb.width = width;
1511 buf->vb.height = height;
1512 buf->vb.field = field;
1513 buf->tvnorm = btv->tvnorm;
1518 /* alloc risc memory */
1519 if (STATE_NEEDS_INIT == buf->vb.state) {
1521 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1526 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1529 buf->vb.state = STATE_PREPARED;
1533 bttv_dma_free(q,btv,buf);
1538 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1540 struct bttv_fh *fh = q->priv_data;
1542 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1545 while (*size * *count > gbuffers * gbufsize)
1551 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1552 enum v4l2_field field)
1554 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1555 struct bttv_fh *fh = q->priv_data;
1557 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1558 fh->width, fh->height, field);
1562 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1564 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1565 struct bttv_fh *fh = q->priv_data;
1566 struct bttv *btv = fh->btv;
1568 buf->vb.state = STATE_QUEUED;
1569 list_add_tail(&buf->vb.queue,&btv->capture);
1570 if (!btv->curr.frame_irq) {
1572 bttv_set_dma(btv, 0x03);
1576 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1578 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1579 struct bttv_fh *fh = q->priv_data;
1581 bttv_dma_free(&fh->cap,fh->btv,buf);
1584 static struct videobuf_queue_ops bttv_video_qops = {
1585 .buf_setup = buffer_setup,
1586 .buf_prepare = buffer_prepare,
1587 .buf_queue = buffer_queue,
1588 .buf_release = buffer_release,
1591 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1595 return BTTV_VERSION_CODE;
1597 /* *** v4l1 *** ************************************************ */
1600 unsigned long *freq = arg;
1606 unsigned long *freq = arg;
1607 mutex_lock(&btv->lock);
1609 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1610 if (btv->has_matchbox && btv->radio_user)
1611 tea5757_set_freq(btv,*freq);
1612 mutex_unlock(&btv->lock);
1618 struct video_tuner *v = arg;
1620 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1622 if (v->tuner) /* Only tuner 0 */
1624 strcpy(v->name, "Television");
1626 v->rangehigh = 0x7FFFFFFF;
1627 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1628 v->mode = btv->tvnorm;
1629 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1630 bttv_call_i2c_clients(btv,cmd,v);
1635 struct video_tuner *v = arg;
1637 if (v->tuner) /* Only tuner 0 */
1639 if (v->mode >= BTTV_TVNORMS)
1642 mutex_lock(&btv->lock);
1643 set_tvnorm(btv,v->mode);
1644 bttv_call_i2c_clients(btv,cmd,v);
1645 mutex_unlock(&btv->lock);
1651 struct video_channel *v = arg;
1652 unsigned int channel = v->channel;
1654 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1657 v->flags = VIDEO_VC_AUDIO;
1658 v->type = VIDEO_TYPE_CAMERA;
1659 v->norm = btv->tvnorm;
1660 if (channel == bttv_tvcards[btv->c.type].tuner) {
1661 strcpy(v->name,"Television");
1662 v->flags|=VIDEO_VC_TUNER;
1663 v->type=VIDEO_TYPE_TV;
1665 } else if (channel == btv->svhs) {
1666 strcpy(v->name,"S-Video");
1668 sprintf(v->name,"Composite%d",channel);
1674 struct video_channel *v = arg;
1675 unsigned int channel = v->channel;
1677 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1679 if (v->norm >= BTTV_TVNORMS)
1682 mutex_lock(&btv->lock);
1683 if (channel == btv->input &&
1684 v->norm == btv->tvnorm) {
1686 mutex_unlock(&btv->lock);
1690 btv->tvnorm = v->norm;
1691 set_input(btv,v->channel);
1692 mutex_unlock(&btv->lock);
1698 struct video_audio *v = arg;
1700 memset(v,0,sizeof(*v));
1701 strcpy(v->name,"Television");
1702 v->flags |= VIDEO_AUDIO_MUTABLE;
1703 v->mode = VIDEO_SOUND_MONO;
1705 mutex_lock(&btv->lock);
1706 bttv_call_i2c_clients(btv,cmd,v);
1708 /* card specific hooks */
1709 if (btv->audio_hook)
1710 btv->audio_hook(btv,v,0);
1712 mutex_unlock(&btv->lock);
1717 struct video_audio *v = arg;
1718 unsigned int audio = v->audio;
1720 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1723 mutex_lock(&btv->lock);
1724 audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
1725 bttv_call_i2c_clients(btv,cmd,v);
1727 /* card specific hooks */
1728 if (btv->audio_hook)
1729 btv->audio_hook(btv,v,1);
1731 mutex_unlock(&btv->lock);
1735 /* *** v4l2 *** ************************************************ */
1736 case VIDIOC_ENUMSTD:
1738 struct v4l2_standard *e = arg;
1739 unsigned int index = e->index;
1741 if (index >= BTTV_TVNORMS)
1743 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1744 bttv_tvnorms[e->index].name);
1750 v4l2_std_id *id = arg;
1751 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1756 v4l2_std_id *id = arg;
1759 for (i = 0; i < BTTV_TVNORMS; i++)
1760 if (*id & bttv_tvnorms[i].v4l2_id)
1762 if (i == BTTV_TVNORMS)
1765 mutex_lock(&btv->lock);
1767 i2c_vidiocschan(btv);
1768 mutex_unlock(&btv->lock);
1771 case VIDIOC_QUERYSTD:
1773 v4l2_std_id *id = arg;
1775 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1776 *id = V4L2_STD_625_50;
1778 *id = V4L2_STD_525_60;
1782 case VIDIOC_ENUMINPUT:
1784 struct v4l2_input *i = arg;
1788 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1790 memset(i,0,sizeof(*i));
1792 i->type = V4L2_INPUT_TYPE_CAMERA;
1794 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1795 sprintf(i->name, "Television");
1796 i->type = V4L2_INPUT_TYPE_TUNER;
1798 } else if (i->index == btv->svhs) {
1799 sprintf(i->name, "S-Video");
1801 sprintf(i->name,"Composite%d",i->index);
1803 if (i->index == btv->input) {
1804 __u32 dstatus = btread(BT848_DSTATUS);
1805 if (0 == (dstatus & BT848_DSTATUS_PRES))
1806 i->status |= V4L2_IN_ST_NO_SIGNAL;
1807 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1808 i->status |= V4L2_IN_ST_NO_H_LOCK;
1810 for (n = 0; n < BTTV_TVNORMS; n++)
1811 i->std |= bttv_tvnorms[n].v4l2_id;
1814 case VIDIOC_G_INPUT:
1820 case VIDIOC_S_INPUT:
1822 unsigned int *i = arg;
1824 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1826 mutex_lock(&btv->lock);
1828 mutex_unlock(&btv->lock);
1832 case VIDIOC_G_TUNER:
1834 struct v4l2_tuner *t = arg;
1836 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1840 mutex_lock(&btv->lock);
1841 memset(t,0,sizeof(*t));
1842 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1843 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
1844 strcpy(t->name, "Television");
1845 t->capability = V4L2_TUNER_CAP_NORM;
1846 t->type = V4L2_TUNER_ANALOG_TV;
1847 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1850 if (btv->audio_hook) {
1852 struct video_audio va;
1853 memset(&va, 0, sizeof(struct video_audio));
1854 btv->audio_hook(btv,&va,0);
1855 t->audmode = V4L2_TUNER_MODE_MONO;
1856 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1857 if(va.mode & VIDEO_SOUND_STEREO) {
1858 t->audmode = V4L2_TUNER_MODE_STEREO;
1859 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1861 if(va.mode & VIDEO_SOUND_LANG2) {
1862 t->audmode = V4L2_TUNER_MODE_LANG1;
1863 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1864 | V4L2_TUNER_SUB_LANG2;
1867 /* FIXME: fill capability+audmode */
1868 mutex_unlock(&btv->lock);
1871 case VIDIOC_S_TUNER:
1873 struct v4l2_tuner *t = arg;
1875 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1879 mutex_lock(&btv->lock);
1880 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
1881 if (btv->audio_hook) {
1882 struct video_audio va;
1883 memset(&va, 0, sizeof(struct video_audio));
1884 if (t->audmode == V4L2_TUNER_MODE_MONO)
1885 va.mode = VIDEO_SOUND_MONO;
1886 else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
1887 t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
1888 va.mode = VIDEO_SOUND_STEREO;
1889 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1890 va.mode = VIDEO_SOUND_LANG1;
1891 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1892 va.mode = VIDEO_SOUND_LANG2;
1893 btv->audio_hook(btv,&va,1);
1895 mutex_unlock(&btv->lock);
1899 case VIDIOC_G_FREQUENCY:
1901 struct v4l2_frequency *f = arg;
1903 memset(f,0,sizeof(*f));
1904 f->type = V4L2_TUNER_ANALOG_TV;
1905 f->frequency = btv->freq;
1908 case VIDIOC_S_FREQUENCY:
1910 struct v4l2_frequency *f = arg;
1912 if (unlikely(f->tuner != 0))
1914 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
1916 mutex_lock(&btv->lock);
1917 btv->freq = f->frequency;
1918 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
1919 if (btv->has_matchbox && btv->radio_user)
1920 tea5757_set_freq(btv,btv->freq);
1921 mutex_unlock(&btv->lock);
1924 case VIDIOC_LOG_STATUS:
1926 printk(KERN_INFO "bttv%d: ================= START STATUS CARD #%d =================\n", btv->c.nr, btv->c.nr);
1927 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
1928 printk(KERN_INFO "bttv%d: ================== END STATUS CARD #%d ==================\n", btv->c.nr, btv->c.nr);
1933 return -ENOIOCTLCMD;
1939 static int verify_window(const struct bttv_tvnorm *tvn,
1940 struct v4l2_window *win, int fixup)
1942 enum v4l2_field field;
1945 if (win->w.width < 48 || win->w.height < 32)
1947 if (win->clipcount > 2048)
1952 maxh = tvn->sheight;
1954 if (V4L2_FIELD_ANY == field) {
1955 field = (win->w.height > maxh/2)
1956 ? V4L2_FIELD_INTERLACED
1960 case V4L2_FIELD_TOP:
1961 case V4L2_FIELD_BOTTOM:
1964 case V4L2_FIELD_INTERLACED:
1970 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1973 if (win->w.width > maxw)
1974 win->w.width = maxw;
1975 if (win->w.height > maxh)
1976 win->w.height = maxh;
1981 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1982 struct v4l2_window *win, int fixup)
1984 struct v4l2_clip *clips = NULL;
1985 int n,size,retval = 0;
1987 if (NULL == fh->ovfmt)
1989 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1991 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1995 /* copy clips -- luckily v4l1 + v4l2 are binary
1996 compatible here ...*/
1998 size = sizeof(*clips)*(n+4);
1999 clips = kmalloc(size,GFP_KERNEL);
2003 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2008 /* clip against screen */
2009 if (NULL != btv->fbuf.base)
2010 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2012 btcx_sort_clips(clips,n);
2014 /* 4-byte alignments */
2015 switch (fh->ovfmt->depth) {
2018 btcx_align(&win->w, clips, n, 3);
2021 btcx_align(&win->w, clips, n, 1);
2024 /* no alignment fixups needed */
2030 mutex_lock(&fh->cap.lock);
2031 kfree(fh->ov.clips);
2032 fh->ov.clips = clips;
2036 fh->ov.field = win->field;
2037 fh->ov.setup_ok = 1;
2038 btv->init.ov.w.width = win->w.width;
2039 btv->init.ov.w.height = win->w.height;
2040 btv->init.ov.field = win->field;
2042 /* update overlay if needed */
2044 if (check_btres(fh, RESOURCE_OVERLAY)) {
2045 struct bttv_buffer *new;
2047 new = videobuf_alloc(sizeof(*new));
2048 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2049 retval = bttv_switch_overlay(btv,fh,new);
2051 mutex_unlock(&fh->cap.lock);
2055 /* ----------------------------------------------------------------------- */
2057 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2059 struct videobuf_queue* q = NULL;
2062 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2065 case V4L2_BUF_TYPE_VBI_CAPTURE:
2074 static int bttv_resource(struct bttv_fh *fh)
2079 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2080 res = RESOURCE_VIDEO;
2082 case V4L2_BUF_TYPE_VBI_CAPTURE:
2091 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2093 struct videobuf_queue *q = bttv_queue(fh);
2094 int res = bttv_resource(fh);
2096 if (check_btres(fh,res))
2098 if (videobuf_queue_is_busy(q))
2105 pix_format_set_size (struct v4l2_pix_format * f,
2106 const struct bttv_format * fmt,
2108 unsigned int height)
2113 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2114 f->bytesperline = width; /* Y plane */
2115 f->sizeimage = (width * height * fmt->depth) >> 3;
2117 f->bytesperline = (width * fmt->depth) >> 3;
2118 f->sizeimage = height * f->bytesperline;
2122 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2125 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2126 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2127 pix_format_set_size (&f->fmt.pix, fh->fmt,
2128 fh->width, fh->height);
2129 f->fmt.pix.field = fh->cap.field;
2130 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2132 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2133 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2134 f->fmt.win.w = fh->ov.w;
2135 f->fmt.win.field = fh->ov.field;
2137 case V4L2_BUF_TYPE_VBI_CAPTURE:
2138 bttv_vbi_get_fmt(fh,f);
2145 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2146 struct v4l2_format *f)
2149 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2151 const struct bttv_format *fmt;
2152 enum v4l2_field field;
2153 unsigned int maxw,maxh;
2155 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2160 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2161 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2162 field = f->fmt.pix.field;
2163 if (V4L2_FIELD_ANY == field)
2164 field = (f->fmt.pix.height > maxh/2)
2165 ? V4L2_FIELD_INTERLACED
2166 : V4L2_FIELD_BOTTOM;
2167 if (V4L2_FIELD_SEQ_BT == field)
2168 field = V4L2_FIELD_SEQ_TB;
2170 case V4L2_FIELD_TOP:
2171 case V4L2_FIELD_BOTTOM:
2172 case V4L2_FIELD_ALTERNATE:
2175 case V4L2_FIELD_INTERLACED:
2177 case V4L2_FIELD_SEQ_TB:
2178 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2185 /* update data for the application */
2186 f->fmt.pix.field = field;
2187 if (f->fmt.pix.width < 48)
2188 f->fmt.pix.width = 48;
2189 if (f->fmt.pix.height < 32)
2190 f->fmt.pix.height = 32;
2191 if (f->fmt.pix.width > maxw)
2192 f->fmt.pix.width = maxw;
2193 if (f->fmt.pix.height > maxh)
2194 f->fmt.pix.height = maxh;
2195 pix_format_set_size (&f->fmt.pix, fmt,
2196 f->fmt.pix.width & ~3,
2201 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2202 return verify_window(&bttv_tvnorms[btv->tvnorm],
2204 case V4L2_BUF_TYPE_VBI_CAPTURE:
2205 bttv_vbi_try_fmt(fh,f);
2212 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2213 struct v4l2_format *f)
2218 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2220 const struct bttv_format *fmt;
2222 retval = bttv_switch_type(fh,f->type);
2225 retval = bttv_try_fmt(fh,btv,f);
2228 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2230 /* update our state informations */
2231 mutex_lock(&fh->cap.lock);
2233 fh->cap.field = f->fmt.pix.field;
2234 fh->cap.last = V4L2_FIELD_NONE;
2235 fh->width = f->fmt.pix.width;
2236 fh->height = f->fmt.pix.height;
2237 btv->init.fmt = fmt;
2238 btv->init.width = f->fmt.pix.width;
2239 btv->init.height = f->fmt.pix.height;
2240 mutex_unlock(&fh->cap.lock);
2244 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2245 if (no_overlay > 0) {
2246 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2249 return setup_window(fh, btv, &f->fmt.win, 1);
2250 case V4L2_BUF_TYPE_VBI_CAPTURE:
2251 retval = bttv_switch_type(fh,f->type);
2254 if (locked_btres(fh->btv, RESOURCE_VBI))
2256 bttv_vbi_try_fmt(fh,f);
2257 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2258 bttv_vbi_get_fmt(fh,f);
2265 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2266 unsigned int cmd, void *arg)
2268 struct bttv_fh *fh = file->private_data;
2269 struct bttv *btv = fh->btv;
2270 unsigned long flags;
2274 v4l_print_ioctl(btv->c.name, cmd);
2277 bttv_reinit_bt848(btv);
2285 case VIDIOC_S_INPUT:
2286 case VIDIOC_S_TUNER:
2287 case VIDIOC_S_FREQUENCY:
2288 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2295 /* *** v4l1 *** ************************************************ */
2298 struct video_capability *cap = arg;
2300 memset(cap,0,sizeof(*cap));
2301 strcpy(cap->name,btv->video_dev->name);
2302 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2304 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2307 cap->type = VID_TYPE_CAPTURE|
2311 if (no_overlay <= 0)
2312 cap->type |= VID_TYPE_OVERLAY;
2314 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2315 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2317 cap->minheight = 32;
2319 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2320 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2326 struct video_picture *pic = arg;
2328 memset(pic,0,sizeof(*pic));
2329 pic->brightness = btv->bright;
2330 pic->contrast = btv->contrast;
2331 pic->hue = btv->hue;
2332 pic->colour = btv->saturation;
2334 pic->depth = fh->fmt->depth;
2335 pic->palette = fh->fmt->palette;
2341 struct video_picture *pic = arg;
2342 const struct bttv_format *fmt;
2344 fmt = format_by_palette(pic->palette);
2347 mutex_lock(&fh->cap.lock);
2348 if (fmt->depth != pic->depth) {
2350 goto fh_unlock_and_return;
2352 if (fmt->flags & FORMAT_FLAGS_RAW) {
2353 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2354 RAW_LINES * 2. F1 is stored at offset 0, F2
2355 at buffer size / 2. */
2356 fh->width = RAW_BPL;
2357 fh->height = gbufsize / RAW_BPL;
2358 btv->init.width = RAW_BPL;
2359 btv->init.height = gbufsize / RAW_BPL;
2363 btv->init.ovfmt = fmt;
2364 btv->init.fmt = fmt;
2366 /* dirty hack time: swap bytes for overlay if the
2367 display adaptor is big endian (insmod option) */
2368 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2369 fmt->palette == VIDEO_PALETTE_RGB565 ||
2370 fmt->palette == VIDEO_PALETTE_RGB32) {
2374 bt848_bright(btv,pic->brightness);
2375 bt848_contrast(btv,pic->contrast);
2376 bt848_hue(btv,pic->hue);
2377 bt848_sat(btv,pic->colour);
2378 mutex_unlock(&fh->cap.lock);
2384 struct video_window *win = arg;
2386 memset(win,0,sizeof(*win));
2387 win->x = fh->ov.w.left;
2388 win->y = fh->ov.w.top;
2389 win->width = fh->ov.w.width;
2390 win->height = fh->ov.w.height;
2395 struct video_window *win = arg;
2396 struct v4l2_window w2;
2398 if (no_overlay > 0) {
2399 printk ("VIDIOCSWIN: no_overlay\n");
2403 w2.field = V4L2_FIELD_ANY;
2406 w2.w.width = win->width;
2407 w2.w.height = win->height;
2408 w2.clipcount = win->clipcount;
2409 w2.clips = (struct v4l2_clip __user *)win->clips;
2410 retval = setup_window(fh, btv, &w2, 0);
2412 /* on v4l1 this ioctl affects the read() size too */
2413 fh->width = fh->ov.w.width;
2414 fh->height = fh->ov.w.height;
2415 btv->init.width = fh->ov.w.width;
2416 btv->init.height = fh->ov.w.height;
2423 struct video_buffer *fbuf = arg;
2425 fbuf->base = btv->fbuf.base;
2426 fbuf->width = btv->fbuf.fmt.width;
2427 fbuf->height = btv->fbuf.fmt.height;
2428 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2430 fbuf->depth = fh->ovfmt->depth;
2435 struct video_buffer *fbuf = arg;
2436 const struct bttv_format *fmt;
2439 if(!capable(CAP_SYS_ADMIN) &&
2440 !capable(CAP_SYS_RAWIO))
2442 end = (unsigned long)fbuf->base +
2443 fbuf->height * fbuf->bytesperline;
2444 mutex_lock(&fh->cap.lock);
2447 switch (fbuf->depth) {
2449 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2452 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2455 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2458 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2462 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2469 goto fh_unlock_and_return;
2473 btv->init.ovfmt = fmt;
2474 btv->init.fmt = fmt;
2475 btv->fbuf.base = fbuf->base;
2476 btv->fbuf.fmt.width = fbuf->width;
2477 btv->fbuf.fmt.height = fbuf->height;
2478 if (fbuf->bytesperline)
2479 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2481 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2482 mutex_unlock(&fh->cap.lock);
2487 case VIDIOC_OVERLAY:
2489 struct bttv_buffer *new;
2494 if (NULL == btv->fbuf.base)
2496 if (!fh->ov.setup_ok) {
2497 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2502 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2505 mutex_lock(&fh->cap.lock);
2507 fh->ov.tvnorm = btv->tvnorm;
2508 new = videobuf_alloc(sizeof(*new));
2509 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2515 retval = bttv_switch_overlay(btv,fh,new);
2516 mutex_unlock(&fh->cap.lock);
2522 struct video_mbuf *mbuf = arg;
2525 mutex_lock(&fh->cap.lock);
2526 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2529 goto fh_unlock_and_return;
2530 memset(mbuf,0,sizeof(*mbuf));
2531 mbuf->frames = gbuffers;
2532 mbuf->size = gbuffers * gbufsize;
2533 for (i = 0; i < gbuffers; i++)
2534 mbuf->offsets[i] = i * gbufsize;
2535 mutex_unlock(&fh->cap.lock);
2538 case VIDIOCMCAPTURE:
2540 struct video_mmap *vm = arg;
2541 struct bttv_buffer *buf;
2542 enum v4l2_field field;
2544 if (vm->frame >= VIDEO_MAX_FRAME)
2547 mutex_lock(&fh->cap.lock);
2549 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2551 goto fh_unlock_and_return;
2552 if (0 == buf->vb.baddr)
2553 goto fh_unlock_and_return;
2554 if (buf->vb.state == STATE_QUEUED ||
2555 buf->vb.state == STATE_ACTIVE)
2556 goto fh_unlock_and_return;
2558 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2559 ? V4L2_FIELD_INTERLACED
2560 : V4L2_FIELD_BOTTOM;
2561 retval = bttv_prepare_buffer(&fh->cap,btv,buf,
2562 format_by_palette(vm->format),
2563 vm->width,vm->height,field);
2565 goto fh_unlock_and_return;
2566 spin_lock_irqsave(&btv->s_lock,flags);
2567 buffer_queue(&fh->cap,&buf->vb);
2568 spin_unlock_irqrestore(&btv->s_lock,flags);
2569 mutex_unlock(&fh->cap.lock);
2575 struct bttv_buffer *buf;
2577 if (*frame >= VIDEO_MAX_FRAME)
2580 mutex_lock(&fh->cap.lock);
2582 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2584 goto fh_unlock_and_return;
2585 retval = videobuf_waiton(&buf->vb,0,1);
2587 goto fh_unlock_and_return;
2588 switch (buf->vb.state) {
2593 videobuf_dma_sync(&fh->cap,&buf->vb.dma);
2594 bttv_dma_free(&fh->cap,btv,buf);
2600 mutex_unlock(&fh->cap.lock);
2606 struct vbi_format *fmt = (void *) arg;
2607 struct v4l2_format fmt2;
2609 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2610 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2614 bttv_vbi_get_fmt(fh, &fmt2);
2616 memset(fmt,0,sizeof(*fmt));
2617 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2618 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2619 fmt->sample_format = VIDEO_PALETTE_RAW;
2620 fmt->start[0] = fmt2.fmt.vbi.start[0];
2621 fmt->count[0] = fmt2.fmt.vbi.count[0];
2622 fmt->start[1] = fmt2.fmt.vbi.start[1];
2623 fmt->count[1] = fmt2.fmt.vbi.count[1];
2624 if (fmt2.fmt.vbi.flags & V4L2_VBI_UNSYNC)
2625 fmt->flags |= VBI_UNSYNC;
2626 if (fmt2.fmt.vbi.flags & V4L2_VBI_INTERLACED)
2627 fmt->flags |= VBI_INTERLACED;
2632 struct vbi_format *fmt = (void *) arg;
2633 struct v4l2_format fmt2;
2635 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2638 bttv_vbi_get_fmt(fh, &fmt2);
2640 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2641 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2642 fmt->sample_format != VIDEO_PALETTE_RAW ||
2643 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2644 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2645 fmt->count[0] != fmt->count[1] ||
2646 fmt->count[0] < 1 ||
2647 fmt->count[0] > 32 /* VBI_MAXLINES */)
2650 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2663 return bttv_common_ioctls(btv,cmd,arg);
2665 /* *** v4l2 *** ************************************************ */
2666 case VIDIOC_QUERYCAP:
2668 struct v4l2_capability *cap = arg;
2672 memset(cap, 0, sizeof (*cap));
2673 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
2674 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
2675 snprintf(cap->bus_info, sizeof (cap->bus_info),
2676 "PCI:%s", pci_name(btv->c.pci));
2677 cap->version = BTTV_VERSION_CODE;
2679 V4L2_CAP_VIDEO_CAPTURE |
2680 V4L2_CAP_VBI_CAPTURE |
2681 V4L2_CAP_READWRITE |
2683 if (no_overlay <= 0)
2684 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2686 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2687 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2688 cap->capabilities |= V4L2_CAP_TUNER;
2692 case VIDIOC_ENUM_FMT:
2694 struct v4l2_fmtdesc *f = arg;
2695 enum v4l2_buf_type type;
2700 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2705 memset(f,0,sizeof(*f));
2708 f->pixelformat = V4L2_PIX_FMT_GREY;
2709 strcpy(f->description,"vbi data");
2713 /* video capture + overlay */
2715 for (i = 0; i < BTTV_FORMATS; i++) {
2716 if (bttv_formats[i].fourcc != -1)
2718 if ((unsigned int)index == f->index)
2721 if (BTTV_FORMATS == i)
2725 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2727 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2728 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2734 memset(f,0,sizeof(*f));
2737 f->pixelformat = bttv_formats[i].fourcc;
2738 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2742 case VIDIOC_TRY_FMT:
2744 struct v4l2_format *f = arg;
2745 return bttv_try_fmt(fh,btv,f);
2749 struct v4l2_format *f = arg;
2750 return bttv_g_fmt(fh,f);
2754 struct v4l2_format *f = arg;
2755 return bttv_s_fmt(fh,btv,f);
2760 struct v4l2_framebuffer *fb = arg;
2763 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2765 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2770 struct v4l2_framebuffer *fb = arg;
2771 const struct bttv_format *fmt;
2773 if(!capable(CAP_SYS_ADMIN) &&
2774 !capable(CAP_SYS_RAWIO))
2778 fmt = format_by_fourcc(fb->fmt.pixelformat);
2781 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2784 mutex_lock(&fh->cap.lock);
2786 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2787 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2788 goto fh_unlock_and_return;
2789 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2790 goto fh_unlock_and_return;
2794 btv->fbuf.base = fb->base;
2795 btv->fbuf.fmt.width = fb->fmt.width;
2796 btv->fbuf.fmt.height = fb->fmt.height;
2797 if (0 != fb->fmt.bytesperline)
2798 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2800 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2804 btv->init.ovfmt = fmt;
2805 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2808 fh->ov.w.width = fb->fmt.width;
2809 fh->ov.w.height = fb->fmt.height;
2810 btv->init.ov.w.width = fb->fmt.width;
2811 btv->init.ov.w.height = fb->fmt.height;
2812 kfree(fh->ov.clips);
2813 fh->ov.clips = NULL;
2816 if (check_btres(fh, RESOURCE_OVERLAY)) {
2817 struct bttv_buffer *new;
2819 new = videobuf_alloc(sizeof(*new));
2820 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2821 retval = bttv_switch_overlay(btv,fh,new);
2824 mutex_unlock(&fh->cap.lock);
2828 case VIDIOC_REQBUFS:
2829 return videobuf_reqbufs(bttv_queue(fh),arg);
2831 case VIDIOC_QUERYBUF:
2832 return videobuf_querybuf(bttv_queue(fh),arg);
2835 return videobuf_qbuf(bttv_queue(fh),arg);
2838 return videobuf_dqbuf(bttv_queue(fh),arg,
2839 file->f_flags & O_NONBLOCK);
2841 case VIDIOC_STREAMON:
2843 int res = bttv_resource(fh);
2845 if (!check_alloc_btres(btv,fh,res))
2847 return videobuf_streamon(bttv_queue(fh));
2849 case VIDIOC_STREAMOFF:
2851 int res = bttv_resource(fh);
2853 retval = videobuf_streamoff(bttv_queue(fh));
2856 free_btres(btv,fh,res);
2860 case VIDIOC_QUERYCTRL:
2862 struct v4l2_queryctrl *c = arg;
2865 if ((c->id < V4L2_CID_BASE ||
2866 c->id >= V4L2_CID_LASTP1) &&
2867 (c->id < V4L2_CID_PRIVATE_BASE ||
2868 c->id >= V4L2_CID_PRIVATE_LASTP1))
2870 for (i = 0; i < BTTV_CTLS; i++)
2871 if (bttv_ctls[i].id == c->id)
2873 if (i == BTTV_CTLS) {
2878 if (btv->audio_hook && i >= 4 && i <= 8) {
2879 struct video_audio va;
2880 memset(&va,0,sizeof(va));
2881 btv->audio_hook(btv,&va,0);
2882 switch (bttv_ctls[i].id) {
2883 case V4L2_CID_AUDIO_VOLUME:
2884 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2887 case V4L2_CID_AUDIO_BALANCE:
2888 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2891 case V4L2_CID_AUDIO_BASS:
2892 if (!(va.flags & VIDEO_AUDIO_BASS))
2895 case V4L2_CID_AUDIO_TREBLE:
2896 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2904 return get_control(btv,arg);
2906 return set_control(btv,arg);
2909 struct v4l2_streamparm *parm = arg;
2910 struct v4l2_standard s;
2911 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2913 memset(parm,0,sizeof(*parm));
2914 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2915 bttv_tvnorms[btv->tvnorm].name);
2916 parm->parm.capture.timeperframe = s.frameperiod;
2920 case VIDIOC_G_PRIORITY:
2922 enum v4l2_priority *p = arg;
2924 *p = v4l2_prio_max(&btv->prio);
2927 case VIDIOC_S_PRIORITY:
2929 enum v4l2_priority *prio = arg;
2931 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2934 case VIDIOC_ENUMSTD:
2937 case VIDIOC_ENUMINPUT:
2938 case VIDIOC_G_INPUT:
2939 case VIDIOC_S_INPUT:
2940 case VIDIOC_G_TUNER:
2941 case VIDIOC_S_TUNER:
2942 case VIDIOC_G_FREQUENCY:
2943 case VIDIOC_S_FREQUENCY:
2944 case VIDIOC_LOG_STATUS:
2945 return bttv_common_ioctls(btv,cmd,arg);
2948 return -ENOIOCTLCMD;
2952 fh_unlock_and_return:
2953 mutex_unlock(&fh->cap.lock);
2957 static int bttv_ioctl(struct inode *inode, struct file *file,
2958 unsigned int cmd, unsigned long arg)
2960 struct bttv_fh *fh = file->private_data;
2964 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2965 return fh->lines * 2 * 2048;
2967 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2971 static ssize_t bttv_read(struct file *file, char __user *data,
2972 size_t count, loff_t *ppos)
2974 struct bttv_fh *fh = file->private_data;
2977 if (fh->btv->errors)
2978 bttv_reinit_bt848(fh->btv);
2979 dprintk("bttv%d: read count=%d type=%s\n",
2980 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2983 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2984 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2986 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2987 file->f_flags & O_NONBLOCK);
2989 case V4L2_BUF_TYPE_VBI_CAPTURE:
2990 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2992 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2993 file->f_flags & O_NONBLOCK);
3001 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3003 struct bttv_fh *fh = file->private_data;
3004 struct bttv_buffer *buf;
3005 enum v4l2_field field;
3007 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3008 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3010 return videobuf_poll_stream(file, &fh->vbi, wait);
3013 if (check_btres(fh,RESOURCE_VIDEO)) {
3014 /* streaming capture */
3015 if (list_empty(&fh->cap.stream))
3017 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3019 /* read() capture */
3020 mutex_lock(&fh->cap.lock);
3021 if (NULL == fh->cap.read_buf) {
3022 /* need to capture a new frame */
3023 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
3024 mutex_unlock(&fh->cap.lock);
3027 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
3028 if (NULL == fh->cap.read_buf) {
3029 mutex_unlock(&fh->cap.lock);
3032 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3033 field = videobuf_next_field(&fh->cap);
3034 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3035 kfree (fh->cap.read_buf);
3036 fh->cap.read_buf = NULL;
3037 mutex_unlock(&fh->cap.lock);
3040 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3041 fh->cap.read_off = 0;
3043 mutex_unlock(&fh->cap.lock);
3044 buf = (struct bttv_buffer*)fh->cap.read_buf;
3047 poll_wait(file, &buf->vb.done, wait);
3048 if (buf->vb.state == STATE_DONE ||
3049 buf->vb.state == STATE_ERROR)
3050 return POLLIN|POLLRDNORM;
3054 static int bttv_open(struct inode *inode, struct file *file)
3056 int minor = iminor(inode);
3057 struct bttv *btv = NULL;
3059 enum v4l2_buf_type type = 0;
3062 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3064 for (i = 0; i < bttv_num; i++) {
3065 if (bttvs[i].video_dev &&
3066 bttvs[i].video_dev->minor == minor) {
3068 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3071 if (bttvs[i].vbi_dev &&
3072 bttvs[i].vbi_dev->minor == minor) {
3074 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3081 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3082 btv->c.nr,v4l2_type_names[type]);
3084 /* allocate per filehandle data */
3085 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3088 file->private_data = fh;
3091 fh->ov.setup_ok = 0;
3092 v4l2_prio_open(&btv->prio,&fh->prio);
3094 videobuf_queue_init(&fh->cap, &bttv_video_qops,
3095 btv->c.pci, &btv->s_lock,
3096 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3097 V4L2_FIELD_INTERLACED,
3098 sizeof(struct bttv_buffer),
3100 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3101 btv->c.pci, &btv->s_lock,
3102 V4L2_BUF_TYPE_VBI_CAPTURE,
3104 sizeof(struct bttv_buffer),
3106 i2c_vidiocschan(btv);
3109 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
3110 bttv_vbi_setlines(fh,btv,16);
3111 bttv_field_count(btv);
3115 static int bttv_release(struct inode *inode, struct file *file)
3117 struct bttv_fh *fh = file->private_data;
3118 struct bttv *btv = fh->btv;
3120 /* turn off overlay */
3121 if (check_btres(fh, RESOURCE_OVERLAY))
3122 bttv_switch_overlay(btv,fh,NULL);
3124 /* stop video capture */
3125 if (check_btres(fh, RESOURCE_VIDEO)) {
3126 videobuf_streamoff(&fh->cap);
3127 free_btres(btv,fh,RESOURCE_VIDEO);
3129 if (fh->cap.read_buf) {
3130 buffer_release(&fh->cap,fh->cap.read_buf);
3131 kfree(fh->cap.read_buf);
3134 /* stop vbi capture */
3135 if (check_btres(fh, RESOURCE_VBI)) {
3136 if (fh->vbi.streaming)
3137 videobuf_streamoff(&fh->vbi);
3138 if (fh->vbi.reading)
3139 videobuf_read_stop(&fh->vbi);
3140 free_btres(btv,fh,RESOURCE_VBI);
3144 videobuf_mmap_free(&fh->cap);
3145 videobuf_mmap_free(&fh->vbi);
3146 v4l2_prio_close(&btv->prio,&fh->prio);
3147 file->private_data = NULL;
3151 bttv_field_count(btv);
3156 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3158 struct bttv_fh *fh = file->private_data;
3160 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3161 fh->btv->c.nr, v4l2_type_names[fh->type],
3162 vma->vm_start, vma->vm_end - vma->vm_start);
3163 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3166 static struct file_operations bttv_fops =
3168 .owner = THIS_MODULE,
3170 .release = bttv_release,
3171 .ioctl = bttv_ioctl,
3172 .compat_ioctl = v4l_compat_ioctl32,
3173 .llseek = no_llseek,
3179 static struct video_device bttv_video_template =
3182 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3183 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3184 .hardware = VID_HARDWARE_BT848,
3189 static struct video_device bttv_vbi_template =
3191 .name = "bt848/878 vbi",
3192 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3193 .hardware = VID_HARDWARE_BT848,
3198 /* ----------------------------------------------------------------------- */
3199 /* radio interface */
3201 static int radio_open(struct inode *inode, struct file *file)
3203 int minor = iminor(inode);
3204 struct bttv *btv = NULL;
3207 dprintk("bttv: open minor=%d\n",minor);
3209 for (i = 0; i < bttv_num; i++) {
3210 if (bttvs[i].radio_dev->minor == minor) {
3218 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3219 mutex_lock(&btv->lock);
3223 file->private_data = btv;
3225 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3226 audio_input(btv,TVAUDIO_INPUT_RADIO);
3228 mutex_unlock(&btv->lock);
3232 static int radio_release(struct inode *inode, struct file *file)
3234 struct bttv *btv = file->private_data;
3235 struct rds_command cmd;
3239 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3244 static int radio_do_ioctl(struct inode *inode, struct file *file,
3245 unsigned int cmd, void *arg)
3247 struct bttv *btv = file->private_data;
3252 struct video_capability *cap = arg;
3254 memset(cap,0,sizeof(*cap));
3255 strcpy(cap->name,btv->radio_dev->name);
3256 cap->type = VID_TYPE_TUNER;
3264 struct video_tuner *v = arg;
3268 memset(v,0,sizeof(*v));
3269 strcpy(v->name, "Radio");
3270 bttv_call_i2c_clients(btv,cmd,v);
3282 case VIDIOC_LOG_STATUS:
3283 return bttv_common_ioctls(btv,cmd,arg);
3286 return -ENOIOCTLCMD;
3291 static int radio_ioctl(struct inode *inode, struct file *file,
3292 unsigned int cmd, unsigned long arg)
3294 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3297 static ssize_t radio_read(struct file *file, char __user *data,
3298 size_t count, loff_t *ppos)
3300 struct bttv *btv = file->private_data;
3301 struct rds_command cmd;
3302 cmd.block_count = count/3;
3304 cmd.instance = file;
3305 cmd.result = -ENODEV;
3307 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3312 static unsigned int radio_poll(struct file *file, poll_table *wait)
3314 struct bttv *btv = file->private_data;
3315 struct rds_command cmd;
3316 cmd.instance = file;
3317 cmd.event_list = wait;
3318 cmd.result = -ENODEV;
3319 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3324 static struct file_operations radio_fops =
3326 .owner = THIS_MODULE,
3329 .release = radio_release,
3330 .ioctl = radio_ioctl,
3331 .llseek = no_llseek,
3335 static struct video_device radio_template =
3337 .name = "bt848/878 radio",
3338 .type = VID_TYPE_TUNER,
3339 .hardware = VID_HARDWARE_BT848,
3340 .fops = &radio_fops,
3344 /* ----------------------------------------------------------------------- */
3345 /* some debug code */
3347 static int bttv_risc_decode(u32 risc)
3349 static char *instr[16] = {
3350 [ BT848_RISC_WRITE >> 28 ] = "write",
3351 [ BT848_RISC_SKIP >> 28 ] = "skip",
3352 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3353 [ BT848_RISC_JUMP >> 28 ] = "jump",
3354 [ BT848_RISC_SYNC >> 28 ] = "sync",
3355 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3356 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3357 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3359 static int incr[16] = {
3360 [ BT848_RISC_WRITE >> 28 ] = 2,
3361 [ BT848_RISC_JUMP >> 28 ] = 2,
3362 [ BT848_RISC_SYNC >> 28 ] = 2,
3363 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3364 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3365 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3367 static char *bits[] = {
3368 "be0", "be1", "be2", "be3/resync",
3369 "set0", "set1", "set2", "set3",
3370 "clr0", "clr1", "clr2", "clr3",
3371 "irq", "res", "eol", "sol",
3375 printk("0x%08x [ %s", risc,
3376 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3377 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3378 if (risc & (1 << (i + 12)))
3379 printk(" %s",bits[i]);
3380 printk(" count=%d ]\n", risc & 0xfff);
3381 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3384 static void bttv_risc_disasm(struct bttv *btv,
3385 struct btcx_riscmem *risc)
3389 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3390 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3391 for (i = 0; i < (risc->size >> 2); i += n) {
3392 printk("%s: 0x%lx: ", btv->c.name,
3393 (unsigned long)(risc->dma + (i<<2)));
3394 n = bttv_risc_decode(risc->cpu[i]);
3395 for (j = 1; j < n; j++)
3396 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3397 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3399 if (0 == risc->cpu[i])
3404 static void bttv_print_riscaddr(struct bttv *btv)
3406 printk(" main: %08Lx\n",
3407 (unsigned long long)btv->main.dma);
3408 printk(" vbi : o=%08Lx e=%08Lx\n",
3409 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3410 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3411 printk(" cap : o=%08Lx e=%08Lx\n",
3412 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3413 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3414 printk(" scr : o=%08Lx e=%08Lx\n",
3415 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3416 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3417 bttv_risc_disasm(btv, &btv->main);
3420 /* ----------------------------------------------------------------------- */
3423 static char *irq_name[] = {
3424 "FMTCHG", // format change detected (525 vs. 625)
3425 "VSYNC", // vertical sync (new field)
3426 "HSYNC", // horizontal sync
3427 "OFLOW", // chroma/luma AGC overflow
3428 "HLOCK", // horizontal lock changed
3429 "VPRES", // video presence changed
3431 "I2CDONE", // hw irc operation finished
3432 "GPINT", // gpio port triggered irq
3434 "RISCI", // risc instruction triggered irq
3435 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3436 "FTRGT", // pixel data fifo overrun
3437 "FDSR", // fifo data stream resyncronisation
3438 "PPERR", // parity error (data transfer)
3439 "RIPERR", // parity error (read risc instructions)
3440 "PABORT", // pci abort
3441 "OCERR", // risc instruction error
3442 "SCERR", // syncronisation error
3445 static void bttv_print_irqbits(u32 print, u32 mark)
3450 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3451 if (print & (1 << i))
3452 printk(" %s",irq_name[i]);
3453 if (mark & (1 << i))
3458 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3460 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3462 (unsigned long)btv->main.dma,
3463 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3464 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3467 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3468 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3469 "Ok, then this is harmless, don't worry ;)\n",
3473 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3475 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3481 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3483 struct bttv_buffer *item;
3485 memset(set,0,sizeof(*set));
3487 /* capture request ? */
3488 if (!list_empty(&btv->capture)) {
3490 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3491 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3493 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3496 /* capture request for other field ? */
3497 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3498 (item->vb.queue.next != &btv->capture)) {
3499 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3500 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3501 if (NULL == set->top &&
3502 V4L2_FIELD_TOP == item->vb.field) {
3505 if (NULL == set->bottom &&
3506 V4L2_FIELD_BOTTOM == item->vb.field) {
3509 if (NULL != set->top && NULL != set->bottom)
3515 /* screen overlay ? */
3516 if (NULL != btv->screen) {
3517 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3518 if (NULL == set->top && NULL == set->bottom) {
3519 set->top = btv->screen;
3520 set->bottom = btv->screen;
3523 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3525 set->top = btv->screen;
3527 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3528 NULL == set->bottom) {
3529 set->bottom = btv->screen;
3534 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3535 btv->c.nr,set->top, set->bottom,
3536 btv->screen,set->frame_irq,set->top_irq);
3541 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3542 struct bttv_buffer_set *curr, unsigned int state)
3546 do_gettimeofday(&ts);
3548 if (wakeup->top == wakeup->bottom) {
3549 if (NULL != wakeup->top && curr->top != wakeup->top) {
3551 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3552 wakeup->top->vb.ts = ts;
3553 wakeup->top->vb.field_count = btv->field_count;
3554 wakeup->top->vb.state = state;
3555 wake_up(&wakeup->top->vb.done);
3558 if (NULL != wakeup->top && curr->top != wakeup->top) {
3560 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3561 wakeup->top->vb.ts = ts;
3562 wakeup->top->vb.field_count = btv->field_count;
3563 wakeup->top->vb.state = state;
3564 wake_up(&wakeup->top->vb.done);
3566 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3568 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3569 wakeup->bottom->vb.ts = ts;
3570 wakeup->bottom->vb.field_count = btv->field_count;
3571 wakeup->bottom->vb.state = state;
3572 wake_up(&wakeup->bottom->vb.done);
3578 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3586 do_gettimeofday(&ts);
3588 wakeup->vb.field_count = btv->field_count;
3589 wakeup->vb.state = state;
3590 wake_up(&wakeup->vb.done);
3593 static void bttv_irq_timeout(unsigned long data)
3595 struct bttv *btv = (struct bttv *)data;
3596 struct bttv_buffer_set old,new;
3597 struct bttv_buffer *ovbi;
3598 struct bttv_buffer *item;
3599 unsigned long flags;
3602 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3603 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3604 btread(BT848_RISC_COUNT));
3605 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3609 spin_lock_irqsave(&btv->s_lock,flags);
3611 /* deactivate stuff */
3612 memset(&new,0,sizeof(new));
3618 bttv_buffer_activate_video(btv, &new);
3619 bttv_buffer_activate_vbi(btv, NULL);
3620 bttv_set_dma(btv, 0);
3623 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3624 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3626 /* cancel all outstanding capture / vbi requests */
3627 while (!list_empty(&btv->capture)) {
3628 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3629 list_del(&item->vb.queue);
3630 item->vb.state = STATE_ERROR;
3631 wake_up(&item->vb.done);
3633 while (!list_empty(&btv->vcapture)) {
3634 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3635 list_del(&item->vb.queue);
3636 item->vb.state = STATE_ERROR;
3637 wake_up(&item->vb.done);
3641 spin_unlock_irqrestore(&btv->s_lock,flags);
3645 bttv_irq_wakeup_top(struct bttv *btv)
3647 struct bttv_buffer *wakeup = btv->curr.top;
3652 spin_lock(&btv->s_lock);
3653 btv->curr.top_irq = 0;
3654 btv->curr.top = NULL;
3655 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3657 do_gettimeofday(&wakeup->vb.ts);
3658 wakeup->vb.field_count = btv->field_count;
3659 wakeup->vb.state = STATE_DONE;
3660 wake_up(&wakeup->vb.done);
3661 spin_unlock(&btv->s_lock);
3664 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3668 if (rc > risc->dma + risc->size)
3674 bttv_irq_switch_video(struct bttv *btv)
3676 struct bttv_buffer_set new;
3677 struct bttv_buffer_set old;
3680 spin_lock(&btv->s_lock);
3682 /* new buffer set */
3683 bttv_irq_next_video(btv, &new);
3684 rc = btread(BT848_RISC_COUNT);
3685 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3686 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3689 bttv_irq_debug_low_latency(btv, rc);
3690 spin_unlock(&btv->s_lock);
3697 btv->loop_irq &= ~1;
3698 bttv_buffer_activate_video(btv, &new);
3699 bttv_set_dma(btv, 0);
3702 if (UNSET != btv->new_input) {
3703 video_mux(btv,btv->new_input);
3704 btv->new_input = UNSET;
3707 /* wake up finished buffers */
3708 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3709 spin_unlock(&btv->s_lock);
3713 bttv_irq_switch_vbi(struct bttv *btv)
3715 struct bttv_buffer *new = NULL;
3716 struct bttv_buffer *old;
3719 spin_lock(&btv->s_lock);
3721 if (!list_empty(&btv->vcapture))
3722 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3725 rc = btread(BT848_RISC_COUNT);
3726 if (NULL != old && (is_active(&old->top, rc) ||
3727 is_active(&old->bottom, rc))) {
3730 bttv_irq_debug_low_latency(btv, rc);
3731 spin_unlock(&btv->s_lock);
3737 btv->loop_irq &= ~4;
3738 bttv_buffer_activate_vbi(btv, new);
3739 bttv_set_dma(btv, 0);
3741 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3742 spin_unlock(&btv->s_lock);
3745 static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3753 btv=(struct bttv *)dev_id;
3755 if (btv->custom_irq)
3756 handled = btv->custom_irq(btv);
3760 /* get/clear interrupt status bits */
3761 stat=btread(BT848_INT_STAT);
3762 astat=stat&btread(BT848_INT_MASK);
3766 btwrite(stat,BT848_INT_STAT);
3768 /* get device status bits */
3769 dstat=btread(BT848_DSTATUS);
3772 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3773 "riscs=%x, riscc=%08x, ",
3774 btv->c.nr, count, btv->field_count,
3775 stat>>28, btread(BT848_RISC_COUNT));
3776 bttv_print_irqbits(stat,astat);
3777 if (stat & BT848_INT_HLOCK)
3778 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3780 if (stat & BT848_INT_VPRES)
3781 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3783 if (stat & BT848_INT_FMTCHG)
3784 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3789 if (astat&BT848_INT_VSYNC)
3792 if ((astat & BT848_INT_GPINT) && btv->remote) {
3793 wake_up(&btv->gpioq);
3794 bttv_input_irq(btv);
3797 if (astat & BT848_INT_I2CDONE) {
3798 btv->i2c_done = stat;
3799 wake_up(&btv->i2c_queue);
3802 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3803 bttv_irq_switch_vbi(btv);
3805 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3806 bttv_irq_wakeup_top(btv);
3808 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3809 bttv_irq_switch_video(btv);
3811 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3812 audio_mute(btv, btv->mute); /* trigger automute */
3814 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3815 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3816 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3817 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3818 btread(BT848_RISC_COUNT));
3819 bttv_print_irqbits(stat,astat);
3822 bttv_print_riscaddr(btv);
3824 if (fdsr && astat & BT848_INT_FDSR) {
3825 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3826 btv->c.nr,btread(BT848_RISC_COUNT));
3828 bttv_print_riscaddr(btv);
3834 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3835 btwrite(0, BT848_INT_MASK);
3838 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3841 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
3843 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3847 bttv_print_irqbits(stat,astat);
3855 return IRQ_RETVAL(handled);
3859 /* ----------------------------------------------------------------------- */
3860 /* initialitation */
3862 static struct video_device *vdev_init(struct bttv *btv,
3863 struct video_device *template,
3866 struct video_device *vfd;
3868 vfd = video_device_alloc();
3873 vfd->dev = &btv->c.pci->dev;
3874 vfd->release = video_device_release;
3875 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3876 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3877 type, bttv_tvcards[btv->c.type].name);
3881 static void bttv_unregister_video(struct bttv *btv)
3883 if (btv->video_dev) {
3884 if (-1 != btv->video_dev->minor)
3885 video_unregister_device(btv->video_dev);
3887 video_device_release(btv->video_dev);
3888 btv->video_dev = NULL;
3891 if (-1 != btv->vbi_dev->minor)
3892 video_unregister_device(btv->vbi_dev);
3894 video_device_release(btv->vbi_dev);
3895 btv->vbi_dev = NULL;
3897 if (btv->radio_dev) {
3898 if (-1 != btv->radio_dev->minor)
3899 video_unregister_device(btv->radio_dev);
3901 video_device_release(btv->radio_dev);
3902 btv->radio_dev = NULL;
3906 /* register video4linux devices */
3907 static int __devinit bttv_register_video(struct bttv *btv)
3909 if (no_overlay <= 0) {
3910 bttv_video_template.type |= VID_TYPE_OVERLAY;
3912 printk("bttv: Overlay support disabled.\n");
3916 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3917 if (NULL == btv->video_dev)
3919 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3921 printk(KERN_INFO "bttv%d: registered device video%d\n",
3922 btv->c.nr,btv->video_dev->minor & 0x1f);
3923 video_device_create_file(btv->video_dev, &class_device_attr_card);
3926 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3927 if (NULL == btv->vbi_dev)
3929 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3931 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3932 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3934 if (!btv->has_radio)
3937 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3938 if (NULL == btv->radio_dev)
3940 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3942 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3943 btv->c.nr,btv->radio_dev->minor & 0x1f);
3949 bttv_unregister_video(btv);
3954 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3955 /* response on cards with no firmware is not enabled by OF */
3956 static void pci_set_command(struct pci_dev *dev)
3958 #if defined(__powerpc__)
3961 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3962 cmd = (cmd | PCI_COMMAND_MEMORY );
3963 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3967 static int __devinit bttv_probe(struct pci_dev *dev,
3968 const struct pci_device_id *pci_id)
3974 if (bttv_num == BTTV_MAX)
3976 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3977 btv=&bttvs[bttv_num];
3978 memset(btv,0,sizeof(*btv));
3979 btv->c.nr = bttv_num;
3980 sprintf(btv->c.name,"bttv%d",btv->c.nr);
3982 /* initialize structs / fill in defaults */
3983 mutex_init(&btv->lock);
3984 mutex_init(&btv->reslock);
3985 spin_lock_init(&btv->s_lock);
3986 spin_lock_init(&btv->gpio_lock);
3987 init_waitqueue_head(&btv->gpioq);
3988 init_waitqueue_head(&btv->i2c_queue);
3989 INIT_LIST_HEAD(&btv->c.subs);
3990 INIT_LIST_HEAD(&btv->capture);
3991 INIT_LIST_HEAD(&btv->vcapture);
3992 v4l2_prio_init(&btv->prio);
3994 init_timer(&btv->timeout);
3995 btv->timeout.function = bttv_irq_timeout;
3996 btv->timeout.data = (unsigned long)btv;
3999 btv->tuner_type = UNSET;
4000 btv->new_input = UNSET;
4001 btv->has_radio=radio[btv->c.nr];
4003 /* pci stuff (init, get irq/mmio, ... */
4005 btv->id = dev->device;
4006 if (pci_enable_device(dev)) {
4007 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4011 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
4012 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4016 if (!request_mem_region(pci_resource_start(dev,0),
4017 pci_resource_len(dev,0),
4019 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
4020 btv->c.nr, pci_resource_start(dev,0));
4023 pci_set_master(dev);
4024 pci_set_command(dev);
4025 pci_set_drvdata(dev,btv);
4027 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4028 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4029 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4030 bttv_num,btv->id, btv->revision, pci_name(dev));
4031 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
4032 btv->c.pci->irq, lat, pci_resource_start(dev,0));
4035 btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
4036 if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
4037 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4045 /* disable irqs, register irq handler */
4046 btwrite(0, BT848_INT_MASK);
4047 result = request_irq(btv->c.pci->irq, bttv_irq,
4048 SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
4050 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4051 bttv_num,btv->c.pci->irq);
4055 if (0 != bttv_handle_chipset(btv)) {
4060 /* init options from insmod args */
4061 btv->opt_combfilter = combfilter;
4062 btv->opt_lumafilter = lumafilter;
4063 btv->opt_automute = automute;
4064 btv->opt_chroma_agc = chroma_agc;
4065 btv->opt_adc_crush = adc_crush;
4066 btv->opt_vcr_hack = vcr_hack;
4067 btv->opt_whitecrush_upper = whitecrush_upper;
4068 btv->opt_whitecrush_lower = whitecrush_lower;
4069 btv->opt_uv_ratio = uv_ratio;
4070 btv->opt_full_luma_range = full_luma_range;
4071 btv->opt_coring = coring;
4073 /* fill struct bttv with some useful defaults */
4074 btv->init.btv = btv;
4075 btv->init.ov.w.width = 320;
4076 btv->init.ov.w.height = 240;
4077 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4078 btv->init.width = 320;
4079 btv->init.height = 240;
4080 btv->init.lines = 16;
4083 /* initialize hardware */
4085 bttv_gpio_tracking(btv,"pre-init");
4087 bttv_risc_init_main(btv);
4091 btwrite(0x00, BT848_GPIO_REG_INP);
4092 btwrite(0x00, BT848_GPIO_OUT_EN);
4094 bttv_gpio_tracking(btv,"init");
4096 /* needs to be done before i2c is registered */
4097 bttv_init_card1(btv);
4099 /* register i2c + gpio */
4102 /* some card-specific stuff (needs working i2c) */
4103 bttv_init_card2(btv);
4106 /* register video4linux + input */
4107 if (!bttv_tvcards[btv->c.type].no_video) {
4108 bttv_register_video(btv);
4109 bt848_bright(btv,32768);
4110 bt848_contrast(btv,32768);
4111 bt848_hue(btv,32768);
4112 bt848_sat(btv,32768);
4117 /* add subdevices */
4118 if (bttv_tvcards[btv->c.type].has_dvb)
4119 bttv_sub_add_device(&btv->c, "dvb");
4121 bttv_input_init(btv);
4123 /* everything is fine */
4128 free_irq(btv->c.pci->irq,btv);
4131 if (btv->bt848_mmio)
4132 iounmap(btv->bt848_mmio);
4133 release_mem_region(pci_resource_start(btv->c.pci,0),
4134 pci_resource_len(btv->c.pci,0));
4135 pci_set_drvdata(dev,NULL);
4139 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4141 struct bttv *btv = pci_get_drvdata(pci_dev);
4144 printk("bttv%d: unloading\n",btv->c.nr);
4146 /* shutdown everything (DMA+IRQs) */
4147 btand(~15, BT848_GPIO_DMA_CTL);
4148 btwrite(0, BT848_INT_MASK);
4149 btwrite(~0x0, BT848_INT_STAT);
4150 btwrite(0x0, BT848_GPIO_OUT_EN);
4152 bttv_gpio_tracking(btv,"cleanup");
4154 /* tell gpio modules we are leaving ... */
4156 wake_up(&btv->gpioq);
4157 bttv_input_fini(btv);
4158 bttv_sub_del_devices(&btv->c);
4160 /* unregister i2c_bus + input */
4163 /* unregister video4linux */
4164 bttv_unregister_video(btv);
4166 /* free allocated memory */
4167 btcx_riscmem_free(btv->c.pci,&btv->main);
4169 /* free ressources */
4170 free_irq(btv->c.pci->irq,btv);
4171 iounmap(btv->bt848_mmio);
4172 release_mem_region(pci_resource_start(btv->c.pci,0),
4173 pci_resource_len(btv->c.pci,0));
4175 pci_set_drvdata(pci_dev, NULL);
4179 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4181 struct bttv *btv = pci_get_drvdata(pci_dev);
4182 struct bttv_buffer_set idle;
4183 unsigned long flags;
4185 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4187 /* stop dma + irqs */
4188 spin_lock_irqsave(&btv->s_lock,flags);
4189 memset(&idle, 0, sizeof(idle));
4190 btv->state.video = btv->curr;
4191 btv->state.vbi = btv->cvbi;
4192 btv->state.loop_irq = btv->loop_irq;
4195 bttv_buffer_activate_video(btv, &idle);
4196 bttv_buffer_activate_vbi(btv, NULL);
4197 bttv_set_dma(btv, 0);
4198 btwrite(0, BT848_INT_MASK);
4199 spin_unlock_irqrestore(&btv->s_lock,flags);
4201 /* save bt878 state */
4202 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4203 btv->state.gpio_data = gpio_read();
4205 /* save pci state */
4206 pci_save_state(pci_dev);
4207 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4208 pci_disable_device(pci_dev);
4209 btv->state.disabled = 1;
4214 static int bttv_resume(struct pci_dev *pci_dev)
4216 struct bttv *btv = pci_get_drvdata(pci_dev);
4217 unsigned long flags;
4220 dprintk("bttv%d: resume\n", btv->c.nr);
4222 /* restore pci state */
4223 if (btv->state.disabled) {
4224 err=pci_enable_device(pci_dev);
4226 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4230 btv->state.disabled = 0;
4232 err=pci_set_power_state(pci_dev, PCI_D0);
4234 pci_disable_device(pci_dev);
4235 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4237 btv->state.disabled = 1;
4241 pci_restore_state(pci_dev);
4243 /* restore bt878 state */
4244 bttv_reinit_bt848(btv);
4245 gpio_inout(0xffffff, btv->state.gpio_enable);
4246 gpio_write(btv->state.gpio_data);
4249 spin_lock_irqsave(&btv->s_lock,flags);
4250 btv->curr = btv->state.video;
4251 btv->cvbi = btv->state.vbi;
4252 btv->loop_irq = btv->state.loop_irq;
4253 bttv_buffer_activate_video(btv, &btv->curr);
4254 bttv_buffer_activate_vbi(btv, btv->cvbi);
4255 bttv_set_dma(btv, 0);
4256 spin_unlock_irqrestore(&btv->s_lock,flags);
4260 static struct pci_device_id bttv_pci_tbl[] = {
4261 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4262 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4263 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4264 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4265 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4266 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4267 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4268 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4272 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4274 static struct pci_driver bttv_pci_driver = {
4276 .id_table = bttv_pci_tbl,
4277 .probe = bttv_probe,
4278 .remove = __devexit_p(bttv_remove),
4279 .suspend = bttv_suspend,
4280 .resume = bttv_resume,
4283 static int bttv_init_module(void)
4287 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4288 (BTTV_VERSION_CODE >> 16) & 0xff,
4289 (BTTV_VERSION_CODE >> 8) & 0xff,
4290 BTTV_VERSION_CODE & 0xff);
4292 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4293 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4295 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4297 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4298 gbufsize = BTTV_MAX_FBUF;
4299 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4301 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4302 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4304 bttv_check_chipset();
4306 bus_register(&bttv_sub_bus_type);
4307 return pci_register_driver(&bttv_pci_driver);
4310 static void bttv_cleanup_module(void)
4312 pci_unregister_driver(&bttv_pci_driver);
4313 bus_unregister(&bttv_sub_bus_type);
4317 module_init(bttv_init_module);
4318 module_exit(bttv_cleanup_module);