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