V4L/DVB (7043): New card supported(partially): Pinnacle 800i
[safe/jmp/linux-2.6] / drivers / media / video / cx88 / cx88-mpeg.c
index 35e6d0c..a21b503 100644 (file)
@@ -23,9 +23,9 @@
  */
 
 #include <linux/module.h>
-#include <linux/moduleparam.h>
 #include <linux/init.h>
 #include <linux/device.h>
+#include <linux/dma-mapping.h>
 #include <linux/interrupt.h>
 #include <asm/delay.h>
 
@@ -44,8 +44,33 @@ module_param(debug,int,0644);
 MODULE_PARM_DESC(debug,"enable debug messages [mpeg]");
 
 #define dprintk(level,fmt, arg...)     if (debug >= level) \
-       printk(KERN_DEBUG "%s/2: " fmt, dev->core->name , ## arg)
+       printk(KERN_DEBUG "%s/2-mpeg: " fmt, dev->core->name, ## arg)
 
+#define mpeg_dbg(level,fmt, arg...)    if (debug >= level) \
+       printk(KERN_DEBUG "%s/2-mpeg: " fmt, core->name, ## arg)
+
+#if defined(CONFIG_MODULES) && defined(MODULE)
+static void request_module_async(struct work_struct *work)
+{
+       struct cx8802_dev *dev=container_of(work, struct cx8802_dev, request_module_wk);
+
+       if (dev->core->board.mpeg & CX88_MPEG_DVB)
+               request_module("cx88-dvb");
+       if (dev->core->board.mpeg & CX88_MPEG_BLACKBIRD)
+               request_module("cx88-blackbird");
+}
+
+static void request_modules(struct cx8802_dev *dev)
+{
+       INIT_WORK(&dev->request_module_wk, request_module_async);
+       schedule_work(&dev->request_module_wk);
+}
+#else
+#define request_modules(dev)
+#endif /* CONFIG_MODULES */
+
+
+static LIST_HEAD(cx8802_devlist);
 /* ------------------------------------------------------------------ */
 
 static int cx8802_start_dma(struct cx8802_dev    *dev,
@@ -54,7 +79,8 @@ static int cx8802_start_dma(struct cx8802_dev    *dev,
 {
        struct cx88_core *core = dev->core;
 
-       dprintk(0, "cx8802_start_dma w: %d, h: %d, f: %d\n", dev->width, dev->height, buf->vb.field);
+       dprintk(1, "cx8802_start_dma w: %d, h: %d, f: %d\n",
+               buf->vb.width, buf->vb.height, buf->vb.field);
 
        /* setup fifo + format */
        cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
@@ -66,27 +92,45 @@ static int cx8802_start_dma(struct cx8802_dev    *dev,
        /* FIXME: this needs a review.
         * also: move to cx88-blackbird + cx88-dvb source files? */
 
-       if (cx88_boards[core->board].dvb) {
+       dprintk( 1, "core->active_type_id = 0x%08x\n", core->active_type_id);
+
+       if ( (core->active_type_id == CX88_MPEG_DVB) &&
+               (core->board.mpeg & CX88_MPEG_DVB) ) {
+
+               dprintk( 1, "cx8802_start_dma doing .dvb\n");
                /* negedge driven & software reset */
                cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
                udelay(100);
                cx_write(MO_PINMUX_IO, 0x00);
-               cx_write(TS_HW_SOP_CNTRL,0x47<<16|188<<4|0x01);
-               switch (core->board) {
+               cx_write(TS_HW_SOP_CNTRL, 0x47<<16|188<<4|0x01);
+               switch (core->boardnr) {
                case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
                case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
                case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
+               case CX88_BOARD_PCHDTV_HD5500:
                        cx_write(TS_SOP_STAT, 1<<13);
                        break;
+               case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
+               case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
+                       cx_write(MO_PINMUX_IO, 0x88); /* Enable MPEG parallel IO and video signal pins */
+                       udelay(100);
+                       break;
+               case CX88_BOARD_HAUPPAUGE_HVR1300:
+                       break;
+               case CX88_BOARD_PINNACLE_PCTV_HD_800i:
+                       /* Enable MPEG parallel port */
+                       cx_write(MO_PINMUX_IO, 0x80);
+                       udelay(100);
+                       break;
                default:
                        cx_write(TS_SOP_STAT, 0x00);
                        break;
                }
                cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
                udelay(100);
-       }
-
-       if (cx88_boards[core->board].blackbird) {
+       } else if ( (core->active_type_id == CX88_MPEG_BLACKBIRD) &&
+               (core->board.mpeg & CX88_MPEG_BLACKBIRD) ) {
+               dprintk( 1, "cx8802_start_dma doing .blackbird\n");
                cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
 
                cx_write(TS_GEN_CNTRL, 0x46); /* punctured clock TS & posedge driven & software reset */
@@ -97,6 +141,10 @@ static int cx8802_start_dma(struct cx8802_dev    *dev,
 
                cx_write(TS_GEN_CNTRL, 0x06); /* punctured clock TS & posedge driven */
                udelay(100);
+       } else {
+               printk( "%s() Failed. Unsupported value in .mpeg (0x%08x)\n", __FUNCTION__,
+                       core->board.mpeg );
+               return -EINVAL;
        }
 
        /* reset counter */
@@ -104,8 +152,8 @@ static int cx8802_start_dma(struct cx8802_dev    *dev,
        q->count = 1;
 
        /* enable irqs */
-       dprintk( 0, "setting the interrupt mask\n" );
-       cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x04);
+       dprintk( 1, "setting the interrupt mask\n" );
+       cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_TSINT);
        cx_set(MO_TS_INTMSK,  0x1f0011);
 
        /* start dma */
@@ -117,13 +165,13 @@ static int cx8802_start_dma(struct cx8802_dev    *dev,
 static int cx8802_stop_dma(struct cx8802_dev *dev)
 {
        struct cx88_core *core = dev->core;
-       dprintk( 0, "cx8802_stop_dma\n" );
+       dprintk( 1, "cx8802_stop_dma\n" );
 
        /* stop dma */
        cx_clear(MO_TS_DMACNTRL, 0x11);
 
        /* disable irqs */
-       cx_clear(MO_PCI_INTMSK, 0x000004);
+       cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT);
        cx_clear(MO_TS_INTMSK, 0x1f0011);
 
        /* Reset the controller */
@@ -135,12 +183,44 @@ static int cx8802_restart_queue(struct cx8802_dev    *dev,
                                struct cx88_dmaqueue *q)
 {
        struct cx88_buffer *buf;
-       struct list_head *item;
 
-       dprintk( 0, "cx8802_restart_queue\n" );
+       dprintk( 1, "cx8802_restart_queue\n" );
        if (list_empty(&q->active))
        {
-               dprintk( 0, "cx8802_restart_queue: queue is empty\n" );
+               struct cx88_buffer *prev;
+               prev = NULL;
+
+               dprintk(1, "cx8802_restart_queue: queue is empty\n" );
+
+               for (;;) {
+                       if (list_empty(&q->queued))
+                               return 0;
+                       buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
+                       if (NULL == prev) {
+                               list_del(&buf->vb.queue);
+                               list_add_tail(&buf->vb.queue,&q->active);
+                               cx8802_start_dma(dev, q, buf);
+                               buf->vb.state = VIDEOBUF_ACTIVE;
+                               buf->count    = q->count++;
+                               mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
+                               dprintk(1,"[%p/%d] restart_queue - first active\n",
+                                       buf,buf->vb.i);
+
+                       } else if (prev->vb.width  == buf->vb.width  &&
+                                  prev->vb.height == buf->vb.height &&
+                                  prev->fmt       == buf->fmt) {
+                               list_del(&buf->vb.queue);
+                               list_add_tail(&buf->vb.queue,&q->active);
+                               buf->vb.state = VIDEOBUF_ACTIVE;
+                               buf->count    = q->count++;
+                               prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
+                               dprintk(1,"[%p/%d] restart_queue - move to active\n",
+                                       buf,buf->vb.i);
+                       } else {
+                               return 0;
+                       }
+                       prev = buf;
+               }
                return 0;
        }
 
@@ -148,72 +228,71 @@ static int cx8802_restart_queue(struct cx8802_dev    *dev,
        dprintk(2,"restart_queue [%p/%d]: restart dma\n",
                buf, buf->vb.i);
        cx8802_start_dma(dev, q, buf);
-       list_for_each(item,&q->active) {
-               buf = list_entry(item, struct cx88_buffer, vb.queue);
+       list_for_each_entry(buf, &q->active, vb.queue)
                buf->count = q->count++;
-       }
        mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
        return 0;
 }
 
 /* ------------------------------------------------------------------ */
 
-int cx8802_buf_prepare(struct cx8802_dev *dev, struct cx88_buffer *buf,
-                       enum v4l2_field field)
+int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
+                       struct cx88_buffer *buf, enum v4l2_field field)
 {
        int size = dev->ts_packet_size * dev->ts_packet_count;
+       struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
        int rc;
 
        dprintk(1, "%s: %p\n", __FUNCTION__, buf);
        if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
                return -EINVAL;
 
-       if (STATE_NEEDS_INIT == buf->vb.state) {
+       if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
                buf->vb.width  = dev->ts_packet_size;
                buf->vb.height = dev->ts_packet_count;
                buf->vb.size   = size;
                buf->vb.field  = field /*V4L2_FIELD_TOP*/;
 
-               if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
+               if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
                        goto fail;
                cx88_risc_databuffer(dev->pci, &buf->risc,
-                                    buf->vb.dma.sglist,
-                                    buf->vb.width, buf->vb.height);
+                                    dma->sglist,
+                                    buf->vb.width, buf->vb.height, 0);
        }
-       buf->vb.state = STATE_PREPARED;
+       buf->vb.state = VIDEOBUF_PREPARED;
        return 0;
 
  fail:
-       cx88_free_buffer(dev->pci,buf);
+       cx88_free_buffer(q,buf);
        return rc;
 }
 
 void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
 {
        struct cx88_buffer    *prev;
-       struct cx88_dmaqueue  *q    = &dev->mpegq;
+       struct cx88_dmaqueue  *cx88q = &dev->mpegq;
 
        dprintk( 1, "cx8802_buf_queue\n" );
        /* add jump to stopper */
        buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
-       buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
-
-       if (list_empty(&q->active)) {
-               dprintk( 0, "queue is empty - first active\n" );
-               list_add_tail(&buf->vb.queue,&q->active);
-               cx8802_start_dma(dev, q, buf);
-               buf->vb.state = STATE_ACTIVE;
-               buf->count    = q->count++;
-               mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
-               dprintk(0,"[%p/%d] %s - first active\n",
+       buf->risc.jmp[1] = cpu_to_le32(cx88q->stopper.dma);
+
+       if (list_empty(&cx88q->active)) {
+               dprintk( 1, "queue is empty - first active\n" );
+               list_add_tail(&buf->vb.queue,&cx88q->active);
+               cx8802_start_dma(dev, cx88q, buf);
+               buf->vb.state = VIDEOBUF_ACTIVE;
+               buf->count    = cx88q->count++;
+               mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT);
+               dprintk(1,"[%p/%d] %s - first active\n",
                        buf, buf->vb.i, __FUNCTION__);
 
        } else {
                dprintk( 1, "queue is not empty - append to active\n" );
-               prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
-               list_add_tail(&buf->vb.queue,&q->active);
-               buf->vb.state = STATE_ACTIVE;
-               buf->count    = q->count++;
+               prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
+               list_add_tail(&buf->vb.queue,&cx88q->active);
+               buf->vb.state = VIDEOBUF_ACTIVE;
+               buf->count    = cx88q->count++;
                prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
                dprintk( 1, "[%p/%d] %s - append to active\n",
                        buf, buf->vb.i, __FUNCTION__);
@@ -232,14 +311,14 @@ static void do_cancel_buffers(struct cx8802_dev *dev, char *reason, int restart)
        while (!list_empty(&q->active)) {
                buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
                list_del(&buf->vb.queue);
-               buf->vb.state = STATE_ERROR;
+               buf->vb.state = VIDEOBUF_ERROR;
                wake_up(&buf->vb.done);
                dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
                        buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
        }
        if (restart)
        {
-               dprintk(0, "restarting queue\n" );
+               dprintk(1, "restarting queue\n" );
                cx8802_restart_queue(dev,q);
        }
        spin_unlock_irqrestore(&dev->slock,flags);
@@ -259,7 +338,7 @@ static void cx8802_timeout(unsigned long data)
 {
        struct cx8802_dev *dev = (struct cx8802_dev*)data;
 
-       dprintk(0, "%s\n",__FUNCTION__);
+       dprintk(1, "%s\n",__FUNCTION__);
 
        if (debug)
                cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
@@ -291,7 +370,8 @@ static void cx8802_mpeg_irq(struct cx8802_dev *dev)
 
        if (debug || (status & mask & ~0xff))
                cx88_print_irqbits(core->name, "irq mpeg ",
-                                  cx88_mpeg_irqs, status, mask);
+                                  cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
+                                  status, mask);
 
        /* risc op code error */
        if (status & (1 << 16)) {
@@ -328,7 +408,7 @@ static void cx8802_mpeg_irq(struct cx8802_dev *dev)
 
 #define MAX_IRQ_LOOP 10
 
-static irqreturn_t cx8802_irq(int irq, void *dev_id, struct pt_regs *regs)
+static irqreturn_t cx8802_irq(int irq, void *dev_id)
 {
        struct cx8802_dev *dev = dev_id;
        struct cx88_core *core = dev->core;
@@ -336,7 +416,8 @@ static irqreturn_t cx8802_irq(int irq, void *dev_id, struct pt_regs *regs)
        int loop, handled = 0;
 
        for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
-               status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x04);
+               status = cx_read(MO_PCI_INTSTAT) &
+                       (core->pci_irqmask | PCI_INT_TSINT);
                if (0 == status)
                        goto out;
                dprintk( 1, "cx8802_irq\n" );
@@ -347,7 +428,7 @@ static irqreturn_t cx8802_irq(int irq, void *dev_id, struct pt_regs *regs)
 
                if (status & core->pci_irqmask)
                        cx88_core_irq(core,status);
-               if (status & 0x04)
+               if (status & PCI_INT_TSINT)
                        cx8802_mpeg_irq(dev);
        };
        if (MAX_IRQ_LOOP == loop) {
@@ -361,10 +442,7 @@ static irqreturn_t cx8802_irq(int irq, void *dev_id, struct pt_regs *regs)
        return IRQ_RETVAL(handled);
 }
 
-/* ----------------------------------------------------------- */
-/* exported stuff                                              */
-
-int cx8802_init_common(struct cx8802_dev *dev)
+static int cx8802_init_common(struct cx8802_dev *dev)
 {
        struct cx88_core *core = dev->core;
        int err;
@@ -373,7 +451,7 @@ int cx8802_init_common(struct cx8802_dev *dev)
        if (pci_enable_device(dev->pci))
                return -EIO;
        pci_set_master(dev->pci);
-       if (!pci_dma_supported(dev->pci,0xffffffff)) {
+       if (!pci_dma_supported(dev->pci,DMA_32BIT_MASK)) {
                printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
                return -EIO;
        }
@@ -381,9 +459,9 @@ int cx8802_init_common(struct cx8802_dev *dev)
        pci_read_config_byte(dev->pci, PCI_CLASS_REVISION, &dev->pci_rev);
        pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER,  &dev->pci_lat);
        printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
-              "latency: %d, mmio: 0x%lx\n", dev->core->name,
+              "latency: %d, mmio: 0x%llx\n", dev->core->name,
               pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
-              dev->pci_lat,pci_resource_start(dev->pci,0));
+              dev->pci_lat,(unsigned long long)pci_resource_start(dev->pci,0));
 
        /* initialize driver struct */
        spin_lock_init(&dev->slock);
@@ -399,7 +477,7 @@ int cx8802_init_common(struct cx8802_dev *dev)
 
        /* get irq */
        err = request_irq(dev->pci->irq, cx8802_irq,
-                         SA_SHIRQ | SA_INTERRUPT, dev->core->name, dev);
+                         IRQF_SHARED | IRQF_DISABLED, dev->core->name, dev);
        if (err < 0) {
                printk(KERN_ERR "%s: can't get IRQ %d\n",
                       dev->core->name, dev->pci->irq);
@@ -412,7 +490,7 @@ int cx8802_init_common(struct cx8802_dev *dev)
        return 0;
 }
 
-void cx8802_fini_common(struct cx8802_dev *dev)
+static void cx8802_fini_common(struct cx8802_dev *dev)
 {
        dprintk( 2, "cx8802_fini_common\n" );
        cx8802_stop_dma(dev);
@@ -428,7 +506,7 @@ void cx8802_fini_common(struct cx8802_dev *dev)
 
 /* ----------------------------------------------------------- */
 
-int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
+static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
 {
        struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
        struct cx88_core *core = dev->core;
@@ -454,7 +532,7 @@ int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
        return 0;
 }
 
-int cx8802_resume_common(struct pci_dev *pci_dev)
+static int cx8802_resume_common(struct pci_dev *pci_dev)
 {
        struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
        struct cx88_core *core = dev->core;
@@ -494,18 +572,313 @@ int cx8802_resume_common(struct pci_dev *pci_dev)
        return 0;
 }
 
+#if defined(CONFIG_VIDEO_CX88_BLACKBIRD) || \
+    defined(CONFIG_VIDEO_CX88_BLACKBIRD_MODULE)
+struct cx8802_dev * cx8802_get_device(struct inode *inode)
+{
+       int minor = iminor(inode);
+       struct cx8802_dev *dev;
+
+       list_for_each_entry(dev, &cx8802_devlist, devlist)
+               if (dev->mpeg_dev && dev->mpeg_dev->minor == minor)
+                       return dev;
+
+       return NULL;
+}
+EXPORT_SYMBOL(cx8802_get_device);
+#endif
+
+struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype)
+{
+       struct cx8802_driver *d;
+
+       list_for_each_entry(d, &dev->drvlist, drvlist)
+               if (d->type_id == btype)
+                       return d;
+
+       return NULL;
+}
+
+/* Driver asked for hardware access. */
+static int cx8802_request_acquire(struct cx8802_driver *drv)
+{
+       struct cx88_core *core = drv->core;
+
+       /* Fail a request for hardware if the device is busy. */
+       if (core->active_type_id != CX88_BOARD_NONE)
+               return -EBUSY;
+
+       if (drv->advise_acquire)
+       {
+               core->active_type_id = drv->type_id;
+               drv->advise_acquire(drv);
+
+               mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
+       }
+
+       return 0;
+}
+
+/* Driver asked to release hardware. */
+static int cx8802_request_release(struct cx8802_driver *drv)
+{
+       struct cx88_core *core = drv->core;
+
+       if (drv->advise_release)
+       {
+               drv->advise_release(drv);
+               core->active_type_id = CX88_BOARD_NONE;
+               mpeg_dbg(1,"%s() Post release GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
+       }
+
+       return 0;
+}
+
+static int cx8802_check_driver(struct cx8802_driver *drv)
+{
+       if (drv == NULL)
+               return -ENODEV;
+
+       if ((drv->type_id != CX88_MPEG_DVB) &&
+               (drv->type_id != CX88_MPEG_BLACKBIRD))
+               return -EINVAL;
+
+       if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
+               (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
+               return -EINVAL;
+
+       if ((drv->probe == NULL) ||
+               (drv->remove == NULL) ||
+               (drv->advise_acquire == NULL) ||
+               (drv->advise_release == NULL))
+               return -EINVAL;
+
+       return 0;
+}
+
+int cx8802_register_driver(struct cx8802_driver *drv)
+{
+       struct cx8802_dev *dev;
+       struct cx8802_driver *driver;
+       int err, i = 0;
+
+       printk(KERN_INFO
+              "cx88/2: registering cx8802 driver, type: %s access: %s\n",
+              drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
+              drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
+
+       if ((err = cx8802_check_driver(drv)) != 0) {
+               printk(KERN_ERR "cx88/2: cx8802_driver is invalid\n");
+               return err;
+       }
+
+       list_for_each_entry(dev, &cx8802_devlist, devlist) {
+               printk(KERN_INFO
+                      "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
+                      dev->core->name, dev->pci->subsystem_vendor,
+                      dev->pci->subsystem_device, dev->core->board.name,
+                      dev->core->boardnr);
+
+               /* Bring up a new struct for each driver instance */
+               driver = kzalloc(sizeof(*drv),GFP_KERNEL);
+               if (driver == NULL)
+                       return -ENOMEM;
+
+               /* Snapshot of the driver registration data */
+               drv->core = dev->core;
+               drv->suspend = cx8802_suspend_common;
+               drv->resume = cx8802_resume_common;
+               drv->request_acquire = cx8802_request_acquire;
+               drv->request_release = cx8802_request_release;
+               memcpy(driver, drv, sizeof(*driver));
+
+               err = drv->probe(driver);
+               if (err == 0) {
+                       i++;
+                       mutex_lock(&drv->core->lock);
+                       list_add_tail(&driver->drvlist, &dev->drvlist);
+                       mutex_unlock(&drv->core->lock);
+               } else {
+                       printk(KERN_ERR
+                              "%s/2: cx8802 probe failed, err = %d\n",
+                              dev->core->name, err);
+               }
+
+       }
+
+       return i ? 0 : -ENODEV;
+}
+
+int cx8802_unregister_driver(struct cx8802_driver *drv)
+{
+       struct cx8802_dev *dev;
+       struct cx8802_driver *d, *dtmp;
+       int err = 0;
+
+       printk(KERN_INFO
+              "cx88/2: unregistering cx8802 driver, type: %s access: %s\n",
+              drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
+              drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
+
+       list_for_each_entry(dev, &cx8802_devlist, devlist) {
+               printk(KERN_INFO
+                      "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
+                      dev->core->name, dev->pci->subsystem_vendor,
+                      dev->pci->subsystem_device, dev->core->board.name,
+                      dev->core->boardnr);
+
+               list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) {
+                       /* only unregister the correct driver type */
+                       if (d->type_id != drv->type_id)
+                               continue;
+
+                       err = d->remove(d);
+                       if (err == 0) {
+                               mutex_lock(&drv->core->lock);
+                               list_del(&d->drvlist);
+                               mutex_unlock(&drv->core->lock);
+                               kfree(d);
+                       } else
+                               printk(KERN_ERR "%s/2: cx8802 driver remove "
+                                      "failed (%d)\n", dev->core->name, err);
+               }
+
+       }
+
+       return err;
+}
+
 /* ----------------------------------------------------------- */
+static int __devinit cx8802_probe(struct pci_dev *pci_dev,
+                              const struct pci_device_id *pci_id)
+{
+       struct cx8802_dev *dev;
+       struct cx88_core  *core;
+       int err;
+
+       /* general setup */
+       core = cx88_core_get(pci_dev);
+       if (NULL == core)
+               return -EINVAL;
+
+       printk("%s/2: cx2388x 8802 Driver Manager\n", core->name);
+
+       err = -ENODEV;
+       if (!core->board.mpeg)
+               goto fail_core;
+
+       err = -ENOMEM;
+       dev = kzalloc(sizeof(*dev),GFP_KERNEL);
+       if (NULL == dev)
+               goto fail_core;
+       dev->pci = pci_dev;
+       dev->core = core;
 
+       err = cx8802_init_common(dev);
+       if (err != 0)
+               goto fail_free;
+
+       INIT_LIST_HEAD(&dev->drvlist);
+       list_add_tail(&dev->devlist,&cx8802_devlist);
+
+       /* Maintain a reference so cx88-video can query the 8802 device. */
+       core->dvbdev = dev;
+
+       /* now autoload cx88-dvb or cx88-blackbird */
+       request_modules(dev);
+       return 0;
+
+ fail_free:
+       kfree(dev);
+ fail_core:
+       cx88_core_put(core,pci_dev);
+       return err;
+}
+
+static void __devexit cx8802_remove(struct pci_dev *pci_dev)
+{
+       struct cx8802_dev *dev;
+
+       dev = pci_get_drvdata(pci_dev);
+
+       dprintk( 1, "%s\n", __FUNCTION__);
+
+       if (!list_empty(&dev->drvlist)) {
+               struct cx8802_driver *drv, *tmp;
+               int err;
+
+               printk(KERN_WARNING "%s/2: Trying to remove cx8802 driver "
+                      "while cx8802 sub-drivers still loaded?!\n",
+                      dev->core->name);
+
+               list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
+                       err = drv->remove(drv);
+                       if (err == 0) {
+                               mutex_lock(&drv->core->lock);
+                               list_del(&drv->drvlist);
+                               mutex_unlock(&drv->core->lock);
+                       } else
+                               printk(KERN_ERR "%s/2: cx8802 driver remove "
+                                      "failed (%d)\n", dev->core->name, err);
+                       kfree(drv);
+               }
+       }
+
+       /* Destroy any 8802 reference. */
+       dev->core->dvbdev = NULL;
+
+       /* common */
+       cx8802_fini_common(dev);
+       cx88_core_put(dev->core,dev->pci);
+       kfree(dev);
+}
+
+static struct pci_device_id cx8802_pci_tbl[] = {
+       {
+               .vendor       = 0x14f1,
+               .device       = 0x8802,
+               .subvendor    = PCI_ANY_ID,
+               .subdevice    = PCI_ANY_ID,
+       },{
+               /* --- end of list --- */
+       }
+};
+MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
+
+static struct pci_driver cx8802_pci_driver = {
+       .name     = "cx88-mpeg driver manager",
+       .id_table = cx8802_pci_tbl,
+       .probe    = cx8802_probe,
+       .remove   = __devexit_p(cx8802_remove),
+};
+
+static int cx8802_init(void)
+{
+       printk(KERN_INFO "cx88/2: cx2388x MPEG-TS Driver Manager version %d.%d.%d loaded\n",
+              (CX88_VERSION_CODE >> 16) & 0xff,
+              (CX88_VERSION_CODE >>  8) & 0xff,
+              CX88_VERSION_CODE & 0xff);
+#ifdef SNAPSHOT
+       printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
+              SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
+#endif
+       return pci_register_driver(&cx8802_pci_driver);
+}
+
+static void cx8802_fini(void)
+{
+       pci_unregister_driver(&cx8802_pci_driver);
+}
+
+module_init(cx8802_init);
+module_exit(cx8802_fini);
 EXPORT_SYMBOL(cx8802_buf_prepare);
 EXPORT_SYMBOL(cx8802_buf_queue);
 EXPORT_SYMBOL(cx8802_cancel_buffers);
 
-EXPORT_SYMBOL(cx8802_init_common);
-EXPORT_SYMBOL(cx8802_fini_common);
-
-EXPORT_SYMBOL(cx8802_suspend_common);
-EXPORT_SYMBOL(cx8802_resume_common);
-
+EXPORT_SYMBOL(cx8802_register_driver);
+EXPORT_SYMBOL(cx8802_unregister_driver);
+EXPORT_SYMBOL(cx8802_get_driver);
 /* ----------------------------------------------------------- */
 /*
  * Local variables: