3 bttv - Bt848 frame grabber driver
5 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
6 & Marcus Metzler <mocm@thp.uni-koeln.de>
7 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
9 some v4l2 code lines are taken from Justin's bttv2 driver which is
10 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
38 #include <media/v4l2-common.h>
39 #include <media/tvaudio.h>
40 #include <media/msp3400.h>
42 #include <linux/dma-mapping.h>
45 #include <asm/byteorder.h>
47 #include <media/rds.h>
50 unsigned int bttv_num; /* number of Bt848s in use */
51 struct bttv bttvs[BTTV_MAX];
53 unsigned int bttv_debug;
54 unsigned int bttv_verbose = 1;
55 unsigned int bttv_gpio;
57 /* config variables */
59 static unsigned int bigendian=1;
61 static unsigned int bigendian;
63 static unsigned int radio[BTTV_MAX];
64 static unsigned int irq_debug;
65 static unsigned int gbuffers = 8;
66 static unsigned int gbufsize = 0x208000;
68 static int video_nr = -1;
69 static int radio_nr = -1;
70 static int vbi_nr = -1;
71 static int debug_latency;
73 static unsigned int fdsr;
76 static unsigned int combfilter;
77 static unsigned int lumafilter;
78 static unsigned int automute = 1;
79 static unsigned int chroma_agc;
80 static unsigned int adc_crush = 1;
81 static unsigned int whitecrush_upper = 0xCF;
82 static unsigned int whitecrush_lower = 0x7F;
83 static unsigned int vcr_hack;
84 static unsigned int irq_iswitch;
85 static unsigned int uv_ratio = 50;
86 static unsigned int full_luma_range;
87 static unsigned int coring;
88 extern int no_overlay;
90 /* API features (turn on/off stuff for testing) */
91 static unsigned int v4l2 = 1;
94 module_param(bttv_verbose, int, 0644);
95 module_param(bttv_gpio, int, 0644);
96 module_param(bttv_debug, int, 0644);
97 module_param(irq_debug, int, 0644);
98 module_param(debug_latency, int, 0644);
100 module_param(fdsr, int, 0444);
101 module_param(video_nr, int, 0444);
102 module_param(radio_nr, int, 0444);
103 module_param(vbi_nr, int, 0444);
104 module_param(gbuffers, int, 0444);
105 module_param(gbufsize, int, 0444);
107 module_param(v4l2, int, 0644);
108 module_param(bigendian, int, 0644);
109 module_param(irq_iswitch, int, 0644);
110 module_param(combfilter, int, 0444);
111 module_param(lumafilter, int, 0444);
112 module_param(automute, int, 0444);
113 module_param(chroma_agc, int, 0444);
114 module_param(adc_crush, int, 0444);
115 module_param(whitecrush_upper, int, 0444);
116 module_param(whitecrush_lower, int, 0444);
117 module_param(vcr_hack, int, 0444);
118 module_param(uv_ratio, int, 0444);
119 module_param(full_luma_range, int, 0444);
120 module_param(coring, int, 0444);
122 module_param_array(radio, int, NULL, 0444);
124 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
125 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
126 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
127 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
128 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
129 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
130 MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
131 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
132 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
133 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
134 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
135 MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
136 MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
137 MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
138 MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
139 MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
140 MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
141 MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
143 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
144 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
145 MODULE_LICENSE("GPL");
147 /* ----------------------------------------------------------------------- */
150 static ssize_t show_card(struct class_device *cd, char *buf)
152 struct video_device *vfd = to_video_device(cd);
153 struct bttv *btv = dev_get_drvdata(vfd->dev);
154 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
156 static CLASS_DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
158 /* ----------------------------------------------------------------------- */
159 /* dvb auto-load setup */
160 #if defined(CONFIG_MODULES) && defined(MODULE)
161 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
162 static void request_module_async(void *ptr)
165 static void request_module_async(struct work_struct *work)
168 request_module("dvb-bt8xx");
171 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
172 #define request_modules(dev)
174 static void request_modules(struct bttv *dev)
176 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
177 INIT_WORK(&dev->request_module_wk, request_module_async, (void*)dev);
179 INIT_WORK(&dev->request_module_wk, request_module_async);
181 schedule_work(&dev->request_module_wk);
185 #define request_modules(dev)
186 #endif /* CONFIG_MODULES */
188 /* ----------------------------------------------------------------------- */
191 /* special timing tables from conexant... */
192 static u8 SRAM_Table[][60] =
194 /* PAL digital input over GPIO[7:0] */
196 45, // 45 bytes following
197 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
198 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
199 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
200 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
201 0x37,0x00,0xAF,0x21,0x00
203 /* NTSC digital input over GPIO[7:0] */
205 51, // 51 bytes following
206 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
207 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
208 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
209 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
210 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
213 // TGB_NTSC392 // quartzsight
214 // This table has been modified to be used for Fusion Rev D
216 0x2A, // size of table = 42
217 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
218 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
219 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
220 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
225 const struct bttv_tvnorm bttv_tvnorms[] = {
227 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
228 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
230 .v4l2_id = V4L2_STD_PAL,
238 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
239 .scaledtwidth = 1135,
245 /* ITU-R frame line number of the first VBI line
246 we can capture, of the first and second field. */
247 .vbistart = { 7,320 },
249 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
257 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
264 .vbistart = { 10, 273 },
266 .v4l2_id = V4L2_STD_SECAM,
274 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
275 .scaledtwidth = 1135,
280 .sram = 0, /* like PAL, correct? */
281 .vbistart = { 7, 320 },
283 .v4l2_id = V4L2_STD_PAL_Nc,
291 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
298 .vbistart = { 7, 320 },
300 .v4l2_id = V4L2_STD_PAL_M,
308 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
315 .vbistart = { 10, 273 },
317 .v4l2_id = V4L2_STD_PAL_N,
325 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
332 .vbistart = { 7, 320},
334 .v4l2_id = V4L2_STD_NTSC_M_JP,
342 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
349 .vbistart = {10, 273},
351 /* that one hopefully works with the strange timing
352 * which video recorders produce when playing a NTSC
353 * tape on a PAL TV ... */
354 .v4l2_id = V4L2_STD_PAL_60,
362 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
363 .scaledtwidth = 1135,
370 .vbistart = { 10, 273 },
373 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
375 /* ----------------------------------------------------------------------- */
377 packed pixel formats must come first */
378 static const struct bttv_format bttv_formats[] = {
380 .name = "8 bpp, gray",
381 .palette = VIDEO_PALETTE_GREY,
382 .fourcc = V4L2_PIX_FMT_GREY,
383 .btformat = BT848_COLOR_FMT_Y8,
385 .flags = FORMAT_FLAGS_PACKED,
387 .name = "8 bpp, dithered color",
388 .palette = VIDEO_PALETTE_HI240,
389 .fourcc = V4L2_PIX_FMT_HI240,
390 .btformat = BT848_COLOR_FMT_RGB8,
392 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
394 .name = "15 bpp RGB, le",
395 .palette = VIDEO_PALETTE_RGB555,
396 .fourcc = V4L2_PIX_FMT_RGB555,
397 .btformat = BT848_COLOR_FMT_RGB15,
399 .flags = FORMAT_FLAGS_PACKED,
401 .name = "15 bpp RGB, be",
403 .fourcc = V4L2_PIX_FMT_RGB555X,
404 .btformat = BT848_COLOR_FMT_RGB15,
405 .btswap = 0x03, /* byteswap */
407 .flags = FORMAT_FLAGS_PACKED,
409 .name = "16 bpp RGB, le",
410 .palette = VIDEO_PALETTE_RGB565,
411 .fourcc = V4L2_PIX_FMT_RGB565,
412 .btformat = BT848_COLOR_FMT_RGB16,
414 .flags = FORMAT_FLAGS_PACKED,
416 .name = "16 bpp RGB, be",
418 .fourcc = V4L2_PIX_FMT_RGB565X,
419 .btformat = BT848_COLOR_FMT_RGB16,
420 .btswap = 0x03, /* byteswap */
422 .flags = FORMAT_FLAGS_PACKED,
424 .name = "24 bpp RGB, le",
425 .palette = VIDEO_PALETTE_RGB24,
426 .fourcc = V4L2_PIX_FMT_BGR24,
427 .btformat = BT848_COLOR_FMT_RGB24,
429 .flags = FORMAT_FLAGS_PACKED,
431 .name = "32 bpp RGB, le",
432 .palette = VIDEO_PALETTE_RGB32,
433 .fourcc = V4L2_PIX_FMT_BGR32,
434 .btformat = BT848_COLOR_FMT_RGB32,
436 .flags = FORMAT_FLAGS_PACKED,
438 .name = "32 bpp RGB, be",
440 .fourcc = V4L2_PIX_FMT_RGB32,
441 .btformat = BT848_COLOR_FMT_RGB32,
442 .btswap = 0x0f, /* byte+word swap */
444 .flags = FORMAT_FLAGS_PACKED,
446 .name = "4:2:2, packed, YUYV",
447 .palette = VIDEO_PALETTE_YUV422,
448 .fourcc = V4L2_PIX_FMT_YUYV,
449 .btformat = BT848_COLOR_FMT_YUY2,
451 .flags = FORMAT_FLAGS_PACKED,
453 .name = "4:2:2, packed, YUYV",
454 .palette = VIDEO_PALETTE_YUYV,
455 .fourcc = V4L2_PIX_FMT_YUYV,
456 .btformat = BT848_COLOR_FMT_YUY2,
458 .flags = FORMAT_FLAGS_PACKED,
460 .name = "4:2:2, packed, UYVY",
461 .palette = VIDEO_PALETTE_UYVY,
462 .fourcc = V4L2_PIX_FMT_UYVY,
463 .btformat = BT848_COLOR_FMT_YUY2,
464 .btswap = 0x03, /* byteswap */
466 .flags = FORMAT_FLAGS_PACKED,
468 .name = "4:2:2, planar, Y-Cb-Cr",
469 .palette = VIDEO_PALETTE_YUV422P,
470 .fourcc = V4L2_PIX_FMT_YUV422P,
471 .btformat = BT848_COLOR_FMT_YCrCb422,
473 .flags = FORMAT_FLAGS_PLANAR,
477 .name = "4:2:0, planar, Y-Cb-Cr",
478 .palette = VIDEO_PALETTE_YUV420P,
479 .fourcc = V4L2_PIX_FMT_YUV420,
480 .btformat = BT848_COLOR_FMT_YCrCb422,
482 .flags = FORMAT_FLAGS_PLANAR,
486 .name = "4:2:0, planar, Y-Cr-Cb",
488 .fourcc = V4L2_PIX_FMT_YVU420,
489 .btformat = BT848_COLOR_FMT_YCrCb422,
491 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
495 .name = "4:1:1, planar, Y-Cb-Cr",
496 .palette = VIDEO_PALETTE_YUV411P,
497 .fourcc = V4L2_PIX_FMT_YUV411P,
498 .btformat = BT848_COLOR_FMT_YCrCb411,
500 .flags = FORMAT_FLAGS_PLANAR,
504 .name = "4:1:0, planar, Y-Cb-Cr",
505 .palette = VIDEO_PALETTE_YUV410P,
506 .fourcc = V4L2_PIX_FMT_YUV410,
507 .btformat = BT848_COLOR_FMT_YCrCb411,
509 .flags = FORMAT_FLAGS_PLANAR,
513 .name = "4:1:0, planar, Y-Cr-Cb",
515 .fourcc = V4L2_PIX_FMT_YVU410,
516 .btformat = BT848_COLOR_FMT_YCrCb411,
518 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
522 .name = "raw scanlines",
523 .palette = VIDEO_PALETTE_RAW,
525 .btformat = BT848_COLOR_FMT_RAW,
527 .flags = FORMAT_FLAGS_RAW,
530 static const unsigned int BTTV_FORMATS = ARRAY_SIZE(bttv_formats);
532 /* ----------------------------------------------------------------------- */
534 #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
535 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
536 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
537 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
538 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
539 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
540 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
541 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
542 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
543 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
544 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
545 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
547 static const struct v4l2_queryctrl no_ctl = {
549 .flags = V4L2_CTRL_FLAG_DISABLED,
551 static const struct v4l2_queryctrl bttv_ctls[] = {
554 .id = V4L2_CID_BRIGHTNESS,
555 .name = "Brightness",
559 .default_value = 32768,
560 .type = V4L2_CTRL_TYPE_INTEGER,
562 .id = V4L2_CID_CONTRAST,
567 .default_value = 32768,
568 .type = V4L2_CTRL_TYPE_INTEGER,
570 .id = V4L2_CID_SATURATION,
571 .name = "Saturation",
575 .default_value = 32768,
576 .type = V4L2_CTRL_TYPE_INTEGER,
583 .default_value = 32768,
584 .type = V4L2_CTRL_TYPE_INTEGER,
588 .id = V4L2_CID_AUDIO_MUTE,
592 .type = V4L2_CTRL_TYPE_BOOLEAN,
594 .id = V4L2_CID_AUDIO_VOLUME,
599 .default_value = 65535,
600 .type = V4L2_CTRL_TYPE_INTEGER,
602 .id = V4L2_CID_AUDIO_BALANCE,
607 .default_value = 32768,
608 .type = V4L2_CTRL_TYPE_INTEGER,
610 .id = V4L2_CID_AUDIO_BASS,
615 .default_value = 32768,
616 .type = V4L2_CTRL_TYPE_INTEGER,
618 .id = V4L2_CID_AUDIO_TREBLE,
623 .default_value = 32768,
624 .type = V4L2_CTRL_TYPE_INTEGER,
626 /* --- private --- */
628 .id = V4L2_CID_PRIVATE_CHROMA_AGC,
629 .name = "chroma agc",
632 .type = V4L2_CTRL_TYPE_BOOLEAN,
634 .id = V4L2_CID_PRIVATE_COMBFILTER,
635 .name = "combfilter",
638 .type = V4L2_CTRL_TYPE_BOOLEAN,
640 .id = V4L2_CID_PRIVATE_AUTOMUTE,
644 .type = V4L2_CTRL_TYPE_BOOLEAN,
646 .id = V4L2_CID_PRIVATE_LUMAFILTER,
647 .name = "luma decimation filter",
650 .type = V4L2_CTRL_TYPE_BOOLEAN,
652 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
656 .type = V4L2_CTRL_TYPE_BOOLEAN,
658 .id = V4L2_CID_PRIVATE_VCR_HACK,
662 .type = V4L2_CTRL_TYPE_BOOLEAN,
664 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
665 .name = "whitecrush upper",
669 .default_value = 0xCF,
670 .type = V4L2_CTRL_TYPE_INTEGER,
672 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
673 .name = "whitecrush lower",
677 .default_value = 0x7F,
678 .type = V4L2_CTRL_TYPE_INTEGER,
680 .id = V4L2_CID_PRIVATE_UV_RATIO,
686 .type = V4L2_CTRL_TYPE_INTEGER,
688 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
689 .name = "full luma range",
692 .type = V4L2_CTRL_TYPE_BOOLEAN,
694 .id = V4L2_CID_PRIVATE_CORING,
700 .type = V4L2_CTRL_TYPE_INTEGER,
706 static const int BTTV_CTLS = ARRAY_SIZE(bttv_ctls);
708 /* ----------------------------------------------------------------------- */
709 /* resource management */
712 int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
714 if (fh->resources & bit)
715 /* have it already allocated */
719 mutex_lock(&btv->reslock);
720 if (btv->resources & bit) {
721 /* no, someone else uses it */
722 mutex_unlock(&btv->reslock);
725 /* it's free, grab it */
726 fh->resources |= bit;
727 btv->resources |= bit;
728 mutex_unlock(&btv->reslock);
733 int check_btres(struct bttv_fh *fh, int bit)
735 return (fh->resources & bit);
739 int locked_btres(struct bttv *btv, int bit)
741 return (btv->resources & bit);
745 void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
747 if ((fh->resources & bits) != bits) {
748 /* trying to free ressources not allocated by us ... */
749 printk("bttv: BUG! (btres)\n");
751 mutex_lock(&btv->reslock);
752 fh->resources &= ~bits;
753 btv->resources &= ~bits;
754 mutex_unlock(&btv->reslock);
757 /* ----------------------------------------------------------------------- */
758 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
760 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
761 PLL_X = Reference pre-divider (0=1, 1=2)
762 PLL_C = Post divider (0=6, 1=4)
763 PLL_I = Integer input
764 PLL_F = Fractional input
766 F_input = 28.636363 MHz:
767 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
770 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
772 unsigned char fl, fh, fi;
774 /* prevent overflows */
787 btwrite(fl, BT848_PLL_F_LO);
788 btwrite(fh, BT848_PLL_F_HI);
789 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
792 static void set_pll(struct bttv *btv)
796 if (!btv->pll.pll_crystal)
799 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
800 dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
804 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
806 if (btv->pll.pll_current == 0)
808 bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
809 btv->c.nr,btv->pll.pll_ifreq);
810 btwrite(0x00,BT848_TGCTRL);
811 btwrite(0x00,BT848_PLL_XCI);
812 btv->pll.pll_current = 0;
816 bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
817 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
818 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
820 for (i=0; i<10; i++) {
821 /* Let other people run while the PLL stabilizes */
825 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
826 btwrite(0,BT848_DSTATUS);
828 btwrite(0x08,BT848_TGCTRL);
829 btv->pll.pll_current = btv->pll.pll_ofreq;
830 bttv_printk(" ok\n");
834 btv->pll.pll_current = -1;
835 bttv_printk("failed\n");
839 /* used to switch between the bt848's analog/digital video capture modes */
840 static void bt848A_set_timing(struct bttv *btv)
843 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
844 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
846 if (UNSET == bttv_tvcards[btv->c.type].muxsel[btv->input]) {
847 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
848 btv->c.nr,table_idx);
850 /* timing change...reset timing generator address */
851 btwrite(0x00, BT848_TGCTRL);
852 btwrite(0x02, BT848_TGCTRL);
853 btwrite(0x00, BT848_TGCTRL);
855 len=SRAM_Table[table_idx][0];
856 for(i = 1; i <= len; i++)
857 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
858 btv->pll.pll_ofreq = 27000000;
861 btwrite(0x11, BT848_TGCTRL);
862 btwrite(0x41, BT848_DVSIF);
864 btv->pll.pll_ofreq = fsc;
866 btwrite(0x0, BT848_DVSIF);
870 /* ----------------------------------------------------------------------- */
872 static void bt848_bright(struct bttv *btv, int bright)
876 // printk("bttv: set bright: %d\n",bright); // DEBUG
877 btv->bright = bright;
879 /* We want -128 to 127 we get 0-65535 */
880 value = (bright >> 8) - 128;
881 btwrite(value & 0xff, BT848_BRIGHT);
884 static void bt848_hue(struct bttv *btv, int hue)
891 value = (hue >> 8) - 128;
892 btwrite(value & 0xff, BT848_HUE);
895 static void bt848_contrast(struct bttv *btv, int cont)
899 btv->contrast = cont;
903 hibit = (value >> 6) & 4;
904 btwrite(value & 0xff, BT848_CONTRAST_LO);
905 btaor(hibit, ~4, BT848_E_CONTROL);
906 btaor(hibit, ~4, BT848_O_CONTROL);
909 static void bt848_sat(struct bttv *btv, int color)
911 int val_u,val_v,hibits;
913 btv->saturation = color;
915 /* 0-511 for the color */
916 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
917 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
918 hibits = (val_u >> 7) & 2;
919 hibits |= (val_v >> 8) & 1;
920 btwrite(val_u & 0xff, BT848_SAT_U_LO);
921 btwrite(val_v & 0xff, BT848_SAT_V_LO);
922 btaor(hibits, ~3, BT848_E_CONTROL);
923 btaor(hibits, ~3, BT848_O_CONTROL);
926 /* ----------------------------------------------------------------------- */
929 video_mux(struct bttv *btv, unsigned int input)
933 if (input >= bttv_tvcards[btv->c.type].video_inputs)
936 /* needed by RemoteVideo MX */
937 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
939 gpio_inout(mask2,mask2);
941 if (input == btv->svhs) {
942 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
943 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
945 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
946 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
948 mux = bttv_tvcards[btv->c.type].muxsel[input] & 3;
949 btaor(mux<<5, ~(3<<5), BT848_IFORM);
950 dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
951 btv->c.nr,input,mux);
953 /* card specific hook */
954 if(bttv_tvcards[btv->c.type].muxsel_hook)
955 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
959 static char *audio_modes[] = {
960 "audio: tuner", "audio: radio", "audio: extern",
961 "audio: intern", "audio: mute"
965 audio_mux(struct bttv *btv, int input, int mute)
967 int gpio_val, signal;
968 struct v4l2_control ctrl;
969 struct i2c_client *c;
971 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
972 bttv_tvcards[btv->c.type].gpiomask);
973 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
979 mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
982 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
984 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
986 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
988 bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
992 ctrl.id = V4L2_CID_AUDIO_MUTE;
993 ctrl.value = btv->mute;
994 bttv_call_i2c_clients(btv, VIDIOC_S_CTRL, &ctrl);
995 c = btv->i2c_msp34xx_client;
997 struct v4l2_routing route;
999 /* Note: the inputs tuner/radio/extern/intern are translated
1000 to msp routings. This assumes common behavior for all msp3400
1001 based TV cards. When this assumption fails, then the
1002 specific MSP routing must be added to the card table.
1003 For now this is sufficient. */
1005 case TVAUDIO_INPUT_RADIO:
1006 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1007 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1009 case TVAUDIO_INPUT_EXTERN:
1010 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1011 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1013 case TVAUDIO_INPUT_INTERN:
1014 /* Yes, this is the same input as for RADIO. I doubt
1015 if this is ever used. The only board with an INTERN
1016 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1017 that was tested. My guess is that the whole INTERN
1018 input does not work. */
1019 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1020 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1022 case TVAUDIO_INPUT_TUNER:
1024 route.input = MSP_INPUT_DEFAULT;
1027 route.output = MSP_OUTPUT_DEFAULT;
1028 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1030 c = btv->i2c_tvaudio_client;
1032 struct v4l2_routing route;
1034 route.input = input;
1036 c->driver->command(c, VIDIOC_INT_S_AUDIO_ROUTING, &route);
1042 audio_mute(struct bttv *btv, int mute)
1044 return audio_mux(btv, btv->audio, mute);
1048 audio_input(struct bttv *btv, int input)
1050 return audio_mux(btv, input, btv->mute);
1054 i2c_vidiocschan(struct bttv *btv)
1056 v4l2_std_id std = bttv_tvnorms[btv->tvnorm].v4l2_id;
1058 bttv_call_i2c_clients(btv, VIDIOC_S_STD, &std);
1059 if (btv->c.type == BTTV_BOARD_VOODOOTV_FM)
1060 bttv_tda9880_setnorm(btv,btv->tvnorm);
1064 set_tvnorm(struct bttv *btv, unsigned int norm)
1066 const struct bttv_tvnorm *tvnorm;
1068 if (norm < 0 || norm >= BTTV_TVNORMS)
1072 tvnorm = &bttv_tvnorms[norm];
1074 btwrite(tvnorm->adelay, BT848_ADELAY);
1075 btwrite(tvnorm->bdelay, BT848_BDELAY);
1076 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1078 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1079 btwrite(1, BT848_VBI_PACK_DEL);
1080 bt848A_set_timing(btv);
1082 switch (btv->c.type) {
1083 case BTTV_BOARD_VOODOOTV_FM:
1084 bttv_tda9880_setnorm(btv,norm);
1091 set_input(struct bttv *btv, unsigned int input)
1093 unsigned long flags;
1097 spin_lock_irqsave(&btv->s_lock,flags);
1098 if (btv->curr.frame_irq) {
1099 /* active capture -> delayed input switch */
1100 btv->new_input = input;
1102 video_mux(btv,input);
1104 spin_unlock_irqrestore(&btv->s_lock,flags);
1106 video_mux(btv,input);
1108 audio_input(btv,(input == bttv_tvcards[btv->c.type].tuner ?
1109 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN));
1110 set_tvnorm(btv,btv->tvnorm);
1111 i2c_vidiocschan(btv);
1114 static void init_irqreg(struct bttv *btv)
1117 btwrite(0xfffffUL, BT848_INT_STAT);
1119 if (bttv_tvcards[btv->c.type].no_video) {
1121 btwrite(BT848_INT_I2CDONE,
1125 btwrite((btv->triton1) |
1126 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1128 (fdsr ? BT848_INT_FDSR : 0) |
1129 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1130 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1136 static void init_bt848(struct bttv *btv)
1140 if (bttv_tvcards[btv->c.type].no_video) {
1141 /* very basic init only */
1146 btwrite(0x00, BT848_CAP_CTL);
1147 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1148 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1150 /* set planar and packed mode trigger points and */
1151 /* set rising edge of inverted GPINTR pin as irq trigger */
1152 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1153 BT848_GPIO_DMA_CTL_PLTP1_16|
1154 BT848_GPIO_DMA_CTL_PLTP23_16|
1155 BT848_GPIO_DMA_CTL_GPINTC|
1156 BT848_GPIO_DMA_CTL_GPINTI,
1157 BT848_GPIO_DMA_CTL);
1159 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1160 btwrite(val, BT848_E_SCLOOP);
1161 btwrite(val, BT848_O_SCLOOP);
1163 btwrite(0x20, BT848_E_VSCALE_HI);
1164 btwrite(0x20, BT848_O_VSCALE_HI);
1165 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1168 btwrite(whitecrush_upper, BT848_WC_UP);
1169 btwrite(whitecrush_lower, BT848_WC_DOWN);
1171 if (btv->opt_lumafilter) {
1172 btwrite(0, BT848_E_CONTROL);
1173 btwrite(0, BT848_O_CONTROL);
1175 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1176 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1179 bt848_bright(btv, btv->bright);
1180 bt848_hue(btv, btv->hue);
1181 bt848_contrast(btv, btv->contrast);
1182 bt848_sat(btv, btv->saturation);
1188 static void bttv_reinit_bt848(struct bttv *btv)
1190 unsigned long flags;
1193 printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
1194 spin_lock_irqsave(&btv->s_lock,flags);
1196 bttv_set_dma(btv,0);
1197 spin_unlock_irqrestore(&btv->s_lock,flags);
1200 btv->pll.pll_current = -1;
1201 set_input(btv,btv->input);
1204 static int get_control(struct bttv *btv, struct v4l2_control *c)
1206 struct video_audio va;
1209 for (i = 0; i < BTTV_CTLS; i++)
1210 if (bttv_ctls[i].id == c->id)
1214 if (btv->audio_hook && i >= 4 && i <= 8) {
1215 memset(&va,0,sizeof(va));
1216 btv->audio_hook(btv,&va,0);
1218 case V4L2_CID_AUDIO_MUTE:
1219 c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
1221 case V4L2_CID_AUDIO_VOLUME:
1222 c->value = va.volume;
1224 case V4L2_CID_AUDIO_BALANCE:
1225 c->value = va.balance;
1227 case V4L2_CID_AUDIO_BASS:
1230 case V4L2_CID_AUDIO_TREBLE:
1231 c->value = va.treble;
1237 case V4L2_CID_BRIGHTNESS:
1238 c->value = btv->bright;
1241 c->value = btv->hue;
1243 case V4L2_CID_CONTRAST:
1244 c->value = btv->contrast;
1246 case V4L2_CID_SATURATION:
1247 c->value = btv->saturation;
1250 case V4L2_CID_AUDIO_MUTE:
1251 case V4L2_CID_AUDIO_VOLUME:
1252 case V4L2_CID_AUDIO_BALANCE:
1253 case V4L2_CID_AUDIO_BASS:
1254 case V4L2_CID_AUDIO_TREBLE:
1255 bttv_call_i2c_clients(btv,VIDIOC_G_CTRL,c);
1258 case V4L2_CID_PRIVATE_CHROMA_AGC:
1259 c->value = btv->opt_chroma_agc;
1261 case V4L2_CID_PRIVATE_COMBFILTER:
1262 c->value = btv->opt_combfilter;
1264 case V4L2_CID_PRIVATE_LUMAFILTER:
1265 c->value = btv->opt_lumafilter;
1267 case V4L2_CID_PRIVATE_AUTOMUTE:
1268 c->value = btv->opt_automute;
1270 case V4L2_CID_PRIVATE_AGC_CRUSH:
1271 c->value = btv->opt_adc_crush;
1273 case V4L2_CID_PRIVATE_VCR_HACK:
1274 c->value = btv->opt_vcr_hack;
1276 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1277 c->value = btv->opt_whitecrush_upper;
1279 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1280 c->value = btv->opt_whitecrush_lower;
1282 case V4L2_CID_PRIVATE_UV_RATIO:
1283 c->value = btv->opt_uv_ratio;
1285 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1286 c->value = btv->opt_full_luma_range;
1288 case V4L2_CID_PRIVATE_CORING:
1289 c->value = btv->opt_coring;
1297 static int set_control(struct bttv *btv, struct v4l2_control *c)
1299 struct video_audio va;
1302 for (i = 0; i < BTTV_CTLS; i++)
1303 if (bttv_ctls[i].id == c->id)
1307 if (btv->audio_hook && i >= 4 && i <= 8) {
1308 memset(&va,0,sizeof(va));
1309 btv->audio_hook(btv,&va,0);
1311 case V4L2_CID_AUDIO_MUTE:
1313 va.flags |= VIDEO_AUDIO_MUTE;
1316 va.flags &= ~VIDEO_AUDIO_MUTE;
1321 case V4L2_CID_AUDIO_VOLUME:
1322 va.volume = c->value;
1324 case V4L2_CID_AUDIO_BALANCE:
1325 va.balance = c->value;
1327 case V4L2_CID_AUDIO_BASS:
1330 case V4L2_CID_AUDIO_TREBLE:
1331 va.treble = c->value;
1334 btv->audio_hook(btv,&va,1);
1338 case V4L2_CID_BRIGHTNESS:
1339 bt848_bright(btv,c->value);
1342 bt848_hue(btv,c->value);
1344 case V4L2_CID_CONTRAST:
1345 bt848_contrast(btv,c->value);
1347 case V4L2_CID_SATURATION:
1348 bt848_sat(btv,c->value);
1350 case V4L2_CID_AUDIO_MUTE:
1351 audio_mute(btv, c->value);
1353 case V4L2_CID_AUDIO_VOLUME:
1354 case V4L2_CID_AUDIO_BALANCE:
1355 case V4L2_CID_AUDIO_BASS:
1356 case V4L2_CID_AUDIO_TREBLE:
1357 bttv_call_i2c_clients(btv,VIDIOC_S_CTRL,c);
1360 case V4L2_CID_PRIVATE_CHROMA_AGC:
1361 btv->opt_chroma_agc = c->value;
1362 val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
1363 btwrite(val, BT848_E_SCLOOP);
1364 btwrite(val, BT848_O_SCLOOP);
1366 case V4L2_CID_PRIVATE_COMBFILTER:
1367 btv->opt_combfilter = c->value;
1369 case V4L2_CID_PRIVATE_LUMAFILTER:
1370 btv->opt_lumafilter = c->value;
1371 if (btv->opt_lumafilter) {
1372 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1373 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1375 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1376 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1379 case V4L2_CID_PRIVATE_AUTOMUTE:
1380 btv->opt_automute = c->value;
1382 case V4L2_CID_PRIVATE_AGC_CRUSH:
1383 btv->opt_adc_crush = c->value;
1384 btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
1387 case V4L2_CID_PRIVATE_VCR_HACK:
1388 btv->opt_vcr_hack = c->value;
1390 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1391 btv->opt_whitecrush_upper = c->value;
1392 btwrite(c->value, BT848_WC_UP);
1394 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1395 btv->opt_whitecrush_lower = c->value;
1396 btwrite(c->value, BT848_WC_DOWN);
1398 case V4L2_CID_PRIVATE_UV_RATIO:
1399 btv->opt_uv_ratio = c->value;
1400 bt848_sat(btv, btv->saturation);
1402 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1403 btv->opt_full_luma_range = c->value;
1404 btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
1406 case V4L2_CID_PRIVATE_CORING:
1407 btv->opt_coring = c->value;
1408 btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
1416 /* ----------------------------------------------------------------------- */
1418 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1420 unsigned int outbits, data;
1421 outbits = btread(BT848_GPIO_OUT_EN);
1422 data = btread(BT848_GPIO_DATA);
1423 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1424 btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
1427 static void bttv_field_count(struct bttv *btv)
1435 /* start field counter */
1436 btor(BT848_INT_VSYNC,BT848_INT_MASK);
1438 /* stop field counter */
1439 btand(~BT848_INT_VSYNC,BT848_INT_MASK);
1440 btv->field_count = 0;
1444 static const struct bttv_format*
1445 format_by_palette(int palette)
1449 for (i = 0; i < BTTV_FORMATS; i++) {
1450 if (-1 == bttv_formats[i].palette)
1452 if (bttv_formats[i].palette == palette)
1453 return bttv_formats+i;
1458 static const struct bttv_format*
1459 format_by_fourcc(int fourcc)
1463 for (i = 0; i < BTTV_FORMATS; i++) {
1464 if (-1 == bttv_formats[i].fourcc)
1466 if (bttv_formats[i].fourcc == fourcc)
1467 return bttv_formats+i;
1472 /* ----------------------------------------------------------------------- */
1476 bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
1477 struct bttv_buffer *new)
1479 struct bttv_buffer *old;
1480 unsigned long flags;
1483 dprintk("switch_overlay: enter [new=%p]\n",new);
1485 new->vb.state = STATE_DONE;
1486 spin_lock_irqsave(&btv->s_lock,flags);
1490 bttv_set_dma(btv, 0x03);
1491 spin_unlock_irqrestore(&btv->s_lock,flags);
1493 free_btres(btv,fh,RESOURCE_OVERLAY);
1495 dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
1496 bttv_dma_free(&fh->cap,btv, old);
1499 dprintk("switch_overlay: done\n");
1503 /* ----------------------------------------------------------------------- */
1504 /* video4linux (1) interface */
1506 static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
1507 struct bttv_buffer *buf,
1508 const struct bttv_format *fmt,
1509 unsigned int width, unsigned int height,
1510 enum v4l2_field field)
1512 int redo_dma_risc = 0;
1515 /* check settings */
1518 if (fmt->btformat == BT848_COLOR_FMT_RAW) {
1520 height = RAW_LINES*2;
1521 if (width*height > buf->vb.bsize)
1523 buf->vb.size = buf->vb.bsize;
1527 width > bttv_tvnorms[btv->tvnorm].swidth ||
1528 height > bttv_tvnorms[btv->tvnorm].sheight)
1530 buf->vb.size = (width * height * fmt->depth) >> 3;
1531 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
1535 /* alloc + fill struct bttv_buffer (if changed) */
1536 if (buf->vb.width != width || buf->vb.height != height ||
1537 buf->vb.field != field ||
1538 buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
1539 buf->vb.width = width;
1540 buf->vb.height = height;
1541 buf->vb.field = field;
1542 buf->tvnorm = btv->tvnorm;
1547 /* alloc risc memory */
1548 if (STATE_NEEDS_INIT == buf->vb.state) {
1550 if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
1555 if (0 != (rc = bttv_buffer_risc(btv,buf)))
1558 buf->vb.state = STATE_PREPARED;
1562 bttv_dma_free(q,btv,buf);
1567 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1569 struct bttv_fh *fh = q->priv_data;
1571 *size = fh->fmt->depth*fh->width*fh->height >> 3;
1574 while (*size * *count > gbuffers * gbufsize)
1580 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
1581 enum v4l2_field field)
1583 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1584 struct bttv_fh *fh = q->priv_data;
1586 return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
1587 fh->width, fh->height, field);
1591 buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1593 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1594 struct bttv_fh *fh = q->priv_data;
1595 struct bttv *btv = fh->btv;
1597 buf->vb.state = STATE_QUEUED;
1598 list_add_tail(&buf->vb.queue,&btv->capture);
1599 if (!btv->curr.frame_irq) {
1601 bttv_set_dma(btv, 0x03);
1605 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1607 struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
1608 struct bttv_fh *fh = q->priv_data;
1610 bttv_dma_free(&fh->cap,fh->btv,buf);
1613 static struct videobuf_queue_ops bttv_video_qops = {
1614 .buf_setup = buffer_setup,
1615 .buf_prepare = buffer_prepare,
1616 .buf_queue = buffer_queue,
1617 .buf_release = buffer_release,
1620 static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
1624 return BTTV_VERSION_CODE;
1626 /* *** v4l1 *** ************************************************ */
1629 unsigned long *freq = arg;
1635 struct v4l2_frequency freq;
1637 memset(&freq, 0, sizeof(freq));
1638 freq.frequency = *(unsigned long *)arg;
1639 mutex_lock(&btv->lock);
1640 freq.type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1641 btv->freq = *(unsigned long *)arg;
1642 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,&freq);
1643 if (btv->has_matchbox && btv->radio_user)
1644 tea5757_set_freq(btv,*(unsigned long *)arg);
1645 mutex_unlock(&btv->lock);
1651 struct video_tuner *v = arg;
1653 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1655 if (v->tuner) /* Only tuner 0 */
1657 strcpy(v->name, "Television");
1659 v->rangehigh = 0x7FFFFFFF;
1660 v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1661 v->mode = btv->tvnorm;
1662 v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1663 bttv_call_i2c_clients(btv,cmd,v);
1668 struct video_tuner *v = arg;
1670 if (v->tuner) /* Only tuner 0 */
1672 if (v->mode >= BTTV_TVNORMS)
1675 mutex_lock(&btv->lock);
1676 set_tvnorm(btv,v->mode);
1677 bttv_call_i2c_clients(btv,cmd,v);
1678 mutex_unlock(&btv->lock);
1684 struct video_channel *v = arg;
1685 unsigned int channel = v->channel;
1687 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1690 v->flags = VIDEO_VC_AUDIO;
1691 v->type = VIDEO_TYPE_CAMERA;
1692 v->norm = btv->tvnorm;
1693 if (channel == bttv_tvcards[btv->c.type].tuner) {
1694 strcpy(v->name,"Television");
1695 v->flags|=VIDEO_VC_TUNER;
1696 v->type=VIDEO_TYPE_TV;
1698 } else if (channel == btv->svhs) {
1699 strcpy(v->name,"S-Video");
1701 sprintf(v->name,"Composite%d",channel);
1707 struct video_channel *v = arg;
1708 unsigned int channel = v->channel;
1710 if (channel >= bttv_tvcards[btv->c.type].video_inputs)
1712 if (v->norm >= BTTV_TVNORMS)
1715 mutex_lock(&btv->lock);
1716 if (channel == btv->input &&
1717 v->norm == btv->tvnorm) {
1719 mutex_unlock(&btv->lock);
1723 btv->tvnorm = v->norm;
1724 set_input(btv,v->channel);
1725 mutex_unlock(&btv->lock);
1731 struct video_audio *v = arg;
1733 memset(v,0,sizeof(*v));
1734 strcpy(v->name,"Television");
1735 v->flags |= VIDEO_AUDIO_MUTABLE;
1736 v->mode = VIDEO_SOUND_MONO;
1738 mutex_lock(&btv->lock);
1739 bttv_call_i2c_clients(btv,cmd,v);
1741 /* card specific hooks */
1742 if (btv->audio_hook)
1743 btv->audio_hook(btv,v,0);
1745 mutex_unlock(&btv->lock);
1750 struct video_audio *v = arg;
1751 unsigned int audio = v->audio;
1753 if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
1756 mutex_lock(&btv->lock);
1757 audio_mute(btv, (v->flags&VIDEO_AUDIO_MUTE) ? 1 : 0);
1758 bttv_call_i2c_clients(btv,cmd,v);
1760 /* card specific hooks */
1761 if (btv->audio_hook)
1762 btv->audio_hook(btv,v,1);
1764 mutex_unlock(&btv->lock);
1768 /* *** v4l2 *** ************************************************ */
1769 case VIDIOC_ENUMSTD:
1771 struct v4l2_standard *e = arg;
1772 unsigned int index = e->index;
1774 if (index >= BTTV_TVNORMS)
1776 v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
1777 bttv_tvnorms[e->index].name);
1783 v4l2_std_id *id = arg;
1784 *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
1789 v4l2_std_id *id = arg;
1792 for (i = 0; i < BTTV_TVNORMS; i++)
1793 if (*id & bttv_tvnorms[i].v4l2_id)
1795 if (i == BTTV_TVNORMS)
1798 mutex_lock(&btv->lock);
1800 i2c_vidiocschan(btv);
1801 mutex_unlock(&btv->lock);
1804 case VIDIOC_QUERYSTD:
1806 v4l2_std_id *id = arg;
1808 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1809 *id = V4L2_STD_625_50;
1811 *id = V4L2_STD_525_60;
1815 case VIDIOC_ENUMINPUT:
1817 struct v4l2_input *i = arg;
1821 if (n >= bttv_tvcards[btv->c.type].video_inputs)
1823 memset(i,0,sizeof(*i));
1825 i->type = V4L2_INPUT_TYPE_CAMERA;
1827 if (i->index == bttv_tvcards[btv->c.type].tuner) {
1828 sprintf(i->name, "Television");
1829 i->type = V4L2_INPUT_TYPE_TUNER;
1831 } else if (i->index == btv->svhs) {
1832 sprintf(i->name, "S-Video");
1834 sprintf(i->name,"Composite%d",i->index);
1836 if (i->index == btv->input) {
1837 __u32 dstatus = btread(BT848_DSTATUS);
1838 if (0 == (dstatus & BT848_DSTATUS_PRES))
1839 i->status |= V4L2_IN_ST_NO_SIGNAL;
1840 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1841 i->status |= V4L2_IN_ST_NO_H_LOCK;
1843 for (n = 0; n < BTTV_TVNORMS; n++)
1844 i->std |= bttv_tvnorms[n].v4l2_id;
1847 case VIDIOC_G_INPUT:
1853 case VIDIOC_S_INPUT:
1855 unsigned int *i = arg;
1857 if (*i > bttv_tvcards[btv->c.type].video_inputs)
1859 mutex_lock(&btv->lock);
1861 mutex_unlock(&btv->lock);
1865 case VIDIOC_G_TUNER:
1867 struct v4l2_tuner *t = arg;
1869 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1873 mutex_lock(&btv->lock);
1874 memset(t,0,sizeof(*t));
1875 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1876 bttv_call_i2c_clients(btv, VIDIOC_G_TUNER, t);
1877 strcpy(t->name, "Television");
1878 t->capability = V4L2_TUNER_CAP_NORM;
1879 t->type = V4L2_TUNER_ANALOG_TV;
1880 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
1883 if (btv->audio_hook) {
1885 struct video_audio va;
1886 memset(&va, 0, sizeof(struct video_audio));
1887 btv->audio_hook(btv,&va,0);
1888 t->audmode = V4L2_TUNER_MODE_MONO;
1889 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1890 if(va.mode & VIDEO_SOUND_STEREO) {
1891 t->audmode = V4L2_TUNER_MODE_STEREO;
1892 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1894 if(va.mode & VIDEO_SOUND_LANG2) {
1895 t->audmode = V4L2_TUNER_MODE_LANG1;
1896 t->rxsubchans = V4L2_TUNER_SUB_LANG1
1897 | V4L2_TUNER_SUB_LANG2;
1900 /* FIXME: fill capability+audmode */
1901 mutex_unlock(&btv->lock);
1904 case VIDIOC_S_TUNER:
1906 struct v4l2_tuner *t = arg;
1908 if (UNSET == bttv_tvcards[btv->c.type].tuner)
1912 mutex_lock(&btv->lock);
1913 bttv_call_i2c_clients(btv, VIDIOC_S_TUNER, t);
1914 if (btv->audio_hook) {
1915 struct video_audio va;
1916 memset(&va, 0, sizeof(struct video_audio));
1917 if (t->audmode == V4L2_TUNER_MODE_MONO)
1918 va.mode = VIDEO_SOUND_MONO;
1919 else if (t->audmode == V4L2_TUNER_MODE_STEREO ||
1920 t->audmode == V4L2_TUNER_MODE_LANG1_LANG2)
1921 va.mode = VIDEO_SOUND_STEREO;
1922 else if (t->audmode == V4L2_TUNER_MODE_LANG1)
1923 va.mode = VIDEO_SOUND_LANG1;
1924 else if (t->audmode == V4L2_TUNER_MODE_LANG2)
1925 va.mode = VIDEO_SOUND_LANG2;
1926 btv->audio_hook(btv,&va,1);
1928 mutex_unlock(&btv->lock);
1932 case VIDIOC_G_FREQUENCY:
1934 struct v4l2_frequency *f = arg;
1936 memset(f,0,sizeof(*f));
1937 f->type = V4L2_TUNER_ANALOG_TV;
1938 f->frequency = btv->freq;
1941 case VIDIOC_S_FREQUENCY:
1943 struct v4l2_frequency *f = arg;
1945 if (unlikely(f->tuner != 0))
1947 if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
1949 mutex_lock(&btv->lock);
1950 btv->freq = f->frequency;
1951 bttv_call_i2c_clients(btv,VIDIOC_S_FREQUENCY,f);
1952 if (btv->has_matchbox && btv->radio_user)
1953 tea5757_set_freq(btv,btv->freq);
1954 mutex_unlock(&btv->lock);
1957 case VIDIOC_LOG_STATUS:
1959 printk(KERN_INFO "bttv%d: ================= START STATUS CARD #%d =================\n", btv->c.nr, btv->c.nr);
1960 bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, NULL);
1961 printk(KERN_INFO "bttv%d: ================== END STATUS CARD #%d ==================\n", btv->c.nr, btv->c.nr);
1966 return -ENOIOCTLCMD;
1972 static int verify_window(const struct bttv_tvnorm *tvn,
1973 struct v4l2_window *win, int fixup)
1975 enum v4l2_field field;
1978 if (win->w.width < 48 || win->w.height < 32)
1980 if (win->clipcount > 2048)
1985 maxh = tvn->sheight;
1987 if (V4L2_FIELD_ANY == field) {
1988 field = (win->w.height > maxh/2)
1989 ? V4L2_FIELD_INTERLACED
1993 case V4L2_FIELD_TOP:
1994 case V4L2_FIELD_BOTTOM:
1997 case V4L2_FIELD_INTERLACED:
2003 if (!fixup && (win->w.width > maxw || win->w.height > maxh))
2006 if (win->w.width > maxw)
2007 win->w.width = maxw;
2008 if (win->w.height > maxh)
2009 win->w.height = maxh;
2014 static int setup_window(struct bttv_fh *fh, struct bttv *btv,
2015 struct v4l2_window *win, int fixup)
2017 struct v4l2_clip *clips = NULL;
2018 int n,size,retval = 0;
2020 if (NULL == fh->ovfmt)
2022 if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
2024 retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
2028 /* copy clips -- luckily v4l1 + v4l2 are binary
2029 compatible here ...*/
2031 size = sizeof(*clips)*(n+4);
2032 clips = kmalloc(size,GFP_KERNEL);
2036 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
2041 /* clip against screen */
2042 if (NULL != btv->fbuf.base)
2043 n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
2045 btcx_sort_clips(clips,n);
2047 /* 4-byte alignments */
2048 switch (fh->ovfmt->depth) {
2051 btcx_align(&win->w, clips, n, 3);
2054 btcx_align(&win->w, clips, n, 1);
2057 /* no alignment fixups needed */
2063 mutex_lock(&fh->cap.lock);
2064 kfree(fh->ov.clips);
2065 fh->ov.clips = clips;
2069 fh->ov.field = win->field;
2070 fh->ov.setup_ok = 1;
2071 btv->init.ov.w.width = win->w.width;
2072 btv->init.ov.w.height = win->w.height;
2073 btv->init.ov.field = win->field;
2075 /* update overlay if needed */
2077 if (check_btres(fh, RESOURCE_OVERLAY)) {
2078 struct bttv_buffer *new;
2080 new = videobuf_alloc(sizeof(*new));
2081 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2082 retval = bttv_switch_overlay(btv,fh,new);
2084 mutex_unlock(&fh->cap.lock);
2088 /* ----------------------------------------------------------------------- */
2090 static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
2092 struct videobuf_queue* q = NULL;
2095 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2098 case V4L2_BUF_TYPE_VBI_CAPTURE:
2107 static int bttv_resource(struct bttv_fh *fh)
2112 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2113 res = RESOURCE_VIDEO;
2115 case V4L2_BUF_TYPE_VBI_CAPTURE:
2124 static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
2126 struct videobuf_queue *q = bttv_queue(fh);
2127 int res = bttv_resource(fh);
2129 if (check_btres(fh,res))
2131 if (videobuf_queue_is_busy(q))
2138 pix_format_set_size (struct v4l2_pix_format * f,
2139 const struct bttv_format * fmt,
2141 unsigned int height)
2146 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2147 f->bytesperline = width; /* Y plane */
2148 f->sizeimage = (width * height * fmt->depth) >> 3;
2150 f->bytesperline = (width * fmt->depth) >> 3;
2151 f->sizeimage = height * f->bytesperline;
2155 static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
2158 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2159 memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
2160 pix_format_set_size (&f->fmt.pix, fh->fmt,
2161 fh->width, fh->height);
2162 f->fmt.pix.field = fh->cap.field;
2163 f->fmt.pix.pixelformat = fh->fmt->fourcc;
2165 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2166 memset(&f->fmt.win,0,sizeof(struct v4l2_window));
2167 f->fmt.win.w = fh->ov.w;
2168 f->fmt.win.field = fh->ov.field;
2170 case V4L2_BUF_TYPE_VBI_CAPTURE:
2171 bttv_vbi_get_fmt(fh,f);
2178 static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
2179 struct v4l2_format *f)
2182 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2184 const struct bttv_format *fmt;
2185 enum v4l2_field field;
2186 unsigned int maxw,maxh;
2188 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2193 maxw = bttv_tvnorms[btv->tvnorm].swidth;
2194 maxh = bttv_tvnorms[btv->tvnorm].sheight;
2195 field = f->fmt.pix.field;
2196 if (V4L2_FIELD_ANY == field)
2197 field = (f->fmt.pix.height > maxh/2)
2198 ? V4L2_FIELD_INTERLACED
2199 : V4L2_FIELD_BOTTOM;
2200 if (V4L2_FIELD_SEQ_BT == field)
2201 field = V4L2_FIELD_SEQ_TB;
2203 case V4L2_FIELD_TOP:
2204 case V4L2_FIELD_BOTTOM:
2205 case V4L2_FIELD_ALTERNATE:
2208 case V4L2_FIELD_INTERLACED:
2210 case V4L2_FIELD_SEQ_TB:
2211 if (fmt->flags & FORMAT_FLAGS_PLANAR)
2218 /* update data for the application */
2219 f->fmt.pix.field = field;
2220 if (f->fmt.pix.width < 48)
2221 f->fmt.pix.width = 48;
2222 if (f->fmt.pix.height < 32)
2223 f->fmt.pix.height = 32;
2224 if (f->fmt.pix.width > maxw)
2225 f->fmt.pix.width = maxw;
2226 if (f->fmt.pix.height > maxh)
2227 f->fmt.pix.height = maxh;
2228 pix_format_set_size (&f->fmt.pix, fmt,
2229 f->fmt.pix.width & ~3,
2234 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2235 return verify_window(&bttv_tvnorms[btv->tvnorm],
2237 case V4L2_BUF_TYPE_VBI_CAPTURE:
2238 bttv_vbi_try_fmt(fh,f);
2245 static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
2246 struct v4l2_format *f)
2251 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2253 const struct bttv_format *fmt;
2255 retval = bttv_switch_type(fh,f->type);
2258 retval = bttv_try_fmt(fh,btv,f);
2261 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2263 /* update our state informations */
2264 mutex_lock(&fh->cap.lock);
2266 fh->cap.field = f->fmt.pix.field;
2267 fh->cap.last = V4L2_FIELD_NONE;
2268 fh->width = f->fmt.pix.width;
2269 fh->height = f->fmt.pix.height;
2270 btv->init.fmt = fmt;
2271 btv->init.width = f->fmt.pix.width;
2272 btv->init.height = f->fmt.pix.height;
2273 mutex_unlock(&fh->cap.lock);
2277 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2278 if (no_overlay > 0) {
2279 printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
2282 return setup_window(fh, btv, &f->fmt.win, 1);
2283 case V4L2_BUF_TYPE_VBI_CAPTURE:
2284 retval = bttv_switch_type(fh,f->type);
2287 if (locked_btres(fh->btv, RESOURCE_VBI))
2289 bttv_vbi_try_fmt(fh,f);
2290 bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
2291 bttv_vbi_get_fmt(fh,f);
2298 static int bttv_do_ioctl(struct inode *inode, struct file *file,
2299 unsigned int cmd, void *arg)
2301 struct bttv_fh *fh = file->private_data;
2302 struct bttv *btv = fh->btv;
2303 unsigned long flags;
2307 v4l_print_ioctl(btv->c.name, cmd);
2310 bttv_reinit_bt848(btv);
2318 case VIDIOC_S_INPUT:
2319 case VIDIOC_S_TUNER:
2320 case VIDIOC_S_FREQUENCY:
2321 retval = v4l2_prio_check(&btv->prio,&fh->prio);
2328 /* *** v4l1 *** ************************************************ */
2331 struct video_capability *cap = arg;
2333 memset(cap,0,sizeof(*cap));
2334 strcpy(cap->name,btv->video_dev->name);
2335 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
2337 cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
2340 cap->type = VID_TYPE_CAPTURE|
2344 if (no_overlay <= 0)
2345 cap->type |= VID_TYPE_OVERLAY;
2347 cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
2348 cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
2350 cap->minheight = 32;
2352 cap->channels = bttv_tvcards[btv->c.type].video_inputs;
2353 cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
2359 struct video_picture *pic = arg;
2361 memset(pic,0,sizeof(*pic));
2362 pic->brightness = btv->bright;
2363 pic->contrast = btv->contrast;
2364 pic->hue = btv->hue;
2365 pic->colour = btv->saturation;
2367 pic->depth = fh->fmt->depth;
2368 pic->palette = fh->fmt->palette;
2374 struct video_picture *pic = arg;
2375 const struct bttv_format *fmt;
2377 fmt = format_by_palette(pic->palette);
2380 mutex_lock(&fh->cap.lock);
2381 if (fmt->depth != pic->depth) {
2383 goto fh_unlock_and_return;
2385 if (fmt->flags & FORMAT_FLAGS_RAW) {
2386 /* VIDIOCMCAPTURE uses gbufsize, not RAW_BPL *
2387 RAW_LINES * 2. F1 is stored at offset 0, F2
2388 at buffer size / 2. */
2389 fh->width = RAW_BPL;
2390 fh->height = gbufsize / RAW_BPL;
2391 btv->init.width = RAW_BPL;
2392 btv->init.height = gbufsize / RAW_BPL;
2396 btv->init.ovfmt = fmt;
2397 btv->init.fmt = fmt;
2399 /* dirty hack time: swap bytes for overlay if the
2400 display adaptor is big endian (insmod option) */
2401 if (fmt->palette == VIDEO_PALETTE_RGB555 ||
2402 fmt->palette == VIDEO_PALETTE_RGB565 ||
2403 fmt->palette == VIDEO_PALETTE_RGB32) {
2407 bt848_bright(btv,pic->brightness);
2408 bt848_contrast(btv,pic->contrast);
2409 bt848_hue(btv,pic->hue);
2410 bt848_sat(btv,pic->colour);
2411 mutex_unlock(&fh->cap.lock);
2417 struct video_window *win = arg;
2419 memset(win,0,sizeof(*win));
2420 win->x = fh->ov.w.left;
2421 win->y = fh->ov.w.top;
2422 win->width = fh->ov.w.width;
2423 win->height = fh->ov.w.height;
2428 struct video_window *win = arg;
2429 struct v4l2_window w2;
2431 if (no_overlay > 0) {
2432 printk ("VIDIOCSWIN: no_overlay\n");
2436 w2.field = V4L2_FIELD_ANY;
2439 w2.w.width = win->width;
2440 w2.w.height = win->height;
2441 w2.clipcount = win->clipcount;
2442 w2.clips = (struct v4l2_clip __user *)win->clips;
2443 retval = setup_window(fh, btv, &w2, 0);
2445 /* on v4l1 this ioctl affects the read() size too */
2446 fh->width = fh->ov.w.width;
2447 fh->height = fh->ov.w.height;
2448 btv->init.width = fh->ov.w.width;
2449 btv->init.height = fh->ov.w.height;
2456 struct video_buffer *fbuf = arg;
2458 fbuf->base = btv->fbuf.base;
2459 fbuf->width = btv->fbuf.fmt.width;
2460 fbuf->height = btv->fbuf.fmt.height;
2461 fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
2463 fbuf->depth = fh->ovfmt->depth;
2466 fbuf->depth = ((fbuf->bytesperline<<3)
2476 struct video_buffer *fbuf = arg;
2477 const struct bttv_format *fmt;
2480 if(!capable(CAP_SYS_ADMIN) &&
2481 !capable(CAP_SYS_RAWIO))
2483 end = (unsigned long)fbuf->base +
2484 fbuf->height * fbuf->bytesperline;
2485 mutex_lock(&fh->cap.lock);
2488 switch (fbuf->depth) {
2490 fmt = format_by_palette(VIDEO_PALETTE_HI240);
2493 fmt = format_by_palette(VIDEO_PALETTE_RGB565);
2496 fmt = format_by_palette(VIDEO_PALETTE_RGB24);
2499 fmt = format_by_palette(VIDEO_PALETTE_RGB32);
2503 fmt = format_by_palette(VIDEO_PALETTE_RGB555);
2510 goto fh_unlock_and_return;
2514 btv->init.ovfmt = fmt;
2515 btv->init.fmt = fmt;
2516 btv->fbuf.base = fbuf->base;
2517 btv->fbuf.fmt.width = fbuf->width;
2518 btv->fbuf.fmt.height = fbuf->height;
2519 if (fbuf->bytesperline)
2520 btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
2522 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
2523 mutex_unlock(&fh->cap.lock);
2528 case VIDIOC_OVERLAY:
2530 struct bttv_buffer *new;
2535 if (NULL == btv->fbuf.base)
2537 if (!fh->ov.setup_ok) {
2538 dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
2543 if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
2546 mutex_lock(&fh->cap.lock);
2548 fh->ov.tvnorm = btv->tvnorm;
2549 new = videobuf_alloc(sizeof(*new));
2550 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
2556 retval = bttv_switch_overlay(btv,fh,new);
2557 mutex_unlock(&fh->cap.lock);
2563 struct video_mbuf *mbuf = arg;
2566 mutex_lock(&fh->cap.lock);
2567 retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
2570 goto fh_unlock_and_return;
2571 memset(mbuf,0,sizeof(*mbuf));
2572 mbuf->frames = gbuffers;
2573 mbuf->size = gbuffers * gbufsize;
2574 for (i = 0; i < gbuffers; i++)
2575 mbuf->offsets[i] = i * gbufsize;
2576 mutex_unlock(&fh->cap.lock);
2579 case VIDIOCMCAPTURE:
2581 struct video_mmap *vm = arg;
2582 struct bttv_buffer *buf;
2583 enum v4l2_field field;
2585 if (vm->frame >= VIDEO_MAX_FRAME)
2588 mutex_lock(&fh->cap.lock);
2590 buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
2592 goto fh_unlock_and_return;
2593 if (0 == buf->vb.baddr)
2594 goto fh_unlock_and_return;
2595 if (buf->vb.state == STATE_QUEUED ||
2596 buf->vb.state == STATE_ACTIVE)
2597 goto fh_unlock_and_return;
2599 field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
2600 ? V4L2_FIELD_INTERLACED
2601 : V4L2_FIELD_BOTTOM;
2602 retval = bttv_prepare_buffer(&fh->cap,btv,buf,
2603 format_by_palette(vm->format),
2604 vm->width,vm->height,field);
2606 goto fh_unlock_and_return;
2607 spin_lock_irqsave(&btv->s_lock,flags);
2608 buffer_queue(&fh->cap,&buf->vb);
2609 spin_unlock_irqrestore(&btv->s_lock,flags);
2610 mutex_unlock(&fh->cap.lock);
2616 struct bttv_buffer *buf;
2618 if (*frame >= VIDEO_MAX_FRAME)
2621 mutex_lock(&fh->cap.lock);
2623 buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
2625 goto fh_unlock_and_return;
2626 retval = videobuf_waiton(&buf->vb,0,1);
2628 goto fh_unlock_and_return;
2629 switch (buf->vb.state) {
2634 videobuf_dma_sync(&fh->cap,&buf->vb.dma);
2635 bttv_dma_free(&fh->cap,btv,buf);
2641 mutex_unlock(&fh->cap.lock);
2647 struct vbi_format *fmt = (void *) arg;
2648 struct v4l2_format fmt2;
2650 if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
2651 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2655 bttv_vbi_get_fmt(fh, &fmt2);
2657 memset(fmt,0,sizeof(*fmt));
2658 fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
2659 fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
2660 fmt->sample_format = VIDEO_PALETTE_RAW;
2661 fmt->start[0] = fmt2.fmt.vbi.start[0];
2662 fmt->count[0] = fmt2.fmt.vbi.count[0];
2663 fmt->start[1] = fmt2.fmt.vbi.start[1];
2664 fmt->count[1] = fmt2.fmt.vbi.count[1];
2665 if (fmt2.fmt.vbi.flags & V4L2_VBI_UNSYNC)
2666 fmt->flags |= VBI_UNSYNC;
2667 if (fmt2.fmt.vbi.flags & V4L2_VBI_INTERLACED)
2668 fmt->flags |= VBI_INTERLACED;
2673 struct vbi_format *fmt = (void *) arg;
2674 struct v4l2_format fmt2;
2676 retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
2679 bttv_vbi_get_fmt(fh, &fmt2);
2681 if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
2682 fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
2683 fmt->sample_format != VIDEO_PALETTE_RAW ||
2684 fmt->start[0] != fmt2.fmt.vbi.start[0] ||
2685 fmt->start[1] != fmt2.fmt.vbi.start[1] ||
2686 fmt->count[0] != fmt->count[1] ||
2687 fmt->count[0] < 1 ||
2688 fmt->count[0] > 32 /* VBI_MAXLINES */)
2691 bttv_vbi_setlines(fh,btv,fmt->count[0]);
2704 return bttv_common_ioctls(btv,cmd,arg);
2706 /* *** v4l2 *** ************************************************ */
2707 case VIDIOC_QUERYCAP:
2709 struct v4l2_capability *cap = arg;
2713 memset(cap, 0, sizeof (*cap));
2714 strlcpy(cap->driver, "bttv", sizeof (cap->driver));
2715 strlcpy(cap->card, btv->video_dev->name, sizeof (cap->card));
2716 snprintf(cap->bus_info, sizeof (cap->bus_info),
2717 "PCI:%s", pci_name(btv->c.pci));
2718 cap->version = BTTV_VERSION_CODE;
2720 V4L2_CAP_VIDEO_CAPTURE |
2721 V4L2_CAP_VBI_CAPTURE |
2722 V4L2_CAP_READWRITE |
2724 if (no_overlay <= 0)
2725 cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
2727 if (bttv_tvcards[btv->c.type].tuner != UNSET &&
2728 bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
2729 cap->capabilities |= V4L2_CAP_TUNER;
2733 case VIDIOC_ENUM_FMT:
2735 struct v4l2_fmtdesc *f = arg;
2736 enum v4l2_buf_type type;
2741 if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
2746 memset(f,0,sizeof(*f));
2749 f->pixelformat = V4L2_PIX_FMT_GREY;
2750 strcpy(f->description,"vbi data");
2754 /* video capture + overlay */
2756 for (i = 0; i < BTTV_FORMATS; i++) {
2757 if (bttv_formats[i].fourcc != -1)
2759 if ((unsigned int)index == f->index)
2762 if (BTTV_FORMATS == i)
2766 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2768 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
2769 if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
2775 memset(f,0,sizeof(*f));
2778 f->pixelformat = bttv_formats[i].fourcc;
2779 strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
2783 case VIDIOC_TRY_FMT:
2785 struct v4l2_format *f = arg;
2786 return bttv_try_fmt(fh,btv,f);
2790 struct v4l2_format *f = arg;
2791 return bttv_g_fmt(fh,f);
2795 struct v4l2_format *f = arg;
2796 return bttv_s_fmt(fh,btv,f);
2801 struct v4l2_framebuffer *fb = arg;
2804 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2806 fb->fmt.pixelformat = fh->ovfmt->fourcc;
2811 struct v4l2_framebuffer *fb = arg;
2812 const struct bttv_format *fmt;
2814 if(!capable(CAP_SYS_ADMIN) &&
2815 !capable(CAP_SYS_RAWIO))
2819 fmt = format_by_fourcc(fb->fmt.pixelformat);
2822 if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
2825 mutex_lock(&fh->cap.lock);
2827 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2828 if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
2829 goto fh_unlock_and_return;
2830 if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
2831 goto fh_unlock_and_return;
2835 btv->fbuf.base = fb->base;
2836 btv->fbuf.fmt.width = fb->fmt.width;
2837 btv->fbuf.fmt.height = fb->fmt.height;
2838 if (0 != fb->fmt.bytesperline)
2839 btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
2841 btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
2845 btv->init.ovfmt = fmt;
2846 if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
2849 fh->ov.w.width = fb->fmt.width;
2850 fh->ov.w.height = fb->fmt.height;
2851 btv->init.ov.w.width = fb->fmt.width;
2852 btv->init.ov.w.height = fb->fmt.height;
2853 kfree(fh->ov.clips);
2854 fh->ov.clips = NULL;
2857 if (check_btres(fh, RESOURCE_OVERLAY)) {
2858 struct bttv_buffer *new;
2860 new = videobuf_alloc(sizeof(*new));
2861 bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
2862 retval = bttv_switch_overlay(btv,fh,new);
2865 mutex_unlock(&fh->cap.lock);
2869 case VIDIOC_REQBUFS:
2870 return videobuf_reqbufs(bttv_queue(fh),arg);
2872 case VIDIOC_QUERYBUF:
2873 return videobuf_querybuf(bttv_queue(fh),arg);
2876 return videobuf_qbuf(bttv_queue(fh),arg);
2879 return videobuf_dqbuf(bttv_queue(fh),arg,
2880 file->f_flags & O_NONBLOCK);
2882 case VIDIOC_STREAMON:
2884 int res = bttv_resource(fh);
2886 if (!check_alloc_btres(btv,fh,res))
2888 return videobuf_streamon(bttv_queue(fh));
2890 case VIDIOC_STREAMOFF:
2892 int res = bttv_resource(fh);
2894 retval = videobuf_streamoff(bttv_queue(fh));
2897 free_btres(btv,fh,res);
2901 case VIDIOC_QUERYCTRL:
2903 struct v4l2_queryctrl *c = arg;
2906 if ((c->id < V4L2_CID_BASE ||
2907 c->id >= V4L2_CID_LASTP1) &&
2908 (c->id < V4L2_CID_PRIVATE_BASE ||
2909 c->id >= V4L2_CID_PRIVATE_LASTP1))
2911 for (i = 0; i < BTTV_CTLS; i++)
2912 if (bttv_ctls[i].id == c->id)
2914 if (i == BTTV_CTLS) {
2919 if (btv->audio_hook && i >= 4 && i <= 8) {
2920 struct video_audio va;
2921 memset(&va,0,sizeof(va));
2922 btv->audio_hook(btv,&va,0);
2923 switch (bttv_ctls[i].id) {
2924 case V4L2_CID_AUDIO_VOLUME:
2925 if (!(va.flags & VIDEO_AUDIO_VOLUME))
2928 case V4L2_CID_AUDIO_BALANCE:
2929 if (!(va.flags & VIDEO_AUDIO_BALANCE))
2932 case V4L2_CID_AUDIO_BASS:
2933 if (!(va.flags & VIDEO_AUDIO_BASS))
2936 case V4L2_CID_AUDIO_TREBLE:
2937 if (!(va.flags & VIDEO_AUDIO_TREBLE))
2945 return get_control(btv,arg);
2947 return set_control(btv,arg);
2950 struct v4l2_streamparm *parm = arg;
2951 struct v4l2_standard s;
2952 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2954 memset(parm,0,sizeof(*parm));
2955 v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
2956 bttv_tvnorms[btv->tvnorm].name);
2957 parm->parm.capture.timeperframe = s.frameperiod;
2961 case VIDIOC_G_PRIORITY:
2963 enum v4l2_priority *p = arg;
2965 *p = v4l2_prio_max(&btv->prio);
2968 case VIDIOC_S_PRIORITY:
2970 enum v4l2_priority *prio = arg;
2972 return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
2975 case VIDIOC_ENUMSTD:
2978 case VIDIOC_ENUMINPUT:
2979 case VIDIOC_G_INPUT:
2980 case VIDIOC_S_INPUT:
2981 case VIDIOC_G_TUNER:
2982 case VIDIOC_S_TUNER:
2983 case VIDIOC_G_FREQUENCY:
2984 case VIDIOC_S_FREQUENCY:
2985 case VIDIOC_LOG_STATUS:
2986 return bttv_common_ioctls(btv,cmd,arg);
2989 return -ENOIOCTLCMD;
2993 fh_unlock_and_return:
2994 mutex_unlock(&fh->cap.lock);
2998 static int bttv_ioctl(struct inode *inode, struct file *file,
2999 unsigned int cmd, unsigned long arg)
3001 struct bttv_fh *fh = file->private_data;
3005 bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
3006 return fh->lines * 2 * 2048;
3008 return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
3012 static ssize_t bttv_read(struct file *file, char __user *data,
3013 size_t count, loff_t *ppos)
3015 struct bttv_fh *fh = file->private_data;
3018 if (fh->btv->errors)
3019 bttv_reinit_bt848(fh->btv);
3020 dprintk("bttv%d: read count=%d type=%s\n",
3021 fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
3024 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3025 if (locked_btres(fh->btv,RESOURCE_VIDEO))
3027 retval = videobuf_read_one(&fh->cap, data, count, ppos,
3028 file->f_flags & O_NONBLOCK);
3030 case V4L2_BUF_TYPE_VBI_CAPTURE:
3031 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3033 retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
3034 file->f_flags & O_NONBLOCK);
3042 static unsigned int bttv_poll(struct file *file, poll_table *wait)
3044 struct bttv_fh *fh = file->private_data;
3045 struct bttv_buffer *buf;
3046 enum v4l2_field field;
3048 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
3049 if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
3051 return videobuf_poll_stream(file, &fh->vbi, wait);
3054 if (check_btres(fh,RESOURCE_VIDEO)) {
3055 /* streaming capture */
3056 if (list_empty(&fh->cap.stream))
3058 buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
3060 /* read() capture */
3061 mutex_lock(&fh->cap.lock);
3062 if (NULL == fh->cap.read_buf) {
3063 /* need to capture a new frame */
3064 if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
3065 mutex_unlock(&fh->cap.lock);
3068 fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
3069 if (NULL == fh->cap.read_buf) {
3070 mutex_unlock(&fh->cap.lock);
3073 fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
3074 field = videobuf_next_field(&fh->cap);
3075 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
3076 kfree (fh->cap.read_buf);
3077 fh->cap.read_buf = NULL;
3078 mutex_unlock(&fh->cap.lock);
3081 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
3082 fh->cap.read_off = 0;
3084 mutex_unlock(&fh->cap.lock);
3085 buf = (struct bttv_buffer*)fh->cap.read_buf;
3088 poll_wait(file, &buf->vb.done, wait);
3089 if (buf->vb.state == STATE_DONE ||
3090 buf->vb.state == STATE_ERROR)
3091 return POLLIN|POLLRDNORM;
3095 static int bttv_open(struct inode *inode, struct file *file)
3097 int minor = iminor(inode);
3098 struct bttv *btv = NULL;
3100 enum v4l2_buf_type type = 0;
3103 dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
3105 for (i = 0; i < bttv_num; i++) {
3106 if (bttvs[i].video_dev &&
3107 bttvs[i].video_dev->minor == minor) {
3109 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3112 if (bttvs[i].vbi_dev &&
3113 bttvs[i].vbi_dev->minor == minor) {
3115 type = V4L2_BUF_TYPE_VBI_CAPTURE;
3122 dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
3123 btv->c.nr,v4l2_type_names[type]);
3125 /* allocate per filehandle data */
3126 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
3129 file->private_data = fh;
3132 fh->ov.setup_ok = 0;
3133 v4l2_prio_open(&btv->prio,&fh->prio);
3135 videobuf_queue_init(&fh->cap, &bttv_video_qops,
3136 btv->c.pci, &btv->s_lock,
3137 V4L2_BUF_TYPE_VIDEO_CAPTURE,
3138 V4L2_FIELD_INTERLACED,
3139 sizeof(struct bttv_buffer),
3141 videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
3142 btv->c.pci, &btv->s_lock,
3143 V4L2_BUF_TYPE_VBI_CAPTURE,
3145 sizeof(struct bttv_buffer),
3147 i2c_vidiocschan(btv);
3150 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
3151 bttv_vbi_setlines(fh,btv,16);
3152 bttv_field_count(btv);
3156 static int bttv_release(struct inode *inode, struct file *file)
3158 struct bttv_fh *fh = file->private_data;
3159 struct bttv *btv = fh->btv;
3161 /* turn off overlay */
3162 if (check_btres(fh, RESOURCE_OVERLAY))
3163 bttv_switch_overlay(btv,fh,NULL);
3165 /* stop video capture */
3166 if (check_btres(fh, RESOURCE_VIDEO)) {
3167 videobuf_streamoff(&fh->cap);
3168 free_btres(btv,fh,RESOURCE_VIDEO);
3170 if (fh->cap.read_buf) {
3171 buffer_release(&fh->cap,fh->cap.read_buf);
3172 kfree(fh->cap.read_buf);
3175 /* stop vbi capture */
3176 if (check_btres(fh, RESOURCE_VBI)) {
3177 if (fh->vbi.streaming)
3178 videobuf_streamoff(&fh->vbi);
3179 if (fh->vbi.reading)
3180 videobuf_read_stop(&fh->vbi);
3181 free_btres(btv,fh,RESOURCE_VBI);
3185 videobuf_mmap_free(&fh->cap);
3186 videobuf_mmap_free(&fh->vbi);
3187 v4l2_prio_close(&btv->prio,&fh->prio);
3188 file->private_data = NULL;
3192 bttv_field_count(btv);
3197 bttv_mmap(struct file *file, struct vm_area_struct *vma)
3199 struct bttv_fh *fh = file->private_data;
3201 dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
3202 fh->btv->c.nr, v4l2_type_names[fh->type],
3203 vma->vm_start, vma->vm_end - vma->vm_start);
3204 return videobuf_mmap_mapper(bttv_queue(fh),vma);
3207 static struct file_operations bttv_fops =
3209 .owner = THIS_MODULE,
3211 .release = bttv_release,
3212 .ioctl = bttv_ioctl,
3213 .compat_ioctl = v4l_compat_ioctl32,
3214 .llseek = no_llseek,
3220 static struct video_device bttv_video_template =
3223 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
3224 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
3225 .hardware = VID_HARDWARE_BT848,
3230 static struct video_device bttv_vbi_template =
3232 .name = "bt848/878 vbi",
3233 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
3234 .hardware = VID_HARDWARE_BT848,
3239 /* ----------------------------------------------------------------------- */
3240 /* radio interface */
3242 static int radio_open(struct inode *inode, struct file *file)
3244 int minor = iminor(inode);
3245 struct bttv *btv = NULL;
3248 dprintk("bttv: open minor=%d\n",minor);
3250 for (i = 0; i < bttv_num; i++) {
3251 if (bttvs[i].radio_dev->minor == minor) {
3259 dprintk("bttv%d: open called (radio)\n",btv->c.nr);
3260 mutex_lock(&btv->lock);
3264 file->private_data = btv;
3266 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,NULL);
3267 audio_input(btv,TVAUDIO_INPUT_RADIO);
3269 mutex_unlock(&btv->lock);
3273 static int radio_release(struct inode *inode, struct file *file)
3275 struct bttv *btv = file->private_data;
3276 struct rds_command cmd;
3280 bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
3285 static int radio_do_ioctl(struct inode *inode, struct file *file,
3286 unsigned int cmd, void *arg)
3288 struct bttv *btv = file->private_data;
3293 struct video_capability *cap = arg;
3295 memset(cap,0,sizeof(*cap));
3296 strcpy(cap->name,btv->radio_dev->name);
3297 cap->type = VID_TYPE_TUNER;
3305 struct video_tuner *v = arg;
3309 memset(v,0,sizeof(*v));
3310 strcpy(v->name, "Radio");
3311 bttv_call_i2c_clients(btv,cmd,v);
3323 case VIDIOC_LOG_STATUS:
3324 return bttv_common_ioctls(btv,cmd,arg);
3327 return -ENOIOCTLCMD;
3332 static int radio_ioctl(struct inode *inode, struct file *file,
3333 unsigned int cmd, unsigned long arg)
3335 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
3338 static ssize_t radio_read(struct file *file, char __user *data,
3339 size_t count, loff_t *ppos)
3341 struct bttv *btv = file->private_data;
3342 struct rds_command cmd;
3343 cmd.block_count = count/3;
3345 cmd.instance = file;
3346 cmd.result = -ENODEV;
3348 bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
3353 static unsigned int radio_poll(struct file *file, poll_table *wait)
3355 struct bttv *btv = file->private_data;
3356 struct rds_command cmd;
3357 cmd.instance = file;
3358 cmd.event_list = wait;
3359 cmd.result = -ENODEV;
3360 bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
3365 static struct file_operations radio_fops =
3367 .owner = THIS_MODULE,
3370 .release = radio_release,
3371 .ioctl = radio_ioctl,
3372 .llseek = no_llseek,
3376 static struct video_device radio_template =
3378 .name = "bt848/878 radio",
3379 .type = VID_TYPE_TUNER,
3380 .hardware = VID_HARDWARE_BT848,
3381 .fops = &radio_fops,
3385 /* ----------------------------------------------------------------------- */
3386 /* some debug code */
3388 static int bttv_risc_decode(u32 risc)
3390 static char *instr[16] = {
3391 [ BT848_RISC_WRITE >> 28 ] = "write",
3392 [ BT848_RISC_SKIP >> 28 ] = "skip",
3393 [ BT848_RISC_WRITEC >> 28 ] = "writec",
3394 [ BT848_RISC_JUMP >> 28 ] = "jump",
3395 [ BT848_RISC_SYNC >> 28 ] = "sync",
3396 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
3397 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
3398 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
3400 static int incr[16] = {
3401 [ BT848_RISC_WRITE >> 28 ] = 2,
3402 [ BT848_RISC_JUMP >> 28 ] = 2,
3403 [ BT848_RISC_SYNC >> 28 ] = 2,
3404 [ BT848_RISC_WRITE123 >> 28 ] = 5,
3405 [ BT848_RISC_SKIP123 >> 28 ] = 2,
3406 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
3408 static char *bits[] = {
3409 "be0", "be1", "be2", "be3/resync",
3410 "set0", "set1", "set2", "set3",
3411 "clr0", "clr1", "clr2", "clr3",
3412 "irq", "res", "eol", "sol",
3416 printk("0x%08x [ %s", risc,
3417 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
3418 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
3419 if (risc & (1 << (i + 12)))
3420 printk(" %s",bits[i]);
3421 printk(" count=%d ]\n", risc & 0xfff);
3422 return incr[risc >> 28] ? incr[risc >> 28] : 1;
3425 static void bttv_risc_disasm(struct bttv *btv,
3426 struct btcx_riscmem *risc)
3430 printk("%s: risc disasm: %p [dma=0x%08lx]\n",
3431 btv->c.name, risc->cpu, (unsigned long)risc->dma);
3432 for (i = 0; i < (risc->size >> 2); i += n) {
3433 printk("%s: 0x%lx: ", btv->c.name,
3434 (unsigned long)(risc->dma + (i<<2)));
3435 n = bttv_risc_decode(risc->cpu[i]);
3436 for (j = 1; j < n; j++)
3437 printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
3438 btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
3440 if (0 == risc->cpu[i])
3445 static void bttv_print_riscaddr(struct bttv *btv)
3447 printk(" main: %08Lx\n",
3448 (unsigned long long)btv->main.dma);
3449 printk(" vbi : o=%08Lx e=%08Lx\n",
3450 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
3451 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
3452 printk(" cap : o=%08Lx e=%08Lx\n",
3453 btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
3454 btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
3455 printk(" scr : o=%08Lx e=%08Lx\n",
3456 btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
3457 btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
3458 bttv_risc_disasm(btv, &btv->main);
3461 /* ----------------------------------------------------------------------- */
3464 static char *irq_name[] = {
3465 "FMTCHG", // format change detected (525 vs. 625)
3466 "VSYNC", // vertical sync (new field)
3467 "HSYNC", // horizontal sync
3468 "OFLOW", // chroma/luma AGC overflow
3469 "HLOCK", // horizontal lock changed
3470 "VPRES", // video presence changed
3472 "I2CDONE", // hw irc operation finished
3473 "GPINT", // gpio port triggered irq
3475 "RISCI", // risc instruction triggered irq
3476 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
3477 "FTRGT", // pixel data fifo overrun
3478 "FDSR", // fifo data stream resyncronisation
3479 "PPERR", // parity error (data transfer)
3480 "RIPERR", // parity error (read risc instructions)
3481 "PABORT", // pci abort
3482 "OCERR", // risc instruction error
3483 "SCERR", // syncronisation error
3486 static void bttv_print_irqbits(u32 print, u32 mark)
3491 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
3492 if (print & (1 << i))
3493 printk(" %s",irq_name[i]);
3494 if (mark & (1 << i))
3499 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
3501 printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
3503 (unsigned long)btv->main.dma,
3504 (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
3505 (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
3508 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
3509 printk("bttv%d: Oh, there (temporarely?) is no input signal. "
3510 "Ok, then this is harmless, don't worry ;)\n",
3514 printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
3516 printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
3522 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
3524 struct bttv_buffer *item;
3526 memset(set,0,sizeof(*set));
3528 /* capture request ? */
3529 if (!list_empty(&btv->capture)) {
3531 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3532 if (V4L2_FIELD_HAS_TOP(item->vb.field))
3534 if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
3537 /* capture request for other field ? */
3538 if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
3539 (item->vb.queue.next != &btv->capture)) {
3540 item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
3541 if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
3542 if (NULL == set->top &&
3543 V4L2_FIELD_TOP == item->vb.field) {
3546 if (NULL == set->bottom &&
3547 V4L2_FIELD_BOTTOM == item->vb.field) {
3550 if (NULL != set->top && NULL != set->bottom)
3556 /* screen overlay ? */
3557 if (NULL != btv->screen) {
3558 if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
3559 if (NULL == set->top && NULL == set->bottom) {
3560 set->top = btv->screen;
3561 set->bottom = btv->screen;
3564 if (V4L2_FIELD_TOP == btv->screen->vb.field &&
3566 set->top = btv->screen;
3568 if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
3569 NULL == set->bottom) {
3570 set->bottom = btv->screen;
3575 dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
3576 btv->c.nr,set->top, set->bottom,
3577 btv->screen,set->frame_irq,set->top_irq);
3582 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
3583 struct bttv_buffer_set *curr, unsigned int state)
3587 do_gettimeofday(&ts);
3589 if (wakeup->top == wakeup->bottom) {
3590 if (NULL != wakeup->top && curr->top != wakeup->top) {
3592 printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
3593 wakeup->top->vb.ts = ts;
3594 wakeup->top->vb.field_count = btv->field_count;
3595 wakeup->top->vb.state = state;
3596 wake_up(&wakeup->top->vb.done);
3599 if (NULL != wakeup->top && curr->top != wakeup->top) {
3601 printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
3602 wakeup->top->vb.ts = ts;
3603 wakeup->top->vb.field_count = btv->field_count;
3604 wakeup->top->vb.state = state;
3605 wake_up(&wakeup->top->vb.done);
3607 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
3609 printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
3610 wakeup->bottom->vb.ts = ts;
3611 wakeup->bottom->vb.field_count = btv->field_count;
3612 wakeup->bottom->vb.state = state;
3613 wake_up(&wakeup->bottom->vb.done);
3619 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
3627 do_gettimeofday(&ts);
3629 wakeup->vb.field_count = btv->field_count;
3630 wakeup->vb.state = state;
3631 wake_up(&wakeup->vb.done);
3634 static void bttv_irq_timeout(unsigned long data)
3636 struct bttv *btv = (struct bttv *)data;
3637 struct bttv_buffer_set old,new;
3638 struct bttv_buffer *ovbi;
3639 struct bttv_buffer *item;
3640 unsigned long flags;
3643 printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
3644 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
3645 btread(BT848_RISC_COUNT));
3646 bttv_print_irqbits(btread(BT848_INT_STAT),0);
3650 spin_lock_irqsave(&btv->s_lock,flags);
3652 /* deactivate stuff */
3653 memset(&new,0,sizeof(new));
3659 bttv_buffer_activate_video(btv, &new);
3660 bttv_buffer_activate_vbi(btv, NULL);
3661 bttv_set_dma(btv, 0);
3664 bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
3665 bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
3667 /* cancel all outstanding capture / vbi requests */
3668 while (!list_empty(&btv->capture)) {
3669 item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
3670 list_del(&item->vb.queue);
3671 item->vb.state = STATE_ERROR;
3672 wake_up(&item->vb.done);
3674 while (!list_empty(&btv->vcapture)) {
3675 item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3676 list_del(&item->vb.queue);
3677 item->vb.state = STATE_ERROR;
3678 wake_up(&item->vb.done);
3682 spin_unlock_irqrestore(&btv->s_lock,flags);
3686 bttv_irq_wakeup_top(struct bttv *btv)
3688 struct bttv_buffer *wakeup = btv->curr.top;
3693 spin_lock(&btv->s_lock);
3694 btv->curr.top_irq = 0;
3695 btv->curr.top = NULL;
3696 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
3698 do_gettimeofday(&wakeup->vb.ts);
3699 wakeup->vb.field_count = btv->field_count;
3700 wakeup->vb.state = STATE_DONE;
3701 wake_up(&wakeup->vb.done);
3702 spin_unlock(&btv->s_lock);
3705 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
3709 if (rc > risc->dma + risc->size)
3715 bttv_irq_switch_video(struct bttv *btv)
3717 struct bttv_buffer_set new;
3718 struct bttv_buffer_set old;
3721 spin_lock(&btv->s_lock);
3723 /* new buffer set */
3724 bttv_irq_next_video(btv, &new);
3725 rc = btread(BT848_RISC_COUNT);
3726 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
3727 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
3730 bttv_irq_debug_low_latency(btv, rc);
3731 spin_unlock(&btv->s_lock);
3738 btv->loop_irq &= ~1;
3739 bttv_buffer_activate_video(btv, &new);
3740 bttv_set_dma(btv, 0);
3743 if (UNSET != btv->new_input) {
3744 video_mux(btv,btv->new_input);
3745 btv->new_input = UNSET;
3748 /* wake up finished buffers */
3749 bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
3750 spin_unlock(&btv->s_lock);
3754 bttv_irq_switch_vbi(struct bttv *btv)
3756 struct bttv_buffer *new = NULL;
3757 struct bttv_buffer *old;
3760 spin_lock(&btv->s_lock);
3762 if (!list_empty(&btv->vcapture))
3763 new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
3766 rc = btread(BT848_RISC_COUNT);
3767 if (NULL != old && (is_active(&old->top, rc) ||
3768 is_active(&old->bottom, rc))) {
3771 bttv_irq_debug_low_latency(btv, rc);
3772 spin_unlock(&btv->s_lock);
3778 btv->loop_irq &= ~4;
3779 bttv_buffer_activate_vbi(btv, new);
3780 bttv_set_dma(btv, 0);
3782 bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
3783 spin_unlock(&btv->s_lock);
3786 static irqreturn_t bttv_irq(int irq, void *dev_id)
3794 btv=(struct bttv *)dev_id;
3796 if (btv->custom_irq)
3797 handled = btv->custom_irq(btv);
3801 /* get/clear interrupt status bits */
3802 stat=btread(BT848_INT_STAT);
3803 astat=stat&btread(BT848_INT_MASK);
3807 btwrite(stat,BT848_INT_STAT);
3809 /* get device status bits */
3810 dstat=btread(BT848_DSTATUS);
3813 printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
3814 "riscs=%x, riscc=%08x, ",
3815 btv->c.nr, count, btv->field_count,
3816 stat>>28, btread(BT848_RISC_COUNT));
3817 bttv_print_irqbits(stat,astat);
3818 if (stat & BT848_INT_HLOCK)
3819 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
3821 if (stat & BT848_INT_VPRES)
3822 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
3824 if (stat & BT848_INT_FMTCHG)
3825 printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
3830 if (astat&BT848_INT_VSYNC)
3833 if ((astat & BT848_INT_GPINT) && btv->remote) {
3834 wake_up(&btv->gpioq);
3835 bttv_input_irq(btv);
3838 if (astat & BT848_INT_I2CDONE) {
3839 btv->i2c_done = stat;
3840 wake_up(&btv->i2c_queue);
3843 if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
3844 bttv_irq_switch_vbi(btv);
3846 if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
3847 bttv_irq_wakeup_top(btv);
3849 if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
3850 bttv_irq_switch_video(btv);
3852 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3853 audio_mute(btv, btv->mute); /* trigger automute */
3855 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3856 printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
3857 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3858 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3859 btread(BT848_RISC_COUNT));
3860 bttv_print_irqbits(stat,astat);
3863 bttv_print_riscaddr(btv);
3865 if (fdsr && astat & BT848_INT_FDSR) {
3866 printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
3867 btv->c.nr,btread(BT848_RISC_COUNT));
3869 bttv_print_riscaddr(btv);
3875 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3876 btwrite(0, BT848_INT_MASK);
3879 "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
3882 "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
3884 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3888 bttv_print_irqbits(stat,astat);
3896 return IRQ_RETVAL(handled);
3900 /* ----------------------------------------------------------------------- */
3901 /* initialitation */
3903 static struct video_device *vdev_init(struct bttv *btv,
3904 struct video_device *template,
3907 struct video_device *vfd;
3909 vfd = video_device_alloc();
3914 vfd->dev = &btv->c.pci->dev;
3915 vfd->release = video_device_release;
3916 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3917 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3918 type, bttv_tvcards[btv->c.type].name);
3922 static void bttv_unregister_video(struct bttv *btv)
3924 if (btv->video_dev) {
3925 if (-1 != btv->video_dev->minor)
3926 video_unregister_device(btv->video_dev);
3928 video_device_release(btv->video_dev);
3929 btv->video_dev = NULL;
3932 if (-1 != btv->vbi_dev->minor)
3933 video_unregister_device(btv->vbi_dev);
3935 video_device_release(btv->vbi_dev);
3936 btv->vbi_dev = NULL;
3938 if (btv->radio_dev) {
3939 if (-1 != btv->radio_dev->minor)
3940 video_unregister_device(btv->radio_dev);
3942 video_device_release(btv->radio_dev);
3943 btv->radio_dev = NULL;
3947 /* register video4linux devices */
3948 static int __devinit bttv_register_video(struct bttv *btv)
3950 if (no_overlay <= 0) {
3951 bttv_video_template.type |= VID_TYPE_OVERLAY;
3953 printk("bttv: Overlay support disabled.\n");
3957 btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
3958 if (NULL == btv->video_dev)
3960 if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
3962 printk(KERN_INFO "bttv%d: registered device video%d\n",
3963 btv->c.nr,btv->video_dev->minor & 0x1f);
3964 if (class_device_create_file(&btv->video_dev->class_dev,
3965 &class_device_attr_card)<0) {
3966 printk(KERN_ERR "bttv%d: class_device_create_file 'card' "
3967 "failed\n", btv->c.nr);
3972 btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
3973 if (NULL == btv->vbi_dev)
3975 if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
3977 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
3978 btv->c.nr,btv->vbi_dev->minor & 0x1f);
3980 if (!btv->has_radio)
3983 btv->radio_dev = vdev_init(btv, &radio_template, "radio");
3984 if (NULL == btv->radio_dev)
3986 if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
3988 printk(KERN_INFO "bttv%d: registered device radio%d\n",
3989 btv->c.nr,btv->radio_dev->minor & 0x1f);
3995 bttv_unregister_video(btv);
4000 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
4001 /* response on cards with no firmware is not enabled by OF */
4002 static void pci_set_command(struct pci_dev *dev)
4004 #if defined(__powerpc__)
4007 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
4008 cmd = (cmd | PCI_COMMAND_MEMORY );
4009 pci_write_config_dword(dev, PCI_COMMAND, cmd);
4013 static int __devinit bttv_probe(struct pci_dev *dev,
4014 const struct pci_device_id *pci_id)
4020 if (bttv_num == BTTV_MAX)
4022 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
4023 btv=&bttvs[bttv_num];
4024 memset(btv,0,sizeof(*btv));
4025 btv->c.nr = bttv_num;
4026 sprintf(btv->c.name,"bttv%d",btv->c.nr);
4028 /* initialize structs / fill in defaults */
4029 mutex_init(&btv->lock);
4030 mutex_init(&btv->reslock);
4031 spin_lock_init(&btv->s_lock);
4032 spin_lock_init(&btv->gpio_lock);
4033 init_waitqueue_head(&btv->gpioq);
4034 init_waitqueue_head(&btv->i2c_queue);
4035 INIT_LIST_HEAD(&btv->c.subs);
4036 INIT_LIST_HEAD(&btv->capture);
4037 INIT_LIST_HEAD(&btv->vcapture);
4038 v4l2_prio_init(&btv->prio);
4040 init_timer(&btv->timeout);
4041 btv->timeout.function = bttv_irq_timeout;
4042 btv->timeout.data = (unsigned long)btv;
4045 btv->tuner_type = UNSET;
4046 btv->new_input = UNSET;
4047 btv->has_radio=radio[btv->c.nr];
4049 /* pci stuff (init, get irq/mmio, ... */
4051 btv->id = dev->device;
4052 if (pci_enable_device(dev)) {
4053 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4057 if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
4058 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
4062 if (!request_mem_region(pci_resource_start(dev,0),
4063 pci_resource_len(dev,0),
4065 printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
4067 (unsigned long long)pci_resource_start(dev,0));
4070 pci_set_master(dev);
4071 pci_set_command(dev);
4072 pci_set_drvdata(dev,btv);
4074 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
4075 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
4076 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
4077 bttv_num,btv->id, btv->revision, pci_name(dev));
4078 printk("irq: %d, latency: %d, mmio: 0x%llx\n",
4079 btv->c.pci->irq, lat,
4080 (unsigned long long)pci_resource_start(dev,0));
4083 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
4084 if (NULL == btv->bt848_mmio) {
4085 printk("bttv%d: ioremap() failed\n", btv->c.nr);
4093 /* disable irqs, register irq handler */
4094 btwrite(0, BT848_INT_MASK);
4095 result = request_irq(btv->c.pci->irq, bttv_irq,
4096 IRQF_SHARED | IRQF_DISABLED,btv->c.name,(void *)btv);
4098 printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
4099 bttv_num,btv->c.pci->irq);
4103 if (0 != bttv_handle_chipset(btv)) {
4108 /* init options from insmod args */
4109 btv->opt_combfilter = combfilter;
4110 btv->opt_lumafilter = lumafilter;
4111 btv->opt_automute = automute;
4112 btv->opt_chroma_agc = chroma_agc;
4113 btv->opt_adc_crush = adc_crush;
4114 btv->opt_vcr_hack = vcr_hack;
4115 btv->opt_whitecrush_upper = whitecrush_upper;
4116 btv->opt_whitecrush_lower = whitecrush_lower;
4117 btv->opt_uv_ratio = uv_ratio;
4118 btv->opt_full_luma_range = full_luma_range;
4119 btv->opt_coring = coring;
4121 /* fill struct bttv with some useful defaults */
4122 btv->init.btv = btv;
4123 btv->init.ov.w.width = 320;
4124 btv->init.ov.w.height = 240;
4125 btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
4126 btv->init.width = 320;
4127 btv->init.height = 240;
4128 btv->init.lines = 16;
4131 /* initialize hardware */
4133 bttv_gpio_tracking(btv,"pre-init");
4135 bttv_risc_init_main(btv);
4139 btwrite(0x00, BT848_GPIO_REG_INP);
4140 btwrite(0x00, BT848_GPIO_OUT_EN);
4142 bttv_gpio_tracking(btv,"init");
4144 /* needs to be done before i2c is registered */
4145 bttv_init_card1(btv);
4147 /* register i2c + gpio */
4150 /* some card-specific stuff (needs working i2c) */
4151 bttv_init_card2(btv);
4154 /* register video4linux + input */
4155 if (!bttv_tvcards[btv->c.type].no_video) {
4156 bttv_register_video(btv);
4157 bt848_bright(btv,32768);
4158 bt848_contrast(btv,32768);
4159 bt848_hue(btv,32768);
4160 bt848_sat(btv,32768);
4165 /* add subdevices and autoload dvb-bt8xx if needed */
4166 if (bttv_tvcards[btv->c.type].has_dvb) {
4167 bttv_sub_add_device(&btv->c, "dvb");
4168 request_modules(btv);
4171 bttv_input_init(btv);
4173 /* everything is fine */
4178 free_irq(btv->c.pci->irq,btv);
4181 if (btv->bt848_mmio)
4182 iounmap(btv->bt848_mmio);
4183 release_mem_region(pci_resource_start(btv->c.pci,0),
4184 pci_resource_len(btv->c.pci,0));
4185 pci_set_drvdata(dev,NULL);
4189 static void __devexit bttv_remove(struct pci_dev *pci_dev)
4191 struct bttv *btv = pci_get_drvdata(pci_dev);
4194 printk("bttv%d: unloading\n",btv->c.nr);
4196 /* shutdown everything (DMA+IRQs) */
4197 btand(~15, BT848_GPIO_DMA_CTL);
4198 btwrite(0, BT848_INT_MASK);
4199 btwrite(~0x0, BT848_INT_STAT);
4200 btwrite(0x0, BT848_GPIO_OUT_EN);
4202 bttv_gpio_tracking(btv,"cleanup");
4204 /* tell gpio modules we are leaving ... */
4206 wake_up(&btv->gpioq);
4207 bttv_input_fini(btv);
4208 bttv_sub_del_devices(&btv->c);
4210 /* unregister i2c_bus + input */
4213 /* unregister video4linux */
4214 bttv_unregister_video(btv);
4216 /* free allocated memory */
4217 btcx_riscmem_free(btv->c.pci,&btv->main);
4219 /* free ressources */
4220 free_irq(btv->c.pci->irq,btv);
4221 iounmap(btv->bt848_mmio);
4222 release_mem_region(pci_resource_start(btv->c.pci,0),
4223 pci_resource_len(btv->c.pci,0));
4225 pci_set_drvdata(pci_dev, NULL);
4230 static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
4232 struct bttv *btv = pci_get_drvdata(pci_dev);
4233 struct bttv_buffer_set idle;
4234 unsigned long flags;
4236 dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
4238 /* stop dma + irqs */
4239 spin_lock_irqsave(&btv->s_lock,flags);
4240 memset(&idle, 0, sizeof(idle));
4241 btv->state.video = btv->curr;
4242 btv->state.vbi = btv->cvbi;
4243 btv->state.loop_irq = btv->loop_irq;
4246 bttv_buffer_activate_video(btv, &idle);
4247 bttv_buffer_activate_vbi(btv, NULL);
4248 bttv_set_dma(btv, 0);
4249 btwrite(0, BT848_INT_MASK);
4250 spin_unlock_irqrestore(&btv->s_lock,flags);
4252 /* save bt878 state */
4253 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
4254 btv->state.gpio_data = gpio_read();
4256 /* save pci state */
4257 pci_save_state(pci_dev);
4258 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
4259 pci_disable_device(pci_dev);
4260 btv->state.disabled = 1;
4265 static int bttv_resume(struct pci_dev *pci_dev)
4267 struct bttv *btv = pci_get_drvdata(pci_dev);
4268 unsigned long flags;
4271 dprintk("bttv%d: resume\n", btv->c.nr);
4273 /* restore pci state */
4274 if (btv->state.disabled) {
4275 err=pci_enable_device(pci_dev);
4277 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4281 btv->state.disabled = 0;
4283 err=pci_set_power_state(pci_dev, PCI_D0);
4285 pci_disable_device(pci_dev);
4286 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
4288 btv->state.disabled = 1;
4292 pci_restore_state(pci_dev);
4294 /* restore bt878 state */
4295 bttv_reinit_bt848(btv);
4296 gpio_inout(0xffffff, btv->state.gpio_enable);
4297 gpio_write(btv->state.gpio_data);
4300 spin_lock_irqsave(&btv->s_lock,flags);
4301 btv->curr = btv->state.video;
4302 btv->cvbi = btv->state.vbi;
4303 btv->loop_irq = btv->state.loop_irq;
4304 bttv_buffer_activate_video(btv, &btv->curr);
4305 bttv_buffer_activate_vbi(btv, btv->cvbi);
4306 bttv_set_dma(btv, 0);
4307 spin_unlock_irqrestore(&btv->s_lock,flags);
4312 static struct pci_device_id bttv_pci_tbl[] = {
4313 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
4314 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4315 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
4316 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4317 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
4318 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4319 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
4320 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4324 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
4326 static struct pci_driver bttv_pci_driver = {
4328 .id_table = bttv_pci_tbl,
4329 .probe = bttv_probe,
4330 .remove = __devexit_p(bttv_remove),
4332 .suspend = bttv_suspend,
4333 .resume = bttv_resume,
4337 static int bttv_init_module(void)
4343 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
4344 (BTTV_VERSION_CODE >> 16) & 0xff,
4345 (BTTV_VERSION_CODE >> 8) & 0xff,
4346 BTTV_VERSION_CODE & 0xff);
4348 printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
4349 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
4351 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
4353 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
4354 gbufsize = BTTV_MAX_FBUF;
4355 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
4357 printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
4358 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
4360 bttv_check_chipset();
4362 ret = bus_register(&bttv_sub_bus_type);
4364 printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
4367 return pci_register_driver(&bttv_pci_driver);
4370 static void bttv_cleanup_module(void)
4372 pci_unregister_driver(&bttv_pci_driver);
4373 bus_unregister(&bttv_sub_bus_type);
4377 module_init(bttv_init_module);
4378 module_exit(bttv_cleanup_module);