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