2 * uvc_queue.c -- USB Video Class driver - Buffers management
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/atomic.h>
15 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 #include <media/videobuf2-vmalloc.h>
27 /* ------------------------------------------------------------------------
28 * Video buffers queue management.
30 * Video queues is initialized by uvc_queue_init(). The function performs
31 * basic initialization of the uvc_video_queue struct and never fails.
33 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
34 * the videobuf2 queue operations by serializing calls to videobuf2 and a
35 * spinlock to protect the IRQ queue that holds the buffers to be processed by
39 /* -----------------------------------------------------------------------------
40 * videobuf2 queue operations
43 static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
44 unsigned int *nbuffers, unsigned int *nplanes,
45 unsigned int sizes[], void *alloc_ctxs[])
47 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
48 struct uvc_streaming *stream =
49 container_of(queue, struct uvc_streaming, queue);
51 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
52 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
56 sizes[0] = stream->ctrl.dwMaxVideoFrameSize;
61 static int uvc_buffer_prepare(struct vb2_buffer *vb)
63 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
64 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
66 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
67 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
68 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
72 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
75 buf->state = UVC_BUF_STATE_QUEUED;
77 buf->mem = vb2_plane_vaddr(vb, 0);
78 buf->length = vb2_plane_size(vb, 0);
79 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
82 buf->bytesused = vb2_get_plane_payload(vb, 0);
87 static void uvc_buffer_queue(struct vb2_buffer *vb)
89 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
90 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
93 spin_lock_irqsave(&queue->irqlock, flags);
94 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
95 list_add_tail(&buf->queue, &queue->irqqueue);
97 /* If the device is disconnected return the buffer to userspace
98 * directly. The next QBUF call will fail with -ENODEV.
100 buf->state = UVC_BUF_STATE_ERROR;
101 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
104 spin_unlock_irqrestore(&queue->irqlock, flags);
107 static int uvc_buffer_finish(struct vb2_buffer *vb)
109 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
110 struct uvc_streaming *stream =
111 container_of(queue, struct uvc_streaming, queue);
112 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
114 uvc_video_clock_update(stream, &vb->v4l2_buf, buf);
118 static void uvc_wait_prepare(struct vb2_queue *vq)
120 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
122 mutex_unlock(&queue->mutex);
125 static void uvc_wait_finish(struct vb2_queue *vq)
127 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
129 mutex_lock(&queue->mutex);
132 static struct vb2_ops uvc_queue_qops = {
133 .queue_setup = uvc_queue_setup,
134 .buf_prepare = uvc_buffer_prepare,
135 .buf_queue = uvc_buffer_queue,
136 .buf_finish = uvc_buffer_finish,
137 .wait_prepare = uvc_wait_prepare,
138 .wait_finish = uvc_wait_finish,
141 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
146 queue->queue.type = type;
147 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
148 queue->queue.drv_priv = queue;
149 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
150 queue->queue.ops = &uvc_queue_qops;
151 queue->queue.mem_ops = &vb2_vmalloc_memops;
152 ret = vb2_queue_init(&queue->queue);
156 mutex_init(&queue->mutex);
157 spin_lock_init(&queue->irqlock);
158 INIT_LIST_HEAD(&queue->irqqueue);
159 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
164 /* -----------------------------------------------------------------------------
165 * V4L2 queue operations
168 int uvc_alloc_buffers(struct uvc_video_queue *queue,
169 struct v4l2_requestbuffers *rb)
173 mutex_lock(&queue->mutex);
174 ret = vb2_reqbufs(&queue->queue, rb);
175 mutex_unlock(&queue->mutex);
177 return ret ? ret : rb->count;
180 void uvc_free_buffers(struct uvc_video_queue *queue)
182 mutex_lock(&queue->mutex);
183 vb2_queue_release(&queue->queue);
184 mutex_unlock(&queue->mutex);
187 int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
191 mutex_lock(&queue->mutex);
192 ret = vb2_querybuf(&queue->queue, buf);
193 mutex_unlock(&queue->mutex);
198 int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
202 mutex_lock(&queue->mutex);
203 ret = vb2_qbuf(&queue->queue, buf);
204 mutex_unlock(&queue->mutex);
209 int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
214 mutex_lock(&queue->mutex);
215 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
216 mutex_unlock(&queue->mutex);
221 int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
225 mutex_lock(&queue->mutex);
226 ret = vb2_mmap(&queue->queue, vma);
227 mutex_unlock(&queue->mutex);
233 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
238 mutex_lock(&queue->mutex);
239 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
240 mutex_unlock(&queue->mutex);
245 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
250 mutex_lock(&queue->mutex);
251 ret = vb2_poll(&queue->queue, file, wait);
252 mutex_unlock(&queue->mutex);
257 /* -----------------------------------------------------------------------------
262 * Check if buffers have been allocated.
264 int uvc_queue_allocated(struct uvc_video_queue *queue)
268 mutex_lock(&queue->mutex);
269 allocated = vb2_is_busy(&queue->queue);
270 mutex_unlock(&queue->mutex);
276 * Enable or disable the video buffers queue.
278 * The queue must be enabled before starting video acquisition and must be
279 * disabled after stopping it. This ensures that the video buffers queue
280 * state can be properly initialized before buffers are accessed from the
283 * Enabling the video queue returns -EBUSY if the queue is already enabled.
285 * Disabling the video queue cancels the queue and removes all buffers from
288 * This function can't be called from interrupt context. Use
289 * uvc_queue_cancel() instead.
291 int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
296 mutex_lock(&queue->mutex);
298 ret = vb2_streamon(&queue->queue, queue->queue.type);
304 ret = vb2_streamoff(&queue->queue, queue->queue.type);
308 spin_lock_irqsave(&queue->irqlock, flags);
309 INIT_LIST_HEAD(&queue->irqqueue);
310 spin_unlock_irqrestore(&queue->irqlock, flags);
314 mutex_unlock(&queue->mutex);
319 * Cancel the video buffers queue.
321 * Cancelling the queue marks all buffers on the irq queue as erroneous,
322 * wakes them up and removes them from the queue.
324 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
327 * This function acquires the irq spinlock and can be called from interrupt
330 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
332 struct uvc_buffer *buf;
335 spin_lock_irqsave(&queue->irqlock, flags);
336 while (!list_empty(&queue->irqqueue)) {
337 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
339 list_del(&buf->queue);
340 buf->state = UVC_BUF_STATE_ERROR;
341 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
343 /* This must be protected by the irqlock spinlock to avoid race
344 * conditions between uvc_buffer_queue and the disconnection event that
345 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
346 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
347 * state outside the queue code.
350 queue->flags |= UVC_QUEUE_DISCONNECTED;
351 spin_unlock_irqrestore(&queue->irqlock, flags);
354 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
355 struct uvc_buffer *buf)
357 struct uvc_buffer *nextbuf;
360 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
362 buf->state = UVC_BUF_STATE_QUEUED;
364 vb2_set_plane_payload(&buf->buf, 0, 0);
368 spin_lock_irqsave(&queue->irqlock, flags);
369 list_del(&buf->queue);
370 if (!list_empty(&queue->irqqueue))
371 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
375 spin_unlock_irqrestore(&queue->irqlock, flags);
377 buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
378 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
379 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);