1 /* 2 * bt878.c: part of the driver for the Pinnacle PCTV Sat DVB PCI card 3 * 4 * Copyright (C) 2002 Peter Hettkamp <peter.hettkamp@htp-tel.de> 5 * 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> 10 * 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. 15 * 16 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. 21 * 22 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 27 * 28 */ 29 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> 42 43 #include <media/dmxdev.h> 44 #include <media/dvbdev.h> 45 #include "bt878.h" 46 #include "dst_priv.h" 47 48 49 /**************************************/ 50 /* Miscellaneous utility definitions */ 51 /**************************************/ 52 53 static unsigned int bt878_verbose = 1; 54 static unsigned int bt878_debug; 55 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)."); 61 62 int bt878_num; 63 struct bt878 bt878[BT878_MAX]; 64 65 EXPORT_SYMBOL(bt878_num); 66 EXPORT_SYMBOL(bt878); 67 68 #define btwrite(dat,adr) bmtwrite((dat), (bt->bt878_mem+(adr))) 69 #define btread(adr) bmtread(bt->bt878_mem+(adr)) 70 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) 74 75 #if defined(dprintk) 76 #undef dprintk 77 #endif 78 #define dprintk(fmt, arg...) \ 79 do { \ 80 if (bt878_debug) \ 81 printk(KERN_DEBUG fmt, ##arg); \ 82 } while (0) 83 84 static void bt878_mem_free(struct bt878 *bt) 85 { 86 if (bt->buf_cpu) { 87 pci_free_consistent(bt->dev, bt->buf_size, bt->buf_cpu, 88 bt->buf_dma); 89 bt->buf_cpu = NULL; 90 } 91 92 if (bt->risc_cpu) { 93 pci_free_consistent(bt->dev, bt->risc_size, bt->risc_cpu, 94 bt->risc_dma); 95 bt->risc_cpu = NULL; 96 } 97 } 98 99 static int bt878_mem_alloc(struct bt878 *bt) 100 { 101 if (!bt->buf_cpu) { 102 bt->buf_size = 128 * 1024; 103 104 bt->buf_cpu = pci_zalloc_consistent(bt->dev, bt->buf_size, 105 &bt->buf_dma); 106 if (!bt->buf_cpu) 107 return -ENOMEM; 108 } 109 110 if (!bt->risc_cpu) { 111 bt->risc_size = PAGE_SIZE; 112 bt->risc_cpu = pci_zalloc_consistent(bt->dev, bt->risc_size, 113 &bt->risc_dma); 114 if (!bt->risc_cpu) { 115 bt878_mem_free(bt); 116 return -ENOMEM; 117 } 118 } 119 120 return 0; 121 } 122 123 /* RISC instructions */ 124 #define RISC_WRITE (0x01 << 28) 125 #define RISC_JUMP (0x07 << 28) 126 #define RISC_SYNC (0x08 << 28) 127 128 /* RISC bits */ 129 #define RISC_WR_SOL (1 << 27) 130 #define RISC_WR_EOL (1 << 26) 131 #define RISC_IRQ (1 << 24) 132 #define RISC_STATUS(status) ((((~status) & 0x0F) << 20) | ((status & 0x0F) << 16)) 133 #define RISC_SYNC_RESYNC (1 << 15) 134 #define RISC_SYNC_FM1 0x06 135 #define RISC_SYNC_VRO 0x0C 136 137 #define RISC_FLUSH() bt->risc_pos = 0 138 #define RISC_INSTR(instr) bt->risc_cpu[bt->risc_pos++] = cpu_to_le32(instr) 139 140 static int bt878_make_risc(struct bt878 *bt) 141 { 142 bt->block_bytes = bt->buf_size >> 4; 143 bt->block_count = 1 << 4; 144 bt->line_bytes = bt->block_bytes; 145 bt->line_count = bt->block_count; 146 147 while (bt->line_bytes > 4095) { 148 bt->line_bytes >>= 1; 149 bt->line_count <<= 1; 150 } 151 152 if (bt->line_count > 255) { 153 printk(KERN_ERR "bt878: buffer size error!\n"); 154 return -EINVAL; 155 } 156 return 0; 157 } 158 159 160 static void bt878_risc_program(struct bt878 *bt, u32 op_sync_orin) 161 { 162 u32 buf_pos = 0; 163 u32 line; 164 165 RISC_FLUSH(); 166 RISC_INSTR(RISC_SYNC | RISC_SYNC_FM1 | op_sync_orin); 167 RISC_INSTR(0); 168 169 dprintk("bt878: risc len lines %u, bytes per line %u\n", 170 bt->line_count, bt->line_bytes); 171 for (line = 0; line < bt->line_count; line++) { 172 // At the beginning of every block we issue an IRQ with previous (finished) block number set 173 if (!(buf_pos % bt->block_bytes)) 174 RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL | 175 RISC_IRQ | 176 RISC_STATUS(((buf_pos / 177 bt->block_bytes) + 178 (bt->block_count - 179 1)) % 180 bt->block_count) | bt-> 181 line_bytes); 182 else 183 RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL | 184 bt->line_bytes); 185 RISC_INSTR(bt->buf_dma + buf_pos); 186 buf_pos += bt->line_bytes; 187 } 188 189 RISC_INSTR(RISC_SYNC | op_sync_orin | RISC_SYNC_VRO); 190 RISC_INSTR(0); 191 192 RISC_INSTR(RISC_JUMP); 193 RISC_INSTR(bt->risc_dma); 194 195 btwrite((bt->line_count << 16) | bt->line_bytes, BT878_APACK_LEN); 196 } 197 198 /*****************************/ 199 /* Start/Stop grabbing funcs */ 200 /*****************************/ 201 202 void bt878_start(struct bt878 *bt, u32 controlreg, u32 op_sync_orin, 203 u32 irq_err_ignore) 204 { 205 u32 int_mask; 206 207 dprintk("bt878 debug: bt878_start (ctl=%8.8x)\n", controlreg); 208 /* complete the writing of the risc dma program now we have 209 * the card specifics 210 */ 211 bt878_risc_program(bt, op_sync_orin); 212 controlreg &= ~0x1f; 213 controlreg |= 0x1b; 214 215 btwrite(bt->risc_dma, BT878_ARISC_START); 216 217 /* original int mask had : 218 * 6 2 8 4 0 219 * 1111 1111 1000 0000 0000 220 * SCERR|OCERR|PABORT|RIPERR|FDSR|FTRGT|FBUS|RISCI 221 * Hacked for DST to: 222 * SCERR | OCERR | FDSR | FTRGT | FBUS | RISCI 223 */ 224 int_mask = BT878_ASCERR | BT878_AOCERR | BT878_APABORT | 225 BT878_ARIPERR | BT878_APPERR | BT878_AFDSR | BT878_AFTRGT | 226 BT878_AFBUS | BT878_ARISCI; 227 228 229 /* ignore pesky bits */ 230 int_mask &= ~irq_err_ignore; 231 232 btwrite(int_mask, BT878_AINT_MASK); 233 btwrite(controlreg, BT878_AGPIO_DMA_CTL); 234 } 235 236 void bt878_stop(struct bt878 *bt) 237 { 238 u32 stat; 239 int i = 0; 240 241 dprintk("bt878 debug: bt878_stop\n"); 242 243 btwrite(0, BT878_AINT_MASK); 244 btand(~0x13, BT878_AGPIO_DMA_CTL); 245 246 do { 247 stat = btread(BT878_AINT_STAT); 248 if (!(stat & BT878_ARISC_EN)) 249 break; 250 i++; 251 } while (i < 500); 252 253 dprintk("bt878(%d) debug: bt878_stop, i=%d, stat=0x%8.8x\n", 254 bt->nr, i, stat); 255 } 256 257 EXPORT_SYMBOL(bt878_start); 258 EXPORT_SYMBOL(bt878_stop); 259 260 /*****************************/ 261 /* Interrupt service routine */ 262 /*****************************/ 263 264 static irqreturn_t bt878_irq(int irq, void *dev_id) 265 { 266 u32 stat, astat, mask; 267 int count; 268 struct bt878 *bt; 269 270 bt = (struct bt878 *) dev_id; 271 272 count = 0; 273 while (1) { 274 stat = btread(BT878_AINT_STAT); 275 mask = btread(BT878_AINT_MASK); 276 if (!(astat = (stat & mask))) 277 return IRQ_NONE; /* this interrupt is not for me */ 278 /* dprintk("bt878(%d) debug: irq count %d, stat 0x%8.8x, mask 0x%8.8x\n",bt->nr,count,stat,mask); */ 279 btwrite(astat, BT878_AINT_STAT); /* try to clear interrupt condition */ 280 281 282 if (astat & (BT878_ASCERR | BT878_AOCERR)) { 283 if (bt878_verbose) { 284 printk(KERN_INFO 285 "bt878(%d): irq%s%s risc_pc=%08x\n", 286 bt->nr, 287 (astat & BT878_ASCERR) ? " SCERR" : 288 "", 289 (astat & BT878_AOCERR) ? " OCERR" : 290 "", btread(BT878_ARISC_PC)); 291 } 292 } 293 if (astat & (BT878_APABORT | BT878_ARIPERR | BT878_APPERR)) { 294 if (bt878_verbose) { 295 printk(KERN_INFO 296 "bt878(%d): irq%s%s%s risc_pc=%08x\n", 297 bt->nr, 298 (astat & BT878_APABORT) ? " PABORT" : 299 "", 300 (astat & BT878_ARIPERR) ? " RIPERR" : 301 "", 302 (astat & BT878_APPERR) ? " PPERR" : 303 "", btread(BT878_ARISC_PC)); 304 } 305 } 306 if (astat & (BT878_AFDSR | BT878_AFTRGT | BT878_AFBUS)) { 307 if (bt878_verbose) { 308 printk(KERN_INFO 309 "bt878(%d): irq%s%s%s risc_pc=%08x\n", 310 bt->nr, 311 (astat & BT878_AFDSR) ? " FDSR" : "", 312 (astat & BT878_AFTRGT) ? " FTRGT" : 313 "", 314 (astat & BT878_AFBUS) ? " FBUS" : "", 315 btread(BT878_ARISC_PC)); 316 } 317 } 318 if (astat & BT878_ARISCI) { 319 bt->finished_block = (stat & BT878_ARISCS) >> 28; 320 tasklet_schedule(&bt->tasklet); 321 break; 322 } 323 count++; 324 if (count > 20) { 325 btwrite(0, BT878_AINT_MASK); 326 printk(KERN_ERR 327 "bt878(%d): IRQ lockup, cleared int mask\n", 328 bt->nr); 329 break; 330 } 331 } 332 return IRQ_HANDLED; 333 } 334 335 int 336 bt878_device_control(struct bt878 *bt, unsigned int cmd, union dst_gpio_packet *mp) 337 { 338 int retval; 339 340 retval = 0; 341 if (mutex_lock_interruptible(&bt->gpio_lock)) 342 return -ERESTARTSYS; 343 /* special gpio signal */ 344 switch (cmd) { 345 case DST_IG_ENABLE: 346 // dprintk("dvb_bt8xx: dst enable mask 0x%02x enb 0x%02x \n", mp->dstg.enb.mask, mp->dstg.enb.enable); 347 retval = bttv_gpio_enable(bt->bttv_nr, 348 mp->enb.mask, 349 mp->enb.enable); 350 break; 351 case DST_IG_WRITE: 352 // dprintk("dvb_bt8xx: dst write gpio mask 0x%02x out 0x%02x\n", mp->dstg.outp.mask, mp->dstg.outp.highvals); 353 retval = bttv_write_gpio(bt->bttv_nr, 354 mp->outp.mask, 355 mp->outp.highvals); 356 357 break; 358 case DST_IG_READ: 359 /* read */ 360 retval = bttv_read_gpio(bt->bttv_nr, &mp->rd.value); 361 // dprintk("dvb_bt8xx: dst read gpio 0x%02x\n", (unsigned)mp->dstg.rd.value); 362 break; 363 case DST_IG_TS: 364 /* Set packet size */ 365 bt->TS_Size = mp->psize; 366 break; 367 368 default: 369 retval = -EINVAL; 370 break; 371 } 372 mutex_unlock(&bt->gpio_lock); 373 return retval; 374 } 375 376 EXPORT_SYMBOL(bt878_device_control); 377 378 #define BROOKTREE_878_DEVICE(vend, dev, name) \ 379 { \ 380 .vendor = PCI_VENDOR_ID_BROOKTREE, \ 381 .device = PCI_DEVICE_ID_BROOKTREE_878, \ 382 .subvendor = (vend), .subdevice = (dev), \ 383 .driver_data = (unsigned long) name \ 384 } 385 386 static const struct pci_device_id bt878_pci_tbl[] = { 387 BROOKTREE_878_DEVICE(0x0071, 0x0101, "Nebula Electronics DigiTV"), 388 BROOKTREE_878_DEVICE(0x1461, 0x0761, "AverMedia AverTV DVB-T 761"), 389 BROOKTREE_878_DEVICE(0x11bd, 0x001c, "Pinnacle PCTV Sat"), 390 BROOKTREE_878_DEVICE(0x11bd, 0x0026, "Pinnacle PCTV SAT CI"), 391 BROOKTREE_878_DEVICE(0x1822, 0x0001, "Twinhan VisionPlus DVB"), 392 BROOKTREE_878_DEVICE(0x270f, 0xfc00, 393 "ChainTech digitop DST-1000 DVB-S"), 394 BROOKTREE_878_DEVICE(0x1461, 0x0771, "AVermedia AverTV DVB-T 771"), 395 BROOKTREE_878_DEVICE(0x18ac, 0xdb10, "DViCO FusionHDTV DVB-T Lite"), 396 BROOKTREE_878_DEVICE(0x18ac, 0xdb11, "Ultraview DVB-T Lite"), 397 BROOKTREE_878_DEVICE(0x18ac, 0xd500, "DViCO FusionHDTV 5 Lite"), 398 BROOKTREE_878_DEVICE(0x7063, 0x2000, "pcHDTV HD-2000 TV"), 399 BROOKTREE_878_DEVICE(0x1822, 0x0026, "DNTV Live! Mini"), 400 { } 401 }; 402 403 MODULE_DEVICE_TABLE(pci, bt878_pci_tbl); 404 405 static const char * card_name(const struct pci_device_id *id) 406 { 407 return id->driver_data ? (const char *)id->driver_data : "Unknown"; 408 } 409 410 /***********************/ 411 /* PCI device handling */ 412 /***********************/ 413 414 static int bt878_probe(struct pci_dev *dev, const struct pci_device_id *pci_id) 415 { 416 int result = 0; 417 unsigned char lat; 418 struct bt878 *bt; 419 unsigned int cardid; 420 421 printk(KERN_INFO "bt878: Bt878 AUDIO function found (%d).\n", 422 bt878_num); 423 if (bt878_num >= BT878_MAX) { 424 printk(KERN_ERR "bt878: Too many devices inserted\n"); 425 return -ENOMEM; 426 } 427 if (pci_enable_device(dev)) 428 return -EIO; 429 430 cardid = dev->subsystem_device << 16; 431 cardid |= dev->subsystem_vendor; 432 433 printk(KERN_INFO "%s: card id=[0x%x],[ %s ] has DVB functions.\n", 434 __func__, cardid, card_name(pci_id)); 435 436 bt = &bt878[bt878_num]; 437 bt->dev = dev; 438 bt->nr = bt878_num; 439 bt->shutdown = 0; 440 441 bt->id = dev->device; 442 bt->irq = dev->irq; 443 bt->bt878_adr = pci_resource_start(dev, 0); 444 if (!request_mem_region(pci_resource_start(dev, 0), 445 pci_resource_len(dev, 0), "bt878")) { 446 result = -EBUSY; 447 goto fail0; 448 } 449 450 bt->revision = dev->revision; 451 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat); 452 453 454 printk(KERN_INFO "bt878(%d): Bt%x (rev %d) at %02x:%02x.%x, ", 455 bt878_num, bt->id, bt->revision, dev->bus->number, 456 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn)); 457 printk("irq: %d, latency: %d, memory: 0x%lx\n", 458 bt->irq, lat, bt->bt878_adr); 459 460 #ifdef __sparc__ 461 bt->bt878_mem = (unsigned char *) bt->bt878_adr; 462 #else 463 bt->bt878_mem = ioremap(bt->bt878_adr, 0x1000); 464 #endif 465 466 /* clear interrupt mask */ 467 btwrite(0, BT848_INT_MASK); 468 469 result = request_irq(bt->irq, bt878_irq, 470 IRQF_SHARED, "bt878", (void *) bt); 471 if (result == -EINVAL) { 472 printk(KERN_ERR "bt878(%d): Bad irq number or handler\n", 473 bt878_num); 474 goto fail1; 475 } 476 if (result == -EBUSY) { 477 printk(KERN_ERR 478 "bt878(%d): IRQ %d busy, change your PnP config in BIOS\n", 479 bt878_num, bt->irq); 480 goto fail1; 481 } 482 if (result < 0) 483 goto fail1; 484 485 pci_set_master(dev); 486 pci_set_drvdata(dev, bt); 487 488 if ((result = bt878_mem_alloc(bt))) { 489 printk(KERN_ERR "bt878: failed to allocate memory!\n"); 490 goto fail2; 491 } 492 493 bt878_make_risc(bt); 494 btwrite(0, BT878_AINT_MASK); 495 bt878_num++; 496 497 return 0; 498 499 fail2: 500 free_irq(bt->irq, bt); 501 fail1: 502 release_mem_region(pci_resource_start(bt->dev, 0), 503 pci_resource_len(bt->dev, 0)); 504 fail0: 505 pci_disable_device(dev); 506 return result; 507 } 508 509 static void bt878_remove(struct pci_dev *pci_dev) 510 { 511 u8 command; 512 struct bt878 *bt = pci_get_drvdata(pci_dev); 513 514 if (bt878_verbose) 515 printk(KERN_INFO "bt878(%d): unloading\n", bt->nr); 516 517 /* turn off all capturing, DMA and IRQs */ 518 btand(~0x13, BT878_AGPIO_DMA_CTL); 519 520 /* first disable interrupts before unmapping the memory! */ 521 btwrite(0, BT878_AINT_MASK); 522 btwrite(~0U, BT878_AINT_STAT); 523 524 /* disable PCI bus-mastering */ 525 pci_read_config_byte(bt->dev, PCI_COMMAND, &command); 526 /* Should this be &=~ ?? */ 527 command &= ~PCI_COMMAND_MASTER; 528 pci_write_config_byte(bt->dev, PCI_COMMAND, command); 529 530 free_irq(bt->irq, bt); 531 printk(KERN_DEBUG "bt878_mem: 0x%p.\n", bt->bt878_mem); 532 if (bt->bt878_mem) 533 iounmap(bt->bt878_mem); 534 535 release_mem_region(pci_resource_start(bt->dev, 0), 536 pci_resource_len(bt->dev, 0)); 537 /* wake up any waiting processes 538 because shutdown flag is set, no new processes (in this queue) 539 are expected 540 */ 541 bt->shutdown = 1; 542 bt878_mem_free(bt); 543 544 pci_disable_device(pci_dev); 545 return; 546 } 547 548 static struct pci_driver bt878_pci_driver = { 549 .name = "bt878", 550 .id_table = bt878_pci_tbl, 551 .probe = bt878_probe, 552 .remove = bt878_remove, 553 }; 554 555 /*******************************/ 556 /* Module management functions */ 557 /*******************************/ 558 559 static int __init bt878_init_module(void) 560 { 561 bt878_num = 0; 562 563 printk(KERN_INFO "bt878: AUDIO driver version %d.%d.%d loaded\n", 564 (BT878_VERSION_CODE >> 16) & 0xff, 565 (BT878_VERSION_CODE >> 8) & 0xff, 566 BT878_VERSION_CODE & 0xff); 567 568 return pci_register_driver(&bt878_pci_driver); 569 } 570 571 static void __exit bt878_cleanup_module(void) 572 { 573 pci_unregister_driver(&bt878_pci_driver); 574 } 575 576 module_init(bt878_init_module); 577 module_exit(bt878_cleanup_module); 578 579 MODULE_LICENSE("GPL"); 580