V4L/DVB: cx18, cx23885, v4l2 doc, MAINTAINERS: Update Andy Walls' email address
[safe/jmp/linux-2.6] / drivers / media / video / cx18 / cx18-streams.c
index 4d5b446..9045f1e 100644 (file)
@@ -4,6 +4,7 @@
  *  Derived from ivtv-streams.c
  *
  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
+ *  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
@@ -22,6 +23,7 @@
  */
 
 #include "cx18-driver.h"
+#include "cx18-io.h"
 #include "cx18-fileops.h"
 #include "cx18-mailbox.h"
 #include "cx18-i2c.h"
 #include "cx18-streams.h"
 #include "cx18-cards.h"
 #include "cx18-scb.h"
-#include "cx18-av-core.h"
 #include "cx18-dvb.h"
 
 #define CX18_DSP0_INTERRUPT_MASK       0xd0004C
 
-static struct file_operations cx18_v4l2_enc_fops = {
+static struct v4l2_file_operations cx18_v4l2_enc_fops = {
        .owner = THIS_MODULE,
        .read = cx18_v4l2_read,
        .open = cx18_v4l2_open,
        /* FIXME change to video_ioctl2 if serialization lock can be removed */
        .ioctl = cx18_v4l2_ioctl,
-       .compat_ioctl = v4l_compat_ioctl32,
        .release = cx18_v4l2_close,
        .poll = cx18_v4l2_enc_poll,
 };
@@ -56,64 +56,55 @@ static struct file_operations cx18_v4l2_enc_fops = {
 static struct {
        const char *name;
        int vfl_type;
-       int minor_offset;
+       int num_offset;
        int dma;
        enum v4l2_buf_type buf_type;
-       struct file_operations *fops;
 } cx18_stream_info[] = {
        {       /* CX18_ENC_STREAM_TYPE_MPG */
                "encoder MPEG",
                VFL_TYPE_GRABBER, 0,
                PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
-               &cx18_v4l2_enc_fops
        },
        {       /* CX18_ENC_STREAM_TYPE_TS */
                "TS",
                VFL_TYPE_GRABBER, -1,
                PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
-               &cx18_v4l2_enc_fops
        },
        {       /* CX18_ENC_STREAM_TYPE_YUV */
                "encoder YUV",
                VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
                PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
-               &cx18_v4l2_enc_fops
        },
        {       /* CX18_ENC_STREAM_TYPE_VBI */
                "encoder VBI",
                VFL_TYPE_VBI, 0,
                PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
-               &cx18_v4l2_enc_fops
        },
        {       /* CX18_ENC_STREAM_TYPE_PCM */
                "encoder PCM audio",
                VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
                PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
-               &cx18_v4l2_enc_fops
        },
        {       /* CX18_ENC_STREAM_TYPE_IDX */
                "encoder IDX",
                VFL_TYPE_GRABBER, -1,
                PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
-               &cx18_v4l2_enc_fops
        },
        {       /* CX18_ENC_STREAM_TYPE_RAD */
                "encoder radio",
                VFL_TYPE_RADIO, 0,
                PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
-               &cx18_v4l2_enc_fops
        },
 };
 
 static void cx18_stream_init(struct cx18 *cx, int type)
 {
        struct cx18_stream *s = &cx->streams[type];
-       struct video_device *dev = s->v4l2dev;
-       u32 max_size = cx->options.megabytes[type] * 1024 * 1024;
+       struct video_device *video_dev = s->video_dev;
 
-       /* we need to keep v4l2dev, so restore it afterwards */
+       /* we need to keep video_dev, so restore it afterwards */
        memset(s, 0, sizeof(*s));
-       s->v4l2dev = dev;
+       s->video_dev = video_dev;
 
        /* initialize cx18_stream fields */
        s->cx = cx;
@@ -122,34 +113,37 @@ static void cx18_stream_init(struct cx18 *cx, int type)
        s->handle = CX18_INVALID_TASK_HANDLE;
 
        s->dma = cx18_stream_info[type].dma;
+       s->buffers = cx->stream_buffers[type];
        s->buf_size = cx->stream_buf_size[type];
-       if (s->buf_size)
-               s->buffers = max_size / s->buf_size;
-       if (s->buffers > 63) {
-               /* Each stream has a maximum of 63 buffers,
-                  ensure we do not exceed that. */
-               s->buffers = 63;
-               s->buf_size = (max_size / s->buffers) & ~0xfff;
-       }
-       spin_lock_init(&s->qlock);
+       INIT_LIST_HEAD(&s->buf_pool);
+       s->bufs_per_mdl = 1;
+       s->mdl_size = s->buf_size * s->bufs_per_mdl;
+
        init_waitqueue_head(&s->waitq);
        s->id = -1;
+       spin_lock_init(&s->q_free.lock);
        cx18_queue_init(&s->q_free);
+       spin_lock_init(&s->q_busy.lock);
+       cx18_queue_init(&s->q_busy);
+       spin_lock_init(&s->q_full.lock);
        cx18_queue_init(&s->q_full);
-       cx18_queue_init(&s->q_io);
+       spin_lock_init(&s->q_idle.lock);
+       cx18_queue_init(&s->q_idle);
+
+       INIT_WORK(&s->out_work_order, cx18_out_work_handler);
 }
 
 static int cx18_prep_dev(struct cx18 *cx, int type)
 {
        struct cx18_stream *s = &cx->streams[type];
        u32 cap = cx->v4l2_cap;
-       int minor_offset = cx18_stream_info[type].minor_offset;
-       int minor;
+       int num_offset = cx18_stream_info[type].num_offset;
+       int num = cx->instance + cx18_first_minor + num_offset;
 
-       /* These four fields are always initialized. If v4l2dev == NULL, then
+       /* These four fields are always initialized. If video_dev == NULL, then
           this stream is not in use. In that case no other fields but these
           four can be used. */
-       s->v4l2dev = NULL;
+       s->video_dev = NULL;
        s->cx = cx;
        s->type = type;
        s->name = cx18_stream_info[type].name;
@@ -163,55 +157,54 @@ static int cx18_prep_dev(struct cx18 *cx, int type)
            !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
                return 0;
 
-       /* card number + user defined offset + device offset */
-       minor = cx->num + cx18_first_minor + minor_offset;
-
        /* User explicitly selected 0 buffers for these streams, so don't
           create them. */
        if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
-           cx->options.megabytes[type] == 0) {
+           cx->stream_buffers[type] == 0) {
                CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
                return 0;
        }
 
        cx18_stream_init(cx, type);
 
-       if (minor_offset == -1)
+       if (num_offset == -1)
                return 0;
 
        /* allocate and initialize the v4l2 video device structure */
-       s->v4l2dev = video_device_alloc();
-       if (s->v4l2dev == NULL) {
+       s->video_dev = video_device_alloc();
+       if (s->video_dev == NULL) {
                CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
                                s->name);
                return -ENOMEM;
        }
 
-       snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
-                       cx->num);
+       snprintf(s->video_dev->name, sizeof(s->video_dev->name), "%s %s",
+                cx->v4l2_dev.name, s->name);
 
-       s->v4l2dev->minor = minor;
-       s->v4l2dev->parent = &cx->dev->dev;
-       s->v4l2dev->fops = cx18_stream_info[type].fops;
-       s->v4l2dev->release = video_device_release;
-       s->v4l2dev->tvnorms = V4L2_STD_ALL;
-       cx18_set_funcs(s->v4l2dev);
+       s->video_dev->num = num;
+       s->video_dev->v4l2_dev = &cx->v4l2_dev;
+       s->video_dev->fops = &cx18_v4l2_enc_fops;
+       s->video_dev->release = video_device_release;
+       s->video_dev->tvnorms = V4L2_STD_ALL;
+       cx18_set_funcs(s->video_dev);
        return 0;
 }
 
 /* Initialize v4l2 variables and register v4l2 devices */
 int cx18_streams_setup(struct cx18 *cx)
 {
-       int type;
+       int type, ret;
 
        /* Setup V4L2 Devices */
        for (type = 0; type < CX18_MAX_STREAMS; type++) {
                /* Prepare device */
-               if (cx18_prep_dev(cx, type))
+               ret = cx18_prep_dev(cx, type);
+               if (ret < 0)
                        break;
 
                /* Allocate Stream */
-               if (cx18_stream_alloc(&cx->streams[type]))
+               ret = cx18_stream_alloc(&cx->streams[type]);
+               if (ret < 0)
                        break;
        }
        if (type == CX18_MAX_STREAMS)
@@ -219,14 +212,15 @@ int cx18_streams_setup(struct cx18 *cx)
 
        /* One or more streams could not be initialized. Clean 'em all up. */
        cx18_streams_cleanup(cx, 0);
-       return -ENOMEM;
+       return ret;
 }
 
 static int cx18_reg_dev(struct cx18 *cx, int type)
 {
        struct cx18_stream *s = &cx->streams[type];
        int vfl_type = cx18_stream_info[type].vfl_type;
-       int minor;
+       const char *name;
+       int num, ret;
 
        /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
         * We need a VFL_TYPE_TS defined.
@@ -235,47 +229,60 @@ static int cx18_reg_dev(struct cx18 *cx, int type)
                /* just return if no DVB is supported */
                if ((cx->card->hw_all & CX18_HW_DVB) == 0)
                        return 0;
-               if (cx18_dvb_register(s) < 0) {
+               ret = cx18_dvb_register(s);
+               if (ret < 0) {
                        CX18_ERR("DVB failed to register\n");
-                       return -EINVAL;
+                       return ret;
                }
        }
 
-       if (s->v4l2dev == NULL)
+       if (s->video_dev == NULL)
                return 0;
 
-       minor = s->v4l2dev->minor;
+       num = s->video_dev->num;
+       /* card number + user defined offset + device offset */
+       if (type != CX18_ENC_STREAM_TYPE_MPG) {
+               struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
+
+               if (s_mpg->video_dev)
+                       num = s_mpg->video_dev->num
+                           + cx18_stream_info[type].num_offset;
+       }
+       video_set_drvdata(s->video_dev, s);
 
        /* Register device. First try the desired minor, then any free one. */
-       if (video_register_device(s->v4l2dev, vfl_type, minor) &&
-                       video_register_device(s->v4l2dev, vfl_type, -1)) {
-               CX18_ERR("Couldn't register v4l2 device for %s minor %d\n",
-                       s->name, minor);
-               video_device_release(s->v4l2dev);
-               s->v4l2dev = NULL;
-               return -ENOMEM;
+       ret = video_register_device_no_warn(s->video_dev, vfl_type, num);
+       if (ret < 0) {
+               CX18_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
+                       s->name, num);
+               video_device_release(s->video_dev);
+               s->video_dev = NULL;
+               return ret;
        }
-       minor = s->v4l2dev->minor;
+
+       name = video_device_node_name(s->video_dev);
 
        switch (vfl_type) {
        case VFL_TYPE_GRABBER:
-               CX18_INFO("Registered device video%d for %s (%d MB)\n",
-                       minor, s->name, cx->options.megabytes[type]);
+               CX18_INFO("Registered device %s for %s (%d x %d.%02d kB)\n",
+                         name, s->name, cx->stream_buffers[type],
+                         cx->stream_buf_size[type] / 1024,
+                         (cx->stream_buf_size[type] * 100 / 1024) % 100);
                break;
 
        case VFL_TYPE_RADIO:
-               CX18_INFO("Registered device radio%d for %s\n",
-                       minor - MINOR_VFL_TYPE_RADIO_MIN, s->name);
+               CX18_INFO("Registered device %s for %s\n", name, s->name);
                break;
 
        case VFL_TYPE_VBI:
-               if (cx->options.megabytes[type])
-                       CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
-                               minor - MINOR_VFL_TYPE_VBI_MIN,
-                               s->name, cx->options.megabytes[type]);
+               if (cx->stream_buffers[type])
+                       CX18_INFO("Registered device %s for %s "
+                                 "(%d x %d bytes)\n",
+                                 name, s->name, cx->stream_buffers[type],
+                                 cx->stream_buf_size[type]);
                else
-                       CX18_INFO("Registered device vbi%d for %s\n",
-                               minor - MINOR_VFL_TYPE_VBI_MIN, s->name);
+                       CX18_INFO("Registered device %s for %s\n",
+                               name, s->name);
                break;
        }
 
@@ -286,18 +293,22 @@ static int cx18_reg_dev(struct cx18 *cx, int type)
 int cx18_streams_register(struct cx18 *cx)
 {
        int type;
-       int err = 0;
+       int err;
+       int ret = 0;
 
        /* Register V4L2 devices */
-       for (type = 0; type < CX18_MAX_STREAMS; type++)
-               err |= cx18_reg_dev(cx, type);
+       for (type = 0; type < CX18_MAX_STREAMS; type++) {
+               err = cx18_reg_dev(cx, type);
+               if (err && ret == 0)
+                       ret = err;
+       }
 
-       if (err == 0)
+       if (ret == 0)
                return 0;
 
        /* One or more streams could not be initialized. Clean 'em all up. */
        cx18_streams_cleanup(cx, 1);
-       return -ENOMEM;
+       return ret;
 }
 
 /* Unregister v4l2 devices */
@@ -308,14 +319,30 @@ void cx18_streams_cleanup(struct cx18 *cx, int unregister)
 
        /* Teardown all streams */
        for (type = 0; type < CX18_MAX_STREAMS; type++) {
-               if (cx->streams[type].dvb.enabled) {
-                       cx18_dvb_unregister(&cx->streams[type]);
-                       cx->streams[type].dvb.enabled = false;
+
+               /* No struct video_device, but can have buffers allocated */
+               if (type == CX18_ENC_STREAM_TYPE_TS) {
+                       if (cx->streams[type].dvb.enabled) {
+                               cx18_dvb_unregister(&cx->streams[type]);
+                               cx->streams[type].dvb.enabled = false;
+                               cx18_stream_free(&cx->streams[type]);
+                       }
+                       continue;
                }
 
-               vdev = cx->streams[type].v4l2dev;
+               /* No struct video_device, but can have buffers allocated */
+               if (type == CX18_ENC_STREAM_TYPE_IDX) {
+                       if (cx->stream_buffers[type] != 0) {
+                               cx->stream_buffers[type] = 0;
+                               cx18_stream_free(&cx->streams[type]);
+                       }
+                       continue;
+               }
+
+               /* If struct video_device exists, can have buffers allocated */
+               vdev = cx->streams[type].video_dev;
 
-               cx->streams[type].v4l2dev = NULL;
+               cx->streams[type].video_dev = NULL;
                if (vdev == NULL)
                        continue;
 
@@ -332,7 +359,7 @@ void cx18_streams_cleanup(struct cx18 *cx, int unregister)
 static void cx18_vbi_setup(struct cx18_stream *s)
 {
        struct cx18 *cx = s->cx;
-       int raw = cx->vbi.sliced_in->service_set == 0;
+       int raw = cx18_raw_vbi(cx);
        u32 data[CX2341X_MBOX_MAX_DATA];
        int lines;
 
@@ -347,57 +374,226 @@ static void cx18_vbi_setup(struct cx18_stream *s)
        }
 
        /* setup VBI registers */
-       cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
-
-       /* determine number of lines and total number of VBI bytes.
-          A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
-          The '- 1' byte is probably an unused U or V byte. Or something...
-          A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
-          header, 42 data bytes + checksum (to be confirmed) */
+       if (raw)
+               v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &cx->vbi.in.fmt.vbi);
+       else
+               v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &cx->vbi.in.fmt.sliced);
+
+       /*
+        * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
+        * VBI when the first analog capture channel starts, as once it starts
+        * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
+        * (i.e. for the VBI capture channels).  We also send it for each
+        * analog capture channel anyway just to make sure we get the proper
+        * behavior
+        */
        if (raw) {
                lines = cx->vbi.count * 2;
        } else {
-               lines = cx->is_60hz ? 24 : 38;
-               if (cx->is_60hz)
-                       lines += 2;
+               /*
+                * For 525/60 systems, according to the VIP 2 & BT.656 std:
+                * The EAV RP code's Field bit toggles on line 4, a few lines
+                * after the Vertcal Blank bit has already toggled.
+                * Tell the encoder to capture 21-4+1=18 lines per field,
+                * since we want lines 10 through 21.
+                *
+                * For 625/50 systems, according to the VIP 2 & BT.656 std:
+                * The EAV RP code's Field bit toggles on line 1, a few lines
+                * after the Vertcal Blank bit has already toggled.
+                * (We've actually set the digitizer so that the Field bit
+                * toggles on line 2.) Tell the encoder to capture 23-2+1=22
+                * lines per field, since we want lines 6 through 23.
+                */
+               lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
        }
 
-       cx->vbi.enc_size = lines *
-               (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
-
        data[0] = s->handle;
        /* Lines per field */
        data[1] = (lines / 2) | ((lines / 2) << 16);
        /* bytes per line */
-       data[2] = (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
+       data[2] = (raw ? vbi_active_samples
+                      : (cx->is_60hz ? vbi_hblank_samples_60Hz
+                                     : vbi_hblank_samples_50Hz));
        /* Every X number of frames a VBI interrupt arrives
           (frames as in 25 or 30 fps) */
        data[3] = 1;
-       /* Setup VBI for the cx25840 digitizer */
+       /*
+        * Set the SAV/EAV RP codes to look for as start/stop points
+        * when in VIP-1.1 mode
+        */
        if (raw) {
+               /*
+                * Start codes for beginning of "active" line in vertical blank
+                * 0x20 (               VerticalBlank                )
+                * 0x60 (     EvenField VerticalBlank                )
+                */
                data[4] = 0x20602060;
-               data[5] = 0x30703070;
+               /*
+                * End codes for end of "active" raw lines and regular lines
+                * 0x30 (               VerticalBlank HorizontalBlank)
+                * 0x70 (     EvenField VerticalBlank HorizontalBlank)
+                * 0x90 (Task                         HorizontalBlank)
+                * 0xd0 (Task EvenField               HorizontalBlank)
+                */
+               data[5] = 0x307090d0;
        } else {
+               /*
+                * End codes for active video, we want data in the hblank region
+                * 0xb0 (Task         0 VerticalBlank HorizontalBlank)
+                * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
+                *
+                * Since the V bit is only allowed to toggle in the EAV RP code,
+                * just before the first active region line, these two
+                * are problematic:
+                * 0x90 (Task                         HorizontalBlank)
+                * 0xd0 (Task EvenField               HorizontalBlank)
+                *
+                * We have set the digitzer such that we don't have to worry
+                * about these problem codes.
+                */
                data[4] = 0xB0F0B0F0;
+               /*
+                * Start codes for beginning of active line in vertical blank
+                * 0xa0 (Task           VerticalBlank                )
+                * 0xe0 (Task EvenField VerticalBlank                )
+                */
                data[5] = 0xA0E0A0E0;
        }
 
        CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
                        data[0], data[1], data[2], data[3], data[4], data[5]);
 
-       if (s->type == CX18_ENC_STREAM_TYPE_VBI)
-               cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
+       cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
+}
+
+void cx18_stream_rotate_idx_mdls(struct cx18 *cx)
+{
+       struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
+       struct cx18_mdl *mdl;
+
+       if (!cx18_stream_enabled(s))
+               return;
+
+       /* Return if the firmware is not running low on MDLs */
+       if ((atomic_read(&s->q_free.depth) + atomic_read(&s->q_busy.depth)) >=
+                                           CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN)
+               return;
+
+       /* Return if there are no MDLs to rotate back to the firmware */
+       if (atomic_read(&s->q_full.depth) < 2)
+               return;
+
+       /*
+        * Take the oldest IDX MDL still holding data, and discard its index
+        * entries by scheduling the MDL to go back to the firmware
+        */
+       mdl = cx18_dequeue(s, &s->q_full);
+       if (mdl != NULL)
+               cx18_enqueue(s, mdl, &s->q_free);
+}
+
+static
+struct cx18_queue *_cx18_stream_put_mdl_fw(struct cx18_stream *s,
+                                          struct cx18_mdl *mdl)
+{
+       struct cx18 *cx = s->cx;
+       struct cx18_queue *q;
+
+       /* Don't give it to the firmware, if we're not running a capture */
+       if (s->handle == CX18_INVALID_TASK_HANDLE ||
+           test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
+           !test_bit(CX18_F_S_STREAMING, &s->s_flags))
+               return cx18_enqueue(s, mdl, &s->q_free);
+
+       q = cx18_enqueue(s, mdl, &s->q_busy);
+       if (q != &s->q_busy)
+               return q; /* The firmware has the max MDLs it can handle */
+
+       cx18_mdl_sync_for_device(s, mdl);
+       cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
+                 (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem,
+                 s->bufs_per_mdl, mdl->id, s->mdl_size);
+       return q;
+}
+
+static
+void _cx18_stream_load_fw_queue(struct cx18_stream *s)
+{
+       struct cx18_queue *q;
+       struct cx18_mdl *mdl;
+
+       if (atomic_read(&s->q_free.depth) == 0 ||
+           atomic_read(&s->q_busy.depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
+               return;
+
+       /* Move from q_free to q_busy notifying the firmware, until the limit */
+       do {
+               mdl = cx18_dequeue(s, &s->q_free);
+               if (mdl == NULL)
+                       break;
+               q = _cx18_stream_put_mdl_fw(s, mdl);
+       } while (atomic_read(&s->q_busy.depth) < CX18_MAX_FW_MDLS_PER_STREAM
+                && q == &s->q_busy);
+}
+
+void cx18_out_work_handler(struct work_struct *work)
+{
+       struct cx18_stream *s =
+                        container_of(work, struct cx18_stream, out_work_order);
+
+       _cx18_stream_load_fw_queue(s);
+}
+
+static void cx18_stream_configure_mdls(struct cx18_stream *s)
+{
+       cx18_unload_queues(s);
+
+       switch (s->type) {
+       case CX18_ENC_STREAM_TYPE_YUV:
+               /*
+                * Height should be a multiple of 32 lines.
+                * Set the MDL size to the exact size needed for one frame.
+                * Use enough buffers per MDL to cover the MDL size
+                */
+               s->mdl_size = 720 * s->cx->params.height * 3 / 2;
+               s->bufs_per_mdl = s->mdl_size / s->buf_size;
+               if (s->mdl_size % s->buf_size)
+                       s->bufs_per_mdl++;
+               break;
+       case CX18_ENC_STREAM_TYPE_VBI:
+               s->bufs_per_mdl = 1;
+               if  (cx18_raw_vbi(s->cx)) {
+                       s->mdl_size = (s->cx->is_60hz ? 12 : 18)
+                                                      * 2 * vbi_active_samples;
+               } else {
+                       /*
+                        * See comment in cx18_vbi_setup() below about the
+                        * extra lines we capture in sliced VBI mode due to
+                        * the lines on which EAV RP codes toggle.
+                       */
+                       s->mdl_size = s->cx->is_60hz
+                                  ? (21 - 4 + 1) * 2 * vbi_hblank_samples_60Hz
+                                  : (23 - 2 + 1) * 2 * vbi_hblank_samples_50Hz;
+               }
+               break;
+       default:
+               s->bufs_per_mdl = 1;
+               s->mdl_size = s->buf_size * s->bufs_per_mdl;
+               break;
+       }
+
+       cx18_load_queues(s);
 }
 
 int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
 {
        u32 data[MAX_MB_ARGUMENTS];
        struct cx18 *cx = s->cx;
-       struct list_head *p;
-       int ts = 0;
        int captype = 0;
+       struct cx18_api_func_private priv;
+       struct cx18_stream *s_idx;
 
-       if (s->v4l2dev == NULL && s->dvb.enabled == 0)
+       if (!cx18_stream_enabled(s))
                return -EINVAL;
 
        CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
@@ -411,9 +607,11 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
                cx->search_pack_header = 0;
                break;
 
+       case CX18_ENC_STREAM_TYPE_IDX:
+               captype = CAPTURE_CHANNEL_TYPE_INDEX;
+               break;
        case CX18_ENC_STREAM_TYPE_TS:
                captype = CAPTURE_CHANNEL_TYPE_TS;
-               ts = 1;
                break;
        case CX18_ENC_STREAM_TYPE_YUV:
                captype = CAPTURE_CHANNEL_TYPE_YUV;
@@ -422,8 +620,16 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
                captype = CAPTURE_CHANNEL_TYPE_PCM;
                break;
        case CX18_ENC_STREAM_TYPE_VBI:
-               captype = cx->vbi.sliced_in->service_set ?
-                   CAPTURE_CHANNEL_TYPE_SLICED_VBI : CAPTURE_CHANNEL_TYPE_VBI;
+#ifdef CX18_ENCODER_PARSES_SLICED
+               captype = cx18_raw_vbi(cx) ?
+                    CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
+#else
+               /*
+                * Currently we set things up so that Sliced VBI from the
+                * digitizer is handled as Raw VBI by the encoder
+                */
+               captype = CAPTURE_CHANNEL_TYPE_VBI;
+#endif
                cx->vbi.frame = 0;
                cx->vbi.inserted_frame = 0;
                memset(cx->vbi.sliced_mpeg_size,
@@ -433,10 +639,6 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
                return -EINVAL;
        }
 
-       /* mute/unmute video */
-       cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
-                 s->handle, !!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags));
-
        /* Clear Streamoff flags in case left from last capture */
        clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
 
@@ -444,67 +646,109 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
        s->handle = data[0];
        cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
 
-       if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
-               /* Stuff from Windows, we don't know what it is */
+       /*
+        * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
+        * set up all the parameters, as it is not obvious which parameters the
+        * firmware shares across capture channel types and which it does not.
+        *
+        * Some of the cx18_vapi() calls below apply to only certain capture
+        * channel types.  We're hoping there's no harm in calling most of them
+        * anyway, as long as the values are all consistent.  Setting some
+        * shared parameters will have no effect once an analog capture channel
+        * has started streaming.
+        */
+       if (captype != CAPTURE_CHANNEL_TYPE_TS) {
                cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
                cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
                cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
                cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
-               cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, s->handle, 12);
 
+               /*
+                * Audio related reset according to
+                * Documentation/video4linux/cx2341x/fw-encoder-api.txt
+                */
+               if (atomic_read(&cx->ana_capturing) == 0)
+                       cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
+                                 s->handle, 12);
+
+               /*
+                * Number of lines for Field 1 & Field 2 according to
+                * Documentation/video4linux/cx2341x/fw-encoder-api.txt
+                * Field 1 is 312 for 625 line systems in BT.656
+                * Field 2 is 313 for 625 line systems in BT.656
+                */
                cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
-                              s->handle, cx->digitizer, cx->digitizer);
+                         s->handle, 312, 313);
 
-               /* Setup VBI */
                if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
                        cx18_vbi_setup(s);
 
-               /* assign program index info.
-                  Mask 7: select I/P/B, Num_req: 400 max */
-               cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
-
-               /* Setup API for Stream */
-               cx2341x_update(cx, cx18_api_func, NULL, &cx->params);
+               /*
+                * Select to receive I, P, and B frame index entries, if the
+                * index stream is enabled.  Otherwise disable index entry
+                * generation.
+                */
+               s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
+               cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 2,
+                                s->handle, cx18_stream_enabled(s_idx) ? 7 : 0);
+
+               /* Call out to the common CX2341x API setup for user controls */
+               priv.cx = cx;
+               priv.s = s;
+               cx2341x_update(&priv, cx18_api_func, NULL, &cx->params);
+
+               /*
+                * When starting a capture and we're set for radio,
+                * ensure the video is muted, despite the user control.
+                */
+               if (!cx->params.video_mute &&
+                   test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
+                       cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
+                                 (cx->params.video_mute_yuv << 8) | 1);
        }
 
        if (atomic_read(&cx->tot_capturing) == 0) {
                clear_bit(CX18_F_I_EOS, &cx->i_flags);
-               write_reg(7, CX18_DSP0_INTERRUPT_MASK);
+               cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
        }
 
        cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
                (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
                (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
 
-       list_for_each(p, &s->q_free.list) {
-               struct cx18_buffer *buf = list_entry(p, struct cx18_buffer, list);
+       /* Init all the cpu_mdls for this stream */
+       cx18_stream_configure_mdls(s);
+       _cx18_stream_load_fw_queue(s);
 
-               writel(buf->dma_handle, &cx->scb->cpu_mdl[buf->id].paddr);
-               writel(s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
-               cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
-                       (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
-                       1, buf->id, s->buf_size);
-       }
        /* begin_capture */
        if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
                CX18_DEBUG_WARN("Error starting capture!\n");
                /* Ensure we're really not capturing before releasing MDLs */
+               set_bit(CX18_F_S_STOPPING, &s->s_flags);
                if (s->type == CX18_ENC_STREAM_TYPE_MPG)
                        cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
                else
                        cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
+               clear_bit(CX18_F_S_STREAMING, &s->s_flags);
+               /* FIXME - CX18_F_S_STREAMOFF as well? */
                cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
                cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
-               /* FIXME - clean-up DSP0_INT mask, i_flags, s_flags, etc. */
+               s->handle = CX18_INVALID_TASK_HANDLE;
+               clear_bit(CX18_F_S_STOPPING, &s->s_flags);
+               if (atomic_read(&cx->tot_capturing) == 0) {
+                       set_bit(CX18_F_I_EOS, &cx->i_flags);
+                       cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
+               }
                return -EINVAL;
        }
 
        /* you're live! sit back and await interrupts :) */
-       if (!ts)
+       if (captype != CAPTURE_CHANNEL_TYPE_TS)
                atomic_inc(&cx->ana_capturing);
        atomic_inc(&cx->tot_capturing);
        return 0;
 }
+EXPORT_SYMBOL(cx18_start_v4l2_encode_stream);
 
 void cx18_stop_all_captures(struct cx18 *cx)
 {
@@ -513,7 +757,7 @@ void cx18_stop_all_captures(struct cx18 *cx)
        for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
                struct cx18_stream *s = &cx->streams[i];
 
-               if (s->v4l2dev == NULL && s->dvb.enabled == 0)
+               if (!cx18_stream_enabled(s))
                        continue;
                if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
                        cx18_stop_v4l2_encode_stream(s, 0);
@@ -525,7 +769,7 @@ int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
        struct cx18 *cx = s->cx;
        unsigned long then;
 
-       if (s->v4l2dev == NULL && s->dvb.enabled == 0)
+       if (!cx18_stream_enabled(s))
                return -EINVAL;
 
        /* This function assumes that you are allowed to stop the capture
@@ -536,6 +780,7 @@ int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
        if (atomic_read(&cx->tot_capturing) == 0)
                return 0;
 
+       set_bit(CX18_F_S_STOPPING, &s->s_flags);
        if (s->type == CX18_ENC_STREAM_TYPE_MPG)
                cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
        else
@@ -547,9 +792,6 @@ int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
                CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
        }
 
-       /* Tell the CX23418 it can't use our buffers anymore */
-       cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
-
        if (s->type != CX18_ENC_STREAM_TYPE_TS)
                atomic_dec(&cx->ana_capturing);
        atomic_dec(&cx->tot_capturing);
@@ -557,17 +799,22 @@ int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
        /* Clear capture and no-read bits */
        clear_bit(CX18_F_S_STREAMING, &s->s_flags);
 
+       /* Tell the CX23418 it can't use our buffers anymore */
+       cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
+
        cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
        s->handle = CX18_INVALID_TASK_HANDLE;
+       clear_bit(CX18_F_S_STOPPING, &s->s_flags);
 
        if (atomic_read(&cx->tot_capturing) > 0)
                return 0;
 
-       write_reg(5, CX18_DSP0_INTERRUPT_MASK);
+       cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
        wake_up(&s->waitq);
 
        return 0;
 }
+EXPORT_SYMBOL(cx18_stop_v4l2_encode_stream);
 
 u32 cx18_find_handle(struct cx18 *cx)
 {
@@ -577,8 +824,26 @@ u32 cx18_find_handle(struct cx18 *cx)
        for (i = 0; i < CX18_MAX_STREAMS; i++) {
                struct cx18_stream *s = &cx->streams[i];
 
-               if (s->v4l2dev && (s->handle != CX18_INVALID_TASK_HANDLE))
+               if (s->video_dev && (s->handle != CX18_INVALID_TASK_HANDLE))
                        return s->handle;
        }
        return CX18_INVALID_TASK_HANDLE;
 }
+
+struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
+{
+       int i;
+       struct cx18_stream *s;
+
+       if (handle == CX18_INVALID_TASK_HANDLE)
+               return NULL;
+
+       for (i = 0; i < CX18_MAX_STREAMS; i++) {
+               s = &cx->streams[i];
+               if (s->handle != handle)
+                       continue;
+               if (cx18_stream_enabled(s))
+                       return s;
+       }
+       return NULL;
+}