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