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