cx88-mpeg.c 21.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 *
 *  Support for the mpeg transport stream transfers
 *  PCI function #2 of the cx2388x.
 *
J
Jelle Foks 已提交
6
 *    (c) 2004 Jelle Foks <jelle@foks.us>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *    (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
 *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
26
#include <linux/slab.h>
L
Linus Torvalds 已提交
27 28
#include <linux/init.h>
#include <linux/device.h>
A
Andrew Morton 已提交
29
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37
#include <linux/interrupt.h>
#include <asm/delay.h>

#include "cx88.h"

/* ------------------------------------------------------------------ */

MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
J
Jelle Foks 已提交
38
MODULE_AUTHOR("Jelle Foks <jelle@foks.us>");
L
Linus Torvalds 已提交
39 40 41
MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
MODULE_LICENSE("GPL");
42
MODULE_VERSION(CX88_VERSION);
L
Linus Torvalds 已提交
43

44
static unsigned int debug;
L
Linus Torvalds 已提交
45 46 47
module_param(debug,int,0644);
MODULE_PARM_DESC(debug,"enable debug messages [mpeg]");

48 49 50 51
#define dprintk(level, fmt, arg...) do {				       \
	if (debug + 1 > level)						       \
		printk(KERN_DEBUG "%s/2-mpeg: " fmt, dev->core->name, ## arg); \
} while(0)
L
Linus Torvalds 已提交
52

53 54 55 56
#define mpeg_dbg(level, fmt, arg...) do {				  \
	if (debug + 1 > level)						  \
		printk(KERN_DEBUG "%s/2-mpeg: " fmt, core->name, ## arg); \
} while(0)
57

58 59 60 61
#if defined(CONFIG_MODULES) && defined(MODULE)
static void request_module_async(struct work_struct *work)
{
	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 70 71 72 73
}

static void request_modules(struct cx8802_dev *dev)
{
	INIT_WORK(&dev->request_module_wk, request_module_async);
	schedule_work(&dev->request_module_wk);
}
74 75 76

static void flush_request_modules(struct cx8802_dev *dev)
{
77
	flush_work(&dev->request_module_wk);
78
}
79 80
#else
#define request_modules(dev)
81
#define flush_request_modules(dev)
82 83 84
#endif /* CONFIG_MODULES */


85
static LIST_HEAD(cx8802_devlist);
86
static DEFINE_MUTEX(cx8802_mutex);
L
Linus Torvalds 已提交
87 88
/* ------------------------------------------------------------------ */

H
Hans Verkuil 已提交
89
int cx8802_start_dma(struct cx8802_dev    *dev,
L
Linus Torvalds 已提交
90 91 92 93 94
			    struct cx88_dmaqueue *q,
			    struct cx88_buffer   *buf)
{
	struct cx88_core *core = dev->core;

95
	dprintk(1, "cx8802_start_dma w: %d, h: %d, f: %d\n",
H
Hans Verkuil 已提交
96
		dev->width, dev->height, dev->field);
L
Linus Torvalds 已提交
97 98 99 100 101 102

	/* setup fifo + format */
	cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
				dev->ts_packet_size, buf->risc.dma);

	/* write TS length to chip */
H
Hans Verkuil 已提交
103
	cx_write(MO_TS_LNGTH, dev->ts_packet_size);
L
Linus Torvalds 已提交
104 105 106 107

	/* FIXME: this needs a review.
	 * also: move to cx88-blackbird + cx88-dvb source files? */

108 109 110
	dprintk( 1, "core->active_type_id = 0x%08x\n", core->active_type_id);

	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");
L
Linus Torvalds 已提交
114
		/* negedge driven & software reset */
115
		cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
L
Linus Torvalds 已提交
116 117
		udelay(100);
		cx_write(MO_PINMUX_IO, 0x00);
118
		cx_write(TS_HW_SOP_CNTRL, 0x47<<16|188<<4|0x01);
119
		switch (core->boardnr) {
120 121 122
		case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
		case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
		case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
123
		case CX88_BOARD_PCHDTV_HD5500:
M
Mauro Carvalho Chehab 已提交
124
			cx_write(TS_SOP_STAT, 1<<13);
125
			break;
126 127 128
		case CX88_BOARD_SAMSUNG_SMT_7020:
			cx_write(TS_SOP_STAT, 0x00);
			break;
129 130 131 132 133
		case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
		case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
			cx_write(MO_PINMUX_IO, 0x88); /* Enable MPEG parallel IO and video signal pins */
			udelay(100);
			break;
134
		case CX88_BOARD_HAUPPAUGE_HVR1300:
135 136 137 138
			/* Enable MPEG parallel IO and video signal pins */
			cx_write(MO_PINMUX_IO, 0x88);
			cx_write(TS_SOP_STAT, 0);
			cx_write(TS_VALERR_CNTRL, 0);
139
			break;
140
		case CX88_BOARD_PINNACLE_PCTV_HD_800i:
141 142 143 144 145 146
			/* Enable MPEG parallel IO and video signal pins */
			cx_write(MO_PINMUX_IO, 0x88);
			cx_write(TS_HW_SOP_CNTRL, (0x47 << 16) | (188 << 4));
			dev->ts_gen_cntrl = 5;
			cx_write(TS_SOP_STAT, 0);
			cx_write(TS_VALERR_CNTRL, 0);
147 148
			udelay(100);
			break;
149
		default:
M
Mauro Carvalho Chehab 已提交
150
			cx_write(TS_SOP_STAT, 0x00);
151
			break;
152
		}
L
Linus Torvalds 已提交
153 154
		cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
		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");
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167
		cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */

		cx_write(TS_GEN_CNTRL, 0x46); /* punctured clock TS & posedge driven & software reset */
		udelay(100);

		cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
		cx_write(TS_VALERR_CNTRL, 0x2000);

		cx_write(TS_GEN_CNTRL, 0x06); /* punctured clock TS & posedge driven */
		udelay(100);
168
	} else {
169
		printk( "%s() Failed. Unsupported value in .mpeg (0x%08x)\n", __func__,
170
			core->board.mpeg );
171
		return -EINVAL;
L
Linus Torvalds 已提交
172 173 174 175 176 177 178
	}

	/* reset counter */
	cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
	q->count = 1;

	/* 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);
L
Linus Torvalds 已提交
182 183

	/* start dma */
184 185
	cx_set(MO_DEV_CNTRL2, (1<<5));
	cx_set(MO_TS_DMACNTRL, 0x11);
L
Linus Torvalds 已提交
186 187 188 189 190 191
	return 0;
}

static int cx8802_stop_dma(struct cx8802_dev *dev)
{
	struct cx88_core *core = dev->core;
192
	dprintk( 1, "cx8802_stop_dma\n" );
L
Linus Torvalds 已提交
193 194 195 196 197

	/* stop dma */
	cx_clear(MO_TS_DMACNTRL, 0x11);

	/* disable irqs */
198
	cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT);
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207 208 209 210
	cx_clear(MO_TS_INTMSK, 0x1f0011);

	/* Reset the controller */
	cx_write(TS_GEN_CNTRL, 0xcd);
	return 0;
}

static int cx8802_restart_queue(struct cx8802_dev    *dev,
				struct cx88_dmaqueue *q)
{
	struct cx88_buffer *buf;

211
	dprintk( 1, "cx8802_restart_queue\n" );
L
Linus Torvalds 已提交
212 213 214
	if (list_empty(&q->active))
		return 0;

H
Hans Verkuil 已提交
215
	buf = list_entry(q->active.next, struct cx88_buffer, list);
L
Linus Torvalds 已提交
216
	dprintk(2,"restart_queue [%p/%d]: restart dma\n",
H
Hans Verkuil 已提交
217
		buf, buf->vb.v4l2_buf.index);
L
Linus Torvalds 已提交
218
	cx8802_start_dma(dev, q, buf);
H
Hans Verkuil 已提交
219
	list_for_each_entry(buf, &q->active, list)
L
Linus Torvalds 已提交
220 221 222 223 224 225
		buf->count = q->count++;
	return 0;
}

/* ------------------------------------------------------------------ */

H
Hans Verkuil 已提交
226
int cx8802_buf_prepare(struct vb2_queue *q, struct cx8802_dev *dev,
227
			struct cx88_buffer *buf, enum v4l2_field field)
L
Linus Torvalds 已提交
228 229
{
	int size = dev->ts_packet_size * dev->ts_packet_count;
H
Hans Verkuil 已提交
230
	struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vb, 0);
L
Linus Torvalds 已提交
231 232
	int rc;

H
Hans Verkuil 已提交
233
	if (vb2_plane_size(&buf->vb, 0) < size)
L
Linus Torvalds 已提交
234
		return -EINVAL;
H
Hans Verkuil 已提交
235
	vb2_set_plane_payload(&buf->vb, 0, size);
L
Linus Torvalds 已提交
236

H
Hans Verkuil 已提交
237 238 239
	rc = dma_map_sg(&dev->pci->dev, sgt->sgl, sgt->nents, DMA_FROM_DEVICE);
	if (!rc)
		return -EIO;
L
Linus Torvalds 已提交
240

H
Hans Verkuil 已提交
241
	rc = cx88_risc_databuffer(dev->pci, &buf->risc, sgt->sgl,
H
Hans Verkuil 已提交
242
			     dev->ts_packet_size, dev->ts_packet_count, 0);
H
Hans Verkuil 已提交
243 244 245 246
	if (rc) {
		btcx_riscmem_free(dev->pci, &buf->risc);
		return rc;
	}
H
Hans Verkuil 已提交
247
	return 0;
L
Linus Torvalds 已提交
248 249 250 251 252
}

void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
{
	struct cx88_buffer    *prev;
253
	struct cx88_dmaqueue  *cx88q = &dev->mpegq;
L
Linus Torvalds 已提交
254

255
	dprintk( 1, "cx8802_buf_queue\n" );
H
Hans Verkuil 已提交
256 257 258 259
	/* add jump to start */
	buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
	buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
L
Linus Torvalds 已提交
260

261
	if (list_empty(&cx88q->active)) {
262
		dprintk( 1, "queue is empty - first active\n" );
H
Hans Verkuil 已提交
263
		list_add_tail(&buf->list, &cx88q->active);
264
		buf->count    = cx88q->count++;
265
		dprintk(1,"[%p/%d] %s - first active\n",
H
Hans Verkuil 已提交
266
			buf, buf->vb.v4l2_buf.index, __func__);
L
Linus Torvalds 已提交
267 268

	} else {
H
Hans Verkuil 已提交
269
		buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
270
		dprintk( 1, "queue is not empty - append to active\n" );
H
Hans Verkuil 已提交
271 272
		prev = list_entry(cx88q->active.prev, struct cx88_buffer, list);
		list_add_tail(&buf->list, &cx88q->active);
273
		buf->count    = cx88q->count++;
L
Linus Torvalds 已提交
274
		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
275
		dprintk( 1, "[%p/%d] %s - append to active\n",
H
Hans Verkuil 已提交
276
			buf, buf->vb.v4l2_buf.index, __func__);
L
Linus Torvalds 已提交
277 278 279 280 281
	}
}

/* ----------------------------------------------------------- */

H
Hans Verkuil 已提交
282
static void do_cancel_buffers(struct cx8802_dev *dev)
L
Linus Torvalds 已提交
283 284 285 286 287 288 289
{
	struct cx88_dmaqueue *q = &dev->mpegq;
	struct cx88_buffer *buf;
	unsigned long flags;

	spin_lock_irqsave(&dev->slock,flags);
	while (!list_empty(&q->active)) {
H
Hans Verkuil 已提交
290 291 292
		buf = list_entry(q->active.next, struct cx88_buffer, list);
		list_del(&buf->list);
		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
293
	}
L
Linus Torvalds 已提交
294 295 296 297 298
	spin_unlock_irqrestore(&dev->slock,flags);
}

void cx8802_cancel_buffers(struct cx8802_dev *dev)
{
299
	dprintk( 1, "cx8802_cancel_buffers" );
L
Linus Torvalds 已提交
300
	cx8802_stop_dma(dev);
H
Hans Verkuil 已提交
301
	do_cancel_buffers(dev);
L
Linus Torvalds 已提交
302 303
}

304
static const char * cx88_mpeg_irqs[32] = {
M
Mauro Carvalho Chehab 已提交
305 306 307 308 309 310 311 312
	"ts_risci1", NULL, NULL, NULL,
	"ts_risci2", NULL, NULL, NULL,
	"ts_oflow",  NULL, NULL, NULL,
	"ts_sync",   NULL, NULL, NULL,
	"opc_err", "par_err", "rip_err", "pci_abort",
	"ts_err?",
};

L
Linus Torvalds 已提交
313 314 315 316 317
static void cx8802_mpeg_irq(struct cx8802_dev *dev)
{
	struct cx88_core *core = dev->core;
	u32 status, mask, count;

318
	dprintk( 1, "cx8802_mpeg_irq\n" );
L
Linus Torvalds 已提交
319 320 321 322 323 324
	status = cx_read(MO_TS_INTSTAT);
	mask   = cx_read(MO_TS_INTMSK);
	if (0 == (status & mask))
		return;

	cx_write(MO_TS_INTSTAT, status);
M
Mauro Carvalho Chehab 已提交
325

L
Linus Torvalds 已提交
326 327
	if (debug || (status & mask & ~0xff))
		cx88_print_irqbits(core->name, "irq mpeg ",
328 329
				   cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
				   status, mask);
L
Linus Torvalds 已提交
330 331 332 333 334 335 336 337 338 339

	/* risc op code error */
	if (status & (1 << 16)) {
		printk(KERN_WARNING "%s: mpeg risc op code error\n",core->name);
		cx_clear(MO_TS_DMACNTRL, 0x11);
		cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
	}

	/* risc1 y */
	if (status & 0x01) {
340
		dprintk( 1, "wake up\n" );
L
Linus Torvalds 已提交
341 342 343 344 345 346
		spin_lock(&dev->slock);
		count = cx_read(MO_TS_GPCNT);
		cx88_wakeup(dev->core, &dev->mpegq, count);
		spin_unlock(&dev->slock);
	}

347 348
	/* other general errors */
	if (status & 0x1f0100) {
349
		dprintk( 0, "general errors: 0x%08x\n", status & 0x1f0100 );
350
		spin_lock(&dev->slock);
L
Linus Torvalds 已提交
351
		cx8802_stop_dma(dev);
352 353
		spin_unlock(&dev->slock);
	}
L
Linus Torvalds 已提交
354 355
}

356 357
#define MAX_IRQ_LOOP 10

358
static irqreturn_t cx8802_irq(int irq, void *dev_id)
L
Linus Torvalds 已提交
359 360 361 362 363 364
{
	struct cx8802_dev *dev = dev_id;
	struct cx88_core *core = dev->core;
	u32 status;
	int loop, handled = 0;

365
	for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
366 367
		status = cx_read(MO_PCI_INTSTAT) &
			(core->pci_irqmask | PCI_INT_TSINT);
L
Linus Torvalds 已提交
368 369
		if (0 == status)
			goto out;
370 371 372
		dprintk( 1, "cx8802_irq\n" );
		dprintk( 1, "    loop: %d/%d\n", loop, MAX_IRQ_LOOP );
		dprintk( 1, "    status: %d\n", status );
L
Linus Torvalds 已提交
373 374 375 376 377
		handled = 1;
		cx_write(MO_PCI_INTSTAT, status);

		if (status & core->pci_irqmask)
			cx88_core_irq(core,status);
378
		if (status & PCI_INT_TSINT)
L
Linus Torvalds 已提交
379
			cx8802_mpeg_irq(dev);
380
	}
381 382
	if (MAX_IRQ_LOOP == loop) {
		dprintk( 0, "clearing mask\n" );
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391
		printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
		       core->name);
		cx_write(MO_PCI_INTMSK,0);
	}

 out:
	return IRQ_RETVAL(handled);
}

392
static int cx8802_init_common(struct cx8802_dev *dev)
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400
{
	struct cx88_core *core = dev->core;
	int err;

	/* pci init */
	if (pci_enable_device(dev->pci))
		return -EIO;
	pci_set_master(dev->pci);
401
	if (!pci_dma_supported(dev->pci,DMA_BIT_MASK(32))) {
L
Linus Torvalds 已提交
402 403 404 405
		printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
		return -EIO;
	}

B
Bjørn Mork 已提交
406
	dev->pci_rev = dev->pci->revision;
407 408
	pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER,  &dev->pci_lat);
	printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
409
	       "latency: %d, mmio: 0x%llx\n", dev->core->name,
L
Linus Torvalds 已提交
410
	       pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
411
	       dev->pci_lat,(unsigned long long)pci_resource_start(dev->pci,0));
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420

	/* initialize driver struct */
	spin_lock_init(&dev->slock);

	/* init dma queue */
	INIT_LIST_HEAD(&dev->mpegq.active);

	/* get irq */
	err = request_irq(dev->pci->irq, cx8802_irq,
421
			  IRQF_SHARED, dev->core->name, dev);
L
Linus Torvalds 已提交
422 423 424 425 426 427 428 429 430 431 432 433
	if (err < 0) {
		printk(KERN_ERR "%s: can't get IRQ %d\n",
		       dev->core->name, dev->pci->irq);
		return err;
	}
	cx_set(MO_PCI_INTMSK, core->pci_irqmask);

	/* everything worked */
	pci_set_drvdata(dev->pci,dev);
	return 0;
}

434
static void cx8802_fini_common(struct cx8802_dev *dev)
L
Linus Torvalds 已提交
435
{
436
	dprintk( 2, "cx8802_fini_common\n" );
L
Linus Torvalds 已提交
437 438 439 440 441 442 443 444 445
	cx8802_stop_dma(dev);
	pci_disable_device(dev->pci);

	/* unregister stuff */
	free_irq(dev->pci->irq, dev);
}

/* ----------------------------------------------------------- */

446
static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
L
Linus Torvalds 已提交
447
{
448
	struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
L
Linus Torvalds 已提交
449
	struct cx88_core *core = dev->core;
450
	unsigned long flags;
L
Linus Torvalds 已提交
451 452

	/* stop mpeg dma */
453
	spin_lock_irqsave(&dev->slock, flags);
L
Linus Torvalds 已提交
454
	if (!list_empty(&dev->mpegq.active)) {
455
		dprintk( 2, "suspend\n" );
L
Linus Torvalds 已提交
456 457 458
		printk("%s: suspend mpeg\n", core->name);
		cx8802_stop_dma(dev);
	}
459
	spin_unlock_irqrestore(&dev->slock, flags);
L
Linus Torvalds 已提交
460 461 462 463 464 465 466 467 468 469 470 471

	/* FIXME -- shutdown device */
	cx88_shutdown(dev->core);

	pci_save_state(pci_dev);
	if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
		pci_disable_device(pci_dev);
		dev->state.disabled = 1;
	}
	return 0;
}

472
static int cx8802_resume_common(struct pci_dev *pci_dev)
L
Linus Torvalds 已提交
473
{
474
	struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
L
Linus Torvalds 已提交
475
	struct cx88_core *core = dev->core;
476
	unsigned long flags;
477
	int err;
L
Linus Torvalds 已提交
478 479

	if (dev->state.disabled) {
480 481 482 483 484 485
		err=pci_enable_device(pci_dev);
		if (err) {
			printk(KERN_ERR "%s: can't enable device\n",
					       dev->core->name);
			return err;
		}
L
Linus Torvalds 已提交
486 487
		dev->state.disabled = 0;
	}
488 489 490 491 492 493 494 495 496
	err=pci_set_power_state(pci_dev, PCI_D0);
	if (err) {
		printk(KERN_ERR "%s: can't enable device\n",
					       dev->core->name);
		pci_disable_device(pci_dev);
		dev->state.disabled = 1;

		return err;
	}
L
Linus Torvalds 已提交
497 498 499 500 501 502
	pci_restore_state(pci_dev);

	/* FIXME: re-initialize hardware */
	cx88_reset(dev->core);

	/* restart video+vbi capture */
503
	spin_lock_irqsave(&dev->slock, flags);
L
Linus Torvalds 已提交
504 505 506 507
	if (!list_empty(&dev->mpegq.active)) {
		printk("%s: resume mpeg\n", core->name);
		cx8802_restart_queue(dev,&dev->mpegq);
	}
508
	spin_unlock_irqrestore(&dev->slock, flags);
L
Linus Torvalds 已提交
509 510 511 512

	return 0;
}

513 514
struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype)
{
515
	struct cx8802_driver *d;
516

517
	list_for_each_entry(d, &dev->drvlist, drvlist)
518 519
		if (d->type_id == btype)
			return d;
520 521 522 523 524

	return NULL;
}

/* Driver asked for hardware access. */
525
static int cx8802_request_acquire(struct cx8802_driver *drv)
526 527
{
	struct cx88_core *core = drv->core;
528
	unsigned int	i;
529 530

	/* Fail a request for hardware if the device is busy. */
531 532
	if (core->active_type_id != CX88_BOARD_NONE &&
	    core->active_type_id != drv->type_id)
533 534
		return -EBUSY;

535 536 537 538 539 540 541 542 543 544 545
	if (drv->type_id == CX88_MPEG_DVB) {
		/* When switching to DVB, always set the input to the tuner */
		core->last_analog_input = core->input;
		core->input = 0;
		for (i = 0;
		     i < (sizeof(core->board.input) / sizeof(struct cx88_input));
		     i++) {
			if (core->board.input[i].type == CX88_VMUX_DVB) {
				core->input = i;
				break;
			}
546 547
		}
	}
548

549 550
	if (drv->advise_acquire)
	{
551
		core->active_ref++;
552 553 554 555
		if (core->active_type_id == CX88_BOARD_NONE) {
			core->active_type_id = drv->type_id;
			drv->advise_acquire(drv);
		}
556

557
		mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
558 559 560 561 562 563
	}

	return 0;
}

/* Driver asked to release hardware. */
564
static int cx8802_request_release(struct cx8802_driver *drv)
565 566 567
{
	struct cx88_core *core = drv->core;

568
	if (drv->advise_release && --core->active_ref == 0)
569
	{
570 571 572 573 574 575
		if (drv->type_id == CX88_MPEG_DVB) {
			/* If the DVB driver is releasing, reset the input
			   state to the last configured analog input */
			core->input = core->last_analog_input;
		}

576 577
		drv->advise_release(drv);
		core->active_type_id = CX88_BOARD_NONE;
578
		mpeg_dbg(1,"%s() Post release GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	}

	return 0;
}

static int cx8802_check_driver(struct cx8802_driver *drv)
{
	if (drv == NULL)
		return -ENODEV;

	if ((drv->type_id != CX88_MPEG_DVB) &&
		(drv->type_id != CX88_MPEG_BLACKBIRD))
		return -EINVAL;

	if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
		(drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
		return -EINVAL;

	if ((drv->probe == NULL) ||
		(drv->remove == NULL) ||
		(drv->advise_acquire == NULL) ||
		(drv->advise_release == NULL))
		return -EINVAL;

	return 0;
}

int cx8802_register_driver(struct cx8802_driver *drv)
{
608
	struct cx8802_dev *dev;
609
	struct cx8802_driver *driver;
610
	int err, i = 0;
611

612 613 614 615
	printk(KERN_INFO
	       "cx88/2: registering cx8802 driver, type: %s access: %s\n",
	       drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
	       drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
616 617

	if ((err = cx8802_check_driver(drv)) != 0) {
618
		printk(KERN_ERR "cx88/2: cx8802_driver is invalid\n");
619 620 621
		return err;
	}

622 623
	mutex_lock(&cx8802_mutex);

624
	list_for_each_entry(dev, &cx8802_devlist, devlist) {
625 626
		printk(KERN_INFO
		       "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
627 628 629
		       dev->core->name, dev->pci->subsystem_vendor,
		       dev->pci->subsystem_device, dev->core->board.name,
		       dev->core->boardnr);
630 631 632

		/* Bring up a new struct for each driver instance */
		driver = kzalloc(sizeof(*drv),GFP_KERNEL);
633 634 635 636
		if (driver == NULL) {
			err = -ENOMEM;
			goto out;
		}
637 638

		/* Snapshot of the driver registration data */
639
		drv->core = dev->core;
640 641 642 643 644 645
		drv->suspend = cx8802_suspend_common;
		drv->resume = cx8802_resume_common;
		drv->request_acquire = cx8802_request_acquire;
		drv->request_release = cx8802_request_release;
		memcpy(driver, drv, sizeof(*driver));

646
		mutex_lock(&drv->core->lock);
647 648
		err = drv->probe(driver);
		if (err == 0) {
649
			i++;
650
			list_add_tail(&driver->drvlist, &dev->drvlist);
651
		} else {
652 653
			printk(KERN_ERR
			       "%s/2: cx8802 probe failed, err = %d\n",
654
			       dev->core->name, err);
655
		}
656
		mutex_unlock(&drv->core->lock);
657 658
	}

659 660 661 662
	err = i ? 0 : -ENODEV;
out:
	mutex_unlock(&cx8802_mutex);
	return err;
663 664 665 666
}

int cx8802_unregister_driver(struct cx8802_driver *drv)
{
667 668 669
	struct cx8802_dev *dev;
	struct cx8802_driver *d, *dtmp;
	int err = 0;
670

671 672 673 674
	printk(KERN_INFO
	       "cx88/2: unregistering cx8802 driver, type: %s access: %s\n",
	       drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
	       drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
675

676 677
	mutex_lock(&cx8802_mutex);

678
	list_for_each_entry(dev, &cx8802_devlist, devlist) {
679 680
		printk(KERN_INFO
		       "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
681 682 683
		       dev->core->name, dev->pci->subsystem_vendor,
		       dev->pci->subsystem_device, dev->core->board.name,
		       dev->core->boardnr);
684

685 686
		mutex_lock(&dev->core->lock);

687
		list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) {
688 689 690 691 692 693
			/* only unregister the correct driver type */
			if (d->type_id != drv->type_id)
				continue;

			err = d->remove(d);
			if (err == 0) {
694
				list_del(&d->drvlist);
695
				kfree(d);
696
			} else
697
				printk(KERN_ERR "%s/2: cx8802 driver remove "
698
				       "failed (%d)\n", dev->core->name, err);
699 700
		}

701
		mutex_unlock(&dev->core->lock);
702 703
	}

704 705
	mutex_unlock(&cx8802_mutex);

706 707 708
	return err;
}

L
Linus Torvalds 已提交
709
/* ----------------------------------------------------------- */
710 711
static int cx8802_probe(struct pci_dev *pci_dev,
			const struct pci_device_id *pci_id)
712 713 714
{
	struct cx8802_dev *dev;
	struct cx88_core  *core;
715
	int err;
716 717 718 719 720 721 722 723 724

	/* general setup */
	core = cx88_core_get(pci_dev);
	if (NULL == core)
		return -EINVAL;

	printk("%s/2: cx2388x 8802 Driver Manager\n", core->name);

	err = -ENODEV;
725
	if (!core->board.mpeg)
726 727 728 729 730 731 732 733 734
		goto fail_core;

	err = -ENOMEM;
	dev = kzalloc(sizeof(*dev),GFP_KERNEL);
	if (NULL == dev)
		goto fail_core;
	dev->pci = pci_dev;
	dev->core = core;

735 736 737
	/* Maintain a reference so cx88-video can query the 8802 device. */
	core->dvbdev = dev;

738 739 740 741
	err = cx8802_init_common(dev);
	if (err != 0)
		goto fail_free;

742
	INIT_LIST_HEAD(&dev->drvlist);
743
	mutex_lock(&cx8802_mutex);
744
	list_add_tail(&dev->devlist,&cx8802_devlist);
745
	mutex_unlock(&cx8802_mutex);
L
Linus Torvalds 已提交
746

747 748
	/* now autoload cx88-dvb or cx88-blackbird */
	request_modules(dev);
749 750 751 752 753
	return 0;

 fail_free:
	kfree(dev);
 fail_core:
754
	core->dvbdev = NULL;
755 756 757 758
	cx88_core_put(core,pci_dev);
	return err;
}

759
static void cx8802_remove(struct pci_dev *pci_dev)
760 761 762 763 764
{
	struct cx8802_dev *dev;

	dev = pci_get_drvdata(pci_dev);

765
	dprintk( 1, "%s\n", __func__);
766

767 768
	flush_request_modules(dev);

769 770
	mutex_lock(&dev->core->lock);

771
	if (!list_empty(&dev->drvlist)) {
772
		struct cx8802_driver *drv, *tmp;
773 774 775 776 777 778
		int err;

		printk(KERN_WARNING "%s/2: Trying to remove cx8802 driver "
		       "while cx8802 sub-drivers still loaded?!\n",
		       dev->core->name);

779
		list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
780 781
			err = drv->remove(drv);
			if (err == 0) {
782
				list_del(&drv->drvlist);
783 784 785
			} else
				printk(KERN_ERR "%s/2: cx8802 driver remove "
				       "failed (%d)\n", dev->core->name, err);
786
			kfree(drv);
787 788 789
		}
	}

790 791
	mutex_unlock(&dev->core->lock);

792 793 794 795 796 797 798 799 800
	/* Destroy any 8802 reference. */
	dev->core->dvbdev = NULL;

	/* common */
	cx8802_fini_common(dev);
	cx88_core_put(dev->core,dev->pci);
	kfree(dev);
}

801
static const struct pci_device_id cx8802_pci_tbl[] = {
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
	{
		.vendor       = 0x14f1,
		.device       = 0x8802,
		.subvendor    = PCI_ANY_ID,
		.subdevice    = PCI_ANY_ID,
	},{
		/* --- end of list --- */
	}
};
MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);

static struct pci_driver cx8802_pci_driver = {
	.name     = "cx88-mpeg driver manager",
	.id_table = cx8802_pci_tbl,
	.probe    = cx8802_probe,
817
	.remove   = cx8802_remove,
818 819
};

820
module_pci_driver(cx8802_pci_driver);
821

L
Linus Torvalds 已提交
822 823 824
EXPORT_SYMBOL(cx8802_buf_prepare);
EXPORT_SYMBOL(cx8802_buf_queue);
EXPORT_SYMBOL(cx8802_cancel_buffers);
H
Hans Verkuil 已提交
825
EXPORT_SYMBOL(cx8802_start_dma);
L
Linus Torvalds 已提交
826

827 828 829
EXPORT_SYMBOL(cx8802_register_driver);
EXPORT_SYMBOL(cx8802_unregister_driver);
EXPORT_SYMBOL(cx8802_get_driver);
L
Linus Torvalds 已提交
830 831 832 833 834
/* ----------------------------------------------------------- */
/*
 * Local variables:
 * c-basic-offset: 8
 * End:
835
 * 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
L
Linus Torvalds 已提交
836
 */