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