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