xref: /linux/drivers/media/pci/saa7134/saa7134-core.c (revision 4949009eb8d40a441dcddcd96e101e77d31cf1b2)
1 /*
2  *
3  * device driver for philips saa7134 based TV cards
4  * driver core
5  *
6  * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22 
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/kmod.h>
29 #include <linux/sound.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/mutex.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/pm.h>
35 
36 #include "saa7134-reg.h"
37 #include "saa7134.h"
38 
39 MODULE_DESCRIPTION("v4l2 driver module for saa7130/34 based TV cards");
40 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
41 MODULE_LICENSE("GPL");
42 MODULE_VERSION(SAA7134_VERSION);
43 
44 
45 /* ------------------------------------------------------------------ */
46 
47 static unsigned int irq_debug;
48 module_param(irq_debug, int, 0644);
49 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
50 
51 static unsigned int core_debug;
52 module_param(core_debug, int, 0644);
53 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
54 
55 static unsigned int gpio_tracking;
56 module_param(gpio_tracking, int, 0644);
57 MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
58 
59 static unsigned int alsa = 1;
60 module_param(alsa, int, 0644);
61 MODULE_PARM_DESC(alsa,"enable/disable ALSA DMA sound [dmasound]");
62 
63 static unsigned int latency = UNSET;
64 module_param(latency, int, 0444);
65 MODULE_PARM_DESC(latency,"pci latency timer");
66 
67 int saa7134_no_overlay=-1;
68 module_param_named(no_overlay, saa7134_no_overlay, int, 0444);
69 MODULE_PARM_DESC(no_overlay,"allow override overlay default (0 disables, 1 enables)"
70 		" [some VIA/SIS chipsets are known to have problem with overlay]");
71 
72 bool saa7134_userptr;
73 module_param(saa7134_userptr, bool, 0644);
74 MODULE_PARM_DESC(saa7134_userptr, "enable page-aligned userptr support");
75 
76 static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
77 static unsigned int vbi_nr[]   = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
78 static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
79 static unsigned int tuner[]    = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
80 static unsigned int card[]     = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
81 
82 
83 module_param_array(video_nr, int, NULL, 0444);
84 module_param_array(vbi_nr,   int, NULL, 0444);
85 module_param_array(radio_nr, int, NULL, 0444);
86 module_param_array(tuner,    int, NULL, 0444);
87 module_param_array(card,     int, NULL, 0444);
88 
89 MODULE_PARM_DESC(video_nr, "video device number");
90 MODULE_PARM_DESC(vbi_nr,   "vbi device number");
91 MODULE_PARM_DESC(radio_nr, "radio device number");
92 MODULE_PARM_DESC(tuner,    "tuner type");
93 MODULE_PARM_DESC(card,     "card type");
94 
95 DEFINE_MUTEX(saa7134_devlist_lock);
96 EXPORT_SYMBOL(saa7134_devlist_lock);
97 LIST_HEAD(saa7134_devlist);
98 EXPORT_SYMBOL(saa7134_devlist);
99 static LIST_HEAD(mops_list);
100 static unsigned int saa7134_devcount;
101 
102 int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
103 int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
104 
105 #define dprintk(fmt, arg...)	if (core_debug) \
106 	printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
107 
108 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
109 {
110 	unsigned long mode,status;
111 
112 	if (!gpio_tracking)
113 		return;
114 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
115 	saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
116 	saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
117 	mode   = saa_readl(SAA7134_GPIO_GPMODE0   >> 2) & 0xfffffff;
118 	status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
119 	printk(KERN_DEBUG
120 	       "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
121 	       dev->name, mode, (~mode) & status, mode & status, msg);
122 }
123 
124 void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)
125 {
126 	u32 index, bitval;
127 
128 	index = 1 << bit_no;
129 	switch (value) {
130 	case 0: /* static value */
131 	case 1:	dprintk("setting GPIO%d to static %d\n", bit_no, value);
132 		/* turn sync mode off if necessary */
133 		if (index & 0x00c00000)
134 			saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x00);
135 		if (value)
136 			bitval = index;
137 		else
138 			bitval = 0;
139 		saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, index);
140 		saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, index, bitval);
141 		break;
142 	case 3:	/* tristate */
143 		dprintk("setting GPIO%d to tristate\n", bit_no);
144 		saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, 0);
145 		break;
146 	}
147 }
148 
149 /* ------------------------------------------------------------------ */
150 
151 
152 /* ----------------------------------------------------------- */
153 /* delayed request_module                                      */
154 
155 #if defined(CONFIG_MODULES) && defined(MODULE)
156 
157 static void request_module_async(struct work_struct *work){
158 	struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
159 	if (card_is_empress(dev))
160 		request_module("saa7134-empress");
161 	if (card_is_dvb(dev))
162 		request_module("saa7134-dvb");
163 	if (card_is_go7007(dev))
164 		request_module("saa7134-go7007");
165 	if (alsa) {
166 		if (dev->pci->device != PCI_DEVICE_ID_PHILIPS_SAA7130)
167 			request_module("saa7134-alsa");
168 	}
169 }
170 
171 static void request_submodules(struct saa7134_dev *dev)
172 {
173 	INIT_WORK(&dev->request_module_wk, request_module_async);
174 	schedule_work(&dev->request_module_wk);
175 }
176 
177 static void flush_request_submodules(struct saa7134_dev *dev)
178 {
179 	flush_work(&dev->request_module_wk);
180 }
181 
182 #else
183 #define request_submodules(dev)
184 #define flush_request_submodules(dev)
185 #endif /* CONFIG_MODULES */
186 
187 /* ------------------------------------------------------------------ */
188 
189 /* nr of (saa7134-)pages for the given buffer size */
190 static int saa7134_buffer_pages(int size)
191 {
192 	size  = PAGE_ALIGN(size);
193 	size += PAGE_SIZE; /* for non-page-aligned buffers */
194 	size /= 4096;
195 	return size;
196 }
197 
198 /* calc max # of buffers from size (must not exceed the 4MB virtual
199  * address space per DMA channel) */
200 int saa7134_buffer_count(unsigned int size, unsigned int count)
201 {
202 	unsigned int maxcount;
203 
204 	maxcount = 1024 / saa7134_buffer_pages(size);
205 	if (count > maxcount)
206 		count = maxcount;
207 	return count;
208 }
209 
210 int saa7134_buffer_startpage(struct saa7134_buf *buf)
211 {
212 	return saa7134_buffer_pages(vb2_plane_size(&buf->vb2, 0)) * buf->vb2.v4l2_buf.index;
213 }
214 
215 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
216 {
217 	unsigned long base;
218 	struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
219 
220 	base  = saa7134_buffer_startpage(buf) * 4096;
221 	base += dma->sgl[0].offset;
222 	return base;
223 }
224 
225 /* ------------------------------------------------------------------ */
226 
227 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
228 {
229 	__le32       *cpu;
230 	dma_addr_t   dma_addr = 0;
231 
232 	cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
233 	if (NULL == cpu)
234 		return -ENOMEM;
235 	pt->size = SAA7134_PGTABLE_SIZE;
236 	pt->cpu  = cpu;
237 	pt->dma  = dma_addr;
238 	return 0;
239 }
240 
241 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
242 			  struct scatterlist *list, unsigned int length,
243 			  unsigned int startpage)
244 {
245 	__le32        *ptr;
246 	unsigned int  i, p;
247 
248 	BUG_ON(NULL == pt || NULL == pt->cpu);
249 
250 	ptr = pt->cpu + startpage;
251 	for (i = 0; i < length; i++, list = sg_next(list)) {
252 		for (p = 0; p * 4096 < list->length; p++, ptr++)
253 			*ptr = cpu_to_le32(sg_dma_address(list) +
254 						list->offset + p * 4096);
255 	}
256 	return 0;
257 }
258 
259 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
260 {
261 	if (NULL == pt->cpu)
262 		return;
263 	pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
264 	pt->cpu = NULL;
265 }
266 
267 /* ------------------------------------------------------------------ */
268 
269 int saa7134_buffer_queue(struct saa7134_dev *dev,
270 			 struct saa7134_dmaqueue *q,
271 			 struct saa7134_buf *buf)
272 {
273 	struct saa7134_buf *next = NULL;
274 	unsigned long flags;
275 
276 	spin_lock_irqsave(&dev->slock, flags);
277 	dprintk("buffer_queue %p\n", buf);
278 	if (NULL == q->curr) {
279 		if (!q->need_two) {
280 			q->curr = buf;
281 			buf->activate(dev, buf, NULL);
282 		} else if (list_empty(&q->queue)) {
283 			list_add_tail(&buf->entry, &q->queue);
284 		} else {
285 			next = list_entry(q->queue.next, struct saa7134_buf,
286 					  entry);
287 			q->curr = buf;
288 			buf->activate(dev, buf, next);
289 		}
290 	} else {
291 		list_add_tail(&buf->entry, &q->queue);
292 	}
293 	spin_unlock_irqrestore(&dev->slock, flags);
294 	return 0;
295 }
296 
297 void saa7134_buffer_finish(struct saa7134_dev *dev,
298 			   struct saa7134_dmaqueue *q,
299 			   unsigned int state)
300 {
301 	dprintk("buffer_finish %p\n", q->curr);
302 
303 	/* finish current buffer */
304 	v4l2_get_timestamp(&q->curr->vb2.v4l2_buf.timestamp);
305 	q->curr->vb2.v4l2_buf.sequence = q->seq_nr++;
306 	vb2_buffer_done(&q->curr->vb2, state);
307 	q->curr = NULL;
308 }
309 
310 void saa7134_buffer_next(struct saa7134_dev *dev,
311 			 struct saa7134_dmaqueue *q)
312 {
313 	struct saa7134_buf *buf,*next = NULL;
314 
315 	assert_spin_locked(&dev->slock);
316 	BUG_ON(NULL != q->curr);
317 
318 	if (!list_empty(&q->queue)) {
319 		/* activate next one from queue */
320 		buf = list_entry(q->queue.next, struct saa7134_buf, entry);
321 		dprintk("buffer_next %p [prev=%p/next=%p]\n",
322 			buf, q->queue.prev, q->queue.next);
323 		list_del(&buf->entry);
324 		if (!list_empty(&q->queue))
325 			next = list_entry(q->queue.next, struct saa7134_buf, entry);
326 		q->curr = buf;
327 		buf->activate(dev, buf, next);
328 		dprintk("buffer_next #2 prev=%p/next=%p\n",
329 			q->queue.prev, q->queue.next);
330 	} else {
331 		/* nothing to do -- just stop DMA */
332 		dprintk("buffer_next %p\n", NULL);
333 		saa7134_set_dmabits(dev);
334 		del_timer(&q->timeout);
335 	}
336 }
337 
338 void saa7134_buffer_timeout(unsigned long data)
339 {
340 	struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue *)data;
341 	struct saa7134_dev *dev = q->dev;
342 	unsigned long flags;
343 
344 	spin_lock_irqsave(&dev->slock, flags);
345 
346 	/* try to reset the hardware (SWRST) */
347 	saa_writeb(SAA7134_REGION_ENABLE, 0x00);
348 	saa_writeb(SAA7134_REGION_ENABLE, 0x80);
349 	saa_writeb(SAA7134_REGION_ENABLE, 0x00);
350 
351 	/* flag current buffer as failed,
352 	   try to start over with the next one. */
353 	if (q->curr) {
354 		dprintk("timeout on %p\n", q->curr);
355 		saa7134_buffer_finish(dev, q, VB2_BUF_STATE_ERROR);
356 	}
357 	saa7134_buffer_next(dev, q);
358 	spin_unlock_irqrestore(&dev->slock, flags);
359 }
360 
361 void saa7134_stop_streaming(struct saa7134_dev *dev, struct saa7134_dmaqueue *q)
362 {
363 	unsigned long flags;
364 	struct list_head *pos, *n;
365 	struct saa7134_buf *tmp;
366 
367 	spin_lock_irqsave(&dev->slock, flags);
368 	if (!list_empty(&q->queue)) {
369 		list_for_each_safe(pos, n, &q->queue) {
370 			 tmp = list_entry(pos, struct saa7134_buf, entry);
371 			 vb2_buffer_done(&tmp->vb2, VB2_BUF_STATE_ERROR);
372 			 list_del(pos);
373 			 tmp = NULL;
374 		}
375 	}
376 	spin_unlock_irqrestore(&dev->slock, flags);
377 	saa7134_buffer_timeout((unsigned long)q); /* also calls del_timer(&q->timeout) */
378 }
379 EXPORT_SYMBOL_GPL(saa7134_stop_streaming);
380 
381 /* ------------------------------------------------------------------ */
382 
383 int saa7134_set_dmabits(struct saa7134_dev *dev)
384 {
385 	u32 split, task=0, ctrl=0, irq=0;
386 	enum v4l2_field cap = V4L2_FIELD_ANY;
387 	enum v4l2_field ov  = V4L2_FIELD_ANY;
388 
389 	assert_spin_locked(&dev->slock);
390 
391 	if (dev->insuspend)
392 		return 0;
393 
394 	/* video capture -- dma 0 + video task A */
395 	if (dev->video_q.curr) {
396 		task |= 0x01;
397 		ctrl |= SAA7134_MAIN_CTRL_TE0;
398 		irq  |= SAA7134_IRQ1_INTE_RA0_1 |
399 			SAA7134_IRQ1_INTE_RA0_0;
400 		cap = dev->field;
401 	}
402 
403 	/* video capture -- dma 1+2 (planar modes) */
404 	if (dev->video_q.curr && dev->fmt->planar) {
405 		ctrl |= SAA7134_MAIN_CTRL_TE4 |
406 			SAA7134_MAIN_CTRL_TE5;
407 	}
408 
409 	/* screen overlay -- dma 0 + video task B */
410 	if (dev->ovenable) {
411 		task |= 0x10;
412 		ctrl |= SAA7134_MAIN_CTRL_TE1;
413 		ov = dev->ovfield;
414 	}
415 
416 	/* vbi capture -- dma 0 + vbi task A+B */
417 	if (dev->vbi_q.curr) {
418 		task |= 0x22;
419 		ctrl |= SAA7134_MAIN_CTRL_TE2 |
420 			SAA7134_MAIN_CTRL_TE3;
421 		irq  |= SAA7134_IRQ1_INTE_RA0_7 |
422 			SAA7134_IRQ1_INTE_RA0_6 |
423 			SAA7134_IRQ1_INTE_RA0_5 |
424 			SAA7134_IRQ1_INTE_RA0_4;
425 	}
426 
427 	/* audio capture -- dma 3 */
428 	if (dev->dmasound.dma_running) {
429 		ctrl |= SAA7134_MAIN_CTRL_TE6;
430 		irq  |= SAA7134_IRQ1_INTE_RA3_1 |
431 			SAA7134_IRQ1_INTE_RA3_0;
432 	}
433 
434 	/* TS capture -- dma 5 */
435 	if (dev->ts_q.curr) {
436 		ctrl |= SAA7134_MAIN_CTRL_TE5;
437 		irq  |= SAA7134_IRQ1_INTE_RA2_1 |
438 			SAA7134_IRQ1_INTE_RA2_0;
439 	}
440 
441 	/* set task conditions + field handling */
442 	if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
443 		/* default config -- use full frames */
444 		saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
445 		saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
446 		saa_writeb(SAA7134_FIELD_HANDLING(TASK_A),  0x02);
447 		saa_writeb(SAA7134_FIELD_HANDLING(TASK_B),  0x02);
448 		split = 0;
449 	} else {
450 		/* split fields between tasks */
451 		if (V4L2_FIELD_TOP == cap) {
452 			/* odd A, even B, repeat */
453 			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
454 			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
455 		} else {
456 			/* odd B, even A, repeat */
457 			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
458 			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
459 		}
460 		saa_writeb(SAA7134_FIELD_HANDLING(TASK_A),  0x01);
461 		saa_writeb(SAA7134_FIELD_HANDLING(TASK_B),  0x01);
462 		split = 1;
463 	}
464 
465 	/* irqs */
466 	saa_writeb(SAA7134_REGION_ENABLE, task);
467 	saa_writel(SAA7134_IRQ1,          irq);
468 	saa_andorl(SAA7134_MAIN_CTRL,
469 		   SAA7134_MAIN_CTRL_TE0 |
470 		   SAA7134_MAIN_CTRL_TE1 |
471 		   SAA7134_MAIN_CTRL_TE2 |
472 		   SAA7134_MAIN_CTRL_TE3 |
473 		   SAA7134_MAIN_CTRL_TE4 |
474 		   SAA7134_MAIN_CTRL_TE5 |
475 		   SAA7134_MAIN_CTRL_TE6,
476 		   ctrl);
477 	dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
478 		task, ctrl, irq, split ? "no" : "yes");
479 
480 	return 0;
481 }
482 
483 /* ------------------------------------------------------------------ */
484 /* IRQ handler + helpers                                              */
485 
486 static char *irqbits[] = {
487 	"DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
488 	"AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
489 	"TRIG_ERR", "CONF_ERR", "LOAD_ERR",
490 	"GPIO16", "GPIO18", "GPIO22", "GPIO23"
491 };
492 #define IRQBITS ARRAY_SIZE(irqbits)
493 
494 static void print_irqstatus(struct saa7134_dev *dev, int loop,
495 			    unsigned long report, unsigned long status)
496 {
497 	unsigned int i;
498 
499 	printk(KERN_DEBUG "%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
500 	       dev->name,loop,jiffies,report,status);
501 	for (i = 0; i < IRQBITS; i++) {
502 		if (!(report & (1 << i)))
503 			continue;
504 		printk(" %s",irqbits[i]);
505 	}
506 	if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
507 		printk(" | RA0=%s,%s,%s,%ld",
508 		       (status & 0x40) ? "vbi"  : "video",
509 		       (status & 0x20) ? "b"    : "a",
510 		       (status & 0x10) ? "odd"  : "even",
511 		       (status & 0x0f));
512 	}
513 	printk("\n");
514 }
515 
516 static irqreturn_t saa7134_irq(int irq, void *dev_id)
517 {
518 	struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
519 	unsigned long report,status;
520 	int loop, handled = 0;
521 
522 	if (dev->insuspend)
523 		goto out;
524 
525 	for (loop = 0; loop < 10; loop++) {
526 		report = saa_readl(SAA7134_IRQ_REPORT);
527 		status = saa_readl(SAA7134_IRQ_STATUS);
528 
529 		/* If dmasound support is active and we get a sound report,
530 		 * mask out the report and let the saa7134-alsa module deal
531 		 * with it */
532 		if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
533 			(dev->dmasound.priv_data != NULL) )
534 		{
535 			if (irq_debug > 1)
536 				printk(KERN_DEBUG "%s/irq: preserving DMA sound interrupt\n",
537 				       dev->name);
538 			report &= ~SAA7134_IRQ_REPORT_DONE_RA3;
539 		}
540 
541 		if (0 == report) {
542 			if (irq_debug > 1)
543 				printk(KERN_DEBUG "%s/irq: no (more) work\n",
544 				       dev->name);
545 			goto out;
546 		}
547 
548 		handled = 1;
549 		saa_writel(SAA7134_IRQ_REPORT,report);
550 		if (irq_debug)
551 			print_irqstatus(dev,loop,report,status);
552 
553 
554 		if ((report & SAA7134_IRQ_REPORT_RDCAP) ||
555 			(report & SAA7134_IRQ_REPORT_INTL))
556 				saa7134_irq_video_signalchange(dev);
557 
558 
559 		if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
560 		    (status & 0x60) == 0)
561 			saa7134_irq_video_done(dev,status);
562 
563 		if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
564 		    (status & 0x40) == 0x40)
565 			saa7134_irq_vbi_done(dev,status);
566 
567 		if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
568 		    card_has_mpeg(dev)) {
569 			if (dev->mops->irq_ts_done != NULL)
570 				dev->mops->irq_ts_done(dev, status);
571 			else
572 				saa7134_irq_ts_done(dev, status);
573 		}
574 
575 		if (report & SAA7134_IRQ_REPORT_GPIO16) {
576 			switch (dev->has_remote) {
577 				case SAA7134_REMOTE_GPIO:
578 					if (!dev->remote)
579 						break;
580 					if  (dev->remote->mask_keydown & 0x10000) {
581 						saa7134_input_irq(dev);
582 					}
583 					break;
584 
585 				case SAA7134_REMOTE_I2C:
586 					break;			/* FIXME: invoke I2C get_key() */
587 
588 				default:			/* GPIO16 not used by IR remote */
589 					break;
590 			}
591 		}
592 
593 		if (report & SAA7134_IRQ_REPORT_GPIO18) {
594 			switch (dev->has_remote) {
595 				case SAA7134_REMOTE_GPIO:
596 					if (!dev->remote)
597 						break;
598 					if ((dev->remote->mask_keydown & 0x40000) ||
599 					    (dev->remote->mask_keyup & 0x40000)) {
600 						saa7134_input_irq(dev);
601 					}
602 					break;
603 
604 				case SAA7134_REMOTE_I2C:
605 					break;			/* FIXME: invoke I2C get_key() */
606 
607 				default:			/* GPIO18 not used by IR remote */
608 					break;
609 			}
610 		}
611 	}
612 
613 	if (10 == loop) {
614 		print_irqstatus(dev,loop,report,status);
615 		if (report & SAA7134_IRQ_REPORT_PE) {
616 			/* disable all parity error */
617 			printk(KERN_WARNING "%s/irq: looping -- "
618 			       "clearing PE (parity error!) enable bit\n",dev->name);
619 			saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
620 		} else if (report & SAA7134_IRQ_REPORT_GPIO16) {
621 			/* disable gpio16 IRQ */
622 			printk(KERN_WARNING "%s/irq: looping -- "
623 			       "clearing GPIO16 enable bit\n",dev->name);
624 			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_P);
625 			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_N);
626 		} else if (report & SAA7134_IRQ_REPORT_GPIO18) {
627 			/* disable gpio18 IRQs */
628 			printk(KERN_WARNING "%s/irq: looping -- "
629 			       "clearing GPIO18 enable bit\n",dev->name);
630 			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_P);
631 			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_N);
632 		} else {
633 			/* disable all irqs */
634 			printk(KERN_WARNING "%s/irq: looping -- "
635 			       "clearing all enable bits\n",dev->name);
636 			saa_writel(SAA7134_IRQ1,0);
637 			saa_writel(SAA7134_IRQ2,0);
638 		}
639 	}
640 
641  out:
642 	return IRQ_RETVAL(handled);
643 }
644 
645 /* ------------------------------------------------------------------ */
646 
647 /* early init (no i2c, no irq) */
648 
649 static int saa7134_hw_enable1(struct saa7134_dev *dev)
650 {
651 	/* RAM FIFO config */
652 	saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
653 	saa_writel(SAA7134_THRESHOULD, 0x02020202);
654 
655 	/* enable audio + video processing */
656 	saa_writel(SAA7134_MAIN_CTRL,
657 			SAA7134_MAIN_CTRL_VPLLE |
658 			SAA7134_MAIN_CTRL_APLLE |
659 			SAA7134_MAIN_CTRL_EXOSC |
660 			SAA7134_MAIN_CTRL_EVFE1 |
661 			SAA7134_MAIN_CTRL_EVFE2 |
662 			SAA7134_MAIN_CTRL_ESFE  |
663 			SAA7134_MAIN_CTRL_EBDAC);
664 
665 	/*
666 	* Initialize OSS _after_ enabling audio clock PLL and audio processing.
667 	* OSS initialization writes to registers via the audio DSP; these
668 	* writes will fail unless the audio clock has been started.  At worst,
669 	* audio will not work.
670 	*/
671 
672 	/* enable peripheral devices */
673 	saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
674 
675 	/* set vertical line numbering start (vbi needs this) */
676 	saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
677 
678 	return 0;
679 }
680 
681 static int saa7134_hwinit1(struct saa7134_dev *dev)
682 {
683 	dprintk("hwinit1\n");
684 
685 	saa_writel(SAA7134_IRQ1, 0);
686 	saa_writel(SAA7134_IRQ2, 0);
687 
688 	/* Clear any stale IRQ reports */
689 	saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
690 
691 	mutex_init(&dev->lock);
692 	spin_lock_init(&dev->slock);
693 
694 	saa7134_track_gpio(dev,"pre-init");
695 	saa7134_video_init1(dev);
696 	saa7134_vbi_init1(dev);
697 	if (card_has_mpeg(dev))
698 		saa7134_ts_init1(dev);
699 	saa7134_input_init1(dev);
700 
701 	saa7134_hw_enable1(dev);
702 
703 	return 0;
704 }
705 
706 /* late init (with i2c + irq) */
707 static int saa7134_hw_enable2(struct saa7134_dev *dev)
708 {
709 
710 	unsigned int irq2_mask;
711 
712 	/* enable IRQ's */
713 	irq2_mask =
714 		SAA7134_IRQ2_INTE_DEC3    |
715 		SAA7134_IRQ2_INTE_DEC2    |
716 		SAA7134_IRQ2_INTE_DEC1    |
717 		SAA7134_IRQ2_INTE_DEC0    |
718 		SAA7134_IRQ2_INTE_PE      |
719 		SAA7134_IRQ2_INTE_AR;
720 
721 	if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
722 		if (dev->remote->mask_keydown & 0x10000)
723 			irq2_mask |= SAA7134_IRQ2_INTE_GPIO16_N;
724 		else {		/* Allow enabling both IRQ edge triggers */
725 			if (dev->remote->mask_keydown & 0x40000)
726 				irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_P;
727 			if (dev->remote->mask_keyup & 0x40000)
728 				irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_N;
729 		}
730 	}
731 
732 	if (dev->has_remote == SAA7134_REMOTE_I2C) {
733 		request_module("ir-kbd-i2c");
734 	}
735 
736 	saa_writel(SAA7134_IRQ1, 0);
737 	saa_writel(SAA7134_IRQ2, irq2_mask);
738 
739 	return 0;
740 }
741 
742 static int saa7134_hwinit2(struct saa7134_dev *dev)
743 {
744 
745 	dprintk("hwinit2\n");
746 
747 	saa7134_video_init2(dev);
748 	saa7134_tvaudio_init2(dev);
749 
750 	saa7134_hw_enable2(dev);
751 
752 	return 0;
753 }
754 
755 
756 /* shutdown */
757 static int saa7134_hwfini(struct saa7134_dev *dev)
758 {
759 	dprintk("hwfini\n");
760 
761 	if (card_has_mpeg(dev))
762 		saa7134_ts_fini(dev);
763 	saa7134_input_fini(dev);
764 	saa7134_vbi_fini(dev);
765 	saa7134_tvaudio_fini(dev);
766 	saa7134_video_fini(dev);
767 	return 0;
768 }
769 
770 static void must_configure_manually(int has_eeprom)
771 {
772 	unsigned int i,p;
773 
774 	if (!has_eeprom)
775 		printk(KERN_WARNING
776 		       "saa7134: <rant>\n"
777 		       "saa7134:  Congratulations!  Your TV card vendor saved a few\n"
778 		       "saa7134:  cents for a eeprom, thus your pci board has no\n"
779 		       "saa7134:  subsystem ID and I can't identify it automatically\n"
780 		       "saa7134: </rant>\n"
781 		       "saa7134: I feel better now.  Ok, here are the good news:\n"
782 		       "saa7134: You can use the card=<nr> insmod option to specify\n"
783 		       "saa7134: which board do you have.  The list:\n");
784 	else
785 		printk(KERN_WARNING
786 		       "saa7134: Board is currently unknown. You might try to use the card=<nr>\n"
787 		       "saa7134: insmod option to specify which board do you have, but this is\n"
788 		       "saa7134: somewhat risky, as might damage your card. It is better to ask\n"
789 		       "saa7134: for support at linux-media@vger.kernel.org.\n"
790 		       "saa7134: The supported cards are:\n");
791 
792 	for (i = 0; i < saa7134_bcount; i++) {
793 		printk(KERN_WARNING "saa7134:   card=%d -> %-40.40s",
794 		       i,saa7134_boards[i].name);
795 		for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
796 			if (saa7134_pci_tbl[p].driver_data != i)
797 				continue;
798 			printk(" %04x:%04x",
799 			       saa7134_pci_tbl[p].subvendor,
800 			       saa7134_pci_tbl[p].subdevice);
801 		}
802 		printk("\n");
803 	}
804 }
805 
806 static struct video_device *vdev_init(struct saa7134_dev *dev,
807 				      struct video_device *template,
808 				      char *type)
809 {
810 	struct video_device *vfd;
811 
812 	vfd = video_device_alloc();
813 	if (NULL == vfd)
814 		return NULL;
815 	*vfd = *template;
816 	vfd->v4l2_dev  = &dev->v4l2_dev;
817 	vfd->release = video_device_release;
818 	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
819 		 dev->name, type, saa7134_boards[dev->board].name);
820 	video_set_drvdata(vfd, dev);
821 	return vfd;
822 }
823 
824 static void saa7134_unregister_video(struct saa7134_dev *dev)
825 {
826 	if (dev->video_dev) {
827 		if (video_is_registered(dev->video_dev))
828 			video_unregister_device(dev->video_dev);
829 		else
830 			video_device_release(dev->video_dev);
831 		dev->video_dev = NULL;
832 	}
833 	if (dev->vbi_dev) {
834 		if (video_is_registered(dev->vbi_dev))
835 			video_unregister_device(dev->vbi_dev);
836 		else
837 			video_device_release(dev->vbi_dev);
838 		dev->vbi_dev = NULL;
839 	}
840 	if (dev->radio_dev) {
841 		if (video_is_registered(dev->radio_dev))
842 			video_unregister_device(dev->radio_dev);
843 		else
844 			video_device_release(dev->radio_dev);
845 		dev->radio_dev = NULL;
846 	}
847 }
848 
849 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
850 			    struct saa7134_dev *dev)
851 {
852 	int err;
853 
854 	if (NULL != dev->mops)
855 		return;
856 	if (saa7134_boards[dev->board].mpeg != ops->type)
857 		return;
858 	err = ops->init(dev);
859 	if (0 != err)
860 		return;
861 	dev->mops = ops;
862 }
863 
864 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
865 			    struct saa7134_dev *dev)
866 {
867 	if (NULL == dev->mops)
868 		return;
869 	if (dev->mops != ops)
870 		return;
871 	dev->mops->fini(dev);
872 	dev->mops = NULL;
873 }
874 
875 static int saa7134_initdev(struct pci_dev *pci_dev,
876 			   const struct pci_device_id *pci_id)
877 {
878 	struct saa7134_dev *dev;
879 	struct saa7134_mpeg_ops *mops;
880 	int err;
881 
882 	if (saa7134_devcount == SAA7134_MAXBOARDS)
883 		return -ENOMEM;
884 
885 	dev = kzalloc(sizeof(*dev),GFP_KERNEL);
886 	if (NULL == dev)
887 		return -ENOMEM;
888 
889 	err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
890 	if (err)
891 		goto fail0;
892 
893 	/* pci init */
894 	dev->pci = pci_dev;
895 	if (pci_enable_device(pci_dev)) {
896 		err = -EIO;
897 		goto fail1;
898 	}
899 
900 	dev->nr = saa7134_devcount;
901 	sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
902 
903 	/* pci quirks */
904 	if (pci_pci_problems) {
905 		if (pci_pci_problems & PCIPCI_TRITON)
906 			printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
907 		if (pci_pci_problems & PCIPCI_NATOMA)
908 			printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
909 		if (pci_pci_problems & PCIPCI_VIAETBF)
910 			printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
911 		if (pci_pci_problems & PCIPCI_VSFX)
912 			printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
913 #ifdef PCIPCI_ALIMAGIK
914 		if (pci_pci_problems & PCIPCI_ALIMAGIK) {
915 			printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
916 			       dev->name);
917 			latency = 0x0A;
918 		}
919 #endif
920 		if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
921 			printk(KERN_INFO "%s: quirk: this driver and your "
922 					"chipset may not work together"
923 					" in overlay mode.\n",dev->name);
924 			if (!saa7134_no_overlay) {
925 				printk(KERN_INFO "%s: quirk: overlay "
926 						"mode will be disabled.\n",
927 						dev->name);
928 				saa7134_no_overlay = 1;
929 			} else {
930 				printk(KERN_INFO "%s: quirk: overlay "
931 						"mode will be forced. Use this"
932 						" option at your own risk.\n",
933 						dev->name);
934 			}
935 		}
936 	}
937 	if (UNSET != latency) {
938 		printk(KERN_INFO "%s: setting pci latency timer to %d\n",
939 		       dev->name,latency);
940 		pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
941 	}
942 
943 	/* print pci info */
944 	dev->pci_rev = pci_dev->revision;
945 	pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
946 	printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
947 	       "latency: %d, mmio: 0x%llx\n", dev->name,
948 	       pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
949 	       dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
950 	pci_set_master(pci_dev);
951 	if (!pci_dma_supported(pci_dev, DMA_BIT_MASK(32))) {
952 		printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
953 		err = -EIO;
954 		goto fail1;
955 	}
956 
957 	/* board config */
958 	dev->board = pci_id->driver_data;
959 	if ((unsigned)card[dev->nr] < saa7134_bcount)
960 		dev->board = card[dev->nr];
961 	if (SAA7134_BOARD_UNKNOWN == dev->board)
962 		must_configure_manually(0);
963 	else if (SAA7134_BOARD_NOAUTO == dev->board) {
964 		must_configure_manually(1);
965 		dev->board = SAA7134_BOARD_UNKNOWN;
966 	}
967 	dev->autodetected = card[dev->nr] != dev->board;
968 	dev->tuner_type = saa7134_boards[dev->board].tuner_type;
969 	dev->tuner_addr = saa7134_boards[dev->board].tuner_addr;
970 	dev->radio_type = saa7134_boards[dev->board].radio_type;
971 	dev->radio_addr = saa7134_boards[dev->board].radio_addr;
972 	dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
973 	if (UNSET != tuner[dev->nr])
974 		dev->tuner_type = tuner[dev->nr];
975 	printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
976 		dev->name,pci_dev->subsystem_vendor,
977 		pci_dev->subsystem_device,saa7134_boards[dev->board].name,
978 		dev->board, dev->autodetected ?
979 		"autodetected" : "insmod option");
980 
981 	/* get mmio */
982 	if (!request_mem_region(pci_resource_start(pci_dev,0),
983 				pci_resource_len(pci_dev,0),
984 				dev->name)) {
985 		err = -EBUSY;
986 		printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx\n",
987 		       dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
988 		goto fail1;
989 	}
990 	dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
991 			     pci_resource_len(pci_dev, 0));
992 	dev->bmmio = (__u8 __iomem *)dev->lmmio;
993 	if (NULL == dev->lmmio) {
994 		err = -EIO;
995 		printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
996 		       dev->name);
997 		goto fail2;
998 	}
999 
1000 	/* initialize hardware #1 */
1001 	saa7134_board_init1(dev);
1002 	saa7134_hwinit1(dev);
1003 
1004 	dev->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
1005 	if (IS_ERR(dev->alloc_ctx)) {
1006 		err = PTR_ERR(dev->alloc_ctx);
1007 		goto fail3;
1008 	}
1009 	/* get irq */
1010 	err = request_irq(pci_dev->irq, saa7134_irq,
1011 			  IRQF_SHARED, dev->name, dev);
1012 	if (err < 0) {
1013 		printk(KERN_ERR "%s: can't get IRQ %d\n",
1014 		       dev->name,pci_dev->irq);
1015 		goto fail4;
1016 	}
1017 
1018 	/* wait a bit, register i2c bus */
1019 	msleep(100);
1020 	saa7134_i2c_register(dev);
1021 	saa7134_board_init2(dev);
1022 
1023 	saa7134_hwinit2(dev);
1024 
1025 	/* load i2c helpers */
1026 	if (card_is_empress(dev)) {
1027 		dev->empress_sd =
1028 			v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
1029 				"saa6752hs",
1030 				saa7134_boards[dev->board].empress_addr, NULL);
1031 
1032 		if (dev->empress_sd)
1033 			dev->empress_sd->grp_id = GRP_EMPRESS;
1034 	}
1035 
1036 	if (saa7134_boards[dev->board].rds_addr) {
1037 		struct v4l2_subdev *sd;
1038 
1039 		sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1040 				&dev->i2c_adap, "saa6588",
1041 				0, I2C_ADDRS(saa7134_boards[dev->board].rds_addr));
1042 		if (sd) {
1043 			printk(KERN_INFO "%s: found RDS decoder\n", dev->name);
1044 			dev->has_rds = 1;
1045 		}
1046 	}
1047 
1048 	mutex_lock(&saa7134_devlist_lock);
1049 	list_for_each_entry(mops, &mops_list, next)
1050 		mpeg_ops_attach(mops, dev);
1051 	list_add_tail(&dev->devlist, &saa7134_devlist);
1052 	mutex_unlock(&saa7134_devlist_lock);
1053 
1054 	/* check for signal */
1055 	saa7134_irq_video_signalchange(dev);
1056 
1057 	if (TUNER_ABSENT != dev->tuner_type)
1058 		saa_call_all(dev, core, s_power, 0);
1059 
1060 	/* register v4l devices */
1061 	if (saa7134_no_overlay > 0)
1062 		printk(KERN_INFO "%s: Overlay support disabled.\n", dev->name);
1063 
1064 	dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
1065 	dev->video_dev->ctrl_handler = &dev->ctrl_handler;
1066 	dev->video_dev->lock = &dev->lock;
1067 	dev->video_dev->queue = &dev->video_vbq;
1068 	err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1069 				    video_nr[dev->nr]);
1070 	if (err < 0) {
1071 		printk(KERN_INFO "%s: can't register video device\n",
1072 		       dev->name);
1073 		goto fail5;
1074 	}
1075 	printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1076 	       dev->name, video_device_node_name(dev->video_dev));
1077 
1078 	dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1079 	dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
1080 	dev->vbi_dev->lock = &dev->lock;
1081 	dev->vbi_dev->queue = &dev->vbi_vbq;
1082 
1083 	err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1084 				    vbi_nr[dev->nr]);
1085 	if (err < 0)
1086 		goto fail5;
1087 	printk(KERN_INFO "%s: registered device %s\n",
1088 	       dev->name, video_device_node_name(dev->vbi_dev));
1089 
1090 	if (card_has_radio(dev)) {
1091 		dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1092 		dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
1093 		dev->radio_dev->lock = &dev->lock;
1094 		err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1095 					    radio_nr[dev->nr]);
1096 		if (err < 0)
1097 			goto fail5;
1098 		printk(KERN_INFO "%s: registered device %s\n",
1099 		       dev->name, video_device_node_name(dev->radio_dev));
1100 	}
1101 
1102 	/* everything worked */
1103 	saa7134_devcount++;
1104 
1105 	if (saa7134_dmasound_init && !dev->dmasound.priv_data)
1106 		saa7134_dmasound_init(dev);
1107 
1108 	request_submodules(dev);
1109 	return 0;
1110 
1111  fail5:
1112 	saa7134_unregister_video(dev);
1113 	saa7134_i2c_unregister(dev);
1114 	free_irq(pci_dev->irq, dev);
1115  fail4:
1116 	vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
1117  fail3:
1118 	saa7134_hwfini(dev);
1119 	iounmap(dev->lmmio);
1120  fail2:
1121 	release_mem_region(pci_resource_start(pci_dev,0),
1122 			   pci_resource_len(pci_dev,0));
1123  fail1:
1124 	v4l2_device_unregister(&dev->v4l2_dev);
1125  fail0:
1126 	kfree(dev);
1127 	return err;
1128 }
1129 
1130 static void saa7134_finidev(struct pci_dev *pci_dev)
1131 {
1132 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1133 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1134 	struct saa7134_mpeg_ops *mops;
1135 
1136 	flush_request_submodules(dev);
1137 
1138 	/* Release DMA sound modules if present */
1139 	if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1140 		saa7134_dmasound_exit(dev);
1141 	}
1142 
1143 	/* debugging ... */
1144 	if (irq_debug) {
1145 		u32 report = saa_readl(SAA7134_IRQ_REPORT);
1146 		u32 status = saa_readl(SAA7134_IRQ_STATUS);
1147 		print_irqstatus(dev,42,report,status);
1148 	}
1149 
1150 	/* disable peripheral devices */
1151 	saa_writeb(SAA7134_SPECIAL_MODE,0);
1152 
1153 	/* shutdown hardware */
1154 	saa_writel(SAA7134_IRQ1,0);
1155 	saa_writel(SAA7134_IRQ2,0);
1156 	saa_writel(SAA7134_MAIN_CTRL,0);
1157 
1158 	/* shutdown subsystems */
1159 	saa7134_hwfini(dev);
1160 
1161 	/* unregister */
1162 	mutex_lock(&saa7134_devlist_lock);
1163 	list_del(&dev->devlist);
1164 	list_for_each_entry(mops, &mops_list, next)
1165 		mpeg_ops_detach(mops, dev);
1166 	mutex_unlock(&saa7134_devlist_lock);
1167 	saa7134_devcount--;
1168 
1169 	saa7134_i2c_unregister(dev);
1170 	saa7134_unregister_video(dev);
1171 
1172 
1173 	/* the DMA sound modules should be unloaded before reaching
1174 	   this, but just in case they are still present... */
1175 	if (dev->dmasound.priv_data != NULL) {
1176 		free_irq(pci_dev->irq, &dev->dmasound);
1177 		dev->dmasound.priv_data = NULL;
1178 	}
1179 
1180 
1181 	/* release resources */
1182 	free_irq(pci_dev->irq, dev);
1183 	vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
1184 	iounmap(dev->lmmio);
1185 	release_mem_region(pci_resource_start(pci_dev,0),
1186 			   pci_resource_len(pci_dev,0));
1187 
1188 
1189 	v4l2_device_unregister(&dev->v4l2_dev);
1190 
1191 	/* free memory */
1192 	kfree(dev);
1193 }
1194 
1195 #ifdef CONFIG_PM
1196 
1197 /* resends a current buffer in queue after resume */
1198 static int saa7134_buffer_requeue(struct saa7134_dev *dev,
1199 				  struct saa7134_dmaqueue *q)
1200 {
1201 	struct saa7134_buf *buf, *next;
1202 
1203 	assert_spin_locked(&dev->slock);
1204 
1205 	buf  = q->curr;
1206 	next = buf;
1207 	dprintk("buffer_requeue\n");
1208 
1209 	if (!buf)
1210 		return 0;
1211 
1212 	dprintk("buffer_requeue : resending active buffers \n");
1213 
1214 	if (!list_empty(&q->queue))
1215 		next = list_entry(q->queue.next, struct saa7134_buf,
1216 					  entry);
1217 	buf->activate(dev, buf, next);
1218 
1219 	return 0;
1220 }
1221 
1222 static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1223 {
1224 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1225 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1226 
1227 	/* disable overlay - apps should enable it explicitly on resume*/
1228 	dev->ovenable = 0;
1229 
1230 	/* Disable interrupts, DMA, and rest of the chip*/
1231 	saa_writel(SAA7134_IRQ1, 0);
1232 	saa_writel(SAA7134_IRQ2, 0);
1233 	saa_writel(SAA7134_MAIN_CTRL, 0);
1234 
1235 	dev->insuspend = 1;
1236 	synchronize_irq(pci_dev->irq);
1237 
1238 	/* ACK interrupts once more, just in case,
1239 		since the IRQ handler won't ack them anymore*/
1240 
1241 	saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1242 
1243 	/* Disable timeout timers - if we have active buffers, we will
1244 	   fill them on resume*/
1245 
1246 	del_timer(&dev->video_q.timeout);
1247 	del_timer(&dev->vbi_q.timeout);
1248 	del_timer(&dev->ts_q.timeout);
1249 
1250 	if (dev->remote)
1251 		saa7134_ir_stop(dev);
1252 
1253 	pci_save_state(pci_dev);
1254 	pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1255 
1256 	return 0;
1257 }
1258 
1259 static int saa7134_resume(struct pci_dev *pci_dev)
1260 {
1261 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1262 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1263 	unsigned long flags;
1264 
1265 	pci_set_power_state(pci_dev, PCI_D0);
1266 	pci_restore_state(pci_dev);
1267 
1268 	/* Do things that are done in saa7134_initdev ,
1269 		except of initializing memory structures.*/
1270 
1271 	saa7134_board_init1(dev);
1272 
1273 	/* saa7134_hwinit1 */
1274 	if (saa7134_boards[dev->board].video_out)
1275 		saa7134_videoport_init(dev);
1276 	if (card_has_mpeg(dev))
1277 		saa7134_ts_init_hw(dev);
1278 	if (dev->remote)
1279 		saa7134_ir_start(dev);
1280 	saa7134_hw_enable1(dev);
1281 
1282 	msleep(100);
1283 
1284 	saa7134_board_init2(dev);
1285 
1286 	/*saa7134_hwinit2*/
1287 	saa7134_set_tvnorm_hw(dev);
1288 	saa7134_tvaudio_setmute(dev);
1289 	saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1290 	saa7134_tvaudio_init(dev);
1291 	saa7134_enable_i2s(dev);
1292 	saa7134_hw_enable2(dev);
1293 
1294 	saa7134_irq_video_signalchange(dev);
1295 
1296 	/*resume unfinished buffer(s)*/
1297 	spin_lock_irqsave(&dev->slock, flags);
1298 	saa7134_buffer_requeue(dev, &dev->video_q);
1299 	saa7134_buffer_requeue(dev, &dev->vbi_q);
1300 	saa7134_buffer_requeue(dev, &dev->ts_q);
1301 
1302 	/* FIXME: Disable DMA audio sound - temporary till proper support
1303 		  is implemented*/
1304 
1305 	dev->dmasound.dma_running = 0;
1306 
1307 	/* start DMA now*/
1308 	dev->insuspend = 0;
1309 	smp_wmb();
1310 	saa7134_set_dmabits(dev);
1311 	spin_unlock_irqrestore(&dev->slock, flags);
1312 
1313 	return 0;
1314 }
1315 #endif
1316 
1317 /* ----------------------------------------------------------- */
1318 
1319 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1320 {
1321 	struct saa7134_dev *dev;
1322 
1323 	mutex_lock(&saa7134_devlist_lock);
1324 	list_for_each_entry(dev, &saa7134_devlist, devlist)
1325 		mpeg_ops_attach(ops, dev);
1326 	list_add_tail(&ops->next,&mops_list);
1327 	mutex_unlock(&saa7134_devlist_lock);
1328 	return 0;
1329 }
1330 
1331 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1332 {
1333 	struct saa7134_dev *dev;
1334 
1335 	mutex_lock(&saa7134_devlist_lock);
1336 	list_del(&ops->next);
1337 	list_for_each_entry(dev, &saa7134_devlist, devlist)
1338 		mpeg_ops_detach(ops, dev);
1339 	mutex_unlock(&saa7134_devlist_lock);
1340 }
1341 
1342 EXPORT_SYMBOL(saa7134_ts_register);
1343 EXPORT_SYMBOL(saa7134_ts_unregister);
1344 
1345 /* ----------------------------------------------------------- */
1346 
1347 static struct pci_driver saa7134_pci_driver = {
1348 	.name     = "saa7134",
1349 	.id_table = saa7134_pci_tbl,
1350 	.probe    = saa7134_initdev,
1351 	.remove   = saa7134_finidev,
1352 #ifdef CONFIG_PM
1353 	.suspend  = saa7134_suspend,
1354 	.resume   = saa7134_resume
1355 #endif
1356 };
1357 
1358 static int __init saa7134_init(void)
1359 {
1360 	INIT_LIST_HEAD(&saa7134_devlist);
1361 	printk(KERN_INFO "saa7130/34: v4l2 driver version %s loaded\n",
1362 	       SAA7134_VERSION);
1363 	return pci_register_driver(&saa7134_pci_driver);
1364 }
1365 
1366 static void __exit saa7134_fini(void)
1367 {
1368 	pci_unregister_driver(&saa7134_pci_driver);
1369 }
1370 
1371 module_init(saa7134_init);
1372 module_exit(saa7134_fini);
1373 
1374 /* ----------------------------------------------------------- */
1375 
1376 EXPORT_SYMBOL(saa7134_set_gpio);
1377 EXPORT_SYMBOL(saa7134_boards);
1378 
1379 /* ----------------- for the DMA sound modules --------------- */
1380 
1381 EXPORT_SYMBOL(saa7134_dmasound_init);
1382 EXPORT_SYMBOL(saa7134_dmasound_exit);
1383 EXPORT_SYMBOL(saa7134_pgtable_free);
1384 EXPORT_SYMBOL(saa7134_pgtable_build);
1385 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1386 EXPORT_SYMBOL(saa7134_set_dmabits);
1387