V4L/DVB (9894): cx18: Use a known open task handle when setting stream CX2341x parameters
[linux-2.6] / drivers / media / video / cx18 / cx18-streams.c
index 0c8e754..63c336c 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@radix.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
@@ -110,7 +111,6 @@ 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;
 
        /* we need to keep v4l2dev, so restore it afterwards */
        memset(s, 0, sizeof(*s));
@@ -123,21 +123,15 @@ 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);
+
+       mutex_init(&s->qlock);
        init_waitqueue_head(&s->waitq);
        s->id = -1;
        cx18_queue_init(&s->q_free);
+       cx18_queue_init(&s->q_busy);
        cx18_queue_init(&s->q_full);
-       cx18_queue_init(&s->q_io);
 }
 
 static int cx18_prep_dev(struct cx18 *cx, int type)
@@ -167,7 +161,7 @@ static int cx18_prep_dev(struct cx18 *cx, int type)
        /* 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;
        }
@@ -200,16 +194,18 @@ static int cx18_prep_dev(struct cx18 *cx, int type)
 /* 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)
@@ -217,14 +213,14 @@ 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 num;
+       int num, ret;
 
        /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
         * We need a VFL_TYPE_TS defined.
@@ -233,9 +229,10 @@ 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;
                }
        }
 
@@ -252,19 +249,21 @@ static int cx18_reg_dev(struct cx18 *cx, int type)
        }
 
        /* Register device. First try the desired minor, then any free one. */
-       if (video_register_device(s->v4l2dev, vfl_type, num)) {
+       ret = video_register_device(s->v4l2dev, vfl_type, num);
+       if (ret < 0) {
                CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
                        s->name, num);
                video_device_release(s->v4l2dev);
                s->v4l2dev = NULL;
-               return -ENOMEM;
+               return ret;
        }
        num = s->v4l2dev->num;
 
        switch (vfl_type) {
        case VFL_TYPE_GRABBER:
-               CX18_INFO("Registered device video%d for %s (%d MB)\n",
-                       num, s->name, cx->options.megabytes[type]);
+               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);
                break;
 
        case VFL_TYPE_RADIO:
@@ -273,10 +272,11 @@ static int cx18_reg_dev(struct cx18 *cx, int type)
                break;
 
        case VFL_TYPE_VBI:
-               if (cx->options.megabytes[type])
-                       CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
-                               num,
-                               s->name, cx->options.megabytes[type]);
+               if (cx->stream_buffers[type])
+                       CX18_INFO("Registered device vbi%d for %s "
+                                 "(%d x %d bytes)\n",
+                                 num, s->name, cx->stream_buffers[type],
+                                 cx->stream_buf_size[type]);
                else
                        CX18_INFO("Registered device vbi%d for %s\n",
                                num, s->name);
@@ -290,18 +290,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 */
@@ -336,7 +340,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;
 
@@ -354,8 +358,7 @@ static void cx18_vbi_setup(struct cx18_stream *s)
        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 raw line takes 1444 bytes: 4 byte SAV code + 2 * 720
           A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
           header, 42 data bytes + checksum (to be confirmed) */
        if (raw) {
@@ -373,14 +376,15 @@ static void cx18_vbi_setup(struct cx18_stream *s)
        /* 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 ? cx->vbi.raw_decoder_line_size
+                      : cx->vbi.sliced_decoder_line_size);
        /* 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 */
        if (raw) {
                data[4] = 0x20602060;
-               data[5] = 0x30703070;
+               data[5] = 0x307090d0;
        } else {
                data[4] = 0xB0F0B0F0;
                data[5] = 0xA0E0A0E0;
@@ -393,11 +397,52 @@ 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)
+{
+       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_STREAMING, &s->s_flags))
+               return cx18_enqueue(s, buf, &s->q_free);
+
+       q = cx18_enqueue(s, buf, &s->q_busy);
+       if (q != &s->q_busy)
+               return q; /* The firmware has the max buffers it can handle */
+
+       cx18_buf_sync_for_device(s, buf);
+       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);
+       return q;
+}
+
+void cx18_stream_load_fw_queue(struct cx18_stream *s)
+{
+       struct cx18_queue *q;
+       struct cx18_buffer *buf;
+
+       if (atomic_read(&s->q_free.buffers) == 0 ||
+           atomic_read(&s->q_busy.buffers) >= 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)
+                       break;
+               q = cx18_stream_put_buf_fw(s, buf);
+       } while (atomic_read(&s->q_busy.buffers) < CX18_MAX_FW_MDLS_PER_STREAM
+                && q == &s->q_busy);
+}
+
 int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
 {
        u32 data[MAX_MB_ARGUMENTS];
        struct cx18 *cx = s->cx;
-       struct list_head *p;
+       struct cx18_buffer *buf;
        int ts = 0;
        int captype = 0;
 
@@ -426,8 +471,8 @@ 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;
+               captype = cx18_raw_vbi(cx) ?
+                    CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
                cx->vbi.frame = 0;
                cx->vbi.inserted_frame = 0;
                memset(cx->vbi.sliced_mpeg_size,
@@ -449,6 +494,8 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
        cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
 
        if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
+               struct cx18_api_func_private priv;
+
                /* Stuff from Windows, we don't know what it is */
                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);
@@ -468,7 +515,9 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
                cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
 
                /* Setup API for Stream */
-               cx2341x_update(cx, cx18_api_func, NULL, &cx->params);
+               priv.cx = cx;
+               priv.s = s;
+               cx2341x_update(&priv, cx18_api_func, NULL, &cx->params);
        }
 
        if (atomic_read(&cx->tot_capturing) == 0) {
@@ -480,16 +529,17 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
                (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_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);
-               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);
        }
+       mutex_unlock(&s->qlock);
+       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");
@@ -498,9 +548,15 @@ int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
                        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;
+               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;
        }
 
@@ -552,9 +608,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);
@@ -562,6 +615,9 @@ 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;
 
@@ -587,3 +643,21 @@ u32 cx18_find_handle(struct cx18 *cx)
        }
        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 (s->v4l2dev || s->dvb.enabled)
+                       return s;
+       }
+       return NULL;
+}