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>
39 #include <linux/dma-mapping.h>
42 #include <asm/byteorder.h>
47 unsigned int bttv_num; /* number of Bt848s in use */
48 struct bttv bttvs[BTTV_MAX];
50 unsigned int bttv_debug = 0;
51 unsigned int bttv_verbose = 1;
52 unsigned int bttv_gpio = 0;
54 /* config variables */
56 static unsigned int bigendian=1;
58 static unsigned int bigendian=0;
60 static unsigned int radio[BTTV_MAX];
61 static unsigned int irq_debug = 0;
62 static unsigned int gbuffers = 8;
63 static unsigned int gbufsize = 0x208000;
65 static int video_nr = -1;
66 static int radio_nr = -1;
67 static int vbi_nr = -1;
68 static int debug_latency = 0;
70 static unsigned int fdsr = 0;
73 static unsigned int combfilter = 0;
74 static unsigned int lumafilter = 0;
75 static unsigned int automute = 1;
76 static unsigned int chroma_agc = 0;
77 static unsigned int adc_crush = 1;
78 static unsigned int whitecrush_upper = 0xCF;
79 static unsigned int whitecrush_lower = 0x7F;
80 static unsigned int vcr_hack = 0;
81 static unsigned int irq_iswitch = 0;
82 static unsigned int uv_ratio = 50;
83 static unsigned int full_luma_range = 0;
84 static unsigned int coring = 0;
85 extern int no_overlay;
87 /* API features (turn on/off stuff for testing) */
88 static unsigned int v4l2 = 1;
92 module_param(bttv_verbose, int, 0644);
93 module_param(bttv_gpio, int, 0644);
94 module_param(bttv_debug, int, 0644);
95 module_param(irq_debug, int, 0644);
96 module_param(debug_latency, int, 0644);
98 module_param(fdsr, int, 0444);
99 module_param(video_nr, int, 0444);
100 module_param(radio_nr, int, 0444);
101 module_param(vbi_nr, int, 0444);
102 module_param(gbuffers, int, 0444);
103 module_param(gbufsize, int, 0444);
105 module_param(v4l2, int, 0644);
106 module_param(bigendian, int, 0644);
107 module_param(irq_iswitch, int, 0644);
108 module_param(combfilter, int, 0444);
109 module_param(lumafilter, int, 0444);
110 module_param(automute, int, 0444);
111 module_param(chroma_agc, int, 0444);
112 module_param(adc_crush, int, 0444);
113 module_param(whitecrush_upper, int, 0444);
114 module_param(whitecrush_lower, int, 0444);
115 module_param(vcr_hack, int, 0444);
116 module_param(uv_ratio, int, 0444);
117 module_param(full_luma_range, int, 0444);
118 module_param(coring, int, 0444);
120 module_param_array(radio, int, NULL, 0444);
122 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
123 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
124 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
125 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
126 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
127 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
128 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
129 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
130 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
131 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
132 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
133 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
134 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
135 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
136 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
137 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
138 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
139 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
141 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
142 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
143 MODULE_LICENSE("GPL");
145 /* ----------------------------------------------------------------------- */
148 static ssize_t show_card(struct class_device *cd, char *buf)
150 struct video_device *vfd = to_video_device(cd);
151 struct bttv *btv = dev_get_drvdata(vfd->dev);
152 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
154 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
156 /* ----------------------------------------------------------------------- */
159 /* special timing tables from conexant... */
160 static u8 SRAM_Table[][60] =
162 /* PAL digital input over GPIO[7:0] */
164 45, // 45 bytes following
165 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
166 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
167 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
168 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
169 0x37,0x00,0xAF,0x21,0x00
171 /* NTSC digital input over GPIO[7:0] */
173 51, // 51 bytes following
174 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
175 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
176 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
177 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
178 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
181 // TGB_NTSC392 // quartzsight
182 // This table has been modified to be used for Fusion Rev D
184 0x2A, // size of table = 42
185 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
186 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
187 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
188 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
193 const struct bttv_tvnorm bttv_tvnorms[] = {
195 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
196 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
198 .v4l2_id = V4L2_STD_PAL,
206 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
207 .scaledtwidth = 1135,
213 /* ITU-R frame line number of the first VBI line
214 we can capture, of the first and second field. */
215 .vbistart = { 7,320 },
217 .v4l2_id = V4L2_STD_NTSC_M,
225 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
232 .vbistart = { 10, 273 },
234 .v4l2_id = V4L2_STD_SECAM,
242 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
243 .scaledtwidth = 1135,
248 .sram = 0, /* like PAL, correct? */
249 .vbistart = { 7, 320 },
251 .v4l2_id = V4L2_STD_PAL_Nc,
259 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
266 .vbistart = { 7, 320 },
268 .v4l2_id = V4L2_STD_PAL_M,
276 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
283 .vbistart = { 10, 273 },
285 .v4l2_id = V4L2_STD_PAL_N,
293 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
300 .vbistart = { 7, 320},
302 .v4l2_id = V4L2_STD_NTSC_M_JP,
310 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
317 .vbistart = {10, 273},
319 /* that one hopefully works with the strange timing
320 * which video recorders produce when playing a NTSC
321 * tape on a PAL TV ... */
322 .v4l2_id = V4L2_STD_PAL_60,
330 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
331 .scaledtwidth = 1135,
338 .vbistart = { 10, 273 },
341 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
343 /* ----------------------------------------------------------------------- */
345 packed pixel formats must come first */
346 static const struct bttv_format bttv_formats[] = {
348 .name = "8 bpp, gray",
349 .palette = VIDEO_PALETTE_GREY,
350 .fourcc = V4L2_PIX_FMT_GREY,
351 .btformat = BT848_COLOR_FMT_Y8,
353 .flags = FORMAT_FLAGS_PACKED,
355 .name = "8 bpp, dithered color",
356 .palette = VIDEO_PALETTE_HI240,
357 .fourcc = V4L2_PIX_FMT_HI240,
358 .btformat = BT848_COLOR_FMT_RGB8,
360 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
362 .name = "15 bpp RGB, le",
363 .palette = VIDEO_PALETTE_RGB555,
364 .fourcc = V4L2_PIX_FMT_RGB555,
365 .btformat = BT848_COLOR_FMT_RGB15,
367 .flags = FORMAT_FLAGS_PACKED,
369 .name = "15 bpp RGB, be",
371 .fourcc = V4L2_PIX_FMT_RGB555X,
372 .btformat = BT848_COLOR_FMT_RGB15,
373 .btswap = 0x03, /* byteswap */
375 .flags = FORMAT_FLAGS_PACKED,
377 .name = "16 bpp RGB, le",
378 .palette = VIDEO_PALETTE_RGB565,
379 .fourcc = V4L2_PIX_FMT_RGB565,
380 .btformat = BT848_COLOR_FMT_RGB16,
382 .flags = FORMAT_FLAGS_PACKED,
384 .name = "16 bpp RGB, be",
386 .fourcc = V4L2_PIX_FMT_RGB565X,
387 .btformat = BT848_COLOR_FMT_RGB16,
388 .btswap = 0x03, /* byteswap */
390 .flags = FORMAT_FLAGS_PACKED,
392 .name = "24 bpp RGB, le",
393 .palette = VIDEO_PALETTE_RGB24,
394 .fourcc = V4L2_PIX_FMT_BGR24,
395 .btformat = BT848_COLOR_FMT_RGB24,
397 .flags = FORMAT_FLAGS_PACKED,
399 .name = "32 bpp RGB, le",
400 .palette = VIDEO_PALETTE_RGB32,
401 .fourcc = V4L2_PIX_FMT_BGR32,
402 .btformat = BT848_COLOR_FMT_RGB32,
404 .flags = FORMAT_FLAGS_PACKED,
406 .name = "32 bpp RGB, be",
408 .fourcc = V4L2_PIX_FMT_RGB32,
409 .btformat = BT848_COLOR_FMT_RGB32,
410 .btswap = 0x0f, /* byte+word swap */
412 .flags = FORMAT_FLAGS_PACKED,
414 .name = "4:2:2, packed, YUYV",
415 .palette = VIDEO_PALETTE_YUV422,
416 .fourcc = V4L2_PIX_FMT_YUYV,
417 .btformat = BT848_COLOR_FMT_YUY2,
419 .flags = FORMAT_FLAGS_PACKED,
421 .name = "4:2:2, packed, YUYV",
422 .palette = VIDEO_PALETTE_YUYV,
423 .fourcc = V4L2_PIX_FMT_YUYV,
424 .btformat = BT848_COLOR_FMT_YUY2,
426 .flags = FORMAT_FLAGS_PACKED,
428 .name = "4:2:2, packed, UYVY",
429 .palette = VIDEO_PALETTE_UYVY,
430 .fourcc = V4L2_PIX_FMT_UYVY,
431 .btformat = BT848_COLOR_FMT_YUY2,
432 .btswap = 0x03, /* byteswap */
434 .flags = FORMAT_FLAGS_PACKED,
436 .name = "4:2:2, planar, Y-Cb-Cr",
437 .palette = VIDEO_PALETTE_YUV422P,
438 .fourcc = V4L2_PIX_FMT_YUV422P,
439 .btformat = BT848_COLOR_FMT_YCrCb422,
441 .flags = FORMAT_FLAGS_PLANAR,
445 .name = "4:2:0, planar, Y-Cb-Cr",
446 .palette = VIDEO_PALETTE_YUV420P,
447 .fourcc = V4L2_PIX_FMT_YUV420,
448 .btformat = BT848_COLOR_FMT_YCrCb422,
450 .flags = FORMAT_FLAGS_PLANAR,
454 .name = "4:2:0, planar, Y-Cr-Cb",
456 .fourcc = V4L2_PIX_FMT_YVU420,
457 .btformat = BT848_COLOR_FMT_YCrCb422,
459 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
463 .name = "4:1:1, planar, Y-Cb-Cr",
464 .palette = VIDEO_PALETTE_YUV411P,
465 .fourcc = V4L2_PIX_FMT_YUV411P,
466 .btformat = BT848_COLOR_FMT_YCrCb411,
468 .flags = FORMAT_FLAGS_PLANAR,
472 .name = "4:1:0, planar, Y-Cb-Cr",
473 .palette = VIDEO_PALETTE_YUV410P,
474 .fourcc = V4L2_PIX_FMT_YUV410,
475 .btformat = BT848_COLOR_FMT_YCrCb411,
477 .flags = FORMAT_FLAGS_PLANAR,
481 .name = "4:1:0, planar, Y-Cr-Cb",
483 .fourcc = V4L2_PIX_FMT_YVU410,
484 .btformat = BT848_COLOR_FMT_YCrCb411,
486 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
490 .name = "raw scanlines",
491 .palette = VIDEO_PALETTE_RAW,
493 .btformat = BT848_COLOR_FMT_RAW,
495 .flags = FORMAT_FLAGS_RAW,
498 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
500 /* ----------------------------------------------------------------------- */
502 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
503 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
504 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
505 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
506 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
507 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
508 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
509 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
510 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
511 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
512 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
513 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
515 static const struct v4l2_queryctrl no_ctl = {
517 .flags = V4L2_CTRL_FLAG_DISABLED,
519 static const struct v4l2_queryctrl bttv_ctls[] = {
522 .id = V4L2_CID_BRIGHTNESS,
523 .name = "Brightness",
527 .default_value = 32768,
528 .type = V4L2_CTRL_TYPE_INTEGER,
530 .id = V4L2_CID_CONTRAST,
535 .default_value = 32768,
536 .type = V4L2_CTRL_TYPE_INTEGER,
538 .id = V4L2_CID_SATURATION,
539 .name = "Saturation",
543 .default_value = 32768,
544 .type = V4L2_CTRL_TYPE_INTEGER,
551 .default_value = 32768,
552 .type = V4L2_CTRL_TYPE_INTEGER,
556 .id = V4L2_CID_AUDIO_MUTE,
560 .type = V4L2_CTRL_TYPE_BOOLEAN,
562 .id = V4L2_CID_AUDIO_VOLUME,
567 .default_value = 65535,
568 .type = V4L2_CTRL_TYPE_INTEGER,
570 .id = V4L2_CID_AUDIO_BALANCE,
575 .default_value = 32768,
576 .type = V4L2_CTRL_TYPE_INTEGER,
578 .id = V4L2_CID_AUDIO_BASS,
583 .default_value = 32768,
584 .type = V4L2_CTRL_TYPE_INTEGER,
586 .id = V4L2_CID_AUDIO_TREBLE,
591 .default_value = 32768,
592 .type = V4L2_CTRL_TYPE_INTEGER,
594 /* --- private --- */
596 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
597 .name = "chroma agc",
600 .type = V4L2_CTRL_TYPE_BOOLEAN,
602 .id = V4L2_CID_PRIVATE_COMBFILTER,
603 .name = "combfilter",
606 .type = V4L2_CTRL_TYPE_BOOLEAN,
608 .id = V4L2_CID_PRIVATE_AUTOMUTE,
612 .type = V4L2_CTRL_TYPE_BOOLEAN,
614 .id = V4L2_CID_PRIVATE_LUMAFILTER,
615 .name = "luma decimation filter",
618 .type = V4L2_CTRL_TYPE_BOOLEAN,
620 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
624 .type = V4L2_CTRL_TYPE_BOOLEAN,
626 .id = V4L2_CID_PRIVATE_VCR_HACK,
630 .type = V4L2_CTRL_TYPE_BOOLEAN,
632 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
633 .name = "whitecrush upper",
637 .default_value = 0xCF,
638 .type = V4L2_CTRL_TYPE_INTEGER,
640 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
641 .name = "whitecrush lower",
645 .default_value = 0x7F,
646 .type = V4L2_CTRL_TYPE_INTEGER,
648 .id = V4L2_CID_PRIVATE_UV_RATIO,
654 .type = V4L2_CTRL_TYPE_INTEGER,
656 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
657 .name = "full luma range",
660 .type = V4L2_CTRL_TYPE_BOOLEAN,
662 .id = V4L2_CID_PRIVATE_CORING,
668 .type = V4L2_CTRL_TYPE_INTEGER,
674 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
676 /* ----------------------------------------------------------------------- */
677 /* resource management */
680 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
682 if (fh->resources & bit)
683 /* have it already allocated */
688 if (btv->resources & bit) {
689 /* no, someone else uses it */
693 /* it's free, grab it */
694 fh->resources |= bit;
695 btv->resources |= bit;
701 int check_btres(struct bttv_fh *fh, int bit)
703 return (fh->resources & bit);
707 int locked_btres(struct bttv *btv, int bit)
709 return (btv->resources & bit);
713 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
715 if ((fh->resources & bits) != bits) {
716 /* trying to free ressources not allocated by us ... */
717 printk("bttv: BUG! (btres)\n");
720 fh->resources &= ~bits;
721 btv->resources &= ~bits;
725 /* ----------------------------------------------------------------------- */
726 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
728 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
729 PLL_X = Reference pre-divider (0=1, 1=2)
730 PLL_C = Post divider (0=6, 1=4)
731 PLL_I = Integer input
732 PLL_F = Fractional input
734 F_input = 28.636363 MHz:
735 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
738 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
740 unsigned char fl, fh, fi;
742 /* prevent overflows */
755 btwrite(fl, BT848_PLL_F_LO);
756 btwrite(fh, BT848_PLL_F_HI);
757 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
760 static void set_pll(struct bttv *btv)
764 if (!btv->pll.pll_crystal)
767 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
768 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
772 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
774 if (btv->pll.pll_current == 0)
776 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
777 btv->c.nr,btv->pll.pll_ifreq);
778 btwrite(0x00,BT848_TGCTRL);
779 btwrite(0x00,BT848_PLL_XCI);
780 btv->pll.pll_current = 0;
784 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
785 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
786 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
788 for (i=0; i<10; i++) {
789 /* Let other people run while the PLL stabilizes */
793 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
794 btwrite(0,BT848_DSTATUS);
796 btwrite(0x08,BT848_TGCTRL);
797 btv->pll.pll_current = btv->pll.pll_ofreq;
798 bttv_printk(" ok\n");
802 btv->pll.pll_current = -1;
803 bttv_printk("failed\n");
807 /* used to switch between the bt848's analog/digital video capture modes */
808 static void bt848A_set_timing(struct bttv *btv)
811 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
812 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
814 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
815 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
816 btv->c.nr,table_idx);
818 /* timing change...reset timing generator address */
819 btwrite(0x00, BT848_TGCTRL);
820 btwrite(0x02, BT848_TGCTRL);
821 btwrite(0x00, BT848_TGCTRL);
823 len=SRAM_Table[table_idx][0];
824 for(i = 1; i <= len; i++)
825 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
826 btv->pll.pll_ofreq = 27000000;
829 btwrite(0x11, BT848_TGCTRL);
830 btwrite(0x41, BT848_DVSIF);
832 btv->pll.pll_ofreq = fsc;
834 btwrite(0x0, BT848_DVSIF);
838 /* ----------------------------------------------------------------------- */
840 static void bt848_bright(struct bttv *btv, int bright)
844 // printk("bttv: set bright: %d\n",bright); // DEBUG
845 btv->bright = bright;
847 /* We want -128 to 127 we get 0-65535 */
848 value = (bright >> 8) - 128;
849 btwrite(value & 0xff, BT848_BRIGHT);
852 static void bt848_hue(struct bttv *btv, int hue)
859 value = (hue >> 8) - 128;
860 btwrite(value & 0xff, BT848_HUE);
863 static void bt848_contrast(struct bttv *btv, int cont)
867 btv->contrast = cont;
871 hibit = (value >> 6) & 4;
872 btwrite(value & 0xff, BT848_CONTRAST_LO);
873 btaor(hibit, ~4, BT848_E_CONTROL);
874 btaor(hibit, ~4, BT848_O_CONTROL);
877 static void bt848_sat(struct bttv *btv, int color)
879 int val_u,val_v,hibits;
881 btv->saturation = color;
883 /* 0-511 for the color */
884 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
885 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
886 hibits = (val_u >> 7) & 2;
887 hibits |= (val_v >> 8) & 1;
888 btwrite(val_u & 0xff, BT848_SAT_U_LO);
889 btwrite(val_v & 0xff, BT848_SAT_V_LO);
890 btaor(hibits, ~3, BT848_E_CONTROL);
891 btaor(hibits, ~3, BT848_O_CONTROL);
894 /* ----------------------------------------------------------------------- */
897 video_mux(struct bttv *btv, unsigned int input)
901 if (input >= bttv_tvcards[btv->c.type].video_inputs)
904 /* needed by RemoteVideo MX */
905 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
907 gpio_inout(mask2,mask2);
909 if (input == btv->svhs) {
910 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
911 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
913 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
914 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
916 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
917 btaor(mux<<5, ~(3<<5), BT848_IFORM);
918 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
919 btv->c.nr,input,mux);
921 /* card specific hook */
922 if(bttv_tvcards[btv->c.type].muxsel_hook)
923 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
927 static char *audio_modes[] = {
928 "audio: tuner", "audio: radio", "audio: extern",
929 "audio: intern", "audio: off"
933 audio_mux(struct bttv *btv, int mode)
935 int val,mux,i2c_mux,signal;
937 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
938 bttv_tvcards[btv->c.type].gpiomask);
939 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
943 btv->audio |= AUDIO_MUTE;
946 btv->audio &= ~AUDIO_MUTE;
952 btv->audio &= AUDIO_MUTE;
955 i2c_mux = mux = (btv->audio & AUDIO_MUTE) ? AUDIO_OFF : btv->audio;
956 if (btv->opt_automute && !signal && !btv->radio_user)
959 val = bttv_tvcards[btv->c.type].audiomux[mux];
960 gpio_bits(bttv_tvcards[btv->c.type].gpiomask,val);
962 bttv_gpio_tracking(btv,audio_modes[mux]);
964 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(i2c_mux));
969 i2c_vidiocschan(struct bttv *btv)
971 struct video_channel c;
973 memset(&c,0,sizeof(c));
974 c.norm = btv->tvnorm;
975 c.channel = btv->input;
976 bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
977 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM)
978 bttv_tda9880_setnorm(btv,c.norm);
982 set_tvnorm(struct bttv *btv, unsigned int norm)
984 const struct bttv_tvnorm *tvnorm;
986 if (norm < 0 || norm >= BTTV_TVNORMS)
990 tvnorm = &bttv_tvnorms[norm];
992 btwrite(tvnorm->adelay, BT848_ADELAY);
993 btwrite(tvnorm->bdelay, BT848_BDELAY);
994 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
996 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
997 btwrite(1, BT848_VBI_PACK_DEL);
998 bt848A_set_timing(btv);
1000 switch (btv->c.type) {
1001 case BTTV_BOARD_VOODOOTV_FM:
1002 bttv_tda9880_setnorm(btv,norm);
1009 set_input(struct bttv *btv, unsigned int input)
1011 unsigned long flags;
1015 spin_lock_irqsave(&btv->s_lock,flags);
1016 if (btv->curr.frame_irq) {
1017 /* active capture -> delayed input switch */
1018 btv->new_input = input;
1020 video_mux(btv,input);
1022 spin_unlock_irqrestore(&btv->s_lock,flags);
1024 video_mux(btv,input);
1026 audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1027 AUDIO_TUNER : AUDIO_EXTERN));
1028 set_tvnorm(btv,btv->tvnorm);
1029 i2c_vidiocschan(btv);
1032 static void init_irqreg(struct bttv *btv)
1035 btwrite(0xfffffUL, BT848_INT_STAT);
1037 if (bttv_tvcards[btv->c.type].no_video) {
1039 btwrite(BT848_INT_I2CDONE,
1043 btwrite((btv->triton1) |
1044 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1046 (fdsr ? BT848_INT_FDSR : 0) |
1047 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1048 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1054 static void init_bt848(struct bttv *btv)
1058 if (bttv_tvcards[btv->c.type].no_video) {
1059 /* very basic init only */
1064 btwrite(0x00, BT848_CAP_CTL);
1065 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1066 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1068 /* set planar and packed mode trigger points and */
1069 /* set rising edge of inverted GPINTR pin as irq trigger */
1070 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1071 BT848_GPIO_DMA_CTL_PLTP1_16|
1072 BT848_GPIO_DMA_CTL_PLTP23_16|
1073 BT848_GPIO_DMA_CTL_GPINTC|
1074 BT848_GPIO_DMA_CTL_GPINTI,
1075 BT848_GPIO_DMA_CTL);
1077 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1078 btwrite(val, BT848_E_SCLOOP);
1079 btwrite(val, BT848_O_SCLOOP);
1081 btwrite(0x20, BT848_E_VSCALE_HI);
1082 btwrite(0x20, BT848_O_VSCALE_HI);
1083 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1086 btwrite(whitecrush_upper, BT848_WC_UP);
1087 btwrite(whitecrush_lower, BT848_WC_DOWN);
1089 if (btv->opt_lumafilter) {
1090 btwrite(0, BT848_E_CONTROL);
1091 btwrite(0, BT848_O_CONTROL);
1093 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1094 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1097 bt848_bright(btv, btv->bright);
1098 bt848_hue(btv, btv->hue);
1099 bt848_contrast(btv, btv->contrast);
1100 bt848_sat(btv, btv->saturation);
1106 static void bttv_reinit_bt848(struct bttv *btv)
1108 unsigned long flags;
1111 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1112 spin_lock_irqsave(&btv->s_lock,flags);
1114 bttv_set_dma(btv,0);
1115 spin_unlock_irqrestore(&btv->s_lock,flags);
1118 btv->pll.pll_current = -1;
1119 set_input(btv,btv->input);
1122 static int get_control(struct bttv *btv, struct v4l2_control *c)
1124 struct video_audio va;
1127 for (i = 0; i < BTTV_CTLS; i++)
1128 if (bttv_ctls[i].id == c->id)
1132 if (i >= 4 && i <= 8) {
1133 memset(&va,0,sizeof(va));
1134 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1135 if (btv->audio_hook)
1136 btv->audio_hook(btv,&va,0);
1139 case V4L2_CID_BRIGHTNESS:
1140 c->value = btv->bright;
1143 c->value = btv->hue;
1145 case V4L2_CID_CONTRAST:
1146 c->value = btv->contrast;
1148 case V4L2_CID_SATURATION:
1149 c->value = btv->saturation;
1152 case V4L2_CID_AUDIO_MUTE:
1153 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1155 case V4L2_CID_AUDIO_VOLUME:
1156 c->value = va.volume;
1158 case V4L2_CID_AUDIO_BALANCE:
1159 c->value = va.balance;
1161 case V4L2_CID_AUDIO_BASS:
1164 case V4L2_CID_AUDIO_TREBLE:
1165 c->value = va.treble;
1168 case V4L2_CID_PRIVATE_CHROMA_AGC:
1169 c->value = btv->opt_chroma_agc;
1171 case V4L2_CID_PRIVATE_COMBFILTER:
1172 c->value = btv->opt_combfilter;
1174 case V4L2_CID_PRIVATE_LUMAFILTER:
1175 c->value = btv->opt_lumafilter;
1177 case V4L2_CID_PRIVATE_AUTOMUTE:
1178 c->value = btv->opt_automute;
1180 case V4L2_CID_PRIVATE_AGC_CRUSH:
1181 c->value = btv->opt_adc_crush;
1183 case V4L2_CID_PRIVATE_VCR_HACK:
1184 c->value = btv->opt_vcr_hack;
1186 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1187 c->value = btv->opt_whitecrush_upper;
1189 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1190 c->value = btv->opt_whitecrush_lower;
1192 case V4L2_CID_PRIVATE_UV_RATIO:
1193 c->value = btv->opt_uv_ratio;
1195 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1196 c->value = btv->opt_full_luma_range;
1198 case V4L2_CID_PRIVATE_CORING:
1199 c->value = btv->opt_coring;
1207 static int set_control(struct bttv *btv, struct v4l2_control *c)
1209 struct video_audio va;
1212 for (i = 0; i < BTTV_CTLS; i++)
1213 if (bttv_ctls[i].id == c->id)
1217 if (i >= 4 && i <= 8) {
1218 memset(&va,0,sizeof(va));
1219 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1220 if (btv->audio_hook)
1221 btv->audio_hook(btv,&va,0);
1224 case V4L2_CID_BRIGHTNESS:
1225 bt848_bright(btv,c->value);
1228 bt848_hue(btv,c->value);
1230 case V4L2_CID_CONTRAST:
1231 bt848_contrast(btv,c->value);
1233 case V4L2_CID_SATURATION:
1234 bt848_sat(btv,c->value);
1236 case V4L2_CID_AUDIO_MUTE:
1238 va.flags |= VIDEO_AUDIO_MUTE;
1239 audio_mux(btv, AUDIO_MUTE);
1241 va.flags &= ~VIDEO_AUDIO_MUTE;
1242 audio_mux(btv, AUDIO_UNMUTE);
1246 case V4L2_CID_AUDIO_VOLUME:
1247 va.volume = c->value;
1249 case V4L2_CID_AUDIO_BALANCE:
1250 va.balance = c->value;
1252 case V4L2_CID_AUDIO_BASS:
1255 case V4L2_CID_AUDIO_TREBLE:
1256 va.treble = c->value;
1259 case V4L2_CID_PRIVATE_CHROMA_AGC:
1260 btv->opt_chroma_agc = c->value;
1261 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1262 btwrite(val, BT848_E_SCLOOP);
1263 btwrite(val, BT848_O_SCLOOP);
1265 case V4L2_CID_PRIVATE_COMBFILTER:
1266 btv->opt_combfilter = c->value;
1268 case V4L2_CID_PRIVATE_LUMAFILTER:
1269 btv->opt_lumafilter = c->value;
1270 if (btv->opt_lumafilter) {
1271 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1272 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1274 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1275 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1278 case V4L2_CID_PRIVATE_AUTOMUTE:
1279 btv->opt_automute = c->value;
1281 case V4L2_CID_PRIVATE_AGC_CRUSH:
1282 btv->opt_adc_crush = c->value;
1283 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1286 case V4L2_CID_PRIVATE_VCR_HACK:
1287 btv->opt_vcr_hack = c->value;
1289 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1290 btv->opt_whitecrush_upper = c->value;
1291 btwrite(c->value, BT848_WC_UP);
1293 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1294 btv->opt_whitecrush_lower = c->value;
1295 btwrite(c->value, BT848_WC_DOWN);
1297 case V4L2_CID_PRIVATE_UV_RATIO:
1298 btv->opt_uv_ratio = c->value;
1299 bt848_sat(btv, btv->saturation);
1301 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1302 btv->opt_full_luma_range = c->value;
1303 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1305 case V4L2_CID_PRIVATE_CORING:
1306 btv->opt_coring = c->value;
1307 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1312 if (i >= 4 && i <= 8) {
1313 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1314 if (btv->audio_hook)
1315 btv->audio_hook(btv,&va,1);
1320 /* ----------------------------------------------------------------------- */
1322 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1324 unsigned int outbits, data;
1325 outbits = btread(BT848_GPIO_OUT_EN);
1326 data = btread(BT848_GPIO_DATA);
1327 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1328 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1331 static void bttv_field_count(struct bttv *btv)
1339 /* start field counter */
1340 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1342 /* stop field counter */
1343 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1344 btv->field_count = 0;
1348 static const struct bttv_format*
1349 format_by_palette(int palette)
1353 for (i = 0; i < BTTV_FORMATS; i++) {
1354 if (-1 == bttv_formats[i].palette)
1356 if (bttv_formats[i].palette == palette)
1357 return bttv_formats+i;
1362 static const struct bttv_format*
1363 format_by_fourcc(int fourcc)
1367 for (i = 0; i < BTTV_FORMATS; i++) {
1368 if (-1 == bttv_formats[i].fourcc)
1370 if (bttv_formats[i].fourcc == fourcc)
1371 return bttv_formats+i;
1376 /* ----------------------------------------------------------------------- */
1380 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1381 struct bttv_buffer *new)
1383 struct bttv_buffer *old;
1384 unsigned long flags;
1387 dprintk("switch_overlay: enter [new=%p]\n",new);
1389 new->vb.state = STATE_DONE;
1390 spin_lock_irqsave(&btv->s_lock,flags);
1394 bttv_set_dma(btv, 0x03);
1395 spin_unlock_irqrestore(&btv->s_lock,flags);
1397 free_btres(btv,fh,RESOURCE_OVERLAY);
1399 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1400 bttv_dma_free(btv, old);
1403 dprintk("switch_overlay: done\n");
1407 /* ----------------------------------------------------------------------- */
1408 /* video4linux (1) interface */
1410 static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
1411 const struct bttv_format *fmt,
1412 unsigned int width, unsigned int height,
1413 enum v4l2_field field)
1415 int redo_dma_risc = 0;
1418 /* check settings */
1421 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1423 height = RAW_LINES*2;
1424 if (width*height > buf->vb.bsize)
1426 buf->vb.size = buf->vb.bsize;
1430 width > bttv_tvnorms[btv->tvnorm].swidth ||
1431 height > bttv_tvnorms[btv->tvnorm].sheight)
1433 buf->vb.size = (width * height * fmt->depth) >> 3;
1434 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1438 /* alloc + fill struct bttv_buffer (if changed) */
1439 if (buf->vb.width != width || buf->vb.height != height ||
1440 buf->vb.field != field ||
1441 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1442 buf->vb.width = width;
1443 buf->vb.height = height;
1444 buf->vb.field = field;
1445 buf->tvnorm = btv->tvnorm;
1450 /* alloc risc memory */
1451 if (STATE_NEEDS_INIT == buf->vb.state) {
1453 if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
1458 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1461 buf->vb.state = STATE_PREPARED;
1465 bttv_dma_free(btv,buf);
1470 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1472 struct bttv_fh *fh = q->priv_data;
1474 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1477 while (*size * *count > gbuffers * gbufsize)
1483 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1484 enum v4l2_field field)
1486 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1487 struct bttv_fh *fh = q->priv_data;
1489 return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
1490 fh->width, fh->height, field);
1494 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1496 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1497 struct bttv_fh *fh = q->priv_data;
1498 struct bttv *btv = fh->btv;
1500 buf->vb.state = STATE_QUEUED;
1501 list_add_tail(&buf->vb.queue,&btv->capture);
1502 if (!btv->curr.frame_irq) {
1504 bttv_set_dma(btv, 0x03);
1508 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1510 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1511 struct bttv_fh *fh = q->priv_data;
1513 bttv_dma_free(fh->btv,buf);
1516 static struct videobuf_queue_ops bttv_video_qops = {
1517 .buf_setup = buffer_setup,
1518 .buf_prepare = buffer_prepare,
1519 .buf_queue = buffer_queue,
1520 .buf_release = buffer_release,
1523 static const char *v4l1_ioctls[] = {
1524 "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
1525 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
1526 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
1527 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
1528 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
1529 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
1531 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1535 return BTTV_VERSION_CODE;
1537 /* *** v4l1 *** ************************************************ */
1540 unsigned long *freq = arg;
1546 unsigned long *freq = arg;
1549 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1550 if (btv->has_matchbox && btv->radio_user)
1551 tea5757_set_freq(btv,*freq);
1558 struct video_tuner *v = arg;
1560 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1562 if (v->tuner) /* Only tuner 0 */
1564 strcpy(v->name, "Television");
1566 v->rangehigh = 0x7FFFFFFF;
1567 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1568 v->mode = btv->tvnorm;
1569 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1570 bttv_call_i2c_clients(btv,cmd,v);
1575 struct video_tuner *v = arg;
1577 if (v->tuner) /* Only tuner 0 */
1579 if (v->mode >= BTTV_TVNORMS)
1583 set_tvnorm(btv,v->mode);
1584 bttv_call_i2c_clients(btv,cmd,v);
1591 struct video_channel *v = arg;
1592 unsigned int channel = v->channel;
1594 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1597 v->flags = VIDEO_VC_AUDIO;
1598 v->type = VIDEO_TYPE_CAMERA;
1599 v->norm = btv->tvnorm;
1600 if (channel == bttv_tvcards[btv->c.type].tuner) {
1601 strcpy(v->name,"Television");
1602 v->flags|=VIDEO_VC_TUNER;
1603 v->type=VIDEO_TYPE_TV;
1605 } else if (channel == btv->svhs) {
1606 strcpy(v->name,"S-Video");
1608 sprintf(v->name,"Composite%d",channel);
1614 struct video_channel *v = arg;
1615 unsigned int channel = v->channel;
1617 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1619 if (v->norm >= BTTV_TVNORMS)
1623 if (channel == btv->input &&
1624 v->norm == btv->tvnorm) {
1630 btv->tvnorm = v->norm;
1631 set_input(btv,v->channel);
1638 struct video_audio *v = arg;
1640 memset(v,0,sizeof(*v));
1641 strcpy(v->name,"Television");
1642 v->flags |= VIDEO_AUDIO_MUTABLE;
1643 v->mode = VIDEO_SOUND_MONO;
1646 bttv_call_i2c_clients(btv,cmd,v);
1648 /* card specific hooks */
1649 if (btv->audio_hook)
1650 btv->audio_hook(btv,v,0);
1657 struct video_audio *v = arg;
1658 unsigned int audio = v->audio;
1660 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1664 audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
1665 bttv_call_i2c_clients(btv,cmd,v);
1667 /* card specific hooks */
1668 if (btv->audio_hook)
1669 btv->audio_hook(btv,v,1);
1675 /* *** v4l2 *** ************************************************ */
1676 case VIDIOC_ENUMSTD:
1678 struct v4l2_standard *e = arg;
1679 unsigned int index = e->index;
1681 if (index >= BTTV_TVNORMS)
1683 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1684 bttv_tvnorms[e->index].name);
1690 v4l2_std_id *id = arg;
1691 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1696 v4l2_std_id *id = arg;
1699 for (i = 0; i < BTTV_TVNORMS; i++)
1700 if (*id & bttv_tvnorms[i].v4l2_id)
1702 if (i == BTTV_TVNORMS)
1707 i2c_vidiocschan(btv);
1711 case VIDIOC_QUERYSTD:
1713 v4l2_std_id *id = arg;
1715 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1716 *id = V4L2_STD_625_50;
1718 *id = V4L2_STD_525_60;
1722 case VIDIOC_ENUMINPUT:
1724 struct v4l2_input *i = arg;
1728 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1730 memset(i,0,sizeof(*i));
1732 i->type = V4L2_INPUT_TYPE_CAMERA;
1734 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1735 sprintf(i->name, "Television");
1736 i->type = V4L2_INPUT_TYPE_TUNER;
1738 } else if (i->index == btv->svhs) {
1739 sprintf(i->name, "S-Video");
1741 sprintf(i->name,"Composite%d",i->index);
1743 if (i->index == btv->input) {
1744 __u32 dstatus = btread(BT848_DSTATUS);
1745 if (0 == (dstatus & BT848_DSTATUS_PRES))
1746 i->status |= V4L2_IN_ST_NO_SIGNAL;
1747 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1748 i->status |= V4L2_IN_ST_NO_H_LOCK;
1750 for (n = 0; n < BTTV_TVNORMS; n++)
1751 i->std |= bttv_tvnorms[n].v4l2_id;
1754 case VIDIOC_G_INPUT:
1760 case VIDIOC_S_INPUT:
1762 unsigned int *i = arg;
1764 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1772 case VIDIOC_G_TUNER:
1774 struct v4l2_tuner *t = arg;
1776 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1781 memset(t,0,sizeof(*t));
1782 strcpy(t->name, "Television");
1783 t->type = V4L2_TUNER_ANALOG_TV;
1784 t->capability = V4L2_TUNER_CAP_NORM;
1785 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1786 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1789 struct video_tuner tuner;
1791 memset(&tuner, 0, sizeof (tuner));
1792 tuner.rangehigh = 0xffffffffUL;
1793 bttv_call_i2c_clients(btv, VIDIOCGTUNER, &tuner);
1794 t->rangelow = tuner.rangelow;
1795 t->rangehigh = tuner.rangehigh;
1799 struct video_audio va;
1800 memset(&va, 0, sizeof(struct video_audio));
1801 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1802 if (btv->audio_hook)
1803 btv->audio_hook(btv,&va,0);
1804 if(va.mode & VIDEO_SOUND_STEREO) {
1805 t->audmode = V4L2_TUNER_MODE_STEREO;
1806 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1808 if(va.mode & VIDEO_SOUND_LANG1) {
1809 t->audmode = V4L2_TUNER_MODE_LANG1;
1810 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1811 | V4L2_TUNER_SUB_LANG2;
1814 /* FIXME: fill capability+audmode */
1818 case VIDIOC_S_TUNER:
1820 struct v4l2_tuner *t = arg;
1822 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1828 struct video_audio va;
1829 memset(&va, 0, sizeof(struct video_audio));
1830 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1831 if (t->audmode == V4L2_TUNER_MODE_MONO)
1832 va.mode = VIDEO_SOUND_MONO;
1833 else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1834 va.mode = VIDEO_SOUND_STEREO;
1835 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1836 va.mode = VIDEO_SOUND_LANG1;
1837 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1838 va.mode = VIDEO_SOUND_LANG2;
1839 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1840 if (btv->audio_hook)
1841 btv->audio_hook(btv,&va,1);
1847 case VIDIOC_G_FREQUENCY:
1849 struct v4l2_frequency *f = arg;
1851 memset(f,0,sizeof(*f));
1852 f->type = V4L2_TUNER_ANALOG_TV;
1853 f->frequency = btv->freq;
1856 case VIDIOC_S_FREQUENCY:
1858 struct v4l2_frequency *f = arg;
1860 if (unlikely(f->tuner != 0))
1862 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
1865 btv->freq = f->frequency;
1866 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1867 if (btv->has_matchbox && btv->radio_user)
1868 tea5757_set_freq(btv,btv->freq);
1872 case VIDIOC_LOG_STATUS:
1874 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
1879 return -ENOIOCTLCMD;
1885 static int verify_window(const struct bttv_tvnorm *tvn,
1886 struct v4l2_window *win, int fixup)
1888 enum v4l2_field field;
1891 if (win->w.width < 48 || win->w.height < 32)
1893 if (win->clipcount > 2048)
1898 maxh = tvn->sheight;
1900 if (V4L2_FIELD_ANY == field) {
1901 field = (win->w.height > maxh/2)
1902 ? V4L2_FIELD_INTERLACED
1906 case V4L2_FIELD_TOP:
1907 case V4L2_FIELD_BOTTOM:
1910 case V4L2_FIELD_INTERLACED:
1916 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1919 if (win->w.width > maxw)
1920 win->w.width = maxw;
1921 if (win->w.height > maxh)
1922 win->w.height = maxh;
1927 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1928 struct v4l2_window *win, int fixup)
1930 struct v4l2_clip *clips = NULL;
1931 int n,size,retval = 0;
1933 if (NULL == fh->ovfmt)
1935 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1937 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1941 /* copy clips -- luckily v4l1 + v4l2 are binary
1942 compatible here ...*/
1944 size = sizeof(*clips)*(n+4);
1945 clips = kmalloc(size,GFP_KERNEL);
1949 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1954 /* clip against screen */
1955 if (NULL != btv->fbuf.base)
1956 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1958 btcx_sort_clips(clips,n);
1960 /* 4-byte alignments */
1961 switch (fh->ovfmt->depth) {
1964 btcx_align(&win->w, clips, n, 3);
1967 btcx_align(&win->w, clips, n, 1);
1970 /* no alignment fixups needed */
1976 down(&fh->cap.lock);
1977 kfree(fh->ov.clips);
1978 fh->ov.clips = clips;
1982 fh->ov.field = win->field;
1983 fh->ov.setup_ok = 1;
1984 btv->init.ov.w.width = win->w.width;
1985 btv->init.ov.w.height = win->w.height;
1986 btv->init.ov.field = win->field;
1988 /* update overlay if needed */
1990 if (check_btres(fh, RESOURCE_OVERLAY)) {
1991 struct bttv_buffer *new;
1993 new = videobuf_alloc(sizeof(*new));
1994 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1995 retval = bttv_switch_overlay(btv,fh,new);
2001 /* ----------------------------------------------------------------------- */
2003 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2005 struct videobuf_queue* q = NULL;
2008 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2011 case V4L2_BUF_TYPE_VBI_CAPTURE:
2020 static int bttv_resource(struct bttv_fh *fh)
2025 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2026 res = RESOURCE_VIDEO;
2028 case V4L2_BUF_TYPE_VBI_CAPTURE:
2037 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2039 struct videobuf_queue *q = bttv_queue(fh);
2040 int res = bttv_resource(fh);
2042 if (check_btres(fh,res))
2044 if (videobuf_queue_is_busy(q))
2051 pix_format_set_size (struct v4l2_pix_format * f,
2052 const struct bttv_format * fmt,
2054 unsigned int height)
2059 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2060 f->bytesperline = width; /* Y plane */
2061 f->sizeimage = (width * height * fmt->depth) >> 3;
2063 f->bytesperline = (width * fmt->depth) >> 3;
2064 f->sizeimage = height * f->bytesperline;
2068 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2071 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2072 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2073 pix_format_set_size (&f->fmt.pix, fh->fmt,
2074 fh->width, fh->height);
2075 f->fmt.pix.field = fh->cap.field;
2076 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2078 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2079 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2080 f->fmt.win.w = fh->ov.w;
2081 f->fmt.win.field = fh->ov.field;
2083 case V4L2_BUF_TYPE_VBI_CAPTURE:
2084 bttv_vbi_get_fmt(fh,f);
2091 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2092 struct v4l2_format *f)
2095 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2097 const struct bttv_format *fmt;
2098 enum v4l2_field field;
2099 unsigned int maxw,maxh;
2101 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2106 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2107 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2108 field = f->fmt.pix.field;
2109 if (V4L2_FIELD_ANY == field)
2110 field = (f->fmt.pix.height > maxh/2)
2111 ? V4L2_FIELD_INTERLACED
2112 : V4L2_FIELD_BOTTOM;
2113 if (V4L2_FIELD_SEQ_BT == field)
2114 field = V4L2_FIELD_SEQ_TB;
2116 case V4L2_FIELD_TOP:
2117 case V4L2_FIELD_BOTTOM:
2118 case V4L2_FIELD_ALTERNATE:
2121 case V4L2_FIELD_INTERLACED:
2123 case V4L2_FIELD_SEQ_TB:
2124 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2131 /* update data for the application */
2132 f->fmt.pix.field = field;
2133 if (f->fmt.pix.width < 48)
2134 f->fmt.pix.width = 48;
2135 if (f->fmt.pix.height < 32)
2136 f->fmt.pix.height = 32;
2137 if (f->fmt.pix.width > maxw)
2138 f->fmt.pix.width = maxw;
2139 if (f->fmt.pix.height > maxh)
2140 f->fmt.pix.height = maxh;
2141 pix_format_set_size (&f->fmt.pix, fmt,
2142 f->fmt.pix.width & ~3,
2147 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2148 return verify_window(&bttv_tvnorms[btv->tvnorm],
2150 case V4L2_BUF_TYPE_VBI_CAPTURE:
2151 bttv_vbi_try_fmt(fh,f);
2158 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2159 struct v4l2_format *f)
2164 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2166 const struct bttv_format *fmt;
2168 retval = bttv_switch_type(fh,f->type);
2171 retval = bttv_try_fmt(fh,btv,f);
2174 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2176 /* update our state informations */
2177 down(&fh->cap.lock);
2179 fh->cap.field = f->fmt.pix.field;
2180 fh->cap.last = V4L2_FIELD_NONE;
2181 fh->width = f->fmt.pix.width;
2182 fh->height = f->fmt.pix.height;
2183 btv->init.fmt = fmt;
2184 btv->init.width = f->fmt.pix.width;
2185 btv->init.height = f->fmt.pix.height;
2190 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2191 if (no_overlay > 0) {
2192 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2195 return setup_window(fh, btv, &f->fmt.win, 1);
2196 case V4L2_BUF_TYPE_VBI_CAPTURE:
2197 retval = bttv_switch_type(fh,f->type);
2200 if (locked_btres(fh->btv, RESOURCE_VBI))
2202 bttv_vbi_try_fmt(fh,f);
2203 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2204 bttv_vbi_get_fmt(fh,f);
2211 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2212 unsigned int cmd, void *arg)
2214 struct bttv_fh *fh = file->private_data;
2215 struct bttv *btv = fh->btv;
2216 unsigned long flags;
2219 if (bttv_debug > 1) {
2220 switch (_IOC_TYPE(cmd)) {
2222 printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
2223 btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
2224 v4l1_ioctls[_IOC_NR(cmd)] : "???");
2227 printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
2228 btv->c.nr, cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
2231 printk("bttv%d: ioctl 0x%x (???)\n",
2236 bttv_reinit_bt848(btv);
2244 case VIDIOC_S_INPUT:
2245 case VIDIOC_S_TUNER:
2246 case VIDIOC_S_FREQUENCY:
2247 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2254 /* *** v4l1 *** ************************************************ */
2257 struct video_capability *cap = arg;
2259 memset(cap,0,sizeof(*cap));
2260 strcpy(cap->name,btv->video_dev->name);
2261 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2263 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2266 cap->type = VID_TYPE_CAPTURE|
2270 if (no_overlay <= 0)
2271 cap->type |= VID_TYPE_OVERLAY;
2273 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2274 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2276 cap->minheight = 32;
2278 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2279 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2285 struct video_picture *pic = arg;
2287 memset(pic,0,sizeof(*pic));
2288 pic->brightness = btv->bright;
2289 pic->contrast = btv->contrast;
2290 pic->hue = btv->hue;
2291 pic->colour = btv->saturation;
2293 pic->depth = fh->fmt->depth;
2294 pic->palette = fh->fmt->palette;
2300 struct video_picture *pic = arg;
2301 const struct bttv_format *fmt;
2303 fmt = format_by_palette(pic->palette);
2306 down(&fh->cap.lock);
2307 if (fmt->depth != pic->depth) {
2309 goto fh_unlock_and_return;
2311 if (fmt->flags & FORMAT_FLAGS_RAW) {
2312 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2313 RAW_LINES * 2. F1 is stored at offset 0, F2
2314 at buffer size / 2. */
2315 fh->width = RAW_BPL;
2316 fh->height = gbufsize / RAW_BPL;
2317 btv->init.width = RAW_BPL;
2318 btv->init.height = gbufsize / RAW_BPL;
2322 btv->init.ovfmt = fmt;
2323 btv->init.fmt = fmt;
2325 /* dirty hack time: swap bytes for overlay if the
2326 display adaptor is big endian (insmod option) */
2327 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2328 fmt->palette == VIDEO_PALETTE_RGB565 ||
2329 fmt->palette == VIDEO_PALETTE_RGB32) {
2333 bt848_bright(btv,pic->brightness);
2334 bt848_contrast(btv,pic->contrast);
2335 bt848_hue(btv,pic->hue);
2336 bt848_sat(btv,pic->colour);
2343 struct video_window *win = arg;
2345 memset(win,0,sizeof(*win));
2346 win->x = fh->ov.w.left;
2347 win->y = fh->ov.w.top;
2348 win->width = fh->ov.w.width;
2349 win->height = fh->ov.w.height;
2354 struct video_window *win = arg;
2355 struct v4l2_window w2;
2357 if (no_overlay > 0) {
2358 printk ("VIDIOCSWIN: no_overlay\n");
2362 w2.field = V4L2_FIELD_ANY;
2365 w2.w.width = win->width;
2366 w2.w.height = win->height;
2367 w2.clipcount = win->clipcount;
2368 w2.clips = (struct v4l2_clip __user *)win->clips;
2369 retval = setup_window(fh, btv, &w2, 0);
2371 /* on v4l1 this ioctl affects the read() size too */
2372 fh->width = fh->ov.w.width;
2373 fh->height = fh->ov.w.height;
2374 btv->init.width = fh->ov.w.width;
2375 btv->init.height = fh->ov.w.height;
2382 struct video_buffer *fbuf = arg;
2384 fbuf->base = btv->fbuf.base;
2385 fbuf->width = btv->fbuf.fmt.width;
2386 fbuf->height = btv->fbuf.fmt.height;
2387 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2389 fbuf->depth = fh->ovfmt->depth;
2394 struct video_buffer *fbuf = arg;
2395 const struct bttv_format *fmt;
2398 if(!capable(CAP_SYS_ADMIN) &&
2399 !capable(CAP_SYS_RAWIO))
2401 end = (unsigned long)fbuf->base +
2402 fbuf->height * fbuf->bytesperline;
2403 down(&fh->cap.lock);
2406 switch (fbuf->depth) {
2408 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2411 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2414 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2417 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2421 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2428 goto fh_unlock_and_return;
2432 btv->init.ovfmt = fmt;
2433 btv->init.fmt = fmt;
2434 btv->fbuf.base = fbuf->base;
2435 btv->fbuf.fmt.width = fbuf->width;
2436 btv->fbuf.fmt.height = fbuf->height;
2437 if (fbuf->bytesperline)
2438 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2440 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2446 case VIDIOC_OVERLAY:
2448 struct bttv_buffer *new;
2453 if (NULL == btv->fbuf.base)
2455 if (!fh->ov.setup_ok) {
2456 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2461 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2464 down(&fh->cap.lock);
2466 fh->ov.tvnorm = btv->tvnorm;
2467 new = videobuf_alloc(sizeof(*new));
2468 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2474 retval = bttv_switch_overlay(btv,fh,new);
2481 struct video_mbuf *mbuf = arg;
2484 down(&fh->cap.lock);
2485 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2488 goto fh_unlock_and_return;
2489 memset(mbuf,0,sizeof(*mbuf));
2490 mbuf->frames = gbuffers;
2491 mbuf->size = gbuffers * gbufsize;
2492 for (i = 0; i < gbuffers; i++)
2493 mbuf->offsets[i] = i * gbufsize;
2497 case VIDIOCMCAPTURE:
2499 struct video_mmap *vm = arg;
2500 struct bttv_buffer *buf;
2501 enum v4l2_field field;
2503 if (vm->frame >= VIDEO_MAX_FRAME)
2506 down(&fh->cap.lock);
2508 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2510 goto fh_unlock_and_return;
2511 if (0 == buf->vb.baddr)
2512 goto fh_unlock_and_return;
2513 if (buf->vb.state == STATE_QUEUED ||
2514 buf->vb.state == STATE_ACTIVE)
2515 goto fh_unlock_and_return;
2517 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2518 ? V4L2_FIELD_INTERLACED
2519 : V4L2_FIELD_BOTTOM;
2520 retval = bttv_prepare_buffer(btv,buf,
2521 format_by_palette(vm->format),
2522 vm->width,vm->height,field);
2524 goto fh_unlock_and_return;
2525 spin_lock_irqsave(&btv->s_lock,flags);
2526 buffer_queue(&fh->cap,&buf->vb);
2527 spin_unlock_irqrestore(&btv->s_lock,flags);
2534 struct bttv_buffer *buf;
2536 if (*frame >= VIDEO_MAX_FRAME)
2539 down(&fh->cap.lock);
2541 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2543 goto fh_unlock_and_return;
2544 retval = videobuf_waiton(&buf->vb,0,1);
2546 goto fh_unlock_and_return;
2547 switch (buf->vb.state) {
2552 videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
2553 bttv_dma_free(btv,buf);
2565 struct vbi_format *fmt = (void *) arg;
2566 struct v4l2_format fmt2;
2568 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2569 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2573 bttv_vbi_get_fmt(fh, &fmt2);
2575 memset(fmt,0,sizeof(*fmt));
2576 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2577 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2578 fmt->sample_format = VIDEO_PALETTE_RAW;
2579 fmt->start[0] = fmt2.fmt.vbi.start[0];
2580 fmt->count[0] = fmt2.fmt.vbi.count[0];
2581 fmt->start[1] = fmt2.fmt.vbi.start[1];
2582 fmt->count[1] = fmt2.fmt.vbi.count[1];
2583 if (fmt2.fmt.vbi.flags & V4L2_VBI_UNSYNC)
2584 fmt->flags |= VBI_UNSYNC;
2585 if (fmt2.fmt.vbi.flags & V4L2_VBI_INTERLACED)
2586 fmt->flags |= VBI_INTERLACED;
2591 struct vbi_format *fmt = (void *) arg;
2592 struct v4l2_format fmt2;
2594 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2597 bttv_vbi_get_fmt(fh, &fmt2);
2599 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2600 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2601 fmt->sample_format != VIDEO_PALETTE_RAW ||
2602 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2603 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2604 fmt->count[0] != fmt->count[1] ||
2605 fmt->count[0] < 1 ||
2606 fmt->count[0] > 32 /* VBI_MAXLINES */)
2609 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2622 return bttv_common_ioctls(btv,cmd,arg);
2624 /* *** v4l2 *** ************************************************ */
2625 case VIDIOC_QUERYCAP:
2627 struct v4l2_capability *cap = arg;
2631 memset(cap, 0, sizeof (*cap));
2632 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
2633 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
2634 snprintf(cap->bus_info, sizeof (cap->bus_info),
2635 "PCI:%s", pci_name(btv->c.pci));
2636 cap->version = BTTV_VERSION_CODE;
2638 V4L2_CAP_VIDEO_CAPTURE |
2639 V4L2_CAP_VBI_CAPTURE |
2640 V4L2_CAP_READWRITE |
2642 if (no_overlay <= 0)
2643 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2645 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2646 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2647 cap->capabilities |= V4L2_CAP_TUNER;
2651 case VIDIOC_ENUM_FMT:
2653 struct v4l2_fmtdesc *f = arg;
2654 enum v4l2_buf_type type;
2659 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2664 memset(f,0,sizeof(*f));
2667 f->pixelformat = V4L2_PIX_FMT_GREY;
2668 strcpy(f->description,"vbi data");
2672 /* video capture + overlay */
2674 for (i = 0; i < BTTV_FORMATS; i++) {
2675 if (bttv_formats[i].fourcc != -1)
2677 if ((unsigned int)index == f->index)
2680 if (BTTV_FORMATS == i)
2684 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2686 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2687 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2693 memset(f,0,sizeof(*f));
2696 f->pixelformat = bttv_formats[i].fourcc;
2697 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2701 case VIDIOC_TRY_FMT:
2703 struct v4l2_format *f = arg;
2704 return bttv_try_fmt(fh,btv,f);
2708 struct v4l2_format *f = arg;
2709 return bttv_g_fmt(fh,f);
2713 struct v4l2_format *f = arg;
2714 return bttv_s_fmt(fh,btv,f);
2719 struct v4l2_framebuffer *fb = arg;
2722 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2724 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2729 struct v4l2_framebuffer *fb = arg;
2730 const struct bttv_format *fmt;
2732 if(!capable(CAP_SYS_ADMIN) &&
2733 !capable(CAP_SYS_RAWIO))
2737 fmt = format_by_fourcc(fb->fmt.pixelformat);
2740 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2743 down(&fh->cap.lock);
2745 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2746 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2747 goto fh_unlock_and_return;
2748 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2749 goto fh_unlock_and_return;
2753 btv->fbuf.base = fb->base;
2754 btv->fbuf.fmt.width = fb->fmt.width;
2755 btv->fbuf.fmt.height = fb->fmt.height;
2756 if (0 != fb->fmt.bytesperline)
2757 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2759 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2763 btv->init.ovfmt = fmt;
2764 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2767 fh->ov.w.width = fb->fmt.width;
2768 fh->ov.w.height = fb->fmt.height;
2769 btv->init.ov.w.width = fb->fmt.width;
2770 btv->init.ov.w.height = fb->fmt.height;
2771 kfree(fh->ov.clips);
2772 fh->ov.clips = NULL;
2775 if (check_btres(fh, RESOURCE_OVERLAY)) {
2776 struct bttv_buffer *new;
2778 new = videobuf_alloc(sizeof(*new));
2779 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2780 retval = bttv_switch_overlay(btv,fh,new);
2787 case VIDIOC_REQBUFS:
2788 return videobuf_reqbufs(bttv_queue(fh),arg);
2790 case VIDIOC_QUERYBUF:
2791 return videobuf_querybuf(bttv_queue(fh),arg);
2794 return videobuf_qbuf(bttv_queue(fh),arg);
2797 return videobuf_dqbuf(bttv_queue(fh),arg,
2798 file->f_flags & O_NONBLOCK);
2800 case VIDIOC_STREAMON:
2802 int res = bttv_resource(fh);
2804 if (!check_alloc_btres(btv,fh,res))
2806 return videobuf_streamon(bttv_queue(fh));
2808 case VIDIOC_STREAMOFF:
2810 int res = bttv_resource(fh);
2812 retval = videobuf_streamoff(bttv_queue(fh));
2815 free_btres(btv,fh,res);
2819 case VIDIOC_QUERYCTRL:
2821 struct v4l2_queryctrl *c = arg;
2824 if ((c->id < V4L2_CID_BASE ||
2825 c->id >= V4L2_CID_LASTP1) &&
2826 (c->id < V4L2_CID_PRIVATE_BASE ||
2827 c->id >= V4L2_CID_PRIVATE_LASTP1))
2829 for (i = 0; i < BTTV_CTLS; i++)
2830 if (bttv_ctls[i].id == c->id)
2832 if (i == BTTV_CTLS) {
2837 if (i >= 4 && i <= 8) {
2838 struct video_audio va;
2839 memset(&va,0,sizeof(va));
2840 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2841 if (btv->audio_hook)
2842 btv->audio_hook(btv,&va,0);
2843 switch (bttv_ctls[i].id) {
2844 case V4L2_CID_AUDIO_VOLUME:
2845 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2848 case V4L2_CID_AUDIO_BALANCE:
2849 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2852 case V4L2_CID_AUDIO_BASS:
2853 if (!(va.flags & VIDEO_AUDIO_BASS))
2856 case V4L2_CID_AUDIO_TREBLE:
2857 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2865 return get_control(btv,arg);
2867 return set_control(btv,arg);
2870 struct v4l2_streamparm *parm = arg;
2871 struct v4l2_standard s;
2872 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2874 memset(parm,0,sizeof(*parm));
2875 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2876 bttv_tvnorms[btv->tvnorm].name);
2877 parm->parm.capture.timeperframe = s.frameperiod;
2881 case VIDIOC_G_PRIORITY:
2883 enum v4l2_priority *p = arg;
2885 *p = v4l2_prio_max(&btv->prio);
2888 case VIDIOC_S_PRIORITY:
2890 enum v4l2_priority *prio = arg;
2892 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2895 case VIDIOC_ENUMSTD:
2898 case VIDIOC_ENUMINPUT:
2899 case VIDIOC_G_INPUT:
2900 case VIDIOC_S_INPUT:
2901 case VIDIOC_G_TUNER:
2902 case VIDIOC_S_TUNER:
2903 case VIDIOC_G_FREQUENCY:
2904 case VIDIOC_S_FREQUENCY:
2905 case VIDIOC_LOG_STATUS:
2906 return bttv_common_ioctls(btv,cmd,arg);
2909 return -ENOIOCTLCMD;
2913 fh_unlock_and_return:
2918 static int bttv_ioctl(struct inode *inode, struct file *file,
2919 unsigned int cmd, unsigned long arg)
2921 struct bttv_fh *fh = file->private_data;
2925 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2926 return fh->lines * 2 * 2048;
2928 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2932 static ssize_t bttv_read(struct file *file, char __user *data,
2933 size_t count, loff_t *ppos)
2935 struct bttv_fh *fh = file->private_data;
2938 if (fh->btv->errors)
2939 bttv_reinit_bt848(fh->btv);
2940 dprintk("bttv%d: read count=%d type=%s\n",
2941 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2944 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2945 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2947 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2948 file->f_flags & O_NONBLOCK);
2950 case V4L2_BUF_TYPE_VBI_CAPTURE:
2951 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2953 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2954 file->f_flags & O_NONBLOCK);
2962 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2964 struct bttv_fh *fh = file->private_data;
2965 struct bttv_buffer *buf;
2966 enum v4l2_field field;
2968 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2969 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2971 return videobuf_poll_stream(file, &fh->vbi, wait);
2974 if (check_btres(fh,RESOURCE_VIDEO)) {
2975 /* streaming capture */
2976 if (list_empty(&fh->cap.stream))
2978 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2980 /* read() capture */
2981 down(&fh->cap.lock);
2982 if (NULL == fh->cap.read_buf) {
2983 /* need to capture a new frame */
2984 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2988 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2989 if (NULL == fh->cap.read_buf) {
2993 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2994 field = videobuf_next_field(&fh->cap);
2995 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2996 kfree (fh->cap.read_buf);
2997 fh->cap.read_buf = NULL;
3001 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3002 fh->cap.read_off = 0;
3005 buf = (struct bttv_buffer*)fh->cap.read_buf;
3008 poll_wait(file, &buf->vb.done, wait);
3009 if (buf->vb.state == STATE_DONE ||
3010 buf->vb.state == STATE_ERROR)
3011 return POLLIN|POLLRDNORM;
3015 static int bttv_open(struct inode *inode, struct file *file)
3017 int minor = iminor(inode);
3018 struct bttv *btv = NULL;
3020 enum v4l2_buf_type type = 0;
3023 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3025 for (i = 0; i < bttv_num; i++) {
3026 if (bttvs[i].video_dev &&
3027 bttvs[i].video_dev->minor == minor) {
3029 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3032 if (bttvs[i].vbi_dev &&
3033 bttvs[i].vbi_dev->minor == minor) {
3035 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3042 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3043 btv->c.nr,v4l2_type_names[type]);
3045 /* allocate per filehandle data */
3046 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3049 file->private_data = fh;
3052 fh->ov.setup_ok = 0;
3053 v4l2_prio_open(&btv->prio,&fh->prio);
3055 videobuf_queue_init(&fh->cap, &bttv_video_qops,
3056 btv->c.pci, &btv->s_lock,
3057 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3058 V4L2_FIELD_INTERLACED,
3059 sizeof(struct bttv_buffer),
3061 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3062 btv->c.pci, &btv->s_lock,
3063 V4L2_BUF_TYPE_VBI_CAPTURE,
3065 sizeof(struct bttv_buffer),
3067 i2c_vidiocschan(btv);
3070 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
3071 bttv_vbi_setlines(fh,btv,16);
3072 bttv_field_count(btv);
3076 static int bttv_release(struct inode *inode, struct file *file)
3078 struct bttv_fh *fh = file->private_data;
3079 struct bttv *btv = fh->btv;
3081 /* turn off overlay */
3082 if (check_btres(fh, RESOURCE_OVERLAY))
3083 bttv_switch_overlay(btv,fh,NULL);
3085 /* stop video capture */
3086 if (check_btres(fh, RESOURCE_VIDEO)) {
3087 videobuf_streamoff(&fh->cap);
3088 free_btres(btv,fh,RESOURCE_VIDEO);
3090 if (fh->cap.read_buf) {
3091 buffer_release(&fh->cap,fh->cap.read_buf);
3092 kfree(fh->cap.read_buf);
3095 /* stop vbi capture */
3096 if (check_btres(fh, RESOURCE_VBI)) {
3097 if (fh->vbi.streaming)
3098 videobuf_streamoff(&fh->vbi);
3099 if (fh->vbi.reading)
3100 videobuf_read_stop(&fh->vbi);
3101 free_btres(btv,fh,RESOURCE_VBI);
3105 videobuf_mmap_free(&fh->cap);
3106 videobuf_mmap_free(&fh->vbi);
3107 v4l2_prio_close(&btv->prio,&fh->prio);
3108 file->private_data = NULL;
3112 bttv_field_count(btv);
3117 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3119 struct bttv_fh *fh = file->private_data;
3121 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3122 fh->btv->c.nr, v4l2_type_names[fh->type],
3123 vma->vm_start, vma->vm_end - vma->vm_start);
3124 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3127 static struct file_operations bttv_fops =
3129 .owner = THIS_MODULE,
3131 .release = bttv_release,
3132 .ioctl = bttv_ioctl,
3133 .compat_ioctl = v4l_compat_ioctl32,
3134 .llseek = no_llseek,
3140 static struct video_device bttv_video_template =
3143 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3144 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3145 .hardware = VID_HARDWARE_BT848,
3150 static struct video_device bttv_vbi_template =
3152 .name = "bt848/878 vbi",
3153 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3154 .hardware = VID_HARDWARE_BT848,
3159 /* ----------------------------------------------------------------------- */
3160 /* radio interface */
3162 static int radio_open(struct inode *inode, struct file *file)
3164 int minor = iminor(inode);
3165 struct bttv *btv = NULL;
3168 dprintk("bttv: open minor=%d\n",minor);
3170 for (i = 0; i < bttv_num; i++) {
3171 if (bttvs[i].radio_dev->minor == minor) {
3179 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3184 file->private_data = btv;
3186 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
3187 audio_mux(btv,AUDIO_RADIO);
3193 static int radio_release(struct inode *inode, struct file *file)
3195 struct bttv *btv = file->private_data;
3196 struct rds_command cmd;
3200 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3205 static int radio_do_ioctl(struct inode *inode, struct file *file,
3206 unsigned int cmd, void *arg)
3208 struct bttv *btv = file->private_data;
3213 struct video_capability *cap = arg;
3215 memset(cap,0,sizeof(*cap));
3216 strcpy(cap->name,btv->radio_dev->name);
3217 cap->type = VID_TYPE_TUNER;
3225 struct video_tuner *v = arg;
3229 memset(v,0,sizeof(*v));
3230 strcpy(v->name, "Radio");
3231 bttv_call_i2c_clients(btv,cmd,v);
3243 return bttv_common_ioctls(btv,cmd,arg);
3246 return -ENOIOCTLCMD;
3251 static int radio_ioctl(struct inode *inode, struct file *file,
3252 unsigned int cmd, unsigned long arg)
3254 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3257 static ssize_t radio_read(struct file *file, char __user *data,
3258 size_t count, loff_t *ppos)
3260 struct bttv *btv = file->private_data;
3261 struct rds_command cmd;
3262 cmd.block_count = count/3;
3264 cmd.instance = file;
3265 cmd.result = -ENODEV;
3267 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3272 static unsigned int radio_poll(struct file *file, poll_table *wait)
3274 struct bttv *btv = file->private_data;
3275 struct rds_command cmd;
3276 cmd.instance = file;
3277 cmd.event_list = wait;
3278 cmd.result = -ENODEV;
3279 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3284 static struct file_operations radio_fops =
3286 .owner = THIS_MODULE,
3289 .release = radio_release,
3290 .ioctl = radio_ioctl,
3291 .llseek = no_llseek,
3295 static struct video_device radio_template =
3297 .name = "bt848/878 radio",
3298 .type = VID_TYPE_TUNER,
3299 .hardware = VID_HARDWARE_BT848,
3300 .fops = &radio_fops,
3304 /* ----------------------------------------------------------------------- */
3305 /* some debug code */
3307 static int bttv_risc_decode(u32 risc)
3309 static char *instr[16] = {
3310 [ BT848_RISC_WRITE >> 28 ] = "write",
3311 [ BT848_RISC_SKIP >> 28 ] = "skip",
3312 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3313 [ BT848_RISC_JUMP >> 28 ] = "jump",
3314 [ BT848_RISC_SYNC >> 28 ] = "sync",
3315 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3316 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3317 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3319 static int incr[16] = {
3320 [ BT848_RISC_WRITE >> 28 ] = 2,
3321 [ BT848_RISC_JUMP >> 28 ] = 2,
3322 [ BT848_RISC_SYNC >> 28 ] = 2,
3323 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3324 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3325 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3327 static char *bits[] = {
3328 "be0", "be1", "be2", "be3/resync",
3329 "set0", "set1", "set2", "set3",
3330 "clr0", "clr1", "clr2", "clr3",
3331 "irq", "res", "eol", "sol",
3335 printk("0x%08x [ %s", risc,
3336 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3337 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3338 if (risc & (1 << (i + 12)))
3339 printk(" %s",bits[i]);
3340 printk(" count=%d ]\n", risc & 0xfff);
3341 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3344 static void bttv_risc_disasm(struct bttv *btv,
3345 struct btcx_riscmem *risc)
3349 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3350 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3351 for (i = 0; i < (risc->size >> 2); i += n) {
3352 printk("%s: 0x%lx: ", btv->c.name,
3353 (unsigned long)(risc->dma + (i<<2)));
3354 n = bttv_risc_decode(risc->cpu[i]);
3355 for (j = 1; j < n; j++)
3356 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3357 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3359 if (0 == risc->cpu[i])
3364 static void bttv_print_riscaddr(struct bttv *btv)
3366 printk(" main: %08Lx\n",
3367 (unsigned long long)btv->main.dma);
3368 printk(" vbi : o=%08Lx e=%08Lx\n",
3369 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3370 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3371 printk(" cap : o=%08Lx e=%08Lx\n",
3372 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3373 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3374 printk(" scr : o=%08Lx e=%08Lx\n",
3375 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3376 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3377 bttv_risc_disasm(btv, &btv->main);
3380 /* ----------------------------------------------------------------------- */
3383 static char *irq_name[] = {
3384 "FMTCHG", // format change detected (525 vs. 625)
3385 "VSYNC", // vertical sync (new field)
3386 "HSYNC", // horizontal sync
3387 "OFLOW", // chroma/luma AGC overflow
3388 "HLOCK", // horizontal lock changed
3389 "VPRES", // video presence changed
3391 "I2CDONE", // hw irc operation finished
3392 "GPINT", // gpio port triggered irq
3394 "RISCI", // risc instruction triggered irq
3395 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3396 "FTRGT", // pixel data fifo overrun
3397 "FDSR", // fifo data stream resyncronisation
3398 "PPERR", // parity error (data transfer)
3399 "RIPERR", // parity error (read risc instructions)
3400 "PABORT", // pci abort
3401 "OCERR", // risc instruction error
3402 "SCERR", // syncronisation error
3405 static void bttv_print_irqbits(u32 print, u32 mark)
3410 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3411 if (print & (1 << i))
3412 printk(" %s",irq_name[i]);
3413 if (mark & (1 << i))
3418 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3420 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3422 (unsigned long)btv->main.dma,
3423 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3424 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3427 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3428 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3429 "Ok, then this is harmless, don't worry ;)\n",
3433 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3435 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3441 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3443 struct bttv_buffer *item;
3445 memset(set,0,sizeof(*set));
3447 /* capture request ? */
3448 if (!list_empty(&btv->capture)) {
3450 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3451 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3453 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3456 /* capture request for other field ? */
3457 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3458 (item->vb.queue.next != &btv->capture)) {
3459 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3460 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3461 if (NULL == set->top &&
3462 V4L2_FIELD_TOP == item->vb.field) {
3465 if (NULL == set->bottom &&
3466 V4L2_FIELD_BOTTOM == item->vb.field) {
3469 if (NULL != set->top && NULL != set->bottom)
3475 /* screen overlay ? */
3476 if (NULL != btv->screen) {
3477 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3478 if (NULL == set->top && NULL == set->bottom) {
3479 set->top = btv->screen;
3480 set->bottom = btv->screen;
3483 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3485 set->top = btv->screen;
3487 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3488 NULL == set->bottom) {
3489 set->bottom = btv->screen;
3494 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3495 btv->c.nr,set->top, set->bottom,
3496 btv->screen,set->frame_irq,set->top_irq);
3501 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3502 struct bttv_buffer_set *curr, unsigned int state)
3506 do_gettimeofday(&ts);
3508 if (wakeup->top == wakeup->bottom) {
3509 if (NULL != wakeup->top && curr->top != wakeup->top) {
3511 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3512 wakeup->top->vb.ts = ts;
3513 wakeup->top->vb.field_count = btv->field_count;
3514 wakeup->top->vb.state = state;
3515 wake_up(&wakeup->top->vb.done);
3518 if (NULL != wakeup->top && curr->top != wakeup->top) {
3520 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3521 wakeup->top->vb.ts = ts;
3522 wakeup->top->vb.field_count = btv->field_count;
3523 wakeup->top->vb.state = state;
3524 wake_up(&wakeup->top->vb.done);
3526 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3528 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3529 wakeup->bottom->vb.ts = ts;
3530 wakeup->bottom->vb.field_count = btv->field_count;
3531 wakeup->bottom->vb.state = state;
3532 wake_up(&wakeup->bottom->vb.done);
3538 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3546 do_gettimeofday(&ts);
3548 wakeup->vb.field_count = btv->field_count;
3549 wakeup->vb.state = state;
3550 wake_up(&wakeup->vb.done);
3553 static void bttv_irq_timeout(unsigned long data)
3555 struct bttv *btv = (struct bttv *)data;
3556 struct bttv_buffer_set old,new;
3557 struct bttv_buffer *ovbi;
3558 struct bttv_buffer *item;
3559 unsigned long flags;
3562 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3563 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3564 btread(BT848_RISC_COUNT));
3565 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3569 spin_lock_irqsave(&btv->s_lock,flags);
3571 /* deactivate stuff */
3572 memset(&new,0,sizeof(new));
3578 bttv_buffer_activate_video(btv, &new);
3579 bttv_buffer_activate_vbi(btv, NULL);
3580 bttv_set_dma(btv, 0);
3583 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3584 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3586 /* cancel all outstanding capture / vbi requests */
3587 while (!list_empty(&btv->capture)) {
3588 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3589 list_del(&item->vb.queue);
3590 item->vb.state = STATE_ERROR;
3591 wake_up(&item->vb.done);
3593 while (!list_empty(&btv->vcapture)) {
3594 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3595 list_del(&item->vb.queue);
3596 item->vb.state = STATE_ERROR;
3597 wake_up(&item->vb.done);
3601 spin_unlock_irqrestore(&btv->s_lock,flags);
3605 bttv_irq_wakeup_top(struct bttv *btv)
3607 struct bttv_buffer *wakeup = btv->curr.top;
3612 spin_lock(&btv->s_lock);
3613 btv->curr.top_irq = 0;
3614 btv->curr.top = NULL;
3615 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3617 do_gettimeofday(&wakeup->vb.ts);
3618 wakeup->vb.field_count = btv->field_count;
3619 wakeup->vb.state = STATE_DONE;
3620 wake_up(&wakeup->vb.done);
3621 spin_unlock(&btv->s_lock);
3624 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3628 if (rc > risc->dma + risc->size)
3634 bttv_irq_switch_video(struct bttv *btv)
3636 struct bttv_buffer_set new;
3637 struct bttv_buffer_set old;
3640 spin_lock(&btv->s_lock);
3642 /* new buffer set */
3643 bttv_irq_next_video(btv, &new);
3644 rc = btread(BT848_RISC_COUNT);
3645 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3646 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3649 bttv_irq_debug_low_latency(btv, rc);
3650 spin_unlock(&btv->s_lock);
3657 btv->loop_irq &= ~1;
3658 bttv_buffer_activate_video(btv, &new);
3659 bttv_set_dma(btv, 0);
3662 if (UNSET != btv->new_input) {
3663 video_mux(btv,btv->new_input);
3664 btv->new_input = UNSET;
3667 /* wake up finished buffers */
3668 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3669 spin_unlock(&btv->s_lock);
3673 bttv_irq_switch_vbi(struct bttv *btv)
3675 struct bttv_buffer *new = NULL;
3676 struct bttv_buffer *old;
3679 spin_lock(&btv->s_lock);
3681 if (!list_empty(&btv->vcapture))
3682 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3685 rc = btread(BT848_RISC_COUNT);
3686 if (NULL != old && (is_active(&old->top, rc) ||
3687 is_active(&old->bottom, rc))) {
3690 bttv_irq_debug_low_latency(btv, rc);
3691 spin_unlock(&btv->s_lock);
3697 btv->loop_irq &= ~4;
3698 bttv_buffer_activate_vbi(btv, new);
3699 bttv_set_dma(btv, 0);
3701 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3702 spin_unlock(&btv->s_lock);
3705 static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3713 btv=(struct bttv *)dev_id;
3715 if (btv->custom_irq)
3716 handled = btv->custom_irq(btv);
3720 /* get/clear interrupt status bits */
3721 stat=btread(BT848_INT_STAT);
3722 astat=stat&btread(BT848_INT_MASK);
3726 btwrite(stat,BT848_INT_STAT);
3728 /* get device status bits */
3729 dstat=btread(BT848_DSTATUS);
3732 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3733 "riscs=%x, riscc=%08x, ",
3734 btv->c.nr, count, btv->field_count,
3735 stat>>28, btread(BT848_RISC_COUNT));
3736 bttv_print_irqbits(stat,astat);
3737 if (stat & BT848_INT_HLOCK)
3738 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3740 if (stat & BT848_INT_VPRES)
3741 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3743 if (stat & BT848_INT_FMTCHG)
3744 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3749 if (astat&BT848_INT_VSYNC)
3752 if ((astat & BT848_INT_GPINT) && btv->remote) {
3753 wake_up(&btv->gpioq);
3754 bttv_input_irq(btv);
3757 if (astat & BT848_INT_I2CDONE) {
3758 btv->i2c_done = stat;
3759 wake_up(&btv->i2c_queue);
3762 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3763 bttv_irq_switch_vbi(btv);
3765 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3766 bttv_irq_wakeup_top(btv);
3768 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3769 bttv_irq_switch_video(btv);
3771 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3774 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3775 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3776 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3777 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3778 btread(BT848_RISC_COUNT));
3779 bttv_print_irqbits(stat,astat);
3782 bttv_print_riscaddr(btv);
3784 if (fdsr && astat & BT848_INT_FDSR) {
3785 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3786 btv->c.nr,btread(BT848_RISC_COUNT));
3788 bttv_print_riscaddr(btv);
3794 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3795 btwrite(0, BT848_INT_MASK);
3798 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3801 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
3803 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3807 bttv_print_irqbits(stat,astat);
3815 return IRQ_RETVAL(handled);
3819 /* ----------------------------------------------------------------------- */
3820 /* initialitation */
3822 static struct video_device *vdev_init(struct bttv *btv,
3823 struct video_device *template,
3826 struct video_device *vfd;
3828 vfd = video_device_alloc();
3833 vfd->dev = &btv->c.pci->dev;
3834 vfd->release = video_device_release;
3835 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3836 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3837 type, bttv_tvcards[btv->c.type].name);
3841 static void bttv_unregister_video(struct bttv *btv)
3843 if (btv->video_dev) {
3844 if (-1 != btv->video_dev->minor)
3845 video_unregister_device(btv->video_dev);
3847 video_device_release(btv->video_dev);
3848 btv->video_dev = NULL;
3851 if (-1 != btv->vbi_dev->minor)
3852 video_unregister_device(btv->vbi_dev);
3854 video_device_release(btv->vbi_dev);
3855 btv->vbi_dev = NULL;
3857 if (btv->radio_dev) {
3858 if (-1 != btv->radio_dev->minor)
3859 video_unregister_device(btv->radio_dev);
3861 video_device_release(btv->radio_dev);
3862 btv->radio_dev = NULL;
3866 /* register video4linux devices */
3867 static int __devinit bttv_register_video(struct bttv *btv)
3869 if (no_overlay <= 0) {
3870 bttv_video_template.type |= VID_TYPE_OVERLAY;
3872 printk("bttv: Overlay support disabled.\n");
3876 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3877 if (NULL == btv->video_dev)
3879 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3881 printk(KERN_INFO "bttv%d: registered device video%d\n",
3882 btv->c.nr,btv->video_dev->minor & 0x1f);
3883 video_device_create_file(btv->video_dev, &class_device_attr_card);
3886 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3887 if (NULL == btv->vbi_dev)
3889 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3891 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3892 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3894 if (!btv->has_radio)
3897 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3898 if (NULL == btv->radio_dev)
3900 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3902 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3903 btv->c.nr,btv->radio_dev->minor & 0x1f);
3909 bttv_unregister_video(btv);
3914 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3915 /* response on cards with no firmware is not enabled by OF */
3916 static void pci_set_command(struct pci_dev *dev)
3918 #if defined(__powerpc__)
3921 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3922 cmd = (cmd | PCI_COMMAND_MEMORY );
3923 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3927 static int __devinit bttv_probe(struct pci_dev *dev,
3928 const struct pci_device_id *pci_id)
3934 if (bttv_num == BTTV_MAX)
3936 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3937 btv=&bttvs[bttv_num];
3938 memset(btv,0,sizeof(*btv));
3939 btv->c.nr = bttv_num;
3940 sprintf(btv->c.name,"bttv%d",btv->c.nr);
3942 /* initialize structs / fill in defaults */
3943 init_MUTEX(&btv->lock);
3944 init_MUTEX(&btv->reslock);
3945 spin_lock_init(&btv->s_lock);
3946 spin_lock_init(&btv->gpio_lock);
3947 init_waitqueue_head(&btv->gpioq);
3948 init_waitqueue_head(&btv->i2c_queue);
3949 INIT_LIST_HEAD(&btv->c.subs);
3950 INIT_LIST_HEAD(&btv->capture);
3951 INIT_LIST_HEAD(&btv->vcapture);
3952 v4l2_prio_init(&btv->prio);
3954 init_timer(&btv->timeout);
3955 btv->timeout.function = bttv_irq_timeout;
3956 btv->timeout.data = (unsigned long)btv;
3959 btv->tuner_type = UNSET;
3960 btv->new_input = UNSET;
3961 btv->has_radio=radio[btv->c.nr];
3963 /* pci stuff (init, get irq/mmio, ... */
3965 btv->id = dev->device;
3966 if (pci_enable_device(dev)) {
3967 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3971 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
3972 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3976 if (!request_mem_region(pci_resource_start(dev,0),
3977 pci_resource_len(dev,0),
3979 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3980 btv->c.nr, pci_resource_start(dev,0));
3983 pci_set_master(dev);
3984 pci_set_command(dev);
3985 pci_set_drvdata(dev,btv);
3987 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3988 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3989 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3990 bttv_num,btv->id, btv->revision, pci_name(dev));
3991 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3992 btv->c.pci->irq, lat, pci_resource_start(dev,0));
3995 btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3996 if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3997 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4005 /* disable irqs, register irq handler */
4006 btwrite(0, BT848_INT_MASK);
4007 result = request_irq(btv->c.pci->irq, bttv_irq,
4008 SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
4010 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4011 bttv_num,btv->c.pci->irq);
4015 if (0 != bttv_handle_chipset(btv)) {
4020 /* init options from insmod args */
4021 btv->opt_combfilter = combfilter;
4022 btv->opt_lumafilter = lumafilter;
4023 btv->opt_automute = automute;
4024 btv->opt_chroma_agc = chroma_agc;
4025 btv->opt_adc_crush = adc_crush;
4026 btv->opt_vcr_hack = vcr_hack;
4027 btv->opt_whitecrush_upper = whitecrush_upper;
4028 btv->opt_whitecrush_lower = whitecrush_lower;
4029 btv->opt_uv_ratio = uv_ratio;
4030 btv->opt_full_luma_range = full_luma_range;
4031 btv->opt_coring = coring;
4033 /* fill struct bttv with some useful defaults */
4034 btv->init.btv = btv;
4035 btv->init.ov.w.width = 320;
4036 btv->init.ov.w.height = 240;
4037 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4038 btv->init.width = 320;
4039 btv->init.height = 240;
4040 btv->init.lines = 16;
4043 /* initialize hardware */
4045 bttv_gpio_tracking(btv,"pre-init");
4047 bttv_risc_init_main(btv);
4051 btwrite(0x00, BT848_GPIO_REG_INP);
4052 btwrite(0x00, BT848_GPIO_OUT_EN);
4054 bttv_gpio_tracking(btv,"init");
4056 /* needs to be done before i2c is registered */
4057 bttv_init_card1(btv);
4059 /* register i2c + gpio */
4062 /* some card-specific stuff (needs working i2c) */
4063 bttv_init_card2(btv);
4066 /* register video4linux + input */
4067 if (!bttv_tvcards[btv->c.type].no_video) {
4068 bttv_register_video(btv);
4069 bt848_bright(btv,32768);
4070 bt848_contrast(btv,32768);
4071 bt848_hue(btv,32768);
4072 bt848_sat(btv,32768);
4073 audio_mux(btv,AUDIO_MUTE);
4077 /* add subdevices */
4078 if (bttv_tvcards[btv->c.type].has_dvb)
4079 bttv_sub_add_device(&btv->c, "dvb");
4081 bttv_input_init(btv);
4083 /* everything is fine */
4088 free_irq(btv->c.pci->irq,btv);
4091 if (btv->bt848_mmio)
4092 iounmap(btv->bt848_mmio);
4093 release_mem_region(pci_resource_start(btv->c.pci,0),
4094 pci_resource_len(btv->c.pci,0));
4095 pci_set_drvdata(dev,NULL);
4099 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4101 struct bttv *btv = pci_get_drvdata(pci_dev);
4104 printk("bttv%d: unloading\n",btv->c.nr);
4106 /* shutdown everything (DMA+IRQs) */
4107 btand(~15, BT848_GPIO_DMA_CTL);
4108 btwrite(0, BT848_INT_MASK);
4109 btwrite(~0x0, BT848_INT_STAT);
4110 btwrite(0x0, BT848_GPIO_OUT_EN);
4112 bttv_gpio_tracking(btv,"cleanup");
4114 /* tell gpio modules we are leaving ... */
4116 wake_up(&btv->gpioq);
4117 bttv_input_fini(btv);
4118 bttv_sub_del_devices(&btv->c);
4120 /* unregister i2c_bus + input */
4123 /* unregister video4linux */
4124 bttv_unregister_video(btv);
4126 /* free allocated memory */
4127 btcx_riscmem_free(btv->c.pci,&btv->main);
4129 /* free ressources */
4130 free_irq(btv->c.pci->irq,btv);
4131 iounmap(btv->bt848_mmio);
4132 release_mem_region(pci_resource_start(btv->c.pci,0),
4133 pci_resource_len(btv->c.pci,0));
4135 pci_set_drvdata(pci_dev, NULL);
4139 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4141 struct bttv *btv = pci_get_drvdata(pci_dev);
4142 struct bttv_buffer_set idle;
4143 unsigned long flags;
4145 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4147 /* stop dma + irqs */
4148 spin_lock_irqsave(&btv->s_lock,flags);
4149 memset(&idle, 0, sizeof(idle));
4150 btv->state.video = btv->curr;
4151 btv->state.vbi = btv->cvbi;
4152 btv->state.loop_irq = btv->loop_irq;
4155 bttv_buffer_activate_video(btv, &idle);
4156 bttv_buffer_activate_vbi(btv, NULL);
4157 bttv_set_dma(btv, 0);
4158 btwrite(0, BT848_INT_MASK);
4159 spin_unlock_irqrestore(&btv->s_lock,flags);
4161 /* save bt878 state */
4162 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4163 btv->state.gpio_data = gpio_read();
4165 /* save pci state */
4166 pci_save_state(pci_dev);
4167 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4168 pci_disable_device(pci_dev);
4169 btv->state.disabled = 1;
4174 static int bttv_resume(struct pci_dev *pci_dev)
4176 struct bttv *btv = pci_get_drvdata(pci_dev);
4177 unsigned long flags;
4180 dprintk("bttv%d: resume\n", btv->c.nr);
4182 /* restore pci state */
4183 if (btv->state.disabled) {
4184 err=pci_enable_device(pci_dev);
4186 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4190 btv->state.disabled = 0;
4192 err=pci_set_power_state(pci_dev, PCI_D0);
4194 pci_disable_device(pci_dev);
4195 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4197 btv->state.disabled = 1;
4201 pci_restore_state(pci_dev);
4203 /* restore bt878 state */
4204 bttv_reinit_bt848(btv);
4205 gpio_inout(0xffffff, btv->state.gpio_enable);
4206 gpio_write(btv->state.gpio_data);
4209 spin_lock_irqsave(&btv->s_lock,flags);
4210 btv->curr = btv->state.video;
4211 btv->cvbi = btv->state.vbi;
4212 btv->loop_irq = btv->state.loop_irq;
4213 bttv_buffer_activate_video(btv, &btv->curr);
4214 bttv_buffer_activate_vbi(btv, btv->cvbi);
4215 bttv_set_dma(btv, 0);
4216 spin_unlock_irqrestore(&btv->s_lock,flags);
4220 static struct pci_device_id bttv_pci_tbl[] = {
4221 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4222 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4223 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4224 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4225 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4226 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4227 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4228 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4232 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4234 static struct pci_driver bttv_pci_driver = {
4236 .id_table = bttv_pci_tbl,
4237 .probe = bttv_probe,
4238 .remove = __devexit_p(bttv_remove),
4239 .suspend = bttv_suspend,
4240 .resume = bttv_resume,
4243 static int bttv_init_module(void)
4247 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4248 (BTTV_VERSION_CODE >> 16) & 0xff,
4249 (BTTV_VERSION_CODE >> 8) & 0xff,
4250 BTTV_VERSION_CODE & 0xff);
4252 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4253 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4255 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4257 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4258 gbufsize = BTTV_MAX_FBUF;
4259 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4261 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4262 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4264 bttv_check_chipset();
4266 bus_register(&bttv_sub_bus_type);
4267 return pci_register_driver(&bttv_pci_driver);
4270 static void bttv_cleanup_module(void)
4272 pci_unregister_driver(&bttv_pci_driver);
4273 bus_unregister(&bttv_sub_bus_type);
4277 module_init(bttv_init_module);
4278 module_exit(bttv_cleanup_module);