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>
40 #include <linux/dma-mapping.h>
43 #include <asm/byteorder.h>
48 unsigned int bttv_num; /* number of Bt848s in use */
49 struct bttv bttvs[BTTV_MAX];
51 unsigned int bttv_debug = 0;
52 unsigned int bttv_verbose = 1;
53 unsigned int bttv_gpio = 0;
55 /* config variables */
57 static unsigned int bigendian=1;
59 static unsigned int bigendian=0;
61 static unsigned int radio[BTTV_MAX];
62 static unsigned int irq_debug = 0;
63 static unsigned int gbuffers = 8;
64 static unsigned int gbufsize = 0x208000;
66 static int video_nr = -1;
67 static int radio_nr = -1;
68 static int vbi_nr = -1;
69 static int debug_latency = 0;
71 static unsigned int fdsr = 0;
74 static unsigned int combfilter = 0;
75 static unsigned int lumafilter = 0;
76 static unsigned int automute = 1;
77 static unsigned int chroma_agc = 0;
78 static unsigned int adc_crush = 1;
79 static unsigned int whitecrush_upper = 0xCF;
80 static unsigned int whitecrush_lower = 0x7F;
81 static unsigned int vcr_hack = 0;
82 static unsigned int irq_iswitch = 0;
83 static unsigned int uv_ratio = 50;
84 static unsigned int full_luma_range = 0;
85 static unsigned int coring = 0;
86 extern int no_overlay;
88 /* API features (turn on/off stuff for testing) */
89 static unsigned int v4l2 = 1;
93 module_param(bttv_verbose, int, 0644);
94 module_param(bttv_gpio, int, 0644);
95 module_param(bttv_debug, int, 0644);
96 module_param(irq_debug, int, 0644);
97 module_param(debug_latency, int, 0644);
99 module_param(fdsr, int, 0444);
100 module_param(video_nr, int, 0444);
101 module_param(radio_nr, int, 0444);
102 module_param(vbi_nr, int, 0444);
103 module_param(gbuffers, int, 0444);
104 module_param(gbufsize, int, 0444);
106 module_param(v4l2, int, 0644);
107 module_param(bigendian, int, 0644);
108 module_param(irq_iswitch, int, 0644);
109 module_param(combfilter, int, 0444);
110 module_param(lumafilter, int, 0444);
111 module_param(automute, int, 0444);
112 module_param(chroma_agc, int, 0444);
113 module_param(adc_crush, int, 0444);
114 module_param(whitecrush_upper, int, 0444);
115 module_param(whitecrush_lower, int, 0444);
116 module_param(vcr_hack, int, 0444);
117 module_param(uv_ratio, int, 0444);
118 module_param(full_luma_range, int, 0444);
119 module_param(coring, int, 0444);
121 module_param_array(radio, int, NULL, 0444);
123 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
124 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
125 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
126 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
127 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
128 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
129 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
130 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
131 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
132 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
133 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
134 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
135 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
136 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
137 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
138 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
139 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
140 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
142 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
143 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
144 MODULE_LICENSE("GPL");
146 /* ----------------------------------------------------------------------- */
149 static ssize_t show_card(struct class_device *cd, char *buf)
151 struct video_device *vfd = to_video_device(cd);
152 struct bttv *btv = dev_get_drvdata(vfd->dev);
153 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
155 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
157 /* ----------------------------------------------------------------------- */
160 /* special timing tables from conexant... */
161 static u8 SRAM_Table[][60] =
163 /* PAL digital input over GPIO[7:0] */
165 45, // 45 bytes following
166 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
167 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
168 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
169 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
170 0x37,0x00,0xAF,0x21,0x00
172 /* NTSC digital input over GPIO[7:0] */
174 51, // 51 bytes following
175 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
176 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
177 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
178 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
179 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
182 // TGB_NTSC392 // quartzsight
183 // This table has been modified to be used for Fusion Rev D
185 0x2A, // size of table = 42
186 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
187 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
188 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
189 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
194 const struct bttv_tvnorm bttv_tvnorms[] = {
196 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
197 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
199 .v4l2_id = V4L2_STD_PAL,
207 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
208 .scaledtwidth = 1135,
214 /* ITU-R frame line number of the first VBI line
215 we can capture, of the first and second field. */
216 .vbistart = { 7,320 },
218 .v4l2_id = V4L2_STD_NTSC_M,
226 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
233 .vbistart = { 10, 273 },
235 .v4l2_id = V4L2_STD_SECAM,
243 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
244 .scaledtwidth = 1135,
249 .sram = 0, /* like PAL, correct? */
250 .vbistart = { 7, 320 },
252 .v4l2_id = V4L2_STD_PAL_Nc,
260 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
267 .vbistart = { 7, 320 },
269 .v4l2_id = V4L2_STD_PAL_M,
277 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
284 .vbistart = { 10, 273 },
286 .v4l2_id = V4L2_STD_PAL_N,
294 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
301 .vbistart = { 7, 320},
303 .v4l2_id = V4L2_STD_NTSC_M_JP,
311 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
318 .vbistart = {10, 273},
320 /* that one hopefully works with the strange timing
321 * which video recorders produce when playing a NTSC
322 * tape on a PAL TV ... */
323 .v4l2_id = V4L2_STD_PAL_60,
331 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
332 .scaledtwidth = 1135,
339 .vbistart = { 10, 273 },
342 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
344 /* ----------------------------------------------------------------------- */
346 packed pixel formats must come first */
347 static const struct bttv_format bttv_formats[] = {
349 .name = "8 bpp, gray",
350 .palette = VIDEO_PALETTE_GREY,
351 .fourcc = V4L2_PIX_FMT_GREY,
352 .btformat = BT848_COLOR_FMT_Y8,
354 .flags = FORMAT_FLAGS_PACKED,
356 .name = "8 bpp, dithered color",
357 .palette = VIDEO_PALETTE_HI240,
358 .fourcc = V4L2_PIX_FMT_HI240,
359 .btformat = BT848_COLOR_FMT_RGB8,
361 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
363 .name = "15 bpp RGB, le",
364 .palette = VIDEO_PALETTE_RGB555,
365 .fourcc = V4L2_PIX_FMT_RGB555,
366 .btformat = BT848_COLOR_FMT_RGB15,
368 .flags = FORMAT_FLAGS_PACKED,
370 .name = "15 bpp RGB, be",
372 .fourcc = V4L2_PIX_FMT_RGB555X,
373 .btformat = BT848_COLOR_FMT_RGB15,
374 .btswap = 0x03, /* byteswap */
376 .flags = FORMAT_FLAGS_PACKED,
378 .name = "16 bpp RGB, le",
379 .palette = VIDEO_PALETTE_RGB565,
380 .fourcc = V4L2_PIX_FMT_RGB565,
381 .btformat = BT848_COLOR_FMT_RGB16,
383 .flags = FORMAT_FLAGS_PACKED,
385 .name = "16 bpp RGB, be",
387 .fourcc = V4L2_PIX_FMT_RGB565X,
388 .btformat = BT848_COLOR_FMT_RGB16,
389 .btswap = 0x03, /* byteswap */
391 .flags = FORMAT_FLAGS_PACKED,
393 .name = "24 bpp RGB, le",
394 .palette = VIDEO_PALETTE_RGB24,
395 .fourcc = V4L2_PIX_FMT_BGR24,
396 .btformat = BT848_COLOR_FMT_RGB24,
398 .flags = FORMAT_FLAGS_PACKED,
400 .name = "32 bpp RGB, le",
401 .palette = VIDEO_PALETTE_RGB32,
402 .fourcc = V4L2_PIX_FMT_BGR32,
403 .btformat = BT848_COLOR_FMT_RGB32,
405 .flags = FORMAT_FLAGS_PACKED,
407 .name = "32 bpp RGB, be",
409 .fourcc = V4L2_PIX_FMT_RGB32,
410 .btformat = BT848_COLOR_FMT_RGB32,
411 .btswap = 0x0f, /* byte+word swap */
413 .flags = FORMAT_FLAGS_PACKED,
415 .name = "4:2:2, packed, YUYV",
416 .palette = VIDEO_PALETTE_YUV422,
417 .fourcc = V4L2_PIX_FMT_YUYV,
418 .btformat = BT848_COLOR_FMT_YUY2,
420 .flags = FORMAT_FLAGS_PACKED,
422 .name = "4:2:2, packed, YUYV",
423 .palette = VIDEO_PALETTE_YUYV,
424 .fourcc = V4L2_PIX_FMT_YUYV,
425 .btformat = BT848_COLOR_FMT_YUY2,
427 .flags = FORMAT_FLAGS_PACKED,
429 .name = "4:2:2, packed, UYVY",
430 .palette = VIDEO_PALETTE_UYVY,
431 .fourcc = V4L2_PIX_FMT_UYVY,
432 .btformat = BT848_COLOR_FMT_YUY2,
433 .btswap = 0x03, /* byteswap */
435 .flags = FORMAT_FLAGS_PACKED,
437 .name = "4:2:2, planar, Y-Cb-Cr",
438 .palette = VIDEO_PALETTE_YUV422P,
439 .fourcc = V4L2_PIX_FMT_YUV422P,
440 .btformat = BT848_COLOR_FMT_YCrCb422,
442 .flags = FORMAT_FLAGS_PLANAR,
446 .name = "4:2:0, planar, Y-Cb-Cr",
447 .palette = VIDEO_PALETTE_YUV420P,
448 .fourcc = V4L2_PIX_FMT_YUV420,
449 .btformat = BT848_COLOR_FMT_YCrCb422,
451 .flags = FORMAT_FLAGS_PLANAR,
455 .name = "4:2:0, planar, Y-Cr-Cb",
457 .fourcc = V4L2_PIX_FMT_YVU420,
458 .btformat = BT848_COLOR_FMT_YCrCb422,
460 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
464 .name = "4:1:1, planar, Y-Cb-Cr",
465 .palette = VIDEO_PALETTE_YUV411P,
466 .fourcc = V4L2_PIX_FMT_YUV411P,
467 .btformat = BT848_COLOR_FMT_YCrCb411,
469 .flags = FORMAT_FLAGS_PLANAR,
473 .name = "4:1:0, planar, Y-Cb-Cr",
474 .palette = VIDEO_PALETTE_YUV410P,
475 .fourcc = V4L2_PIX_FMT_YUV410,
476 .btformat = BT848_COLOR_FMT_YCrCb411,
478 .flags = FORMAT_FLAGS_PLANAR,
482 .name = "4:1:0, planar, Y-Cr-Cb",
484 .fourcc = V4L2_PIX_FMT_YVU410,
485 .btformat = BT848_COLOR_FMT_YCrCb411,
487 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
491 .name = "raw scanlines",
492 .palette = VIDEO_PALETTE_RAW,
494 .btformat = BT848_COLOR_FMT_RAW,
496 .flags = FORMAT_FLAGS_RAW,
499 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
501 /* ----------------------------------------------------------------------- */
503 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
504 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
505 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
506 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
507 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
508 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
509 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
510 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
511 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
512 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
513 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
514 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
516 static const struct v4l2_queryctrl no_ctl = {
518 .flags = V4L2_CTRL_FLAG_DISABLED,
520 static const struct v4l2_queryctrl bttv_ctls[] = {
523 .id = V4L2_CID_BRIGHTNESS,
524 .name = "Brightness",
528 .default_value = 32768,
529 .type = V4L2_CTRL_TYPE_INTEGER,
531 .id = V4L2_CID_CONTRAST,
536 .default_value = 32768,
537 .type = V4L2_CTRL_TYPE_INTEGER,
539 .id = V4L2_CID_SATURATION,
540 .name = "Saturation",
544 .default_value = 32768,
545 .type = V4L2_CTRL_TYPE_INTEGER,
552 .default_value = 32768,
553 .type = V4L2_CTRL_TYPE_INTEGER,
557 .id = V4L2_CID_AUDIO_MUTE,
561 .type = V4L2_CTRL_TYPE_BOOLEAN,
563 .id = V4L2_CID_AUDIO_VOLUME,
568 .default_value = 65535,
569 .type = V4L2_CTRL_TYPE_INTEGER,
571 .id = V4L2_CID_AUDIO_BALANCE,
576 .default_value = 32768,
577 .type = V4L2_CTRL_TYPE_INTEGER,
579 .id = V4L2_CID_AUDIO_BASS,
584 .default_value = 32768,
585 .type = V4L2_CTRL_TYPE_INTEGER,
587 .id = V4L2_CID_AUDIO_TREBLE,
592 .default_value = 32768,
593 .type = V4L2_CTRL_TYPE_INTEGER,
595 /* --- private --- */
597 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
598 .name = "chroma agc",
601 .type = V4L2_CTRL_TYPE_BOOLEAN,
603 .id = V4L2_CID_PRIVATE_COMBFILTER,
604 .name = "combfilter",
607 .type = V4L2_CTRL_TYPE_BOOLEAN,
609 .id = V4L2_CID_PRIVATE_AUTOMUTE,
613 .type = V4L2_CTRL_TYPE_BOOLEAN,
615 .id = V4L2_CID_PRIVATE_LUMAFILTER,
616 .name = "luma decimation filter",
619 .type = V4L2_CTRL_TYPE_BOOLEAN,
621 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
625 .type = V4L2_CTRL_TYPE_BOOLEAN,
627 .id = V4L2_CID_PRIVATE_VCR_HACK,
631 .type = V4L2_CTRL_TYPE_BOOLEAN,
633 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
634 .name = "whitecrush upper",
638 .default_value = 0xCF,
639 .type = V4L2_CTRL_TYPE_INTEGER,
641 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
642 .name = "whitecrush lower",
646 .default_value = 0x7F,
647 .type = V4L2_CTRL_TYPE_INTEGER,
649 .id = V4L2_CID_PRIVATE_UV_RATIO,
655 .type = V4L2_CTRL_TYPE_INTEGER,
657 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
658 .name = "full luma range",
661 .type = V4L2_CTRL_TYPE_BOOLEAN,
663 .id = V4L2_CID_PRIVATE_CORING,
669 .type = V4L2_CTRL_TYPE_INTEGER,
675 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
677 /* ----------------------------------------------------------------------- */
678 /* resource management */
681 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
683 if (fh->resources & bit)
684 /* have it already allocated */
689 if (btv->resources & bit) {
690 /* no, someone else uses it */
694 /* it's free, grab it */
695 fh->resources |= bit;
696 btv->resources |= bit;
702 int check_btres(struct bttv_fh *fh, int bit)
704 return (fh->resources & bit);
708 int locked_btres(struct bttv *btv, int bit)
710 return (btv->resources & bit);
714 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
716 if ((fh->resources & bits) != bits) {
717 /* trying to free ressources not allocated by us ... */
718 printk("bttv: BUG! (btres)\n");
721 fh->resources &= ~bits;
722 btv->resources &= ~bits;
726 /* ----------------------------------------------------------------------- */
727 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
729 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
730 PLL_X = Reference pre-divider (0=1, 1=2)
731 PLL_C = Post divider (0=6, 1=4)
732 PLL_I = Integer input
733 PLL_F = Fractional input
735 F_input = 28.636363 MHz:
736 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
739 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
741 unsigned char fl, fh, fi;
743 /* prevent overflows */
756 btwrite(fl, BT848_PLL_F_LO);
757 btwrite(fh, BT848_PLL_F_HI);
758 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
761 static void set_pll(struct bttv *btv)
765 if (!btv->pll.pll_crystal)
768 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
769 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
773 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
775 if (btv->pll.pll_current == 0)
777 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
778 btv->c.nr,btv->pll.pll_ifreq);
779 btwrite(0x00,BT848_TGCTRL);
780 btwrite(0x00,BT848_PLL_XCI);
781 btv->pll.pll_current = 0;
785 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
786 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
787 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
789 for (i=0; i<10; i++) {
790 /* Let other people run while the PLL stabilizes */
794 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
795 btwrite(0,BT848_DSTATUS);
797 btwrite(0x08,BT848_TGCTRL);
798 btv->pll.pll_current = btv->pll.pll_ofreq;
799 bttv_printk(" ok\n");
803 btv->pll.pll_current = -1;
804 bttv_printk("failed\n");
808 /* used to switch between the bt848's analog/digital video capture modes */
809 static void bt848A_set_timing(struct bttv *btv)
812 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
813 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
815 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
816 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
817 btv->c.nr,table_idx);
819 /* timing change...reset timing generator address */
820 btwrite(0x00, BT848_TGCTRL);
821 btwrite(0x02, BT848_TGCTRL);
822 btwrite(0x00, BT848_TGCTRL);
824 len=SRAM_Table[table_idx][0];
825 for(i = 1; i <= len; i++)
826 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
827 btv->pll.pll_ofreq = 27000000;
830 btwrite(0x11, BT848_TGCTRL);
831 btwrite(0x41, BT848_DVSIF);
833 btv->pll.pll_ofreq = fsc;
835 btwrite(0x0, BT848_DVSIF);
839 /* ----------------------------------------------------------------------- */
841 static void bt848_bright(struct bttv *btv, int bright)
845 // printk("bttv: set bright: %d\n",bright); // DEBUG
846 btv->bright = bright;
848 /* We want -128 to 127 we get 0-65535 */
849 value = (bright >> 8) - 128;
850 btwrite(value & 0xff, BT848_BRIGHT);
853 static void bt848_hue(struct bttv *btv, int hue)
860 value = (hue >> 8) - 128;
861 btwrite(value & 0xff, BT848_HUE);
864 static void bt848_contrast(struct bttv *btv, int cont)
868 btv->contrast = cont;
872 hibit = (value >> 6) & 4;
873 btwrite(value & 0xff, BT848_CONTRAST_LO);
874 btaor(hibit, ~4, BT848_E_CONTROL);
875 btaor(hibit, ~4, BT848_O_CONTROL);
878 static void bt848_sat(struct bttv *btv, int color)
880 int val_u,val_v,hibits;
882 btv->saturation = color;
884 /* 0-511 for the color */
885 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
886 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
887 hibits = (val_u >> 7) & 2;
888 hibits |= (val_v >> 8) & 1;
889 btwrite(val_u & 0xff, BT848_SAT_U_LO);
890 btwrite(val_v & 0xff, BT848_SAT_V_LO);
891 btaor(hibits, ~3, BT848_E_CONTROL);
892 btaor(hibits, ~3, BT848_O_CONTROL);
895 /* ----------------------------------------------------------------------- */
898 video_mux(struct bttv *btv, unsigned int input)
902 if (input >= bttv_tvcards[btv->c.type].video_inputs)
905 /* needed by RemoteVideo MX */
906 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
908 gpio_inout(mask2,mask2);
910 if (input == btv->svhs) {
911 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
912 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
914 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
915 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
917 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
918 btaor(mux<<5, ~(3<<5), BT848_IFORM);
919 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
920 btv->c.nr,input,mux);
922 /* card specific hook */
923 if(bttv_tvcards[btv->c.type].muxsel_hook)
924 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
928 static char *audio_modes[] = {
929 "audio: tuner", "audio: radio", "audio: extern",
930 "audio: intern", "audio: off"
934 audio_mux(struct bttv *btv, int mode)
936 int val,mux,i2c_mux,signal;
938 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
939 bttv_tvcards[btv->c.type].gpiomask);
940 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
944 btv->audio |= AUDIO_MUTE;
947 btv->audio &= ~AUDIO_MUTE;
953 btv->audio &= AUDIO_MUTE;
956 i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
957 if (btv->opt_automute && !signal && !btv->radio_user)
960 val = bttv_tvcards[btv->c.type].audiomux[mux];
961 gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
963 bttv_gpio_tracking(btv,audio_modes[mux]);
965 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
970 i2c_vidiocschan(struct bttv *btv)
972 struct video_channel c;
974 memset(&c,0,sizeof(c));
975 c.norm = btv->tvnorm;
976 c.channel = btv->input;
977 bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
978 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM)
979 bttv_tda9880_setnorm(btv,c.norm);
983 set_tvnorm(struct bttv *btv, unsigned int norm)
985 const struct bttv_tvnorm *tvnorm;
987 if (norm < 0 || norm >= BTTV_TVNORMS)
991 tvnorm = &bttv_tvnorms[norm];
993 btwrite(tvnorm->adelay, BT848_ADELAY);
994 btwrite(tvnorm->bdelay, BT848_BDELAY);
995 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
997 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
998 btwrite(1, BT848_VBI_PACK_DEL);
999 bt848A_set_timing(btv);
1001 switch (btv->c.type) {
1002 case BTTV_BOARD_VOODOOTV_FM:
1003 bttv_tda9880_setnorm(btv,norm);
1010 set_input(struct bttv *btv, unsigned int input)
1012 unsigned long flags;
1016 spin_lock_irqsave(&btv->s_lock,flags);
1017 if (btv->curr.frame_irq) {
1018 /* active capture -> delayed input switch */
1019 btv->new_input = input;
1021 video_mux(btv,input);
1023 spin_unlock_irqrestore(&btv->s_lock,flags);
1025 video_mux(btv,input);
1027 audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1028 AUDIO_TUNER : AUDIO_EXTERN));
1029 set_tvnorm(btv,btv->tvnorm);
1030 i2c_vidiocschan(btv);
1033 static void init_irqreg(struct bttv *btv)
1036 btwrite(0xfffffUL, BT848_INT_STAT);
1038 if (bttv_tvcards[btv->c.type].no_video) {
1040 btwrite(BT848_INT_I2CDONE,
1044 btwrite((btv->triton1) |
1045 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1047 (fdsr ? BT848_INT_FDSR : 0) |
1048 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1049 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1055 static void init_bt848(struct bttv *btv)
1059 if (bttv_tvcards[btv->c.type].no_video) {
1060 /* very basic init only */
1065 btwrite(0x00, BT848_CAP_CTL);
1066 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1067 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1069 /* set planar and packed mode trigger points and */
1070 /* set rising edge of inverted GPINTR pin as irq trigger */
1071 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1072 BT848_GPIO_DMA_CTL_PLTP1_16|
1073 BT848_GPIO_DMA_CTL_PLTP23_16|
1074 BT848_GPIO_DMA_CTL_GPINTC|
1075 BT848_GPIO_DMA_CTL_GPINTI,
1076 BT848_GPIO_DMA_CTL);
1078 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1079 btwrite(val, BT848_E_SCLOOP);
1080 btwrite(val, BT848_O_SCLOOP);
1082 btwrite(0x20, BT848_E_VSCALE_HI);
1083 btwrite(0x20, BT848_O_VSCALE_HI);
1084 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1087 btwrite(whitecrush_upper, BT848_WC_UP);
1088 btwrite(whitecrush_lower, BT848_WC_DOWN);
1090 if (btv->opt_lumafilter) {
1091 btwrite(0, BT848_E_CONTROL);
1092 btwrite(0, BT848_O_CONTROL);
1094 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1095 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1098 bt848_bright(btv, btv->bright);
1099 bt848_hue(btv, btv->hue);
1100 bt848_contrast(btv, btv->contrast);
1101 bt848_sat(btv, btv->saturation);
1107 static void bttv_reinit_bt848(struct bttv *btv)
1109 unsigned long flags;
1112 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1113 spin_lock_irqsave(&btv->s_lock,flags);
1115 bttv_set_dma(btv,0);
1116 spin_unlock_irqrestore(&btv->s_lock,flags);
1119 btv->pll.pll_current = -1;
1120 set_input(btv,btv->input);
1123 static int get_control(struct bttv *btv, struct v4l2_control *c)
1125 struct video_audio va;
1128 for (i = 0; i < BTTV_CTLS; i++)
1129 if (bttv_ctls[i].id == c->id)
1133 if (i >= 4 && i <= 8) {
1134 memset(&va,0,sizeof(va));
1135 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1136 if (btv->audio_hook)
1137 btv->audio_hook(btv,&va,0);
1140 case V4L2_CID_BRIGHTNESS:
1141 c->value = btv->bright;
1144 c->value = btv->hue;
1146 case V4L2_CID_CONTRAST:
1147 c->value = btv->contrast;
1149 case V4L2_CID_SATURATION:
1150 c->value = btv->saturation;
1153 case V4L2_CID_AUDIO_MUTE:
1154 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1156 case V4L2_CID_AUDIO_VOLUME:
1157 c->value = va.volume;
1159 case V4L2_CID_AUDIO_BALANCE:
1160 c->value = va.balance;
1162 case V4L2_CID_AUDIO_BASS:
1165 case V4L2_CID_AUDIO_TREBLE:
1166 c->value = va.treble;
1169 case V4L2_CID_PRIVATE_CHROMA_AGC:
1170 c->value = btv->opt_chroma_agc;
1172 case V4L2_CID_PRIVATE_COMBFILTER:
1173 c->value = btv->opt_combfilter;
1175 case V4L2_CID_PRIVATE_LUMAFILTER:
1176 c->value = btv->opt_lumafilter;
1178 case V4L2_CID_PRIVATE_AUTOMUTE:
1179 c->value = btv->opt_automute;
1181 case V4L2_CID_PRIVATE_AGC_CRUSH:
1182 c->value = btv->opt_adc_crush;
1184 case V4L2_CID_PRIVATE_VCR_HACK:
1185 c->value = btv->opt_vcr_hack;
1187 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1188 c->value = btv->opt_whitecrush_upper;
1190 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1191 c->value = btv->opt_whitecrush_lower;
1193 case V4L2_CID_PRIVATE_UV_RATIO:
1194 c->value = btv->opt_uv_ratio;
1196 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1197 c->value = btv->opt_full_luma_range;
1199 case V4L2_CID_PRIVATE_CORING:
1200 c->value = btv->opt_coring;
1208 static int set_control(struct bttv *btv, struct v4l2_control *c)
1210 struct video_audio va;
1213 for (i = 0; i < BTTV_CTLS; i++)
1214 if (bttv_ctls[i].id == c->id)
1218 if (i >= 4 && i <= 8) {
1219 memset(&va,0,sizeof(va));
1220 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1221 if (btv->audio_hook)
1222 btv->audio_hook(btv,&va,0);
1225 case V4L2_CID_BRIGHTNESS:
1226 bt848_bright(btv,c->value);
1229 bt848_hue(btv,c->value);
1231 case V4L2_CID_CONTRAST:
1232 bt848_contrast(btv,c->value);
1234 case V4L2_CID_SATURATION:
1235 bt848_sat(btv,c->value);
1237 case V4L2_CID_AUDIO_MUTE:
1239 va.flags |= VIDEO_AUDIO_MUTE;
1240 audio_mux(btv, AUDIO_MUTE);
1242 va.flags &= ~VIDEO_AUDIO_MUTE;
1243 audio_mux(btv, AUDIO_UNMUTE);
1247 case V4L2_CID_AUDIO_VOLUME:
1248 va.volume = c->value;
1250 case V4L2_CID_AUDIO_BALANCE:
1251 va.balance = c->value;
1253 case V4L2_CID_AUDIO_BASS:
1256 case V4L2_CID_AUDIO_TREBLE:
1257 va.treble = c->value;
1260 case V4L2_CID_PRIVATE_CHROMA_AGC:
1261 btv->opt_chroma_agc = c->value;
1262 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1263 btwrite(val, BT848_E_SCLOOP);
1264 btwrite(val, BT848_O_SCLOOP);
1266 case V4L2_CID_PRIVATE_COMBFILTER:
1267 btv->opt_combfilter = c->value;
1269 case V4L2_CID_PRIVATE_LUMAFILTER:
1270 btv->opt_lumafilter = c->value;
1271 if (btv->opt_lumafilter) {
1272 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1273 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1275 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1276 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1279 case V4L2_CID_PRIVATE_AUTOMUTE:
1280 btv->opt_automute = c->value;
1282 case V4L2_CID_PRIVATE_AGC_CRUSH:
1283 btv->opt_adc_crush = c->value;
1284 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1287 case V4L2_CID_PRIVATE_VCR_HACK:
1288 btv->opt_vcr_hack = c->value;
1290 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1291 btv->opt_whitecrush_upper = c->value;
1292 btwrite(c->value, BT848_WC_UP);
1294 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1295 btv->opt_whitecrush_lower = c->value;
1296 btwrite(c->value, BT848_WC_DOWN);
1298 case V4L2_CID_PRIVATE_UV_RATIO:
1299 btv->opt_uv_ratio = c->value;
1300 bt848_sat(btv, btv->saturation);
1302 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1303 btv->opt_full_luma_range = c->value;
1304 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1306 case V4L2_CID_PRIVATE_CORING:
1307 btv->opt_coring = c->value;
1308 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1313 if (i >= 4 && i <= 8) {
1314 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1315 if (btv->audio_hook)
1316 btv->audio_hook(btv,&va,1);
1321 /* ----------------------------------------------------------------------- */
1323 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1325 unsigned int outbits, data;
1326 outbits = btread(BT848_GPIO_OUT_EN);
1327 data = btread(BT848_GPIO_DATA);
1328 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1329 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1332 static void bttv_field_count(struct bttv *btv)
1340 /* start field counter */
1341 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1343 /* stop field counter */
1344 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1345 btv->field_count = 0;
1349 static const struct bttv_format*
1350 format_by_palette(int palette)
1354 for (i = 0; i < BTTV_FORMATS; i++) {
1355 if (-1 == bttv_formats[i].palette)
1357 if (bttv_formats[i].palette == palette)
1358 return bttv_formats+i;
1363 static const struct bttv_format*
1364 format_by_fourcc(int fourcc)
1368 for (i = 0; i < BTTV_FORMATS; i++) {
1369 if (-1 == bttv_formats[i].fourcc)
1371 if (bttv_formats[i].fourcc == fourcc)
1372 return bttv_formats+i;
1377 /* ----------------------------------------------------------------------- */
1381 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1382 struct bttv_buffer *new)
1384 struct bttv_buffer *old;
1385 unsigned long flags;
1388 dprintk("switch_overlay: enter [new=%p]\n",new);
1390 new->vb.state = STATE_DONE;
1391 spin_lock_irqsave(&btv->s_lock,flags);
1395 bttv_set_dma(btv, 0x03);
1396 spin_unlock_irqrestore(&btv->s_lock,flags);
1398 free_btres(btv,fh,RESOURCE_OVERLAY);
1400 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1401 bttv_dma_free(btv, old);
1404 dprintk("switch_overlay: done\n");
1408 /* ----------------------------------------------------------------------- */
1409 /* video4linux (1) interface */
1411 static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
1412 const struct bttv_format *fmt,
1413 unsigned int width, unsigned int height,
1414 enum v4l2_field field)
1416 int redo_dma_risc = 0;
1419 /* check settings */
1422 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1424 height = RAW_LINES*2;
1425 if (width*height > buf->vb.bsize)
1427 buf->vb.size = buf->vb.bsize;
1431 width > bttv_tvnorms[btv->tvnorm].swidth ||
1432 height > bttv_tvnorms[btv->tvnorm].sheight)
1434 buf->vb.size = (width * height * fmt->depth) >> 3;
1435 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1439 /* alloc + fill struct bttv_buffer (if changed) */
1440 if (buf->vb.width != width || buf->vb.height != height ||
1441 buf->vb.field != field ||
1442 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1443 buf->vb.width = width;
1444 buf->vb.height = height;
1445 buf->vb.field = field;
1446 buf->tvnorm = btv->tvnorm;
1451 /* alloc risc memory */
1452 if (STATE_NEEDS_INIT == buf->vb.state) {
1454 if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
1459 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1462 buf->vb.state = STATE_PREPARED;
1466 bttv_dma_free(btv,buf);
1471 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1473 struct bttv_fh *fh = q->priv_data;
1475 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1478 while (*size * *count > gbuffers * gbufsize)
1484 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1485 enum v4l2_field field)
1487 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1488 struct bttv_fh *fh = q->priv_data;
1490 return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
1491 fh->width, fh->height, field);
1495 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1497 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1498 struct bttv_fh *fh = q->priv_data;
1499 struct bttv *btv = fh->btv;
1501 buf->vb.state = STATE_QUEUED;
1502 list_add_tail(&buf->vb.queue,&btv->capture);
1503 if (!btv->curr.frame_irq) {
1505 bttv_set_dma(btv, 0x03);
1509 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1511 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1512 struct bttv_fh *fh = q->priv_data;
1514 bttv_dma_free(fh->btv,buf);
1517 static struct videobuf_queue_ops bttv_video_qops = {
1518 .buf_setup = buffer_setup,
1519 .buf_prepare = buffer_prepare,
1520 .buf_queue = buffer_queue,
1521 .buf_release = buffer_release,
1524 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1528 return BTTV_VERSION_CODE;
1530 /* *** v4l1 *** ************************************************ */
1533 unsigned long *freq = arg;
1539 unsigned long *freq = arg;
1542 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1543 if (btv->has_matchbox && btv->radio_user)
1544 tea5757_set_freq(btv,*freq);
1551 struct video_tuner *v = arg;
1553 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1555 if (v->tuner) /* Only tuner 0 */
1557 strcpy(v->name, "Television");
1559 v->rangehigh = 0x7FFFFFFF;
1560 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1561 v->mode = btv->tvnorm;
1562 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1563 bttv_call_i2c_clients(btv,cmd,v);
1568 struct video_tuner *v = arg;
1570 if (v->tuner) /* Only tuner 0 */
1572 if (v->mode >= BTTV_TVNORMS)
1576 set_tvnorm(btv,v->mode);
1577 bttv_call_i2c_clients(btv,cmd,v);
1584 struct video_channel *v = arg;
1585 unsigned int channel = v->channel;
1587 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1590 v->flags = VIDEO_VC_AUDIO;
1591 v->type = VIDEO_TYPE_CAMERA;
1592 v->norm = btv->tvnorm;
1593 if (channel == bttv_tvcards[btv->c.type].tuner) {
1594 strcpy(v->name,"Television");
1595 v->flags|=VIDEO_VC_TUNER;
1596 v->type=VIDEO_TYPE_TV;
1598 } else if (channel == btv->svhs) {
1599 strcpy(v->name,"S-Video");
1601 sprintf(v->name,"Composite%d",channel);
1607 struct video_channel *v = arg;
1608 unsigned int channel = v->channel;
1610 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1612 if (v->norm >= BTTV_TVNORMS)
1616 if (channel == btv->input &&
1617 v->norm == btv->tvnorm) {
1623 btv->tvnorm = v->norm;
1624 set_input(btv,v->channel);
1631 struct video_audio *v = arg;
1633 memset(v,0,sizeof(*v));
1634 strcpy(v->name,"Television");
1635 v->flags |= VIDEO_AUDIO_MUTABLE;
1636 v->mode = VIDEO_SOUND_MONO;
1639 bttv_call_i2c_clients(btv,cmd,v);
1641 /* card specific hooks */
1642 if (btv->audio_hook)
1643 btv->audio_hook(btv,v,0);
1650 struct video_audio *v = arg;
1651 unsigned int audio = v->audio;
1653 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1657 audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
1658 bttv_call_i2c_clients(btv,cmd,v);
1660 /* card specific hooks */
1661 if (btv->audio_hook)
1662 btv->audio_hook(btv,v,1);
1668 /* *** v4l2 *** ************************************************ */
1669 case VIDIOC_ENUMSTD:
1671 struct v4l2_standard *e = arg;
1672 unsigned int index = e->index;
1674 if (index >= BTTV_TVNORMS)
1676 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1677 bttv_tvnorms[e->index].name);
1683 v4l2_std_id *id = arg;
1684 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1689 v4l2_std_id *id = arg;
1692 for (i = 0; i < BTTV_TVNORMS; i++)
1693 if (*id & bttv_tvnorms[i].v4l2_id)
1695 if (i == BTTV_TVNORMS)
1700 i2c_vidiocschan(btv);
1704 case VIDIOC_QUERYSTD:
1706 v4l2_std_id *id = arg;
1708 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1709 *id = V4L2_STD_625_50;
1711 *id = V4L2_STD_525_60;
1715 case VIDIOC_ENUMINPUT:
1717 struct v4l2_input *i = arg;
1721 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1723 memset(i,0,sizeof(*i));
1725 i->type = V4L2_INPUT_TYPE_CAMERA;
1727 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1728 sprintf(i->name, "Television");
1729 i->type = V4L2_INPUT_TYPE_TUNER;
1731 } else if (i->index == btv->svhs) {
1732 sprintf(i->name, "S-Video");
1734 sprintf(i->name,"Composite%d",i->index);
1736 if (i->index == btv->input) {
1737 __u32 dstatus = btread(BT848_DSTATUS);
1738 if (0 == (dstatus & BT848_DSTATUS_PRES))
1739 i->status |= V4L2_IN_ST_NO_SIGNAL;
1740 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1741 i->status |= V4L2_IN_ST_NO_H_LOCK;
1743 for (n = 0; n < BTTV_TVNORMS; n++)
1744 i->std |= bttv_tvnorms[n].v4l2_id;
1747 case VIDIOC_G_INPUT:
1753 case VIDIOC_S_INPUT:
1755 unsigned int *i = arg;
1757 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1765 case VIDIOC_G_TUNER:
1767 struct v4l2_tuner *t = arg;
1769 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1774 memset(t,0,sizeof(*t));
1775 strcpy(t->name, "Television");
1776 t->type = V4L2_TUNER_ANALOG_TV;
1777 t->capability = V4L2_TUNER_CAP_NORM;
1778 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1779 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1782 struct video_tuner tuner;
1784 memset(&tuner, 0, sizeof (tuner));
1785 tuner.rangehigh = 0xffffffffUL;
1786 bttv_call_i2c_clients(btv, VIDIOCGTUNER, &tuner);
1787 t->rangelow = tuner.rangelow;
1788 t->rangehigh = tuner.rangehigh;
1792 struct video_audio va;
1793 memset(&va, 0, sizeof(struct video_audio));
1794 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1795 if (btv->audio_hook)
1796 btv->audio_hook(btv,&va,0);
1797 if(va.mode & VIDEO_SOUND_STEREO) {
1798 t->audmode = V4L2_TUNER_MODE_STEREO;
1799 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1801 if(va.mode & VIDEO_SOUND_LANG1) {
1802 t->audmode = V4L2_TUNER_MODE_LANG1;
1803 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1804 | V4L2_TUNER_SUB_LANG2;
1807 /* FIXME: fill capability+audmode */
1811 case VIDIOC_S_TUNER:
1813 struct v4l2_tuner *t = arg;
1815 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1821 struct video_audio va;
1822 memset(&va, 0, sizeof(struct video_audio));
1823 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1824 if (t->audmode == V4L2_TUNER_MODE_MONO)
1825 va.mode = VIDEO_SOUND_MONO;
1826 else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1827 va.mode = VIDEO_SOUND_STEREO;
1828 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1829 va.mode = VIDEO_SOUND_LANG1;
1830 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1831 va.mode = VIDEO_SOUND_LANG2;
1832 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1833 if (btv->audio_hook)
1834 btv->audio_hook(btv,&va,1);
1840 case VIDIOC_G_FREQUENCY:
1842 struct v4l2_frequency *f = arg;
1844 memset(f,0,sizeof(*f));
1845 f->type = V4L2_TUNER_ANALOG_TV;
1846 f->frequency = btv->freq;
1849 case VIDIOC_S_FREQUENCY:
1851 struct v4l2_frequency *f = arg;
1853 if (unlikely(f->tuner != 0))
1855 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
1858 btv->freq = f->frequency;
1859 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1860 if (btv->has_matchbox && btv->radio_user)
1861 tea5757_set_freq(btv,btv->freq);
1865 case VIDIOC_LOG_STATUS:
1867 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
1872 return -ENOIOCTLCMD;
1878 static int verify_window(const struct bttv_tvnorm *tvn,
1879 struct v4l2_window *win, int fixup)
1881 enum v4l2_field field;
1884 if (win->w.width < 48 || win->w.height < 32)
1886 if (win->clipcount > 2048)
1891 maxh = tvn->sheight;
1893 if (V4L2_FIELD_ANY == field) {
1894 field = (win->w.height > maxh/2)
1895 ? V4L2_FIELD_INTERLACED
1899 case V4L2_FIELD_TOP:
1900 case V4L2_FIELD_BOTTOM:
1903 case V4L2_FIELD_INTERLACED:
1909 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1912 if (win->w.width > maxw)
1913 win->w.width = maxw;
1914 if (win->w.height > maxh)
1915 win->w.height = maxh;
1920 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1921 struct v4l2_window *win, int fixup)
1923 struct v4l2_clip *clips = NULL;
1924 int n,size,retval = 0;
1926 if (NULL == fh->ovfmt)
1928 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1930 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1934 /* copy clips -- luckily v4l1 + v4l2 are binary
1935 compatible here ...*/
1937 size = sizeof(*clips)*(n+4);
1938 clips = kmalloc(size,GFP_KERNEL);
1942 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1947 /* clip against screen */
1948 if (NULL != btv->fbuf.base)
1949 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1951 btcx_sort_clips(clips,n);
1953 /* 4-byte alignments */
1954 switch (fh->ovfmt->depth) {
1957 btcx_align(&win->w, clips, n, 3);
1960 btcx_align(&win->w, clips, n, 1);
1963 /* no alignment fixups needed */
1969 down(&fh->cap.lock);
1970 kfree(fh->ov.clips);
1971 fh->ov.clips = clips;
1975 fh->ov.field = win->field;
1976 fh->ov.setup_ok = 1;
1977 btv->init.ov.w.width = win->w.width;
1978 btv->init.ov.w.height = win->w.height;
1979 btv->init.ov.field = win->field;
1981 /* update overlay if needed */
1983 if (check_btres(fh, RESOURCE_OVERLAY)) {
1984 struct bttv_buffer *new;
1986 new = videobuf_alloc(sizeof(*new));
1987 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1988 retval = bttv_switch_overlay(btv,fh,new);
1994 /* ----------------------------------------------------------------------- */
1996 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
1998 struct videobuf_queue* q = NULL;
2001 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2004 case V4L2_BUF_TYPE_VBI_CAPTURE:
2013 static int bttv_resource(struct bttv_fh *fh)
2018 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2019 res = RESOURCE_VIDEO;
2021 case V4L2_BUF_TYPE_VBI_CAPTURE:
2030 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2032 struct videobuf_queue *q = bttv_queue(fh);
2033 int res = bttv_resource(fh);
2035 if (check_btres(fh,res))
2037 if (videobuf_queue_is_busy(q))
2044 pix_format_set_size (struct v4l2_pix_format * f,
2045 const struct bttv_format * fmt,
2047 unsigned int height)
2052 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2053 f->bytesperline = width; /* Y plane */
2054 f->sizeimage = (width * height * fmt->depth) >> 3;
2056 f->bytesperline = (width * fmt->depth) >> 3;
2057 f->sizeimage = height * f->bytesperline;
2061 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2064 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2065 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2066 pix_format_set_size (&f->fmt.pix, fh->fmt,
2067 fh->width, fh->height);
2068 f->fmt.pix.field = fh->cap.field;
2069 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2071 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2072 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2073 f->fmt.win.w = fh->ov.w;
2074 f->fmt.win.field = fh->ov.field;
2076 case V4L2_BUF_TYPE_VBI_CAPTURE:
2077 bttv_vbi_get_fmt(fh,f);
2084 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2085 struct v4l2_format *f)
2088 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2090 const struct bttv_format *fmt;
2091 enum v4l2_field field;
2092 unsigned int maxw,maxh;
2094 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2099 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2100 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2101 field = f->fmt.pix.field;
2102 if (V4L2_FIELD_ANY == field)
2103 field = (f->fmt.pix.height > maxh/2)
2104 ? V4L2_FIELD_INTERLACED
2105 : V4L2_FIELD_BOTTOM;
2106 if (V4L2_FIELD_SEQ_BT == field)
2107 field = V4L2_FIELD_SEQ_TB;
2109 case V4L2_FIELD_TOP:
2110 case V4L2_FIELD_BOTTOM:
2111 case V4L2_FIELD_ALTERNATE:
2114 case V4L2_FIELD_INTERLACED:
2116 case V4L2_FIELD_SEQ_TB:
2117 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2124 /* update data for the application */
2125 f->fmt.pix.field = field;
2126 if (f->fmt.pix.width < 48)
2127 f->fmt.pix.width = 48;
2128 if (f->fmt.pix.height < 32)
2129 f->fmt.pix.height = 32;
2130 if (f->fmt.pix.width > maxw)
2131 f->fmt.pix.width = maxw;
2132 if (f->fmt.pix.height > maxh)
2133 f->fmt.pix.height = maxh;
2134 pix_format_set_size (&f->fmt.pix, fmt,
2135 f->fmt.pix.width & ~3,
2140 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2141 return verify_window(&bttv_tvnorms[btv->tvnorm],
2143 case V4L2_BUF_TYPE_VBI_CAPTURE:
2144 bttv_vbi_try_fmt(fh,f);
2151 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2152 struct v4l2_format *f)
2157 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2159 const struct bttv_format *fmt;
2161 retval = bttv_switch_type(fh,f->type);
2164 retval = bttv_try_fmt(fh,btv,f);
2167 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2169 /* update our state informations */
2170 down(&fh->cap.lock);
2172 fh->cap.field = f->fmt.pix.field;
2173 fh->cap.last = V4L2_FIELD_NONE;
2174 fh->width = f->fmt.pix.width;
2175 fh->height = f->fmt.pix.height;
2176 btv->init.fmt = fmt;
2177 btv->init.width = f->fmt.pix.width;
2178 btv->init.height = f->fmt.pix.height;
2183 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2184 if (no_overlay > 0) {
2185 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2188 return setup_window(fh, btv, &f->fmt.win, 1);
2189 case V4L2_BUF_TYPE_VBI_CAPTURE:
2190 retval = bttv_switch_type(fh,f->type);
2193 if (locked_btres(fh->btv, RESOURCE_VBI))
2195 bttv_vbi_try_fmt(fh,f);
2196 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2197 bttv_vbi_get_fmt(fh,f);
2204 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2205 unsigned int cmd, void *arg)
2207 struct bttv_fh *fh = file->private_data;
2208 struct bttv *btv = fh->btv;
2209 unsigned long flags;
2213 v4l_print_ioctl(btv->c.name, cmd);
2216 bttv_reinit_bt848(btv);
2224 case VIDIOC_S_INPUT:
2225 case VIDIOC_S_TUNER:
2226 case VIDIOC_S_FREQUENCY:
2227 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2234 /* *** v4l1 *** ************************************************ */
2237 struct video_capability *cap = arg;
2239 memset(cap,0,sizeof(*cap));
2240 strcpy(cap->name,btv->video_dev->name);
2241 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2243 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2246 cap->type = VID_TYPE_CAPTURE|
2250 if (no_overlay <= 0)
2251 cap->type |= VID_TYPE_OVERLAY;
2253 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2254 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2256 cap->minheight = 32;
2258 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2259 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2265 struct video_picture *pic = arg;
2267 memset(pic,0,sizeof(*pic));
2268 pic->brightness = btv->bright;
2269 pic->contrast = btv->contrast;
2270 pic->hue = btv->hue;
2271 pic->colour = btv->saturation;
2273 pic->depth = fh->fmt->depth;
2274 pic->palette = fh->fmt->palette;
2280 struct video_picture *pic = arg;
2281 const struct bttv_format *fmt;
2283 fmt = format_by_palette(pic->palette);
2286 down(&fh->cap.lock);
2287 if (fmt->depth != pic->depth) {
2289 goto fh_unlock_and_return;
2291 if (fmt->flags & FORMAT_FLAGS_RAW) {
2292 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2293 RAW_LINES * 2. F1 is stored at offset 0, F2
2294 at buffer size / 2. */
2295 fh->width = RAW_BPL;
2296 fh->height = gbufsize / RAW_BPL;
2297 btv->init.width = RAW_BPL;
2298 btv->init.height = gbufsize / RAW_BPL;
2302 btv->init.ovfmt = fmt;
2303 btv->init.fmt = fmt;
2305 /* dirty hack time: swap bytes for overlay if the
2306 display adaptor is big endian (insmod option) */
2307 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2308 fmt->palette == VIDEO_PALETTE_RGB565 ||
2309 fmt->palette == VIDEO_PALETTE_RGB32) {
2313 bt848_bright(btv,pic->brightness);
2314 bt848_contrast(btv,pic->contrast);
2315 bt848_hue(btv,pic->hue);
2316 bt848_sat(btv,pic->colour);
2323 struct video_window *win = arg;
2325 memset(win,0,sizeof(*win));
2326 win->x = fh->ov.w.left;
2327 win->y = fh->ov.w.top;
2328 win->width = fh->ov.w.width;
2329 win->height = fh->ov.w.height;
2334 struct video_window *win = arg;
2335 struct v4l2_window w2;
2337 if (no_overlay > 0) {
2338 printk ("VIDIOCSWIN: no_overlay\n");
2342 w2.field = V4L2_FIELD_ANY;
2345 w2.w.width = win->width;
2346 w2.w.height = win->height;
2347 w2.clipcount = win->clipcount;
2348 w2.clips = (struct v4l2_clip __user *)win->clips;
2349 retval = setup_window(fh, btv, &w2, 0);
2351 /* on v4l1 this ioctl affects the read() size too */
2352 fh->width = fh->ov.w.width;
2353 fh->height = fh->ov.w.height;
2354 btv->init.width = fh->ov.w.width;
2355 btv->init.height = fh->ov.w.height;
2362 struct video_buffer *fbuf = arg;
2364 fbuf->base = btv->fbuf.base;
2365 fbuf->width = btv->fbuf.fmt.width;
2366 fbuf->height = btv->fbuf.fmt.height;
2367 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2369 fbuf->depth = fh->ovfmt->depth;
2374 struct video_buffer *fbuf = arg;
2375 const struct bttv_format *fmt;
2378 if(!capable(CAP_SYS_ADMIN) &&
2379 !capable(CAP_SYS_RAWIO))
2381 end = (unsigned long)fbuf->base +
2382 fbuf->height * fbuf->bytesperline;
2383 down(&fh->cap.lock);
2386 switch (fbuf->depth) {
2388 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2391 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2394 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2397 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2401 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2408 goto fh_unlock_and_return;
2412 btv->init.ovfmt = fmt;
2413 btv->init.fmt = fmt;
2414 btv->fbuf.base = fbuf->base;
2415 btv->fbuf.fmt.width = fbuf->width;
2416 btv->fbuf.fmt.height = fbuf->height;
2417 if (fbuf->bytesperline)
2418 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2420 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2426 case VIDIOC_OVERLAY:
2428 struct bttv_buffer *new;
2433 if (NULL == btv->fbuf.base)
2435 if (!fh->ov.setup_ok) {
2436 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2441 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2444 down(&fh->cap.lock);
2446 fh->ov.tvnorm = btv->tvnorm;
2447 new = videobuf_alloc(sizeof(*new));
2448 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2454 retval = bttv_switch_overlay(btv,fh,new);
2461 struct video_mbuf *mbuf = arg;
2464 down(&fh->cap.lock);
2465 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2468 goto fh_unlock_and_return;
2469 memset(mbuf,0,sizeof(*mbuf));
2470 mbuf->frames = gbuffers;
2471 mbuf->size = gbuffers * gbufsize;
2472 for (i = 0; i < gbuffers; i++)
2473 mbuf->offsets[i] = i * gbufsize;
2477 case VIDIOCMCAPTURE:
2479 struct video_mmap *vm = arg;
2480 struct bttv_buffer *buf;
2481 enum v4l2_field field;
2483 if (vm->frame >= VIDEO_MAX_FRAME)
2486 down(&fh->cap.lock);
2488 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2490 goto fh_unlock_and_return;
2491 if (0 == buf->vb.baddr)
2492 goto fh_unlock_and_return;
2493 if (buf->vb.state == STATE_QUEUED ||
2494 buf->vb.state == STATE_ACTIVE)
2495 goto fh_unlock_and_return;
2497 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2498 ? V4L2_FIELD_INTERLACED
2499 : V4L2_FIELD_BOTTOM;
2500 retval = bttv_prepare_buffer(btv,buf,
2501 format_by_palette(vm->format),
2502 vm->width,vm->height,field);
2504 goto fh_unlock_and_return;
2505 spin_lock_irqsave(&btv->s_lock,flags);
2506 buffer_queue(&fh->cap,&buf->vb);
2507 spin_unlock_irqrestore(&btv->s_lock,flags);
2514 struct bttv_buffer *buf;
2516 if (*frame >= VIDEO_MAX_FRAME)
2519 down(&fh->cap.lock);
2521 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2523 goto fh_unlock_and_return;
2524 retval = videobuf_waiton(&buf->vb,0,1);
2526 goto fh_unlock_and_return;
2527 switch (buf->vb.state) {
2532 videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
2533 bttv_dma_free(btv,buf);
2545 struct vbi_format *fmt = (void *) arg;
2546 struct v4l2_format fmt2;
2548 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2549 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2553 bttv_vbi_get_fmt(fh, &fmt2);
2555 memset(fmt,0,sizeof(*fmt));
2556 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2557 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2558 fmt->sample_format = VIDEO_PALETTE_RAW;
2559 fmt->start[0] = fmt2.fmt.vbi.start[0];
2560 fmt->count[0] = fmt2.fmt.vbi.count[0];
2561 fmt->start[1] = fmt2.fmt.vbi.start[1];
2562 fmt->count[1] = fmt2.fmt.vbi.count[1];
2563 if (fmt2.fmt.vbi.flags & V4L2_VBI_UNSYNC)
2564 fmt->flags |= VBI_UNSYNC;
2565 if (fmt2.fmt.vbi.flags & V4L2_VBI_INTERLACED)
2566 fmt->flags |= VBI_INTERLACED;
2571 struct vbi_format *fmt = (void *) arg;
2572 struct v4l2_format fmt2;
2574 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2577 bttv_vbi_get_fmt(fh, &fmt2);
2579 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2580 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2581 fmt->sample_format != VIDEO_PALETTE_RAW ||
2582 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2583 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2584 fmt->count[0] != fmt->count[1] ||
2585 fmt->count[0] < 1 ||
2586 fmt->count[0] > 32 /* VBI_MAXLINES */)
2589 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2602 return bttv_common_ioctls(btv,cmd,arg);
2604 /* *** v4l2 *** ************************************************ */
2605 case VIDIOC_QUERYCAP:
2607 struct v4l2_capability *cap = arg;
2611 memset(cap, 0, sizeof (*cap));
2612 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
2613 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
2614 snprintf(cap->bus_info, sizeof (cap->bus_info),
2615 "PCI:%s", pci_name(btv->c.pci));
2616 cap->version = BTTV_VERSION_CODE;
2618 V4L2_CAP_VIDEO_CAPTURE |
2619 V4L2_CAP_VBI_CAPTURE |
2620 V4L2_CAP_READWRITE |
2622 if (no_overlay <= 0)
2623 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2625 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2626 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2627 cap->capabilities |= V4L2_CAP_TUNER;
2631 case VIDIOC_ENUM_FMT:
2633 struct v4l2_fmtdesc *f = arg;
2634 enum v4l2_buf_type type;
2639 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2644 memset(f,0,sizeof(*f));
2647 f->pixelformat = V4L2_PIX_FMT_GREY;
2648 strcpy(f->description,"vbi data");
2652 /* video capture + overlay */
2654 for (i = 0; i < BTTV_FORMATS; i++) {
2655 if (bttv_formats[i].fourcc != -1)
2657 if ((unsigned int)index == f->index)
2660 if (BTTV_FORMATS == i)
2664 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2666 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2667 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2673 memset(f,0,sizeof(*f));
2676 f->pixelformat = bttv_formats[i].fourcc;
2677 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2681 case VIDIOC_TRY_FMT:
2683 struct v4l2_format *f = arg;
2684 return bttv_try_fmt(fh,btv,f);
2688 struct v4l2_format *f = arg;
2689 return bttv_g_fmt(fh,f);
2693 struct v4l2_format *f = arg;
2694 return bttv_s_fmt(fh,btv,f);
2699 struct v4l2_framebuffer *fb = arg;
2702 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2704 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2709 struct v4l2_framebuffer *fb = arg;
2710 const struct bttv_format *fmt;
2712 if(!capable(CAP_SYS_ADMIN) &&
2713 !capable(CAP_SYS_RAWIO))
2717 fmt = format_by_fourcc(fb->fmt.pixelformat);
2720 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2723 down(&fh->cap.lock);
2725 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2726 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2727 goto fh_unlock_and_return;
2728 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2729 goto fh_unlock_and_return;
2733 btv->fbuf.base = fb->base;
2734 btv->fbuf.fmt.width = fb->fmt.width;
2735 btv->fbuf.fmt.height = fb->fmt.height;
2736 if (0 != fb->fmt.bytesperline)
2737 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2739 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2743 btv->init.ovfmt = fmt;
2744 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2747 fh->ov.w.width = fb->fmt.width;
2748 fh->ov.w.height = fb->fmt.height;
2749 btv->init.ov.w.width = fb->fmt.width;
2750 btv->init.ov.w.height = fb->fmt.height;
2751 kfree(fh->ov.clips);
2752 fh->ov.clips = NULL;
2755 if (check_btres(fh, RESOURCE_OVERLAY)) {
2756 struct bttv_buffer *new;
2758 new = videobuf_alloc(sizeof(*new));
2759 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2760 retval = bttv_switch_overlay(btv,fh,new);
2767 case VIDIOC_REQBUFS:
2768 return videobuf_reqbufs(bttv_queue(fh),arg);
2770 case VIDIOC_QUERYBUF:
2771 return videobuf_querybuf(bttv_queue(fh),arg);
2774 return videobuf_qbuf(bttv_queue(fh),arg);
2777 return videobuf_dqbuf(bttv_queue(fh),arg,
2778 file->f_flags & O_NONBLOCK);
2780 case VIDIOC_STREAMON:
2782 int res = bttv_resource(fh);
2784 if (!check_alloc_btres(btv,fh,res))
2786 return videobuf_streamon(bttv_queue(fh));
2788 case VIDIOC_STREAMOFF:
2790 int res = bttv_resource(fh);
2792 retval = videobuf_streamoff(bttv_queue(fh));
2795 free_btres(btv,fh,res);
2799 case VIDIOC_QUERYCTRL:
2801 struct v4l2_queryctrl *c = arg;
2804 if ((c->id < V4L2_CID_BASE ||
2805 c->id >= V4L2_CID_LASTP1) &&
2806 (c->id < V4L2_CID_PRIVATE_BASE ||
2807 c->id >= V4L2_CID_PRIVATE_LASTP1))
2809 for (i = 0; i < BTTV_CTLS; i++)
2810 if (bttv_ctls[i].id == c->id)
2812 if (i == BTTV_CTLS) {
2817 if (i >= 4 && i <= 8) {
2818 struct video_audio va;
2819 memset(&va,0,sizeof(va));
2820 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2821 if (btv->audio_hook)
2822 btv->audio_hook(btv,&va,0);
2823 switch (bttv_ctls[i].id) {
2824 case V4L2_CID_AUDIO_VOLUME:
2825 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2828 case V4L2_CID_AUDIO_BALANCE:
2829 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2832 case V4L2_CID_AUDIO_BASS:
2833 if (!(va.flags & VIDEO_AUDIO_BASS))
2836 case V4L2_CID_AUDIO_TREBLE:
2837 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2845 return get_control(btv,arg);
2847 return set_control(btv,arg);
2850 struct v4l2_streamparm *parm = arg;
2851 struct v4l2_standard s;
2852 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2854 memset(parm,0,sizeof(*parm));
2855 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2856 bttv_tvnorms[btv->tvnorm].name);
2857 parm->parm.capture.timeperframe = s.frameperiod;
2861 case VIDIOC_G_PRIORITY:
2863 enum v4l2_priority *p = arg;
2865 *p = v4l2_prio_max(&btv->prio);
2868 case VIDIOC_S_PRIORITY:
2870 enum v4l2_priority *prio = arg;
2872 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2875 case VIDIOC_ENUMSTD:
2878 case VIDIOC_ENUMINPUT:
2879 case VIDIOC_G_INPUT:
2880 case VIDIOC_S_INPUT:
2881 case VIDIOC_G_TUNER:
2882 case VIDIOC_S_TUNER:
2883 case VIDIOC_G_FREQUENCY:
2884 case VIDIOC_S_FREQUENCY:
2885 case VIDIOC_LOG_STATUS:
2886 return bttv_common_ioctls(btv,cmd,arg);
2889 return -ENOIOCTLCMD;
2893 fh_unlock_and_return:
2898 static int bttv_ioctl(struct inode *inode, struct file *file,
2899 unsigned int cmd, unsigned long arg)
2901 struct bttv_fh *fh = file->private_data;
2905 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2906 return fh->lines * 2 * 2048;
2908 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2912 static ssize_t bttv_read(struct file *file, char __user *data,
2913 size_t count, loff_t *ppos)
2915 struct bttv_fh *fh = file->private_data;
2918 if (fh->btv->errors)
2919 bttv_reinit_bt848(fh->btv);
2920 dprintk("bttv%d: read count=%d type=%s\n",
2921 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2924 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2925 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2927 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2928 file->f_flags & O_NONBLOCK);
2930 case V4L2_BUF_TYPE_VBI_CAPTURE:
2931 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2933 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2934 file->f_flags & O_NONBLOCK);
2942 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2944 struct bttv_fh *fh = file->private_data;
2945 struct bttv_buffer *buf;
2946 enum v4l2_field field;
2948 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2949 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2951 return videobuf_poll_stream(file, &fh->vbi, wait);
2954 if (check_btres(fh,RESOURCE_VIDEO)) {
2955 /* streaming capture */
2956 if (list_empty(&fh->cap.stream))
2958 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2960 /* read() capture */
2961 down(&fh->cap.lock);
2962 if (NULL == fh->cap.read_buf) {
2963 /* need to capture a new frame */
2964 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2968 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2969 if (NULL == fh->cap.read_buf) {
2973 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2974 field = videobuf_next_field(&fh->cap);
2975 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2976 kfree (fh->cap.read_buf);
2977 fh->cap.read_buf = NULL;
2981 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
2982 fh->cap.read_off = 0;
2985 buf = (struct bttv_buffer*)fh->cap.read_buf;
2988 poll_wait(file, &buf->vb.done, wait);
2989 if (buf->vb.state == STATE_DONE ||
2990 buf->vb.state == STATE_ERROR)
2991 return POLLIN|POLLRDNORM;
2995 static int bttv_open(struct inode *inode, struct file *file)
2997 int minor = iminor(inode);
2998 struct bttv *btv = NULL;
3000 enum v4l2_buf_type type = 0;
3003 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3005 for (i = 0; i < bttv_num; i++) {
3006 if (bttvs[i].video_dev &&
3007 bttvs[i].video_dev->minor == minor) {
3009 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3012 if (bttvs[i].vbi_dev &&
3013 bttvs[i].vbi_dev->minor == minor) {
3015 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3022 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3023 btv->c.nr,v4l2_type_names[type]);
3025 /* allocate per filehandle data */
3026 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3029 file->private_data = fh;
3032 fh->ov.setup_ok = 0;
3033 v4l2_prio_open(&btv->prio,&fh->prio);
3035 videobuf_queue_init(&fh->cap, &bttv_video_qops,
3036 btv->c.pci, &btv->s_lock,
3037 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3038 V4L2_FIELD_INTERLACED,
3039 sizeof(struct bttv_buffer),
3041 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3042 btv->c.pci, &btv->s_lock,
3043 V4L2_BUF_TYPE_VBI_CAPTURE,
3045 sizeof(struct bttv_buffer),
3047 i2c_vidiocschan(btv);
3050 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
3051 bttv_vbi_setlines(fh,btv,16);
3052 bttv_field_count(btv);
3056 static int bttv_release(struct inode *inode, struct file *file)
3058 struct bttv_fh *fh = file->private_data;
3059 struct bttv *btv = fh->btv;
3061 /* turn off overlay */
3062 if (check_btres(fh, RESOURCE_OVERLAY))
3063 bttv_switch_overlay(btv,fh,NULL);
3065 /* stop video capture */
3066 if (check_btres(fh, RESOURCE_VIDEO)) {
3067 videobuf_streamoff(&fh->cap);
3068 free_btres(btv,fh,RESOURCE_VIDEO);
3070 if (fh->cap.read_buf) {
3071 buffer_release(&fh->cap,fh->cap.read_buf);
3072 kfree(fh->cap.read_buf);
3075 /* stop vbi capture */
3076 if (check_btres(fh, RESOURCE_VBI)) {
3077 if (fh->vbi.streaming)
3078 videobuf_streamoff(&fh->vbi);
3079 if (fh->vbi.reading)
3080 videobuf_read_stop(&fh->vbi);
3081 free_btres(btv,fh,RESOURCE_VBI);
3085 videobuf_mmap_free(&fh->cap);
3086 videobuf_mmap_free(&fh->vbi);
3087 v4l2_prio_close(&btv->prio,&fh->prio);
3088 file->private_data = NULL;
3092 bttv_field_count(btv);
3097 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3099 struct bttv_fh *fh = file->private_data;
3101 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3102 fh->btv->c.nr, v4l2_type_names[fh->type],
3103 vma->vm_start, vma->vm_end - vma->vm_start);
3104 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3107 static struct file_operations bttv_fops =
3109 .owner = THIS_MODULE,
3111 .release = bttv_release,
3112 .ioctl = bttv_ioctl,
3113 .compat_ioctl = v4l_compat_ioctl32,
3114 .llseek = no_llseek,
3120 static struct video_device bttv_video_template =
3123 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3124 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3125 .hardware = VID_HARDWARE_BT848,
3130 static struct video_device bttv_vbi_template =
3132 .name = "bt848/878 vbi",
3133 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3134 .hardware = VID_HARDWARE_BT848,
3139 /* ----------------------------------------------------------------------- */
3140 /* radio interface */
3142 static int radio_open(struct inode *inode, struct file *file)
3144 int minor = iminor(inode);
3145 struct bttv *btv = NULL;
3148 dprintk("bttv: open minor=%d\n",minor);
3150 for (i = 0; i < bttv_num; i++) {
3151 if (bttvs[i].radio_dev->minor == minor) {
3159 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3164 file->private_data = btv;
3166 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
3167 audio_mux(btv,AUDIO_RADIO);
3173 static int radio_release(struct inode *inode, struct file *file)
3175 struct bttv *btv = file->private_data;
3176 struct rds_command cmd;
3180 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3185 static int radio_do_ioctl(struct inode *inode, struct file *file,
3186 unsigned int cmd, void *arg)
3188 struct bttv *btv = file->private_data;
3193 struct video_capability *cap = arg;
3195 memset(cap,0,sizeof(*cap));
3196 strcpy(cap->name,btv->radio_dev->name);
3197 cap->type = VID_TYPE_TUNER;
3205 struct video_tuner *v = arg;
3209 memset(v,0,sizeof(*v));
3210 strcpy(v->name, "Radio");
3211 bttv_call_i2c_clients(btv,cmd,v);
3223 return bttv_common_ioctls(btv,cmd,arg);
3226 return -ENOIOCTLCMD;
3231 static int radio_ioctl(struct inode *inode, struct file *file,
3232 unsigned int cmd, unsigned long arg)
3234 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3237 static ssize_t radio_read(struct file *file, char __user *data,
3238 size_t count, loff_t *ppos)
3240 struct bttv *btv = file->private_data;
3241 struct rds_command cmd;
3242 cmd.block_count = count/3;
3244 cmd.instance = file;
3245 cmd.result = -ENODEV;
3247 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3252 static unsigned int radio_poll(struct file *file, poll_table *wait)
3254 struct bttv *btv = file->private_data;
3255 struct rds_command cmd;
3256 cmd.instance = file;
3257 cmd.event_list = wait;
3258 cmd.result = -ENODEV;
3259 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3264 static struct file_operations radio_fops =
3266 .owner = THIS_MODULE,
3269 .release = radio_release,
3270 .ioctl = radio_ioctl,
3271 .llseek = no_llseek,
3275 static struct video_device radio_template =
3277 .name = "bt848/878 radio",
3278 .type = VID_TYPE_TUNER,
3279 .hardware = VID_HARDWARE_BT848,
3280 .fops = &radio_fops,
3284 /* ----------------------------------------------------------------------- */
3285 /* some debug code */
3287 static int bttv_risc_decode(u32 risc)
3289 static char *instr[16] = {
3290 [ BT848_RISC_WRITE >> 28 ] = "write",
3291 [ BT848_RISC_SKIP >> 28 ] = "skip",
3292 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3293 [ BT848_RISC_JUMP >> 28 ] = "jump",
3294 [ BT848_RISC_SYNC >> 28 ] = "sync",
3295 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3296 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3297 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3299 static int incr[16] = {
3300 [ BT848_RISC_WRITE >> 28 ] = 2,
3301 [ BT848_RISC_JUMP >> 28 ] = 2,
3302 [ BT848_RISC_SYNC >> 28 ] = 2,
3303 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3304 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3305 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3307 static char *bits[] = {
3308 "be0", "be1", "be2", "be3/resync",
3309 "set0", "set1", "set2", "set3",
3310 "clr0", "clr1", "clr2", "clr3",
3311 "irq", "res", "eol", "sol",
3315 printk("0x%08x [ %s", risc,
3316 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3317 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3318 if (risc & (1 << (i + 12)))
3319 printk(" %s",bits[i]);
3320 printk(" count=%d ]\n", risc & 0xfff);
3321 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3324 static void bttv_risc_disasm(struct bttv *btv,
3325 struct btcx_riscmem *risc)
3329 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3330 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3331 for (i = 0; i < (risc->size >> 2); i += n) {
3332 printk("%s: 0x%lx: ", btv->c.name,
3333 (unsigned long)(risc->dma + (i<<2)));
3334 n = bttv_risc_decode(risc->cpu[i]);
3335 for (j = 1; j < n; j++)
3336 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3337 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3339 if (0 == risc->cpu[i])
3344 static void bttv_print_riscaddr(struct bttv *btv)
3346 printk(" main: %08Lx\n",
3347 (unsigned long long)btv->main.dma);
3348 printk(" vbi : o=%08Lx e=%08Lx\n",
3349 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3350 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3351 printk(" cap : o=%08Lx e=%08Lx\n",
3352 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3353 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3354 printk(" scr : o=%08Lx e=%08Lx\n",
3355 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3356 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3357 bttv_risc_disasm(btv, &btv->main);
3360 /* ----------------------------------------------------------------------- */
3363 static char *irq_name[] = {
3364 "FMTCHG", // format change detected (525 vs. 625)
3365 "VSYNC", // vertical sync (new field)
3366 "HSYNC", // horizontal sync
3367 "OFLOW", // chroma/luma AGC overflow
3368 "HLOCK", // horizontal lock changed
3369 "VPRES", // video presence changed
3371 "I2CDONE", // hw irc operation finished
3372 "GPINT", // gpio port triggered irq
3374 "RISCI", // risc instruction triggered irq
3375 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3376 "FTRGT", // pixel data fifo overrun
3377 "FDSR", // fifo data stream resyncronisation
3378 "PPERR", // parity error (data transfer)
3379 "RIPERR", // parity error (read risc instructions)
3380 "PABORT", // pci abort
3381 "OCERR", // risc instruction error
3382 "SCERR", // syncronisation error
3385 static void bttv_print_irqbits(u32 print, u32 mark)
3390 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3391 if (print & (1 << i))
3392 printk(" %s",irq_name[i]);
3393 if (mark & (1 << i))
3398 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3400 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3402 (unsigned long)btv->main.dma,
3403 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3404 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3407 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3408 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3409 "Ok, then this is harmless, don't worry ;)\n",
3413 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3415 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3421 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3423 struct bttv_buffer *item;
3425 memset(set,0,sizeof(*set));
3427 /* capture request ? */
3428 if (!list_empty(&btv->capture)) {
3430 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3431 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3433 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3436 /* capture request for other field ? */
3437 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3438 (item->vb.queue.next != &btv->capture)) {
3439 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3440 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3441 if (NULL == set->top &&
3442 V4L2_FIELD_TOP == item->vb.field) {
3445 if (NULL == set->bottom &&
3446 V4L2_FIELD_BOTTOM == item->vb.field) {
3449 if (NULL != set->top && NULL != set->bottom)
3455 /* screen overlay ? */
3456 if (NULL != btv->screen) {
3457 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3458 if (NULL == set->top && NULL == set->bottom) {
3459 set->top = btv->screen;
3460 set->bottom = btv->screen;
3463 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3465 set->top = btv->screen;
3467 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3468 NULL == set->bottom) {
3469 set->bottom = btv->screen;
3474 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3475 btv->c.nr,set->top, set->bottom,
3476 btv->screen,set->frame_irq,set->top_irq);
3481 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3482 struct bttv_buffer_set *curr, unsigned int state)
3486 do_gettimeofday(&ts);
3488 if (wakeup->top == wakeup->bottom) {
3489 if (NULL != wakeup->top && curr->top != wakeup->top) {
3491 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3492 wakeup->top->vb.ts = ts;
3493 wakeup->top->vb.field_count = btv->field_count;
3494 wakeup->top->vb.state = state;
3495 wake_up(&wakeup->top->vb.done);
3498 if (NULL != wakeup->top && curr->top != wakeup->top) {
3500 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3501 wakeup->top->vb.ts = ts;
3502 wakeup->top->vb.field_count = btv->field_count;
3503 wakeup->top->vb.state = state;
3504 wake_up(&wakeup->top->vb.done);
3506 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3508 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3509 wakeup->bottom->vb.ts = ts;
3510 wakeup->bottom->vb.field_count = btv->field_count;
3511 wakeup->bottom->vb.state = state;
3512 wake_up(&wakeup->bottom->vb.done);
3518 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3526 do_gettimeofday(&ts);
3528 wakeup->vb.field_count = btv->field_count;
3529 wakeup->vb.state = state;
3530 wake_up(&wakeup->vb.done);
3533 static void bttv_irq_timeout(unsigned long data)
3535 struct bttv *btv = (struct bttv *)data;
3536 struct bttv_buffer_set old,new;
3537 struct bttv_buffer *ovbi;
3538 struct bttv_buffer *item;
3539 unsigned long flags;
3542 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3543 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3544 btread(BT848_RISC_COUNT));
3545 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3549 spin_lock_irqsave(&btv->s_lock,flags);
3551 /* deactivate stuff */
3552 memset(&new,0,sizeof(new));
3558 bttv_buffer_activate_video(btv, &new);
3559 bttv_buffer_activate_vbi(btv, NULL);
3560 bttv_set_dma(btv, 0);
3563 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3564 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3566 /* cancel all outstanding capture / vbi requests */
3567 while (!list_empty(&btv->capture)) {
3568 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3569 list_del(&item->vb.queue);
3570 item->vb.state = STATE_ERROR;
3571 wake_up(&item->vb.done);
3573 while (!list_empty(&btv->vcapture)) {
3574 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3575 list_del(&item->vb.queue);
3576 item->vb.state = STATE_ERROR;
3577 wake_up(&item->vb.done);
3581 spin_unlock_irqrestore(&btv->s_lock,flags);
3585 bttv_irq_wakeup_top(struct bttv *btv)
3587 struct bttv_buffer *wakeup = btv->curr.top;
3592 spin_lock(&btv->s_lock);
3593 btv->curr.top_irq = 0;
3594 btv->curr.top = NULL;
3595 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3597 do_gettimeofday(&wakeup->vb.ts);
3598 wakeup->vb.field_count = btv->field_count;
3599 wakeup->vb.state = STATE_DONE;
3600 wake_up(&wakeup->vb.done);
3601 spin_unlock(&btv->s_lock);
3604 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3608 if (rc > risc->dma + risc->size)
3614 bttv_irq_switch_video(struct bttv *btv)
3616 struct bttv_buffer_set new;
3617 struct bttv_buffer_set old;
3620 spin_lock(&btv->s_lock);
3622 /* new buffer set */
3623 bttv_irq_next_video(btv, &new);
3624 rc = btread(BT848_RISC_COUNT);
3625 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3626 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3629 bttv_irq_debug_low_latency(btv, rc);
3630 spin_unlock(&btv->s_lock);
3637 btv->loop_irq &= ~1;
3638 bttv_buffer_activate_video(btv, &new);
3639 bttv_set_dma(btv, 0);
3642 if (UNSET != btv->new_input) {
3643 video_mux(btv,btv->new_input);
3644 btv->new_input = UNSET;
3647 /* wake up finished buffers */
3648 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3649 spin_unlock(&btv->s_lock);
3653 bttv_irq_switch_vbi(struct bttv *btv)
3655 struct bttv_buffer *new = NULL;
3656 struct bttv_buffer *old;
3659 spin_lock(&btv->s_lock);
3661 if (!list_empty(&btv->vcapture))
3662 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3665 rc = btread(BT848_RISC_COUNT);
3666 if (NULL != old && (is_active(&old->top, rc) ||
3667 is_active(&old->bottom, rc))) {
3670 bttv_irq_debug_low_latency(btv, rc);
3671 spin_unlock(&btv->s_lock);
3677 btv->loop_irq &= ~4;
3678 bttv_buffer_activate_vbi(btv, new);
3679 bttv_set_dma(btv, 0);
3681 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3682 spin_unlock(&btv->s_lock);
3685 static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3693 btv=(struct bttv *)dev_id;
3695 if (btv->custom_irq)
3696 handled = btv->custom_irq(btv);
3700 /* get/clear interrupt status bits */
3701 stat=btread(BT848_INT_STAT);
3702 astat=stat&btread(BT848_INT_MASK);
3706 btwrite(stat,BT848_INT_STAT);
3708 /* get device status bits */
3709 dstat=btread(BT848_DSTATUS);
3712 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3713 "riscs=%x, riscc=%08x, ",
3714 btv->c.nr, count, btv->field_count,
3715 stat>>28, btread(BT848_RISC_COUNT));
3716 bttv_print_irqbits(stat,astat);
3717 if (stat & BT848_INT_HLOCK)
3718 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3720 if (stat & BT848_INT_VPRES)
3721 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3723 if (stat & BT848_INT_FMTCHG)
3724 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3729 if (astat&BT848_INT_VSYNC)
3732 if ((astat & BT848_INT_GPINT) && btv->remote) {
3733 wake_up(&btv->gpioq);
3734 bttv_input_irq(btv);
3737 if (astat & BT848_INT_I2CDONE) {
3738 btv->i2c_done = stat;
3739 wake_up(&btv->i2c_queue);
3742 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3743 bttv_irq_switch_vbi(btv);
3745 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3746 bttv_irq_wakeup_top(btv);
3748 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3749 bttv_irq_switch_video(btv);
3751 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3754 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3755 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3756 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3757 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3758 btread(BT848_RISC_COUNT));
3759 bttv_print_irqbits(stat,astat);
3762 bttv_print_riscaddr(btv);
3764 if (fdsr && astat & BT848_INT_FDSR) {
3765 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3766 btv->c.nr,btread(BT848_RISC_COUNT));
3768 bttv_print_riscaddr(btv);
3774 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3775 btwrite(0, BT848_INT_MASK);
3778 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3781 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
3783 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3787 bttv_print_irqbits(stat,astat);
3795 return IRQ_RETVAL(handled);
3799 /* ----------------------------------------------------------------------- */
3800 /* initialitation */
3802 static struct video_device *vdev_init(struct bttv *btv,
3803 struct video_device *template,
3806 struct video_device *vfd;
3808 vfd = video_device_alloc();
3813 vfd->dev = &btv->c.pci->dev;
3814 vfd->release = video_device_release;
3815 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3816 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3817 type, bttv_tvcards[btv->c.type].name);
3821 static void bttv_unregister_video(struct bttv *btv)
3823 if (btv->video_dev) {
3824 if (-1 != btv->video_dev->minor)
3825 video_unregister_device(btv->video_dev);
3827 video_device_release(btv->video_dev);
3828 btv->video_dev = NULL;
3831 if (-1 != btv->vbi_dev->minor)
3832 video_unregister_device(btv->vbi_dev);
3834 video_device_release(btv->vbi_dev);
3835 btv->vbi_dev = NULL;
3837 if (btv->radio_dev) {
3838 if (-1 != btv->radio_dev->minor)
3839 video_unregister_device(btv->radio_dev);
3841 video_device_release(btv->radio_dev);
3842 btv->radio_dev = NULL;
3846 /* register video4linux devices */
3847 static int __devinit bttv_register_video(struct bttv *btv)
3849 if (no_overlay <= 0) {
3850 bttv_video_template.type |= VID_TYPE_OVERLAY;
3852 printk("bttv: Overlay support disabled.\n");
3856 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3857 if (NULL == btv->video_dev)
3859 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3861 printk(KERN_INFO "bttv%d: registered device video%d\n",
3862 btv->c.nr,btv->video_dev->minor & 0x1f);
3863 video_device_create_file(btv->video_dev, &class_device_attr_card);
3866 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3867 if (NULL == btv->vbi_dev)
3869 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3871 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3872 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3874 if (!btv->has_radio)
3877 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3878 if (NULL == btv->radio_dev)
3880 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3882 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3883 btv->c.nr,btv->radio_dev->minor & 0x1f);
3889 bttv_unregister_video(btv);
3894 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3895 /* response on cards with no firmware is not enabled by OF */
3896 static void pci_set_command(struct pci_dev *dev)
3898 #if defined(__powerpc__)
3901 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3902 cmd = (cmd | PCI_COMMAND_MEMORY );
3903 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3907 static int __devinit bttv_probe(struct pci_dev *dev,
3908 const struct pci_device_id *pci_id)
3914 if (bttv_num == BTTV_MAX)
3916 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3917 btv=&bttvs[bttv_num];
3918 memset(btv,0,sizeof(*btv));
3919 btv->c.nr = bttv_num;
3920 sprintf(btv->c.name,"bttv%d",btv->c.nr);
3922 /* initialize structs / fill in defaults */
3923 init_MUTEX(&btv->lock);
3924 init_MUTEX(&btv->reslock);
3925 spin_lock_init(&btv->s_lock);
3926 spin_lock_init(&btv->gpio_lock);
3927 init_waitqueue_head(&btv->gpioq);
3928 init_waitqueue_head(&btv->i2c_queue);
3929 INIT_LIST_HEAD(&btv->c.subs);
3930 INIT_LIST_HEAD(&btv->capture);
3931 INIT_LIST_HEAD(&btv->vcapture);
3932 v4l2_prio_init(&btv->prio);
3934 init_timer(&btv->timeout);
3935 btv->timeout.function = bttv_irq_timeout;
3936 btv->timeout.data = (unsigned long)btv;
3939 btv->tuner_type = UNSET;
3940 btv->new_input = UNSET;
3941 btv->has_radio=radio[btv->c.nr];
3943 /* pci stuff (init, get irq/mmio, ... */
3945 btv->id = dev->device;
3946 if (pci_enable_device(dev)) {
3947 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3951 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
3952 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3956 if (!request_mem_region(pci_resource_start(dev,0),
3957 pci_resource_len(dev,0),
3959 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3960 btv->c.nr, pci_resource_start(dev,0));
3963 pci_set_master(dev);
3964 pci_set_command(dev);
3965 pci_set_drvdata(dev,btv);
3967 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3968 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3969 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3970 bttv_num,btv->id, btv->revision, pci_name(dev));
3971 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3972 btv->c.pci->irq, lat, pci_resource_start(dev,0));
3975 btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3976 if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3977 printk("bttv%d: ioremap() failed\n", btv->c.nr);
3985 /* disable irqs, register irq handler */
3986 btwrite(0, BT848_INT_MASK);
3987 result = request_irq(btv->c.pci->irq, bttv_irq,
3988 SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
3990 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
3991 bttv_num,btv->c.pci->irq);
3995 if (0 != bttv_handle_chipset(btv)) {
4000 /* init options from insmod args */
4001 btv->opt_combfilter = combfilter;
4002 btv->opt_lumafilter = lumafilter;
4003 btv->opt_automute = automute;
4004 btv->opt_chroma_agc = chroma_agc;
4005 btv->opt_adc_crush = adc_crush;
4006 btv->opt_vcr_hack = vcr_hack;
4007 btv->opt_whitecrush_upper = whitecrush_upper;
4008 btv->opt_whitecrush_lower = whitecrush_lower;
4009 btv->opt_uv_ratio = uv_ratio;
4010 btv->opt_full_luma_range = full_luma_range;
4011 btv->opt_coring = coring;
4013 /* fill struct bttv with some useful defaults */
4014 btv->init.btv = btv;
4015 btv->init.ov.w.width = 320;
4016 btv->init.ov.w.height = 240;
4017 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4018 btv->init.width = 320;
4019 btv->init.height = 240;
4020 btv->init.lines = 16;
4023 /* initialize hardware */
4025 bttv_gpio_tracking(btv,"pre-init");
4027 bttv_risc_init_main(btv);
4031 btwrite(0x00, BT848_GPIO_REG_INP);
4032 btwrite(0x00, BT848_GPIO_OUT_EN);
4034 bttv_gpio_tracking(btv,"init");
4036 /* needs to be done before i2c is registered */
4037 bttv_init_card1(btv);
4039 /* register i2c + gpio */
4042 /* some card-specific stuff (needs working i2c) */
4043 bttv_init_card2(btv);
4046 /* register video4linux + input */
4047 if (!bttv_tvcards[btv->c.type].no_video) {
4048 bttv_register_video(btv);
4049 bt848_bright(btv,32768);
4050 bt848_contrast(btv,32768);
4051 bt848_hue(btv,32768);
4052 bt848_sat(btv,32768);
4053 audio_mux(btv,AUDIO_MUTE);
4057 /* add subdevices */
4058 if (bttv_tvcards[btv->c.type].has_dvb)
4059 bttv_sub_add_device(&btv->c, "dvb");
4061 bttv_input_init(btv);
4063 /* everything is fine */
4068 free_irq(btv->c.pci->irq,btv);
4071 if (btv->bt848_mmio)
4072 iounmap(btv->bt848_mmio);
4073 release_mem_region(pci_resource_start(btv->c.pci,0),
4074 pci_resource_len(btv->c.pci,0));
4075 pci_set_drvdata(dev,NULL);
4079 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4081 struct bttv *btv = pci_get_drvdata(pci_dev);
4084 printk("bttv%d: unloading\n",btv->c.nr);
4086 /* shutdown everything (DMA+IRQs) */
4087 btand(~15, BT848_GPIO_DMA_CTL);
4088 btwrite(0, BT848_INT_MASK);
4089 btwrite(~0x0, BT848_INT_STAT);
4090 btwrite(0x0, BT848_GPIO_OUT_EN);
4092 bttv_gpio_tracking(btv,"cleanup");
4094 /* tell gpio modules we are leaving ... */
4096 wake_up(&btv->gpioq);
4097 bttv_input_fini(btv);
4098 bttv_sub_del_devices(&btv->c);
4100 /* unregister i2c_bus + input */
4103 /* unregister video4linux */
4104 bttv_unregister_video(btv);
4106 /* free allocated memory */
4107 btcx_riscmem_free(btv->c.pci,&btv->main);
4109 /* free ressources */
4110 free_irq(btv->c.pci->irq,btv);
4111 iounmap(btv->bt848_mmio);
4112 release_mem_region(pci_resource_start(btv->c.pci,0),
4113 pci_resource_len(btv->c.pci,0));
4115 pci_set_drvdata(pci_dev, NULL);
4119 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4121 struct bttv *btv = pci_get_drvdata(pci_dev);
4122 struct bttv_buffer_set idle;
4123 unsigned long flags;
4125 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4127 /* stop dma + irqs */
4128 spin_lock_irqsave(&btv->s_lock,flags);
4129 memset(&idle, 0, sizeof(idle));
4130 btv->state.video = btv->curr;
4131 btv->state.vbi = btv->cvbi;
4132 btv->state.loop_irq = btv->loop_irq;
4135 bttv_buffer_activate_video(btv, &idle);
4136 bttv_buffer_activate_vbi(btv, NULL);
4137 bttv_set_dma(btv, 0);
4138 btwrite(0, BT848_INT_MASK);
4139 spin_unlock_irqrestore(&btv->s_lock,flags);
4141 /* save bt878 state */
4142 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4143 btv->state.gpio_data = gpio_read();
4145 /* save pci state */
4146 pci_save_state(pci_dev);
4147 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4148 pci_disable_device(pci_dev);
4149 btv->state.disabled = 1;
4154 static int bttv_resume(struct pci_dev *pci_dev)
4156 struct bttv *btv = pci_get_drvdata(pci_dev);
4157 unsigned long flags;
4160 dprintk("bttv%d: resume\n", btv->c.nr);
4162 /* restore pci state */
4163 if (btv->state.disabled) {
4164 err=pci_enable_device(pci_dev);
4166 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4170 btv->state.disabled = 0;
4172 err=pci_set_power_state(pci_dev, PCI_D0);
4174 pci_disable_device(pci_dev);
4175 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4177 btv->state.disabled = 1;
4181 pci_restore_state(pci_dev);
4183 /* restore bt878 state */
4184 bttv_reinit_bt848(btv);
4185 gpio_inout(0xffffff, btv->state.gpio_enable);
4186 gpio_write(btv->state.gpio_data);
4189 spin_lock_irqsave(&btv->s_lock,flags);
4190 btv->curr = btv->state.video;
4191 btv->cvbi = btv->state.vbi;
4192 btv->loop_irq = btv->state.loop_irq;
4193 bttv_buffer_activate_video(btv, &btv->curr);
4194 bttv_buffer_activate_vbi(btv, btv->cvbi);
4195 bttv_set_dma(btv, 0);
4196 spin_unlock_irqrestore(&btv->s_lock,flags);
4200 static struct pci_device_id bttv_pci_tbl[] = {
4201 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4202 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4203 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4204 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4205 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4206 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4207 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4208 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4212 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4214 static struct pci_driver bttv_pci_driver = {
4216 .id_table = bttv_pci_tbl,
4217 .probe = bttv_probe,
4218 .remove = __devexit_p(bttv_remove),
4219 .suspend = bttv_suspend,
4220 .resume = bttv_resume,
4223 static int bttv_init_module(void)
4227 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4228 (BTTV_VERSION_CODE >> 16) & 0xff,
4229 (BTTV_VERSION_CODE >> 8) & 0xff,
4230 BTTV_VERSION_CODE & 0xff);
4232 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4233 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4235 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4237 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4238 gbufsize = BTTV_MAX_FBUF;
4239 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4241 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4242 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4244 bttv_check_chipset();
4246 bus_register(&bttv_sub_bus_type);
4247 return pci_register_driver(&bttv_pci_driver);
4250 static void bttv_cleanup_module(void)
4252 pci_unregister_driver(&bttv_pci_driver);
4253 bus_unregister(&bttv_sub_bus_type);
4257 module_init(bttv_init_module);
4258 module_exit(bttv_cleanup_module);