fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / media / video / cx88 / cx88-mpeg.c
1 /*
2  *
3  *  Support for the mpeg transport stream transfers
4  *  PCI function #2 of the cx2388x.
5  *
6  *    (c) 2004 Jelle Foks <jelle@foks.8m.com>
7  *    (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8  *    (c) 2004 Gerd Knorr <kraxel@bytesex.org>
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/device.h>
29 #include <linux/interrupt.h>
30 #include <asm/delay.h>
31
32 #include "cx88.h"
33
34 /* ------------------------------------------------------------------ */
35
36 MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
37 MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>");
38 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
39 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
40 MODULE_LICENSE("GPL");
41
42 static unsigned int debug = 0;
43 module_param(debug,int,0644);
44 MODULE_PARM_DESC(debug,"enable debug messages [mpeg]");
45
46 #define dprintk(level,fmt, arg...)      if (debug >= level) \
47         printk(KERN_DEBUG "%s/2-mpeg: " fmt, dev->core->name, ## arg)
48
49 #define mpeg_dbg(level,fmt, arg...)     if (debug >= level) \
50         printk(KERN_DEBUG "%s/2-mpeg: " fmt, core->name, ## arg)
51
52 #if defined(CONFIG_MODULES) && defined(MODULE)
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
54 static void request_module_async(void *ptr)
55 {
56         struct cx8802_dev *dev=(struct cx8802_dev*)ptr;
57 #else
58 static void request_module_async(struct work_struct *work)
59 {
60         struct cx8802_dev *dev=container_of(work, struct cx8802_dev, request_module_wk);
61 #endif
62
63         if (cx88_boards[dev->core->board].mpeg & CX88_MPEG_DVB)
64                 request_module("cx88-dvb");
65         if (cx88_boards[dev->core->board].mpeg & CX88_MPEG_BLACKBIRD)
66                 request_module("cx88-blackbird");
67 }
68
69 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
70 #define request_modules(dev)
71 #else
72 static void request_modules(struct cx8802_dev *dev)
73 {
74 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
75         INIT_WORK(&dev->request_module_wk, request_module_async, (void*)dev);
76 #else
77         INIT_WORK(&dev->request_module_wk, request_module_async);
78 #endif
79         schedule_work(&dev->request_module_wk);
80 }
81 #endif
82 #else
83 #define request_modules(dev)
84 #endif /* CONFIG_MODULES */
85
86 static LIST_HEAD(cx8802_devlist);
87 /* ------------------------------------------------------------------ */
88
89 static int cx8802_start_dma(struct cx8802_dev    *dev,
90                             struct cx88_dmaqueue *q,
91                             struct cx88_buffer   *buf)
92 {
93         struct cx88_core *core = dev->core;
94
95         dprintk(1, "cx8802_start_dma w: %d, h: %d, f: %d\n", dev->width, dev->height, buf->vb.field);
96
97         /* setup fifo + format */
98         cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
99                                 dev->ts_packet_size, buf->risc.dma);
100
101         /* write TS length to chip */
102         cx_write(MO_TS_LNGTH, buf->vb.width);
103
104         /* FIXME: this needs a review.
105          * also: move to cx88-blackbird + cx88-dvb source files? */
106
107         dprintk( 1, "core->active_type_id = 0x%08x\n", core->active_type_id);
108
109         if ( (core->active_type_id == CX88_MPEG_DVB) &&
110                 (cx88_boards[core->board].mpeg & CX88_MPEG_DVB) ) {
111
112                 dprintk( 1, "cx8802_start_dma doing .dvb\n");
113                 /* negedge driven & software reset */
114                 cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
115                 udelay(100);
116                 cx_write(MO_PINMUX_IO, 0x00);
117                 cx_write(TS_HW_SOP_CNTRL,0x47<<16|188<<4|0x01);
118                 switch (core->board) {
119                 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
120                 case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
121                 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
122                 case CX88_BOARD_PCHDTV_HD5500:
123                         cx_write(TS_SOP_STAT, 1<<13);
124                         break;
125                 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
126                 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
127                         cx_write(MO_PINMUX_IO, 0x88); /* Enable MPEG parallel IO and video signal pins */
128                         udelay(100);
129                         break;
130                 case CX88_BOARD_HAUPPAUGE_HVR1300:
131                         break;
132                 default:
133                         cx_write(TS_SOP_STAT, 0x00);
134                         break;
135                 }
136                 cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
137                 udelay(100);
138         } else if ( (core->active_type_id == CX88_MPEG_BLACKBIRD) &&
139                 (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) ) {
140                 dprintk( 1, "cx8802_start_dma doing .blackbird\n");
141                 cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
142
143                 cx_write(TS_GEN_CNTRL, 0x46); /* punctured clock TS & posedge driven & software reset */
144                 udelay(100);
145
146                 cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
147                 cx_write(TS_VALERR_CNTRL, 0x2000);
148
149                 cx_write(TS_GEN_CNTRL, 0x06); /* punctured clock TS & posedge driven */
150                 udelay(100);
151         } else {
152                 printk( "%s() Failed. Unsupported value in .mpeg (0x%08x)\n", __FUNCTION__,
153                         cx88_boards[core->board].mpeg );
154                 return -EINVAL;
155         }
156
157         /* reset counter */
158         cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
159         q->count = 1;
160
161         /* enable irqs */
162         dprintk( 1, "setting the interrupt mask\n" );
163         cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x04);
164         cx_set(MO_TS_INTMSK,  0x1f0011);
165
166         /* start dma */
167         cx_set(MO_DEV_CNTRL2, (1<<5));
168         cx_set(MO_TS_DMACNTRL, 0x11);
169         return 0;
170 }
171
172 static int cx8802_stop_dma(struct cx8802_dev *dev)
173 {
174         struct cx88_core *core = dev->core;
175         dprintk( 1, "cx8802_stop_dma\n" );
176
177         /* stop dma */
178         cx_clear(MO_TS_DMACNTRL, 0x11);
179
180         /* disable irqs */
181         cx_clear(MO_PCI_INTMSK, 0x000004);
182         cx_clear(MO_TS_INTMSK, 0x1f0011);
183
184         /* Reset the controller */
185         cx_write(TS_GEN_CNTRL, 0xcd);
186         return 0;
187 }
188
189 static int cx8802_restart_queue(struct cx8802_dev    *dev,
190                                 struct cx88_dmaqueue *q)
191 {
192         struct cx88_buffer *buf;
193         struct list_head *item;
194
195        dprintk( 1, "cx8802_restart_queue\n" );
196         if (list_empty(&q->active))
197         {
198                struct cx88_buffer *prev;
199                prev = NULL;
200
201                dprintk(1, "cx8802_restart_queue: queue is empty\n" );
202
203                for (;;) {
204                        if (list_empty(&q->queued))
205                                return 0;
206                        buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
207                        if (NULL == prev) {
208                                list_del(&buf->vb.queue);
209                                list_add_tail(&buf->vb.queue,&q->active);
210                                cx8802_start_dma(dev, q, buf);
211                                buf->vb.state = STATE_ACTIVE;
212                                buf->count    = q->count++;
213                                mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
214                                dprintk(1,"[%p/%d] restart_queue - first active\n",
215                                        buf,buf->vb.i);
216
217                        } else if (prev->vb.width  == buf->vb.width  &&
218                                   prev->vb.height == buf->vb.height &&
219                                   prev->fmt       == buf->fmt) {
220                                list_del(&buf->vb.queue);
221                                list_add_tail(&buf->vb.queue,&q->active);
222                                buf->vb.state = STATE_ACTIVE;
223                                buf->count    = q->count++;
224                                prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
225                                dprintk(1,"[%p/%d] restart_queue - move to active\n",
226                                        buf,buf->vb.i);
227                        } else {
228                                return 0;
229                        }
230                        prev = buf;
231                }
232                 return 0;
233         }
234
235         buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
236         dprintk(2,"restart_queue [%p/%d]: restart dma\n",
237                 buf, buf->vb.i);
238         cx8802_start_dma(dev, q, buf);
239         list_for_each(item,&q->active) {
240                 buf = list_entry(item, struct cx88_buffer, vb.queue);
241                 buf->count = q->count++;
242         }
243         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
244         return 0;
245 }
246
247 /* ------------------------------------------------------------------ */
248
249 int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
250                         struct cx88_buffer *buf, enum v4l2_field field)
251 {
252         int size = dev->ts_packet_size * dev->ts_packet_count;
253         int rc;
254
255         dprintk(1, "%s: %p\n", __FUNCTION__, buf);
256         if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
257                 return -EINVAL;
258
259         if (STATE_NEEDS_INIT == buf->vb.state) {
260                 buf->vb.width  = dev->ts_packet_size;
261                 buf->vb.height = dev->ts_packet_count;
262                 buf->vb.size   = size;
263                 buf->vb.field  = field /*V4L2_FIELD_TOP*/;
264
265                 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
266                         goto fail;
267                 cx88_risc_databuffer(dev->pci, &buf->risc,
268                                      buf->vb.dma.sglist,
269                                      buf->vb.width, buf->vb.height);
270         }
271         buf->vb.state = STATE_PREPARED;
272         return 0;
273
274  fail:
275         cx88_free_buffer(q,buf);
276         return rc;
277 }
278
279 void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
280 {
281         struct cx88_buffer    *prev;
282         struct cx88_dmaqueue  *cx88q = &dev->mpegq;
283
284         dprintk( 1, "cx8802_buf_queue\n" );
285         /* add jump to stopper */
286         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
287         buf->risc.jmp[1] = cpu_to_le32(cx88q->stopper.dma);
288
289         if (list_empty(&cx88q->active)) {
290                 dprintk( 1, "queue is empty - first active\n" );
291                 list_add_tail(&buf->vb.queue,&cx88q->active);
292                 cx8802_start_dma(dev, cx88q, buf);
293                 buf->vb.state = STATE_ACTIVE;
294                 buf->count    = cx88q->count++;
295                 mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT);
296                 dprintk(1,"[%p/%d] %s - first active\n",
297                         buf, buf->vb.i, __FUNCTION__);
298
299         } else {
300                 dprintk( 1, "queue is not empty - append to active\n" );
301                 prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
302                 list_add_tail(&buf->vb.queue,&cx88q->active);
303                 buf->vb.state = STATE_ACTIVE;
304                 buf->count    = cx88q->count++;
305                 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
306                 dprintk( 1, "[%p/%d] %s - append to active\n",
307                         buf, buf->vb.i, __FUNCTION__);
308         }
309 }
310
311 /* ----------------------------------------------------------- */
312
313 static void do_cancel_buffers(struct cx8802_dev *dev, char *reason, int restart)
314 {
315         struct cx88_dmaqueue *q = &dev->mpegq;
316         struct cx88_buffer *buf;
317         unsigned long flags;
318
319         spin_lock_irqsave(&dev->slock,flags);
320         while (!list_empty(&q->active)) {
321                 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
322                 list_del(&buf->vb.queue);
323                 buf->vb.state = STATE_ERROR;
324                 wake_up(&buf->vb.done);
325                 dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
326                         buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
327         }
328         if (restart)
329         {
330                 dprintk(1, "restarting queue\n" );
331                 cx8802_restart_queue(dev,q);
332         }
333         spin_unlock_irqrestore(&dev->slock,flags);
334 }
335
336 void cx8802_cancel_buffers(struct cx8802_dev *dev)
337 {
338         struct cx88_dmaqueue *q = &dev->mpegq;
339
340         dprintk( 1, "cx8802_cancel_buffers" );
341         del_timer_sync(&q->timeout);
342         cx8802_stop_dma(dev);
343         do_cancel_buffers(dev,"cancel",0);
344 }
345
346 static void cx8802_timeout(unsigned long data)
347 {
348         struct cx8802_dev *dev = (struct cx8802_dev*)data;
349
350         dprintk(0, "%s\n",__FUNCTION__);
351
352         if (debug)
353                 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
354         cx8802_stop_dma(dev);
355         do_cancel_buffers(dev,"timeout",1);
356 }
357
358 static char *cx88_mpeg_irqs[32] = {
359         "ts_risci1", NULL, NULL, NULL,
360         "ts_risci2", NULL, NULL, NULL,
361         "ts_oflow",  NULL, NULL, NULL,
362         "ts_sync",   NULL, NULL, NULL,
363         "opc_err", "par_err", "rip_err", "pci_abort",
364         "ts_err?",
365 };
366
367 static void cx8802_mpeg_irq(struct cx8802_dev *dev)
368 {
369         struct cx88_core *core = dev->core;
370         u32 status, mask, count;
371
372         dprintk( 1, "cx8802_mpeg_irq\n" );
373         status = cx_read(MO_TS_INTSTAT);
374         mask   = cx_read(MO_TS_INTMSK);
375         if (0 == (status & mask))
376                 return;
377
378         cx_write(MO_TS_INTSTAT, status);
379
380         if (debug || (status & mask & ~0xff))
381                 cx88_print_irqbits(core->name, "irq mpeg ",
382                                    cx88_mpeg_irqs, status, mask);
383
384         /* risc op code error */
385         if (status & (1 << 16)) {
386                 printk(KERN_WARNING "%s: mpeg risc op code error\n",core->name);
387                 cx_clear(MO_TS_DMACNTRL, 0x11);
388                 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
389         }
390
391         /* risc1 y */
392         if (status & 0x01) {
393                 dprintk( 1, "wake up\n" );
394                 spin_lock(&dev->slock);
395                 count = cx_read(MO_TS_GPCNT);
396                 cx88_wakeup(dev->core, &dev->mpegq, count);
397                 spin_unlock(&dev->slock);
398         }
399
400         /* risc2 y */
401         if (status & 0x10) {
402                 spin_lock(&dev->slock);
403                 cx8802_restart_queue(dev,&dev->mpegq);
404                 spin_unlock(&dev->slock);
405         }
406
407         /* other general errors */
408         if (status & 0x1f0100) {
409                 dprintk( 0, "general errors: 0x%08x\n", status & 0x1f0100 );
410                 spin_lock(&dev->slock);
411                 cx8802_stop_dma(dev);
412                 cx8802_restart_queue(dev,&dev->mpegq);
413                 spin_unlock(&dev->slock);
414         }
415 }
416
417 #define MAX_IRQ_LOOP 10
418
419 static irqreturn_t cx8802_irq(int irq, void *dev_id)
420 {
421         struct cx8802_dev *dev = dev_id;
422         struct cx88_core *core = dev->core;
423         u32 status;
424         int loop, handled = 0;
425
426         for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
427                 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x04);
428                 if (0 == status)
429                         goto out;
430                 dprintk( 1, "cx8802_irq\n" );
431                 dprintk( 1, "    loop: %d/%d\n", loop, MAX_IRQ_LOOP );
432                 dprintk( 1, "    status: %d\n", status );
433                 handled = 1;
434                 cx_write(MO_PCI_INTSTAT, status);
435
436                 if (status & core->pci_irqmask)
437                         cx88_core_irq(core,status);
438                 if (status & 0x04)
439                         cx8802_mpeg_irq(dev);
440         };
441         if (MAX_IRQ_LOOP == loop) {
442                 dprintk( 0, "clearing mask\n" );
443                 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
444                        core->name);
445                 cx_write(MO_PCI_INTMSK,0);
446         }
447
448  out:
449         return IRQ_RETVAL(handled);
450 }
451
452 /* ----------------------------------------------------------- */
453 /* exported stuff                                              */
454
455 int cx8802_init_common(struct cx8802_dev *dev)
456 {
457         struct cx88_core *core = dev->core;
458         int err;
459
460         /* pci init */
461         if (pci_enable_device(dev->pci))
462                 return -EIO;
463         pci_set_master(dev->pci);
464         if (!pci_dma_supported(dev->pci,0xffffffff)) {
465                 printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
466                 return -EIO;
467         }
468
469         pci_read_config_byte(dev->pci, PCI_CLASS_REVISION, &dev->pci_rev);
470         pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER,  &dev->pci_lat);
471         printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
472                "latency: %d, mmio: 0x%llx\n", dev->core->name,
473                pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
474                dev->pci_lat,(unsigned long long)pci_resource_start(dev->pci,0));
475
476         /* initialize driver struct */
477         spin_lock_init(&dev->slock);
478
479         /* init dma queue */
480         INIT_LIST_HEAD(&dev->mpegq.active);
481         INIT_LIST_HEAD(&dev->mpegq.queued);
482         dev->mpegq.timeout.function = cx8802_timeout;
483         dev->mpegq.timeout.data     = (unsigned long)dev;
484         init_timer(&dev->mpegq.timeout);
485         cx88_risc_stopper(dev->pci,&dev->mpegq.stopper,
486                           MO_TS_DMACNTRL,0x11,0x00);
487
488         /* get irq */
489         err = request_irq(dev->pci->irq, cx8802_irq,
490                           IRQF_SHARED | IRQF_DISABLED, dev->core->name, dev);
491         if (err < 0) {
492                 printk(KERN_ERR "%s: can't get IRQ %d\n",
493                        dev->core->name, dev->pci->irq);
494                 return err;
495         }
496         cx_set(MO_PCI_INTMSK, core->pci_irqmask);
497
498         /* everything worked */
499         pci_set_drvdata(dev->pci,dev);
500         return 0;
501 }
502
503 void cx8802_fini_common(struct cx8802_dev *dev)
504 {
505         dprintk( 2, "cx8802_fini_common\n" );
506         cx8802_stop_dma(dev);
507         pci_disable_device(dev->pci);
508
509         /* unregister stuff */
510         free_irq(dev->pci->irq, dev);
511         pci_set_drvdata(dev->pci, NULL);
512
513         /* free memory */
514         btcx_riscmem_free(dev->pci,&dev->mpegq.stopper);
515 }
516
517 /* ----------------------------------------------------------- */
518
519 int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
520 {
521         struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
522         struct cx88_core *core = dev->core;
523
524         /* stop mpeg dma */
525         spin_lock(&dev->slock);
526         if (!list_empty(&dev->mpegq.active)) {
527                 dprintk( 2, "suspend\n" );
528                 printk("%s: suspend mpeg\n", core->name);
529                 cx8802_stop_dma(dev);
530                 del_timer(&dev->mpegq.timeout);
531         }
532         spin_unlock(&dev->slock);
533
534         /* FIXME -- shutdown device */
535         cx88_shutdown(dev->core);
536
537         pci_save_state(pci_dev);
538         if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
539                 pci_disable_device(pci_dev);
540                 dev->state.disabled = 1;
541         }
542         return 0;
543 }
544
545 int cx8802_resume_common(struct pci_dev *pci_dev)
546 {
547         struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
548         struct cx88_core *core = dev->core;
549         int err;
550
551         if (dev->state.disabled) {
552                 err=pci_enable_device(pci_dev);
553                 if (err) {
554                         printk(KERN_ERR "%s: can't enable device\n",
555                                                dev->core->name);
556                         return err;
557                 }
558                 dev->state.disabled = 0;
559         }
560         err=pci_set_power_state(pci_dev, PCI_D0);
561         if (err) {
562                 printk(KERN_ERR "%s: can't enable device\n",
563                                                dev->core->name);
564                 pci_disable_device(pci_dev);
565                 dev->state.disabled = 1;
566
567                 return err;
568         }
569         pci_restore_state(pci_dev);
570
571         /* FIXME: re-initialize hardware */
572         cx88_reset(dev->core);
573
574         /* restart video+vbi capture */
575         spin_lock(&dev->slock);
576         if (!list_empty(&dev->mpegq.active)) {
577                 printk("%s: resume mpeg\n", core->name);
578                 cx8802_restart_queue(dev,&dev->mpegq);
579         }
580         spin_unlock(&dev->slock);
581
582         return 0;
583 }
584
585 struct cx8802_dev * cx8802_get_device(struct inode *inode)
586 {
587         int minor = iminor(inode);
588         struct cx8802_dev *h = NULL;
589         struct list_head *list;
590
591         list_for_each(list,&cx8802_devlist) {
592                 h = list_entry(list, struct cx8802_dev, devlist);
593                 if (h->mpeg_dev->minor == minor)
594                         return h;
595         }
596
597         return NULL;
598 }
599
600 struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype)
601 {
602         struct cx8802_dev *h = NULL;
603         struct cx8802_driver *d = NULL;
604         struct list_head *list;
605         struct list_head *list2;
606
607         list_for_each(list,&cx8802_devlist) {
608                 h = list_entry(list, struct cx8802_dev, devlist);
609                 if (h != dev)
610                         continue;
611
612                 list_for_each(list2, &h->drvlist.devlist) {
613                         d = list_entry(list2, struct cx8802_driver, devlist);
614
615                         /* only unregister the correct driver type */
616                         if (d->type_id == btype) {
617                                 return d;
618                         }
619                 }
620         }
621
622         return NULL;
623 }
624
625 /* Driver asked for hardware access. */
626 int cx8802_request_acquire(struct cx8802_driver *drv)
627 {
628         struct cx88_core *core = drv->core;
629
630         /* Fail a request for hardware if the device is busy. */
631         if (core->active_type_id != CX88_BOARD_NONE)
632                 return -EBUSY;
633
634         if (drv->advise_acquire)
635         {
636                 core->active_type_id = drv->type_id;
637                 drv->advise_acquire(drv);
638
639                 mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
640         }
641
642         return 0;
643 }
644
645 /* Driver asked to release hardware. */
646 int cx8802_request_release(struct cx8802_driver *drv)
647 {
648         struct cx88_core *core = drv->core;
649
650         if (drv->advise_release)
651         {
652                 drv->advise_release(drv);
653                 core->active_type_id = CX88_BOARD_NONE;
654                 mpeg_dbg(1,"%s() Post release GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
655         }
656
657         return 0;
658 }
659
660 static int cx8802_check_driver(struct cx8802_driver *drv)
661 {
662         if (drv == NULL)
663                 return -ENODEV;
664
665         if ((drv->type_id != CX88_MPEG_DVB) &&
666                 (drv->type_id != CX88_MPEG_BLACKBIRD))
667                 return -EINVAL;
668
669         if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
670                 (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
671                 return -EINVAL;
672
673         if ((drv->probe == NULL) ||
674                 (drv->remove == NULL) ||
675                 (drv->advise_acquire == NULL) ||
676                 (drv->advise_release == NULL))
677                 return -EINVAL;
678
679         return 0;
680 }
681
682 int cx8802_register_driver(struct cx8802_driver *drv)
683 {
684         struct cx8802_dev *h;
685         struct cx8802_driver *driver;
686         struct list_head *list;
687         int err = 0, i = 0;
688
689         printk(KERN_INFO "%s() ->registering driver type=%s access=%s\n", __FUNCTION__ ,
690                 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
691                 drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
692
693         if ((err = cx8802_check_driver(drv)) != 0) {
694                 printk(KERN_INFO "%s() cx8802_driver is invalid\n", __FUNCTION__ );
695                 return err;
696         }
697
698         list_for_each(list,&cx8802_devlist) {
699                 h = list_entry(list, struct cx8802_dev, devlist);
700
701                 printk(KERN_INFO "CORE %s: subsystem: %04x:%04x, board: %s [card=%d]\n",
702                         h->core->name,h->pci->subsystem_vendor,
703                         h->pci->subsystem_device,cx88_boards[h->core->board].name,
704                         h->core->board);
705
706                 /* Bring up a new struct for each driver instance */
707                 driver = kzalloc(sizeof(*drv),GFP_KERNEL);
708                 if (driver == NULL)
709                         return -ENOMEM;
710
711                 /* Snapshot of the driver registration data */
712                 drv->core = h->core;
713                 drv->suspend = cx8802_suspend_common;
714                 drv->resume = cx8802_resume_common;
715                 drv->request_acquire = cx8802_request_acquire;
716                 drv->request_release = cx8802_request_release;
717                 memcpy(driver, drv, sizeof(*driver));
718
719                 err = drv->probe(driver);
720                 if (err == 0) {
721                         i++;
722                         mutex_lock(&drv->core->lock);
723                         list_add_tail(&driver->devlist,&h->drvlist.devlist);
724                         mutex_unlock(&drv->core->lock);
725                 } else {
726                         printk(KERN_ERR "%s() ->probe failed err = %d\n", __FUNCTION__, err);
727                 }
728
729         }
730         if (i == 0)
731                 err = -ENODEV;
732         else
733                 err = 0;
734
735         return err;
736 }
737
738 int cx8802_unregister_driver(struct cx8802_driver *drv)
739 {
740         struct cx8802_dev *h;
741         struct cx8802_driver *d;
742         struct list_head *list;
743         struct list_head *list2, *q;
744         int err = 0, i = 0;
745
746         printk(KERN_INFO "%s() ->unregistering driver type=%s\n", __FUNCTION__ ,
747                 drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird");
748
749         list_for_each(list,&cx8802_devlist) {
750                 i++;
751                 h = list_entry(list, struct cx8802_dev, devlist);
752
753                 printk(KERN_INFO "CORE %s: subsystem: %04x:%04x, board: %s [card=%d]\n",
754                         h->core->name,h->pci->subsystem_vendor,
755                         h->pci->subsystem_device,cx88_boards[h->core->board].name,
756                         h->core->board);
757
758                 list_for_each_safe(list2, q, &h->drvlist.devlist) {
759                         d = list_entry(list2, struct cx8802_driver, devlist);
760
761                         /* only unregister the correct driver type */
762                         if (d->type_id != drv->type_id)
763                                 continue;
764
765                         err = d->remove(d);
766                         if (err == 0) {
767                                 mutex_lock(&drv->core->lock);
768                                 list_del(list2);
769                                 mutex_unlock(&drv->core->lock);
770                         } else
771                                 printk(KERN_ERR "%s() ->remove failed err = %d\n", __FUNCTION__, err);
772
773                 }
774
775         }
776
777         return err;
778 }
779
780 /* ----------------------------------------------------------- */
781 static int __devinit cx8802_probe(struct pci_dev *pci_dev,
782                                const struct pci_device_id *pci_id)
783 {
784         struct cx8802_dev *dev;
785         struct cx88_core  *core;
786         int err;
787
788         /* general setup */
789         core = cx88_core_get(pci_dev);
790         if (NULL == core)
791                 return -EINVAL;
792
793         printk("%s/2: cx2388x 8802 Driver Manager\n", core->name);
794
795         err = -ENODEV;
796         if (!cx88_boards[core->board].mpeg)
797                 goto fail_core;
798
799         err = -ENOMEM;
800         dev = kzalloc(sizeof(*dev),GFP_KERNEL);
801         if (NULL == dev)
802                 goto fail_core;
803         dev->pci = pci_dev;
804         dev->core = core;
805
806         err = cx8802_init_common(dev);
807         if (err != 0)
808                 goto fail_free;
809
810         INIT_LIST_HEAD(&dev->drvlist.devlist);
811         list_add_tail(&dev->devlist,&cx8802_devlist);
812
813         /* Maintain a reference so cx88-video can query the 8802 device. */
814         core->dvbdev = dev;
815
816         /* now autoload cx88-dvb or cx88-blackbird */
817         request_modules(dev);
818         return 0;
819
820  fail_free:
821         kfree(dev);
822  fail_core:
823         cx88_core_put(core,pci_dev);
824         return err;
825 }
826
827 static void __devexit cx8802_remove(struct pci_dev *pci_dev)
828 {
829         struct cx8802_dev *dev;
830         struct cx8802_driver *h;
831         struct list_head *list;
832
833         dev = pci_get_drvdata(pci_dev);
834
835         dprintk( 1, "%s\n", __FUNCTION__);
836
837         list_for_each(list,&dev->drvlist.devlist) {
838                 h = list_entry(list, struct cx8802_driver, devlist);
839                 dprintk( 1, " ->driver\n");
840                 if (h->remove == NULL) {
841                         printk(KERN_ERR "%s .. skipping driver, no probe function\n", __FUNCTION__);
842                         continue;
843                 }
844                 printk(KERN_INFO "%s .. Removing driver type %d\n", __FUNCTION__, h->type_id);
845                 cx8802_unregister_driver(h);
846                 list_del(&dev->drvlist.devlist);
847         }
848
849         /* Destroy any 8802 reference. */
850         dev->core->dvbdev = NULL;
851
852         /* common */
853         cx8802_fini_common(dev);
854         cx88_core_put(dev->core,dev->pci);
855         kfree(dev);
856 }
857
858 static struct pci_device_id cx8802_pci_tbl[] = {
859         {
860                 .vendor       = 0x14f1,
861                 .device       = 0x8802,
862                 .subvendor    = PCI_ANY_ID,
863                 .subdevice    = PCI_ANY_ID,
864         },{
865                 /* --- end of list --- */
866         }
867 };
868 MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
869
870 static struct pci_driver cx8802_pci_driver = {
871         .name     = "cx88-mpeg driver manager",
872         .id_table = cx8802_pci_tbl,
873         .probe    = cx8802_probe,
874         .remove   = __devexit_p(cx8802_remove),
875 };
876
877 static int cx8802_init(void)
878 {
879         printk(KERN_INFO "cx2388x cx88-mpeg Driver Manager version %d.%d.%d loaded\n",
880                (CX88_VERSION_CODE >> 16) & 0xff,
881                (CX88_VERSION_CODE >>  8) & 0xff,
882                CX88_VERSION_CODE & 0xff);
883 #ifdef SNAPSHOT
884         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
885                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
886 #endif
887         return pci_register_driver(&cx8802_pci_driver);
888 }
889
890 static void cx8802_fini(void)
891 {
892         pci_unregister_driver(&cx8802_pci_driver);
893 }
894
895 module_init(cx8802_init);
896 module_exit(cx8802_fini);
897 EXPORT_SYMBOL(cx8802_buf_prepare);
898 EXPORT_SYMBOL(cx8802_buf_queue);
899 EXPORT_SYMBOL(cx8802_cancel_buffers);
900
901 EXPORT_SYMBOL(cx8802_init_common);
902 EXPORT_SYMBOL(cx8802_fini_common);
903
904 EXPORT_SYMBOL(cx8802_register_driver);
905 EXPORT_SYMBOL(cx8802_unregister_driver);
906 EXPORT_SYMBOL(cx8802_get_device);
907 EXPORT_SYMBOL(cx8802_get_driver);
908 /* ----------------------------------------------------------- */
909 /*
910  * Local variables:
911  * c-basic-offset: 8
912  * End:
913  * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
914  */