• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Support for the mpeg transport stream transfers
5  *  PCI function #2 of the cx2388x.
6  *
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>
10  */
11 
12 #include "cx88.h"
13 
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>
21 
22 /* ------------------------------------------------------------------ */
23 
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);
30 
31 static unsigned int debug;
32 module_param(debug, int, 0644);
33 MODULE_PARM_DESC(debug, "enable debug messages [mpeg]");
34 
35 #define dprintk(level, fmt, arg...) do {				\
36 	if (debug + 1 > level)						\
37 		printk(KERN_DEBUG pr_fmt("%s: mpeg:" fmt),		\
38 			__func__, ##arg);				\
39 } while (0)
40 
41 #if defined(CONFIG_MODULES) && defined(MODULE)
request_module_async(struct work_struct * work)42 static void request_module_async(struct work_struct *work)
43 {
44 	struct cx8802_dev *dev = container_of(work, struct cx8802_dev,
45 					      request_module_wk);
46 
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");
51 }
52 
request_modules(struct cx8802_dev * dev)53 static void request_modules(struct cx8802_dev *dev)
54 {
55 	INIT_WORK(&dev->request_module_wk, request_module_async);
56 	schedule_work(&dev->request_module_wk);
57 }
58 
flush_request_modules(struct cx8802_dev * dev)59 static void flush_request_modules(struct cx8802_dev *dev)
60 {
61 	flush_work(&dev->request_module_wk);
62 }
63 #else
64 #define request_modules(dev)
65 #define flush_request_modules(dev)
66 #endif /* CONFIG_MODULES */
67 
68 static LIST_HEAD(cx8802_devlist);
69 static DEFINE_MUTEX(cx8802_mutex);
70 /* ------------------------------------------------------------------ */
71 
cx8802_start_dma(struct cx8802_dev * dev,struct cx88_dmaqueue * q,struct cx88_buffer * buf)72 int cx8802_start_dma(struct cx8802_dev    *dev,
73 		     struct cx88_dmaqueue *q,
74 		     struct cx88_buffer   *buf)
75 {
76 	struct cx88_core *core = dev->core;
77 
78 	dprintk(1, "w: %d, h: %d, f: %d\n",
79 		core->width, core->height, core->field);
80 
81 	/* setup fifo + format */
82 	cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
83 				dev->ts_packet_size, buf->risc.dma);
84 
85 	/* write TS length to chip */
86 	cx_write(MO_TS_LNGTH, dev->ts_packet_size);
87 
88 	/*
89 	 * FIXME: this needs a review.
90 	 * also: move to cx88-blackbird + cx88-dvb source files?
91 	 */
92 
93 	dprintk(1, "core->active_type_id = 0x%08x\n", core->active_type_id);
94 
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);
100 		udelay(100);
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);
109 			break;
110 		case CX88_BOARD_SAMSUNG_SMT_7020:
111 			cx_write(TS_SOP_STAT, 0x00);
112 			break;
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);
117 			udelay(100);
118 			break;
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);
124 			break;
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);
132 			udelay(100);
133 			break;
134 		default:
135 			cx_write(TS_SOP_STAT, 0x00);
136 			break;
137 		}
138 		cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
139 		udelay(100);
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 */
144 
145 		/* punctured clock TS & posedge driven & software reset */
146 		cx_write(TS_GEN_CNTRL, 0x46);
147 		udelay(100);
148 
149 		cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
150 		cx_write(TS_VALERR_CNTRL, 0x2000);
151 
152 		/* punctured clock TS & posedge driven */
153 		cx_write(TS_GEN_CNTRL, 0x06);
154 		udelay(100);
155 	} else {
156 		pr_err("%s() Failed. Unsupported value in .mpeg (0x%08x)\n",
157 		       __func__, core->board.mpeg);
158 		return -EINVAL;
159 	}
160 
161 	/* reset counter */
162 	cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
163 	q->count = 0;
164 
165 	/* clear interrupt status register */
166 	cx_write(MO_TS_INTSTAT,  0x1f1111);
167 
168 	/* enable irqs */
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);
172 
173 	/* start dma */
174 	cx_set(MO_DEV_CNTRL2, (1 << 5));
175 	cx_set(MO_TS_DMACNTRL, 0x11);
176 	return 0;
177 }
178 EXPORT_SYMBOL(cx8802_start_dma);
179 
cx8802_stop_dma(struct cx8802_dev * dev)180 static int cx8802_stop_dma(struct cx8802_dev *dev)
181 {
182 	struct cx88_core *core = dev->core;
183 
184 	dprintk(1, "\n");
185 
186 	/* stop dma */
187 	cx_clear(MO_TS_DMACNTRL, 0x11);
188 
189 	/* disable irqs */
190 	cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT);
191 	cx_clear(MO_TS_INTMSK, 0x1f0011);
192 
193 	/* Reset the controller */
194 	cx_write(TS_GEN_CNTRL, 0xcd);
195 	return 0;
196 }
197 
cx8802_restart_queue(struct cx8802_dev * dev,struct cx88_dmaqueue * q)198 static int cx8802_restart_queue(struct cx8802_dev    *dev,
199 				struct cx88_dmaqueue *q)
200 {
201 	struct cx88_buffer *buf;
202 
203 	dprintk(1, "\n");
204 	if (list_empty(&q->active))
205 		return 0;
206 
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);
211 	return 0;
212 }
213 
214 /* ------------------------------------------------------------------ */
215 
cx8802_buf_prepare(struct vb2_queue * q,struct cx8802_dev * dev,struct cx88_buffer * buf)216 int cx8802_buf_prepare(struct vb2_queue *q, struct cx8802_dev *dev,
217 		       struct cx88_buffer *buf)
218 {
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;
222 	int rc;
223 
224 	if (vb2_plane_size(&buf->vb.vb2_buf, 0) < size)
225 		return -EINVAL;
226 	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
227 
228 	rc = cx88_risc_databuffer(dev->pci, risc, sgt->sgl,
229 				  dev->ts_packet_size, dev->ts_packet_count, 0);
230 	if (rc) {
231 		if (risc->cpu)
232 			pci_free_consistent(dev->pci, risc->size,
233 					    risc->cpu, risc->dma);
234 		memset(risc, 0, sizeof(*risc));
235 		return rc;
236 	}
237 	return 0;
238 }
239 EXPORT_SYMBOL(cx8802_buf_prepare);
240 
cx8802_buf_queue(struct cx8802_dev * dev,struct cx88_buffer * buf)241 void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
242 {
243 	struct cx88_buffer    *prev;
244 	struct cx88_dmaqueue  *cx88q = &dev->mpegq;
245 
246 	dprintk(1, "\n");
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);
251 
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__);
257 
258 	} else {
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__);
266 	}
267 }
268 EXPORT_SYMBOL(cx8802_buf_queue);
269 
270 /* ----------------------------------------------------------- */
271 
do_cancel_buffers(struct cx8802_dev * dev)272 static void do_cancel_buffers(struct cx8802_dev *dev)
273 {
274 	struct cx88_dmaqueue *q = &dev->mpegq;
275 	struct cx88_buffer *buf;
276 	unsigned long flags;
277 
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);
283 	}
284 	spin_unlock_irqrestore(&dev->slock, flags);
285 }
286 
cx8802_cancel_buffers(struct cx8802_dev * dev)287 void cx8802_cancel_buffers(struct cx8802_dev *dev)
288 {
289 	dprintk(1, "\n");
290 	cx8802_stop_dma(dev);
291 	do_cancel_buffers(dev);
292 }
293 EXPORT_SYMBOL(cx8802_cancel_buffers);
294 
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",
301 	"ts_err?",
302 };
303 
cx8802_mpeg_irq(struct cx8802_dev * dev)304 static void cx8802_mpeg_irq(struct cx8802_dev *dev)
305 {
306 	struct cx88_core *core = dev->core;
307 	u32 status, mask, count;
308 
309 	dprintk(1, "\n");
310 	status = cx_read(MO_TS_INTSTAT);
311 	mask   = cx_read(MO_TS_INTMSK);
312 	if (0 == (status & mask))
313 		return;
314 
315 	cx_write(MO_TS_INTSTAT, status);
316 
317 	if (debug || (status & mask & ~0xff))
318 		cx88_print_irqbits("irq mpeg ",
319 				   cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
320 				   status, mask);
321 
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]);
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 
cx8802_irq(int irq,void * dev_id)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 
cx8802_init_common(struct cx8802_dev * dev)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 
cx8802_fini_common(struct cx8802_dev * dev)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 
cx8802_suspend_common(struct pci_dev * pci_dev,pm_message_t state)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,
456 				pci_choose_state(pci_dev, state)) != 0) {
457 		pci_disable_device(pci_dev);
458 		dev->state.disabled = 1;
459 	}
460 	return 0;
461 }
462 
cx8802_resume_common(struct pci_dev * pci_dev)463 static int cx8802_resume_common(struct pci_dev *pci_dev)
464 {
465 	struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
466 	unsigned long flags;
467 	int err;
468 
469 	if (dev->state.disabled) {
470 		err = pci_enable_device(pci_dev);
471 		if (err) {
472 			pr_err("can't enable device\n");
473 			return err;
474 		}
475 		dev->state.disabled = 0;
476 	}
477 	err = pci_set_power_state(pci_dev, PCI_D0);
478 	if (err) {
479 		pr_err("can't enable device\n");
480 		pci_disable_device(pci_dev);
481 		dev->state.disabled = 1;
482 
483 		return err;
484 	}
485 	pci_restore_state(pci_dev);
486 
487 	/* FIXME: re-initialize hardware */
488 	cx88_reset(dev->core);
489 
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);
495 	}
496 	spin_unlock_irqrestore(&dev->slock, flags);
497 
498 	return 0;
499 }
500 
cx8802_get_driver(struct cx8802_dev * dev,enum cx88_board_type btype)501 struct cx8802_driver *cx8802_get_driver(struct cx8802_dev *dev,
502 					enum cx88_board_type btype)
503 {
504 	struct cx8802_driver *d;
505 
506 	list_for_each_entry(d, &dev->drvlist, drvlist)
507 		if (d->type_id == btype)
508 			return d;
509 
510 	return NULL;
511 }
512 EXPORT_SYMBOL(cx8802_get_driver);
513 
514 /* Driver asked for hardware access. */
cx8802_request_acquire(struct cx8802_driver * drv)515 static int cx8802_request_acquire(struct cx8802_driver *drv)
516 {
517 	struct cx88_core *core = drv->core;
518 	unsigned int	i;
519 
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)
523 		return -EBUSY;
524 
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;
528 		core->input = 0;
529 		for (i = 0;
530 		     i < (sizeof(core->board.input) /
531 			  sizeof(struct cx88_input));
532 		     i++) {
533 			if (core->board.input[i].type == CX88_VMUX_DVB) {
534 				core->input = i;
535 				break;
536 			}
537 		}
538 	}
539 
540 	if (drv->advise_acquire) {
541 		core->active_ref++;
542 		if (core->active_type_id == CX88_BOARD_NONE) {
543 			core->active_type_id = drv->type_id;
544 			drv->advise_acquire(drv);
545 		}
546 
547 		dprintk(1, "Post acquire GPIO=%x\n", cx_read(MO_GP0_IO));
548 	}
549 
550 	return 0;
551 }
552 
553 /* Driver asked to release hardware. */
cx8802_request_release(struct cx8802_driver * drv)554 static int cx8802_request_release(struct cx8802_driver *drv)
555 {
556 	struct cx88_core *core = drv->core;
557 
558 	if (drv->advise_release && --core->active_ref == 0) {
559 		if (drv->type_id == CX88_MPEG_DVB) {
560 			/*
561 			 * If the DVB driver is releasing, reset the input
562 			 * state to the last configured analog input
563 			 */
564 			core->input = core->last_analog_input;
565 		}
566 
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));
570 	}
571 
572 	return 0;
573 }
574 
cx8802_check_driver(struct cx8802_driver * drv)575 static int cx8802_check_driver(struct cx8802_driver *drv)
576 {
577 	if (!drv)
578 		return -ENODEV;
579 
580 	if ((drv->type_id != CX88_MPEG_DVB) &&
581 	    (drv->type_id != CX88_MPEG_BLACKBIRD))
582 		return -EINVAL;
583 
584 	if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
585 	    (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
586 		return -EINVAL;
587 
588 	if ((!drv->probe) ||
589 	    (!drv->remove) ||
590 	    (!drv->advise_acquire) ||
591 	    (!drv->advise_release))
592 		return -EINVAL;
593 
594 	return 0;
595 }
596 
cx8802_register_driver(struct cx8802_driver * drv)597 int cx8802_register_driver(struct cx8802_driver *drv)
598 {
599 	struct cx8802_dev *dev;
600 	struct cx8802_driver *driver;
601 	int err, i = 0;
602 
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");
607 
608 	err = cx8802_check_driver(drv);
609 	if (err) {
610 		pr_err("cx8802_driver is invalid\n");
611 		return err;
612 	}
613 
614 	mutex_lock(&cx8802_mutex);
615 
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,
620 			dev->core->boardnr);
621 
622 		/* Bring up a new struct for each driver instance */
623 		driver = kzalloc(sizeof(*drv), GFP_KERNEL);
624 		if (!driver) {
625 			err = -ENOMEM;
626 			goto out;
627 		}
628 
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));
636 
637 		mutex_lock(&drv->core->lock);
638 		err = drv->probe(driver);
639 		if (err == 0) {
640 			i++;
641 			list_add_tail(&driver->drvlist, &dev->drvlist);
642 		} else {
643 			pr_err("cx8802 probe failed, err = %d\n", err);
644 		}
645 		mutex_unlock(&drv->core->lock);
646 	}
647 
648 	err = i ? 0 : -ENODEV;
649 out:
650 	mutex_unlock(&cx8802_mutex);
651 	return err;
652 }
653 EXPORT_SYMBOL(cx8802_register_driver);
654 
cx8802_unregister_driver(struct cx8802_driver * drv)655 int cx8802_unregister_driver(struct cx8802_driver *drv)
656 {
657 	struct cx8802_dev *dev;
658 	struct cx8802_driver *d, *dtmp;
659 	int err = 0;
660 
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");
665 
666 	mutex_lock(&cx8802_mutex);
667 
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,
672 			dev->core->boardnr);
673 
674 		mutex_lock(&dev->core->lock);
675 
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)
679 				continue;
680 
681 			err = d->remove(d);
682 			if (err == 0) {
683 				list_del(&d->drvlist);
684 				kfree(d);
685 			} else
686 				pr_err("cx8802 driver remove failed (%d)\n",
687 				       err);
688 		}
689 
690 		mutex_unlock(&dev->core->lock);
691 	}
692 
693 	mutex_unlock(&cx8802_mutex);
694 
695 	return err;
696 }
697 EXPORT_SYMBOL(cx8802_unregister_driver);
698 
699 /* ----------------------------------------------------------- */
cx8802_probe(struct pci_dev * pci_dev,const struct pci_device_id * pci_id)700 static int cx8802_probe(struct pci_dev *pci_dev,
701 			const struct pci_device_id *pci_id)
702 {
703 	struct cx8802_dev *dev;
704 	struct cx88_core  *core;
705 	int err;
706 
707 	/* general setup */
708 	core = cx88_core_get(pci_dev);
709 	if (!core)
710 		return -EINVAL;
711 
712 	pr_info("cx2388x 8802 Driver Manager\n");
713 
714 	err = -ENODEV;
715 	if (!core->board.mpeg)
716 		goto fail_core;
717 
718 	err = -ENOMEM;
719 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
720 	if (!dev)
721 		goto fail_core;
722 	dev->pci = pci_dev;
723 	dev->core = core;
724 
725 	/* Maintain a reference so cx88-video can query the 8802 device. */
726 	core->dvbdev = dev;
727 
728 	err = cx8802_init_common(dev);
729 	if (err != 0)
730 		goto fail_dev;
731 
732 	INIT_LIST_HEAD(&dev->drvlist);
733 	mutex_lock(&cx8802_mutex);
734 	list_add_tail(&dev->devlist, &cx8802_devlist);
735 	mutex_unlock(&cx8802_mutex);
736 
737 	/* now autoload cx88-dvb or cx88-blackbird */
738 	request_modules(dev);
739 	return 0;
740 
741  fail_dev:
742 	kfree(dev);
743  fail_core:
744 	core->dvbdev = NULL;
745 	cx88_core_put(core, pci_dev);
746 	return err;
747 }
748 
cx8802_remove(struct pci_dev * pci_dev)749 static void cx8802_remove(struct pci_dev *pci_dev)
750 {
751 	struct cx8802_dev *dev;
752 
753 	dev = pci_get_drvdata(pci_dev);
754 
755 	dprintk(1, "%s\n", __func__);
756 
757 	flush_request_modules(dev);
758 
759 	mutex_lock(&dev->core->lock);
760 
761 	if (!list_empty(&dev->drvlist)) {
762 		struct cx8802_driver *drv, *tmp;
763 		int err;
764 
765 		pr_warn("Trying to remove cx8802 driver while cx8802 sub-drivers still loaded?!\n");
766 
767 		list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
768 			err = drv->remove(drv);
769 			if (err == 0) {
770 				list_del(&drv->drvlist);
771 			} else
772 				pr_err("cx8802 driver remove failed (%d)\n",
773 				       err);
774 			kfree(drv);
775 		}
776 	}
777 
778 	mutex_unlock(&dev->core->lock);
779 
780 	/* Destroy any 8802 reference. */
781 	dev->core->dvbdev = NULL;
782 
783 	/* common */
784 	cx8802_fini_common(dev);
785 	cx88_core_put(dev->core, dev->pci);
786 	kfree(dev);
787 }
788 
789 static const struct pci_device_id cx8802_pci_tbl[] = {
790 	{
791 		.vendor       = 0x14f1,
792 		.device       = 0x8802,
793 		.subvendor    = PCI_ANY_ID,
794 		.subdevice    = PCI_ANY_ID,
795 	}, {
796 		/* --- end of list --- */
797 	}
798 };
799 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
800 
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,
806 };
807 
808 module_pci_driver(cx8802_pci_driver);
809