GNU Linux-libre 4.14.294-gnu1
[releases.git] / drivers / media / pci / cx88 / cx88-mpeg.c
1 /*
2  *
3  *  Support for the mpeg transport stream transfers
4  *  PCI function #2 of the cx2388x.
5  *
6  *    (c) 2004 Jelle Foks <jelle@foks.us>
7  *    (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  */
20
21 #include "cx88.h"
22
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/device.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/interrupt.h>
29 #include <linux/delay.h>
30
31 /* ------------------------------------------------------------------ */
32
33 MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
34 MODULE_AUTHOR("Jelle Foks <jelle@foks.us>");
35 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
36 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
37 MODULE_LICENSE("GPL");
38 MODULE_VERSION(CX88_VERSION);
39
40 static unsigned int debug;
41 module_param(debug, int, 0644);
42 MODULE_PARM_DESC(debug, "enable debug messages [mpeg]");
43
44 #define dprintk(level, fmt, arg...) do {                                \
45         if (debug + 1 > level)                                          \
46                 printk(KERN_DEBUG pr_fmt("%s: mpeg:" fmt),              \
47                         __func__, ##arg);                               \
48 } while (0)
49
50 #if defined(CONFIG_MODULES) && defined(MODULE)
51 static void request_module_async(struct work_struct *work)
52 {
53         struct cx8802_dev *dev = container_of(work, struct cx8802_dev,
54                                               request_module_wk);
55
56         if (dev->core->board.mpeg & CX88_MPEG_DVB)
57                 request_module("cx88-dvb");
58         if (dev->core->board.mpeg & CX88_MPEG_BLACKBIRD)
59                 request_module("cx88-blackbird");
60 }
61
62 static void request_modules(struct cx8802_dev *dev)
63 {
64         INIT_WORK(&dev->request_module_wk, request_module_async);
65         schedule_work(&dev->request_module_wk);
66 }
67
68 static void flush_request_modules(struct cx8802_dev *dev)
69 {
70         flush_work(&dev->request_module_wk);
71 }
72 #else
73 #define request_modules(dev)
74 #define flush_request_modules(dev)
75 #endif /* CONFIG_MODULES */
76
77 static LIST_HEAD(cx8802_devlist);
78 static DEFINE_MUTEX(cx8802_mutex);
79 /* ------------------------------------------------------------------ */
80
81 int cx8802_start_dma(struct cx8802_dev    *dev,
82                      struct cx88_dmaqueue *q,
83                      struct cx88_buffer   *buf)
84 {
85         struct cx88_core *core = dev->core;
86
87         dprintk(1, "w: %d, h: %d, f: %d\n",
88                 core->width, core->height, core->field);
89
90         /* setup fifo + format */
91         cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
92                                 dev->ts_packet_size, buf->risc.dma);
93
94         /* write TS length to chip */
95         cx_write(MO_TS_LNGTH, dev->ts_packet_size);
96
97         /*
98          * FIXME: this needs a review.
99          * also: move to cx88-blackbird + cx88-dvb source files?
100          */
101
102         dprintk(1, "core->active_type_id = 0x%08x\n", core->active_type_id);
103
104         if ((core->active_type_id == CX88_MPEG_DVB) &&
105             (core->board.mpeg & CX88_MPEG_DVB)) {
106                 dprintk(1, "cx8802_start_dma doing .dvb\n");
107                 /* negedge driven & software reset */
108                 cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
109                 udelay(100);
110                 cx_write(MO_PINMUX_IO, 0x00);
111                 cx_write(TS_HW_SOP_CNTRL, 0x47 << 16 | 188 << 4 | 0x01);
112                 switch (core->boardnr) {
113                 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
114                 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
115                 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
116                 case CX88_BOARD_PCHDTV_HD5500:
117                         cx_write(TS_SOP_STAT, 1 << 13);
118                         break;
119                 case CX88_BOARD_SAMSUNG_SMT_7020:
120                         cx_write(TS_SOP_STAT, 0x00);
121                         break;
122                 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
123                 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
124                         /* Enable MPEG parallel IO and video signal pins */
125                         cx_write(MO_PINMUX_IO, 0x88);
126                         udelay(100);
127                         break;
128                 case CX88_BOARD_HAUPPAUGE_HVR1300:
129                         /* Enable MPEG parallel IO and video signal pins */
130                         cx_write(MO_PINMUX_IO, 0x88);
131                         cx_write(TS_SOP_STAT, 0);
132                         cx_write(TS_VALERR_CNTRL, 0);
133                         break;
134                 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
135                         /* Enable MPEG parallel IO and video signal pins */
136                         cx_write(MO_PINMUX_IO, 0x88);
137                         cx_write(TS_HW_SOP_CNTRL, (0x47 << 16) | (188 << 4));
138                         dev->ts_gen_cntrl = 5;
139                         cx_write(TS_SOP_STAT, 0);
140                         cx_write(TS_VALERR_CNTRL, 0);
141                         udelay(100);
142                         break;
143                 default:
144                         cx_write(TS_SOP_STAT, 0x00);
145                         break;
146                 }
147                 cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
148                 udelay(100);
149         } else if ((core->active_type_id == CX88_MPEG_BLACKBIRD) &&
150                 (core->board.mpeg & CX88_MPEG_BLACKBIRD)) {
151                 dprintk(1, "cx8802_start_dma doing .blackbird\n");
152                 cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
153
154                 /* punctured clock TS & posedge driven & software reset */
155                 cx_write(TS_GEN_CNTRL, 0x46);
156                 udelay(100);
157
158                 cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
159                 cx_write(TS_VALERR_CNTRL, 0x2000);
160
161                 /* punctured clock TS & posedge driven */
162                 cx_write(TS_GEN_CNTRL, 0x06);
163                 udelay(100);
164         } else {
165                 pr_err("%s() Failed. Unsupported value in .mpeg (0x%08x)\n",
166                        __func__, core->board.mpeg);
167                 return -EINVAL;
168         }
169
170         /* reset counter */
171         cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
172         q->count = 0;
173
174         /* clear interrupt status register */
175         cx_write(MO_TS_INTSTAT,  0x1f1111);
176
177         /* enable irqs */
178         dprintk(1, "setting the interrupt mask\n");
179         cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_TSINT);
180         cx_set(MO_TS_INTMSK,  0x1f0011);
181
182         /* start dma */
183         cx_set(MO_DEV_CNTRL2, (1 << 5));
184         cx_set(MO_TS_DMACNTRL, 0x11);
185         return 0;
186 }
187 EXPORT_SYMBOL(cx8802_start_dma);
188
189 static int cx8802_stop_dma(struct cx8802_dev *dev)
190 {
191         struct cx88_core *core = dev->core;
192
193         dprintk(1, "\n");
194
195         /* stop dma */
196         cx_clear(MO_TS_DMACNTRL, 0x11);
197
198         /* disable irqs */
199         cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT);
200         cx_clear(MO_TS_INTMSK, 0x1f0011);
201
202         /* Reset the controller */
203         cx_write(TS_GEN_CNTRL, 0xcd);
204         return 0;
205 }
206
207 static int cx8802_restart_queue(struct cx8802_dev    *dev,
208                                 struct cx88_dmaqueue *q)
209 {
210         struct cx88_buffer *buf;
211
212         dprintk(1, "\n");
213         if (list_empty(&q->active))
214                 return 0;
215
216         buf = list_entry(q->active.next, struct cx88_buffer, list);
217         dprintk(2, "restart_queue [%p/%d]: restart dma\n",
218                 buf, buf->vb.vb2_buf.index);
219         cx8802_start_dma(dev, q, buf);
220         return 0;
221 }
222
223 /* ------------------------------------------------------------------ */
224
225 int cx8802_buf_prepare(struct vb2_queue *q, struct cx8802_dev *dev,
226                        struct cx88_buffer *buf)
227 {
228         int size = dev->ts_packet_size * dev->ts_packet_count;
229         struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0);
230         struct cx88_riscmem *risc = &buf->risc;
231         int rc;
232
233         if (vb2_plane_size(&buf->vb.vb2_buf, 0) < size)
234                 return -EINVAL;
235         vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
236
237         rc = cx88_risc_databuffer(dev->pci, risc, sgt->sgl,
238                                   dev->ts_packet_size, dev->ts_packet_count, 0);
239         if (rc) {
240                 if (risc->cpu)
241                         pci_free_consistent(dev->pci, risc->size,
242                                             risc->cpu, risc->dma);
243                 memset(risc, 0, sizeof(*risc));
244                 return rc;
245         }
246         return 0;
247 }
248 EXPORT_SYMBOL(cx8802_buf_prepare);
249
250 void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
251 {
252         struct cx88_buffer    *prev;
253         struct cx88_dmaqueue  *cx88q = &dev->mpegq;
254
255         dprintk(1, "\n");
256         /* add jump to start */
257         buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
258         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
259         buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
260
261         if (list_empty(&cx88q->active)) {
262                 dprintk(1, "queue is empty - first active\n");
263                 list_add_tail(&buf->list, &cx88q->active);
264                 dprintk(1, "[%p/%d] %s - first active\n",
265                         buf, buf->vb.vb2_buf.index, __func__);
266
267         } else {
268                 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
269                 dprintk(1, "queue is not empty - append to active\n");
270                 prev = list_entry(cx88q->active.prev, struct cx88_buffer, list);
271                 list_add_tail(&buf->list, &cx88q->active);
272                 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
273                 dprintk(1, "[%p/%d] %s - append to active\n",
274                         buf, buf->vb.vb2_buf.index, __func__);
275         }
276 }
277 EXPORT_SYMBOL(cx8802_buf_queue);
278
279 /* ----------------------------------------------------------- */
280
281 static void do_cancel_buffers(struct cx8802_dev *dev)
282 {
283         struct cx88_dmaqueue *q = &dev->mpegq;
284         struct cx88_buffer *buf;
285         unsigned long flags;
286
287         spin_lock_irqsave(&dev->slock, flags);
288         while (!list_empty(&q->active)) {
289                 buf = list_entry(q->active.next, struct cx88_buffer, list);
290                 list_del(&buf->list);
291                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
292         }
293         spin_unlock_irqrestore(&dev->slock, flags);
294 }
295
296 void cx8802_cancel_buffers(struct cx8802_dev *dev)
297 {
298         dprintk(1, "\n");
299         cx8802_stop_dma(dev);
300         do_cancel_buffers(dev);
301 }
302 EXPORT_SYMBOL(cx8802_cancel_buffers);
303
304 static const char *cx88_mpeg_irqs[32] = {
305         "ts_risci1", NULL, NULL, NULL,
306         "ts_risci2", NULL, NULL, NULL,
307         "ts_oflow",  NULL, NULL, NULL,
308         "ts_sync",   NULL, NULL, NULL,
309         "opc_err", "par_err", "rip_err", "pci_abort",
310         "ts_err?",
311 };
312
313 static void cx8802_mpeg_irq(struct cx8802_dev *dev)
314 {
315         struct cx88_core *core = dev->core;
316         u32 status, mask, count;
317
318         dprintk(1, "\n");
319         status = cx_read(MO_TS_INTSTAT);
320         mask   = cx_read(MO_TS_INTMSK);
321         if (0 == (status & mask))
322                 return;
323
324         cx_write(MO_TS_INTSTAT, status);
325
326         if (debug || (status & mask & ~0xff))
327                 cx88_print_irqbits("irq mpeg ",
328                                    cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
329                                    status, mask);
330
331         /* risc op code error */
332         if (status & (1 << 16)) {
333                 pr_warn("mpeg risc op code error\n");
334                 cx_clear(MO_TS_DMACNTRL, 0x11);
335                 cx88_sram_channel_dump(dev->core,
336                                        &cx88_sram_channels[SRAM_CH28]);
337         }
338
339         /* risc1 y */
340         if (status & 0x01) {
341                 dprintk(1, "wake up\n");
342                 spin_lock(&dev->slock);
343                 count = cx_read(MO_TS_GPCNT);
344                 cx88_wakeup(dev->core, &dev->mpegq, count);
345                 spin_unlock(&dev->slock);
346         }
347
348         /* other general errors */
349         if (status & 0x1f0100) {
350                 dprintk(0, "general errors: 0x%08x\n", status & 0x1f0100);
351                 spin_lock(&dev->slock);
352                 cx8802_stop_dma(dev);
353                 spin_unlock(&dev->slock);
354         }
355 }
356
357 #define MAX_IRQ_LOOP 10
358
359 static irqreturn_t cx8802_irq(int irq, void *dev_id)
360 {
361         struct cx8802_dev *dev = dev_id;
362         struct cx88_core *core = dev->core;
363         u32 status;
364         int loop, handled = 0;
365
366         for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
367                 status = cx_read(MO_PCI_INTSTAT) &
368                         (core->pci_irqmask | PCI_INT_TSINT);
369                 if (status == 0)
370                         goto out;
371                 dprintk(1, "cx8802_irq\n");
372                 dprintk(1, "    loop: %d/%d\n", loop, MAX_IRQ_LOOP);
373                 dprintk(1, "    status: %d\n", status);
374                 handled = 1;
375                 cx_write(MO_PCI_INTSTAT, status);
376
377                 if (status & core->pci_irqmask)
378                         cx88_core_irq(core, status);
379                 if (status & PCI_INT_TSINT)
380                         cx8802_mpeg_irq(dev);
381         }
382         if (loop == MAX_IRQ_LOOP) {
383                 dprintk(0, "clearing mask\n");
384                 pr_warn("irq loop -- clearing mask\n");
385                 cx_write(MO_PCI_INTMSK, 0);
386         }
387
388  out:
389         return IRQ_RETVAL(handled);
390 }
391
392 static int cx8802_init_common(struct cx8802_dev *dev)
393 {
394         struct cx88_core *core = dev->core;
395         int err;
396
397         /* pci init */
398         if (pci_enable_device(dev->pci))
399                 return -EIO;
400         pci_set_master(dev->pci);
401         err = pci_set_dma_mask(dev->pci, DMA_BIT_MASK(32));
402         if (err) {
403                 pr_err("Oops: no 32bit PCI DMA ???\n");
404                 return -EIO;
405         }
406
407         dev->pci_rev = dev->pci->revision;
408         pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER,  &dev->pci_lat);
409         pr_info("found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
410                 pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
411                 dev->pci_lat,
412                 (unsigned long long)pci_resource_start(dev->pci, 0));
413
414         /* initialize driver struct */
415         spin_lock_init(&dev->slock);
416
417         /* init dma queue */
418         INIT_LIST_HEAD(&dev->mpegq.active);
419
420         /* get irq */
421         err = request_irq(dev->pci->irq, cx8802_irq,
422                           IRQF_SHARED, dev->core->name, dev);
423         if (err < 0) {
424                 pr_err("can't get IRQ %d\n", dev->pci->irq);
425                 return err;
426         }
427         cx_set(MO_PCI_INTMSK, core->pci_irqmask);
428
429         /* everything worked */
430         pci_set_drvdata(dev->pci, dev);
431         return 0;
432 }
433
434 static void cx8802_fini_common(struct cx8802_dev *dev)
435 {
436         dprintk(2, "\n");
437         cx8802_stop_dma(dev);
438         pci_disable_device(dev->pci);
439
440         /* unregister stuff */
441         free_irq(dev->pci->irq, dev);
442 }
443
444 /* ----------------------------------------------------------- */
445
446 static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
447 {
448         struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
449         unsigned long flags;
450
451         /* stop mpeg dma */
452         spin_lock_irqsave(&dev->slock, flags);
453         if (!list_empty(&dev->mpegq.active)) {
454                 dprintk(2, "suspend\n");
455                 pr_info("suspend mpeg\n");
456                 cx8802_stop_dma(dev);
457         }
458         spin_unlock_irqrestore(&dev->slock, flags);
459
460         /* FIXME -- shutdown device */
461         cx88_shutdown(dev->core);
462
463         pci_save_state(pci_dev);
464         if (pci_set_power_state(pci_dev,
465                                 pci_choose_state(pci_dev, state)) != 0) {
466                 pci_disable_device(pci_dev);
467                 dev->state.disabled = 1;
468         }
469         return 0;
470 }
471
472 static int cx8802_resume_common(struct pci_dev *pci_dev)
473 {
474         struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
475         unsigned long flags;
476         int err;
477
478         if (dev->state.disabled) {
479                 err = pci_enable_device(pci_dev);
480                 if (err) {
481                         pr_err("can't enable device\n");
482                         return err;
483                 }
484                 dev->state.disabled = 0;
485         }
486         err = pci_set_power_state(pci_dev, PCI_D0);
487         if (err) {
488                 pr_err("can't enable device\n");
489                 pci_disable_device(pci_dev);
490                 dev->state.disabled = 1;
491
492                 return err;
493         }
494         pci_restore_state(pci_dev);
495
496         /* FIXME: re-initialize hardware */
497         cx88_reset(dev->core);
498
499         /* restart video+vbi capture */
500         spin_lock_irqsave(&dev->slock, flags);
501         if (!list_empty(&dev->mpegq.active)) {
502                 pr_info("resume mpeg\n");
503                 cx8802_restart_queue(dev, &dev->mpegq);
504         }
505         spin_unlock_irqrestore(&dev->slock, flags);
506
507         return 0;
508 }
509
510 struct cx8802_driver *cx8802_get_driver(struct cx8802_dev *dev,
511                                         enum cx88_board_type btype)
512 {
513         struct cx8802_driver *d;
514
515         list_for_each_entry(d, &dev->drvlist, drvlist)
516                 if (d->type_id == btype)
517                         return d;
518
519         return NULL;
520 }
521 EXPORT_SYMBOL(cx8802_get_driver);
522
523 /* Driver asked for hardware access. */
524 static int cx8802_request_acquire(struct cx8802_driver *drv)
525 {
526         struct cx88_core *core = drv->core;
527         unsigned int    i;
528
529         /* Fail a request for hardware if the device is busy. */
530         if (core->active_type_id != CX88_BOARD_NONE &&
531             core->active_type_id != drv->type_id)
532                 return -EBUSY;
533
534         if (drv->type_id == CX88_MPEG_DVB) {
535                 /* When switching to DVB, always set the input to the tuner */
536                 core->last_analog_input = core->input;
537                 core->input = 0;
538                 for (i = 0;
539                      i < (sizeof(core->board.input) /
540                           sizeof(struct cx88_input));
541                      i++) {
542                         if (core->board.input[i].type == CX88_VMUX_DVB) {
543                                 core->input = i;
544                                 break;
545                         }
546                 }
547         }
548
549         if (drv->advise_acquire) {
550                 core->active_ref++;
551                 if (core->active_type_id == CX88_BOARD_NONE) {
552                         core->active_type_id = drv->type_id;
553                         drv->advise_acquire(drv);
554                 }
555
556                 dprintk(1, "Post acquire GPIO=%x\n", cx_read(MO_GP0_IO));
557         }
558
559         return 0;
560 }
561
562 /* Driver asked to release hardware. */
563 static int cx8802_request_release(struct cx8802_driver *drv)
564 {
565         struct cx88_core *core = drv->core;
566
567         if (drv->advise_release && --core->active_ref == 0) {
568                 if (drv->type_id == CX88_MPEG_DVB) {
569                         /*
570                          * If the DVB driver is releasing, reset the input
571                          * state to the last configured analog input
572                          */
573                         core->input = core->last_analog_input;
574                 }
575
576                 drv->advise_release(drv);
577                 core->active_type_id = CX88_BOARD_NONE;
578                 dprintk(1, "Post release GPIO=%x\n", cx_read(MO_GP0_IO));
579         }
580
581         return 0;
582 }
583
584 static int cx8802_check_driver(struct cx8802_driver *drv)
585 {
586         if (!drv)
587                 return -ENODEV;
588
589         if ((drv->type_id != CX88_MPEG_DVB) &&
590             (drv->type_id != CX88_MPEG_BLACKBIRD))
591                 return -EINVAL;
592
593         if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
594             (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
595                 return -EINVAL;
596
597         if ((!drv->probe) ||
598             (!drv->remove) ||
599             (!drv->advise_acquire) ||
600             (!drv->advise_release))
601                 return -EINVAL;
602
603         return 0;
604 }
605
606 int cx8802_register_driver(struct cx8802_driver *drv)
607 {
608         struct cx8802_dev *dev;
609         struct cx8802_driver *driver;
610         int err, i = 0;
611
612         pr_info("registering cx8802 driver, type: %s access: %s\n",
613                 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
614                 drv->hw_access == CX8802_DRVCTL_SHARED ?
615                                   "shared" : "exclusive");
616
617         err = cx8802_check_driver(drv);
618         if (err) {
619                 pr_err("cx8802_driver is invalid\n");
620                 return err;
621         }
622
623         mutex_lock(&cx8802_mutex);
624
625         list_for_each_entry(dev, &cx8802_devlist, devlist) {
626                 pr_info("subsystem: %04x:%04x, board: %s [card=%d]\n",
627                         dev->pci->subsystem_vendor,
628                         dev->pci->subsystem_device, dev->core->board.name,
629                         dev->core->boardnr);
630
631                 /* Bring up a new struct for each driver instance */
632                 driver = kzalloc(sizeof(*drv), GFP_KERNEL);
633                 if (!driver) {
634                         err = -ENOMEM;
635                         goto out;
636                 }
637
638                 /* Snapshot of the driver registration data */
639                 drv->core = dev->core;
640                 drv->suspend = cx8802_suspend_common;
641                 drv->resume = cx8802_resume_common;
642                 drv->request_acquire = cx8802_request_acquire;
643                 drv->request_release = cx8802_request_release;
644                 memcpy(driver, drv, sizeof(*driver));
645
646                 mutex_lock(&drv->core->lock);
647                 err = drv->probe(driver);
648                 if (err == 0) {
649                         i++;
650                         list_add_tail(&driver->drvlist, &dev->drvlist);
651                 } else {
652                         pr_err("cx8802 probe failed, err = %d\n", err);
653                 }
654                 mutex_unlock(&drv->core->lock);
655         }
656
657         err = i ? 0 : -ENODEV;
658 out:
659         mutex_unlock(&cx8802_mutex);
660         return err;
661 }
662 EXPORT_SYMBOL(cx8802_register_driver);
663
664 int cx8802_unregister_driver(struct cx8802_driver *drv)
665 {
666         struct cx8802_dev *dev;
667         struct cx8802_driver *d, *dtmp;
668         int err = 0;
669
670         pr_info("unregistering cx8802 driver, type: %s access: %s\n",
671                 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
672                 drv->hw_access == CX8802_DRVCTL_SHARED ?
673                                   "shared" : "exclusive");
674
675         mutex_lock(&cx8802_mutex);
676
677         list_for_each_entry(dev, &cx8802_devlist, devlist) {
678                 pr_info("subsystem: %04x:%04x, board: %s [card=%d]\n",
679                         dev->pci->subsystem_vendor,
680                         dev->pci->subsystem_device, dev->core->board.name,
681                         dev->core->boardnr);
682
683                 mutex_lock(&dev->core->lock);
684
685                 list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) {
686                         /* only unregister the correct driver type */
687                         if (d->type_id != drv->type_id)
688                                 continue;
689
690                         err = d->remove(d);
691                         if (err == 0) {
692                                 list_del(&d->drvlist);
693                                 kfree(d);
694                         } else
695                                 pr_err("cx8802 driver remove failed (%d)\n",
696                                        err);
697                 }
698
699                 mutex_unlock(&dev->core->lock);
700         }
701
702         mutex_unlock(&cx8802_mutex);
703
704         return err;
705 }
706 EXPORT_SYMBOL(cx8802_unregister_driver);
707
708 /* ----------------------------------------------------------- */
709 static int cx8802_probe(struct pci_dev *pci_dev,
710                         const struct pci_device_id *pci_id)
711 {
712         struct cx8802_dev *dev;
713         struct cx88_core  *core;
714         int err;
715
716         /* general setup */
717         core = cx88_core_get(pci_dev);
718         if (!core)
719                 return -EINVAL;
720
721         pr_info("cx2388x 8802 Driver Manager\n");
722
723         err = -ENODEV;
724         if (!core->board.mpeg)
725                 goto fail_core;
726
727         err = -ENOMEM;
728         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
729         if (!dev)
730                 goto fail_core;
731         dev->pci = pci_dev;
732         dev->core = core;
733
734         /* Maintain a reference so cx88-video can query the 8802 device. */
735         core->dvbdev = dev;
736
737         err = cx8802_init_common(dev);
738         if (err != 0)
739                 goto fail_dev;
740
741         INIT_LIST_HEAD(&dev->drvlist);
742         mutex_lock(&cx8802_mutex);
743         list_add_tail(&dev->devlist, &cx8802_devlist);
744         mutex_unlock(&cx8802_mutex);
745
746         /* now autoload cx88-dvb or cx88-blackbird */
747         request_modules(dev);
748         return 0;
749
750  fail_dev:
751         kfree(dev);
752  fail_core:
753         core->dvbdev = NULL;
754         cx88_core_put(core, pci_dev);
755         return err;
756 }
757
758 static void cx8802_remove(struct pci_dev *pci_dev)
759 {
760         struct cx8802_dev *dev;
761
762         dev = pci_get_drvdata(pci_dev);
763
764         dprintk(1, "%s\n", __func__);
765
766         flush_request_modules(dev);
767
768         mutex_lock(&dev->core->lock);
769
770         if (!list_empty(&dev->drvlist)) {
771                 struct cx8802_driver *drv, *tmp;
772                 int err;
773
774                 pr_warn("Trying to remove cx8802 driver while cx8802 sub-drivers still loaded?!\n");
775
776                 list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
777                         err = drv->remove(drv);
778                         if (err == 0) {
779                                 list_del(&drv->drvlist);
780                         } else
781                                 pr_err("cx8802 driver remove failed (%d)\n",
782                                        err);
783                         kfree(drv);
784                 }
785         }
786
787         mutex_unlock(&dev->core->lock);
788
789         /* Destroy any 8802 reference. */
790         dev->core->dvbdev = NULL;
791
792         /* common */
793         cx8802_fini_common(dev);
794         cx88_core_put(dev->core, dev->pci);
795         kfree(dev);
796 }
797
798 static const struct pci_device_id cx8802_pci_tbl[] = {
799         {
800                 .vendor       = 0x14f1,
801                 .device       = 0x8802,
802                 .subvendor    = PCI_ANY_ID,
803                 .subdevice    = PCI_ANY_ID,
804         }, {
805                 /* --- end of list --- */
806         }
807 };
808 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
809
810 static struct pci_driver cx8802_pci_driver = {
811         .name     = "cx88-mpeg driver manager",
812         .id_table = cx8802_pci_tbl,
813         .probe    = cx8802_probe,
814         .remove   = cx8802_remove,
815 };
816
817 module_pci_driver(cx8802_pci_driver);