V4L/DVB: cx18, cx23885, v4l2 doc, MAINTAINERS: Update Andy Walls' email address
[safe/jmp/linux-2.6] / drivers / media / video / cx18 / cx18-driver.c
index 1518a58..df60f27 100644 (file)
@@ -4,7 +4,7 @@
  *  Derived from ivtv-driver.c
  *
  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
- *  Copyright (C) 2008  Andy Walls <awalls@radix.net>
+ *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -30,6 +30,7 @@
 #include "cx18-irq.h"
 #include "cx18-gpio.h"
 #include "cx18-firmware.h"
+#include "cx18-queue.h"
 #include "cx18-streams.h"
 #include "cx18-av-core.h"
 #include "cx18-scb.h"
 
 #include <media/tveeprom.h>
 
-
-/* var to keep track of the number of array elements in use */
-int cx18_cards_active;
-
 /* If you have already X v4l cards, then set this to X. This way
    the device numbers stay matched. Example: you have a WinTV card
    without radio and a Compro H900 with. Normally this would give a
@@ -50,11 +47,9 @@ int cx18_cards_active;
    setting this to 1 you ensure that radio0 is now also radio1. */
 int cx18_first_minor;
 
-/* Master variable for all cx18 info */
-struct cx18 *cx18_cards[CX18_MAX_CARDS];
-
-/* Protects cx18_cards_active */
-DEFINE_SPINLOCK(cx18_cards_lock);
+/* Callback for registering extensions */
+int (*cx18_ext_init)(struct cx18 *);
+EXPORT_SYMBOL(cx18_ext_init);
 
 /* add your revision and whatnot here */
 static struct pci_device_id cx18_pci_tbl[] __devinitdata = {
@@ -65,6 +60,8 @@ static struct pci_device_id cx18_pci_tbl[] __devinitdata = {
 
 MODULE_DEVICE_TABLE(pci, cx18_pci_tbl);
 
+static atomic_t cx18_instance = ATOMIC_INIT(0);
+
 /* Parameter declarations */
 static int cardtype[CX18_MAX_CARDS];
 static int tuner[CX18_MAX_CARDS] = { -1, -1, -1, -1, -1, -1, -1, -1,
@@ -94,12 +91,11 @@ static int enc_ts_bufsize = CX18_DEFAULT_ENC_TS_BUFSIZE;
 static int enc_mpg_bufsize = CX18_DEFAULT_ENC_MPG_BUFSIZE;
 static int enc_idx_bufsize = CX18_DEFAULT_ENC_IDX_BUFSIZE;
 static int enc_yuv_bufsize = CX18_DEFAULT_ENC_YUV_BUFSIZE;
-/* VBI bufsize based on standards supported by card tuner for now */
 static int enc_pcm_bufsize = CX18_DEFAULT_ENC_PCM_BUFSIZE;
 
 static int enc_ts_bufs = -1;
 static int enc_mpg_bufs = -1;
-static int enc_idx_bufs = -1;
+static int enc_idx_bufs = CX18_MAX_FW_MDLS_PER_STREAM;
 static int enc_yuv_bufs = -1;
 static int enc_vbi_bufs = -1;
 static int enc_pcm_bufs = -1;
@@ -135,7 +131,6 @@ module_param(enc_ts_bufsize, int, 0644);
 module_param(enc_mpg_bufsize, int, 0644);
 module_param(enc_idx_bufsize, int, 0644);
 module_param(enc_yuv_bufsize, int, 0644);
-/* VBI bufsize based on standards supported by card tuner for now */
 module_param(enc_pcm_bufsize, int, 0644);
 
 module_param(enc_ts_bufs, int, 0644);
@@ -159,7 +154,8 @@ MODULE_PARM_DESC(cardtype,
                 "\t\t\t 4 = Yuan MPC718\n"
                 "\t\t\t 5 = Conexant Raptor PAL/SECAM\n"
                 "\t\t\t 6 = Toshiba Qosmio DVB-T/Analog\n"
-                "\t\t\t 7 = Leadtek WinFast PVR2100/DVR3100 H\n"
+                "\t\t\t 7 = Leadtek WinFast PVR2100\n"
+                "\t\t\t 8 = Leadtek WinFast DVR3100 H\n"
                 "\t\t\t 0 = Autodetect (default)\n"
                 "\t\t\t-1 = Ignore this card\n\t\t");
 MODULE_PARM_DESC(pal, "Set PAL standard: B, G, H, D, K, I, M, N, Nc, 60");
@@ -204,20 +200,25 @@ MODULE_PARM_DESC(enc_mpg_bufs,
                 "Number of encoder MPG buffers\n"
                 "\t\t\tDefault is computed from other enc_mpg_* parameters");
 MODULE_PARM_DESC(enc_idx_buffers,
-                "Encoder IDX buffer memory (MB). (enc_idx_bufs can override)\n"
-                "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFFERS));
+                "(Deprecated) Encoder IDX buffer memory (MB)\n"
+                "\t\t\tIgnored, except 0 disables IDX buffer allocations\n"
+                "\t\t\tDefault: 1 [Enabled]");
 MODULE_PARM_DESC(enc_idx_bufsize,
                 "Size of an encoder IDX buffer (kB)\n"
-                "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_IDX_BUFSIZE));
+                "\t\t\tAllowed values are multiples of 1.5 kB rounded up\n"
+                "\t\t\t(multiples of size required for 64 index entries)\n"
+                "\t\t\tDefault: 2");
 MODULE_PARM_DESC(enc_idx_bufs,
                 "Number of encoder IDX buffers\n"
-                "\t\t\tDefault is computed from other enc_idx_* parameters");
+                "\t\t\tDefault: " __stringify(CX18_MAX_FW_MDLS_PER_STREAM));
 MODULE_PARM_DESC(enc_yuv_buffers,
                 "Encoder YUV buffer memory (MB). (enc_yuv_bufs can override)\n"
                 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFFERS));
 MODULE_PARM_DESC(enc_yuv_bufsize,
                 "Size of an encoder YUV buffer (kB)\n"
-                "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_YUV_BUFSIZE));
+                "\t\t\tAllowed values are multiples of 33.75 kB rounded up\n"
+                "\t\t\t(multiples of size required for 32 screen lines)\n"
+                "\t\t\tDefault: 102");
 MODULE_PARM_DESC(enc_yuv_bufs,
                 "Number of encoder YUV buffers\n"
                 "\t\t\tDefault is computed from other enc_yuv_* parameters");
@@ -226,7 +227,7 @@ MODULE_PARM_DESC(enc_vbi_buffers,
                 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_VBI_BUFFERS));
 MODULE_PARM_DESC(enc_vbi_bufs,
                 "Number of encoder VBI buffers\n"
-                "\t\t\tDefault is computed from enc_vbi_buffers & tuner std");
+                "\t\t\tDefault is computed from enc_vbi_buffers");
 MODULE_PARM_DESC(enc_pcm_buffers,
                 "Encoder PCM buffer memory (MB). (enc_pcm_bufs can override)\n"
                 "\t\t\tDefault: " __stringify(CX18_DEFAULT_ENC_PCM_BUFFERS));
@@ -237,7 +238,8 @@ MODULE_PARM_DESC(enc_pcm_bufs,
                 "Number of encoder PCM buffers\n"
                 "\t\t\tDefault is computed from other enc_pcm_* parameters");
 
-MODULE_PARM_DESC(cx18_first_minor, "Set kernel number assigned to first card");
+MODULE_PARM_DESC(cx18_first_minor,
+                "Set device node number assigned to first card");
 
 MODULE_AUTHOR("Hans Verkuil");
 MODULE_DESCRIPTION("CX23418 driver");
@@ -246,6 +248,28 @@ MODULE_LICENSE("GPL");
 
 MODULE_VERSION(CX18_VERSION);
 
+#if defined(CONFIG_MODULES) && defined(MODULE)
+static void request_module_async(struct work_struct *work)
+{
+       struct cx18 *dev = container_of(work, struct cx18, request_module_wk);
+
+       /* Make sure cx18-alsa module is loaded */
+       request_module("cx18-alsa");
+
+       /* Initialize cx18-alsa for this instance of the cx18 device */
+       if (cx18_ext_init != NULL)
+               cx18_ext_init(dev);
+}
+
+static void request_modules(struct cx18 *dev)
+{
+       INIT_WORK(&dev->request_module_wk, request_module_async);
+       schedule_work(&dev->request_module_wk);
+}
+#else
+#define request_modules(dev)
+#endif /* CONFIG_MODULES */
+
 /* Generic utility functions */
 int cx18_msleep_timeout(unsigned int msecs, int intr)
 {
@@ -274,14 +298,51 @@ static void cx18_iounmap(struct cx18 *cx)
        }
 }
 
+static void cx18_eeprom_dump(struct cx18 *cx, unsigned char *eedata, int len)
+{
+       int i;
+
+       CX18_INFO("eeprom dump:\n");
+       for (i = 0; i < len; i++) {
+               if (0 == (i % 16))
+                       CX18_INFO("eeprom %02x:", i);
+               printk(KERN_CONT " %02x", eedata[i]);
+               if (15 == (i % 16))
+                       printk(KERN_CONT "\n");
+       }
+}
+
 /* Hauppauge card? get values from tveeprom */
 void cx18_read_eeprom(struct cx18 *cx, struct tveeprom *tv)
 {
+       struct i2c_client c;
        u8 eedata[256];
 
-       cx->i2c_client[0].addr = 0xA0 >> 1;
-       tveeprom_read(&cx->i2c_client[0], eedata, sizeof(eedata));
-       tveeprom_hauppauge_analog(&cx->i2c_client[0], tv, eedata);
+       memset(&c, 0, sizeof(c));
+       strlcpy(c.name, "cx18 tveeprom tmp", sizeof(c.name));
+       c.adapter = &cx->i2c_adap[0];
+       c.addr = 0xA0 >> 1;
+
+       memset(tv, 0, sizeof(*tv));
+       if (tveeprom_read(&c, eedata, sizeof(eedata)))
+               return;
+
+       switch (cx->card->type) {
+       case CX18_CARD_HVR_1600_ESMT:
+       case CX18_CARD_HVR_1600_SAMSUNG:
+               tveeprom_hauppauge_analog(&c, tv, eedata);
+               break;
+       case CX18_CARD_YUAN_MPC718:
+               tv->model = 0x718;
+               cx18_eeprom_dump(cx, eedata, sizeof(eedata));
+               CX18_INFO("eeprom PCI ID: %02x%02x:%02x%02x\n",
+                         eedata[2], eedata[1], eedata[4], eedata[3]);
+               break;
+       default:
+               tv->model = 0xffffffff;
+               cx18_eeprom_dump(cx, eedata, sizeof(eedata));
+               break;
+       }
 }
 
 static void cx18_process_eeprom(struct cx18 *cx)
@@ -299,6 +360,11 @@ static void cx18_process_eeprom(struct cx18 *cx)
        case 74000 ... 74999:
                cx->card = cx18_get_card(CX18_CARD_HVR_1600_ESMT);
                break;
+       case 0x718:
+               return;
+       case 0xffffffff:
+               CX18_INFO("Unknown EEPROM encoding\n");
+               return;
        case 0:
                CX18_ERR("Invalid EEPROM\n");
                return;
@@ -315,7 +381,7 @@ static void cx18_process_eeprom(struct cx18 *cx)
        CX18_INFO("Autodetected %s\n", cx->card_name);
 
        if (tv.tuner_type == TUNER_ABSENT)
-               CX18_ERR("tveeprom cannot autodetect tuner!");
+               CX18_ERR("tveeprom cannot autodetect tuner!\n");
 
        if (cx->options.tuner == -1)
                cx->options.tuner = tv.tuner_type;
@@ -448,48 +514,89 @@ static void cx18_process_options(struct cx18 *cx)
        cx->stream_buf_size[CX18_ENC_STREAM_TYPE_MPG] = enc_mpg_bufsize;
        cx->stream_buf_size[CX18_ENC_STREAM_TYPE_IDX] = enc_idx_bufsize;
        cx->stream_buf_size[CX18_ENC_STREAM_TYPE_YUV] = enc_yuv_bufsize;
-       cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = 0; /* computed later */
+       cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_active_samples * 36;
        cx->stream_buf_size[CX18_ENC_STREAM_TYPE_PCM] = enc_pcm_bufsize;
        cx->stream_buf_size[CX18_ENC_STREAM_TYPE_RAD] = 0; /* control no data */
 
-       /* Except for VBI ensure stream_buffers & stream_buf_size are valid */
+       /* Ensure stream_buffers & stream_buf_size are valid */
        for (i = 0; i < CX18_MAX_STREAMS; i++) {
-               /* User said to use 0 buffers */
-               if (cx->stream_buffers[i] == 0) {
-                       cx->options.megabytes[i] = 0;
-                       cx->stream_buf_size[i] = 0;
-                       continue;
-               }
-               /* User said to use 0 MB total */
-               if (cx->options.megabytes[i] <= 0) {
+               if (cx->stream_buffers[i] == 0 ||     /* User said 0 buffers */
+                   cx->options.megabytes[i] <= 0 ||  /* User said 0 MB total */
+                   cx->stream_buf_size[i] <= 0) {    /* User said buf size 0 */
                        cx->options.megabytes[i] = 0;
                        cx->stream_buffers[i] = 0;
                        cx->stream_buf_size[i] = 0;
                        continue;
                }
-               /* VBI is computed later or user said buffer has size 0 */
-               if (cx->stream_buf_size[i] <= 0) {
-                       if (i != CX18_ENC_STREAM_TYPE_VBI) {
-                               cx->options.megabytes[i] = 0;
-                               cx->stream_buffers[i] = 0;
-                               cx->stream_buf_size[i] = 0;
-                       }
-                       continue;
+               /*
+                * YUV is a special case where the stream_buf_size needs to be
+                * an integral multiple of 33.75 kB (storage for 32 screens
+                * lines to maintain alignment in case of lost buffers).
+                *
+                * IDX is a special case where the stream_buf_size should be
+                * an integral multiple of 1.5 kB (storage for 64 index entries
+                * to maintain alignment in case of lost buffers).
+                *
+                */
+               if (i == CX18_ENC_STREAM_TYPE_YUV) {
+                       cx->stream_buf_size[i] *= 1024;
+                       cx->stream_buf_size[i] -=
+                          (cx->stream_buf_size[i] % CX18_UNIT_ENC_YUV_BUFSIZE);
+
+                       if (cx->stream_buf_size[i] < CX18_UNIT_ENC_YUV_BUFSIZE)
+                               cx->stream_buf_size[i] =
+                                               CX18_UNIT_ENC_YUV_BUFSIZE;
+               } else if (i == CX18_ENC_STREAM_TYPE_IDX) {
+                       cx->stream_buf_size[i] *= 1024;
+                       cx->stream_buf_size[i] -=
+                          (cx->stream_buf_size[i] % CX18_UNIT_ENC_IDX_BUFSIZE);
+
+                       if (cx->stream_buf_size[i] < CX18_UNIT_ENC_IDX_BUFSIZE)
+                               cx->stream_buf_size[i] =
+                                               CX18_UNIT_ENC_IDX_BUFSIZE;
                }
-               if (cx->stream_buffers[i] < 0) {
-                       cx->stream_buffers[i] = cx->options.megabytes[i] * 1024
-                                               / cx->stream_buf_size[i];
+               /*
+                * YUV and IDX are special cases where the stream_buf_size is
+                * now in bytes.
+                * VBI is a special case where the stream_buf_size is fixed
+                * and already in bytes
+                */
+               if (i == CX18_ENC_STREAM_TYPE_VBI ||
+                   i == CX18_ENC_STREAM_TYPE_YUV ||
+                   i == CX18_ENC_STREAM_TYPE_IDX) {
+                       if (cx->stream_buffers[i] < 0) {
+                               cx->stream_buffers[i] =
+                                       cx->options.megabytes[i] * 1024 * 1024
+                                       / cx->stream_buf_size[i];
+                       } else {
+                               /* N.B. This might round down to 0 */
+                               cx->options.megabytes[i] =
+                                       cx->stream_buffers[i]
+                                       * cx->stream_buf_size[i]/(1024 * 1024);
+                       }
                } else {
-                       /* N.B. This might round down to 0 */
-                       cx->options.megabytes[i] =
-                         cx->stream_buffers[i] * cx->stream_buf_size[i] / 1024;
+                       /* All other streams have stream_buf_size in kB here */
+                       if (cx->stream_buffers[i] < 0) {
+                               cx->stream_buffers[i] =
+                                               cx->options.megabytes[i] * 1024
+                                               / cx->stream_buf_size[i];
+                       } else {
+                               /* N.B. This might round down to 0 */
+                               cx->options.megabytes[i] =
+                                               cx->stream_buffers[i]
+                                               * cx->stream_buf_size[i] / 1024;
+                       }
+                       /* convert from kB to bytes */
+                       cx->stream_buf_size[i] *= 1024;
                }
-               cx->stream_buf_size[i] *= 1024; /* convert from kB to bytes */
+               CX18_DEBUG_INFO("Stream type %d options: %d MB, %d buffers, "
+                               "%d bytes\n", i, cx->options.megabytes[i],
+                               cx->stream_buffers[i], cx->stream_buf_size[i]);
        }
 
-       cx->options.cardtype = cardtype[cx->num];
-       cx->options.tuner = tuner[cx->num];
-       cx->options.radio = radio[cx->num];
+       cx->options.cardtype = cardtype[cx->instance];
+       cx->options.tuner = tuner[cx->instance];
+       cx->options.radio = radio[cx->instance];
 
        cx->std = cx18_parse_std(cx);
        if (cx->options.cardtype == -1) {
@@ -545,37 +652,67 @@ done:
        cx->card_i2c = cx->card->i2c;
 }
 
+static int __devinit cx18_create_in_workq(struct cx18 *cx)
+{
+       snprintf(cx->in_workq_name, sizeof(cx->in_workq_name), "%s-in",
+                cx->v4l2_dev.name);
+       cx->in_work_queue = create_singlethread_workqueue(cx->in_workq_name);
+       if (cx->in_work_queue == NULL) {
+               CX18_ERR("Unable to create incoming mailbox handler thread\n");
+               return -ENOMEM;
+       }
+       return 0;
+}
+
+static int __devinit cx18_create_out_workq(struct cx18 *cx)
+{
+       snprintf(cx->out_workq_name, sizeof(cx->out_workq_name), "%s-out",
+                cx->v4l2_dev.name);
+       cx->out_work_queue = create_workqueue(cx->out_workq_name);
+       if (cx->out_work_queue == NULL) {
+               CX18_ERR("Unable to create outgoing mailbox handler threads\n");
+               return -ENOMEM;
+       }
+       return 0;
+}
+
+static void __devinit cx18_init_in_work_orders(struct cx18 *cx)
+{
+       int i;
+       for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {
+               cx->in_work_order[i].cx = cx;
+               cx->in_work_order[i].str = cx->epu_debug_str;
+               INIT_WORK(&cx->in_work_order[i].work, cx18_in_work_handler);
+       }
+}
+
 /* Precondition: the cx18 structure has been memset to 0. Only
-   the dev and num fields have been filled in.
+   the dev and instance fields have been filled in.
    No assumptions on the card type may be made here (see cx18_init_struct2
    for that).
  */
 static int __devinit cx18_init_struct1(struct cx18 *cx)
 {
-       int i;
+       int ret;
 
        cx->base_addr = pci_resource_start(cx->pci_dev, 0);
 
        mutex_init(&cx->serialize_lock);
-       mutex_init(&cx->i2c_bus_lock[0]);
-       mutex_init(&cx->i2c_bus_lock[1]);
        mutex_init(&cx->gpio_lock);
        mutex_init(&cx->epu2apu_mb_lock);
        mutex_init(&cx->epu2cpu_mb_lock);
 
-       spin_lock_init(&cx->lock);
+       ret = cx18_create_out_workq(cx);
+       if (ret)
+               return ret;
 
-       cx->work_queue = create_singlethread_workqueue(cx->name);
-       if (cx->work_queue == NULL) {
-               CX18_ERR("Unable to create work hander thread\n");
-               return -ENOMEM;
+       ret = cx18_create_in_workq(cx);
+       if (ret) {
+               destroy_workqueue(cx->out_work_queue);
+               return ret;
        }
 
-       for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++) {
-               cx->epu_work_order[i].cx = cx;
-               cx->epu_work_order[i].str = cx->epu_debug_str;
-               INIT_WORK(&cx->epu_work_order[i].work, cx18_epu_work_handler);
-       }
+       cx18_init_in_work_orders(cx);
 
        /* start counting open_id at 1 */
        cx->open_id = 1;
@@ -588,7 +725,8 @@ static int __devinit cx18_init_struct1(struct cx18 *cx)
                (cx->params.video_temporal_filter_mode << 1) |
                (cx->params.video_median_filter_type << 2);
        cx->params.port = CX2341X_PORT_MEMORY;
-       cx->params.capabilities = CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_AC3;
+       cx->params.capabilities =
+                               CX2341X_CAP_HAS_TS | CX2341X_CAP_HAS_SLICED_VBI;
        init_waitqueue_head(&cx->cap_w);
        init_waitqueue_head(&cx->mb_apu_waitq);
        init_waitqueue_head(&cx->mb_cpu_waitq);
@@ -598,49 +736,12 @@ static int __devinit cx18_init_struct1(struct cx18 *cx)
        cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
        cx->vbi.sliced_in = &cx->vbi.in.fmt.sliced;
 
-       /*
-        * The VBI line sizes depend on the pixel clock and the horiz rate
-        *
-        * (1/Fh)*(2*Fp) = Samples/line
-        *     = 4 bytes EAV + Anc data in hblank + 4 bytes SAV + active samples
-        *
-        *  Sliced VBI is sent as ancillary data during horizontal blanking
-        *  Raw VBI is sent as active video samples during vertcal blanking
-        *
-        *  We use a  BT.656 pxiel clock of 13.5 MHz and a BT.656 active line
-        *  length of 720 pixels @ 4:2:2 sampling.  Thus...
-        *
-        *  For systems that use a 15.734 kHz horizontal rate, such as
-        *  NTSC-M, PAL-M, PAL-60, and other 60 Hz/525 line systems, we have:
-        *
-        *  (1/15.734 kHz) * 2 * 13.5 MHz = 1716 samples/line =
-        *  4 bytes SAV + 268 bytes anc data + 4 bytes SAV + 1440 active samples
-        *
-        *  For systems that use a 15.625 kHz horizontal rate, such as
-        *  PAL-B/G/H, PAL-I, SECAM-L and other 50 Hz/625 line systems, we have:
-        *
-        *  (1/15.625 kHz) * 2 * 13.5 MHz = 1728 samples/line =
-        *  4 bytes SAV + 280 bytes anc data + 4 bytes SAV + 1440 active samples
-        *
-        */
-
-       /* FIXME: init these based on tuner std & modify when std changes */
-       /* CX18-AV-Core number of VBI samples output per horizontal line */
-       cx->vbi.raw_decoder_line_size = 1444;   /* 4 byte SAV + 2 * 720 */
-       cx->vbi.sliced_decoder_line_size = 272; /* 60 Hz: 268+4, 50 Hz: 280+4 */
-
-       /* CX18-AV-Core VBI samples/line possibly rounded up */
-       cx->vbi.raw_size = 1444;   /* Real max size is 1444 */
-       cx->vbi.sliced_size = 284; /* Real max size is  284 */
-
-       /*
-        * CX18-AV-Core SAV/EAV RP codes in VIP 1.x mode
-        * Task Field VerticalBlank HorizontalBlank 0 0 0 0
-        */
-       cx->vbi.raw_decoder_sav_odd_field = 0x20;     /*   V  */
-       cx->vbi.raw_decoder_sav_even_field = 0x60;    /*  FV  */
-       cx->vbi.sliced_decoder_sav_odd_field = 0xB0;  /* T VH - actually EAV */
-       cx->vbi.sliced_decoder_sav_even_field = 0xF0; /* TFVH - actually EAV */
+       /* IVTV style VBI insertion into MPEG streams */
+       INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_buf.list);
+       INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.list);
+       INIT_LIST_HEAD(&cx->vbi.sliced_mpeg_mdl.buf_list);
+       list_add(&cx->vbi.sliced_mpeg_buf.list,
+                &cx->vbi.sliced_mpeg_mdl.buf_list);
        return 0;
 }
 
@@ -669,12 +770,6 @@ static void __devinit cx18_init_struct2(struct cx18 *cx)
                i = 0;
        cx->active_input = i;
        cx->audio_input = cx->card->video_inputs[i].audio_index;
-       cx->av_state.vid_input = CX18_AV_COMPOSITE7;
-       cx->av_state.aud_input = CX18_AV_AUDIO8;
-       cx->av_state.audclk_freq = 48000;
-       cx->av_state.audmode = V4L2_TUNER_MODE_LANG1;
-       /* FIXME - 8 is NTSC value, investigate */
-       cx->av_state.vbi_line_offset = 8;
 }
 
 static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
@@ -686,15 +781,16 @@ static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
        CX18_DEBUG_INFO("Enabling pci device\n");
 
        if (pci_enable_device(pci_dev)) {
-               CX18_ERR("Can't enable device %d!\n", cx->num);
+               CX18_ERR("Can't enable device %d!\n", cx->instance);
                return -EIO;
        }
        if (pci_set_dma_mask(pci_dev, 0xffffffff)) {
-               CX18_ERR("No suitable DMA available on card %d.\n", cx->num);
+               CX18_ERR("No suitable DMA available, card %d\n", cx->instance);
                return -EIO;
        }
        if (!request_mem_region(cx->base_addr, CX18_MEM_SIZE, "cx18 encoder")) {
-               CX18_ERR("Cannot request encoder memory region on card %d.\n", cx->num);
+               CX18_ERR("Cannot request encoder memory region, card %d\n",
+                        cx->instance);
                return -EIO;
        }
 
@@ -722,54 +818,50 @@ static int cx18_setup_pci(struct cx18 *cx, struct pci_dev *pci_dev,
        return 0;
 }
 
-#ifdef MODULE
-static u32 cx18_request_module(struct cx18 *cx, u32 hw,
-               const char *name, u32 id)
-{
-       if ((hw & id) == 0)
-               return hw;
-       if (request_module("%s", name) != 0) {
-               CX18_ERR("Failed to load module %s\n", name);
-               return hw & ~id;
-       }
-       CX18_DEBUG_INFO("Loaded module %s\n", name);
-       return hw;
-}
-#endif
-
-static void cx18_load_and_init_modules(struct cx18 *cx)
+static void cx18_init_subdevs(struct cx18 *cx)
 {
        u32 hw = cx->card->hw_all;
+       u32 device;
        int i;
 
-#ifdef MODULE
-       /* load modules */
-#ifdef CONFIG_MEDIA_TUNER_MODULE
-       hw = cx18_request_module(cx, hw, "tuner", CX18_HW_TUNER);
-#endif
-#ifdef CONFIG_VIDEO_CS5345_MODULE
-       hw = cx18_request_module(cx, hw, "cs5345", CX18_HW_CS5345);
-#endif
-#endif
-
-       /* check which i2c devices are actually found */
-       for (i = 0; i < 32; i++) {
-               u32 device = 1 << i;
+       for (i = 0, device = 1; i < 32; i++, device <<= 1) {
 
                if (!(device & hw))
                        continue;
-               if (device == CX18_HW_GPIO || device == CX18_HW_TVEEPROM ||
-                   device == CX18_HW_CX23418 || device == CX18_HW_DVB) {
-                       /* These 'devices' do not use i2c probing */
+
+               switch (device) {
+               case CX18_HW_DVB:
+               case CX18_HW_TVEEPROM:
+                       /* These subordinate devices do not use probing */
                        cx->hw_flags |= device;
-                       continue;
-               }
-               cx18_i2c_register(cx, i);
-               if (cx18_i2c_hw_addr(cx, device) > 0)
+                       break;
+               case CX18_HW_418_AV:
+                       /* The A/V decoder gets probed earlier to set PLLs */
+                       /* Just note that the card uses it (i.e. has analog) */
                        cx->hw_flags |= device;
+                       break;
+               case CX18_HW_GPIO_RESET_CTRL:
+                       /*
+                        * The Reset Controller gets probed and added to
+                        * hw_flags earlier for i2c adapter/bus initialization
+                        */
+                       break;
+               case CX18_HW_GPIO_MUX:
+                       if (cx18_gpio_register(cx, device) == 0)
+                               cx->hw_flags |= device;
+                       break;
+               default:
+                       if (cx18_i2c_register(cx, i) == 0)
+                               cx->hw_flags |= device;
+                       break;
+               }
        }
 
-       hw = cx->hw_flags;
+       if (cx->hw_flags & CX18_HW_418_AV)
+               cx->sd_av = cx18_find_hw(cx, CX18_HW_418_AV);
+
+       if (cx->card->hw_muxer != 0)
+               cx->sd_extmux = cx18_find_hw(cx, cx->card->hw_muxer);
 }
 
 static int __devinit cx18_probe(struct pci_dev *pci_dev,
@@ -777,56 +869,53 @@ static int __devinit cx18_probe(struct pci_dev *pci_dev,
 {
        int retval = 0;
        int i;
-       int vbi_buf_size;
        u32 devtype;
        struct cx18 *cx;
 
-       spin_lock(&cx18_cards_lock);
-
-       /* Make sure we've got a place for this card */
-       if (cx18_cards_active == CX18_MAX_CARDS) {
-               printk(KERN_ERR "cx18:  Maximum number of cards detected (%d).\n",
-                             cx18_cards_active);
-               spin_unlock(&cx18_cards_lock);
+       /* FIXME - module parameter arrays constrain max instances */
+       i = atomic_inc_return(&cx18_instance) - 1;
+       if (i >= CX18_MAX_CARDS) {
+               printk(KERN_ERR "cx18: cannot manage card %d, driver has a "
+                      "limit of 0 - %d\n", i, CX18_MAX_CARDS - 1);
                return -ENOMEM;
        }
 
        cx = kzalloc(sizeof(struct cx18), GFP_ATOMIC);
-       if (!cx) {
-               spin_unlock(&cx18_cards_lock);
+       if (cx == NULL) {
+               printk(KERN_ERR "cx18: cannot manage card %d, out of memory\n",
+                      i);
                return -ENOMEM;
        }
-       cx18_cards[cx18_cards_active] = cx;
-       cx->num = cx18_cards_active++;
-       snprintf(cx->name, sizeof(cx->name), "cx18-%d", cx->num);
-       CX18_INFO("Initializing card #%d\n", cx->num);
-
-       spin_unlock(&cx18_cards_lock);
-
        cx->pci_dev = pci_dev;
+       cx->instance = i;
+
        retval = v4l2_device_register(&pci_dev->dev, &cx->v4l2_dev);
        if (retval) {
-               CX18_ERR("Call to v4l2_device_register() failed\n");
-               goto err;
+               printk(KERN_ERR "cx18: v4l2_device_register of card %d failed"
+                      "\n", cx->instance);
+               kfree(cx);
+               return retval;
        }
-       CX18_DEBUG_INFO("registered v4l2_device name: %s\n", cx->v4l2_dev.name);
+       snprintf(cx->v4l2_dev.name, sizeof(cx->v4l2_dev.name), "cx18-%d",
+                cx->instance);
+       CX18_INFO("Initializing card %d\n", cx->instance);
 
        cx18_process_options(cx);
        if (cx->options.cardtype == -1) {
                retval = -ENODEV;
-               goto unregister_v4l2;
-       }
-       if (cx18_init_struct1(cx)) {
-               retval = -ENOMEM;
-               goto unregister_v4l2;
+               goto err;
        }
 
+       retval = cx18_init_struct1(cx);
+       if (retval)
+               goto err;
+
        CX18_DEBUG_INFO("base addr: 0x%08x\n", cx->base_addr);
 
        /* PCI Device Setup */
        retval = cx18_setup_pci(cx, pci_dev, pci_id);
        if (retval != 0)
-               goto free_workqueue;
+               goto free_workqueues;
 
        /* map io memory */
        CX18_DEBUG_INFO("attempting ioremap at 0x%08x len 0x%08x\n",
@@ -861,6 +950,22 @@ static int __devinit cx18_probe(struct pci_dev *pci_dev,
 
        cx18_gpio_init(cx);
 
+       /* Initialize integrated A/V decoder early to set PLLs, just in case */
+       retval = cx18_av_probe(cx);
+       if (retval) {
+               CX18_ERR("Could not register A/V decoder subdevice\n");
+               goto free_map;
+       }
+
+       /* Initialize GPIO Reset Controller to do chip resets during i2c init */
+       if (cx->card->hw_all & CX18_HW_GPIO_RESET_CTRL) {
+               if (cx18_gpio_register(cx, CX18_HW_GPIO_RESET_CTRL) != 0)
+                       CX18_WARN("Could not register GPIO reset controller"
+                                 "subdevice; proceeding anyway.\n");
+               else
+                       cx->hw_flags |= CX18_HW_GPIO_RESET_CTRL;
+       }
+
        /* active i2c  */
        CX18_DEBUG_INFO("activating i2c...\n");
        retval = init_cx18_i2c(cx);
@@ -869,8 +974,6 @@ static int __devinit cx18_probe(struct pci_dev *pci_dev,
                goto free_map;
        }
 
-       CX18_DEBUG_INFO("Active card count: %d.\n", cx18_cards_active);
-
        if (cx->card->hw_all & CX18_HW_TVEEPROM) {
                /* Based on the model number the cardtype may be changed.
                   The PCI IDs are not always reliable. */
@@ -887,7 +990,8 @@ static int __devinit cx18_probe(struct pci_dev *pci_dev,
 
        /* Register IRQ */
        retval = request_irq(cx->pci_dev->irq, cx18_irq_handler,
-                            IRQF_SHARED | IRQF_DISABLED, cx->name, (void *)cx);
+                            IRQF_SHARED | IRQF_DISABLED,
+                            cx->v4l2_dev.name, (void *)cx);
        if (retval) {
                CX18_ERR("Failed to register irq %d\n", retval);
                goto free_i2c;
@@ -922,33 +1026,14 @@ static int __devinit cx18_probe(struct pci_dev *pci_dev,
           initialization. */
        cx18_init_struct2(cx);
 
-       cx18_load_and_init_modules(cx);
+       cx18_init_subdevs(cx);
 
-       if (cx->std & V4L2_STD_525_60) {
+       if (cx->std & V4L2_STD_525_60)
                cx->is_60hz = 1;
-               cx->is_out_60hz = 1;
-       } else {
+       else
                cx->is_50hz = 1;
-               cx->is_out_50hz = 1;
-       }
-       cx->params.video_gop_size = cx->is_60hz ? 15 : 12;
-
-       /*
-        * FIXME: setting the buffer size based on the tuner standard is
-        * suboptimal, as the CVBS and SVideo inputs could use a different std
-        * and the buffer could end up being too small in that case.
-        */
-       vbi_buf_size = cx->vbi.raw_size * (cx->is_60hz ? 24 : 36) / 2;
-       cx->stream_buf_size[CX18_ENC_STREAM_TYPE_VBI] = vbi_buf_size;
 
-       if (cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] < 0)
-               cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] =
-                  cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] * 1024 * 1024
-                  / vbi_buf_size;
-       else
-               cx->options.megabytes[CX18_ENC_STREAM_TYPE_VBI] =
-                    cx->stream_buffers[CX18_ENC_STREAM_TYPE_VBI] * vbi_buf_size
-                    / (1024 * 1024);
+       cx->params.video_gop_size = cx->is_60hz ? 15 : 12;
 
        if (cx->options.radio > 0)
                cx->v4l2_cap |= V4L2_CAP_RADIO;
@@ -961,7 +1046,7 @@ static int __devinit cx18_probe(struct pci_dev *pci_dev,
                setup.mode_mask = T_ANALOG_TV;  /* matches TV tuners */
                setup.tuner_callback = (setup.type == TUNER_XC2028) ?
                        cx18_reset_tuner_gpio : NULL;
-               cx18_call_i2c_clients(cx, TUNER_SET_TYPE_ADDR, &setup);
+               cx18_call_all(cx, tuner, s_type_addr, &setup);
                if (setup.type == TUNER_XC2028) {
                        static struct xc2028_ctrl ctrl = {
                                .fname = XC2028_DEFAULT_FIRMWARE,
@@ -971,7 +1056,7 @@ static int __devinit cx18_probe(struct pci_dev *pci_dev,
                                .tuner = cx->options.tuner,
                                .priv = &ctrl,
                        };
-                       cx18_call_i2c_clients(cx, TUNER_SET_CONFIG, &cfg);
+                       cx18_call_all(cx, tuner, s_config, &cfg);
                }
        }
 
@@ -990,7 +1075,10 @@ static int __devinit cx18_probe(struct pci_dev *pci_dev,
                goto free_streams;
        }
 
-       CX18_INFO("Initialized card #%d: %s\n", cx->num, cx->card_name);
+       CX18_INFO("Initialized card: %s\n", cx->card_name);
+
+       /* Load cx18 submodules (cx18-alsa) */
+       request_modules(cx);
 
        return 0;
 
@@ -1004,20 +1092,16 @@ free_map:
        cx18_iounmap(cx);
 free_mem:
        release_mem_region(cx->base_addr, CX18_MEM_SIZE);
-free_workqueue:
-       destroy_workqueue(cx->work_queue);
-unregister_v4l2:
-       v4l2_device_unregister(&cx->v4l2_dev);
+free_workqueues:
+       destroy_workqueue(cx->in_work_queue);
+       destroy_workqueue(cx->out_work_queue);
 err:
        if (retval == 0)
                retval = -ENODEV;
        CX18_ERR("Error %d on initialization\n", retval);
 
-       i = cx->num;
-       spin_lock(&cx18_cards_lock);
-       kfree(cx18_cards[i]);
-       cx18_cards[i] = NULL;
-       spin_unlock(&cx18_cards_lock);
+       v4l2_device_unregister(&cx->v4l2_dev);
+       kfree(cx);
        return retval;
 }
 
@@ -1093,6 +1177,9 @@ int cx18_init_on_first_open(struct cx18 *cx)
        cx18_vapi(cx, CX18_APU_RESETAI, 0);
        cx18_vapi(cx, CX18_APU_STOP, 1, CX18_APU_ENCODING_METHOD_MPEG);
 
+       /* Init the A/V decoder, if it hasn't been already */
+       v4l2_subdev_call(cx->sd_av, core, load_fw);
+
        vf.tuner = 0;
        vf.type = V4L2_TUNER_ANALOG_TV;
        vf.frequency = 6400; /* the tuner 'baseline' frequency */
@@ -1116,34 +1203,48 @@ int cx18_init_on_first_open(struct cx18 *cx)
        return 0;
 }
 
-static void cx18_cancel_epu_work_orders(struct cx18 *cx)
+static void cx18_cancel_in_work_orders(struct cx18 *cx)
+{
+       int i;
+       for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++)
+               cancel_work_sync(&cx->in_work_order[i].work);
+}
+
+static void cx18_cancel_out_work_orders(struct cx18 *cx)
 {
        int i;
-       for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++)
-               cancel_work_sync(&cx->epu_work_order[i].work);
+       for (i = 0; i < CX18_MAX_STREAMS; i++)
+               if (&cx->streams[i].video_dev != NULL)
+                       cancel_work_sync(&cx->streams[i].out_work_order);
 }
 
 static void cx18_remove(struct pci_dev *pci_dev)
 {
        struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
-       struct cx18 *cx = container_of(v4l2_dev, struct cx18, v4l2_dev);
+       struct cx18 *cx = to_cx18(v4l2_dev);
+       int i;
 
-       CX18_DEBUG_INFO("Removing Card #%d\n", cx->num);
+       CX18_DEBUG_INFO("Removing Card\n");
 
        /* Stop all captures */
        CX18_DEBUG_INFO("Stopping all streams\n");
        if (atomic_read(&cx->tot_capturing) > 0)
                cx18_stop_all_captures(cx);
 
-       /* Interrupts */
+       /* Stop interrupts that cause incoming work to be queued */
        cx18_sw1_irq_disable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
+
+       /* Incoming work can cause outgoing work, so clean up incoming first */
+       cx18_cancel_in_work_orders(cx);
+       cx18_cancel_out_work_orders(cx);
+
+       /* Stop ack interrupts that may have been needed for work to finish */
        cx18_sw2_irq_disable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
 
        cx18_halt_firmware(cx);
 
-       cx18_cancel_epu_work_orders(cx);
-
-       destroy_workqueue(cx->work_queue);
+       destroy_workqueue(cx->in_work_queue);
+       destroy_workqueue(cx->out_work_queue);
 
        cx18_streams_cleanup(cx, 1);
 
@@ -1157,11 +1258,17 @@ static void cx18_remove(struct pci_dev *pci_dev)
 
        pci_disable_device(cx->pci_dev);
 
-       v4l2_device_unregister(v4l2_dev);
+       if (cx->vbi.sliced_mpeg_data[0] != NULL)
+               for (i = 0; i < CX18_VBI_FRAMES; i++)
+                       kfree(cx->vbi.sliced_mpeg_data[i]);
 
-       CX18_INFO("Removed %s, card #%d\n", cx->card_name, cx->num);
+       CX18_INFO("Removed %s\n", cx->card_name);
+
+       v4l2_device_unregister(v4l2_dev);
+       kfree(cx);
 }
 
+
 /* define a pci_driver for card detection */
 static struct pci_driver cx18_pci_driver = {
       .name =     "cx18",
@@ -1170,11 +1277,10 @@ static struct pci_driver cx18_pci_driver = {
       .remove =   cx18_remove,
 };
 
-static int module_start(void)
+static int __init module_start(void)
 {
-       printk(KERN_INFO "cx18:  Start initialization, version %s\n", CX18_VERSION);
-
-       memset(cx18_cards, 0, sizeof(cx18_cards));
+       printk(KERN_INFO "cx18:  Start initialization, version %s\n",
+              CX18_VERSION);
 
        /* Validate parameters */
        if (cx18_first_minor < 0 || cx18_first_minor >= CX18_MAX_CARDS) {
@@ -1196,18 +1302,9 @@ static int module_start(void)
        return 0;
 }
 
-static void module_cleanup(void)
+static void __exit module_cleanup(void)
 {
-       int i;
-
        pci_unregister_driver(&cx18_pci_driver);
-
-       for (i = 0; i < cx18_cards_active; i++) {
-               if (cx18_cards[i] == NULL)
-                       continue;
-               kfree(cx18_cards[i]);
-       }
-
 }
 
 module_init(module_start);