cx88-mpeg.c 23.8 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 26 27
 *    (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>
#include <linux/init.h>
#include <linux/device.h>
A
Andrew Morton 已提交
28
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36
#include <linux/interrupt.h>
#include <asm/delay.h>

#include "cx88.h"

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

MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
J
Jelle Foks 已提交
37
MODULE_AUTHOR("Jelle Foks <jelle@foks.us>");
L
Linus Torvalds 已提交
38 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
static unsigned int debug;
L
Linus Torvalds 已提交
43 44 45 46
module_param(debug,int,0644);
MODULE_PARM_DESC(debug,"enable debug messages [mpeg]");

#define dprintk(level,fmt, arg...)	if (debug >= level) \
47
	printk(KERN_DEBUG "%s/2-mpeg: " fmt, dev->core->name, ## arg)
L
Linus Torvalds 已提交
48

49 50 51
#define mpeg_dbg(level,fmt, arg...)	if (debug >= level) \
	printk(KERN_DEBUG "%s/2-mpeg: " fmt, core->name, ## arg)

52 53 54 55
#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);
56

57
	if (dev->core->board.mpeg & CX88_MPEG_DVB)
58
		request_module("cx88-dvb");
59
	if (dev->core->board.mpeg & CX88_MPEG_BLACKBIRD)
60
		request_module("cx88-blackbird");
61 62 63 64 65 66 67 68 69 70 71 72
}

static void request_modules(struct cx8802_dev *dev)
{
	INIT_WORK(&dev->request_module_wk, request_module_async);
	schedule_work(&dev->request_module_wk);
}
#else
#define request_modules(dev)
#endif /* CONFIG_MODULES */


73
static LIST_HEAD(cx8802_devlist);
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81
/* ------------------------------------------------------------------ */

static int cx8802_start_dma(struct cx8802_dev    *dev,
			    struct cx88_dmaqueue *q,
			    struct cx88_buffer   *buf)
{
	struct cx88_core *core = dev->core;

82 83
	dprintk(1, "cx8802_start_dma w: %d, h: %d, f: %d\n",
		buf->vb.width, buf->vb.height, buf->vb.field);
L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91 92 93 94

	/* 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 */
	cx_write(MO_TS_LNGTH, buf->vb.width);

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

95 96 97
	dprintk( 1, "core->active_type_id = 0x%08x\n", core->active_type_id);

	if ( (core->active_type_id == CX88_MPEG_DVB) &&
98
		(core->board.mpeg & CX88_MPEG_DVB) ) {
99 100

		dprintk( 1, "cx8802_start_dma doing .dvb\n");
L
Linus Torvalds 已提交
101
		/* negedge driven & software reset */
102
		cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
L
Linus Torvalds 已提交
103 104
		udelay(100);
		cx_write(MO_PINMUX_IO, 0x00);
105
		cx_write(TS_HW_SOP_CNTRL, 0x47<<16|188<<4|0x01);
106
		switch (core->boardnr) {
107 108 109
		case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
		case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
		case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
110
		case CX88_BOARD_PCHDTV_HD5500:
M
Mauro Carvalho Chehab 已提交
111
			cx_write(TS_SOP_STAT, 1<<13);
112
			break;
113 114 115 116 117
		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;
118
		case CX88_BOARD_HAUPPAUGE_HVR1300:
119 120 121 122
			/* 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);
123
			break;
124
		case CX88_BOARD_PINNACLE_PCTV_HD_800i:
125 126 127 128 129 130
			/* 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);
131 132
			udelay(100);
			break;
133
		default:
M
Mauro Carvalho Chehab 已提交
134
			cx_write(TS_SOP_STAT, 0x00);
135
			break;
136
		}
L
Linus Torvalds 已提交
137 138
		cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
		udelay(100);
139
	} else if ( (core->active_type_id == CX88_MPEG_BLACKBIRD) &&
140
		(core->board.mpeg & CX88_MPEG_BLACKBIRD) ) {
141
		dprintk( 1, "cx8802_start_dma doing .blackbird\n");
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151
		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);
152
	} else {
153
		printk( "%s() Failed. Unsupported value in .mpeg (0x%08x)\n", __func__,
154
			core->board.mpeg );
155
		return -EINVAL;
L
Linus Torvalds 已提交
156 157 158 159 160 161 162
	}

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

	/* enable irqs */
163
	dprintk( 1, "setting the interrupt mask\n" );
164
	cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_TSINT);
165
	cx_set(MO_TS_INTMSK,  0x1f0011);
L
Linus Torvalds 已提交
166 167

	/* start dma */
168 169
	cx_set(MO_DEV_CNTRL2, (1<<5));
	cx_set(MO_TS_DMACNTRL, 0x11);
L
Linus Torvalds 已提交
170 171 172 173 174 175
	return 0;
}

static int cx8802_stop_dma(struct cx8802_dev *dev)
{
	struct cx88_core *core = dev->core;
176
	dprintk( 1, "cx8802_stop_dma\n" );
L
Linus Torvalds 已提交
177 178 179 180 181

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

	/* disable irqs */
182
	cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT);
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190 191 192 193 194
	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;

195
	dprintk( 1, "cx8802_restart_queue\n" );
L
Linus Torvalds 已提交
196
	if (list_empty(&q->active))
197
	{
198 199 200 201 202 203 204 205 206 207 208 209 210
		struct cx88_buffer *prev;
		prev = NULL;

		dprintk(1, "cx8802_restart_queue: queue is empty\n" );

		for (;;) {
			if (list_empty(&q->queued))
				return 0;
			buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
			if (NULL == prev) {
				list_del(&buf->vb.queue);
				list_add_tail(&buf->vb.queue,&q->active);
				cx8802_start_dma(dev, q, buf);
211
				buf->vb.state = VIDEOBUF_ACTIVE;
212 213 214 215 216 217 218 219 220 221
				buf->count    = q->count++;
				mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
				dprintk(1,"[%p/%d] restart_queue - first active\n",
					buf,buf->vb.i);

			} else if (prev->vb.width  == buf->vb.width  &&
				   prev->vb.height == buf->vb.height &&
				   prev->fmt       == buf->fmt) {
				list_del(&buf->vb.queue);
				list_add_tail(&buf->vb.queue,&q->active);
222
				buf->vb.state = VIDEOBUF_ACTIVE;
223 224 225 226 227 228 229 230 231
				buf->count    = q->count++;
				prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
				dprintk(1,"[%p/%d] restart_queue - move to active\n",
					buf,buf->vb.i);
			} else {
				return 0;
			}
			prev = buf;
		}
L
Linus Torvalds 已提交
232
		return 0;
233
	}
L
Linus Torvalds 已提交
234 235 236 237 238

	buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
	dprintk(2,"restart_queue [%p/%d]: restart dma\n",
		buf, buf->vb.i);
	cx8802_start_dma(dev, q, buf);
239
	list_for_each_entry(buf, &q->active, vb.queue)
L
Linus Torvalds 已提交
240 241 242 243 244 245 246
		buf->count = q->count++;
	mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
	return 0;
}

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

247 248
int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
			struct cx88_buffer *buf, enum v4l2_field field)
L
Linus Torvalds 已提交
249 250
{
	int size = dev->ts_packet_size * dev->ts_packet_count;
251
	struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
L
Linus Torvalds 已提交
252 253
	int rc;

254
	dprintk(1, "%s: %p\n", __func__, buf);
L
Linus Torvalds 已提交
255 256 257
	if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
		return -EINVAL;

258
	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
L
Linus Torvalds 已提交
259 260 261
		buf->vb.width  = dev->ts_packet_size;
		buf->vb.height = dev->ts_packet_count;
		buf->vb.size   = size;
262
		buf->vb.field  = field /*V4L2_FIELD_TOP*/;
L
Linus Torvalds 已提交
263

264
		if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
L
Linus Torvalds 已提交
265 266
			goto fail;
		cx88_risc_databuffer(dev->pci, &buf->risc,
267
				     dma->sglist,
268
				     buf->vb.width, buf->vb.height, 0);
L
Linus Torvalds 已提交
269
	}
270
	buf->vb.state = VIDEOBUF_PREPARED;
L
Linus Torvalds 已提交
271 272 273
	return 0;

 fail:
274
	cx88_free_buffer(q,buf);
L
Linus Torvalds 已提交
275 276 277 278 279 280
	return rc;
}

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

283
	dprintk( 1, "cx8802_buf_queue\n" );
L
Linus Torvalds 已提交
284 285
	/* add jump to stopper */
	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
286
	buf->risc.jmp[1] = cpu_to_le32(cx88q->stopper.dma);
L
Linus Torvalds 已提交
287

288
	if (list_empty(&cx88q->active)) {
289
		dprintk( 1, "queue is empty - first active\n" );
290 291
		list_add_tail(&buf->vb.queue,&cx88q->active);
		cx8802_start_dma(dev, cx88q, buf);
292
		buf->vb.state = VIDEOBUF_ACTIVE;
293 294
		buf->count    = cx88q->count++;
		mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT);
295
		dprintk(1,"[%p/%d] %s - first active\n",
296
			buf, buf->vb.i, __func__);
L
Linus Torvalds 已提交
297 298

	} else {
299
		dprintk( 1, "queue is not empty - append to active\n" );
300 301
		prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
		list_add_tail(&buf->vb.queue,&cx88q->active);
302
		buf->vb.state = VIDEOBUF_ACTIVE;
303
		buf->count    = cx88q->count++;
L
Linus Torvalds 已提交
304
		prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
305
		dprintk( 1, "[%p/%d] %s - append to active\n",
306
			buf, buf->vb.i, __func__);
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	}
}

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

static void do_cancel_buffers(struct cx8802_dev *dev, char *reason, int restart)
{
	struct cx88_dmaqueue *q = &dev->mpegq;
	struct cx88_buffer *buf;
	unsigned long flags;

	spin_lock_irqsave(&dev->slock,flags);
	while (!list_empty(&q->active)) {
		buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
		list_del(&buf->vb.queue);
322
		buf->vb.state = VIDEOBUF_ERROR;
L
Linus Torvalds 已提交
323 324 325 326 327
		wake_up(&buf->vb.done);
		dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
			buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
	}
	if (restart)
328
	{
329
		dprintk(1, "restarting queue\n" );
L
Linus Torvalds 已提交
330
		cx8802_restart_queue(dev,q);
331
	}
L
Linus Torvalds 已提交
332 333 334 335 336 337 338
	spin_unlock_irqrestore(&dev->slock,flags);
}

void cx8802_cancel_buffers(struct cx8802_dev *dev)
{
	struct cx88_dmaqueue *q = &dev->mpegq;

339
	dprintk( 1, "cx8802_cancel_buffers" );
L
Linus Torvalds 已提交
340 341 342 343 344 345 346 347 348
	del_timer_sync(&q->timeout);
	cx8802_stop_dma(dev);
	do_cancel_buffers(dev,"cancel",0);
}

static void cx8802_timeout(unsigned long data)
{
	struct cx8802_dev *dev = (struct cx8802_dev*)data;

349
	dprintk(1, "%s\n",__func__);
L
Linus Torvalds 已提交
350 351 352 353 354 355 356

	if (debug)
		cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
	cx8802_stop_dma(dev);
	do_cancel_buffers(dev,"timeout",1);
}

M
Mauro Carvalho Chehab 已提交
357 358 359 360 361 362 363 364 365
static char *cx88_mpeg_irqs[32] = {
	"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 已提交
366 367 368 369 370
static void cx8802_mpeg_irq(struct cx8802_dev *dev)
{
	struct cx88_core *core = dev->core;
	u32 status, mask, count;

371
	dprintk( 1, "cx8802_mpeg_irq\n" );
L
Linus Torvalds 已提交
372 373 374 375 376 377
	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 已提交
378

L
Linus Torvalds 已提交
379 380
	if (debug || (status & mask & ~0xff))
		cx88_print_irqbits(core->name, "irq mpeg ",
381 382
				   cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
				   status, mask);
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392

	/* 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) {
393
		dprintk( 1, "wake up\n" );
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406
		spin_lock(&dev->slock);
		count = cx_read(MO_TS_GPCNT);
		cx88_wakeup(dev->core, &dev->mpegq, count);
		spin_unlock(&dev->slock);
	}

	/* risc2 y */
	if (status & 0x10) {
		spin_lock(&dev->slock);
		cx8802_restart_queue(dev,&dev->mpegq);
		spin_unlock(&dev->slock);
	}

407 408
	/* other general errors */
	if (status & 0x1f0100) {
409
		dprintk( 0, "general errors: 0x%08x\n", status & 0x1f0100 );
410
		spin_lock(&dev->slock);
L
Linus Torvalds 已提交
411
		cx8802_stop_dma(dev);
412 413 414
		cx8802_restart_queue(dev,&dev->mpegq);
		spin_unlock(&dev->slock);
	}
L
Linus Torvalds 已提交
415 416
}

417 418
#define MAX_IRQ_LOOP 10

419
static irqreturn_t cx8802_irq(int irq, void *dev_id)
L
Linus Torvalds 已提交
420 421 422 423 424 425
{
	struct cx8802_dev *dev = dev_id;
	struct cx88_core *core = dev->core;
	u32 status;
	int loop, handled = 0;

426
	for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
427 428
		status = cx_read(MO_PCI_INTSTAT) &
			(core->pci_irqmask | PCI_INT_TSINT);
L
Linus Torvalds 已提交
429 430
		if (0 == status)
			goto out;
431 432 433
		dprintk( 1, "cx8802_irq\n" );
		dprintk( 1, "    loop: %d/%d\n", loop, MAX_IRQ_LOOP );
		dprintk( 1, "    status: %d\n", status );
L
Linus Torvalds 已提交
434 435 436 437 438
		handled = 1;
		cx_write(MO_PCI_INTSTAT, status);

		if (status & core->pci_irqmask)
			cx88_core_irq(core,status);
439
		if (status & PCI_INT_TSINT)
L
Linus Torvalds 已提交
440 441
			cx8802_mpeg_irq(dev);
	};
442 443
	if (MAX_IRQ_LOOP == loop) {
		dprintk( 0, "clearing mask\n" );
L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451 452
		printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
		       core->name);
		cx_write(MO_PCI_INTMSK,0);
	}

 out:
	return IRQ_RETVAL(handled);
}

453
static int cx8802_init_common(struct cx8802_dev *dev)
L
Linus Torvalds 已提交
454 455 456 457 458 459 460 461
{
	struct cx88_core *core = dev->core;
	int err;

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

	pci_read_config_byte(dev->pci, PCI_CLASS_REVISION, &dev->pci_rev);
468 469
	pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER,  &dev->pci_lat);
	printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
470
	       "latency: %d, mmio: 0x%llx\n", dev->core->name,
L
Linus Torvalds 已提交
471
	       pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
472
	       dev->pci_lat,(unsigned long long)pci_resource_start(dev->pci,0));
L
Linus Torvalds 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

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

	/* init dma queue */
	INIT_LIST_HEAD(&dev->mpegq.active);
	INIT_LIST_HEAD(&dev->mpegq.queued);
	dev->mpegq.timeout.function = cx8802_timeout;
	dev->mpegq.timeout.data     = (unsigned long)dev;
	init_timer(&dev->mpegq.timeout);
	cx88_risc_stopper(dev->pci,&dev->mpegq.stopper,
			  MO_TS_DMACNTRL,0x11,0x00);

	/* get irq */
	err = request_irq(dev->pci->irq, cx8802_irq,
488
			  IRQF_SHARED | IRQF_DISABLED, dev->core->name, dev);
L
Linus Torvalds 已提交
489 490 491 492 493 494 495 496 497 498 499 500
	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;
}

501
static void cx8802_fini_common(struct cx8802_dev *dev)
L
Linus Torvalds 已提交
502
{
503
	dprintk( 2, "cx8802_fini_common\n" );
L
Linus Torvalds 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516
	cx8802_stop_dma(dev);
	pci_disable_device(dev->pci);

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

	/* free memory */
	btcx_riscmem_free(dev->pci,&dev->mpegq.stopper);
}

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

517
static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
L
Linus Torvalds 已提交
518
{
519
	struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
L
Linus Torvalds 已提交
520 521 522 523 524
	struct cx88_core *core = dev->core;

	/* stop mpeg dma */
	spin_lock(&dev->slock);
	if (!list_empty(&dev->mpegq.active)) {
525
		dprintk( 2, "suspend\n" );
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
		printk("%s: suspend mpeg\n", core->name);
		cx8802_stop_dma(dev);
		del_timer(&dev->mpegq.timeout);
	}
	spin_unlock(&dev->slock);

	/* 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;
}

543
static int cx8802_resume_common(struct pci_dev *pci_dev)
L
Linus Torvalds 已提交
544
{
545
	struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
L
Linus Torvalds 已提交
546
	struct cx88_core *core = dev->core;
547
	int err;
L
Linus Torvalds 已提交
548 549

	if (dev->state.disabled) {
550 551 552 553 554 555
		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 已提交
556 557
		dev->state.disabled = 0;
	}
558 559 560 561 562 563 564 565 566
	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 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	pci_restore_state(pci_dev);

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

	/* restart video+vbi capture */
	spin_lock(&dev->slock);
	if (!list_empty(&dev->mpegq.active)) {
		printk("%s: resume mpeg\n", core->name);
		cx8802_restart_queue(dev,&dev->mpegq);
	}
	spin_unlock(&dev->slock);

	return 0;
}

583 584
#if defined(CONFIG_VIDEO_CX88_BLACKBIRD) || \
    defined(CONFIG_VIDEO_CX88_BLACKBIRD_MODULE)
585
struct cx8802_dev *cx8802_get_device(int minor)
586
{
587
	struct cx8802_dev *dev;
588

589 590 591
	list_for_each_entry(dev, &cx8802_devlist, devlist)
		if (dev->mpeg_dev && dev->mpeg_dev->minor == minor)
			return dev;
592 593 594

	return NULL;
}
595 596
EXPORT_SYMBOL(cx8802_get_device);
#endif
597 598 599

struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype)
{
600
	struct cx8802_driver *d;
601

602
	list_for_each_entry(d, &dev->drvlist, drvlist)
603 604
		if (d->type_id == btype)
			return d;
605 606 607 608 609

	return NULL;
}

/* Driver asked for hardware access. */
610
static int cx8802_request_acquire(struct cx8802_driver *drv)
611 612 613 614
{
	struct cx88_core *core = drv->core;

	/* Fail a request for hardware if the device is busy. */
615 616
	if (core->active_type_id != CX88_BOARD_NONE &&
	    core->active_type_id != drv->type_id)
617 618
		return -EBUSY;

619 620
	core->input = CX88_VMUX_DVB;

621 622
	if (drv->advise_acquire)
	{
623
		mutex_lock(&drv->core->lock);
624
		core->active_ref++;
625 626 627 628 629
		if (core->active_type_id == CX88_BOARD_NONE) {
			core->active_type_id = drv->type_id;
			drv->advise_acquire(drv);
		}
		mutex_unlock(&drv->core->lock);
630

631
		mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
632 633 634 635 636 637
	}

	return 0;
}

/* Driver asked to release hardware. */
638
static int cx8802_request_release(struct cx8802_driver *drv)
639 640 641
{
	struct cx88_core *core = drv->core;

642
	mutex_lock(&drv->core->lock);
643
	if (drv->advise_release && --core->active_ref == 0)
644 645 646
	{
		drv->advise_release(drv);
		core->active_type_id = CX88_BOARD_NONE;
647
		mpeg_dbg(1,"%s() Post release GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
648
	}
649
	mutex_unlock(&drv->core->lock);
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

	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)
{
678
	struct cx8802_dev *dev;
679
	struct cx8802_driver *driver;
680
	int err, i = 0;
681

682 683 684 685
	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");
686 687

	if ((err = cx8802_check_driver(drv)) != 0) {
688
		printk(KERN_ERR "cx88/2: cx8802_driver is invalid\n");
689 690 691
		return err;
	}

692
	list_for_each_entry(dev, &cx8802_devlist, devlist) {
693 694
		printk(KERN_INFO
		       "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
695 696 697
		       dev->core->name, dev->pci->subsystem_vendor,
		       dev->pci->subsystem_device, dev->core->board.name,
		       dev->core->boardnr);
698 699 700 701 702 703 704

		/* Bring up a new struct for each driver instance */
		driver = kzalloc(sizeof(*drv),GFP_KERNEL);
		if (driver == NULL)
			return -ENOMEM;

		/* Snapshot of the driver registration data */
705
		drv->core = dev->core;
706 707 708 709 710 711 712 713
		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));

		err = drv->probe(driver);
		if (err == 0) {
714
			i++;
715
			mutex_lock(&drv->core->lock);
716
			list_add_tail(&driver->drvlist, &dev->drvlist);
717 718
			mutex_unlock(&drv->core->lock);
		} else {
719 720
			printk(KERN_ERR
			       "%s/2: cx8802 probe failed, err = %d\n",
721
			       dev->core->name, err);
722 723 724 725
		}

	}

726
	return i ? 0 : -ENODEV;
727 728 729 730
}

int cx8802_unregister_driver(struct cx8802_driver *drv)
{
731 732 733
	struct cx8802_dev *dev;
	struct cx8802_driver *d, *dtmp;
	int err = 0;
734

735 736 737 738
	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");
739

740
	list_for_each_entry(dev, &cx8802_devlist, devlist) {
741 742
		printk(KERN_INFO
		       "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
743 744 745
		       dev->core->name, dev->pci->subsystem_vendor,
		       dev->pci->subsystem_device, dev->core->board.name,
		       dev->core->boardnr);
746

747
		list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) {
748 749 750 751 752 753 754
			/* only unregister the correct driver type */
			if (d->type_id != drv->type_id)
				continue;

			err = d->remove(d);
			if (err == 0) {
				mutex_lock(&drv->core->lock);
755
				list_del(&d->drvlist);
756
				mutex_unlock(&drv->core->lock);
757
				kfree(d);
758
			} else
759
				printk(KERN_ERR "%s/2: cx8802 driver remove "
760
				       "failed (%d)\n", dev->core->name, err);
761 762 763 764 765 766 767
		}

	}

	return err;
}

L
Linus Torvalds 已提交
768
/* ----------------------------------------------------------- */
769 770 771 772 773
static int __devinit cx8802_probe(struct pci_dev *pci_dev,
			       const struct pci_device_id *pci_id)
{
	struct cx8802_dev *dev;
	struct cx88_core  *core;
774
	int err;
775 776 777 778 779 780 781 782 783

	/* 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;
784
	if (!core->board.mpeg)
785 786 787 788 789 790 791 792 793
		goto fail_core;

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

794 795 796
	/* Maintain a reference so cx88-video can query the 8802 device. */
	core->dvbdev = dev;

797 798 799 800
	err = cx8802_init_common(dev);
	if (err != 0)
		goto fail_free;

801
	INIT_LIST_HEAD(&dev->drvlist);
802
	list_add_tail(&dev->devlist,&cx8802_devlist);
L
Linus Torvalds 已提交
803

804 805
	/* now autoload cx88-dvb or cx88-blackbird */
	request_modules(dev);
806 807 808 809 810
	return 0;

 fail_free:
	kfree(dev);
 fail_core:
811
	core->dvbdev = NULL;
812 813 814 815 816 817 818 819 820 821
	cx88_core_put(core,pci_dev);
	return err;
}

static void __devexit cx8802_remove(struct pci_dev *pci_dev)
{
	struct cx8802_dev *dev;

	dev = pci_get_drvdata(pci_dev);

822
	dprintk( 1, "%s\n", __func__);
823

824
	if (!list_empty(&dev->drvlist)) {
825
		struct cx8802_driver *drv, *tmp;
826 827 828 829 830 831
		int err;

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

832
		list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
833 834 835
			err = drv->remove(drv);
			if (err == 0) {
				mutex_lock(&drv->core->lock);
836
				list_del(&drv->drvlist);
837 838 839 840
				mutex_unlock(&drv->core->lock);
			} else
				printk(KERN_ERR "%s/2: cx8802 driver remove "
				       "failed (%d)\n", dev->core->name, err);
841
			kfree(drv);
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
		}
	}

	/* Destroy any 8802 reference. */
	dev->core->dvbdev = NULL;

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

static struct pci_device_id cx8802_pci_tbl[] = {
	{
		.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,
	.remove   = __devexit_p(cx8802_remove),
};

static int cx8802_init(void)
{
875
	printk(KERN_INFO "cx88/2: cx2388x MPEG-TS Driver Manager version %d.%d.%d loaded\n",
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
	       (CX88_VERSION_CODE >> 16) & 0xff,
	       (CX88_VERSION_CODE >>  8) & 0xff,
	       CX88_VERSION_CODE & 0xff);
#ifdef SNAPSHOT
	printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
	       SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
#endif
	return pci_register_driver(&cx8802_pci_driver);
}

static void cx8802_fini(void)
{
	pci_unregister_driver(&cx8802_pci_driver);
}

module_init(cx8802_init);
module_exit(cx8802_fini);
L
Linus Torvalds 已提交
893 894 895 896
EXPORT_SYMBOL(cx8802_buf_prepare);
EXPORT_SYMBOL(cx8802_buf_queue);
EXPORT_SYMBOL(cx8802_cancel_buffers);

897 898 899
EXPORT_SYMBOL(cx8802_register_driver);
EXPORT_SYMBOL(cx8802_unregister_driver);
EXPORT_SYMBOL(cx8802_get_driver);
L
Linus Torvalds 已提交
900 901 902 903 904
/* ----------------------------------------------------------- */
/*
 * Local variables:
 * c-basic-offset: 8
 * End:
905
 * 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 已提交
906
 */