saa7134-core.c 36.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 *
 * device driver for philips saa7134 based TV cards
 * driver core
 *
 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
 *
 *  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/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/kmod.h>
#include <linux/sound.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
32
#include <linux/mutex.h>
33
#include <linux/dma-mapping.h>
34
#include <linux/pm.h>
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44

#include "saa7134-reg.h"
#include "saa7134.h"

MODULE_DESCRIPTION("v4l2 driver module for saa7130/34 based TV cards");
MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
MODULE_LICENSE("GPL");

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

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

49
static unsigned int core_debug;
L
Linus Torvalds 已提交
50 51 52
module_param(core_debug, int, 0644);
MODULE_PARM_DESC(core_debug,"enable debug messages [core]");

53
static unsigned int gpio_tracking;
L
Linus Torvalds 已提交
54 55 56
module_param(gpio_tracking, int, 0644);
MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");

57
static unsigned int alsa = 1;
58
module_param(alsa, int, 0644);
59
MODULE_PARM_DESC(alsa,"enable/disable ALSA DMA sound [dmasound]");
60

L
Linus Torvalds 已提交
61 62 63 64
static unsigned int latency = UNSET;
module_param(latency, int, 0444);
MODULE_PARM_DESC(latency,"pci latency timer");

65 66
int saa7134_no_overlay=-1;
module_param_named(no_overlay, saa7134_no_overlay, int, 0444);
67 68 69
MODULE_PARM_DESC(no_overlay,"allow override overlay default (0 disables, 1 enables)"
		" [some VIA/SIS chipsets are known to have problem with overlay]");

L
Linus Torvalds 已提交
70 71 72 73 74 75
static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
static unsigned int vbi_nr[]   = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
static unsigned int tuner[]    = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
static unsigned int card[]     = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };

76

L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88
module_param_array(video_nr, int, NULL, 0444);
module_param_array(vbi_nr,   int, NULL, 0444);
module_param_array(radio_nr, int, NULL, 0444);
module_param_array(tuner,    int, NULL, 0444);
module_param_array(card,     int, NULL, 0444);

MODULE_PARM_DESC(video_nr, "video device number");
MODULE_PARM_DESC(vbi_nr,   "vbi device number");
MODULE_PARM_DESC(radio_nr, "radio device number");
MODULE_PARM_DESC(tuner,    "tuner type");
MODULE_PARM_DESC(card,     "card type");

89 90
DEFINE_MUTEX(saa7134_devlist_lock);
EXPORT_SYMBOL(saa7134_devlist_lock);
L
Linus Torvalds 已提交
91
LIST_HEAD(saa7134_devlist);
92
EXPORT_SYMBOL(saa7134_devlist);
L
Linus Torvalds 已提交
93 94 95
static LIST_HEAD(mops_list);
static unsigned int saa7134_devcount;

96 97
int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
98

L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#define dprintk(fmt, arg...)	if (core_debug) \
	printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)

void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
{
	unsigned long mode,status;

	if (!gpio_tracking)
		return;
	/* rising SAA7134_GPIO_GPRESCAN reads the status */
	saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
	saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
	mode   = saa_readl(SAA7134_GPIO_GPMODE0   >> 2) & 0xfffffff;
	status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
	printk(KERN_DEBUG
	       "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
	       dev->name, mode, (~mode) & status, mode & status, msg);
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)
{
	u32 index, bitval;

	index = 1 << bit_no;
	switch (value) {
	case 0: /* static value */
	case 1:	dprintk("setting GPIO%d to static %d\n", bit_no, value);
		/* turn sync mode off if necessary */
		if (index & 0x00c00000)
			saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x00);
		if (value)
			bitval = index;
		else
			bitval = 0;
		saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, index);
		saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, index, bitval);
		break;
	case 3:	/* tristate */
		dprintk("setting GPIO%d to tristate\n", bit_no);
		saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, 0);
		break;
140 141 142
	}
}

L
Linus Torvalds 已提交
143 144 145 146 147 148
/* ------------------------------------------------------------------ */


/* ----------------------------------------------------------- */
/* delayed request_module                                      */

149
#if defined(CONFIG_MODULES) && defined(MODULE)
L
Linus Torvalds 已提交
150

151 152 153
static void request_module_async(struct work_struct *work){
	struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
	if (card_is_empress(dev))
154
		request_module("saa7134-empress");
155
	if (card_is_dvb(dev))
156
		request_module("saa7134-dvb");
157 158 159 160
	if (alsa) {
		if (dev->pci->device != PCI_DEVICE_ID_PHILIPS_SAA7130)
			request_module("saa7134-alsa");
	}
L
Linus Torvalds 已提交
161 162
}

163
static void request_submodules(struct saa7134_dev *dev)
L
Linus Torvalds 已提交
164
{
165 166
	INIT_WORK(&dev->request_module_wk, request_module_async);
	schedule_work(&dev->request_module_wk);
L
Linus Torvalds 已提交
167 168 169
}

#else
170
#define request_submodules(dev)
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
#endif /* CONFIG_MODULES */

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

/* nr of (saa7134-)pages for the given buffer size */
static int saa7134_buffer_pages(int size)
{
	size  = PAGE_ALIGN(size);
	size += PAGE_SIZE; /* for non-page-aligned buffers */
	size /= 4096;
	return size;
}

/* calc max # of buffers from size (must not exceed the 4MB virtual
 * address space per DMA channel) */
int saa7134_buffer_count(unsigned int size, unsigned int count)
{
	unsigned int maxcount;

	maxcount = 1024 / saa7134_buffer_pages(size);
	if (count > maxcount)
		count = maxcount;
	return count;
}

int saa7134_buffer_startpage(struct saa7134_buf *buf)
{
	return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
}

unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
{
	unsigned long base;
204
	struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
L
Linus Torvalds 已提交
205 206

	base  = saa7134_buffer_startpage(buf) * 4096;
207
	base += dma->sglist[0].offset;
L
Linus Torvalds 已提交
208 209 210 211 212 213 214
	return base;
}

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

int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
{
215
	__le32       *cpu;
216
	dma_addr_t   dma_addr = 0;
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230

	cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
	if (NULL == cpu)
		return -ENOMEM;
	pt->size = SAA7134_PGTABLE_SIZE;
	pt->cpu  = cpu;
	pt->dma  = dma_addr;
	return 0;
}

int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
			  struct scatterlist *list, unsigned int length,
			  unsigned int startpage)
{
231
	__le32        *ptr;
L
Linus Torvalds 已提交
232 233 234 235 236 237 238
	unsigned int  i,p;

	BUG_ON(NULL == pt || NULL == pt->cpu);

	ptr = pt->cpu + startpage;
	for (i = 0; i < length; i++, list++)
		for (p = 0; p * 4096 < list->length; p++, ptr++)
G
Gerd Knorr 已提交
239
			*ptr = cpu_to_le32(sg_dma_address(list) - list->offset);
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252
	return 0;
}

void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
{
	if (NULL == pt->cpu)
		return;
	pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
	pt->cpu = NULL;
}

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

253
void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf)
L
Linus Torvalds 已提交
254
{
255
	struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
256
	BUG_ON(in_interrupt());
L
Linus Torvalds 已提交
257 258

	videobuf_waiton(&buf->vb,0,0);
259 260
	videobuf_dma_unmap(q, dma);
	videobuf_dma_free(dma);
261
	buf->vb.state = VIDEOBUF_NEEDS_INIT;
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}

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

int saa7134_buffer_queue(struct saa7134_dev *dev,
			 struct saa7134_dmaqueue *q,
			 struct saa7134_buf *buf)
{
	struct saa7134_buf *next = NULL;

	assert_spin_locked(&dev->slock);
	dprintk("buffer_queue %p\n",buf);
	if (NULL == q->curr) {
		if (!q->need_two) {
			q->curr = buf;
			buf->activate(dev,buf,NULL);
		} else if (list_empty(&q->queue)) {
			list_add_tail(&buf->vb.queue,&q->queue);
280
			buf->vb.state = VIDEOBUF_QUEUED;
L
Linus Torvalds 已提交
281 282 283 284 285 286 287 288
		} else {
			next = list_entry(q->queue.next,struct saa7134_buf,
					  vb.queue);
			q->curr = buf;
			buf->activate(dev,buf,next);
		}
	} else {
		list_add_tail(&buf->vb.queue,&q->queue);
289
		buf->vb.state = VIDEOBUF_QUEUED;
L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	}
	return 0;
}

void saa7134_buffer_finish(struct saa7134_dev *dev,
			   struct saa7134_dmaqueue *q,
			   unsigned int state)
{
	assert_spin_locked(&dev->slock);
	dprintk("buffer_finish %p\n",q->curr);

	/* finish current buffer */
	q->curr->vb.state = state;
	do_gettimeofday(&q->curr->vb.ts);
	wake_up(&q->curr->vb.done);
	q->curr = NULL;
}

void saa7134_buffer_next(struct saa7134_dev *dev,
			 struct saa7134_dmaqueue *q)
{
	struct saa7134_buf *buf,*next = NULL;

	assert_spin_locked(&dev->slock);
	BUG_ON(NULL != q->curr);

	if (!list_empty(&q->queue)) {
		/* activate next one from queue */
		buf = list_entry(q->queue.next,struct saa7134_buf,vb.queue);
		dprintk("buffer_next %p [prev=%p/next=%p]\n",
			buf,q->queue.prev,q->queue.next);
		list_del(&buf->vb.queue);
		if (!list_empty(&q->queue))
			next = list_entry(q->queue.next,struct saa7134_buf,
					  vb.queue);
		q->curr = buf;
		buf->activate(dev,buf,next);
		dprintk("buffer_next #2 prev=%p/next=%p\n",
			q->queue.prev,q->queue.next);
	} else {
		/* nothing to do -- just stop DMA */
		dprintk("buffer_next %p\n",NULL);
		saa7134_set_dmabits(dev);
		del_timer(&q->timeout);
334 335 336 337

		if (card_has_mpeg(dev))
			if (dev->ts_started)
				saa7134_ts_stop(dev);
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	}
}

void saa7134_buffer_timeout(unsigned long data)
{
	struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
	struct saa7134_dev *dev = q->dev;
	unsigned long flags;

	spin_lock_irqsave(&dev->slock,flags);

	/* try to reset the hardware (SWRST) */
	saa_writeb(SAA7134_REGION_ENABLE, 0x00);
	saa_writeb(SAA7134_REGION_ENABLE, 0x80);
	saa_writeb(SAA7134_REGION_ENABLE, 0x00);

	/* flag current buffer as failed,
	   try to start over with the next one. */
	if (q->curr) {
		dprintk("timeout on %p\n",q->curr);
358
		saa7134_buffer_finish(dev,q,VIDEOBUF_ERROR);
L
Linus Torvalds 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	}
	saa7134_buffer_next(dev,q);
	spin_unlock_irqrestore(&dev->slock,flags);
}

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

int saa7134_set_dmabits(struct saa7134_dev *dev)
{
	u32 split, task=0, ctrl=0, irq=0;
	enum v4l2_field cap = V4L2_FIELD_ANY;
	enum v4l2_field ov  = V4L2_FIELD_ANY;

	assert_spin_locked(&dev->slock);

374
	if (dev->insuspend)
375 376
		return 0;

L
Linus Torvalds 已提交
377 378 379 380 381 382
	/* video capture -- dma 0 + video task A */
	if (dev->video_q.curr) {
		task |= 0x01;
		ctrl |= SAA7134_MAIN_CTRL_TE0;
		irq  |= SAA7134_IRQ1_INTE_RA0_1 |
			SAA7134_IRQ1_INTE_RA0_0;
383
		cap = dev->video_q.curr->vb.field;
L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	}

	/* video capture -- dma 1+2 (planar modes) */
	if (dev->video_q.curr &&
	    dev->video_q.curr->fmt->planar) {
		ctrl |= SAA7134_MAIN_CTRL_TE4 |
			SAA7134_MAIN_CTRL_TE5;
	}

	/* screen overlay -- dma 0 + video task B */
	if (dev->ovenable) {
		task |= 0x10;
		ctrl |= SAA7134_MAIN_CTRL_TE1;
		ov = dev->ovfield;
	}

	/* vbi capture -- dma 0 + vbi task A+B */
	if (dev->vbi_q.curr) {
		task |= 0x22;
		ctrl |= SAA7134_MAIN_CTRL_TE2 |
			SAA7134_MAIN_CTRL_TE3;
		irq  |= SAA7134_IRQ1_INTE_RA0_7 |
			SAA7134_IRQ1_INTE_RA0_6 |
			SAA7134_IRQ1_INTE_RA0_5 |
			SAA7134_IRQ1_INTE_RA0_4;
	}

	/* audio capture -- dma 3 */
412
	if (dev->dmasound.dma_running) {
L
Linus Torvalds 已提交
413 414 415 416 417 418 419 420
		ctrl |= SAA7134_MAIN_CTRL_TE6;
		irq  |= SAA7134_IRQ1_INTE_RA3_1 |
			SAA7134_IRQ1_INTE_RA3_0;
	}

	/* TS capture -- dma 5 */
	if (dev->ts_q.curr) {
		ctrl |= SAA7134_MAIN_CTRL_TE5;
421
		irq  |= SAA7134_IRQ1_INTE_RA2_1 |
L
Linus Torvalds 已提交
422
			SAA7134_IRQ1_INTE_RA2_0;
423 424 425 426 427 428 429 430 431 432 433 434 435

		/* dma: setup channel 5 (= TS) */

		saa_writeb(SAA7134_TS_DMA0, (dev->ts.nr_packets - 1) & 0xff);
		saa_writeb(SAA7134_TS_DMA1,
			((dev->ts.nr_packets - 1) >> 8) & 0xff);
		/* TSNOPIT=0, TSCOLAP=0 */
		saa_writeb(SAA7134_TS_DMA2,
			(((dev->ts.nr_packets - 1) >> 16) & 0x3f) | 0x00);
		saa_writel(SAA7134_RS_PITCH(5), TS_PACKET_SIZE);
		saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_16 |
						  SAA7134_RS_CONTROL_ME |
						  (dev->ts.pt_ts.dma >> 12));
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	}

	/* set task conditions + field handling */
	if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
		/* default config -- use full frames */
		saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
		saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
		saa_writeb(SAA7134_FIELD_HANDLING(TASK_A),  0x02);
		saa_writeb(SAA7134_FIELD_HANDLING(TASK_B),  0x02);
		split = 0;
	} else {
		/* split fields between tasks */
		if (V4L2_FIELD_TOP == cap) {
			/* odd A, even B, repeat */
			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
		} else {
			/* odd B, even A, repeat */
			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
		}
		saa_writeb(SAA7134_FIELD_HANDLING(TASK_A),  0x01);
		saa_writeb(SAA7134_FIELD_HANDLING(TASK_B),  0x01);
		split = 1;
	}

	/* irqs */
	saa_writeb(SAA7134_REGION_ENABLE, task);
	saa_writel(SAA7134_IRQ1,          irq);
	saa_andorl(SAA7134_MAIN_CTRL,
		   SAA7134_MAIN_CTRL_TE0 |
		   SAA7134_MAIN_CTRL_TE1 |
		   SAA7134_MAIN_CTRL_TE2 |
		   SAA7134_MAIN_CTRL_TE3 |
		   SAA7134_MAIN_CTRL_TE4 |
		   SAA7134_MAIN_CTRL_TE5 |
		   SAA7134_MAIN_CTRL_TE6,
		   ctrl);
	dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
		task, ctrl, irq, split ? "no" : "yes");

	return 0;
}

/* ------------------------------------------------------------------ */
/* IRQ handler + helpers                                              */

static char *irqbits[] = {
	"DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
	"AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
	"TRIG_ERR", "CONF_ERR", "LOAD_ERR",
	"GPIO16?", "GPIO18", "GPIO22", "GPIO23"
};
#define IRQBITS ARRAY_SIZE(irqbits)

static void print_irqstatus(struct saa7134_dev *dev, int loop,
			    unsigned long report, unsigned long status)
{
	unsigned int i;

	printk(KERN_DEBUG "%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
	       dev->name,loop,jiffies,report,status);
	for (i = 0; i < IRQBITS; i++) {
		if (!(report & (1 << i)))
			continue;
		printk(" %s",irqbits[i]);
	}
	if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
		printk(" | RA0=%s,%s,%s,%ld",
		       (status & 0x40) ? "vbi"  : "video",
		       (status & 0x20) ? "b"    : "a",
		       (status & 0x10) ? "odd"  : "even",
		       (status & 0x0f));
	}
	printk("\n");
}

513
static irqreturn_t saa7134_irq(int irq, void *dev_id)
L
Linus Torvalds 已提交
514 515 516 517 518
{
	struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
	unsigned long report,status;
	int loop, handled = 0;

519 520 521
	if (dev->insuspend)
		goto out;

L
Linus Torvalds 已提交
522 523 524
	for (loop = 0; loop < 10; loop++) {
		report = saa_readl(SAA7134_IRQ_REPORT);
		status = saa_readl(SAA7134_IRQ_STATUS);
525

526 527 528
		/* If dmasound support is active and we get a sound report,
		 * mask out the report and let the saa7134-alsa module deal
		 * with it */
529 530 531
		if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
			(dev->dmasound.priv_data != NULL) )
		{
532
			if (irq_debug > 1)
533 534 535 536 537 538 539 540
				printk(KERN_DEBUG "%s/irq: preserving DMA sound interrupt\n",
				       dev->name);
			report &= ~SAA7134_IRQ_REPORT_DONE_RA3;
		}

		if (0 == report) {
			if (irq_debug > 1)
				printk(KERN_DEBUG "%s/irq: no (more) work\n",
541 542 543 544
				       dev->name);
			goto out;
		}

L
Linus Torvalds 已提交
545 546 547 548 549 550
		handled = 1;
		saa_writel(SAA7134_IRQ_REPORT,report);
		if (irq_debug)
			print_irqstatus(dev,loop,report,status);


551 552 553 554
		if ((report & SAA7134_IRQ_REPORT_RDCAP) ||
			(report & SAA7134_IRQ_REPORT_INTL))
				saa7134_irq_video_signalchange(dev);

L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567

		if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
		    (status & 0x60) == 0)
			saa7134_irq_video_done(dev,status);

		if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
		    (status & 0x40) == 0x40)
			saa7134_irq_vbi_done(dev,status);

		if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
		    card_has_mpeg(dev))
			saa7134_irq_ts_done(dev,status);

568 569 570
		if (report & SAA7134_IRQ_REPORT_GPIO16) {
			switch (dev->has_remote) {
				case SAA7134_REMOTE_GPIO:
571 572
					if (!dev->remote)
						break;
573 574 575 576 577 578 579 580 581 582 583 584
					if  (dev->remote->mask_keydown & 0x10000) {
						saa7134_input_irq(dev);
					}
					break;

				case SAA7134_REMOTE_I2C:
					break;			/* FIXME: invoke I2C get_key() */

				default:			/* GPIO16 not used by IR remote */
					break;
			}
		}
585

586 587 588
		if (report & SAA7134_IRQ_REPORT_GPIO18) {
			switch (dev->has_remote) {
				case SAA7134_REMOTE_GPIO:
589 590
					if (!dev->remote)
						break;
591 592 593 594 595 596 597 598 599 600 601 602 603
					if ((dev->remote->mask_keydown & 0x40000) ||
					    (dev->remote->mask_keyup & 0x40000)) {
						saa7134_input_irq(dev);
					}
					break;

				case SAA7134_REMOTE_I2C:
					break;			/* FIXME: invoke I2C get_key() */

				default:			/* GPIO18 not used by IR remote */
					break;
			}
		}
L
Linus Torvalds 已提交
604 605 606 607 608 609 610 611 612
	}

	if (10 == loop) {
		print_irqstatus(dev,loop,report,status);
		if (report & SAA7134_IRQ_REPORT_PE) {
			/* disable all parity error */
			printk(KERN_WARNING "%s/irq: looping -- "
			       "clearing PE (parity error!) enable bit\n",dev->name);
			saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
613 614
		} else if (report & SAA7134_IRQ_REPORT_GPIO16) {
			/* disable gpio16 IRQ */
L
Linus Torvalds 已提交
615
			printk(KERN_WARNING "%s/irq: looping -- "
616 617 618 619 620 621 622
			       "clearing GPIO16 enable bit\n",dev->name);
			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16);
		} else if (report & SAA7134_IRQ_REPORT_GPIO18) {
			/* disable gpio18 IRQs */
			printk(KERN_WARNING "%s/irq: looping -- "
			       "clearing GPIO18 enable bit\n",dev->name);
			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
L
Linus Torvalds 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
		} else {
			/* disable all irqs */
			printk(KERN_WARNING "%s/irq: looping -- "
			       "clearing all enable bits\n",dev->name);
			saa_writel(SAA7134_IRQ1,0);
			saa_writel(SAA7134_IRQ2,0);
		}
	}

 out:
	return IRQ_RETVAL(handled);
}

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

/* early init (no i2c, no irq) */
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

static int saa7134_hw_enable1(struct saa7134_dev *dev)
{
	/* RAM FIFO config */
	saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
	saa_writel(SAA7134_THRESHOULD, 0x02020202);

	/* enable audio + video processing */
	saa_writel(SAA7134_MAIN_CTRL,
			SAA7134_MAIN_CTRL_VPLLE |
			SAA7134_MAIN_CTRL_APLLE |
			SAA7134_MAIN_CTRL_EXOSC |
			SAA7134_MAIN_CTRL_EVFE1 |
			SAA7134_MAIN_CTRL_EVFE2 |
			SAA7134_MAIN_CTRL_ESFE  |
			SAA7134_MAIN_CTRL_EBDAC);

	/*
	* Initialize OSS _after_ enabling audio clock PLL and audio processing.
	* OSS initialization writes to registers via the audio DSP; these
	* writes will fail unless the audio clock has been started.  At worst,
	* audio will not work.
	*/

	/* enable peripheral devices */
	saa_writeb(SAA7134_SPECIAL_MODE, 0x01);

	/* set vertical line numbering start (vbi needs this) */
	saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);

	return 0;
}

L
Linus Torvalds 已提交
672 673 674 675 676 677
static int saa7134_hwinit1(struct saa7134_dev *dev)
{
	dprintk("hwinit1\n");

	saa_writel(SAA7134_IRQ1, 0);
	saa_writel(SAA7134_IRQ2, 0);
678 679 680 681

	/* Clear any stale IRQ reports */
	saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));

682
	mutex_init(&dev->lock);
L
Linus Torvalds 已提交
683 684 685 686 687 688 689 690 691
	spin_lock_init(&dev->slock);

	saa7134_track_gpio(dev,"pre-init");
	saa7134_video_init1(dev);
	saa7134_vbi_init1(dev);
	if (card_has_mpeg(dev))
		saa7134_ts_init1(dev);
	saa7134_input_init1(dev);

692
	saa7134_hw_enable1(dev);
L
Linus Torvalds 已提交
693 694 695 696 697

	return 0;
}

/* late init (with i2c + irq) */
698
static int saa7134_hw_enable2(struct saa7134_dev *dev)
L
Linus Torvalds 已提交
699 700
{

701
	unsigned int irq2_mask;
L
Linus Torvalds 已提交
702 703

	/* enable IRQ's */
704
	irq2_mask =
705 706 707 708 709 710 711
		SAA7134_IRQ2_INTE_DEC3    |
		SAA7134_IRQ2_INTE_DEC2    |
		SAA7134_IRQ2_INTE_DEC1    |
		SAA7134_IRQ2_INTE_DEC0    |
		SAA7134_IRQ2_INTE_PE      |
		SAA7134_IRQ2_INTE_AR;

712
	if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
713 714 715 716 717 718 719
		if (dev->remote->mask_keydown & 0x10000)
			irq2_mask |= SAA7134_IRQ2_INTE_GPIO16;
		else if (dev->remote->mask_keydown & 0x40000)
			irq2_mask |= SAA7134_IRQ2_INTE_GPIO18;
		else if (dev->remote->mask_keyup & 0x40000)
			irq2_mask |= SAA7134_IRQ2_INTE_GPIO18A;
	}
720

721 722 723 724
	if (dev->has_remote == SAA7134_REMOTE_I2C) {
		request_module("ir-kbd-i2c");
	}

L
Linus Torvalds 已提交
725
	saa_writel(SAA7134_IRQ1, 0);
726
	saa_writel(SAA7134_IRQ2, irq2_mask);
L
Linus Torvalds 已提交
727 728 729 730

	return 0;
}

731 732 733 734 735 736 737 738 739 740 741 742 743 744
static int saa7134_hwinit2(struct saa7134_dev *dev)
{

	dprintk("hwinit2\n");

	saa7134_video_init2(dev);
	saa7134_tvaudio_init2(dev);

	saa7134_hw_enable2(dev);

	return 0;
}


L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
/* shutdown */
static int saa7134_hwfini(struct saa7134_dev *dev)
{
	dprintk("hwfini\n");

	if (card_has_mpeg(dev))
		saa7134_ts_fini(dev);
	saa7134_input_fini(dev);
	saa7134_vbi_fini(dev);
	saa7134_tvaudio_fini(dev);
	return 0;
}

static void __devinit must_configure_manually(void)
{
	unsigned int i,p;

	printk(KERN_WARNING
	       "saa7134: <rant>\n"
	       "saa7134:  Congratulations!  Your TV card vendor saved a few\n"
	       "saa7134:  cents for a eeprom, thus your pci board has no\n"
	       "saa7134:  subsystem ID and I can't identify it automatically\n"
	       "saa7134: </rant>\n"
	       "saa7134: I feel better now.  Ok, here are the good news:\n"
	       "saa7134: You can use the card=<nr> insmod option to specify\n"
	       "saa7134: which board do you have.  The list:\n");
	for (i = 0; i < saa7134_bcount; i++) {
		printk(KERN_WARNING "saa7134:   card=%d -> %-40.40s",
		       i,saa7134_boards[i].name);
		for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
			if (saa7134_pci_tbl[p].driver_data != i)
				continue;
			printk(" %04x:%04x",
			       saa7134_pci_tbl[p].subvendor,
			       saa7134_pci_tbl[p].subdevice);
		}
		printk("\n");
	}
}

static struct video_device *vdev_init(struct saa7134_dev *dev,
				      struct video_device *template,
				      char *type)
{
	struct video_device *vfd;

	vfd = video_device_alloc();
	if (NULL == vfd)
		return NULL;
	*vfd = *template;
795
	vfd->v4l2_dev  = &dev->v4l2_dev;
L
Linus Torvalds 已提交
796
	vfd->release = video_device_release;
797
	vfd->debug   = video_debug;
L
Linus Torvalds 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
		 dev->name, type, saa7134_boards[dev->board].name);
	return vfd;
}

static void saa7134_unregister_video(struct saa7134_dev *dev)
{
	if (dev->video_dev) {
		if (-1 != dev->video_dev->minor)
			video_unregister_device(dev->video_dev);
		else
			video_device_release(dev->video_dev);
		dev->video_dev = NULL;
	}
	if (dev->vbi_dev) {
		if (-1 != dev->vbi_dev->minor)
			video_unregister_device(dev->vbi_dev);
		else
			video_device_release(dev->vbi_dev);
		dev->vbi_dev = NULL;
	}
	if (dev->radio_dev) {
		if (-1 != dev->radio_dev->minor)
			video_unregister_device(dev->radio_dev);
		else
			video_device_release(dev->radio_dev);
		dev->radio_dev = NULL;
	}
}

static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
			    struct saa7134_dev *dev)
{
	int err;

	if (NULL != dev->mops)
		return;
	if (saa7134_boards[dev->board].mpeg != ops->type)
		return;
	err = ops->init(dev);
	if (0 != err)
		return;
	dev->mops = ops;
}

static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
			    struct saa7134_dev *dev)
{
	if (NULL == dev->mops)
		return;
	if (dev->mops != ops)
		return;
	dev->mops->fini(dev);
	dev->mops = NULL;
}

static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
				     const struct pci_device_id *pci_id)
{
	struct saa7134_dev *dev;
	struct saa7134_mpeg_ops *mops;
	int err;

861 862 863
	if (saa7134_devcount == SAA7134_MAXBOARDS)
		return -ENOMEM;

P
 
Panagiotis Issaris 已提交
864
	dev = kzalloc(sizeof(*dev),GFP_KERNEL);
L
Linus Torvalds 已提交
865 866 867
	if (NULL == dev)
		return -ENOMEM;

868 869 870 871
	err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
	if (err)
		goto fail0;

L
Linus Torvalds 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
	/* pci init */
	dev->pci = pci_dev;
	if (pci_enable_device(pci_dev)) {
		err = -EIO;
		goto fail1;
	}

	dev->nr = saa7134_devcount;
	sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);

	/* pci quirks */
	if (pci_pci_problems) {
		if (pci_pci_problems & PCIPCI_TRITON)
			printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
		if (pci_pci_problems & PCIPCI_NATOMA)
			printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
		if (pci_pci_problems & PCIPCI_VIAETBF)
			printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
		if (pci_pci_problems & PCIPCI_VSFX)
			printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
#ifdef PCIPCI_ALIMAGIK
		if (pci_pci_problems & PCIPCI_ALIMAGIK) {
			printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
			       dev->name);
			latency = 0x0A;
		}
#endif
899
		if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
900 901 902
			printk(KERN_INFO "%s: quirk: this driver and your "
					"chipset may not work together"
					" in overlay mode.\n",dev->name);
903
			if (!saa7134_no_overlay) {
904 905 906
				printk(KERN_INFO "%s: quirk: overlay "
						"mode will be disabled.\n",
						dev->name);
907
				saa7134_no_overlay = 1;
908 909 910 911 912 913 914
			} else {
				printk(KERN_INFO "%s: quirk: overlay "
						"mode will be forced. Use this"
						" option at your own risk.\n",
						dev->name);
			}
		}
L
Linus Torvalds 已提交
915 916 917 918 919 920 921 922 923
	}
	if (UNSET != latency) {
		printk(KERN_INFO "%s: setting pci latency timer to %d\n",
		       dev->name,latency);
		pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
	}

	/* print pci info */
	pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
924 925
	pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
	printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
926
	       "latency: %d, mmio: 0x%llx\n", dev->name,
L
Linus Torvalds 已提交
927
	       pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
928
	       dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
L
Linus Torvalds 已提交
929
	pci_set_master(pci_dev);
930
	if (!pci_dma_supported(pci_dev, DMA_BIT_MASK(32))) {
L
Linus Torvalds 已提交
931 932 933 934 935 936 937 938 939 940 941 942 943 944
		printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
		err = -EIO;
		goto fail1;
	}

	/* board config */
	dev->board = pci_id->driver_data;
	if (card[dev->nr] >= 0 &&
	    card[dev->nr] < saa7134_bcount)
		dev->board = card[dev->nr];
	if (SAA7134_BOARD_NOAUTO == dev->board) {
		must_configure_manually();
		dev->board = SAA7134_BOARD_UNKNOWN;
	}
945
	dev->autodetected = card[dev->nr] != dev->board;
946 947
	dev->tuner_type = saa7134_boards[dev->board].tuner_type;
	dev->tuner_addr = saa7134_boards[dev->board].tuner_addr;
948 949
	dev->radio_type = saa7134_boards[dev->board].radio_type;
	dev->radio_addr = saa7134_boards[dev->board].radio_addr;
L
Linus Torvalds 已提交
950 951 952
	dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
	if (UNSET != tuner[dev->nr])
		dev->tuner_type = tuner[dev->nr];
953
	printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
954 955 956 957
		dev->name,pci_dev->subsystem_vendor,
		pci_dev->subsystem_device,saa7134_boards[dev->board].name,
		dev->board, dev->autodetected ?
		"autodetected" : "insmod option");
L
Linus Torvalds 已提交
958 959 960 961 962 963

	/* get mmio */
	if (!request_mem_region(pci_resource_start(pci_dev,0),
				pci_resource_len(pci_dev,0),
				dev->name)) {
		err = -EBUSY;
964 965
		printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx\n",
		       dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
L
Linus Torvalds 已提交
966 967
		goto fail1;
	}
968 969
	dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
			     pci_resource_len(pci_dev, 0));
L
Linus Torvalds 已提交
970 971 972 973 974 975 976 977 978 979 980 981 982 983
	dev->bmmio = (__u8 __iomem *)dev->lmmio;
	if (NULL == dev->lmmio) {
		err = -EIO;
		printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
		       dev->name);
		goto fail2;
	}

	/* initialize hardware #1 */
	saa7134_board_init1(dev);
	saa7134_hwinit1(dev);

	/* get irq */
	err = request_irq(pci_dev->irq, saa7134_irq,
984
			  IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
L
Linus Torvalds 已提交
985 986 987 988 989 990 991 992 993 994
	if (err < 0) {
		printk(KERN_ERR "%s: can't get IRQ %d\n",
		       dev->name,pci_dev->irq);
		goto fail3;
	}

	/* wait a bit, register i2c bus */
	msleep(100);
	saa7134_i2c_register(dev);
	saa7134_board_init2(dev);
995

L
Linus Torvalds 已提交
996 997 998
	saa7134_hwinit2(dev);

	/* load i2c helpers */
999
	if (card_is_empress(dev)) {
1000
		struct v4l2_subdev *sd =
1001
			v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
1002
				"saa6752hs", "saa6752hs",
1003
				saa7134_boards[dev->board].empress_addr, NULL);
1004 1005 1006

		if (sd)
			sd->grp_id = GRP_EMPRESS;
L
Linus Torvalds 已提交
1007
	}
1008

1009 1010 1011
	if (saa7134_boards[dev->board].rds_addr) {
		struct v4l2_subdev *sd;

1012
		sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1013
				&dev->i2c_adap,	"saa6588", "saa6588",
1014
				0, I2C_ADDRS(saa7134_boards[dev->board].rds_addr));
1015
		if (sd) {
1016
			printk(KERN_INFO "%s: found RDS decoder\n", dev->name);
1017 1018
			dev->has_rds = 1;
		}
1019 1020
	}

1021
	request_submodules(dev);
1022

L
Linus Torvalds 已提交
1023 1024
	v4l2_prio_init(&dev->prio);

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	mutex_lock(&saa7134_devlist_lock);
	list_for_each_entry(mops, &mops_list, next)
		mpeg_ops_attach(mops, dev);
	list_add_tail(&dev->devlist, &saa7134_devlist);
	mutex_unlock(&saa7134_devlist_lock);

	/* check for signal */
	saa7134_irq_video_signalchange(dev);

	if (TUNER_ABSENT != dev->tuner_type)
1035
		saa_call_all(dev, core, s_power, 0);
1036

L
Linus Torvalds 已提交
1037
	/* register v4l devices */
1038 1039 1040
	if (saa7134_no_overlay > 0)
		printk(KERN_INFO "%s: Overlay support disabled.\n", dev->name);

L
Linus Torvalds 已提交
1041 1042 1043 1044 1045 1046 1047 1048
	dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
	err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
				    video_nr[dev->nr]);
	if (err < 0) {
		printk(KERN_INFO "%s: can't register video device\n",
		       dev->name);
		goto fail4;
	}
1049 1050
	printk(KERN_INFO "%s: registered device %s [v4l2]\n",
	       dev->name, video_device_node_name(dev->video_dev));
L
Linus Torvalds 已提交
1051

1052 1053
	dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");

L
Linus Torvalds 已提交
1054 1055 1056 1057
	err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
				    vbi_nr[dev->nr]);
	if (err < 0)
		goto fail4;
1058 1059
	printk(KERN_INFO "%s: registered device %s\n",
	       dev->name, video_device_node_name(dev->vbi_dev));
L
Linus Torvalds 已提交
1060 1061 1062 1063 1064 1065 1066

	if (card_has_radio(dev)) {
		dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
		err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
					    radio_nr[dev->nr]);
		if (err < 0)
			goto fail4;
1067 1068
		printk(KERN_INFO "%s: registered device %s\n",
		       dev->name, video_device_node_name(dev->radio_dev));
L
Linus Torvalds 已提交
1069 1070 1071 1072 1073
	}

	/* everything worked */
	saa7134_devcount++;

1074
	if (saa7134_dmasound_init && !dev->dmasound.priv_data)
1075
		saa7134_dmasound_init(dev);
1076

L
Linus Torvalds 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	return 0;

 fail4:
	saa7134_unregister_video(dev);
	saa7134_i2c_unregister(dev);
	free_irq(pci_dev->irq, dev);
 fail3:
	saa7134_hwfini(dev);
	iounmap(dev->lmmio);
 fail2:
	release_mem_region(pci_resource_start(pci_dev,0),
			   pci_resource_len(pci_dev,0));
 fail1:
1090 1091
	v4l2_device_unregister(&dev->v4l2_dev);
 fail0:
L
Linus Torvalds 已提交
1092 1093 1094 1095 1096 1097
	kfree(dev);
	return err;
}

static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
{
1098 1099
	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
L
Linus Torvalds 已提交
1100 1101
	struct saa7134_mpeg_ops *mops;

1102
	/* Release DMA sound modules if present */
1103 1104
	if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
		saa7134_dmasound_exit(dev);
1105 1106
	}

L
Linus Torvalds 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	/* debugging ... */
	if (irq_debug) {
		u32 report = saa_readl(SAA7134_IRQ_REPORT);
		u32 status = saa_readl(SAA7134_IRQ_STATUS);
		print_irqstatus(dev,42,report,status);
	}

	/* disable peripheral devices */
	saa_writeb(SAA7134_SPECIAL_MODE,0);

	/* shutdown hardware */
	saa_writel(SAA7134_IRQ1,0);
	saa_writel(SAA7134_IRQ2,0);
	saa_writel(SAA7134_MAIN_CTRL,0);

	/* shutdown subsystems */
	saa7134_hwfini(dev);

	/* unregister */
1126
	mutex_lock(&saa7134_devlist_lock);
L
Linus Torvalds 已提交
1127
	list_del(&dev->devlist);
1128
	list_for_each_entry(mops, &mops_list, next)
L
Linus Torvalds 已提交
1129
		mpeg_ops_detach(mops, dev);
1130
	mutex_unlock(&saa7134_devlist_lock);
L
Linus Torvalds 已提交
1131 1132 1133 1134 1135
	saa7134_devcount--;

	saa7134_i2c_unregister(dev);
	saa7134_unregister_video(dev);

1136

1137 1138 1139 1140 1141 1142 1143
	/* the DMA sound modules should be unloaded before reaching
	   this, but just in case they are still present... */
	if (dev->dmasound.priv_data != NULL) {
		free_irq(pci_dev->irq, &dev->dmasound);
		dev->dmasound.priv_data = NULL;
	}

1144

1145
	/* release resources */
L
Linus Torvalds 已提交
1146 1147 1148 1149 1150
	free_irq(pci_dev->irq, dev);
	iounmap(dev->lmmio);
	release_mem_region(pci_resource_start(pci_dev,0),
			   pci_resource_len(pci_dev,0));

1151 1152

	v4l2_device_unregister(&dev->v4l2_dev);
L
Linus Torvalds 已提交
1153 1154 1155 1156 1157

	/* free memory */
	kfree(dev);
}

1158
#ifdef CONFIG_PM
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184

/* resends a current buffer in queue after resume */
static int saa7134_buffer_requeue(struct saa7134_dev *dev,
				  struct saa7134_dmaqueue *q)
{
	struct saa7134_buf *buf, *next;

	assert_spin_locked(&dev->slock);

	buf  = q->curr;
	next = buf;
	dprintk("buffer_requeue\n");

	if (!buf)
		return 0;

	dprintk("buffer_requeue : resending active buffers \n");

	if (!list_empty(&q->queue))
		next = list_entry(q->queue.next, struct saa7134_buf,
					  vb.queue);
	buf->activate(dev, buf, next);

	return 0;
}

1185 1186
static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
{
1187 1188
	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1189

1190 1191 1192 1193
	/* disable overlay - apps should enable it explicitly on resume*/
	dev->ovenable = 0;

	/* Disable interrupts, DMA, and rest of the chip*/
1194 1195
	saa_writel(SAA7134_IRQ1, 0);
	saa_writel(SAA7134_IRQ2, 0);
1196
	saa_writel(SAA7134_MAIN_CTRL, 0);
1197

1198
	dev->insuspend = 1;
1199 1200 1201 1202 1203 1204
	synchronize_irq(pci_dev->irq);

	/* ACK interrupts once more, just in case,
		since the IRQ handler won't ack them anymore*/

	saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1205 1206 1207 1208 1209 1210 1211

	/* Disable timeout timers - if we have active buffers, we will
	   fill them on resume*/

	del_timer(&dev->video_q.timeout);
	del_timer(&dev->vbi_q.timeout);
	del_timer(&dev->ts_q.timeout);
1212 1213 1214

	if (dev->remote)
		saa7134_ir_stop(dev);
1215

1216
	pci_save_state(pci_dev);
1217
	pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1218

1219
	return 0;
1220 1221 1222 1223
}

static int saa7134_resume(struct pci_dev *pci_dev)
{
1224 1225
	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1226
	unsigned long flags;
1227 1228

	pci_set_power_state(pci_dev, PCI_D0);
1229
	pci_restore_state(pci_dev);
1230 1231 1232 1233 1234 1235

	/* Do things that are done in saa7134_initdev ,
		except of initializing memory structures.*/

	saa7134_board_init1(dev);

1236
	/* saa7134_hwinit1 */
1237 1238 1239 1240
	if (saa7134_boards[dev->board].video_out)
		saa7134_videoport_init(dev);
	if (card_has_mpeg(dev))
		saa7134_ts_init_hw(dev);
1241 1242
	if (dev->remote)
		saa7134_ir_start(dev, dev->remote);
1243
	saa7134_hw_enable1(dev);
1244

1245
	msleep(100);
1246

1247 1248
	saa7134_board_init2(dev);

1249 1250
	/*saa7134_hwinit2*/
	saa7134_set_tvnorm_hw(dev);
1251 1252
	saa7134_tvaudio_setmute(dev);
	saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1253
	saa7134_tvaudio_init(dev);
1254
	saa7134_tvaudio_do_scan(dev);
1255
	saa7134_enable_i2s(dev);
1256
	saa7134_hw_enable2(dev);
1257

1258 1259
	saa7134_irq_video_signalchange(dev);

1260 1261
	/*resume unfinished buffer(s)*/
	spin_lock_irqsave(&dev->slock, flags);
1262 1263 1264 1265
	saa7134_buffer_requeue(dev, &dev->video_q);
	saa7134_buffer_requeue(dev, &dev->vbi_q);
	saa7134_buffer_requeue(dev, &dev->ts_q);

1266 1267 1268 1269 1270
	/* FIXME: Disable DMA audio sound - temporary till proper support
		  is implemented*/

	dev->dmasound.dma_running = 0;

1271
	/* start DMA now*/
1272
	dev->insuspend = 0;
1273
	smp_wmb();
1274 1275 1276
	saa7134_set_dmabits(dev);
	spin_unlock_irqrestore(&dev->slock, flags);

1277 1278
	return 0;
}
1279
#endif
1280

L
Linus Torvalds 已提交
1281 1282 1283 1284 1285 1286
/* ----------------------------------------------------------- */

int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
{
	struct saa7134_dev *dev;

1287
	mutex_lock(&saa7134_devlist_lock);
1288
	list_for_each_entry(dev, &saa7134_devlist, devlist)
L
Linus Torvalds 已提交
1289 1290
		mpeg_ops_attach(ops, dev);
	list_add_tail(&ops->next,&mops_list);
1291
	mutex_unlock(&saa7134_devlist_lock);
L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298
	return 0;
}

void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
{
	struct saa7134_dev *dev;

1299
	mutex_lock(&saa7134_devlist_lock);
L
Linus Torvalds 已提交
1300
	list_del(&ops->next);
1301
	list_for_each_entry(dev, &saa7134_devlist, devlist)
L
Linus Torvalds 已提交
1302
		mpeg_ops_detach(ops, dev);
1303
	mutex_unlock(&saa7134_devlist_lock);
L
Linus Torvalds 已提交
1304 1305 1306 1307 1308 1309 1310 1311
}

EXPORT_SYMBOL(saa7134_ts_register);
EXPORT_SYMBOL(saa7134_ts_unregister);

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

static struct pci_driver saa7134_pci_driver = {
1312 1313 1314 1315
	.name     = "saa7134",
	.id_table = saa7134_pci_tbl,
	.probe    = saa7134_initdev,
	.remove   = __devexit_p(saa7134_finidev),
1316
#ifdef CONFIG_PM
1317 1318
	.suspend  = saa7134_suspend,
	.resume   = saa7134_resume
1319
#endif
L
Linus Torvalds 已提交
1320 1321
};

1322
static int __init saa7134_init(void)
L
Linus Torvalds 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
{
	INIT_LIST_HEAD(&saa7134_devlist);
	printk(KERN_INFO "saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
	       (SAA7134_VERSION_CODE >> 16) & 0xff,
	       (SAA7134_VERSION_CODE >>  8) & 0xff,
	       SAA7134_VERSION_CODE & 0xff);
#ifdef SNAPSHOT
	printk(KERN_INFO "saa7130/34: snapshot date %04d-%02d-%02d\n",
	       SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
#endif
1333
	return pci_register_driver(&saa7134_pci_driver);
L
Linus Torvalds 已提交
1334 1335
}

1336
static void __exit saa7134_fini(void)
L
Linus Torvalds 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345
{
	pci_unregister_driver(&saa7134_pci_driver);
}

module_init(saa7134_init);
module_exit(saa7134_fini);

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

1346
EXPORT_SYMBOL(saa7134_set_gpio);
L
Linus Torvalds 已提交
1347 1348
EXPORT_SYMBOL(saa7134_boards);

1349
/* ----------------- for the DMA sound modules --------------- */
1350

1351 1352
EXPORT_SYMBOL(saa7134_dmasound_init);
EXPORT_SYMBOL(saa7134_dmasound_exit);
1353 1354 1355 1356 1357
EXPORT_SYMBOL(saa7134_pgtable_free);
EXPORT_SYMBOL(saa7134_pgtable_build);
EXPORT_SYMBOL(saa7134_pgtable_alloc);
EXPORT_SYMBOL(saa7134_set_dmabits);

L
Linus Torvalds 已提交
1358 1359 1360 1361 1362 1363
/* ----------------------------------------------------------- */
/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */