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