Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  *  Support for the mpeg transport stream transfers
0005  *  PCI function #2 of the cx2388x.
0006  *
0007  *    (c) 2004 Jelle Foks <jelle@foks.us>
0008  *    (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
0009  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
0010  */
0011 
0012 #include "cx88.h"
0013 
0014 #include <linux/module.h>
0015 #include <linux/slab.h>
0016 #include <linux/init.h>
0017 #include <linux/device.h>
0018 #include <linux/dma-mapping.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/delay.h>
0021 
0022 /* ------------------------------------------------------------------ */
0023 
0024 MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
0025 MODULE_AUTHOR("Jelle Foks <jelle@foks.us>");
0026 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
0027 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
0028 MODULE_LICENSE("GPL");
0029 MODULE_VERSION(CX88_VERSION);
0030 
0031 static unsigned int debug;
0032 module_param(debug, int, 0644);
0033 MODULE_PARM_DESC(debug, "enable debug messages [mpeg]");
0034 
0035 #define dprintk(level, fmt, arg...) do {                \
0036     if (debug + 1 > level)                      \
0037         printk(KERN_DEBUG pr_fmt("%s: mpeg:" fmt),      \
0038             __func__, ##arg);               \
0039 } while (0)
0040 
0041 #if defined(CONFIG_MODULES) && defined(MODULE)
0042 static void request_module_async(struct work_struct *work)
0043 {
0044     struct cx8802_dev *dev = container_of(work, struct cx8802_dev,
0045                           request_module_wk);
0046 
0047     if (dev->core->board.mpeg & CX88_MPEG_DVB)
0048         request_module("cx88-dvb");
0049     if (dev->core->board.mpeg & CX88_MPEG_BLACKBIRD)
0050         request_module("cx88-blackbird");
0051 }
0052 
0053 static void request_modules(struct cx8802_dev *dev)
0054 {
0055     INIT_WORK(&dev->request_module_wk, request_module_async);
0056     schedule_work(&dev->request_module_wk);
0057 }
0058 
0059 static void flush_request_modules(struct cx8802_dev *dev)
0060 {
0061     flush_work(&dev->request_module_wk);
0062 }
0063 #else
0064 #define request_modules(dev)
0065 #define flush_request_modules(dev)
0066 #endif /* CONFIG_MODULES */
0067 
0068 static LIST_HEAD(cx8802_devlist);
0069 static DEFINE_MUTEX(cx8802_mutex);
0070 /* ------------------------------------------------------------------ */
0071 
0072 int cx8802_start_dma(struct cx8802_dev    *dev,
0073              struct cx88_dmaqueue *q,
0074              struct cx88_buffer   *buf)
0075 {
0076     struct cx88_core *core = dev->core;
0077 
0078     dprintk(1, "w: %d, h: %d, f: %d\n",
0079         core->width, core->height, core->field);
0080 
0081     /* setup fifo + format */
0082     cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
0083                 dev->ts_packet_size, buf->risc.dma);
0084 
0085     /* write TS length to chip */
0086     cx_write(MO_TS_LNGTH, dev->ts_packet_size);
0087 
0088     /*
0089      * FIXME: this needs a review.
0090      * also: move to cx88-blackbird + cx88-dvb source files?
0091      */
0092 
0093     dprintk(1, "core->active_type_id = 0x%08x\n", core->active_type_id);
0094 
0095     if ((core->active_type_id == CX88_MPEG_DVB) &&
0096         (core->board.mpeg & CX88_MPEG_DVB)) {
0097         dprintk(1, "cx8802_start_dma doing .dvb\n");
0098         /* negedge driven & software reset */
0099         cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
0100         udelay(100);
0101         cx_write(MO_PINMUX_IO, 0x00);
0102         cx_write(TS_HW_SOP_CNTRL, 0x47 << 16 | 188 << 4 | 0x01);
0103         switch (core->boardnr) {
0104         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
0105         case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
0106         case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
0107         case CX88_BOARD_PCHDTV_HD5500:
0108             cx_write(TS_SOP_STAT, 1 << 13);
0109             break;
0110         case CX88_BOARD_SAMSUNG_SMT_7020:
0111             cx_write(TS_SOP_STAT, 0x00);
0112             break;
0113         case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
0114         case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
0115             /* Enable MPEG parallel IO and video signal pins */
0116             cx_write(MO_PINMUX_IO, 0x88);
0117             udelay(100);
0118             break;
0119         case CX88_BOARD_HAUPPAUGE_HVR1300:
0120             /* Enable MPEG parallel IO and video signal pins */
0121             cx_write(MO_PINMUX_IO, 0x88);
0122             cx_write(TS_SOP_STAT, 0);
0123             cx_write(TS_VALERR_CNTRL, 0);
0124             break;
0125         case CX88_BOARD_PINNACLE_PCTV_HD_800i:
0126             /* Enable MPEG parallel IO and video signal pins */
0127             cx_write(MO_PINMUX_IO, 0x88);
0128             cx_write(TS_HW_SOP_CNTRL, (0x47 << 16) | (188 << 4));
0129             dev->ts_gen_cntrl = 5;
0130             cx_write(TS_SOP_STAT, 0);
0131             cx_write(TS_VALERR_CNTRL, 0);
0132             udelay(100);
0133             break;
0134         default:
0135             cx_write(TS_SOP_STAT, 0x00);
0136             break;
0137         }
0138         cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
0139         udelay(100);
0140     } else if ((core->active_type_id == CX88_MPEG_BLACKBIRD) &&
0141         (core->board.mpeg & CX88_MPEG_BLACKBIRD)) {
0142         dprintk(1, "cx8802_start_dma doing .blackbird\n");
0143         cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
0144 
0145         /* punctured clock TS & posedge driven & software reset */
0146         cx_write(TS_GEN_CNTRL, 0x46);
0147         udelay(100);
0148 
0149         cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
0150         cx_write(TS_VALERR_CNTRL, 0x2000);
0151 
0152         /* punctured clock TS & posedge driven */
0153         cx_write(TS_GEN_CNTRL, 0x06);
0154         udelay(100);
0155     } else {
0156         pr_err("%s() Failed. Unsupported value in .mpeg (0x%08x)\n",
0157                __func__, core->board.mpeg);
0158         return -EINVAL;
0159     }
0160 
0161     /* reset counter */
0162     cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
0163     q->count = 0;
0164 
0165     /* clear interrupt status register */
0166     cx_write(MO_TS_INTSTAT,  0x1f1111);
0167 
0168     /* enable irqs */
0169     dprintk(1, "setting the interrupt mask\n");
0170     cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_TSINT);
0171     cx_set(MO_TS_INTMSK,  0x1f0011);
0172 
0173     /* start dma */
0174     cx_set(MO_DEV_CNTRL2, (1 << 5));
0175     cx_set(MO_TS_DMACNTRL, 0x11);
0176     return 0;
0177 }
0178 EXPORT_SYMBOL(cx8802_start_dma);
0179 
0180 static int cx8802_stop_dma(struct cx8802_dev *dev)
0181 {
0182     struct cx88_core *core = dev->core;
0183 
0184     dprintk(1, "\n");
0185 
0186     /* stop dma */
0187     cx_clear(MO_TS_DMACNTRL, 0x11);
0188 
0189     /* disable irqs */
0190     cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT);
0191     cx_clear(MO_TS_INTMSK, 0x1f0011);
0192 
0193     /* Reset the controller */
0194     cx_write(TS_GEN_CNTRL, 0xcd);
0195     return 0;
0196 }
0197 
0198 static int cx8802_restart_queue(struct cx8802_dev    *dev,
0199                 struct cx88_dmaqueue *q)
0200 {
0201     struct cx88_buffer *buf;
0202 
0203     dprintk(1, "\n");
0204     if (list_empty(&q->active))
0205         return 0;
0206 
0207     buf = list_entry(q->active.next, struct cx88_buffer, list);
0208     dprintk(2, "restart_queue [%p/%d]: restart dma\n",
0209         buf, buf->vb.vb2_buf.index);
0210     cx8802_start_dma(dev, q, buf);
0211     return 0;
0212 }
0213 
0214 /* ------------------------------------------------------------------ */
0215 
0216 int cx8802_buf_prepare(struct vb2_queue *q, struct cx8802_dev *dev,
0217                struct cx88_buffer *buf)
0218 {
0219     int size = dev->ts_packet_size * dev->ts_packet_count;
0220     struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0);
0221     struct cx88_riscmem *risc = &buf->risc;
0222     int rc;
0223 
0224     if (vb2_plane_size(&buf->vb.vb2_buf, 0) < size)
0225         return -EINVAL;
0226     vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
0227 
0228     rc = cx88_risc_databuffer(dev->pci, risc, sgt->sgl,
0229                   dev->ts_packet_size, dev->ts_packet_count, 0);
0230     if (rc) {
0231         if (risc->cpu)
0232             dma_free_coherent(&dev->pci->dev, risc->size,
0233                       risc->cpu, risc->dma);
0234         memset(risc, 0, sizeof(*risc));
0235         return rc;
0236     }
0237     return 0;
0238 }
0239 EXPORT_SYMBOL(cx8802_buf_prepare);
0240 
0241 void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
0242 {
0243     struct cx88_buffer    *prev;
0244     struct cx88_dmaqueue  *cx88q = &dev->mpegq;
0245 
0246     dprintk(1, "\n");
0247     /* add jump to start */
0248     buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
0249     buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
0250     buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
0251 
0252     if (list_empty(&cx88q->active)) {
0253         dprintk(1, "queue is empty - first active\n");
0254         list_add_tail(&buf->list, &cx88q->active);
0255         dprintk(1, "[%p/%d] %s - first active\n",
0256             buf, buf->vb.vb2_buf.index, __func__);
0257 
0258     } else {
0259         buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
0260         dprintk(1, "queue is not empty - append to active\n");
0261         prev = list_entry(cx88q->active.prev, struct cx88_buffer, list);
0262         list_add_tail(&buf->list, &cx88q->active);
0263         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
0264         dprintk(1, "[%p/%d] %s - append to active\n",
0265             buf, buf->vb.vb2_buf.index, __func__);
0266     }
0267 }
0268 EXPORT_SYMBOL(cx8802_buf_queue);
0269 
0270 /* ----------------------------------------------------------- */
0271 
0272 static void do_cancel_buffers(struct cx8802_dev *dev)
0273 {
0274     struct cx88_dmaqueue *q = &dev->mpegq;
0275     struct cx88_buffer *buf;
0276     unsigned long flags;
0277 
0278     spin_lock_irqsave(&dev->slock, flags);
0279     while (!list_empty(&q->active)) {
0280         buf = list_entry(q->active.next, struct cx88_buffer, list);
0281         list_del(&buf->list);
0282         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
0283     }
0284     spin_unlock_irqrestore(&dev->slock, flags);
0285 }
0286 
0287 void cx8802_cancel_buffers(struct cx8802_dev *dev)
0288 {
0289     dprintk(1, "\n");
0290     cx8802_stop_dma(dev);
0291     do_cancel_buffers(dev);
0292 }
0293 EXPORT_SYMBOL(cx8802_cancel_buffers);
0294 
0295 static const char *cx88_mpeg_irqs[32] = {
0296     "ts_risci1", NULL, NULL, NULL,
0297     "ts_risci2", NULL, NULL, NULL,
0298     "ts_oflow",  NULL, NULL, NULL,
0299     "ts_sync",   NULL, NULL, NULL,
0300     "opc_err", "par_err", "rip_err", "pci_abort",
0301     "ts_err?",
0302 };
0303 
0304 static void cx8802_mpeg_irq(struct cx8802_dev *dev)
0305 {
0306     struct cx88_core *core = dev->core;
0307     u32 status, mask, count;
0308 
0309     dprintk(1, "\n");
0310     status = cx_read(MO_TS_INTSTAT);
0311     mask   = cx_read(MO_TS_INTMSK);
0312     if (0 == (status & mask))
0313         return;
0314 
0315     cx_write(MO_TS_INTSTAT, status);
0316 
0317     if (debug || (status & mask & ~0xff))
0318         cx88_print_irqbits("irq mpeg ",
0319                    cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
0320                    status, mask);
0321 
0322     /* risc op code error */
0323     if (status & (1 << 16)) {
0324         pr_warn("mpeg risc op code error\n");
0325         cx_clear(MO_TS_DMACNTRL, 0x11);
0326         cx88_sram_channel_dump(dev->core,
0327                        &cx88_sram_channels[SRAM_CH28]);
0328     }
0329 
0330     /* risc1 y */
0331     if (status & 0x01) {
0332         dprintk(1, "wake up\n");
0333         spin_lock(&dev->slock);
0334         count = cx_read(MO_TS_GPCNT);
0335         cx88_wakeup(dev->core, &dev->mpegq, count);
0336         spin_unlock(&dev->slock);
0337     }
0338 
0339     /* other general errors */
0340     if (status & 0x1f0100) {
0341         dprintk(0, "general errors: 0x%08x\n", status & 0x1f0100);
0342         spin_lock(&dev->slock);
0343         cx8802_stop_dma(dev);
0344         spin_unlock(&dev->slock);
0345     }
0346 }
0347 
0348 #define MAX_IRQ_LOOP 10
0349 
0350 static irqreturn_t cx8802_irq(int irq, void *dev_id)
0351 {
0352     struct cx8802_dev *dev = dev_id;
0353     struct cx88_core *core = dev->core;
0354     u32 status;
0355     int loop, handled = 0;
0356 
0357     for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
0358         status = cx_read(MO_PCI_INTSTAT) &
0359             (core->pci_irqmask | PCI_INT_TSINT);
0360         if (status == 0)
0361             goto out;
0362         dprintk(1, "cx8802_irq\n");
0363         dprintk(1, "    loop: %d/%d\n", loop, MAX_IRQ_LOOP);
0364         dprintk(1, "    status: %d\n", status);
0365         handled = 1;
0366         cx_write(MO_PCI_INTSTAT, status);
0367 
0368         if (status & core->pci_irqmask)
0369             cx88_core_irq(core, status);
0370         if (status & PCI_INT_TSINT)
0371             cx8802_mpeg_irq(dev);
0372     }
0373     if (loop == MAX_IRQ_LOOP) {
0374         dprintk(0, "clearing mask\n");
0375         pr_warn("irq loop -- clearing mask\n");
0376         cx_write(MO_PCI_INTMSK, 0);
0377     }
0378 
0379  out:
0380     return IRQ_RETVAL(handled);
0381 }
0382 
0383 static int cx8802_init_common(struct cx8802_dev *dev)
0384 {
0385     struct cx88_core *core = dev->core;
0386     int err;
0387 
0388     /* pci init */
0389     if (pci_enable_device(dev->pci))
0390         return -EIO;
0391     pci_set_master(dev->pci);
0392     err = dma_set_mask(&dev->pci->dev, DMA_BIT_MASK(32));
0393     if (err) {
0394         pr_err("Oops: no 32bit PCI DMA ???\n");
0395         return -EIO;
0396     }
0397 
0398     dev->pci_rev = dev->pci->revision;
0399     pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER,  &dev->pci_lat);
0400     pr_info("found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
0401         pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
0402         dev->pci_lat,
0403         (unsigned long long)pci_resource_start(dev->pci, 0));
0404 
0405     /* initialize driver struct */
0406     spin_lock_init(&dev->slock);
0407 
0408     /* init dma queue */
0409     INIT_LIST_HEAD(&dev->mpegq.active);
0410 
0411     /* get irq */
0412     err = request_irq(dev->pci->irq, cx8802_irq,
0413               IRQF_SHARED, dev->core->name, dev);
0414     if (err < 0) {
0415         pr_err("can't get IRQ %d\n", dev->pci->irq);
0416         return err;
0417     }
0418     cx_set(MO_PCI_INTMSK, core->pci_irqmask);
0419 
0420     /* everything worked */
0421     pci_set_drvdata(dev->pci, dev);
0422     return 0;
0423 }
0424 
0425 static void cx8802_fini_common(struct cx8802_dev *dev)
0426 {
0427     dprintk(2, "\n");
0428     cx8802_stop_dma(dev);
0429     pci_disable_device(dev->pci);
0430 
0431     /* unregister stuff */
0432     free_irq(dev->pci->irq, dev);
0433 }
0434 
0435 /* ----------------------------------------------------------- */
0436 
0437 static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
0438 {
0439     struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
0440     unsigned long flags;
0441 
0442     /* stop mpeg dma */
0443     spin_lock_irqsave(&dev->slock, flags);
0444     if (!list_empty(&dev->mpegq.active)) {
0445         dprintk(2, "suspend\n");
0446         pr_info("suspend mpeg\n");
0447         cx8802_stop_dma(dev);
0448     }
0449     spin_unlock_irqrestore(&dev->slock, flags);
0450 
0451     /* FIXME -- shutdown device */
0452     cx88_shutdown(dev->core);
0453 
0454     pci_save_state(pci_dev);
0455     if (pci_set_power_state(pci_dev,
0456                 pci_choose_state(pci_dev, state)) != 0) {
0457         pci_disable_device(pci_dev);
0458         dev->state.disabled = 1;
0459     }
0460     return 0;
0461 }
0462 
0463 static int cx8802_resume_common(struct pci_dev *pci_dev)
0464 {
0465     struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
0466     unsigned long flags;
0467     int err;
0468 
0469     if (dev->state.disabled) {
0470         err = pci_enable_device(pci_dev);
0471         if (err) {
0472             pr_err("can't enable device\n");
0473             return err;
0474         }
0475         dev->state.disabled = 0;
0476     }
0477     err = pci_set_power_state(pci_dev, PCI_D0);
0478     if (err) {
0479         pr_err("can't enable device\n");
0480         pci_disable_device(pci_dev);
0481         dev->state.disabled = 1;
0482 
0483         return err;
0484     }
0485     pci_restore_state(pci_dev);
0486 
0487     /* FIXME: re-initialize hardware */
0488     cx88_reset(dev->core);
0489 
0490     /* restart video+vbi capture */
0491     spin_lock_irqsave(&dev->slock, flags);
0492     if (!list_empty(&dev->mpegq.active)) {
0493         pr_info("resume mpeg\n");
0494         cx8802_restart_queue(dev, &dev->mpegq);
0495     }
0496     spin_unlock_irqrestore(&dev->slock, flags);
0497 
0498     return 0;
0499 }
0500 
0501 struct cx8802_driver *cx8802_get_driver(struct cx8802_dev *dev,
0502                     enum cx88_board_type btype)
0503 {
0504     struct cx8802_driver *d;
0505 
0506     list_for_each_entry(d, &dev->drvlist, drvlist)
0507         if (d->type_id == btype)
0508             return d;
0509 
0510     return NULL;
0511 }
0512 EXPORT_SYMBOL(cx8802_get_driver);
0513 
0514 /* Driver asked for hardware access. */
0515 static int cx8802_request_acquire(struct cx8802_driver *drv)
0516 {
0517     struct cx88_core *core = drv->core;
0518     unsigned int    i;
0519 
0520     /* Fail a request for hardware if the device is busy. */
0521     if (core->active_type_id != CX88_BOARD_NONE &&
0522         core->active_type_id != drv->type_id)
0523         return -EBUSY;
0524 
0525     if (drv->type_id == CX88_MPEG_DVB) {
0526         /* When switching to DVB, always set the input to the tuner */
0527         core->last_analog_input = core->input;
0528         core->input = 0;
0529         for (i = 0;
0530              i < ARRAY_SIZE(core->board.input);
0531              i++) {
0532             if (core->board.input[i].type == CX88_VMUX_DVB) {
0533                 core->input = i;
0534                 break;
0535             }
0536         }
0537     }
0538 
0539     if (drv->advise_acquire) {
0540         core->active_ref++;
0541         if (core->active_type_id == CX88_BOARD_NONE) {
0542             core->active_type_id = drv->type_id;
0543             drv->advise_acquire(drv);
0544         }
0545 
0546         dprintk(1, "Post acquire GPIO=%x\n", cx_read(MO_GP0_IO));
0547     }
0548 
0549     return 0;
0550 }
0551 
0552 /* Driver asked to release hardware. */
0553 static int cx8802_request_release(struct cx8802_driver *drv)
0554 {
0555     struct cx88_core *core = drv->core;
0556 
0557     if (drv->advise_release && --core->active_ref == 0) {
0558         if (drv->type_id == CX88_MPEG_DVB) {
0559             /*
0560              * If the DVB driver is releasing, reset the input
0561              * state to the last configured analog input
0562              */
0563             core->input = core->last_analog_input;
0564         }
0565 
0566         drv->advise_release(drv);
0567         core->active_type_id = CX88_BOARD_NONE;
0568         dprintk(1, "Post release GPIO=%x\n", cx_read(MO_GP0_IO));
0569     }
0570 
0571     return 0;
0572 }
0573 
0574 static int cx8802_check_driver(struct cx8802_driver *drv)
0575 {
0576     if (!drv)
0577         return -ENODEV;
0578 
0579     if ((drv->type_id != CX88_MPEG_DVB) &&
0580         (drv->type_id != CX88_MPEG_BLACKBIRD))
0581         return -EINVAL;
0582 
0583     if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
0584         (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
0585         return -EINVAL;
0586 
0587     if ((!drv->probe) ||
0588         (!drv->remove) ||
0589         (!drv->advise_acquire) ||
0590         (!drv->advise_release))
0591         return -EINVAL;
0592 
0593     return 0;
0594 }
0595 
0596 int cx8802_register_driver(struct cx8802_driver *drv)
0597 {
0598     struct cx8802_dev *dev;
0599     struct cx8802_driver *driver;
0600     int err, i = 0;
0601 
0602     pr_info("registering cx8802 driver, type: %s access: %s\n",
0603         drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
0604         drv->hw_access == CX8802_DRVCTL_SHARED ?
0605                   "shared" : "exclusive");
0606 
0607     err = cx8802_check_driver(drv);
0608     if (err) {
0609         pr_err("cx8802_driver is invalid\n");
0610         return err;
0611     }
0612 
0613     mutex_lock(&cx8802_mutex);
0614 
0615     list_for_each_entry(dev, &cx8802_devlist, devlist) {
0616         pr_info("subsystem: %04x:%04x, board: %s [card=%d]\n",
0617             dev->pci->subsystem_vendor,
0618             dev->pci->subsystem_device, dev->core->board.name,
0619             dev->core->boardnr);
0620 
0621         /* Bring up a new struct for each driver instance */
0622         driver = kzalloc(sizeof(*drv), GFP_KERNEL);
0623         if (!driver) {
0624             err = -ENOMEM;
0625             goto out;
0626         }
0627 
0628         /* Snapshot of the driver registration data */
0629         drv->core = dev->core;
0630         drv->suspend = cx8802_suspend_common;
0631         drv->resume = cx8802_resume_common;
0632         drv->request_acquire = cx8802_request_acquire;
0633         drv->request_release = cx8802_request_release;
0634         memcpy(driver, drv, sizeof(*driver));
0635 
0636         mutex_lock(&drv->core->lock);
0637         err = drv->probe(driver);
0638         if (err == 0) {
0639             i++;
0640             list_add_tail(&driver->drvlist, &dev->drvlist);
0641         } else {
0642             pr_err("cx8802 probe failed, err = %d\n", err);
0643         }
0644         mutex_unlock(&drv->core->lock);
0645     }
0646 
0647     err = i ? 0 : -ENODEV;
0648 out:
0649     mutex_unlock(&cx8802_mutex);
0650     return err;
0651 }
0652 EXPORT_SYMBOL(cx8802_register_driver);
0653 
0654 int cx8802_unregister_driver(struct cx8802_driver *drv)
0655 {
0656     struct cx8802_dev *dev;
0657     struct cx8802_driver *d, *dtmp;
0658     int err = 0;
0659 
0660     pr_info("unregistering cx8802 driver, type: %s access: %s\n",
0661         drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
0662         drv->hw_access == CX8802_DRVCTL_SHARED ?
0663                   "shared" : "exclusive");
0664 
0665     mutex_lock(&cx8802_mutex);
0666 
0667     list_for_each_entry(dev, &cx8802_devlist, devlist) {
0668         pr_info("subsystem: %04x:%04x, board: %s [card=%d]\n",
0669             dev->pci->subsystem_vendor,
0670             dev->pci->subsystem_device, dev->core->board.name,
0671             dev->core->boardnr);
0672 
0673         mutex_lock(&dev->core->lock);
0674 
0675         list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) {
0676             /* only unregister the correct driver type */
0677             if (d->type_id != drv->type_id)
0678                 continue;
0679 
0680             err = d->remove(d);
0681             if (err == 0) {
0682                 list_del(&d->drvlist);
0683                 kfree(d);
0684             } else
0685                 pr_err("cx8802 driver remove failed (%d)\n",
0686                        err);
0687         }
0688 
0689         mutex_unlock(&dev->core->lock);
0690     }
0691 
0692     mutex_unlock(&cx8802_mutex);
0693 
0694     return err;
0695 }
0696 EXPORT_SYMBOL(cx8802_unregister_driver);
0697 
0698 /* ----------------------------------------------------------- */
0699 static int cx8802_probe(struct pci_dev *pci_dev,
0700             const struct pci_device_id *pci_id)
0701 {
0702     struct cx8802_dev *dev;
0703     struct cx88_core  *core;
0704     int err;
0705 
0706     /* general setup */
0707     core = cx88_core_get(pci_dev);
0708     if (!core)
0709         return -EINVAL;
0710 
0711     pr_info("cx2388x 8802 Driver Manager\n");
0712 
0713     err = -ENODEV;
0714     if (!core->board.mpeg)
0715         goto fail_core;
0716 
0717     err = -ENOMEM;
0718     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0719     if (!dev)
0720         goto fail_core;
0721     dev->pci = pci_dev;
0722     dev->core = core;
0723 
0724     /* Maintain a reference so cx88-video can query the 8802 device. */
0725     core->dvbdev = dev;
0726 
0727     err = cx8802_init_common(dev);
0728     if (err != 0)
0729         goto fail_dev;
0730 
0731     INIT_LIST_HEAD(&dev->drvlist);
0732     mutex_lock(&cx8802_mutex);
0733     list_add_tail(&dev->devlist, &cx8802_devlist);
0734     mutex_unlock(&cx8802_mutex);
0735 
0736     /* now autoload cx88-dvb or cx88-blackbird */
0737     request_modules(dev);
0738     return 0;
0739 
0740  fail_dev:
0741     kfree(dev);
0742  fail_core:
0743     core->dvbdev = NULL;
0744     cx88_core_put(core, pci_dev);
0745     return err;
0746 }
0747 
0748 static void cx8802_remove(struct pci_dev *pci_dev)
0749 {
0750     struct cx8802_dev *dev;
0751 
0752     dev = pci_get_drvdata(pci_dev);
0753 
0754     dprintk(1, "%s\n", __func__);
0755 
0756     flush_request_modules(dev);
0757 
0758     mutex_lock(&dev->core->lock);
0759 
0760     if (!list_empty(&dev->drvlist)) {
0761         struct cx8802_driver *drv, *tmp;
0762         int err;
0763 
0764         pr_warn("Trying to remove cx8802 driver while cx8802 sub-drivers still loaded?!\n");
0765 
0766         list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
0767             err = drv->remove(drv);
0768             if (err == 0) {
0769                 list_del(&drv->drvlist);
0770             } else
0771                 pr_err("cx8802 driver remove failed (%d)\n",
0772                        err);
0773             kfree(drv);
0774         }
0775     }
0776 
0777     mutex_unlock(&dev->core->lock);
0778 
0779     /* Destroy any 8802 reference. */
0780     dev->core->dvbdev = NULL;
0781 
0782     /* common */
0783     cx8802_fini_common(dev);
0784     cx88_core_put(dev->core, dev->pci);
0785     kfree(dev);
0786 }
0787 
0788 static const struct pci_device_id cx8802_pci_tbl[] = {
0789     {
0790         .vendor       = 0x14f1,
0791         .device       = 0x8802,
0792         .subvendor    = PCI_ANY_ID,
0793         .subdevice    = PCI_ANY_ID,
0794     }, {
0795         /* --- end of list --- */
0796     }
0797 };
0798 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
0799 
0800 static struct pci_driver cx8802_pci_driver = {
0801     .name     = "cx88-mpeg driver manager",
0802     .id_table = cx8802_pci_tbl,
0803     .probe    = cx8802_probe,
0804     .remove   = cx8802_remove,
0805 };
0806 
0807 module_pci_driver(cx8802_pci_driver);