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