3 bttv - Bt848 frame grabber driver
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
6 & Marcus Metzler <mocm@thp.uni-koeln.de>
7 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
38 #include <media/v4l2-common.h>
39 #include <media/tvaudio.h>
41 #include <linux/dma-mapping.h>
44 #include <asm/byteorder.h>
46 #include <media/rds.h>
49 unsigned int bttv_num; /* number of Bt848s in use */
50 struct bttv bttvs[BTTV_MAX];
52 unsigned int bttv_debug;
53 unsigned int bttv_verbose = 1;
54 unsigned int bttv_gpio;
56 /* config variables */
58 static unsigned int bigendian=1;
60 static unsigned int bigendian;
62 static unsigned int radio[BTTV_MAX];
63 static unsigned int irq_debug;
64 static unsigned int gbuffers = 8;
65 static unsigned int gbufsize = 0x208000;
67 static int video_nr = -1;
68 static int radio_nr = -1;
69 static int vbi_nr = -1;
70 static int debug_latency;
72 static unsigned int fdsr;
75 static unsigned int combfilter;
76 static unsigned int lumafilter;
77 static unsigned int automute = 1;
78 static unsigned int chroma_agc;
79 static unsigned int adc_crush = 1;
80 static unsigned int whitecrush_upper = 0xCF;
81 static unsigned int whitecrush_lower = 0x7F;
82 static unsigned int vcr_hack;
83 static unsigned int irq_iswitch;
84 static unsigned int uv_ratio = 50;
85 static unsigned int full_luma_range;
86 static unsigned int coring;
87 extern int no_overlay;
89 /* API features (turn on/off stuff for testing) */
90 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 | V4L2_STD_NTSC_M_KR,
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 */
688 mutex_lock(&btv->reslock);
689 if (btv->resources & bit) {
690 /* no, someone else uses it */
691 mutex_unlock(&btv->reslock);
694 /* it's free, grab it */
695 fh->resources |= bit;
696 btv->resources |= bit;
697 mutex_unlock(&btv->reslock);
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");
720 mutex_lock(&btv->reslock);
721 fh->resources &= ~bits;
722 btv->resources &= ~bits;
723 mutex_unlock(&btv->reslock);
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: mute"
934 audio_mux(struct bttv *btv, int input, int mute)
936 int gpio_val, signal;
937 struct v4l2_audio aud_input;
938 struct v4l2_control ctrl;
939 struct i2c_client *c;
941 memset(&aud_input, 0, sizeof(aud_input));
942 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
943 bttv_tvcards[btv->c.type].gpiomask);
944 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
950 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
953 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
955 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
956 aud_input.index = btv->audio;
958 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
960 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
964 ctrl.id = V4L2_CID_AUDIO_MUTE;
965 /* take automute into account, just btv->mute is not enough */
967 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
968 c = btv->i2c_msp34xx_client;
970 c->driver->command(c, VIDIOC_S_AUDIO, &aud_input);
971 c = btv->i2c_tvaudio_client;
973 c->driver->command(c, VIDIOC_S_AUDIO, &aud_input);
978 audio_mute(struct bttv *btv, int mute)
980 return audio_mux(btv, btv->audio, mute);
984 audio_input(struct bttv *btv, int input)
986 return audio_mux(btv, input, btv->mute);
990 i2c_vidiocschan(struct bttv *btv)
992 struct video_channel c;
994 memset(&c,0,sizeof(c));
995 c.norm = btv->tvnorm;
996 c.channel = btv->input;
997 bttv_call_i2c_clients(btv,VIDIOCSCHAN,&c);
998 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM)
999 bttv_tda9880_setnorm(btv,c.norm);
1003 set_tvnorm(struct bttv *btv, unsigned int norm)
1005 const struct bttv_tvnorm *tvnorm;
1007 if (norm < 0 || norm >= BTTV_TVNORMS)
1011 tvnorm = &bttv_tvnorms[norm];
1013 btwrite(tvnorm->adelay, BT848_ADELAY);
1014 btwrite(tvnorm->bdelay, BT848_BDELAY);
1015 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1017 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1018 btwrite(1, BT848_VBI_PACK_DEL);
1019 bt848A_set_timing(btv);
1021 switch (btv->c.type) {
1022 case BTTV_BOARD_VOODOOTV_FM:
1023 bttv_tda9880_setnorm(btv,norm);
1030 set_input(struct bttv *btv, unsigned int input)
1032 unsigned long flags;
1036 spin_lock_irqsave(&btv->s_lock,flags);
1037 if (btv->curr.frame_irq) {
1038 /* active capture -> delayed input switch */
1039 btv->new_input = input;
1041 video_mux(btv,input);
1043 spin_unlock_irqrestore(&btv->s_lock,flags);
1045 video_mux(btv,input);
1047 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1048 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
1049 set_tvnorm(btv,btv->tvnorm);
1050 i2c_vidiocschan(btv);
1053 static void init_irqreg(struct bttv *btv)
1056 btwrite(0xfffffUL, BT848_INT_STAT);
1058 if (bttv_tvcards[btv->c.type].no_video) {
1060 btwrite(BT848_INT_I2CDONE,
1064 btwrite((btv->triton1) |
1065 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1067 (fdsr ? BT848_INT_FDSR : 0) |
1068 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1069 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1075 static void init_bt848(struct bttv *btv)
1079 if (bttv_tvcards[btv->c.type].no_video) {
1080 /* very basic init only */
1085 btwrite(0x00, BT848_CAP_CTL);
1086 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1087 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1089 /* set planar and packed mode trigger points and */
1090 /* set rising edge of inverted GPINTR pin as irq trigger */
1091 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1092 BT848_GPIO_DMA_CTL_PLTP1_16|
1093 BT848_GPIO_DMA_CTL_PLTP23_16|
1094 BT848_GPIO_DMA_CTL_GPINTC|
1095 BT848_GPIO_DMA_CTL_GPINTI,
1096 BT848_GPIO_DMA_CTL);
1098 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1099 btwrite(val, BT848_E_SCLOOP);
1100 btwrite(val, BT848_O_SCLOOP);
1102 btwrite(0x20, BT848_E_VSCALE_HI);
1103 btwrite(0x20, BT848_O_VSCALE_HI);
1104 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1107 btwrite(whitecrush_upper, BT848_WC_UP);
1108 btwrite(whitecrush_lower, BT848_WC_DOWN);
1110 if (btv->opt_lumafilter) {
1111 btwrite(0, BT848_E_CONTROL);
1112 btwrite(0, BT848_O_CONTROL);
1114 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1115 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1118 bt848_bright(btv, btv->bright);
1119 bt848_hue(btv, btv->hue);
1120 bt848_contrast(btv, btv->contrast);
1121 bt848_sat(btv, btv->saturation);
1127 static void bttv_reinit_bt848(struct bttv *btv)
1129 unsigned long flags;
1132 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1133 spin_lock_irqsave(&btv->s_lock,flags);
1135 bttv_set_dma(btv,0);
1136 spin_unlock_irqrestore(&btv->s_lock,flags);
1139 btv->pll.pll_current = -1;
1140 set_input(btv,btv->input);
1143 static int get_control(struct bttv *btv, struct v4l2_control *c)
1145 struct video_audio va;
1148 for (i = 0; i < BTTV_CTLS; i++)
1149 if (bttv_ctls[i].id == c->id)
1153 if (i >= 4 && i <= 8) {
1154 memset(&va,0,sizeof(va));
1155 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1156 if (btv->audio_hook)
1157 btv->audio_hook(btv,&va,0);
1160 case V4L2_CID_BRIGHTNESS:
1161 c->value = btv->bright;
1164 c->value = btv->hue;
1166 case V4L2_CID_CONTRAST:
1167 c->value = btv->contrast;
1169 case V4L2_CID_SATURATION:
1170 c->value = btv->saturation;
1173 case V4L2_CID_AUDIO_MUTE:
1174 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1176 case V4L2_CID_AUDIO_VOLUME:
1177 c->value = va.volume;
1179 case V4L2_CID_AUDIO_BALANCE:
1180 c->value = va.balance;
1182 case V4L2_CID_AUDIO_BASS:
1185 case V4L2_CID_AUDIO_TREBLE:
1186 c->value = va.treble;
1189 case V4L2_CID_PRIVATE_CHROMA_AGC:
1190 c->value = btv->opt_chroma_agc;
1192 case V4L2_CID_PRIVATE_COMBFILTER:
1193 c->value = btv->opt_combfilter;
1195 case V4L2_CID_PRIVATE_LUMAFILTER:
1196 c->value = btv->opt_lumafilter;
1198 case V4L2_CID_PRIVATE_AUTOMUTE:
1199 c->value = btv->opt_automute;
1201 case V4L2_CID_PRIVATE_AGC_CRUSH:
1202 c->value = btv->opt_adc_crush;
1204 case V4L2_CID_PRIVATE_VCR_HACK:
1205 c->value = btv->opt_vcr_hack;
1207 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1208 c->value = btv->opt_whitecrush_upper;
1210 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1211 c->value = btv->opt_whitecrush_lower;
1213 case V4L2_CID_PRIVATE_UV_RATIO:
1214 c->value = btv->opt_uv_ratio;
1216 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1217 c->value = btv->opt_full_luma_range;
1219 case V4L2_CID_PRIVATE_CORING:
1220 c->value = btv->opt_coring;
1228 static int set_control(struct bttv *btv, struct v4l2_control *c)
1230 struct video_audio va;
1233 for (i = 0; i < BTTV_CTLS; i++)
1234 if (bttv_ctls[i].id == c->id)
1238 if (i >= 4 && i <= 8) {
1239 memset(&va,0,sizeof(va));
1240 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1241 if (btv->audio_hook)
1242 btv->audio_hook(btv,&va,0);
1245 case V4L2_CID_BRIGHTNESS:
1246 bt848_bright(btv,c->value);
1249 bt848_hue(btv,c->value);
1251 case V4L2_CID_CONTRAST:
1252 bt848_contrast(btv,c->value);
1254 case V4L2_CID_SATURATION:
1255 bt848_sat(btv,c->value);
1257 case V4L2_CID_AUDIO_MUTE:
1259 va.flags |= VIDEO_AUDIO_MUTE;
1262 va.flags &= ~VIDEO_AUDIO_MUTE;
1267 case V4L2_CID_AUDIO_VOLUME:
1268 va.volume = c->value;
1270 case V4L2_CID_AUDIO_BALANCE:
1271 va.balance = c->value;
1273 case V4L2_CID_AUDIO_BASS:
1276 case V4L2_CID_AUDIO_TREBLE:
1277 va.treble = c->value;
1280 case V4L2_CID_PRIVATE_CHROMA_AGC:
1281 btv->opt_chroma_agc = c->value;
1282 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1283 btwrite(val, BT848_E_SCLOOP);
1284 btwrite(val, BT848_O_SCLOOP);
1286 case V4L2_CID_PRIVATE_COMBFILTER:
1287 btv->opt_combfilter = c->value;
1289 case V4L2_CID_PRIVATE_LUMAFILTER:
1290 btv->opt_lumafilter = c->value;
1291 if (btv->opt_lumafilter) {
1292 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1293 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1295 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1296 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1299 case V4L2_CID_PRIVATE_AUTOMUTE:
1300 btv->opt_automute = c->value;
1302 case V4L2_CID_PRIVATE_AGC_CRUSH:
1303 btv->opt_adc_crush = c->value;
1304 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1307 case V4L2_CID_PRIVATE_VCR_HACK:
1308 btv->opt_vcr_hack = c->value;
1310 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1311 btv->opt_whitecrush_upper = c->value;
1312 btwrite(c->value, BT848_WC_UP);
1314 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1315 btv->opt_whitecrush_lower = c->value;
1316 btwrite(c->value, BT848_WC_DOWN);
1318 case V4L2_CID_PRIVATE_UV_RATIO:
1319 btv->opt_uv_ratio = c->value;
1320 bt848_sat(btv, btv->saturation);
1322 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1323 btv->opt_full_luma_range = c->value;
1324 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1326 case V4L2_CID_PRIVATE_CORING:
1327 btv->opt_coring = c->value;
1328 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1333 if (i >= 4 && i <= 8) {
1334 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1335 if (btv->audio_hook)
1336 btv->audio_hook(btv,&va,1);
1341 /* ----------------------------------------------------------------------- */
1343 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1345 unsigned int outbits, data;
1346 outbits = btread(BT848_GPIO_OUT_EN);
1347 data = btread(BT848_GPIO_DATA);
1348 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1349 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1352 static void bttv_field_count(struct bttv *btv)
1360 /* start field counter */
1361 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1363 /* stop field counter */
1364 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1365 btv->field_count = 0;
1369 static const struct bttv_format*
1370 format_by_palette(int palette)
1374 for (i = 0; i < BTTV_FORMATS; i++) {
1375 if (-1 == bttv_formats[i].palette)
1377 if (bttv_formats[i].palette == palette)
1378 return bttv_formats+i;
1383 static const struct bttv_format*
1384 format_by_fourcc(int fourcc)
1388 for (i = 0; i < BTTV_FORMATS; i++) {
1389 if (-1 == bttv_formats[i].fourcc)
1391 if (bttv_formats[i].fourcc == fourcc)
1392 return bttv_formats+i;
1397 /* ----------------------------------------------------------------------- */
1401 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1402 struct bttv_buffer *new)
1404 struct bttv_buffer *old;
1405 unsigned long flags;
1408 dprintk("switch_overlay: enter [new=%p]\n",new);
1410 new->vb.state = STATE_DONE;
1411 spin_lock_irqsave(&btv->s_lock,flags);
1415 bttv_set_dma(btv, 0x03);
1416 spin_unlock_irqrestore(&btv->s_lock,flags);
1418 free_btres(btv,fh,RESOURCE_OVERLAY);
1420 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1421 bttv_dma_free(&fh->cap,btv, old);
1424 dprintk("switch_overlay: done\n");
1428 /* ----------------------------------------------------------------------- */
1429 /* video4linux (1) interface */
1431 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1432 struct bttv_buffer *buf,
1433 const struct bttv_format *fmt,
1434 unsigned int width, unsigned int height,
1435 enum v4l2_field field)
1437 int redo_dma_risc = 0;
1440 /* check settings */
1443 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1445 height = RAW_LINES*2;
1446 if (width*height > buf->vb.bsize)
1448 buf->vb.size = buf->vb.bsize;
1452 width > bttv_tvnorms[btv->tvnorm].swidth ||
1453 height > bttv_tvnorms[btv->tvnorm].sheight)
1455 buf->vb.size = (width * height * fmt->depth) >> 3;
1456 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1460 /* alloc + fill struct bttv_buffer (if changed) */
1461 if (buf->vb.width != width || buf->vb.height != height ||
1462 buf->vb.field != field ||
1463 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1464 buf->vb.width = width;
1465 buf->vb.height = height;
1466 buf->vb.field = field;
1467 buf->tvnorm = btv->tvnorm;
1472 /* alloc risc memory */
1473 if (STATE_NEEDS_INIT == buf->vb.state) {
1475 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1480 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1483 buf->vb.state = STATE_PREPARED;
1487 bttv_dma_free(q,btv,buf);
1492 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1494 struct bttv_fh *fh = q->priv_data;
1496 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1499 while (*size * *count > gbuffers * gbufsize)
1505 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1506 enum v4l2_field field)
1508 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1509 struct bttv_fh *fh = q->priv_data;
1511 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1512 fh->width, fh->height, field);
1516 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1518 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1519 struct bttv_fh *fh = q->priv_data;
1520 struct bttv *btv = fh->btv;
1522 buf->vb.state = STATE_QUEUED;
1523 list_add_tail(&buf->vb.queue,&btv->capture);
1524 if (!btv->curr.frame_irq) {
1526 bttv_set_dma(btv, 0x03);
1530 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1532 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1533 struct bttv_fh *fh = q->priv_data;
1535 bttv_dma_free(&fh->cap,fh->btv,buf);
1538 static struct videobuf_queue_ops bttv_video_qops = {
1539 .buf_setup = buffer_setup,
1540 .buf_prepare = buffer_prepare,
1541 .buf_queue = buffer_queue,
1542 .buf_release = buffer_release,
1545 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1549 return BTTV_VERSION_CODE;
1551 /* *** v4l1 *** ************************************************ */
1554 unsigned long *freq = arg;
1560 unsigned long *freq = arg;
1561 mutex_lock(&btv->lock);
1563 bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
1564 if (btv->has_matchbox && btv->radio_user)
1565 tea5757_set_freq(btv,*freq);
1566 mutex_unlock(&btv->lock);
1572 struct video_tuner *v = arg;
1574 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1576 if (v->tuner) /* Only tuner 0 */
1578 strcpy(v->name, "Television");
1580 v->rangehigh = 0x7FFFFFFF;
1581 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1582 v->mode = btv->tvnorm;
1583 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1584 bttv_call_i2c_clients(btv,cmd,v);
1589 struct video_tuner *v = arg;
1591 if (v->tuner) /* Only tuner 0 */
1593 if (v->mode >= BTTV_TVNORMS)
1596 mutex_lock(&btv->lock);
1597 set_tvnorm(btv,v->mode);
1598 bttv_call_i2c_clients(btv,cmd,v);
1599 mutex_unlock(&btv->lock);
1605 struct video_channel *v = arg;
1606 unsigned int channel = v->channel;
1608 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1611 v->flags = VIDEO_VC_AUDIO;
1612 v->type = VIDEO_TYPE_CAMERA;
1613 v->norm = btv->tvnorm;
1614 if (channel == bttv_tvcards[btv->c.type].tuner) {
1615 strcpy(v->name,"Television");
1616 v->flags|=VIDEO_VC_TUNER;
1617 v->type=VIDEO_TYPE_TV;
1619 } else if (channel == btv->svhs) {
1620 strcpy(v->name,"S-Video");
1622 sprintf(v->name,"Composite%d",channel);
1628 struct video_channel *v = arg;
1629 unsigned int channel = v->channel;
1631 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1633 if (v->norm >= BTTV_TVNORMS)
1636 mutex_lock(&btv->lock);
1637 if (channel == btv->input &&
1638 v->norm == btv->tvnorm) {
1640 mutex_unlock(&btv->lock);
1644 btv->tvnorm = v->norm;
1645 set_input(btv,v->channel);
1646 mutex_unlock(&btv->lock);
1652 struct video_audio *v = arg;
1654 memset(v,0,sizeof(*v));
1655 strcpy(v->name,"Television");
1656 v->flags |= VIDEO_AUDIO_MUTABLE;
1657 v->mode = VIDEO_SOUND_MONO;
1659 mutex_lock(&btv->lock);
1660 bttv_call_i2c_clients(btv,cmd,v);
1662 /* card specific hooks */
1663 if (btv->audio_hook)
1664 btv->audio_hook(btv,v,0);
1666 mutex_unlock(&btv->lock);
1671 struct video_audio *v = arg;
1672 unsigned int audio = v->audio;
1674 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1677 mutex_lock(&btv->lock);
1678 audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
1679 bttv_call_i2c_clients(btv,cmd,v);
1681 /* card specific hooks */
1682 if (btv->audio_hook)
1683 btv->audio_hook(btv,v,1);
1685 mutex_unlock(&btv->lock);
1689 /* *** v4l2 *** ************************************************ */
1690 case VIDIOC_ENUMSTD:
1692 struct v4l2_standard *e = arg;
1693 unsigned int index = e->index;
1695 if (index >= BTTV_TVNORMS)
1697 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1698 bttv_tvnorms[e->index].name);
1704 v4l2_std_id *id = arg;
1705 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1710 v4l2_std_id *id = arg;
1713 for (i = 0; i < BTTV_TVNORMS; i++)
1714 if (*id & bttv_tvnorms[i].v4l2_id)
1716 if (i == BTTV_TVNORMS)
1719 mutex_lock(&btv->lock);
1721 i2c_vidiocschan(btv);
1722 mutex_unlock(&btv->lock);
1725 case VIDIOC_QUERYSTD:
1727 v4l2_std_id *id = arg;
1729 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1730 *id = V4L2_STD_625_50;
1732 *id = V4L2_STD_525_60;
1736 case VIDIOC_ENUMINPUT:
1738 struct v4l2_input *i = arg;
1742 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1744 memset(i,0,sizeof(*i));
1746 i->type = V4L2_INPUT_TYPE_CAMERA;
1748 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1749 sprintf(i->name, "Television");
1750 i->type = V4L2_INPUT_TYPE_TUNER;
1752 } else if (i->index == btv->svhs) {
1753 sprintf(i->name, "S-Video");
1755 sprintf(i->name,"Composite%d",i->index);
1757 if (i->index == btv->input) {
1758 __u32 dstatus = btread(BT848_DSTATUS);
1759 if (0 == (dstatus & BT848_DSTATUS_PRES))
1760 i->status |= V4L2_IN_ST_NO_SIGNAL;
1761 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1762 i->status |= V4L2_IN_ST_NO_H_LOCK;
1764 for (n = 0; n < BTTV_TVNORMS; n++)
1765 i->std |= bttv_tvnorms[n].v4l2_id;
1768 case VIDIOC_G_INPUT:
1774 case VIDIOC_S_INPUT:
1776 unsigned int *i = arg;
1778 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1780 mutex_lock(&btv->lock);
1782 mutex_unlock(&btv->lock);
1786 case VIDIOC_G_TUNER:
1788 struct v4l2_tuner *t = arg;
1790 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1794 mutex_lock(&btv->lock);
1795 memset(t,0,sizeof(*t));
1796 strcpy(t->name, "Television");
1797 t->type = V4L2_TUNER_ANALOG_TV;
1798 t->capability = V4L2_TUNER_CAP_NORM;
1799 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1800 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1803 struct video_tuner tuner;
1805 memset(&tuner, 0, sizeof (tuner));
1806 tuner.rangehigh = 0xffffffffUL;
1807 bttv_call_i2c_clients(btv, VIDIOCGTUNER, &tuner);
1808 t->rangelow = tuner.rangelow;
1809 t->rangehigh = tuner.rangehigh;
1813 struct video_audio va;
1814 memset(&va, 0, sizeof(struct video_audio));
1815 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1816 if (btv->audio_hook)
1817 btv->audio_hook(btv,&va,0);
1818 if(va.mode & VIDEO_SOUND_STEREO) {
1819 t->audmode = V4L2_TUNER_MODE_STEREO;
1820 t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1822 if(va.mode & VIDEO_SOUND_LANG1) {
1823 t->audmode = V4L2_TUNER_MODE_LANG1;
1824 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1825 | V4L2_TUNER_SUB_LANG2;
1828 /* FIXME: fill capability+audmode */
1829 mutex_unlock(&btv->lock);
1832 case VIDIOC_S_TUNER:
1834 struct v4l2_tuner *t = arg;
1836 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1840 mutex_lock(&btv->lock);
1842 struct video_audio va;
1843 memset(&va, 0, sizeof(struct video_audio));
1844 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
1845 if (t->audmode == V4L2_TUNER_MODE_MONO)
1846 va.mode = VIDEO_SOUND_MONO;
1847 else if (t->audmode == V4L2_TUNER_MODE_STEREO)
1848 va.mode = VIDEO_SOUND_STEREO;
1849 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1850 va.mode = VIDEO_SOUND_LANG1;
1851 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1852 va.mode = VIDEO_SOUND_LANG2;
1853 bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
1854 if (btv->audio_hook)
1855 btv->audio_hook(btv,&va,1);
1857 mutex_unlock(&btv->lock);
1861 case VIDIOC_G_FREQUENCY:
1863 struct v4l2_frequency *f = arg;
1865 memset(f,0,sizeof(*f));
1866 f->type = V4L2_TUNER_ANALOG_TV;
1867 f->frequency = btv->freq;
1870 case VIDIOC_S_FREQUENCY:
1872 struct v4l2_frequency *f = arg;
1874 if (unlikely(f->tuner != 0))
1876 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
1878 mutex_lock(&btv->lock);
1879 btv->freq = f->frequency;
1880 bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
1881 if (btv->has_matchbox && btv->radio_user)
1882 tea5757_set_freq(btv,btv->freq);
1883 mutex_unlock(&btv->lock);
1886 case VIDIOC_LOG_STATUS:
1888 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
1893 return -ENOIOCTLCMD;
1899 static int verify_window(const struct bttv_tvnorm *tvn,
1900 struct v4l2_window *win, int fixup)
1902 enum v4l2_field field;
1905 if (win->w.width < 48 || win->w.height < 32)
1907 if (win->clipcount > 2048)
1912 maxh = tvn->sheight;
1914 if (V4L2_FIELD_ANY == field) {
1915 field = (win->w.height > maxh/2)
1916 ? V4L2_FIELD_INTERLACED
1920 case V4L2_FIELD_TOP:
1921 case V4L2_FIELD_BOTTOM:
1924 case V4L2_FIELD_INTERLACED:
1930 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
1933 if (win->w.width > maxw)
1934 win->w.width = maxw;
1935 if (win->w.height > maxh)
1936 win->w.height = maxh;
1941 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
1942 struct v4l2_window *win, int fixup)
1944 struct v4l2_clip *clips = NULL;
1945 int n,size,retval = 0;
1947 if (NULL == fh->ovfmt)
1949 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
1951 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
1955 /* copy clips -- luckily v4l1 + v4l2 are binary
1956 compatible here ...*/
1958 size = sizeof(*clips)*(n+4);
1959 clips = kmalloc(size,GFP_KERNEL);
1963 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1968 /* clip against screen */
1969 if (NULL != btv->fbuf.base)
1970 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
1972 btcx_sort_clips(clips,n);
1974 /* 4-byte alignments */
1975 switch (fh->ovfmt->depth) {
1978 btcx_align(&win->w, clips, n, 3);
1981 btcx_align(&win->w, clips, n, 1);
1984 /* no alignment fixups needed */
1990 mutex_lock(&fh->cap.lock);
1991 kfree(fh->ov.clips);
1992 fh->ov.clips = clips;
1996 fh->ov.field = win->field;
1997 fh->ov.setup_ok = 1;
1998 btv->init.ov.w.width = win->w.width;
1999 btv->init.ov.w.height = win->w.height;
2000 btv->init.ov.field = win->field;
2002 /* update overlay if needed */
2004 if (check_btres(fh, RESOURCE_OVERLAY)) {
2005 struct bttv_buffer *new;
2007 new = videobuf_alloc(sizeof(*new));
2008 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2009 retval = bttv_switch_overlay(btv,fh,new);
2011 mutex_unlock(&fh->cap.lock);
2015 /* ----------------------------------------------------------------------- */
2017 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2019 struct videobuf_queue* q = NULL;
2022 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2025 case V4L2_BUF_TYPE_VBI_CAPTURE:
2034 static int bttv_resource(struct bttv_fh *fh)
2039 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2040 res = RESOURCE_VIDEO;
2042 case V4L2_BUF_TYPE_VBI_CAPTURE:
2051 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2053 struct videobuf_queue *q = bttv_queue(fh);
2054 int res = bttv_resource(fh);
2056 if (check_btres(fh,res))
2058 if (videobuf_queue_is_busy(q))
2065 pix_format_set_size (struct v4l2_pix_format * f,
2066 const struct bttv_format * fmt,
2068 unsigned int height)
2073 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2074 f->bytesperline = width; /* Y plane */
2075 f->sizeimage = (width * height * fmt->depth) >> 3;
2077 f->bytesperline = (width * fmt->depth) >> 3;
2078 f->sizeimage = height * f->bytesperline;
2082 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2085 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2086 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2087 pix_format_set_size (&f->fmt.pix, fh->fmt,
2088 fh->width, fh->height);
2089 f->fmt.pix.field = fh->cap.field;
2090 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2092 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2093 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2094 f->fmt.win.w = fh->ov.w;
2095 f->fmt.win.field = fh->ov.field;
2097 case V4L2_BUF_TYPE_VBI_CAPTURE:
2098 bttv_vbi_get_fmt(fh,f);
2105 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2106 struct v4l2_format *f)
2109 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2111 const struct bttv_format *fmt;
2112 enum v4l2_field field;
2113 unsigned int maxw,maxh;
2115 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2120 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2121 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2122 field = f->fmt.pix.field;
2123 if (V4L2_FIELD_ANY == field)
2124 field = (f->fmt.pix.height > maxh/2)
2125 ? V4L2_FIELD_INTERLACED
2126 : V4L2_FIELD_BOTTOM;
2127 if (V4L2_FIELD_SEQ_BT == field)
2128 field = V4L2_FIELD_SEQ_TB;
2130 case V4L2_FIELD_TOP:
2131 case V4L2_FIELD_BOTTOM:
2132 case V4L2_FIELD_ALTERNATE:
2135 case V4L2_FIELD_INTERLACED:
2137 case V4L2_FIELD_SEQ_TB:
2138 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2145 /* update data for the application */
2146 f->fmt.pix.field = field;
2147 if (f->fmt.pix.width < 48)
2148 f->fmt.pix.width = 48;
2149 if (f->fmt.pix.height < 32)
2150 f->fmt.pix.height = 32;
2151 if (f->fmt.pix.width > maxw)
2152 f->fmt.pix.width = maxw;
2153 if (f->fmt.pix.height > maxh)
2154 f->fmt.pix.height = maxh;
2155 pix_format_set_size (&f->fmt.pix, fmt,
2156 f->fmt.pix.width & ~3,
2161 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2162 return verify_window(&bttv_tvnorms[btv->tvnorm],
2164 case V4L2_BUF_TYPE_VBI_CAPTURE:
2165 bttv_vbi_try_fmt(fh,f);
2172 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2173 struct v4l2_format *f)
2178 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2180 const struct bttv_format *fmt;
2182 retval = bttv_switch_type(fh,f->type);
2185 retval = bttv_try_fmt(fh,btv,f);
2188 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2190 /* update our state informations */
2191 mutex_lock(&fh->cap.lock);
2193 fh->cap.field = f->fmt.pix.field;
2194 fh->cap.last = V4L2_FIELD_NONE;
2195 fh->width = f->fmt.pix.width;
2196 fh->height = f->fmt.pix.height;
2197 btv->init.fmt = fmt;
2198 btv->init.width = f->fmt.pix.width;
2199 btv->init.height = f->fmt.pix.height;
2200 mutex_unlock(&fh->cap.lock);
2204 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2205 if (no_overlay > 0) {
2206 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2209 return setup_window(fh, btv, &f->fmt.win, 1);
2210 case V4L2_BUF_TYPE_VBI_CAPTURE:
2211 retval = bttv_switch_type(fh,f->type);
2214 if (locked_btres(fh->btv, RESOURCE_VBI))
2216 bttv_vbi_try_fmt(fh,f);
2217 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2218 bttv_vbi_get_fmt(fh,f);
2225 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2226 unsigned int cmd, void *arg)
2228 struct bttv_fh *fh = file->private_data;
2229 struct bttv *btv = fh->btv;
2230 unsigned long flags;
2234 v4l_print_ioctl(btv->c.name, cmd);
2237 bttv_reinit_bt848(btv);
2245 case VIDIOC_S_INPUT:
2246 case VIDIOC_S_TUNER:
2247 case VIDIOC_S_FREQUENCY:
2248 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2255 /* *** v4l1 *** ************************************************ */
2258 struct video_capability *cap = arg;
2260 memset(cap,0,sizeof(*cap));
2261 strcpy(cap->name,btv->video_dev->name);
2262 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2264 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2267 cap->type = VID_TYPE_CAPTURE|
2271 if (no_overlay <= 0)
2272 cap->type |= VID_TYPE_OVERLAY;
2274 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2275 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2277 cap->minheight = 32;
2279 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2280 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2286 struct video_picture *pic = arg;
2288 memset(pic,0,sizeof(*pic));
2289 pic->brightness = btv->bright;
2290 pic->contrast = btv->contrast;
2291 pic->hue = btv->hue;
2292 pic->colour = btv->saturation;
2294 pic->depth = fh->fmt->depth;
2295 pic->palette = fh->fmt->palette;
2301 struct video_picture *pic = arg;
2302 const struct bttv_format *fmt;
2304 fmt = format_by_palette(pic->palette);
2307 mutex_lock(&fh->cap.lock);
2308 if (fmt->depth != pic->depth) {
2310 goto fh_unlock_and_return;
2312 if (fmt->flags & FORMAT_FLAGS_RAW) {
2313 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2314 RAW_LINES * 2. F1 is stored at offset 0, F2
2315 at buffer size / 2. */
2316 fh->width = RAW_BPL;
2317 fh->height = gbufsize / RAW_BPL;
2318 btv->init.width = RAW_BPL;
2319 btv->init.height = gbufsize / RAW_BPL;
2323 btv->init.ovfmt = fmt;
2324 btv->init.fmt = fmt;
2326 /* dirty hack time: swap bytes for overlay if the
2327 display adaptor is big endian (insmod option) */
2328 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2329 fmt->palette == VIDEO_PALETTE_RGB565 ||
2330 fmt->palette == VIDEO_PALETTE_RGB32) {
2334 bt848_bright(btv,pic->brightness);
2335 bt848_contrast(btv,pic->contrast);
2336 bt848_hue(btv,pic->hue);
2337 bt848_sat(btv,pic->colour);
2338 mutex_unlock(&fh->cap.lock);
2344 struct video_window *win = arg;
2346 memset(win,0,sizeof(*win));
2347 win->x = fh->ov.w.left;
2348 win->y = fh->ov.w.top;
2349 win->width = fh->ov.w.width;
2350 win->height = fh->ov.w.height;
2355 struct video_window *win = arg;
2356 struct v4l2_window w2;
2358 if (no_overlay > 0) {
2359 printk ("VIDIOCSWIN: no_overlay\n");
2363 w2.field = V4L2_FIELD_ANY;
2366 w2.w.width = win->width;
2367 w2.w.height = win->height;
2368 w2.clipcount = win->clipcount;
2369 w2.clips = (struct v4l2_clip __user *)win->clips;
2370 retval = setup_window(fh, btv, &w2, 0);
2372 /* on v4l1 this ioctl affects the read() size too */
2373 fh->width = fh->ov.w.width;
2374 fh->height = fh->ov.w.height;
2375 btv->init.width = fh->ov.w.width;
2376 btv->init.height = fh->ov.w.height;
2383 struct video_buffer *fbuf = arg;
2385 fbuf->base = btv->fbuf.base;
2386 fbuf->width = btv->fbuf.fmt.width;
2387 fbuf->height = btv->fbuf.fmt.height;
2388 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2390 fbuf->depth = fh->ovfmt->depth;
2395 struct video_buffer *fbuf = arg;
2396 const struct bttv_format *fmt;
2399 if(!capable(CAP_SYS_ADMIN) &&
2400 !capable(CAP_SYS_RAWIO))
2402 end = (unsigned long)fbuf->base +
2403 fbuf->height * fbuf->bytesperline;
2404 mutex_lock(&fh->cap.lock);
2407 switch (fbuf->depth) {
2409 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2412 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2415 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2418 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2422 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2429 goto fh_unlock_and_return;
2433 btv->init.ovfmt = fmt;
2434 btv->init.fmt = fmt;
2435 btv->fbuf.base = fbuf->base;
2436 btv->fbuf.fmt.width = fbuf->width;
2437 btv->fbuf.fmt.height = fbuf->height;
2438 if (fbuf->bytesperline)
2439 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2441 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2442 mutex_unlock(&fh->cap.lock);
2447 case VIDIOC_OVERLAY:
2449 struct bttv_buffer *new;
2454 if (NULL == btv->fbuf.base)
2456 if (!fh->ov.setup_ok) {
2457 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2462 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2465 mutex_lock(&fh->cap.lock);
2467 fh->ov.tvnorm = btv->tvnorm;
2468 new = videobuf_alloc(sizeof(*new));
2469 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2475 retval = bttv_switch_overlay(btv,fh,new);
2476 mutex_unlock(&fh->cap.lock);
2482 struct video_mbuf *mbuf = arg;
2485 mutex_lock(&fh->cap.lock);
2486 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2489 goto fh_unlock_and_return;
2490 memset(mbuf,0,sizeof(*mbuf));
2491 mbuf->frames = gbuffers;
2492 mbuf->size = gbuffers * gbufsize;
2493 for (i = 0; i < gbuffers; i++)
2494 mbuf->offsets[i] = i * gbufsize;
2495 mutex_unlock(&fh->cap.lock);
2498 case VIDIOCMCAPTURE:
2500 struct video_mmap *vm = arg;
2501 struct bttv_buffer *buf;
2502 enum v4l2_field field;
2504 if (vm->frame >= VIDEO_MAX_FRAME)
2507 mutex_lock(&fh->cap.lock);
2509 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2511 goto fh_unlock_and_return;
2512 if (0 == buf->vb.baddr)
2513 goto fh_unlock_and_return;
2514 if (buf->vb.state == STATE_QUEUED ||
2515 buf->vb.state == STATE_ACTIVE)
2516 goto fh_unlock_and_return;
2518 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2519 ? V4L2_FIELD_INTERLACED
2520 : V4L2_FIELD_BOTTOM;
2521 retval = bttv_prepare_buffer(&fh->cap,btv,buf,
2522 format_by_palette(vm->format),
2523 vm->width,vm->height,field);
2525 goto fh_unlock_and_return;
2526 spin_lock_irqsave(&btv->s_lock,flags);
2527 buffer_queue(&fh->cap,&buf->vb);
2528 spin_unlock_irqrestore(&btv->s_lock,flags);
2529 mutex_unlock(&fh->cap.lock);
2535 struct bttv_buffer *buf;
2537 if (*frame >= VIDEO_MAX_FRAME)
2540 mutex_lock(&fh->cap.lock);
2542 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2544 goto fh_unlock_and_return;
2545 retval = videobuf_waiton(&buf->vb,0,1);
2547 goto fh_unlock_and_return;
2548 switch (buf->vb.state) {
2553 videobuf_dma_sync(&fh->cap,&buf->vb.dma);
2554 bttv_dma_free(&fh->cap,btv,buf);
2560 mutex_unlock(&fh->cap.lock);
2566 struct vbi_format *fmt = (void *) arg;
2567 struct v4l2_format fmt2;
2569 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2570 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2574 bttv_vbi_get_fmt(fh, &fmt2);
2576 memset(fmt,0,sizeof(*fmt));
2577 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2578 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2579 fmt->sample_format = VIDEO_PALETTE_RAW;
2580 fmt->start[0] = fmt2.fmt.vbi.start[0];
2581 fmt->count[0] = fmt2.fmt.vbi.count[0];
2582 fmt->start[1] = fmt2.fmt.vbi.start[1];
2583 fmt->count[1] = fmt2.fmt.vbi.count[1];
2584 if (fmt2.fmt.vbi.flags & V4L2_VBI_UNSYNC)
2585 fmt->flags |= VBI_UNSYNC;
2586 if (fmt2.fmt.vbi.flags & V4L2_VBI_INTERLACED)
2587 fmt->flags |= VBI_INTERLACED;
2592 struct vbi_format *fmt = (void *) arg;
2593 struct v4l2_format fmt2;
2595 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2598 bttv_vbi_get_fmt(fh, &fmt2);
2600 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2601 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2602 fmt->sample_format != VIDEO_PALETTE_RAW ||
2603 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2604 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2605 fmt->count[0] != fmt->count[1] ||
2606 fmt->count[0] < 1 ||
2607 fmt->count[0] > 32 /* VBI_MAXLINES */)
2610 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2623 return bttv_common_ioctls(btv,cmd,arg);
2625 /* *** v4l2 *** ************************************************ */
2626 case VIDIOC_QUERYCAP:
2628 struct v4l2_capability *cap = arg;
2632 memset(cap, 0, sizeof (*cap));
2633 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
2634 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
2635 snprintf(cap->bus_info, sizeof (cap->bus_info),
2636 "PCI:%s", pci_name(btv->c.pci));
2637 cap->version = BTTV_VERSION_CODE;
2639 V4L2_CAP_VIDEO_CAPTURE |
2640 V4L2_CAP_VBI_CAPTURE |
2641 V4L2_CAP_READWRITE |
2643 if (no_overlay <= 0)
2644 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2646 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2647 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2648 cap->capabilities |= V4L2_CAP_TUNER;
2652 case VIDIOC_ENUM_FMT:
2654 struct v4l2_fmtdesc *f = arg;
2655 enum v4l2_buf_type type;
2660 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2665 memset(f,0,sizeof(*f));
2668 f->pixelformat = V4L2_PIX_FMT_GREY;
2669 strcpy(f->description,"vbi data");
2673 /* video capture + overlay */
2675 for (i = 0; i < BTTV_FORMATS; i++) {
2676 if (bttv_formats[i].fourcc != -1)
2678 if ((unsigned int)index == f->index)
2681 if (BTTV_FORMATS == i)
2685 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2687 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2688 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2694 memset(f,0,sizeof(*f));
2697 f->pixelformat = bttv_formats[i].fourcc;
2698 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2702 case VIDIOC_TRY_FMT:
2704 struct v4l2_format *f = arg;
2705 return bttv_try_fmt(fh,btv,f);
2709 struct v4l2_format *f = arg;
2710 return bttv_g_fmt(fh,f);
2714 struct v4l2_format *f = arg;
2715 return bttv_s_fmt(fh,btv,f);
2720 struct v4l2_framebuffer *fb = arg;
2723 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2725 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2730 struct v4l2_framebuffer *fb = arg;
2731 const struct bttv_format *fmt;
2733 if(!capable(CAP_SYS_ADMIN) &&
2734 !capable(CAP_SYS_RAWIO))
2738 fmt = format_by_fourcc(fb->fmt.pixelformat);
2741 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2744 mutex_lock(&fh->cap.lock);
2746 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2747 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2748 goto fh_unlock_and_return;
2749 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2750 goto fh_unlock_and_return;
2754 btv->fbuf.base = fb->base;
2755 btv->fbuf.fmt.width = fb->fmt.width;
2756 btv->fbuf.fmt.height = fb->fmt.height;
2757 if (0 != fb->fmt.bytesperline)
2758 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2760 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2764 btv->init.ovfmt = fmt;
2765 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2768 fh->ov.w.width = fb->fmt.width;
2769 fh->ov.w.height = fb->fmt.height;
2770 btv->init.ov.w.width = fb->fmt.width;
2771 btv->init.ov.w.height = fb->fmt.height;
2772 kfree(fh->ov.clips);
2773 fh->ov.clips = NULL;
2776 if (check_btres(fh, RESOURCE_OVERLAY)) {
2777 struct bttv_buffer *new;
2779 new = videobuf_alloc(sizeof(*new));
2780 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2781 retval = bttv_switch_overlay(btv,fh,new);
2784 mutex_unlock(&fh->cap.lock);
2788 case VIDIOC_REQBUFS:
2789 return videobuf_reqbufs(bttv_queue(fh),arg);
2791 case VIDIOC_QUERYBUF:
2792 return videobuf_querybuf(bttv_queue(fh),arg);
2795 return videobuf_qbuf(bttv_queue(fh),arg);
2798 return videobuf_dqbuf(bttv_queue(fh),arg,
2799 file->f_flags & O_NONBLOCK);
2801 case VIDIOC_STREAMON:
2803 int res = bttv_resource(fh);
2805 if (!check_alloc_btres(btv,fh,res))
2807 return videobuf_streamon(bttv_queue(fh));
2809 case VIDIOC_STREAMOFF:
2811 int res = bttv_resource(fh);
2813 retval = videobuf_streamoff(bttv_queue(fh));
2816 free_btres(btv,fh,res);
2820 case VIDIOC_QUERYCTRL:
2822 struct v4l2_queryctrl *c = arg;
2825 if ((c->id < V4L2_CID_BASE ||
2826 c->id >= V4L2_CID_LASTP1) &&
2827 (c->id < V4L2_CID_PRIVATE_BASE ||
2828 c->id >= V4L2_CID_PRIVATE_LASTP1))
2830 for (i = 0; i < BTTV_CTLS; i++)
2831 if (bttv_ctls[i].id == c->id)
2833 if (i == BTTV_CTLS) {
2838 if (i >= 4 && i <= 8) {
2839 struct video_audio va;
2840 memset(&va,0,sizeof(va));
2841 bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
2842 if (btv->audio_hook)
2843 btv->audio_hook(btv,&va,0);
2844 switch (bttv_ctls[i].id) {
2845 case V4L2_CID_AUDIO_VOLUME:
2846 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2849 case V4L2_CID_AUDIO_BALANCE:
2850 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2853 case V4L2_CID_AUDIO_BASS:
2854 if (!(va.flags & VIDEO_AUDIO_BASS))
2857 case V4L2_CID_AUDIO_TREBLE:
2858 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2866 return get_control(btv,arg);
2868 return set_control(btv,arg);
2871 struct v4l2_streamparm *parm = arg;
2872 struct v4l2_standard s;
2873 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2875 memset(parm,0,sizeof(*parm));
2876 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2877 bttv_tvnorms[btv->tvnorm].name);
2878 parm->parm.capture.timeperframe = s.frameperiod;
2882 case VIDIOC_G_PRIORITY:
2884 enum v4l2_priority *p = arg;
2886 *p = v4l2_prio_max(&btv->prio);
2889 case VIDIOC_S_PRIORITY:
2891 enum v4l2_priority *prio = arg;
2893 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2896 case VIDIOC_ENUMSTD:
2899 case VIDIOC_ENUMINPUT:
2900 case VIDIOC_G_INPUT:
2901 case VIDIOC_S_INPUT:
2902 case VIDIOC_G_TUNER:
2903 case VIDIOC_S_TUNER:
2904 case VIDIOC_G_FREQUENCY:
2905 case VIDIOC_S_FREQUENCY:
2906 case VIDIOC_LOG_STATUS:
2907 return bttv_common_ioctls(btv,cmd,arg);
2910 return -ENOIOCTLCMD;
2914 fh_unlock_and_return:
2915 mutex_unlock(&fh->cap.lock);
2919 static int bttv_ioctl(struct inode *inode, struct file *file,
2920 unsigned int cmd, unsigned long arg)
2922 struct bttv_fh *fh = file->private_data;
2926 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2927 return fh->lines * 2 * 2048;
2929 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
2933 static ssize_t bttv_read(struct file *file, char __user *data,
2934 size_t count, loff_t *ppos)
2936 struct bttv_fh *fh = file->private_data;
2939 if (fh->btv->errors)
2940 bttv_reinit_bt848(fh->btv);
2941 dprintk("bttv%d: read count=%d type=%s\n",
2942 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
2945 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2946 if (locked_btres(fh->btv,RESOURCE_VIDEO))
2948 retval = videobuf_read_one(&fh->cap, data, count, ppos,
2949 file->f_flags & O_NONBLOCK);
2951 case V4L2_BUF_TYPE_VBI_CAPTURE:
2952 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2954 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
2955 file->f_flags & O_NONBLOCK);
2963 static unsigned int bttv_poll(struct file *file, poll_table *wait)
2965 struct bttv_fh *fh = file->private_data;
2966 struct bttv_buffer *buf;
2967 enum v4l2_field field;
2969 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2970 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
2972 return videobuf_poll_stream(file, &fh->vbi, wait);
2975 if (check_btres(fh,RESOURCE_VIDEO)) {
2976 /* streaming capture */
2977 if (list_empty(&fh->cap.stream))
2979 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
2981 /* read() capture */
2982 mutex_lock(&fh->cap.lock);
2983 if (NULL == fh->cap.read_buf) {
2984 /* need to capture a new frame */
2985 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
2986 mutex_unlock(&fh->cap.lock);
2989 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
2990 if (NULL == fh->cap.read_buf) {
2991 mutex_unlock(&fh->cap.lock);
2994 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
2995 field = videobuf_next_field(&fh->cap);
2996 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
2997 kfree (fh->cap.read_buf);
2998 fh->cap.read_buf = NULL;
2999 mutex_unlock(&fh->cap.lock);
3002 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3003 fh->cap.read_off = 0;
3005 mutex_unlock(&fh->cap.lock);
3006 buf = (struct bttv_buffer*)fh->cap.read_buf;
3009 poll_wait(file, &buf->vb.done, wait);
3010 if (buf->vb.state == STATE_DONE ||
3011 buf->vb.state == STATE_ERROR)
3012 return POLLIN|POLLRDNORM;
3016 static int bttv_open(struct inode *inode, struct file *file)
3018 int minor = iminor(inode);
3019 struct bttv *btv = NULL;
3021 enum v4l2_buf_type type = 0;
3024 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3026 for (i = 0; i < bttv_num; i++) {
3027 if (bttvs[i].video_dev &&
3028 bttvs[i].video_dev->minor == minor) {
3030 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3033 if (bttvs[i].vbi_dev &&
3034 bttvs[i].vbi_dev->minor == minor) {
3036 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3043 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3044 btv->c.nr,v4l2_type_names[type]);
3046 /* allocate per filehandle data */
3047 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3050 file->private_data = fh;
3053 fh->ov.setup_ok = 0;
3054 v4l2_prio_open(&btv->prio,&fh->prio);
3056 videobuf_queue_init(&fh->cap, &bttv_video_qops,
3057 btv->c.pci, &btv->s_lock,
3058 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3059 V4L2_FIELD_INTERLACED,
3060 sizeof(struct bttv_buffer),
3062 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3063 btv->c.pci, &btv->s_lock,
3064 V4L2_BUF_TYPE_VBI_CAPTURE,
3066 sizeof(struct bttv_buffer),
3068 i2c_vidiocschan(btv);
3071 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
3072 bttv_vbi_setlines(fh,btv,16);
3073 bttv_field_count(btv);
3077 static int bttv_release(struct inode *inode, struct file *file)
3079 struct bttv_fh *fh = file->private_data;
3080 struct bttv *btv = fh->btv;
3082 /* turn off overlay */
3083 if (check_btres(fh, RESOURCE_OVERLAY))
3084 bttv_switch_overlay(btv,fh,NULL);
3086 /* stop video capture */
3087 if (check_btres(fh, RESOURCE_VIDEO)) {
3088 videobuf_streamoff(&fh->cap);
3089 free_btres(btv,fh,RESOURCE_VIDEO);
3091 if (fh->cap.read_buf) {
3092 buffer_release(&fh->cap,fh->cap.read_buf);
3093 kfree(fh->cap.read_buf);
3096 /* stop vbi capture */
3097 if (check_btres(fh, RESOURCE_VBI)) {
3098 if (fh->vbi.streaming)
3099 videobuf_streamoff(&fh->vbi);
3100 if (fh->vbi.reading)
3101 videobuf_read_stop(&fh->vbi);
3102 free_btres(btv,fh,RESOURCE_VBI);
3106 videobuf_mmap_free(&fh->cap);
3107 videobuf_mmap_free(&fh->vbi);
3108 v4l2_prio_close(&btv->prio,&fh->prio);
3109 file->private_data = NULL;
3113 bttv_field_count(btv);
3118 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3120 struct bttv_fh *fh = file->private_data;
3122 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3123 fh->btv->c.nr, v4l2_type_names[fh->type],
3124 vma->vm_start, vma->vm_end - vma->vm_start);
3125 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3128 static struct file_operations bttv_fops =
3130 .owner = THIS_MODULE,
3132 .release = bttv_release,
3133 .ioctl = bttv_ioctl,
3134 .compat_ioctl = v4l_compat_ioctl32,
3135 .llseek = no_llseek,
3141 static struct video_device bttv_video_template =
3144 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3145 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3146 .hardware = VID_HARDWARE_BT848,
3151 static struct video_device bttv_vbi_template =
3153 .name = "bt848/878 vbi",
3154 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3155 .hardware = VID_HARDWARE_BT848,
3160 /* ----------------------------------------------------------------------- */
3161 /* radio interface */
3163 static int radio_open(struct inode *inode, struct file *file)
3165 int minor = iminor(inode);
3166 struct bttv *btv = NULL;
3169 dprintk("bttv: open minor=%d\n",minor);
3171 for (i = 0; i < bttv_num; i++) {
3172 if (bttvs[i].radio_dev->minor == minor) {
3180 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3181 mutex_lock(&btv->lock);
3185 file->private_data = btv;
3187 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3188 audio_input(btv,TVAUDIO_INPUT_RADIO);
3190 mutex_unlock(&btv->lock);
3194 static int radio_release(struct inode *inode, struct file *file)
3196 struct bttv *btv = file->private_data;
3197 struct rds_command cmd;
3201 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3206 static int radio_do_ioctl(struct inode *inode, struct file *file,
3207 unsigned int cmd, void *arg)
3209 struct bttv *btv = file->private_data;
3214 struct video_capability *cap = arg;
3216 memset(cap,0,sizeof(*cap));
3217 strcpy(cap->name,btv->radio_dev->name);
3218 cap->type = VID_TYPE_TUNER;
3226 struct video_tuner *v = arg;
3230 memset(v,0,sizeof(*v));
3231 strcpy(v->name, "Radio");
3232 bttv_call_i2c_clients(btv,cmd,v);
3244 case VIDIOC_LOG_STATUS:
3245 return bttv_common_ioctls(btv,cmd,arg);
3248 return -ENOIOCTLCMD;
3253 static int radio_ioctl(struct inode *inode, struct file *file,
3254 unsigned int cmd, unsigned long arg)
3256 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3259 static ssize_t radio_read(struct file *file, char __user *data,
3260 size_t count, loff_t *ppos)
3262 struct bttv *btv = file->private_data;
3263 struct rds_command cmd;
3264 cmd.block_count = count/3;
3266 cmd.instance = file;
3267 cmd.result = -ENODEV;
3269 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3274 static unsigned int radio_poll(struct file *file, poll_table *wait)
3276 struct bttv *btv = file->private_data;
3277 struct rds_command cmd;
3278 cmd.instance = file;
3279 cmd.event_list = wait;
3280 cmd.result = -ENODEV;
3281 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3286 static struct file_operations radio_fops =
3288 .owner = THIS_MODULE,
3291 .release = radio_release,
3292 .ioctl = radio_ioctl,
3293 .llseek = no_llseek,
3297 static struct video_device radio_template =
3299 .name = "bt848/878 radio",
3300 .type = VID_TYPE_TUNER,
3301 .hardware = VID_HARDWARE_BT848,
3302 .fops = &radio_fops,
3306 /* ----------------------------------------------------------------------- */
3307 /* some debug code */
3309 static int bttv_risc_decode(u32 risc)
3311 static char *instr[16] = {
3312 [ BT848_RISC_WRITE >> 28 ] = "write",
3313 [ BT848_RISC_SKIP >> 28 ] = "skip",
3314 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3315 [ BT848_RISC_JUMP >> 28 ] = "jump",
3316 [ BT848_RISC_SYNC >> 28 ] = "sync",
3317 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3318 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3319 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3321 static int incr[16] = {
3322 [ BT848_RISC_WRITE >> 28 ] = 2,
3323 [ BT848_RISC_JUMP >> 28 ] = 2,
3324 [ BT848_RISC_SYNC >> 28 ] = 2,
3325 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3326 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3327 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3329 static char *bits[] = {
3330 "be0", "be1", "be2", "be3/resync",
3331 "set0", "set1", "set2", "set3",
3332 "clr0", "clr1", "clr2", "clr3",
3333 "irq", "res", "eol", "sol",
3337 printk("0x%08x [ %s", risc,
3338 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3339 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3340 if (risc & (1 << (i + 12)))
3341 printk(" %s",bits[i]);
3342 printk(" count=%d ]\n", risc & 0xfff);
3343 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3346 static void bttv_risc_disasm(struct bttv *btv,
3347 struct btcx_riscmem *risc)
3351 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3352 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3353 for (i = 0; i < (risc->size >> 2); i += n) {
3354 printk("%s: 0x%lx: ", btv->c.name,
3355 (unsigned long)(risc->dma + (i<<2)));
3356 n = bttv_risc_decode(risc->cpu[i]);
3357 for (j = 1; j < n; j++)
3358 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3359 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3361 if (0 == risc->cpu[i])
3366 static void bttv_print_riscaddr(struct bttv *btv)
3368 printk(" main: %08Lx\n",
3369 (unsigned long long)btv->main.dma);
3370 printk(" vbi : o=%08Lx e=%08Lx\n",
3371 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3372 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3373 printk(" cap : o=%08Lx e=%08Lx\n",
3374 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3375 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3376 printk(" scr : o=%08Lx e=%08Lx\n",
3377 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3378 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3379 bttv_risc_disasm(btv, &btv->main);
3382 /* ----------------------------------------------------------------------- */
3385 static char *irq_name[] = {
3386 "FMTCHG", // format change detected (525 vs. 625)
3387 "VSYNC", // vertical sync (new field)
3388 "HSYNC", // horizontal sync
3389 "OFLOW", // chroma/luma AGC overflow
3390 "HLOCK", // horizontal lock changed
3391 "VPRES", // video presence changed
3393 "I2CDONE", // hw irc operation finished
3394 "GPINT", // gpio port triggered irq
3396 "RISCI", // risc instruction triggered irq
3397 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3398 "FTRGT", // pixel data fifo overrun
3399 "FDSR", // fifo data stream resyncronisation
3400 "PPERR", // parity error (data transfer)
3401 "RIPERR", // parity error (read risc instructions)
3402 "PABORT", // pci abort
3403 "OCERR", // risc instruction error
3404 "SCERR", // syncronisation error
3407 static void bttv_print_irqbits(u32 print, u32 mark)
3412 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3413 if (print & (1 << i))
3414 printk(" %s",irq_name[i]);
3415 if (mark & (1 << i))
3420 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3422 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3424 (unsigned long)btv->main.dma,
3425 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3426 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3429 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3430 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3431 "Ok, then this is harmless, don't worry ;)\n",
3435 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3437 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3443 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3445 struct bttv_buffer *item;
3447 memset(set,0,sizeof(*set));
3449 /* capture request ? */
3450 if (!list_empty(&btv->capture)) {
3452 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3453 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3455 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3458 /* capture request for other field ? */
3459 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3460 (item->vb.queue.next != &btv->capture)) {
3461 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3462 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3463 if (NULL == set->top &&
3464 V4L2_FIELD_TOP == item->vb.field) {
3467 if (NULL == set->bottom &&
3468 V4L2_FIELD_BOTTOM == item->vb.field) {
3471 if (NULL != set->top && NULL != set->bottom)
3477 /* screen overlay ? */
3478 if (NULL != btv->screen) {
3479 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3480 if (NULL == set->top && NULL == set->bottom) {
3481 set->top = btv->screen;
3482 set->bottom = btv->screen;
3485 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3487 set->top = btv->screen;
3489 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3490 NULL == set->bottom) {
3491 set->bottom = btv->screen;
3496 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3497 btv->c.nr,set->top, set->bottom,
3498 btv->screen,set->frame_irq,set->top_irq);
3503 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3504 struct bttv_buffer_set *curr, unsigned int state)
3508 do_gettimeofday(&ts);
3510 if (wakeup->top == wakeup->bottom) {
3511 if (NULL != wakeup->top && curr->top != wakeup->top) {
3513 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3514 wakeup->top->vb.ts = ts;
3515 wakeup->top->vb.field_count = btv->field_count;
3516 wakeup->top->vb.state = state;
3517 wake_up(&wakeup->top->vb.done);
3520 if (NULL != wakeup->top && curr->top != wakeup->top) {
3522 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3523 wakeup->top->vb.ts = ts;
3524 wakeup->top->vb.field_count = btv->field_count;
3525 wakeup->top->vb.state = state;
3526 wake_up(&wakeup->top->vb.done);
3528 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3530 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3531 wakeup->bottom->vb.ts = ts;
3532 wakeup->bottom->vb.field_count = btv->field_count;
3533 wakeup->bottom->vb.state = state;
3534 wake_up(&wakeup->bottom->vb.done);
3540 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3548 do_gettimeofday(&ts);
3550 wakeup->vb.field_count = btv->field_count;
3551 wakeup->vb.state = state;
3552 wake_up(&wakeup->vb.done);
3555 static void bttv_irq_timeout(unsigned long data)
3557 struct bttv *btv = (struct bttv *)data;
3558 struct bttv_buffer_set old,new;
3559 struct bttv_buffer *ovbi;
3560 struct bttv_buffer *item;
3561 unsigned long flags;
3564 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3565 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3566 btread(BT848_RISC_COUNT));
3567 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3571 spin_lock_irqsave(&btv->s_lock,flags);
3573 /* deactivate stuff */
3574 memset(&new,0,sizeof(new));
3580 bttv_buffer_activate_video(btv, &new);
3581 bttv_buffer_activate_vbi(btv, NULL);
3582 bttv_set_dma(btv, 0);
3585 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3586 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3588 /* cancel all outstanding capture / vbi requests */
3589 while (!list_empty(&btv->capture)) {
3590 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3591 list_del(&item->vb.queue);
3592 item->vb.state = STATE_ERROR;
3593 wake_up(&item->vb.done);
3595 while (!list_empty(&btv->vcapture)) {
3596 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3597 list_del(&item->vb.queue);
3598 item->vb.state = STATE_ERROR;
3599 wake_up(&item->vb.done);
3603 spin_unlock_irqrestore(&btv->s_lock,flags);
3607 bttv_irq_wakeup_top(struct bttv *btv)
3609 struct bttv_buffer *wakeup = btv->curr.top;
3614 spin_lock(&btv->s_lock);
3615 btv->curr.top_irq = 0;
3616 btv->curr.top = NULL;
3617 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3619 do_gettimeofday(&wakeup->vb.ts);
3620 wakeup->vb.field_count = btv->field_count;
3621 wakeup->vb.state = STATE_DONE;
3622 wake_up(&wakeup->vb.done);
3623 spin_unlock(&btv->s_lock);
3626 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3630 if (rc > risc->dma + risc->size)
3636 bttv_irq_switch_video(struct bttv *btv)
3638 struct bttv_buffer_set new;
3639 struct bttv_buffer_set old;
3642 spin_lock(&btv->s_lock);
3644 /* new buffer set */
3645 bttv_irq_next_video(btv, &new);
3646 rc = btread(BT848_RISC_COUNT);
3647 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3648 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3651 bttv_irq_debug_low_latency(btv, rc);
3652 spin_unlock(&btv->s_lock);
3659 btv->loop_irq &= ~1;
3660 bttv_buffer_activate_video(btv, &new);
3661 bttv_set_dma(btv, 0);
3664 if (UNSET != btv->new_input) {
3665 video_mux(btv,btv->new_input);
3666 btv->new_input = UNSET;
3669 /* wake up finished buffers */
3670 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3671 spin_unlock(&btv->s_lock);
3675 bttv_irq_switch_vbi(struct bttv *btv)
3677 struct bttv_buffer *new = NULL;
3678 struct bttv_buffer *old;
3681 spin_lock(&btv->s_lock);
3683 if (!list_empty(&btv->vcapture))
3684 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3687 rc = btread(BT848_RISC_COUNT);
3688 if (NULL != old && (is_active(&old->top, rc) ||
3689 is_active(&old->bottom, rc))) {
3692 bttv_irq_debug_low_latency(btv, rc);
3693 spin_unlock(&btv->s_lock);
3699 btv->loop_irq &= ~4;
3700 bttv_buffer_activate_vbi(btv, new);
3701 bttv_set_dma(btv, 0);
3703 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3704 spin_unlock(&btv->s_lock);
3707 static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3715 btv=(struct bttv *)dev_id;
3717 if (btv->custom_irq)
3718 handled = btv->custom_irq(btv);
3722 /* get/clear interrupt status bits */
3723 stat=btread(BT848_INT_STAT);
3724 astat=stat&btread(BT848_INT_MASK);
3728 btwrite(stat,BT848_INT_STAT);
3730 /* get device status bits */
3731 dstat=btread(BT848_DSTATUS);
3734 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3735 "riscs=%x, riscc=%08x, ",
3736 btv->c.nr, count, btv->field_count,
3737 stat>>28, btread(BT848_RISC_COUNT));
3738 bttv_print_irqbits(stat,astat);
3739 if (stat & BT848_INT_HLOCK)
3740 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3742 if (stat & BT848_INT_VPRES)
3743 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3745 if (stat & BT848_INT_FMTCHG)
3746 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3751 if (astat&BT848_INT_VSYNC)
3754 if ((astat & BT848_INT_GPINT) && btv->remote) {
3755 wake_up(&btv->gpioq);
3756 bttv_input_irq(btv);
3759 if (astat & BT848_INT_I2CDONE) {
3760 btv->i2c_done = stat;
3761 wake_up(&btv->i2c_queue);
3764 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3765 bttv_irq_switch_vbi(btv);
3767 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3768 bttv_irq_wakeup_top(btv);
3770 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3771 bttv_irq_switch_video(btv);
3773 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3774 audio_mute(btv, btv->mute); /* trigger automute */
3776 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3777 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3778 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3779 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3780 btread(BT848_RISC_COUNT));
3781 bttv_print_irqbits(stat,astat);
3784 bttv_print_riscaddr(btv);
3786 if (fdsr && astat & BT848_INT_FDSR) {
3787 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3788 btv->c.nr,btread(BT848_RISC_COUNT));
3790 bttv_print_riscaddr(btv);
3796 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3797 btwrite(0, BT848_INT_MASK);
3800 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3803 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
3805 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3809 bttv_print_irqbits(stat,astat);
3817 return IRQ_RETVAL(handled);
3821 /* ----------------------------------------------------------------------- */
3822 /* initialitation */
3824 static struct video_device *vdev_init(struct bttv *btv,
3825 struct video_device *template,
3828 struct video_device *vfd;
3830 vfd = video_device_alloc();
3835 vfd->dev = &btv->c.pci->dev;
3836 vfd->release = video_device_release;
3837 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3838 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3839 type, bttv_tvcards[btv->c.type].name);
3843 static void bttv_unregister_video(struct bttv *btv)
3845 if (btv->video_dev) {
3846 if (-1 != btv->video_dev->minor)
3847 video_unregister_device(btv->video_dev);
3849 video_device_release(btv->video_dev);
3850 btv->video_dev = NULL;
3853 if (-1 != btv->vbi_dev->minor)
3854 video_unregister_device(btv->vbi_dev);
3856 video_device_release(btv->vbi_dev);
3857 btv->vbi_dev = NULL;
3859 if (btv->radio_dev) {
3860 if (-1 != btv->radio_dev->minor)
3861 video_unregister_device(btv->radio_dev);
3863 video_device_release(btv->radio_dev);
3864 btv->radio_dev = NULL;
3868 /* register video4linux devices */
3869 static int __devinit bttv_register_video(struct bttv *btv)
3871 if (no_overlay <= 0) {
3872 bttv_video_template.type |= VID_TYPE_OVERLAY;
3874 printk("bttv: Overlay support disabled.\n");
3878 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3879 if (NULL == btv->video_dev)
3881 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3883 printk(KERN_INFO "bttv%d: registered device video%d\n",
3884 btv->c.nr,btv->video_dev->minor & 0x1f);
3885 video_device_create_file(btv->video_dev, &class_device_attr_card);
3888 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3889 if (NULL == btv->vbi_dev)
3891 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3893 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3894 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3896 if (!btv->has_radio)
3899 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3900 if (NULL == btv->radio_dev)
3902 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3904 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3905 btv->c.nr,btv->radio_dev->minor & 0x1f);
3911 bttv_unregister_video(btv);
3916 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3917 /* response on cards with no firmware is not enabled by OF */
3918 static void pci_set_command(struct pci_dev *dev)
3920 #if defined(__powerpc__)
3923 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3924 cmd = (cmd | PCI_COMMAND_MEMORY );
3925 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3929 static int __devinit bttv_probe(struct pci_dev *dev,
3930 const struct pci_device_id *pci_id)
3936 if (bttv_num == BTTV_MAX)
3938 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3939 btv=&bttvs[bttv_num];
3940 memset(btv,0,sizeof(*btv));
3941 btv->c.nr = bttv_num;
3942 sprintf(btv->c.name,"bttv%d",btv->c.nr);
3944 /* initialize structs / fill in defaults */
3945 mutex_init(&btv->lock);
3946 mutex_init(&btv->reslock);
3947 spin_lock_init(&btv->s_lock);
3948 spin_lock_init(&btv->gpio_lock);
3949 init_waitqueue_head(&btv->gpioq);
3950 init_waitqueue_head(&btv->i2c_queue);
3951 INIT_LIST_HEAD(&btv->c.subs);
3952 INIT_LIST_HEAD(&btv->capture);
3953 INIT_LIST_HEAD(&btv->vcapture);
3954 v4l2_prio_init(&btv->prio);
3956 init_timer(&btv->timeout);
3957 btv->timeout.function = bttv_irq_timeout;
3958 btv->timeout.data = (unsigned long)btv;
3961 btv->tuner_type = UNSET;
3962 btv->new_input = UNSET;
3963 btv->has_radio=radio[btv->c.nr];
3965 /* pci stuff (init, get irq/mmio, ... */
3967 btv->id = dev->device;
3968 if (pci_enable_device(dev)) {
3969 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3973 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
3974 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3978 if (!request_mem_region(pci_resource_start(dev,0),
3979 pci_resource_len(dev,0),
3981 printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
3982 btv->c.nr, pci_resource_start(dev,0));
3985 pci_set_master(dev);
3986 pci_set_command(dev);
3987 pci_set_drvdata(dev,btv);
3989 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3990 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3991 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
3992 bttv_num,btv->id, btv->revision, pci_name(dev));
3993 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3994 btv->c.pci->irq, lat, pci_resource_start(dev,0));
3997 btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
3998 if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
3999 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4007 /* disable irqs, register irq handler */
4008 btwrite(0, BT848_INT_MASK);
4009 result = request_irq(btv->c.pci->irq, bttv_irq,
4010 SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
4012 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4013 bttv_num,btv->c.pci->irq);
4017 if (0 != bttv_handle_chipset(btv)) {
4022 /* init options from insmod args */
4023 btv->opt_combfilter = combfilter;
4024 btv->opt_lumafilter = lumafilter;
4025 btv->opt_automute = automute;
4026 btv->opt_chroma_agc = chroma_agc;
4027 btv->opt_adc_crush = adc_crush;
4028 btv->opt_vcr_hack = vcr_hack;
4029 btv->opt_whitecrush_upper = whitecrush_upper;
4030 btv->opt_whitecrush_lower = whitecrush_lower;
4031 btv->opt_uv_ratio = uv_ratio;
4032 btv->opt_full_luma_range = full_luma_range;
4033 btv->opt_coring = coring;
4035 /* fill struct bttv with some useful defaults */
4036 btv->init.btv = btv;
4037 btv->init.ov.w.width = 320;
4038 btv->init.ov.w.height = 240;
4039 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4040 btv->init.width = 320;
4041 btv->init.height = 240;
4042 btv->init.lines = 16;
4045 /* initialize hardware */
4047 bttv_gpio_tracking(btv,"pre-init");
4049 bttv_risc_init_main(btv);
4053 btwrite(0x00, BT848_GPIO_REG_INP);
4054 btwrite(0x00, BT848_GPIO_OUT_EN);
4056 bttv_gpio_tracking(btv,"init");
4058 /* needs to be done before i2c is registered */
4059 bttv_init_card1(btv);
4061 /* register i2c + gpio */
4064 /* some card-specific stuff (needs working i2c) */
4065 bttv_init_card2(btv);
4068 /* register video4linux + input */
4069 if (!bttv_tvcards[btv->c.type].no_video) {
4070 bttv_register_video(btv);
4071 bt848_bright(btv,32768);
4072 bt848_contrast(btv,32768);
4073 bt848_hue(btv,32768);
4074 bt848_sat(btv,32768);
4079 /* add subdevices */
4080 if (bttv_tvcards[btv->c.type].has_dvb)
4081 bttv_sub_add_device(&btv->c, "dvb");
4083 bttv_input_init(btv);
4085 /* everything is fine */
4090 free_irq(btv->c.pci->irq,btv);
4093 if (btv->bt848_mmio)
4094 iounmap(btv->bt848_mmio);
4095 release_mem_region(pci_resource_start(btv->c.pci,0),
4096 pci_resource_len(btv->c.pci,0));
4097 pci_set_drvdata(dev,NULL);
4101 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4103 struct bttv *btv = pci_get_drvdata(pci_dev);
4106 printk("bttv%d: unloading\n",btv->c.nr);
4108 /* shutdown everything (DMA+IRQs) */
4109 btand(~15, BT848_GPIO_DMA_CTL);
4110 btwrite(0, BT848_INT_MASK);
4111 btwrite(~0x0, BT848_INT_STAT);
4112 btwrite(0x0, BT848_GPIO_OUT_EN);
4114 bttv_gpio_tracking(btv,"cleanup");
4116 /* tell gpio modules we are leaving ... */
4118 wake_up(&btv->gpioq);
4119 bttv_input_fini(btv);
4120 bttv_sub_del_devices(&btv->c);
4122 /* unregister i2c_bus + input */
4125 /* unregister video4linux */
4126 bttv_unregister_video(btv);
4128 /* free allocated memory */
4129 btcx_riscmem_free(btv->c.pci,&btv->main);
4131 /* free ressources */
4132 free_irq(btv->c.pci->irq,btv);
4133 iounmap(btv->bt848_mmio);
4134 release_mem_region(pci_resource_start(btv->c.pci,0),
4135 pci_resource_len(btv->c.pci,0));
4137 pci_set_drvdata(pci_dev, NULL);
4141 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4143 struct bttv *btv = pci_get_drvdata(pci_dev);
4144 struct bttv_buffer_set idle;
4145 unsigned long flags;
4147 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4149 /* stop dma + irqs */
4150 spin_lock_irqsave(&btv->s_lock,flags);
4151 memset(&idle, 0, sizeof(idle));
4152 btv->state.video = btv->curr;
4153 btv->state.vbi = btv->cvbi;
4154 btv->state.loop_irq = btv->loop_irq;
4157 bttv_buffer_activate_video(btv, &idle);
4158 bttv_buffer_activate_vbi(btv, NULL);
4159 bttv_set_dma(btv, 0);
4160 btwrite(0, BT848_INT_MASK);
4161 spin_unlock_irqrestore(&btv->s_lock,flags);
4163 /* save bt878 state */
4164 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4165 btv->state.gpio_data = gpio_read();
4167 /* save pci state */
4168 pci_save_state(pci_dev);
4169 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4170 pci_disable_device(pci_dev);
4171 btv->state.disabled = 1;
4176 static int bttv_resume(struct pci_dev *pci_dev)
4178 struct bttv *btv = pci_get_drvdata(pci_dev);
4179 unsigned long flags;
4182 dprintk("bttv%d: resume\n", btv->c.nr);
4184 /* restore pci state */
4185 if (btv->state.disabled) {
4186 err=pci_enable_device(pci_dev);
4188 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4192 btv->state.disabled = 0;
4194 err=pci_set_power_state(pci_dev, PCI_D0);
4196 pci_disable_device(pci_dev);
4197 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4199 btv->state.disabled = 1;
4203 pci_restore_state(pci_dev);
4205 /* restore bt878 state */
4206 bttv_reinit_bt848(btv);
4207 gpio_inout(0xffffff, btv->state.gpio_enable);
4208 gpio_write(btv->state.gpio_data);
4211 spin_lock_irqsave(&btv->s_lock,flags);
4212 btv->curr = btv->state.video;
4213 btv->cvbi = btv->state.vbi;
4214 btv->loop_irq = btv->state.loop_irq;
4215 bttv_buffer_activate_video(btv, &btv->curr);
4216 bttv_buffer_activate_vbi(btv, btv->cvbi);
4217 bttv_set_dma(btv, 0);
4218 spin_unlock_irqrestore(&btv->s_lock,flags);
4222 static struct pci_device_id bttv_pci_tbl[] = {
4223 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4224 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4225 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4226 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4227 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4228 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4229 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4230 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4234 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4236 static struct pci_driver bttv_pci_driver = {
4238 .id_table = bttv_pci_tbl,
4239 .probe = bttv_probe,
4240 .remove = __devexit_p(bttv_remove),
4241 .suspend = bttv_suspend,
4242 .resume = bttv_resume,
4245 static int bttv_init_module(void)
4249 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4250 (BTTV_VERSION_CODE >> 16) & 0xff,
4251 (BTTV_VERSION_CODE >> 8) & 0xff,
4252 BTTV_VERSION_CODE & 0xff);
4254 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4255 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4257 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4259 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4260 gbufsize = BTTV_MAX_FBUF;
4261 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4263 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4264 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4266 bttv_check_chipset();
4268 bus_register(&bttv_sub_bus_type);
4269 return pci_register_driver(&bttv_pci_driver);
4272 static void bttv_cleanup_module(void)
4274 pci_unregister_driver(&bttv_pci_driver);
4275 bus_unregister(&bttv_sub_bus_type);
4279 module_init(bttv_init_module);
4280 module_exit(bttv_cleanup_module);