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 	saa7134_video_fini(dev);
755 	return 0;
756 }
757 
758 static void must_configure_manually(int has_eeprom)
759 {
760 	unsigned int i,p;
761 
762 	if (!has_eeprom)
763 		printk(KERN_WARNING
764 		       "saa7134: <rant>\n"
765 		       "saa7134:  Congratulations!  Your TV card vendor saved a few\n"
766 		       "saa7134:  cents for a eeprom, thus your pci board has no\n"
767 		       "saa7134:  subsystem ID and I can't identify it automatically\n"
768 		       "saa7134: </rant>\n"
769 		       "saa7134: I feel better now.  Ok, here are the good news:\n"
770 		       "saa7134: You can use the card=<nr> insmod option to specify\n"
771 		       "saa7134: which board do you have.  The list:\n");
772 	else
773 		printk(KERN_WARNING
774 		       "saa7134: Board is currently unknown. You might try to use the card=<nr>\n"
775 		       "saa7134: insmod option to specify which board do you have, but this is\n"
776 		       "saa7134: somewhat risky, as might damage your card. It is better to ask\n"
777 		       "saa7134: for support at linux-media@vger.kernel.org.\n"
778 		       "saa7134: The supported cards are:\n");
779 
780 	for (i = 0; i < saa7134_bcount; i++) {
781 		printk(KERN_WARNING "saa7134:   card=%d -> %-40.40s",
782 		       i,saa7134_boards[i].name);
783 		for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
784 			if (saa7134_pci_tbl[p].driver_data != i)
785 				continue;
786 			printk(" %04x:%04x",
787 			       saa7134_pci_tbl[p].subvendor,
788 			       saa7134_pci_tbl[p].subdevice);
789 		}
790 		printk("\n");
791 	}
792 }
793 
794 static struct video_device *vdev_init(struct saa7134_dev *dev,
795 				      struct video_device *template,
796 				      char *type)
797 {
798 	struct video_device *vfd;
799 
800 	vfd = video_device_alloc();
801 	if (NULL == vfd)
802 		return NULL;
803 	*vfd = *template;
804 	vfd->v4l2_dev  = &dev->v4l2_dev;
805 	vfd->release = video_device_release;
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, 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 		dev->empress_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 (dev->empress_sd)
1017 			dev->empress_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 	dev->video_dev->ctrl_handler = &dev->ctrl_handler;
1050 	err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1051 				    video_nr[dev->nr]);
1052 	if (err < 0) {
1053 		printk(KERN_INFO "%s: can't register video device\n",
1054 		       dev->name);
1055 		goto fail4;
1056 	}
1057 	printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1058 	       dev->name, video_device_node_name(dev->video_dev));
1059 
1060 	dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1061 	dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
1062 
1063 	err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1064 				    vbi_nr[dev->nr]);
1065 	if (err < 0)
1066 		goto fail4;
1067 	printk(KERN_INFO "%s: registered device %s\n",
1068 	       dev->name, video_device_node_name(dev->vbi_dev));
1069 
1070 	if (card_has_radio(dev)) {
1071 		dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1072 		dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
1073 		err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1074 					    radio_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->radio_dev));
1079 	}
1080 
1081 	/* everything worked */
1082 	saa7134_devcount++;
1083 
1084 	if (saa7134_dmasound_init && !dev->dmasound.priv_data)
1085 		saa7134_dmasound_init(dev);
1086 
1087 	request_submodules(dev);
1088 	return 0;
1089 
1090  fail4:
1091 	saa7134_unregister_video(dev);
1092 	saa7134_i2c_unregister(dev);
1093 	free_irq(pci_dev->irq, dev);
1094  fail3:
1095 	saa7134_hwfini(dev);
1096 	iounmap(dev->lmmio);
1097  fail2:
1098 	release_mem_region(pci_resource_start(pci_dev,0),
1099 			   pci_resource_len(pci_dev,0));
1100  fail1:
1101 	v4l2_device_unregister(&dev->v4l2_dev);
1102  fail0:
1103 	kfree(dev);
1104 	return err;
1105 }
1106 
1107 static void saa7134_finidev(struct pci_dev *pci_dev)
1108 {
1109 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1110 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1111 	struct saa7134_mpeg_ops *mops;
1112 
1113 	flush_request_submodules(dev);
1114 
1115 	/* Release DMA sound modules if present */
1116 	if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1117 		saa7134_dmasound_exit(dev);
1118 	}
1119 
1120 	/* debugging ... */
1121 	if (irq_debug) {
1122 		u32 report = saa_readl(SAA7134_IRQ_REPORT);
1123 		u32 status = saa_readl(SAA7134_IRQ_STATUS);
1124 		print_irqstatus(dev,42,report,status);
1125 	}
1126 
1127 	/* disable peripheral devices */
1128 	saa_writeb(SAA7134_SPECIAL_MODE,0);
1129 
1130 	/* shutdown hardware */
1131 	saa_writel(SAA7134_IRQ1,0);
1132 	saa_writel(SAA7134_IRQ2,0);
1133 	saa_writel(SAA7134_MAIN_CTRL,0);
1134 
1135 	/* shutdown subsystems */
1136 	saa7134_hwfini(dev);
1137 
1138 	/* unregister */
1139 	mutex_lock(&saa7134_devlist_lock);
1140 	list_del(&dev->devlist);
1141 	list_for_each_entry(mops, &mops_list, next)
1142 		mpeg_ops_detach(mops, dev);
1143 	mutex_unlock(&saa7134_devlist_lock);
1144 	saa7134_devcount--;
1145 
1146 	saa7134_i2c_unregister(dev);
1147 	saa7134_unregister_video(dev);
1148 
1149 
1150 	/* the DMA sound modules should be unloaded before reaching
1151 	   this, but just in case they are still present... */
1152 	if (dev->dmasound.priv_data != NULL) {
1153 		free_irq(pci_dev->irq, &dev->dmasound);
1154 		dev->dmasound.priv_data = NULL;
1155 	}
1156 
1157 
1158 	/* release resources */
1159 	free_irq(pci_dev->irq, dev);
1160 	iounmap(dev->lmmio);
1161 	release_mem_region(pci_resource_start(pci_dev,0),
1162 			   pci_resource_len(pci_dev,0));
1163 
1164 
1165 	v4l2_device_unregister(&dev->v4l2_dev);
1166 
1167 	/* free memory */
1168 	kfree(dev);
1169 }
1170 
1171 #ifdef CONFIG_PM
1172 
1173 /* resends a current buffer in queue after resume */
1174 static int saa7134_buffer_requeue(struct saa7134_dev *dev,
1175 				  struct saa7134_dmaqueue *q)
1176 {
1177 	struct saa7134_buf *buf, *next;
1178 
1179 	assert_spin_locked(&dev->slock);
1180 
1181 	buf  = q->curr;
1182 	next = buf;
1183 	dprintk("buffer_requeue\n");
1184 
1185 	if (!buf)
1186 		return 0;
1187 
1188 	dprintk("buffer_requeue : resending active buffers \n");
1189 
1190 	if (!list_empty(&q->queue))
1191 		next = list_entry(q->queue.next, struct saa7134_buf,
1192 					  vb.queue);
1193 	buf->activate(dev, buf, next);
1194 
1195 	return 0;
1196 }
1197 
1198 static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1199 {
1200 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1201 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1202 
1203 	/* disable overlay - apps should enable it explicitly on resume*/
1204 	dev->ovenable = 0;
1205 
1206 	/* Disable interrupts, DMA, and rest of the chip*/
1207 	saa_writel(SAA7134_IRQ1, 0);
1208 	saa_writel(SAA7134_IRQ2, 0);
1209 	saa_writel(SAA7134_MAIN_CTRL, 0);
1210 
1211 	dev->insuspend = 1;
1212 	synchronize_irq(pci_dev->irq);
1213 
1214 	/* ACK interrupts once more, just in case,
1215 		since the IRQ handler won't ack them anymore*/
1216 
1217 	saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1218 
1219 	/* Disable timeout timers - if we have active buffers, we will
1220 	   fill them on resume*/
1221 
1222 	del_timer(&dev->video_q.timeout);
1223 	del_timer(&dev->vbi_q.timeout);
1224 	del_timer(&dev->ts_q.timeout);
1225 
1226 	if (dev->remote)
1227 		saa7134_ir_stop(dev);
1228 
1229 	pci_save_state(pci_dev);
1230 	pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1231 
1232 	return 0;
1233 }
1234 
1235 static int saa7134_resume(struct pci_dev *pci_dev)
1236 {
1237 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1238 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1239 	unsigned long flags;
1240 
1241 	pci_set_power_state(pci_dev, PCI_D0);
1242 	pci_restore_state(pci_dev);
1243 
1244 	/* Do things that are done in saa7134_initdev ,
1245 		except of initializing memory structures.*/
1246 
1247 	saa7134_board_init1(dev);
1248 
1249 	/* saa7134_hwinit1 */
1250 	if (saa7134_boards[dev->board].video_out)
1251 		saa7134_videoport_init(dev);
1252 	if (card_has_mpeg(dev))
1253 		saa7134_ts_init_hw(dev);
1254 	if (dev->remote)
1255 		saa7134_ir_start(dev);
1256 	saa7134_hw_enable1(dev);
1257 
1258 	msleep(100);
1259 
1260 	saa7134_board_init2(dev);
1261 
1262 	/*saa7134_hwinit2*/
1263 	saa7134_set_tvnorm_hw(dev);
1264 	saa7134_tvaudio_setmute(dev);
1265 	saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1266 	saa7134_tvaudio_init(dev);
1267 	saa7134_enable_i2s(dev);
1268 	saa7134_hw_enable2(dev);
1269 
1270 	saa7134_irq_video_signalchange(dev);
1271 
1272 	/*resume unfinished buffer(s)*/
1273 	spin_lock_irqsave(&dev->slock, flags);
1274 	saa7134_buffer_requeue(dev, &dev->video_q);
1275 	saa7134_buffer_requeue(dev, &dev->vbi_q);
1276 	saa7134_buffer_requeue(dev, &dev->ts_q);
1277 
1278 	/* FIXME: Disable DMA audio sound - temporary till proper support
1279 		  is implemented*/
1280 
1281 	dev->dmasound.dma_running = 0;
1282 
1283 	/* start DMA now*/
1284 	dev->insuspend = 0;
1285 	smp_wmb();
1286 	saa7134_set_dmabits(dev);
1287 	spin_unlock_irqrestore(&dev->slock, flags);
1288 
1289 	return 0;
1290 }
1291 #endif
1292 
1293 /* ----------------------------------------------------------- */
1294 
1295 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1296 {
1297 	struct saa7134_dev *dev;
1298 
1299 	mutex_lock(&saa7134_devlist_lock);
1300 	list_for_each_entry(dev, &saa7134_devlist, devlist)
1301 		mpeg_ops_attach(ops, dev);
1302 	list_add_tail(&ops->next,&mops_list);
1303 	mutex_unlock(&saa7134_devlist_lock);
1304 	return 0;
1305 }
1306 
1307 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1308 {
1309 	struct saa7134_dev *dev;
1310 
1311 	mutex_lock(&saa7134_devlist_lock);
1312 	list_del(&ops->next);
1313 	list_for_each_entry(dev, &saa7134_devlist, devlist)
1314 		mpeg_ops_detach(ops, dev);
1315 	mutex_unlock(&saa7134_devlist_lock);
1316 }
1317 
1318 EXPORT_SYMBOL(saa7134_ts_register);
1319 EXPORT_SYMBOL(saa7134_ts_unregister);
1320 
1321 /* ----------------------------------------------------------- */
1322 
1323 static struct pci_driver saa7134_pci_driver = {
1324 	.name     = "saa7134",
1325 	.id_table = saa7134_pci_tbl,
1326 	.probe    = saa7134_initdev,
1327 	.remove   = saa7134_finidev,
1328 #ifdef CONFIG_PM
1329 	.suspend  = saa7134_suspend,
1330 	.resume   = saa7134_resume
1331 #endif
1332 };
1333 
1334 static int __init saa7134_init(void)
1335 {
1336 	INIT_LIST_HEAD(&saa7134_devlist);
1337 	printk(KERN_INFO "saa7130/34: v4l2 driver version %s loaded\n",
1338 	       SAA7134_VERSION);
1339 	return pci_register_driver(&saa7134_pci_driver);
1340 }
1341 
1342 static void __exit saa7134_fini(void)
1343 {
1344 	pci_unregister_driver(&saa7134_pci_driver);
1345 }
1346 
1347 module_init(saa7134_init);
1348 module_exit(saa7134_fini);
1349 
1350 /* ----------------------------------------------------------- */
1351 
1352 EXPORT_SYMBOL(saa7134_set_gpio);
1353 EXPORT_SYMBOL(saa7134_boards);
1354 
1355 /* ----------------- for the DMA sound modules --------------- */
1356 
1357 EXPORT_SYMBOL(saa7134_dmasound_init);
1358 EXPORT_SYMBOL(saa7134_dmasound_exit);
1359 EXPORT_SYMBOL(saa7134_pgtable_free);
1360 EXPORT_SYMBOL(saa7134_pgtable_build);
1361 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1362 EXPORT_SYMBOL(saa7134_set_dmabits);
1363 
1364 /* ----------------------------------------------------------- */
1365 /*
1366  * Local variables:
1367  * c-basic-offset: 8
1368  * End:
1369  */
1370