V4L/DVB (12393): cx88: fix regression in tuning for Geniatech X8000 MT
[safe/jmp/linux-2.6] / drivers / media / video / cx88 / cx88-mpeg.c
index 11d9865..7172dcf 100644 (file)
@@ -3,7 +3,7 @@
  *  Support for the mpeg transport stream transfers
  *  PCI function #2 of the cx2388x.
  *
- *    (c) 2004 Jelle Foks <jelle@foks.8m.com>
+ *    (c) 2004 Jelle Foks <jelle@foks.us>
  *    (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
  *
 /* ------------------------------------------------------------------ */
 
 MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
-MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>");
+MODULE_AUTHOR("Jelle Foks <jelle@foks.us>");
 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
 MODULE_LICENSE("GPL");
 
-static unsigned int debug = 0;
+static unsigned int debug;
 module_param(debug,int,0644);
 MODULE_PARM_DESC(debug,"enable debug messages [mpeg]");
 
@@ -102,7 +102,7 @@ static int cx8802_start_dma(struct cx8802_dev    *dev,
                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);
+               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:
@@ -116,6 +116,19 @@ static int cx8802_start_dma(struct cx8802_dev    *dev,
                        udelay(100);
                        break;
                case CX88_BOARD_HAUPPAUGE_HVR1300:
+                       /* Enable MPEG parallel IO and video signal pins */
+                       cx_write(MO_PINMUX_IO, 0x88);
+                       cx_write(TS_SOP_STAT, 0);
+                       cx_write(TS_VALERR_CNTRL, 0);
+                       break;
+               case CX88_BOARD_PINNACLE_PCTV_HD_800i:
+                       /* Enable MPEG parallel IO and video signal pins */
+                       cx_write(MO_PINMUX_IO, 0x88);
+                       cx_write(TS_HW_SOP_CNTRL, (0x47 << 16) | (188 << 4));
+                       dev->ts_gen_cntrl = 5;
+                       cx_write(TS_SOP_STAT, 0);
+                       cx_write(TS_VALERR_CNTRL, 0);
+                       udelay(100);
                        break;
                default:
                        cx_write(TS_SOP_STAT, 0x00);
@@ -137,7 +150,7 @@ 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__,
+               printk( "%s() Failed. Unsupported value in .mpeg (0x%08x)\n", __func__,
                        core->board.mpeg );
                return -EINVAL;
        }
@@ -178,7 +191,6 @@ static int cx8802_restart_queue(struct cx8802_dev    *dev,
                                struct cx88_dmaqueue *q)
 {
        struct cx88_buffer *buf;
-       struct list_head *item;
 
        dprintk( 1, "cx8802_restart_queue\n" );
        if (list_empty(&q->active))
@@ -196,7 +208,7 @@ static int cx8802_restart_queue(struct cx8802_dev    *dev,
                                list_del(&buf->vb.queue);
                                list_add_tail(&buf->vb.queue,&q->active);
                                cx8802_start_dma(dev, q, buf);
-                               buf->vb.state = STATE_ACTIVE;
+                               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",
@@ -207,7 +219,7 @@ static int cx8802_restart_queue(struct cx8802_dev    *dev,
                                   prev->fmt       == buf->fmt) {
                                list_del(&buf->vb.queue);
                                list_add_tail(&buf->vb.queue,&q->active);
-                               buf->vb.state = STATE_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",
@@ -224,10 +236,8 @@ 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;
 }
@@ -241,11 +251,11 @@ int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
        struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
        int rc;
 
-       dprintk(1, "%s: %p\n", __FUNCTION__, buf);
+       dprintk(1, "%s: %p\n", __func__, 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;
@@ -257,7 +267,7 @@ int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
                                     dma->sglist,
                                     buf->vb.width, buf->vb.height, 0);
        }
-       buf->vb.state = STATE_PREPARED;
+       buf->vb.state = VIDEOBUF_PREPARED;
        return 0;
 
  fail:
@@ -279,21 +289,21 @@ void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
                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 = STATE_ACTIVE;
+               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__);
+                       buf, buf->vb.i, __func__);
 
        } else {
                dprintk( 1, "queue is not empty - append to active\n" );
                prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
                list_add_tail(&buf->vb.queue,&cx88q->active);
-               buf->vb.state = STATE_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__);
+                       buf, buf->vb.i, __func__);
        }
 }
 
@@ -309,7 +319,7 @@ 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);
@@ -336,7 +346,7 @@ static void cx8802_timeout(unsigned long data)
 {
        struct cx8802_dev *dev = (struct cx8802_dev*)data;
 
-       dprintk(1, "%s\n",__FUNCTION__);
+       dprintk(1, "%s\n",__func__);
 
        if (debug)
                cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
@@ -440,10 +450,7 @@ static irqreturn_t cx8802_irq(int irq, void *dev_id)
        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;
@@ -452,7 +459,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,DMA_32BIT_MASK)) {
+       if (!pci_dma_supported(dev->pci,DMA_BIT_MASK(32))) {
                printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
                return -EIO;
        }
@@ -491,7 +498,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);
@@ -507,7 +514,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;
@@ -533,7 +540,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;
@@ -575,17 +582,13 @@ int cx8802_resume_common(struct pci_dev *pci_dev)
 
 #if defined(CONFIG_VIDEO_CX88_BLACKBIRD) || \
     defined(CONFIG_VIDEO_CX88_BLACKBIRD_MODULE)
-struct cx8802_dev * cx8802_get_device(struct inode *inode)
+struct cx8802_dev *cx8802_get_device(int minor)
 {
-       int minor = iminor(inode);
-       struct cx8802_dev *h = NULL;
-       struct list_head *list;
-
-       list_for_each(list,&cx8802_devlist) {
-               h = list_entry(list, struct cx8802_dev, devlist);
-               if (h->mpeg_dev && h->mpeg_dev->minor == minor)
-                       return h;
-       }
+       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;
 }
@@ -595,14 +598,10 @@ EXPORT_SYMBOL(cx8802_get_device);
 struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype)
 {
        struct cx8802_driver *d;
-       struct list_head *list;
-
-       list_for_each(list, &dev->drvlist) {
-               d = list_entry(list, struct cx8802_driver, drvlist);
 
+       list_for_each_entry(d, &dev->drvlist, drvlist)
                if (d->type_id == btype)
                        return d;
-       }
 
        return NULL;
 }
@@ -613,15 +612,23 @@ 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)
+       if (core->active_type_id != CX88_BOARD_NONE &&
+           core->active_type_id != drv->type_id)
                return -EBUSY;
 
+       core->input = CX88_VMUX_DVB;
+
        if (drv->advise_acquire)
        {
-               core->active_type_id = drv->type_id;
-               drv->advise_acquire(drv);
+               mutex_lock(&drv->core->lock);
+               core->active_ref++;
+               if (core->active_type_id == CX88_BOARD_NONE) {
+                       core->active_type_id = drv->type_id;
+                       drv->advise_acquire(drv);
+               }
+               mutex_unlock(&drv->core->lock);
 
-               mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
+               mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
        }
 
        return 0;
@@ -632,12 +639,14 @@ static int cx8802_request_release(struct cx8802_driver *drv)
 {
        struct cx88_core *core = drv->core;
 
-       if (drv->advise_release)
+       mutex_lock(&drv->core->lock);
+       if (drv->advise_release && --core->active_ref == 0)
        {
                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));
+               mpeg_dbg(1,"%s() Post release GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
        }
+       mutex_unlock(&drv->core->lock);
 
        return 0;
 }
@@ -666,10 +675,9 @@ static int cx8802_check_driver(struct cx8802_driver *drv)
 
 int cx8802_register_driver(struct cx8802_driver *drv)
 {
-       struct cx8802_dev *h;
+       struct cx8802_dev *dev;
        struct cx8802_driver *driver;
-       struct list_head *list;
-       int err = 0, i = 0;
+       int err, i = 0;
 
        printk(KERN_INFO
               "cx88/2: registering cx8802 driver, type: %s access: %s\n",
@@ -681,14 +689,12 @@ int cx8802_register_driver(struct cx8802_driver *drv)
                return err;
        }
 
-       list_for_each(list,&cx8802_devlist) {
-               h = list_entry(list, struct cx8802_dev, devlist);
-
+       list_for_each_entry(dev, &cx8802_devlist, devlist) {
                printk(KERN_INFO
                       "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
-                      h->core->name, h->pci->subsystem_vendor,
-                      h->pci->subsystem_device, h->core->board.name,
-                      h->core->boardnr);
+                      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);
@@ -696,7 +702,7 @@ int cx8802_register_driver(struct cx8802_driver *drv)
                        return -ENOMEM;
 
                /* Snapshot of the driver registration data */
-               drv->core = h->core;
+               drv->core = dev->core;
                drv->suspend = cx8802_suspend_common;
                drv->resume = cx8802_resume_common;
                drv->request_acquire = cx8802_request_acquire;
@@ -707,49 +713,38 @@ int cx8802_register_driver(struct cx8802_driver *drv)
                if (err == 0) {
                        i++;
                        mutex_lock(&drv->core->lock);
-                       list_add_tail(&driver->drvlist, &h->drvlist);
+                       list_add_tail(&driver->drvlist, &dev->drvlist);
                        mutex_unlock(&drv->core->lock);
                } else {
                        printk(KERN_ERR
                               "%s/2: cx8802 probe failed, err = %d\n",
-                              h->core->name, err);
+                              dev->core->name, err);
                }
 
        }
-       if (i == 0)
-               err = -ENODEV;
-       else
-               err = 0;
 
-       return err;
+       return i ? 0 : -ENODEV;
 }
 
 int cx8802_unregister_driver(struct cx8802_driver *drv)
 {
-       struct cx8802_dev *h;
-       struct cx8802_driver *d;
-       struct list_head *list;
-       struct list_head *list2, *q;
-       int err = 0, i = 0;
+       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(list,&cx8802_devlist) {
-               i++;
-               h = list_entry(list, struct cx8802_dev, devlist);
-
+       list_for_each_entry(dev, &cx8802_devlist, devlist) {
                printk(KERN_INFO
                       "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
-                      h->core->name, h->pci->subsystem_vendor,
-                      h->pci->subsystem_device, h->core->board.name,
-                      h->core->boardnr);
-
-               list_for_each_safe(list2, q, &h->drvlist) {
-                       d = list_entry(list2, struct cx8802_driver, drvlist);
+                      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;
@@ -757,11 +752,12 @@ int cx8802_unregister_driver(struct cx8802_driver *drv)
                        err = d->remove(d);
                        if (err == 0) {
                                mutex_lock(&drv->core->lock);
-                               list_del(list2);
+                               list_del(&d->drvlist);
                                mutex_unlock(&drv->core->lock);
+                               kfree(d);
                        } else
                                printk(KERN_ERR "%s/2: cx8802 driver remove "
-                                      "failed (%d)\n", h->core->name, err);
+                                      "failed (%d)\n", dev->core->name, err);
                }
 
        }
@@ -795,6 +791,9 @@ static int __devinit cx8802_probe(struct pci_dev *pci_dev,
        dev->pci = pci_dev;
        dev->core = core;
 
+       /* Maintain a reference so cx88-video can query the 8802 device. */
+       core->dvbdev = dev;
+
        err = cx8802_init_common(dev);
        if (err != 0)
                goto fail_free;
@@ -802,9 +801,6 @@ static int __devinit cx8802_probe(struct pci_dev *pci_dev,
        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;
@@ -812,6 +808,7 @@ static int __devinit cx8802_probe(struct pci_dev *pci_dev,
  fail_free:
        kfree(dev);
  fail_core:
+       core->dvbdev = NULL;
        cx88_core_put(core,pci_dev);
        return err;
 }
@@ -822,28 +819,26 @@ static void __devexit cx8802_remove(struct pci_dev *pci_dev)
 
        dev = pci_get_drvdata(pci_dev);
 
-       dprintk( 1, "%s\n", __FUNCTION__);
+       dprintk( 1, "%s\n", __func__);
 
        if (!list_empty(&dev->drvlist)) {
-               struct list_head *list, *tmp;
-               struct cx8802_driver *drv;
+               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_safe(list, tmp, &dev->drvlist) {
-                       drv = list_entry(list, struct cx8802_driver, drvlist);
-
+               list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
                        err = drv->remove(drv);
                        if (err == 0) {
                                mutex_lock(&drv->core->lock);
-                               list_del(list);
+                               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);
                }
        }
 
@@ -899,9 +894,6 @@ 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_register_driver);
 EXPORT_SYMBOL(cx8802_unregister_driver);
 EXPORT_SYMBOL(cx8802_get_driver);