v4l2: dequeue buffers properly on VIDIOC_STREAMOFF
[cascardo/linux.git] / drivers / media / video / videobuf2-core.c
index d26b1cc..0956bd2 100644 (file)
@@ -842,6 +842,7 @@ void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
 {
        struct vb2_queue *q = vb->vb2_queue;
        unsigned long flags;
+       unsigned int plane;
 
        if (vb->state != VB2_BUF_STATE_ACTIVE)
                return;
@@ -852,6 +853,10 @@ void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
        dprintk(4, "Done processing on buffer %d, state: %d\n",
                        vb->v4l2_buf.index, vb->state);
 
+       /* sync buffers */
+       for (plane = 0; plane < vb->num_planes; ++plane)
+               call_memop(q, finish, vb->planes[plane].mem_priv);
+
        /* Add the buffer to the done buffers list */
        spin_lock_irqsave(&q->done_lock, flags);
        vb->state = state;
@@ -1134,9 +1139,15 @@ err:
 static void __enqueue_in_driver(struct vb2_buffer *vb)
 {
        struct vb2_queue *q = vb->vb2_queue;
+       unsigned int plane;
 
        vb->state = VB2_BUF_STATE_ACTIVE;
        atomic_inc(&q->queued_count);
+
+       /* sync buffers */
+       for (plane = 0; plane < vb->num_planes; ++plane)
+               call_memop(q, prepare, vb->planes[plane].mem_priv);
+
        q->ops->buf_queue(vb);
 }
 
@@ -1462,6 +1473,35 @@ int vb2_wait_for_all_buffers(struct vb2_queue *q)
 }
 EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);
 
+/**
+ * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state
+ */
+static void __vb2_dqbuf(struct vb2_buffer *vb)
+{
+       struct vb2_queue *q = vb->vb2_queue;
+       unsigned int i;
+
+       /* nothing to do if the buffer is already dequeued */
+       if (vb->state == VB2_BUF_STATE_DEQUEUED)
+               return;
+
+       vb->state = VB2_BUF_STATE_DEQUEUED;
+
+       /* unmap DMABUF buffer
+        * TODO: this unpins the buffer(dma_buf_unmap_attachment()).. but
+        * really we want to do this just after DMA, not when the
+        * buffer is dequeued..
+        */
+       if (q->memory == V4L2_MEMORY_DMABUF) {
+               for (i = 0; i < vb->num_planes; ++i) {
+                       if (!vb->planes[i].dbuf_mapped)
+                               continue;
+                       call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
+                       vb->planes[i].dbuf_mapped = 0;
+               }
+       }
+}
+
 /**
  * vb2_dqbuf() - Dequeue a buffer to the userspace
  * @q:         videobuf2 queue
@@ -1510,19 +1550,6 @@ int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
                return ret;
        }
 
-       /* TODO: this unpins the buffer(dma_buf_unmap_attachment()).. but
-        * really we want to do this just after DMA, not when the
-        * buffer is dequeued..
-        */
-       if (q->memory == V4L2_MEMORY_DMABUF) {
-               unsigned int i;
-
-               for (i = 0; i < vb->num_planes; ++i) {
-                       call_memop(q, unmap_dmabuf, vb->planes[i].mem_priv);
-                       vb->planes[i].dbuf_mapped = 0;
-               }
-       }
-
        switch (vb->state) {
        case VB2_BUF_STATE_DONE:
                dprintk(3, "dqbuf: Returning done buffer\n");
@@ -1539,11 +1566,12 @@ int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
        __fill_v4l2_buffer(vb, b);
        /* Remove from videobuf queue */
        list_del(&vb->queued_entry);
+       /* go back to dequeued state */
+       __vb2_dqbuf(vb);
 
        dprintk(1, "dqbuf of buffer %d, with state %d\n",
                        vb->v4l2_buf.index, vb->state);
 
-       vb->state = VB2_BUF_STATE_DEQUEUED;
        return 0;
 }
 EXPORT_SYMBOL_GPL(vb2_dqbuf);
@@ -1582,7 +1610,7 @@ static void __vb2_queue_cancel(struct vb2_queue *q)
         * Reinitialize all buffers for next use.
         */
        for (i = 0; i < q->num_buffers; ++i)
-               q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED;
+               __vb2_dqbuf(q->bufs[i]);
 }
 
 /**
@@ -1686,6 +1714,78 @@ int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
 }
 EXPORT_SYMBOL_GPL(vb2_streamoff);
 
+/**
+ * vb2_expbuf() - Export a buffer as a file descriptor
+ * @q:         videobuf2 queue
+ * @eb:                export buffer structure passed from userspace to vidioc_expbuf
+ *             handler in driver
+ *
+ * The return values from this function are intended to be directly returned
+ * from vidioc_expbuf handler in driver.
+ */
+int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
+{
+       struct vb2_buffer *vb = NULL;
+       struct vb2_plane *vb_plane;
+       int ret;
+       struct dma_buf *dbuf;
+
+       if (q->memory != V4L2_MEMORY_MMAP) {
+               dprintk(1, "Queue is not currently set up for mmap\n");
+               return -EINVAL;
+       }
+
+       if (!q->mem_ops->get_dmabuf) {
+               dprintk(1, "Queue does not support DMA buffer exporting\n");
+               return -EINVAL;
+       }
+
+       if (eb->flags & ~O_CLOEXEC) {
+               dprintk(1, "Queue does support only O_CLOEXEC flag\n");
+               return -EINVAL;
+       }
+
+       /*
+        * Find the plane corresponding to the offset passed by userspace.
+        */
+       if (eb->type != q->type) {
+               dprintk(1, "invalid type %u\n", eb->type);
+               return -EINVAL;
+       }
+       if (eb->index >= q->num_buffers) {
+               dprintk(1, "invalid buffer %u\n", eb->index);
+               return -EINVAL;
+       }
+       vb = q->bufs[eb->index];
+       if (eb->plane >= vb->num_planes) {
+               dprintk(1, "invalid plane %u\n", eb->plane);
+               return -EINVAL;
+       }
+       vb_plane = &vb->planes[eb->plane];
+
+       dbuf = call_memop(q, get_dmabuf, vb_plane->mem_priv);
+       if (IS_ERR_OR_NULL(dbuf)) {
+               dprintk(1, "Failed to export buffer %d, plane %d\n",
+                       eb->index, eb->plane);
+               return -EINVAL;
+       }
+
+       ret = dma_buf_fd(dbuf, eb->flags);
+       if (ret < 0) {
+               dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
+                       eb->index, eb->plane, ret);
+               dma_buf_put(dbuf);
+               return ret;
+       }
+
+       dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
+               eb->index, eb->plane, ret);
+       eb->fd = ret;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(vb2_expbuf);
+
 /**
  * __find_plane_by_offset() - find plane associated with the given offset off
  */