3 * device driver for philips saa7134 based TV cards
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
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.
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>
36 #include "saa7134-reg.h"
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");
43 /* ------------------------------------------------------------------ */
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]");
49 static unsigned int core_debug = 0;
50 module_param(core_debug, int, 0644);
51 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
53 static unsigned int gpio_tracking = 0;
54 module_param(gpio_tracking, int, 0644);
55 MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
57 static unsigned int alsa = 0;
58 module_param(alsa, int, 0644);
59 MODULE_PARM_DESC(alsa,"enable ALSA DMA sound [dmasound]");
61 static unsigned int oss = 0;
62 module_param(oss, int, 0644);
63 MODULE_PARM_DESC(oss,"enable OSS DMA sound [dmasound]");
65 static unsigned int latency = UNSET;
66 module_param(latency, int, 0444);
67 MODULE_PARM_DESC(latency,"pci latency timer");
69 static int no_overlay=-1;
70 module_param(no_overlay, int, 0444);
71 MODULE_PARM_DESC(no_overlay,"allow override overlay default (0 disables, 1 enables)"
72 " [some VIA/SIS chipsets are known to have problem with overlay]");
74 static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
75 static unsigned int vbi_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
76 static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
77 static unsigned int tuner[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
78 static unsigned int card[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
81 module_param_array(video_nr, int, NULL, 0444);
82 module_param_array(vbi_nr, int, NULL, 0444);
83 module_param_array(radio_nr, int, NULL, 0444);
84 module_param_array(tuner, int, NULL, 0444);
85 module_param_array(card, int, NULL, 0444);
87 MODULE_PARM_DESC(video_nr, "video device number");
88 MODULE_PARM_DESC(vbi_nr, "vbi device number");
89 MODULE_PARM_DESC(radio_nr, "radio device number");
90 MODULE_PARM_DESC(tuner, "tuner type");
91 MODULE_PARM_DESC(card, "card type");
93 static DEFINE_MUTEX(devlist_lock);
94 LIST_HEAD(saa7134_devlist);
95 static LIST_HEAD(mops_list);
96 static unsigned int saa7134_devcount;
98 int (*dmasound_init)(struct saa7134_dev *dev);
99 int (*dmasound_exit)(struct saa7134_dev *dev);
101 #define dprintk(fmt, arg...) if (core_debug) \
102 printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
104 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
106 unsigned long mode,status;
110 /* rising SAA7134_GPIO_GPRESCAN reads the status */
111 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
112 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
113 mode = saa_readl(SAA7134_GPIO_GPMODE0 >> 2) & 0xfffffff;
114 status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
116 "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
117 dev->name, mode, (~mode) & status, mode & status, msg);
120 /* ------------------------------------------------------------------ */
123 /* ----------------------------------------------------------- */
124 /* delayed request_module */
126 #if defined(CONFIG_MODULES) && defined(MODULE)
127 static int need_empress;
129 static int need_alsa;
132 static int pending_call(struct notifier_block *self, unsigned long state,
135 if (module != THIS_MODULE || state != MODULE_STATE_LIVE)
139 request_module("saa7134-empress");
141 request_module("saa7134-dvb");
143 request_module("saa7134-alsa");
145 request_module("saa7134-oss");
149 static int pending_registered;
150 static struct notifier_block pending_notifier = {
151 .notifier_call = pending_call,
154 static void request_module_depend(char *name, int *flag)
157 switch (THIS_MODULE->state) {
158 case MODULE_STATE_COMING:
159 if (!pending_registered) {
160 err = register_module_notifier(&pending_notifier);
161 pending_registered = 1;
165 case MODULE_STATE_LIVE:
166 request_module(name);
175 #define request_module_depend(name,flag)
176 #endif /* CONFIG_MODULES */
178 /* ------------------------------------------------------------------ */
180 /* nr of (saa7134-)pages for the given buffer size */
181 static int saa7134_buffer_pages(int size)
183 size = PAGE_ALIGN(size);
184 size += PAGE_SIZE; /* for non-page-aligned buffers */
189 /* calc max # of buffers from size (must not exceed the 4MB virtual
190 * address space per DMA channel) */
191 int saa7134_buffer_count(unsigned int size, unsigned int count)
193 unsigned int maxcount;
195 maxcount = 1024 / saa7134_buffer_pages(size);
196 if (count > maxcount)
201 int saa7134_buffer_startpage(struct saa7134_buf *buf)
203 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
206 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
210 base = saa7134_buffer_startpage(buf) * 4096;
211 base += buf->vb.dma.sglist[0].offset;
215 /* ------------------------------------------------------------------ */
217 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
222 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
225 pt->size = SAA7134_PGTABLE_SIZE;
231 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
232 struct scatterlist *list, unsigned int length,
233 unsigned int startpage)
238 BUG_ON(NULL == pt || NULL == pt->cpu);
240 ptr = pt->cpu + startpage;
241 for (i = 0; i < length; i++, list++)
242 for (p = 0; p * 4096 < list->length; p++, ptr++)
243 *ptr = cpu_to_le32(sg_dma_address(list) - list->offset);
247 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
251 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
255 /* ------------------------------------------------------------------ */
257 void saa7134_dma_free(struct saa7134_dev *dev,struct saa7134_buf *buf)
262 videobuf_waiton(&buf->vb,0,0);
263 videobuf_dma_pci_unmap(dev->pci, &buf->vb.dma);
264 videobuf_dma_free(&buf->vb.dma);
265 buf->vb.state = STATE_NEEDS_INIT;
268 /* ------------------------------------------------------------------ */
270 int saa7134_buffer_queue(struct saa7134_dev *dev,
271 struct saa7134_dmaqueue *q,
272 struct saa7134_buf *buf)
274 struct saa7134_buf *next = NULL;
276 assert_spin_locked(&dev->slock);
277 dprintk("buffer_queue %p\n",buf);
278 if (NULL == q->curr) {
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;
286 next = list_entry(q->queue.next,struct saa7134_buf,
289 buf->activate(dev,buf,next);
292 list_add_tail(&buf->vb.queue,&q->queue);
293 buf->vb.state = STATE_QUEUED;
298 void saa7134_buffer_finish(struct saa7134_dev *dev,
299 struct saa7134_dmaqueue *q,
302 assert_spin_locked(&dev->slock);
303 dprintk("buffer_finish %p\n",q->curr);
305 /* finish current buffer */
306 q->curr->vb.state = state;
307 do_gettimeofday(&q->curr->vb.ts);
308 wake_up(&q->curr->vb.done);
312 void saa7134_buffer_next(struct saa7134_dev *dev,
313 struct saa7134_dmaqueue *q)
315 struct saa7134_buf *buf,*next = NULL;
317 assert_spin_locked(&dev->slock);
318 BUG_ON(NULL != q->curr);
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,
330 buf->activate(dev,buf,next);
331 dprintk("buffer_next #2 prev=%p/next=%p\n",
332 q->queue.prev,q->queue.next);
334 /* nothing to do -- just stop DMA */
335 dprintk("buffer_next %p\n",NULL);
336 saa7134_set_dmabits(dev);
337 del_timer(&q->timeout);
341 void saa7134_buffer_timeout(unsigned long data)
343 struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
344 struct saa7134_dev *dev = q->dev;
347 spin_lock_irqsave(&dev->slock,flags);
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);
354 /* flag current buffer as failed,
355 try to start over with the next one. */
357 dprintk("timeout on %p\n",q->curr);
358 saa7134_buffer_finish(dev,q,STATE_ERROR);
360 saa7134_buffer_next(dev,q);
361 spin_unlock_irqrestore(&dev->slock,flags);
364 /* ------------------------------------------------------------------ */
366 int saa7134_set_dmabits(struct saa7134_dev *dev)
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;
372 assert_spin_locked(&dev->slock);
374 /* video capture -- dma 0 + video task A */
375 if (dev->video_q.curr) {
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;
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;
390 /* screen overlay -- dma 0 + video task B */
393 ctrl |= SAA7134_MAIN_CTRL_TE1;
397 /* vbi capture -- dma 0 + vbi task A+B */
398 if (dev->vbi_q.curr) {
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;
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;
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;
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);
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);
439 /* odd B, even A, repeat */
440 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
441 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
443 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
444 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
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,
460 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
461 task, ctrl, irq, split ? "no" : "yes");
466 /* ------------------------------------------------------------------ */
467 /* IRQ handler + helpers */
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"
475 #define IRQBITS ARRAY_SIZE(irqbits)
477 static void print_irqstatus(struct saa7134_dev *dev, int loop,
478 unsigned long report, unsigned long status)
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)))
487 printk(" %s",irqbits[i]);
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",
499 static irqreturn_t saa7134_irq(int irq, void *dev_id, struct pt_regs *regs)
501 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
502 unsigned long report,status;
503 int loop, handled = 0;
505 for (loop = 0; loop < 10; loop++) {
506 report = saa_readl(SAA7134_IRQ_REPORT);
507 status = saa_readl(SAA7134_IRQ_STATUS);
510 printk(KERN_DEBUG "%s/irq: no (more) work\n",
515 /* If dmasound support is active and we get a sound report, exit
516 and let the saa7134-alsa/oss module deal with it */
518 if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
519 (dev->dmasound.priv_data != NULL) )
522 printk(KERN_DEBUG "%s/irq: ignoring interrupt for DMA sound\n",
528 saa_writel(SAA7134_IRQ_REPORT,report);
530 print_irqstatus(dev,loop,report,status);
533 if (report & SAA7134_IRQ_REPORT_RDCAP /* _INTL */)
534 saa7134_irq_video_intl(dev);
536 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
537 (status & 0x60) == 0)
538 saa7134_irq_video_done(dev,status);
540 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
541 (status & 0x40) == 0x40)
542 saa7134_irq_vbi_done(dev,status);
544 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
546 saa7134_irq_ts_done(dev,status);
548 if (report & SAA7134_IRQ_REPORT_GPIO16) {
549 switch (dev->has_remote) {
550 case SAA7134_REMOTE_GPIO:
551 if (dev->remote->mask_keydown & 0x10000) {
552 saa7134_input_irq(dev);
556 case SAA7134_REMOTE_I2C:
557 break; /* FIXME: invoke I2C get_key() */
559 default: /* GPIO16 not used by IR remote */
564 if (report & SAA7134_IRQ_REPORT_GPIO18) {
565 switch (dev->has_remote) {
566 case SAA7134_REMOTE_GPIO:
567 if ((dev->remote->mask_keydown & 0x40000) ||
568 (dev->remote->mask_keyup & 0x40000)) {
569 saa7134_input_irq(dev);
573 case SAA7134_REMOTE_I2C:
574 break; /* FIXME: invoke I2C get_key() */
576 default: /* GPIO18 not used by IR remote */
583 print_irqstatus(dev,loop,report,status);
584 if (report & SAA7134_IRQ_REPORT_PE) {
585 /* disable all parity error */
586 printk(KERN_WARNING "%s/irq: looping -- "
587 "clearing PE (parity error!) enable bit\n",dev->name);
588 saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
589 } else if (report & SAA7134_IRQ_REPORT_GPIO16) {
590 /* disable gpio16 IRQ */
591 printk(KERN_WARNING "%s/irq: looping -- "
592 "clearing GPIO16 enable bit\n",dev->name);
593 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16);
594 } else if (report & SAA7134_IRQ_REPORT_GPIO18) {
595 /* disable gpio18 IRQs */
596 printk(KERN_WARNING "%s/irq: looping -- "
597 "clearing GPIO18 enable bit\n",dev->name);
598 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
600 /* disable all irqs */
601 printk(KERN_WARNING "%s/irq: looping -- "
602 "clearing all enable bits\n",dev->name);
603 saa_writel(SAA7134_IRQ1,0);
604 saa_writel(SAA7134_IRQ2,0);
609 return IRQ_RETVAL(handled);
612 /* ------------------------------------------------------------------ */
614 /* early init (no i2c, no irq) */
615 static int saa7134_hwinit1(struct saa7134_dev *dev)
617 dprintk("hwinit1\n");
619 saa_writel(SAA7134_IRQ1, 0);
620 saa_writel(SAA7134_IRQ2, 0);
621 mutex_init(&dev->lock);
622 spin_lock_init(&dev->slock);
624 saa7134_track_gpio(dev,"pre-init");
625 saa7134_video_init1(dev);
626 saa7134_vbi_init1(dev);
627 if (card_has_mpeg(dev))
628 saa7134_ts_init1(dev);
629 saa7134_input_init1(dev);
631 /* RAM FIFO config */
632 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
633 saa_writel(SAA7134_THRESHOULD,0x02020202);
635 /* enable audio + video processing */
636 saa_writel(SAA7134_MAIN_CTRL,
637 SAA7134_MAIN_CTRL_VPLLE |
638 SAA7134_MAIN_CTRL_APLLE |
639 SAA7134_MAIN_CTRL_EXOSC |
640 SAA7134_MAIN_CTRL_EVFE1 |
641 SAA7134_MAIN_CTRL_EVFE2 |
642 SAA7134_MAIN_CTRL_ESFE |
643 SAA7134_MAIN_CTRL_EBDAC);
646 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
647 * OSS initialization writes to registers via the audio DSP; these
648 * writes will fail unless the audio clock has been started. At worst,
649 * audio will not work.
652 /* enable peripheral devices */
653 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
655 /* set vertical line numbering start (vbi needs this) */
656 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
661 /* late init (with i2c + irq) */
662 static int saa7134_hwinit2(struct saa7134_dev *dev)
664 unsigned int irq2_mask;
665 dprintk("hwinit2\n");
667 saa7134_video_init2(dev);
668 saa7134_tvaudio_init2(dev);
672 SAA7134_IRQ2_INTE_DEC3 |
673 SAA7134_IRQ2_INTE_DEC2 |
674 SAA7134_IRQ2_INTE_DEC1 |
675 SAA7134_IRQ2_INTE_DEC0 |
676 SAA7134_IRQ2_INTE_PE |
677 SAA7134_IRQ2_INTE_AR;
679 if (dev->has_remote == SAA7134_REMOTE_GPIO) {
680 if (dev->remote->mask_keydown & 0x10000)
681 irq2_mask |= SAA7134_IRQ2_INTE_GPIO16;
682 else if (dev->remote->mask_keydown & 0x40000)
683 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18;
684 else if (dev->remote->mask_keyup & 0x40000)
685 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18A;
688 saa_writel(SAA7134_IRQ1, 0);
689 saa_writel(SAA7134_IRQ2, irq2_mask);
695 static int saa7134_hwfini(struct saa7134_dev *dev)
699 if (card_has_mpeg(dev))
700 saa7134_ts_fini(dev);
701 saa7134_input_fini(dev);
702 saa7134_vbi_fini(dev);
703 saa7134_video_fini(dev);
704 saa7134_tvaudio_fini(dev);
708 static void __devinit must_configure_manually(void)
714 "saa7134: Congratulations! Your TV card vendor saved a few\n"
715 "saa7134: cents for a eeprom, thus your pci board has no\n"
716 "saa7134: subsystem ID and I can't identify it automatically\n"
718 "saa7134: I feel better now. Ok, here are the good news:\n"
719 "saa7134: You can use the card=<nr> insmod option to specify\n"
720 "saa7134: which board do you have. The list:\n");
721 for (i = 0; i < saa7134_bcount; i++) {
722 printk(KERN_WARNING "saa7134: card=%d -> %-40.40s",
723 i,saa7134_boards[i].name);
724 for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
725 if (saa7134_pci_tbl[p].driver_data != i)
728 saa7134_pci_tbl[p].subvendor,
729 saa7134_pci_tbl[p].subdevice);
735 static struct video_device *vdev_init(struct saa7134_dev *dev,
736 struct video_device *template,
739 struct video_device *vfd;
741 vfd = video_device_alloc();
746 vfd->dev = &dev->pci->dev;
747 vfd->release = video_device_release;
748 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
749 dev->name, type, saa7134_boards[dev->board].name);
753 static void saa7134_unregister_video(struct saa7134_dev *dev)
755 if (dev->video_dev) {
756 if (-1 != dev->video_dev->minor)
757 video_unregister_device(dev->video_dev);
759 video_device_release(dev->video_dev);
760 dev->video_dev = NULL;
763 if (-1 != dev->vbi_dev->minor)
764 video_unregister_device(dev->vbi_dev);
766 video_device_release(dev->vbi_dev);
769 if (dev->radio_dev) {
770 if (-1 != dev->radio_dev->minor)
771 video_unregister_device(dev->radio_dev);
773 video_device_release(dev->radio_dev);
774 dev->radio_dev = NULL;
778 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
779 struct saa7134_dev *dev)
783 if (NULL != dev->mops)
785 if (saa7134_boards[dev->board].mpeg != ops->type)
787 err = ops->init(dev);
793 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
794 struct saa7134_dev *dev)
796 if (NULL == dev->mops)
798 if (dev->mops != ops)
800 dev->mops->fini(dev);
804 static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
805 const struct pci_device_id *pci_id)
807 struct saa7134_dev *dev;
808 struct list_head *item;
809 struct saa7134_mpeg_ops *mops;
812 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
818 if (pci_enable_device(pci_dev)) {
823 dev->nr = saa7134_devcount;
824 sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
827 if (pci_pci_problems) {
828 if (pci_pci_problems & PCIPCI_TRITON)
829 printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
830 if (pci_pci_problems & PCIPCI_NATOMA)
831 printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
832 if (pci_pci_problems & PCIPCI_VIAETBF)
833 printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
834 if (pci_pci_problems & PCIPCI_VSFX)
835 printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
836 #ifdef PCIPCI_ALIMAGIK
837 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
838 printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
843 if (pci_pci_problems & PCIPCI_FAIL) {
844 printk(KERN_INFO "%s: quirk: this driver and your "
845 "chipset may not work together"
846 " in overlay mode.\n",dev->name);
848 printk(KERN_INFO "%s: quirk: overlay "
849 "mode will be disabled.\n",
853 printk(KERN_INFO "%s: quirk: overlay "
854 "mode will be forced. Use this"
855 " option at your own risk.\n",
860 if (UNSET != latency) {
861 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
863 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
867 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
868 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
869 printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
870 "latency: %d, mmio: 0x%lx\n", dev->name,
871 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
872 dev->pci_lat,pci_resource_start(pci_dev,0));
873 pci_set_master(pci_dev);
874 if (!pci_dma_supported(pci_dev,0xffffffff)) {
875 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
881 dev->board = pci_id->driver_data;
882 if (card[dev->nr] >= 0 &&
883 card[dev->nr] < saa7134_bcount)
884 dev->board = card[dev->nr];
885 if (SAA7134_BOARD_NOAUTO == dev->board) {
886 must_configure_manually();
887 dev->board = SAA7134_BOARD_UNKNOWN;
889 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
890 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
891 if (UNSET != tuner[dev->nr])
892 dev->tuner_type = tuner[dev->nr];
893 printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
894 dev->name,pci_dev->subsystem_vendor,
895 pci_dev->subsystem_device,saa7134_boards[dev->board].name,
896 dev->board, card[dev->nr] == dev->board ?
897 "insmod option" : "autodetected");
900 if (!request_mem_region(pci_resource_start(pci_dev,0),
901 pci_resource_len(pci_dev,0),
904 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%lx\n",
905 dev->name,pci_resource_start(pci_dev,0));
908 dev->lmmio = ioremap(pci_resource_start(pci_dev,0), 0x1000);
909 dev->bmmio = (__u8 __iomem *)dev->lmmio;
910 if (NULL == dev->lmmio) {
912 printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
917 /* initialize hardware #1 */
918 saa7134_board_init1(dev);
919 saa7134_hwinit1(dev);
922 err = request_irq(pci_dev->irq, saa7134_irq,
923 SA_SHIRQ | SA_INTERRUPT, dev->name, dev);
925 printk(KERN_ERR "%s: can't get IRQ %d\n",
926 dev->name,pci_dev->irq);
930 /* wait a bit, register i2c bus */
932 saa7134_i2c_register(dev);
934 /* initialize hardware #2 */
935 saa7134_board_init2(dev);
936 saa7134_hwinit2(dev);
938 /* load i2c helpers */
939 if (TUNER_ABSENT != dev->tuner_type)
940 request_module("tuner");
941 if (dev->tda9887_conf)
942 request_module("tda9887");
943 if (card_is_empress(dev)) {
944 request_module("saa6752hs");
945 request_module_depend("saa7134-empress",&need_empress);
948 if (card_is_dvb(dev))
949 request_module_depend("saa7134-dvb",&need_dvb);
953 request_module_depend("saa7134-alsa",&need_alsa);
956 request_module_depend("saa7134-oss",&need_oss);
958 v4l2_prio_init(&dev->prio);
960 /* register v4l devices */
961 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
962 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
965 printk(KERN_INFO "%s: can't register video device\n",
969 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
970 dev->name,dev->video_dev->minor & 0x1f);
972 dev->vbi_dev = vdev_init(dev,&saa7134_vbi_template,"vbi");
973 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
977 printk(KERN_INFO "%s: registered device vbi%d\n",
978 dev->name,dev->vbi_dev->minor & 0x1f);
980 if (card_has_radio(dev)) {
981 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
982 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
986 printk(KERN_INFO "%s: registered device radio%d\n",
987 dev->name,dev->radio_dev->minor & 0x1f);
990 /* everything worked */
991 pci_set_drvdata(pci_dev,dev);
994 mutex_lock(&devlist_lock);
995 list_for_each(item,&mops_list) {
996 mops = list_entry(item, struct saa7134_mpeg_ops, next);
997 mpeg_ops_attach(mops, dev);
999 list_add_tail(&dev->devlist,&saa7134_devlist);
1000 mutex_unlock(&devlist_lock);
1002 /* check for signal */
1003 saa7134_irq_video_intl(dev);
1005 if (dmasound_init && !dev->dmasound.priv_data) {
1012 saa7134_unregister_video(dev);
1013 saa7134_i2c_unregister(dev);
1014 free_irq(pci_dev->irq, dev);
1016 saa7134_hwfini(dev);
1017 iounmap(dev->lmmio);
1019 release_mem_region(pci_resource_start(pci_dev,0),
1020 pci_resource_len(pci_dev,0));
1026 static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1028 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1029 struct list_head *item;
1030 struct saa7134_mpeg_ops *mops;
1032 /* Release DMA sound modules if present */
1033 if (dmasound_exit && dev->dmasound.priv_data) {
1039 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1040 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1041 print_irqstatus(dev,42,report,status);
1044 /* disable peripheral devices */
1045 saa_writeb(SAA7134_SPECIAL_MODE,0);
1047 /* shutdown hardware */
1048 saa_writel(SAA7134_IRQ1,0);
1049 saa_writel(SAA7134_IRQ2,0);
1050 saa_writel(SAA7134_MAIN_CTRL,0);
1052 /* shutdown subsystems */
1053 saa7134_hwfini(dev);
1056 mutex_lock(&devlist_lock);
1057 list_del(&dev->devlist);
1058 list_for_each(item,&mops_list) {
1059 mops = list_entry(item, struct saa7134_mpeg_ops, next);
1060 mpeg_ops_detach(mops, dev);
1062 mutex_unlock(&devlist_lock);
1065 saa7134_i2c_unregister(dev);
1066 saa7134_unregister_video(dev);
1069 /* the DMA sound modules should be unloaded before reaching
1070 this, but just in case they are still present... */
1071 if (dev->dmasound.priv_data != NULL) {
1072 free_irq(pci_dev->irq, &dev->dmasound);
1073 dev->dmasound.priv_data = NULL;
1077 /* release resources */
1078 free_irq(pci_dev->irq, dev);
1079 iounmap(dev->lmmio);
1080 release_mem_region(pci_resource_start(pci_dev,0),
1081 pci_resource_len(pci_dev,0));
1083 pci_set_drvdata(pci_dev, NULL);
1089 /* ----------------------------------------------------------- */
1091 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1093 struct list_head *item;
1094 struct saa7134_dev *dev;
1096 mutex_lock(&devlist_lock);
1097 list_for_each(item,&saa7134_devlist) {
1098 dev = list_entry(item, struct saa7134_dev, devlist);
1099 mpeg_ops_attach(ops, dev);
1101 list_add_tail(&ops->next,&mops_list);
1102 mutex_unlock(&devlist_lock);
1106 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1108 struct list_head *item;
1109 struct saa7134_dev *dev;
1111 mutex_lock(&devlist_lock);
1112 list_del(&ops->next);
1113 list_for_each(item,&saa7134_devlist) {
1114 dev = list_entry(item, struct saa7134_dev, devlist);
1115 mpeg_ops_detach(ops, dev);
1117 mutex_unlock(&devlist_lock);
1120 EXPORT_SYMBOL(saa7134_ts_register);
1121 EXPORT_SYMBOL(saa7134_ts_unregister);
1123 /* ----------------------------------------------------------- */
1125 static struct pci_driver saa7134_pci_driver = {
1127 .id_table = saa7134_pci_tbl,
1128 .probe = saa7134_initdev,
1129 .remove = __devexit_p(saa7134_finidev),
1132 static int saa7134_init(void)
1134 INIT_LIST_HEAD(&saa7134_devlist);
1135 printk(KERN_INFO "saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
1136 (SAA7134_VERSION_CODE >> 16) & 0xff,
1137 (SAA7134_VERSION_CODE >> 8) & 0xff,
1138 SAA7134_VERSION_CODE & 0xff);
1140 printk(KERN_INFO "saa7130/34: snapshot date %04d-%02d-%02d\n",
1141 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1143 return pci_register_driver(&saa7134_pci_driver);
1146 static void saa7134_fini(void)
1148 #if defined(CONFIG_MODULES) && defined(MODULE)
1149 if (pending_registered)
1150 unregister_module_notifier(&pending_notifier);
1151 #endif /* CONFIG_MODULES */
1152 pci_unregister_driver(&saa7134_pci_driver);
1155 module_init(saa7134_init);
1156 module_exit(saa7134_fini);
1158 /* ----------------------------------------------------------- */
1160 EXPORT_SYMBOL(saa7134_i2c_call_clients);
1161 EXPORT_SYMBOL(saa7134_devlist);
1162 EXPORT_SYMBOL(saa7134_boards);
1164 /* ----------------- for the DMA sound modules --------------- */
1166 EXPORT_SYMBOL(dmasound_init);
1167 EXPORT_SYMBOL(dmasound_exit);
1168 EXPORT_SYMBOL(saa7134_pgtable_free);
1169 EXPORT_SYMBOL(saa7134_pgtable_build);
1170 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1171 EXPORT_SYMBOL(saa7134_set_dmabits);
1173 /* ----------------------------------------------------------- */