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