]> Pileus Git - ~andy/linux/blob - drivers/media/video/saa7164/saa7164-core.c
V4L/DVB (12923): SAA7164: Add support for the NXP SAA7164 silicon
[~andy/linux] / drivers / media / video / saa7164 / saa7164-core.c
1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kmod.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <asm/div64.h>
32
33 #include "saa7164.h"
34
35 MODULE_DESCRIPTION("Driver for NXP SAA7164 based TV cards");
36 MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
37 MODULE_LICENSE("GPL");
38
39 /*
40   1 Basic
41   2
42   4 i2c
43   8 api
44  16 cmd
45  32 bus
46  */
47
48 unsigned int debug;
49 module_param(debug, int, 0644);
50 MODULE_PARM_DESC(debug, "enable debug messages");
51
52 static unsigned int card[]  = {[0 ... (SAA7164_MAXBOARDS - 1)] = UNSET };
53 module_param_array(card,  int, NULL, 0444);
54 MODULE_PARM_DESC(card, "card type");
55
56 static unsigned int saa7164_devcount;
57
58 static DEFINE_MUTEX(devlist);
59 LIST_HEAD(saa7164_devlist);
60
61 #define INT_SIZE 16
62
63 static void saa7164_work_cmdhandler(struct work_struct *w)
64 {
65         struct saa7164_dev *dev = container_of(w, struct saa7164_dev, workcmd);
66
67         /* Wake up any complete commands */
68         saa7164_cmd_signal(dev, 0);
69 }
70
71 static void saa7164_buffer_deliver(struct saa7164_buffer *buf)
72 {
73         struct saa7164_tsport *port = buf->port;
74
75         /* Feed the transport payload into the kernel demux */
76         dvb_dmx_swfilter_packets(&port->dvb.demux, buf->cpu,
77                 SAA7164_TS_NUMBER_OF_LINES);
78
79 }
80
81 static irqreturn_t saa7164_irq_ts(struct saa7164_tsport *port)
82 {
83         struct saa7164_dev *dev = port->dev;
84         struct saa7164_buffer *buf;
85         struct list_head *c, *n;
86         int wp, i = 0, rp;
87
88         /* Find the current write point from the hardware */
89         wp = saa7164_readl(port->bufcounter);
90         if (wp > (port->hwcfg.buffercount - 1))
91                 BUG();
92
93         /* Find the previous buffer to the current write point */
94         if (wp == 0)
95                 rp = 7;
96         else
97                 rp = wp - 1;
98
99         /* Lookup the WP in the buffer list */
100         /* TODO: turn this into a worker thread */
101         list_for_each_safe(c, n, &port->dmaqueue.list) {
102                 buf = list_entry(c, struct saa7164_buffer, list);
103                 if (i++ > port->hwcfg.buffercount)
104                         BUG();
105
106                 if (buf->nr == rp) {
107                         /* Found the buffer, deal with it */
108                         dprintk(DBGLVL_IRQ, "%s() wp: %d processing: %d\n",
109                                 __func__, wp, rp);
110                         saa7164_buffer_deliver(buf);
111                         break;
112                 }
113
114         }
115         return 0;
116 }
117
118 /* Primary IRQ handler and dispatch mechanism */
119 static irqreturn_t saa7164_irq(int irq, void *dev_id)
120 {
121         struct saa7164_dev *dev = dev_id;
122         u32 hwacc = 0, interruptid;
123         u32 intstat[INT_SIZE/4];
124         int i, handled = 0, bit;
125
126         /* Check that the hardware is accessable. If the status bytes are
127          * 0xFF then the device is not accessable, the the IRQ belongs
128          * to another driver.
129          */
130         for (i = 0; i < INT_SIZE/4; i++) {
131
132                 /* TODO: Convert into saa7164_readl() */
133                 /* Read the 4 hardware interrupt registers */
134                 intstat[i] = *(dev->InterruptStatus + i);
135
136                 if (intstat[i] != 0xffffffff)
137                         hwacc = 1;
138         }
139         if (hwacc == 0) {
140                 handled = 0;
141                 goto out;
142         }
143
144         handled = 1;
145
146         /* For each of the HW interrupt registers */
147         for (i = 0; i < INT_SIZE/4; i++) {
148
149                 if (intstat[i]) {
150                         /* Each function of the board has it's own interruptid.
151                          * Find the function that triggered then call
152                          * it's handler.
153                          */
154                         for (bit = 0; bit < 32; bit++) {
155
156                                 if (((intstat[i] >> bit) & 0x00000001) == 0)
157                                         continue;
158
159                                 /* Calculate the interrupt id (0x00 to 0x7f) */
160
161                                 interruptid = (i * 32) + bit;
162                                 if (interruptid == dev->intfdesc.bInterruptId) {
163                                         /* A response to an cmd/api call */
164                                         schedule_work(&dev->workcmd);
165                                 } else if (interruptid ==
166                                         dev->ts1.hwcfg.interruptid) {
167
168                                         /* Transport path 1 */
169                                         saa7164_irq_ts(&dev->ts1);
170
171                                 } else if (interruptid ==
172                                         dev->ts2.hwcfg.interruptid) {
173
174                                         /* Transport path 2 */
175                                         saa7164_irq_ts(&dev->ts2);
176
177                                 } else {
178                                         /* Find the function */
179                                         dprintk(DBGLVL_IRQ,
180                                                 "%s() unhandled interrupt "
181                                                 "reg 0x%x bit 0x%x "
182                                                 "intid = 0x%x\n",
183                                                 __func__, i, bit, interruptid);
184                                 }
185                         }
186
187                         /* TODO: Convert into saa7164_writel() */
188                         /* Ack it */
189                         *(dev->InterruptAck + i) = intstat[i];
190
191                 }
192         }
193 out:
194         return IRQ_RETVAL(handled);
195 }
196
197 void saa7164_getfirmwarestatus(struct saa7164_dev *dev)
198 {
199         struct saa7164_fw_status *s = &dev->fw_status;
200
201         dev->fw_status.status = saa7164_readl(SAA_DEVICE_SYSINIT_STATUS);
202         dev->fw_status.mode = saa7164_readl(SAA_DEVICE_SYSINIT_MODE);
203         dev->fw_status.spec = saa7164_readl(SAA_DEVICE_SYSINIT_SPEC);
204         dev->fw_status.inst = saa7164_readl(SAA_DEVICE_SYSINIT_INST);
205         dev->fw_status.cpuload = saa7164_readl(SAA_DEVICE_SYSINIT_CPULOAD);
206         dev->fw_status.remainheap =
207                 saa7164_readl(SAA_DEVICE_SYSINIT_REMAINHEAP);
208
209         dprintk(1, "Firmware status:\n");
210         dprintk(1, " .status     = 0x%08x\n", s->status);
211         dprintk(1, " .mode       = 0x%08x\n", s->mode);
212         dprintk(1, " .spec       = 0x%08x\n", s->spec);
213         dprintk(1, " .inst       = 0x%08x\n", s->inst);
214         dprintk(1, " .cpuload    = 0x%08x\n", s->cpuload);
215         dprintk(1, " .remainheap = 0x%08x\n", s->remainheap);
216 }
217
218 u32 saa7164_getcurrentfirmwareversion(struct saa7164_dev *dev)
219 {
220         u32 reg;
221
222         reg = saa7164_readl(SAA_DEVICE_VERSION);
223         dprintk(1, "Device running firmware version %d.%d.%d.%d (0x%x)\n",
224                 (reg & 0x0000fc00) >> 10,
225                 (reg & 0x000003e0) >> 5,
226                 (reg & 0x0000001f),
227                 (reg & 0xffff0000) >> 16,
228                 reg);
229
230         return reg;
231 }
232
233 /* TODO: Debugging func, remove */
234 void saa7164_dumphex16(struct saa7164_dev *dev, u8 *buf, int len)
235 {
236         int i;
237
238         printk(KERN_INFO "--------------------> "
239                 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
240
241         for (i = 0; i < len; i += 16)
242                 printk(KERN_INFO "         [0x%08x] "
243                         "%02x %02x %02x %02x %02x %02x %02x %02x "
244                         "%02x %02x %02x %02x %02x %02x %02x %02x\n", i,
245                 *(buf+i+0), *(buf+i+1), *(buf+i+2), *(buf+i+3),
246                 *(buf+i+4), *(buf+i+5), *(buf+i+6), *(buf+i+7),
247                 *(buf+i+8), *(buf+i+9), *(buf+i+10), *(buf+i+11),
248                 *(buf+i+12), *(buf+i+13), *(buf+i+14), *(buf+i+15));
249 }
250
251 /* TODO: Debugging func, remove */
252 void saa7164_dumpregs(struct saa7164_dev *dev, u32 addr)
253 {
254         int i;
255
256         dprintk(1, "--------------------> "
257                 "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
258
259         for (i = 0; i < 0x100; i += 16)
260                 dprintk(1, "region0[0x%08x] = "
261                         "%02x %02x %02x %02x %02x %02x %02x %02x"
262                         " %02x %02x %02x %02x %02x %02x %02x %02x\n", i,
263                         (u8)saa7164_readb(addr + i + 0),
264                         (u8)saa7164_readb(addr + i + 1),
265                         (u8)saa7164_readb(addr + i + 2),
266                         (u8)saa7164_readb(addr + i + 3),
267                         (u8)saa7164_readb(addr + i + 4),
268                         (u8)saa7164_readb(addr + i + 5),
269                         (u8)saa7164_readb(addr + i + 6),
270                         (u8)saa7164_readb(addr + i + 7),
271                         (u8)saa7164_readb(addr + i + 8),
272                         (u8)saa7164_readb(addr + i + 9),
273                         (u8)saa7164_readb(addr + i + 10),
274                         (u8)saa7164_readb(addr + i + 11),
275                         (u8)saa7164_readb(addr + i + 12),
276                         (u8)saa7164_readb(addr + i + 13),
277                         (u8)saa7164_readb(addr + i + 14),
278                         (u8)saa7164_readb(addr + i + 15)
279                         );
280 }
281
282 static void saa7164_dump_hwdesc(struct saa7164_dev *dev)
283 {
284         dprintk(1, "@0x%p hwdesc sizeof(tmComResHWDescr_t) = %lu bytes\n",
285                 &dev->hwdesc, sizeof(tmComResHWDescr_t));
286
287         dprintk(1, " .bLength = 0x%x\n", dev->hwdesc.bLength);
288         dprintk(1, " .bDescriptorType = 0x%x\n", dev->hwdesc.bDescriptorType);
289         dprintk(1, " .bDescriptorSubtype = 0x%x\n",
290                 dev->hwdesc.bDescriptorSubtype);
291
292         dprintk(1, " .bcdSpecVersion = 0x%x\n", dev->hwdesc.bcdSpecVersion);
293         dprintk(1, " .dwClockFrequency = 0x%x\n", dev->hwdesc.dwClockFrequency);
294         dprintk(1, " .dwClockUpdateRes = 0x%x\n", dev->hwdesc.dwClockUpdateRes);
295         dprintk(1, " .bCapabilities = 0x%x\n", dev->hwdesc.bCapabilities);
296         dprintk(1, " .dwDeviceRegistersLocation = 0x%x\n",
297                 dev->hwdesc.dwDeviceRegistersLocation);
298
299         dprintk(1, " .dwHostMemoryRegion = 0x%x\n",
300                 dev->hwdesc.dwHostMemoryRegion);
301
302         dprintk(1, " .dwHostMemoryRegionSize = 0x%x\n",
303                 dev->hwdesc.dwHostMemoryRegionSize);
304
305         dprintk(1, " .dwHostHibernatMemRegion = 0x%x\n",
306                 dev->hwdesc.dwHostHibernatMemRegion);
307
308         dprintk(1, " .dwHostHibernatMemRegionSize = 0x%x\n",
309                 dev->hwdesc.dwHostHibernatMemRegionSize);
310 }
311
312 static void saa7164_dump_intfdesc(struct saa7164_dev *dev)
313 {
314         dprintk(1, "@0x%p intfdesc "
315                 "sizeof(tmComResInterfaceDescr_t) = %lu bytes\n",
316                 &dev->intfdesc, sizeof(tmComResInterfaceDescr_t));
317
318         dprintk(1, " .bLength = 0x%x\n", dev->intfdesc.bLength);
319         dprintk(1, " .bDescriptorType = 0x%x\n", dev->intfdesc.bDescriptorType);
320         dprintk(1, " .bDescriptorSubtype = 0x%x\n",
321                 dev->intfdesc.bDescriptorSubtype);
322
323         dprintk(1, " .bFlags = 0x%x\n", dev->intfdesc.bFlags);
324         dprintk(1, " .bInterfaceType = 0x%x\n", dev->intfdesc.bInterfaceType);
325         dprintk(1, " .bInterfaceId = 0x%x\n", dev->intfdesc.bInterfaceId);
326         dprintk(1, " .bBaseInterface = 0x%x\n", dev->intfdesc.bBaseInterface);
327         dprintk(1, " .bInterruptId = 0x%x\n", dev->intfdesc.bInterruptId);
328         dprintk(1, " .bDebugInterruptId = 0x%x\n",
329                 dev->intfdesc.bDebugInterruptId);
330
331         dprintk(1, " .BARLocation = 0x%x\n", dev->intfdesc.BARLocation);
332 }
333
334 static void saa7164_dump_busdesc(struct saa7164_dev *dev)
335 {
336         dprintk(1, "@0x%p busdesc sizeof(tmComResBusDescr_t) = %lu bytes\n",
337                 &dev->busdesc, sizeof(tmComResBusDescr_t));
338
339         dprintk(1, " .CommandRing   = 0x%016Lx\n", dev->busdesc.CommandRing);
340         dprintk(1, " .ResponseRing  = 0x%016Lx\n", dev->busdesc.ResponseRing);
341         dprintk(1, " .CommandWrite  = 0x%x\n", dev->busdesc.CommandWrite);
342         dprintk(1, " .CommandRead   = 0x%x\n", dev->busdesc.CommandRead);
343         dprintk(1, " .ResponseWrite = 0x%x\n", dev->busdesc.ResponseWrite);
344         dprintk(1, " .ResponseRead  = 0x%x\n", dev->busdesc.ResponseRead);
345 }
346
347 /* Much of the hardware configuration and PCI registers are configured
348  * dynamically depending on firmware. We have to cache some initial
349  * structures then use these to locate other important structures
350  * from PCI space.
351  */
352 static void saa7164_get_descriptors(struct saa7164_dev *dev)
353 {
354         memcpy(&dev->hwdesc, dev->bmmio, sizeof(tmComResHWDescr_t));
355         memcpy(&dev->intfdesc, dev->bmmio + sizeof(tmComResHWDescr_t),
356                 sizeof(tmComResInterfaceDescr_t));
357         memcpy(&dev->busdesc, dev->bmmio + dev->intfdesc.BARLocation,
358                 sizeof(tmComResBusDescr_t));
359
360         if (dev->hwdesc.bLength != sizeof(tmComResHWDescr_t)) {
361                 printk(KERN_ERR "Structure tmComResHWDescr_t is mangled\n");
362                 printk(KERN_ERR "Need %x got %lu\n", dev->hwdesc.bLength,
363                         sizeof(tmComResHWDescr_t));
364         } else
365                 saa7164_dump_hwdesc(dev);
366
367         if (dev->intfdesc.bLength != sizeof(tmComResInterfaceDescr_t)) {
368                 printk(KERN_ERR "struct tmComResInterfaceDescr_t is mangled\n");
369                 printk(KERN_ERR "Need %x got %lu\n", dev->intfdesc.bLength,
370                         sizeof(tmComResInterfaceDescr_t));
371         } else
372                 saa7164_dump_intfdesc(dev);
373
374         saa7164_dump_busdesc(dev);
375 }
376
377 static int saa7164_pci_quirks(struct saa7164_dev *dev)
378 {
379         return 0;
380 }
381
382 static int get_resources(struct saa7164_dev *dev)
383 {
384         if (request_mem_region(pci_resource_start(dev->pci, 0),
385                 pci_resource_len(dev->pci, 0), dev->name)) {
386
387                 if (request_mem_region(pci_resource_start(dev->pci, 2),
388                         pci_resource_len(dev->pci, 2), dev->name))
389                         return 0;
390         }
391
392         printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx or 0x%llx\n",
393                 dev->name,
394                 (u64)pci_resource_start(dev->pci, 0),
395                 (u64)pci_resource_start(dev->pci, 2));
396
397         return -EBUSY;
398 }
399
400 static int saa7164_dev_setup(struct saa7164_dev *dev)
401 {
402         int i;
403
404         mutex_init(&dev->lock);
405         atomic_inc(&dev->refcount);
406         dev->nr = saa7164_devcount++;
407
408         sprintf(dev->name, "saa7164[%d]", dev->nr);
409
410         mutex_lock(&devlist);
411         list_add_tail(&dev->devlist, &saa7164_devlist);
412         mutex_unlock(&devlist);
413
414         /* board config */
415         dev->board = UNSET;
416         if (card[dev->nr] < saa7164_bcount)
417                 dev->board = card[dev->nr];
418
419         for (i = 0; UNSET == dev->board  &&  i < saa7164_idcount; i++)
420                 if (dev->pci->subsystem_vendor == saa7164_subids[i].subvendor &&
421                         dev->pci->subsystem_device ==
422                                 saa7164_subids[i].subdevice)
423                                 dev->board = saa7164_subids[i].card;
424
425         if (UNSET == dev->board) {
426                 dev->board = SAA7164_BOARD_UNKNOWN;
427                 saa7164_card_list(dev);
428         }
429
430         dev->pci_bus  = dev->pci->bus->number;
431         dev->pci_slot = PCI_SLOT(dev->pci->devfn);
432
433         /* I2C Defaults / setup */
434         dev->i2c_bus[0].dev = dev;
435         dev->i2c_bus[0].nr = 0;
436         dev->i2c_bus[1].dev = dev;
437         dev->i2c_bus[1].nr = 1;
438         dev->i2c_bus[2].dev = dev;
439         dev->i2c_bus[2].nr = 2;
440
441         /* Transport port A Defaults / setup */
442         dev->ts1.dev = dev;
443         dev->ts1.nr = 0;
444         mutex_init(&dev->ts1.dvb.lock);
445         INIT_LIST_HEAD(&dev->ts1.dmaqueue.list);
446         INIT_LIST_HEAD(&dev->ts1.dummy_dmaqueue.list);
447         mutex_init(&dev->ts1.dmaqueue_lock);
448         mutex_init(&dev->ts1.dummy_dmaqueue_lock);
449
450         /* Transport port B Defaults / setup */
451         dev->ts2.dev = dev;
452         dev->ts2.nr = 1;
453         mutex_init(&dev->ts2.dvb.lock);
454         INIT_LIST_HEAD(&dev->ts2.dmaqueue.list);
455         INIT_LIST_HEAD(&dev->ts2.dummy_dmaqueue.list);
456         mutex_init(&dev->ts2.dmaqueue_lock);
457         mutex_init(&dev->ts2.dummy_dmaqueue_lock);
458
459         if (get_resources(dev) < 0) {
460                 printk(KERN_ERR "CORE %s No more PCIe resources for "
461                        "subsystem: %04x:%04x\n",
462                        dev->name, dev->pci->subsystem_vendor,
463                        dev->pci->subsystem_device);
464
465                 saa7164_devcount--;
466                 return -ENODEV;
467         }
468
469         /* PCI/e allocations */
470         dev->lmmio = ioremap(pci_resource_start(dev->pci, 0),
471                              pci_resource_len(dev->pci, 0));
472
473         dev->lmmio2 = ioremap(pci_resource_start(dev->pci, 2),
474                              pci_resource_len(dev->pci, 2));
475
476         printk(KERN_INFO "CORE %s: dev->lmmio  = 0x%p\n", dev->name,
477                 dev->lmmio);
478
479         printk(KERN_INFO "CORE %s: dev->lmmio2 = 0x%p\n", dev->name,
480                 dev->lmmio2);
481
482         dev->bmmio = (u8 __iomem *)dev->lmmio;
483         dev->bmmio2 = (u8 __iomem *)dev->lmmio2;
484         printk(KERN_INFO "CORE %s: dev->bmmio  = 0x%p\n", dev->name,
485                 dev->bmmio);
486
487         printk(KERN_INFO "CORE %s: dev->bmmio2 = 0x%p\n", dev->name,
488                 dev->bmmio2);
489
490         /* TODO: Magic defines used in the windows driver, define these */
491         dev->InterruptStatus = (u32 *)(dev->bmmio + 0x183000 + 0xf80);
492         dev->InterruptAck = (u32 *)(dev->bmmio + 0x183000 + 0xf90);
493
494         printk(KERN_INFO
495                 "CORE %s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
496                dev->name, dev->pci->subsystem_vendor,
497                dev->pci->subsystem_device, saa7164_boards[dev->board].name,
498                dev->board, card[dev->nr] == dev->board ?
499                "insmod option" : "autodetected");
500
501         saa7164_pci_quirks(dev);
502
503         return 0;
504 }
505
506 static void saa7164_dev_unregister(struct saa7164_dev *dev)
507 {
508         dprintk(1, "%s()\n", __func__);
509
510         release_mem_region(pci_resource_start(dev->pci, 0),
511                 pci_resource_len(dev->pci, 0));
512
513         release_mem_region(pci_resource_start(dev->pci, 2),
514                 pci_resource_len(dev->pci, 2));
515
516         if (!atomic_dec_and_test(&dev->refcount))
517                 return;
518
519         iounmap(dev->lmmio);
520         iounmap(dev->lmmio2);
521
522         return;
523 }
524
525 static int __devinit saa7164_initdev(struct pci_dev *pci_dev,
526                                      const struct pci_device_id *pci_id)
527 {
528         struct saa7164_dev *dev;
529         int err, i;
530         u32 version;
531
532         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
533         if (NULL == dev)
534                 return -ENOMEM;
535
536         /* pci init */
537         dev->pci = pci_dev;
538         if (pci_enable_device(pci_dev)) {
539                 err = -EIO;
540                 goto fail_free;
541         }
542
543         if (saa7164_dev_setup(dev) < 0) {
544                 err = -EINVAL;
545                 goto fail_free;
546         }
547
548         /* print pci info */
549         pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
550         pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
551         printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
552                "latency: %d, mmio: 0x%llx\n", dev->name,
553                pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
554                dev->pci_lat,
555                 (unsigned long long)pci_resource_start(pci_dev, 0));
556
557         pci_set_master(pci_dev);
558         /* TODO */
559         if (!pci_dma_supported(pci_dev, 0xffffffff)) {
560                 printk("%s/0: Oops: no 32bit PCI DMA ???\n", dev->name);
561                 err = -EIO;
562                 goto fail_irq;
563         }
564
565         err = request_irq(pci_dev->irq, saa7164_irq,
566                 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
567         if (err < 0) {
568                 printk(KERN_ERR "%s: can't get IRQ %d\n", dev->name,
569                         pci_dev->irq);
570                 err = -EIO;
571                 goto fail_irq;
572         }
573
574         pci_set_drvdata(pci_dev, dev);
575
576         saa7164_pci_quirks(dev);
577
578         /* Init the internal command list */
579         for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
580                 dev->cmds[i].seqno = i;
581                 dev->cmds[i].inuse = 0;
582                 mutex_init(&dev->cmds[i].lock);
583                 init_waitqueue_head(&dev->cmds[i].wait);
584         }
585
586         /* We need a deferred interrupt handler for cmd handling */
587         INIT_WORK(&dev->workcmd, saa7164_work_cmdhandler);
588
589         /* Only load the firmware if we know the board */
590         if (dev->board != SAA7164_BOARD_UNKNOWN) {
591
592                 err = saa7164_downloadfirmware(dev);
593                 if (err < 0) {
594                         printk(KERN_ERR
595                                 "Failed to boot firmware, cannot continue\n");
596                         goto fail_irq;
597                 }
598
599                 saa7164_get_descriptors(dev);
600                 saa7164_dumpregs(dev, 0);
601                 saa7164_getcurrentfirmwareversion(dev);
602                 saa7164_getfirmwarestatus(dev);
603                 err = saa7164_bus_setup(dev);
604                 if (err < 0)
605                         printk(KERN_ERR
606                                 "Failed to setup the bus, will continue\n");
607                 saa7164_bus_dump(dev);
608
609                 /* Ping the running firmware via the command bus and get the
610                  * firmware version, this checks the bus is running OK.
611                  */
612                 version = 0;
613                 if (saa7164_api_get_fw_version(dev, &version) == SAA_OK)
614                         dprintk(1, "Bus is operating correctly using "
615                                 "version %d.%d.%d.%d (0x%x)\n",
616                                 (version & 0x0000fc00) >> 10,
617                                 (version & 0x000003e0) >> 5,
618                                 (version & 0x0000001f),
619                                 (version & 0xffff0000) >> 16,
620                                 version);
621                 else
622                         printk(KERN_ERR
623                                 "Failed to communicate with the firmware\n");
624
625                 /* Bring up the I2C buses */
626                 saa7164_i2c_register(&dev->i2c_bus[0]);
627                 saa7164_i2c_register(&dev->i2c_bus[1]);
628                 saa7164_i2c_register(&dev->i2c_bus[2]);
629                 saa7164_gpio_setup(dev);
630                 saa7164_card_setup(dev);
631
632
633                 /* Parse the dynamic device configuration, find various
634                  * media endpoints (MPEG, WMV, PS, TS) and cache their
635                  * configuration details into the driver, so we can
636                  * reference them later during simething_register() func,
637                  * interrupt handlers, deferred work handlers etc.
638                  */
639                 saa7164_api_enum_subdevs(dev);
640
641                 /* Try a few API commands - just for exercise purposes */
642                 saa7164_api_test(dev);
643
644                 /* Begin to create the video sub-systems and register funcs */
645                 if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB) {
646                         if (saa7164_dvb_register(&dev->ts1) < 0) {
647                                 printk(KERN_ERR "%s() Failed to register "
648                                         "dvb adapters on porta\n",
649                                         __func__);
650                         }
651                 }
652
653                 if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB) {
654                         if (saa7164_dvb_register(&dev->ts2) < 0) {
655                                 printk(KERN_ERR"%s() Failed to register "
656                                         "dvb adapters on portb\n",
657                                         __func__);
658                         }
659                 }
660
661         } /* != BOARD_UNKNOWN */
662         else
663                 printk(KERN_ERR "%s() Unsupported board detected, "
664                         "registering without firmware\n", __func__);
665
666         return 0;
667
668 fail_irq:
669         saa7164_dev_unregister(dev);
670 fail_free:
671         kfree(dev);
672         return err;
673 }
674
675 static void saa7164_shutdown(struct saa7164_dev *dev)
676 {
677         dprintk(1, "%s()\n", __func__);
678 }
679
680 static void __devexit saa7164_finidev(struct pci_dev *pci_dev)
681 {
682         struct saa7164_dev *dev = pci_get_drvdata(pci_dev);
683
684         saa7164_shutdown(dev);
685
686         if (saa7164_boards[dev->board].porta == SAA7164_MPEG_DVB)
687                 saa7164_dvb_unregister(&dev->ts1);
688
689         if (saa7164_boards[dev->board].portb == SAA7164_MPEG_DVB)
690                 saa7164_dvb_unregister(&dev->ts2);
691
692         saa7164_i2c_unregister(&dev->i2c_bus[0]);
693         saa7164_i2c_unregister(&dev->i2c_bus[1]);
694         saa7164_i2c_unregister(&dev->i2c_bus[2]);
695
696         pci_disable_device(pci_dev);
697
698         /* unregister stuff */
699         free_irq(pci_dev->irq, dev);
700         pci_set_drvdata(pci_dev, NULL);
701
702         mutex_lock(&devlist);
703         list_del(&dev->devlist);
704         mutex_unlock(&devlist);
705
706         saa7164_dev_unregister(dev);
707         kfree(dev);
708 }
709
710 static struct pci_device_id saa7164_pci_tbl[] = {
711         {
712                 /* SAA7164 */
713                 .vendor       = 0x1131,
714                 .device       = 0x7164,
715                 .subvendor    = PCI_ANY_ID,
716                 .subdevice    = PCI_ANY_ID,
717         }, {
718                 /* --- end of list --- */
719         }
720 };
721 MODULE_DEVICE_TABLE(pci, saa7164_pci_tbl);
722
723 static struct pci_driver saa7164_pci_driver = {
724         .name     = "saa7164",
725         .id_table = saa7164_pci_tbl,
726         .probe    = saa7164_initdev,
727         .remove   = __devexit_p(saa7164_finidev),
728         /* TODO */
729         .suspend  = NULL,
730         .resume   = NULL,
731 };
732
733 static int saa7164_init(void)
734 {
735         printk(KERN_INFO "saa7164 driver loaded\n");
736         return pci_register_driver(&saa7164_pci_driver);
737 }
738
739 static void saa7164_fini(void)
740 {
741         pci_unregister_driver(&saa7164_pci_driver);
742 }
743
744 module_init(saa7164_init);
745 module_exit(saa7164_fini);
746