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