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