Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / media / dvb / bt8xx / bt878.c
blob62d205eb4246204156537f4099a852be8bb65241
1 /*
2 * bt878.c: part of the driver for the Pinnacle PCTV Sat DVB PCI card
4 * Copyright (C) 2002 Peter Hettkamp <peter.hettkamp@htp-tel.de>
6 * large parts based on the bttv driver
7 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@metzlerbros.de)
8 * & Marcus Metzler (mocm@metzlerbros.de)
9 * (c) 1999,2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/pci.h>
33 #include <asm/io.h>
34 #include <linux/ioport.h>
35 #include <asm/pgtable.h>
36 #include <asm/page.h>
37 #include <linux/types.h>
38 #include <linux/interrupt.h>
39 #include <linux/kmod.h>
40 #include <linux/vmalloc.h>
41 #include <linux/init.h>
43 #include "dmxdev.h"
44 #include "dvbdev.h"
45 #include "bt878.h"
46 #include "dst_priv.h"
49 /**************************************/
50 /* Miscellaneous utility definitions */
51 /**************************************/
53 static unsigned int bt878_verbose = 1;
54 static unsigned int bt878_debug;
56 module_param_named(verbose, bt878_verbose, int, 0444);
57 MODULE_PARM_DESC(verbose,
58 "verbose startup messages, default is 1 (yes)");
59 module_param_named(debug, bt878_debug, int, 0644);
60 MODULE_PARM_DESC(debug, "Turn on/off debugging, default is 0 (off).");
62 int bt878_num;
63 struct bt878 bt878[BT878_MAX];
65 EXPORT_SYMBOL(bt878_num);
66 EXPORT_SYMBOL(bt878);
68 #define btwrite(dat,adr) bmtwrite((dat), (bt->bt878_mem+(adr)))
69 #define btread(adr) bmtread(bt->bt878_mem+(adr))
71 #define btand(dat,adr) btwrite((dat) & btread(adr), adr)
72 #define btor(dat,adr) btwrite((dat) | btread(adr), adr)
73 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
75 #if defined(dprintk)
76 #undef dprintk
77 #endif
78 <<<<<<< HEAD:drivers/media/dvb/bt8xx/bt878.c
79 #define dprintk if(bt878_debug) printk
80 =======
81 #define dprintk(fmt, arg...) \
82 do { \
83 if (bt878_debug) \
84 printk(KERN_DEBUG fmt, ##arg); \
85 } while (0)
86 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/dvb/bt8xx/bt878.c
88 static void bt878_mem_free(struct bt878 *bt)
90 if (bt->buf_cpu) {
91 pci_free_consistent(bt->dev, bt->buf_size, bt->buf_cpu,
92 bt->buf_dma);
93 bt->buf_cpu = NULL;
96 if (bt->risc_cpu) {
97 pci_free_consistent(bt->dev, bt->risc_size, bt->risc_cpu,
98 bt->risc_dma);
99 bt->risc_cpu = NULL;
103 static int bt878_mem_alloc(struct bt878 *bt)
105 if (!bt->buf_cpu) {
106 bt->buf_size = 128 * 1024;
108 bt->buf_cpu =
109 pci_alloc_consistent(bt->dev, bt->buf_size,
110 &bt->buf_dma);
112 if (!bt->buf_cpu)
113 return -ENOMEM;
115 memset(bt->buf_cpu, 0, bt->buf_size);
118 if (!bt->risc_cpu) {
119 bt->risc_size = PAGE_SIZE;
120 bt->risc_cpu =
121 pci_alloc_consistent(bt->dev, bt->risc_size,
122 &bt->risc_dma);
124 if (!bt->risc_cpu) {
125 bt878_mem_free(bt);
126 return -ENOMEM;
129 memset(bt->risc_cpu, 0, bt->risc_size);
132 return 0;
135 /* RISC instructions */
136 #define RISC_WRITE (0x01 << 28)
137 #define RISC_JUMP (0x07 << 28)
138 #define RISC_SYNC (0x08 << 28)
140 /* RISC bits */
141 #define RISC_WR_SOL (1 << 27)
142 #define RISC_WR_EOL (1 << 26)
143 #define RISC_IRQ (1 << 24)
144 #define RISC_STATUS(status) ((((~status) & 0x0F) << 20) | ((status & 0x0F) << 16))
145 #define RISC_SYNC_RESYNC (1 << 15)
146 #define RISC_SYNC_FM1 0x06
147 #define RISC_SYNC_VRO 0x0C
149 #define RISC_FLUSH() bt->risc_pos = 0
150 #define RISC_INSTR(instr) bt->risc_cpu[bt->risc_pos++] = cpu_to_le32(instr)
152 static int bt878_make_risc(struct bt878 *bt)
154 bt->block_bytes = bt->buf_size >> 4;
155 bt->block_count = 1 << 4;
156 bt->line_bytes = bt->block_bytes;
157 bt->line_count = bt->block_count;
159 while (bt->line_bytes > 4095) {
160 bt->line_bytes >>= 1;
161 bt->line_count <<= 1;
164 if (bt->line_count > 255) {
165 <<<<<<< HEAD:drivers/media/dvb/bt8xx/bt878.c
166 printk("bt878: buffer size error!\n");
167 =======
168 printk(KERN_ERR "bt878: buffer size error!\n");
169 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/dvb/bt8xx/bt878.c
170 return -EINVAL;
172 return 0;
176 static void bt878_risc_program(struct bt878 *bt, u32 op_sync_orin)
178 u32 buf_pos = 0;
179 u32 line;
181 RISC_FLUSH();
182 RISC_INSTR(RISC_SYNC | RISC_SYNC_FM1 | op_sync_orin);
183 RISC_INSTR(0);
185 dprintk("bt878: risc len lines %u, bytes per line %u\n",
186 bt->line_count, bt->line_bytes);
187 for (line = 0; line < bt->line_count; line++) {
188 // At the beginning of every block we issue an IRQ with previous (finished) block number set
189 if (!(buf_pos % bt->block_bytes))
190 RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL |
191 RISC_IRQ |
192 RISC_STATUS(((buf_pos /
193 bt->block_bytes) +
194 (bt->block_count -
195 1)) %
196 bt->block_count) | bt->
197 line_bytes);
198 else
199 RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL |
200 bt->line_bytes);
201 RISC_INSTR(bt->buf_dma + buf_pos);
202 buf_pos += bt->line_bytes;
205 RISC_INSTR(RISC_SYNC | op_sync_orin | RISC_SYNC_VRO);
206 RISC_INSTR(0);
208 RISC_INSTR(RISC_JUMP);
209 RISC_INSTR(bt->risc_dma);
211 btwrite((bt->line_count << 16) | bt->line_bytes, BT878_APACK_LEN);
214 /*****************************/
215 /* Start/Stop grabbing funcs */
216 /*****************************/
218 void bt878_start(struct bt878 *bt, u32 controlreg, u32 op_sync_orin,
219 u32 irq_err_ignore)
221 u32 int_mask;
223 dprintk("bt878 debug: bt878_start (ctl=%8.8x)\n", controlreg);
224 /* complete the writing of the risc dma program now we have
225 * the card specifics
227 bt878_risc_program(bt, op_sync_orin);
228 controlreg &= ~0x1f;
229 controlreg |= 0x1b;
231 btwrite(bt->risc_dma, BT878_ARISC_START);
233 /* original int mask had :
234 * 6 2 8 4 0
235 * 1111 1111 1000 0000 0000
236 * SCERR|OCERR|PABORT|RIPERR|FDSR|FTRGT|FBUS|RISCI
237 * Hacked for DST to:
238 * SCERR | OCERR | FDSR | FTRGT | FBUS | RISCI
240 int_mask = BT878_ASCERR | BT878_AOCERR | BT878_APABORT |
241 BT878_ARIPERR | BT878_APPERR | BT878_AFDSR | BT878_AFTRGT |
242 BT878_AFBUS | BT878_ARISCI;
245 /* ignore pesky bits */
246 int_mask &= ~irq_err_ignore;
248 btwrite(int_mask, BT878_AINT_MASK);
249 btwrite(controlreg, BT878_AGPIO_DMA_CTL);
252 void bt878_stop(struct bt878 *bt)
254 u32 stat;
255 int i = 0;
257 dprintk("bt878 debug: bt878_stop\n");
259 btwrite(0, BT878_AINT_MASK);
260 btand(~0x13, BT878_AGPIO_DMA_CTL);
262 do {
263 stat = btread(BT878_AINT_STAT);
264 if (!(stat & BT878_ARISC_EN))
265 break;
266 i++;
267 } while (i < 500);
269 dprintk("bt878(%d) debug: bt878_stop, i=%d, stat=0x%8.8x\n",
270 bt->nr, i, stat);
273 EXPORT_SYMBOL(bt878_start);
274 EXPORT_SYMBOL(bt878_stop);
276 /*****************************/
277 /* Interrupt service routine */
278 /*****************************/
280 static irqreturn_t bt878_irq(int irq, void *dev_id)
282 u32 stat, astat, mask;
283 int count;
284 struct bt878 *bt;
286 bt = (struct bt878 *) dev_id;
288 count = 0;
289 while (1) {
290 stat = btread(BT878_AINT_STAT);
291 mask = btread(BT878_AINT_MASK);
292 if (!(astat = (stat & mask)))
293 return IRQ_NONE; /* this interrupt is not for me */
294 /* dprintk("bt878(%d) debug: irq count %d, stat 0x%8.8x, mask 0x%8.8x\n",bt->nr,count,stat,mask); */
295 btwrite(astat, BT878_AINT_STAT); /* try to clear interrupt condition */
298 if (astat & (BT878_ASCERR | BT878_AOCERR)) {
299 if (bt878_verbose) {
300 <<<<<<< HEAD:drivers/media/dvb/bt8xx/bt878.c
301 printk("bt878(%d): irq%s%s risc_pc=%08x\n",
302 =======
303 printk(KERN_INFO
304 "bt878(%d): irq%s%s risc_pc=%08x\n",
305 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/dvb/bt8xx/bt878.c
306 bt->nr,
307 (astat & BT878_ASCERR) ? " SCERR" :
309 (astat & BT878_AOCERR) ? " OCERR" :
310 "", btread(BT878_ARISC_PC));
313 if (astat & (BT878_APABORT | BT878_ARIPERR | BT878_APPERR)) {
314 if (bt878_verbose) {
315 <<<<<<< HEAD:drivers/media/dvb/bt8xx/bt878.c
316 printk
317 ("bt878(%d): irq%s%s%s risc_pc=%08x\n",
318 =======
319 printk(KERN_INFO
320 "bt878(%d): irq%s%s%s risc_pc=%08x\n",
321 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/dvb/bt8xx/bt878.c
322 bt->nr,
323 (astat & BT878_APABORT) ? " PABORT" :
325 (astat & BT878_ARIPERR) ? " RIPERR" :
327 (astat & BT878_APPERR) ? " PPERR" :
328 "", btread(BT878_ARISC_PC));
331 if (astat & (BT878_AFDSR | BT878_AFTRGT | BT878_AFBUS)) {
332 if (bt878_verbose) {
333 <<<<<<< HEAD:drivers/media/dvb/bt8xx/bt878.c
334 printk
335 ("bt878(%d): irq%s%s%s risc_pc=%08x\n",
336 =======
337 printk(KERN_INFO
338 "bt878(%d): irq%s%s%s risc_pc=%08x\n",
339 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/dvb/bt8xx/bt878.c
340 bt->nr,
341 (astat & BT878_AFDSR) ? " FDSR" : "",
342 (astat & BT878_AFTRGT) ? " FTRGT" :
344 (astat & BT878_AFBUS) ? " FBUS" : "",
345 btread(BT878_ARISC_PC));
348 if (astat & BT878_ARISCI) {
349 bt->finished_block = (stat & BT878_ARISCS) >> 28;
350 tasklet_schedule(&bt->tasklet);
351 break;
353 count++;
354 if (count > 20) {
355 btwrite(0, BT878_AINT_MASK);
356 printk(KERN_ERR
357 "bt878(%d): IRQ lockup, cleared int mask\n",
358 bt->nr);
359 break;
362 return IRQ_HANDLED;
366 bt878_device_control(struct bt878 *bt, unsigned int cmd, union dst_gpio_packet *mp)
368 int retval;
370 retval = 0;
371 if (mutex_lock_interruptible(&bt->gpio_lock))
372 return -ERESTARTSYS;
373 /* special gpio signal */
374 switch (cmd) {
375 case DST_IG_ENABLE:
376 // dprintk("dvb_bt8xx: dst enable mask 0x%02x enb 0x%02x \n", mp->dstg.enb.mask, mp->dstg.enb.enable);
377 retval = bttv_gpio_enable(bt->bttv_nr,
378 mp->enb.mask,
379 mp->enb.enable);
380 break;
381 case DST_IG_WRITE:
382 // dprintk("dvb_bt8xx: dst write gpio mask 0x%02x out 0x%02x\n", mp->dstg.outp.mask, mp->dstg.outp.highvals);
383 retval = bttv_write_gpio(bt->bttv_nr,
384 mp->outp.mask,
385 mp->outp.highvals);
387 break;
388 case DST_IG_READ:
389 /* read */
390 retval = bttv_read_gpio(bt->bttv_nr, &mp->rd.value);
391 // dprintk("dvb_bt8xx: dst read gpio 0x%02x\n", (unsigned)mp->dstg.rd.value);
392 break;
393 case DST_IG_TS:
394 /* Set packet size */
395 bt->TS_Size = mp->psize;
396 break;
398 default:
399 retval = -EINVAL;
400 break;
402 mutex_unlock(&bt->gpio_lock);
403 return retval;
406 EXPORT_SYMBOL(bt878_device_control);
408 #define BROOKTREE_878_DEVICE(vend, dev, name) \
410 .vendor = PCI_VENDOR_ID_BROOKTREE, \
411 .device = PCI_DEVICE_ID_BROOKTREE_878, \
412 .subvendor = (vend), .subdevice = (dev), \
413 .driver_data = (unsigned long) name \
416 static struct pci_device_id bt878_pci_tbl[] __devinitdata = {
417 BROOKTREE_878_DEVICE(0x0071, 0x0101, "Nebula Electronics DigiTV"),
418 BROOKTREE_878_DEVICE(0x1461, 0x0761, "AverMedia AverTV DVB-T 761"),
419 BROOKTREE_878_DEVICE(0x11bd, 0x001c, "Pinnacle PCTV Sat"),
420 BROOKTREE_878_DEVICE(0x11bd, 0x0026, "Pinnacle PCTV SAT CI"),
421 BROOKTREE_878_DEVICE(0x1822, 0x0001, "Twinhan VisionPlus DVB"),
422 BROOKTREE_878_DEVICE(0x270f, 0xfc00,
423 "ChainTech digitop DST-1000 DVB-S"),
424 BROOKTREE_878_DEVICE(0x1461, 0x0771, "AVermedia AverTV DVB-T 771"),
425 BROOKTREE_878_DEVICE(0x18ac, 0xdb10, "DViCO FusionHDTV DVB-T Lite"),
426 BROOKTREE_878_DEVICE(0x18ac, 0xdb11, "Ultraview DVB-T Lite"),
427 BROOKTREE_878_DEVICE(0x18ac, 0xd500, "DViCO FusionHDTV 5 Lite"),
428 BROOKTREE_878_DEVICE(0x7063, 0x2000, "pcHDTV HD-2000 TV"),
429 BROOKTREE_878_DEVICE(0x1822, 0x0026, "DNTV Live! Mini"),
433 MODULE_DEVICE_TABLE(pci, bt878_pci_tbl);
435 static const char * __devinit card_name(const struct pci_device_id *id)
437 return id->driver_data ? (const char *)id->driver_data : "Unknown";
440 /***********************/
441 /* PCI device handling */
442 /***********************/
444 static int __devinit bt878_probe(struct pci_dev *dev,
445 const struct pci_device_id *pci_id)
447 int result = 0;
448 unsigned char lat;
449 struct bt878 *bt;
450 #if defined(__powerpc__)
451 unsigned int cmd;
452 #endif
453 unsigned int cardid;
455 printk(KERN_INFO "bt878: Bt878 AUDIO function found (%d).\n",
456 bt878_num);
457 if (bt878_num >= BT878_MAX) {
458 printk(KERN_ERR "bt878: Too many devices inserted\n");
459 result = -ENOMEM;
460 goto fail0;
462 if (pci_enable_device(dev))
463 return -EIO;
465 cardid = dev->subsystem_device << 16;
466 cardid |= dev->subsystem_vendor;
468 printk(KERN_INFO "%s: card id=[0x%x],[ %s ] has DVB functions.\n",
469 __func__, cardid, card_name(pci_id));
471 bt = &bt878[bt878_num];
472 bt->dev = dev;
473 bt->nr = bt878_num;
474 bt->shutdown = 0;
476 bt->id = dev->device;
477 bt->irq = dev->irq;
478 bt->bt878_adr = pci_resource_start(dev, 0);
479 if (!request_mem_region(pci_resource_start(dev, 0),
480 pci_resource_len(dev, 0), "bt878")) {
481 result = -EBUSY;
482 goto fail0;
485 pci_read_config_byte(dev, PCI_CLASS_REVISION, &bt->revision);
486 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
489 printk(KERN_INFO "bt878(%d): Bt%x (rev %d) at %02x:%02x.%x, ",
490 bt878_num, bt->id, bt->revision, dev->bus->number,
491 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
492 printk("irq: %d, latency: %d, memory: 0x%lx\n",
493 bt->irq, lat, bt->bt878_adr);
496 #if defined(__powerpc__)
497 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
498 /* response on cards with no firmware is not enabled by OF */
499 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
500 cmd = (cmd | PCI_COMMAND_MEMORY);
501 pci_write_config_dword(dev, PCI_COMMAND, cmd);
502 #endif
504 #ifdef __sparc__
505 bt->bt878_mem = (unsigned char *) bt->bt878_adr;
506 #else
507 bt->bt878_mem = ioremap(bt->bt878_adr, 0x1000);
508 #endif
510 /* clear interrupt mask */
511 btwrite(0, BT848_INT_MASK);
513 result = request_irq(bt->irq, bt878_irq,
514 IRQF_SHARED | IRQF_DISABLED, "bt878",
515 (void *) bt);
516 if (result == -EINVAL) {
517 printk(KERN_ERR "bt878(%d): Bad irq number or handler\n",
518 bt878_num);
519 goto fail1;
521 if (result == -EBUSY) {
522 printk(KERN_ERR
523 "bt878(%d): IRQ %d busy, change your PnP config in BIOS\n",
524 bt878_num, bt->irq);
525 goto fail1;
527 if (result < 0)
528 goto fail1;
530 pci_set_master(dev);
531 pci_set_drvdata(dev, bt);
533 /* if(init_bt878(btv) < 0) {
534 bt878_remove(dev);
535 return -EIO;
539 if ((result = bt878_mem_alloc(bt))) {
540 <<<<<<< HEAD:drivers/media/dvb/bt8xx/bt878.c
541 printk("bt878: failed to allocate memory!\n");
542 =======
543 printk(KERN_ERR "bt878: failed to allocate memory!\n");
544 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/dvb/bt8xx/bt878.c
545 goto fail2;
548 bt878_make_risc(bt);
549 btwrite(0, BT878_AINT_MASK);
550 bt878_num++;
552 return 0;
554 fail2:
555 free_irq(bt->irq, bt);
556 fail1:
557 release_mem_region(pci_resource_start(bt->dev, 0),
558 pci_resource_len(bt->dev, 0));
559 fail0:
560 pci_disable_device(dev);
561 return result;
564 static void __devexit bt878_remove(struct pci_dev *pci_dev)
566 u8 command;
567 struct bt878 *bt = pci_get_drvdata(pci_dev);
569 if (bt878_verbose)
570 <<<<<<< HEAD:drivers/media/dvb/bt8xx/bt878.c
571 printk("bt878(%d): unloading\n", bt->nr);
572 =======
573 printk(KERN_INFO "bt878(%d): unloading\n", bt->nr);
574 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/dvb/bt8xx/bt878.c
576 /* turn off all capturing, DMA and IRQs */
577 btand(~0x13, BT878_AGPIO_DMA_CTL);
579 /* first disable interrupts before unmapping the memory! */
580 btwrite(0, BT878_AINT_MASK);
581 btwrite(~0U, BT878_AINT_STAT);
583 /* disable PCI bus-mastering */
584 pci_read_config_byte(bt->dev, PCI_COMMAND, &command);
585 /* Should this be &=~ ?? */
586 command &= ~PCI_COMMAND_MASTER;
587 pci_write_config_byte(bt->dev, PCI_COMMAND, command);
589 free_irq(bt->irq, bt);
590 printk(KERN_DEBUG "bt878_mem: 0x%p.\n", bt->bt878_mem);
591 if (bt->bt878_mem)
592 iounmap(bt->bt878_mem);
594 release_mem_region(pci_resource_start(bt->dev, 0),
595 pci_resource_len(bt->dev, 0));
596 /* wake up any waiting processes
597 because shutdown flag is set, no new processes (in this queue)
598 are expected
600 bt->shutdown = 1;
601 bt878_mem_free(bt);
603 pci_set_drvdata(pci_dev, NULL);
604 pci_disable_device(pci_dev);
605 return;
608 static struct pci_driver bt878_pci_driver = {
609 .name = "bt878",
610 .id_table = bt878_pci_tbl,
611 .probe = bt878_probe,
612 .remove = bt878_remove,
615 static int bt878_pci_driver_registered;
617 /*******************************/
618 /* Module management functions */
619 /*******************************/
621 static int bt878_init_module(void)
623 bt878_num = 0;
624 bt878_pci_driver_registered = 0;
626 printk(KERN_INFO "bt878: AUDIO driver version %d.%d.%d loaded\n",
627 (BT878_VERSION_CODE >> 16) & 0xff,
628 (BT878_VERSION_CODE >> 8) & 0xff,
629 BT878_VERSION_CODE & 0xff);
631 bt878_check_chipset();
633 /* later we register inside of bt878_find_audio_dma()
634 * because we may want to ignore certain cards */
635 bt878_pci_driver_registered = 1;
636 return pci_register_driver(&bt878_pci_driver);
639 static void bt878_cleanup_module(void)
641 if (bt878_pci_driver_registered) {
642 bt878_pci_driver_registered = 0;
643 pci_unregister_driver(&bt878_pci_driver);
645 return;
648 module_init(bt878_init_module);
649 module_exit(bt878_cleanup_module);
651 //MODULE_AUTHOR("XXX");
652 MODULE_LICENSE("GPL");
655 * Local variables:
656 * c-basic-offset: 8
657 * End: