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