Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / drivers / char / bttv.c
blobb17efd42963d8f5e4c56e0ccdd9ce93f5c154beb
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)
6 (c) 1999,2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/version.h>
24 #include <linux/module.h>
25 #include <linux/delay.h>
26 #include <linux/errno.h>
27 #include <linux/fs.h>
28 #include <linux/kernel.h>
29 #include <linux/major.h>
30 #include <linux/malloc.h>
31 #include <linux/mm.h>
32 #include <linux/poll.h>
33 #include <linux/pci.h>
34 #include <linux/signal.h>
35 #include <asm/io.h>
36 #include <linux/ioport.h>
37 #include <asm/pgtable.h>
38 #include <asm/page.h>
39 #include <linux/sched.h>
40 #include <asm/segment.h>
41 #include <linux/types.h>
42 #include <linux/wrapper.h>
43 #include <linux/interrupt.h>
44 #include <linux/kmod.h>
45 #include <linux/vmalloc.h>
46 #include <linux/init.h>
48 #include "bttv.h"
49 #include "tuner.h"
51 #define DEBUG(x) /* Debug driver */
52 #define IDEBUG(x) /* Debug interrupt handler */
53 #define MIN(a,b) (((a)>(b))?(b):(a))
54 #define MAX(a,b) (((a)>(b))?(a):(b))
57 /* Anybody who uses more than four? */
58 #define BTTV_MAX 4
59 static void bt848_set_risc_jmps(struct bttv *btv, int state);
61 static int bttv_num; /* number of Bt848s in use */
62 static struct bttv bttvs[BTTV_MAX];
65 /* insmod args */
66 MODULE_PARM(triton1,"i");
67 MODULE_PARM(remap,"1-4i");
68 MODULE_PARM(radio,"1-4i");
69 MODULE_PARM(card,"1-4i");
70 MODULE_PARM(pll,"1-4i");
71 MODULE_PARM(bigendian,"i");
72 MODULE_PARM(fieldnr,"i");
73 MODULE_PARM(verbose,"i");
74 MODULE_PARM(debug,"i");
75 MODULE_PARM(autoload,"i");
76 MODULE_PARM(gbuffers,"i");
77 MODULE_PARM(gbufsize,"i");
79 EXPORT_SYMBOL(bttv_get_id);
80 EXPORT_SYMBOL(bttv_gpio_enable);
81 EXPORT_SYMBOL(bttv_read_gpio);
82 EXPORT_SYMBOL(bttv_write_gpio);
83 EXPORT_SYMBOL(bttv_get_gpio_queue);
85 MODULE_DESCRIPTION("bttv - v4l driver module for bt848/878 based cards");
86 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
88 #if defined(__sparc__) || defined(__powerpc__)
89 static unsigned int bigendian=1;
90 #else
91 static unsigned int bigendian=0;
92 #endif
93 static int triton1=0;
94 static unsigned long remap[BTTV_MAX];
95 static unsigned int radio[BTTV_MAX];
96 static unsigned int card[BTTV_MAX] = { 0, 0, 0, 0 };
97 static unsigned int pll[BTTV_MAX] = { -1, -1, -1, -1};
98 static unsigned int fieldnr = 0;
99 static unsigned int verbose = 1;
100 static unsigned int debug = 0;
101 static unsigned int gbuffers = 2;
102 static unsigned int gbufsize = BTTV_MAX_FBUF;
103 #ifdef MODULE
104 static unsigned int autoload = 1;
105 #else
106 static unsigned int autoload = 0;
107 #endif
110 #define I2C_TIMING (0x7<<4)
111 #define I2C_DELAY 10
113 #define I2C_SET(CTRL,DATA) \
114 { btwrite((CTRL<<1)|(DATA), BT848_I2C); udelay(I2C_DELAY); }
115 #define I2C_GET() (btread(BT848_I2C)&1)
117 #define BURSTOFFSET 76
118 #define BTTV_ERRORS 5
121 /* ----------------------------------------------------------------------- */
122 /* Exported functions - for other modules which want to access the */
123 /* gpio ports (IR for example) */
124 /* see bttv.h for comments */
126 int bttv_get_id(unsigned int card)
128 if (card >= bttv_num) {
129 return -1;
131 return bttvs[card].type;
134 int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data)
136 struct bttv *btv;
138 if (card >= bttv_num) {
139 return -EINVAL;
142 btv = &bttvs[card];
143 btaor(data, ~mask, BT848_GPIO_OUT_EN);
144 return 0;
147 int bttv_read_gpio(unsigned int card, unsigned long *data)
149 struct bttv *btv;
151 if (card >= bttv_num) {
152 return -EINVAL;
155 btv = &bttvs[card];
157 if(btv->shutdown) {
158 return -ENODEV;
161 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
162 because we set direct input on init */
163 *data = btread(BT848_GPIO_DATA);
164 return 0;
167 int bttv_write_gpio(unsigned int card, unsigned long mask, unsigned long data)
169 struct bttv *btv;
171 if (card >= bttv_num) {
172 return -EINVAL;
175 btv = &bttvs[card];
177 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
178 because direct input is set on init */
179 btaor(data & mask, ~mask, BT848_GPIO_DATA);
180 return 0;
183 WAIT_QUEUE* bttv_get_gpio_queue(unsigned int card)
185 struct bttv *btv;
187 if (card >= bttv_num) {
188 return NULL;
191 btv = &bttvs[card];
192 if (bttvs[card].shutdown) {
193 return NULL;
195 return &btv->gpioq;
198 /*******************************/
199 /* Memory management functions */
200 /*******************************/
202 #define MDEBUG(x) do { } while(0) /* Debug memory management */
204 /* [DaveM] I've recoded most of this so that:
205 * 1) It's easier to tell what is happening
206 * 2) It's more portable, especially for translating things
207 * out of vmalloc mapped areas in the kernel.
208 * 3) Less unnecessary translations happen.
210 * The code used to assume that the kernel vmalloc mappings
211 * existed in the page tables of every process, this is simply
212 * not guarenteed. We now use pgd_offset_k which is the
213 * defined way to get at the kernel page tables.
216 /* Given PGD from the address space's page table, return the kernel
217 * virtual mapping of the physical memory mapped at ADR.
219 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
221 unsigned long ret = 0UL;
222 pmd_t *pmd;
223 pte_t *ptep, pte;
225 if (!pgd_none(*pgd)) {
226 pmd = pmd_offset(pgd, adr);
227 if (!pmd_none(*pmd)) {
228 ptep = pte_offset(pmd, adr);
229 pte = *ptep;
230 if(pte_present(pte))
231 ret = (page_address(pte_page(pte))|(adr&(PAGE_SIZE-1)));
234 MDEBUG(printk("uv2kva(%lx-->%lx)", adr, ret));
235 return ret;
238 static inline unsigned long uvirt_to_bus(unsigned long adr)
240 unsigned long kva, ret;
242 kva = uvirt_to_kva(pgd_offset(current->mm, adr), adr);
243 ret = virt_to_bus((void *)kva);
244 MDEBUG(printk("uv2b(%lx-->%lx)", adr, ret));
245 return ret;
248 static inline unsigned long kvirt_to_bus(unsigned long adr)
250 unsigned long va, kva, ret;
252 va = VMALLOC_VMADDR(adr);
253 kva = uvirt_to_kva(pgd_offset_k(va), va);
254 ret = virt_to_bus((void *)kva);
255 MDEBUG(printk("kv2b(%lx-->%lx)", adr, ret));
256 return ret;
259 /* Here we want the physical address of the memory.
260 * This is used when initializing the contents of the
261 * area and marking the pages as reserved.
263 static inline unsigned long kvirt_to_pa(unsigned long adr)
265 unsigned long va, kva, ret;
267 va = VMALLOC_VMADDR(adr);
268 kva = uvirt_to_kva(pgd_offset_k(va), va);
269 ret = __pa(kva);
270 MDEBUG(printk("kv2pa(%lx-->%lx)", adr, ret));
271 return ret;
274 static void * rvmalloc(signed long size)
276 void * mem;
277 unsigned long adr, page;
279 mem=vmalloc(size);
280 if (mem)
282 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
283 adr=(unsigned long) mem;
284 while (size > 0)
286 page = kvirt_to_pa(adr);
287 mem_map_reserve(MAP_NR(__va(page)));
288 adr+=PAGE_SIZE;
289 size-=PAGE_SIZE;
292 return mem;
295 static void rvfree(void * mem, signed long size)
297 unsigned long adr, page;
299 if (mem)
301 adr=(unsigned long) mem;
302 while (size > 0)
304 page = kvirt_to_pa(adr);
305 mem_map_unreserve(MAP_NR(__va(page)));
306 adr+=PAGE_SIZE;
307 size-=PAGE_SIZE;
309 vfree(mem);
316 * Create the giant waste of buffer space we need for now
317 * until we get DMA to user space sorted out (probably 2.3.x)
319 * We only create this as and when someone uses mmap
322 static int fbuffer_alloc(struct bttv *btv)
324 if(!btv->fbuffer)
325 btv->fbuffer=(unsigned char *) rvmalloc(gbuffers*gbufsize);
326 else
327 printk(KERN_ERR "bttv%d: Double alloc of fbuffer!\n",
328 btv->nr);
329 if(!btv->fbuffer)
330 return -ENOBUFS;
331 return 0;
335 /* ----------------------------------------------------------------------- */
336 /* I2C functions */
338 static void bttv_bit_setscl(void *data, int state)
340 struct bttv *btv = (struct bttv*)data;
342 if (state)
343 btv->i2c_state |= 0x02;
344 else
345 btv->i2c_state &= ~0x02;
346 btwrite(btv->i2c_state, BT848_I2C);
347 btread(BT848_I2C);
350 static void bttv_bit_setsda(void *data, int state)
352 struct bttv *btv = (struct bttv*)data;
354 if (state)
355 btv->i2c_state |= 0x01;
356 else
357 btv->i2c_state &= ~0x01;
358 btwrite(btv->i2c_state, BT848_I2C);
359 btread(BT848_I2C);
362 static int bttv_bit_getscl(void *data)
364 struct bttv *btv = (struct bttv*)data;
365 int state;
367 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
368 return state;
371 static int bttv_bit_getsda(void *data)
373 struct bttv *btv = (struct bttv*)data;
374 int state;
376 state = btread(BT848_I2C) & 0x01;
377 return state;
380 static void bttv_inc_use(struct i2c_adapter *adap)
382 MOD_INC_USE_COUNT;
385 static void bttv_dec_use(struct i2c_adapter *adap)
387 MOD_DEC_USE_COUNT;
390 static void call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
392 int i;
394 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
395 if (NULL == btv->i2c_clients[i])
396 continue;
397 if (NULL == btv->i2c_clients[i]->driver->command)
398 continue;
399 btv->i2c_clients[i]->driver->command(
400 btv->i2c_clients[i],cmd,arg);
404 static int attach_inform(struct i2c_client *client)
406 struct bttv *btv = (struct bttv*)client->adapter->data;
407 int i;
409 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
410 if (btv->i2c_clients[i] == NULL ||
411 btv->i2c_clients[i]->driver->id == client->driver->id) {
412 btv->i2c_clients[i] = client;
413 break;
416 if (btv->tuner_type != -1)
417 call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
418 if (verbose)
419 printk("bttv%d: i2c attach [%s]\n",btv->nr,client->name);
420 return 0;
423 static int detach_inform(struct i2c_client *client)
425 struct bttv *btv = (struct bttv*)client->adapter->data;
426 int i;
428 if (verbose)
429 printk("bttv%d: i2c detach [%s]\n",btv->nr,client->name);
430 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
431 if (NULL != btv->i2c_clients[i] &&
432 btv->i2c_clients[i]->driver->id == client->driver->id) {
433 btv->i2c_clients[i] = NULL;
434 break;
437 return 0;
440 static struct i2c_algo_bit_data i2c_algo_template = {
441 NULL,
442 bttv_bit_setsda,
443 bttv_bit_setscl,
444 bttv_bit_getsda,
445 bttv_bit_getscl,
446 10, 10, 100,
449 static struct i2c_adapter i2c_adap_template = {
450 "bt848",
451 I2C_HW_B_BT848,
452 NULL,
453 NULL,
454 bttv_inc_use,
455 bttv_dec_use,
456 attach_inform,
457 detach_inform,
458 NULL,
461 static struct i2c_client i2c_client_template = {
462 "bttv internal",
466 NULL,
467 NULL
470 static int __init init_bttv_i2c(struct bttv *btv)
472 /* i2c bit_adapter */
473 memcpy(&btv->i2c_adap, &i2c_adap_template, sizeof(struct i2c_adapter));
474 memcpy(&btv->i2c_algo, &i2c_algo_template, sizeof(struct i2c_algo_bit_data));
475 memcpy(&btv->i2c_client, &i2c_client_template, sizeof(struct i2c_client));
477 sprintf(btv->i2c_adap.name+strlen(btv->i2c_adap.name),
478 " #%d", btv->nr);
479 btv->i2c_algo.data = btv;
480 btv->i2c_adap.data = btv;
481 btv->i2c_adap.algo_data = &btv->i2c_algo;
482 btv->i2c_client.adapter = &btv->i2c_adap;
484 bttv_bit_setscl(btv,1);
485 bttv_bit_setsda(btv,1);
487 btv->i2c_ok = i2c_bit_add_bus(&btv->i2c_adap);
488 return btv->i2c_ok;
491 /* read I2C */
492 static int __init I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
494 unsigned char buffer = 0;
496 if (0 != btv->i2c_ok)
497 return -1;
498 if (verbose && NULL != probe_for)
499 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
500 btv->nr,probe_for,addr);
501 btv->i2c_client.addr = addr >> 1;
502 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
503 if (NULL != probe_for) {
504 if (verbose)
505 printk("not found\n");
506 } else
507 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
508 btv->nr,addr);
509 return -1;
511 if (verbose && NULL != probe_for)
512 printk("found\n");
513 return buffer;
516 /* write I2C */
517 static int __init I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
518 unsigned char b2, int both)
520 unsigned char buffer[2];
521 int bytes = both ? 2 : 1;
523 if (0 != btv->i2c_ok)
524 return -1;
525 btv->i2c_client.addr = addr >> 1;
526 buffer[0] = b1;
527 buffer[1] = b2;
528 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
529 return -1;
530 return 0;
533 /* read EEPROM */
534 static void __init readee(struct bttv *btv, unsigned char *eedata, int addr)
536 int i;
538 if (I2CWrite(btv, addr, 0, -1, 0)<0) {
539 printk(KERN_WARNING "bttv: readee error\n");
540 return;
542 btv->i2c_client.addr = addr >> 1;
543 for (i=0; i<256; i+=16) {
544 if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
545 printk(KERN_WARNING "bttv: readee error\n");
546 break;
552 /* ----------------------------------------------------------------------- */
553 /* some hauppauge specific stuff */
555 static unsigned char eeprom_data[256];
556 static struct HAUPPAUGE_TUNER
558 int id;
559 char *name;
561 hauppauge_tuner[] __initdata =
563 { TUNER_ABSENT, "" },
564 { TUNER_ABSENT, "External" },
565 { TUNER_ABSENT, "Unspecified" },
566 { TUNER_ABSENT, "Philips FI1216" },
567 { TUNER_PHILIPS_SECAM, "Philips FI1216MF" },
568 { TUNER_PHILIPS_NTSC, "Philips FI1236" },
569 { TUNER_ABSENT, "Philips FI1246" },
570 { TUNER_ABSENT, "Philips FI1256" },
571 { TUNER_PHILIPS_PAL, "Philips FI1216 MK2" },
572 { TUNER_PHILIPS_SECAM, "Philips FI1216MF MK2" },
573 { TUNER_PHILIPS_NTSC, "Philips FI1236 MK2" },
574 { TUNER_PHILIPS_PAL_I, "Philips FI1246 MK2" },
575 { TUNER_ABSENT, "Philips FI1256 MK2" },
576 { TUNER_ABSENT, "Temic 4032FY5" },
577 { TUNER_TEMIC_PAL, "Temic 4002FH5" },
578 { TUNER_TEMIC_PAL_I, "Temic 4062FY5" },
579 { TUNER_ABSENT, "Philips FR1216 MK2" },
580 { TUNER_PHILIPS_SECAM, "Philips FR1216MF MK2" },
581 { TUNER_PHILIPS_NTSC, "Philips FR1236 MK2" },
582 { TUNER_PHILIPS_PAL_I, "Philips FR1246 MK2" },
583 { TUNER_ABSENT, "Philips FR1256 MK2" },
584 { TUNER_PHILIPS_PAL, "Philips FM1216" },
585 { TUNER_ABSENT, "Philips FM1216MF" },
586 { TUNER_PHILIPS_NTSC, "Philips FM1236" },
587 { TUNER_PHILIPS_PAL_I, "Philips FM1246" },
588 { TUNER_ABSENT, "Philips FM1256" },
589 { TUNER_TEMIC_4036FY5_NTSC, "Temic 4036FY5" },
590 { TUNER_ABSENT, "Samsung TCPN9082D" },
591 { TUNER_ABSENT, "Samsung TCPM9092P" },
592 { TUNER_TEMIC_PAL, "Temic 4006FH5" },
593 { TUNER_ABSENT, "Samsung TCPN9085D" },
594 { TUNER_ABSENT, "Samsung TCPB9085P" },
595 { TUNER_ABSENT, "Samsung TCPL9091P" },
596 { TUNER_ABSENT, "Temic 4039FR5" },
597 { TUNER_ABSENT, "Philips FQ1216 ME" },
598 { TUNER_TEMIC_PAL_I, "Temic 4066FY5" },
599 { TUNER_ABSENT, "Philips TD1536" },
600 { TUNER_ABSENT, "Philips TD1536D" },
601 { TUNER_ABSENT, "Philips FMR1236" },
602 { TUNER_ABSENT, "Philips FI1256MP" },
603 { TUNER_ABSENT, "Samsung TCPQ9091P" },
604 { TUNER_ABSENT, "Temic 4006FN5" },
605 { TUNER_ABSENT, "Temic 4009FR5" },
606 { TUNER_ABSENT, "Temic 4046FM5" },
609 static void __init hauppauge_eeprom(struct bttv *btv)
611 if (eeprom_data[9] < sizeof(hauppauge_tuner)/sizeof(struct HAUPPAUGE_TUNER))
613 btv->tuner_type = hauppauge_tuner[eeprom_data[9]].id;
614 if (verbose)
615 printk("bttv%d: Hauppauge eeprom: tuner=%s (%d)\n",btv->nr,
616 hauppauge_tuner[eeprom_data[9]].name,btv->tuner_type);
619 return;
622 static void __init hauppauge_boot_msp34xx(struct bttv *btv)
624 int i;
626 /* reset/enable the MSP on some Hauppauge cards */
627 /* Thanks to Kyösti Mälkki (kmalkki@cc.hut.fi)! */
628 btaor(32, ~32, BT848_GPIO_OUT_EN);
629 btaor(0, ~32, BT848_GPIO_DATA);
630 udelay(2500);
631 btaor(32, ~32, BT848_GPIO_DATA);
633 if (verbose)
634 printk("bttv%d: Hauppauge msp34xx: reset line init\n",btv->nr);
636 /* look if the msp3400 driver is already registered */
637 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
638 if (btv->i2c_clients[i] != NULL &&
639 btv->i2c_clients[i]->driver->id == I2C_DRIVERID_MSP3400) {
640 return;
644 /* if not: look for the chip ... */
645 if (I2CRead(btv, I2C_MSP3400, "MSP34xx")) {
646 /* ... if found re-register to trigger a i2c bus rescan, */
647 /* this time with the msp34xx chip activated */
648 i2c_bit_del_bus(&btv->i2c_adap);
649 i2c_bit_add_bus(&btv->i2c_adap);
654 /* Imagenation L-Model PXC200 Framegrabber */
655 /* This is basically the same procedure as
656 * used by Alessandro Rubini in his pxc200
657 * driver, but using BTTV functions */
658 static void __init init_PXC200(struct bttv *btv)
660 static const int vals[] = { 0x08, 0x09, 0x0a, 0x0b, 0x0d, 0x0d,
661 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
662 0x00 };
663 int i,tmp;
665 /* Initialise GPIO-connevted stuff */
666 btwrite(1<<13,BT848_GPIO_OUT_EN); /* Reset pin only */
667 btwrite(0,BT848_GPIO_DATA);
668 udelay(3);
669 btwrite(1<<13,BT848_GPIO_DATA);
670 /* GPIO inputs are pulled up, so no need to drive
671 * reset pin any longer */
672 btwrite(0,BT848_GPIO_OUT_EN);
674 /* we could/should try and reset/control the AD pots? but
675 right now we simply turned off the crushing. Without
676 this the AGC drifts drifts
677 remember the EN is reverse logic -->
678 setting BT848_ADC_AGC_EN disable the AGC
679 tboult@eecs.lehigh.edu
681 btwrite(BT848_ADC_RESERVED|BT848_ADC_AGC_EN, BT848_ADC);
683 /* Initialise MAX517 DAC */
684 printk(KERN_INFO "Setting DAC reference voltage level ...\n");
685 I2CWrite(btv,0x5E,0,0x80,1);
687 /* Initialise 12C508 PIC */
688 /* The I2CWrite and I2CRead commmands are actually to the
689 * same chips - but the R/W bit is included in the address
690 * argument so the numbers are different */
692 printk(KERN_INFO "Initialising 12C508 PIC chip ...\n");
694 for (i = 0; i < sizeof(vals)/sizeof(int); i++) {
695 tmp=I2CWrite(btv,0x1E,vals[i],0,1);
696 printk(KERN_INFO "I2C Write(0x08) = %i\nI2C Read () = %x\n\n",
697 tmp,I2CRead(btv,0x1F,NULL));
699 printk(KERN_INFO "PXC200 Initialised.\n");
702 /* ----------------------------------------------------------------------- */
704 static struct CARD {
705 unsigned id;
706 int cardnr;
707 char *name;
708 } cards[] __initdata = {
709 { 0x00011002, BTTV_HAUPPAUGE878, "ATI TV Wonder" },
710 { 0x00011461, BTTV_AVPHONE98, "AVerMedia TVPhone98" },
711 { 0x00031461, BTTV_AVPHONE98, "AVerMedia TVPhone98" },
712 { 0x00041461, BTTV_AVPHONE98, "AVerMedia TVPhone98" },
713 { 0x10b42636, BTTV_HAUPPAUGE878, "STB ???" },
714 { 0x1118153b, BTTV_TERRATVALUE, "Terratec TV Value" },
715 { 0x1200bd11, BTTV_PINNACLERAVE, "Pinnacle PCTV Rave" },
716 { 0x13eb0070, BTTV_HAUPPAUGE878, "Hauppauge WinTV" },
717 { 0x14610002, BTTV_AVERMEDIA98, "Avermedia TVCapture 98" },
718 { 0x18501851, BTTV_CHRONOS_VS2, "Chronos Video Shuttle II" },
719 { 0x18521852, BTTV_TYPHOON_TVIEW, "Typhoon TView TV/FM Tuner" },
720 { 0x263610b4, BTTV_STB2, "STB TV PCI FM, P/N 6000704" },
721 { 0x3000144f, BTTV_MAGICTVIEW063, "TView 99 (CPH063)" },
722 { 0x300014ff, BTTV_MAGICTVIEW061, "TView 99 (CPH061)" },
723 { 0x3002144f, BTTV_MAGICTVIEW061, "Askey Magic TView" },
724 { 0x300214ff, BTTV_PHOEBE_TVMAS, "Phoebe TV Master" },
725 { 0x402010fc, 0 /* no tvcards entry yet */, "I-O Data Co. GV-BCV3/PCI" },
726 { 0x6606217d, BTTV_WINFAST2000, "Leadtek WinFast TV 2000" },
727 { 0, -1, NULL }
730 struct tvcard
732 char *name;
733 int video_inputs;
734 int audio_inputs;
735 int tuner;
736 int svhs;
737 u32 gpiomask;
738 u32 muxsel[8];
739 u32 audiomux[6]; /* Tuner, Radio, external, internal, mute, stereo */
740 u32 gpiomask2; /* GPIO MUX mask */
742 /* look for these i2c audio chips */
743 int msp34xx:1;
744 int tda8425:1;
745 int tda9840:1;
746 int tda985x:1;
747 int tea63xx:1;
748 int tea64xx:1;
749 int tda7432:1;
750 int tda9875:1;
752 /* other settings */
753 int pll;
754 int tuner_type;
757 static struct tvcard tvcards[] =
759 /* 0x00 */
760 { " *** UNKNOWN *** ",
761 3, 1, 0, 2, 0, { 2, 3, 1, 1}, { 0, 0, 0, 0, 0},0,
762 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
763 { "MIRO PCTV",
764 4, 1, 0, 2,15, { 2, 3, 1, 1}, { 2, 0, 0, 0,10},0,
765 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
766 { "Hauppauge old",
767 4, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4},0,
768 1,1,0,1,0,0,0,1, PLL_NONE, -1 },
769 { "STB",
770 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 4, 0, 2, 3, 1},0,
771 0,1,1,1,1,0,0,1, PLL_NONE, -1 },
773 { "Intel",
774 3, 1, 0, -1, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4},0,
775 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
776 { "Diamond DTV2000",
777 3, 1, 0, 2, 3, { 2, 3, 1, 1}, { 0, 1, 0, 1, 3},0,
778 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
779 { "AVerMedia TVPhone",
780 3, 1, 0, 3,15, { 2, 3, 1, 1}, {12, 4,11,11, 0},0,
781 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
782 { "MATRIX-Vision MV-Delta",
783 5, 1, -1, 3, 0, { 2, 3, 1, 0, 0},{0 }, 0,
784 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
786 /* 0x08 */
787 { "Fly Video II",
788 3, 1, 0, 2, 0xc00, { 2, 3, 1, 1},
789 { 0, 0xc00, 0x800, 0x400, 0xc00, 0},0,
790 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
791 { "TurboTV",
792 3, 1, 0, 2, 3, { 2, 3, 1, 1}, { 1, 1, 2, 3, 0},0,
793 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
794 { "Hauppauge new (bt878)",
795 4, 1, 0, 2, 7, { 2, 0, 1, 1}, { 0, 1, 2, 3, 4},0,
796 1,1,0,1,0,0,0,1, PLL_28, -1 },
797 { "MIRO PCTV pro",
798 3, 1, 0, 2, 65551, { 2, 3, 1, 1}, {1,65537, 0, 0,10},0,
799 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
801 { "ADS Technologies Channel Surfer TV",
802 3, 1, 2, 2, 15, { 2, 3, 1, 1}, { 13, 14, 11, 7, 0, 0},0,
803 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
804 { "AVerMedia TVCapture 98",
805 3, 4, 0, 2, 15, { 2, 3, 1, 1}, { 13, 14, 11, 7, 0, 0},0,
806 1,1,1,1,0,0,0,1, PLL_28, -1 },
807 { "Aimslab VHX",
808 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 0, 1, 2, 3, 4},0,
809 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
810 { "Zoltrix TV-Max",
811 3, 1, 0, 2,15, { 2, 3, 1, 1}, {0 , 0, 1 , 0, 10},0,
812 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
814 /* 0x10 */
815 { "Pixelview PlayTV (bt878)",
816 3, 1, 0, 2, 0x01fe00, { 2, 0, 1, 1},
817 { 0x01c000, 0, 0x018000, 0x014000, 0x002000, 0 },0,
818 1,1,1,1,0,0,0,1, PLL_28, -1 },
819 { "Leadtek WinView 601",
820 3, 1, 0, 2, 0x8300f8, { 2, 3, 1, 1,0},
821 { 0x4fa007,0xcfa007,0xcfa007,0xcfa007,0xcfa007,0xcfa007},0,
822 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
823 { "AVEC Intercapture",
824 3, 2, 0, 2, 0, {2, 3, 1, 1}, {1, 0, 0, 0, 0},0,
825 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
826 { "LifeView FlyKit w/o Tuner",
827 3, 1, -1, -1, 0x8dff00, { 2, 3, 1, 1}, { 0 },0,
828 0,0,0,0,0,0,0,1, PLL_NONE, -1 },
830 { "CEI Raffles Card",
831 3, 3, 0, 2, 0, {2, 3, 1, 1}, {0, 0, 0, 0 ,0},0,
832 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
833 { "Lucky Star Image World ConferenceTV",
834 3, 1, 0, 2, 0x00fffe07, { 2, 3, 1, 1}, { 131072, 1, 1638400, 3, 4},0,
835 1,1,1,1,0,0,0,1, PLL_28, TUNER_PHILIPS_PAL_I },
836 { "Phoebe Tv Master + FM",
837 3, 1, 0, 2, 0xc00, { 2, 3, 1, 1},{0, 1, 0x800, 0x400, 0xc00, 0},0,
838 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
839 { "Modular Technology MM205 PCTV, bt878",
840 2, 1, 0, -1, 7, { 2, 3 }, { 0, 0, 0, 0, 0 },0,
841 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
843 /* 0x18 */
844 { "Askey/Typhoon/Anubis Magic TView CPH051/061 (bt878)",
845 3, 1, 0, 2, 0xe00, { 2, 3, 1, 1}, {0x400, 0x400, 0x400, 0x400, 0},0,
846 1,1,1,1,0,0,0,1, PLL_28, -1 },
847 { "Terratec/Vobis TV-Boostar",
848 3, 1, 0, 2, 16777215 , { 2, 3, 1, 1}, { 131072, 1, 1638400, 3,4},0,
849 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
850 { "Newer Hauppauge WinCam (bt878)",
851 4, 1, 0, 3, 7, { 2, 0, 1, 1}, { 0, 1, 2, 3, 4},0,
852 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
853 { "MAXI TV Video PCI2",
854 3, 1, 0, 2, 0xffff, { 2, 3, 1, 1}, { 0, 1, 2, 3, 0xc00},0,
855 1,1,1,1,0,0,0,1, PLL_NONE, TUNER_PHILIPS_SECAM },
857 { "Terratec TerraTV+",
858 3, 1, 0, 2, 0x70000, { 2, 3, 1, 1},
859 { 0x20000, 0x30000, 0x00000, 0x10000, 0x40000},0,
860 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
861 { "Imagenation PXC200",
862 5, 1, -1, 4, 0, { 2, 3, 1, 0, 0}, { 0 }, 0,
863 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
864 { "FlyVideo 98",
865 3, 1, 0, 2, 0x8dff00, {2, 3, 1, 1},
866 { 0, 0x8dff00, 0x8df700, 0x8de700, 0x8dff00, 0 },0,
867 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
868 { "iProTV",
869 3, 1, 0, 2, 1, { 2, 3, 1, 1}, { 1, 0, 0, 0, 0 },0,
870 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
872 /* 0x20 */
873 { "Intel Create and Share PCI",
874 4, 1, 0, 2, 7, { 2, 3, 1, 1}, { 4, 4, 4, 4, 4},0,
875 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
876 { "Terratec TerraTValue",
877 3, 1, 0, 2, 0xffff00, { 2, 3, 1, 1},
878 { 0x500, 0, 0x300, 0x900, 0x900},0,
879 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
880 { "Leadtek WinFast 2000",
881 3, 1, 0, 2, 0xfff000, { 2, 3, 1, 1,0},
882 { 0x621000,0x620100,0x621100,0x620000,0xE210000,0x620000},0,
883 1,1,1,1,1,0,0,1, PLL_28, -1 },
884 { "Chronos Video Shuttle II",
885 3, 3, 0, 2, 0x1800, { 2, 3, 1, 1}, { 0, 0, 0x1000, 0x1000, 0x0800},0,
886 1,1,1,1,0,0,0,1, PLL_28, -1 },
888 { "Typhoon TView TV/FM Tuner",
889 3, 3, 0, 2, 0x1800, { 2, 3, 1, 1}, { 0, 0x800, 0, 0, 0x1800, 0 },0,
890 1,1,1,1,0,0,0,1, PLL_28, -1 },
891 { "PixelView PlayTV pro",
892 3, 1, 0, 2, 0xff, { 2, 3, 1, 1 },
893 { 0x21, 0x20, 0x24, 0x2c, 0x29, 0x29 }, 0,
894 0,0,0,0,0,0,0,1, PLL_28, -1 },
895 { "TView99 CPH063",
896 3, 1, 0, 2, 0x551e00, { 2, 3, 1, 1},
897 { 0x551400, 0x551200, 0, 0, 0, 0x551200 }, 0,
898 1,1,1,1,0,0,0,1, PLL_28, -1 },
899 { "Pinnacle PCTV Rave",
900 3, 1, 0, 2, 0x03000F, { 2, 3, 1, 1}, { 2, 0, 0, 0, 1},0,
901 1,1,1,1,0,0,0,1, PLL_NONE, -1 },
903 /* 0x28 */
904 { "STB2",
905 3, 1, 0, 2, 7, { 2, 3, 1, 1}, { 4, 0, 2, 3, 1},0,
906 0,1,1,1,0,1,1,1, PLL_NONE, -1 },
907 { "AVerMedia TVPhone 98",
908 3, 4, 0, 2, 4, { 2, 3, 1, 1}, { 13, 14, 11, 7, 0, 0},0,
909 1,1,1,1,0,0,0,1, PLL_28, 5 },
910 { "ProVideo PV951", /* pic16c54 */
911 3, 1, 0, 2, 0, { 2, 3, 1, 1}, { 0, 0, 0, 0, 0},0,
912 0,0,0,0,0,0,0,0, PLL_28, 1 },
914 #define TVCARDS (sizeof(tvcards)/sizeof(struct tvcard))
916 static void __init dump_eeprom(struct bttv *btv,int addr)
918 int i;
920 if (verbose < 2)
921 return;
922 /* for debugging: dump eeprom to syslog */
923 printk(KERN_DEBUG "bttv%d: dump eeprom @ 0x%02x\n",btv->nr,addr);
924 for (i = 0; i < 256;) {
925 printk(KERN_DEBUG " %02x:",i);
926 do {
927 printk(" %02x",eeprom_data[i++]);
928 } while (i % 16);
929 printk("\n");
933 static int __init idcard_eeprom(struct bttv *btv)
935 unsigned id;
936 int i,n;
938 id = (eeprom_data[252] << 24) |
939 (eeprom_data[253] << 16) |
940 (eeprom_data[254] << 8) |
941 (eeprom_data[255]);
942 if (id == 0 || id == 0xffffffff)
943 return -1;
945 /* look for the card */
946 for (n = -1, i = 0; cards[i].id != 0; i++)
947 if (cards[i].id == id)
948 n = i;
950 if (n != -1) {
951 /* found it */
952 printk(KERN_INFO "bttv%d: id: %s (0x%08x)\n",
953 btv->nr,cards[n].name,id);
954 if (verbose)
955 printk(KERN_INFO "bttv%d: => card=%d (%s)\n",
956 btv->nr,cards[n].cardnr,
957 tvcards[cards[n].cardnr].name);
958 return cards[n].cardnr;
959 } else {
960 /* 404 */
961 printk(KERN_INFO "bttv%d: id: unknown (0x%08x)\n",
962 btv->nr, id);
963 printk(KERN_INFO "please mail id, board name and "
964 "the correct card= insmod option to "
965 "kraxel@goldbach.in-berlin.de\n");
966 return -1;
970 /* ----------------------------------------------------------------------- */
972 static void audio(struct bttv *btv, int mode, int no_irq_context)
974 btaor(tvcards[btv->type].gpiomask, ~tvcards[btv->type].gpiomask,
975 BT848_GPIO_OUT_EN);
977 switch (mode)
979 case AUDIO_MUTE:
980 btv->audio|=AUDIO_MUTE;
981 break;
982 case AUDIO_UNMUTE:
983 btv->audio&=~AUDIO_MUTE;
984 mode=btv->audio;
985 break;
986 case AUDIO_OFF:
987 mode=AUDIO_OFF;
988 break;
989 case AUDIO_ON:
990 mode=btv->audio;
991 break;
992 default:
993 btv->audio&=AUDIO_MUTE;
994 btv->audio|=mode;
995 break;
997 /* if audio mute or not in H-lock, turn audio off */
998 if ((btv->audio&AUDIO_MUTE))
999 mode=AUDIO_OFF;
1000 if ((mode == AUDIO_TUNER) && (btv->radio))
1001 mode = AUDIO_RADIO;
1002 btaor(tvcards[btv->type].audiomux[mode],
1003 ~tvcards[btv->type].gpiomask, BT848_GPIO_DATA);
1004 if (no_irq_context)
1005 call_i2c_clients(btv,AUDC_SET_INPUT,&(mode));
1009 extern inline void bt848_dma(struct bttv *btv, uint state)
1011 if (state)
1012 btor(3, BT848_GPIO_DMA_CTL);
1013 else
1014 btand(~3, BT848_GPIO_DMA_CTL);
1018 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC*/
1020 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
1021 PLL_X = Reference pre-divider (0=1, 1=2)
1022 PLL_C = Post divider (0=6, 1=4)
1023 PLL_I = Integer input
1024 PLL_F = Fractional input
1026 F_input = 28.636363 MHz:
1027 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
1030 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
1032 unsigned char fl, fh, fi;
1034 /* prevent overflows */
1035 fin/=4;
1036 fout/=4;
1038 fout*=12;
1039 fi=fout/fin;
1041 fout=(fout%fin)*256;
1042 fh=fout/fin;
1044 fout=(fout%fin)*256;
1045 fl=fout/fin;
1047 /*printk("0x%02x 0x%02x 0x%02x\n", fi, fh, fl);*/
1048 btwrite(fl, BT848_PLL_F_LO);
1049 btwrite(fh, BT848_PLL_F_HI);
1050 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
1053 static int set_pll(struct bttv *btv)
1055 int i;
1056 unsigned long tv;
1058 if (!btv->pll.pll_crystal)
1059 return 0;
1061 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
1062 /* no PLL needed */
1063 if (btv->pll.pll_current == 0) {
1064 /* printk ("bttv%d: PLL: is off\n",btv->nr); */
1065 return 0;
1067 printk ("bttv%d: PLL: switching off\n",btv->nr);
1068 btwrite(0x00,BT848_TGCTRL);
1069 btwrite(0x00,BT848_PLL_XCI);
1070 btv->pll.pll_current = 0;
1071 return 0;
1074 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
1075 /* printk("bttv%d: PLL: no change required\n",btv->nr); */
1076 return 1;
1079 if (verbose)
1080 printk("bttv%d: PLL: %d => %d ... ",btv->nr,
1081 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1083 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
1085 /* Let other people run while the PLL stabilizes */
1086 tv=jiffies+HZ/10; /* .1 seconds */
1089 schedule();
1091 while(time_before(jiffies,tv));
1093 for (i=0; i<100; i++)
1095 if ((btread(BT848_DSTATUS)&BT848_DSTATUS_PLOCK))
1096 btwrite(0,BT848_DSTATUS);
1097 else
1099 btwrite(0x08,BT848_TGCTRL);
1100 btv->pll.pll_current = btv->pll.pll_ofreq;
1101 if (verbose)
1102 printk("ok\n");
1103 return 1;
1105 mdelay(10);
1107 btv->pll.pll_current = 0;
1108 if (verbose)
1109 printk("oops\n");
1110 return -1;
1113 static void bt848_muxsel(struct bttv *btv, unsigned int input)
1115 btaor(tvcards[btv->type].gpiomask2,~tvcards[btv->type].gpiomask2,
1116 BT848_GPIO_OUT_EN);
1118 /* This seems to get rid of some synchronization problems */
1119 btand(~(3<<5), BT848_IFORM);
1120 mdelay(10);
1123 input %= tvcards[btv->type].video_inputs;
1124 if (input==tvcards[btv->type].svhs)
1126 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
1127 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
1129 else
1131 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
1132 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
1134 btaor((tvcards[btv->type].muxsel[input&7]&3)<<5, ~(3<<5), BT848_IFORM);
1135 audio(btv, (input!=tvcards[btv->type].tuner) ?
1136 AUDIO_EXTERN : AUDIO_TUNER, 1);
1137 btaor(tvcards[btv->type].muxsel[input]>>4,
1138 ~tvcards[btv->type].gpiomask2, BT848_GPIO_DATA);
1142 struct tvnorm
1144 u32 Fsc;
1145 u16 swidth, sheight; /* scaled standard width, height */
1146 u16 totalwidth;
1147 u8 adelay, bdelay, iform;
1148 u32 scaledtwidth;
1149 u16 hdelayx1, hactivex1;
1150 u16 vdelay;
1151 u8 vbipack;
1154 static struct tvnorm tvnorms[] = {
1155 /* PAL-BDGHI */
1156 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
1157 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
1158 { 35468950,
1159 924, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
1160 1135, 186, 924, 0x20, 255},
1162 /* NTSC */
1163 { 28636363,
1164 768, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC|BT848_IFORM_XT0),
1165 910, 128, 910, 0x1a, 144},
1166 #if 0
1167 /* SECAM EAST */
1168 { 35468950,
1169 768, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM|BT848_IFORM_XT1),
1170 944, 186, 922, 0x20, 255},
1171 #else
1172 /* SECAM L */
1173 { 35468950,
1174 924, 576, 1135, 0x7f, 0xb0, (BT848_IFORM_SECAM|BT848_IFORM_XT1),
1175 1135, 186, 922, 0x20, 255},
1176 #endif
1177 /* PAL-NC */
1178 { 28636363,
1179 640, 576, 910, 0x68, 0x5d, (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
1180 780, 130, 734, 0x1a, 144},
1181 /* PAL-M */
1182 { 28636363,
1183 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
1184 780, 135, 754, 0x1a, 144},
1185 /* PAL-N */
1186 { 35468950,
1187 768, 576, 1135, 0x7f, 0x72, (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
1188 944, 186, 922, 0x20, 144},
1189 /* NTSC-Japan */
1190 { 28636363,
1191 640, 480, 910, 0x68, 0x5d, (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
1192 780, 135, 754, 0x16, 144},
1194 #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
1195 #define VBI_SPL 2044
1197 /* RISC command to write one VBI data line */
1198 #define VBI_RISC BT848_RISC_WRITE|VBI_SPL|BT848_RISC_EOL|BT848_RISC_SOL
1200 static void make_vbitab(struct bttv *btv)
1202 int i;
1203 unsigned int *po=(unsigned int *) btv->vbi_odd;
1204 unsigned int *pe=(unsigned int *) btv->vbi_even;
1206 if (debug)
1207 printk("bttv%d: vbi1: po=%08lx pe=%08lx\n",
1208 btv->nr,virt_to_bus(po), virt_to_bus(pe));
1210 *(po++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1); *(po++)=0;
1211 for (i=0; i<16; i++)
1213 *(po++)=cpu_to_le32(VBI_RISC);
1214 *(po++)=cpu_to_le32(kvirt_to_bus((unsigned long)btv->vbibuf+i*2048));
1216 *(po++)=cpu_to_le32(BT848_RISC_JUMP);
1217 *(po++)=cpu_to_le32(virt_to_bus(btv->risc_jmp+4));
1219 *(pe++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1); *(pe++)=0;
1220 for (i=16; i<32; i++)
1222 *(pe++)=cpu_to_le32(VBI_RISC);
1223 *(pe++)=cpu_to_le32(kvirt_to_bus((unsigned long)btv->vbibuf+i*2048));
1225 *(pe++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(0x01<<16));
1226 *(pe++)=cpu_to_le32(virt_to_bus(btv->risc_jmp+10));
1228 if (debug)
1229 printk("bttv%d: vbi2: po=%08lx pe=%08lx\n",
1230 btv->nr,virt_to_bus(po), virt_to_bus(pe));
1233 static int fmtbppx2[16] = {
1234 8, 6, 4, 4, 4, 3, 2, 2, 4, 3, 0, 0, 0, 0, 2, 0
1237 static int palette2fmt[] = {
1239 BT848_COLOR_FMT_Y8,
1240 BT848_COLOR_FMT_RGB8,
1241 BT848_COLOR_FMT_RGB16,
1242 BT848_COLOR_FMT_RGB24,
1243 BT848_COLOR_FMT_RGB32,
1244 BT848_COLOR_FMT_RGB15,
1245 BT848_COLOR_FMT_YUY2,
1246 BT848_COLOR_FMT_BtYUV,
1250 BT848_COLOR_FMT_RAW,
1251 BT848_COLOR_FMT_YCrCb422,
1252 BT848_COLOR_FMT_YCrCb411,
1253 BT848_COLOR_FMT_YCrCb422,
1254 BT848_COLOR_FMT_YCrCb411,
1256 #define PALETTEFMT_MAX (sizeof(palette2fmt)/sizeof(int))
1258 static int make_rawrisctab(struct bttv *btv, unsigned int *ro,
1259 unsigned int *re, unsigned int *vbuf)
1261 unsigned long line;
1262 unsigned long bpl=1024; /* bytes per line */
1263 unsigned long vadr=(unsigned long) vbuf;
1265 *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1266 *(ro++)=cpu_to_le32(0);
1267 *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1268 *(re++)=cpu_to_le32(0);
1270 /* In PAL 650 blocks of 256 DWORDs are sampled, but only if VDELAY
1271 is 2 and without separate VBI grabbing.
1272 We'll have to handle this inside the IRQ handler ... */
1274 for (line=0; line < 640; line++)
1276 *(ro++)=cpu_to_le32(BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL);
1277 *(ro++)=cpu_to_le32(kvirt_to_bus(vadr));
1278 *(re++)=cpu_to_le32(BT848_RISC_WRITE|bpl|BT848_RISC_SOL|BT848_RISC_EOL);
1279 *(re++)=cpu_to_le32(kvirt_to_bus(vadr+gbufsize/2));
1280 vadr+=bpl;
1283 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
1284 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
1285 *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
1286 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
1288 return 0;
1291 static int make_prisctab(struct bttv *btv, unsigned int *ro,
1292 unsigned int *re,
1293 unsigned int *vbuf, unsigned short width,
1294 unsigned short height, unsigned short fmt)
1296 unsigned long line, lmask;
1297 unsigned long bl, blcr, blcb, rcmd;
1298 unsigned long todo;
1299 unsigned int **rp;
1300 int inter;
1301 unsigned long cbadr, cradr;
1302 unsigned long vadr=(unsigned long) vbuf;
1303 int shift, csize;
1305 if (debug)
1306 printk("bttv%d: prisc1: ro=%08lx re=%08lx\n",
1307 btv->nr,virt_to_bus(ro), virt_to_bus(re));
1309 switch(fmt)
1311 case VIDEO_PALETTE_YUV422P:
1312 csize=(width*height)>>1;
1313 shift=1;
1314 lmask=0;
1315 break;
1317 case VIDEO_PALETTE_YUV411P:
1318 csize=(width*height)>>2;
1319 shift=2;
1320 lmask=0;
1321 break;
1323 case VIDEO_PALETTE_YUV420P:
1324 csize=(width*height)>>2;
1325 shift=1;
1326 lmask=1;
1327 break;
1329 case VIDEO_PALETTE_YUV410P:
1330 csize=(width*height)>>4;
1331 shift=2;
1332 lmask=3;
1333 break;
1335 default:
1336 return -1;
1338 cbadr=vadr+(width*height);
1339 cradr=cbadr+csize;
1340 inter = (height>tvnorms[btv->win.norm].sheight/2) ? 1 : 0;
1342 *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
1343 *(ro++)=0;
1344 *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
1345 *(re++)=0;
1347 for (line=0; line < (height<<(1^inter)); line++)
1349 if(line==height)
1351 vadr+=csize<<1;
1352 cbadr=vadr+(width*height);
1353 cradr=cbadr+csize;
1355 if (inter)
1356 rp= (line&1) ? &re : &ro;
1357 else
1358 rp= (line>=height) ? &ro : &re;
1361 if(line&lmask)
1362 rcmd=BT848_RISC_WRITE1S23|BT848_RISC_SOL;
1363 else
1364 rcmd=BT848_RISC_WRITE123|BT848_RISC_SOL;
1366 todo=width;
1367 while(todo)
1369 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
1370 blcr=(PAGE_SIZE-((PAGE_SIZE-1)&cradr))<<shift;
1371 blcb=(PAGE_SIZE-((PAGE_SIZE-1)&cbadr))<<shift;
1372 bl=(blcr<bl) ? blcr : bl;
1373 bl=(blcb<bl) ? blcb : bl;
1374 bl=(bl>todo) ? todo : bl;
1375 blcr=bl>>shift;
1376 blcb=blcr;
1377 /* bl now containts the longest row that can be written */
1378 todo-=bl;
1379 if(!todo) rcmd|=BT848_RISC_EOL; /* if this is the last EOL */
1381 *((*rp)++)=cpu_to_le32(rcmd|bl);
1382 *((*rp)++)=cpu_to_le32(blcb|(blcr<<16));
1383 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
1384 vadr+=bl;
1385 if((rcmd&(15<<28))==BT848_RISC_WRITE123)
1387 *((*rp)++)=(kvirt_to_bus(cbadr));
1388 cbadr+=blcb;
1389 *((*rp)++)=cpu_to_le32(kvirt_to_bus(cradr));
1390 cradr+=blcr;
1393 rcmd&=~BT848_RISC_SOL; /* only the first has SOL */
1397 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
1398 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
1399 *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
1400 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
1402 if (debug)
1403 printk("bttv%d: prisc2: ro=%08lx re=%08lx\n",
1404 btv->nr,virt_to_bus(ro), virt_to_bus(re));
1406 return 0;
1409 static int make_vrisctab(struct bttv *btv, unsigned int *ro,
1410 unsigned int *re,
1411 unsigned int *vbuf, unsigned short width,
1412 unsigned short height, unsigned short palette)
1414 unsigned long line;
1415 unsigned long bpl; /* bytes per line */
1416 unsigned long bl;
1417 unsigned long todo;
1418 unsigned int **rp;
1419 int inter;
1420 unsigned long vadr=(unsigned long) vbuf;
1422 if (palette==VIDEO_PALETTE_RAW)
1423 return make_rawrisctab(btv, ro, re, vbuf);
1424 if (palette>=VIDEO_PALETTE_PLANAR)
1425 return make_prisctab(btv, ro, re, vbuf, width, height, palette);
1427 if (debug)
1428 printk("bttv%d: vrisc1: ro=%08lx re=%08lx\n",
1429 btv->nr,virt_to_bus(ro), virt_to_bus(re));
1431 inter = (height>tvnorms[btv->win.norm].sheight/2) ? 1 : 0;
1432 bpl=width*fmtbppx2[palette2fmt[palette]&0xf]/2;
1434 *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1435 *(ro++)=cpu_to_le32(0);
1436 *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1437 *(re++)=cpu_to_le32(0);
1439 for (line=0; line < (height<<(1^inter)); line++)
1441 if (inter)
1442 rp= (line&1) ? &re : &ro;
1443 else
1444 rp= (line>=height) ? &ro : &re;
1446 bl=PAGE_SIZE-((PAGE_SIZE-1)&vadr);
1447 if (bpl<=bl)
1449 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
1450 BT848_RISC_EOL|bpl);
1451 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
1452 vadr+=bpl;
1454 else
1456 todo=bpl;
1457 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|bl);
1458 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
1459 vadr+=bl;
1460 todo-=bl;
1461 while (todo>PAGE_SIZE)
1463 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|PAGE_SIZE);
1464 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
1465 vadr+=PAGE_SIZE;
1466 todo-=PAGE_SIZE;
1468 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_EOL|todo);
1469 *((*rp)++)=cpu_to_le32(kvirt_to_bus(vadr));
1470 vadr+=todo;
1474 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
1475 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
1476 *(re++)=cpu_to_le32(BT848_RISC_JUMP|BT848_RISC_IRQ|(2<<16));
1477 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
1479 if (debug)
1480 printk("bttv%d: vrisc2: ro=%08lx re=%08lx\n",
1481 btv->nr,virt_to_bus(ro), virt_to_bus(re));
1483 return 0;
1486 static unsigned char lmaskt[8] =
1487 { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
1488 static unsigned char rmaskt[8] =
1489 { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
1491 static void clip_draw_rectangle(unsigned char *clipmap, int x, int y, int w, int h)
1493 unsigned char lmask, rmask, *p;
1494 int W, l, r;
1495 int i;
1497 if (debug)
1498 printk("bttv clip: %dx%d+%d+%d\n",w,h,x,y);
1500 /* bitmap is fixed width, 128 bytes (1024 pixels represented) */
1501 if (x<0)
1503 w+=x;
1504 x=0;
1506 if (y<0)
1508 h+=y;
1509 y=0;
1511 if (w < 0 || h < 0) /* catch bad clips */
1512 return;
1513 /* out of range data should just fall through */
1514 if (y+h>=625)
1515 h=625-y;
1516 if (x+w>=1024)
1517 w=1024-x;
1519 l=x>>3;
1520 r=(x+w-1)>>3;
1521 W=r-l-1;
1522 lmask=lmaskt[x&7];
1523 rmask=rmaskt[(x+w-1)&7];
1524 p=clipmap+128*y+l;
1526 if (W>0)
1528 for (i=0; i<h; i++, p+=128)
1530 *p|=lmask;
1531 memset(p+1, 0xff, W);
1532 p[W+1]|=rmask;
1534 } else if (!W) {
1535 for (i=0; i<h; i++, p+=128)
1537 p[0]|=lmask;
1538 p[1]|=rmask;
1540 } else {
1541 for (i=0; i<h; i++, p+=128)
1542 p[0]|=lmask&rmask;
1548 static void make_clip_tab(struct bttv *btv, struct video_clip *cr, int ncr)
1550 int i, line, x, y, bpl, width, height, inter, maxw;
1551 unsigned int bpp, dx, sx, **rp, *ro, *re, flags, len;
1552 unsigned long adr;
1553 unsigned char *clipmap, *clipline, cbit, lastbit, outofmem;
1555 /* take care: bpp != btv->win.bpp is allowed here */
1556 bpp = fmtbppx2[btv->win.color_fmt&0xf]/2;
1557 bpl=btv->win.bpl;
1558 adr=btv->win.vidadr + btv->win.x * btv->win.bpp + btv->win.y * bpl;
1559 inter=(btv->win.interlace&1)^1;
1560 width=btv->win.width;
1561 height=btv->win.height;
1562 if (debug)
1563 printk("bttv%d: clip1: pal=%d size=%dx%d, bpl=%d bpp=%d\n",
1564 btv->nr,btv->picture.palette,width,height,bpl,bpp);
1565 if(width > 1023)
1566 width = 1023; /* sanity check */
1567 if(height > 625)
1568 height = 625; /* sanity check */
1569 ro=btv->risc_scr_odd;
1570 re=btv->risc_scr_even;
1572 if (debug)
1573 printk("bttv%d: clip: ro=%08lx re=%08lx\n",
1574 btv->nr,virt_to_bus(ro), virt_to_bus(re));
1576 if ((clipmap=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL) {
1577 /* can't clip, don't generate any risc code */
1578 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
1579 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
1580 *(re++)=cpu_to_le32(BT848_RISC_JUMP);
1581 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
1583 if (ncr < 0) { /* bitmap was pased */
1584 memcpy(clipmap, (unsigned char *)cr, VIDEO_CLIPMAP_SIZE);
1585 } else { /* convert rectangular clips to a bitmap */
1586 memset(clipmap, 0, VIDEO_CLIPMAP_SIZE); /* clear map */
1587 for (i=0; i<ncr; i++)
1588 clip_draw_rectangle(clipmap, cr[i].x, cr[i].y,
1589 cr[i].width, cr[i].height);
1591 /* clip against viewing window AND screen
1592 so we do not have to rely on the user program
1594 maxw = (bpl - btv->win.x * btv->win.bpp) / bpp;
1595 clip_draw_rectangle(clipmap, (width > maxw) ? maxw : width,
1596 0, 1024, 768);
1597 clip_draw_rectangle(clipmap,0,(btv->win.y+height>btv->win.sheight) ?
1598 (btv->win.sheight-btv->win.y) : height,1024,768);
1599 if (btv->win.x<0)
1600 clip_draw_rectangle(clipmap, 0, 0, -(btv->win.x), 768);
1601 if (btv->win.y<0)
1602 clip_draw_rectangle(clipmap, 0, 0, 1024, -(btv->win.y));
1604 *(ro++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1605 *(ro++)=cpu_to_le32(0);
1606 *(re++)=cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
1607 *(re++)=cpu_to_le32(0);
1609 /* translate bitmap to risc code */
1610 for (line=outofmem=0; line < (height<<inter) && !outofmem; line++)
1612 y = line>>inter;
1613 rp= (line&1) ? &re : &ro;
1614 clipline = clipmap + (y<<7); /* running pointers ... */
1615 lastbit = *clipline & 1;
1616 for(x=dx=0,sx=0; x<=width && !outofmem;) {
1617 if (0 == (x&7)) {
1618 /* check bytes not bits if we can ... */
1619 if (lastbit) {
1620 while (0xff==*clipline && x<width-8) {
1621 x += 8;
1622 dx += 8;
1623 clipline++;
1625 } else {
1626 while (0x00==*clipline && x<width-8) {
1627 x += 8;
1628 dx += 8;
1629 clipline++;
1633 cbit = *clipline & (1<<(x&7));
1634 if (x < width && !lastbit == !cbit) {
1635 dx++;
1636 } else {
1637 /* generate the dma controller code */
1638 len = dx * bpp;
1639 flags = ((bpp==4) ? BT848_RISC_BYTE3 : 0);
1640 flags |= ((!sx) ? BT848_RISC_SOL : 0);
1641 flags |= ((sx + dx == width) ? BT848_RISC_EOL : 0);
1642 if (!lastbit) {
1643 *((*rp)++)=cpu_to_le32(BT848_RISC_WRITE|flags|len);
1644 *((*rp)++)=cpu_to_le32(adr + bpp * sx);
1645 } else {
1646 *((*rp)++)=cpu_to_le32(BT848_RISC_SKIP|flags|len);
1648 lastbit=cbit;
1649 sx += dx;
1650 dx = 1;
1651 if (ro - btv->risc_scr_odd>RISCMEM_LEN/2 - 16)
1652 outofmem++;
1653 if (re - btv->risc_scr_even>RISCMEM_LEN/2 - 16)
1654 outofmem++;
1656 x++;
1657 if (0 == (x&7))
1658 clipline++;
1660 if ((!inter)||(line&1))
1661 adr+=bpl;
1664 vfree(clipmap);
1665 /* outofmem flag relies on the following code to discard extra data */
1666 *(ro++)=cpu_to_le32(BT848_RISC_JUMP);
1667 *(ro++)=cpu_to_le32(btv->bus_vbi_even);
1668 *(re++)=cpu_to_le32(BT848_RISC_JUMP);
1669 *(re++)=cpu_to_le32(btv->bus_vbi_odd);
1671 if (debug)
1672 printk("bttv%d: clip2: pal=%d size=%dx%d, bpl=%d bpp=%d\n",
1673 btv->nr,btv->picture.palette,width,height,bpl,bpp);
1677 * Set the registers for the size we have specified. Don't bother
1678 * trying to understand this without the BT848 manual in front of
1679 * you [AC].
1681 * PS: The manual is free for download in .pdf format from
1682 * www.brooktree.com - nicely done those folks.
1685 static inline void bt848_set_eogeo(struct bttv *btv, struct tvnorm *tvn,
1686 int odd, int width, int height)
1688 u16 vscale, hscale;
1689 u32 xsf, sr;
1690 u16 hdelay;
1691 u8 crop, vtc;
1692 int inter = (height>tvn->sheight/2) ? 0 : 1;
1693 int off = odd ? 0x80 : 0x00;
1695 xsf = (width*tvn->scaledtwidth)/tvn->swidth;
1696 hscale = ((tvn->totalwidth*4096UL)/xsf-4096);
1697 hdelay = tvn->hdelayx1;
1698 hdelay = (hdelay*width)/tvn->swidth;
1699 hdelay &= 0x3fe;
1700 sr=((tvn->sheight>>inter)*512)/height-512;
1701 vscale=(0x10000UL-sr)&0x1fff;
1702 crop=((width>>8)&0x03)|((hdelay>>6)&0x0c)|
1703 ((tvn->sheight>>4)&0x30)|((tvn->vdelay>>2)&0xc0);
1704 vscale |= inter ? (BT848_VSCALE_INT<<8) : 0;
1706 #if 0
1707 /* Some people say interpolation looks bad ... */
1708 vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
1709 if (width < 767)
1710 btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
1711 else
1712 btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
1713 #else
1714 vtc = 0;
1715 btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
1716 #endif
1718 btwrite(vtc, BT848_E_VTC+off);
1719 btwrite(hscale>>8, BT848_E_HSCALE_HI+off);
1720 btwrite(hscale&0xff, BT848_E_HSCALE_LO+off);
1721 btaor((vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
1722 btwrite(vscale&0xff, BT848_E_VSCALE_LO+off);
1723 btwrite(width&0xff, BT848_E_HACTIVE_LO+off);
1724 btwrite(hdelay&0xff, BT848_E_HDELAY_LO+off);
1725 btwrite(tvn->sheight&0xff, BT848_E_VACTIVE_LO+off);
1726 btwrite(tvn->vdelay&0xff, BT848_E_VDELAY_LO+off);
1727 btwrite(crop, BT848_E_CROP+off);
1731 static void bt848_set_geo(struct bttv *btv,
1732 int no_irq_context)
1734 u16 ewidth, eheight, owidth, oheight;
1735 u16 format, bswap;
1736 struct tvnorm *tvn;
1737 unsigned long flags;
1739 spin_lock_irqsave(&btv->s_lock, flags);
1741 tvn=&tvnorms[btv->win.norm];
1743 btwrite(tvn->adelay, BT848_ADELAY);
1744 btwrite(tvn->bdelay, BT848_BDELAY);
1745 btaor(tvn->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH), BT848_IFORM);
1746 btwrite(tvn->vbipack, BT848_VBI_PACK_SIZE);
1747 btwrite(1, BT848_VBI_PACK_DEL);
1749 btv->pll.pll_ofreq = tvn->Fsc;
1750 if (no_irq_context)
1751 set_pll(btv);
1753 btv->win.interlace = (btv->win.height>tvn->sheight/2) ? 1 : 0;
1755 if (0 == btv->risc_cap_odd &&
1756 0 == btv->risc_cap_even) {
1757 /* overlay only */
1758 owidth = btv->win.width;
1759 oheight = btv->win.height;
1760 ewidth = btv->win.width;
1761 eheight = btv->win.height;
1762 format = btv->win.color_fmt;
1763 bswap = btv->fb_color_ctl;
1764 } else if (-1 != btv->gq_grab &&
1765 0 == btv->risc_cap_odd &&
1766 !btv->win.interlace &&
1767 btv->scr_on) {
1768 /* odd field -> overlay, even field -> capture */
1769 owidth = btv->win.width;
1770 oheight = btv->win.height;
1771 ewidth = btv->gbuf[btv->gq_grab].width;
1772 eheight = btv->gbuf[btv->gq_grab].height;
1773 format = (btv->win.color_fmt & 0xf0) |
1774 (btv->gbuf[btv->gq_grab].fmt & 0x0f);
1775 bswap = btv->fb_color_ctl & 0x0a;
1776 } else {
1777 /* capture only */
1778 owidth = btv->gbuf[btv->gq_grab].width;
1779 oheight = btv->gbuf[btv->gq_grab].height;
1780 ewidth = btv->gbuf[btv->gq_grab].width;
1781 eheight = btv->gbuf[btv->gq_grab].height;
1782 format = btv->gbuf[btv->gq_grab].fmt;
1783 bswap = 0;
1786 /* program odd + even fields */
1787 bt848_set_eogeo(btv, tvn, 1, owidth, oheight);
1788 bt848_set_eogeo(btv, tvn, 0, ewidth, eheight);
1790 btwrite(format, BT848_COLOR_FMT);
1791 btwrite(bswap | BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1793 spin_unlock_irqrestore(&btv->s_lock, flags);
1797 static int bpp2fmt[4] = {
1798 BT848_COLOR_FMT_RGB8, BT848_COLOR_FMT_RGB16,
1799 BT848_COLOR_FMT_RGB24, BT848_COLOR_FMT_RGB32
1802 static void bt848_set_winsize(struct bttv *btv)
1804 unsigned short format;
1806 if (btv->picture.palette > 0 && btv->picture.palette <= VIDEO_PALETTE_YUV422) {
1807 /* format set by VIDIOCSPICT */
1808 format = palette2fmt[btv->picture.palette];
1809 } else {
1810 /* use default for the given color depth */
1811 format = (btv->win.depth==15) ? BT848_COLOR_FMT_RGB15 :
1812 bpp2fmt[(btv->win.bpp-1)&3];
1814 btv->win.color_fmt = format;
1815 if (bigendian &&
1816 format == BT848_COLOR_FMT_RGB32) {
1817 btv->fb_color_ctl =
1818 BT848_COLOR_CTL_WSWAP_ODD |
1819 BT848_COLOR_CTL_WSWAP_EVEN |
1820 BT848_COLOR_CTL_BSWAP_ODD |
1821 BT848_COLOR_CTL_BSWAP_EVEN;
1822 } else if (bigendian &&
1823 (format == BT848_COLOR_FMT_RGB16 ||
1824 format == BT848_COLOR_FMT_RGB15)) {
1825 btv->fb_color_ctl =
1826 BT848_COLOR_CTL_BSWAP_ODD |
1827 BT848_COLOR_CTL_BSWAP_EVEN;
1828 } else {
1829 btv->fb_color_ctl = 0;
1832 /* RGB8 seems to be a 9x5x5 GRB color cube starting at
1833 * color 16. Why the h... can't they even mention this in the
1834 * data sheet? [AC - because it's a standard format so I guess
1835 * it never occurred to them]
1836 * Enable dithering in this mode.
1839 if (format==BT848_COLOR_FMT_RGB8)
1840 btand(~BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1841 else
1842 btor(BT848_CAP_CTL_DITH_FRAME, BT848_CAP_CTL);
1844 bt848_set_geo(btv,1);
1849 * Grab into virtual memory.
1850 * Currently only does double buffering. Do we need more?
1853 static int vgrab(struct bttv *btv, struct video_mmap *mp)
1855 unsigned int *ro, *re;
1856 unsigned int *vbuf;
1857 unsigned long flags;
1859 if(btv->fbuffer==NULL)
1861 if(fbuffer_alloc(btv))
1862 return -ENOBUFS;
1865 if(mp->frame >= gbuffers || mp->frame < 0)
1866 return -EINVAL;
1867 if(btv->gbuf[mp->frame].stat != GBUFFER_UNUSED)
1868 return -EBUSY;
1870 if(mp->height < 32 || mp->width < 32)
1871 return -EINVAL;
1872 if (mp->format >= PALETTEFMT_MAX)
1873 return -EINVAL;
1875 if (mp->height*mp->width*fmtbppx2[palette2fmt[mp->format]&0x0f]/2
1876 > gbufsize)
1877 return -EINVAL;
1878 if(-1 == palette2fmt[mp->format])
1879 return -EINVAL;
1882 * Ok load up the BT848
1885 vbuf=(unsigned int *)(btv->fbuffer+gbufsize*mp->frame);
1886 ro=btv->gbuf[mp->frame].risc;
1887 re=ro+2048;
1888 make_vrisctab(btv, ro, re, vbuf, mp->width, mp->height, mp->format);
1890 if (debug)
1891 printk("bttv%d: cap vgrab: queue %d (%d:%dx%d)\n",
1892 btv->nr,mp->frame,mp->format,mp->width,mp->height);
1893 spin_lock_irqsave(&btv->s_lock, flags);
1894 btv->gbuf[mp->frame].stat = GBUFFER_GRABBING;
1895 btv->gbuf[mp->frame].fmt = palette2fmt[mp->format];
1896 btv->gbuf[mp->frame].width = mp->width;
1897 btv->gbuf[mp->frame].height = mp->height;
1898 btv->gbuf[mp->frame].ro = virt_to_bus(ro);
1899 btv->gbuf[mp->frame].re = virt_to_bus(re);
1901 #if 1
1902 if (mp->height <= tvnorms[btv->win.norm].sheight/2 &&
1903 mp->format != VIDEO_PALETTE_RAW)
1904 btv->gbuf[mp->frame].ro = 0;
1905 #endif
1907 if (btv->gq_in == btv->gq_out) {
1908 btv->gq_start = 1;
1909 btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ);
1911 btv->gqueue[btv->gq_in++] = mp->frame;
1912 btv->gq_in = btv->gq_in % MAX_GBUFFERS;
1914 spin_unlock_irqrestore(&btv->s_lock, flags);
1915 btor(3, BT848_CAP_CTL);
1916 btor(3, BT848_GPIO_DMA_CTL);
1917 return 0;
1921 static long bttv_write(struct video_device *v, const char *buf, unsigned long count, int nonblock)
1923 return -EINVAL;
1926 static long bttv_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
1928 struct bttv *btv= (struct bttv *)v;
1929 int q,todo;
1931 /* BROKEN: RETURNS VBI WHEN IT SHOULD RETURN GRABBED VIDEO FRAME */
1932 todo=count;
1933 while (todo && todo>(q=VBIBUF_SIZE-btv->vbip))
1935 unsigned long flags;
1937 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, q))
1938 return -EFAULT;
1939 todo-=q;
1940 buf+=q;
1942 spin_lock_irqsave(&btv->s_lock, flags);
1943 if (todo && q==VBIBUF_SIZE-btv->vbip)
1945 if(nonblock)
1947 spin_unlock_irqrestore(&btv->s_lock, flags);
1948 if(count==todo)
1949 return -EWOULDBLOCK;
1950 return count-todo;
1952 spin_unlock_irqrestore(&btv->s_lock, flags);
1953 interruptible_sleep_on(&btv->vbiq);
1954 if(signal_pending(current))
1956 if(todo==count)
1957 return -EINTR;
1958 else
1959 return count-todo;
1961 } else
1962 spin_unlock_irqrestore(&btv->s_lock, flags);
1964 if (todo)
1966 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, todo))
1967 return -EFAULT;
1968 btv->vbip+=todo;
1970 return count;
1973 static inline void burst(int on)
1975 tvnorms[0].scaledtwidth = 1135 - (on?BURSTOFFSET-2:0);
1976 tvnorms[0].hdelayx1 = 186 - (on?BURSTOFFSET :0);
1977 tvnorms[2].scaledtwidth = 1135 - (on?BURSTOFFSET-2:0);
1978 tvnorms[2].hdelayx1 = 186 - (on?BURSTOFFSET :0);
1981 static void bt848_restart(struct bttv *btv)
1983 if (verbose)
1984 printk("bttv%d: resetting chip\n",btv->nr);
1985 btwrite(~0x0UL, BT848_INT_STAT);
1986 btand(~15, BT848_GPIO_DMA_CTL);
1987 btwrite(0, BT848_SRESET);
1988 btwrite(virt_to_bus(btv->risc_jmp+2),
1989 BT848_RISC_STRT_ADD);
1991 /* enforce pll reprogramming */
1992 btv->pll.pll_current = 0;
1993 set_pll(btv);
1995 btv->errors = 0;
1996 btv->needs_restart = 0;
1997 bt848_set_geo(btv,0);
1998 bt848_set_risc_jmps(btv,-1);
2002 * Open a bttv card. Right now the flags stuff is just playing
2005 static int bttv_open(struct video_device *dev, int flags)
2007 struct bttv *btv = (struct bttv *)dev;
2008 int i,ret;
2010 ret = -EBUSY;
2012 MOD_INC_USE_COUNT;
2013 down(&btv->lock);
2014 if (btv->user)
2015 goto out_unlock;
2017 btv->fbuffer=(unsigned char *) rvmalloc(gbuffers*gbufsize);
2018 ret = -ENOMEM;
2019 if (!btv->fbuffer)
2020 goto out_unlock;
2022 btv->gq_in = 0;
2023 btv->gq_out = 0;
2024 btv->gq_grab = -1;
2025 for (i = 0; i < gbuffers; i++)
2026 btv->gbuf[i].stat = GBUFFER_UNUSED;
2028 if (btv->needs_restart)
2029 bt848_restart(btv);
2030 burst(0);
2031 set_pll(btv);
2032 btv->user++;
2033 up(&btv->lock);
2034 return 0;
2036 out_unlock:
2037 up(&btv->lock);
2038 MOD_DEC_USE_COUNT;
2039 return ret;
2042 static void bttv_close(struct video_device *dev)
2044 struct bttv *btv=(struct bttv *)dev;
2046 down(&btv->lock);
2047 btv->user--;
2048 btv->scr_on = 0;
2049 btv->risc_cap_odd = 0;
2050 btv->risc_cap_even = 0;
2051 bt848_set_risc_jmps(btv,-1);
2054 * A word of warning. At this point the chip
2055 * is still capturing because its FIFO hasn't emptied
2056 * and the DMA control operations are posted PCI
2057 * operations.
2060 btread(BT848_I2C); /* This fixes the PCI posting delay */
2062 if (-1 != btv->gq_grab) {
2064 * This is sucky but right now I can't find a good way to
2065 * be sure its safe to free the buffer. We wait 5-6 fields
2066 * which is more than sufficient to be sure.
2068 current->state = TASK_UNINTERRUPTIBLE;
2069 schedule_timeout(HZ/10); /* Wait 1/10th of a second */
2073 * We have allowed it to drain.
2076 if(btv->fbuffer)
2077 rvfree((void *) btv->fbuffer, gbuffers*gbufsize);
2078 btv->fbuffer=0;
2079 up(&btv->lock);
2080 MOD_DEC_USE_COUNT;
2084 /***********************************/
2085 /* ioctls and supporting functions */
2086 /***********************************/
2088 extern inline void bt848_bright(struct bttv *btv, uint bright)
2090 btwrite(bright&0xff, BT848_BRIGHT);
2093 extern inline void bt848_hue(struct bttv *btv, uint hue)
2095 btwrite(hue&0xff, BT848_HUE);
2098 extern inline void bt848_contrast(struct bttv *btv, uint cont)
2100 unsigned int conthi;
2102 conthi=(cont>>6)&4;
2103 btwrite(cont&0xff, BT848_CONTRAST_LO);
2104 btaor(conthi, ~4, BT848_E_CONTROL);
2105 btaor(conthi, ~4, BT848_O_CONTROL);
2108 extern inline void bt848_sat_u(struct bttv *btv, unsigned long data)
2110 u32 datahi;
2112 datahi=(data>>7)&2;
2113 btwrite(data&0xff, BT848_SAT_U_LO);
2114 btaor(datahi, ~2, BT848_E_CONTROL);
2115 btaor(datahi, ~2, BT848_O_CONTROL);
2118 static inline void bt848_sat_v(struct bttv *btv, unsigned long data)
2120 u32 datahi;
2122 datahi=(data>>8)&1;
2123 btwrite(data&0xff, BT848_SAT_V_LO);
2124 btaor(datahi, ~1, BT848_E_CONTROL);
2125 btaor(datahi, ~1, BT848_O_CONTROL);
2129 * ioctl routine
2133 static int bttv_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2135 struct bttv *btv=(struct bttv *)dev;
2136 int i,ret = 0;
2138 if (debug) printk("bttv%d: ioctl 0x%x\n",btv->nr,cmd);
2140 switch (cmd) {
2141 case VIDIOCGCAP:
2143 struct video_capability b;
2144 strcpy(b.name,btv->video_dev.name);
2145 b.type = VID_TYPE_CAPTURE|
2146 ((tvcards[btv->type].tuner != -1) ? VID_TYPE_TUNER : 0) |
2147 VID_TYPE_OVERLAY|
2148 VID_TYPE_CLIPPING|
2149 VID_TYPE_FRAMERAM|
2150 VID_TYPE_SCALES;
2151 b.channels = tvcards[btv->type].video_inputs;
2152 b.audios = tvcards[btv->type].audio_inputs;
2153 b.maxwidth = tvnorms[btv->win.norm].swidth;
2154 b.maxheight = tvnorms[btv->win.norm].sheight;
2155 b.minwidth = 32;
2156 b.minheight = 32;
2157 if(copy_to_user(arg,&b,sizeof(b)))
2158 return -EFAULT;
2159 return 0;
2161 case VIDIOCGCHAN:
2163 struct video_channel v;
2164 if(copy_from_user(&v, arg,sizeof(v)))
2165 return -EFAULT;
2166 v.flags=VIDEO_VC_AUDIO;
2167 v.tuners=0;
2168 v.type=VIDEO_TYPE_CAMERA;
2169 v.norm = btv->win.norm;
2170 if (v.channel>=tvcards[btv->type].video_inputs)
2171 return -EINVAL;
2172 if(v.channel==tvcards[btv->type].tuner)
2174 strcpy(v.name,"Television");
2175 v.flags|=VIDEO_VC_TUNER;
2176 v.type=VIDEO_TYPE_TV;
2177 v.tuners=1;
2179 else if(v.channel==tvcards[btv->type].svhs)
2180 strcpy(v.name,"S-Video");
2181 else
2182 sprintf(v.name,"Composite%d",v.channel);
2184 if(copy_to_user(arg,&v,sizeof(v)))
2185 return -EFAULT;
2186 return 0;
2189 * Each channel has 1 tuner
2191 case VIDIOCSCHAN:
2193 struct video_channel v;
2194 if(copy_from_user(&v, arg,sizeof(v)))
2195 return -EFAULT;
2197 if (v.channel>tvcards[btv->type].video_inputs)
2198 return -EINVAL;
2199 if (v.norm > (sizeof(tvnorms)/sizeof(*tvnorms)))
2200 return -EOPNOTSUPP;
2202 call_i2c_clients(btv,cmd,&v);
2203 down(&btv->lock);
2204 bt848_muxsel(btv, v.channel);
2205 btv->channel=v.channel;
2206 if (btv->win.norm != v.norm) {
2207 btv->win.norm = v.norm;
2208 make_vbitab(btv);
2209 bt848_set_winsize(btv);
2211 up(&btv->lock);
2212 return 0;
2214 case VIDIOCGTUNER:
2216 struct video_tuner v;
2217 if(copy_from_user(&v,arg,sizeof(v))!=0)
2218 return -EFAULT;
2219 if(v.tuner||btv->channel) /* Only tuner 0 */
2220 return -EINVAL;
2221 strcpy(v.name, "Television");
2222 v.rangelow=0;
2223 v.rangehigh=0xFFFFFFFF;
2224 v.flags=VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
2225 v.mode = btv->win.norm;
2226 v.signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
2227 call_i2c_clients(btv,cmd,&v);
2228 if(copy_to_user(arg,&v,sizeof(v)))
2229 return -EFAULT;
2230 return 0;
2232 /* We have but one tuner */
2233 case VIDIOCSTUNER:
2235 struct video_tuner v;
2236 if(copy_from_user(&v, arg, sizeof(v)))
2237 return -EFAULT;
2238 /* Only one channel has a tuner */
2239 if(v.tuner!=tvcards[btv->type].tuner)
2240 return -EINVAL;
2242 if(v.mode!=VIDEO_MODE_PAL&&v.mode!=VIDEO_MODE_NTSC
2243 &&v.mode!=VIDEO_MODE_SECAM)
2244 return -EOPNOTSUPP;
2245 call_i2c_clients(btv,cmd,&v);
2246 if (btv->win.norm != v.mode) {
2247 btv->win.norm = v.mode;
2248 down(&btv->lock);
2249 set_pll(btv);
2250 make_vbitab(btv);
2251 bt848_set_winsize(btv);
2252 up(&btv->lock);
2254 return 0;
2256 case VIDIOCGPICT:
2258 struct video_picture p=btv->picture;
2259 if(copy_to_user(arg, &p, sizeof(p)))
2260 return -EFAULT;
2261 return 0;
2263 case VIDIOCSPICT:
2265 struct video_picture p;
2266 if(copy_from_user(&p, arg,sizeof(p)))
2267 return -EFAULT;
2268 if (p.palette > PALETTEFMT_MAX)
2269 return -EINVAL;
2270 down(&btv->lock);
2271 /* We want -128 to 127 we get 0-65535 */
2272 bt848_bright(btv, (p.brightness>>8)-128);
2273 /* 0-511 for the colour */
2274 bt848_sat_u(btv, p.colour>>7);
2275 bt848_sat_v(btv, ((p.colour>>7)*201L)/237);
2276 /* -128 to 127 */
2277 bt848_hue(btv, (p.hue>>8)-128);
2278 /* 0-511 */
2279 bt848_contrast(btv, p.contrast>>7);
2280 btv->picture = p;
2281 up(&btv->lock);
2282 return 0;
2284 case VIDIOCSWIN:
2286 struct video_window vw;
2287 struct video_clip *vcp = NULL;
2289 if(copy_from_user(&vw,arg,sizeof(vw)))
2290 return -EFAULT;
2292 if(vw.flags || vw.width < 16 || vw.height < 16)
2294 down(&btv->lock);
2295 btv->scr_on = 0;
2296 bt848_set_risc_jmps(btv,-1);
2297 up(&btv->lock);
2298 return -EINVAL;
2300 if (btv->win.bpp < 4)
2301 { /* adjust and align writes */
2302 vw.x = (vw.x + 3) & ~3;
2303 vw.width &= ~3;
2305 down(&btv->lock);
2306 if (btv->needs_restart)
2307 bt848_restart(btv);
2308 btv->win.x=vw.x;
2309 btv->win.y=vw.y;
2310 btv->win.width=vw.width;
2311 btv->win.height=vw.height;
2313 bt848_set_risc_jmps(btv,0);
2315 bt848_set_winsize(btv);
2316 up(&btv->lock);
2319 * Do any clips.
2321 if(vw.clipcount<0) {
2322 if((vcp=vmalloc(VIDEO_CLIPMAP_SIZE))==NULL)
2323 return -ENOMEM;
2324 if(copy_from_user(vcp, vw.clips,
2325 VIDEO_CLIPMAP_SIZE)) {
2326 vfree(vcp);
2327 return -EFAULT;
2329 } else if (vw.clipcount) {
2330 if((vcp=vmalloc(sizeof(struct video_clip)*
2331 (vw.clipcount))) == NULL)
2332 return -ENOMEM;
2333 if(copy_from_user(vcp,vw.clips,
2334 sizeof(struct video_clip)*
2335 vw.clipcount)) {
2336 vfree(vcp);
2337 return -EFAULT;
2340 down(&btv->lock);
2341 make_clip_tab(btv, vcp, vw.clipcount);
2342 if (vw.clipcount != 0)
2343 vfree(vcp);
2344 bt848_set_risc_jmps(btv,-1);
2345 up(&btv->lock);
2346 return 0;
2348 case VIDIOCGWIN:
2350 struct video_window vw;
2351 /* Oh for a COBOL move corresponding .. */
2352 vw.x=btv->win.x;
2353 vw.y=btv->win.y;
2354 vw.width=btv->win.width;
2355 vw.height=btv->win.height;
2356 vw.chromakey=0;
2357 vw.flags=0;
2358 if(btv->win.interlace)
2359 vw.flags|=VIDEO_WINDOW_INTERLACE;
2360 if(copy_to_user(arg,&vw,sizeof(vw)))
2361 return -EFAULT;
2362 return 0;
2364 case VIDIOCCAPTURE:
2366 int v;
2367 if(copy_from_user(&v, arg,sizeof(v)))
2368 return -EFAULT;
2369 if(btv->win.vidadr == 0)
2370 return -EINVAL;
2371 if (btv->win.width==0 || btv->win.height==0)
2372 return -EINVAL;
2373 down(&btv->lock);
2374 if (v == 1 && btv->win.vidadr != 0)
2375 btv->scr_on = 1;
2376 if (v == 0)
2377 btv->scr_on = 0;
2378 bt848_set_risc_jmps(btv,-1);
2379 up(&btv->lock);
2380 return 0;
2382 case VIDIOCGFBUF:
2384 struct video_buffer v;
2385 v.base=(void *)btv->win.vidadr;
2386 v.height=btv->win.sheight;
2387 v.width=btv->win.swidth;
2388 v.depth=btv->win.depth;
2389 v.bytesperline=btv->win.bpl;
2390 if(copy_to_user(arg, &v,sizeof(v)))
2391 return -EFAULT;
2392 return 0;
2395 case VIDIOCSFBUF:
2397 struct video_buffer v;
2398 if(!capable(CAP_SYS_ADMIN) &&
2399 !capable(CAP_SYS_RAWIO))
2400 return -EPERM;
2401 if(copy_from_user(&v, arg,sizeof(v)))
2402 return -EFAULT;
2403 if(v.depth!=8 && v.depth!=15 && v.depth!=16 &&
2404 v.depth!=24 && v.depth!=32 && v.width > 16 &&
2405 v.height > 16 && v.bytesperline > 16)
2406 return -EINVAL;
2407 down(&btv->lock);
2408 if (v.base)
2409 btv->win.vidadr=(unsigned long)v.base;
2410 btv->win.sheight=v.height;
2411 btv->win.swidth=v.width;
2412 btv->win.bpp=((v.depth+7)&0x38)/8;
2413 btv->win.depth=v.depth;
2414 btv->win.bpl=v.bytesperline;
2416 /* set sefault color format */
2417 switch (btv->win.bpp) {
2418 case 8: btv->picture.palette = VIDEO_PALETTE_HI240; break;
2419 case 15: btv->picture.palette = VIDEO_PALETTE_RGB555; break;
2420 case 16: btv->picture.palette = VIDEO_PALETTE_RGB565; break;
2421 case 24: btv->picture.palette = VIDEO_PALETTE_RGB24; break;
2422 case 32: btv->picture.palette = VIDEO_PALETTE_RGB32; break;
2425 if (debug)
2426 printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
2427 v.base, v.width,v.height, btv->win.bpp, btv->win.bpl);
2428 bt848_set_winsize(btv);
2429 up(&btv->lock);
2430 return 0;
2432 case VIDIOCKEY:
2434 /* Will be handled higher up .. */
2435 return 0;
2437 case VIDIOCGFREQ:
2439 unsigned long v=btv->win.freq;
2440 if(copy_to_user(arg,&v,sizeof(v)))
2441 return -EFAULT;
2442 return 0;
2444 case VIDIOCSFREQ:
2446 unsigned long v;
2447 if(copy_from_user(&v, arg, sizeof(v)))
2448 return -EFAULT;
2449 btv->win.freq=v;
2450 call_i2c_clients(btv,cmd,&v);
2451 return 0;
2454 case VIDIOCGAUDIO:
2456 struct video_audio v;
2458 v=btv->audio_dev;
2459 v.flags&=~(VIDEO_AUDIO_MUTE|VIDEO_AUDIO_MUTABLE);
2460 v.flags|=VIDEO_AUDIO_MUTABLE;
2461 strcpy(v.name,"TV");
2463 v.mode = VIDEO_SOUND_MONO;
2464 call_i2c_clients(btv,cmd,&v);
2466 if (btv->type == BTTV_TERRATV) {
2467 v.mode = VIDEO_SOUND_MONO;
2468 v.mode |= VIDEO_SOUND_STEREO;
2469 v.mode |= VIDEO_SOUND_LANG1|VIDEO_SOUND_LANG2;
2471 } else if (btv->audio_chip == TDA9840) {
2472 /* begin of Horrible Hack <grin@tolna.net> */
2473 v.flags|=VIDEO_AUDIO_VOLUME;
2474 v.mode = VIDEO_SOUND_MONO;
2475 v.mode |= VIDEO_SOUND_STEREO;
2476 v.mode |= VIDEO_SOUND_LANG1|VIDEO_SOUND_LANG2;
2477 v.volume = 32768; /* fixme */
2478 v.step = 4096;
2481 if(copy_to_user(arg,&v,sizeof(v)))
2482 return -EFAULT;
2483 return 0;
2485 case VIDIOCSAUDIO:
2487 struct video_audio v;
2489 if(copy_from_user(&v,arg, sizeof(v)))
2490 return -EFAULT;
2491 down(&btv->lock);
2492 if(v.flags&VIDEO_AUDIO_MUTE)
2493 audio(btv, AUDIO_MUTE, 1);
2494 /* One audio source per tuner -- huh? <GA> */
2495 if(v.audio<0 || v.audio >= tvcards[btv->type].audio_inputs) {
2496 up(&btv->lock);
2497 return -EINVAL;
2499 /* bt848_muxsel(btv,v.audio); */
2500 if(!(v.flags&VIDEO_AUDIO_MUTE))
2501 audio(btv, AUDIO_UNMUTE, 1);
2503 up(&btv->lock);
2504 call_i2c_clients(btv,cmd,&v);
2505 down(&btv->lock);
2507 if (btv->type == BTTV_TERRATV) {
2508 unsigned int con = 0;
2509 btor(0x180000, BT848_GPIO_OUT_EN);
2510 if (v.mode & VIDEO_SOUND_LANG2)
2511 con = 0x080000;
2512 if (v.mode & VIDEO_SOUND_STEREO)
2513 con = 0x180000;
2514 btaor(con, ~0x180000, BT848_GPIO_DATA);
2516 } else if (btv->type == BTTV_WINVIEW_601) {
2517 /* PT2254A programming Jon Tombs, jon@gte.esi.us.es */
2518 int bits_out, loops, vol, data;
2520 /* 32 levels logarithmic */
2521 vol = 32 - ((v.volume>>11));
2522 /* units */
2523 bits_out = (PT2254_DBS_IN_2>>(vol%5));
2524 /* tens */
2525 bits_out |= (PT2254_DBS_IN_10>>(vol/5));
2526 bits_out |= PT2254_L_CHANEL | PT2254_R_CHANEL;
2527 data = btread(BT848_GPIO_DATA);
2528 data &= ~(WINVIEW_PT2254_CLK| WINVIEW_PT2254_DATA|
2529 WINVIEW_PT2254_STROBE);
2530 for (loops = 17; loops >= 0 ; loops--) {
2531 if (bits_out & (1<<loops))
2532 data |= WINVIEW_PT2254_DATA;
2533 else
2534 data &= ~WINVIEW_PT2254_DATA;
2535 btwrite(data, BT848_GPIO_DATA);
2536 udelay(5);
2537 data |= WINVIEW_PT2254_CLK;
2538 btwrite(data, BT848_GPIO_DATA);
2539 udelay(5);
2540 data &= ~WINVIEW_PT2254_CLK;
2541 btwrite(data, BT848_GPIO_DATA);
2543 data |= WINVIEW_PT2254_STROBE;
2544 data &= ~WINVIEW_PT2254_DATA;
2545 btwrite(data, BT848_GPIO_DATA);
2546 udelay(10);
2547 data &= ~WINVIEW_PT2254_STROBE;
2548 btwrite(data, BT848_GPIO_DATA);
2551 btv->audio_dev=v;
2552 up(&btv->lock);
2553 return 0;
2556 case VIDIOCSYNC:
2557 if(copy_from_user((void *)&i,arg,sizeof(int)))
2558 return -EFAULT;
2559 if (i < 0 || i >= gbuffers)
2560 return -EINVAL;
2561 switch (btv->gbuf[i].stat) {
2562 case GBUFFER_UNUSED:
2563 ret = -EINVAL;
2564 break;
2565 case GBUFFER_GRABBING:
2566 while(btv->gbuf[i].stat==GBUFFER_GRABBING) {
2567 if (debug)
2568 printk("bttv%d: cap sync: sleep on %d\n",btv->nr,i);
2569 interruptible_sleep_on(&btv->capq);
2570 if(signal_pending(current))
2571 return -EINTR;
2573 /* fall throuth */
2574 case GBUFFER_DONE:
2575 case GBUFFER_ERROR:
2576 ret = (btv->gbuf[i].stat == GBUFFER_ERROR) ? -EIO : 0;
2577 if (debug)
2578 printk("bttv%d: cap sync: buffer %d, retval %d\n",btv->nr,i,ret);
2579 btv->gbuf[i].stat = GBUFFER_UNUSED;
2581 if (btv->needs_restart) {
2582 down(&btv->lock);
2583 bt848_restart(btv);
2584 up(&btv->lock);
2586 return ret;
2588 case BTTV_FIELDNR:
2589 if(copy_to_user((void *) arg, (void *) &btv->last_field,
2590 sizeof(btv->last_field)))
2591 return -EFAULT;
2592 break;
2594 case BTTV_PLLSET: {
2595 struct bttv_pll_info p;
2596 if(!capable(CAP_SYS_ADMIN))
2597 return -EPERM;
2598 if(copy_from_user(&p , (void *) arg, sizeof(btv->pll)))
2599 return -EFAULT;
2600 down(&btv->lock);
2601 btv->pll.pll_ifreq = p.pll_ifreq;
2602 btv->pll.pll_ofreq = p.pll_ofreq;
2603 btv->pll.pll_crystal = p.pll_crystal;
2604 up(&btv->lock);
2606 break;
2609 case VIDIOCMCAPTURE:
2611 struct video_mmap vm;
2612 int ret;
2613 if(copy_from_user((void *) &vm, (void *) arg, sizeof(vm)))
2614 return -EFAULT;
2615 down(&btv->lock);
2616 ret = vgrab(btv, &vm);
2617 up(&btv->lock);
2618 return ret;
2621 case VIDIOCGMBUF:
2623 struct video_mbuf vm;
2624 memset(&vm, 0 , sizeof(vm));
2625 vm.size=gbufsize*gbuffers;
2626 vm.frames=gbuffers;
2627 for (i = 0; i < gbuffers; i++)
2628 vm.offsets[i]=i*gbufsize;
2629 if(copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
2630 return -EFAULT;
2631 return 0;
2634 case VIDIOCGUNIT:
2636 struct video_unit vu;
2637 vu.video=btv->video_dev.minor;
2638 vu.vbi=btv->vbi_dev.minor;
2639 if(btv->radio_dev.minor!=-1)
2640 vu.radio=btv->radio_dev.minor;
2641 else
2642 vu.radio=VIDEO_NO_UNIT;
2643 vu.audio=VIDEO_NO_UNIT;
2644 vu.teletext=VIDEO_NO_UNIT;
2645 if(copy_to_user((void *)arg, (void *)&vu, sizeof(vu)))
2646 return -EFAULT;
2647 return 0;
2650 case BTTV_BURST_ON:
2652 burst(1);
2653 return 0;
2656 case BTTV_BURST_OFF:
2658 burst(0);
2659 return 0;
2662 case BTTV_VERSION:
2664 return BTTV_VERSION_CODE;
2667 case BTTV_PICNR:
2669 /* return picture;*/
2670 return 0;
2673 default:
2674 return -ENOIOCTLCMD;
2676 return 0;
2679 static int bttv_init_done(struct video_device *dev)
2681 return 0;
2685 * This maps the vmalloced and reserved fbuffer to user space.
2687 * FIXME:
2688 * - PAGE_READONLY should suffice!?
2689 * - remap_page_range is kind of inefficient for page by page remapping.
2690 * But e.g. pte_alloc() does not work in modules ... :-(
2693 static int do_bttv_mmap(struct bttv *btv, const char *adr, unsigned long size)
2695 unsigned long start=(unsigned long) adr;
2696 unsigned long page,pos;
2698 if (size>gbuffers*gbufsize)
2699 return -EINVAL;
2700 if (!btv->fbuffer) {
2701 if(fbuffer_alloc(btv))
2702 return -EINVAL;
2704 pos=(unsigned long) btv->fbuffer;
2705 while (size > 0) {
2706 page = kvirt_to_pa(pos);
2707 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
2708 return -EAGAIN;
2709 start+=PAGE_SIZE;
2710 pos+=PAGE_SIZE;
2711 size-=PAGE_SIZE;
2713 return 0;
2716 static int bttv_mmap(struct video_device *dev, const char *adr, unsigned long size)
2718 struct bttv *btv=(struct bttv *)dev;
2719 int r;
2721 down(&btv->lock);
2722 r=do_bttv_mmap(btv, adr, size);
2723 up(&btv->lock);
2724 return r;
2728 static struct video_device bttv_template=
2730 "UNSET",
2731 VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY|VID_TYPE_TELETEXT,
2732 VID_HARDWARE_BT848,
2733 bttv_open,
2734 bttv_close,
2735 bttv_read,
2736 bttv_write,
2737 NULL,
2738 bttv_ioctl,
2739 bttv_mmap,
2740 bttv_init_done,
2741 NULL,
2747 static long vbi_read(struct video_device *v, char *buf, unsigned long count,
2748 int nonblock)
2750 struct bttv *btv=(struct bttv *)(v-2);
2751 int q,todo;
2753 todo=count;
2754 while (todo && todo>(q=VBIBUF_SIZE-btv->vbip))
2756 unsigned long flags;
2758 if (btv->needs_restart) {
2759 down(&btv->lock);
2760 bt848_restart(btv);
2761 up(&btv->lock);
2763 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, q))
2764 return -EFAULT;
2765 todo-=q;
2766 buf+=q;
2768 spin_lock_irqsave(&btv->s_lock, flags);
2769 if (todo && q==VBIBUF_SIZE-btv->vbip)
2771 if(nonblock)
2773 spin_unlock_irqrestore(&btv->s_lock, flags);
2774 if(count==todo)
2775 return -EWOULDBLOCK;
2776 return count-todo;
2778 spin_unlock_irqrestore(&btv->s_lock, flags);
2779 interruptible_sleep_on(&btv->vbiq);
2780 if(signal_pending(current))
2782 if(todo==count)
2783 return -EINTR;
2784 else
2785 return count-todo;
2787 } else
2788 spin_unlock_irqrestore(&btv->s_lock, flags);
2790 if (todo)
2792 if(copy_to_user((void *) buf, (void *) btv->vbibuf+btv->vbip, todo))
2793 return -EFAULT;
2794 btv->vbip+=todo;
2796 return count;
2799 static unsigned int vbi_poll(struct video_device *dev, struct file *file,
2800 poll_table *wait)
2802 struct bttv *btv=(struct bttv *)(dev-2);
2803 unsigned int mask = 0;
2805 poll_wait(file, &btv->vbiq, wait);
2807 if (btv->vbip < VBIBUF_SIZE)
2808 mask |= (POLLIN | POLLRDNORM);
2810 return mask;
2813 static int vbi_open(struct video_device *dev, int flags)
2815 struct bttv *btv=(struct bttv *)(dev-2);
2817 MOD_INC_USE_COUNT;
2819 down(&btv->lock);
2820 if (btv->needs_restart)
2821 bt848_restart(btv);
2822 btv->vbip=VBIBUF_SIZE;
2823 btv->vbi_on = 1;
2824 bt848_set_risc_jmps(btv,-1);
2825 up(&btv->lock);
2827 return 0;
2830 static void vbi_close(struct video_device *dev)
2832 struct bttv *btv=(struct bttv *)(dev-2);
2834 down(&btv->lock);
2835 btv->vbi_on = 0;
2836 bt848_set_risc_jmps(btv,-1);
2837 up(&btv->lock);
2839 MOD_DEC_USE_COUNT;
2842 static int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2844 struct bttv *btv=(struct bttv *)dev;
2846 switch (cmd) {
2847 case VIDIOCGCAP:
2849 struct video_capability b;
2850 strcpy(b.name,btv->vbi_dev.name);
2851 b.type = ((tvcards[btv->type].tuner != -1) ? VID_TYPE_TUNER : 0) |
2852 VID_TYPE_TELETEXT;
2853 b.channels = 0;
2854 b.audios = 0;
2855 b.maxwidth = 0;
2856 b.maxheight = 0;
2857 b.minwidth = 0;
2858 b.minheight = 0;
2859 if(copy_to_user(arg,&b,sizeof(b)))
2860 return -EFAULT;
2861 return 0;
2863 case VIDIOCGFREQ:
2864 case VIDIOCSFREQ:
2865 return bttv_ioctl((struct video_device *)btv,cmd,arg);
2866 case BTTV_VBISIZE:
2867 /* make alevt happy :-) */
2868 return VBIBUF_SIZE;
2869 default:
2870 return -EINVAL;
2874 static struct video_device vbi_template=
2876 "bttv vbi",
2877 VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
2878 VID_HARDWARE_BT848,
2879 vbi_open,
2880 vbi_close,
2881 vbi_read,
2882 bttv_write,
2883 vbi_poll,
2884 vbi_ioctl,
2885 NULL, /* no mmap yet */
2886 bttv_init_done,
2887 NULL,
2893 static int radio_open(struct video_device *dev, int flags)
2895 struct bttv *btv = (struct bttv *)(dev-1);
2896 unsigned long v;
2898 MOD_INC_USE_COUNT;
2899 down(&btv->lock);
2900 if (btv->user)
2901 goto busy_unlock;
2902 btv->user++;
2904 btv->radio = 1;
2905 v = 400*16;
2906 call_i2c_clients(btv,VIDIOCSFREQ,&v);
2907 call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
2908 bt848_muxsel(btv,0);
2909 up(&btv->lock);
2911 return 0;
2913 busy_unlock:
2914 up(&btv->lock);
2915 MOD_DEC_USE_COUNT;
2916 return -EBUSY;
2919 static void radio_close(struct video_device *dev)
2921 struct bttv *btv=(struct bttv *)(dev-1);
2923 down(&btv->lock);
2924 btv->user--;
2925 btv->radio = 0;
2926 up(&btv->lock);
2927 MOD_DEC_USE_COUNT;
2930 static long radio_read(struct video_device *v, char *buf, unsigned long count, int nonblock)
2932 return -EINVAL;
2935 static int radio_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
2937 struct bttv *btv=(struct bttv *)(dev-1);
2938 switch (cmd) {
2939 case VIDIOCGCAP:
2941 struct video_capability v;
2942 strcpy(v.name,btv->video_dev.name);
2943 v.type = VID_TYPE_TUNER;
2944 v.channels = 1;
2945 v.audios = 1;
2946 /* No we don't do pictures */
2947 v.maxwidth = 0;
2948 v.maxheight = 0;
2949 v.minwidth = 0;
2950 v.minheight = 0;
2951 if (copy_to_user(arg, &v, sizeof(v)))
2952 return -EFAULT;
2953 return 0;
2954 break;
2956 case VIDIOCGTUNER:
2958 struct video_tuner v;
2959 if(copy_from_user(&v,arg,sizeof(v))!=0)
2960 return -EFAULT;
2961 if(v.tuner||btv->channel) /* Only tuner 0 */
2962 return -EINVAL;
2963 strcpy(v.name, "Radio");
2964 v.rangelow=(int)(76*16); /* jp: 76.0MHz - 89.9MHz */
2965 v.rangehigh=(int)(108*16); /* eu: 87.5MHz - 108.0MHz */
2966 v.flags= 0; /* XXX */
2967 v.mode = 0; /* XXX */
2968 if(copy_to_user(arg,&v,sizeof(v)))
2969 return -EFAULT;
2970 return 0;
2972 case VIDIOCSTUNER:
2974 struct video_tuner v;
2975 if(copy_from_user(&v, arg, sizeof(v)))
2976 return -EFAULT;
2977 /* Only channel 0 has a tuner */
2978 if(v.tuner!=0 || btv->channel)
2979 return -EINVAL;
2980 /* XXX anything to do ??? */
2981 return 0;
2983 case VIDIOCGFREQ:
2984 case VIDIOCSFREQ:
2985 case VIDIOCGAUDIO:
2986 case VIDIOCSAUDIO:
2987 bttv_ioctl((struct video_device *)btv,cmd,arg);
2988 break;
2989 default:
2990 return -ENOIOCTLCMD;
2992 return 0;
2995 static struct video_device radio_template=
2997 "bttv radio",
2998 VID_TYPE_TUNER,
2999 VID_HARDWARE_BT848,
3000 radio_open,
3001 radio_close,
3002 radio_read, /* just returns -EINVAL */
3003 bttv_write, /* just returns -EINVAL */
3004 NULL, /* no poll */
3005 radio_ioctl,
3006 NULL, /* no mmap */
3007 bttv_init_done, /* just returns 0 */
3008 NULL,
3014 #define TRITON_PCON 0x50
3015 #define TRITON_BUS_CONCURRENCY (1<<0)
3016 #define TRITON_STREAMING (1<<1)
3017 #define TRITON_WRITE_BURST (1<<2)
3018 #define TRITON_PEER_CONCURRENCY (1<<3)
3021 static void __init handle_chipset(void)
3023 struct pci_dev *dev = NULL;
3025 /* Just in case some nut set this to something dangerous */
3026 if (triton1)
3027 triton1=BT848_INT_ETBF;
3029 while ((dev = pci_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_496, dev)))
3031 /* Beware the SiS 85C496 my friend - rev 49 don't work with a bttv */
3032 printk(KERN_WARNING "BT848 and SIS 85C496 chipset don't always work together.\n");
3035 while ((dev = pci_find_device(PCI_VENDOR_ID_INTEL,
3036 PCI_DEVICE_ID_INTEL_82441, dev)))
3038 unsigned char b;
3039 pci_read_config_byte(dev, 0x53, &b);
3040 DEBUG(printk(KERN_INFO "bttv: Host bridge: 82441FX Natoma, "));
3041 DEBUG(printk("bufcon=0x%02x\n",b));
3044 while ((dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437, dev)))
3046 printk(KERN_INFO "bttv: Host bridge 82437FX Triton PIIX\n");
3047 triton1=BT848_INT_ETBF;
3052 /* can tda9855.c handle this too maybe? */
3053 static void __init init_tda9840(struct bttv *btv)
3055 /* Horrible Hack */
3056 I2CWrite(btv, I2C_TDA9840, TDA9840_SW, 0x2a, 1); /* sound mode switching */
3057 /* 00 - mute
3058 10 - mono / averaged stereo
3059 2a - stereo
3060 12 - dual A
3061 1a - dual AB
3062 16 - dual BA
3063 1e - dual B
3064 7a - external */
3067 /* Figure out card and tuner type */
3069 static void __init idcard(struct bttv *btv)
3071 int type,eeprom = 0;
3073 btwrite(0, BT848_GPIO_OUT_EN);
3074 DEBUG(printk(KERN_DEBUG "bttv%d: GPIO: 0x%08x\n", btv->nr, btread(BT848_GPIO_DATA)));
3076 /* Default the card to the user-selected one. */
3077 if (card[btv->nr] >= 0 && card[btv->nr] < TVCARDS)
3078 btv->type=card[btv->nr];
3080 /* If we were asked to auto-detect, then do so! */
3081 if (btv->type == BTTV_UNKNOWN) {
3083 /* many bt878 cards have a eeprom @ 0xa0 => read ID
3084 and try to identify it */
3085 if (I2CRead(btv, I2C_HAUPEE, "eeprom") >= 0) {
3086 eeprom = 0xa0;
3087 readee(btv,eeprom_data,0xa0);
3088 dump_eeprom(btv,0xa0); /* DEBUG */
3089 type = idcard_eeprom(btv);
3090 if (-1 != type) {
3091 btv->type = type;
3092 } else if (btv->id <= 849) {
3093 /* for unknown bt848, assume old Hauppauge */
3094 btv->type=BTTV_HAUPPAUGE;
3097 /* STB cards have a eeprom @ 0xae (old bt848) */
3098 } else if (I2CRead(btv, I2C_STBEE, "eeprom")>=0) {
3099 btv->type=BTTV_STB;
3102 #if 0
3103 /* check for msp34xx */
3104 if (I2CRead(btv, 0x80, "msp3400")>=0)
3105 btv->type = BTTV_MIROPRO;
3106 else
3107 btv->type = BTTV_MIRO;
3108 #endif
3111 /* print which board we have found */
3112 sprintf(btv->video_dev.name,"BT%d%s(%.22s)",
3113 btv->id,
3114 (btv->id==848 && btv->revision==0x12) ? "A" : "",
3115 tvcards[btv->type].name);
3116 printk(KERN_INFO "bttv%d: model: %s\n",btv->nr,btv->video_dev.name);
3119 /* board specific initialisations */
3120 if (btv->type == BTTV_MIRO || btv->type == BTTV_MIROPRO) {
3121 /* auto detect tuner for MIRO cards */
3122 btv->tuner_type=((btread(BT848_GPIO_DATA)>>10)-1)&7;
3124 if (btv->type == BTTV_HAUPPAUGE || btv->type == BTTV_HAUPPAUGE878) {
3125 /* pick up some config infos from the eeprom */
3126 if (0xa0 != eeprom) {
3127 eeprom = 0xa0;
3128 readee(btv,eeprom_data,0xa0);
3130 hauppauge_eeprom(btv);
3131 hauppauge_boot_msp34xx(btv);
3133 if (btv->type == BTTV_PXC200)
3134 init_PXC200(btv);
3137 /* pll configuration */
3138 if (!(btv->id==848 && btv->revision==0x11)) {
3139 /* defaults from card list */
3140 if (PLL_28 == tvcards[btv->type].pll) {
3141 btv->pll.pll_ifreq=28636363;
3142 btv->pll.pll_crystal=BT848_IFORM_XT0;
3144 /* insmod options can override */
3145 switch (pll[btv->nr]) {
3146 case 0: /* none */
3147 btv->pll.pll_crystal = 0;
3148 btv->pll.pll_ifreq = 0;
3149 btv->pll.pll_ofreq = 0;
3150 break;
3151 case 1: /* 28 MHz */
3152 btv->pll.pll_ifreq = 28636363;
3153 btv->pll.pll_ofreq = 0;
3154 btv->pll.pll_crystal=BT848_IFORM_XT0;
3155 break;
3156 case 2: /* 35 MHz */
3157 btv->pll.pll_ifreq = 35468950;
3158 btv->pll.pll_ofreq = 0;
3159 btv->pll.pll_crystal=BT848_IFORM_XT1;
3160 break;
3165 /* tuner configuration */
3166 if (-1 != tvcards[btv->type].tuner_type)
3167 btv->tuner_type = tvcards[btv->type].tuner_type;
3168 if (btv->tuner_type != -1)
3169 call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
3171 /* try to detect audio/fader chips */
3172 if (tvcards[btv->type].msp34xx &&
3173 I2CRead(btv, I2C_MSP3400, "MSP34xx") >=0) {
3174 if (autoload)
3175 request_module("msp3400");
3178 if (tvcards[btv->type].tda8425 &&
3179 I2CRead(btv, I2C_TDA8425, "TDA8425") >=0) {
3180 if (autoload)
3181 request_module("tda8425");
3184 if (tvcards[btv->type].tda9840 &&
3185 I2CRead(btv, I2C_TDA9840, "TDA9840") >=0) {
3186 init_tda9840(btv);
3187 btv->audio_chip = TDA9840;
3188 /* move this to a module too? */
3189 init_tda9840(btv);
3192 if (tvcards[btv->type].tda985x &&
3193 I2CRead(btv, I2C_TDA9850, "TDA985x") >=0) {
3194 if (autoload)
3195 request_module("tda985x");
3198 if (tvcards[btv->type].tda9875 &&
3199 I2CRead(btv, I2C_TDA9875, "TDA9875") >=0) {
3200 if (autoload)
3201 request_module("tda9875");
3204 if (tvcards[btv->type].tda7432 &&
3205 I2CRead(btv, I2C_TDA7432, "TDA7432") >=0) {
3206 if (autoload)
3207 request_module("tda7432");
3210 if (tvcards[btv->type].tea63xx) {
3211 if (autoload)
3212 request_module("tea6300");
3215 if (tvcards[btv->type].tea64xx) {
3216 if (autoload)
3217 request_module("tea6420");
3220 if (tvcards[btv->type].tuner != -1) {
3221 if (autoload)
3222 request_module("tuner");
3225 audio(btv, AUDIO_MUTE, 1);
3229 static void bt848_set_risc_jmps(struct bttv *btv, int flags)
3231 unsigned long irq_flags;
3233 spin_lock_irqsave(&btv->s_lock, irq_flags);
3235 if (-1 == flags) {
3236 /* defaults */
3237 flags = 0;
3238 if (btv->scr_on)
3239 flags |= 0x03;
3240 if (btv->vbi_on)
3241 flags |= 0x0c;
3244 if (debug) printk("bttv%d: set_risc_jmp %08lx:",
3245 btv->nr,virt_to_bus(btv->risc_jmp));
3247 /* Sync to start of odd field */
3248 btv->risc_jmp[0]=cpu_to_le32(BT848_RISC_SYNC|BT848_RISC_RESYNC
3249 |BT848_FIFO_STATUS_VRE);
3250 btv->risc_jmp[1]=cpu_to_le32(0);
3252 /* Jump to odd vbi sub */
3253 btv->risc_jmp[2]=cpu_to_le32(BT848_RISC_JUMP|(0xd<<20));
3254 if (flags&8) {
3255 if (debug) printk(" ev=%08lx",virt_to_bus(btv->vbi_odd));
3256 btv->risc_jmp[3]=cpu_to_le32(virt_to_bus(btv->vbi_odd));
3257 } else {
3258 if (debug) printk(" -----------");
3259 btv->risc_jmp[3]=cpu_to_le32(virt_to_bus(btv->risc_jmp+4));
3262 /* Jump to odd sub */
3263 btv->risc_jmp[4]=cpu_to_le32(BT848_RISC_JUMP|(0xe<<20));
3264 if (0 != btv->risc_cap_odd) {
3265 if (debug) printk(" e%d=%08x",btv->gq_grab,btv->risc_cap_odd);
3266 flags |= 3;
3267 btv->risc_jmp[5]=cpu_to_le32(btv->risc_cap_odd);
3268 } else if (flags&2) {
3269 if (debug) printk(" eo=%08lx",virt_to_bus(btv->risc_scr_odd));
3270 btv->risc_jmp[5]=cpu_to_le32(virt_to_bus(btv->risc_scr_odd));
3271 } else {
3272 if (debug) printk(" -----------");
3273 btv->risc_jmp[5]=cpu_to_le32(virt_to_bus(btv->risc_jmp+6));
3277 /* Sync to start of even field */
3278 btv->risc_jmp[6]=cpu_to_le32(BT848_RISC_SYNC|BT848_RISC_RESYNC
3279 |BT848_FIFO_STATUS_VRO);
3280 btv->risc_jmp[7]=cpu_to_le32(0);
3282 /* Jump to even vbi sub */
3283 btv->risc_jmp[8]=cpu_to_le32(BT848_RISC_JUMP);
3284 if (flags&4) {
3285 if (debug) printk(" ov=%08lx",virt_to_bus(btv->vbi_even));
3286 btv->risc_jmp[9]=cpu_to_le32(virt_to_bus(btv->vbi_even));
3287 } else {
3288 if (debug) printk(" -----------");
3289 btv->risc_jmp[9]=cpu_to_le32(virt_to_bus(btv->risc_jmp+10));
3292 /* Jump to even sub */
3293 btv->risc_jmp[10]=cpu_to_le32(BT848_RISC_JUMP|(8<<20));
3294 if (0 != btv->risc_cap_even) {
3295 if (debug) printk(" o%d=%08x",btv->gq_grab,btv->risc_cap_even);
3296 flags |= 3;
3297 btv->risc_jmp[11]=cpu_to_le32(btv->risc_cap_even);
3298 } else if (flags&1) {
3299 if (debug) printk(" oo=%08lx",virt_to_bus(btv->risc_scr_even));
3300 btv->risc_jmp[11]=cpu_to_le32(virt_to_bus(btv->risc_scr_even));
3301 } else {
3302 if (debug) printk(" -----------");
3303 btv->risc_jmp[11]=cpu_to_le32(virt_to_bus(btv->risc_jmp+12));
3306 if (btv->gq_start) {
3307 btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP|(0x8<<16)|BT848_RISC_IRQ);
3308 } else {
3309 btv->risc_jmp[12]=cpu_to_le32(BT848_RISC_JUMP);
3311 btv->risc_jmp[13]=cpu_to_le32(virt_to_bus(btv->risc_jmp));
3313 /* enable cpaturing and DMA */
3314 if (debug) printk(" flags=0x%x dma=%s\n",
3315 flags,(flags&0x0f) ? "on" : "off");
3316 btaor(flags, ~0x0f, BT848_CAP_CTL);
3317 if (flags&0x0f)
3318 bt848_dma(btv, 3);
3319 else
3320 bt848_dma(btv, 0);
3322 spin_unlock_irqrestore(&btv->s_lock, irq_flags);
3325 static int __init init_video_dev(struct bttv *btv)
3327 memcpy(&btv->video_dev,&bttv_template, sizeof(bttv_template));
3328 memcpy(&btv->vbi_dev,&vbi_template, sizeof(vbi_template));
3329 memcpy(&btv->radio_dev,&radio_template,sizeof(radio_template));
3331 idcard(btv);
3333 if(video_register_device(&btv->video_dev,VFL_TYPE_GRABBER)<0)
3334 return -1;
3335 if(video_register_device(&btv->vbi_dev,VFL_TYPE_VBI)<0)
3337 video_unregister_device(&btv->video_dev);
3338 return -1;
3340 if (radio[btv->nr])
3342 if(video_register_device(&btv->radio_dev, VFL_TYPE_RADIO)<0)
3344 video_unregister_device(&btv->vbi_dev);
3345 video_unregister_device(&btv->video_dev);
3346 return -1;
3349 return 1;
3352 static int __init init_bt848(struct bttv *btv)
3354 int j;
3356 btv->user=0;
3357 init_MUTEX(&btv->lock);
3359 /* dump current state of the gpio registers before changing them,
3360 * might help to make a new card work */
3361 if (verbose >= 2)
3362 printk("bttv%d: gpio: out_enable=0x%x, data=0x%x, in=0x%x\n",
3363 btv->nr,
3364 btread(BT848_GPIO_OUT_EN),
3365 btread(BT848_GPIO_DATA),
3366 btread(BT848_GPIO_REG_INP));
3368 /* reset the bt848 */
3369 btwrite(0, BT848_SRESET);
3370 DEBUG(printk(KERN_DEBUG "bttv%d: bt848_mem: 0x%lx\n", btv->nr, (unsigned long) btv->bt848_mem));
3372 /* not registered yet */
3373 btv->video_dev.minor = -1;
3374 btv->radio_dev.minor = -1;
3375 btv->vbi_dev.minor = -1;
3377 /* default setup for max. PAL size in a 1024xXXX hicolor framebuffer */
3378 btv->win.norm=0; /* change this to 1 for NTSC, 2 for SECAM */
3379 btv->win.interlace=1;
3380 btv->win.x=0;
3381 btv->win.y=0;
3382 btv->win.width=768; /* 640 */
3383 btv->win.height=576; /* 480 */
3384 btv->win.bpp=2;
3385 btv->win.depth=16;
3386 btv->win.color_fmt=BT848_COLOR_FMT_RGB16;
3387 btv->win.bpl=1024*btv->win.bpp;
3388 btv->win.swidth=1024;
3389 btv->win.sheight=768;
3390 btv->win.vidadr=0;
3391 btv->vbi_on=0;
3392 btv->scr_on=0;
3394 btv->risc_scr_odd=0;
3395 btv->risc_scr_even=0;
3396 btv->risc_cap_odd=0;
3397 btv->risc_cap_even=0;
3398 btv->risc_jmp=0;
3399 btv->vbibuf=0;
3400 btv->field=btv->last_field=0;
3402 btv->errors=0;
3403 btv->needs_restart=0;
3405 /* i2c */
3406 btv->tuner_type=-1;
3407 init_bttv_i2c(btv);
3409 if (!(btv->risc_scr_odd=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
3410 return -1;
3411 if (!(btv->risc_scr_even=(unsigned int *) kmalloc(RISCMEM_LEN/2, GFP_KERNEL)))
3412 return -1;
3413 if (!(btv->risc_jmp =(unsigned int *) kmalloc(2048, GFP_KERNEL)))
3414 return -1;
3415 DEBUG(printk(KERN_DEBUG "risc_jmp: %p\n",btv->risc_jmp));
3416 btv->vbi_odd=btv->risc_jmp+16;
3417 btv->vbi_even=btv->vbi_odd+256;
3418 btv->bus_vbi_odd=virt_to_bus(btv->risc_jmp+12);
3419 btv->bus_vbi_even=virt_to_bus(btv->risc_jmp+6);
3421 btwrite(virt_to_bus(btv->risc_jmp+2), BT848_RISC_STRT_ADD);
3422 btv->vbibuf=(unsigned char *) vmalloc(VBIBUF_SIZE);
3423 if (!btv->vbibuf)
3424 return -1;
3425 if (!(btv->gbuf = kmalloc(sizeof(struct bttv_gbuf)*gbuffers,GFP_KERNEL)))
3426 return -1;
3427 for (j = 0; j < gbuffers; j++) {
3428 if (!(btv->gbuf[j].risc = kmalloc(16384,GFP_KERNEL)))
3429 return -1;
3432 memset(btv->vbibuf, 0, VBIBUF_SIZE); /* We don't want to return random
3433 memory to the user */
3435 btv->fbuffer=NULL;
3437 bt848_muxsel(btv, 1);
3438 bt848_set_winsize(btv);
3440 /* btwrite(0, BT848_TDEC); */
3441 btwrite(0x10, BT848_COLOR_CTL);
3442 btwrite(0x00, BT848_CAP_CTL);
3443 /* set planar and packed mode trigger points and */
3444 /* set rising edge of inverted GPINTR pin as irq trigger */
3445 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
3446 BT848_GPIO_DMA_CTL_PLTP1_16|
3447 BT848_GPIO_DMA_CTL_PLTP23_16|
3448 BT848_GPIO_DMA_CTL_GPINTC|
3449 BT848_GPIO_DMA_CTL_GPINTI,
3450 BT848_GPIO_DMA_CTL);
3452 /* select direct input */
3453 btwrite(0x00, BT848_GPIO_REG_INP);
3455 btwrite(BT848_IFORM_MUX1 | BT848_IFORM_XTAUTO | BT848_IFORM_AUTO,
3456 BT848_IFORM);
3458 btwrite(0xd8, BT848_CONTRAST_LO);
3459 bt848_bright(btv, 0x10);
3461 btwrite(0x20, BT848_E_VSCALE_HI);
3462 btwrite(0x20, BT848_O_VSCALE_HI);
3463 btwrite(/*BT848_ADC_SYNC_T|*/
3464 BT848_ADC_RESERVED|BT848_ADC_CRUSH, BT848_ADC);
3466 btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
3467 btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
3469 btv->picture.colour=254<<7;
3470 btv->picture.brightness=128<<8;
3471 btv->picture.hue=128<<8;
3472 btv->picture.contrast=0xd8<<7;
3474 btwrite(0x00, BT848_E_SCLOOP);
3475 btwrite(0x00, BT848_O_SCLOOP);
3477 /* clear interrupt status */
3478 btwrite(~0x0UL, BT848_INT_STAT);
3480 /* set interrupt mask */
3481 btwrite(btv->triton1|
3482 /*BT848_INT_PABORT|BT848_INT_RIPERR|BT848_INT_PPERR|
3483 BT848_INT_FDSR|BT848_INT_FTRGT|BT848_INT_FBUS|*/
3484 (fieldnr ? BT848_INT_VSYNC : 0)|
3485 BT848_INT_GPINT|
3486 BT848_INT_SCERR|
3487 BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
3488 BT848_INT_FMTCHG|BT848_INT_HLOCK,
3489 BT848_INT_MASK);
3491 make_vbitab(btv);
3492 bt848_set_risc_jmps(btv,-1);
3495 * Now add the template and register the device unit.
3497 init_video_dev(btv);
3499 return 0;
3502 static void bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
3504 u32 stat,astat;
3505 u32 dstat;
3506 int count,i;
3507 struct bttv *btv;
3509 btv=(struct bttv *)dev_id;
3510 count=0;
3511 while (1)
3513 /* get/clear interrupt status bits */
3514 stat=btread(BT848_INT_STAT);
3515 astat=stat&btread(BT848_INT_MASK);
3516 if (!astat)
3517 return;
3518 IDEBUG(printk ("bttv%d: astat=%08x\n", btv->nr, astat));
3519 IDEBUG(printk ("bttv%d: stat=%08x\n", btv->nr, stat));
3521 /* get device status bits */
3522 dstat=btread(BT848_DSTATUS);
3524 if (astat&BT848_INT_GPINT) {
3525 IDEBUG(printk ("bttv%d: IRQ_GPINT\n", btv->nr));
3526 wake_up_interruptible(&btv->gpioq);
3529 if (astat&BT848_INT_FMTCHG)
3531 IDEBUG(printk ("bttv%d: IRQ_FMTCHG\n", btv->nr));
3532 /*btv->win.norm&=
3533 (dstat&BT848_DSTATUS_NUML) ? (~1) : (~0); */
3535 if (astat&BT848_INT_VPRES)
3537 IDEBUG(printk ("bttv%d: IRQ_VPRES\n", btv->nr));
3539 if (astat&BT848_INT_VSYNC)
3541 IDEBUG(printk ("bttv%d: IRQ_VSYNC\n", btv->nr));
3542 btv->field++;
3544 if (astat&(BT848_INT_SCERR|BT848_INT_OCERR)) {
3545 if (verbose)
3546 printk("bttv%d: irq:%s%s risc_count=%08x\n",
3547 btv->nr,
3548 (astat&BT848_INT_SCERR) ? " SCERR" : "",
3549 (astat&BT848_INT_OCERR) ? " OCERR" : "",
3550 btread(BT848_RISC_COUNT));
3551 btv->errors++;
3552 if (btv->errors < BTTV_ERRORS) {
3553 btand(~15, BT848_GPIO_DMA_CTL);
3554 btwrite(virt_to_bus(btv->risc_jmp+2),
3555 BT848_RISC_STRT_ADD);
3556 bt848_set_geo(btv,0);
3557 bt848_set_risc_jmps(btv,-1);
3558 } else {
3559 if (verbose)
3560 printk("bttv%d: aiee: error loops\n",btv->nr);
3561 /* cancel all outstanding grab requests */
3562 btv->gq_in = 0;
3563 btv->gq_out = 0;
3564 btv->gq_grab = -1;
3565 for (i = 0; i < gbuffers; i++)
3566 if (btv->gbuf[i].stat == GBUFFER_GRABBING)
3567 btv->gbuf[i].stat = GBUFFER_ERROR;
3568 /* disable DMA */
3569 btv->risc_cap_odd = 0;
3570 btv->risc_cap_even = 0;
3571 bt848_set_risc_jmps(btv,0);
3573 btv->needs_restart = 1;
3574 wake_up_interruptible(&btv->vbiq);
3575 wake_up_interruptible(&btv->capq);
3578 if (astat&BT848_INT_RISCI)
3580 if (debug > 1)
3581 printk("bttv%d: IRQ_RISCI\n",btv->nr);
3583 /* captured VBI frame */
3584 if (stat&(1<<28))
3586 btv->vbip=0;
3587 /* inc vbi frame count for detecting drops */
3588 (*(u32 *)&(btv->vbibuf[VBIBUF_SIZE - 4]))++;
3589 wake_up_interruptible(&btv->vbiq);
3592 /* captured full frame */
3593 if (stat&(2<<28) && btv->gq_grab != -1)
3595 btv->last_field=btv->field;
3596 if (debug)
3597 printk("bttv%d: cap irq: done %d\n",btv->nr,btv->gq_grab);
3598 do_gettimeofday(&btv->gbuf[btv->gq_grab].tv);
3599 btv->gbuf[btv->gq_grab].stat = GBUFFER_DONE;
3600 btv->gq_grab = -1;
3601 if (btv->gq_in != btv->gq_out)
3603 btv->gq_grab = btv->gqueue[btv->gq_out++];
3604 btv->gq_out = btv->gq_out % MAX_GBUFFERS;
3605 if (debug)
3606 printk("bttv%d: cap irq: capture %d\n",btv->nr,btv->gq_grab);
3607 btv->risc_cap_odd = btv->gbuf[btv->gq_grab].ro;
3608 btv->risc_cap_even = btv->gbuf[btv->gq_grab].re;
3609 bt848_set_risc_jmps(btv,-1);
3610 bt848_set_geo(btv,0);
3611 btwrite(BT848_COLOR_CTL_GAMMA,
3612 BT848_COLOR_CTL);
3613 } else {
3614 btv->risc_cap_odd = 0;
3615 btv->risc_cap_even = 0;
3616 bt848_set_risc_jmps(btv,-1);
3617 bt848_set_geo(btv,0);
3618 btwrite(btv->fb_color_ctl | BT848_COLOR_CTL_GAMMA,
3619 BT848_COLOR_CTL);
3621 wake_up_interruptible(&btv->capq);
3622 break;
3624 if (stat&(8<<28))
3626 btv->gq_start = 0;
3627 btv->gq_grab = btv->gqueue[btv->gq_out++];
3628 btv->gq_out = btv->gq_out % MAX_GBUFFERS;
3629 if (debug)
3630 printk("bttv%d: cap irq: capture %d\n",btv->nr,btv->gq_grab);
3631 btv->risc_cap_odd = btv->gbuf[btv->gq_grab].ro;
3632 btv->risc_cap_even = btv->gbuf[btv->gq_grab].re;
3633 bt848_set_risc_jmps(btv,-1);
3634 bt848_set_geo(btv,0);
3635 btwrite(BT848_COLOR_CTL_GAMMA,
3636 BT848_COLOR_CTL);
3639 if (astat&BT848_INT_OCERR)
3641 IDEBUG(printk ("bttv%d: IRQ_OCERR\n", btv->nr));
3643 if (astat&BT848_INT_PABORT)
3645 IDEBUG(printk ("bttv%d: IRQ_PABORT\n", btv->nr));
3647 if (astat&BT848_INT_RIPERR)
3649 IDEBUG(printk ("bttv%d: IRQ_RIPERR\n", btv->nr));
3651 if (astat&BT848_INT_PPERR)
3653 IDEBUG(printk ("bttv%d: IRQ_PPERR\n", btv->nr));
3655 if (astat&BT848_INT_FDSR)
3657 IDEBUG(printk ("bttv%d: IRQ_FDSR\n", btv->nr));
3659 if (astat&BT848_INT_FTRGT)
3661 IDEBUG(printk ("bttv%d: IRQ_FTRGT\n", btv->nr));
3663 if (astat&BT848_INT_FBUS)
3665 IDEBUG(printk ("bttv%d: IRQ_FBUS\n", btv->nr));
3667 if (astat&BT848_INT_HLOCK)
3669 if ((dstat&BT848_DSTATUS_HLOC) || (btv->radio))
3670 audio(btv, AUDIO_ON,0);
3671 else
3672 audio(btv, AUDIO_OFF,0);
3675 if (astat&BT848_INT_I2CDONE)
3677 IDEBUG(printk ("bttv%d: IRQ_I2CDONE\n", btv->nr));
3680 btwrite(astat,BT848_INT_STAT);
3682 count++;
3683 if (count > 10)
3684 printk (KERN_WARNING "bttv%d: irq loop %d\n",
3685 btv->nr,count);
3686 if (count > 20)
3688 btwrite(0, BT848_INT_MASK);
3689 printk(KERN_ERR
3690 "bttv%d: IRQ lockup, cleared int mask\n", btv->nr);
3698 * Scan for a Bt848 card, request the irq and map the io memory
3701 static void __init bttv_remove(struct pci_dev *pci_dev)
3703 u8 command;
3704 int j;
3705 struct bttv *btv = pci_dev->driver_data;
3707 /* unregister i2c_bus */
3708 i2c_bit_del_bus(&btv->i2c_adap);
3710 /* turn off all capturing, DMA and IRQs */
3711 btand(~15, BT848_GPIO_DMA_CTL);
3713 /* first disable interrupts before unmapping the memory! */
3714 btwrite(0, BT848_INT_MASK);
3715 btwrite(~0x0UL,BT848_INT_STAT);
3716 btwrite(0x0, BT848_GPIO_OUT_EN);
3718 /* disable PCI bus-mastering */
3719 pci_read_config_byte(btv->dev, PCI_COMMAND, &command);
3720 /* Should this be &=~ ?? */
3721 command&=~PCI_COMMAND_MASTER;
3722 pci_write_config_byte(btv->dev, PCI_COMMAND, command);
3724 /* unmap and free memory */
3725 for (j = 0; j < gbuffers; j++)
3726 if (btv->gbuf[j].risc)
3727 kfree(btv->gbuf[j].risc);
3728 if (btv->gbuf)
3729 kfree((void *) btv->gbuf);
3731 if (btv->risc_scr_odd)
3732 kfree((void *) btv->risc_scr_odd);
3734 if (btv->risc_scr_even)
3735 kfree((void *) btv->risc_scr_even);
3737 DEBUG(printk(KERN_DEBUG "free: risc_jmp: 0x%p.\n", btv->risc_jmp));
3738 if (btv->risc_jmp)
3739 kfree((void *) btv->risc_jmp);
3741 DEBUG(printk(KERN_DEBUG "bt848_vbibuf: 0x%p.\n", btv->vbibuf));
3742 if (btv->vbibuf)
3743 vfree((void *) btv->vbibuf);
3745 free_irq(btv->irq,btv);
3746 DEBUG(printk(KERN_DEBUG "bt848_mem: 0x%p.\n", btv->bt848_mem));
3747 if (btv->bt848_mem)
3748 iounmap(btv->bt848_mem);
3750 if(btv->video_dev.minor!=-1)
3751 video_unregister_device(&btv->video_dev);
3752 if(btv->vbi_dev.minor!=-1)
3753 video_unregister_device(&btv->vbi_dev);
3754 if (radio[btv->nr] && btv->radio_dev.minor != -1)
3755 video_unregister_device(&btv->radio_dev);
3757 release_mem_region(btv->bt848_adr,
3758 pci_resource_len(btv->dev,0));
3759 /* wake up any waiting processes
3760 because shutdown flag is set, no new processes (in this queue)
3761 are expected
3763 btv->shutdown=1;
3764 wake_up(&btv->gpioq);
3766 return;
3770 static int __init bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
3772 int result;
3773 unsigned char command;
3774 struct bttv *btv;
3775 #if defined(__powerpc__)
3776 unsigned int cmd;
3777 #endif
3779 printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
3781 btv=&bttvs[bttv_num];
3782 btv->dev=dev;
3783 btv->nr = bttv_num;
3784 btv->bt848_mem=NULL;
3785 btv->vbibuf=NULL;
3786 btv->risc_jmp=NULL;
3787 btv->vbi_odd=NULL;
3788 btv->vbi_even=NULL;
3789 init_waitqueue_head(&btv->vbiq);
3790 init_waitqueue_head(&btv->capq);
3791 init_waitqueue_head(&btv->capqo);
3792 init_waitqueue_head(&btv->capqe);
3793 btv->vbip=VBIBUF_SIZE;
3795 init_waitqueue_head(&btv->gpioq);
3796 btv->s_lock = SPIN_LOCK_UNLOCKED;
3797 btv->shutdown=0;
3799 btv->id=dev->device;
3800 btv->irq=dev->irq;
3801 btv->bt848_adr=pci_resource_start(dev, 0);
3802 if (pci_enable_device(dev))
3803 return -EIO;
3804 if (!request_mem_region(btv->bt848_adr,
3805 pci_resource_len(dev,0),
3806 "bttv")) {
3807 return -EBUSY;
3809 if (btv->id >= 878)
3810 btv->i2c_command = 0x83;
3811 else
3812 btv->i2c_command=(I2C_TIMING | BT848_I2C_SCL | BT848_I2C_SDA);
3814 pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
3815 printk(KERN_INFO "bttv%d: Brooktree Bt%d (rev %d) ",
3816 bttv_num,btv->id, btv->revision);
3817 printk("bus: %d, devfn: %d, ",dev->bus->number, dev->devfn);
3818 printk("irq: %d, ",btv->irq);
3819 printk("memory: 0x%lx.\n", btv->bt848_adr);
3821 #if defined(__powerpc__)
3822 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3823 /* response on cards with no firmware is not enabled by OF */
3824 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3825 cmd = (cmd | PCI_COMMAND_MEMORY );
3826 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3827 #endif
3829 #ifdef __sparc__
3830 btv->bt848_mem=(unsigned char *)btv->bt848_adr;
3831 #else
3832 btv->bt848_mem=ioremap(btv->bt848_adr, 0x1000);
3833 #endif
3835 /* clear interrupt mask */
3836 btwrite(0, BT848_INT_MASK);
3838 result = request_irq(btv->irq, bttv_irq,
3839 SA_SHIRQ | SA_INTERRUPT,"bttv",(void *)btv);
3840 if (result==-EINVAL)
3842 printk(KERN_ERR "bttv%d: Bad irq number or handler\n",
3843 bttv_num);
3844 goto fail;
3846 if (result==-EBUSY)
3848 printk(KERN_ERR "bttv%d: IRQ %d busy, change your PnP config in BIOS\n",bttv_num,btv->irq);
3849 goto fail;
3851 if (result < 0)
3852 goto fail;
3854 pci_set_master(dev);
3856 btv->triton1=triton1 ? BT848_INT_ETBF : 0;
3857 if (triton1 && btv->id >= 878)
3859 btv->triton1 = 0;
3860 printk("bttv: Enabling 430FX compatibilty for bt878\n");
3861 pci_read_config_byte(dev, BT878_DEVCTRL, &command);
3862 command|=BT878_EN_TBFX;
3863 pci_write_config_byte(dev, BT878_DEVCTRL, command);
3864 pci_read_config_byte(dev, BT878_DEVCTRL, &command);
3865 if (!(command&BT878_EN_TBFX))
3867 printk("bttv: 430FX compatibility could not be enabled\n");
3868 free_irq(btv->irq,btv);
3869 result = -1;
3870 goto fail;
3874 dev->driver_data = btv;
3876 if(init_bt848(btv) < 0) {
3877 bttv_remove(dev);
3878 return -EIO;
3881 bttv_num++;
3883 return 0;
3885 fail:
3886 release_mem_region(btv->bt848_adr,
3887 pci_resource_len(btv->dev,0));
3888 return result;
3891 static struct pci_device_id bttv_pci_tbl[] __initdata = {
3892 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
3893 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3894 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
3895 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3896 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
3897 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3898 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
3899 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3900 {0,}
3903 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
3905 static struct pci_driver bttv_pci_driver = {
3906 name:"bttv",
3907 id_table:bttv_pci_tbl,
3908 probe:bttv_probe,
3909 remove:bttv_remove,
3912 static int __init bttv_init_module(void)
3914 bttv_num = 0;
3916 printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
3917 (BTTV_VERSION_CODE >> 16) & 0xff,
3918 (BTTV_VERSION_CODE >> 8) & 0xff,
3919 BTTV_VERSION_CODE & 0xff);
3920 if (gbuffers < 2 || gbuffers > MAX_GBUFFERS)
3921 gbuffers = 2;
3922 if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
3923 gbufsize = BTTV_MAX_FBUF;
3924 if (verbose)
3925 printk(KERN_INFO "bttv: using %d buffers with %dk (%dk total) for capture\n",
3926 gbuffers,gbufsize/1024,gbuffers*gbufsize/1024);
3928 handle_chipset();
3930 return pci_module_init(&bttv_pci_driver);
3933 static void __exit bttv_cleanup_module(void)
3935 pci_unregister_driver(&bttv_pci_driver);
3936 return;
3939 module_init(bttv_init_module);
3940 module_exit(bttv_cleanup_module);
3943 * Local variables:
3944 * c-basic-offset: 8
3945 * End: