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