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 0932b76..9045f1e 100644 (file)
@@ -4,7 +4,7 @@
  *  Derived from ivtv-streams.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
@@ -115,13 +115,22 @@ static void cx18_stream_init(struct cx18 *cx, int type)
        s->dma = cx18_stream_info[type].dma;
        s->buffers = cx->stream_buffers[type];
        s->buf_size = cx->stream_buf_size[type];
+       INIT_LIST_HEAD(&s->buf_pool);
+       s->bufs_per_mdl = 1;
+       s->mdl_size = s->buf_size * s->bufs_per_mdl;
 
-       mutex_init(&s->qlock);
        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);
+       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)
@@ -210,6 +219,7 @@ 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;
+       const char *name;
        int num, ret;
 
        /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
@@ -241,37 +251,38 @@ static int cx18_reg_dev(struct cx18 *cx, int type)
        video_set_drvdata(s->video_dev, s);
 
        /* Register device. First try the desired minor, then any free one. */
-       ret = video_register_device(s->video_dev, vfl_type, num);
+       ret = video_register_device_no_warn(s->video_dev, vfl_type, num);
        if (ret < 0) {
-               CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
+               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;
        }
-       num = s->video_dev->num;
+
+       name = video_device_node_name(s->video_dev);
 
        switch (vfl_type) {
        case VFL_TYPE_GRABBER:
-               CX18_INFO("Registered device video%d for %s (%d x %d kB)\n",
-                         num, s->name, cx->stream_buffers[type],
-                         cx->stream_buf_size[type]/1024);
+               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",
-                       num, s->name);
+               CX18_INFO("Registered device %s for %s\n", name, s->name);
                break;
 
        case VFL_TYPE_VBI:
                if (cx->stream_buffers[type])
-                       CX18_INFO("Registered device vbi%d for %s "
+                       CX18_INFO("Registered device %s for %s "
                                  "(%d x %d bytes)\n",
-                                 num, s->name, cx->stream_buffers[type],
+                                 name, s->name, cx->stream_buffers[type],
                                  cx->stream_buf_size[type]);
                else
-                       CX18_INFO("Registered device vbi%d for %s\n",
-                               num, s->name);
+                       CX18_INFO("Registered device %s for %s\n",
+                               name, s->name);
                break;
        }
 
@@ -308,11 +319,27 @@ 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;
                }
 
+               /* 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].video_dev = NULL;
@@ -347,7 +374,10 @@ static void cx18_vbi_setup(struct cx18_stream *s)
        }
 
        /* setup VBI registers */
-       v4l2_subdev_call(cx->sd_av, video, s_fmt, &cx->vbi.in);
+       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
@@ -367,9 +397,14 @@ static void cx18_vbi_setup(struct cx18_stream *s)
                 * Tell the encoder to capture 21-4+1=18 lines per field,
                 * since we want lines 10 through 21.
                 *
-                * FIXME - revisit for 625/50 systems
+                * 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 : 38;
+               lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
        }
 
        data[0] = s->handle;
@@ -431,56 +466,134 @@ static void cx18_vbi_setup(struct cx18_stream *s)
        cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
 }
 
-struct cx18_queue *cx18_stream_put_buf_fw(struct cx18_stream *s,
-                                         struct cx18_buffer *buf)
+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, buf, &s->q_free);
+               return cx18_enqueue(s, mdl, &s->q_free);
 
-       q = cx18_enqueue(s, buf, &s->q_busy);
+       q = cx18_enqueue(s, mdl, &s->q_busy);
        if (q != &s->q_busy)
-               return q; /* The firmware has the max buffers it can handle */
+               return q; /* The firmware has the max MDLs it can handle */
 
-       cx18_buf_sync_for_device(s, buf);
+       cx18_mdl_sync_for_device(s, mdl);
        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);
+                 (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem,
+                 s->bufs_per_mdl, mdl->id, s->mdl_size);
        return q;
 }
 
-void cx18_stream_load_fw_queue(struct cx18_stream *s)
+static
+void _cx18_stream_load_fw_queue(struct cx18_stream *s)
 {
        struct cx18_queue *q;
-       struct cx18_buffer *buf;
+       struct cx18_mdl *mdl;
 
-       if (atomic_read(&s->q_free.buffers) == 0 ||
-           atomic_read(&s->q_busy.buffers) >= CX18_MAX_FW_MDLS_PER_STREAM)
+       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 {
-               buf = cx18_dequeue(s, &s->q_free);
-               if (buf == NULL)
+               mdl = cx18_dequeue(s, &s->q_free);
+               if (mdl == NULL)
                        break;
-               q = cx18_stream_put_buf_fw(s, buf);
-       } while (atomic_read(&s->q_busy.buffers) < CX18_MAX_FW_MDLS_PER_STREAM
+               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 cx18_buffer *buf;
        int captype = 0;
        struct cx18_api_func_private priv;
+       struct cx18_stream *s_idx;
 
-       if (s->video_dev == NULL && s->dvb.enabled == 0)
+       if (!cx18_stream_enabled(s))
                return -EINVAL;
 
        CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
@@ -494,6 +607,9 @@ 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;
                break;
@@ -568,11 +684,13 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
                        cx18_vbi_setup(s);
 
                /*
-                * assign program index info.
-                * Mask 7: select I/P/B, Num_req: 400 max
-                * FIXME - currently we have this hardcoded as disabled
+                * Select to receive I, P, and B frame index entries, if the
+                * index stream is enabled.  Otherwise disable index entry
+                * generation.
                 */
-               cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
+               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;
@@ -599,20 +717,14 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
                (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
 
        /* Init all the cpu_mdls for this stream */
-       cx18_flush_queues(s);
-       mutex_lock(&s->qlock);
-       list_for_each_entry(buf, &s->q_free.list, list) {
-               cx18_writel(cx, buf->dma_handle,
-                                       &cx->scb->cpu_mdl[buf->id].paddr);
-               cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
-       }
-       mutex_unlock(&s->qlock);
-       cx18_stream_load_fw_queue(s);
+       cx18_stream_configure_mdls(s);
+       _cx18_stream_load_fw_queue(s);
 
        /* 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
@@ -622,6 +734,7 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
                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) {
                        set_bit(CX18_F_I_EOS, &cx->i_flags);
                        cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
@@ -635,6 +748,7 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
        atomic_inc(&cx->tot_capturing);
        return 0;
 }
+EXPORT_SYMBOL(cx18_start_v4l2_encode_stream);
 
 void cx18_stop_all_captures(struct cx18 *cx)
 {
@@ -643,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->video_dev == 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);
@@ -655,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->video_dev == NULL && s->dvb.enabled == 0)
+       if (!cx18_stream_enabled(s))
                return -EINVAL;
 
        /* This function assumes that you are allowed to stop the capture
@@ -666,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
@@ -689,6 +804,7 @@ int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
 
        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;
@@ -698,6 +814,7 @@ int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
 
        return 0;
 }
+EXPORT_SYMBOL(cx18_stop_v4l2_encode_stream);
 
 u32 cx18_find_handle(struct cx18 *cx)
 {
@@ -725,7 +842,7 @@ struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
                s = &cx->streams[i];
                if (s->handle != handle)
                        continue;
-               if (s->video_dev || s->dvb.enabled)
+               if (cx18_stream_enabled(s))
                        return s;
        }
        return NULL;