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