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>
35 #include "saa7134-reg.h"
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");
42 /* ------------------------------------------------------------------ */
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]");
48 static unsigned int core_debug = 0;
49 module_param(core_debug, int, 0644);
50 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
52 static unsigned int gpio_tracking = 0;
53 module_param(gpio_tracking, int, 0644);
54 MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
56 static unsigned int alsa = 0;
57 module_param(alsa, int, 0644);
58 MODULE_PARM_DESC(alsa,"enable ALSA DMA sound [dmasound]");
60 static unsigned int oss = 0;
61 module_param(oss, int, 0644);
62 MODULE_PARM_DESC(oss,"enable OSS DMA sound [dmasound]");
64 static unsigned int latency = UNSET;
65 module_param(latency, int, 0444);
66 MODULE_PARM_DESC(latency,"pci latency timer");
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 };
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);
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");
87 static DECLARE_MUTEX(devlist_lock);
88 LIST_HEAD(saa7134_devlist);
89 static LIST_HEAD(mops_list);
90 static unsigned int saa7134_devcount;
92 int (*dmasound_init)(struct saa7134_dev *dev);
93 int (*dmasound_exit)(struct saa7134_dev *dev);
95 #define dprintk(fmt, arg...) if (core_debug) \
96 printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
98 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
100 unsigned long mode,status;
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;
110 "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
111 dev->name, mode, (~mode) & status, mode & status, msg);
114 /* ------------------------------------------------------------------ */
117 /* ----------------------------------------------------------- */
118 /* delayed request_module */
120 #if defined(CONFIG_MODULES) && defined(MODULE)
121 static int need_empress;
123 static int need_alsa;
126 static int pending_call(struct notifier_block *self, unsigned long state,
129 if (module != THIS_MODULE || state != MODULE_STATE_LIVE)
133 request_module("saa7134-empress");
135 request_module("saa7134-dvb");
137 request_module("saa7134-alsa");
139 request_module("saa7134-oss");
143 static int pending_registered=0;
144 static struct notifier_block pending_notifier = {
145 .notifier_call = pending_call,
148 static void request_module_depend(char *name, int *flag)
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;
159 case MODULE_STATE_LIVE:
160 request_module(name);
169 #define request_module_depend(name,flag)
170 #endif /* CONFIG_MODULES */
172 /* ------------------------------------------------------------------ */
174 /* nr of (saa7134-)pages for the given buffer size */
175 static int saa7134_buffer_pages(int size)
177 size = PAGE_ALIGN(size);
178 size += PAGE_SIZE; /* for non-page-aligned buffers */
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)
187 unsigned int maxcount;
189 maxcount = 1024 / saa7134_buffer_pages(size);
190 if (count > maxcount)
195 int saa7134_buffer_startpage(struct saa7134_buf *buf)
197 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
200 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
204 base = saa7134_buffer_startpage(buf) * 4096;
205 base += buf->vb.dma.sglist[0].offset;
209 /* ------------------------------------------------------------------ */
211 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
216 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
219 pt->size = SAA7134_PGTABLE_SIZE;
225 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
226 struct scatterlist *list, unsigned int length,
227 unsigned int startpage)
232 BUG_ON(NULL == pt || NULL == pt->cpu);
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);
241 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
245 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
249 /* ------------------------------------------------------------------ */
251 void saa7134_dma_free(struct saa7134_dev *dev,struct saa7134_buf *buf)
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;
262 /* ------------------------------------------------------------------ */
264 int saa7134_buffer_queue(struct saa7134_dev *dev,
265 struct saa7134_dmaqueue *q,
266 struct saa7134_buf *buf)
268 struct saa7134_buf *next = NULL;
270 assert_spin_locked(&dev->slock);
271 dprintk("buffer_queue %p\n",buf);
272 if (NULL == q->curr) {
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;
280 next = list_entry(q->queue.next,struct saa7134_buf,
283 buf->activate(dev,buf,next);
286 list_add_tail(&buf->vb.queue,&q->queue);
287 buf->vb.state = STATE_QUEUED;
292 void saa7134_buffer_finish(struct saa7134_dev *dev,
293 struct saa7134_dmaqueue *q,
296 assert_spin_locked(&dev->slock);
297 dprintk("buffer_finish %p\n",q->curr);
299 /* finish current buffer */
300 q->curr->vb.state = state;
301 do_gettimeofday(&q->curr->vb.ts);
302 wake_up(&q->curr->vb.done);
306 void saa7134_buffer_next(struct saa7134_dev *dev,
307 struct saa7134_dmaqueue *q)
309 struct saa7134_buf *buf,*next = NULL;
311 assert_spin_locked(&dev->slock);
312 BUG_ON(NULL != q->curr);
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,
324 buf->activate(dev,buf,next);
325 dprintk("buffer_next #2 prev=%p/next=%p\n",
326 q->queue.prev,q->queue.next);
328 /* nothing to do -- just stop DMA */
329 dprintk("buffer_next %p\n",NULL);
330 saa7134_set_dmabits(dev);
331 del_timer(&q->timeout);
335 void saa7134_buffer_timeout(unsigned long data)
337 struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
338 struct saa7134_dev *dev = q->dev;
341 spin_lock_irqsave(&dev->slock,flags);
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);
348 /* flag current buffer as failed,
349 try to start over with the next one. */
351 dprintk("timeout on %p\n",q->curr);
352 saa7134_buffer_finish(dev,q,STATE_ERROR);
354 saa7134_buffer_next(dev,q);
355 spin_unlock_irqrestore(&dev->slock,flags);
358 /* ------------------------------------------------------------------ */
360 int saa7134_set_dmabits(struct saa7134_dev *dev)
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;
366 assert_spin_locked(&dev->slock);
368 /* video capture -- dma 0 + video task A */
369 if (dev->video_q.curr) {
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;
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;
384 /* screen overlay -- dma 0 + video task B */
387 ctrl |= SAA7134_MAIN_CTRL_TE1;
391 /* vbi capture -- dma 0 + vbi task A+B */
392 if (dev->vbi_q.curr) {
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;
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;
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;
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);
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);
433 /* odd B, even A, repeat */
434 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
435 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
437 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
438 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
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,
454 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
455 task, ctrl, irq, split ? "no" : "yes");
460 /* ------------------------------------------------------------------ */
461 /* IRQ handler + helpers */
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"
469 #define IRQBITS ARRAY_SIZE(irqbits)
471 static void print_irqstatus(struct saa7134_dev *dev, int loop,
472 unsigned long report, unsigned long status)
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)))
481 printk(" %s",irqbits[i]);
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",
493 static irqreturn_t saa7134_irq(int irq, void *dev_id, struct pt_regs *regs)
495 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
496 unsigned long report,status;
497 int loop, handled = 0;
499 for (loop = 0; loop < 10; loop++) {
500 report = saa_readl(SAA7134_IRQ_REPORT);
501 status = saa_readl(SAA7134_IRQ_STATUS);
504 printk(KERN_DEBUG "%s/irq: no (more) work\n",
509 /* If dmasound support is active and we get a sound report, exit
510 and let the saa7134-alsa/oss module deal with it */
512 if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
513 (dev->dmasound.priv_data != NULL) )
516 printk(KERN_DEBUG "%s/irq: ignoring interrupt for DMA sound\n",
522 saa_writel(SAA7134_IRQ_REPORT,report);
524 print_irqstatus(dev,loop,report,status);
527 if (report & SAA7134_IRQ_REPORT_RDCAP /* _INTL */)
528 saa7134_irq_video_intl(dev);
530 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
531 (status & 0x60) == 0)
532 saa7134_irq_video_done(dev,status);
534 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
535 (status & 0x40) == 0x40)
536 saa7134_irq_vbi_done(dev,status);
538 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
540 saa7134_irq_ts_done(dev,status);
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);
550 case SAA7134_REMOTE_I2C:
551 break; /* FIXME: invoke I2C get_key() */
553 default: /* GPIO16 not used by IR remote */
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);
567 case SAA7134_REMOTE_I2C:
568 break; /* FIXME: invoke I2C get_key() */
570 default: /* GPIO18 not used by IR remote */
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);
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);
603 return IRQ_RETVAL(handled);
606 /* ------------------------------------------------------------------ */
608 /* early init (no i2c, no irq) */
609 static int saa7134_hwinit1(struct saa7134_dev *dev)
611 dprintk("hwinit1\n");
613 saa_writel(SAA7134_IRQ1, 0);
614 saa_writel(SAA7134_IRQ2, 0);
615 init_MUTEX(&dev->lock);
616 spin_lock_init(&dev->slock);
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);
625 /* RAM FIFO config */
626 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
627 saa_writel(SAA7134_THRESHOULD,0x02020202);
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);
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.
646 /* enable peripheral devices */
647 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
649 /* set vertical line numbering start (vbi needs this) */
650 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
655 /* late init (with i2c + irq) */
656 static int saa7134_hwinit2(struct saa7134_dev *dev)
658 unsigned int irq2_mask;
659 dprintk("hwinit2\n");
661 saa7134_video_init2(dev);
662 saa7134_tvaudio_init2(dev);
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;
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;
682 saa_writel(SAA7134_IRQ1, 0);
683 saa_writel(SAA7134_IRQ2, irq2_mask);
689 static int saa7134_hwfini(struct saa7134_dev *dev)
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);
702 static void __devinit must_configure_manually(void)
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"
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)
722 saa7134_pci_tbl[p].subvendor,
723 saa7134_pci_tbl[p].subdevice);
729 static struct video_device *vdev_init(struct saa7134_dev *dev,
730 struct video_device *template,
733 struct video_device *vfd;
735 vfd = video_device_alloc();
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);
747 static void saa7134_unregister_video(struct saa7134_dev *dev)
749 if (dev->video_dev) {
750 if (-1 != dev->video_dev->minor)
751 video_unregister_device(dev->video_dev);
753 video_device_release(dev->video_dev);
754 dev->video_dev = NULL;
757 if (-1 != dev->vbi_dev->minor)
758 video_unregister_device(dev->vbi_dev);
760 video_device_release(dev->vbi_dev);
763 if (dev->radio_dev) {
764 if (-1 != dev->radio_dev->minor)
765 video_unregister_device(dev->radio_dev);
767 video_device_release(dev->radio_dev);
768 dev->radio_dev = NULL;
772 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
773 struct saa7134_dev *dev)
777 if (NULL != dev->mops)
779 if (saa7134_boards[dev->board].mpeg != ops->type)
781 err = ops->init(dev);
787 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
788 struct saa7134_dev *dev)
790 if (NULL == dev->mops)
792 if (dev->mops != ops)
794 dev->mops->fini(dev);
798 static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
799 const struct pci_device_id *pci_id)
801 struct saa7134_dev *dev;
802 struct list_head *item;
803 struct saa7134_mpeg_ops *mops;
806 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
812 if (pci_enable_device(pci_dev)) {
817 dev->nr = saa7134_devcount;
818 sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
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",
838 if (UNSET != latency) {
839 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
841 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
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);
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;
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");
878 if (!request_mem_region(pci_resource_start(pci_dev,0),
879 pci_resource_len(pci_dev,0),
882 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%lx\n",
883 dev->name,pci_resource_start(pci_dev,0));
886 dev->lmmio = ioremap(pci_resource_start(pci_dev,0), 0x1000);
887 dev->bmmio = (__u8 __iomem *)dev->lmmio;
888 if (NULL == dev->lmmio) {
890 printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
895 /* initialize hardware #1 */
896 saa7134_board_init1(dev);
897 saa7134_hwinit1(dev);
900 err = request_irq(pci_dev->irq, saa7134_irq,
901 SA_SHIRQ | SA_INTERRUPT, dev->name, dev);
903 printk(KERN_ERR "%s: can't get IRQ %d\n",
904 dev->name,pci_dev->irq);
908 /* wait a bit, register i2c bus */
910 saa7134_i2c_register(dev);
912 /* initialize hardware #2 */
913 saa7134_board_init2(dev);
914 saa7134_hwinit2(dev);
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);
926 if (card_is_dvb(dev))
927 request_module_depend("saa7134-dvb",&need_dvb);
931 request_module_depend("saa7134-alsa",&need_alsa);
934 request_module_depend("saa7134-oss",&need_oss);
936 v4l2_prio_init(&dev->prio);
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,
943 printk(KERN_INFO "%s: can't register video device\n",
947 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
948 dev->name,dev->video_dev->minor & 0x1f);
950 dev->vbi_dev = vdev_init(dev,&saa7134_vbi_template,"vbi");
951 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
955 printk(KERN_INFO "%s: registered device vbi%d\n",
956 dev->name,dev->vbi_dev->minor & 0x1f);
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,
964 printk(KERN_INFO "%s: registered device radio%d\n",
965 dev->name,dev->radio_dev->minor & 0x1f);
968 /* everything worked */
969 pci_set_drvdata(pci_dev,dev);
973 list_for_each(item,&mops_list) {
974 mops = list_entry(item, struct saa7134_mpeg_ops, next);
975 mpeg_ops_attach(mops, dev);
977 list_add_tail(&dev->devlist,&saa7134_devlist);
980 /* check for signal */
981 saa7134_irq_video_intl(dev);
983 if (dmasound_init && !dev->dmasound.priv_data) {
990 saa7134_unregister_video(dev);
991 saa7134_i2c_unregister(dev);
992 free_irq(pci_dev->irq, dev);
997 release_mem_region(pci_resource_start(pci_dev,0),
998 pci_resource_len(pci_dev,0));
1004 static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1006 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1007 struct list_head *item;
1008 struct saa7134_mpeg_ops *mops;
1010 /* Release DMA sound modules if present */
1011 if (dmasound_exit && dev->dmasound.priv_data) {
1017 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1018 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1019 print_irqstatus(dev,42,report,status);
1022 /* disable peripheral devices */
1023 saa_writeb(SAA7134_SPECIAL_MODE,0);
1025 /* shutdown hardware */
1026 saa_writel(SAA7134_IRQ1,0);
1027 saa_writel(SAA7134_IRQ2,0);
1028 saa_writel(SAA7134_MAIN_CTRL,0);
1030 /* shutdown subsystems */
1031 saa7134_hwfini(dev);
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);
1043 saa7134_i2c_unregister(dev);
1044 saa7134_unregister_video(dev);
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;
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));
1061 pci_set_drvdata(pci_dev, NULL);
1067 /* ----------------------------------------------------------- */
1069 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1071 struct list_head *item;
1072 struct saa7134_dev *dev;
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);
1079 list_add_tail(&ops->next,&mops_list);
1084 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1086 struct list_head *item;
1087 struct saa7134_dev *dev;
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);
1098 EXPORT_SYMBOL(saa7134_ts_register);
1099 EXPORT_SYMBOL(saa7134_ts_unregister);
1101 /* ----------------------------------------------------------- */
1103 static struct pci_driver saa7134_pci_driver = {
1105 .id_table = saa7134_pci_tbl,
1106 .probe = saa7134_initdev,
1107 .remove = __devexit_p(saa7134_finidev),
1110 static int saa7134_init(void)
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);
1118 printk(KERN_INFO "saa7130/34: snapshot date %04d-%02d-%02d\n",
1119 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1121 return pci_register_driver(&saa7134_pci_driver);
1124 static void saa7134_fini(void)
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);
1133 module_init(saa7134_init);
1134 module_exit(saa7134_fini);
1136 /* ----------------------------------------------------------- */
1138 EXPORT_SYMBOL(saa7134_i2c_call_clients);
1139 EXPORT_SYMBOL(saa7134_devlist);
1140 EXPORT_SYMBOL(saa7134_boards);
1142 /* ----------------- for the DMA sound modules --------------- */
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);
1151 /* ----------------------------------------------------------- */