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 unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
70 static unsigned int vbi_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
71 static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
72 static unsigned int tuner[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
73 static unsigned int card[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
76 module_param_array(video_nr, int, NULL, 0444);
77 module_param_array(vbi_nr, int, NULL, 0444);
78 module_param_array(radio_nr, int, NULL, 0444);
79 module_param_array(tuner, int, NULL, 0444);
80 module_param_array(card, int, NULL, 0444);
82 MODULE_PARM_DESC(video_nr, "video device number");
83 MODULE_PARM_DESC(vbi_nr, "vbi device number");
84 MODULE_PARM_DESC(radio_nr, "radio device number");
85 MODULE_PARM_DESC(tuner, "tuner type");
86 MODULE_PARM_DESC(card, "card type");
88 static DEFINE_MUTEX(devlist_lock);
89 LIST_HEAD(saa7134_devlist);
90 static LIST_HEAD(mops_list);
91 static unsigned int saa7134_devcount;
93 int (*dmasound_init)(struct saa7134_dev *dev);
94 int (*dmasound_exit)(struct saa7134_dev *dev);
96 #define dprintk(fmt, arg...) if (core_debug) \
97 printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
99 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
101 unsigned long mode,status;
105 /* rising SAA7134_GPIO_GPRESCAN reads the status */
106 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
107 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
108 mode = saa_readl(SAA7134_GPIO_GPMODE0 >> 2) & 0xfffffff;
109 status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
111 "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
112 dev->name, mode, (~mode) & status, mode & status, msg);
115 /* ------------------------------------------------------------------ */
118 /* ----------------------------------------------------------- */
119 /* delayed request_module */
121 #if defined(CONFIG_MODULES) && defined(MODULE)
122 static int need_empress;
124 static int need_alsa;
127 static int pending_call(struct notifier_block *self, unsigned long state,
130 if (module != THIS_MODULE || state != MODULE_STATE_LIVE)
134 request_module("saa7134-empress");
136 request_module("saa7134-dvb");
138 request_module("saa7134-alsa");
140 request_module("saa7134-oss");
144 static int pending_registered;
145 static struct notifier_block pending_notifier = {
146 .notifier_call = pending_call,
149 static void request_module_depend(char *name, int *flag)
152 switch (THIS_MODULE->state) {
153 case MODULE_STATE_COMING:
154 if (!pending_registered) {
155 err = register_module_notifier(&pending_notifier);
156 pending_registered = 1;
160 case MODULE_STATE_LIVE:
161 request_module(name);
170 #define request_module_depend(name,flag)
171 #endif /* CONFIG_MODULES */
173 /* ------------------------------------------------------------------ */
175 /* nr of (saa7134-)pages for the given buffer size */
176 static int saa7134_buffer_pages(int size)
178 size = PAGE_ALIGN(size);
179 size += PAGE_SIZE; /* for non-page-aligned buffers */
184 /* calc max # of buffers from size (must not exceed the 4MB virtual
185 * address space per DMA channel) */
186 int saa7134_buffer_count(unsigned int size, unsigned int count)
188 unsigned int maxcount;
190 maxcount = 1024 / saa7134_buffer_pages(size);
191 if (count > maxcount)
196 int saa7134_buffer_startpage(struct saa7134_buf *buf)
198 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
201 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
205 base = saa7134_buffer_startpage(buf) * 4096;
206 base += buf->vb.dma.sglist[0].offset;
210 /* ------------------------------------------------------------------ */
212 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
217 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
220 pt->size = SAA7134_PGTABLE_SIZE;
226 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
227 struct scatterlist *list, unsigned int length,
228 unsigned int startpage)
233 BUG_ON(NULL == pt || NULL == pt->cpu);
235 ptr = pt->cpu + startpage;
236 for (i = 0; i < length; i++, list++)
237 for (p = 0; p * 4096 < list->length; p++, ptr++)
238 *ptr = cpu_to_le32(sg_dma_address(list) - list->offset);
242 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
246 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
250 /* ------------------------------------------------------------------ */
252 void saa7134_dma_free(struct saa7134_dev *dev,struct saa7134_buf *buf)
257 videobuf_waiton(&buf->vb,0,0);
258 videobuf_dma_pci_unmap(dev->pci, &buf->vb.dma);
259 videobuf_dma_free(&buf->vb.dma);
260 buf->vb.state = STATE_NEEDS_INIT;
263 /* ------------------------------------------------------------------ */
265 int saa7134_buffer_queue(struct saa7134_dev *dev,
266 struct saa7134_dmaqueue *q,
267 struct saa7134_buf *buf)
269 struct saa7134_buf *next = NULL;
271 assert_spin_locked(&dev->slock);
272 dprintk("buffer_queue %p\n",buf);
273 if (NULL == q->curr) {
276 buf->activate(dev,buf,NULL);
277 } else if (list_empty(&q->queue)) {
278 list_add_tail(&buf->vb.queue,&q->queue);
279 buf->vb.state = STATE_QUEUED;
281 next = list_entry(q->queue.next,struct saa7134_buf,
284 buf->activate(dev,buf,next);
287 list_add_tail(&buf->vb.queue,&q->queue);
288 buf->vb.state = STATE_QUEUED;
293 void saa7134_buffer_finish(struct saa7134_dev *dev,
294 struct saa7134_dmaqueue *q,
297 assert_spin_locked(&dev->slock);
298 dprintk("buffer_finish %p\n",q->curr);
300 /* finish current buffer */
301 q->curr->vb.state = state;
302 do_gettimeofday(&q->curr->vb.ts);
303 wake_up(&q->curr->vb.done);
307 void saa7134_buffer_next(struct saa7134_dev *dev,
308 struct saa7134_dmaqueue *q)
310 struct saa7134_buf *buf,*next = NULL;
312 assert_spin_locked(&dev->slock);
313 BUG_ON(NULL != q->curr);
315 if (!list_empty(&q->queue)) {
316 /* activate next one from queue */
317 buf = list_entry(q->queue.next,struct saa7134_buf,vb.queue);
318 dprintk("buffer_next %p [prev=%p/next=%p]\n",
319 buf,q->queue.prev,q->queue.next);
320 list_del(&buf->vb.queue);
321 if (!list_empty(&q->queue))
322 next = list_entry(q->queue.next,struct saa7134_buf,
325 buf->activate(dev,buf,next);
326 dprintk("buffer_next #2 prev=%p/next=%p\n",
327 q->queue.prev,q->queue.next);
329 /* nothing to do -- just stop DMA */
330 dprintk("buffer_next %p\n",NULL);
331 saa7134_set_dmabits(dev);
332 del_timer(&q->timeout);
336 void saa7134_buffer_timeout(unsigned long data)
338 struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
339 struct saa7134_dev *dev = q->dev;
342 spin_lock_irqsave(&dev->slock,flags);
344 /* try to reset the hardware (SWRST) */
345 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
346 saa_writeb(SAA7134_REGION_ENABLE, 0x80);
347 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
349 /* flag current buffer as failed,
350 try to start over with the next one. */
352 dprintk("timeout on %p\n",q->curr);
353 saa7134_buffer_finish(dev,q,STATE_ERROR);
355 saa7134_buffer_next(dev,q);
356 spin_unlock_irqrestore(&dev->slock,flags);
359 /* ------------------------------------------------------------------ */
361 int saa7134_set_dmabits(struct saa7134_dev *dev)
363 u32 split, task=0, ctrl=0, irq=0;
364 enum v4l2_field cap = V4L2_FIELD_ANY;
365 enum v4l2_field ov = V4L2_FIELD_ANY;
367 assert_spin_locked(&dev->slock);
369 /* video capture -- dma 0 + video task A */
370 if (dev->video_q.curr) {
372 ctrl |= SAA7134_MAIN_CTRL_TE0;
373 irq |= SAA7134_IRQ1_INTE_RA0_1 |
374 SAA7134_IRQ1_INTE_RA0_0;
375 cap = dev->video_q.curr->vb.field;
378 /* video capture -- dma 1+2 (planar modes) */
379 if (dev->video_q.curr &&
380 dev->video_q.curr->fmt->planar) {
381 ctrl |= SAA7134_MAIN_CTRL_TE4 |
382 SAA7134_MAIN_CTRL_TE5;
385 /* screen overlay -- dma 0 + video task B */
388 ctrl |= SAA7134_MAIN_CTRL_TE1;
392 /* vbi capture -- dma 0 + vbi task A+B */
393 if (dev->vbi_q.curr) {
395 ctrl |= SAA7134_MAIN_CTRL_TE2 |
396 SAA7134_MAIN_CTRL_TE3;
397 irq |= SAA7134_IRQ1_INTE_RA0_7 |
398 SAA7134_IRQ1_INTE_RA0_6 |
399 SAA7134_IRQ1_INTE_RA0_5 |
400 SAA7134_IRQ1_INTE_RA0_4;
403 /* audio capture -- dma 3 */
404 if (dev->dmasound.dma_running) {
405 ctrl |= SAA7134_MAIN_CTRL_TE6;
406 irq |= SAA7134_IRQ1_INTE_RA3_1 |
407 SAA7134_IRQ1_INTE_RA3_0;
410 /* TS capture -- dma 5 */
411 if (dev->ts_q.curr) {
412 ctrl |= SAA7134_MAIN_CTRL_TE5;
413 irq |= SAA7134_IRQ1_INTE_RA2_3 |
414 SAA7134_IRQ1_INTE_RA2_2 |
415 SAA7134_IRQ1_INTE_RA2_1 |
416 SAA7134_IRQ1_INTE_RA2_0;
419 /* set task conditions + field handling */
420 if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
421 /* default config -- use full frames */
422 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
423 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
424 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x02);
425 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x02);
428 /* split fields between tasks */
429 if (V4L2_FIELD_TOP == cap) {
430 /* odd A, even B, repeat */
431 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
432 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
434 /* odd B, even A, repeat */
435 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
436 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
438 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
439 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
444 saa_writeb(SAA7134_REGION_ENABLE, task);
445 saa_writel(SAA7134_IRQ1, irq);
446 saa_andorl(SAA7134_MAIN_CTRL,
447 SAA7134_MAIN_CTRL_TE0 |
448 SAA7134_MAIN_CTRL_TE1 |
449 SAA7134_MAIN_CTRL_TE2 |
450 SAA7134_MAIN_CTRL_TE3 |
451 SAA7134_MAIN_CTRL_TE4 |
452 SAA7134_MAIN_CTRL_TE5 |
453 SAA7134_MAIN_CTRL_TE6,
455 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
456 task, ctrl, irq, split ? "no" : "yes");
461 /* ------------------------------------------------------------------ */
462 /* IRQ handler + helpers */
464 static char *irqbits[] = {
465 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
466 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
467 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
468 "GPIO16?", "GPIO18", "GPIO22", "GPIO23"
470 #define IRQBITS ARRAY_SIZE(irqbits)
472 static void print_irqstatus(struct saa7134_dev *dev, int loop,
473 unsigned long report, unsigned long status)
477 printk(KERN_DEBUG "%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
478 dev->name,loop,jiffies,report,status);
479 for (i = 0; i < IRQBITS; i++) {
480 if (!(report & (1 << i)))
482 printk(" %s",irqbits[i]);
484 if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
485 printk(" | RA0=%s,%s,%s,%ld",
486 (status & 0x40) ? "vbi" : "video",
487 (status & 0x20) ? "b" : "a",
488 (status & 0x10) ? "odd" : "even",
494 static irqreturn_t saa7134_irq(int irq, void *dev_id, struct pt_regs *regs)
496 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
497 unsigned long report,status;
498 int loop, handled = 0;
500 for (loop = 0; loop < 10; loop++) {
501 report = saa_readl(SAA7134_IRQ_REPORT);
502 status = saa_readl(SAA7134_IRQ_STATUS);
505 printk(KERN_DEBUG "%s/irq: no (more) work\n",
510 /* If dmasound support is active and we get a sound report, exit
511 and let the saa7134-alsa/oss module deal with it */
513 if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
514 (dev->dmasound.priv_data != NULL) )
517 printk(KERN_DEBUG "%s/irq: ignoring interrupt for DMA sound\n",
523 saa_writel(SAA7134_IRQ_REPORT,report);
525 print_irqstatus(dev,loop,report,status);
528 if (report & SAA7134_IRQ_REPORT_RDCAP /* _INTL */)
529 saa7134_irq_video_intl(dev);
531 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
532 (status & 0x60) == 0)
533 saa7134_irq_video_done(dev,status);
535 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
536 (status & 0x40) == 0x40)
537 saa7134_irq_vbi_done(dev,status);
539 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
541 saa7134_irq_ts_done(dev,status);
543 if (report & SAA7134_IRQ_REPORT_GPIO16) {
544 switch (dev->has_remote) {
545 case SAA7134_REMOTE_GPIO:
546 if (dev->remote->mask_keydown & 0x10000) {
547 saa7134_input_irq(dev);
551 case SAA7134_REMOTE_I2C:
552 break; /* FIXME: invoke I2C get_key() */
554 default: /* GPIO16 not used by IR remote */
559 if (report & SAA7134_IRQ_REPORT_GPIO18) {
560 switch (dev->has_remote) {
561 case SAA7134_REMOTE_GPIO:
562 if ((dev->remote->mask_keydown & 0x40000) ||
563 (dev->remote->mask_keyup & 0x40000)) {
564 saa7134_input_irq(dev);
568 case SAA7134_REMOTE_I2C:
569 break; /* FIXME: invoke I2C get_key() */
571 default: /* GPIO18 not used by IR remote */
578 print_irqstatus(dev,loop,report,status);
579 if (report & SAA7134_IRQ_REPORT_PE) {
580 /* disable all parity error */
581 printk(KERN_WARNING "%s/irq: looping -- "
582 "clearing PE (parity error!) enable bit\n",dev->name);
583 saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
584 } else if (report & SAA7134_IRQ_REPORT_GPIO16) {
585 /* disable gpio16 IRQ */
586 printk(KERN_WARNING "%s/irq: looping -- "
587 "clearing GPIO16 enable bit\n",dev->name);
588 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16);
589 } else if (report & SAA7134_IRQ_REPORT_GPIO18) {
590 /* disable gpio18 IRQs */
591 printk(KERN_WARNING "%s/irq: looping -- "
592 "clearing GPIO18 enable bit\n",dev->name);
593 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
595 /* disable all irqs */
596 printk(KERN_WARNING "%s/irq: looping -- "
597 "clearing all enable bits\n",dev->name);
598 saa_writel(SAA7134_IRQ1,0);
599 saa_writel(SAA7134_IRQ2,0);
604 return IRQ_RETVAL(handled);
607 /* ------------------------------------------------------------------ */
609 /* early init (no i2c, no irq) */
610 static int saa7134_hwinit1(struct saa7134_dev *dev)
612 dprintk("hwinit1\n");
614 saa_writel(SAA7134_IRQ1, 0);
615 saa_writel(SAA7134_IRQ2, 0);
616 init_MUTEX(&dev->lock);
617 spin_lock_init(&dev->slock);
619 saa7134_track_gpio(dev,"pre-init");
620 saa7134_video_init1(dev);
621 saa7134_vbi_init1(dev);
622 if (card_has_mpeg(dev))
623 saa7134_ts_init1(dev);
624 saa7134_input_init1(dev);
626 /* RAM FIFO config */
627 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
628 saa_writel(SAA7134_THRESHOULD,0x02020202);
630 /* enable audio + video processing */
631 saa_writel(SAA7134_MAIN_CTRL,
632 SAA7134_MAIN_CTRL_VPLLE |
633 SAA7134_MAIN_CTRL_APLLE |
634 SAA7134_MAIN_CTRL_EXOSC |
635 SAA7134_MAIN_CTRL_EVFE1 |
636 SAA7134_MAIN_CTRL_EVFE2 |
637 SAA7134_MAIN_CTRL_ESFE |
638 SAA7134_MAIN_CTRL_EBDAC);
641 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
642 * OSS initialization writes to registers via the audio DSP; these
643 * writes will fail unless the audio clock has been started. At worst,
644 * audio will not work.
647 /* enable peripheral devices */
648 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
650 /* set vertical line numbering start (vbi needs this) */
651 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
656 /* late init (with i2c + irq) */
657 static int saa7134_hwinit2(struct saa7134_dev *dev)
659 unsigned int irq2_mask;
660 dprintk("hwinit2\n");
662 saa7134_video_init2(dev);
663 saa7134_tvaudio_init2(dev);
667 SAA7134_IRQ2_INTE_DEC3 |
668 SAA7134_IRQ2_INTE_DEC2 |
669 SAA7134_IRQ2_INTE_DEC1 |
670 SAA7134_IRQ2_INTE_DEC0 |
671 SAA7134_IRQ2_INTE_PE |
672 SAA7134_IRQ2_INTE_AR;
674 if (dev->has_remote == SAA7134_REMOTE_GPIO) {
675 if (dev->remote->mask_keydown & 0x10000)
676 irq2_mask |= SAA7134_IRQ2_INTE_GPIO16;
677 else if (dev->remote->mask_keydown & 0x40000)
678 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18;
679 else if (dev->remote->mask_keyup & 0x40000)
680 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18A;
683 saa_writel(SAA7134_IRQ1, 0);
684 saa_writel(SAA7134_IRQ2, irq2_mask);
690 static int saa7134_hwfini(struct saa7134_dev *dev)
694 if (card_has_mpeg(dev))
695 saa7134_ts_fini(dev);
696 saa7134_input_fini(dev);
697 saa7134_vbi_fini(dev);
698 saa7134_video_fini(dev);
699 saa7134_tvaudio_fini(dev);
703 static void __devinit must_configure_manually(void)
709 "saa7134: Congratulations! Your TV card vendor saved a few\n"
710 "saa7134: cents for a eeprom, thus your pci board has no\n"
711 "saa7134: subsystem ID and I can't identify it automatically\n"
713 "saa7134: I feel better now. Ok, here are the good news:\n"
714 "saa7134: You can use the card=<nr> insmod option to specify\n"
715 "saa7134: which board do you have. The list:\n");
716 for (i = 0; i < saa7134_bcount; i++) {
717 printk(KERN_WARNING "saa7134: card=%d -> %-40.40s",
718 i,saa7134_boards[i].name);
719 for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
720 if (saa7134_pci_tbl[p].driver_data != i)
723 saa7134_pci_tbl[p].subvendor,
724 saa7134_pci_tbl[p].subdevice);
730 static struct video_device *vdev_init(struct saa7134_dev *dev,
731 struct video_device *template,
734 struct video_device *vfd;
736 vfd = video_device_alloc();
741 vfd->dev = &dev->pci->dev;
742 vfd->release = video_device_release;
743 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
744 dev->name, type, saa7134_boards[dev->board].name);
748 static void saa7134_unregister_video(struct saa7134_dev *dev)
750 if (dev->video_dev) {
751 if (-1 != dev->video_dev->minor)
752 video_unregister_device(dev->video_dev);
754 video_device_release(dev->video_dev);
755 dev->video_dev = NULL;
758 if (-1 != dev->vbi_dev->minor)
759 video_unregister_device(dev->vbi_dev);
761 video_device_release(dev->vbi_dev);
764 if (dev->radio_dev) {
765 if (-1 != dev->radio_dev->minor)
766 video_unregister_device(dev->radio_dev);
768 video_device_release(dev->radio_dev);
769 dev->radio_dev = NULL;
773 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
774 struct saa7134_dev *dev)
778 if (NULL != dev->mops)
780 if (saa7134_boards[dev->board].mpeg != ops->type)
782 err = ops->init(dev);
788 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
789 struct saa7134_dev *dev)
791 if (NULL == dev->mops)
793 if (dev->mops != ops)
795 dev->mops->fini(dev);
799 static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
800 const struct pci_device_id *pci_id)
802 struct saa7134_dev *dev;
803 struct list_head *item;
804 struct saa7134_mpeg_ops *mops;
807 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
813 if (pci_enable_device(pci_dev)) {
818 dev->nr = saa7134_devcount;
819 sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
822 if (pci_pci_problems) {
823 if (pci_pci_problems & PCIPCI_TRITON)
824 printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
825 if (pci_pci_problems & PCIPCI_NATOMA)
826 printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
827 if (pci_pci_problems & PCIPCI_VIAETBF)
828 printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
829 if (pci_pci_problems & PCIPCI_VSFX)
830 printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
831 #ifdef PCIPCI_ALIMAGIK
832 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
833 printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
839 if (UNSET != latency) {
840 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
842 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
846 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
847 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
848 printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
849 "latency: %d, mmio: 0x%lx\n", dev->name,
850 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
851 dev->pci_lat,pci_resource_start(pci_dev,0));
852 pci_set_master(pci_dev);
853 if (!pci_dma_supported(pci_dev,0xffffffff)) {
854 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
860 dev->board = pci_id->driver_data;
861 if (card[dev->nr] >= 0 &&
862 card[dev->nr] < saa7134_bcount)
863 dev->board = card[dev->nr];
864 if (SAA7134_BOARD_NOAUTO == dev->board) {
865 must_configure_manually();
866 dev->board = SAA7134_BOARD_UNKNOWN;
868 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
869 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
870 if (UNSET != tuner[dev->nr])
871 dev->tuner_type = tuner[dev->nr];
872 printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
873 dev->name,pci_dev->subsystem_vendor,
874 pci_dev->subsystem_device,saa7134_boards[dev->board].name,
875 dev->board, card[dev->nr] == dev->board ?
876 "insmod option" : "autodetected");
879 if (!request_mem_region(pci_resource_start(pci_dev,0),
880 pci_resource_len(pci_dev,0),
883 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%lx\n",
884 dev->name,pci_resource_start(pci_dev,0));
887 dev->lmmio = ioremap(pci_resource_start(pci_dev,0), 0x1000);
888 dev->bmmio = (__u8 __iomem *)dev->lmmio;
889 if (NULL == dev->lmmio) {
891 printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
896 /* initialize hardware #1 */
897 saa7134_board_init1(dev);
898 saa7134_hwinit1(dev);
901 err = request_irq(pci_dev->irq, saa7134_irq,
902 SA_SHIRQ | SA_INTERRUPT, dev->name, dev);
904 printk(KERN_ERR "%s: can't get IRQ %d\n",
905 dev->name,pci_dev->irq);
909 /* wait a bit, register i2c bus */
911 saa7134_i2c_register(dev);
913 /* initialize hardware #2 */
914 saa7134_board_init2(dev);
915 saa7134_hwinit2(dev);
917 /* load i2c helpers */
918 if (TUNER_ABSENT != dev->tuner_type)
919 request_module("tuner");
920 if (dev->tda9887_conf)
921 request_module("tda9887");
922 if (card_is_empress(dev)) {
923 request_module("saa6752hs");
924 request_module_depend("saa7134-empress",&need_empress);
927 if (card_is_dvb(dev))
928 request_module_depend("saa7134-dvb",&need_dvb);
932 request_module_depend("saa7134-alsa",&need_alsa);
935 request_module_depend("saa7134-oss",&need_oss);
937 v4l2_prio_init(&dev->prio);
939 /* register v4l devices */
940 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
941 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
944 printk(KERN_INFO "%s: can't register video device\n",
948 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
949 dev->name,dev->video_dev->minor & 0x1f);
951 dev->vbi_dev = vdev_init(dev,&saa7134_vbi_template,"vbi");
952 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
956 printk(KERN_INFO "%s: registered device vbi%d\n",
957 dev->name,dev->vbi_dev->minor & 0x1f);
959 if (card_has_radio(dev)) {
960 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
961 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
965 printk(KERN_INFO "%s: registered device radio%d\n",
966 dev->name,dev->radio_dev->minor & 0x1f);
969 /* everything worked */
970 pci_set_drvdata(pci_dev,dev);
973 mutex_lock(&devlist_lock);
974 list_for_each(item,&mops_list) {
975 mops = list_entry(item, struct saa7134_mpeg_ops, next);
976 mpeg_ops_attach(mops, dev);
978 list_add_tail(&dev->devlist,&saa7134_devlist);
979 mutex_unlock(&devlist_lock);
981 /* check for signal */
982 saa7134_irq_video_intl(dev);
984 if (dmasound_init && !dev->dmasound.priv_data) {
991 saa7134_unregister_video(dev);
992 saa7134_i2c_unregister(dev);
993 free_irq(pci_dev->irq, dev);
998 release_mem_region(pci_resource_start(pci_dev,0),
999 pci_resource_len(pci_dev,0));
1005 static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1007 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1008 struct list_head *item;
1009 struct saa7134_mpeg_ops *mops;
1011 /* Release DMA sound modules if present */
1012 if (dmasound_exit && dev->dmasound.priv_data) {
1018 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1019 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1020 print_irqstatus(dev,42,report,status);
1023 /* disable peripheral devices */
1024 saa_writeb(SAA7134_SPECIAL_MODE,0);
1026 /* shutdown hardware */
1027 saa_writel(SAA7134_IRQ1,0);
1028 saa_writel(SAA7134_IRQ2,0);
1029 saa_writel(SAA7134_MAIN_CTRL,0);
1031 /* shutdown subsystems */
1032 saa7134_hwfini(dev);
1035 mutex_lock(&devlist_lock);
1036 list_del(&dev->devlist);
1037 list_for_each(item,&mops_list) {
1038 mops = list_entry(item, struct saa7134_mpeg_ops, next);
1039 mpeg_ops_detach(mops, dev);
1041 mutex_unlock(&devlist_lock);
1044 saa7134_i2c_unregister(dev);
1045 saa7134_unregister_video(dev);
1048 /* the DMA sound modules should be unloaded before reaching
1049 this, but just in case they are still present... */
1050 if (dev->dmasound.priv_data != NULL) {
1051 free_irq(pci_dev->irq, &dev->dmasound);
1052 dev->dmasound.priv_data = NULL;
1056 /* release resources */
1057 free_irq(pci_dev->irq, dev);
1058 iounmap(dev->lmmio);
1059 release_mem_region(pci_resource_start(pci_dev,0),
1060 pci_resource_len(pci_dev,0));
1062 pci_set_drvdata(pci_dev, NULL);
1068 /* ----------------------------------------------------------- */
1070 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1072 struct list_head *item;
1073 struct saa7134_dev *dev;
1075 mutex_lock(&devlist_lock);
1076 list_for_each(item,&saa7134_devlist) {
1077 dev = list_entry(item, struct saa7134_dev, devlist);
1078 mpeg_ops_attach(ops, dev);
1080 list_add_tail(&ops->next,&mops_list);
1081 mutex_unlock(&devlist_lock);
1085 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1087 struct list_head *item;
1088 struct saa7134_dev *dev;
1090 mutex_lock(&devlist_lock);
1091 list_del(&ops->next);
1092 list_for_each(item,&saa7134_devlist) {
1093 dev = list_entry(item, struct saa7134_dev, devlist);
1094 mpeg_ops_detach(ops, dev);
1096 mutex_unlock(&devlist_lock);
1099 EXPORT_SYMBOL(saa7134_ts_register);
1100 EXPORT_SYMBOL(saa7134_ts_unregister);
1102 /* ----------------------------------------------------------- */
1104 static struct pci_driver saa7134_pci_driver = {
1106 .id_table = saa7134_pci_tbl,
1107 .probe = saa7134_initdev,
1108 .remove = __devexit_p(saa7134_finidev),
1111 static int saa7134_init(void)
1113 INIT_LIST_HEAD(&saa7134_devlist);
1114 printk(KERN_INFO "saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
1115 (SAA7134_VERSION_CODE >> 16) & 0xff,
1116 (SAA7134_VERSION_CODE >> 8) & 0xff,
1117 SAA7134_VERSION_CODE & 0xff);
1119 printk(KERN_INFO "saa7130/34: snapshot date %04d-%02d-%02d\n",
1120 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1122 return pci_register_driver(&saa7134_pci_driver);
1125 static void saa7134_fini(void)
1127 #if defined(CONFIG_MODULES) && defined(MODULE)
1128 if (pending_registered)
1129 unregister_module_notifier(&pending_notifier);
1130 #endif /* CONFIG_MODULES */
1131 pci_unregister_driver(&saa7134_pci_driver);
1134 module_init(saa7134_init);
1135 module_exit(saa7134_fini);
1137 /* ----------------------------------------------------------- */
1139 EXPORT_SYMBOL(saa7134_i2c_call_clients);
1140 EXPORT_SYMBOL(saa7134_devlist);
1141 EXPORT_SYMBOL(saa7134_boards);
1143 /* ----------------- for the DMA sound modules --------------- */
1145 EXPORT_SYMBOL(dmasound_init);
1146 EXPORT_SYMBOL(dmasound_exit);
1147 EXPORT_SYMBOL(saa7134_pgtable_free);
1148 EXPORT_SYMBOL(saa7134_pgtable_build);
1149 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1150 EXPORT_SYMBOL(saa7134_set_dmabits);
1152 /* ----------------------------------------------------------- */