Linux 2.2.0
[davej-history.git] / drivers / char / bttv.c
blob90c1174679b71fce345ca8d2a6811a84e5b0cd16
1 /*
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)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/version.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
26 #include <linux/fs.h>
27 #include <linux/kernel.h>
28 #include <linux/major.h>
29 #include <linux/malloc.h>
30 #include <linux/mm.h>
31 #if LINUX_VERSION_CODE >= 0x020100
32 #include <linux/poll.h>
33 #endif
34 #include <linux/pci.h>
35 #include <linux/signal.h>
36 #include <asm/io.h>
37 #include <linux/ioport.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/wrapper.h>
44 #include <linux/interrupt.h>
45 #include <linux/version.h>
47 #if LINUX_VERSION_CODE >= 0x020100
48 #include <asm/uaccess.h>
49 #include <linux/vmalloc.h>
50 #else
51 #include <linux/bios32.h>
52 #define mdelay(x) udelay((x)*1000)
53 #define signal_pending(current) (current->signal & ~current->blocked)
54 #define sigfillset(set)
56 static inline int time_before(unsigned long a, unsigned long b)
58 return((long)((a) - (b)) < 0L);
61 static inline unsigned long
62 copy_to_user(void *to, const void *from, unsigned long n)
64 memcpy_tofs(to,from,n);
65 return 0;
68 static inline unsigned long
69 copy_from_user(void *to, const void *from, unsigned long n)
71 memcpy_fromfs(to,from,n);
72 return 0;
74 #define ioremap vremap
75 #define iounmap vfree
76 #endif
78 #include <linux/videodev.h>
79 #include <linux/i2c.h>
80 #include "bttv.h"
81 #include "tuner.h"
83 #define DEBUG(x) /* Debug driver */
84 #define IDEBUG(x) /* Debug interrupt handler */
86 #if LINUX_VERSION_CODE >= 0x020117
87 MODULE_PARM(vidmem,"i");
88 MODULE_PARM(triton1,"i");
89 MODULE_PARM(remap,"1-4i");
90 MODULE_PARM(radio,"1-4i");
91 MODULE_PARM(card,"1-4i");
92 MODULE_PARM(pll,"1-4i");
93 #endif
95 /* Anybody who uses more than four? */
96 #define BTTV_MAX 4
98 static int find_vga(void);
99 static void bt848_set_risc_jmps(struct bttv *btv);
101 static unsigned int vidmem=0; /* manually set video mem address */
102 static int triton1=0;
103 #ifndef USE_PLL
104 /* 0=no pll, 1=28MHz, 2=34MHz */
105 #define USE_PLL 0
106 #endif
107 #ifndef CARD_DEFAULT
108 /* card type (see bttv.h) 0=autodetect */
109 #define CARD_DEFAULT 0
110 #endif
112 static unsigned int remap[BTTV_MAX]; /* remap Bt848 */
113 static unsigned int radio[BTTV_MAX];
114 static unsigned int card[BTTV_MAX] = { CARD_DEFAULT, CARD_DEFAULT,
115 CARD_DEFAULT, CARD_DEFAULT };
116 static unsigned int pll[BTTV_MAX] = { USE_PLL, USE_PLL, USE_PLL, USE_PLL };
118 static int bttv_num; /* number of Bt848s in use */
119 static struct bttv bttvs[BTTV_MAX];
121 #define I2C_TIMING (0x7<<4)
122 #define I2C_DELAY 10
123 #define I2C_SET(CTRL,DATA) \
124 { btwrite((CTRL<<1)|(DATA), BT848_I2C); udelay(I2C_DELAY); }
125 #define I2C_GET() (btread(BT848_I2C)&1)
127 #define EEPROM_WRITE_DELAY 20000
128 #define BURSTOFFSET 76
132 /*******************************/
133 /* Memory management functions */
134 /*******************************/
136 /* convert virtual user memory address to physical address */
137 /* (virt_to_phys only works for kmalloced kernel memory) */
139 static inline unsigned long uvirt_to_phys(unsigned long adr)
141 pgd_t *pgd;
142 pmd_t *pmd;
143 pte_t *ptep, pte;
145 pgd = pgd_offset(current->mm, adr);
146 if (pgd_none(*pgd))
147 return 0;
148 pmd = pmd_offset(pgd, adr);
149 if (pmd_none(*pmd))
150 return 0;
151 ptep = pte_offset(pmd, adr/*&(~PGDIR_MASK)*/);
152 pte = *ptep;
153 if(pte_present(pte))
154 return
155 virt_to_phys((void *)(pte_page(pte)|(adr&(PAGE_SIZE-1))));
156 return 0;
159 static inline unsigned long uvirt_to_bus(unsigned long adr)
161 return virt_to_bus(phys_to_virt(uvirt_to_phys(adr)));
164 /* convert virtual kernel memory address to physical address */
165 /* (virt_to_phys only works for kmalloced kernel memory) */
167 static inline unsigned long kvirt_to_phys(unsigned long adr)
169 return uvirt_to_phys(VMALLOC_VMADDR(adr));
172 static inline unsigned long kvirt_to_bus(unsigned long adr)
174 return uvirt_to_bus(VMALLOC_VMADDR(adr));
177 static void * rvmalloc(unsigned long size)
179 void * mem;
180 unsigned long adr, page;
182 mem=vmalloc(size);
183 if (mem)
185 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
186 adr=(unsigned long) mem;
187 while (size > 0)
189 page = kvirt_to_phys(adr);
190 mem_map_reserve(MAP_NR(phys_to_virt(page)));
191 adr+=PAGE_SIZE;
192 size-=PAGE_SIZE;
195 return mem;
198 static void rvfree(void * mem, unsigned long size)
200 unsigned long adr, page;
202 if (mem)
204 adr=(unsigned long) mem;
205 while (size > 0)
207 page = kvirt_to_phys(adr);
208 mem_map_unreserve(MAP_NR(phys_to_virt(page)));
209 adr+=PAGE_SIZE;
210 size-=PAGE_SIZE;
212 vfree(mem);
219 * Create the giant waste of buffer space we need for now
220 * until we get DMA to user space sorted out (probably 2.3.x)
222 * We only create this as and when someone uses mmap
225 static int fbuffer_alloc(struct bttv *btv)
227 if(!btv->fbuffer)
228 btv->fbuffer=(unsigned char *) rvmalloc(2*BTTV_MAX_FBUF);
229 else
230 printk(KERN_ERR "bttv%d: Double alloc of fbuffer!\n",
231 btv->nr);
232 if(!btv->fbuffer)
233 return -ENOBUFS;
234 return 0;
238 /* ----------------------------------------------------------------------- */
239 /* I2C functions */
241 /* software I2C functions */
243 static void i2c_setlines(struct i2c_bus *bus,int ctrl,int data)
245 struct bttv *btv = (struct bttv*)bus->data;
246 btwrite((ctrl<<1)|data, BT848_I2C);
247 udelay(I2C_DELAY);
250 static int i2c_getdataline(struct i2c_bus *bus)
252 struct bttv *btv = (struct bttv*)bus->data;
253 return btread(BT848_I2C)&1;
256 /* hardware I2C functions */
258 /* read I2C */
259 static int I2CRead(struct i2c_bus *bus, unsigned char addr)
261 u32 i;
262 u32 stat;
263 struct bttv *btv = (struct bttv*)bus->data;
265 /* clear status bit ; BT848_INT_RACK is ro */
266 btwrite(BT848_INT_I2CDONE, BT848_INT_STAT);
268 btwrite(((addr & 0xff) << 24) | btv->i2c_command, BT848_I2C);
271 * Timeout for I2CRead is 1 second (this should be enough, really!)
273 for (i=1000; i; i--)
275 stat=btread(BT848_INT_STAT);
276 if (stat & BT848_INT_I2CDONE)
277 break;
278 mdelay(1);
281 if (!i)
283 printk(KERN_DEBUG "bttv%d: I2CRead timeout\n",
284 btv->nr);
285 return -1;
287 if (!(stat & BT848_INT_RACK))
288 return -2;
290 i=(btread(BT848_I2C)>>8)&0xff;
291 return i;
294 /* set both to write both bytes, reset it to write only b1 */
296 static int I2CWrite(struct i2c_bus *bus, unsigned char addr, unsigned char b1,
297 unsigned char b2, int both)
299 u32 i;
300 u32 data;
301 u32 stat;
302 struct bttv *btv = (struct bttv*)bus->data;
304 /* clear status bit; BT848_INT_RACK is ro */
305 btwrite(BT848_INT_I2CDONE, BT848_INT_STAT);
307 data=((addr & 0xff) << 24) | ((b1 & 0xff) << 16) | btv->i2c_command;
308 if (both)
310 data|=((b2 & 0xff) << 8);
311 data|=BT848_I2C_W3B;
314 btwrite(data, BT848_I2C);
316 for (i=0x1000; i; i--)
318 stat=btread(BT848_INT_STAT);
319 if (stat & BT848_INT_I2CDONE)
320 break;
321 mdelay(1);
324 if (!i)
326 printk(KERN_DEBUG "bttv%d: I2CWrite timeout\n",
327 btv->nr);
328 return -1;
330 if (!(stat & BT848_INT_RACK))
331 return -2;
333 return 0;
336 /* read EEPROM */
337 static void readee(struct i2c_bus *bus, unsigned char *eedata)
339 int i, k;
341 if (I2CWrite(bus, 0xa0, 0, -1, 0)<0)
343 printk(KERN_WARNING "bttv: readee error\n");
344 return;
347 for (i=0; i<256; i++)
349 k=I2CRead(bus, 0xa1);
350 if (k<0)
352 printk(KERN_WARNING "bttv: readee error\n");
353 break;
355 eedata[i]=k;
359 /* write EEPROM */
360 static void writeee(struct i2c_bus *bus, unsigned char *eedata)
362 int i;
364 for (i=0; i<256; i++)
366 if (I2CWrite(bus, 0xa0, i, eedata[i], 1)<0)
368 printk(KERN_WARNING "bttv: writeee error (%d)\n", i);
369 break;
371 udelay(EEPROM_WRITE_DELAY);
375 void attach_inform(struct i2c_bus *bus, int id)
377 struct bttv *btv = (struct bttv*)bus->data;
379 switch (id)
381 case I2C_DRIVERID_MSP3400:
382 btv->have_msp3400 = 1;
383 break;
384 case I2C_DRIVERID_TUNER:
385 btv->have_tuner = 1;
386 if (btv->tuner_type != -1)
387 i2c_control_device(&(btv->i2c),
388 I2C_DRIVERID_TUNER,
389 TUNER_SET_TYPE,&btv->tuner_type);
390 break;
394 void detach_inform(struct i2c_bus *bus, int id)
396 struct bttv *btv = (struct bttv*)bus->data;
398 switch (id)
400 case I2C_DRIVERID_MSP3400:
401 btv->have_msp3400 = 0;
402 break;
403 case I2C_DRIVERID_TUNER:
404 btv->have_tuner = 0;
405 break;
409 static struct i2c_bus bttv_i2c_bus_template =
411 "bt848",
412 I2C_BUSID_BT848,
413 NULL,
415 #if LINUX_VERSION_CODE >= 0x020100
416 SPIN_LOCK_UNLOCKED,
417 #endif
419 attach_inform,
420 detach_inform,
422 i2c_setlines,
423 i2c_getdataline,
424 I2CRead,
425 I2CWrite,
428 /* ----------------------------------------------------------------------- */
429 /* some hauppauge specific stuff */
431 static unsigned char eeprom_data[256];
432 static struct HAUPPAUGE_TUNER
434 int id;
435 char *name;
437 hauppauge_tuner[] =
439 { TUNER_ABSENT, "" },
440 { TUNER_ABSENT, "External" },
441 { TUNER_ABSENT, "Unspecified" },
442 { TUNER_ABSENT, "Philips FI1216" },
443 { TUNER_ABSENT, "Philips FI1216MF" },
444 { TUNER_PHILIPS_NTSC, "Philips FI1236" },
445 { TUNER_ABSENT, "Philips FI1246" },
446 { TUNER_ABSENT, "Philips FI1256" },
447 { TUNER_PHILIPS_PAL, "Philips FI1216 MK2" },
448 { TUNER_PHILIPS_SECAM, "Philips FI1216MF MK2" },
449 { TUNER_PHILIPS_NTSC, "Philips FI1236 MK2" },
450 { TUNER_PHILIPS_PAL_I, "Philips FI1246 MK2" },
451 { TUNER_ABSENT, "Philips FI1256 MK2" },
452 { TUNER_ABSENT, "Temic 4032FY5" },
453 { TUNER_TEMIC_PAL, "Temic 4002FH5" },
454 { TUNER_TEMIC_PAL_I, "Temic 4062FY5" },
455 { TUNER_ABSENT, "Philips FR1216 MK2" },
456 { TUNER_PHILIPS_SECAM, "Philips FR1216MF MK2" },
457 { TUNER_PHILIPS_NTSC, "Philips FR1236 MK2" },
458 { TUNER_PHILIPS_PAL_I, "Philips FR1246 MK2" },
459 { TUNER_ABSENT, "Philips FR1256 MK2" },
460 { TUNER_PHILIPS_PAL, "Philips FM1216" },
461 { TUNER_ABSENT, "Philips FM1216MF" },
462 { TUNER_PHILIPS_NTSC, "Philips FM1236" },
465 static void
466 hauppauge_eeprom(struct i2c_bus *bus)
468 struct bttv *btv = (struct bttv*)bus->data;
470 readee(bus, eeprom_data);
471 if (eeprom_data[9] < sizeof(hauppauge_tuner)/sizeof(struct HAUPPAUGE_TUNER))
473 btv->tuner_type = hauppauge_tuner[eeprom_data[9]].id;
474 printk("bttv%d: Hauppauge eeprom: tuner=%s (%d)\n",btv->nr,
475 hauppauge_tuner[eeprom_data[9]].name,btv->tuner_type);
479 static void
480 hauppauge_msp_reset(struct bttv *btv)
482 /* Reset the MSP on some Hauppauge cards */
483 /* Thanks to Kyösti Mälkki (kmalkki@cc.hut.fi)! */
484 /* Can this hurt cards without one? What about Miros with MSP? */
485 btaor(32, ~32, BT848_GPIO_OUT_EN);
486 btaor(0, ~32, BT848_GPIO_DATA);
487 udelay(2500);
488 btaor(32, ~32, BT848_GPIO_DATA);
489 /* btaor(0, ~32, BT848_GPIO_OUT_EN); */
492 /* ----------------------------------------------------------------------- */
495 struct tvcard
497 int video_inputs;
498 int audio_inputs;
499 int tuner;
500 int svhs;
501 u32 gpiomask;
502 u32 muxsel[8];
503 u32 audiomux[6]; /* Tuner, Radio, internal, external, mute, stereo */
504 u32 gpiomask2; /* GPIO MUX mask */
507 static struct tvcard tvcards[] =
509 /* default */
510 { 3, 1, 0, 2, 0, { 2, 3, 1, 1}, { 0, 0, 0, 0, 0}},
511 /* MIRO */
512 { 4, 1, 0, 2,15, { 2, 3, 1, 1}, { 2, 0, 0, 0,10}},
513 /* Hauppauge */
514 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4}},
515 /* STB */
516 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 4, 0, 2, 3, 1}},
517 /* Intel??? */
518 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4}},
519 /* Diamond DTV2000 */
520 { 3, 1, 0, 2, 3, { 2, 3, 1, 1}, { 0, 1, 0, 1, 3}},
521 /* AVerMedia TVPhone */
522 { 3, 1, 0, 3,15, { 2, 3, 1, 1}, {12, 0,11,11, 0}},
523 /* Matrix Vision MV-Delta */
524 { 5, 1, -1, 3, 0, { 2, 3, 1, 0, 0}},
525 /* Fly Video II */
526 { 3, 1, 0, 2, 0xc00, { 2, 3, 1, 1},
527 {0, 0xc00, 0x800, 0x400, 0xc00, 0}},
528 /* TurboTV */
529 { 3, 1, 0, 2, 3, { 2, 3, 1, 1}, { 1, 1, 2, 3, 0}},
530 /* Newer Hauppauge (bt878) */
531 { 3, 1, 0, 2, 7, { 2, 0, 1, 1}, { 0, 1, 2, 3, 4}},
532 /* MIRO PCTV pro */
533 { 3, 1, 0, 2, 65551, { 2, 3, 1, 1}, {1,65537, 0, 0,10}},
534 /* ADS Technologies Channel Surfer TV (and maybe TV+FM) */
535 { 3, 4, 0, 2, 15, { 2, 3, 1, 1}, { 13, 14, 11, 7, 0, 0}, 0},
536 /* AVerMedia TVCapture 98 */
537 { 3, 4, 0, 2, 15, { 2, 3, 1, 1}, { 13, 14, 11, 7, 0, 0}, 0},
538 /* Aimslab VHX */
539 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4}},
541 #define TVCARDS (sizeof(tvcards)/sizeof(tvcard))
543 static void audio(struct bttv *btv, int mode)
545 btaor(tvcards[btv->type].gpiomask, ~tvcards[btv->type].gpiomask,
546 BT848_GPIO_OUT_EN);
548 switch (mode)
550 case AUDIO_MUTE:
551 btv->audio|=AUDIO_MUTE;
552 break;
553 case AUDIO_UNMUTE:
554 btv->audio&=~AUDIO_MUTE;
555 mode=btv->audio;
556 break;
557 case AUDIO_OFF:
558 mode=AUDIO_OFF;
559 break;
560 case AUDIO_ON:
561 mode=btv->audio;
562 break;
563 default:
564 btv->audio&=AUDIO_MUTE;
565 btv->audio|=mode;
566 break;
568 /* if audio mute or not in H-lock, turn audio off */
569 if ((btv->audio&AUDIO_MUTE)
570 #if 0
572 (!btv->radio && !(btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC))
573 #endif
575 mode=AUDIO_OFF;
576 if ((mode == 0) && (btv->radio))
577 mode = 1;
578 btaor(tvcards[btv->type].audiomux[mode],
579 ~tvcards[btv->type].gpiomask, BT848_GPIO_DATA);
583 extern inline void bt848_dma(struct bttv *btv, uint state)
585 if (state)
586 btor(3, BT848_GPIO_DMA_CTL);
587 else
588 btand(~3, BT848_GPIO_DMA_CTL);
592 static void bt848_cap(struct bttv *btv, uint state)
594 if (state)
596 btv->cap|=3;
597 bt848_set_risc_jmps(btv);
599 else
601 btv->cap&=~3;
602 bt848_set_risc_jmps(btv);
607 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC*/
609 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
610 PLL_X = Reference pre-divider (0=1, 1=2)
611 PLL_C = Post divider (0=6, 1=4)
612 PLL_I = Integer input
613 PLL_F = Fractional input
615 F_input = 28.636363 MHz:
616 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
619 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
621 unsigned char fl, fh, fi;
623 /* prevent overflows */
624 fin/=4;
625 fout/=4;
627 fout*=12;
628 fi=fout/fin;
630 fout=(fout%fin)*256;
631 fh=fout/fin;
633 fout=(fout%fin)*256;
634 fl=fout/fin;
636 /*printk("0x%02x 0x%02x 0x%02x\n", fi, fh, fl);*/
637 btwrite(fl, BT848_PLL_F_LO);
638 btwrite(fh, BT848_PLL_F_HI);
639 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
642 static int set_pll(struct bttv *btv)
644 int i;
645 unsigned long tv;
647 if (!btv->pll.pll_crystal)
648 return 0;
650 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
651 /* no PLL needed */
652 if (btv->pll.pll_current == 0) {
653 /* printk ("bttv%d: PLL: is off\n",btv->nr); */
654 return 0;
656 printk ("bttv%d: PLL: switching off\n",btv->nr);
657 btwrite(0x00,BT848_TGCTRL);
658 btwrite(0x00,BT848_PLL_XCI);
659 btv->pll.pll_current = 0;
660 return 0;
663 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
664 /* printk("bttv%d: PLL: no change required\n",btv->nr); */
665 return 1;
668 printk("bttv%d: PLL: %d => %d ... ",btv->nr,
669 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
671 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
673 /* Let other people run while the PLL stabilizes */
674 tv=jiffies+HZ/10; /* .1 seconds */
677 schedule();
679 while(time_before(jiffies,tv));
681 for (i=0; i<10; i++)
683 if ((btread(BT848_DSTATUS)&BT848_DSTATUS_PLOCK))
684 btwrite(0,BT848_DSTATUS);
685 else
687 btwrite(0x08,BT848_TGCTRL);
688 btv->pll.pll_current = btv->pll.pll_ofreq;
689 printk("ok\n");
690 return 1;
692 mdelay(10);
694 btv->pll.pll_current = 0;
695 printk("oops\n");
696 return -1;
699 static void bt848_muxsel(struct bttv *btv, unsigned int input)
701 btaor(tvcards[btv->type].gpiomask2,~tvcards[btv->type].gpiomask2,
702 BT848_GPIO_OUT_EN);
704 /* This seems to get rid of some synchronization problems */
705 btand(~(3<<5), BT848_IFORM);
706 mdelay(10);
709 input %= tvcards[btv->type].video_inputs;
710 if (input==tvcards[btv->type].svhs)
712 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
713 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
715 else
717 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
718 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
720 btaor((tvcards[btv->type].muxsel[input&7]&3)<<5, ~(3<<5), BT848_IFORM);
721 audio(btv, (input!=tvcards[btv->type].tuner) ?
722 AUDIO_EXTERN : AUDIO_TUNER);
723 btaor(tvcards[btv->type].muxsel[input]>>4,
724 ~tvcards[btv->type].gpiomask2, BT848_GPIO_DATA);
728 * Set the registers for the size we have specified. Don't bother
729 * trying to understand this without the BT848 manual in front of
730 * you [AC].
732 * PS: The manual is free for download in .pdf format from
733 * www.brooktree.com - nicely done those folks.
736 struct tvnorm
738 u32 Fsc;
739 u16 swidth, sheight; /* scaled standard width, height */
740 u16 totalwidth;
741 u8 adelay, bdelay, iform;
742 u32 scaledtwidth;
743 u16 hdelayx1, hactivex1;
744 u16 vdelay;
745 u8 vbipack;
748 static struct tvnorm tvnorms[] = {
749 /* PAL-BDGHI */
750 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
751 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
752 #ifdef VIDEODAT
753 { 35468950,
754 924, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
755 1135, 186, 924, 0x20, 255},
756 #else
757 { 35468950,
758 924, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
759 1135, 186, 924, 0x20, 255},
760 #endif
762 { 35468950,
763 768, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
764 944, 186, 922, 0x20, 255},
766 /* NTSC */
767 { 28636363,
768 768, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC|BT848_IFORM_XT0),
769 910, 128, 910, 0x1a, 144},
771 { 28636363,
772 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC|BT848_IFORM_XT0),
773 780, 122, 754, 0x1a, 144},
775 #if 0
776 /* SECAM EAST */
777 { 35468950,
778 768, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM|BT848_IFORM_XT1),
779 944, 186, 922, 0x20, 255},
780 #else
781 /* SECAM L */
782 { 35468950,
783 924, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM|BT848_IFORM_XT1),
784 1135, 186, 922, 0x20, 255},
785 #endif
786 /* PAL-NC */
787 { 28636363,
788 640, 576, 910, 0x68, 0x5d, (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
789 780, 130, 734, 0x1a, 144},
790 /* PAL-M */
791 { 28636363,
792 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
793 780, 135, 754, 0x1a, 144},
794 /* PAL-N */
795 { 35468950,
796 768, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
797 944, 186, 922, 0x20, 144},
798 /* NTSC-Japan */
799 { 28636363,
800 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
801 780, 135, 754, 0x16, 144},
803 #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
804 #define VBI_SPL 2044
806 /* RISC command to write one VBI data line */
807 #define VBI_RISC BT848_RISC_WRITE|VBI_SPL|BT848_RISC_EOL|BT848_RISC_SOL
809 static void make_vbitab(struct bttv *btv)
811 int i;
812 unsigned int *po=(unsigned int *) btv->vbi_odd;
813 unsigned int *pe=(unsigned int *) btv->vbi_even;
815 DEBUG(printk(KERN_DEBUG "vbiodd: 0x%08x\n",(int)btv->vbi_odd));
816 DEBUG(printk(KERN_DEBUG "vbievn: 0x%08x\n",(int)btv->vbi_even));
817 DEBUG(printk(KERN_DEBUG "po: 0x%08x\n",(int)po));
818 DEBUG(printk(KERN_DEBUG "pe: 0x%08x\n",(int)pe));
820 *(po++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(po++)=0;
821 for (i=0; i<16; i++)
823 *(po++)=VBI_RISC;
824 *(po++)=kvirt_to_bus((unsigned long)btv->vbibuf+i*2048);
826 *(po++)=BT848_RISC_JUMP;
827 *(po++)=virt_to_bus(btv->risc_jmp+4);
829 *(pe++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(pe++)=0;
830 for (i=16; i<32; i++)
832 *(pe++)=VBI_RISC;
833 *(pe++)=kvirt_to_bus((unsigned long)btv->vbibuf+i*2048);
835 *(pe++)=BT848_RISC_JUMP|BT848_RISC_IRQ|(0x01<<16);
836 *(pe++)=virt_to_bus(btv->risc_jmp+10);
837 DEBUG(printk(KERN_DEBUG "po: 0x%08x\n",(int)po));
838 DEBUG(printk(KERN_DEBUG "pe: 0x%08x\n",(int)pe));
841 int fmtbppx2[16] = {
842 8, 6, 4, 4, 4, 3, 2, 2, 4, 3, 0, 0, 0, 0, 2, 0
845 int palette2fmt[] = {
847 BT848_COLOR_FMT_Y8,
848 BT848_COLOR_FMT_RGB8,
849 BT848_COLOR_FMT_RGB16,
850 BT848_COLOR_FMT_RGB24,
851 BT848_COLOR_FMT_RGB32,
852 BT848_COLOR_FMT_RGB15,
853 BT848_COLOR_FMT_YUY2,
854 BT848_COLOR_FMT_BtYUV,
858 BT848_COLOR_FMT_RAW,
859 BT848_COLOR_FMT_YCrCb422,
860 BT848_COLOR_FMT_YCrCb411,
861 BT848_COLOR_FMT_YCrCb422,
862 BT848_COLOR_FMT_YCrCb411,
864 #define PALETTEFMT_MAX (sizeof(palette2fmt)/sizeof(int))
866 static int make_rawrisctab(struct bttv *btv, unsigned int *ro,
867 unsigned int *re, unsigned int *vbuf)
869 unsigned long line;
870 unsigned long bpl=1024; /* bytes per line */
871 unsigned long vadr=(unsigned long) vbuf;
873 *(ro++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(ro++)=0;
874 *(re++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(re++)=0;
876 /* In PAL 650 blocks of 256 DWORDs are sampled, but only if VDELAY
877 is 2 and without separate VBI grabbing.
878 We'll have to handle this inside the IRQ handler ... */
880 for (line=0; line < 640; line++)
882 *(ro++)=BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL;
883 *(ro++)=kvirt_to_bus(vadr);
884 *(re++)=BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL;
885 *(re++)=kvirt_to_bus(vadr+BTTV_MAX_FBUF/2);
886 vadr+=bpl;
889 *(ro++)=BT848_RISC_JUMP;
890 *(ro++)=btv->bus_vbi_even;
891 *(re++)=BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16);
892 *(re++)=btv->bus_vbi_odd;
894 return 0;
898 static int make_prisctab(struct bttv *btv, unsigned int *ro,
899 unsigned int *re,
900 unsigned int *vbuf, unsigned short width,
901 unsigned short height, unsigned short fmt)
903 unsigned long line, lmask;
904 unsigned long bl, blcr, blcb, rcmd;
905 unsigned long todo;
906 unsigned int **rp;
907 int inter;
908 unsigned long cbadr, cradr;
909 unsigned long vadr=(unsigned long) vbuf;
910 int shift, csize;
913 switch(fmt)
915 case VIDEO_PALETTE_YUV422P:
916 csize=(width*height)>>1;
917 shift=1;
918 lmask=0;
919 break;
921 case VIDEO_PALETTE_YUV411P:
922 csize=(width*height)>>2;
923 shift=2;
924 lmask=0;
925 break;
927 case VIDEO_PALETTE_YUV420P:
928 csize=(width*height)>>2;
929 shift=1;
930 lmask=1;
931 break;
933 case VIDEO_PALETTE_YUV410P:
934 csize=(width*height)>>4;
935 shift=2;
936 lmask=3;
937 break;
939 default:
940 return -1;
942 cbadr=vadr+(width*height);
943 cradr=cbadr+csize;
944 inter = (height>btv->win.cropheight/2) ? 1 : 0;
946 *(ro++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3; *(ro++)=0;
947 *(re++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3; *(re++)=0;
949 for (line=0; line < (height<<(1^inter)); line++)
951 if(line==height)
953 vadr+=csize<<1;
954 cbadr=vadr+(width*height);
955 cradr=cbadr+csize;
957 if (inter)
958 rp= (line&1) ? &re : &ro;
959 else
960 rp= (line>=height) ? &re : &ro;
963 if(line&lmask)
964 rcmd=BT848_RISC_WRITE1S23|BT848_RISC_SOL;
965 else
966 rcmd=BT848_RISC_WRITE123|BT848_RISC_SOL;
968 todo=width;
969 while(todo)
971 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
972 blcr=(PAGE_SIZE-((PAGE_SIZE-1)&cradr))<<shift;
973 blcb=(PAGE_SIZE-((PAGE_SIZE-1)&cbadr))<<shift;
974 bl=(blcr<bl) ? blcr : bl;
975 bl=(blcb<bl) ? blcb : bl;
976 bl=(bl>todo) ? todo : bl;
977 blcr=bl>>shift;
978 blcb=blcr;
979 /* bl now containts the longest row that can be written */
980 todo-=bl;
981 if(!todo) rcmd|=BT848_RISC_EOL; /* if this is the last EOL */
983 *((*rp)++)=rcmd|bl;
984 *((*rp)++)=blcb|(blcr<<16);
985 *((*rp)++)=kvirt_to_bus(vadr);
986 vadr+=bl;
987 if((rcmd&(15<<28))==BT848_RISC_WRITE123)
989 *((*rp)++)=kvirt_to_bus(cbadr);
990 cbadr+=blcb;
991 *((*rp)++)=kvirt_to_bus(cradr);
992 cradr+=blcr;
995 rcmd&=~BT848_RISC_SOL; /* only the first has SOL */
999 *(ro++)=BT848_RISC_JUMP;
1000 *(ro++)=btv->bus_vbi_even;
1001 *(re++)=BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16);
1002 *(re++)=btv->bus_vbi_odd;
1004 return 0;
1007 static int make_vrisctab(struct bttv *btv, unsigned int *ro,
1008 unsigned int *re,
1009 unsigned int *vbuf, unsigned short width,
1010 unsigned short height, unsigned short palette)
1012 unsigned long line;
1013 unsigned long bpl; /* bytes per line */
1014 unsigned long bl;
1015 unsigned long todo;
1016 unsigned int **rp;
1017 int inter;
1018 unsigned long vadr=(unsigned long) vbuf;
1020 if (palette==VIDEO_PALETTE_RAW)
1021 return make_rawrisctab(btv, ro, re, vbuf);
1022 if (palette>=VIDEO_PALETTE_PLANAR)
1023 return make_prisctab(btv, ro, re, vbuf, width, height, palette);
1026 inter = (height>btv->win.cropheight/2) ? 1 : 0;
1027 bpl=width*fmtbppx2[palette2fmt[palette]&0xf]/2;
1029 *(ro++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(ro++)=0;
1030 *(re++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(re++)=0;
1032 for (line=0; line < (height<<(1^inter)); line++)
1034 if (inter)
1035 rp= (line&1) ? &re : &ro;
1036 else
1037 rp= (line>=height) ? &re : &ro;
1039 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
1040 if (bpl<=bl)
1042 *((*rp)++)=BT848_RISC_WRITE|BT848_RISC_SOL|
1043 BT848_RISC_EOL|bpl;
1044 *((*rp)++)=kvirt_to_bus(vadr);
1045 vadr+=bpl;
1047 else
1049 todo=bpl;
1050 *((*rp)++)=BT848_RISC_WRITE|BT848_RISC_SOL|bl;
1051 *((*rp)++)=kvirt_to_bus(vadr);
1052 vadr+=bl;
1053 todo-=bl;
1054 while (todo>PAGE_SIZE)
1056 *((*rp)++)=BT848_RISC_WRITE|PAGE_SIZE;
1057 *((*rp)++)=kvirt_to_bus(vadr);
1058 vadr+=PAGE_SIZE;
1059 todo-=PAGE_SIZE;
1061 *((*rp)++)=BT848_RISC_WRITE|BT848_RISC_EOL|todo;
1062 *((*rp)++)=kvirt_to_bus(vadr);
1063 vadr+=todo;
1067 *(ro++)=BT848_RISC_JUMP;
1068 *(ro++)=btv->bus_vbi_even;
1069 *(re++)=BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16);
1070 *(re++)=btv->bus_vbi_odd;
1072 return 0;
1075 static unsigned char lmaskt[8] =
1076 { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
1077 static unsigned char rmaskt[8] =
1078 { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1080 static void clip_draw_rectangle(unsigned char *clipmap, int x, int y, int w, int h)
1082 unsigned char lmask, rmask, *p;
1083 int W, l, r;
1084 int i;
1085 /* bitmap is fixed width, 128 bytes (1024 pixels represented) */
1086 if (x<0)
1088 w+=x;
1089 x=0;
1091 if (y<0)
1093 h+=y;
1094 y=0;
1096 if (w < 0 || h < 0) /* catch bad clips */
1097 return;
1098 /* out of range data should just fall through */
1099 if (y+h>=625)
1100 h=625-y;
1101 if (x+w>=1024)
1102 w=1024-x;
1104 l=x>>3;
1105 r=(x+w)>>3;
1106 W=r-l-1;
1107 lmask=lmaskt[x&7];
1108 rmask=rmaskt[(x+w)&7];
1109 p=clipmap+128*y+l;
1111 if (W>0)
1113 for (i=0; i<h; i++, p+=128)
1115 *p|=lmask;
1116 memset(p+1, 0xff, W);
1117 p[W+1]|=rmask;
1119 } else if (!W) {
1120 for (i=0; i<h; i++, p+=128)
1122 p[0]|=lmask;
1123 p[1]|=rmask;
1125 } else {
1126 for (i=0; i<h; i++, p+=128)
1127 p[0]|=lmask&rmask;
1133 static void make_clip_tab(struct bttv *btv, struct video_clip *cr, int ncr)
1135 int i, line, x, y, bpl, width, height, inter;
1136 unsigned int bpp, dx, sx, **rp, *ro, *re, flags, len;
1137 unsigned long adr;
1138 unsigned char *clipmap, cbit, lastbit, outofmem;
1140 inter=(btv->win.interlace&1)^1;
1141 bpp=btv->win.bpp;
1142 if (bpp==15) /* handle 15bpp as 16bpp in calculations */
1143 bpp++;
1144 bpl=btv->win.bpl;
1145 ro=btv->risc_odd;
1146 re=btv->risc_even;
1147 if((width=btv->win.width)>1023)
1148 width = 1023; /* sanity check */
1149 if((height=btv->win.height)>625)
1150 height = 625; /* sanity check */
1151 adr=btv->win.vidadr+btv->win.x*bpp+btv->win.y*bpl;
1152 if ((clipmap=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL) {
1153 /* can't clip, don't generate any risc code */
1154 *(ro++)=BT848_RISC_JUMP;
1155 *(ro++)=btv->bus_vbi_even;
1156 *(re++)=BT848_RISC_JUMP;
1157 *(re++)=btv->bus_vbi_odd;
1159 if (ncr < 0) { /* bitmap was pased */
1160 memcpy(clipmap, (unsigned char *)cr, VIDEO_CLIPMAP_SIZE);
1161 } else { /* convert rectangular clips to a bitmap */
1162 memset(clipmap, 0, VIDEO_CLIPMAP_SIZE); /* clear map */
1163 for (i=0; i<ncr; i++)
1164 clip_draw_rectangle(clipmap, cr[i].x, cr[i].y,
1165 cr[i].width, cr[i].height);
1167 /* clip against viewing window AND screen
1168 so we do not have to rely on the user program
1170 clip_draw_rectangle(clipmap,(btv->win.x+width>btv->win.swidth) ?
1171 (btv->win.swidth-btv->win.x) : width, 0, 1024, 768);
1172 clip_draw_rectangle(clipmap,0,(btv->win.y+height>btv->win.sheight) ?
1173 (btv->win.sheight-btv->win.y) : height,1024,768);
1174 if (btv->win.x<0)
1175 clip_draw_rectangle(clipmap, 0, 0, -(btv->win.x), 768);
1176 if (btv->win.y<0)
1177 clip_draw_rectangle(clipmap, 0, 0, 1024, -(btv->win.y));
1179 *(ro++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(ro++)=0;
1180 *(re++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(re++)=0;
1182 /* translate bitmap to risc code */
1183 for (line=outofmem=0; line < (height<<inter) && !outofmem; line++)
1185 y = line>>inter;
1186 rp= (line&1) ? &re : &ro;
1187 lastbit=(clipmap[y<<7]&1);
1188 for(x=dx=1,sx=0; x<=width && !outofmem; x++) {
1189 cbit = (clipmap[(y<<7)+(x>>3)] & (1<<(x&7)));
1190 if (x < width && !lastbit == !cbit)
1191 dx++;
1192 else { /* generate the dma controller code */
1193 len = dx * bpp;
1194 flags = ((bpp==4) ? BT848_RISC_BYTE3 : 0);
1195 flags |= ((!sx) ? BT848_RISC_SOL : 0);
1196 flags |= ((sx + dx == width) ? BT848_RISC_EOL : 0);
1197 if (!lastbit) {
1198 *((*rp)++)=BT848_RISC_WRITE|flags|len;
1199 *((*rp)++)=adr + bpp * sx;
1200 } else
1201 *((*rp)++)=BT848_RISC_SKIP|flags|len;
1202 lastbit=cbit;
1203 sx += dx;
1204 dx = 1;
1205 if (ro - btv->risc_odd > RISCMEM_LEN/2 - 16)
1206 outofmem++;
1207 if (re - btv->risc_even > RISCMEM_LEN/2 - 16)
1208 outofmem++;
1211 if ((!inter)||(line&1))
1212 adr+=bpl;
1214 vfree(clipmap);
1215 /* outofmem flag relies on the following code to discard extra data */
1216 *(ro++)=BT848_RISC_JUMP;
1217 *(ro++)=btv->bus_vbi_even;
1218 *(re++)=BT848_RISC_JUMP;
1219 *(re++)=btv->bus_vbi_odd;
1222 /* set geometry for even/odd frames
1223 just if you are wondering:
1224 handling of even and odd frames will be separated, e.g. for grabbing
1225 the even ones as RGB into videomem and the others as YUV in main memory for
1226 compressing and sending to the video conferencing partner.
1229 static inline void bt848_set_eogeo(struct bttv *btv, int odd, u8 vtc,
1230 u16 hscale, u16 vscale,
1231 u16 hactive, u16 vactive,
1232 u16 hdelay, u16 vdelay,
1233 u8 crop)
1235 int off = odd ? 0x80 : 0x00;
1237 btwrite(vtc, BT848_E_VTC+off);
1238 btwrite(hscale>>8, BT848_E_HSCALE_HI+off);
1239 btwrite(hscale&0xff, BT848_E_HSCALE_LO+off);
1240 btaor((vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
1241 btwrite(vscale&0xff, BT848_E_VSCALE_LO+off);
1242 btwrite(hactive&0xff, BT848_E_HACTIVE_LO+off);
1243 btwrite(hdelay&0xff, BT848_E_HDELAY_LO+off);
1244 btwrite(vactive&0xff, BT848_E_VACTIVE_LO+off);
1245 btwrite(vdelay&0xff, BT848_E_VDELAY_LO+off);
1246 btwrite(crop, BT848_E_CROP+off);
1250 static void bt848_set_geo(struct bttv *btv, u16 width, u16 height, u16 fmt)
1252 u16 vscale, hscale;
1253 u32 xsf, sr;
1254 u16 hdelay, vdelay;
1255 u16 hactive, vactive;
1256 u16 inter;
1257 u8 crop, vtc;
1258 struct tvnorm *tvn;
1260 if (!width || !height)
1261 return;
1263 tvn=&tvnorms[btv->win.norm];
1265 btv->win.cropheight=tvn->sheight;
1266 btv->win.cropwidth=tvn->swidth;
1269 if (btv->win.cropwidth>tvn->cropwidth)
1270 btv->win.cropwidth=tvn->cropwidth;
1271 if (btv->win.cropheight>tvn->cropheight)
1272 btv->win.cropheight=tvn->cropheight;
1274 if (width>btv->win.cropwidth)
1275 width=btv->win.cropwidth;
1276 if (height>btv->win.cropheight)
1277 height=btv->win.cropheight;
1279 btwrite(tvn->adelay, BT848_ADELAY);
1280 btwrite(tvn->bdelay, BT848_BDELAY);
1281 btaor(tvn->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH), BT848_IFORM);
1282 btwrite(tvn->vbipack, BT848_VBI_PACK_SIZE);
1283 btwrite(1, BT848_VBI_PACK_DEL);
1285 btv->pll.pll_ofreq = tvn->Fsc;
1286 set_pll(btv);
1288 btwrite(fmt, BT848_COLOR_FMT);
1289 hactive=width;
1291 vtc=0;
1292 /* Some people say interpolation looks bad ... */
1293 /* vtc = (hactive < 193) ? 2 : ((hactive < 385) ? 1 : 0); */
1295 btv->win.interlace = (height>btv->win.cropheight/2) ? 1 : 0;
1296 inter=(btv->win.interlace&1)^1;
1297 vdelay=btv->win.cropy+tvn->vdelay;
1299 xsf = (hactive*tvn->scaledtwidth)/btv->win.cropwidth;
1300 hscale = ((tvn->totalwidth*4096UL)/xsf-4096);
1302 hdelay=tvn->hdelayx1+btv->win.cropx;
1303 hdelay=(hdelay*hactive)/btv->win.cropwidth;
1304 hdelay&=0x3fe;
1306 sr=((btv->win.cropheight>>inter)*512)/height-512;
1307 vscale=(0x10000UL-sr)&0x1fff;
1308 vactive=btv->win.cropheight;
1309 crop=((hactive>>8)&0x03)|((hdelay>>6)&0x0c)|
1310 ((vactive>>4)&0x30)|((vdelay>>2)&0xc0);
1311 vscale|= btv->win.interlace ? (BT848_VSCALE_INT<<8) : 0;
1313 bt848_set_eogeo(btv, 0, vtc, hscale, vscale, hactive, vactive,
1314 hdelay, vdelay, crop);
1315 bt848_set_eogeo(btv, 1, vtc, hscale, vscale, hactive, vactive,
1316 hdelay, vdelay, crop);
1320 int bpp2fmt[4] = {
1321 BT848_COLOR_FMT_RGB8, BT848_COLOR_FMT_RGB16,
1322 BT848_COLOR_FMT_RGB24, BT848_COLOR_FMT_RGB32
1325 static void bt848_set_winsize(struct bttv *btv)
1327 unsigned short format;
1329 btv->win.color_fmt = format =
1330 (btv->win.depth==15) ? BT848_COLOR_FMT_RGB15 :
1331 bpp2fmt[(btv->win.bpp-1)&3];
1333 /* RGB8 seems to be a 9x5x5 GRB color cube starting at
1334 * color 16. Why the h... can't they even mention this in the
1335 * data sheet? [AC - because it's a standard format so I guess
1336 * it never occurred to them]
1337 * Enable dithering in this mode.
1340 if (format==BT848_COLOR_FMT_RGB8)
1341 btand(~BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1342 else
1343 btor(BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1345 bt848_set_geo(btv, btv->win.width, btv->win.height, format);
1349 * Set TSA5522 synthesizer frequency in 1/16 Mhz steps
1352 static void set_freq(struct bttv *btv, unsigned short freq)
1354 int fixme = freq; /* XXX */
1356 /* mute */
1357 if (btv->have_msp3400)
1358 i2c_control_device(&(btv->i2c),I2C_DRIVERID_MSP3400,
1359 MSP_SWITCH_MUTE,0);
1361 /* switch channel */
1362 if (btv->have_tuner) {
1363 if (btv->radio) {
1364 i2c_control_device(&(btv->i2c), I2C_DRIVERID_TUNER,
1365 TUNER_SET_RADIOFREQ,&fixme);
1366 } else {
1367 i2c_control_device(&(btv->i2c), I2C_DRIVERID_TUNER,
1368 TUNER_SET_TVFREQ,&fixme);
1372 if (btv->have_msp3400) {
1373 if (btv->radio) {
1374 i2c_control_device(&(btv->i2c),I2C_DRIVERID_MSP3400,
1375 MSP_SET_RADIO,0);
1376 } else {
1377 i2c_control_device(&(btv->i2c),I2C_DRIVERID_MSP3400,
1378 MSP_SET_TVNORM,&(btv->win.norm));
1379 i2c_control_device(&(btv->i2c),I2C_DRIVERID_MSP3400,
1380 MSP_NEWCHANNEL,0);
1386 * Grab into virtual memory.
1387 * Currently only does double buffering. Do we need more?
1390 static int vgrab(struct bttv *btv, struct video_mmap *mp)
1392 unsigned int *ro, *re;
1393 unsigned int *vbuf;
1395 if(btv->fbuffer==NULL)
1397 if(fbuffer_alloc(btv))
1398 return -ENOBUFS;
1400 if(btv->grabbing >= MAX_GBUFFERS)
1401 return -ENOBUFS;
1404 * No grabbing past the end of the buffer!
1407 if(mp->frame>1 || mp->frame <0)
1408 return -EINVAL;
1410 if(mp->height <0 || mp->width <0)
1411 return -EINVAL;
1413 /* This doesn´t work like this for NTSC anyway.
1414 So, better check the total image size ...
1417 if(mp->height>576 || mp->width>768+BURSTOFFSET)
1418 return -EINVAL;
1420 if (mp->format >= PALETTEFMT_MAX)
1421 return -EINVAL;
1422 if (mp->height*mp->width*fmtbppx2[palette2fmt[mp->format]&0x0f]/2
1423 > BTTV_MAX_FBUF)
1424 return -EINVAL;
1425 if(-1 == palette2fmt[mp->format])
1426 return -EINVAL;
1429 * FIXME: Check the format of the grab here. This is probably
1430 * also less restrictive than the normal overlay grabs. Stuff
1431 * like QCIF has meaning as a capture.
1435 * Ok load up the BT848
1438 vbuf=(unsigned int *)(btv->fbuffer+BTTV_MAX_FBUF*mp->frame);
1439 /* if (!(btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC))
1440 return -EAGAIN;*/
1441 ro=btv->grisc+(((btv->grabcount++)&1) ? 4096 :0);
1442 re=ro+2048;
1443 make_vrisctab(btv, ro, re, vbuf, mp->width, mp->height, mp->format);
1444 /* bt848_set_risc_jmps(btv); */
1445 btv->frame_stat[mp->frame] = GBUFFER_GRABBING;
1446 if (btv->grabbing) {
1447 btv->gfmt_next=palette2fmt[mp->format];
1448 btv->gwidth_next=mp->width;
1449 btv->gheight_next=mp->height;
1450 btv->gro_next=virt_to_bus(ro);
1451 btv->gre_next=virt_to_bus(re);
1452 btv->grf_next=mp->frame;
1453 } else {
1454 btv->gfmt=palette2fmt[mp->format];
1455 btv->gwidth=mp->width;
1456 btv->gheight=mp->height;
1457 btv->gro=virt_to_bus(ro);
1458 btv->gre=virt_to_bus(re);
1459 btv->grf=mp->frame;
1461 if (!(btv->grabbing++)) {
1462 if(mp->format>=VIDEO_PALETTE_COMPONENT) {
1463 btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI);
1464 btor(BT848_VSCALE_COMB, BT848_O_VSCALE_HI);
1466 btv->risc_jmp[12]=BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ;
1468 btor(3, BT848_CAP_CTL);
1469 btor(3, BT848_GPIO_DMA_CTL);
1470 /* interruptible_sleep_on(&btv->capq); */
1471 return 0;
1474 static long bttv_write(struct video_device *v, const char *buf, unsigned long count, int nonblock)
1476 return -EINVAL;
1479 static long bttv_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
1481 struct bttv *btv= (struct bttv *)v;
1482 int q,todo;
1483 /* BROKEN: RETURNS VBI WHEN IT SHOULD RETURN GRABBED VIDEO FRAME */
1484 todo=count;
1485 while (todo && todo>(q=VBIBUF_SIZE-btv->vbip))
1487 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, q))
1488 return -EFAULT;
1489 todo-=q;
1490 buf+=q;
1492 cli();
1493 if (todo && q==VBIBUF_SIZE-btv->vbip)
1495 if(nonblock)
1497 sti();
1498 if(count==todo)
1499 return -EWOULDBLOCK;
1500 return count-todo;
1502 interruptible_sleep_on(&btv->vbiq);
1503 sti();
1504 if(signal_pending(current))
1506 if(todo==count)
1507 return -EINTR;
1508 else
1509 return count-todo;
1513 if (todo)
1515 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, todo))
1516 return -EFAULT;
1517 btv->vbip+=todo;
1519 return count;
1523 * Open a bttv card. Right now the flags stuff is just playing
1526 static int bttv_open(struct video_device *dev, int flags)
1528 struct bttv *btv = (struct bttv *)dev;
1529 int users, i;
1531 if (btv->user)
1532 return -EBUSY;
1533 audio(btv, AUDIO_UNMUTE);
1534 for (i=users=0; i<bttv_num; i++)
1535 users+=bttvs[i].user;
1536 if (users==1)
1537 find_vga();
1538 btv->fbuffer=NULL;
1539 if (!btv->fbuffer)
1540 btv->fbuffer=(unsigned char *) rvmalloc(2*BTTV_MAX_FBUF);
1541 if (!btv->fbuffer)
1542 return -EINVAL;
1543 btv->grabbing = 0;
1544 btv->grab = 0;
1545 btv->lastgrab = 0;
1546 for (i = 0; i < MAX_GBUFFERS; i++)
1547 btv->frame_stat[i] = GBUFFER_UNUSED;
1549 btv->user++;
1550 MOD_INC_USE_COUNT;
1551 return 0;
1554 static void bttv_close(struct video_device *dev)
1556 struct bttv *btv=(struct bttv *)dev;
1558 btv->user--;
1559 audio(btv, AUDIO_INTERN);
1560 btv->cap&=~3;
1561 bt848_set_risc_jmps(btv);
1563 if(btv->fbuffer)
1564 rvfree((void *) btv->fbuffer, 2*BTTV_MAX_FBUF);
1565 btv->fbuffer=0;
1566 MOD_DEC_USE_COUNT;
1570 /***********************************/
1571 /* ioctls and supporting functions */
1572 /***********************************/
1574 extern inline void bt848_bright(struct bttv *btv, uint bright)
1576 btwrite(bright&0xff, BT848_BRIGHT);
1579 extern inline void bt848_hue(struct bttv *btv, uint hue)
1581 btwrite(hue&0xff, BT848_HUE);
1584 extern inline void bt848_contrast(struct bttv *btv, uint cont)
1586 unsigned int conthi;
1588 conthi=(cont>>6)&4;
1589 btwrite(cont&0xff, BT848_CONTRAST_LO);
1590 btaor(conthi, ~4, BT848_E_CONTROL);
1591 btaor(conthi, ~4, BT848_O_CONTROL);
1594 extern inline void bt848_sat_u(struct bttv *btv, unsigned long data)
1596 u32 datahi;
1598 datahi=(data>>7)&2;
1599 btwrite(data&0xff, BT848_SAT_U_LO);
1600 btaor(datahi, ~2, BT848_E_CONTROL);
1601 btaor(datahi, ~2, BT848_O_CONTROL);
1604 static inline void bt848_sat_v(struct bttv *btv, unsigned long data)
1606 u32 datahi;
1608 datahi=(data>>8)&1;
1609 btwrite(data&0xff, BT848_SAT_V_LO);
1610 btaor(datahi, ~1, BT848_E_CONTROL);
1611 btaor(datahi, ~1, BT848_O_CONTROL);
1615 * ioctl routine
1619 static int bttv_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
1621 unsigned char eedata[256];
1622 struct bttv *btv=(struct bttv *)dev;
1623 int i;
1625 switch (cmd)
1627 case VIDIOCGCAP:
1629 struct video_capability b;
1630 strcpy(b.name,btv->video_dev.name);
1631 b.type = VID_TYPE_CAPTURE|
1632 VID_TYPE_TUNER|
1633 VID_TYPE_TELETEXT|
1634 VID_TYPE_OVERLAY|
1635 VID_TYPE_CLIPPING|
1636 VID_TYPE_FRAMERAM|
1637 VID_TYPE_SCALES;
1638 b.channels = tvcards[btv->type].video_inputs;
1639 b.audios = tvcards[btv->type].audio_inputs;
1640 b.maxwidth = tvnorms[btv->win.norm].swidth;
1641 b.maxheight = tvnorms[btv->win.norm].sheight;
1642 b.minwidth = 32;
1643 b.minheight = 32;
1644 if(copy_to_user(arg,&b,sizeof(b)))
1645 return -EFAULT;
1646 return 0;
1648 case VIDIOCGCHAN:
1650 struct video_channel v;
1651 if(copy_from_user(&v, arg,sizeof(v)))
1652 return -EFAULT;
1653 v.flags=VIDEO_VC_AUDIO;
1654 v.tuners=0;
1655 v.type=VIDEO_TYPE_CAMERA;
1656 v.norm = btv->win.norm;
1657 if (v.channel>=tvcards[btv->type].video_inputs)
1658 return -EINVAL;
1659 if(v.channel==tvcards[btv->type].tuner)
1661 strcpy(v.name,"Television");
1662 v.flags|=VIDEO_VC_TUNER;
1663 v.type=VIDEO_TYPE_TV;
1664 v.tuners=1;
1666 else if(v.channel==tvcards[btv->type].svhs)
1667 strcpy(v.name,"S-Video");
1668 else
1669 sprintf(v.name,"Composite%d",v.channel);
1671 if(copy_to_user(arg,&v,sizeof(v)))
1672 return -EFAULT;
1673 return 0;
1676 * Each channel has 1 tuner
1678 case VIDIOCSCHAN:
1680 struct video_channel v;
1681 if(copy_from_user(&v, arg,sizeof(v)))
1682 return -EFAULT;
1684 if (v.channel>tvcards[btv->type].video_inputs)
1685 return -EINVAL;
1686 bt848_muxsel(btv, v.channel);
1687 if(v.norm!=VIDEO_MODE_PAL&&v.norm!=VIDEO_MODE_NTSC
1688 &&v.norm!=VIDEO_MODE_SECAM)
1689 return -EOPNOTSUPP;
1690 btv->win.norm = v.norm;
1691 make_vbitab(btv);
1692 bt848_set_winsize(btv);
1693 btv->channel=v.channel;
1694 return 0;
1696 case VIDIOCGTUNER:
1698 struct video_tuner v;
1699 if(copy_from_user(&v,arg,sizeof(v))!=0)
1700 return -EFAULT;
1701 if(v.tuner||btv->channel) /* Only tuner 0 */
1702 return -EINVAL;
1703 strcpy(v.name, "Television");
1704 v.rangelow=0;
1705 v.rangehigh=0xFFFFFFFF;
1706 v.flags=VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1707 if (btv->audio_chip == TDA9840) {
1708 v.flags |= VIDEO_AUDIO_VOLUME;
1709 v.mode = VIDEO_SOUND_MONO|VIDEO_SOUND_STEREO;
1710 v.mode |= VIDEO_SOUND_LANG1|VIDEO_SOUND_LANG2;
1712 if (btv->audio_chip == TDA9850) {
1713 unsigned char ALR1;
1714 ALR1 = I2CRead(&(btv->i2c), I2C_TDA9850|1);
1715 if (ALR1 & 32)
1716 v.flags |= VIDEO_TUNER_STEREO_ON;
1718 v.mode = btv->win.norm;
1719 v.signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1720 if(copy_to_user(arg,&v,sizeof(v)))
1721 return -EFAULT;
1722 return 0;
1724 /* We have but one tuner */
1725 case VIDIOCSTUNER:
1727 struct video_tuner v;
1728 if(copy_from_user(&v, arg, sizeof(v)))
1729 return -EFAULT;
1730 /* Only one channel has a tuner */
1731 if(v.tuner!=tvcards[btv->type].tuner)
1732 return -EINVAL;
1734 if(v.mode!=VIDEO_MODE_PAL&&v.mode!=VIDEO_MODE_NTSC
1735 &&v.mode!=VIDEO_MODE_SECAM)
1736 return -EOPNOTSUPP;
1737 btv->win.norm = v.mode;
1738 bt848_set_winsize(btv);
1739 return 0;
1741 case VIDIOCGPICT:
1743 struct video_picture p=btv->picture;
1744 if(btv->win.depth==8)
1745 p.palette=VIDEO_PALETTE_HI240;
1746 if(btv->win.depth==15)
1747 p.palette=VIDEO_PALETTE_RGB555;
1748 if(btv->win.depth==16)
1749 p.palette=VIDEO_PALETTE_RGB565;
1750 if(btv->win.depth==24)
1751 p.palette=VIDEO_PALETTE_RGB24;
1752 if(btv->win.depth==32)
1753 p.palette=VIDEO_PALETTE_RGB32;
1755 if(copy_to_user(arg, &p, sizeof(p)))
1756 return -EFAULT;
1757 return 0;
1759 case VIDIOCSPICT:
1761 struct video_picture p;
1762 int format;
1763 if(copy_from_user(&p, arg,sizeof(p)))
1764 return -EFAULT;
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);
1770 /* -128 to 127 */
1771 bt848_hue(btv, (p.hue>>8)-128);
1772 /* 0-511 */
1773 bt848_contrast(btv, p.contrast>>7);
1774 btv->picture = p;
1776 /* set palette if bpp matches */
1777 if (p.palette < sizeof(palette2fmt)/sizeof(int)) {
1778 format = palette2fmt[p.palette];
1779 if (fmtbppx2[format&0x0f]/2 == btv->win.bpp)
1780 btv->win.color_fmt = format;
1782 return 0;
1784 case VIDIOCSWIN:
1786 struct video_window vw;
1787 struct video_clip *vcp = NULL;
1788 int on;
1790 if(copy_from_user(&vw,arg,sizeof(vw)))
1791 return -EFAULT;
1793 if(vw.flags || vw.width < 16 || vw.height < 16)
1795 bt848_cap(btv,0);
1796 return -EINVAL;
1798 if (btv->win.bpp < 4)
1799 { /* 32-bit align start and adjust width */
1800 int i = vw.x;
1801 vw.x = (vw.x + 3) & ~3;
1802 i = vw.x - i;
1803 vw.width -= i;
1805 btv->win.x=vw.x;
1806 btv->win.y=vw.y;
1807 btv->win.width=vw.width;
1808 btv->win.height=vw.height;
1810 if(btv->win.height>btv->win.cropheight/2)
1811 btv->win.interlace=1;
1812 else
1813 btv->win.interlace=0;
1815 on=(btv->cap&3);
1817 bt848_cap(btv,0);
1818 bt848_set_winsize(btv);
1821 * Do any clips.
1823 if(vw.clipcount<0) {
1824 if((vcp=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL)
1825 return -ENOMEM;
1826 if(copy_from_user(vcp, vw.clips,
1827 VIDEO_CLIPMAP_SIZE)) {
1828 vfree(vcp);
1829 return -EFAULT;
1831 } else if (vw.clipcount) {
1832 if((vcp=vmalloc(sizeof(struct video_clip)*
1833 (vw.clipcount))) == NULL)
1834 return -ENOMEM;
1835 if(copy_from_user(vcp,vw.clips,
1836 sizeof(struct video_clip)*
1837 vw.clipcount)) {
1838 vfree(vcp);
1839 return -EFAULT;
1842 make_clip_tab(btv, vcp, vw.clipcount);
1843 if (vw.clipcount != 0)
1844 vfree(vcp);
1845 if(on && btv->win.vidadr!=0)
1846 bt848_cap(btv,1);
1847 return 0;
1849 case VIDIOCGWIN:
1851 struct video_window vw;
1852 /* Oh for a COBOL move corresponding .. */
1853 vw.x=btv->win.x;
1854 vw.y=btv->win.y;
1855 vw.width=btv->win.width;
1856 vw.height=btv->win.height;
1857 vw.chromakey=0;
1858 vw.flags=0;
1859 if(btv->win.interlace)
1860 vw.flags|=VIDEO_WINDOW_INTERLACE;
1861 if(copy_to_user(arg,&vw,sizeof(vw)))
1862 return -EFAULT;
1863 return 0;
1865 case VIDIOCCAPTURE:
1867 int v;
1868 if(copy_from_user(&v, arg,sizeof(v)))
1869 return -EFAULT;
1870 if(v==0)
1872 bt848_cap(btv,0);
1874 else
1876 if(btv->win.vidadr==0 || btv->win.width==0
1877 || btv->win.height==0)
1878 return -EINVAL;
1879 bt848_cap(btv,1);
1881 return 0;
1883 case VIDIOCGFBUF:
1885 struct video_buffer v;
1886 v.base=(void *)btv->win.vidadr;
1887 v.height=btv->win.sheight;
1888 v.width=btv->win.swidth;
1889 v.depth=btv->win.depth;
1890 v.bytesperline=btv->win.bpl;
1891 if(copy_to_user(arg, &v,sizeof(v)))
1892 return -EFAULT;
1893 return 0;
1896 case VIDIOCSFBUF:
1898 struct video_buffer v;
1899 #if LINUX_VERSION_CODE >= 0x020100
1900 if(!capable(CAP_SYS_ADMIN))
1901 #else
1902 if(!suser())
1903 #endif
1904 return -EPERM;
1905 if(copy_from_user(&v, arg,sizeof(v)))
1906 return -EFAULT;
1907 if(v.depth!=8 && v.depth!=15 && v.depth!=16 &&
1908 v.depth!=24 && v.depth!=32 && v.width > 16 &&
1909 v.height > 16 && v.bytesperline > 16)
1910 return -EINVAL;
1911 if (v.base)
1913 if ((unsigned long)v.base&1)
1914 btv->win.vidadr=(unsigned long)(PAGE_OFFSET|uvirt_to_bus((unsigned long)v.base));
1915 else
1916 btv->win.vidadr=(unsigned long)v.base;
1918 btv->win.sheight=v.height;
1919 btv->win.swidth=v.width;
1920 btv->win.bpp=((v.depth+7)&0x38)/8;
1921 btv->win.depth=v.depth;
1922 btv->win.bpl=v.bytesperline;
1924 DEBUG(printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1925 v.base, v.width,v.height, btv->win.bpp, btv->win.bpl));
1926 bt848_set_winsize(btv);
1927 return 0;
1929 case VIDIOCKEY:
1931 /* Will be handled higher up .. */
1932 return 0;
1934 case VIDIOCGFREQ:
1936 unsigned long v=btv->win.freq;
1937 if(copy_to_user(arg,&v,sizeof(v)))
1938 return -EFAULT;
1939 return 0;
1941 case VIDIOCSFREQ:
1943 unsigned long v;
1944 if(copy_from_user(&v, arg, sizeof(v)))
1945 return -EFAULT;
1946 btv->win.freq=v;
1947 set_freq(btv, btv->win.freq);
1948 return 0;
1951 case VIDIOCGAUDIO:
1953 struct video_audio v;
1954 v=btv->audio_dev;
1955 v.flags&=~(VIDEO_AUDIO_MUTE|VIDEO_AUDIO_MUTABLE);
1956 v.flags|=VIDEO_AUDIO_MUTABLE;
1957 strcpy(v.name,"TV");
1958 if (btv->audio_chip == TDA9850) {
1959 unsigned char ALR1;
1960 ALR1 = I2CRead(&(btv->i2c), I2C_TDA9850|1);
1961 v.mode = VIDEO_SOUND_MONO;
1962 v.mode |= (ALR1 & 32) ? VIDEO_SOUND_STEREO:0;
1963 v.mode |= (ALR1 & 64) ? VIDEO_SOUND_LANG1:0;
1965 if (btv->have_msp3400)
1967 v.flags|=VIDEO_AUDIO_VOLUME |
1968 VIDEO_AUDIO_BASS |
1969 VIDEO_AUDIO_TREBLE;
1970 i2c_control_device(&(btv->i2c),
1971 I2C_DRIVERID_MSP3400,
1972 MSP_GET_VOLUME,&(v.volume));
1973 i2c_control_device(&(btv->i2c),
1974 I2C_DRIVERID_MSP3400,
1975 MSP_GET_BASS,&(v.bass));
1976 i2c_control_device(&(btv->i2c),
1977 I2C_DRIVERID_MSP3400,
1978 MSP_GET_TREBLE,&(v.treble));
1979 i2c_control_device(&(btv->i2c),
1980 I2C_DRIVERID_MSP3400,
1981 MSP_GET_STEREO,&(v.mode));
1983 else v.mode = VIDEO_SOUND_MONO;
1984 if(copy_to_user(arg,&v,sizeof(v)))
1985 return -EFAULT;
1986 return 0;
1988 case VIDIOCSAUDIO:
1990 struct video_audio v;
1991 if(copy_from_user(&v,arg, sizeof(v)))
1992 return -EFAULT;
1993 if(v.flags&VIDEO_AUDIO_MUTE)
1994 audio(btv, AUDIO_MUTE);
1995 /* One audio source per tuner */
1996 /* if(v.audio!=0) */
1997 /* ADSTech TV card has more than one */
1998 if(v.audio<0 || v.audio >= tvcards[btv->type].audio_inputs)
1999 return -EINVAL;
2000 bt848_muxsel(btv,v.audio);
2001 if(!(v.flags&VIDEO_AUDIO_MUTE))
2002 audio(btv, AUDIO_UNMUTE);
2003 if (btv->audio_chip == TDA9850) {
2004 unsigned char con3 = 0;
2005 if (v.mode & VIDEO_SOUND_LANG1)
2006 con3 = 0x80; /* sap */
2007 if (v.mode & VIDEO_SOUND_STEREO)
2008 con3 = 0x40; /* stereo */
2009 I2CWrite(&(btv->i2c), I2C_TDA9850,
2010 TDA9850_CON3, con3, 1);
2012 if (btv->have_msp3400)
2014 i2c_control_device(&(btv->i2c),
2015 I2C_DRIVERID_MSP3400,
2016 MSP_SET_VOLUME,&(v.volume));
2017 i2c_control_device(&(btv->i2c),
2018 I2C_DRIVERID_MSP3400,
2019 MSP_SET_BASS,&(v.bass));
2020 i2c_control_device(&(btv->i2c),
2021 I2C_DRIVERID_MSP3400,
2022 MSP_SET_TREBLE,&(v.treble));
2023 i2c_control_device(&(btv->i2c),
2024 I2C_DRIVERID_MSP3400,
2025 MSP_SET_STEREO,&(v.mode));
2027 btv->audio_dev=v;
2028 return 0;
2031 case VIDIOCSYNC:
2032 if(copy_from_user((void *)&i,arg,sizeof(int)))
2033 return -EFAULT;
2034 /* if(i>1 || i<0)
2035 return -EINVAL;
2037 switch (btv->frame_stat[i]) {
2038 case GBUFFER_UNUSED:
2039 return -EINVAL;
2040 case GBUFFER_GRABBING:
2041 while(btv->frame_stat[i]==GBUFFER_GRABBING) {
2042 interruptible_sleep_on(&btv->capq);
2043 if(signal_pending(current))
2044 return -EINTR;
2046 /* fall */
2047 case GBUFFER_DONE:
2048 btv->frame_stat[i] = GBUFFER_UNUSED;
2049 break;
2051 return 0;
2053 case BTTV_WRITEE:
2054 #if LINUX_VERSION_CODE >= 0x020100
2055 if(!capable(CAP_SYS_ADMIN))
2056 #else
2057 if(!suser())
2058 #endif
2059 return -EPERM;
2060 if(copy_from_user((void *) eedata, (void *) arg, 256))
2061 return -EFAULT;
2062 writeee(&(btv->i2c), eedata);
2063 return 0;
2065 case BTTV_READEE:
2066 #if LINUX_VERSION_CODE >= 0x020100
2067 if(!capable(CAP_SYS_ADMIN))
2068 #else
2069 if(!suser())
2070 #endif
2071 return -EPERM;
2072 readee(&(btv->i2c), eedata);
2073 if(copy_to_user((void *) arg, (void *) eedata, 256))
2074 return -EFAULT;
2075 break;
2077 case BTTV_FIELDNR:
2078 if(copy_to_user((void *) arg, (void *) &btv->last_field,
2079 sizeof(btv->last_field)))
2080 return -EFAULT;
2081 break;
2083 case BTTV_PLLSET: {
2084 struct bttv_pll_info p;
2085 #if LINUX_VERSION_CODE >= 0x020100
2086 if(!capable(CAP_SYS_ADMIN))
2087 #else
2088 if(!suser())
2089 #endif
2090 return -EPERM;
2091 if(copy_from_user(&p , (void *) arg, sizeof(btv->pll)))
2092 return -EFAULT;
2093 btv->pll.pll_ifreq = p.pll_ifreq;
2094 btv->pll.pll_ofreq = p.pll_ofreq;
2095 btv->pll.pll_crystal = p.pll_crystal;
2097 break;
2099 case VIDIOCMCAPTURE:
2101 struct video_mmap vm;
2102 if(copy_from_user((void *) &vm, (void *) arg, sizeof(vm)))
2103 return -EFAULT;
2104 if (btv->frame_stat[vm.frame] == GBUFFER_GRABBING)
2105 return -EBUSY;
2106 return vgrab(btv, &vm);
2109 case VIDIOCGMBUF:
2111 struct video_mbuf vm;
2112 memset(&vm, 0 , sizeof(vm));
2113 vm.size=BTTV_MAX_FBUF*2;
2114 vm.frames=2;
2115 vm.offsets[0]=0;
2116 vm.offsets[1]=BTTV_MAX_FBUF;
2117 if(copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
2118 return -EFAULT;
2119 return 0;
2122 case VIDIOCGUNIT:
2124 struct video_unit vu;
2125 vu.video=btv->video_dev.minor;
2126 vu.vbi=btv->vbi_dev.minor;
2127 if(btv->radio_dev.minor!=-1)
2128 vu.radio=btv->radio_dev.minor;
2129 else
2130 vu.radio=VIDEO_NO_UNIT;
2131 vu.audio=VIDEO_NO_UNIT;
2132 if(btv->have_msp3400)
2134 i2c_control_device(&(btv->i2c), I2C_DRIVERID_MSP3400,
2135 MSP_GET_UNIT, &vu.audio);
2137 vu.teletext=VIDEO_NO_UNIT;
2138 if(copy_to_user((void *)arg, (void *)&vu, sizeof(vu)))
2139 return -EFAULT;
2140 return 0;
2143 case BTTV_BURST_ON:
2145 tvnorms[0].scaledtwidth=1135-BURSTOFFSET-2;
2146 tvnorms[0].hdelayx1=186-BURSTOFFSET;
2147 return 0;
2150 case BTTV_BURST_OFF:
2152 tvnorms[0].scaledtwidth=1135;
2153 tvnorms[0].hdelayx1=186;
2154 return 0;
2157 case BTTV_VERSION:
2159 return BTTV_VERSION_CODE;
2162 case BTTV_PICNR:
2164 /* return picture;*/
2165 return 0;
2168 default:
2169 return -ENOIOCTLCMD;
2171 return 0;
2174 static int bttv_init_done(struct video_device *dev)
2176 return 0;
2180 * This maps the vmalloced and reserved fbuffer to user space.
2182 * FIXME:
2183 * - PAGE_READONLY should suffice!?
2184 * - remap_page_range is kind of inefficient for page by page remapping.
2185 * But e.g. pte_alloc() does not work in modules ... :-(
2188 static int bttv_mmap(struct video_device *dev, const char *adr, unsigned long size)
2190 struct bttv *btv=(struct bttv *)dev;
2191 unsigned long start=(unsigned long) adr;
2192 unsigned long page,pos;
2194 if (size>2*BTTV_MAX_FBUF)
2195 return -EINVAL;
2196 if (!btv->fbuffer)
2198 if(fbuffer_alloc(btv))
2199 return -EINVAL;
2201 pos=(unsigned long) btv->fbuffer;
2202 while (size > 0)
2204 page = kvirt_to_phys(pos);
2205 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
2206 return -EAGAIN;
2207 start+=PAGE_SIZE;
2208 pos+=PAGE_SIZE;
2209 size-=PAGE_SIZE;
2211 return 0;
2214 static struct video_device bttv_template=
2216 "UNSET",
2217 VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY|VID_TYPE_TELETEXT,
2218 VID_HARDWARE_BT848,
2219 bttv_open,
2220 bttv_close,
2221 bttv_read,
2222 bttv_write,
2223 #if LINUX_VERSION_CODE >= 0x020100
2224 NULL, /* poll */
2225 #endif
2226 bttv_ioctl,
2227 bttv_mmap,
2228 bttv_init_done,
2229 NULL,
2235 static long vbi_read(struct video_device *v, char *buf, unsigned long count,
2236 int nonblock)
2238 struct bttv *btv=(struct bttv *)(v-2);
2239 int q,todo;
2241 todo=count;
2242 while (todo && todo>(q=VBIBUF_SIZE-btv->vbip))
2244 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, q))
2245 return -EFAULT;
2246 todo-=q;
2247 buf+=q;
2249 cli();
2250 if (todo && q==VBIBUF_SIZE-btv->vbip)
2252 if(nonblock)
2254 sti();
2255 if(count==todo)
2256 return -EWOULDBLOCK;
2257 return count-todo;
2259 interruptible_sleep_on(&btv->vbiq);
2260 sti();
2261 if(signal_pending(current))
2263 if(todo==count)
2264 return -EINTR;
2265 else
2266 return count-todo;
2270 if (todo)
2272 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, todo))
2273 return -EFAULT;
2274 btv->vbip+=todo;
2276 return count;
2279 #if LINUX_VERSION_CODE >= 0x020100
2280 static unsigned int vbi_poll(struct video_device *dev, struct file *file,
2281 poll_table *wait)
2283 struct bttv *btv=(struct bttv *)(dev-2);
2284 unsigned int mask = 0;
2286 poll_wait(file, &btv->vbiq, wait);
2288 if (btv->vbip < VBIBUF_SIZE)
2289 mask |= (POLLIN | POLLRDNORM);
2291 return mask;
2293 #endif
2295 static int vbi_open(struct video_device *dev, int flags)
2297 struct bttv *btv=(struct bttv *)(dev-2);
2299 btv->vbip=VBIBUF_SIZE;
2300 btv->cap|=0x0c;
2301 bt848_set_risc_jmps(btv);
2303 MOD_INC_USE_COUNT;
2304 return 0;
2307 static void vbi_close(struct video_device *dev)
2309 struct bttv *btv=(struct bttv *)(dev-2);
2311 btv->cap&=~0x0c;
2312 bt848_set_risc_jmps(btv);
2314 MOD_DEC_USE_COUNT;
2318 static int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2320 return -EINVAL;
2323 static struct video_device vbi_template=
2325 "bttv vbi",
2326 VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
2327 VID_HARDWARE_BT848,
2328 vbi_open,
2329 vbi_close,
2330 vbi_read,
2331 bttv_write,
2332 #if LINUX_VERSION_CODE >= 0x020100
2333 vbi_poll,
2334 #endif
2335 vbi_ioctl,
2336 NULL, /* no mmap yet */
2337 bttv_init_done,
2338 NULL,
2344 static int radio_open(struct video_device *dev, int flags)
2346 struct bttv *btv = (struct bttv *)(dev-1);
2348 if (btv->user)
2349 return -EBUSY;
2350 btv->user++;
2351 set_freq(btv,400*16);
2352 btv->radio = 1;
2353 bt848_muxsel(btv,0);
2354 audio(btv, AUDIO_UNMUTE);
2356 MOD_INC_USE_COUNT;
2357 return 0;
2360 static void radio_close(struct video_device *dev)
2362 struct bttv *btv=(struct bttv *)(dev-1);
2364 btv->user--;
2365 btv->radio = 0;
2366 audio(btv, AUDIO_MUTE);
2367 MOD_DEC_USE_COUNT;
2370 static long radio_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
2372 return -EINVAL;
2375 static int radio_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2377 struct bttv *btv=(struct bttv *)(dev-1);
2378 switch (cmd) {
2379 case VIDIOCGCAP:
2381 struct video_capability v;
2382 strcpy(v.name,btv->video_dev.name);
2383 v.type = VID_TYPE_TUNER;
2384 v.channels = 1;
2385 v.audios = 1;
2386 /* No we don't do pictures */
2387 v.maxwidth = 0;
2388 v.maxheight = 0;
2389 v.minwidth = 0;
2390 v.minheight = 0;
2391 if (copy_to_user(arg, &v, sizeof(v)))
2392 return -EFAULT;
2393 return 0;
2394 break;
2396 case VIDIOCGTUNER:
2398 struct video_tuner v;
2399 if(copy_from_user(&v,arg,sizeof(v))!=0)
2400 return -EFAULT;
2401 if(v.tuner||btv->channel) /* Only tuner 0 */
2402 return -EINVAL;
2403 strcpy(v.name, "Radio");
2404 v.rangelow=(int)(87.5*16);
2405 v.rangehigh=(int)(108*16);
2406 v.flags= 0; /* XXX */
2407 v.mode = 0; /* XXX */
2408 if(copy_to_user(arg,&v,sizeof(v)))
2409 return -EFAULT;
2410 return 0;
2412 case VIDIOCSTUNER:
2414 struct video_tuner v;
2415 if(copy_from_user(&v, arg, sizeof(v)))
2416 return -EFAULT;
2417 /* Only channel 0 has a tuner */
2418 if(v.tuner!=0 || btv->channel)
2419 return -EINVAL;
2420 /* XXX anything to do ??? */
2421 return 0;
2423 case VIDIOCGFREQ:
2424 case VIDIOCSFREQ:
2425 case VIDIOCGAUDIO:
2426 case VIDIOCSAUDIO:
2427 bttv_ioctl((struct video_device *)btv,cmd,arg);
2428 break;
2429 default:
2430 return -ENOIOCTLCMD;
2432 return 0;
2435 static struct video_device radio_template=
2437 "bttv radio",
2438 VID_TYPE_TUNER,
2439 VID_HARDWARE_BT848,
2440 radio_open,
2441 radio_close,
2442 radio_read, /* just returns -EINVAL */
2443 bttv_write, /* just returns -EINVAL */
2444 #if LINUX_VERSION_CODE >= 0x020100
2445 NULL, /* no poll */
2446 #endif
2447 radio_ioctl,
2448 NULL, /* no mmap */
2449 bttv_init_done, /* just returns 0 */
2450 NULL,
2456 struct vidbases
2458 unsigned short vendor, device;
2459 char *name;
2460 uint badr;
2463 static struct vidbases vbs[] = {
2464 { PCI_VENDOR_ID_ALLIANCE, PCI_DEVICE_ID_ALLIANCE_AT3D,
2465 "Alliance AT3D", PCI_BASE_ADDRESS_0},
2466 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_215CT222,
2467 "ATI MACH64 CT", PCI_BASE_ADDRESS_0},
2468 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_210888GX,
2469 "ATI MACH64 Winturbo", PCI_BASE_ADDRESS_0},
2470 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_215GT,
2471 "ATI MACH64 GT", PCI_BASE_ADDRESS_0},
2472 { PCI_VENDOR_ID_CIRRUS, 0, "Cirrus Logic", PCI_BASE_ADDRESS_0},
2473 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TGA,
2474 "DEC DC21030", PCI_BASE_ADDRESS_0},
2475 { PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL,
2476 "Matrox Millennium", PCI_BASE_ADDRESS_1},
2477 { PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL_2,
2478 "Matrox Millennium II", PCI_BASE_ADDRESS_0},
2479 { PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL_2_AGP,
2480 "Matrox Millennium II AGP", PCI_BASE_ADDRESS_0},
2481 { PCI_VENDOR_ID_MATROX, 0x051a, "Matrox Mystique", PCI_BASE_ADDRESS_1},
2482 { PCI_VENDOR_ID_MATROX, 0x0521, "Matrox G200", PCI_BASE_ADDRESS_0},
2483 { PCI_VENDOR_ID_N9, PCI_DEVICE_ID_N9_I128,
2484 "Number Nine Imagine 128", PCI_BASE_ADDRESS_0},
2485 { PCI_VENDOR_ID_N9, PCI_DEVICE_ID_N9_I128_2,
2486 "Number Nine Imagine 128 Series 2", PCI_BASE_ADDRESS_0},
2487 { PCI_VENDOR_ID_S3, 0, "S3", PCI_BASE_ADDRESS_0},
2488 { PCI_VENDOR_ID_TSENG, 0, "TSENG", PCI_BASE_ADDRESS_0},
2489 { PCI_VENDOR_ID_NVIDIA_SGS, PCI_DEVICE_ID_NVIDIA_SGS_RIVA128,
2490 "Riva128", PCI_BASE_ADDRESS_1},
2494 /* DEC TGA offsets stolen from XFree-3.2 */
2496 static uint dec_offsets[4] = {
2497 0x200000,
2498 0x804000,
2500 0x1004000
2503 #define NR_CARDS (sizeof(vbs)/sizeof(struct vidbases))
2505 /* Scan for PCI display adapter
2506 if more than one card is present the last one is used for now */
2508 #if LINUX_VERSION_CODE >= 0x020100
2510 static int find_vga(void)
2512 unsigned short badr;
2513 int found = 0, i, tga_type;
2514 unsigned int vidadr=0;
2515 struct pci_dev *dev;
2518 for (dev = pci_devices; dev != NULL; dev = dev->next)
2520 if (dev->class != PCI_CLASS_NOT_DEFINED_VGA &&
2521 ((dev->class) >> 16 != PCI_BASE_CLASS_DISPLAY))
2523 continue;
2525 if (PCI_FUNC(dev->devfn) != 0)
2526 continue;
2528 badr=0;
2529 printk(KERN_INFO "bttv: PCI display adapter: ");
2530 for (i=0; i<NR_CARDS; i++)
2532 if (dev->vendor == vbs[i].vendor)
2534 if (vbs[i].device)
2535 if (vbs[i].device!=dev->device)
2536 continue;
2537 printk("%s.\n", vbs[i].name);
2538 badr=vbs[i].badr;
2539 break;
2542 if (!badr)
2544 printk(KERN_ERR "bttv: Unknown video memory base address.\n");
2545 continue;
2547 pci_read_config_dword(dev, badr, &vidadr);
2548 if (vidadr & PCI_BASE_ADDRESS_SPACE_IO)
2550 printk(KERN_ERR "bttv: Memory seems to be I/O memory.\n");
2551 printk(KERN_ERR "bttv: Check entry for your card type in bttv.c vidbases struct.\n");
2552 continue;
2554 vidadr &= PCI_BASE_ADDRESS_MEM_MASK;
2555 if (!vidadr)
2557 printk(KERN_ERR "bttv: Memory @ 0, must be something wrong!");
2558 continue;
2561 if (dev->vendor == PCI_VENDOR_ID_DEC &&
2562 dev->device == PCI_DEVICE_ID_DEC_TGA)
2564 tga_type = (readl((unsigned long)vidadr) >> 12) & 0x0f;
2565 if (tga_type != 0 && tga_type != 1 && tga_type != 3)
2567 printk(KERN_ERR "bttv: TGA type (0x%x) unrecognized!\n", tga_type);
2568 found--;
2570 vidadr+=dec_offsets[tga_type];
2572 DEBUG(printk(KERN_DEBUG "bttv: memory @ 0x%08x, ", vidadr));
2573 DEBUG(printk(KERN_DEBUG "devfn: 0x%04x.\n", dev->devfn));
2574 found++;
2577 if (vidmem)
2579 vidadr=vidmem<<20;
2580 printk(KERN_INFO "bttv: Video memory override: 0x%08x\n", vidadr);
2581 found=1;
2583 for (i=0; i<BTTV_MAX; i++)
2584 bttvs[i].win.vidadr=vidadr;
2586 return found;
2589 #else
2590 static int find_vga(void)
2592 unsigned int devfn, class, vendev;
2593 unsigned short vendor, device, badr;
2594 int found=0, bus=0, i, tga_type;
2595 unsigned int vidadr=0;
2598 for (devfn = 0; devfn < 0xff; devfn++)
2600 if (PCI_FUNC(devfn) != 0)
2601 continue;
2602 pcibios_read_config_dword(bus, devfn, PCI_VENDOR_ID, &vendev);
2603 if (vendev == 0xffffffff || vendev == 0x00000000)
2604 continue;
2605 pcibios_read_config_word(bus, devfn, PCI_VENDOR_ID, &vendor);
2606 pcibios_read_config_word(bus, devfn, PCI_DEVICE_ID, &device);
2607 pcibios_read_config_dword(bus, devfn, PCI_CLASS_REVISION, &class);
2608 class = class >> 16;
2609 /* if (class == PCI_CLASS_DISPLAY_VGA) {*/
2610 if ((class>>8) == PCI_BASE_CLASS_DISPLAY ||
2611 /* Number 9 GXE64Pro needs this */
2612 class == PCI_CLASS_NOT_DEFINED_VGA)
2614 badr=0;
2615 printk(KERN_INFO "bttv: PCI display adapter: ");
2616 for (i=0; i<NR_CARDS; i++)
2618 if (vendor==vbs[i].vendor)
2620 if (vbs[i].device)
2621 if (vbs[i].device!=device)
2622 continue;
2623 printk("%s.\n", vbs[i].name);
2624 badr=vbs[i].badr;
2625 break;
2628 if (NR_CARDS == i)
2629 printk("UNKNOWN.\n");
2630 if (!badr)
2632 printk(KERN_ERR "bttv: Unknown video memory base address.\n");
2633 continue;
2635 pcibios_read_config_dword(bus, devfn, badr, &vidadr);
2636 if (vidadr & PCI_BASE_ADDRESS_SPACE_IO)
2638 printk(KERN_ERR "bttv: Memory seems to be I/O memory.\n");
2639 printk(KERN_ERR "bttv: Check entry for your card type in bttv.c vidbases struct.\n");
2640 continue;
2642 vidadr &= PCI_BASE_ADDRESS_MEM_MASK;
2643 if (!vidadr)
2645 printk(KERN_ERR "bttv: Memory @ 0, must be something wrong!\n");
2646 continue;
2649 if (vendor==PCI_VENDOR_ID_DEC)
2650 if (device==PCI_DEVICE_ID_DEC_TGA)
2652 tga_type = (readl((unsigned long)vidadr) >> 12) & 0x0f;
2653 if (tga_type != 0 && tga_type != 1 && tga_type != 3)
2655 printk(KERN_ERR "bttv: TGA type (0x%x) unrecognized!\n", tga_type);
2656 found--;
2658 vidadr+=dec_offsets[tga_type];
2661 DEBUG(printk(KERN_DEBUG "bttv: memory @ 0x%08x, ", vidadr));
2662 DEBUG(printk(KERN_DEBUG "devfn: 0x%04x.\n", devfn));
2663 found++;
2667 if (vidmem)
2669 if (vidmem < 0x1000)
2670 vidadr=vidmem<<20;
2671 else
2672 vidadr=vidmem;
2673 printk(KERN_INFO "bttv: Video memory override: 0x%08x\n", vidadr);
2674 found=1;
2676 for (i=0; i<BTTV_MAX; i++)
2677 bttvs[i].win.vidadr=vidadr;
2679 return found;
2681 #endif
2684 #define TRITON_PCON 0x50
2685 #define TRITON_BUS_CONCURRENCY (1<<0)
2686 #define TRITON_STREAMING (1<<1)
2687 #define TRITON_WRITE_BURST (1<<2)
2688 #define TRITON_PEER_CONCURRENCY (1<<3)
2691 #if LINUX_VERSION_CODE >= 0x020100
2693 static void handle_chipset(void)
2695 struct pci_dev *dev = NULL;
2697 /* Just in case some nut set this to something dangerous */
2698 if (triton1)
2699 triton1=BT848_INT_ETBF;
2701 while ((dev = pci_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_496, dev)))
2703 /* Beware the SiS 85C496 my friend - rev 49 don't work with a bttv */
2704 printk(KERN_WARNING "BT848 and SIS 85C496 chipset don't always work together.\n");
2707 /* dev == NULL */
2709 while ((dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441, dev)))
2711 unsigned char b;
2712 pci_read_config_byte(dev, 0x53, &b);
2713 DEBUG(printk(KERN_INFO "bttv: Host bridge: 82441FX Natoma, "));
2714 DEBUG(printk("bufcon=0x%02x\n",b));
2717 while ((dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437, dev)))
2719 /* unsigned char b;
2720 unsigned char bo;*/
2722 printk(KERN_INFO "bttv: Host bridge 82437FX Triton PIIX\n");
2723 triton1=BT848_INT_ETBF;
2725 #if 0
2726 /* The ETBF bit SHOULD make all this unnecessary */
2727 /* 430FX (Triton I) freezes with bus concurrency on -> switch it off */
2729 pci_read_config_byte(dev, TRITON_PCON, &b);
2730 bo=b;
2731 DEBUG(printk(KERN_DEBUG "bttv: 82437FX: PCON: 0x%x\n",b));
2732 if(!(b & TRITON_BUS_CONCURRENCY))
2734 printk(KERN_WARNING "bttv: 82437FX: disabling bus concurrency\n");
2735 b |= TRITON_BUS_CONCURRENCY;
2737 if(b & TRITON_PEER_CONCURRENCY)
2739 printk(KERN_WARNING "bttv: 82437FX: disabling peer concurrency\n");
2740 b &= ~TRITON_PEER_CONCURRENCY;
2742 if(!(b & TRITON_STREAMING))
2744 printk(KERN_WARNING "bttv: 82437FX: disabling streaming\n");
2745 b |= TRITON_STREAMING;
2748 if (b!=bo)
2750 pci_write_config_byte(dev, TRITON_PCON, b);
2751 printk(KERN_DEBUG "bttv: 82437FX: PCON changed to: 0x%x\n",b);
2753 #endif
2756 #else
2757 static void handle_chipset(void)
2759 int index;
2761 for (index = 0; index < 8; index++)
2763 unsigned char bus, devfn;
2764 unsigned char b;
2766 /* Beware the SiS 85C496 my friend - rev 49 don't work with a bttv */
2768 if (!pcibios_find_device(PCI_VENDOR_ID_SI,
2769 PCI_DEVICE_ID_SI_496,
2770 index, &bus, &devfn))
2772 printk(KERN_WARNING "BT848 and SIS 85C496 chipset don't always work together.\n");
2775 if (!pcibios_find_device(PCI_VENDOR_ID_INTEL,
2776 PCI_DEVICE_ID_INTEL_82441,
2777 index, &bus, &devfn))
2779 pcibios_read_config_byte(bus, devfn, 0x53, &b);
2780 DEBUG(printk(KERN_INFO "bttv: Host bridge: 82441FX Natoma, "));
2781 DEBUG(printk("bufcon=0x%02x\n",b));
2784 if (!pcibios_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437,
2785 index, &bus, &devfn))
2787 printk(KERN_INFO "bttv: Host bridge 82437FX Triton PIIX\n");
2788 triton1=BT848_INT_ETBF;
2790 #if 0
2791 /* The ETBF bit SHOULD make all this unnecessary */
2792 /* 430FX (Triton I) freezes with bus concurrency on -> switch it off */
2794 unsigned char bo;
2796 pcibios_read_config_byte(bus, devfn, TRITON_PCON, &b);
2797 bo=b;
2798 DEBUG(printk(KERN_DEBUG "bttv: 82437FX: PCON: 0x%x\n",b));
2800 if(!(b & TRITON_BUS_CONCURRENCY))
2802 printk(KERN_WARNING "bttv: 82437FX: disabling bus concurrency\n");
2803 b |= TRITON_BUS_CONCURRENCY;
2806 if(b & TRITON_PEER_CONCURRENCY)
2808 printk(KERN_WARNING "bttv: 82437FX: disabling peer concurrency\n");
2809 b &= ~TRITON_PEER_CONCURRENCY;
2811 if(!(b & TRITON_STREAMING))
2813 printk(KERN_WARNING "bttv: 82437FX: disabling streaming\n");
2814 b |= TRITON_STREAMING;
2817 if (b!=bo)
2819 pcibios_write_config_byte(bus, devfn, TRITON_PCON, b);
2820 printk(KERN_DEBUG "bttv: 82437FX: PCON changed to: 0x%x\n",b);
2823 #endif
2827 #endif
2829 static void init_tda8425(struct i2c_bus *bus)
2831 I2CWrite(bus, I2C_TDA8425, TDA8425_VL, 0xFC, 1); /* volume left 0dB */
2832 I2CWrite(bus, I2C_TDA8425, TDA8425_VR, 0xFC, 1); /* volume right 0dB */
2833 I2CWrite(bus, I2C_TDA8425, TDA8425_BA, 0xF6, 1); /* bass 0dB */
2834 I2CWrite(bus, I2C_TDA8425, TDA8425_TR, 0xF6, 1); /* treble 0dB */
2835 I2CWrite(bus, I2C_TDA8425, TDA8425_S1, 0xCE, 1); /* mute off */
2838 static void init_tda9840(struct i2c_bus *bus)
2840 I2CWrite(bus, I2C_TDA9840, TDA9840_SW, 0x2A, 1); /* Sound mode switching */
2843 static void init_tda9850(struct i2c_bus *bus)
2845 I2CWrite(bus, I2C_TDA9850, TDA9850_CON1, 0x08, 1); /* noise threshold st */
2846 I2CWrite(bus, I2C_TDA9850, TDA9850_CON2, 0x08, 1); /* noise threshold sap */
2847 I2CWrite(bus, I2C_TDA9850, TDA9850_CON3, 0x40, 1); /* stereo mode */
2848 I2CWrite(bus, I2C_TDA9850, TDA9850_CON4, 0x07, 1); /* 0 dB input gain?*/
2849 I2CWrite(bus, I2C_TDA9850, TDA9850_ALI1, 0x10, 1); /* wideband alignment? */
2850 I2CWrite(bus, I2C_TDA9850, TDA9850_ALI2, 0x10, 1); /* spectral alignment? */
2851 I2CWrite(bus, I2C_TDA9850, TDA9850_ALI3, 0x03, 1);
2856 /* Figure out card and tuner type */
2858 static void idcard(int i)
2860 struct bttv *btv = &bttvs[i];
2862 btwrite(0, BT848_GPIO_OUT_EN);
2863 DEBUG(printk(KERN_DEBUG "bttv%d: GPIO: 0x%08x\n", i, btread(BT848_GPIO_DATA)));
2865 /* Default the card to the user-selected one. */
2866 btv->type=card[i];
2867 btv->tuner_type=-1; /* use default tuner type */
2869 /* If we were asked to auto-detect, then do so!
2870 Right now this will only recognize Miro, Hauppauge or STB
2872 if (btv->type == BTTV_UNKNOWN)
2874 if (I2CRead(&(btv->i2c), I2C_HAUPEE)>=0)
2876 if(btv->id>849)
2877 btv->type=BTTV_HAUPPAUGE878;
2878 else
2879 btv->type=BTTV_HAUPPAUGE;
2881 } else if (I2CRead(&(btv->i2c), I2C_STBEE)>=0) {
2882 btv->type=BTTV_STB;
2883 } else
2884 if (I2CRead(&(btv->i2c), I2C_VHX)>=0) {
2885 btv->type=BTTV_VHX;
2886 } else {
2887 if (I2CRead(&(btv->i2c), 0x80)>=0) /* check for msp34xx */
2888 btv->type = BTTV_MIROPRO;
2889 else
2890 btv->type=BTTV_MIRO;
2894 /* board specific initialisations */
2895 if (btv->type == BTTV_MIRO || btv->type == BTTV_MIROPRO) {
2896 /* auto detect tuner for MIRO cards */
2897 btv->tuner_type=((btread(BT848_GPIO_DATA)>>10)-1)&7;
2899 if (btv->type == BTTV_HAUPPAUGE || btv->type == BTTV_HAUPPAUGE878) {
2900 hauppauge_msp_reset(btv);
2901 hauppauge_eeprom(&(btv->i2c));
2902 if (btv->type == BTTV_HAUPPAUGE878) {
2903 /* all bt878 hauppauge boards use this ... */
2904 btv->pll.pll_ifreq=28636363;
2905 btv->pll.pll_crystal=BT848_IFORM_XT0;
2908 if(btv->type==BTTV_AVERMEDIA98)
2910 btv->pll.pll_ifreq=28636363;
2911 btv->pll.pll_crystal=BT848_IFORM_XT0;
2914 if (btv->have_tuner && btv->tuner_type != -1)
2915 i2c_control_device(&(btv->i2c),
2916 I2C_DRIVERID_TUNER,
2917 TUNER_SET_TYPE,&btv->tuner_type);
2920 if (I2CRead(&(btv->i2c), I2C_TDA9840) >=0)
2922 btv->audio_chip = TDA9840;
2923 printk(KERN_INFO "bttv%d: audio chip: TDA9840\n", btv->nr);
2926 if (I2CRead(&(btv->i2c), I2C_TDA9850) >=0)
2928 btv->audio_chip = TDA9850;
2929 printk(KERN_INFO "bttv%d: audio chip: TDA9850\n",btv->nr);
2932 if (I2CRead(&(btv->i2c), I2C_TDA8425) >=0)
2934 btv->audio_chip = TDA8425;
2935 printk(KERN_INFO "bttv%d: audio chip: TDA8425\n",btv->nr);
2938 switch(btv->audio_chip)
2940 case TDA9850:
2941 init_tda9850(&(btv->i2c));
2942 break;
2943 case TDA9840:
2944 init_tda9840(&(btv->i2c));
2945 break;
2946 case TDA8425:
2947 init_tda8425(&(btv->i2c));
2948 break;
2951 printk(KERN_INFO "bttv%d: model: ",btv->nr);
2953 sprintf(btv->video_dev.name,"BT%d",btv->id);
2954 switch (btv->type)
2956 case BTTV_MIRO:
2957 case BTTV_MIROPRO:
2958 strcat(btv->video_dev.name,
2959 (btv->type == BTTV_MIRO) ? "(Miro)" : "(Miro pro)");
2960 break;
2961 case BTTV_HAUPPAUGE:
2962 strcat(btv->video_dev.name,"(Hauppauge old)");
2963 break;
2964 case BTTV_HAUPPAUGE878:
2965 strcat(btv->video_dev.name,"(Hauppauge new)");
2966 break;
2967 case BTTV_STB:
2968 strcat(btv->video_dev.name,"(STB)");
2969 break;
2970 case BTTV_INTEL:
2971 strcat(btv->video_dev.name,"(Intel)");
2972 break;
2973 case BTTV_DIAMOND:
2974 strcat(btv->video_dev.name,"(Diamond)");
2975 break;
2976 case BTTV_AVERMEDIA:
2977 strcat(btv->video_dev.name,"(AVerMedia)");
2978 break;
2979 case BTTV_MATRIX_VISION:
2980 strcat(btv->video_dev.name,"(MATRIX-Vision)");
2981 break;
2982 case BTTV_AVERMEDIA98:
2983 strcat(btv->video_dev.name,"(AVerMedia TVCapture 98)");
2984 break;
2985 case BTTV_VHX:
2986 strcpy(btv->video_dev.name,"BT848(Aimslab-VHX)");
2987 break;
2989 printk("%s\n",btv->video_dev.name);
2990 audio(btv, AUDIO_MUTE);
2995 static void bt848_set_risc_jmps(struct bttv *btv)
2997 int flags=btv->cap;
2999 /* Sync to start of odd field */
3000 btv->risc_jmp[0]=BT848_RISC_SYNC|BT848_RISC_RESYNC|BT848_FIFO_STATUS_VRE;
3001 btv->risc_jmp[1]=0;
3003 /* Jump to odd vbi sub */
3004 btv->risc_jmp[2]=BT848_RISC_JUMP|(0x5<<20);
3005 if (flags&8)
3006 btv->risc_jmp[3]=virt_to_bus(btv->vbi_odd);
3007 else
3008 btv->risc_jmp[3]=virt_to_bus(btv->risc_jmp+4);
3010 /* Jump to odd sub */
3011 btv->risc_jmp[4]=BT848_RISC_JUMP|(0x6<<20);
3012 if (flags&2)
3013 btv->risc_jmp[5]=virt_to_bus(btv->risc_odd);
3014 else
3015 btv->risc_jmp[5]=virt_to_bus(btv->risc_jmp+6);
3018 /* Sync to start of even field */
3019 btv->risc_jmp[6]=BT848_RISC_SYNC|BT848_RISC_RESYNC|BT848_FIFO_STATUS_VRO;
3020 btv->risc_jmp[7]=0;
3022 /* Jump to even vbi sub */
3023 btv->risc_jmp[8]=BT848_RISC_JUMP;
3024 if (flags&4)
3025 btv->risc_jmp[9]=virt_to_bus(btv->vbi_even);
3026 else
3027 btv->risc_jmp[9]=virt_to_bus(btv->risc_jmp+10);
3029 /* Jump to even sub */
3030 btv->risc_jmp[10]=BT848_RISC_JUMP|(8<<20);
3031 if (flags&1)
3032 btv->risc_jmp[11]=virt_to_bus(btv->risc_even);
3033 else
3034 btv->risc_jmp[11]=virt_to_bus(btv->risc_jmp+12);
3036 btv->risc_jmp[12]=BT848_RISC_JUMP;
3037 btv->risc_jmp[13]=virt_to_bus(btv->risc_jmp);
3039 /* enable cpaturing and DMA */
3040 btaor(flags, ~0x0f, BT848_CAP_CTL);
3041 if (flags&0x0f)
3042 bt848_dma(btv, 3);
3043 else
3044 bt848_dma(btv, 0);
3047 static int init_bt848(int i)
3049 struct bttv *btv = &bttvs[i];
3051 btv->user=0;
3053 /* reset the bt848 */
3054 btwrite(0, BT848_SRESET);
3055 DEBUG(printk(KERN_DEBUG "bttv%d: bt848_mem: 0x%08x\n",i,(unsigned int) btv->bt848_mem));
3057 /* default setup for max. PAL size in a 1024xXXX hicolor framebuffer */
3058 btv->win.norm=0; /* change this to 1 for NTSC, 2 for SECAM */
3059 btv->win.interlace=1;
3060 btv->win.x=0;
3061 btv->win.y=0;
3062 btv->win.width=768; /* 640 */
3063 btv->win.height=576; /* 480 */
3064 btv->win.cropwidth=768; /* 640 */
3065 btv->win.cropheight=576; /* 480 */
3066 btv->win.cropx=0;
3067 btv->win.cropy=0;
3068 btv->win.bpp=2;
3069 btv->win.depth=16;
3070 btv->win.color_fmt=BT848_COLOR_FMT_RGB16;
3071 btv->win.bpl=1024*btv->win.bpp;
3072 btv->win.swidth=1024;
3073 btv->win.sheight=768;
3074 btv->cap=0;
3076 btv->gmode=0;
3077 btv->risc_odd=0;
3078 btv->risc_even=0;
3079 btv->risc_jmp=0;
3080 btv->vbibuf=0;
3081 btv->grisc=0;
3082 btv->grabbing=0;
3083 btv->grabcount=0;
3084 btv->grab=0;
3085 btv->lastgrab=0;
3086 btv->field=btv->last_field=0;
3087 /* cevans - prevents panic if initialization bails due to memory
3088 * alloc failures!
3090 btv->video_dev.minor = -1;
3091 btv->vbi_dev.minor = -1;
3092 btv->radio_dev.minor = -1;
3094 /* i2c */
3095 memcpy(&(btv->i2c),&bttv_i2c_bus_template,sizeof(struct i2c_bus));
3096 sprintf(btv->i2c.name,"bt848-%d",i);
3097 btv->i2c.data = btv;
3099 if (!(btv->risc_odd=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
3100 return -1;
3101 if (!(btv->risc_even=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
3102 return -1;
3103 if (!(btv->risc_jmp =(unsigned int *) kmalloc(2048, GFP_KERNEL)))
3104 return -1;
3105 DEBUG(printk(KERN_DEBUG "risc_jmp: %p\n",btv->risc_jmp));
3106 btv->vbi_odd=btv->risc_jmp+16;
3107 btv->vbi_even=btv->vbi_odd+256;
3108 btv->bus_vbi_odd=virt_to_bus(btv->risc_jmp+12);
3109 btv->bus_vbi_even=virt_to_bus(btv->risc_jmp+6);
3111 btwrite(virt_to_bus(btv->risc_jmp+2), BT848_RISC_STRT_ADD);
3112 btv->vbibuf=(unsigned char *) vmalloc(VBIBUF_SIZE);
3113 if (!btv->vbibuf)
3114 return -1;
3115 if (!(btv->grisc=(unsigned int *) kmalloc(32768, GFP_KERNEL)))
3116 return -1;
3118 memset(btv->vbibuf, 0, VBIBUF_SIZE); /* We don't want to return random
3119 memory to the user */
3121 btv->fbuffer=NULL;
3123 bt848_muxsel(btv, 1);
3124 bt848_set_winsize(btv);
3126 /* btwrite(0, BT848_TDEC); */
3127 btwrite(0x10, BT848_COLOR_CTL);
3128 btwrite(0x00, BT848_CAP_CTL);
3129 btwrite(0xac, BT848_GPIO_DMA_CTL);
3131 /* select direct input */
3132 btwrite(0x00, BT848_GPIO_REG_INP);
3134 btwrite(BT848_IFORM_MUX1 | BT848_IFORM_XTAUTO | BT848_IFORM_PAL_BDGHI,
3135 BT848_IFORM);
3137 btwrite(0xd8, BT848_CONTRAST_LO);
3138 bt848_bright(btv, 0x10);
3140 btwrite(0x20, BT848_E_VSCALE_HI);
3141 btwrite(0x20, BT848_O_VSCALE_HI);
3142 btwrite(/*BT848_ADC_SYNC_T|*/
3143 BT848_ADC_RESERVED|BT848_ADC_CRUSH, BT848_ADC);
3145 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
3146 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
3148 btv->picture.colour=254<<7;
3149 btv->picture.brightness=128<<8;
3150 btv->picture.hue=128<<8;
3151 btv->picture.contrast=0xd8<<7;
3153 btwrite(0x00, BT848_E_SCLOOP);
3154 btwrite(0x00, BT848_O_SCLOOP);
3156 /* clear interrupt status */
3157 btwrite(0xfffffUL, BT848_INT_STAT);
3159 /* set interrupt mask */
3160 btwrite(btv->triton1|
3161 /*BT848_INT_PABORT|BT848_INT_RIPERR|BT848_INT_PPERR|
3162 BT848_INT_FDSR|BT848_INT_FTRGT|BT848_INT_FBUS|*/
3163 BT848_INT_VSYNC|
3164 BT848_INT_SCERR|
3165 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
3166 BT848_INT_FMTCHG|BT848_INT_HLOCK,
3167 BT848_INT_MASK);
3169 make_vbitab(btv);
3170 bt848_set_risc_jmps(btv);
3173 * Now add the template and register the device unit.
3176 memcpy(&btv->video_dev,&bttv_template, sizeof(bttv_template));
3177 memcpy(&btv->vbi_dev,&vbi_template, sizeof(vbi_template));
3178 memcpy(&btv->radio_dev,&radio_template,sizeof(radio_template));
3180 idcard(i);
3182 if(video_register_device(&btv->video_dev,VFL_TYPE_GRABBER)<0)
3183 return -1;
3184 if(video_register_device(&btv->vbi_dev,VFL_TYPE_VBI)<0)
3186 video_unregister_device(&btv->video_dev);
3187 return -1;
3189 if (radio[i])
3191 if(video_register_device(&btv->radio_dev, VFL_TYPE_RADIO)<0)
3193 video_unregister_device(&btv->vbi_dev);
3194 video_unregister_device(&btv->video_dev);
3195 return -1;
3198 i2c_register_bus(&btv->i2c);
3200 return 0;
3203 static void bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3205 u32 stat,astat;
3206 u32 dstat;
3207 int count;
3208 struct bttv *btv;
3210 btv=(struct bttv *)dev_id;
3211 count=0;
3212 while (1)
3214 /* get/clear interrupt status bits */
3215 stat=btread(BT848_INT_STAT);
3216 astat=stat&btread(BT848_INT_MASK);
3217 if (!astat)
3218 return;
3219 btwrite(astat,BT848_INT_STAT);
3220 IDEBUG(printk ("bttv%d: astat %08x\n", btv->nr, astat));
3221 IDEBUG(printk ("bttv%d: stat %08x\n", btv->nr, stat));
3223 /* get device status bits */
3224 dstat=btread(BT848_DSTATUS);
3226 if (astat&BT848_INT_FMTCHG)
3228 IDEBUG(printk ("bttv%d: IRQ_FMTCHG\n", btv->nr));
3229 /*btv->win.norm&=
3230 (dstat&BT848_DSTATUS_NUML) ? (~1) : (~0); */
3232 if (astat&BT848_INT_VPRES)
3234 IDEBUG(printk ("bttv%d: IRQ_VPRES\n", btv->nr));
3236 if (astat&BT848_INT_VSYNC)
3238 IDEBUG(printk ("bttv%d: IRQ_VSYNC\n", btv->nr));
3239 btv->field++;
3241 if (astat&BT848_INT_SCERR) {
3242 IDEBUG(printk ("bttv%d: IRQ_SCERR\n", btv->nr));
3243 bt848_dma(btv, 0);
3244 bt848_dma(btv, 1);
3245 wake_up_interruptible(&btv->vbiq);
3246 wake_up_interruptible(&btv->capq);
3249 if (astat&BT848_INT_RISCI)
3251 IDEBUG(printk ("bttv%d: IRQ_RISCI\n", btv->nr));
3253 /* captured VBI frame */
3254 if (stat&(1<<28))
3256 btv->vbip=0;
3257 /* inc vbi frame count for detecting drops */
3258 (*(u32 *)&(btv->vbibuf[VBIBUF_SIZE - 4]))++;
3259 wake_up_interruptible(&btv->vbiq);
3262 /* captured full frame */
3263 if (stat&(2<<28))
3265 wake_up_interruptible(&btv->capq);
3266 btv->last_field=btv->field;
3267 btv->grab++;
3268 btv->frame_stat[btv->grf] = GBUFFER_DONE;
3269 if ((--btv->grabbing))
3271 btv->gfmt = btv->gfmt_next;
3272 btv->gwidth = btv->gwidth_next;
3273 btv->gheight = btv->gheight_next;
3274 btv->gro = btv->gro_next;
3275 btv->gre = btv->gre_next;
3276 btv->grf = btv->grf_next;
3277 btv->risc_jmp[5]=btv->gro;
3278 btv->risc_jmp[11]=btv->gre;
3279 bt848_set_geo(btv, btv->gwidth,
3280 btv->gheight,
3281 btv->gfmt);
3282 } else {
3283 bt848_set_risc_jmps(btv);
3284 btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI);
3285 btand(~BT848_VSCALE_COMB, BT848_O_VSCALE_HI);
3286 bt848_set_geo(btv, btv->win.width,
3287 btv->win.height,
3288 btv->win.color_fmt);
3290 wake_up_interruptible(&btv->capq);
3291 break;
3293 if (stat&(8<<28))
3295 btv->risc_jmp[5]=btv->gro;
3296 btv->risc_jmp[11]=btv->gre;
3297 btv->risc_jmp[12]=BT848_RISC_JUMP;
3298 bt848_set_geo(btv, btv->gwidth, btv->gheight,
3299 btv->gfmt);
3302 if (astat&BT848_INT_OCERR)
3304 IDEBUG(printk ("bttv%d: IRQ_OCERR\n", btv->nr));
3306 if (astat&BT848_INT_PABORT)
3308 IDEBUG(printk ("bttv%d: IRQ_PABORT\n", btv->nr));
3310 if (astat&BT848_INT_RIPERR)
3312 IDEBUG(printk ("bttv%d: IRQ_RIPERR\n", btv->nr));
3314 if (astat&BT848_INT_PPERR)
3316 IDEBUG(printk ("bttv%d: IRQ_PPERR\n", btv->nr));
3318 if (astat&BT848_INT_FDSR)
3320 IDEBUG(printk ("bttv%d: IRQ_FDSR\n", btv->nr));
3322 if (astat&BT848_INT_FTRGT)
3324 IDEBUG(printk ("bttv%d: IRQ_FTRGT\n", btv->nr));
3326 if (astat&BT848_INT_FBUS)
3328 IDEBUG(printk ("bttv%d: IRQ_FBUS\n", btv->nr));
3330 if (astat&BT848_INT_HLOCK)
3332 if ((dstat&BT848_DSTATUS_HLOC) || (btv->radio))
3333 audio(btv, AUDIO_ON);
3334 else
3335 audio(btv, AUDIO_OFF);
3338 if (astat&BT848_INT_I2CDONE)
3342 count++;
3343 if (count > 10)
3344 printk (KERN_WARNING "bttv%d: irq loop %d\n",
3345 btv->nr,count);
3346 if (count > 20)
3348 btwrite(0, BT848_INT_MASK);
3349 printk(KERN_ERR
3350 "bttv%d: IRQ lockup, cleared int mask\n", btv->nr);
3358 * Scan for a Bt848 card, request the irq and map the io memory
3361 #if LINUX_VERSION_CODE >= 0x020100
3362 int configure_bt848(struct pci_dev *dev, int bttv_num)
3364 int result;
3365 unsigned char command;
3366 struct bttv *btv;
3368 btv=&bttvs[bttv_num];
3369 btv->dev=dev;
3370 btv->nr = bttv_num;
3371 btv->bt848_mem=NULL;
3372 btv->vbibuf=NULL;
3373 btv->risc_jmp=NULL;
3374 btv->vbi_odd=NULL;
3375 btv->vbi_even=NULL;
3376 btv->vbiq=NULL;
3377 btv->capq=NULL;
3378 btv->capqo=NULL;
3379 btv->capqe=NULL;
3380 btv->vbip=VBIBUF_SIZE;
3382 btv->id=dev->device;
3383 btv->irq=dev->irq;
3384 btv->bt848_adr=dev->base_address[0];
3385 if (btv->id >= 878)
3386 btv->i2c_command = 0x83;
3387 else
3388 btv->i2c_command=(I2C_TIMING | BT848_I2C_SCL | BT848_I2C_SDA);
3390 if (remap[bttv_num])
3392 if (remap[bttv_num] < 0x1000)
3393 remap[bttv_num]<<=20;
3394 remap[bttv_num]&=PCI_BASE_ADDRESS_MEM_MASK;
3395 printk(KERN_INFO "bttv%d: remapping to : 0x%08x.\n",
3396 bttv_num,remap[bttv_num]);
3397 remap[bttv_num]|=btv->bt848_adr&(~PCI_BASE_ADDRESS_MEM_MASK);
3398 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, remap[bttv_num]);
3399 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &btv->bt848_adr);
3400 btv->dev->base_address[0] = btv->bt848_adr;
3402 btv->bt848_adr&=PCI_BASE_ADDRESS_MEM_MASK;
3403 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3404 printk(KERN_INFO "bttv%d: Brooktree Bt%d (rev %d) ",
3405 bttv_num,btv->id, btv->revision);
3406 printk("bus: %d, devfn: %d, ",dev->bus->number, dev->devfn);
3407 printk("irq: %d, ",btv->irq);
3408 printk("memory: 0x%08x.\n", btv->bt848_adr);
3410 btv->pll.pll_crystal = 0;
3411 btv->pll.pll_ifreq = 0;
3412 btv->pll.pll_ofreq = 0;
3413 btv->pll.pll_current = 0;
3414 if (!(btv->id==848 && btv->revision==0x11)) {
3415 switch (pll[btv->nr]) {
3416 case 0:
3417 /* off */
3418 break;
3419 case 1:
3420 /* 28 MHz crystal installed */
3421 btv->pll.pll_ifreq=28636363;
3422 btv->pll.pll_crystal=BT848_IFORM_XT0;
3423 break;
3424 case 2:
3425 /* 35 MHz crystal installed */
3426 btv->pll.pll_ifreq=35468950;
3427 btv->pll.pll_crystal=BT848_IFORM_XT1;
3428 break;
3432 btv->bt848_mem=ioremap(btv->bt848_adr, 0x1000);
3434 /* clear interrupt mask */
3435 btwrite(0, BT848_INT_MASK);
3437 result = request_irq(btv->irq, bttv_irq,
3438 SA_SHIRQ | SA_INTERRUPT,"bttv",(void *)btv);
3439 if (result==-EINVAL)
3441 printk(KERN_ERR "bttv%d: Bad irq number or handler\n",
3442 bttv_num);
3443 return -EINVAL;
3445 if (result==-EBUSY)
3447 printk(KERN_ERR "bttv%d: IRQ %d busy, change your PnP config in BIOS\n",bttv_num,btv->irq);
3448 return result;
3450 if (result < 0)
3451 return result;
3453 pci_set_master(dev);
3455 btv->triton1=triton1 ? BT848_INT_ETBF : 0;
3456 if (triton1 && btv->id >= 878)
3458 btv->triton1 = 0;
3459 printk("bttv: Enabling 430FX compatibilty for bt878\n");
3460 pci_read_config_byte(dev, BT878_DEVCTRL, &command);
3461 command|=BT878_EN_TBFX;
3462 pci_write_config_byte(dev, BT878_DEVCTRL, command);
3463 pci_read_config_byte(dev, BT878_DEVCTRL, &command);
3464 if (!(command&BT878_EN_TBFX))
3466 printk("bttv: 430FX compatibility could not be enabled\n");
3467 return -1;
3471 return 0;
3474 static int find_bt848(void)
3476 struct pci_dev *dev = pci_devices;
3477 int result=0;
3479 bttv_num=0;
3481 while (dev)
3483 if (dev->vendor == PCI_VENDOR_ID_BROOKTREE)
3484 if ((dev->device == PCI_DEVICE_ID_BT848)||
3485 (dev->device == PCI_DEVICE_ID_BT849)||
3486 (dev->device == PCI_DEVICE_ID_BT878)||
3487 (dev->device == PCI_DEVICE_ID_BT879))
3488 result=configure_bt848(dev,bttv_num++);
3489 if (result)
3490 return result;
3491 dev = dev->next;
3493 if(bttv_num)
3494 printk(KERN_INFO "bttv: %d Bt8xx card(s) found.\n", bttv_num);
3495 return bttv_num;
3497 #else
3498 static int find_bt848(void)
3500 short pci_index;
3501 unsigned char command, latency;
3502 int result;
3503 unsigned char bus, devfn;
3504 struct bttv *btv;
3506 bttv_num=0;
3508 if (!pcibios_present())
3510 DEBUG(printk(KERN_DEBUG "bttv%d: PCI-BIOS not present or not accessable!\n",bttv_num));
3511 return 0;
3514 for (pci_index = 0;
3515 !pcibios_find_device(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
3516 pci_index, &bus, &devfn)
3517 ||!pcibios_find_device(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
3518 pci_index, &bus, &devfn)
3519 ||!pcibios_find_device(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
3520 pci_index, &bus, &devfn)
3521 ||!pcibios_find_device(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
3522 pci_index, &bus, &devfn);
3523 ++pci_index)
3525 btv=&bttvs[bttv_num];
3526 btv->nr = bttv_num;
3527 btv->bus=bus;
3528 btv->devfn=devfn;
3529 btv->bt848_mem=NULL;
3530 btv->vbibuf=NULL;
3531 btv->risc_jmp=NULL;
3532 btv->vbi_odd=NULL;
3533 btv->vbi_even=NULL;
3534 btv->vbiq=NULL;
3535 btv->capq=NULL;
3536 btv->capqo=NULL;
3537 btv->capqe=NULL;
3539 btv->vbip=VBIBUF_SIZE;
3541 pcibios_read_config_word(btv->bus, btv->devfn, PCI_DEVICE_ID,
3542 &btv->id);
3543 pcibios_read_config_byte(btv->bus, btv->devfn,
3544 PCI_INTERRUPT_LINE, &btv->irq);
3545 pcibios_read_config_dword(btv->bus, btv->devfn, PCI_BASE_ADDRESS_0,
3546 &btv->bt848_adr);
3547 if (btv->id >= 878)
3548 btv->i2c_command = 0x83;
3549 else
3550 btv->i2c_command=
3551 (I2C_TIMING | BT848_I2C_SCL | BT848_I2C_SDA);
3553 if (remap[bttv_num])
3555 if (remap[bttv_num] < 0x1000)
3556 remap[bttv_num]<<=20;
3557 remap[bttv_num]&=PCI_BASE_ADDRESS_MEM_MASK;
3558 printk(KERN_INFO "bttv%d: remapping to : 0x%08x.\n",
3559 bttv_num,remap[bttv_num]);
3560 remap[bttv_num]|=btv->bt848_adr&(~PCI_BASE_ADDRESS_MEM_MASK);
3561 pcibios_write_config_dword(btv->bus, btv->devfn, PCI_BASE_ADDRESS_0,
3562 remap[bttv_num]);
3563 pcibios_read_config_dword(btv->bus, btv->devfn, PCI_BASE_ADDRESS_0,
3564 &btv->bt848_adr);
3567 btv->bt848_adr&=PCI_BASE_ADDRESS_MEM_MASK;
3568 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_CLASS_REVISION,
3569 &btv->revision);
3570 printk(KERN_INFO "bttv%d: Brooktree Bt%d (rev %d) ",
3571 bttv_num,btv->id, btv->revision);
3572 printk("bus: %d, devfn: %d, ",
3573 btv->bus, btv->devfn);
3574 printk("irq: %d, ",btv->irq);
3575 printk("memory: 0x%08x.\n", btv->bt848_adr);
3577 btv->pll.pll_crystal = 0;
3578 btv->pll.pll_ifreq = 0;
3579 btv->pll.pll_ofreq = 0;
3580 btv->pll.pll_current = 0;
3581 if (!(btv->id==848 && btv->revision==0x11)) {
3582 switch (pll[btv->nr]) {
3583 case 0:
3584 /* off */
3585 break;
3586 case 1:
3587 /* 28 MHz crystal installed */
3588 btv->pll.pll_ifreq=28636363;
3589 btv->pll.pll_crystal=BT848_IFORM_XT0;
3590 break;
3591 case 2:
3592 /* 35 MHz crystal installed */
3593 btv->pll.pll_ifreq=35468950;
3594 btv->pll.pll_crystal=BT848_IFORM_XT1;
3595 break;
3599 btv->bt848_mem=ioremap(btv->bt848_adr, 0x1000);
3601 result = request_irq(btv->irq, bttv_irq,
3602 SA_SHIRQ | SA_INTERRUPT,"bttv",(void *)btv);
3603 if (result==-EINVAL)
3605 printk(KERN_ERR "bttv%d: Bad irq number or handler\n",
3606 bttv_num);
3607 return -EINVAL;
3609 if (result==-EBUSY)
3611 printk(KERN_ERR "bttv%d: IRQ %d busy, change your PnP config in BIOS\n",bttv_num,btv->irq);
3612 return result;
3614 if (result < 0)
3615 return result;
3617 /* Enable bus-mastering */
3618 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_COMMAND, &command);
3619 command|=PCI_COMMAND_MASTER;
3620 pcibios_write_config_byte(btv->bus, btv->devfn, PCI_COMMAND, command);
3621 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_COMMAND, &command);
3622 if (!(command&PCI_COMMAND_MASTER))
3624 printk(KERN_ERR "bttv%d: PCI bus-mastering could not be enabled\n",bttv_num);
3625 return -1;
3627 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_LATENCY_TIMER,
3628 &latency);
3629 if (!latency)
3631 latency=32;
3632 pcibios_write_config_byte(btv->bus, btv->devfn,
3633 PCI_LATENCY_TIMER, latency);
3635 DEBUG(printk(KERN_DEBUG "bttv%d: latency: %02x\n",
3636 bttv_num, latency));
3638 btv->triton1=triton1 ? BT848_INT_ETBF : 0;
3639 if (triton1 && btv->id >= 878)
3641 triton1 = 0;
3642 printk("bttv: Enabling 430FX compatibilty for bt878\n");
3643 pcibios_read_config_byte(btv->bus, btv->devfn, BT878_DEVCTRL, &command);
3644 command|=BT878_EN_TBFX;
3645 pcibios_write_config_byte(btv->bus, btv->devfn, BT878_DEVCTRL, command);
3646 pcibios_read_config_byte(btv->bus, btv->devfn, BT878_DEVCTRL, &command);
3647 if (!(command&BT878_EN_TBFX))
3649 printk("bttv: 430FX compatibility could not be enabled\n");
3650 return -1;
3654 bttv_num++;
3656 if(bttv_num)
3657 printk(KERN_INFO "bttv: %d Bt8xx card(s) found.\n", bttv_num);
3658 return bttv_num;
3660 #endif
3662 static void release_bttv(void)
3664 u8 command;
3665 int i;
3666 struct bttv *btv;
3668 for (i=0;i<bttv_num; i++)
3670 btv=&bttvs[i];
3672 /* turn off all capturing, DMA and IRQs */
3674 btand(~15, BT848_GPIO_DMA_CTL);
3676 /* first disable interrupts before unmapping the memory! */
3677 btwrite(0, BT848_INT_MASK);
3678 btwrite(0xffffffffUL,BT848_INT_STAT);
3679 btwrite(0x0, BT848_GPIO_OUT_EN);
3681 /* unregister i2c_bus */
3682 i2c_unregister_bus((&btv->i2c));
3684 /* disable PCI bus-mastering */
3685 #if LINUX_VERSION_CODE >= 0x020100
3686 pci_read_config_byte(btv->dev, PCI_COMMAND, &command);
3687 /* Should this be &=~ ?? */
3688 command&=~PCI_COMMAND_MASTER;
3689 pci_write_config_byte(btv->dev, PCI_COMMAND, command);
3690 #else
3691 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_COMMAND, &command);
3692 command&=~PCI_COMMAND_MASTER;
3693 pcibios_write_config_byte(btv->bus, btv->devfn, PCI_COMMAND, command);
3695 #endif
3697 /* unmap and free memory */
3698 if (btv->grisc)
3699 kfree((void *) btv->grisc);
3701 if (btv->risc_odd)
3702 kfree((void *) btv->risc_odd);
3704 if (btv->risc_even)
3705 kfree((void *) btv->risc_even);
3707 DEBUG(printk(KERN_DEBUG "free: risc_jmp: 0x%08x.\n", btv->risc_jmp));
3708 if (btv->risc_jmp)
3709 kfree((void *) btv->risc_jmp);
3711 DEBUG(printk(KERN_DEBUG "bt848_vbibuf: 0x%08x.\n", btv->vbibuf));
3712 if (btv->vbibuf)
3713 vfree((void *) btv->vbibuf);
3716 free_irq(btv->irq,btv);
3717 DEBUG(printk(KERN_DEBUG "bt848_mem: 0x%08x.\n", btv->bt848_mem));
3718 if (btv->bt848_mem)
3719 iounmap(btv->bt848_mem);
3721 if(btv->video_dev.minor!=-1)
3722 video_unregister_device(&btv->video_dev);
3723 if(btv->vbi_dev.minor!=-1)
3724 video_unregister_device(&btv->vbi_dev);
3725 if (radio[btv->nr] && btv->radio_dev.minor != -1)
3726 video_unregister_device(&btv->radio_dev);
3730 #ifdef MODULE
3732 int init_module(void)
3734 #else
3735 int init_bttv_cards(struct video_init *unused)
3737 #endif
3738 int i;
3740 handle_chipset();
3741 if (find_bt848()<0)
3742 return -EIO;
3744 /* initialize Bt848s */
3745 for (i=0; i<bttv_num; i++)
3747 if (init_bt848(i)<0)
3749 release_bttv();
3750 return -EIO;
3754 return 0;
3759 #ifdef MODULE
3761 void cleanup_module(void)
3763 release_bttv();
3766 #endif
3769 * Local variables:
3770 * c-indent-level: 8
3771 * c-brace-imaginary-offset: 0
3772 * c-brace-offset: -8
3773 * c-argdecl-indent: 8
3774 * c-label-offset: -8
3775 * c-continued-statement-offset: 8
3776 * c-continued-brace-offset: 0
3777 * indent-tabs-mode: nil
3778 * tab-width: 8
3779 * End: