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 
saa7134_track_gpio(struct saa7134_dev * dev,char * msg)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 
saa7134_set_gpio(struct saa7134_dev * dev,int bit_no,int value)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 
request_module_async(struct work_struct * work)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 
request_submodules(struct saa7134_dev * dev)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 
flush_request_submodules(struct saa7134_dev * dev)171 static void flush_request_submodules(struct saa7134_dev *dev)
172 {
173 	flush_work_sync(&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 */
saa7134_buffer_pages(int 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) */
saa7134_buffer_count(unsigned int size,unsigned int count)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 
saa7134_buffer_startpage(struct saa7134_buf * buf)204 int saa7134_buffer_startpage(struct saa7134_buf *buf)
205 {
206 	return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
207 }
208 
saa7134_buffer_base(struct saa7134_buf * buf)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 
saa7134_pgtable_alloc(struct pci_dev * pci,struct saa7134_pgtable * pt)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 
saa7134_pgtable_build(struct pci_dev * pci,struct saa7134_pgtable * pt,struct scatterlist * list,unsigned int length,unsigned int startpage)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 
saa7134_pgtable_free(struct pci_dev * pci,struct saa7134_pgtable * pt)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 
saa7134_dma_free(struct videobuf_queue * q,struct saa7134_buf * buf)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 
saa7134_buffer_queue(struct saa7134_dev * dev,struct saa7134_dmaqueue * q,struct saa7134_buf * buf)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 
saa7134_buffer_finish(struct saa7134_dev * dev,struct saa7134_dmaqueue * q,unsigned int state)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 	do_gettimeofday(&q->curr->vb.ts);
312 	wake_up(&q->curr->vb.done);
313 	q->curr = NULL;
314 }
315 
saa7134_buffer_next(struct saa7134_dev * dev,struct saa7134_dmaqueue * q)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 
saa7134_buffer_timeout(unsigned long data)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 
saa7134_set_dmabits(struct saa7134_dev * dev)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 
print_irqstatus(struct saa7134_dev * dev,int loop,unsigned long report,unsigned long status)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 
saa7134_irq(int irq,void * dev_id)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 
saa7134_hw_enable1(struct saa7134_dev * dev)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 
saa7134_hwinit1(struct saa7134_dev * dev)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) */
saa7134_hw_enable2(struct saa7134_dev * dev)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 
saa7134_hwinit2(struct saa7134_dev * dev)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 */
saa7134_hwfini(struct saa7134_dev * dev)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 
must_configure_manually(int has_eeprom)757 static void __devinit 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 
vdev_init(struct saa7134_dev * dev,struct video_device * template,char * type)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 	video_set_drvdata(vfd, dev);
809 	return vfd;
810 }
811 
saa7134_unregister_video(struct saa7134_dev * dev)812 static void saa7134_unregister_video(struct saa7134_dev *dev)
813 {
814 	if (dev->video_dev) {
815 		if (video_is_registered(dev->video_dev))
816 			video_unregister_device(dev->video_dev);
817 		else
818 			video_device_release(dev->video_dev);
819 		dev->video_dev = NULL;
820 	}
821 	if (dev->vbi_dev) {
822 		if (video_is_registered(dev->vbi_dev))
823 			video_unregister_device(dev->vbi_dev);
824 		else
825 			video_device_release(dev->vbi_dev);
826 		dev->vbi_dev = NULL;
827 	}
828 	if (dev->radio_dev) {
829 		if (video_is_registered(dev->radio_dev))
830 			video_unregister_device(dev->radio_dev);
831 		else
832 			video_device_release(dev->radio_dev);
833 		dev->radio_dev = NULL;
834 	}
835 }
836 
mpeg_ops_attach(struct saa7134_mpeg_ops * ops,struct saa7134_dev * dev)837 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
838 			    struct saa7134_dev *dev)
839 {
840 	int err;
841 
842 	if (NULL != dev->mops)
843 		return;
844 	if (saa7134_boards[dev->board].mpeg != ops->type)
845 		return;
846 	err = ops->init(dev);
847 	if (0 != err)
848 		return;
849 	dev->mops = ops;
850 }
851 
mpeg_ops_detach(struct saa7134_mpeg_ops * ops,struct saa7134_dev * dev)852 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
853 			    struct saa7134_dev *dev)
854 {
855 	if (NULL == dev->mops)
856 		return;
857 	if (dev->mops != ops)
858 		return;
859 	dev->mops->fini(dev);
860 	dev->mops = NULL;
861 }
862 
saa7134_initdev(struct pci_dev * pci_dev,const struct pci_device_id * pci_id)863 static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
864 				     const struct pci_device_id *pci_id)
865 {
866 	struct saa7134_dev *dev;
867 	struct saa7134_mpeg_ops *mops;
868 	int err;
869 
870 	if (saa7134_devcount == SAA7134_MAXBOARDS)
871 		return -ENOMEM;
872 
873 	dev = kzalloc(sizeof(*dev),GFP_KERNEL);
874 	if (NULL == dev)
875 		return -ENOMEM;
876 
877 	err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
878 	if (err)
879 		goto fail0;
880 
881 	/* pci init */
882 	dev->pci = pci_dev;
883 	if (pci_enable_device(pci_dev)) {
884 		err = -EIO;
885 		goto fail1;
886 	}
887 
888 	dev->nr = saa7134_devcount;
889 	sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
890 
891 	/* pci quirks */
892 	if (pci_pci_problems) {
893 		if (pci_pci_problems & PCIPCI_TRITON)
894 			printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
895 		if (pci_pci_problems & PCIPCI_NATOMA)
896 			printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
897 		if (pci_pci_problems & PCIPCI_VIAETBF)
898 			printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
899 		if (pci_pci_problems & PCIPCI_VSFX)
900 			printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
901 #ifdef PCIPCI_ALIMAGIK
902 		if (pci_pci_problems & PCIPCI_ALIMAGIK) {
903 			printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
904 			       dev->name);
905 			latency = 0x0A;
906 		}
907 #endif
908 		if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
909 			printk(KERN_INFO "%s: quirk: this driver and your "
910 					"chipset may not work together"
911 					" in overlay mode.\n",dev->name);
912 			if (!saa7134_no_overlay) {
913 				printk(KERN_INFO "%s: quirk: overlay "
914 						"mode will be disabled.\n",
915 						dev->name);
916 				saa7134_no_overlay = 1;
917 			} else {
918 				printk(KERN_INFO "%s: quirk: overlay "
919 						"mode will be forced. Use this"
920 						" option at your own risk.\n",
921 						dev->name);
922 			}
923 		}
924 	}
925 	if (UNSET != latency) {
926 		printk(KERN_INFO "%s: setting pci latency timer to %d\n",
927 		       dev->name,latency);
928 		pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
929 	}
930 
931 	/* print pci info */
932 	dev->pci_rev = pci_dev->revision;
933 	pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
934 	printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
935 	       "latency: %d, mmio: 0x%llx\n", dev->name,
936 	       pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
937 	       dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
938 	pci_set_master(pci_dev);
939 	if (!pci_dma_supported(pci_dev, DMA_BIT_MASK(32))) {
940 		printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
941 		err = -EIO;
942 		goto fail1;
943 	}
944 
945 	/* board config */
946 	dev->board = pci_id->driver_data;
947 	if (card[dev->nr] >= 0 &&
948 	    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 	v4l2_prio_init(&dev->prio);
1033 
1034 	mutex_lock(&saa7134_devlist_lock);
1035 	list_for_each_entry(mops, &mops_list, next)
1036 		mpeg_ops_attach(mops, dev);
1037 	list_add_tail(&dev->devlist, &saa7134_devlist);
1038 	mutex_unlock(&saa7134_devlist_lock);
1039 
1040 	/* check for signal */
1041 	saa7134_irq_video_signalchange(dev);
1042 
1043 	if (TUNER_ABSENT != dev->tuner_type)
1044 		saa_call_all(dev, core, s_power, 0);
1045 
1046 	/* register v4l devices */
1047 	if (saa7134_no_overlay > 0)
1048 		printk(KERN_INFO "%s: Overlay support disabled.\n", dev->name);
1049 
1050 	dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
1051 	err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1052 				    video_nr[dev->nr]);
1053 	if (err < 0) {
1054 		printk(KERN_INFO "%s: can't register video device\n",
1055 		       dev->name);
1056 		goto fail4;
1057 	}
1058 	printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1059 	       dev->name, video_device_node_name(dev->video_dev));
1060 
1061 	dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
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 		err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1073 					    radio_nr[dev->nr]);
1074 		if (err < 0)
1075 			goto fail4;
1076 		printk(KERN_INFO "%s: registered device %s\n",
1077 		       dev->name, video_device_node_name(dev->radio_dev));
1078 	}
1079 
1080 	/* everything worked */
1081 	saa7134_devcount++;
1082 
1083 	if (saa7134_dmasound_init && !dev->dmasound.priv_data)
1084 		saa7134_dmasound_init(dev);
1085 
1086 	request_submodules(dev);
1087 	return 0;
1088 
1089  fail4:
1090 	saa7134_unregister_video(dev);
1091 	saa7134_i2c_unregister(dev);
1092 	free_irq(pci_dev->irq, dev);
1093  fail3:
1094 	saa7134_hwfini(dev);
1095 	iounmap(dev->lmmio);
1096  fail2:
1097 	release_mem_region(pci_resource_start(pci_dev,0),
1098 			   pci_resource_len(pci_dev,0));
1099  fail1:
1100 	v4l2_device_unregister(&dev->v4l2_dev);
1101  fail0:
1102 	kfree(dev);
1103 	return err;
1104 }
1105 
saa7134_finidev(struct pci_dev * pci_dev)1106 static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1107 {
1108 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1109 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1110 	struct saa7134_mpeg_ops *mops;
1111 
1112 	flush_request_submodules(dev);
1113 
1114 	/* Release DMA sound modules if present */
1115 	if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1116 		saa7134_dmasound_exit(dev);
1117 	}
1118 
1119 	/* debugging ... */
1120 	if (irq_debug) {
1121 		u32 report = saa_readl(SAA7134_IRQ_REPORT);
1122 		u32 status = saa_readl(SAA7134_IRQ_STATUS);
1123 		print_irqstatus(dev,42,report,status);
1124 	}
1125 
1126 	/* disable peripheral devices */
1127 	saa_writeb(SAA7134_SPECIAL_MODE,0);
1128 
1129 	/* shutdown hardware */
1130 	saa_writel(SAA7134_IRQ1,0);
1131 	saa_writel(SAA7134_IRQ2,0);
1132 	saa_writel(SAA7134_MAIN_CTRL,0);
1133 
1134 	/* shutdown subsystems */
1135 	saa7134_hwfini(dev);
1136 
1137 	/* unregister */
1138 	mutex_lock(&saa7134_devlist_lock);
1139 	list_del(&dev->devlist);
1140 	list_for_each_entry(mops, &mops_list, next)
1141 		mpeg_ops_detach(mops, dev);
1142 	mutex_unlock(&saa7134_devlist_lock);
1143 	saa7134_devcount--;
1144 
1145 	saa7134_i2c_unregister(dev);
1146 	saa7134_unregister_video(dev);
1147 
1148 
1149 	/* the DMA sound modules should be unloaded before reaching
1150 	   this, but just in case they are still present... */
1151 	if (dev->dmasound.priv_data != NULL) {
1152 		free_irq(pci_dev->irq, &dev->dmasound);
1153 		dev->dmasound.priv_data = NULL;
1154 	}
1155 
1156 
1157 	/* release resources */
1158 	free_irq(pci_dev->irq, dev);
1159 	iounmap(dev->lmmio);
1160 	release_mem_region(pci_resource_start(pci_dev,0),
1161 			   pci_resource_len(pci_dev,0));
1162 
1163 
1164 	v4l2_device_unregister(&dev->v4l2_dev);
1165 
1166 	/* free memory */
1167 	kfree(dev);
1168 }
1169 
1170 #ifdef CONFIG_PM
1171 
1172 /* resends a current buffer in queue after resume */
saa7134_buffer_requeue(struct saa7134_dev * dev,struct saa7134_dmaqueue * q)1173 static int saa7134_buffer_requeue(struct saa7134_dev *dev,
1174 				  struct saa7134_dmaqueue *q)
1175 {
1176 	struct saa7134_buf *buf, *next;
1177 
1178 	assert_spin_locked(&dev->slock);
1179 
1180 	buf  = q->curr;
1181 	next = buf;
1182 	dprintk("buffer_requeue\n");
1183 
1184 	if (!buf)
1185 		return 0;
1186 
1187 	dprintk("buffer_requeue : resending active buffers \n");
1188 
1189 	if (!list_empty(&q->queue))
1190 		next = list_entry(q->queue.next, struct saa7134_buf,
1191 					  vb.queue);
1192 	buf->activate(dev, buf, next);
1193 
1194 	return 0;
1195 }
1196 
saa7134_suspend(struct pci_dev * pci_dev,pm_message_t state)1197 static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1198 {
1199 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1200 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1201 
1202 	/* disable overlay - apps should enable it explicitly on resume*/
1203 	dev->ovenable = 0;
1204 
1205 	/* Disable interrupts, DMA, and rest of the chip*/
1206 	saa_writel(SAA7134_IRQ1, 0);
1207 	saa_writel(SAA7134_IRQ2, 0);
1208 	saa_writel(SAA7134_MAIN_CTRL, 0);
1209 
1210 	dev->insuspend = 1;
1211 	synchronize_irq(pci_dev->irq);
1212 
1213 	/* ACK interrupts once more, just in case,
1214 		since the IRQ handler won't ack them anymore*/
1215 
1216 	saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1217 
1218 	/* Disable timeout timers - if we have active buffers, we will
1219 	   fill them on resume*/
1220 
1221 	del_timer(&dev->video_q.timeout);
1222 	del_timer(&dev->vbi_q.timeout);
1223 	del_timer(&dev->ts_q.timeout);
1224 
1225 	if (dev->remote)
1226 		saa7134_ir_stop(dev);
1227 
1228 	pci_save_state(pci_dev);
1229 	pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1230 
1231 	return 0;
1232 }
1233 
saa7134_resume(struct pci_dev * pci_dev)1234 static int saa7134_resume(struct pci_dev *pci_dev)
1235 {
1236 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1237 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1238 	unsigned long flags;
1239 
1240 	pci_set_power_state(pci_dev, PCI_D0);
1241 	pci_restore_state(pci_dev);
1242 
1243 	/* Do things that are done in saa7134_initdev ,
1244 		except of initializing memory structures.*/
1245 
1246 	saa7134_board_init1(dev);
1247 
1248 	/* saa7134_hwinit1 */
1249 	if (saa7134_boards[dev->board].video_out)
1250 		saa7134_videoport_init(dev);
1251 	if (card_has_mpeg(dev))
1252 		saa7134_ts_init_hw(dev);
1253 	if (dev->remote)
1254 		saa7134_ir_start(dev);
1255 	saa7134_hw_enable1(dev);
1256 
1257 	msleep(100);
1258 
1259 	saa7134_board_init2(dev);
1260 
1261 	/*saa7134_hwinit2*/
1262 	saa7134_set_tvnorm_hw(dev);
1263 	saa7134_tvaudio_setmute(dev);
1264 	saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1265 	saa7134_tvaudio_init(dev);
1266 	saa7134_enable_i2s(dev);
1267 	saa7134_hw_enable2(dev);
1268 
1269 	saa7134_irq_video_signalchange(dev);
1270 
1271 	/*resume unfinished buffer(s)*/
1272 	spin_lock_irqsave(&dev->slock, flags);
1273 	saa7134_buffer_requeue(dev, &dev->video_q);
1274 	saa7134_buffer_requeue(dev, &dev->vbi_q);
1275 	saa7134_buffer_requeue(dev, &dev->ts_q);
1276 
1277 	/* FIXME: Disable DMA audio sound - temporary till proper support
1278 		  is implemented*/
1279 
1280 	dev->dmasound.dma_running = 0;
1281 
1282 	/* start DMA now*/
1283 	dev->insuspend = 0;
1284 	smp_wmb();
1285 	saa7134_set_dmabits(dev);
1286 	spin_unlock_irqrestore(&dev->slock, flags);
1287 
1288 	return 0;
1289 }
1290 #endif
1291 
1292 /* ----------------------------------------------------------- */
1293 
saa7134_ts_register(struct saa7134_mpeg_ops * ops)1294 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1295 {
1296 	struct saa7134_dev *dev;
1297 
1298 	mutex_lock(&saa7134_devlist_lock);
1299 	list_for_each_entry(dev, &saa7134_devlist, devlist)
1300 		mpeg_ops_attach(ops, dev);
1301 	list_add_tail(&ops->next,&mops_list);
1302 	mutex_unlock(&saa7134_devlist_lock);
1303 	return 0;
1304 }
1305 
saa7134_ts_unregister(struct saa7134_mpeg_ops * ops)1306 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1307 {
1308 	struct saa7134_dev *dev;
1309 
1310 	mutex_lock(&saa7134_devlist_lock);
1311 	list_del(&ops->next);
1312 	list_for_each_entry(dev, &saa7134_devlist, devlist)
1313 		mpeg_ops_detach(ops, dev);
1314 	mutex_unlock(&saa7134_devlist_lock);
1315 }
1316 
1317 EXPORT_SYMBOL(saa7134_ts_register);
1318 EXPORT_SYMBOL(saa7134_ts_unregister);
1319 
1320 /* ----------------------------------------------------------- */
1321 
1322 static struct pci_driver saa7134_pci_driver = {
1323 	.name     = "saa7134",
1324 	.id_table = saa7134_pci_tbl,
1325 	.probe    = saa7134_initdev,
1326 	.remove   = __devexit_p(saa7134_finidev),
1327 #ifdef CONFIG_PM
1328 	.suspend  = saa7134_suspend,
1329 	.resume   = saa7134_resume
1330 #endif
1331 };
1332 
saa7134_init(void)1333 static int __init saa7134_init(void)
1334 {
1335 	INIT_LIST_HEAD(&saa7134_devlist);
1336 	printk(KERN_INFO "saa7130/34: v4l2 driver version %s loaded\n",
1337 	       SAA7134_VERSION);
1338 	return pci_register_driver(&saa7134_pci_driver);
1339 }
1340 
saa7134_fini(void)1341 static void __exit saa7134_fini(void)
1342 {
1343 	pci_unregister_driver(&saa7134_pci_driver);
1344 }
1345 
1346 module_init(saa7134_init);
1347 module_exit(saa7134_fini);
1348 
1349 /* ----------------------------------------------------------- */
1350 
1351 EXPORT_SYMBOL(saa7134_set_gpio);
1352 EXPORT_SYMBOL(saa7134_boards);
1353 
1354 /* ----------------- for the DMA sound modules --------------- */
1355 
1356 EXPORT_SYMBOL(saa7134_dmasound_init);
1357 EXPORT_SYMBOL(saa7134_dmasound_exit);
1358 EXPORT_SYMBOL(saa7134_pgtable_free);
1359 EXPORT_SYMBOL(saa7134_pgtable_build);
1360 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1361 EXPORT_SYMBOL(saa7134_set_dmabits);
1362 
1363 /* ----------------------------------------------------------- */
1364 /*
1365  * Local variables:
1366  * c-basic-offset: 8
1367  * End:
1368  */
1369