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