2 bttv - Bt848 frame grabber driver
4 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
5 & Marcus Metzler (mocm@thp.uni-koeln.de)
6 (c) 1999-2003 Gerd Knorr <kraxel@goldbach.in-berlin.de>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/major.h>
29 #include <linux/slab.h>
31 #include <linux/poll.h>
32 #include <linux/pci.h>
33 #include <linux/signal.h>
34 #include <linux/ioport.h>
35 #include <linux/sched.h>
36 #include <linux/types.h>
37 #include <linux/wrapper.h>
38 #include <linux/interrupt.h>
39 #include <linux/kmod.h>
40 #include <linux/vmalloc.h>
41 #include <linux/init.h>
42 #include <linux/pagemap.h>
45 #include <asm/pgtable.h>
47 #include <asm/byteorder.h>
51 #define DEBUG(x) /* Debug driver */
52 #define MIN(a,b) (((a)>(b))?(b):(a))
53 #define MAX(a,b) (((a)>(b))?(a):(b))
56 static void bt848_set_risc_jmps(struct bttv *btv, int state);
57 static void make_vbitab(struct bttv *btv);
58 static void bt848_set_winsize(struct bttv *btv);
60 unsigned int bttv_num; /* number of Bt848s in use */
61 struct bttv bttvs[BTTV_MAX];
63 /* configuration variables */
65 static unsigned int bigendian=1;
67 static unsigned int bigendian=0;
69 static unsigned int radio[BTTV_MAX];
70 static unsigned int fieldnr = 0;
71 static unsigned int gpint = 1;
72 static unsigned int irq_debug = 0;
73 static unsigned int gbuffers = 4;
74 static unsigned int gbufsize = BTTV_MAX_FBUF;
76 static unsigned int combfilter = 0;
77 static unsigned int lumafilter = 0;
78 static unsigned int automute = 1;
79 static unsigned int chroma_agc = 0;
80 static unsigned int adc_crush = 1;
81 static int video_nr = -1;
82 static int radio_nr = -1;
83 static int vbi_nr = -1;
84 unsigned int bttv_debug = 0;
85 unsigned int bttv_verbose = 1;
86 unsigned int bttv_gpio = 0;
89 MODULE_PARM(radio,"1-" __stringify(BTTV_MAX) "i");
90 MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
91 MODULE_PARM(bigendian,"i");
92 MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
93 MODULE_PARM(fieldnr,"i");
94 MODULE_PARM_DESC(fieldnr,"count fields, default is 0 (no)");
95 MODULE_PARM(bttv_verbose,"i");
96 MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
97 MODULE_PARM(bttv_gpio,"i");
98 MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
99 MODULE_PARM(bttv_debug,"i");
100 MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
101 MODULE_PARM(irq_debug,"i");
102 MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
103 MODULE_PARM(gbuffers,"i");
104 MODULE_PARM_DESC(gbuffers,"number of capture buffers, default is 2 (64 max)");
105 MODULE_PARM(gbufsize,"i");
106 MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
107 MODULE_PARM(gpint,"i");
109 MODULE_PARM(combfilter,"i");
110 MODULE_PARM(lumafilter,"i");
111 MODULE_PARM(automute,"i");
112 MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
113 MODULE_PARM(chroma_agc,"i");
114 MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
115 MODULE_PARM(adc_crush,"i");
116 MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
118 MODULE_PARM(video_nr,"i");
119 MODULE_PARM(radio_nr,"i");
120 MODULE_PARM(vbi_nr,"i");
122 MODULE_DESCRIPTION("bttv - v4l driver module for bt848/878 based cards");
123 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
124 MODULE_LICENSE("GPL");
128 static int __init p_radio(char *str) { return bttv_parse(str,BTTV_MAX,radio); }
129 __setup("bttv.radio=", p_radio);
132 #define I2C_TIMING (0x7<<4)
135 #define I2C_SET(CTRL,DATA) \
136 { btwrite((CTRL<<1)|(DATA), BT848_I2C); udelay(I2C_DELAY); }
137 #define I2C_GET() (btread(BT848_I2C)&1)
139 #define BURSTOFFSET 76
140 #define BTTV_ERRORS 5
143 /*******************************/
144 /* Memory management functions */
145 /*******************************/
148 static inline unsigned long kvirt_to_bus(unsigned long adr)
152 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
153 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
154 return virt_to_bus((void *)kva);
157 /* Here we want the physical address of the memory.
158 * This is used when initializing the contents of the
159 * area and marking the pages as reserved.
161 static inline unsigned long kvirt_to_pa(unsigned long adr)
165 kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
166 kva |= adr & (PAGE_SIZE-1); /* restore the offset */
170 static void * rvmalloc(signed long size)
176 mem=vmalloc_32(size);
178 printk(KERN_INFO "bttv: vmalloc_32(%ld) failed\n",size);
180 /* Clear the ram out, no junk to the user */
181 memset(mem, 0, size);
182 adr=(unsigned long) mem;
184 page = vmalloc_to_page((void *)adr);
185 mem_map_reserve(page);
193 static void rvfree(void * mem, signed long size)
199 adr=(unsigned long) mem;
201 page = vmalloc_to_page((void *)adr);
202 mem_map_unreserve(page);
213 * Create the giant waste of buffer space we need for now
214 * until we get DMA to user space sorted out (probably 2.3.x)
216 * We only create this as and when someone uses mmap
219 static int fbuffer_alloc(struct bttv *btv)
222 btv->fbuffer=(unsigned char *) rvmalloc(gbuffers*gbufsize);
224 printk(KERN_ERR "bttv%d: Double alloc of fbuffer!\n",
231 /* ----------------------------------------------------------------------- */
233 void bttv_gpio_tracking(struct bttv *btv, char *comment)
235 unsigned int outbits, data;
236 outbits = btread(BT848_GPIO_OUT_EN);
237 data = btread(BT848_GPIO_DATA);
238 printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
239 btv->nr,outbits,data & outbits, data & ~outbits, comment);
242 static char *audio_modes[] = { "audio: tuner", "audio: radio", "audio: extern",
243 "audio: intern", "audio: off" };
245 static void audio(struct bttv *btv, int mode)
247 if (bttv_tvcards[btv->type].gpiomask)
248 btaor(bttv_tvcards[btv->type].gpiomask,
249 ~bttv_tvcards[btv->type].gpiomask,
255 btv->audio|=AUDIO_MUTE;
258 btv->audio&=~AUDIO_MUTE;
268 btv->audio&=AUDIO_MUTE;
272 /* if audio mute or not in H-lock, turn audio off */
273 if ((btv->audio&AUDIO_MUTE))
275 if ((mode == AUDIO_TUNER) && (btv->radio))
277 if (bttv_tvcards[btv->type].gpiomask)
278 btaor(bttv_tvcards[btv->type].audiomux[mode],
279 ~bttv_tvcards[btv->type].gpiomask,
282 bttv_gpio_tracking(btv,audio_modes[mode]);
284 bttv_call_i2c_clients(btv,AUDC_SET_INPUT,&(mode));
288 static inline void bt848_dma(struct bttv *btv, uint state)
291 btor(3, BT848_GPIO_DMA_CTL);
293 btand(~3, BT848_GPIO_DMA_CTL);
297 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC*/
299 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
300 PLL_X = Reference pre-divider (0=1, 1=2)
301 PLL_C = Post divider (0=6, 1=4)
302 PLL_I = Integer input
303 PLL_F = Fractional input
305 F_input = 28.636363 MHz:
306 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
309 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
311 unsigned char fl, fh, fi;
313 /* prevent overflows */
326 /*printk("0x%02x 0x%02x 0x%02x\n", fi, fh, fl);*/
327 btwrite(fl, BT848_PLL_F_LO);
328 btwrite(fh, BT848_PLL_F_HI);
329 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
332 static void set_pll(struct bttv *btv)
336 if (!btv->pll.pll_crystal)
339 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
340 dprintk("bttv%d: PLL: no change required\n",btv->nr);
344 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
346 if (btv->pll.pll_current == 0)
348 vprintk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
349 btv->nr,btv->pll.pll_ifreq);
350 btwrite(0x00,BT848_TGCTRL);
351 btwrite(0x00,BT848_PLL_XCI);
352 btv->pll.pll_current = 0;
356 vprintk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->nr,
357 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
358 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
360 for (i=0; i<10; i++) {
361 /* Let other people run while the PLL stabilizes */
363 current->state = TASK_INTERRUPTIBLE;
364 schedule_timeout(HZ/10);
366 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
367 btwrite(0,BT848_DSTATUS);
369 btwrite(0x08,BT848_TGCTRL);
370 btv->pll.pll_current = btv->pll.pll_ofreq;
375 btv->pll.pll_current = -1;
380 static void bt848_muxsel(struct bttv *btv, unsigned int input)
382 dprintk("bttv%d: bt848_muxsel %d\n",btv->nr,input);
384 if (bttv_tvcards[btv->type].muxsel[input] < 0) {
385 dprintk("bttv%d: digital ccir muxsel\n", btv->nr);
386 btv->channel = input;
390 /* needed by RemoteVideo MX */
391 if (bttv_tvcards[btv->type].gpiomask2)
392 btaor(bttv_tvcards[btv->type].gpiomask2,
393 ~bttv_tvcards[btv->type].gpiomask2,
397 /* This seems to get rid of some synchronization problems */
398 btand(~(3<<5), BT848_IFORM);
402 input %= bttv_tvcards[btv->type].video_inputs;
403 if (input == btv->svhs) {
404 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
405 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
407 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
408 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
411 btaor((bttv_tvcards[btv->type].muxsel[input]&3)<<5, ~(3<<5), BT848_IFORM);
412 audio(btv, (input!=bttv_tvcards[btv->type].tuner) ?
413 AUDIO_EXTERN : AUDIO_TUNER);
415 if (bttv_tvcards[btv->type].gpiomask2)
416 btaor(bttv_tvcards[btv->type].muxsel[input]>>4,
417 ~bttv_tvcards[btv->type].gpiomask2,
420 /* card specific hook */
421 if (bttv_tvcards[btv->type].muxsel_hook)
422 bttv_tvcards[btv->type].muxsel_hook(btv, input);
425 bttv_gpio_tracking(btv,"muxsel");
430 /* special timing tables from conexant... */
431 static u8 SRAM_Table[][60] =
433 /* PAL digital input over GPIO[7:0] */
435 45, // 45 bytes following
436 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
437 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
438 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
439 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
440 0x37,0x00,0xAF,0x21,0x00
442 /* NTSC digital input over GPIO[7:0] */
444 51, // 51 bytes following
445 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
446 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
447 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
448 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
449 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
452 // TGB_NTSC392 // quartzsight
453 // This table has been modified to be used for Fusion Rev D
455 0x2A, // size of table = 42
456 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
457 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
458 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
459 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
467 u16 swidth, sheight; /* scaled standard width, height */
469 u8 adelay, bdelay, iform;
471 u16 hdelayx1, hactivex1;
474 int sram; /* index into SRAM_Table */
477 static struct tvnorm tvnorms[] = {
479 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
480 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
488 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
489 .scaledtwidth = 1135,
493 .vdelay = VBI_MAXLINES*2,
507 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
522 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
523 .scaledtwidth = 1135,
528 .sram = 0, /* like PAL, correct? */
537 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
552 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
567 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
582 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
590 /* Quartzsight digital camera
591 * From Bt832 datasheet: 393x304 pixel @30Hz,
592 * Visible: 352x288 pixel
596 .sheight = 576, //2*288 ?
600 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
609 #define TVNORMS ARRAY_SIZE(tvnorms)
611 /* used to switch between the bt848's analog/digital video capture modes */
612 void bt848A_set_timing(struct bttv *btv)
615 int table_idx = tvnorms[btv->win.norm].sram;
616 int fsc = tvnorms[btv->win.norm].Fsc;
618 if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0) {
619 dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
622 /* timing change...reset timing generator address */
623 btwrite(0x00, BT848_TGCTRL);
624 btwrite(0x02, BT848_TGCTRL);
625 btwrite(0x00, BT848_TGCTRL);
627 len=SRAM_Table[table_idx][0];
628 for(i = 1; i <= len; i++)
629 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
630 btv->pll.pll_ofreq = 27000000;
633 btwrite(0x11, BT848_TGCTRL);
634 btwrite(0x41, BT848_DVSIF);
636 btv->pll.pll_ofreq = fsc;
638 btwrite(0x0, BT848_DVSIF);
642 static void bttv_set_norm(struct bttv *btv, int new_norm)
644 unsigned long irq_flags;
646 if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0 &&
647 bttv_tvcards[btv->type].digital_mode == DIGITAL_MODE_CAMERA) {
648 //override norm by quartzsight mode
650 dprintk("bttv%d: set_norm fix-up digital: %d\n",
654 if (btv->win.norm != new_norm) {
655 btv->win.norm = new_norm;
658 spin_lock_irqsave(&btv->s_lock, irq_flags);
659 bt848_set_winsize(btv);
660 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
662 bt848A_set_timing(btv);
664 case BTTV_VOODOOTV_FM:
665 bttv_tda9880_setnorm(btv,new_norm);
669 osprey_540_set_norm(btv,new_norm);
677 /* RISC command to write one VBI data line */
678 #define VBI_RISC BT848_RISC_WRITE|VBI_SPL|BT848_RISC_EOL|BT848_RISC_SOL
680 static void make_vbitab(struct bttv *btv)
683 unsigned int *po=(unsigned int *) btv->vbi_odd;
684 unsigned int *pe=(unsigned int *) btv->vbi_even;
687 printk("bttv%d: vbi1: po=%08lx pe=%08lx\n",
688 btv->nr,virt_to_bus(po), virt_to_bus(pe));
690 *(po++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1); *(po++)=0;
691 for (i=0; i<VBI_MAXLINES; i++) {
692 *(po++)=cpu_to_le32(VBI_RISC);
693 *(po++)=cpu_to_le32(kvirt_to_bus((unsigned long)btv->vbibuf+i*2048));
695 *(po++)=cpu_to_le32(BT848_RISC_JUMP);
696 *(po++)=cpu_to_le32(virt_to_bus(btv->risc_jmp+4));
698 *(pe++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1); *(pe++)=0;
699 for (i=VBI_MAXLINES; i<VBI_MAXLINES*2; i++) {
700 *(pe++)=cpu_to_le32(VBI_RISC);
701 *(pe++)=cpu_to_le32(kvirt_to_bus((unsigned long)btv->vbibuf+i*2048));
703 *(pe++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(0x01<<16));
704 *(pe++)=cpu_to_le32(virt_to_bus(btv->risc_jmp+10));
707 printk("bttv%d: vbi2: po=%08lx pe=%08lx\n",
708 btv->nr,virt_to_bus(po), virt_to_bus(pe));
711 static unsigned int fmtbppx2[16] = {
712 8, 6, 4, 4, 4, 3, 2, 2, 4, 3, 0, 0, 0, 0, 2, 0
715 static unsigned int palette2fmt[] = {
718 BT848_COLOR_FMT_RGB8,
719 BT848_COLOR_FMT_RGB16,
720 BT848_COLOR_FMT_RGB24,
721 BT848_COLOR_FMT_RGB32,
722 BT848_COLOR_FMT_RGB15,
723 BT848_COLOR_FMT_YUY2,
724 BT848_COLOR_FMT_YUY2,
729 BT848_COLOR_FMT_YCrCb422,
730 BT848_COLOR_FMT_YCrCb411,
731 BT848_COLOR_FMT_YCrCb422,
732 BT848_COLOR_FMT_YCrCb411,
734 #define PALETTEFMT_MAX ARRAY_SIZE(palette2fmt)
736 static int make_rawrisctab(struct bttv *btv, u32 *ro, u32 *re, u32 *vbuf)
739 u32 bpl=1024; /* bytes per line */
740 unsigned long vadr=(unsigned long) vbuf;
742 *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
743 *(ro++)=cpu_to_le32(0);
744 *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
745 *(re++)=cpu_to_le32(0);
747 /* In PAL 650 blocks of 256 DWORDs are sampled, but only if VDELAY
748 is 2 and without separate VBI grabbing.
749 We'll have to handle this inside the IRQ handler ... */
751 for (line=0; line < 640; line++)
753 *(ro++)=cpu_to_le32(BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL);
754 *(ro++)=cpu_to_le32(kvirt_to_bus(vadr));
755 *(re++)=cpu_to_le32(BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL);
756 *(re++)=cpu_to_le32(kvirt_to_bus(vadr+gbufsize/2));
760 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
761 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
762 *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
763 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
768 static int make_prisctab(struct bttv *btv, u32 *ro, u32 *re, u32 *vbuf,
769 u16 width, u16 height, u16 fmt)
772 u32 bl, blcr, blcb, rcmd;
776 unsigned long cbadr, cradr;
777 unsigned long vadr=(unsigned long) vbuf;
781 printk("bttv%d: prisc1: ro=%08lx re=%08lx\n",
782 btv->nr,virt_to_bus(ro), virt_to_bus(re));
786 case VIDEO_PALETTE_YUV422P:
787 csize=(width*height)>>1;
792 case VIDEO_PALETTE_YUV411P:
793 csize=(width*height)>>2;
798 case VIDEO_PALETTE_YUV420P:
799 csize=(width*height)>>2;
804 case VIDEO_PALETTE_YUV410P:
805 csize=(width*height)>>4;
813 cbadr=vadr+(width*height);
815 inter = (height>tvnorms[btv->win.norm].sheight/2) ? 1 : 0;
817 *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
819 *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
822 for (line=0; line < (height<<(1^inter)); line++)
827 cbadr=vadr+(width*height);
831 rp= (line&1) ? &re : &ro;
833 rp= (line>=height) ? &ro : &re;
837 rcmd=BT848_RISC_WRITE1S23|BT848_RISC_SOL;
839 rcmd=BT848_RISC_WRITE123|BT848_RISC_SOL;
844 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
845 blcr=(PAGE_SIZE-((PAGE_SIZE-1)&cradr))<<shift;
846 blcb=(PAGE_SIZE-((PAGE_SIZE-1)&cbadr))<<shift;
847 bl=(blcr<bl) ? blcr : bl;
848 bl=(blcb<bl) ? blcb : bl;
849 bl=(bl>todo) ? todo : bl;
852 /* bl now containts the longest row that can be written */
854 if(!todo) rcmd|=BT848_RISC_EOL; /* if this is the last EOL */
856 *((*rp)++)=cpu_to_le32(rcmd|bl);
857 *((*rp)++)=cpu_to_le32(blcb|(blcr<<16));
858 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
860 if ((rcmd&(15<<28))==BT848_RISC_WRITE123)
862 *((*rp)++)=cpu_to_le32(kvirt_to_bus(cbadr));
864 *((*rp)++)=cpu_to_le32(kvirt_to_bus(cradr));
868 rcmd&=~BT848_RISC_SOL; /* only the first has SOL */
872 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
873 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
874 *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
875 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
878 printk("bttv%d: prisc2: ro=%08lx re=%08lx\n",
879 btv->nr,virt_to_bus(ro), virt_to_bus(re));
884 static int make_vrisctab(struct bttv *btv, u32 *ro, u32 *re, u32 *vbuf,
885 u16 width, u16 height, u16 palette)
888 u32 bpl; /* bytes per line */
893 unsigned long vadr=(unsigned long)vbuf;
895 if (palette==VIDEO_PALETTE_RAW)
896 return make_rawrisctab(btv, ro, re, vbuf);
897 if (palette>=VIDEO_PALETTE_PLANAR)
898 return make_prisctab(btv, ro, re, vbuf, width, height, palette);
900 printk("bttv%d: vrisc1: ro=%08lx re=%08lx\n",
901 btv->nr,virt_to_bus(ro), virt_to_bus(re));
903 inter = (height>tvnorms[btv->win.norm].sheight/2) ? 1 : 0;
904 bpl=width*fmtbppx2[palette2fmt[palette]&0xf]/2;
906 *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
907 *(ro++)=cpu_to_le32(0);
908 *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
909 *(re++)=cpu_to_le32(0);
911 for (line=0; line < (height<<(1^inter)); line++)
914 rp= (line&1) ? &re : &ro;
916 rp= (line>=height) ? &ro : &re;
918 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
921 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
923 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
929 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|bl);
930 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
933 while (todo>PAGE_SIZE)
935 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|PAGE_SIZE);
936 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
940 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_EOL|todo);
941 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
946 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
947 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
948 *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
949 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
952 printk("bttv%d: vrisc2: ro=%08lx re=%08lx\n",
953 btv->nr,virt_to_bus(ro), virt_to_bus(re));
958 static unsigned char lmaskt[8] =
959 { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
960 static unsigned char rmaskt[8] =
961 { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
963 static void clip_draw_rectangle(unsigned char *clipmap, int x, int y, int w, int h)
965 unsigned char lmask, rmask, *p;
970 printk("bttv clip: %dx%d+%d+%d\n",w,h,x,y);
972 /* bitmap is fixed width, 128 bytes (1024 pixels represented) */
983 if (w < 0 || h < 0) /* catch bad clips */
985 /* out of range data should just fall through */
995 rmask=rmaskt[(x+w-1)&7];
1000 for (i=0; i<h; i++, p+=128)
1003 memset(p+1, 0xff, W);
1007 for (i=0; i<h; i++, p+=128)
1013 for (i=0; i<h; i++, p+=128)
1020 static void make_clip_tab(struct bttv *btv, struct video_clip *cr, int ncr)
1022 u32 line, x, y, bpl, width, height, inter, maxw;
1023 u32 bpp, dx, sx, **rp, *ro, *re, flags, len;
1026 unsigned char *clipmap, *clipline, cbit, lastbit, outofmem;
1028 /* take care: bpp != btv->win.bpp is allowed here */
1029 bpp = fmtbppx2[btv->win.color_fmt&0xf]/2;
1031 adr=btv->win.vidadr + btv->win.x * btv->win.bpp + btv->win.y * bpl;
1032 inter=(btv->win.interlace&1)^1;
1033 width=btv->win.width;
1034 height=btv->win.height;
1036 printk("bttv%d: clip1: pal=%d size=%dx%d, bpl=%d bpp=%d\n",
1037 btv->nr,btv->picture.palette,width,height,bpl,bpp);
1039 width = 1023; /* sanity check */
1041 height = 625; /* sanity check */
1042 ro=btv->risc_scr_odd;
1043 re=btv->risc_scr_even;
1046 printk("bttv%d: clip: ro=%08lx re=%08lx\n",
1047 btv->nr,virt_to_bus(ro), virt_to_bus(re));
1049 if ((clipmap=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL) {
1050 /* can't clip, don't generate any risc code */
1051 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
1052 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
1053 *(re++)=cpu_to_le32(BT848_RISC_JUMP);
1054 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
1057 if (ncr < 0) { /* bitmap was pased */
1058 memcpy(clipmap, (unsigned char *)cr, VIDEO_CLIPMAP_SIZE);
1059 } else { /* convert rectangular clips to a bitmap */
1060 memset(clipmap, 0, VIDEO_CLIPMAP_SIZE); /* clear map */
1061 for (i=0; i<ncr; i++)
1062 clip_draw_rectangle(clipmap, cr[i].x, cr[i].y,
1063 cr[i].width, cr[i].height);
1065 /* clip against viewing window AND screen
1066 so we do not have to rely on the user program
1068 maxw = (bpl - btv->win.x * btv->win.bpp) / bpp;
1069 clip_draw_rectangle(clipmap, (width > maxw) ? maxw : width,
1071 clip_draw_rectangle(clipmap,0,(btv->win.y+height>btv->win.sheight) ?
1072 (btv->win.sheight-btv->win.y) : height,1024,768);
1074 clip_draw_rectangle(clipmap, 0, 0, -(btv->win.x), 768);
1076 clip_draw_rectangle(clipmap, 0, 0, 1024, -(btv->win.y));
1078 *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1079 *(ro++)=cpu_to_le32(0);
1080 *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1081 *(re++)=cpu_to_le32(0);
1083 /* translate bitmap to risc code */
1084 for (line=outofmem=0; line < (height<<inter) && !outofmem; line++)
1087 rp= (line&1) ? &re : &ro;
1088 clipline = clipmap + (y<<7); /* running pointers ... */
1089 lastbit = *clipline & 1;
1090 for(x=dx=0,sx=0; x<=width && !outofmem;) {
1092 /* check bytes not bits if we can ... */
1094 while (0xff==*clipline && x<width-8) {
1100 while (0x00==*clipline && x<width-8) {
1107 cbit = *clipline & (1<<(x&7));
1108 if (x < width && !lastbit == !cbit) {
1111 /* generate the dma controller code */
1113 flags = ((bpp==4) ? BT848_RISC_BYTE3 : 0);
1114 flags |= ((!sx) ? BT848_RISC_SOL : 0);
1115 flags |= ((sx + dx == width) ? BT848_RISC_EOL : 0);
1117 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|flags|len);
1118 *((*rp)++)=cpu_to_le32(adr + bpp * sx);
1120 *((*rp)++)=cpu_to_le32(BT848_RISC_SKIP|flags|len);
1125 if (ro - btv->risc_scr_odd>(RISCMEM_LEN>>3) - 16)
1127 if (re - btv->risc_scr_even>(RISCMEM_LEN>>3) - 16)
1134 if ((!inter)||(line&1))
1139 /* outofmem flag relies on the following code to discard extra data */
1140 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
1141 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
1142 *(re++)=cpu_to_le32(BT848_RISC_JUMP);
1143 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
1146 printk("bttv%d: clip2: pal=%d size=%dx%d, bpl=%d bpp=%d\n",
1147 btv->nr,btv->picture.palette,width,height,bpl,bpp);
1151 * Set the registers for the size we have specified. Don't bother
1152 * trying to understand this without the BT848 manual in front of
1155 * PS: The manual is free for download in .pdf format from
1156 * www.brooktree.com - nicely done those folks.
1159 static inline void bt848_set_eogeo(struct bttv *btv, struct tvnorm *tvn,
1160 int odd, int width, int height)
1167 int inter = (height>tvn->sheight/2) ? 0 : 1;
1168 int off = odd ? 0x80 : 0x00;
1170 int swidth = tvn->swidth;
1171 int totalwidth = tvn->totalwidth;
1172 int scaledtwidth = tvn->scaledtwidth;
1174 if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0) {
1175 dprintk("bttv%d: DIGITAL_MODE_VIDEO override width\n",btv->nr);
1181 xsf = (width*scaledtwidth)/swidth;
1182 hscale = ((totalwidth*4096UL)/xsf-4096);
1183 hdelay = tvn->hdelayx1;
1184 hdelay = (hdelay*width)/swidth;
1186 sr=((tvn->sheight>>inter)*512)/height-512;
1187 vscale=(0x10000UL-sr)&0x1fff;
1188 crop=((width>>8)&0x03)|((hdelay>>6)&0x0c)|
1189 ((tvn->sheight>>4)&0x30)|((tvn->vdelay>>2)&0xc0);
1190 vscale |= inter ? (BT848_VSCALE_INT<<8) : 0;
1193 /* Some people say interpolation looks bad ... */
1194 vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
1196 btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
1198 btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
1201 btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
1204 btwrite(vtc, BT848_E_VTC+off);
1205 btwrite(hscale>>8, BT848_E_HSCALE_HI+off);
1206 btwrite(hscale&0xff, BT848_E_HSCALE_LO+off);
1207 btaor((vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
1208 btwrite(vscale&0xff, BT848_E_VSCALE_LO+off);
1209 btwrite(width&0xff, BT848_E_HACTIVE_LO+off);
1210 btwrite(hdelay&0xff, BT848_E_HDELAY_LO+off);
1211 btwrite(tvn->sheight&0xff, BT848_E_VACTIVE_LO+off);
1212 btwrite(tvn->vdelay&0xff, BT848_E_VDELAY_LO+off);
1213 btwrite(crop, BT848_E_CROP+off);
1217 static void bt848_set_geo(struct bttv *btv)
1219 u16 ewidth, eheight, owidth, oheight;
1223 tvn=&tvnorms[btv->win.norm];
1225 btwrite(tvn->adelay, BT848_ADELAY);
1226 btwrite(tvn->bdelay, BT848_BDELAY);
1227 btaor(tvn->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH), BT848_IFORM);
1228 btwrite(tvn->vbipack, BT848_VBI_PACK_SIZE);
1229 if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0)
1230 btwrite(0x39, BT848_VBI_PACK_DEL);
1232 btwrite(0x01, BT848_VBI_PACK_DEL);
1234 btv->pll.pll_ofreq = tvn->Fsc;
1235 if (!in_interrupt())
1238 btv->win.interlace = (btv->win.height>tvn->sheight/2) ? 1 : 0;
1240 if (0 == btv->risc_cap_odd &&
1241 0 == btv->risc_cap_even) {
1243 owidth = btv->win.width;
1244 oheight = btv->win.height;
1245 ewidth = btv->win.width;
1246 eheight = btv->win.height;
1247 format = btv->win.color_fmt;
1248 bswap = btv->fb_color_ctl;
1249 } else if (-1 != btv->gq_grab &&
1250 0 == btv->risc_cap_odd &&
1251 !btv->win.interlace &&
1253 /* odd field -> overlay, even field -> capture */
1254 owidth = btv->win.width;
1255 oheight = btv->win.height;
1256 ewidth = btv->gbuf[btv->gq_grab].width;
1257 eheight = btv->gbuf[btv->gq_grab].height;
1258 format = (btv->win.color_fmt & 0xf0) |
1259 (btv->gbuf[btv->gq_grab].fmt & 0x0f);
1260 bswap = btv->fb_color_ctl & 0x0a;
1263 owidth = btv->gbuf[btv->gq_grab].width;
1264 oheight = btv->gbuf[btv->gq_grab].height;
1265 ewidth = btv->gbuf[btv->gq_grab].width;
1266 eheight = btv->gbuf[btv->gq_grab].height;
1267 format = btv->gbuf[btv->gq_grab].fmt;
1271 /* program odd + even fields */
1272 bt848_set_eogeo(btv, tvn, 1, owidth, oheight);
1273 bt848_set_eogeo(btv, tvn, 0, ewidth, eheight);
1275 btwrite(format, BT848_COLOR_FMT);
1276 btwrite(bswap | BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1280 static int bpp2fmt[4] = {
1281 BT848_COLOR_FMT_RGB8, BT848_COLOR_FMT_RGB16,
1282 BT848_COLOR_FMT_RGB24, BT848_COLOR_FMT_RGB32
1285 static void bt848_set_winsize(struct bttv *btv)
1287 unsigned short format;
1289 if (btv->picture.palette > 0 && btv->picture.palette <= VIDEO_PALETTE_YUV422) {
1290 /* format set by VIDIOCSPICT */
1291 format = palette2fmt[btv->picture.palette];
1293 /* use default for the given color depth */
1294 format = (btv->win.depth==15) ? BT848_COLOR_FMT_RGB15 :
1295 bpp2fmt[(btv->win.bpp-1)&3];
1297 btv->win.color_fmt = format;
1299 format == BT848_COLOR_FMT_RGB32) {
1301 BT848_COLOR_CTL_WSWAP_ODD |
1302 BT848_COLOR_CTL_WSWAP_EVEN |
1303 BT848_COLOR_CTL_BSWAP_ODD |
1304 BT848_COLOR_CTL_BSWAP_EVEN;
1305 } else if (bigendian &&
1306 (format == BT848_COLOR_FMT_RGB16 ||
1307 format == BT848_COLOR_FMT_RGB15)) {
1309 BT848_COLOR_CTL_BSWAP_ODD |
1310 BT848_COLOR_CTL_BSWAP_EVEN;
1312 btv->fb_color_ctl = 0;
1315 /* RGB8 seems to be a 9x5x5 GRB color cube starting at
1316 * color 16. Why the h... can't they even mention this in the
1317 * data sheet? [AC - because it's a standard format so I guess
1318 * it never occurred to them]
1319 * Enable dithering in this mode.
1322 if (format==BT848_COLOR_FMT_RGB8)
1323 btand(~BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1325 btor(BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1331 * Grab into virtual memory.
1334 static int vgrab(struct bttv *btv, struct video_mmap *mp)
1338 unsigned long flags;
1340 if(btv->fbuffer==NULL)
1342 if(fbuffer_alloc(btv))
1346 if(mp->frame >= gbuffers || mp->frame < 0)
1348 if(btv->gbuf[mp->frame].stat != GBUFFER_UNUSED)
1351 if(mp->height < 32 || mp->width < 48)
1353 if (mp->format >= PALETTEFMT_MAX)
1356 if ((unsigned int)mp->height * mp->width *
1357 fmtbppx2[palette2fmt[mp->format]&0x0f]/2 > gbufsize)
1359 if (UNSET == palette2fmt[mp->format])
1363 * Ok load up the BT848
1366 vbuf=(unsigned int *)(btv->fbuffer+gbufsize*mp->frame);
1367 ro=btv->gbuf[mp->frame].risc;
1369 make_vrisctab(btv, ro, re, vbuf, mp->width, mp->height, mp->format);
1372 printk("bttv%d: cap vgrab: queue %d (%d:%dx%d)\n",
1373 btv->nr,mp->frame,mp->format,mp->width,mp->height);
1374 spin_lock_irqsave(&btv->s_lock, flags);
1375 btv->gbuf[mp->frame].stat = GBUFFER_GRABBING;
1376 btv->gbuf[mp->frame].fmt = palette2fmt[mp->format];
1377 btv->gbuf[mp->frame].width = mp->width;
1378 btv->gbuf[mp->frame].height = mp->height;
1379 btv->gbuf[mp->frame].ro = virt_to_bus(ro);
1380 btv->gbuf[mp->frame].re = virt_to_bus(re);
1383 if (mp->height <= tvnorms[btv->win.norm].sheight/2 &&
1384 mp->format != VIDEO_PALETTE_RAW)
1385 btv->gbuf[mp->frame].ro = 0;
1388 if (-1 == btv->gq_grab && btv->gq_in == btv->gq_out) {
1390 btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ);
1392 btv->gqueue[btv->gq_in++] = mp->frame;
1393 btv->gq_in = btv->gq_in % MAX_GBUFFERS;
1395 btor(3, BT848_CAP_CTL);
1396 btor(3, BT848_GPIO_DMA_CTL);
1397 spin_unlock_irqrestore(&btv->s_lock, flags);
1401 static long bttv_write(struct video_device *v, const char *buf, unsigned long count, int nonblock)
1406 static long bttv_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
1408 /* use bttv 0.9.x if you need capture via read() */
1412 static inline void burst(int on)
1414 tvnorms[0].scaledtwidth = 1135 - (on?BURSTOFFSET-2:0);
1415 tvnorms[0].hdelayx1 = 186 - (on?BURSTOFFSET :0);
1416 tvnorms[2].scaledtwidth = 1135 - (on?BURSTOFFSET-2:0);
1417 tvnorms[2].hdelayx1 = 186 - (on?BURSTOFFSET :0);
1421 * called from irq handler on fatal errors. Takes the grabber chip
1422 * offline, flag it needs a reinitialization (which can't be done
1423 * from irq context) and wake up all sleeping proccesses. They would
1424 * block forever else. We also need someone who actually does the
1425 * reinitialization from process context...
1427 static void bt848_offline(struct bttv *btv)
1430 spin_lock(&btv->s_lock);
1432 /* cancel all outstanding grab requests */
1436 for (i = 0; i < gbuffers; i++)
1437 if (btv->gbuf[i].stat == GBUFFER_GRABBING)
1438 btv->gbuf[i].stat = GBUFFER_ERROR;
1440 /* disable screen overlay and DMA */
1441 btv->risc_cap_odd = 0;
1442 btv->risc_cap_even = 0;
1443 bt848_set_risc_jmps(btv,0);
1445 /* flag the chip needs a restart */
1446 btv->needs_restart = 1;
1447 spin_unlock(&btv->s_lock);
1449 wake_up_interruptible(&btv->vbiq);
1450 wake_up_interruptible(&btv->capq);
1453 static void bt848_restart(struct bttv *btv)
1455 unsigned long irq_flags;
1458 printk("bttv%d: resetting chip\n",btv->nr);
1459 btwrite(0xfffffUL, BT848_INT_STAT);
1460 btand(~15, BT848_GPIO_DMA_CTL);
1461 btwrite(0, BT848_SRESET);
1462 btwrite(virt_to_bus(btv->risc_jmp+2),
1463 BT848_RISC_STRT_ADD);
1465 /* enforce pll reprogramming */
1466 btv->pll.pll_current = -1;
1469 spin_lock_irqsave(&btv->s_lock, irq_flags);
1471 btv->needs_restart = 0;
1473 bt848_set_risc_jmps(btv,-1);
1474 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1478 * Open a bttv card. Right now the flags stuff is just playing
1481 static int bttv_open(struct video_device *dev, int flags)
1483 struct bttv *btv = (struct bttv *)dev;
1489 printk("bttv%d: open called\n",btv->nr);
1495 btv->fbuffer=(unsigned char *) rvmalloc(gbuffers*gbufsize);
1503 for (i = 0; i < gbuffers; i++)
1504 btv->gbuf[i].stat = GBUFFER_UNUSED;
1506 if (btv->needs_restart)
1519 static void bttv_close(struct video_device *dev)
1521 struct bttv *btv=(struct bttv *)dev;
1522 unsigned long irq_flags;
1527 spin_lock_irqsave(&btv->s_lock, irq_flags);
1528 need_wait = (-1 != btv->gq_grab);
1534 btv->risc_cap_odd = 0;
1535 btv->risc_cap_even = 0;
1536 bt848_set_risc_jmps(btv,-1);
1537 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1540 * A word of warning. At this point the chip
1541 * is still capturing because its FIFO hasn't emptied
1542 * and the DMA control operations are posted PCI
1546 btread(BT848_I2C); /* This fixes the PCI posting delay */
1550 * This is sucky but right now I can't find a good way to
1551 * be sure its safe to free the buffer. We wait 5-6 fields
1552 * which is more than sufficient to be sure.
1554 current->state = TASK_UNINTERRUPTIBLE;
1555 schedule_timeout(HZ/10); /* Wait 1/10th of a second */
1559 * We have allowed it to drain.
1563 rvfree((void *) btv->fbuffer, gbuffers*gbufsize);
1569 /***********************************/
1570 /* ioctls and supporting functions */
1571 /***********************************/
1573 static inline void bt848_bright(struct bttv *btv, uint bright)
1575 btwrite(bright&0xff, BT848_BRIGHT);
1578 static inline void bt848_hue(struct bttv *btv, uint hue)
1580 btwrite(hue&0xff, BT848_HUE);
1583 static inline void bt848_contrast(struct bttv *btv, uint cont)
1585 unsigned int conthi;
1588 btwrite(cont&0xff, BT848_CONTRAST_LO);
1589 btaor(conthi, ~4, BT848_E_CONTROL);
1590 btaor(conthi, ~4, BT848_O_CONTROL);
1593 static inline void bt848_sat_u(struct bttv *btv, unsigned long data)
1598 btwrite(data&0xff, BT848_SAT_U_LO);
1599 btaor(datahi, ~2, BT848_E_CONTROL);
1600 btaor(datahi, ~2, BT848_O_CONTROL);
1603 static inline void bt848_sat_v(struct bttv *btv, unsigned long data)
1608 btwrite(data&0xff, BT848_SAT_V_LO);
1609 btaor(datahi, ~1, BT848_E_CONTROL);
1610 btaor(datahi, ~1, BT848_O_CONTROL);
1617 static int bttv_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
1619 struct bttv *btv=(struct bttv *)dev;
1620 unsigned long irq_flags;
1624 printk("bttv%d: ioctl 0x%x\n",btv->nr,cmd);
1629 struct video_capability b;
1630 strcpy(b.name,btv->video_dev.name);
1631 b.type = VID_TYPE_CAPTURE|
1632 ((bttv_tvcards[btv->type].tuner != UNSET) ? VID_TYPE_TUNER : 0) |
1637 b.channels = bttv_tvcards[btv->type].video_inputs;
1638 b.audios = bttv_tvcards[btv->type].audio_inputs;
1639 b.maxwidth = tvnorms[btv->win.norm].swidth;
1640 b.maxheight = tvnorms[btv->win.norm].sheight;
1643 if(copy_to_user(arg,&b,sizeof(b)))
1649 struct video_channel v;
1650 unsigned int channel;
1652 if(copy_from_user(&v, arg,sizeof(v)))
1654 channel = v.channel;
1655 if (channel>=bttv_tvcards[btv->type].video_inputs)
1657 v.flags=VIDEO_VC_AUDIO;
1659 v.type=VIDEO_TYPE_CAMERA;
1660 v.norm = btv->win.norm;
1661 if(channel==bttv_tvcards[btv->type].tuner)
1663 strcpy(v.name,"Television");
1664 v.flags|=VIDEO_VC_TUNER;
1665 v.type=VIDEO_TYPE_TV;
1668 else if (channel == btv->svhs)
1669 strcpy(v.name,"S-Video");
1670 else if (bttv_tvcards[btv->type].muxsel[v.channel] < 0)
1671 strcpy(v.name,"Digital Video");
1673 sprintf(v.name,"Composite%d",v.channel);
1675 if(copy_to_user(arg,&v,sizeof(v)))
1680 * Each channel has 1 tuner
1684 struct video_channel v;
1685 unsigned int channel;
1687 if(copy_from_user(&v, arg,sizeof(v)))
1689 channel = v.channel;
1691 if (channel>bttv_tvcards[btv->type].video_inputs)
1693 if (v.norm > TVNORMS)
1696 bttv_call_i2c_clients(btv,cmd,&v);
1698 bt848_muxsel(btv, channel);
1699 bttv_set_norm(btv, v.norm);
1705 struct video_tuner v;
1706 if(copy_from_user(&v,arg,sizeof(v))!=0)
1708 #if 0 /* tuner.signal might be of intrest for non-tuner sources too ... */
1709 if(v.tuner||btv->channel) /* Only tuner 0 */
1712 strcpy(v.name, "Television");
1714 v.rangehigh=0xFFFFFFFF;
1715 v.flags=VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1716 v.mode = btv->win.norm;
1717 v.signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1718 bttv_call_i2c_clients(btv,cmd,&v);
1719 if(copy_to_user(arg,&v,sizeof(v)))
1723 /* We have but one tuner */
1726 struct video_tuner v;
1729 if(copy_from_user(&v, arg, sizeof(v)))
1732 /* Only one channel has a tuner */
1733 if(tuner!=bttv_tvcards[btv->type].tuner)
1736 if(v.mode!=VIDEO_MODE_PAL&&v.mode!=VIDEO_MODE_NTSC
1737 &&v.mode!=VIDEO_MODE_SECAM)
1739 bttv_call_i2c_clients(btv,cmd,&v);
1740 if (btv->win.norm != v.mode) {
1742 bttv_set_norm(btv,v.mode);
1749 struct video_picture p=btv->picture;
1750 if(copy_to_user(arg, &p, sizeof(p)))
1756 struct video_picture p;
1758 if (copy_from_user(&p, arg,sizeof(p)))
1760 if (p.palette > PALETTEFMT_MAX)
1762 if (UNSET == palette2fmt[p.palette])
1765 /* We want -128 to 127 we get 0-65535 */
1766 bt848_bright(btv, (p.brightness>>8)-128);
1767 /* 0-511 for the colour */
1768 bt848_sat_u(btv, p.colour>>7);
1769 bt848_sat_v(btv, ((p.colour>>7)*201L)/237);
1771 bt848_hue(btv, (p.hue>>8)-128);
1773 bt848_contrast(btv, p.contrast>>7);
1780 struct video_window vw;
1781 struct video_clip *vcp = NULL;
1783 if(copy_from_user(&vw,arg,sizeof(vw)))
1787 if(vw.flags || vw.width < 16 || vw.height < 16)
1789 spin_lock_irqsave(&btv->s_lock, irq_flags);
1791 bt848_set_risc_jmps(btv,-1);
1792 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1796 if (btv->win.bpp < 4)
1797 { /* adjust and align writes */
1798 vw.x = (vw.x + 3) & ~3;
1801 if (btv->needs_restart)
1805 btv->win.width=vw.width;
1806 btv->win.height=vw.height;
1808 spin_lock_irqsave(&btv->s_lock, irq_flags);
1809 bt848_set_risc_jmps(btv,0);
1810 bt848_set_winsize(btv);
1811 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1816 if(vw.clipcount<0) {
1817 if((vcp=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL) {
1821 if(copy_from_user(vcp, vw.clips,
1822 VIDEO_CLIPMAP_SIZE)) {
1827 } else if (vw.clipcount > 2048) {
1830 } else if (vw.clipcount) {
1831 if((vcp=vmalloc(sizeof(struct video_clip)*
1832 (vw.clipcount))) == NULL) {
1836 if(copy_from_user(vcp,vw.clips,
1837 sizeof(struct video_clip)*
1844 make_clip_tab(btv, vcp, vw.clipcount);
1845 if (vw.clipcount != 0)
1847 spin_lock_irqsave(&btv->s_lock, irq_flags);
1848 bt848_set_risc_jmps(btv,-1);
1849 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1855 struct video_window vw;
1856 memset(&vw,0,sizeof(vw));
1859 vw.width=btv->win.width;
1860 vw.height=btv->win.height;
1861 if(btv->win.interlace)
1862 vw.flags|=VIDEO_WINDOW_INTERLACE;
1863 if(copy_to_user(arg,&vw,sizeof(vw)))
1870 if(copy_from_user(&v, arg,sizeof(v)))
1872 if(btv->win.vidadr == 0)
1874 if (btv->win.width==0 || btv->win.height==0)
1876 if (1 == no_overlay)
1878 spin_lock_irqsave(&btv->s_lock, irq_flags);
1879 if (v == 1 && btv->win.vidadr != 0)
1883 bt848_set_risc_jmps(btv,-1);
1884 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1889 struct video_buffer v;
1890 v.base=(void *)btv->win.vidadr;
1891 v.height=btv->win.sheight;
1892 v.width=btv->win.swidth;
1893 v.depth=btv->win.depth;
1894 v.bytesperline=btv->win.bpl;
1895 if(copy_to_user(arg, &v,sizeof(v)))
1902 struct video_buffer v;
1903 if(!capable(CAP_SYS_ADMIN) &&
1904 !capable(CAP_SYS_RAWIO))
1906 if(copy_from_user(&v, arg,sizeof(v)))
1908 if(v.depth!=8 && v.depth!=15 && v.depth!=16 &&
1909 v.depth!=24 && v.depth!=32 && v.width > 16 &&
1910 v.height > 16 && v.bytesperline > 16)
1914 btv->win.vidadr=(unsigned long)v.base;
1915 btv->win.sheight=v.height;
1916 btv->win.swidth=v.width;
1917 btv->win.bpp=((v.depth+7)&0x38)/8;
1918 btv->win.depth=v.depth;
1919 btv->win.bpl=v.bytesperline;
1921 #if 0 /* was broken for ages and nobody noticed. Looks like we don't need
1922 it any more as everybody explicitly sets the palette using VIDIOCSPICT
1924 /* set sefault color format */
1926 case 8: btv->picture.palette = VIDEO_PALETTE_HI240; break;
1927 case 15: btv->picture.palette = VIDEO_PALETTE_RGB555; break;
1928 case 16: btv->picture.palette = VIDEO_PALETTE_RGB565; break;
1929 case 24: btv->picture.palette = VIDEO_PALETTE_RGB24; break;
1930 case 32: btv->picture.palette = VIDEO_PALETTE_RGB32; break;
1935 printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1936 v.base, v.width,v.height, btv->win.bpp, btv->win.bpl);
1937 spin_lock_irqsave(&btv->s_lock, irq_flags);
1938 bt848_set_winsize(btv);
1939 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
1945 /* Will be handled higher up .. */
1950 unsigned long v=btv->win.freq;
1951 if(copy_to_user(arg,&v,sizeof(v)))
1958 if(copy_from_user(&v, arg, sizeof(v)))
1961 bttv_call_i2c_clients(btv,cmd,&v);
1963 if (btv->radio && btv->has_matchbox)
1964 tea5757_set_freq(btv,v);
1971 struct video_audio v;
1974 v.flags&=~(VIDEO_AUDIO_MUTE|VIDEO_AUDIO_MUTABLE);
1975 v.flags|=VIDEO_AUDIO_MUTABLE;
1976 strcpy(v.name,"TV");
1978 v.mode = VIDEO_SOUND_MONO;
1979 bttv_call_i2c_clients(btv,cmd,&v);
1981 /* card specific hooks */
1982 if (btv->audio_hook)
1983 btv->audio_hook(btv,&v,0);
1985 if(copy_to_user(arg,&v,sizeof(v)))
1991 struct video_audio v;
1994 if(copy_from_user(&v,arg, sizeof(v)))
1997 if(n >= bttv_tvcards[btv->type].audio_inputs)
2000 if(v.flags&VIDEO_AUDIO_MUTE)
2001 audio(btv, AUDIO_MUTE);
2002 /* bt848_muxsel(btv,v.audio); */
2003 if(!(v.flags&VIDEO_AUDIO_MUTE))
2004 audio(btv, AUDIO_UNMUTE);
2006 bttv_call_i2c_clients(btv,cmd,&v);
2008 /* card specific hooks */
2009 if (btv->audio_hook)
2010 btv->audio_hook(btv,&v,1);
2019 DECLARE_WAITQUEUE(wait, current);
2022 if(copy_from_user((void *)&i,arg,sizeof(int)))
2026 switch (btv->gbuf[i].stat) {
2027 case GBUFFER_UNUSED:
2030 case GBUFFER_GRABBING:
2031 add_wait_queue(&btv->capq, &wait);
2032 current->state = TASK_INTERRUPTIBLE;
2033 while(btv->gbuf[i].stat==GBUFFER_GRABBING) {
2035 printk("bttv%d: cap sync: sleep on %d\n",btv->nr,i);
2037 if(signal_pending(current)) {
2038 remove_wait_queue(&btv->capq, &wait);
2039 current->state = TASK_RUNNING;
2043 remove_wait_queue(&btv->capq, &wait);
2044 current->state = TASK_RUNNING;
2048 ret = (btv->gbuf[i].stat == GBUFFER_ERROR) ? -EIO : 0;
2050 printk("bttv%d: cap sync: buffer %d, retval %d\n",btv->nr,i,ret);
2051 btv->gbuf[i].stat = GBUFFER_UNUSED;
2053 if (btv->needs_restart) {
2062 if(copy_to_user((void *) arg, (void *) &btv->last_field,
2063 sizeof(btv->last_field)))
2068 struct bttv_pll_info p;
2069 if(!capable(CAP_SYS_ADMIN))
2071 if(copy_from_user(&p , (void *) arg, sizeof(btv->pll)))
2074 btv->pll.pll_ifreq = p.pll_ifreq;
2075 btv->pll.pll_ofreq = p.pll_ofreq;
2076 btv->pll.pll_crystal = p.pll_crystal;
2081 case VIDIOCMCAPTURE:
2083 struct video_mmap vm;
2085 if(copy_from_user((void *) &vm, (void *) arg, sizeof(vm)))
2088 ret = vgrab(btv, &vm);
2095 struct video_mbuf vm;
2098 memset(&vm, 0 , sizeof(vm));
2099 vm.size=gbufsize*gbuffers;
2101 for (i = 0; i < gbuffers; i++)
2102 vm.offsets[i]=i*gbufsize;
2103 if(copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
2110 struct video_unit vu;
2111 vu.video=btv->video_dev.minor;
2112 vu.vbi=btv->vbi_dev.minor;
2113 if(btv->radio_dev.minor!=-1)
2114 vu.radio=btv->radio_dev.minor;
2116 vu.radio=VIDEO_NO_UNIT;
2117 vu.audio=VIDEO_NO_UNIT;
2118 vu.teletext=VIDEO_NO_UNIT;
2119 if(copy_to_user((void *)arg, (void *)&vu, sizeof(vu)))
2130 case BTTV_BURST_OFF:
2138 return BTTV_VERSION_CODE;
2143 /* return picture;*/
2148 return -ENOIOCTLCMD;
2154 * This maps the vmalloced and reserved fbuffer to user space.
2157 * - PAGE_READONLY should suffice!?
2158 * - remap_page_range is kind of inefficient for page by page remapping.
2159 * But e.g. pte_alloc() does not work in modules ... :-(
2162 static int do_bttv_mmap(struct bttv *btv, const char *adr, unsigned long size)
2164 unsigned long start=(unsigned long) adr;
2165 unsigned long page,pos;
2167 if (size>gbuffers*gbufsize)
2169 if (!btv->fbuffer) {
2170 if(fbuffer_alloc(btv))
2173 pos=(unsigned long) btv->fbuffer;
2175 page = kvirt_to_pa(pos);
2176 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
2185 static int bttv_mmap(struct video_device *dev, const char *adr, unsigned long size)
2187 struct bttv *btv=(struct bttv *)dev;
2191 r=do_bttv_mmap(btv, adr, size);
2197 static struct video_device bttv_template=
2199 .owner = THIS_MODULE,
2200 .name = "bttv video",
2201 .type = VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY|VID_TYPE_TELETEXT,
2202 .hardware = VID_HARDWARE_BT848,
2204 .close = bttv_close,
2206 .write = bttv_write,
2207 .ioctl = bttv_ioctl,
2213 static long vbi_read(struct video_device *v, char *buf, unsigned long count,
2216 struct bttv *btv=(struct bttv *)(v-2);
2219 DECLARE_WAITQUEUE(wait, current);
2222 while (todo && todo>(q=VBIBUF_SIZE-btv->vbip))
2224 if (btv->needs_restart) {
2229 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, q))
2234 add_wait_queue(&btv->vbiq, &wait);
2235 current->state = TASK_INTERRUPTIBLE;
2236 if (todo && q==VBIBUF_SIZE-btv->vbip)
2240 remove_wait_queue(&btv->vbiq, &wait);
2241 current->state = TASK_RUNNING;
2243 return -EWOULDBLOCK;
2247 if(signal_pending(current))
2249 remove_wait_queue(&btv->vbiq, &wait);
2250 current->state = TASK_RUNNING;
2257 remove_wait_queue(&btv->vbiq, &wait);
2258 current->state = TASK_RUNNING;
2262 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, todo))
2269 static unsigned int vbi_poll(struct video_device *dev, struct file *file,
2272 struct bttv *btv=(struct bttv *)(dev-2);
2273 unsigned int mask = 0;
2275 poll_wait(file, &btv->vbiq, wait);
2277 if (btv->vbip < VBIBUF_SIZE)
2278 mask |= (POLLIN | POLLRDNORM);
2283 static int vbi_open(struct video_device *dev, int flags)
2285 struct bttv *btv=(struct bttv *)(dev-2);
2286 unsigned long irq_flags;
2289 if (btv->needs_restart)
2292 btv->vbip=VBIBUF_SIZE;
2293 spin_lock_irqsave(&btv->s_lock, irq_flags);
2295 bt848_set_risc_jmps(btv,-1);
2296 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
2302 static void vbi_close(struct video_device *dev)
2304 struct bttv *btv=(struct bttv *)(dev-2);
2305 unsigned long irq_flags;
2307 spin_lock_irqsave(&btv->s_lock, irq_flags);
2309 bt848_set_risc_jmps(btv,-1);
2310 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
2313 static int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2315 struct bttv *btv=(struct bttv *)(dev-2);
2320 struct video_capability b;
2321 strcpy(b.name,btv->vbi_dev.name);
2322 b.type = ((bttv_tvcards[btv->type].tuner != UNSET) ? VID_TYPE_TUNER : 0)
2323 | VID_TYPE_TELETEXT;
2330 if(copy_to_user(arg,&b,sizeof(b)))
2341 return bttv_ioctl(dev-2,cmd,arg);
2343 /* make alevt happy :-) */
2350 static struct video_device vbi_template=
2352 .owner = THIS_MODULE,
2354 .type = VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
2355 .hardware = VID_HARDWARE_BT848,
2359 .write = bttv_write,
2366 static int radio_open(struct video_device *dev, int flags)
2368 struct bttv *btv = (struct bttv *)(dev-1);
2376 bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
2377 bt848_muxsel(btv,0);
2387 static void radio_close(struct video_device *dev)
2389 struct bttv *btv=(struct bttv *)(dev-1);
2397 static long radio_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
2402 static int radio_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2404 struct bttv *btv=(struct bttv *)(dev-1);
2408 struct video_capability v;
2409 strcpy(v.name,btv->video_dev.name);
2410 v.type = VID_TYPE_TUNER;
2413 /* No we don't do pictures */
2418 if (copy_to_user(arg, &v, sizeof(v)))
2425 struct video_tuner v;
2426 if(copy_from_user(&v,arg,sizeof(v))!=0)
2428 if(v.tuner||btv->channel) /* Only tuner 0 */
2430 strcpy(v.name, "Radio");
2431 /* japan: 76.0 MHz - 89.9 MHz
2432 western europe: 87.5 MHz - 108.0 MHz
2433 russia: 65.0 MHz - 108.0 MHz */
2434 v.rangelow=(int)(65*16);
2435 v.rangehigh=(int)(108*16);
2436 v.flags= 0; /* XXX */
2437 v.mode = 0; /* XXX */
2438 bttv_call_i2c_clients(btv,cmd,&v);
2439 if(copy_to_user(arg,&v,sizeof(v)))
2445 struct video_tuner v;
2446 if(copy_from_user(&v, arg, sizeof(v)))
2448 /* Only channel 0 has a tuner */
2449 if(v.tuner!=0 || btv->channel)
2451 /* XXX anything to do ??? */
2458 bttv_ioctl((struct video_device *)btv,cmd,arg);
2461 return -ENOIOCTLCMD;
2466 static struct video_device radio_template=
2468 .owner = THIS_MODULE,
2469 .name = "bttv radio",
2470 .type = VID_TYPE_TUNER,
2471 .hardware = VID_HARDWARE_BT848,
2473 .close = radio_close,
2474 .read = radio_read, /* just returns -EINVAL */
2475 .write = bttv_write, /* just returns -EINVAL */
2476 .ioctl = radio_ioctl,
2481 static void bt848_set_risc_jmps(struct bttv *btv, int flags)
2490 if (bttv_tvcards[btv->type].muxsel[btv->channel] < 0)
2494 if ((0 != btv->risc_cap_even) ||
2495 (0 != btv->risc_cap_odd))
2501 printk("bttv%d: set_risc_jmp %08lx:",
2502 btv->nr,virt_to_bus(btv->risc_jmp));
2504 /* Sync to start of odd field */
2505 btv->risc_jmp[0]=cpu_to_le32(BT848_RISC_SYNC|BT848_RISC_RESYNC
2506 |BT848_FIFO_STATUS_VRE);
2507 btv->risc_jmp[1]=cpu_to_le32(0);
2509 /* Jump to odd vbi sub */
2510 btv->risc_jmp[2]=cpu_to_le32(BT848_RISC_JUMP|(0xd<<20));
2513 printk(" ev=%08lx",virt_to_bus(btv->vbi_odd));
2514 btv->risc_jmp[3]=cpu_to_le32(virt_to_bus(btv->vbi_odd));
2517 printk(" -----------");
2518 btv->risc_jmp[3]=cpu_to_le32(virt_to_bus(btv->risc_jmp+4));
2521 /* Jump to odd sub */
2522 btv->risc_jmp[4]=cpu_to_le32(BT848_RISC_JUMP|(0xe<<20));
2523 if (0 != btv->risc_cap_odd) {
2525 printk(" e%d=%08x",btv->gq_grab,btv->risc_cap_odd);
2527 btv->risc_jmp[5]=cpu_to_le32(btv->risc_cap_odd);
2528 } else if ((flags&2) &&
2529 (!btv->win.interlace || 0 == btv->risc_cap_even)) {
2531 printk(" eo=%08lx",virt_to_bus(btv->risc_scr_odd));
2532 btv->risc_jmp[5]=cpu_to_le32(virt_to_bus(btv->risc_scr_odd));
2535 printk(" -----------");
2536 btv->risc_jmp[5]=cpu_to_le32(virt_to_bus(btv->risc_jmp+6));
2540 /* Sync to start of even field */
2541 btv->risc_jmp[6]=cpu_to_le32(BT848_RISC_SYNC|BT848_RISC_RESYNC
2542 |BT848_FIFO_STATUS_VRO);
2543 btv->risc_jmp[7]=cpu_to_le32(0);
2545 /* Jump to even vbi sub */
2546 btv->risc_jmp[8]=cpu_to_le32(BT848_RISC_JUMP);
2549 printk(" ov=%08lx",virt_to_bus(btv->vbi_even));
2550 btv->risc_jmp[9]=cpu_to_le32(virt_to_bus(btv->vbi_even));
2553 printk(" -----------");
2554 btv->risc_jmp[9]=cpu_to_le32(virt_to_bus(btv->risc_jmp+10));
2557 /* Jump to even sub */
2558 btv->risc_jmp[10]=cpu_to_le32(BT848_RISC_JUMP|(8<<20));
2559 if (0 != btv->risc_cap_even) {
2561 printk(" o%d=%08x",btv->gq_grab,btv->risc_cap_even);
2563 btv->risc_jmp[11]=cpu_to_le32(btv->risc_cap_even);
2564 } else if ((flags&1) &&
2565 btv->win.interlace) {
2567 printk(" oo=%08lx",virt_to_bus(btv->risc_scr_even));
2568 btv->risc_jmp[11]=cpu_to_le32(virt_to_bus(btv->risc_scr_even));
2571 printk(" -----------");
2572 btv->risc_jmp[11]=cpu_to_le32(virt_to_bus(btv->risc_jmp+12));
2575 if (btv->gq_start) {
2576 btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ);
2578 btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP);
2580 btv->risc_jmp[13]=cpu_to_le32(virt_to_bus(btv->risc_jmp));
2582 /* enable cpaturing and DMA */
2584 printk(" flags=0x%x dma=%s\n",
2585 flags,(flags&0x0f) ? "on" : "off");
2586 btaor(flags, ~0x0f, BT848_CAP_CTL);
2593 static int __devinit init_video_dev(struct bttv *btv)
2595 audio(btv, AUDIO_MUTE);
2597 if (video_register_device(&btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
2599 printk(KERN_INFO "bttv%d: registered device video%d\n",
2600 btv->nr,btv->video_dev.minor & 0x1f);
2601 if (video_register_device(&btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0) {
2602 video_unregister_device(&btv->video_dev);
2605 printk(KERN_INFO "bttv%d: registered device vbi%d\n",
2606 btv->nr,btv->vbi_dev.minor & 0x1f);
2607 if (btv->has_radio) {
2608 if(video_register_device(&btv->radio_dev, VFL_TYPE_RADIO, radio_nr)<0) {
2609 video_unregister_device(&btv->vbi_dev);
2610 video_unregister_device(&btv->video_dev);
2613 printk(KERN_INFO "bttv%d: registered device radio%d\n",
2614 btv->nr,btv->radio_dev.minor & 0x1f);
2619 static int __devinit init_bt848(struct bttv *btv)
2623 unsigned long irq_flags;
2626 init_MUTEX(&btv->lock);
2628 /* dump current state of the gpio registers before changing them,
2629 * might help to make a new card work */
2631 bttv_gpio_tracking(btv,"init #1");
2632 bttv_gpio_tracking(btv,"init #1");
2635 /* reset the bt848 */
2636 btwrite(0, BT848_SRESET);
2638 /* not registered yet */
2639 btv->video_dev.minor = -1;
2640 btv->radio_dev.minor = -1;
2641 btv->vbi_dev.minor = -1;
2643 /* default setup for max. PAL size in a 1024xXXX hicolor framebuffer */
2644 btv->win.norm=0; /* change this to 1 for NTSC, 2 for SECAM */
2645 btv->win.interlace=1;
2649 btv->win.height=240;
2652 btv->win.color_fmt=BT848_COLOR_FMT_RGB16;
2653 btv->win.bpl=1024*btv->win.bpp;
2654 btv->win.swidth=1024;
2655 btv->win.sheight=768;
2660 btv->risc_scr_odd=0;
2661 btv->risc_scr_even=0;
2662 btv->risc_cap_odd=0;
2663 btv->risc_cap_even=0;
2666 btv->field=btv->last_field=0;
2669 btv->needs_restart=0;
2670 btv->has_radio=radio[btv->nr];
2672 if (!(btv->risc_scr_odd=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
2674 if (!(btv->risc_scr_even=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
2676 if (!(btv->risc_jmp =(unsigned int *) kmalloc(2048, GFP_KERNEL)))
2678 btv->vbi_odd=btv->risc_jmp+16;
2679 btv->vbi_even=btv->vbi_odd+256;
2680 btv->bus_vbi_odd=virt_to_bus(btv->risc_jmp+12);
2681 btv->bus_vbi_even=virt_to_bus(btv->risc_jmp+6);
2683 btwrite(virt_to_bus(btv->risc_jmp+2), BT848_RISC_STRT_ADD);
2684 btv->vbibuf=(unsigned char *) vmalloc_32(VBIBUF_SIZE);
2687 if (!(btv->gbuf = kmalloc(sizeof(struct bttv_gbuf)*gbuffers,GFP_KERNEL)))
2689 for (j = 0; j < gbuffers; j++) {
2690 if (!(btv->gbuf[j].risc = kmalloc(16384,GFP_KERNEL)))
2694 memset(btv->vbibuf, 0, VBIBUF_SIZE); /* We don't want to return random
2695 memory to the user */
2699 /* btwrite(0, BT848_TDEC); */
2700 btwrite(0x10, BT848_COLOR_CTL);
2701 btwrite(0x00, BT848_CAP_CTL);
2702 /* set planar and packed mode trigger points and */
2703 /* set rising edge of inverted GPINTR pin as irq trigger */
2704 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
2705 BT848_GPIO_DMA_CTL_PLTP1_16|
2706 BT848_GPIO_DMA_CTL_PLTP23_16|
2707 BT848_GPIO_DMA_CTL_GPINTC|
2708 BT848_GPIO_DMA_CTL_GPINTI,
2709 BT848_GPIO_DMA_CTL);
2711 /* select direct input */
2712 btwrite(0x00, BT848_GPIO_REG_INP);
2713 btwrite(0x00, BT848_GPIO_OUT_EN);
2715 bttv_gpio_tracking(btv,"init #2");
2717 btwrite(BT848_IFORM_MUX1 | BT848_IFORM_XTAUTO | BT848_IFORM_AUTO,
2720 btwrite(0xd8, BT848_CONTRAST_LO);
2721 bt848_bright(btv, 0x10);
2723 btwrite(0x20, BT848_E_VSCALE_HI);
2724 btwrite(0x20, BT848_O_VSCALE_HI);
2725 btwrite(BT848_ADC_RESERVED | (adc_crush ? BT848_ADC_CRUSH : 0),
2729 btwrite(0, BT848_E_CONTROL);
2730 btwrite(0, BT848_O_CONTROL);
2732 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
2733 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
2736 btv->picture.colour = 254<<7;
2737 btv->picture.brightness = 128<<8;
2738 btv->picture.hue = 128<<8;
2739 btv->picture.contrast = 0xd8<<7;
2740 btv->picture.palette = VIDEO_PALETTE_RGB24;
2742 val = chroma_agc ? BT848_SCLOOP_CAGC : 0;
2743 btwrite(val, BT848_E_SCLOOP);
2744 btwrite(val, BT848_O_SCLOOP);
2746 /* clear interrupt status */
2747 btwrite(0xfffffUL, BT848_INT_STAT);
2749 /* set interrupt mask */
2750 btwrite(btv->triton1|
2751 /*BT848_INT_PABORT|BT848_INT_RIPERR|BT848_INT_PPERR|
2752 BT848_INT_FDSR|BT848_INT_FTRGT|BT848_INT_FBUS|*/
2753 (fieldnr ? BT848_INT_VSYNC : 0) |
2754 (gpint ? BT848_INT_GPINT : 0) |
2756 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
2757 BT848_INT_FMTCHG|BT848_INT_HLOCK,
2761 spin_lock_irqsave(&btv->s_lock, irq_flags);
2762 bt848_set_risc_jmps(btv,-1);
2763 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
2765 /* needs to be done before i2c is registered */
2766 bttv_init_card1(btv);
2769 btv->tuner_type = UNSET;
2770 btv->pinnacle_id = UNSET;
2773 /* some card-specific stuff (needs working i2c) */
2774 bttv_init_card2(btv);
2776 bt848_muxsel(btv, 1);
2777 bt848_set_winsize(btv);
2780 * Now add the template and register the device unit.
2782 init_video_dev(btv);
2787 /* ----------------------------------------------------------------------- */
2789 static char *irq_name[] = { "FMTCHG", "VSYNC", "HSYNC", "OFLOW", "HLOCK",
2790 "VPRES", "6", "7", "I2CDONE", "GPINT", "10",
2791 "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
2792 "RIPERR", "PABORT", "OCERR", "SCERR" };
2794 static void bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
2801 btv=(struct bttv *)dev_id;
2805 /* get/clear interrupt status bits */
2806 stat=btread(BT848_INT_STAT);
2807 astat=stat&btread(BT848_INT_MASK);
2810 btwrite(stat,BT848_INT_STAT);
2812 /* get device status bits */
2813 dstat=btread(BT848_DSTATUS);
2817 printk(KERN_DEBUG "bttv%d: irq loop=%d risc=%x, bits:",
2818 btv->nr, count, stat>>28);
2819 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
2820 if (stat & (1 << i))
2821 printk(" %s",irq_name[i]);
2822 if (astat & (1 << i))
2825 if (stat & BT848_INT_HLOCK)
2826 printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
2828 if (stat & BT848_INT_VPRES)
2829 printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
2831 if (stat & BT848_INT_FMTCHG)
2832 printk(" NUML => %s", (dstat & BT848_DSTATUS_PRES)
2837 if (astat&BT848_INT_GPINT)
2838 wake_up_interruptible(&btv->gpioq);
2840 if (astat&BT848_INT_VSYNC)
2843 if (astat&(BT848_INT_SCERR|BT848_INT_OCERR)) {
2845 printk("bttv%d: irq:%s%s risc_count=%08x\n",
2847 (astat&BT848_INT_SCERR) ? " SCERR" : "",
2848 (astat&BT848_INT_OCERR) ? " OCERR" : "",
2849 btread(BT848_RISC_COUNT));
2851 if (btv->errors < BTTV_ERRORS) {
2852 spin_lock(&btv->s_lock);
2853 btand(~15, BT848_GPIO_DMA_CTL);
2854 btwrite(virt_to_bus(btv->risc_jmp+2),
2855 BT848_RISC_STRT_ADD);
2857 bt848_set_risc_jmps(btv,-1);
2858 spin_unlock(&btv->s_lock);
2861 printk("bttv%d: aiee: error loops\n",btv->nr);
2865 if (astat&BT848_INT_RISCI)
2868 printk("bttv%d: IRQ_RISCI\n",btv->nr);
2870 /* captured VBI frame */
2874 /* inc vbi frame count for detecting drops */
2875 (*(u32 *)&(btv->vbibuf[VBIBUF_SIZE - 4]))++;
2876 wake_up_interruptible(&btv->vbiq);
2879 /* captured full frame */
2880 if (stat&(2<<28) && btv->gq_grab != -1)
2882 btv->last_field=btv->field;
2884 printk("bttv%d: cap irq: done %d\n",btv->nr,btv->gq_grab);
2885 do_gettimeofday(&btv->gbuf[btv->gq_grab].tv);
2886 spin_lock(&btv->s_lock);
2887 btv->gbuf[btv->gq_grab].stat = GBUFFER_DONE;
2889 if (btv->gq_in != btv->gq_out)
2891 btv->gq_grab = btv->gqueue[btv->gq_out++];
2892 btv->gq_out = btv->gq_out % MAX_GBUFFERS;
2894 printk("bttv%d: cap irq: capture %d\n",btv->nr,btv->gq_grab);
2895 btv->risc_cap_odd = btv->gbuf[btv->gq_grab].ro;
2896 btv->risc_cap_even = btv->gbuf[btv->gq_grab].re;
2897 bt848_set_risc_jmps(btv,-1);
2899 btwrite(BT848_COLOR_CTL_GAMMA,
2902 btv->risc_cap_odd = 0;
2903 btv->risc_cap_even = 0;
2904 bt848_set_risc_jmps(btv,-1);
2906 btwrite(btv->fb_color_ctl | BT848_COLOR_CTL_GAMMA,
2909 spin_unlock(&btv->s_lock);
2910 wake_up_interruptible(&btv->capq);
2913 if (stat&(8<<28) && btv->gq_start)
2915 spin_lock(&btv->s_lock);
2917 btv->gq_grab = btv->gqueue[btv->gq_out++];
2918 btv->gq_out = btv->gq_out % MAX_GBUFFERS;
2920 printk("bttv%d: cap irq: capture %d [start]\n",btv->nr,btv->gq_grab);
2921 btv->risc_cap_odd = btv->gbuf[btv->gq_grab].ro;
2922 btv->risc_cap_even = btv->gbuf[btv->gq_grab].re;
2923 bt848_set_risc_jmps(btv,-1);
2925 btwrite(BT848_COLOR_CTL_GAMMA,
2927 spin_unlock(&btv->s_lock);
2931 if (automute && (astat&BT848_INT_HLOCK)) {
2932 if ((dstat&BT848_DSTATUS_HLOC) || (btv->radio))
2933 audio(btv, AUDIO_ON);
2935 audio(btv, AUDIO_OFF);
2940 btwrite(0, BT848_INT_MASK);
2942 "bttv%d: IRQ lockup, cleared int mask\n", btv->nr);
2951 * Scan for a Bt848 card, request the irq and map the io memory
2954 static void bttv_remove(struct pci_dev *pci_dev)
2958 struct bttv *btv = pci_get_drvdata(pci_dev);
2961 printk("bttv%d: unloading\n",btv->nr);
2963 /* unregister i2c_bus */
2964 if (0 == btv->i2c_rc)
2965 i2c_bit_del_bus(&btv->i2c_adap);
2967 /* turn off all capturing, DMA and IRQs */
2968 btand(~15, BT848_GPIO_DMA_CTL);
2970 /* first disable interrupts before unmapping the memory! */
2971 btwrite(0, BT848_INT_MASK);
2972 btwrite(~(u32)0,BT848_INT_STAT);
2973 btwrite(0, BT848_GPIO_OUT_EN);
2975 bttv_gpio_tracking(btv,"cleanup");
2977 /* disable PCI bus-mastering */
2978 pci_read_config_byte(btv->dev, PCI_COMMAND, &command);
2979 command &= ~PCI_COMMAND_MASTER;
2980 pci_write_config_byte(btv->dev, PCI_COMMAND, command);
2982 /* unmap and free memory */
2983 for (j = 0; j < gbuffers; j++)
2984 if (btv->gbuf[j].risc)
2985 kfree(btv->gbuf[j].risc);
2987 kfree((void *) btv->gbuf);
2989 if (btv->risc_scr_odd)
2990 kfree((void *) btv->risc_scr_odd);
2992 if (btv->risc_scr_even)
2993 kfree((void *) btv->risc_scr_even);
2995 DEBUG(printk(KERN_DEBUG "free: risc_jmp: 0x%p.\n", btv->risc_jmp));
2997 kfree((void *) btv->risc_jmp);
2999 DEBUG(printk(KERN_DEBUG "bt848_vbibuf: 0x%p.\n", btv->vbibuf));
3001 vfree((void *) btv->vbibuf);
3003 free_irq(btv->dev->irq,btv);
3004 DEBUG(printk(KERN_DEBUG "bt848_mem: 0x%p.\n", btv->bt848_mem));
3006 iounmap(btv->bt848_mem);
3008 if (btv->video_dev.minor!=-1)
3009 video_unregister_device(&btv->video_dev);
3010 if (btv->vbi_dev.minor!=-1)
3011 video_unregister_device(&btv->vbi_dev);
3012 if (btv->radio_dev.minor != -1)
3013 video_unregister_device(&btv->radio_dev);
3015 release_mem_region(pci_resource_start(btv->dev,0),
3016 pci_resource_len(btv->dev,0));
3017 /* wake up any waiting processes
3018 because shutdown flag is set, no new processes (in this queue)
3022 wake_up(&btv->gpioq);
3024 pci_set_drvdata(pci_dev, NULL);
3029 static int __devinit bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
3034 #if defined(__powerpc__)
3038 if (bttv_num == BTTV_MAX)
3040 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3042 btv=&bttvs[bttv_num];
3045 btv->bt848_mem=NULL;
3050 init_waitqueue_head(&btv->vbiq);
3051 init_waitqueue_head(&btv->capq);
3052 btv->vbip=VBIBUF_SIZE;
3053 btv->s_lock = SPIN_LOCK_UNLOCKED;
3054 init_waitqueue_head(&btv->gpioq);
3057 memcpy(&btv->video_dev,&bttv_template, sizeof(bttv_template));
3058 memcpy(&btv->vbi_dev,&vbi_template, sizeof(vbi_template));
3059 memcpy(&btv->radio_dev,&radio_template,sizeof(radio_template));
3061 btv->id=dev->device;
3062 btv->bt848_adr=pci_resource_start(dev,0);
3063 if (pci_enable_device(dev)) {
3064 printk(KERN_WARNING "bttv%d: Can't enable device.\n",
3068 if (pci_set_dma_mask(dev, 0xffffffff)) {
3069 printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
3073 if (!request_mem_region(pci_resource_start(dev,0),
3074 pci_resource_len(dev,0),
3079 btv->i2c_command = 0x83;
3081 btv->i2c_command=(I2C_TIMING | BT848_I2C_SCL | BT848_I2C_SDA);
3083 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3084 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3085 printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %02x:%02x.%x, ",
3086 bttv_num,btv->id, btv->revision, dev->bus->number,
3087 PCI_SLOT(dev->devfn),PCI_FUNC(dev->devfn));
3088 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
3089 btv->dev->irq, lat, btv->bt848_adr);
3093 #if defined(__powerpc__)
3094 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3095 /* response on cards with no firmware is not enabled by OF */
3096 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3097 cmd = (cmd | PCI_COMMAND_MEMORY );
3098 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3102 btv->bt848_mem=(unsigned char *)btv->bt848_adr;
3104 btv->bt848_mem=ioremap(btv->bt848_adr, 0x1000);
3107 /* clear interrupt mask */
3108 btwrite(0, BT848_INT_MASK);
3110 result = request_irq(btv->dev->irq, bttv_irq,
3111 SA_SHIRQ | SA_INTERRUPT,"bttv",(void *)btv);
3112 if (result==-EINVAL)
3114 printk(KERN_ERR "bttv%d: Bad irq number or handler\n",
3120 printk(KERN_ERR "bttv%d: IRQ %d busy, change your PnP config in BIOS\n",bttv_num,btv->dev->irq);
3126 if (0 != bttv_handle_chipset(btv)) {
3131 pci_set_master(dev);
3132 pci_set_drvdata(dev,btv);
3134 if(init_bt848(btv) < 0) {
3143 free_irq(btv->dev->irq,btv);
3145 release_mem_region(pci_resource_start(btv->dev,0),
3146 pci_resource_len(btv->dev,0));
3150 static struct pci_device_id bttv_pci_tbl[] = {
3151 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
3152 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3153 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
3154 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3155 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
3156 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3157 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
3158 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3162 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
3164 static struct pci_driver bttv_pci_driver = {
3166 .id_table = bttv_pci_tbl,
3167 .probe = bttv_probe,
3168 .remove = bttv_remove,
3171 static int bttv_init_module(void)
3176 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
3177 (BTTV_VERSION_CODE >> 16) & 0xff,
3178 (BTTV_VERSION_CODE >> 8) & 0xff,
3179 BTTV_VERSION_CODE & 0xff);
3180 if (gbuffers < 2 || gbuffers > MAX_GBUFFERS)
3182 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
3183 gbufsize = BTTV_MAX_FBUF;
3185 printk(KERN_INFO "bttv: using %d buffers with %dk (%dk total) for capture\n",
3186 gbuffers,gbufsize/1024,gbuffers*gbufsize/1024);
3188 bttv_check_chipset();
3190 rc = pci_module_init(&bttv_pci_driver);
3191 if (-ENODEV == rc) {
3192 /* plenty of people trying to use bttv for the cx2388x ... */
3193 if (NULL != pci_find_device(0x14f1, 0x8800, NULL))
3194 printk("bttv doesn't support your Conexant 2388x card.\n");
3199 static void bttv_cleanup_module(void)
3201 pci_unregister_driver(&bttv_pci_driver);
3205 module_init(bttv_init_module);
3206 module_exit(bttv_cleanup_module);