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