Import 2.3.4pre3
[davej-history.git] / drivers / char / bttv.c
blob2c500e24820a7f4864219392b709844920f82bf3
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
124 #define I2C_SET(CTRL,DATA) \
125 { btwrite((CTRL<<1)|(DATA), BT848_I2C); udelay(I2C_DELAY); }
126 #define I2C_GET() (btread(BT848_I2C)&1)
128 #define EEPROM_WRITE_DELAY 20000
129 #define BURSTOFFSET 76
133 /*******************************/
134 /* Memory management functions */
135 /*******************************/
137 /* convert virtual user memory address to physical address */
138 /* (virt_to_phys only works for kmalloced kernel memory) */
140 static inline unsigned long uvirt_to_phys(unsigned long adr)
142 pgd_t *pgd;
143 pmd_t *pmd;
144 pte_t *ptep, pte;
146 pgd = pgd_offset(current->mm, adr);
147 if (pgd_none(*pgd))
148 return 0;
149 pmd = pmd_offset(pgd, adr);
150 if (pmd_none(*pmd))
151 return 0;
152 ptep = pte_offset(pmd, adr/*&(~PGDIR_MASK)*/);
153 pte = *ptep;
154 if(pte_present(pte))
155 return
156 virt_to_phys((void *)(pte_page(pte)|(adr&(PAGE_SIZE-1))));
157 return 0;
160 static inline unsigned long uvirt_to_bus(unsigned long adr)
162 return virt_to_bus(phys_to_virt(uvirt_to_phys(adr)));
165 /* convert virtual kernel memory address to physical address */
166 /* (virt_to_phys only works for kmalloced kernel memory) */
168 static inline unsigned long kvirt_to_phys(unsigned long adr)
170 return uvirt_to_phys(VMALLOC_VMADDR(adr));
173 static inline unsigned long kvirt_to_bus(unsigned long adr)
175 return uvirt_to_bus(VMALLOC_VMADDR(adr));
178 static void * rvmalloc(unsigned long size)
180 void * mem;
181 unsigned long adr, page;
183 mem=vmalloc(size);
184 if (mem)
186 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
187 adr=(unsigned long) mem;
188 while (size > 0)
190 page = kvirt_to_phys(adr);
191 mem_map_reserve(MAP_NR(phys_to_virt(page)));
192 adr+=PAGE_SIZE;
193 size-=PAGE_SIZE;
196 return mem;
199 static void rvfree(void * mem, unsigned long size)
201 unsigned long adr, page;
203 if (mem)
205 adr=(unsigned long) mem;
206 while (size > 0)
208 page = kvirt_to_phys(adr);
209 mem_map_unreserve(MAP_NR(phys_to_virt(page)));
210 adr+=PAGE_SIZE;
211 size-=PAGE_SIZE;
213 vfree(mem);
220 * Create the giant waste of buffer space we need for now
221 * until we get DMA to user space sorted out (probably 2.3.x)
223 * We only create this as and when someone uses mmap
226 static int fbuffer_alloc(struct bttv *btv)
228 if(!btv->fbuffer)
229 btv->fbuffer=(unsigned char *) rvmalloc(2*BTTV_MAX_FBUF);
230 else
231 printk(KERN_ERR "bttv%d: Double alloc of fbuffer!\n",
232 btv->nr);
233 if(!btv->fbuffer)
234 return -ENOBUFS;
235 return 0;
239 /* ----------------------------------------------------------------------- */
240 /* I2C functions */
242 /* software I2C functions */
244 static void i2c_setlines(struct i2c_bus *bus,int ctrl,int data)
246 struct bttv *btv = (struct bttv*)bus->data;
247 btwrite((ctrl<<1)|data, BT848_I2C);
248 btread(BT848_I2C); /* flush buffers */
249 udelay(I2C_DELAY);
252 static int i2c_getdataline(struct i2c_bus *bus)
254 struct bttv *btv = (struct bttv*)bus->data;
255 return btread(BT848_I2C)&1;
258 /* hardware I2C functions */
260 /* read I2C */
261 static int I2CRead(struct i2c_bus *bus, unsigned char addr)
263 u32 i;
264 u32 stat;
265 struct bttv *btv = (struct bttv*)bus->data;
267 /* clear status bit ; BT848_INT_RACK is ro */
268 btwrite(BT848_INT_I2CDONE, BT848_INT_STAT);
270 btwrite(((addr & 0xff) << 24) | btv->i2c_command, BT848_I2C);
273 * Timeout for I2CRead is 1 second (this should be enough, really!)
275 for (i=1000; i; i--)
277 stat=btread(BT848_INT_STAT);
278 if (stat & BT848_INT_I2CDONE)
279 break;
280 mdelay(1);
283 if (!i)
285 printk(KERN_DEBUG "bttv%d: I2CRead timeout\n",
286 btv->nr);
287 return -1;
289 if (!(stat & BT848_INT_RACK))
290 return -2;
292 i=(btread(BT848_I2C)>>8)&0xff;
293 return i;
296 /* set both to write both bytes, reset it to write only b1 */
298 static int I2CWrite(struct i2c_bus *bus, unsigned char addr, unsigned char b1,
299 unsigned char b2, int both)
301 u32 i;
302 u32 data;
303 u32 stat;
304 struct bttv *btv = (struct bttv*)bus->data;
306 /* clear status bit; BT848_INT_RACK is ro */
307 btwrite(BT848_INT_I2CDONE, BT848_INT_STAT);
309 data=((addr & 0xff) << 24) | ((b1 & 0xff) << 16) | btv->i2c_command;
310 if (both)
312 data|=((b2 & 0xff) << 8);
313 data|=BT848_I2C_W3B;
316 btwrite(data, BT848_I2C);
318 for (i=0x1000; i; i--)
320 stat=btread(BT848_INT_STAT);
321 if (stat & BT848_INT_I2CDONE)
322 break;
323 mdelay(1);
326 if (!i)
328 printk(KERN_DEBUG "bttv%d: I2CWrite timeout\n",
329 btv->nr);
330 return -1;
332 if (!(stat & BT848_INT_RACK))
333 return -2;
335 return 0;
338 /* read EEPROM */
339 static void readee(struct i2c_bus *bus, unsigned char *eedata)
341 int i, k;
343 if (I2CWrite(bus, 0xa0, 0, -1, 0)<0)
345 printk(KERN_WARNING "bttv: readee error\n");
346 return;
349 for (i=0; i<256; i++)
351 k=I2CRead(bus, 0xa1);
352 if (k<0)
354 printk(KERN_WARNING "bttv: readee error\n");
355 break;
357 eedata[i]=k;
361 /* write EEPROM */
362 static void writeee(struct i2c_bus *bus, unsigned char *eedata)
364 int i;
366 for (i=0; i<256; i++)
368 if (I2CWrite(bus, 0xa0, i, eedata[i], 1)<0)
370 printk(KERN_WARNING "bttv: writeee error (%d)\n", i);
371 break;
373 udelay(EEPROM_WRITE_DELAY);
377 void attach_inform(struct i2c_bus *bus, int id)
379 struct bttv *btv = (struct bttv*)bus->data;
381 switch (id)
383 case I2C_DRIVERID_MSP3400:
384 btv->have_msp3400 = 1;
385 break;
386 case I2C_DRIVERID_TUNER:
387 btv->have_tuner = 1;
388 if (btv->tuner_type != -1)
389 i2c_control_device(&(btv->i2c),
390 I2C_DRIVERID_TUNER,
391 TUNER_SET_TYPE,&btv->tuner_type);
392 break;
396 void detach_inform(struct i2c_bus *bus, int id)
398 struct bttv *btv = (struct bttv*)bus->data;
400 switch (id)
402 case I2C_DRIVERID_MSP3400:
403 btv->have_msp3400 = 0;
404 break;
405 case I2C_DRIVERID_TUNER:
406 btv->have_tuner = 0;
407 break;
411 static struct i2c_bus bttv_i2c_bus_template =
413 "bt848",
414 I2C_BUSID_BT848,
415 NULL,
417 #if LINUX_VERSION_CODE >= 0x020100
418 SPIN_LOCK_UNLOCKED,
419 #endif
421 attach_inform,
422 detach_inform,
424 i2c_setlines,
425 i2c_getdataline,
426 I2CRead,
427 I2CWrite,
430 /* ----------------------------------------------------------------------- */
431 /* some hauppauge specific stuff */
433 static unsigned char eeprom_data[256];
434 static struct HAUPPAUGE_TUNER
436 int id;
437 char *name;
439 hauppauge_tuner[] =
441 { TUNER_ABSENT, "" },
442 { TUNER_ABSENT, "External" },
443 { TUNER_ABSENT, "Unspecified" },
444 { TUNER_ABSENT, "Philips FI1216" },
445 { TUNER_ABSENT, "Philips FI1216MF" },
446 { TUNER_PHILIPS_NTSC, "Philips FI1236" },
447 { TUNER_ABSENT, "Philips FI1246" },
448 { TUNER_ABSENT, "Philips FI1256" },
449 { TUNER_PHILIPS_PAL, "Philips FI1216 MK2" },
450 { TUNER_PHILIPS_SECAM, "Philips FI1216MF MK2" },
451 { TUNER_PHILIPS_NTSC, "Philips FI1236 MK2" },
452 { TUNER_PHILIPS_PAL_I, "Philips FI1246 MK2" },
453 { TUNER_ABSENT, "Philips FI1256 MK2" },
454 { TUNER_ABSENT, "Temic 4032FY5" },
455 { TUNER_TEMIC_PAL, "Temic 4002FH5" },
456 { TUNER_TEMIC_PAL_I, "Temic 4062FY5" },
457 { TUNER_ABSENT, "Philips FR1216 MK2" },
458 { TUNER_PHILIPS_SECAM, "Philips FR1216MF MK2" },
459 { TUNER_PHILIPS_NTSC, "Philips FR1236 MK2" },
460 { TUNER_PHILIPS_PAL_I, "Philips FR1246 MK2" },
461 { TUNER_ABSENT, "Philips FR1256 MK2" },
462 { TUNER_PHILIPS_PAL, "Philips FM1216" },
463 { TUNER_ABSENT, "Philips FM1216MF" },
464 { TUNER_PHILIPS_NTSC, "Philips FM1236" },
467 static void
468 hauppauge_eeprom(struct i2c_bus *bus)
470 struct bttv *btv = (struct bttv*)bus->data;
472 readee(bus, eeprom_data);
473 if (eeprom_data[9] < sizeof(hauppauge_tuner)/sizeof(struct HAUPPAUGE_TUNER))
475 btv->tuner_type = hauppauge_tuner[eeprom_data[9]].id;
476 printk("bttv%d: Hauppauge eeprom: tuner=%s (%d)\n",btv->nr,
477 hauppauge_tuner[eeprom_data[9]].name,btv->tuner_type);
481 static void
482 hauppauge_msp_reset(struct bttv *btv)
484 /* Reset the MSP on some Hauppauge cards */
485 /* Thanks to Kyösti Mälkki (kmalkki@cc.hut.fi)! */
486 /* Can this hurt cards without one? What about Miros with MSP? */
487 btaor(32, ~32, BT848_GPIO_OUT_EN);
488 btaor(0, ~32, BT848_GPIO_DATA);
489 udelay(2500);
490 btaor(32, ~32, BT848_GPIO_DATA);
491 /* btaor(0, ~32, BT848_GPIO_OUT_EN); */
494 /* ----------------------------------------------------------------------- */
497 struct tvcard
499 int video_inputs;
500 int audio_inputs;
501 int tuner;
502 int svhs;
503 u32 gpiomask;
504 u32 muxsel[8];
505 u32 audiomux[6]; /* Tuner, Radio, internal, external, mute, stereo */
506 u32 gpiomask2; /* GPIO MUX mask */
509 static struct tvcard tvcards[] =
511 /* default */
512 { 3, 1, 0, 2, 0, { 2, 3, 1, 1}, { 0, 0, 0, 0, 0}},
513 /* MIRO */
514 { 4, 1, 0, 2,15, { 2, 3, 1, 1}, { 2, 0, 0, 0,10}},
515 /* Hauppauge */
516 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4}},
517 /* STB */
518 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 4, 0, 2, 3, 1}},
519 /* Intel??? */
520 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4}},
521 /* Diamond DTV2000 */
522 { 3, 1, 0, 2, 3, { 2, 3, 1, 1}, { 0, 1, 0, 1, 3}},
523 /* AVerMedia TVPhone */
524 { 3, 1, 0, 3,15, { 2, 3, 1, 1}, {12, 0,11,11, 0}},
525 /* Matrix Vision MV-Delta */
526 { 5, 1, -1, 3, 0, { 2, 3, 1, 0, 0}},
527 /* Fly Video II */
528 { 3, 1, 0, 2, 0xc00, { 2, 3, 1, 1},
529 {0, 0xc00, 0x800, 0x400, 0xc00, 0}},
530 /* TurboTV */
531 { 3, 1, 0, 2, 3, { 2, 3, 1, 1}, { 1, 1, 2, 3, 0}},
532 /* Newer Hauppauge (bt878) */
533 { 3, 1, 0, 2, 7, { 2, 0, 1, 1}, { 0, 1, 2, 3, 4}},
534 /* MIRO PCTV pro */
535 { 3, 1, 0, 2, 65551, { 2, 3, 1, 1}, {1,65537, 0, 0,10}},
536 /* ADS Technologies Channel Surfer TV (and maybe TV+FM) */
537 { 3, 4, 0, 2, 15, { 2, 3, 1, 1}, { 13, 14, 11, 7, 0, 0}, 0},
538 /* AVerMedia TVCapture 98 */
539 { 3, 4, 0, 2, 15, { 2, 3, 1, 1}, { 13, 14, 11, 7, 0, 0}, 0},
540 /* Aimslab VHX */
541 { 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4}},
542 /* Zoltrix TV-Max */
543 { 3, 1, 0, 2,15, { 2, 3, 1, 1}, { 0, 0, 0, 0, 0}},
544 /* Pixelview PlayTV (bt878) */
545 { 3, 4, 0, 2, 0x01e000, { 2, 0, 1, 1}, {0x01c000, 0, 0x018000, 0x014000, 0x002000, 0 }},
546 /* "Leadtek WinView 601", */
547 { 3, 1, 0, 2, 0x8300f8, { 2, 3, 1, 1,0}, {0x4fa007,0xcfa007,0xcfa007,0xcfa007,0xcfa007,0xcfa007}},
549 #define TVCARDS (sizeof(tvcards)/sizeof(tvcard))
551 static void audio(struct bttv *btv, int mode)
553 btaor(tvcards[btv->type].gpiomask, ~tvcards[btv->type].gpiomask,
554 BT848_GPIO_OUT_EN);
556 switch (mode)
558 case AUDIO_MUTE:
559 btv->audio|=AUDIO_MUTE;
560 break;
561 case AUDIO_UNMUTE:
562 btv->audio&=~AUDIO_MUTE;
563 mode=btv->audio;
564 break;
565 case AUDIO_OFF:
566 mode=AUDIO_OFF;
567 break;
568 case AUDIO_ON:
569 mode=btv->audio;
570 break;
571 default:
572 btv->audio&=AUDIO_MUTE;
573 btv->audio|=mode;
574 break;
576 /* if audio mute or not in H-lock, turn audio off */
577 if ((btv->audio&AUDIO_MUTE)
578 #if 0
580 (!btv->radio && !(btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC))
581 #endif
583 mode=AUDIO_OFF;
584 if ((mode == 0) && (btv->radio))
585 mode = 1;
586 btaor(tvcards[btv->type].audiomux[mode],
587 ~tvcards[btv->type].gpiomask, BT848_GPIO_DATA);
591 extern inline void bt848_dma(struct bttv *btv, uint state)
593 if (state)
594 btor(3, BT848_GPIO_DMA_CTL);
595 else
596 btand(~3, BT848_GPIO_DMA_CTL);
600 static void bt848_cap(struct bttv *btv, uint state)
602 if (state)
604 btv->cap|=3;
605 bt848_set_risc_jmps(btv);
607 else
609 btv->cap&=~3;
610 bt848_set_risc_jmps(btv);
615 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC*/
617 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
618 PLL_X = Reference pre-divider (0=1, 1=2)
619 PLL_C = Post divider (0=6, 1=4)
620 PLL_I = Integer input
621 PLL_F = Fractional input
623 F_input = 28.636363 MHz:
624 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
627 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
629 unsigned char fl, fh, fi;
631 /* prevent overflows */
632 fin/=4;
633 fout/=4;
635 fout*=12;
636 fi=fout/fin;
638 fout=(fout%fin)*256;
639 fh=fout/fin;
641 fout=(fout%fin)*256;
642 fl=fout/fin;
644 /*printk("0x%02x 0x%02x 0x%02x\n", fi, fh, fl);*/
645 btwrite(fl, BT848_PLL_F_LO);
646 btwrite(fh, BT848_PLL_F_HI);
647 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
650 static int set_pll(struct bttv *btv)
652 int i;
653 unsigned long tv;
655 if (!btv->pll.pll_crystal)
656 return 0;
658 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
659 /* no PLL needed */
660 if (btv->pll.pll_current == 0) {
661 /* printk ("bttv%d: PLL: is off\n",btv->nr); */
662 return 0;
664 printk ("bttv%d: PLL: switching off\n",btv->nr);
665 btwrite(0x00,BT848_TGCTRL);
666 btwrite(0x00,BT848_PLL_XCI);
667 btv->pll.pll_current = 0;
668 return 0;
671 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
672 /* printk("bttv%d: PLL: no change required\n",btv->nr); */
673 return 1;
676 printk("bttv%d: PLL: %d => %d ... ",btv->nr,
677 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
679 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
681 /* Let other people run while the PLL stabilizes */
682 tv=jiffies+HZ/10; /* .1 seconds */
685 schedule();
687 while(time_before(jiffies,tv));
689 for (i=0; i<10; i++)
691 if ((btread(BT848_DSTATUS)&BT848_DSTATUS_PLOCK))
692 btwrite(0,BT848_DSTATUS);
693 else
695 btwrite(0x08,BT848_TGCTRL);
696 btv->pll.pll_current = btv->pll.pll_ofreq;
697 printk("ok\n");
698 return 1;
700 mdelay(10);
702 btv->pll.pll_current = 0;
703 printk("oops\n");
704 return -1;
707 static void bt848_muxsel(struct bttv *btv, unsigned int input)
709 btaor(tvcards[btv->type].gpiomask2,~tvcards[btv->type].gpiomask2,
710 BT848_GPIO_OUT_EN);
712 /* This seems to get rid of some synchronization problems */
713 btand(~(3<<5), BT848_IFORM);
714 mdelay(10);
717 input %= tvcards[btv->type].video_inputs;
718 if (input==tvcards[btv->type].svhs)
720 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
721 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
723 else
725 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
726 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
728 btaor((tvcards[btv->type].muxsel[input&7]&3)<<5, ~(3<<5), BT848_IFORM);
729 audio(btv, (input!=tvcards[btv->type].tuner) ?
730 AUDIO_EXTERN : AUDIO_TUNER);
731 btaor(tvcards[btv->type].muxsel[input]>>4,
732 ~tvcards[btv->type].gpiomask2, BT848_GPIO_DATA);
736 * Set the registers for the size we have specified. Don't bother
737 * trying to understand this without the BT848 manual in front of
738 * you [AC].
740 * PS: The manual is free for download in .pdf format from
741 * www.brooktree.com - nicely done those folks.
744 struct tvnorm
746 u32 Fsc;
747 u16 swidth, sheight; /* scaled standard width, height */
748 u16 totalwidth;
749 u8 adelay, bdelay, iform;
750 u32 scaledtwidth;
751 u16 hdelayx1, hactivex1;
752 u16 vdelay;
753 u8 vbipack;
756 static struct tvnorm tvnorms[] = {
757 /* PAL-BDGHI */
758 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
759 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
760 #ifdef VIDEODAT
761 { 35468950,
762 924, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
763 1135, 186, 924, 0x20, 255},
764 #else
765 { 35468950,
766 924, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
767 1135, 186, 924, 0x20, 255},
768 #endif
770 { 35468950,
771 768, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
772 944, 186, 922, 0x20, 255},
774 /* NTSC */
775 { 28636363,
776 768, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC|BT848_IFORM_XT0),
777 910, 128, 910, 0x1a, 144},
779 { 28636363,
780 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC|BT848_IFORM_XT0),
781 780, 122, 754, 0x1a, 144},
783 #if 0
784 /* SECAM EAST */
785 { 35468950,
786 768, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM|BT848_IFORM_XT1),
787 944, 186, 922, 0x20, 255},
788 #else
789 /* SECAM L */
790 { 35468950,
791 924, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM|BT848_IFORM_XT1),
792 1135, 186, 922, 0x20, 255},
793 #endif
794 /* PAL-NC */
795 { 28636363,
796 640, 576, 910, 0x68, 0x5d, (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
797 780, 130, 734, 0x1a, 144},
798 /* PAL-M */
799 { 28636363,
800 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
801 780, 135, 754, 0x1a, 144},
802 /* PAL-N */
803 { 35468950,
804 768, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
805 944, 186, 922, 0x20, 144},
806 /* NTSC-Japan */
807 { 28636363,
808 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
809 780, 135, 754, 0x16, 144},
811 #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
812 #define VBI_SPL 2044
814 /* RISC command to write one VBI data line */
815 #define VBI_RISC BT848_RISC_WRITE|VBI_SPL|BT848_RISC_EOL|BT848_RISC_SOL
817 static void make_vbitab(struct bttv *btv)
819 int i;
820 unsigned int *po=(unsigned int *) btv->vbi_odd;
821 unsigned int *pe=(unsigned int *) btv->vbi_even;
823 DEBUG(printk(KERN_DEBUG "vbiodd: 0x%08x\n",(int)btv->vbi_odd));
824 DEBUG(printk(KERN_DEBUG "vbievn: 0x%08x\n",(int)btv->vbi_even));
825 DEBUG(printk(KERN_DEBUG "po: 0x%08x\n",(int)po));
826 DEBUG(printk(KERN_DEBUG "pe: 0x%08x\n",(int)pe));
828 *(po++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(po++)=0;
829 for (i=0; i<16; i++)
831 *(po++)=VBI_RISC;
832 *(po++)=kvirt_to_bus((unsigned long)btv->vbibuf+i*2048);
834 *(po++)=BT848_RISC_JUMP;
835 *(po++)=virt_to_bus(btv->risc_jmp+4);
837 *(pe++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(pe++)=0;
838 for (i=16; i<32; i++)
840 *(pe++)=VBI_RISC;
841 *(pe++)=kvirt_to_bus((unsigned long)btv->vbibuf+i*2048);
843 *(pe++)=BT848_RISC_JUMP|BT848_RISC_IRQ|(0x01<<16);
844 *(pe++)=virt_to_bus(btv->risc_jmp+10);
845 DEBUG(printk(KERN_DEBUG "po: 0x%08x\n",(int)po));
846 DEBUG(printk(KERN_DEBUG "pe: 0x%08x\n",(int)pe));
849 int fmtbppx2[16] = {
850 8, 6, 4, 4, 4, 3, 2, 2, 4, 3, 0, 0, 0, 0, 2, 0
853 int palette2fmt[] = {
855 BT848_COLOR_FMT_Y8,
856 BT848_COLOR_FMT_RGB8,
857 BT848_COLOR_FMT_RGB16,
858 BT848_COLOR_FMT_RGB24,
859 BT848_COLOR_FMT_RGB32,
860 BT848_COLOR_FMT_RGB15,
861 BT848_COLOR_FMT_YUY2,
862 BT848_COLOR_FMT_BtYUV,
866 BT848_COLOR_FMT_RAW,
867 BT848_COLOR_FMT_YCrCb422,
868 BT848_COLOR_FMT_YCrCb411,
869 BT848_COLOR_FMT_YCrCb422,
870 BT848_COLOR_FMT_YCrCb411,
872 #define PALETTEFMT_MAX (sizeof(palette2fmt)/sizeof(int))
874 static int make_rawrisctab(struct bttv *btv, unsigned int *ro,
875 unsigned int *re, unsigned int *vbuf)
877 unsigned long line;
878 unsigned long bpl=1024; /* bytes per line */
879 unsigned long vadr=(unsigned long) vbuf;
881 *(ro++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(ro++)=0;
882 *(re++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(re++)=0;
884 /* In PAL 650 blocks of 256 DWORDs are sampled, but only if VDELAY
885 is 2 and without separate VBI grabbing.
886 We'll have to handle this inside the IRQ handler ... */
888 for (line=0; line < 640; line++)
890 *(ro++)=BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL;
891 *(ro++)=kvirt_to_bus(vadr);
892 *(re++)=BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL;
893 *(re++)=kvirt_to_bus(vadr+BTTV_MAX_FBUF/2);
894 vadr+=bpl;
897 *(ro++)=BT848_RISC_JUMP;
898 *(ro++)=btv->bus_vbi_even;
899 *(re++)=BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16);
900 *(re++)=btv->bus_vbi_odd;
902 return 0;
906 static int make_prisctab(struct bttv *btv, unsigned int *ro,
907 unsigned int *re,
908 unsigned int *vbuf, unsigned short width,
909 unsigned short height, unsigned short fmt)
911 unsigned long line, lmask;
912 unsigned long bl, blcr, blcb, rcmd;
913 unsigned long todo;
914 unsigned int **rp;
915 int inter;
916 unsigned long cbadr, cradr;
917 unsigned long vadr=(unsigned long) vbuf;
918 int shift, csize;
921 switch(fmt)
923 case VIDEO_PALETTE_YUV422P:
924 csize=(width*height)>>1;
925 shift=1;
926 lmask=0;
927 break;
929 case VIDEO_PALETTE_YUV411P:
930 csize=(width*height)>>2;
931 shift=2;
932 lmask=0;
933 break;
935 case VIDEO_PALETTE_YUV420P:
936 csize=(width*height)>>2;
937 shift=1;
938 lmask=1;
939 break;
941 case VIDEO_PALETTE_YUV410P:
942 csize=(width*height)>>4;
943 shift=2;
944 lmask=3;
945 break;
947 default:
948 return -1;
950 cbadr=vadr+(width*height);
951 cradr=cbadr+csize;
952 inter = (height>btv->win.cropheight/2) ? 1 : 0;
954 *(ro++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3; *(ro++)=0;
955 *(re++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3; *(re++)=0;
957 for (line=0; line < (height<<(1^inter)); line++)
959 if(line==height)
961 vadr+=csize<<1;
962 cbadr=vadr+(width*height);
963 cradr=cbadr+csize;
965 if (inter)
966 rp= (line&1) ? &re : &ro;
967 else
968 rp= (line>=height) ? &re : &ro;
971 if(line&lmask)
972 rcmd=BT848_RISC_WRITE1S23|BT848_RISC_SOL;
973 else
974 rcmd=BT848_RISC_WRITE123|BT848_RISC_SOL;
976 todo=width;
977 while(todo)
979 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
980 blcr=(PAGE_SIZE-((PAGE_SIZE-1)&cradr))<<shift;
981 blcb=(PAGE_SIZE-((PAGE_SIZE-1)&cbadr))<<shift;
982 bl=(blcr<bl) ? blcr : bl;
983 bl=(blcb<bl) ? blcb : bl;
984 bl=(bl>todo) ? todo : bl;
985 blcr=bl>>shift;
986 blcb=blcr;
987 /* bl now containts the longest row that can be written */
988 todo-=bl;
989 if(!todo) rcmd|=BT848_RISC_EOL; /* if this is the last EOL */
991 *((*rp)++)=rcmd|bl;
992 *((*rp)++)=blcb|(blcr<<16);
993 *((*rp)++)=kvirt_to_bus(vadr);
994 vadr+=bl;
995 if((rcmd&(15<<28))==BT848_RISC_WRITE123)
997 *((*rp)++)=kvirt_to_bus(cbadr);
998 cbadr+=blcb;
999 *((*rp)++)=kvirt_to_bus(cradr);
1000 cradr+=blcr;
1003 rcmd&=~BT848_RISC_SOL; /* only the first has SOL */
1007 *(ro++)=BT848_RISC_JUMP;
1008 *(ro++)=btv->bus_vbi_even;
1009 *(re++)=BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16);
1010 *(re++)=btv->bus_vbi_odd;
1012 return 0;
1015 static int make_vrisctab(struct bttv *btv, unsigned int *ro,
1016 unsigned int *re,
1017 unsigned int *vbuf, unsigned short width,
1018 unsigned short height, unsigned short palette)
1020 unsigned long line;
1021 unsigned long bpl; /* bytes per line */
1022 unsigned long bl;
1023 unsigned long todo;
1024 unsigned int **rp;
1025 int inter;
1026 unsigned long vadr=(unsigned long) vbuf;
1028 if (palette==VIDEO_PALETTE_RAW)
1029 return make_rawrisctab(btv, ro, re, vbuf);
1030 if (palette>=VIDEO_PALETTE_PLANAR)
1031 return make_prisctab(btv, ro, re, vbuf, width, height, palette);
1034 inter = (height>btv->win.cropheight/2) ? 1 : 0;
1035 bpl=width*fmtbppx2[palette2fmt[palette]&0xf]/2;
1037 *(ro++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(ro++)=0;
1038 *(re++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(re++)=0;
1040 for (line=0; line < (height<<(1^inter)); line++)
1042 if (inter)
1043 rp= (line&1) ? &re : &ro;
1044 else
1045 rp= (line>=height) ? &re : &ro;
1047 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
1048 if (bpl<=bl)
1050 *((*rp)++)=BT848_RISC_WRITE|BT848_RISC_SOL|
1051 BT848_RISC_EOL|bpl;
1052 *((*rp)++)=kvirt_to_bus(vadr);
1053 vadr+=bpl;
1055 else
1057 todo=bpl;
1058 *((*rp)++)=BT848_RISC_WRITE|BT848_RISC_SOL|bl;
1059 *((*rp)++)=kvirt_to_bus(vadr);
1060 vadr+=bl;
1061 todo-=bl;
1062 while (todo>PAGE_SIZE)
1064 *((*rp)++)=BT848_RISC_WRITE|PAGE_SIZE;
1065 *((*rp)++)=kvirt_to_bus(vadr);
1066 vadr+=PAGE_SIZE;
1067 todo-=PAGE_SIZE;
1069 *((*rp)++)=BT848_RISC_WRITE|BT848_RISC_EOL|todo;
1070 *((*rp)++)=kvirt_to_bus(vadr);
1071 vadr+=todo;
1075 *(ro++)=BT848_RISC_JUMP;
1076 *(ro++)=btv->bus_vbi_even;
1077 *(re++)=BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16);
1078 *(re++)=btv->bus_vbi_odd;
1080 return 0;
1083 static unsigned char lmaskt[8] =
1084 { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
1085 static unsigned char rmaskt[8] =
1086 { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1088 static void clip_draw_rectangle(unsigned char *clipmap, int x, int y, int w, int h)
1090 unsigned char lmask, rmask, *p;
1091 int W, l, r;
1092 int i;
1093 /* bitmap is fixed width, 128 bytes (1024 pixels represented) */
1094 if (x<0)
1096 w+=x;
1097 x=0;
1099 if (y<0)
1101 h+=y;
1102 y=0;
1104 if (w < 0 || h < 0) /* catch bad clips */
1105 return;
1106 /* out of range data should just fall through */
1107 if (y+h>=625)
1108 h=625-y;
1109 if (x+w>=1024)
1110 w=1024-x;
1112 l=x>>3;
1113 r=(x+w)>>3;
1114 W=r-l-1;
1115 lmask=lmaskt[x&7];
1116 rmask=rmaskt[(x+w)&7];
1117 p=clipmap+128*y+l;
1119 if (W>0)
1121 for (i=0; i<h; i++, p+=128)
1123 *p|=lmask;
1124 memset(p+1, 0xff, W);
1125 p[W+1]|=rmask;
1127 } else if (!W) {
1128 for (i=0; i<h; i++, p+=128)
1130 p[0]|=lmask;
1131 p[1]|=rmask;
1133 } else {
1134 for (i=0; i<h; i++, p+=128)
1135 p[0]|=lmask&rmask;
1141 static void make_clip_tab(struct bttv *btv, struct video_clip *cr, int ncr)
1143 int i, line, x, y, bpl, width, height, inter;
1144 unsigned int bpp, dx, sx, **rp, *ro, *re, flags, len;
1145 unsigned long adr;
1146 unsigned char *clipmap, cbit, lastbit, outofmem;
1148 inter=(btv->win.interlace&1)^1;
1149 bpp=btv->win.bpp;
1150 if (bpp==15) /* handle 15bpp as 16bpp in calculations */
1151 bpp++;
1152 bpl=btv->win.bpl;
1153 ro=btv->risc_odd;
1154 re=btv->risc_even;
1155 if((width=btv->win.width)>1023)
1156 width = 1023; /* sanity check */
1157 if((height=btv->win.height)>625)
1158 height = 625; /* sanity check */
1159 adr=btv->win.vidadr+btv->win.x*bpp+btv->win.y*bpl;
1160 if ((clipmap=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL) {
1161 /* can't clip, don't generate any risc code */
1162 *(ro++)=BT848_RISC_JUMP;
1163 *(ro++)=btv->bus_vbi_even;
1164 *(re++)=BT848_RISC_JUMP;
1165 *(re++)=btv->bus_vbi_odd;
1167 if (ncr < 0) { /* bitmap was pased */
1168 memcpy(clipmap, (unsigned char *)cr, VIDEO_CLIPMAP_SIZE);
1169 } else { /* convert rectangular clips to a bitmap */
1170 memset(clipmap, 0, VIDEO_CLIPMAP_SIZE); /* clear map */
1171 for (i=0; i<ncr; i++)
1172 clip_draw_rectangle(clipmap, cr[i].x, cr[i].y,
1173 cr[i].width, cr[i].height);
1175 /* clip against viewing window AND screen
1176 so we do not have to rely on the user program
1178 clip_draw_rectangle(clipmap,(btv->win.x+width>btv->win.swidth) ?
1179 (btv->win.swidth-btv->win.x) : width, 0, 1024, 768);
1180 clip_draw_rectangle(clipmap,0,(btv->win.y+height>btv->win.sheight) ?
1181 (btv->win.sheight-btv->win.y) : height,1024,768);
1182 if (btv->win.x<0)
1183 clip_draw_rectangle(clipmap, 0, 0, -(btv->win.x), 768);
1184 if (btv->win.y<0)
1185 clip_draw_rectangle(clipmap, 0, 0, 1024, -(btv->win.y));
1187 *(ro++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(ro++)=0;
1188 *(re++)=BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1; *(re++)=0;
1190 /* translate bitmap to risc code */
1191 for (line=outofmem=0; line < (height<<inter) && !outofmem; line++)
1193 y = line>>inter;
1194 rp= (line&1) ? &re : &ro;
1195 lastbit=(clipmap[y<<7]&1);
1196 for(x=dx=1,sx=0; x<=width && !outofmem; x++) {
1197 cbit = (clipmap[(y<<7)+(x>>3)] & (1<<(x&7)));
1198 if (x < width && !lastbit == !cbit)
1199 dx++;
1200 else { /* generate the dma controller code */
1201 len = dx * bpp;
1202 flags = ((bpp==4) ? BT848_RISC_BYTE3 : 0);
1203 flags |= ((!sx) ? BT848_RISC_SOL : 0);
1204 flags |= ((sx + dx == width) ? BT848_RISC_EOL : 0);
1205 if (!lastbit) {
1206 *((*rp)++)=BT848_RISC_WRITE|flags|len;
1207 *((*rp)++)=adr + bpp * sx;
1208 } else
1209 *((*rp)++)=BT848_RISC_SKIP|flags|len;
1210 lastbit=cbit;
1211 sx += dx;
1212 dx = 1;
1213 if (ro - btv->risc_odd > RISCMEM_LEN/2 - 16)
1214 outofmem++;
1215 if (re - btv->risc_even > RISCMEM_LEN/2 - 16)
1216 outofmem++;
1219 if ((!inter)||(line&1))
1220 adr+=bpl;
1222 vfree(clipmap);
1223 /* outofmem flag relies on the following code to discard extra data */
1224 *(ro++)=BT848_RISC_JUMP;
1225 *(ro++)=btv->bus_vbi_even;
1226 *(re++)=BT848_RISC_JUMP;
1227 *(re++)=btv->bus_vbi_odd;
1230 /* set geometry for even/odd frames
1231 just if you are wondering:
1232 handling of even and odd frames will be separated, e.g. for grabbing
1233 the even ones as RGB into videomem and the others as YUV in main memory for
1234 compressing and sending to the video conferencing partner.
1237 static inline void bt848_set_eogeo(struct bttv *btv, int odd, u8 vtc,
1238 u16 hscale, u16 vscale,
1239 u16 hactive, u16 vactive,
1240 u16 hdelay, u16 vdelay,
1241 u8 crop)
1243 int off = odd ? 0x80 : 0x00;
1245 btwrite(vtc, BT848_E_VTC+off);
1246 btwrite(hscale>>8, BT848_E_HSCALE_HI+off);
1247 btwrite(hscale&0xff, BT848_E_HSCALE_LO+off);
1248 btaor((vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
1249 btwrite(vscale&0xff, BT848_E_VSCALE_LO+off);
1250 btwrite(hactive&0xff, BT848_E_HACTIVE_LO+off);
1251 btwrite(hdelay&0xff, BT848_E_HDELAY_LO+off);
1252 btwrite(vactive&0xff, BT848_E_VACTIVE_LO+off);
1253 btwrite(vdelay&0xff, BT848_E_VDELAY_LO+off);
1254 btwrite(crop, BT848_E_CROP+off);
1258 static void bt848_set_geo(struct bttv *btv, u16 width, u16 height, u16 fmt)
1260 u16 vscale, hscale;
1261 u32 xsf, sr;
1262 u16 hdelay, vdelay;
1263 u16 hactive, vactive;
1264 u16 inter;
1265 u8 crop, vtc;
1266 struct tvnorm *tvn;
1268 if (!width || !height)
1269 return;
1271 tvn=&tvnorms[btv->win.norm];
1273 btv->win.cropheight=tvn->sheight;
1274 btv->win.cropwidth=tvn->swidth;
1277 if (btv->win.cropwidth>tvn->cropwidth)
1278 btv->win.cropwidth=tvn->cropwidth;
1279 if (btv->win.cropheight>tvn->cropheight)
1280 btv->win.cropheight=tvn->cropheight;
1282 if (width>btv->win.cropwidth)
1283 width=btv->win.cropwidth;
1284 if (height>btv->win.cropheight)
1285 height=btv->win.cropheight;
1287 btwrite(tvn->adelay, BT848_ADELAY);
1288 btwrite(tvn->bdelay, BT848_BDELAY);
1289 btaor(tvn->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH), BT848_IFORM);
1290 btwrite(tvn->vbipack, BT848_VBI_PACK_SIZE);
1291 btwrite(1, BT848_VBI_PACK_DEL);
1293 btv->pll.pll_ofreq = tvn->Fsc;
1294 set_pll(btv);
1296 btwrite(fmt, BT848_COLOR_FMT);
1297 hactive=width;
1299 vtc=0;
1300 /* Some people say interpolation looks bad ... */
1301 /* vtc = (hactive < 193) ? 2 : ((hactive < 385) ? 1 : 0); */
1303 btv->win.interlace = (height>btv->win.cropheight/2) ? 1 : 0;
1304 inter=(btv->win.interlace&1)^1;
1305 vdelay=btv->win.cropy+tvn->vdelay;
1307 xsf = (hactive*tvn->scaledtwidth)/btv->win.cropwidth;
1308 hscale = ((tvn->totalwidth*4096UL)/xsf-4096);
1310 hdelay=tvn->hdelayx1+btv->win.cropx;
1311 hdelay=(hdelay*hactive)/btv->win.cropwidth;
1312 hdelay&=0x3fe;
1314 sr=((btv->win.cropheight>>inter)*512)/height-512;
1315 vscale=(0x10000UL-sr)&0x1fff;
1316 vactive=btv->win.cropheight;
1317 crop=((hactive>>8)&0x03)|((hdelay>>6)&0x0c)|
1318 ((vactive>>4)&0x30)|((vdelay>>2)&0xc0);
1319 vscale|= btv->win.interlace ? (BT848_VSCALE_INT<<8) : 0;
1321 bt848_set_eogeo(btv, 0, vtc, hscale, vscale, hactive, vactive,
1322 hdelay, vdelay, crop);
1323 bt848_set_eogeo(btv, 1, vtc, hscale, vscale, hactive, vactive,
1324 hdelay, vdelay, crop);
1328 int bpp2fmt[4] = {
1329 BT848_COLOR_FMT_RGB8, BT848_COLOR_FMT_RGB16,
1330 BT848_COLOR_FMT_RGB24, BT848_COLOR_FMT_RGB32
1333 static void bt848_set_winsize(struct bttv *btv)
1335 unsigned short format;
1337 btv->win.color_fmt = format =
1338 (btv->win.depth==15) ? BT848_COLOR_FMT_RGB15 :
1339 bpp2fmt[(btv->win.bpp-1)&3];
1341 /* RGB8 seems to be a 9x5x5 GRB color cube starting at
1342 * color 16. Why the h... can't they even mention this in the
1343 * data sheet? [AC - because it's a standard format so I guess
1344 * it never occurred to them]
1345 * Enable dithering in this mode.
1348 if (format==BT848_COLOR_FMT_RGB8)
1349 btand(~BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1350 else
1351 btor(BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1353 bt848_set_geo(btv, btv->win.width, btv->win.height, format);
1357 * Set TSA5522 synthesizer frequency in 1/16 Mhz steps
1360 static void set_freq(struct bttv *btv, unsigned short freq)
1362 int fixme = freq; /* XXX */
1364 /* mute */
1365 if (btv->have_msp3400)
1366 i2c_control_device(&(btv->i2c),I2C_DRIVERID_MSP3400,
1367 MSP_SWITCH_MUTE,0);
1369 /* switch channel */
1370 if (btv->have_tuner) {
1371 if (btv->radio) {
1372 i2c_control_device(&(btv->i2c), I2C_DRIVERID_TUNER,
1373 TUNER_SET_RADIOFREQ,&fixme);
1374 } else {
1375 i2c_control_device(&(btv->i2c), I2C_DRIVERID_TUNER,
1376 TUNER_SET_TVFREQ,&fixme);
1380 if (btv->have_msp3400) {
1381 if (btv->radio) {
1382 i2c_control_device(&(btv->i2c),I2C_DRIVERID_MSP3400,
1383 MSP_SET_RADIO,0);
1384 } else {
1385 i2c_control_device(&(btv->i2c),I2C_DRIVERID_MSP3400,
1386 MSP_SET_TVNORM,&(btv->win.norm));
1387 i2c_control_device(&(btv->i2c),I2C_DRIVERID_MSP3400,
1388 MSP_NEWCHANNEL,0);
1394 * Grab into virtual memory.
1395 * Currently only does double buffering. Do we need more?
1398 static int vgrab(struct bttv *btv, struct video_mmap *mp)
1400 unsigned int *ro, *re;
1401 unsigned int *vbuf;
1403 if(btv->fbuffer==NULL)
1405 if(fbuffer_alloc(btv))
1406 return -ENOBUFS;
1408 if(btv->grabbing >= MAX_GBUFFERS)
1409 return -ENOBUFS;
1412 * No grabbing past the end of the buffer!
1415 if(mp->frame>1 || mp->frame <0)
1416 return -EINVAL;
1418 if(mp->height <0 || mp->width <0)
1419 return -EINVAL;
1421 /* This doesn´t work like this for NTSC anyway.
1422 So, better check the total image size ...
1425 if(mp->height>576 || mp->width>768+BURSTOFFSET)
1426 return -EINVAL;
1428 if (mp->format >= PALETTEFMT_MAX)
1429 return -EINVAL;
1430 if (mp->height*mp->width*fmtbppx2[palette2fmt[mp->format]&0x0f]/2
1431 > BTTV_MAX_FBUF)
1432 return -EINVAL;
1433 if(-1 == palette2fmt[mp->format])
1434 return -EINVAL;
1437 * FIXME: Check the format of the grab here. This is probably
1438 * also less restrictive than the normal overlay grabs. Stuff
1439 * like QCIF has meaning as a capture.
1443 * Ok load up the BT848
1446 vbuf=(unsigned int *)(btv->fbuffer+BTTV_MAX_FBUF*mp->frame);
1447 /* if (!(btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC))
1448 return -EAGAIN;*/
1449 ro=btv->grisc+(((btv->grabcount++)&1) ? 4096 :0);
1450 re=ro+2048;
1451 make_vrisctab(btv, ro, re, vbuf, mp->width, mp->height, mp->format);
1452 /* bt848_set_risc_jmps(btv); */
1453 btv->frame_stat[mp->frame] = GBUFFER_GRABBING;
1454 if (btv->grabbing) {
1455 btv->gfmt_next=palette2fmt[mp->format];
1456 btv->gwidth_next=mp->width;
1457 btv->gheight_next=mp->height;
1458 btv->gro_next=virt_to_bus(ro);
1459 btv->gre_next=virt_to_bus(re);
1460 btv->grf_next=mp->frame;
1461 } else {
1462 btv->gfmt=palette2fmt[mp->format];
1463 btv->gwidth=mp->width;
1464 btv->gheight=mp->height;
1465 btv->gro=virt_to_bus(ro);
1466 btv->gre=virt_to_bus(re);
1467 btv->grf=mp->frame;
1469 if (!(btv->grabbing++)) {
1470 if(mp->format>=VIDEO_PALETTE_COMPONENT) {
1471 btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI);
1472 btor(BT848_VSCALE_COMB, BT848_O_VSCALE_HI);
1474 btv->risc_jmp[12]=BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ;
1476 btor(3, BT848_CAP_CTL);
1477 btor(3, BT848_GPIO_DMA_CTL);
1478 /* interruptible_sleep_on(&btv->capq); */
1479 return 0;
1482 static long bttv_write(struct video_device *v, const char *buf, unsigned long count, int nonblock)
1484 return -EINVAL;
1487 static long bttv_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
1489 struct bttv *btv= (struct bttv *)v;
1490 int q,todo;
1491 /* BROKEN: RETURNS VBI WHEN IT SHOULD RETURN GRABBED VIDEO FRAME */
1492 todo=count;
1493 while (todo && todo>(q=VBIBUF_SIZE-btv->vbip))
1495 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, q))
1496 return -EFAULT;
1497 todo-=q;
1498 buf+=q;
1500 cli();
1501 if (todo && q==VBIBUF_SIZE-btv->vbip)
1503 if(nonblock)
1505 sti();
1506 if(count==todo)
1507 return -EWOULDBLOCK;
1508 return count-todo;
1510 interruptible_sleep_on(&btv->vbiq);
1511 sti();
1512 if(signal_pending(current))
1514 if(todo==count)
1515 return -EINTR;
1516 else
1517 return count-todo;
1521 if (todo)
1523 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, todo))
1524 return -EFAULT;
1525 btv->vbip+=todo;
1527 return count;
1531 * Open a bttv card. Right now the flags stuff is just playing
1534 static int bttv_open(struct video_device *dev, int flags)
1536 struct bttv *btv = (struct bttv *)dev;
1537 int users, i;
1539 if (btv->user)
1540 return -EBUSY;
1541 audio(btv, AUDIO_UNMUTE);
1542 for (i=users=0; i<bttv_num; i++)
1543 users+=bttvs[i].user;
1544 if (users==1)
1545 find_vga();
1546 btv->fbuffer=NULL;
1547 if (!btv->fbuffer)
1548 btv->fbuffer=(unsigned char *) rvmalloc(2*BTTV_MAX_FBUF);
1549 if (!btv->fbuffer)
1550 return -EINVAL;
1551 btv->grabbing = 0;
1552 btv->grab = 0;
1553 btv->lastgrab = 0;
1554 for (i = 0; i < MAX_GBUFFERS; i++)
1555 btv->frame_stat[i] = GBUFFER_UNUSED;
1557 btv->user++;
1558 MOD_INC_USE_COUNT;
1559 return 0;
1562 static void bttv_close(struct video_device *dev)
1564 struct bttv *btv=(struct bttv *)dev;
1566 btv->user--;
1567 audio(btv, AUDIO_INTERN);
1568 btv->cap&=~3;
1569 bt848_set_risc_jmps(btv);
1572 * A word of warning. At this point the chip
1573 * is still capturing because its FIFO hasn't emptied
1574 * and the DMA control operations are posted PCI
1575 * operations.
1578 btread(BT848_I2C); /* This fixes the PCI posting delay */
1581 * This is sucky but right now I can't find a good way to
1582 * be sure its safe to free the buffer. We wait 5-6 fields
1583 * which is more than sufficient to be sure.
1586 current->state = TASK_UNINTERRUPTIBLE;
1587 schedule_timeout(HZ/10); /* Wait 1/10th of a second */
1590 * We have allowed it to drain.
1592 if(btv->fbuffer)
1593 rvfree((void *) btv->fbuffer, 2*BTTV_MAX_FBUF);
1594 btv->fbuffer=0;
1595 MOD_DEC_USE_COUNT;
1599 /***********************************/
1600 /* ioctls and supporting functions */
1601 /***********************************/
1603 extern inline void bt848_bright(struct bttv *btv, uint bright)
1605 btwrite(bright&0xff, BT848_BRIGHT);
1608 extern inline void bt848_hue(struct bttv *btv, uint hue)
1610 btwrite(hue&0xff, BT848_HUE);
1613 extern inline void bt848_contrast(struct bttv *btv, uint cont)
1615 unsigned int conthi;
1617 conthi=(cont>>6)&4;
1618 btwrite(cont&0xff, BT848_CONTRAST_LO);
1619 btaor(conthi, ~4, BT848_E_CONTROL);
1620 btaor(conthi, ~4, BT848_O_CONTROL);
1623 extern inline void bt848_sat_u(struct bttv *btv, unsigned long data)
1625 u32 datahi;
1627 datahi=(data>>7)&2;
1628 btwrite(data&0xff, BT848_SAT_U_LO);
1629 btaor(datahi, ~2, BT848_E_CONTROL);
1630 btaor(datahi, ~2, BT848_O_CONTROL);
1633 static inline void bt848_sat_v(struct bttv *btv, unsigned long data)
1635 u32 datahi;
1637 datahi=(data>>8)&1;
1638 btwrite(data&0xff, BT848_SAT_V_LO);
1639 btaor(datahi, ~1, BT848_E_CONTROL);
1640 btaor(datahi, ~1, BT848_O_CONTROL);
1644 * ioctl routine
1648 static int bttv_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
1650 unsigned char eedata[256];
1651 struct bttv *btv=(struct bttv *)dev;
1652 int i;
1654 switch (cmd)
1656 case VIDIOCGCAP:
1658 struct video_capability b;
1659 strcpy(b.name,btv->video_dev.name);
1660 b.type = VID_TYPE_CAPTURE|
1661 VID_TYPE_TUNER|
1662 VID_TYPE_TELETEXT|
1663 VID_TYPE_OVERLAY|
1664 VID_TYPE_CLIPPING|
1665 VID_TYPE_FRAMERAM|
1666 VID_TYPE_SCALES;
1667 b.channels = tvcards[btv->type].video_inputs;
1668 b.audios = tvcards[btv->type].audio_inputs;
1669 b.maxwidth = tvnorms[btv->win.norm].swidth;
1670 b.maxheight = tvnorms[btv->win.norm].sheight;
1671 b.minwidth = 32;
1672 b.minheight = 32;
1673 if(copy_to_user(arg,&b,sizeof(b)))
1674 return -EFAULT;
1675 return 0;
1677 case VIDIOCGCHAN:
1679 struct video_channel v;
1680 if(copy_from_user(&v, arg,sizeof(v)))
1681 return -EFAULT;
1682 v.flags=VIDEO_VC_AUDIO;
1683 v.tuners=0;
1684 v.type=VIDEO_TYPE_CAMERA;
1685 v.norm = btv->win.norm;
1686 if (v.channel>=tvcards[btv->type].video_inputs)
1687 return -EINVAL;
1688 if(v.channel==tvcards[btv->type].tuner)
1690 strcpy(v.name,"Television");
1691 v.flags|=VIDEO_VC_TUNER;
1692 v.type=VIDEO_TYPE_TV;
1693 v.tuners=1;
1695 else if(v.channel==tvcards[btv->type].svhs)
1696 strcpy(v.name,"S-Video");
1697 else
1698 sprintf(v.name,"Composite%d",v.channel);
1700 if(copy_to_user(arg,&v,sizeof(v)))
1701 return -EFAULT;
1702 return 0;
1705 * Each channel has 1 tuner
1707 case VIDIOCSCHAN:
1709 struct video_channel v;
1710 if(copy_from_user(&v, arg,sizeof(v)))
1711 return -EFAULT;
1713 if (v.channel>tvcards[btv->type].video_inputs)
1714 return -EINVAL;
1715 bt848_muxsel(btv, v.channel);
1716 if(v.norm!=VIDEO_MODE_PAL&&v.norm!=VIDEO_MODE_NTSC
1717 &&v.norm!=VIDEO_MODE_SECAM)
1718 return -EOPNOTSUPP;
1719 btv->win.norm = v.norm;
1720 make_vbitab(btv);
1721 bt848_set_winsize(btv);
1722 btv->channel=v.channel;
1723 return 0;
1725 case VIDIOCGTUNER:
1727 struct video_tuner v;
1728 if(copy_from_user(&v,arg,sizeof(v))!=0)
1729 return -EFAULT;
1730 if(v.tuner||btv->channel) /* Only tuner 0 */
1731 return -EINVAL;
1732 strcpy(v.name, "Television");
1733 v.rangelow=0;
1734 v.rangehigh=0xFFFFFFFF;
1735 v.flags=VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
1736 if (btv->audio_chip == TDA9840) {
1737 v.flags |= VIDEO_AUDIO_VOLUME;
1738 v.mode = VIDEO_SOUND_MONO|VIDEO_SOUND_STEREO;
1739 v.mode |= VIDEO_SOUND_LANG1|VIDEO_SOUND_LANG2;
1741 if (btv->audio_chip == TDA9850) {
1742 unsigned char ALR1;
1743 ALR1 = I2CRead(&(btv->i2c), I2C_TDA9850|1);
1744 if (ALR1 & 32)
1745 v.flags |= VIDEO_TUNER_STEREO_ON;
1747 v.mode = btv->win.norm;
1748 v.signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
1749 if(copy_to_user(arg,&v,sizeof(v)))
1750 return -EFAULT;
1751 return 0;
1753 /* We have but one tuner */
1754 case VIDIOCSTUNER:
1756 struct video_tuner v;
1757 if(copy_from_user(&v, arg, sizeof(v)))
1758 return -EFAULT;
1759 /* Only one channel has a tuner */
1760 if(v.tuner!=tvcards[btv->type].tuner)
1761 return -EINVAL;
1763 if(v.mode!=VIDEO_MODE_PAL&&v.mode!=VIDEO_MODE_NTSC
1764 &&v.mode!=VIDEO_MODE_SECAM)
1765 return -EOPNOTSUPP;
1766 btv->win.norm = v.mode;
1767 bt848_set_winsize(btv);
1768 return 0;
1770 case VIDIOCGPICT:
1772 struct video_picture p=btv->picture;
1773 if(btv->win.depth==8)
1774 p.palette=VIDEO_PALETTE_HI240;
1775 if(btv->win.depth==15)
1776 p.palette=VIDEO_PALETTE_RGB555;
1777 if(btv->win.depth==16)
1778 p.palette=VIDEO_PALETTE_RGB565;
1779 if(btv->win.depth==24)
1780 p.palette=VIDEO_PALETTE_RGB24;
1781 if(btv->win.depth==32)
1782 p.palette=VIDEO_PALETTE_RGB32;
1784 if(copy_to_user(arg, &p, sizeof(p)))
1785 return -EFAULT;
1786 return 0;
1788 case VIDIOCSPICT:
1790 struct video_picture p;
1791 int format;
1792 if(copy_from_user(&p, arg,sizeof(p)))
1793 return -EFAULT;
1794 /* We want -128 to 127 we get 0-65535 */
1795 bt848_bright(btv, (p.brightness>>8)-128);
1796 /* 0-511 for the colour */
1797 bt848_sat_u(btv, p.colour>>7);
1798 bt848_sat_v(btv, ((p.colour>>7)*201L)/237);
1799 /* -128 to 127 */
1800 bt848_hue(btv, (p.hue>>8)-128);
1801 /* 0-511 */
1802 bt848_contrast(btv, p.contrast>>7);
1803 btv->picture = p;
1805 /* set palette if bpp matches */
1806 if (p.palette < sizeof(palette2fmt)/sizeof(int)) {
1807 format = palette2fmt[p.palette];
1808 if (fmtbppx2[format&0x0f]/2 == btv->win.bpp)
1809 btv->win.color_fmt = format;
1811 return 0;
1813 case VIDIOCSWIN:
1815 struct video_window vw;
1816 struct video_clip *vcp = NULL;
1817 int on;
1819 if(copy_from_user(&vw,arg,sizeof(vw)))
1820 return -EFAULT;
1822 if(vw.flags || vw.width < 16 || vw.height < 16)
1824 bt848_cap(btv,0);
1825 return -EINVAL;
1827 if (btv->win.bpp < 4)
1828 { /* 32-bit align start and adjust width */
1829 int i = vw.x;
1830 vw.x = (vw.x + 3) & ~3;
1831 i = vw.x - i;
1832 vw.width -= i;
1834 btv->win.x=vw.x;
1835 btv->win.y=vw.y;
1836 btv->win.width=vw.width;
1837 btv->win.height=vw.height;
1839 if(btv->win.height>btv->win.cropheight/2)
1840 btv->win.interlace=1;
1841 else
1842 btv->win.interlace=0;
1844 on=(btv->cap&3);
1846 bt848_cap(btv,0);
1847 bt848_set_winsize(btv);
1850 * Do any clips.
1852 if(vw.clipcount<0) {
1853 if((vcp=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL)
1854 return -ENOMEM;
1855 if(copy_from_user(vcp, vw.clips,
1856 VIDEO_CLIPMAP_SIZE)) {
1857 vfree(vcp);
1858 return -EFAULT;
1860 } else if (vw.clipcount) {
1861 if((vcp=vmalloc(sizeof(struct video_clip)*
1862 (vw.clipcount))) == NULL)
1863 return -ENOMEM;
1864 if(copy_from_user(vcp,vw.clips,
1865 sizeof(struct video_clip)*
1866 vw.clipcount)) {
1867 vfree(vcp);
1868 return -EFAULT;
1871 make_clip_tab(btv, vcp, vw.clipcount);
1872 if (vw.clipcount != 0)
1873 vfree(vcp);
1874 if(on && btv->win.vidadr!=0)
1875 bt848_cap(btv,1);
1876 return 0;
1878 case VIDIOCGWIN:
1880 struct video_window vw;
1881 /* Oh for a COBOL move corresponding .. */
1882 vw.x=btv->win.x;
1883 vw.y=btv->win.y;
1884 vw.width=btv->win.width;
1885 vw.height=btv->win.height;
1886 vw.chromakey=0;
1887 vw.flags=0;
1888 if(btv->win.interlace)
1889 vw.flags|=VIDEO_WINDOW_INTERLACE;
1890 if(copy_to_user(arg,&vw,sizeof(vw)))
1891 return -EFAULT;
1892 return 0;
1894 case VIDIOCCAPTURE:
1896 int v;
1897 if(copy_from_user(&v, arg,sizeof(v)))
1898 return -EFAULT;
1899 if(v==0)
1901 bt848_cap(btv,0);
1903 else
1905 if(btv->win.vidadr==0 || btv->win.width==0
1906 || btv->win.height==0)
1907 return -EINVAL;
1908 bt848_cap(btv,1);
1910 return 0;
1912 case VIDIOCGFBUF:
1914 struct video_buffer v;
1915 v.base=(void *)btv->win.vidadr;
1916 v.height=btv->win.sheight;
1917 v.width=btv->win.swidth;
1918 v.depth=btv->win.depth;
1919 v.bytesperline=btv->win.bpl;
1920 if(copy_to_user(arg, &v,sizeof(v)))
1921 return -EFAULT;
1922 return 0;
1925 case VIDIOCSFBUF:
1927 struct video_buffer v;
1928 #if LINUX_VERSION_CODE >= 0x020100
1929 if(!capable(CAP_SYS_ADMIN))
1930 #else
1931 if(!suser())
1932 #endif
1933 return -EPERM;
1934 if(copy_from_user(&v, arg,sizeof(v)))
1935 return -EFAULT;
1936 if(v.depth!=8 && v.depth!=15 && v.depth!=16 &&
1937 v.depth!=24 && v.depth!=32 && v.width > 16 &&
1938 v.height > 16 && v.bytesperline > 16)
1939 return -EINVAL;
1940 if (v.base)
1942 if ((unsigned long)v.base&1)
1943 btv->win.vidadr=(unsigned long)(PAGE_OFFSET|uvirt_to_bus((unsigned long)v.base));
1944 else
1945 btv->win.vidadr=(unsigned long)v.base;
1947 btv->win.sheight=v.height;
1948 btv->win.swidth=v.width;
1949 btv->win.bpp=((v.depth+7)&0x38)/8;
1950 btv->win.depth=v.depth;
1951 btv->win.bpl=v.bytesperline;
1953 DEBUG(printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1954 v.base, v.width,v.height, btv->win.bpp, btv->win.bpl));
1955 bt848_set_winsize(btv);
1956 return 0;
1958 case VIDIOCKEY:
1960 /* Will be handled higher up .. */
1961 return 0;
1963 case VIDIOCGFREQ:
1965 unsigned long v=btv->win.freq;
1966 if(copy_to_user(arg,&v,sizeof(v)))
1967 return -EFAULT;
1968 return 0;
1970 case VIDIOCSFREQ:
1972 unsigned long v;
1973 if(copy_from_user(&v, arg, sizeof(v)))
1974 return -EFAULT;
1975 btv->win.freq=v;
1976 set_freq(btv, btv->win.freq);
1977 return 0;
1980 case VIDIOCGAUDIO:
1982 struct video_audio v;
1983 v=btv->audio_dev;
1984 v.flags&=~(VIDEO_AUDIO_MUTE|VIDEO_AUDIO_MUTABLE);
1985 v.flags|=VIDEO_AUDIO_MUTABLE;
1986 strcpy(v.name,"TV");
1987 if (btv->audio_chip == TDA9850) {
1988 unsigned char ALR1;
1989 ALR1 = I2CRead(&(btv->i2c), I2C_TDA9850|1);
1990 v.mode = VIDEO_SOUND_MONO;
1991 v.mode |= (ALR1 & 32) ? VIDEO_SOUND_STEREO:0;
1992 v.mode |= (ALR1 & 64) ? VIDEO_SOUND_LANG1:0;
1994 if (btv->have_msp3400)
1996 v.flags|=VIDEO_AUDIO_VOLUME |
1997 VIDEO_AUDIO_BASS |
1998 VIDEO_AUDIO_TREBLE;
1999 i2c_control_device(&(btv->i2c),
2000 I2C_DRIVERID_MSP3400,
2001 MSP_GET_VOLUME,&(v.volume));
2002 i2c_control_device(&(btv->i2c),
2003 I2C_DRIVERID_MSP3400,
2004 MSP_GET_BASS,&(v.bass));
2005 i2c_control_device(&(btv->i2c),
2006 I2C_DRIVERID_MSP3400,
2007 MSP_GET_TREBLE,&(v.treble));
2008 i2c_control_device(&(btv->i2c),
2009 I2C_DRIVERID_MSP3400,
2010 MSP_GET_STEREO,&(v.mode));
2012 else v.mode = VIDEO_SOUND_MONO;
2013 if(copy_to_user(arg,&v,sizeof(v)))
2014 return -EFAULT;
2015 return 0;
2017 case VIDIOCSAUDIO:
2019 struct video_audio v;
2020 if(copy_from_user(&v,arg, sizeof(v)))
2021 return -EFAULT;
2022 if(v.flags&VIDEO_AUDIO_MUTE)
2023 audio(btv, AUDIO_MUTE);
2024 /* One audio source per tuner */
2025 /* if(v.audio!=0) */
2026 /* ADSTech TV card has more than one */
2027 if(v.audio<0 || v.audio >= tvcards[btv->type].audio_inputs)
2028 return -EINVAL;
2029 bt848_muxsel(btv,v.audio);
2030 if(!(v.flags&VIDEO_AUDIO_MUTE))
2031 audio(btv, AUDIO_UNMUTE);
2032 if (btv->audio_chip == TDA9850) {
2033 unsigned char con3 = 0;
2034 if (v.mode & VIDEO_SOUND_LANG1)
2035 con3 = 0x80; /* sap */
2036 if (v.mode & VIDEO_SOUND_STEREO)
2037 con3 = 0x40; /* stereo */
2038 I2CWrite(&(btv->i2c), I2C_TDA9850,
2039 TDA9850_CON3, con3, 1);
2042 /* PT2254A programming Jon Tombs, jon@gte.esi.us.es */
2043 if (btv->type == BTTV_WINVIEW_601) {
2044 int bits_out, loops, vol, data;
2046 /* 32 levels logarithmic */
2047 vol = 32 - ((v.volume>>11));
2048 /* units */
2049 bits_out = (PT2254_DBS_IN_2>>(vol%5));
2050 /* tens */
2051 bits_out |= (PT2254_DBS_IN_10>>(vol/5));
2052 bits_out |= PT2254_L_CHANEL | PT2254_R_CHANEL;
2053 data = btread(BT848_GPIO_DATA);
2054 data &= ~(WINVIEW_PT2254_CLK| WINVIEW_PT2254_DATA|
2055 WINVIEW_PT2254_STROBE);
2056 for (loops = 17; loops >= 0 ; loops--) {
2057 if (bits_out & (1<<loops))
2058 data |= WINVIEW_PT2254_DATA;
2059 else
2060 data &= ~WINVIEW_PT2254_DATA;
2061 btwrite(data, BT848_GPIO_DATA);
2062 udelay(5);
2063 data |= WINVIEW_PT2254_CLK;
2064 btwrite(data, BT848_GPIO_DATA);
2065 udelay(5);
2066 data &= ~WINVIEW_PT2254_CLK;
2067 btwrite(data, BT848_GPIO_DATA);
2069 data |= WINVIEW_PT2254_STROBE;
2070 data &= ~WINVIEW_PT2254_DATA;
2071 btwrite(data, BT848_GPIO_DATA);
2072 udelay(10);
2073 data &= ~WINVIEW_PT2254_STROBE;
2074 btwrite(data, BT848_GPIO_DATA);
2076 if (btv->have_msp3400)
2078 i2c_control_device(&(btv->i2c),
2079 I2C_DRIVERID_MSP3400,
2080 MSP_SET_VOLUME,&(v.volume));
2081 i2c_control_device(&(btv->i2c),
2082 I2C_DRIVERID_MSP3400,
2083 MSP_SET_BASS,&(v.bass));
2084 i2c_control_device(&(btv->i2c),
2085 I2C_DRIVERID_MSP3400,
2086 MSP_SET_TREBLE,&(v.treble));
2087 i2c_control_device(&(btv->i2c),
2088 I2C_DRIVERID_MSP3400,
2089 MSP_SET_STEREO,&(v.mode));
2091 btv->audio_dev=v;
2092 return 0;
2095 case VIDIOCSYNC:
2096 if(copy_from_user((void *)&i,arg,sizeof(int)))
2097 return -EFAULT;
2098 /* if(i>1 || i<0)
2099 return -EINVAL;
2101 switch (btv->frame_stat[i]) {
2102 case GBUFFER_UNUSED:
2103 return -EINVAL;
2104 case GBUFFER_GRABBING:
2105 while(btv->frame_stat[i]==GBUFFER_GRABBING) {
2106 interruptible_sleep_on(&btv->capq);
2107 if(signal_pending(current))
2108 return -EINTR;
2110 /* fall */
2111 case GBUFFER_DONE:
2112 btv->frame_stat[i] = GBUFFER_UNUSED;
2113 break;
2115 return 0;
2117 case BTTV_WRITEE:
2118 #if LINUX_VERSION_CODE >= 0x020100
2119 if(!capable(CAP_SYS_ADMIN))
2120 #else
2121 if(!suser())
2122 #endif
2123 return -EPERM;
2124 if(copy_from_user((void *) eedata, (void *) arg, 256))
2125 return -EFAULT;
2126 writeee(&(btv->i2c), eedata);
2127 return 0;
2129 case BTTV_READEE:
2130 #if LINUX_VERSION_CODE >= 0x020100
2131 if(!capable(CAP_SYS_ADMIN))
2132 #else
2133 if(!suser())
2134 #endif
2135 return -EPERM;
2136 readee(&(btv->i2c), eedata);
2137 if(copy_to_user((void *) arg, (void *) eedata, 256))
2138 return -EFAULT;
2139 break;
2141 case BTTV_FIELDNR:
2142 if(copy_to_user((void *) arg, (void *) &btv->last_field,
2143 sizeof(btv->last_field)))
2144 return -EFAULT;
2145 break;
2147 case BTTV_PLLSET: {
2148 struct bttv_pll_info p;
2149 #if LINUX_VERSION_CODE >= 0x020100
2150 if(!capable(CAP_SYS_ADMIN))
2151 #else
2152 if(!suser())
2153 #endif
2154 return -EPERM;
2155 if(copy_from_user(&p , (void *) arg, sizeof(btv->pll)))
2156 return -EFAULT;
2157 btv->pll.pll_ifreq = p.pll_ifreq;
2158 btv->pll.pll_ofreq = p.pll_ofreq;
2159 btv->pll.pll_crystal = p.pll_crystal;
2161 break;
2163 case VIDIOCMCAPTURE:
2165 struct video_mmap vm;
2166 if(copy_from_user((void *) &vm, (void *) arg, sizeof(vm)))
2167 return -EFAULT;
2168 if (btv->frame_stat[vm.frame] == GBUFFER_GRABBING)
2169 return -EBUSY;
2170 return vgrab(btv, &vm);
2173 case VIDIOCGMBUF:
2175 struct video_mbuf vm;
2176 memset(&vm, 0 , sizeof(vm));
2177 vm.size=BTTV_MAX_FBUF*2;
2178 vm.frames=2;
2179 vm.offsets[0]=0;
2180 vm.offsets[1]=BTTV_MAX_FBUF;
2181 if(copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
2182 return -EFAULT;
2183 return 0;
2186 case VIDIOCGUNIT:
2188 struct video_unit vu;
2189 vu.video=btv->video_dev.minor;
2190 vu.vbi=btv->vbi_dev.minor;
2191 if(btv->radio_dev.minor!=-1)
2192 vu.radio=btv->radio_dev.minor;
2193 else
2194 vu.radio=VIDEO_NO_UNIT;
2195 vu.audio=VIDEO_NO_UNIT;
2196 if(btv->have_msp3400)
2198 i2c_control_device(&(btv->i2c), I2C_DRIVERID_MSP3400,
2199 MSP_GET_UNIT, &vu.audio);
2201 vu.teletext=VIDEO_NO_UNIT;
2202 if(copy_to_user((void *)arg, (void *)&vu, sizeof(vu)))
2203 return -EFAULT;
2204 return 0;
2207 case BTTV_BURST_ON:
2209 tvnorms[0].scaledtwidth=1135-BURSTOFFSET-2;
2210 tvnorms[0].hdelayx1=186-BURSTOFFSET;
2211 return 0;
2214 case BTTV_BURST_OFF:
2216 tvnorms[0].scaledtwidth=1135;
2217 tvnorms[0].hdelayx1=186;
2218 return 0;
2221 case BTTV_VERSION:
2223 return BTTV_VERSION_CODE;
2226 case BTTV_PICNR:
2228 /* return picture;*/
2229 return 0;
2232 default:
2233 return -ENOIOCTLCMD;
2235 return 0;
2238 static int bttv_init_done(struct video_device *dev)
2240 return 0;
2244 * This maps the vmalloced and reserved fbuffer to user space.
2246 * FIXME:
2247 * - PAGE_READONLY should suffice!?
2248 * - remap_page_range is kind of inefficient for page by page remapping.
2249 * But e.g. pte_alloc() does not work in modules ... :-(
2252 static int bttv_mmap(struct video_device *dev, const char *adr, unsigned long size)
2254 struct bttv *btv=(struct bttv *)dev;
2255 unsigned long start=(unsigned long) adr;
2256 unsigned long page,pos;
2258 if (size>2*BTTV_MAX_FBUF)
2259 return -EINVAL;
2260 if (!btv->fbuffer)
2262 if(fbuffer_alloc(btv))
2263 return -EINVAL;
2265 pos=(unsigned long) btv->fbuffer;
2266 while (size > 0)
2268 page = kvirt_to_phys(pos);
2269 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
2270 return -EAGAIN;
2271 start+=PAGE_SIZE;
2272 pos+=PAGE_SIZE;
2273 size-=PAGE_SIZE;
2275 return 0;
2278 static struct video_device bttv_template=
2280 "UNSET",
2281 VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY|VID_TYPE_TELETEXT,
2282 VID_HARDWARE_BT848,
2283 bttv_open,
2284 bttv_close,
2285 bttv_read,
2286 bttv_write,
2287 #if LINUX_VERSION_CODE >= 0x020100
2288 NULL, /* poll */
2289 #endif
2290 bttv_ioctl,
2291 bttv_mmap,
2292 bttv_init_done,
2293 NULL,
2299 static long vbi_read(struct video_device *v, char *buf, unsigned long count,
2300 int nonblock)
2302 struct bttv *btv=(struct bttv *)(v-2);
2303 int q,todo;
2305 todo=count;
2306 while (todo && todo>(q=VBIBUF_SIZE-btv->vbip))
2308 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, q))
2309 return -EFAULT;
2310 todo-=q;
2311 buf+=q;
2313 cli();
2314 if (todo && q==VBIBUF_SIZE-btv->vbip)
2316 if(nonblock)
2318 sti();
2319 if(count==todo)
2320 return -EWOULDBLOCK;
2321 return count-todo;
2323 interruptible_sleep_on(&btv->vbiq);
2324 sti();
2325 if(signal_pending(current))
2327 if(todo==count)
2328 return -EINTR;
2329 else
2330 return count-todo;
2334 if (todo)
2336 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, todo))
2337 return -EFAULT;
2338 btv->vbip+=todo;
2340 return count;
2343 #if LINUX_VERSION_CODE >= 0x020100
2344 static unsigned int vbi_poll(struct video_device *dev, struct file *file,
2345 poll_table *wait)
2347 struct bttv *btv=(struct bttv *)(dev-2);
2348 unsigned int mask = 0;
2350 poll_wait(file, &btv->vbiq, wait);
2352 if (btv->vbip < VBIBUF_SIZE)
2353 mask |= (POLLIN | POLLRDNORM);
2355 return mask;
2357 #endif
2359 static int vbi_open(struct video_device *dev, int flags)
2361 struct bttv *btv=(struct bttv *)(dev-2);
2363 btv->vbip=VBIBUF_SIZE;
2364 btv->cap|=0x0c;
2365 bt848_set_risc_jmps(btv);
2367 MOD_INC_USE_COUNT;
2368 return 0;
2371 static void vbi_close(struct video_device *dev)
2373 struct bttv *btv=(struct bttv *)(dev-2);
2375 btv->cap&=~0x0c;
2376 bt848_set_risc_jmps(btv);
2378 MOD_DEC_USE_COUNT;
2382 static int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2384 return -EINVAL;
2387 static struct video_device vbi_template=
2389 "bttv vbi",
2390 VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
2391 VID_HARDWARE_BT848,
2392 vbi_open,
2393 vbi_close,
2394 vbi_read,
2395 bttv_write,
2396 #if LINUX_VERSION_CODE >= 0x020100
2397 vbi_poll,
2398 #endif
2399 vbi_ioctl,
2400 NULL, /* no mmap yet */
2401 bttv_init_done,
2402 NULL,
2408 static int radio_open(struct video_device *dev, int flags)
2410 struct bttv *btv = (struct bttv *)(dev-1);
2412 if (btv->user)
2413 return -EBUSY;
2414 btv->user++;
2415 set_freq(btv,400*16);
2416 btv->radio = 1;
2417 bt848_muxsel(btv,0);
2418 audio(btv, AUDIO_UNMUTE);
2420 MOD_INC_USE_COUNT;
2421 return 0;
2424 static void radio_close(struct video_device *dev)
2426 struct bttv *btv=(struct bttv *)(dev-1);
2428 btv->user--;
2429 btv->radio = 0;
2430 /*audio(btv, AUDIO_MUTE);*/
2431 MOD_DEC_USE_COUNT;
2434 static long radio_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
2436 return -EINVAL;
2439 static int radio_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2441 struct bttv *btv=(struct bttv *)(dev-1);
2442 switch (cmd) {
2443 case VIDIOCGCAP:
2445 struct video_capability v;
2446 strcpy(v.name,btv->video_dev.name);
2447 v.type = VID_TYPE_TUNER;
2448 v.channels = 1;
2449 v.audios = 1;
2450 /* No we don't do pictures */
2451 v.maxwidth = 0;
2452 v.maxheight = 0;
2453 v.minwidth = 0;
2454 v.minheight = 0;
2455 if (copy_to_user(arg, &v, sizeof(v)))
2456 return -EFAULT;
2457 return 0;
2458 break;
2460 case VIDIOCGTUNER:
2462 struct video_tuner v;
2463 if(copy_from_user(&v,arg,sizeof(v))!=0)
2464 return -EFAULT;
2465 if(v.tuner||btv->channel) /* Only tuner 0 */
2466 return -EINVAL;
2467 strcpy(v.name, "Radio");
2468 v.rangelow=(int)(87.5*16);
2469 v.rangehigh=(int)(108*16);
2470 v.flags= 0; /* XXX */
2471 v.mode = 0; /* XXX */
2472 if(copy_to_user(arg,&v,sizeof(v)))
2473 return -EFAULT;
2474 return 0;
2476 case VIDIOCSTUNER:
2478 struct video_tuner v;
2479 if(copy_from_user(&v, arg, sizeof(v)))
2480 return -EFAULT;
2481 /* Only channel 0 has a tuner */
2482 if(v.tuner!=0 || btv->channel)
2483 return -EINVAL;
2484 /* XXX anything to do ??? */
2485 return 0;
2487 case VIDIOCGFREQ:
2488 case VIDIOCSFREQ:
2489 case VIDIOCGAUDIO:
2490 case VIDIOCSAUDIO:
2491 bttv_ioctl((struct video_device *)btv,cmd,arg);
2492 break;
2493 default:
2494 return -ENOIOCTLCMD;
2496 return 0;
2499 static struct video_device radio_template=
2501 "bttv radio",
2502 VID_TYPE_TUNER,
2503 VID_HARDWARE_BT848,
2504 radio_open,
2505 radio_close,
2506 radio_read, /* just returns -EINVAL */
2507 bttv_write, /* just returns -EINVAL */
2508 #if LINUX_VERSION_CODE >= 0x020100
2509 NULL, /* no poll */
2510 #endif
2511 radio_ioctl,
2512 NULL, /* no mmap */
2513 bttv_init_done, /* just returns 0 */
2514 NULL,
2520 struct vidbases
2522 unsigned short vendor, device;
2523 char *name;
2524 uint badr;
2527 static struct vidbases vbs[] = {
2528 { PCI_VENDOR_ID_ALLIANCE, PCI_DEVICE_ID_ALLIANCE_AT3D,
2529 "Alliance AT3D", PCI_BASE_ADDRESS_0},
2530 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_215CT222,
2531 "ATI MACH64 CT", PCI_BASE_ADDRESS_0},
2532 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_210888GX,
2533 "ATI MACH64 Winturbo", PCI_BASE_ADDRESS_0},
2534 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_215GT,
2535 "ATI MACH64 GT", PCI_BASE_ADDRESS_0},
2536 { PCI_VENDOR_ID_CIRRUS, 0, "Cirrus Logic", PCI_BASE_ADDRESS_0},
2537 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TGA,
2538 "DEC DC21030", PCI_BASE_ADDRESS_0},
2539 { PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL,
2540 "Matrox Millennium", PCI_BASE_ADDRESS_1},
2541 { PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL_2,
2542 "Matrox Millennium II", PCI_BASE_ADDRESS_0},
2543 { PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL_2_AGP,
2544 "Matrox Millennium II AGP", PCI_BASE_ADDRESS_0},
2545 { PCI_VENDOR_ID_MATROX, 0x051a, "Matrox Mystique", PCI_BASE_ADDRESS_1},
2546 { PCI_VENDOR_ID_MATROX, 0x0521, "Matrox G200", PCI_BASE_ADDRESS_0},
2547 { PCI_VENDOR_ID_N9, PCI_DEVICE_ID_N9_I128,
2548 "Number Nine Imagine 128", PCI_BASE_ADDRESS_0},
2549 { PCI_VENDOR_ID_N9, PCI_DEVICE_ID_N9_I128_2,
2550 "Number Nine Imagine 128 Series 2", PCI_BASE_ADDRESS_0},
2551 { PCI_VENDOR_ID_S3, 0, "S3", PCI_BASE_ADDRESS_0},
2552 { PCI_VENDOR_ID_TSENG, 0, "TSENG", PCI_BASE_ADDRESS_0},
2553 { PCI_VENDOR_ID_NVIDIA_SGS, PCI_DEVICE_ID_NVIDIA_SGS_RIVA128,
2554 "Riva128", PCI_BASE_ADDRESS_1},
2558 /* DEC TGA offsets stolen from XFree-3.2 */
2560 static uint dec_offsets[4] = {
2561 0x200000,
2562 0x804000,
2564 0x1004000
2567 #define NR_CARDS (sizeof(vbs)/sizeof(struct vidbases))
2569 /* Scan for PCI display adapter
2570 if more than one card is present the last one is used for now */
2572 #if LINUX_VERSION_CODE >= 0x020100
2574 static int find_vga(void)
2576 unsigned short badr;
2577 int found = 0, i, tga_type;
2578 unsigned int vidadr=0;
2579 struct pci_dev *dev;
2582 for (dev = pci_devices; dev != NULL; dev = dev->next)
2584 if (dev->class != PCI_CLASS_NOT_DEFINED_VGA &&
2585 ((dev->class) >> 16 != PCI_BASE_CLASS_DISPLAY))
2587 continue;
2589 if (PCI_FUNC(dev->devfn) != 0)
2590 continue;
2592 badr=0;
2593 printk(KERN_INFO "bttv: PCI display adapter: ");
2594 for (i=0; i<NR_CARDS; i++)
2596 if (dev->vendor == vbs[i].vendor)
2598 if (vbs[i].device)
2599 if (vbs[i].device!=dev->device)
2600 continue;
2601 printk("%s.\n", vbs[i].name);
2602 badr=vbs[i].badr;
2603 break;
2606 if (!badr)
2608 printk(KERN_ERR "bttv: Unknown video memory base address.\n");
2609 continue;
2611 pci_read_config_dword(dev, badr, &vidadr);
2612 if (vidadr & PCI_BASE_ADDRESS_SPACE_IO)
2614 printk(KERN_ERR "bttv: Memory seems to be I/O memory.\n");
2615 printk(KERN_ERR "bttv: Check entry for your card type in bttv.c vidbases struct.\n");
2616 continue;
2618 vidadr &= PCI_BASE_ADDRESS_MEM_MASK;
2619 if (!vidadr)
2621 printk(KERN_ERR "bttv: Memory @ 0, must be something wrong!");
2622 continue;
2625 if (dev->vendor == PCI_VENDOR_ID_DEC &&
2626 dev->device == PCI_DEVICE_ID_DEC_TGA)
2628 tga_type = (readl((unsigned long)vidadr) >> 12) & 0x0f;
2629 if (tga_type != 0 && tga_type != 1 && tga_type != 3)
2631 printk(KERN_ERR "bttv: TGA type (0x%x) unrecognized!\n", tga_type);
2632 found--;
2634 vidadr+=dec_offsets[tga_type];
2636 DEBUG(printk(KERN_DEBUG "bttv: memory @ 0x%08x, ", vidadr));
2637 DEBUG(printk(KERN_DEBUG "devfn: 0x%04x.\n", dev->devfn));
2638 found++;
2641 if (vidmem)
2643 vidadr=vidmem<<20;
2644 printk(KERN_INFO "bttv: Video memory override: 0x%08x\n", vidadr);
2645 found=1;
2647 for (i=0; i<BTTV_MAX; i++)
2648 bttvs[i].win.vidadr=vidadr;
2650 return found;
2653 #else
2654 static int find_vga(void)
2656 unsigned int devfn, class, vendev;
2657 unsigned short vendor, device, badr;
2658 int found=0, bus=0, i, tga_type;
2659 unsigned int vidadr=0;
2662 for (devfn = 0; devfn < 0xff; devfn++)
2664 if (PCI_FUNC(devfn) != 0)
2665 continue;
2666 pcibios_read_config_dword(bus, devfn, PCI_VENDOR_ID, &vendev);
2667 if (vendev == 0xffffffff || vendev == 0x00000000)
2668 continue;
2669 pcibios_read_config_word(bus, devfn, PCI_VENDOR_ID, &vendor);
2670 pcibios_read_config_word(bus, devfn, PCI_DEVICE_ID, &device);
2671 pcibios_read_config_dword(bus, devfn, PCI_CLASS_REVISION, &class);
2672 class = class >> 16;
2673 /* if (class == PCI_CLASS_DISPLAY_VGA) {*/
2674 if ((class>>8) == PCI_BASE_CLASS_DISPLAY ||
2675 /* Number 9 GXE64Pro needs this */
2676 class == PCI_CLASS_NOT_DEFINED_VGA)
2678 badr=0;
2679 printk(KERN_INFO "bttv: PCI display adapter: ");
2680 for (i=0; i<NR_CARDS; i++)
2682 if (vendor==vbs[i].vendor)
2684 if (vbs[i].device)
2685 if (vbs[i].device!=device)
2686 continue;
2687 printk("%s.\n", vbs[i].name);
2688 badr=vbs[i].badr;
2689 break;
2692 if (NR_CARDS == i)
2693 printk("UNKNOWN.\n");
2694 if (!badr)
2696 printk(KERN_ERR "bttv: Unknown video memory base address.\n");
2697 continue;
2699 pcibios_read_config_dword(bus, devfn, badr, &vidadr);
2700 if (vidadr & PCI_BASE_ADDRESS_SPACE_IO)
2702 printk(KERN_ERR "bttv: Memory seems to be I/O memory.\n");
2703 printk(KERN_ERR "bttv: Check entry for your card type in bttv.c vidbases struct.\n");
2704 continue;
2706 vidadr &= PCI_BASE_ADDRESS_MEM_MASK;
2707 if (!vidadr)
2709 printk(KERN_ERR "bttv: Memory @ 0, must be something wrong!\n");
2710 continue;
2713 if (vendor==PCI_VENDOR_ID_DEC)
2714 if (device==PCI_DEVICE_ID_DEC_TGA)
2716 tga_type = (readl((unsigned long)vidadr) >> 12) & 0x0f;
2717 if (tga_type != 0 && tga_type != 1 && tga_type != 3)
2719 printk(KERN_ERR "bttv: TGA type (0x%x) unrecognized!\n", tga_type);
2720 found--;
2722 vidadr+=dec_offsets[tga_type];
2725 DEBUG(printk(KERN_DEBUG "bttv: memory @ 0x%08x, ", vidadr));
2726 DEBUG(printk(KERN_DEBUG "devfn: 0x%04x.\n", devfn));
2727 found++;
2731 if (vidmem)
2733 if (vidmem < 0x1000)
2734 vidadr=vidmem<<20;
2735 else
2736 vidadr=vidmem;
2737 printk(KERN_INFO "bttv: Video memory override: 0x%08x\n", vidadr);
2738 found=1;
2740 for (i=0; i<BTTV_MAX; i++)
2741 bttvs[i].win.vidadr=vidadr;
2743 return found;
2745 #endif
2748 #define TRITON_PCON 0x50
2749 #define TRITON_BUS_CONCURRENCY (1<<0)
2750 #define TRITON_STREAMING (1<<1)
2751 #define TRITON_WRITE_BURST (1<<2)
2752 #define TRITON_PEER_CONCURRENCY (1<<3)
2755 #if LINUX_VERSION_CODE >= 0x020100
2757 static void handle_chipset(void)
2759 struct pci_dev *dev = NULL;
2761 /* Just in case some nut set this to something dangerous */
2762 if (triton1)
2763 triton1=BT848_INT_ETBF;
2765 while ((dev = pci_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_496, dev)))
2767 /* Beware the SiS 85C496 my friend - rev 49 don't work with a bttv */
2768 printk(KERN_WARNING "BT848 and SIS 85C496 chipset don't always work together.\n");
2771 /* dev == NULL */
2773 while ((dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441, dev)))
2775 unsigned char b;
2776 pci_read_config_byte(dev, 0x53, &b);
2777 DEBUG(printk(KERN_INFO "bttv: Host bridge: 82441FX Natoma, "));
2778 DEBUG(printk("bufcon=0x%02x\n",b));
2781 while ((dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437, dev)))
2783 /* unsigned char b;
2784 unsigned char bo;*/
2786 printk(KERN_INFO "bttv: Host bridge 82437FX Triton PIIX\n");
2787 triton1=BT848_INT_ETBF;
2789 #if 0
2790 /* The ETBF bit SHOULD make all this unnecessary */
2791 /* 430FX (Triton I) freezes with bus concurrency on -> switch it off */
2793 pci_read_config_byte(dev, TRITON_PCON, &b);
2794 bo=b;
2795 DEBUG(printk(KERN_DEBUG "bttv: 82437FX: PCON: 0x%x\n",b));
2796 if(!(b & TRITON_BUS_CONCURRENCY))
2798 printk(KERN_WARNING "bttv: 82437FX: disabling bus concurrency\n");
2799 b |= TRITON_BUS_CONCURRENCY;
2801 if(b & TRITON_PEER_CONCURRENCY)
2803 printk(KERN_WARNING "bttv: 82437FX: disabling peer concurrency\n");
2804 b &= ~TRITON_PEER_CONCURRENCY;
2806 if(!(b & TRITON_STREAMING))
2808 printk(KERN_WARNING "bttv: 82437FX: disabling streaming\n");
2809 b |= TRITON_STREAMING;
2812 if (b!=bo)
2814 pci_write_config_byte(dev, TRITON_PCON, b);
2815 printk(KERN_DEBUG "bttv: 82437FX: PCON changed to: 0x%x\n",b);
2817 #endif
2820 #else
2821 static void handle_chipset(void)
2823 int index;
2825 for (index = 0; index < 8; index++)
2827 unsigned char bus, devfn;
2828 unsigned char b;
2830 /* Beware the SiS 85C496 my friend - rev 49 don't work with a bttv */
2832 if (!pcibios_find_device(PCI_VENDOR_ID_SI,
2833 PCI_DEVICE_ID_SI_496,
2834 index, &bus, &devfn))
2836 printk(KERN_WARNING "BT848 and SIS 85C496 chipset don't always work together.\n");
2839 if (!pcibios_find_device(PCI_VENDOR_ID_INTEL,
2840 PCI_DEVICE_ID_INTEL_82441,
2841 index, &bus, &devfn))
2843 pcibios_read_config_byte(bus, devfn, 0x53, &b);
2844 DEBUG(printk(KERN_INFO "bttv: Host bridge: 82441FX Natoma, "));
2845 DEBUG(printk("bufcon=0x%02x\n",b));
2848 if (!pcibios_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437,
2849 index, &bus, &devfn))
2851 printk(KERN_INFO "bttv: Host bridge 82437FX Triton PIIX\n");
2852 triton1=BT848_INT_ETBF;
2854 #if 0
2855 /* The ETBF bit SHOULD make all this unnecessary */
2856 /* 430FX (Triton I) freezes with bus concurrency on -> switch it off */
2858 unsigned char bo;
2860 pcibios_read_config_byte(bus, devfn, TRITON_PCON, &b);
2861 bo=b;
2862 DEBUG(printk(KERN_DEBUG "bttv: 82437FX: PCON: 0x%x\n",b));
2864 if(!(b & TRITON_BUS_CONCURRENCY))
2866 printk(KERN_WARNING "bttv: 82437FX: disabling bus concurrency\n");
2867 b |= TRITON_BUS_CONCURRENCY;
2870 if(b & TRITON_PEER_CONCURRENCY)
2872 printk(KERN_WARNING "bttv: 82437FX: disabling peer concurrency\n");
2873 b &= ~TRITON_PEER_CONCURRENCY;
2875 if(!(b & TRITON_STREAMING))
2877 printk(KERN_WARNING "bttv: 82437FX: disabling streaming\n");
2878 b |= TRITON_STREAMING;
2881 if (b!=bo)
2883 pcibios_write_config_byte(bus, devfn, TRITON_PCON, b);
2884 printk(KERN_DEBUG "bttv: 82437FX: PCON changed to: 0x%x\n",b);
2887 #endif
2891 #endif
2893 static void init_tea6300(struct i2c_bus *bus)
2895 I2CWrite(bus, I2C_TEA6300, TEA6300_VL, 0x35, 1); /* volume left 0dB */
2896 I2CWrite(bus, I2C_TEA6300, TEA6300_VR, 0x35, 1); /* volume right 0dB */
2897 I2CWrite(bus, I2C_TEA6300, TEA6300_BA, 0x07, 1); /* bass 0dB */
2898 I2CWrite(bus, I2C_TEA6300, TEA6300_TR, 0x07, 1); /* treble 0dB */
2899 I2CWrite(bus, I2C_TEA6300, TEA6300_FA, 0x0f, 1); /* fader off */
2900 I2CWrite(bus, I2C_TEA6300, TEA6300_SW, 0x01, 1); /* mute off input A */
2903 static void init_tda8425(struct i2c_bus *bus)
2905 I2CWrite(bus, I2C_TDA8425, TDA8425_VL, 0xFC, 1); /* volume left 0dB */
2906 I2CWrite(bus, I2C_TDA8425, TDA8425_VR, 0xFC, 1); /* volume right 0dB */
2907 I2CWrite(bus, I2C_TDA8425, TDA8425_BA, 0xF6, 1); /* bass 0dB */
2908 I2CWrite(bus, I2C_TDA8425, TDA8425_TR, 0xF6, 1); /* treble 0dB */
2909 I2CWrite(bus, I2C_TDA8425, TDA8425_S1, 0xCE, 1); /* mute off */
2912 static void init_tda9840(struct i2c_bus *bus)
2914 I2CWrite(bus, I2C_TDA9840, TDA9840_SW, 0x2A, 1); /* Sound mode switching */
2917 static void init_tda9850(struct i2c_bus *bus)
2919 I2CWrite(bus, I2C_TDA9850, TDA9850_CON1, 0x08, 1); /* noise threshold st */
2920 I2CWrite(bus, I2C_TDA9850, TDA9850_CON2, 0x08, 1); /* noise threshold sap */
2921 I2CWrite(bus, I2C_TDA9850, TDA9850_CON3, 0x40, 1); /* stereo mode */
2922 I2CWrite(bus, I2C_TDA9850, TDA9850_CON4, 0x07, 1); /* 0 dB input gain?*/
2923 I2CWrite(bus, I2C_TDA9850, TDA9850_ALI1, 0x10, 1); /* wideband alignment? */
2924 I2CWrite(bus, I2C_TDA9850, TDA9850_ALI2, 0x10, 1); /* spectral alignment? */
2925 I2CWrite(bus, I2C_TDA9850, TDA9850_ALI3, 0x03, 1);
2930 /* Figure out card and tuner type */
2932 static void idcard(int i)
2934 struct bttv *btv = &bttvs[i];
2936 btwrite(0, BT848_GPIO_OUT_EN);
2937 DEBUG(printk(KERN_DEBUG "bttv%d: GPIO: 0x%08x\n", i, btread(BT848_GPIO_DATA)));
2939 /* Default the card to the user-selected one. */
2940 btv->type=card[i];
2941 btv->tuner_type=-1; /* use default tuner type */
2943 /* If we were asked to auto-detect, then do so!
2944 Right now this will only recognize Miro, Hauppauge or STB
2946 if (btv->type == BTTV_UNKNOWN)
2948 if (I2CRead(&(btv->i2c), I2C_HAUPEE)>=0)
2950 if(btv->id>849)
2951 btv->type=BTTV_HAUPPAUGE878;
2952 else
2953 btv->type=BTTV_HAUPPAUGE;
2955 } else if (I2CRead(&(btv->i2c), I2C_STBEE)>=0) {
2956 btv->type=BTTV_STB;
2957 } else {
2958 if (I2CRead(&(btv->i2c), 0x80)>=0) /* check for msp34xx */
2959 btv->type = BTTV_MIROPRO;
2960 else
2961 btv->type=BTTV_MIRO;
2965 /* board specific initialisations */
2966 if (btv->type == BTTV_MIRO || btv->type == BTTV_MIROPRO) {
2967 /* auto detect tuner for MIRO cards */
2968 btv->tuner_type=((btread(BT848_GPIO_DATA)>>10)-1)&7;
2970 if (btv->type == BTTV_HAUPPAUGE || btv->type == BTTV_HAUPPAUGE878) {
2971 hauppauge_msp_reset(btv);
2972 hauppauge_eeprom(&(btv->i2c));
2973 if (btv->type == BTTV_HAUPPAUGE878) {
2974 /* all bt878 hauppauge boards use this ... */
2975 btv->pll.pll_ifreq=28636363;
2976 btv->pll.pll_crystal=BT848_IFORM_XT0;
2980 if (btv->type == BTTV_PIXVIEWPLAYTV) {
2981 btv->pll.pll_ifreq=28636363;
2982 btv->pll.pll_crystal=BT848_IFORM_XT0;
2985 if(btv->type==BTTV_AVERMEDIA98)
2987 btv->pll.pll_ifreq=28636363;
2988 btv->pll.pll_crystal=BT848_IFORM_XT0;
2991 if (btv->have_tuner && btv->tuner_type != -1)
2992 i2c_control_device(&(btv->i2c),
2993 I2C_DRIVERID_TUNER,
2994 TUNER_SET_TYPE,&btv->tuner_type);
2997 if (I2CRead(&(btv->i2c), I2C_TDA9840) >=0)
2999 btv->audio_chip = TDA9840;
3000 printk(KERN_INFO "bttv%d: audio chip: TDA9840\n", btv->nr);
3003 if (I2CRead(&(btv->i2c), I2C_TDA9850) >=0)
3005 btv->audio_chip = TDA9850;
3006 printk(KERN_INFO "bttv%d: audio chip: TDA9850\n",btv->nr);
3009 if (I2CRead(&(btv->i2c), I2C_TDA8425) >=0)
3011 btv->audio_chip = TDA8425;
3012 printk(KERN_INFO "bttv%d: audio chip: TDA8425\n",btv->nr);
3015 switch(btv->audio_chip)
3017 case TDA9850:
3018 init_tda9850(&(btv->i2c));
3019 break;
3020 case TDA9840:
3021 init_tda9840(&(btv->i2c));
3022 break;
3023 case TDA8425:
3024 init_tda8425(&(btv->i2c));
3025 break;
3028 if (I2CRead(&(btv->i2c), I2C_TEA6300) >=0)
3030 printk(KERN_INFO "bttv%d: fader chip: TEA6300\n",btv->nr);
3031 btv->audio_chip = TEA6300;
3032 init_tea6300(&(btv->i2c));
3033 } else
3034 printk(KERN_INFO "bttv%d: NO fader chip: TEA6300\n",btv->nr);
3036 printk(KERN_INFO "bttv%d: model: ",btv->nr);
3038 sprintf(btv->video_dev.name,"BT%d",btv->id);
3039 switch (btv->type)
3041 case BTTV_MIRO:
3042 case BTTV_MIROPRO:
3043 strcat(btv->video_dev.name,
3044 (btv->type == BTTV_MIRO) ? "(Miro)" : "(Miro pro)");
3045 break;
3046 case BTTV_HAUPPAUGE:
3047 strcat(btv->video_dev.name,"(Hauppauge old)");
3048 break;
3049 case BTTV_HAUPPAUGE878:
3050 strcat(btv->video_dev.name,"(Hauppauge new)");
3051 break;
3052 case BTTV_STB:
3053 strcat(btv->video_dev.name,"(STB)");
3054 break;
3055 case BTTV_INTEL:
3056 strcat(btv->video_dev.name,"(Intel)");
3057 break;
3058 case BTTV_DIAMOND:
3059 strcat(btv->video_dev.name,"(Diamond)");
3060 break;
3061 case BTTV_AVERMEDIA:
3062 strcat(btv->video_dev.name,"(AVerMedia)");
3063 break;
3064 case BTTV_MATRIX_VISION:
3065 strcat(btv->video_dev.name,"(MATRIX-Vision)");
3066 break;
3067 case BTTV_AVERMEDIA98:
3068 strcat(btv->video_dev.name,"(AVerMedia TVCapture 98)");
3069 break;
3070 case BTTV_VHX:
3071 strcpy(btv->video_dev.name,"BT848(Aimslab-VHX)");
3072 break;
3073 case BTTV_WINVIEW_601:
3074 strcpy(btv->video_dev.name,"BT848(Leadtek WinView 601)");
3075 break;
3077 printk("%s\n",btv->video_dev.name);
3078 audio(btv, AUDIO_MUTE);
3083 static void bt848_set_risc_jmps(struct bttv *btv)
3085 int flags=btv->cap;
3087 /* Sync to start of odd field */
3088 btv->risc_jmp[0]=BT848_RISC_SYNC|BT848_RISC_RESYNC|BT848_FIFO_STATUS_VRE;
3089 btv->risc_jmp[1]=0;
3091 /* Jump to odd vbi sub */
3092 btv->risc_jmp[2]=BT848_RISC_JUMP|(0x5<<20);
3093 if (flags&8)
3094 btv->risc_jmp[3]=virt_to_bus(btv->vbi_odd);
3095 else
3096 btv->risc_jmp[3]=virt_to_bus(btv->risc_jmp+4);
3098 /* Jump to odd sub */
3099 btv->risc_jmp[4]=BT848_RISC_JUMP|(0x6<<20);
3100 if (flags&2)
3101 btv->risc_jmp[5]=virt_to_bus(btv->risc_odd);
3102 else
3103 btv->risc_jmp[5]=virt_to_bus(btv->risc_jmp+6);
3106 /* Sync to start of even field */
3107 btv->risc_jmp[6]=BT848_RISC_SYNC|BT848_RISC_RESYNC|BT848_FIFO_STATUS_VRO;
3108 btv->risc_jmp[7]=0;
3110 /* Jump to even vbi sub */
3111 btv->risc_jmp[8]=BT848_RISC_JUMP;
3112 if (flags&4)
3113 btv->risc_jmp[9]=virt_to_bus(btv->vbi_even);
3114 else
3115 btv->risc_jmp[9]=virt_to_bus(btv->risc_jmp+10);
3117 /* Jump to even sub */
3118 btv->risc_jmp[10]=BT848_RISC_JUMP|(8<<20);
3119 if (flags&1)
3120 btv->risc_jmp[11]=virt_to_bus(btv->risc_even);
3121 else
3122 btv->risc_jmp[11]=virt_to_bus(btv->risc_jmp+12);
3124 btv->risc_jmp[12]=BT848_RISC_JUMP;
3125 btv->risc_jmp[13]=virt_to_bus(btv->risc_jmp);
3127 /* enable capturing */
3128 btaor(flags, ~0x0f, BT848_CAP_CTL);
3129 if (flags&0x0f)
3130 bt848_dma(btv, 3);
3131 else
3132 bt848_dma(btv, 0);
3135 static int init_bt848(int i)
3137 struct bttv *btv = &bttvs[i];
3139 btv->user=0;
3141 /* reset the bt848 */
3142 btwrite(0, BT848_SRESET);
3143 DEBUG(printk(KERN_DEBUG "bttv%d: bt848_mem: 0x%08x\n",i,(unsigned int) btv->bt848_mem));
3145 /* default setup for max. PAL size in a 1024xXXX hicolor framebuffer */
3146 btv->win.norm=0; /* change this to 1 for NTSC, 2 for SECAM */
3147 btv->win.interlace=1;
3148 btv->win.x=0;
3149 btv->win.y=0;
3150 btv->win.width=768; /* 640 */
3151 btv->win.height=576; /* 480 */
3152 btv->win.cropwidth=768; /* 640 */
3153 btv->win.cropheight=576; /* 480 */
3154 btv->win.cropx=0;
3155 btv->win.cropy=0;
3156 btv->win.bpp=2;
3157 btv->win.depth=16;
3158 btv->win.color_fmt=BT848_COLOR_FMT_RGB16;
3159 btv->win.bpl=1024*btv->win.bpp;
3160 btv->win.swidth=1024;
3161 btv->win.sheight=768;
3162 btv->cap=0;
3164 btv->gmode=0;
3165 btv->risc_odd=0;
3166 btv->risc_even=0;
3167 btv->risc_jmp=0;
3168 btv->vbibuf=0;
3169 btv->grisc=0;
3170 btv->grabbing=0;
3171 btv->grabcount=0;
3172 btv->grab=0;
3173 btv->lastgrab=0;
3174 btv->field=btv->last_field=0;
3175 /* cevans - prevents panic if initialization bails due to memory
3176 * alloc failures!
3178 btv->video_dev.minor = -1;
3179 btv->vbi_dev.minor = -1;
3180 btv->radio_dev.minor = -1;
3182 /* i2c */
3183 memcpy(&(btv->i2c),&bttv_i2c_bus_template,sizeof(struct i2c_bus));
3184 sprintf(btv->i2c.name,"bt848-%d",i);
3185 btv->i2c.data = btv;
3187 if (!(btv->risc_odd=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
3188 return -1;
3189 if (!(btv->risc_even=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
3190 return -1;
3191 if (!(btv->risc_jmp =(unsigned int *) kmalloc(2048, GFP_KERNEL)))
3192 return -1;
3193 DEBUG(printk(KERN_DEBUG "risc_jmp: %p\n",btv->risc_jmp));
3194 btv->vbi_odd=btv->risc_jmp+16;
3195 btv->vbi_even=btv->vbi_odd+256;
3196 btv->bus_vbi_odd=virt_to_bus(btv->risc_jmp+12);
3197 btv->bus_vbi_even=virt_to_bus(btv->risc_jmp+6);
3199 btwrite(virt_to_bus(btv->risc_jmp+2), BT848_RISC_STRT_ADD);
3200 btv->vbibuf=(unsigned char *) vmalloc(VBIBUF_SIZE);
3201 if (!btv->vbibuf)
3202 return -1;
3203 if (!(btv->grisc=(unsigned int *) kmalloc(32768, GFP_KERNEL)))
3204 return -1;
3206 memset(btv->vbibuf, 0, VBIBUF_SIZE); /* We don't want to return random
3207 memory to the user */
3209 btv->fbuffer=NULL;
3211 bt848_muxsel(btv, 1);
3212 bt848_set_winsize(btv);
3214 /* btwrite(0, BT848_TDEC); */
3215 btwrite(0x10, BT848_COLOR_CTL);
3216 btwrite(0x00, BT848_CAP_CTL);
3217 btwrite(0xac, BT848_GPIO_DMA_CTL);
3219 /* select direct input */
3220 btwrite(0x00, BT848_GPIO_REG_INP);
3222 btwrite(BT848_IFORM_MUX1 | BT848_IFORM_XTAUTO | BT848_IFORM_PAL_BDGHI,
3223 BT848_IFORM);
3225 btwrite(0xd8, BT848_CONTRAST_LO);
3226 bt848_bright(btv, 0x10);
3228 btwrite(0x20, BT848_E_VSCALE_HI);
3229 btwrite(0x20, BT848_O_VSCALE_HI);
3230 btwrite(/*BT848_ADC_SYNC_T|*/
3231 BT848_ADC_RESERVED|BT848_ADC_CRUSH, BT848_ADC);
3233 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
3234 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
3236 btv->picture.colour=254<<7;
3237 btv->picture.brightness=128<<8;
3238 btv->picture.hue=128<<8;
3239 btv->picture.contrast=0xd8<<7;
3241 btwrite(0x00, BT848_E_SCLOOP);
3242 btwrite(0x00, BT848_O_SCLOOP);
3244 /* clear interrupt status */
3245 btwrite(0xfffffUL, BT848_INT_STAT);
3247 /* set interrupt mask */
3248 btwrite(btv->triton1|
3249 /*BT848_INT_PABORT|BT848_INT_RIPERR|BT848_INT_PPERR|
3250 BT848_INT_FDSR|BT848_INT_FTRGT|BT848_INT_FBUS|*/
3251 BT848_INT_VSYNC|
3252 BT848_INT_SCERR|
3253 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
3254 BT848_INT_FMTCHG|BT848_INT_HLOCK,
3255 BT848_INT_MASK);
3257 make_vbitab(btv);
3258 bt848_set_risc_jmps(btv);
3261 * Now add the template and register the device unit.
3264 memcpy(&btv->video_dev,&bttv_template, sizeof(bttv_template));
3265 memcpy(&btv->vbi_dev,&vbi_template, sizeof(vbi_template));
3266 memcpy(&btv->radio_dev,&radio_template,sizeof(radio_template));
3268 idcard(i);
3270 if(video_register_device(&btv->video_dev,VFL_TYPE_GRABBER)<0)
3271 return -1;
3272 if(video_register_device(&btv->vbi_dev,VFL_TYPE_VBI)<0)
3274 video_unregister_device(&btv->video_dev);
3275 return -1;
3277 if (radio[i])
3279 if(video_register_device(&btv->radio_dev, VFL_TYPE_RADIO)<0)
3281 video_unregister_device(&btv->vbi_dev);
3282 video_unregister_device(&btv->video_dev);
3283 return -1;
3286 i2c_register_bus(&btv->i2c);
3288 return 0;
3291 static void bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3293 u32 stat,astat;
3294 u32 dstat;
3295 int count;
3296 struct bttv *btv;
3298 btv=(struct bttv *)dev_id;
3299 count=0;
3300 while (1)
3302 /* get/clear interrupt status bits */
3303 stat=btread(BT848_INT_STAT);
3304 astat=stat&btread(BT848_INT_MASK);
3305 if (!astat)
3306 return;
3307 btwrite(astat,BT848_INT_STAT);
3308 IDEBUG(printk ("bttv%d: astat %08x\n", btv->nr, astat));
3309 IDEBUG(printk ("bttv%d: stat %08x\n", btv->nr, stat));
3311 /* get device status bits */
3312 dstat=btread(BT848_DSTATUS);
3314 if (astat&BT848_INT_FMTCHG)
3316 IDEBUG(printk ("bttv%d: IRQ_FMTCHG\n", btv->nr));
3317 /*btv->win.norm&=
3318 (dstat&BT848_DSTATUS_NUML) ? (~1) : (~0); */
3320 if (astat&BT848_INT_VPRES)
3322 IDEBUG(printk ("bttv%d: IRQ_VPRES\n", btv->nr));
3324 if (astat&BT848_INT_VSYNC)
3326 IDEBUG(printk ("bttv%d: IRQ_VSYNC\n", btv->nr));
3327 btv->field++;
3329 if (astat&BT848_INT_SCERR) {
3330 IDEBUG(printk ("bttv%d: IRQ_SCERR\n", btv->nr));
3331 bt848_dma(btv, 0);
3332 bt848_dma(btv, 3);
3333 wake_up_interruptible(&btv->vbiq);
3334 wake_up_interruptible(&btv->capq);
3337 if (astat&BT848_INT_RISCI)
3339 IDEBUG(printk ("bttv%d: IRQ_RISCI\n", btv->nr));
3341 /* captured VBI frame */
3342 if (stat&(1<<28))
3344 btv->vbip=0;
3345 /* inc vbi frame count for detecting drops */
3346 (*(u32 *)&(btv->vbibuf[VBIBUF_SIZE - 4]))++;
3347 wake_up_interruptible(&btv->vbiq);
3350 /* captured full frame */
3351 if (stat&(2<<28))
3353 wake_up_interruptible(&btv->capq);
3354 btv->last_field=btv->field;
3355 btv->grab++;
3356 btv->frame_stat[btv->grf] = GBUFFER_DONE;
3357 if ((--btv->grabbing))
3359 btv->gfmt = btv->gfmt_next;
3360 btv->gwidth = btv->gwidth_next;
3361 btv->gheight = btv->gheight_next;
3362 btv->gro = btv->gro_next;
3363 btv->gre = btv->gre_next;
3364 btv->grf = btv->grf_next;
3365 btv->risc_jmp[5]=btv->gro;
3366 btv->risc_jmp[11]=btv->gre;
3367 bt848_set_geo(btv, btv->gwidth,
3368 btv->gheight,
3369 btv->gfmt);
3370 } else {
3371 bt848_set_risc_jmps(btv);
3372 btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI);
3373 btand(~BT848_VSCALE_COMB, BT848_O_VSCALE_HI);
3374 bt848_set_geo(btv, btv->win.width,
3375 btv->win.height,
3376 btv->win.color_fmt);
3378 wake_up_interruptible(&btv->capq);
3379 break;
3381 if (stat&(8<<28))
3383 btv->risc_jmp[5]=btv->gro;
3384 btv->risc_jmp[11]=btv->gre;
3385 btv->risc_jmp[12]=BT848_RISC_JUMP;
3386 bt848_set_geo(btv, btv->gwidth, btv->gheight,
3387 btv->gfmt);
3390 if (astat&BT848_INT_OCERR)
3392 IDEBUG(printk ("bttv%d: IRQ_OCERR\n", btv->nr));
3394 if (astat&BT848_INT_PABORT)
3396 IDEBUG(printk ("bttv%d: IRQ_PABORT\n", btv->nr));
3398 if (astat&BT848_INT_RIPERR)
3400 IDEBUG(printk ("bttv%d: IRQ_RIPERR\n", btv->nr));
3402 if (astat&BT848_INT_PPERR)
3404 IDEBUG(printk ("bttv%d: IRQ_PPERR\n", btv->nr));
3406 if (astat&BT848_INT_FDSR)
3408 IDEBUG(printk ("bttv%d: IRQ_FDSR\n", btv->nr));
3410 if (astat&BT848_INT_FTRGT)
3412 IDEBUG(printk ("bttv%d: IRQ_FTRGT\n", btv->nr));
3414 if (astat&BT848_INT_FBUS)
3416 IDEBUG(printk ("bttv%d: IRQ_FBUS\n", btv->nr));
3418 if (astat&BT848_INT_HLOCK)
3420 if ((dstat&BT848_DSTATUS_HLOC) || (btv->radio))
3421 audio(btv, AUDIO_ON);
3422 else
3423 audio(btv, AUDIO_OFF);
3426 if (astat&BT848_INT_I2CDONE)
3430 count++;
3431 if (count > 10)
3432 printk (KERN_WARNING "bttv%d: irq loop %d\n",
3433 btv->nr,count);
3434 if (count > 20)
3436 btwrite(0, BT848_INT_MASK);
3437 printk(KERN_ERR
3438 "bttv%d: IRQ lockup, cleared int mask\n", btv->nr);
3446 * Scan for a Bt848 card, request the irq and map the io memory
3449 #if LINUX_VERSION_CODE >= 0x020100
3450 int configure_bt848(struct pci_dev *dev, int bttv_num)
3452 int result;
3453 unsigned char command;
3454 struct bttv *btv;
3456 btv=&bttvs[bttv_num];
3457 btv->dev=dev;
3458 btv->nr = bttv_num;
3459 btv->bt848_mem=NULL;
3460 btv->vbibuf=NULL;
3461 btv->risc_jmp=NULL;
3462 btv->vbi_odd=NULL;
3463 btv->vbi_even=NULL;
3464 init_waitqueue_head(&btv->vbiq);
3465 init_waitqueue_head(&btv->capq);
3466 init_waitqueue_head(&btv->capqo);
3467 init_waitqueue_head(&btv->capqe);
3468 btv->vbip=VBIBUF_SIZE;
3470 btv->id=dev->device;
3471 btv->irq=dev->irq;
3472 btv->bt848_adr=dev->base_address[0];
3473 if (btv->id >= 878)
3474 btv->i2c_command = 0x83;
3475 else
3476 btv->i2c_command=(I2C_TIMING | BT848_I2C_SCL | BT848_I2C_SDA);
3478 if (remap[bttv_num])
3480 if (remap[bttv_num] < 0x1000)
3481 remap[bttv_num]<<=20;
3482 remap[bttv_num]&=PCI_BASE_ADDRESS_MEM_MASK;
3483 printk(KERN_INFO "bttv%d: remapping to : 0x%08x.\n",
3484 bttv_num,remap[bttv_num]);
3485 remap[bttv_num]|=btv->bt848_adr&(~PCI_BASE_ADDRESS_MEM_MASK);
3486 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, remap[bttv_num]);
3487 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &btv->bt848_adr);
3488 btv->dev->base_address[0] = btv->bt848_adr;
3490 btv->bt848_adr&=PCI_BASE_ADDRESS_MEM_MASK;
3491 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3492 printk(KERN_INFO "bttv%d: Brooktree Bt%d (rev %d) ",
3493 bttv_num,btv->id, btv->revision);
3494 printk("bus: %d, devfn: %d, ",dev->bus->number, dev->devfn);
3495 printk("irq: %d, ",btv->irq);
3496 printk("memory: 0x%08x.\n", btv->bt848_adr);
3498 btv->pll.pll_crystal = 0;
3499 btv->pll.pll_ifreq = 0;
3500 btv->pll.pll_ofreq = 0;
3501 btv->pll.pll_current = 0;
3502 if (!(btv->id==848 && btv->revision==0x11)) {
3503 switch (pll[btv->nr]) {
3504 case 0:
3505 /* off */
3506 break;
3507 case 1:
3508 /* 28 MHz crystal installed */
3509 btv->pll.pll_ifreq=28636363;
3510 btv->pll.pll_crystal=BT848_IFORM_XT0;
3511 break;
3512 case 2:
3513 /* 35 MHz crystal installed */
3514 btv->pll.pll_ifreq=35468950;
3515 btv->pll.pll_crystal=BT848_IFORM_XT1;
3516 break;
3520 btv->bt848_mem=ioremap(btv->bt848_adr, 0x1000);
3522 /* clear interrupt mask */
3523 btwrite(0, BT848_INT_MASK);
3525 result = request_irq(btv->irq, bttv_irq,
3526 SA_SHIRQ | SA_INTERRUPT,"bttv",(void *)btv);
3527 if (result==-EINVAL)
3529 printk(KERN_ERR "bttv%d: Bad irq number or handler\n",
3530 bttv_num);
3531 return -EINVAL;
3533 if (result==-EBUSY)
3535 printk(KERN_ERR "bttv%d: IRQ %d busy, change your PnP config in BIOS\n",bttv_num,btv->irq);
3536 return result;
3538 if (result < 0)
3539 return result;
3541 pci_set_master(dev);
3543 btv->triton1=triton1 ? BT848_INT_ETBF : 0;
3544 if (triton1 && btv->id >= 878)
3546 btv->triton1 = 0;
3547 printk("bttv: Enabling 430FX compatibilty for bt878\n");
3548 pci_read_config_byte(dev, BT878_DEVCTRL, &command);
3549 command|=BT878_EN_TBFX;
3550 pci_write_config_byte(dev, BT878_DEVCTRL, command);
3551 pci_read_config_byte(dev, BT878_DEVCTRL, &command);
3552 if (!(command&BT878_EN_TBFX))
3554 printk("bttv: 430FX compatibility could not be enabled\n");
3555 return -1;
3559 return 0;
3562 static int find_bt848(void)
3564 struct pci_dev *dev = pci_devices;
3565 int result=0;
3567 bttv_num=0;
3569 while (dev)
3571 if (dev->vendor == PCI_VENDOR_ID_BROOKTREE)
3572 if ((dev->device == PCI_DEVICE_ID_BT848)||
3573 (dev->device == PCI_DEVICE_ID_BT849)||
3574 (dev->device == PCI_DEVICE_ID_BT878)||
3575 (dev->device == PCI_DEVICE_ID_BT879))
3576 result=configure_bt848(dev,bttv_num++);
3577 if (result)
3578 return result;
3579 dev = dev->next;
3581 if(bttv_num)
3582 printk(KERN_INFO "bttv: %d Bt8xx card(s) found.\n", bttv_num);
3583 return bttv_num;
3585 #else
3586 static int find_bt848(void)
3588 short pci_index;
3589 unsigned char command, latency;
3590 int result;
3591 unsigned char bus, devfn;
3592 struct bttv *btv;
3594 bttv_num=0;
3596 if (!pcibios_present())
3598 DEBUG(printk(KERN_DEBUG "bttv%d: PCI-BIOS not present or not accessable!\n",bttv_num));
3599 return 0;
3602 for (pci_index = 0;
3603 !pcibios_find_device(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
3604 pci_index, &bus, &devfn)
3605 ||!pcibios_find_device(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
3606 pci_index, &bus, &devfn)
3607 ||!pcibios_find_device(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
3608 pci_index, &bus, &devfn)
3609 ||!pcibios_find_device(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
3610 pci_index, &bus, &devfn);
3611 ++pci_index)
3613 btv=&bttvs[bttv_num];
3614 btv->nr = bttv_num;
3615 btv->bus=bus;
3616 btv->devfn=devfn;
3617 btv->bt848_mem=NULL;
3618 btv->vbibuf=NULL;
3619 btv->risc_jmp=NULL;
3620 btv->vbi_odd=NULL;
3621 btv->vbi_even=NULL;
3622 btv->vbiq=NULL;
3623 btv->capq=NULL;
3624 btv->capqo=NULL;
3625 btv->capqe=NULL;
3627 btv->vbip=VBIBUF_SIZE;
3629 pcibios_read_config_word(btv->bus, btv->devfn, PCI_DEVICE_ID,
3630 &btv->id);
3631 pcibios_read_config_byte(btv->bus, btv->devfn,
3632 PCI_INTERRUPT_LINE, &btv->irq);
3633 pcibios_read_config_dword(btv->bus, btv->devfn, PCI_BASE_ADDRESS_0,
3634 &btv->bt848_adr);
3635 if (btv->id >= 878)
3636 btv->i2c_command = 0x83;
3637 else
3638 btv->i2c_command=
3639 (I2C_TIMING | BT848_I2C_SCL | BT848_I2C_SDA);
3641 if (remap[bttv_num])
3643 if (remap[bttv_num] < 0x1000)
3644 remap[bttv_num]<<=20;
3645 remap[bttv_num]&=PCI_BASE_ADDRESS_MEM_MASK;
3646 printk(KERN_INFO "bttv%d: remapping to : 0x%08x.\n",
3647 bttv_num,remap[bttv_num]);
3648 remap[bttv_num]|=btv->bt848_adr&(~PCI_BASE_ADDRESS_MEM_MASK);
3649 pcibios_write_config_dword(btv->bus, btv->devfn, PCI_BASE_ADDRESS_0,
3650 remap[bttv_num]);
3651 pcibios_read_config_dword(btv->bus, btv->devfn, PCI_BASE_ADDRESS_0,
3652 &btv->bt848_adr);
3655 btv->bt848_adr&=PCI_BASE_ADDRESS_MEM_MASK;
3656 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_CLASS_REVISION,
3657 &btv->revision);
3658 printk(KERN_INFO "bttv%d: Brooktree Bt%d (rev %d) ",
3659 bttv_num,btv->id, btv->revision);
3660 printk("bus: %d, devfn: %d, ",
3661 btv->bus, btv->devfn);
3662 printk("irq: %d, ",btv->irq);
3663 printk("memory: 0x%08x.\n", btv->bt848_adr);
3665 btv->pll.pll_crystal = 0;
3666 btv->pll.pll_ifreq = 0;
3667 btv->pll.pll_ofreq = 0;
3668 btv->pll.pll_current = 0;
3669 if (!(btv->id==848 && btv->revision==0x11)) {
3670 switch (pll[btv->nr]) {
3671 case 0:
3672 /* off */
3673 break;
3674 case 1:
3675 /* 28 MHz crystal installed */
3676 btv->pll.pll_ifreq=28636363;
3677 btv->pll.pll_crystal=BT848_IFORM_XT0;
3678 break;
3679 case 2:
3680 /* 35 MHz crystal installed */
3681 btv->pll.pll_ifreq=35468950;
3682 btv->pll.pll_crystal=BT848_IFORM_XT1;
3683 break;
3687 btv->bt848_mem=ioremap(btv->bt848_adr, 0x1000);
3689 result = request_irq(btv->irq, bttv_irq,
3690 SA_SHIRQ | SA_INTERRUPT,"bttv",(void *)btv);
3691 if (result==-EINVAL)
3693 printk(KERN_ERR "bttv%d: Bad irq number or handler\n",
3694 bttv_num);
3695 return -EINVAL;
3697 if (result==-EBUSY)
3699 printk(KERN_ERR "bttv%d: IRQ %d busy, change your PnP config in BIOS\n",bttv_num,btv->irq);
3700 return result;
3702 if (result < 0)
3703 return result;
3705 /* Enable bus-mastering */
3706 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_COMMAND, &command);
3707 command|=PCI_COMMAND_MASTER;
3708 pcibios_write_config_byte(btv->bus, btv->devfn, PCI_COMMAND, command);
3709 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_COMMAND, &command);
3710 if (!(command&PCI_COMMAND_MASTER))
3712 printk(KERN_ERR "bttv%d: PCI bus-mastering could not be enabled\n",bttv_num);
3713 return -1;
3715 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_LATENCY_TIMER,
3716 &latency);
3717 if (!latency)
3719 latency=32;
3720 pcibios_write_config_byte(btv->bus, btv->devfn,
3721 PCI_LATENCY_TIMER, latency);
3723 DEBUG(printk(KERN_DEBUG "bttv%d: latency: %02x\n",
3724 bttv_num, latency));
3726 btv->triton1=triton1 ? BT848_INT_ETBF : 0;
3727 if (triton1 && btv->id >= 878)
3729 triton1 = 0;
3730 printk("bttv: Enabling 430FX compatibilty for bt878\n");
3731 pcibios_read_config_byte(btv->bus, btv->devfn, BT878_DEVCTRL, &command);
3732 command|=BT878_EN_TBFX;
3733 pcibios_write_config_byte(btv->bus, btv->devfn, BT878_DEVCTRL, command);
3734 pcibios_read_config_byte(btv->bus, btv->devfn, BT878_DEVCTRL, &command);
3735 if (!(command&BT878_EN_TBFX))
3737 printk("bttv: 430FX compatibility could not be enabled\n");
3738 return -1;
3742 bttv_num++;
3744 if(bttv_num)
3745 printk(KERN_INFO "bttv: %d Bt8xx card(s) found.\n", bttv_num);
3746 return bttv_num;
3748 #endif
3750 static void release_bttv(void)
3752 u8 command;
3753 int i;
3754 struct bttv *btv;
3756 for (i=0;i<bttv_num; i++)
3758 btv=&bttvs[i];
3760 /* turn off all capturing, DMA and IRQs */
3762 btand(~15, BT848_GPIO_DMA_CTL);
3764 /* first disable interrupts before unmapping the memory! */
3765 btwrite(0, BT848_INT_MASK);
3766 btwrite(0xffffffffUL,BT848_INT_STAT);
3767 btwrite(0x0, BT848_GPIO_OUT_EN);
3769 /* unregister i2c_bus */
3770 i2c_unregister_bus((&btv->i2c));
3772 /* disable PCI bus-mastering */
3773 #if LINUX_VERSION_CODE >= 0x020100
3774 pci_read_config_byte(btv->dev, PCI_COMMAND, &command);
3775 /* Should this be &=~ ?? */
3776 command&=~PCI_COMMAND_MASTER;
3777 pci_write_config_byte(btv->dev, PCI_COMMAND, command);
3778 #else
3779 pcibios_read_config_byte(btv->bus, btv->devfn, PCI_COMMAND, &command);
3780 command&=~PCI_COMMAND_MASTER;
3781 pcibios_write_config_byte(btv->bus, btv->devfn, PCI_COMMAND, command);
3783 #endif
3785 /* unmap and free memory */
3786 if (btv->grisc)
3787 kfree((void *) btv->grisc);
3789 if (btv->risc_odd)
3790 kfree((void *) btv->risc_odd);
3792 if (btv->risc_even)
3793 kfree((void *) btv->risc_even);
3795 DEBUG(printk(KERN_DEBUG "free: risc_jmp: 0x%08x.\n", btv->risc_jmp));
3796 if (btv->risc_jmp)
3797 kfree((void *) btv->risc_jmp);
3799 DEBUG(printk(KERN_DEBUG "bt848_vbibuf: 0x%08x.\n", btv->vbibuf));
3800 if (btv->vbibuf)
3801 vfree((void *) btv->vbibuf);
3804 free_irq(btv->irq,btv);
3805 DEBUG(printk(KERN_DEBUG "bt848_mem: 0x%08x.\n", btv->bt848_mem));
3806 if (btv->bt848_mem)
3807 iounmap(btv->bt848_mem);
3809 if(btv->video_dev.minor!=-1)
3810 video_unregister_device(&btv->video_dev);
3811 if(btv->vbi_dev.minor!=-1)
3812 video_unregister_device(&btv->vbi_dev);
3813 if (radio[btv->nr] && btv->radio_dev.minor != -1)
3814 video_unregister_device(&btv->radio_dev);
3818 #ifdef MODULE
3820 EXPORT_NO_SYMBOLS;
3822 int init_module(void)
3824 #else
3825 int init_bttv_cards(struct video_init *unused)
3827 #endif
3828 int i;
3830 handle_chipset();
3831 if (find_bt848()<0)
3832 return -EIO;
3834 /* initialize Bt848s */
3835 for (i=0; i<bttv_num; i++)
3837 if (init_bt848(i)<0)
3839 release_bttv();
3840 return -EIO;
3844 return 0;
3849 #ifdef MODULE
3851 void cleanup_module(void)
3853 release_bttv();
3856 #endif
3859 * Local variables:
3860 * c-indent-level: 8
3861 * c-brace-imaginary-offset: 0
3862 * c-brace-offset: -8
3863 * c-argdecl-indent: 8
3864 * c-label-offset: -8
3865 * c-continued-statement-offset: 8
3866 * c-continued-brace-offset: 0
3867 * indent-tabs-mode: nil
3868 * tab-width: 8
3869 * End: