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