Import 2.1.81
[davej-history.git] / drivers / char / bttv.c
blobcd821406f510c08e4b404a4546560df6b0330ba2
1 /*
2 bttv - Bt848 frame grabber driver
4 Copyright (C) 1996,97 Ralph Metzler (rjkm@thp.uni-koeln.de)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 Modified to put the RISC code writer in the kernel and to fit a
21 common (and I hope safe) kernel interface. When we have an X extension
22 all will now be really sweet.
25 #include <linux/module.h>
26 #include <linux/bios32.h>
27 #include <linux/config.h>
28 #include <linux/delay.h>
29 #include <linux/errno.h>
30 #include <linux/fs.h>
31 #include <linux/kernel.h>
32 #include <linux/major.h>
33 #include <linux/malloc.h>
34 #include <linux/mm.h>
35 #include <linux/pci.h>
36 #include <linux/signal.h>
37 #include <asm/io.h>
38 #include <asm/pgtable.h>
39 #include <asm/page.h>
40 #include <linux/sched.h>
41 #include <asm/segment.h>
42 #include <linux/types.h>
43 #include <linux/videodev.h>
45 #include <linux/version.h>
46 #include <asm/uaccess.h>
48 #include "bttv.h"
49 #include "tuner.h"
51 #define DEBUG(x) /* Debug driver */
52 #define IDEBUG(x) /* Debug interrupt handler */
54 static unsigned int remap=0;
55 static unsigned int vidmem=0;
56 static unsigned int tuner=0; /* Default tuner */
57 MODULE_PARM(tuner,"i");
59 static int find_vga(void);
60 static void bt848_set_risc_jmps(struct bttv *btv);
62 /* Anybody who uses more than four? */
63 #define BTTV_MAX 4
65 static int bttv_num;
66 static struct bttv bttvs[BTTV_MAX];
68 #define I2C_TIMING (0x7<<4)
69 #define I2C_COMMAND (I2C_TIMING | BT848_I2C_SCL | BT848_I2C_SDA)
71 #define AUDIO_MUTE_DELAY 10000
72 #define FREQ_CHANGE_DELAY 20000
73 #define EEPROM_WRITE_DELAY 20000
76 /*******************************/
77 /* Memory management functions */
78 /*******************************/
80 /* convert virtual user memory address to physical address */
81 /* (virt_to_phys only works for kmalloced kernel memory) */
83 static inline ulong uvirt_to_phys(ulong adr)
85 pgd_t *pgd;
86 pmd_t *pmd;
87 pte_t *ptep, pte;
89 /* printk("adr: 0x%08x\n",adr);*/
90 pgd = pgd_offset(current->mm, adr);
91 /* printk("pgd: 0x%08x\n",pgd);*/
92 if (pgd_none(*pgd))
93 return 0;
94 pmd = pmd_offset(pgd, adr);
95 /* printk("pmd: 0x%08x\n",pmd); */
96 if (pmd_none(*pmd))
97 return 0;
98 ptep = pte_offset(pmd, adr&(~PGDIR_MASK));
99 pte = *ptep;
100 if(pte_present(pte))
101 return (pte_page(pte)|(adr&(PAGE_SIZE-1)));
102 return 0;
105 /* convert virtual kernel memory address to physical address */
106 /* (virt_to_phys only works for kmalloced kernel memory) */
108 static inline ulong kvirt_to_phys(ulong adr)
110 return uvirt_to_phys(VMALLOC_VMADDR(adr));
113 static inline ulong kvirt_to_bus(ulong adr)
115 return virt_to_bus(phys_to_virt(kvirt_to_phys(adr)));
119 /*****************/
120 /* I2C functions */
121 /*****************/
123 static int I2CRead(struct bttv *btv, int addr)
125 u32 i;
126 u32 stat;
128 /* clear status bit ; BT848_INT_RACK is ro */
129 btwrite(BT848_INT_I2CDONE, BT848_INT_STAT);
131 btwrite(((addr & 0xff) << 24) | I2C_COMMAND, BT848_I2C);
134 * Timeout for I2CRead is 1 second (this should be enough, really!)
136 for (i=1000; i; i--)
138 stat=btread(BT848_INT_STAT);
139 if (stat & BT848_INT_I2CDONE)
140 break;
141 udelay(1000); /* 1ms, as I2C is 1kHz (?) */
144 if (!i) {
145 printk(KERN_DEBUG "bttv: I2CRead timeout\n");
146 return -1;
148 if (!(stat & BT848_INT_RACK))
149 return -2;
151 i=(btread(BT848_I2C)>>8)&0xff;
152 return i;
155 /* set both to write both bytes, reset it to write only b1 */
157 static int I2CWrite(struct bttv *btv, unchar addr, unchar b1,
158 unchar b2, int both)
160 u32 i;
161 u32 data;
162 u32 stat;
164 /* clear status bit; BT848_INT_RACK is ro */
165 btwrite(BT848_INT_I2CDONE, BT848_INT_STAT);
167 data=((addr & 0xff) << 24) | ((b1 & 0xff) << 16) | I2C_COMMAND;
168 if (both)
170 data|=((b2 & 0xff) << 8);
171 data|=BT848_I2C_W3B;
174 btwrite(data, BT848_I2C);
176 for (i=1000; i; i--)
178 stat=btread(BT848_INT_STAT);
179 if (stat & BT848_INT_I2CDONE)
180 break;
181 udelay(1000);
184 if (!i) {
185 printk(KERN_DEBUG "bttv: I2CWrite timeout\n");
186 return -1;
188 if (!(stat & BT848_INT_RACK))
189 return -2;
191 return 0;
194 static void readee(struct bttv *btv, unchar *eedata)
196 int i, k;
198 if (I2CWrite(btv, 0xa0, 0, -1, 0)<0)
200 printk(KERN_WARNING "bttv: readee error\n");
201 return;
204 for (i=0; i<256; i++)
206 k=I2CRead(btv, 0xa1);
207 if (k<0)
209 printk(KERN_WARNING "bttv: readee error\n");
210 break;
212 eedata[i]=k;
216 static void writeee(struct bttv *btv, unchar *eedata)
218 int i;
220 for (i=0; i<256; i++)
222 if (I2CWrite(btv, 0xa0, i, eedata[i], 1)<0)
224 printk(KERN_WARNING "bttv: writeee error (%d)\n", i);
225 break;
227 udelay(EEPROM_WRITE_DELAY);
232 * Tuner, internal, external and mute
235 static unchar audiomuxs[][4] =
237 { 0x00, 0x00, 0x00, 0x00}, /* unknown */
238 { 0x02, 0x00, 0x00, 0x0a}, /* MIRO */
239 { 0x00, 0x02, 0x03, 0x04}, /* Hauppauge */
240 { 0x04, 0x02, 0x03, 0x01}, /* STB */
241 { 0x01, 0x02, 0x03, 0x04}, /* Intel??? */
242 { 0x01, 0x02, 0x03, 0x04}, /* Diamond DTV2000??? */
245 static void audio(struct bttv *btv, int mode)
247 btwrite(0x0f, BT848_GPIO_OUT_EN);
248 btwrite(0x00, BT848_GPIO_REG_INP);
250 switch (mode)
252 case AUDIO_UNMUTE:
253 btv->audio&=~AUDIO_MUTE;
254 mode=btv->audio;
255 break;
256 case AUDIO_OFF:
257 mode=AUDIO_OFF;
258 break;
259 case AUDIO_ON:
260 mode=btv->audio;
261 break;
262 default:
263 btv->audio&=AUDIO_MUTE;
264 btv->audio|=mode;
265 break;
267 if ((btv->audio&AUDIO_MUTE) || !(btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC))
268 mode=AUDIO_OFF;
269 btaor(audiomuxs[btv->type][mode] , ~0x0f, BT848_GPIO_DATA);
273 extern inline void bt848_dma(struct bttv *btv, uint state)
275 if (state)
276 btor(3, BT848_GPIO_DMA_CTL);
277 else
278 btand(~3, BT848_GPIO_DMA_CTL);
282 static void bt848_cap(struct bttv *btv, uint state)
284 if (state)
286 btv->cap|=3;
287 bt848_set_risc_jmps(btv);
289 else
291 btv->cap&=~3;
292 bt848_set_risc_jmps(btv);
296 static void bt848_muxsel(struct bttv *btv, uint input)
298 input&=3;
300 /* This seems to get rid of some synchronization problems */
301 btand(~(3<<5), BT848_IFORM);
302 udelay(10000);
304 if (input==3)
306 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
307 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
309 else
311 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
312 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
314 if (input==2)
315 input=3;
316 btaor(((input+2)&3)<<5, ~(3<<5), BT848_IFORM);
317 audio(btv, input ? AUDIO_EXTERN : AUDIO_TUNER);
321 #define VBIBUF_SIZE 65536
323 static void make_vbitab(struct bttv *btv)
325 int i;
326 dword *po=(dword *) btv->vbi_odd;
327 dword *pe=(dword *) btv->vbi_even;
329 DEBUG(printk(KERN_DEBUG "vbiodd: 0x%08x\n",(int)btv->vbi_odd));
330 DEBUG(printk(KERN_DEBUG "vbievn: 0x%08x\n",(int)btv->vbi_even));
331 DEBUG(printk(KERN_DEBUG "po: 0x%08x\n",(int)po));
332 DEBUG(printk(KERN_DEBUG "pe: 0x%08x\n",(int)pe));
334 *(po++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(po++)=0;
335 for (i=0; i<16; i++)
337 *(po++)=BT848_RISC_WRITE|2044|BT848_RISC_EOL|BT848_RISC_SOL|(13<<20);
338 *(po++)=kvirt_to_bus((ulong)btv->vbibuf+i*2048);
340 *(po++)=BT848_RISC_JUMP;
341 *(po++)=virt_to_bus(btv->risc_jmp+4);
343 *(pe++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(pe++)=0;
344 for (i=16; i<32; i++)
346 *(pe++)=BT848_RISC_WRITE|2044|BT848_RISC_EOL|BT848_RISC_SOL;
347 *(pe++)=kvirt_to_bus((ulong)btv->vbibuf+i*2048);
349 *(pe++)=BT848_RISC_JUMP|BT848_RISC_IRQ|(0x01<<16);
350 *(pe++)=virt_to_bus(btv->risc_jmp+10);
354 * Set the registers for the size we have specified. Don't bother
355 * trying to understand this without the BT848 manual in front of
356 * you [AC].
358 * PS: The manual is free for download in .pdf format from
359 * www.brooktree.com - nicely done those folks.
362 static void bt848_set_size(struct bttv *btv)
364 u16 vscale, hscale;
365 u32 xsf, sr;
366 u16 hdelay, vdelay;
367 u16 hactive, vactive;
368 u16 inter;
369 u8 crop;
372 * No window , no try...
375 if (!btv->win.width)
376 return;
377 if (!btv->win.height)
378 return;
380 inter=(btv->win.interlace&1)^1;
382 switch (btv->win.bpp)
385 * RGB8 seems to be a 9x5x5 GRB color cube starting at color 16
386 * Why the h... can't they even mention this in the datasheet???
388 case 1:
389 btwrite(BT848_COLOR_FMT_RGB8, BT848_COLOR_FMT);
390 btand(~0x10, BT848_CAP_CTL); /* Dithering looks much better in this mode */
391 break;
392 case 2:
393 btwrite(BT848_COLOR_FMT_RGB16, BT848_COLOR_FMT);
394 btor(0x10, BT848_CAP_CTL);
395 break;
396 case 3:
397 btwrite(BT848_COLOR_FMT_RGB24, BT848_COLOR_FMT);
398 btor(0x10, BT848_CAP_CTL);
399 break;
400 case 4:
401 btwrite(BT848_COLOR_FMT_RGB32, BT848_COLOR_FMT);
402 btor(0x10, BT848_CAP_CTL);
403 break;
407 * Set things up according to the final picture width.
410 hactive=btv->win.width;
411 if (hactive < 193)
413 btwrite (2, BT848_E_VTC);
414 btwrite (2, BT848_O_VTC);
416 else if (hactive < 385)
418 btwrite (1, BT848_E_VTC);
419 btwrite (1, BT848_O_VTC);
421 else
423 btwrite (0, BT848_E_VTC);
424 btwrite (0, BT848_O_VTC);
428 * Ok are we doing Never The Same Color or PAL ?
431 if (btv->win.norm==1)
433 btv->win.cropwidth=640;
434 btv->win.cropheight=480;
435 btwrite(0x68, BT848_ADELAY);
436 btwrite(0x5d, BT848_BDELAY);
437 btaor(BT848_IFORM_NTSC, ~7, BT848_IFORM);
438 btaor(BT848_IFORM_XT0, ~0x18, BT848_IFORM);
439 xsf = (btv->win.width*365625UL)/300000UL;
440 hscale = ((910UL*4096UL)/xsf-4096);
441 vdelay=btv->win.cropy+0x16;
442 hdelay=((hactive*135)/754+btv->win.cropx)&0x3fe;
444 else
446 btv->win.cropwidth=768;
447 btv->win.cropheight=576;
448 if (btv->win.norm==0)
450 btwrite(0x7f, BT848_ADELAY);
451 btwrite(0x72, BT848_BDELAY);
452 btaor(BT848_IFORM_PAL_BDGHI, ~BT848_IFORM_NORM, BT848_IFORM);
454 else
456 btwrite(0x7f, BT848_ADELAY);
457 btwrite(0x00, BT848_BDELAY);
458 btaor(BT848_IFORM_SECAM, ~BT848_IFORM_NORM, BT848_IFORM);
460 btaor(BT848_IFORM_XT1, ~0x18, BT848_IFORM);
461 xsf = (btv->win.width*36875UL)/30000UL;
462 hscale = ((1135UL*4096UL)/xsf-4096);
463 vdelay=btv->win.cropy+0x20;
464 hdelay=((hactive*186)/922+btv->win.cropx)&0x3fe;
466 sr=((btv->win.cropheight>>inter)*512)/btv->win.height-512;
467 vscale=(0x10000UL-sr)&0x1fff;
468 vactive=btv->win.cropheight;
470 #if 0
471 printk("bttv: hscale=0x%04x, ",hscale);
472 printk("bttv: vscale=0x%04x\n",vscale);
474 printk("bttv: hdelay =0x%04x\n",hdelay);
475 printk("bttv: hactive=0x%04x\n",hactive);
476 printk("bttv: vdelay =0x%04x\n",vdelay);
477 printk("bttv: vactive=0x%04x\n",vactive);
478 #endif
481 * Interlace is set elsewhere according to the final image
482 * size we desire
485 if (btv->win.interlace)
487 btor(BT848_VSCALE_INT, BT848_E_VSCALE_HI);
488 btor(BT848_VSCALE_INT, BT848_O_VSCALE_HI);
490 else
492 btand(~BT848_VSCALE_INT, BT848_E_VSCALE_HI);
493 btand(~BT848_VSCALE_INT, BT848_O_VSCALE_HI);
497 * Load her up
500 btwrite(hscale>>8, BT848_E_HSCALE_HI);
501 btwrite(hscale>>8, BT848_O_HSCALE_HI);
502 btwrite(hscale&0xff, BT848_E_HSCALE_LO);
503 btwrite(hscale&0xff, BT848_O_HSCALE_LO);
505 btwrite((vscale>>8)|(btread(BT848_E_VSCALE_HI)&0xe0), BT848_E_VSCALE_HI);
506 btwrite((vscale>>8)|(btread(BT848_O_VSCALE_HI)&0xe0), BT848_O_VSCALE_HI);
507 btwrite(vscale&0xff, BT848_E_VSCALE_LO);
508 btwrite(vscale&0xff, BT848_O_VSCALE_LO);
510 btwrite(hactive&0xff, BT848_E_HACTIVE_LO);
511 btwrite(hactive&0xff, BT848_O_HACTIVE_LO);
512 btwrite(hdelay&0xff, BT848_E_HDELAY_LO);
513 btwrite(hdelay&0xff, BT848_O_HDELAY_LO);
515 btwrite(vactive&0xff, BT848_E_VACTIVE_LO);
516 btwrite(vactive&0xff, BT848_O_VACTIVE_LO);
517 btwrite(vdelay&0xff, BT848_E_VDELAY_LO);
518 btwrite(vdelay&0xff, BT848_O_VDELAY_LO);
520 crop=((hactive>>8)&0x03)|((hdelay>>6)&0x0c)|
521 ((vactive>>4)&0x30)|((vdelay>>2)&0xc0);
522 btwrite(crop, BT848_E_CROP);
523 btwrite(crop, BT848_O_CROP);
528 * The floats in the tuner struct are computed at compile time
529 * by gcc and cast back to integers. Thus we don't violate the
530 * "no float in kernel" rule.
533 static struct tunertype tuners[] = {
534 {"Temic PAL", TEMIC, PAL,
535 16*140.25,16*463.25,0x02,0x04,0x01,0x8e,0xc2, 623},
536 {"Philips PAL_I", Philips, PAL_I,
537 16*140.25,16*463.25,0x00,0x00,0x00,0x00,0x00, 623},
538 {"Philips NTSC", Philips, NTSC,
539 16*157.25,16*451.25,0xA0,0x90,0x30,0x8e,0xc0, 732},
540 {"Philips SECAM", Philips, SECAM,
541 16*168.25,16*447.25,0xA3,0x93,0x33,0x8e,0xc0, 623},
542 {"NoTuner", NoTuner, NOTUNER,
543 0 ,0 ,0x00,0x00,0x00,0x00,0x00, 0},
544 {"Philips PAL", Philips, PAL,
545 16*168.25,16*447.25,0xA0,0x90,0x30,0x8e,0xc0, 623},
546 {"Temic NTSC", TEMIC, NTSC,
547 16*157.25,16*463.25,0x02,0x04,0x01,0x8e,0xc2, 732},
548 {"TEMIC PAL_I", TEMIC, PAL_I,
549 0 ,0 ,0x00,0x00,0x00,0x00,0xc2, 623},
553 * Set TSA5522 synthesizer frequency in 1/16 Mhz steps
556 static void set_freq(struct bttv *btv, ushort freq)
558 u8 config;
559 u16 div;
560 struct tunertype *tun=&tuners[btv->tuner];
561 int oldAudio = btv->audio;
563 audio(btv, AUDIO_MUTE);
564 udelay(AUDIO_MUTE_DELAY);
566 if (freq < tun->thresh1)
567 config = tun->VHF_L;
568 else if (freq < tun->thresh2)
569 config = tun->VHF_H;
570 else
571 config = tun->UHF;
573 if(freq < tun->thresh1)
574 config = tun->VHF_L;
575 else if(freq < tun->thresh2)
576 config = tun->VHF_H;
577 else
578 config=tun->UHF;
580 div=freq+tun->IFPCoff;
582 div&=0x7fff;
584 if (I2CWrite(btv, btv->tuneradr, (div>>8)&0x7f, div&0xff, 1)<0)
585 return;
586 I2CWrite(btv, btv->tuneradr, tun->config, config, 1);
587 if (!(oldAudio & AUDIO_MUTE))
589 udelay(FREQ_CHANGE_DELAY);
590 audio(btv, AUDIO_UNMUTE);
594 static long bttv_write(struct video_device *v, const char *buf, unsigned long count, int nonblock)
596 return -EINVAL;
599 static long bttv_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
601 struct bttv *btv= (struct bttv *)v;
602 int q,todo;
604 todo=count;
605 while (todo && todo>(q=VBIBUF_SIZE-btv->vbip))
607 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, q))
608 return -EFAULT;
609 todo-=q;
610 buf+=q;
612 /* btv->vbip=0; */
613 cli();
614 if (todo && q==VBIBUF_SIZE-btv->vbip)
616 if(nonblock)
618 sti();
619 if(count==todo)
620 return -EWOULDBLOCK;
621 return count-todo;
623 interruptible_sleep_on(&btv->vbiq);
624 sti();
625 if(signal_pending(current))
627 if(todo==count)
628 return -EINTR;
629 else
630 return count-todo;
634 if (todo)
636 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, todo))
637 return -EFAULT;
638 btv->vbip+=todo;
640 return count;
644 * Open a bttv card. Right now the flags stuff is just playing
647 static int bttv_open(struct video_device *dev, int flags)
649 struct bttv *btv = (struct bttv *)dev;
650 int users, i;
652 switch (flags)
654 case 0:
655 if (btv->user)
656 return -EBUSY;
657 btv->user++;
658 audio(btv, AUDIO_UNMUTE);
659 for (i=users=0; i<bttv_num; i++)
660 users+=bttvs[i].user;
661 if (users==1)
662 find_vga();
663 break;
664 case 1:
665 break;
666 case 2:
667 btv->vbip=VBIBUF_SIZE;
668 btv->cap|=0x0c;
669 bt848_set_risc_jmps(btv);
670 break;
672 MOD_INC_USE_COUNT;
673 return 0;
676 static void bttv_close(struct video_device *dev)
678 struct bttv *btv=(struct bttv *)dev;
680 btv->user--;
681 audio(btv, AUDIO_MUTE);
682 btv->cap&=~3;
683 #if 0 /* FIXME */
684 if(minor&0x20)
686 btv->cap&=~0x0c;
688 #endif
689 bt848_set_risc_jmps(btv);
691 MOD_DEC_USE_COUNT;
694 /***********************************/
695 /* ioctls and supporting functions */
696 /***********************************/
698 extern inline void bt848_bright(struct bttv *btv, uint bright)
700 btwrite(bright&0xff, BT848_BRIGHT);
703 extern inline void bt848_hue(struct bttv *btv, uint hue)
705 btwrite(hue&0xff, BT848_HUE);
708 extern inline void bt848_contrast(struct bttv *btv, uint cont)
710 unsigned int conthi;
712 conthi=(cont>>6)&4;
713 btwrite(cont&0xff, BT848_CONTRAST_LO);
714 btaor(conthi, ~4, BT848_E_CONTROL);
715 btaor(conthi, ~4, BT848_O_CONTROL);
718 extern inline void bt848_sat_u(struct bttv *btv, ulong data)
720 u32 datahi;
722 datahi=(data>>7)&2;
723 btwrite(data&0xff, BT848_SAT_U_LO);
724 btaor(datahi, ~2, BT848_E_CONTROL);
725 btaor(datahi, ~2, BT848_O_CONTROL);
728 static inline void bt848_sat_v(struct bttv *btv, ulong data)
730 u32 datahi;
732 datahi=(data>>8)&1;
733 btwrite(data&0xff, BT848_SAT_V_LO);
734 btaor(datahi, ~1, BT848_E_CONTROL);
735 btaor(datahi, ~1, BT848_O_CONTROL);
739 * Cliprect -> risc table.
741 * FIXME: This is generating wrong code when we have some kinds of
742 * rectangle lists. If you generate overlapped rectangles then it
743 * gets a bit confused. Since we add the frame buffer clip rectangles
744 * we need to fix this. Better yet to rewrite this function.
747 static void write_risc_data(struct bttv *btv, struct video_clip *vp, int count)
749 int i;
750 u32 yy, y, x, dx, ox;
751 u32 *rmem, *rmem2;
752 struct video_clip first, *cur, *cur2, *nx, first2, *prev, *nx2;
753 u32 *rp, rpo=0, rpe=0, p, bpsl;
754 u32 *rpp;
755 u32 mask;
756 int interlace;
757 int depth;
759 rmem=(u32 *)btv->risc_odd;
760 rmem2=(u32 *)btv->risc_even;
761 depth=btv->win.bpp;
763 /* create y-sorted list */
765 first.next=NULL;
766 for (i=0; i<count; i++)
768 cur=&first;
769 while ((nx=cur->next) && (vp[i].y > cur->next->y))
770 cur=nx;
771 cur->next=&(vp[i]);
772 vp[i].next=nx;
774 first2.next=NULL;
776 rmem[rpo++]=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; rmem[rpo++]=0;
778 rmem2[rpe++]=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; rmem2[rpe++]=0;
782 * 32bit depth frame buffers need extra flags setting
785 if (depth==4)
786 mask=BT848_RISC_BYTE3;
787 else
788 mask=0;
790 bpsl=btv->win.width*btv->win.bpp;
791 p=btv->win.vidadr+btv->win.x*btv->win.bpp+
792 btv->win.y*btv->win.bpl;
794 interlace=btv->win.interlace;
797 * Loop through all lines
800 for (yy=0; yy<(btv->win.height<<(1^interlace)); yy++)
802 y=yy>>(1^interlace);
805 * Even or odd frame generation. We have to
806 * write the RISC instructions to the right stream.
809 if(!(y&1))
811 rp=&rpo;
812 rpp=rmem;
814 else
816 rp=&rpe;
817 rpp=rmem2;
822 * first2 is the header of a list of "active" rectangles. We add
823 * rectangles as we hit their top and remove them as they fall off
824 * the bottom
827 /* remove rects with y2 > y */
828 if ((cur=first2.next))
830 prev=&first2;
833 if (cur->y+cur->height < y)
834 prev->next=cur->next;
835 else
836 prev=cur;
838 while ((cur=cur->next));
842 * Fixme - we have to handle overlapped rectangles
843 * here, but the overlap might be partial
846 /* add rect to second (x-sorted) list if rect.y == y */
847 if ((cur=first.next))
849 while ((cur) && (cur->y == y))
851 first.next=cur->next;
852 cur2=&first2;
853 while ((nx2=cur2->next) && (cur->x > cur2->next->x))
854 cur2=nx2;
855 cur2->next=cur;
856 cur->next=nx2;
857 cur=first.next;
863 * Begin writing the RISC script
866 dx=x=0;
869 * Starting at x position 0 on a new scan line
870 * write to location p, don't yet write the number
871 * of pixels for the instruction
874 rpp[(*rp)++]=BT848_RISC_WRITE|BT848_RISC_SOL;
875 rpp[(*rp)++]=p;
878 * For each rectangle we have in the "active" list - sorted left to
879 * right..
882 for (cur2=first2.next; cur2; cur2=cur2->next)
885 * If we are to the left of the first drawing area
888 if (x+dx < cur2->x)
890 /* Bytes pending ? */
891 if (dx)
893 /* For a delta away from the start we need to write a SKIP */
894 if (x)
895 rpp[(*rp)++]=BT848_RISC_SKIP|(dx*depth);
896 else
897 /* Rewrite the start of line WRITE to a SKIP */
898 rpp[(*rp)-2]|=BT848_RISC_BYTE_ALL|(dx*depth);
899 /* Move X to the next point (drawing start) */
900 x=x+dx;
902 /* Ok how far are we from the start of the next rectangle ? */
903 dx=cur2->x-x;
904 /* dx is now the size of data to write */
906 /* If this isnt the left edge generate a "write continue" */
907 if (x)
908 rpp[(*rp)++]=BT848_RISC_WRITEC|(dx*depth)|mask;
909 else
910 /* Fill in the byte count on the initial WRITE */
911 rpp[(*rp)-2]|=(dx*depth)|mask;
912 /* Move to the start of the rectangle */
913 x=cur2->x;
914 /* x is our left dx is byte size of hole */
915 dx=cur2->width+1;
917 else
918 /* Already in a clip zone.. set dx */
919 if (x+dx < cur2->x+cur2->width)
920 dx=cur2->x+cur2->width-x+1;
922 /* now treat the rest of the line */
923 ox=x;
924 if (dx)
926 /* Complete the SKIP to eat to the end of the gap */
927 if (x)
928 rpp[(*rp)++]=BT848_RISC_SKIP|(dx*depth);
929 else
930 /* Rewrite to SKIP start to this point */
931 rpp[(*rp)-2]|=BT848_RISC_BYTE_ALL|(dx*depth);
932 x=x+dx;
936 * Not at the right hand edge ?
939 if ((dx=btv->win.width-x)!=0)
941 /* Write to edge of display */
942 if (x)
943 rpp[(*rp)++]=BT848_RISC_WRITEC|(dx*depth)|BT848_RISC_EOL|mask;
944 else
945 /* Entire frame is a write - patch first order */
946 rpp[(*rp)-2]|=(dx*depth)|BT848_RISC_EOL|mask;
948 else
950 /* End of line if needed */
951 if (ox)
952 rpp[(*rp)-1]|=BT848_RISC_EOL|mask;
953 else
955 /* Skip the line : write a SKIP + start/end of line marks */
956 (*rp)--;
957 rpp[(*rp)-1]=BT848_RISC_SKIP|(btv->win.width*depth)|
958 BT848_RISC_EOL|BT848_RISC_SOL;
962 * Move the video render pointer on a line
964 if (interlace||(y&1))
965 p+=btv->win.bpl;
969 * Attach the interframe jumps
972 rmem[rpo++]=BT848_RISC_JUMP;
973 rmem[rpo++]=btv->bus_vbi_even;
975 rmem2[rpe++]=BT848_RISC_JUMP;
976 rmem2[rpe++]=btv->bus_vbi_odd;
980 * Helper for adding clips.
983 static void new_risc_clip(struct video_window *vw, struct video_clip *vcp, int x, int y, int w, int h)
985 vcp[vw->clipcount].x=x;
986 vcp[vw->clipcount].y=y;
987 vcp[vw->clipcount].width=w;
988 vcp[vw->clipcount].height=h;
989 vw->clipcount++;
993 * ioctl routine
996 static int bttv_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
998 unsigned char eedata[256];
999 /* unsigned long data;*/
1000 /* static ushort creg;*/
1001 struct bttv *btv=(struct bttv *)dev;
1002 static int lastchan=0;
1004 switch (cmd)
1006 case VIDIOCGCAP:
1008 struct video_capability b;
1009 strcpy(b.name,btv->video_dev.name);
1010 b.type = VID_TYPE_CAPTURE|
1011 VID_TYPE_TUNER|
1012 VID_TYPE_TELETEXT|
1013 VID_TYPE_OVERLAY|
1014 VID_TYPE_CLIPPING|
1015 VID_TYPE_FRAMERAM|
1016 VID_TYPE_SCALES;
1017 b.channels = 4; /* tv , input, svhs */
1018 b.audios = 4; /* tv, input, svhs */
1019 b.maxwidth = 768;
1020 b.maxheight = 576;
1021 b.minwidth = 32;
1022 b.minheight = 32;
1023 if(copy_to_user(arg,&b,sizeof(b)))
1024 return -EFAULT;
1025 return 0;
1027 case VIDIOCGCHAN:
1029 struct video_channel v;
1030 if(copy_from_user(&v, arg,sizeof(v)))
1031 return -EFAULT;
1032 v.flags=VIDEO_VC_AUDIO;
1033 v.tuners=0;
1034 v.type=VIDEO_TYPE_CAMERA;
1035 switch(v.channel)
1037 case 0:
1038 strcpy(v.name,"Television");
1039 v.flags|=VIDEO_VC_TUNER;
1040 v.type=VIDEO_TYPE_TV;
1041 v.tuners=1;
1042 break;
1043 case 1:
1044 strcpy(v.name,"Composite1");
1045 break;
1046 case 2:
1047 strcpy(v.name,"Composite2");
1048 break;
1049 case 3:
1050 strcpy(v.name,"SVHS");
1051 break;
1052 default:
1053 return -EINVAL;
1055 if(copy_to_user(arg,&v,sizeof(v)))
1056 return -EFAULT;
1057 return 0;
1060 * Each channel has 1 tuner
1062 case VIDIOCSCHAN:
1064 int v;
1065 if(copy_from_user(&v, arg, sizeof(v)))
1066 return -EFAULT;
1067 bt848_muxsel(btv, v);
1068 lastchan=v;
1069 return 0;
1071 case VIDIOCGTUNER:
1073 struct video_tuner v;
1074 if(copy_from_user(&v,arg,sizeof(v))!=0)
1075 return -EFAULT;
1076 if(v.tuner||lastchan) /* Only tuner 0 */
1077 return -EINVAL;
1078 strcpy(v.name, "Television");
1079 v.rangelow=0;
1080 v.rangehigh=0xFFFFFFFF;
1081 v.flags=VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC;
1082 v.mode = btv->win.norm;
1083 if(copy_to_user(arg,&v,sizeof(v)))
1084 return -EFAULT;
1085 return 0;
1087 /* We have but tuner 0 */
1088 case VIDIOCSTUNER:
1090 struct video_tuner v;
1091 if(copy_from_user(&v, arg, sizeof(v)))
1092 return -EFAULT;
1093 /* Only channel 0 has a tuner */
1094 if(v.tuner!=0 || lastchan)
1095 return -EINVAL;
1096 if(v.mode!=VIDEO_MODE_PAL&&v.mode!=VIDEO_MODE_NTSC)
1097 return -EOPNOTSUPP;
1098 btv->win.norm = v.mode;
1099 bt848_set_size(btv);
1100 return 0;
1102 case VIDIOCGPICT:
1104 struct video_picture p=btv->picture;
1105 if(btv->win.bpp==1)
1106 p.palette=VIDEO_PALETTE_HI240;
1107 if(btv->win.bpp==2)
1108 p.palette=VIDEO_PALETTE_RGB565;
1109 if(btv->win.bpp==3)
1110 p.palette=VIDEO_PALETTE_RGB24;
1111 if(btv->win.bpp==4)
1112 p.palette=VIDEO_PALETTE_RGB32;
1113 if(copy_to_user(arg, &p, sizeof(p)))
1114 return -EFAULT;
1115 return 0;
1117 case VIDIOCSPICT:
1119 struct video_picture p;
1120 if(copy_from_user(&p, arg,sizeof(p)))
1121 return -EFAULT;
1122 /* We want -128 to 127 we get 0-65535 */
1123 bt848_bright(btv, (p.brightness>>8)-128);
1124 /* 0-511 for the colour */
1125 bt848_sat_u(btv, p.colour>>7);
1126 bt848_sat_v(btv, ((p.colour>>7)*201L)/237);
1127 /* -128 to 127 */
1128 bt848_hue(btv, (p.hue>>8)-128);
1129 /* 0-511 */
1130 bt848_contrast(btv, p.contrast>>7);
1131 btv->picture=p;
1132 return 0;
1134 case VIDIOCSWIN:
1136 struct video_window vw;
1137 struct video_clip *vcp;
1138 int on;
1140 if(copy_from_user(&vw,arg,sizeof(vw)))
1141 return -EFAULT;
1143 if(vw.flags)
1144 return -EINVAL;
1146 btv->win.x=vw.x;
1147 btv->win.y=vw.y;
1148 btv->win.width=vw.width;
1149 btv->win.height=vw.height;
1151 if(btv->win.height>btv->win.cropheight/2)
1152 btv->win.interlace=1;
1153 else
1154 btv->win.interlace=0;
1156 on=(btv->cap&3)?1:0;
1158 bt848_cap(btv,0);
1159 bt848_set_size(btv);
1161 if(vw.clipcount>256)
1162 return -EDOM; /* Too many! */
1165 * Do any clips.
1168 vcp=vmalloc(sizeof(struct video_clip)*(vw.clipcount+4));
1169 if(vcp==NULL)
1170 return -ENOMEM;
1171 if(vw.clipcount && copy_from_user(vcp,vw.clips,sizeof(struct video_clip)*vw.clipcount))
1172 return -EFAULT;
1174 * Impose display clips
1176 if(btv->win.x<0)
1177 new_risc_clip(&vw, vcp, 0, 0, -btv->win.x, btv->win.height-1);
1178 if(btv->win.y<0)
1179 new_risc_clip(&vw, vcp, 0, 0, btv->win.width-1,-btv->win.y);
1180 if(btv->win.x+btv->win.width> btv->win.swidth)
1181 new_risc_clip(&vw, vcp, btv->win.swidth-btv->win.x, 0, btv->win.width-1, btv->win.height-1);
1182 if(btv->win.y+btv->win.height > btv->win.sheight)
1183 new_risc_clip(&vw, vcp, 0, btv->win.sheight-btv->win.y, btv->win.width-1, btv->win.height-1);
1185 * Question: Do we need to walk the clip list
1186 * and saw off any clips outside the window
1187 * frame or will write_risc_tab do the right
1188 * thing ?
1190 write_risc_data(btv,vcp, vw.clipcount);
1191 vfree(vcp);
1192 if(on)
1193 bt848_cap(btv,1);
1194 return 0;
1196 case VIDIOCGWIN:
1198 struct video_window vw;
1199 /* Oh for a COBOL move corresponding .. */
1200 vw.x=btv->win.x;
1201 vw.y=btv->win.y;
1202 vw.width=btv->win.width;
1203 vw.height=btv->win.height;
1204 vw.chromakey=0;
1205 vw.flags=0;
1206 if(btv->win.interlace)
1207 vw.flags|=VIDEO_WINDOW_INTERLACE;
1208 if(copy_to_user(arg,&vw,sizeof(vw)))
1209 return -EFAULT;
1210 return 0;
1212 case VIDIOCCAPTURE:
1214 int v;
1215 if(copy_from_user(&v, arg,sizeof(v)))
1216 return -EFAULT;
1217 if(btv->win.vidadr==0 || btv->win.width==0 || btv->win.height==0)
1218 return -EINVAL;
1219 if(v==0)
1221 bt848_cap(btv,0);
1223 else
1225 bt848_cap(btv,1);
1227 return 0;
1229 case VIDIOCGFBUF:
1231 struct video_buffer v;
1232 v.base=(void *)btv->win.vidadr;
1233 v.height=btv->win.sheight;
1234 v.width=btv->win.swidth;
1235 v.depth=btv->win.bpp*8;
1236 v.bytesperline=btv->win.bpl;
1237 if(copy_to_user(arg, &v,sizeof(v)))
1238 return -EFAULT;
1239 return 0;
1242 case VIDIOCSFBUF:
1244 struct video_buffer v;
1245 if(!suser())
1246 return -EPERM;
1247 if(copy_from_user(&v, arg,sizeof(v)))
1248 return -EFAULT;
1249 if(v.depth!=8 && v.depth!=16 && v.depth!=24 && v.depth!=32)
1250 return -EINVAL;
1251 btv->win.vidadr=(int)v.base;
1252 btv->win.sheight=v.height;
1253 btv->win.swidth=v.width;
1254 btv->win.bpp=v.depth/8;
1255 btv->win.bpl=v.bytesperline;
1257 DEBUG(printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1258 v.base, v.width,v.height, btv->win.bpp, btv->win.bpl));
1259 bt848_set_size(btv);
1260 return 0;
1262 case VIDIOCKEY:
1264 /* Will be handled higher up .. */
1265 return 0;
1267 case VIDIOCGFREQ:
1269 unsigned long v=btv->win.freq;
1270 if(copy_to_user(arg,&v,sizeof(v)))
1271 return -EFAULT;
1272 return 0;
1274 case VIDIOCSFREQ:
1276 unsigned long v;
1277 if(copy_from_user(&v, arg, sizeof(v)))
1278 return -EFAULT;
1279 btv->win.freq=v;
1280 set_freq(btv, btv->win.freq);
1281 return 0;
1284 case VIDIOCGAUDIO:
1286 struct video_audio v;
1287 v=btv->audio_dev;
1288 v.flags&=~(VIDEO_AUDIO_MUTE|VIDEO_AUDIO_MUTABLE);
1289 v.flags|=VIDEO_AUDIO_MUTABLE;
1290 strcpy(v.name,"TV");
1291 if(copy_to_user(arg,&v,sizeof(v)))
1292 return -EFAULT;
1293 return 0;
1295 case VIDIOCSAUDIO:
1297 struct video_audio v;
1298 if(copy_from_user(&v,arg, sizeof(v)))
1299 return -EFAULT;
1300 if(v.flags&VIDEO_AUDIO_MUTE)
1301 audio(btv, AUDIO_MUTE);
1302 if(v.audio<0||v.audio>2)
1303 return -EINVAL;
1304 bt848_muxsel(btv,v.audio);
1305 if(!(v.flags&VIDEO_AUDIO_MUTE))
1306 audio(btv, AUDIO_UNMUTE);
1307 btv->audio_dev=v;
1308 return 0;
1311 case BTTV_WRITEEE:
1312 if(copy_from_user((void *) eedata, (void *) arg, 256))
1313 return -EFAULT;
1314 writeee(btv, eedata);
1315 break;
1317 case BTTV_READEE:
1318 readee(btv, eedata);
1319 if(copy_to_user((void *) arg, (void *) eedata, 256))
1320 return -EFAULT;
1321 break;
1323 default:
1324 return -ENOIOCTLCMD;
1326 return 0;
1329 static int bttv_init_done(struct video_device *dev)
1331 return 0;
1334 static struct video_device bttv_template=
1336 "UNSET",
1337 VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY|VID_TYPE_TELETEXT,
1338 VID_HARDWARE_BT848,
1339 bttv_open,
1340 bttv_close,
1341 bttv_read,
1342 bttv_write,
1343 bttv_ioctl,
1344 NULL, /* no mmap yet */
1345 bttv_init_done,
1346 NULL,
1351 struct vidbases
1353 ushort vendor, device;
1354 char *name;
1355 uint badr;
1358 static struct vidbases vbs[] = {
1359 { PCI_VENDOR_ID_TSENG, 0, "TSENG", PCI_BASE_ADDRESS_0},
1360 { PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL,
1361 "Matrox Millennium", PCI_BASE_ADDRESS_1},
1362 { PCI_VENDOR_ID_MATROX, 0x051a, "Matrox Mystique", PCI_BASE_ADDRESS_1},
1363 { PCI_VENDOR_ID_S3, 0, "S3", PCI_BASE_ADDRESS_0},
1364 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_210888GX,
1365 "ATI MACH64 Winturbo", PCI_BASE_ADDRESS_0},
1366 { PCI_VENDOR_ID_CIRRUS, 0, "Cirrus Logic", PCI_BASE_ADDRESS_0},
1367 { PCI_VENDOR_ID_N9, PCI_DEVICE_ID_N9_I128,
1368 "Number Nine Imagine 128", PCI_BASE_ADDRESS_0},
1369 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TGA,
1370 "DEC DC21030", PCI_BASE_ADDRESS_0},
1374 /* DEC TGA offsets stolen from XFree-3.2 */
1376 static uint dec_offsets[4] = {
1377 0x200000,
1378 0x804000,
1380 0x1004000
1383 #define NR_CARDS (sizeof(vbs)/sizeof(struct vidbases))
1385 /* Scan for PCI display adapter
1386 if more than one card is present the last one is used for now */
1388 static int find_vga(void)
1390 unsigned int devfn, class, vendev;
1391 ushort vendor, device, badr;
1392 int found=0, bus=0, i, tga_type;
1393 unsigned int vidadr=0;
1396 for (devfn = 0; devfn < 0xff; devfn++)
1398 if (PCI_FUNC(devfn) != 0)
1399 continue;
1400 pcibios_read_config_dword(bus, devfn, PCI_VENDOR_ID, &vendev);
1401 if (vendev == 0xffffffff || vendev == 0x00000000)
1402 continue;
1403 pcibios_read_config_word(bus, devfn, PCI_VENDOR_ID, &vendor);
1404 pcibios_read_config_word(bus, devfn, PCI_DEVICE_ID, &device);
1405 pcibios_read_config_dword(bus, devfn, PCI_CLASS_REVISION, &class);
1406 class = class >> 16;
1407 /* if (class == PCI_CLASS_DISPLAY_VGA) {*/
1408 if ((class>>8) == PCI_BASE_CLASS_DISPLAY ||
1409 /* Number 9 GXE64Pro needs this */
1410 class == PCI_CLASS_NOT_DEFINED_VGA)
1412 badr=0;
1413 printk(KERN_INFO "bttv: PCI display adapter: ");
1414 for (i=0; i<NR_CARDS; i++)
1416 if (vendor==vbs[i].vendor)
1418 if (vbs[i].device)
1419 if (vbs[i].device!=device)
1420 continue;
1421 printk("%s.\n", vbs[i].name);
1422 badr=vbs[i].badr;
1423 break;
1426 if (!badr)
1428 printk(KERN_ERR "bttv: Unknown video memory base address.\n");
1429 continue;
1431 pcibios_read_config_dword(bus, devfn, badr, &vidadr);
1432 if (vidadr & PCI_BASE_ADDRESS_SPACE_IO)
1434 printk(KERN_ERR "bttv: Memory seems to be I/O memory.\n");
1435 printk(KERN_ERR "bttv: Check entry for your card type in bttv.c vidbases struct.\n");
1436 continue;
1438 vidadr &= PCI_BASE_ADDRESS_MEM_MASK;
1439 if (!vidadr)
1441 printk(KERN_ERR "bttv: Memory @ 0, must be something wrong!");
1442 continue;
1445 if (vendor==PCI_VENDOR_ID_DEC)
1446 if (device==PCI_DEVICE_ID_DEC_TGA)
1448 tga_type = (readl((unsigned long)vidadr) >> 12) & 0x0f;
1449 if (tga_type != 0 && tga_type != 1 && tga_type != 3)
1451 printk(KERN_ERR "bttv: TGA type (0x%x) unrecognized!\n", tga_type);
1452 found--;
1454 vidadr+=dec_offsets[tga_type];
1457 DEBUG(printk(KERN_DEBUG "bttv: memory @ 0x%08x, ", vidadr));
1458 DEBUG(printk(KERN_DEBUG "devfn: 0x%04x.\n", devfn));
1459 found++;
1463 if (vidmem)
1465 vidadr=vidmem<<20;
1466 printk(KERN_INFO "bttv: Video memory override: 0x%08x\n", vidadr);
1467 found=1;
1469 for (i=0; i<BTTV_MAX; i++)
1470 bttvs[i].win.vidadr=vidadr;
1472 return found;
1475 #define TRITON_PCON 0x50
1476 #define TRITON_BUS_CONCURRENCY (1<<0)
1477 #define TRITON_STREAMING (1<<1)
1478 #define TRITON_WRITE_BURST (1<<2)
1479 #define TRITON_PEER_CONCURRENCY (1<<3)
1481 static void handle_chipset(void)
1483 int index;
1485 for (index = 0; index < 8; index++)
1487 unsigned char bus, devfn;
1488 unsigned char b, bo;
1490 /* Beware the SiS 85C496 my friend - rev 49 don't work with a bttv */
1492 if (!pcibios_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_496, index, &bus, &devfn))
1494 printk(KERN_WARNING "BT848 and SIS 85C496 chipset don't always work together.\n");
1497 if (!pcibios_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441,
1498 index, &bus, &devfn))
1500 pcibios_read_config_byte(bus, devfn, 0x53, &b);
1501 DEBUG(printk(KERN_INFO "bttv: Host bridge: 82441FX Natoma, "));
1502 DEBUG(printk("bufcon=0x%02x\n",b));
1505 if (!pcibios_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441,
1506 index, &bus, &devfn))
1508 pcibios_read_config_byte(bus, devfn, 0x53, &b);
1509 DEBUG(printk(KERN_INFO "bttv: Host bridge: 82441FX Natoma, "));
1510 DEBUG(printk("bufcon=0x%02x\n",b));
1513 if (!pcibios_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437,
1514 index, &bus, &devfn))
1516 printk(KERN_INFO "bttv: Host bridge 82437FX Triton PIIX\n");
1517 pcibios_read_config_byte(bus, devfn, TRITON_PCON, &b);
1518 bo=b;
1519 DEBUG(printk(KERN_DEBUG "bttv: 82437FX: PCON: 0x%x\n",b));
1521 /* 430FX (Triton I) freezes with bus concurrency on -> switch it off */
1522 if(!(b & TRITON_BUS_CONCURRENCY))
1524 printk(KERN_WARNING "bttv: 82437FX: disabling bus concurrency\n");
1525 b |= TRITON_BUS_CONCURRENCY;
1528 /* still freezes on other boards -> switch off even more */
1529 if(b & TRITON_PEER_CONCURRENCY)
1531 printk(KERN_WARNING "bttv: 82437FX: disabling peer concurrency\n");
1532 b &= ~TRITON_PEER_CONCURRENCY;
1534 if(!(b & TRITON_STREAMING))
1536 printk(KERN_WARNING "bttv: 82437FX: disabling streaming\n");
1537 b |= TRITON_STREAMING;
1540 if (b!=bo)
1542 pcibios_write_config_byte(bus, devfn, TRITON_PCON, b);
1543 printk(KERN_DEBUG "bttv: 82437FX: PCON changed to: 0x%x\n",b);
1549 static void init_tda9850(struct bttv *btv)
1551 I2CWrite(btv, I2C_TDA9850, TDA9850_CON3, 0, 1);
1554 /* Figure out card and tuner type */
1556 static void idcard(struct bttv *btv)
1558 int i;
1560 btwrite(0, BT848_GPIO_OUT_EN);
1561 DEBUG(printk(KERN_DEBUG "bttv: GPIO: 0x%08x\n", btread(BT848_GPIO_DATA)));
1563 btv->type=BTTV_MIRO;
1564 btv->tuner=tuner;
1566 if (I2CRead(btv, I2C_HAUPEE)>=0)
1567 btv->type=BTTV_HAUPPAUGE;
1568 else if (I2CRead(btv, I2C_STBEE)>=0)
1569 btv->type=BTTV_STB;
1571 for (i=0xc0; i<0xd0; i+=2)
1573 if (I2CRead(btv, i)>=0)
1575 btv->tuneradr=i;
1576 break;
1580 btv->dbx = I2CRead(btv, I2C_TDA9850) ? 0 : 1;
1582 if (btv->dbx)
1583 init_tda9850(btv);
1585 /* How do I detect the tuner type for other cards but Miro ??? */
1586 printk(KERN_INFO "bttv: model: ");
1587 switch (btv->type)
1589 case BTTV_MIRO:
1590 btv->tuner=((btread(BT848_GPIO_DATA)>>10)-1)&7;
1591 printk("MIRO");
1592 strcpy(btv->video_dev.name,"BT848(Miro)");
1593 break;
1594 case BTTV_HAUPPAUGE:
1595 printk("HAUPPAUGE");
1596 strcpy(btv->video_dev.name,"BT848(Hauppauge)");
1597 break;
1598 case BTTV_STB:
1599 printk("STB");
1600 strcpy(btv->video_dev.name,"BT848(STB)");
1601 break;
1602 case BTTV_INTEL:
1603 printk("Intel");
1604 strcpy(btv->video_dev.name,"BT848(Intel)");
1605 break;
1606 case BTTV_DIAMOND:
1607 printk("Diamond");
1608 strcpy(btv->video_dev.name,"BT848(Diamond)");
1609 break;
1611 printk(" (%s @ 0x%02x)\n", tuners[btv->tuner].name, btv->tuneradr);
1612 audio(btv, AUDIO_MUTE);
1616 static void bt848_set_risc_jmps(struct bttv *btv)
1618 int flags=btv->cap;
1620 btv->risc_jmp[0]=BT848_RISC_SYNC|BT848_RISC_RESYNC|BT848_FIFO_STATUS_VRE;
1621 btv->risc_jmp[1]=0;
1623 btv->risc_jmp[2]=BT848_RISC_JUMP;
1624 if (flags&8)
1625 btv->risc_jmp[3]=virt_to_bus(btv->vbi_odd);
1626 else
1627 btv->risc_jmp[3]=virt_to_bus(btv->risc_jmp+4);
1629 btv->risc_jmp[4]=BT848_RISC_JUMP;
1630 if (flags&2)
1631 btv->risc_jmp[5]=virt_to_bus(btv->risc_odd);
1632 else
1633 btv->risc_jmp[5]=virt_to_bus(btv->risc_jmp+6);
1635 btv->risc_jmp[6]=BT848_RISC_SYNC|BT848_RISC_RESYNC|BT848_FIFO_STATUS_VRO;
1636 btv->risc_jmp[7]=0;
1638 btv->risc_jmp[8]=BT848_RISC_JUMP;
1639 if (flags&4)
1640 btv->risc_jmp[9]=virt_to_bus(btv->vbi_even);
1641 else
1642 btv->risc_jmp[9]=virt_to_bus(btv->risc_jmp+10);
1644 btv->risc_jmp[10]=BT848_RISC_JUMP;
1645 if (flags&1)
1646 btv->risc_jmp[11]=virt_to_bus(btv->risc_even);
1647 else
1648 btv->risc_jmp[11]=virt_to_bus(btv->risc_jmp);
1650 btaor(flags, ~0x0f, BT848_CAP_CTL);
1651 if (flags&0x0f)
1652 bt848_dma(btv, 3);
1653 else
1654 bt848_dma(btv, 0);
1658 static int init_bt848(struct bttv *btv)
1660 /* reset the bt848 */
1661 btwrite(0,BT848_SRESET);
1662 btv->user=0;
1664 DEBUG(printk(KERN_DEBUG "bttv: bt848_mem: 0x%08x\n",(unsigned int) btv->bt848_mem));
1666 /* default setup for max. PAL size in a 1024xXXX hicolor framebuffer */
1668 btv->win.norm=0; /* change this to 1 for NTSC, 2 for SECAM */
1669 btv->win.interlace=1;
1670 btv->win.x=0;
1671 btv->win.y=0;
1672 btv->win.width=768; /* 640 */
1673 btv->win.height=576; /* 480 */
1674 btv->win.cropwidth=768; /* 640 */
1675 btv->win.cropheight=576; /* 480 */
1676 btv->win.cropx=0;
1677 btv->win.cropy=0;
1678 btv->win.bpp=2;
1679 btv->win.bpl=1024*btv->win.bpp;
1680 btv->win.swidth=1024;
1681 btv->win.sheight=768;
1682 btv->cap=0;
1684 if (!(btv->risc_odd=(dword *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
1685 return -1;
1686 if (!(btv->risc_even=(dword *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
1687 return -1;
1688 if (!(btv->risc_jmp =(dword *) kmalloc(1024, GFP_KERNEL)))
1689 return -1;
1690 btv->vbi_odd=btv->risc_jmp+12;
1691 btv->vbi_even=btv->vbi_odd+256;
1692 btv->bus_vbi_odd=virt_to_bus(btv->risc_jmp);
1693 btv->bus_vbi_even=virt_to_bus(btv->risc_jmp+6);
1695 btwrite(virt_to_bus(btv->risc_jmp+2), BT848_RISC_STRT_ADD);
1696 btv->vbibuf=(unchar *) vmalloc(VBIBUF_SIZE);
1697 if (!btv->vbibuf)
1698 return -1;
1700 bt848_muxsel(btv, 1);
1701 bt848_set_size(btv);
1703 /* btwrite(0, BT848_TDEC); */
1704 btwrite(0x10, BT848_COLOR_CTL);
1705 btwrite(0x00, BT848_CAP_CTL);
1707 btwrite(0x0ff, BT848_VBI_PACK_SIZE);
1708 btwrite(1, BT848_VBI_PACK_DEL);
1710 btwrite(0xfc, BT848_GPIO_DMA_CTL);
1711 btwrite(BT848_IFORM_MUX1 | BT848_IFORM_XTAUTO | BT848_IFORM_PAL_BDGHI,
1712 BT848_IFORM);
1714 bt848_bright(btv, 0x10);
1715 btwrite(0xd8, BT848_CONTRAST_LO);
1717 btwrite(0x60, BT848_E_VSCALE_HI);
1718 btwrite(0x60, BT848_O_VSCALE_HI);
1719 btwrite(/*BT848_ADC_SYNC_T|*/
1720 BT848_ADC_RESERVED|BT848_ADC_CRUSH, BT848_ADC);
1722 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1723 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1724 btwrite(0x00, BT848_E_SCLOOP);
1725 btwrite(0x00, BT848_O_SCLOOP);
1727 btwrite(0xffffffUL,BT848_INT_STAT);
1728 /* BT848_INT_PABORT|BT848_INT_RIPERR|BT848_INT_PPERR|BT848_INT_FDSR|
1729 BT848_INT_FTRGT|BT848_INT_FBUS|*/
1730 btwrite(BT848_INT_ETBF|
1731 BT848_INT_SCERR|
1732 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
1733 BT848_INT_FMTCHG|BT848_INT_HLOCK,
1734 BT848_INT_MASK);
1736 /* make_risctab(btv); */
1737 make_vbitab(btv);
1738 bt848_set_risc_jmps(btv);
1741 * Now add the template and register the device unit.
1744 memcpy(&btv->video_dev,&bttv_template,sizeof(bttv_template));
1745 idcard(btv);
1747 btv->picture.brightness=0x90<<8;
1748 btv->picture.contrast = 0x70 << 8;
1749 btv->picture.colour = 0x70<<8;
1750 btv->picture.hue = 0x8000;
1752 if(video_register_device(&btv->video_dev)<0)
1753 return -1;
1754 return 0;
1758 static void bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
1760 u32 stat,astat;
1761 u32 dstat;
1762 int count;
1763 struct bttv *btv;
1765 btv=(struct bttv *)dev_id;
1766 count=0;
1767 while (1)
1769 /* get/clear interrupt status bits */
1770 stat=btread(BT848_INT_STAT);
1771 astat=stat&btread(BT848_INT_MASK);
1772 if (!astat)
1773 return;
1774 btwrite(astat,BT848_INT_STAT);
1775 IDEBUG(printk ("bttv: astat %08x\n",astat));
1776 IDEBUG(printk ("bttv: stat %08x\n",stat));
1778 /* get device status bits */
1779 dstat=btread(BT848_DSTATUS);
1781 if (astat&BT848_INT_FMTCHG)
1783 IDEBUG(printk ("bttv: IRQ_FMTCHG\n"));
1784 /* btv->win.norm&=(dstat&BT848_DSTATUS_NUML) ? (~1) : (~0); */
1786 if (astat&BT848_INT_VPRES)
1788 IDEBUG(printk ("bttv: IRQ_VPRES\n"));
1790 if (astat&BT848_INT_VSYNC)
1792 IDEBUG(printk ("bttv: IRQ_VSYNC\n"));
1794 if (astat&BT848_INT_SCERR) {
1795 IDEBUG(printk ("bttv: IRQ_SCERR\n"));
1796 bt848_dma(btv, 0);
1797 bt848_dma(btv, 1);
1798 wake_up_interruptible(&btv->vbiq);
1799 wake_up_interruptible(&btv->capq);
1801 if (astat&BT848_INT_RISCI)
1803 IDEBUG(printk ("bttv: IRQ_RISCI\n"));
1804 /* printk ("bttv: IRQ_RISCI%d\n",stat>>28); */
1805 if (stat&(1<<28))
1807 btv->vbip=0;
1808 wake_up_interruptible(&btv->vbiq);
1810 if (stat&(2<<28))
1812 bt848_set_risc_jmps(btv);
1813 wake_up_interruptible(&btv->capq);
1814 break;
1817 if (astat&BT848_INT_OCERR)
1819 IDEBUG(printk ("bttv: IRQ_OCERR\n"));
1821 if (astat&BT848_INT_PABORT)
1823 IDEBUG(printk ("bttv: IRQ_PABORT\n"));
1825 if (astat&BT848_INT_RIPERR)
1827 IDEBUG(printk ("bttv: IRQ_RIPERR\n"));
1829 if (astat&BT848_INT_PPERR)
1831 IDEBUG(printk ("bttv: IRQ_PPERR\n"));
1833 if (astat&BT848_INT_FDSR)
1835 IDEBUG(printk ("bttv: IRQ_FDSR\n"));
1837 if (astat&BT848_INT_FTRGT)
1839 IDEBUG(printk ("bttv: IRQ_FTRGT\n"));
1841 if (astat&BT848_INT_FBUS)
1843 IDEBUG(printk ("bttv: IRQ_FBUS\n"));
1845 if (astat&BT848_INT_HLOCK)
1847 if (dstat&BT848_DSTATUS_HLOC)
1848 audio(btv, AUDIO_ON);
1849 else
1850 audio(btv, AUDIO_OFF);
1853 if (astat&BT848_INT_I2CDONE)
1857 count++;
1858 if (count > 10)
1859 printk (KERN_WARNING "bttv: irq loop %d\n", count);
1860 if (count > 20)
1862 btwrite(0, BT848_INT_MASK);
1863 printk(KERN_ERR "bttv: IRQ lockup, cleared int mask\n");
1870 * Scan for a Bt848 card, request the irq and map the io memory
1873 static int find_bt848(void)
1875 short pci_index;
1876 unsigned char command, latency;
1877 int result;
1878 unsigned char bus, devfn;
1879 struct bttv *btv;
1881 bttv_num=0;
1883 if (!pcibios_present())
1885 DEBUG(printk(KERN_DEBUG "bttv: PCI-BIOS not present or not accessable!\n"));
1886 return 0;
1889 for (pci_index = 0;
1890 !pcibios_find_device(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
1891 pci_index, &bus, &devfn);
1892 ++pci_index)
1894 btv=&bttvs[bttv_num];
1895 btv->bus=bus;
1896 btv->devfn=devfn;
1897 btv->bt848_mem=NULL;
1898 btv->vbibuf=NULL;
1899 btv->risc_jmp=NULL;
1900 btv->vbi_odd=NULL;
1901 btv->vbi_even=NULL;
1902 btv->vbiq=NULL;
1903 btv->capq=NULL;
1904 btv->vbip=VBIBUF_SIZE;
1906 pcibios_read_config_byte(btv->bus, btv->devfn,
1907 PCI_INTERRUPT_LINE, &btv->irq);
1908 pcibios_read_config_dword(btv->bus, btv->devfn, PCI_BASE_ADDRESS_0,
1909 &btv->bt848_adr);
1911 if (remap&&(!bttv_num))
1913 remap<<=20;
1914 remap&=PCI_BASE_ADDRESS_MEM_MASK;
1915 printk(KERN_INFO "Remapping to : 0x%08x.\n", remap);
1916 remap|=btv->bt848_adr&(~PCI_BASE_ADDRESS_MEM_MASK);
1917 pcibios_write_config_dword(btv->bus, btv->devfn, PCI_BASE_ADDRESS_0,
1918 remap);
1919 pcibios_read_config_dword(btv->bus, btv->devfn, PCI_BASE_ADDRESS_0,
1920 &btv->bt848_adr);
1923 btv->bt848_adr&=PCI_BASE_ADDRESS_MEM_MASK;
1924 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_CLASS_REVISION,
1925 &btv->revision);
1926 printk(KERN_INFO "bttv: Brooktree Bt848 (rev %d) ",btv->revision);
1927 printk("bus: %d, devfn: %d, ",
1928 btv->bus, btv->devfn);
1929 printk("irq: %d, ",btv->irq);
1930 printk("memory: 0x%08x.\n", btv->bt848_adr);
1932 btv->bt848_mem=ioremap(btv->bt848_adr, 0x1000);
1934 result = request_irq(btv->irq, bttv_irq,
1935 SA_SHIRQ | SA_INTERRUPT,"bttv",(void *)btv);
1936 if (result==-EINVAL)
1938 printk(KERN_ERR "bttv: Bad irq number or handler\n");
1939 return -EINVAL;
1941 if (result==-EBUSY)
1943 printk(KERN_ERR "bttv: IRQ %d busy, change your PnP config in BIOS\n",btv->irq);
1944 return result;
1946 if (result < 0)
1947 return result;
1949 /* Enable bus-mastering */
1950 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_COMMAND, &command);
1951 command|=PCI_COMMAND_MASTER;
1952 pcibios_write_config_byte(btv->bus, btv->devfn, PCI_COMMAND, command);
1953 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_COMMAND, &command);
1954 if (!(command&PCI_COMMAND_MASTER))
1956 printk(KERN_ERR "bttv: PCI bus-mastering could not be enabled\n");
1957 return -1;
1959 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_LATENCY_TIMER,
1960 &latency);
1961 if (!latency)
1963 latency=32;
1964 pcibios_write_config_byte(btv->bus, btv->devfn, PCI_LATENCY_TIMER,
1965 latency);
1967 DEBUG(printk(KERN_DEBUG "bttv: latency: %02x\n", latency));
1968 bttv_num++;
1970 if(bttv_num)
1971 printk(KERN_INFO "bttv: %d Bt848 card(s) found.\n", bttv_num);
1972 return bttv_num;
1975 static void release_bttv(void)
1977 u8 command;
1978 int i;
1979 struct bttv *btv;
1981 for (i=0;i<bttv_num; i++)
1983 btv=&bttvs[i];
1984 /* turn off all capturing, DMA and IRQs */
1986 /* first disable interrupts before unmapping the memory! */
1987 btwrite(0, BT848_INT_MASK);
1988 btwrite(0xffffffffUL,BT848_INT_STAT);
1989 btwrite(0x0, BT848_GPIO_OUT_EN);
1991 bt848_cap(btv, 0);
1993 /* disable PCI bus-mastering */
1994 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_COMMAND, &command);
1995 command|=PCI_COMMAND_MASTER;
1996 pcibios_write_config_byte(btv->bus, btv->devfn, PCI_COMMAND, command);
1998 /* unmap and free memory */
1999 if (btv->risc_odd)
2000 kfree((void *) btv->risc_odd);
2002 if (btv->risc_even)
2003 kfree((void *) btv->risc_even);
2005 DEBUG(printk(KERN_DEBUG "free: risc_jmp: 0x%08x.\n", btv->risc_jmp));
2006 if (btv->risc_jmp)
2007 kfree((void *) btv->risc_jmp);
2009 DEBUG(printk(KERN_DEBUG "bt848_vbibuf: 0x%08x.\n", btv->vbibuf));
2010 if (btv->vbibuf)
2011 vfree((void *) btv->vbibuf);
2012 free_irq(btv->irq,btv);
2013 DEBUG(printk(KERN_DEBUG "bt848_mem: 0x%08x.\n", btv->bt848_mem));
2014 if (btv->bt848_mem)
2015 iounmap(btv->bt848_mem);
2016 video_unregister_device(&btv->video_dev);
2021 #ifdef MODULE
2023 int init_module(void)
2025 #else
2026 int init_bttv_cards(struct video_init *unused)
2028 #endif
2029 int i;
2031 handle_chipset();
2032 if (find_bt848()<0)
2033 return -EIO;
2035 for (i=0; i<bttv_num; i++)
2037 if (init_bt848(&bttvs[i])<0)
2039 release_bttv();
2040 return -EIO;
2043 return 0;
2046 #ifdef MODULE
2048 void cleanup_module(void)
2050 release_bttv();
2053 #endif