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