1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Support for the mpeg transport stream transfers
5 * PCI function #2 of the cx2388x.
7 * (c) 2004 Jelle Foks <jelle@foks.us>
8 * (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
9 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
22 /* ------------------------------------------------------------------ */
24 MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
25 MODULE_AUTHOR("Jelle Foks <jelle@foks.us>");
26 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
27 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
28 MODULE_LICENSE("GPL");
29 MODULE_VERSION(CX88_VERSION);
31 static unsigned int debug;
32 module_param(debug, int, 0644);
33 MODULE_PARM_DESC(debug, "enable debug messages [mpeg]");
35 #define dprintk(level, fmt, arg...) do { \
36 if (debug + 1 > level) \
37 printk(KERN_DEBUG pr_fmt("%s: mpeg:" fmt), \
41 #if defined(CONFIG_MODULES) && defined(MODULE)
42 static void request_module_async(struct work_struct *work)
44 struct cx8802_dev *dev = container_of(work, struct cx8802_dev,
47 if (dev->core->board.mpeg & CX88_MPEG_DVB)
48 request_module("cx88-dvb");
49 if (dev->core->board.mpeg & CX88_MPEG_BLACKBIRD)
50 request_module("cx88-blackbird");
53 static void request_modules(struct cx8802_dev *dev)
55 INIT_WORK(&dev->request_module_wk, request_module_async);
56 schedule_work(&dev->request_module_wk);
59 static void flush_request_modules(struct cx8802_dev *dev)
61 flush_work(&dev->request_module_wk);
64 #define request_modules(dev)
65 #define flush_request_modules(dev)
66 #endif /* CONFIG_MODULES */
68 static LIST_HEAD(cx8802_devlist);
69 static DEFINE_MUTEX(cx8802_mutex);
70 /* ------------------------------------------------------------------ */
72 int cx8802_start_dma(struct cx8802_dev *dev,
73 struct cx88_dmaqueue *q,
74 struct cx88_buffer *buf)
76 struct cx88_core *core = dev->core;
78 dprintk(1, "w: %d, h: %d, f: %d\n",
79 core->width, core->height, core->field);
81 /* setup fifo + format */
82 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
83 dev->ts_packet_size, buf->risc.dma);
85 /* write TS length to chip */
86 cx_write(MO_TS_LNGTH, dev->ts_packet_size);
89 * FIXME: this needs a review.
90 * also: move to cx88-blackbird + cx88-dvb source files?
93 dprintk(1, "core->active_type_id = 0x%08x\n", core->active_type_id);
95 if ((core->active_type_id == CX88_MPEG_DVB) &&
96 (core->board.mpeg & CX88_MPEG_DVB)) {
97 dprintk(1, "cx8802_start_dma doing .dvb\n");
98 /* negedge driven & software reset */
99 cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
101 cx_write(MO_PINMUX_IO, 0x00);
102 cx_write(TS_HW_SOP_CNTRL, 0x47 << 16 | 188 << 4 | 0x01);
103 switch (core->boardnr) {
104 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
105 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
106 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
107 case CX88_BOARD_PCHDTV_HD5500:
108 cx_write(TS_SOP_STAT, 1 << 13);
110 case CX88_BOARD_SAMSUNG_SMT_7020:
111 cx_write(TS_SOP_STAT, 0x00);
113 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
114 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
115 /* Enable MPEG parallel IO and video signal pins */
116 cx_write(MO_PINMUX_IO, 0x88);
119 case CX88_BOARD_HAUPPAUGE_HVR1300:
120 /* Enable MPEG parallel IO and video signal pins */
121 cx_write(MO_PINMUX_IO, 0x88);
122 cx_write(TS_SOP_STAT, 0);
123 cx_write(TS_VALERR_CNTRL, 0);
125 case CX88_BOARD_PINNACLE_PCTV_HD_800i:
126 /* Enable MPEG parallel IO and video signal pins */
127 cx_write(MO_PINMUX_IO, 0x88);
128 cx_write(TS_HW_SOP_CNTRL, (0x47 << 16) | (188 << 4));
129 dev->ts_gen_cntrl = 5;
130 cx_write(TS_SOP_STAT, 0);
131 cx_write(TS_VALERR_CNTRL, 0);
135 cx_write(TS_SOP_STAT, 0x00);
138 cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
140 } else if ((core->active_type_id == CX88_MPEG_BLACKBIRD) &&
141 (core->board.mpeg & CX88_MPEG_BLACKBIRD)) {
142 dprintk(1, "cx8802_start_dma doing .blackbird\n");
143 cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
145 /* punctured clock TS & posedge driven & software reset */
146 cx_write(TS_GEN_CNTRL, 0x46);
149 cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
150 cx_write(TS_VALERR_CNTRL, 0x2000);
152 /* punctured clock TS & posedge driven */
153 cx_write(TS_GEN_CNTRL, 0x06);
156 pr_err("%s() Failed. Unsupported value in .mpeg (0x%08x)\n",
157 __func__, core->board.mpeg);
162 cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
165 /* clear interrupt status register */
166 cx_write(MO_TS_INTSTAT, 0x1f1111);
169 dprintk(1, "setting the interrupt mask\n");
170 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_TSINT);
171 cx_set(MO_TS_INTMSK, 0x1f0011);
174 cx_set(MO_DEV_CNTRL2, (1 << 5));
175 cx_set(MO_TS_DMACNTRL, 0x11);
178 EXPORT_SYMBOL(cx8802_start_dma);
180 static int cx8802_stop_dma(struct cx8802_dev *dev)
182 struct cx88_core *core = dev->core;
187 cx_clear(MO_TS_DMACNTRL, 0x11);
190 cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT);
191 cx_clear(MO_TS_INTMSK, 0x1f0011);
193 /* Reset the controller */
194 cx_write(TS_GEN_CNTRL, 0xcd);
198 static int cx8802_restart_queue(struct cx8802_dev *dev,
199 struct cx88_dmaqueue *q)
201 struct cx88_buffer *buf;
204 if (list_empty(&q->active))
207 buf = list_entry(q->active.next, struct cx88_buffer, list);
208 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
209 buf, buf->vb.vb2_buf.index);
210 cx8802_start_dma(dev, q, buf);
214 /* ------------------------------------------------------------------ */
216 int cx8802_buf_prepare(struct vb2_queue *q, struct cx8802_dev *dev,
217 struct cx88_buffer *buf)
219 int size = dev->ts_packet_size * dev->ts_packet_count;
220 struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0);
221 struct cx88_riscmem *risc = &buf->risc;
224 if (vb2_plane_size(&buf->vb.vb2_buf, 0) < size)
226 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
228 rc = cx88_risc_databuffer(dev->pci, risc, sgt->sgl,
229 dev->ts_packet_size, dev->ts_packet_count, 0);
232 pci_free_consistent(dev->pci, risc->size,
233 risc->cpu, risc->dma);
234 memset(risc, 0, sizeof(*risc));
239 EXPORT_SYMBOL(cx8802_buf_prepare);
241 void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
243 struct cx88_buffer *prev;
244 struct cx88_dmaqueue *cx88q = &dev->mpegq;
247 /* add jump to start */
248 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
249 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
250 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
252 if (list_empty(&cx88q->active)) {
253 dprintk(1, "queue is empty - first active\n");
254 list_add_tail(&buf->list, &cx88q->active);
255 dprintk(1, "[%p/%d] %s - first active\n",
256 buf, buf->vb.vb2_buf.index, __func__);
259 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
260 dprintk(1, "queue is not empty - append to active\n");
261 prev = list_entry(cx88q->active.prev, struct cx88_buffer, list);
262 list_add_tail(&buf->list, &cx88q->active);
263 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
264 dprintk(1, "[%p/%d] %s - append to active\n",
265 buf, buf->vb.vb2_buf.index, __func__);
268 EXPORT_SYMBOL(cx8802_buf_queue);
270 /* ----------------------------------------------------------- */
272 static void do_cancel_buffers(struct cx8802_dev *dev)
274 struct cx88_dmaqueue *q = &dev->mpegq;
275 struct cx88_buffer *buf;
278 spin_lock_irqsave(&dev->slock, flags);
279 while (!list_empty(&q->active)) {
280 buf = list_entry(q->active.next, struct cx88_buffer, list);
281 list_del(&buf->list);
282 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
284 spin_unlock_irqrestore(&dev->slock, flags);
287 void cx8802_cancel_buffers(struct cx8802_dev *dev)
290 cx8802_stop_dma(dev);
291 do_cancel_buffers(dev);
293 EXPORT_SYMBOL(cx8802_cancel_buffers);
295 static const char *cx88_mpeg_irqs[32] = {
296 "ts_risci1", NULL, NULL, NULL,
297 "ts_risci2", NULL, NULL, NULL,
298 "ts_oflow", NULL, NULL, NULL,
299 "ts_sync", NULL, NULL, NULL,
300 "opc_err", "par_err", "rip_err", "pci_abort",
304 static void cx8802_mpeg_irq(struct cx8802_dev *dev)
306 struct cx88_core *core = dev->core;
307 u32 status, mask, count;
310 status = cx_read(MO_TS_INTSTAT);
311 mask = cx_read(MO_TS_INTMSK);
312 if (0 == (status & mask))
315 cx_write(MO_TS_INTSTAT, status);
317 if (debug || (status & mask & ~0xff))
318 cx88_print_irqbits("irq mpeg ",
319 cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
322 /* risc op code error */
323 if (status & (1 << 16)) {
324 pr_warn("mpeg risc op code error\n");
325 cx_clear(MO_TS_DMACNTRL, 0x11);
326 cx88_sram_channel_dump(dev->core,
327 &cx88_sram_channels[SRAM_CH28]);
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);
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);
348 #define MAX_IRQ_LOOP 10
350 static irqreturn_t cx8802_irq(int irq, void *dev_id)
352 struct cx8802_dev *dev = dev_id;
353 struct cx88_core *core = dev->core;
355 int loop, handled = 0;
357 for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
358 status = cx_read(MO_PCI_INTSTAT) &
359 (core->pci_irqmask | PCI_INT_TSINT);
362 dprintk(1, "cx8802_irq\n");
363 dprintk(1, " loop: %d/%d\n", loop, MAX_IRQ_LOOP);
364 dprintk(1, " status: %d\n", status);
366 cx_write(MO_PCI_INTSTAT, status);
368 if (status & core->pci_irqmask)
369 cx88_core_irq(core, status);
370 if (status & PCI_INT_TSINT)
371 cx8802_mpeg_irq(dev);
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);
380 return IRQ_RETVAL(handled);
383 static int cx8802_init_common(struct cx8802_dev *dev)
385 struct cx88_core *core = dev->core;
389 if (pci_enable_device(dev->pci))
391 pci_set_master(dev->pci);
392 err = pci_set_dma_mask(dev->pci, DMA_BIT_MASK(32));
394 pr_err("Oops: no 32bit PCI DMA ???\n");
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,
403 (unsigned long long)pci_resource_start(dev->pci, 0));
405 /* initialize driver struct */
406 spin_lock_init(&dev->slock);
409 INIT_LIST_HEAD(&dev->mpegq.active);
412 err = request_irq(dev->pci->irq, cx8802_irq,
413 IRQF_SHARED, dev->core->name, dev);
415 pr_err("can't get IRQ %d\n", dev->pci->irq);
418 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
420 /* everything worked */
421 pci_set_drvdata(dev->pci, dev);
425 static void cx8802_fini_common(struct cx8802_dev *dev)
428 cx8802_stop_dma(dev);
429 pci_disable_device(dev->pci);
431 /* unregister stuff */
432 free_irq(dev->pci->irq, dev);
435 /* ----------------------------------------------------------- */
437 static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
439 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
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);
449 spin_unlock_irqrestore(&dev->slock, flags);
451 /* FIXME -- shutdown device */
452 cx88_shutdown(dev->core);
454 pci_save_state(pci_dev);
455 if (pci_set_power_state(pci_dev,
456 pci_choose_state(pci_dev, state)) != 0) {
457 pci_disable_device(pci_dev);
458 dev->state.disabled = 1;
463 static int cx8802_resume_common(struct pci_dev *pci_dev)
465 struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
469 if (dev->state.disabled) {
470 err = pci_enable_device(pci_dev);
472 pr_err("can't enable device\n");
475 dev->state.disabled = 0;
477 err = pci_set_power_state(pci_dev, PCI_D0);
479 pr_err("can't enable device\n");
480 pci_disable_device(pci_dev);
481 dev->state.disabled = 1;
485 pci_restore_state(pci_dev);
487 /* FIXME: re-initialize hardware */
488 cx88_reset(dev->core);
490 /* restart video+vbi capture */
491 spin_lock_irqsave(&dev->slock, flags);
492 if (!list_empty(&dev->mpegq.active)) {
493 pr_info("resume mpeg\n");
494 cx8802_restart_queue(dev, &dev->mpegq);
496 spin_unlock_irqrestore(&dev->slock, flags);
501 struct cx8802_driver *cx8802_get_driver(struct cx8802_dev *dev,
502 enum cx88_board_type btype)
504 struct cx8802_driver *d;
506 list_for_each_entry(d, &dev->drvlist, drvlist)
507 if (d->type_id == btype)
512 EXPORT_SYMBOL(cx8802_get_driver);
514 /* Driver asked for hardware access. */
515 static int cx8802_request_acquire(struct cx8802_driver *drv)
517 struct cx88_core *core = drv->core;
520 /* Fail a request for hardware if the device is busy. */
521 if (core->active_type_id != CX88_BOARD_NONE &&
522 core->active_type_id != drv->type_id)
525 if (drv->type_id == CX88_MPEG_DVB) {
526 /* When switching to DVB, always set the input to the tuner */
527 core->last_analog_input = core->input;
530 i < (sizeof(core->board.input) /
531 sizeof(struct cx88_input));
533 if (core->board.input[i].type == CX88_VMUX_DVB) {
540 if (drv->advise_acquire) {
542 if (core->active_type_id == CX88_BOARD_NONE) {
543 core->active_type_id = drv->type_id;
544 drv->advise_acquire(drv);
547 dprintk(1, "Post acquire GPIO=%x\n", cx_read(MO_GP0_IO));
553 /* Driver asked to release hardware. */
554 static int cx8802_request_release(struct cx8802_driver *drv)
556 struct cx88_core *core = drv->core;
558 if (drv->advise_release && --core->active_ref == 0) {
559 if (drv->type_id == CX88_MPEG_DVB) {
561 * If the DVB driver is releasing, reset the input
562 * state to the last configured analog input
564 core->input = core->last_analog_input;
567 drv->advise_release(drv);
568 core->active_type_id = CX88_BOARD_NONE;
569 dprintk(1, "Post release GPIO=%x\n", cx_read(MO_GP0_IO));
575 static int cx8802_check_driver(struct cx8802_driver *drv)
580 if ((drv->type_id != CX88_MPEG_DVB) &&
581 (drv->type_id != CX88_MPEG_BLACKBIRD))
584 if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
585 (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
590 (!drv->advise_acquire) ||
591 (!drv->advise_release))
597 int cx8802_register_driver(struct cx8802_driver *drv)
599 struct cx8802_dev *dev;
600 struct cx8802_driver *driver;
603 pr_info("registering cx8802 driver, type: %s access: %s\n",
604 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
605 drv->hw_access == CX8802_DRVCTL_SHARED ?
606 "shared" : "exclusive");
608 err = cx8802_check_driver(drv);
610 pr_err("cx8802_driver is invalid\n");
614 mutex_lock(&cx8802_mutex);
616 list_for_each_entry(dev, &cx8802_devlist, devlist) {
617 pr_info("subsystem: %04x:%04x, board: %s [card=%d]\n",
618 dev->pci->subsystem_vendor,
619 dev->pci->subsystem_device, dev->core->board.name,
622 /* Bring up a new struct for each driver instance */
623 driver = kzalloc(sizeof(*drv), GFP_KERNEL);
629 /* Snapshot of the driver registration data */
630 drv->core = dev->core;
631 drv->suspend = cx8802_suspend_common;
632 drv->resume = cx8802_resume_common;
633 drv->request_acquire = cx8802_request_acquire;
634 drv->request_release = cx8802_request_release;
635 memcpy(driver, drv, sizeof(*driver));
637 mutex_lock(&drv->core->lock);
638 err = drv->probe(driver);
641 list_add_tail(&driver->drvlist, &dev->drvlist);
643 pr_err("cx8802 probe failed, err = %d\n", err);
645 mutex_unlock(&drv->core->lock);
648 err = i ? 0 : -ENODEV;
650 mutex_unlock(&cx8802_mutex);
653 EXPORT_SYMBOL(cx8802_register_driver);
655 int cx8802_unregister_driver(struct cx8802_driver *drv)
657 struct cx8802_dev *dev;
658 struct cx8802_driver *d, *dtmp;
661 pr_info("unregistering cx8802 driver, type: %s access: %s\n",
662 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
663 drv->hw_access == CX8802_DRVCTL_SHARED ?
664 "shared" : "exclusive");
666 mutex_lock(&cx8802_mutex);
668 list_for_each_entry(dev, &cx8802_devlist, devlist) {
669 pr_info("subsystem: %04x:%04x, board: %s [card=%d]\n",
670 dev->pci->subsystem_vendor,
671 dev->pci->subsystem_device, dev->core->board.name,
674 mutex_lock(&dev->core->lock);
676 list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) {
677 /* only unregister the correct driver type */
678 if (d->type_id != drv->type_id)
683 list_del(&d->drvlist);
686 pr_err("cx8802 driver remove failed (%d)\n",
690 mutex_unlock(&dev->core->lock);
693 mutex_unlock(&cx8802_mutex);
697 EXPORT_SYMBOL(cx8802_unregister_driver);
699 /* ----------------------------------------------------------- */
700 static int cx8802_probe(struct pci_dev *pci_dev,
701 const struct pci_device_id *pci_id)
703 struct cx8802_dev *dev;
704 struct cx88_core *core;
708 core = cx88_core_get(pci_dev);
712 pr_info("cx2388x 8802 Driver Manager\n");
715 if (!core->board.mpeg)
719 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
725 /* Maintain a reference so cx88-video can query the 8802 device. */
728 err = cx8802_init_common(dev);
732 INIT_LIST_HEAD(&dev->drvlist);
733 mutex_lock(&cx8802_mutex);
734 list_add_tail(&dev->devlist, &cx8802_devlist);
735 mutex_unlock(&cx8802_mutex);
737 /* now autoload cx88-dvb or cx88-blackbird */
738 request_modules(dev);
745 cx88_core_put(core, pci_dev);
749 static void cx8802_remove(struct pci_dev *pci_dev)
751 struct cx8802_dev *dev;
753 dev = pci_get_drvdata(pci_dev);
755 dprintk(1, "%s\n", __func__);
757 flush_request_modules(dev);
759 mutex_lock(&dev->core->lock);
761 if (!list_empty(&dev->drvlist)) {
762 struct cx8802_driver *drv, *tmp;
765 pr_warn("Trying to remove cx8802 driver while cx8802 sub-drivers still loaded?!\n");
767 list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
768 err = drv->remove(drv);
770 list_del(&drv->drvlist);
772 pr_err("cx8802 driver remove failed (%d)\n",
778 mutex_unlock(&dev->core->lock);
780 /* Destroy any 8802 reference. */
781 dev->core->dvbdev = NULL;
784 cx8802_fini_common(dev);
785 cx88_core_put(dev->core, dev->pci);
789 static const struct pci_device_id cx8802_pci_tbl[] = {
793 .subvendor = PCI_ANY_ID,
794 .subdevice = PCI_ANY_ID,
796 /* --- end of list --- */
799 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
801 static struct pci_driver cx8802_pci_driver = {
802 .name = "cx88-mpeg driver manager",
803 .id_table = cx8802_pci_tbl,
804 .probe = cx8802_probe,
805 .remove = cx8802_remove,
808 module_pci_driver(cx8802_pci_driver);