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.
13 #include <linux/atomic.h>
14 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/videodev2.h>
20 #include <linux/vmalloc.h>
21 #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_video *video = container_of(queue, struct uvc_video, queue);
50 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
51 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
55 sizes[0] = video->imagesize;
60 static int uvc_buffer_prepare(struct vb2_buffer *vb)
62 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
63 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
65 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
66 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
67 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
71 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
74 buf->state = UVC_BUF_STATE_QUEUED;
75 buf->mem = vb2_plane_vaddr(vb, 0);
76 buf->length = vb2_plane_size(vb, 0);
77 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
80 buf->bytesused = vb2_get_plane_payload(vb, 0);
85 static void uvc_buffer_queue(struct vb2_buffer *vb)
87 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
88 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
91 spin_lock_irqsave(&queue->irqlock, flags);
93 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
94 list_add_tail(&buf->queue, &queue->irqqueue);
96 /* If the device is disconnected return the buffer to userspace
97 * directly. The next QBUF call will fail with -ENODEV.
99 buf->state = UVC_BUF_STATE_ERROR;
100 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
103 spin_unlock_irqrestore(&queue->irqlock, flags);
106 static void uvc_wait_prepare(struct vb2_queue *vq)
108 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
110 mutex_unlock(&queue->mutex);
113 static void uvc_wait_finish(struct vb2_queue *vq)
115 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
117 mutex_lock(&queue->mutex);
120 static struct vb2_ops uvc_queue_qops = {
121 .queue_setup = uvc_queue_setup,
122 .buf_prepare = uvc_buffer_prepare,
123 .buf_queue = uvc_buffer_queue,
124 .wait_prepare = uvc_wait_prepare,
125 .wait_finish = uvc_wait_finish,
128 static int uvc_queue_init(struct uvc_video_queue *queue,
129 enum v4l2_buf_type type)
133 queue->queue.type = type;
134 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
135 queue->queue.drv_priv = queue;
136 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
137 queue->queue.ops = &uvc_queue_qops;
138 queue->queue.mem_ops = &vb2_vmalloc_memops;
139 ret = vb2_queue_init(&queue->queue);
143 mutex_init(&queue->mutex);
144 spin_lock_init(&queue->irqlock);
145 INIT_LIST_HEAD(&queue->irqqueue);
152 * Free the video buffers.
154 static void uvc_free_buffers(struct uvc_video_queue *queue)
156 mutex_lock(&queue->mutex);
157 vb2_queue_release(&queue->queue);
158 mutex_unlock(&queue->mutex);
162 * Allocate the video buffers.
164 static int uvc_alloc_buffers(struct uvc_video_queue *queue,
165 struct v4l2_requestbuffers *rb)
169 mutex_lock(&queue->mutex);
170 ret = vb2_reqbufs(&queue->queue, rb);
171 mutex_unlock(&queue->mutex);
173 return ret ? ret : rb->count;
176 static int uvc_query_buffer(struct uvc_video_queue *queue,
177 struct v4l2_buffer *buf)
181 mutex_lock(&queue->mutex);
182 ret = vb2_querybuf(&queue->queue, buf);
183 mutex_unlock(&queue->mutex);
188 static int uvc_queue_buffer(struct uvc_video_queue *queue,
189 struct v4l2_buffer *buf)
194 mutex_lock(&queue->mutex);
195 ret = vb2_qbuf(&queue->queue, buf);
196 spin_lock_irqsave(&queue->irqlock, flags);
197 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
198 queue->flags &= ~UVC_QUEUE_PAUSED;
199 spin_unlock_irqrestore(&queue->irqlock, flags);
200 mutex_unlock(&queue->mutex);
206 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
209 static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
210 struct v4l2_buffer *buf, int nonblocking)
214 mutex_lock(&queue->mutex);
215 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
216 mutex_unlock(&queue->mutex);
222 * Poll the video queue.
224 * This function implements video queue polling and is intended to be used by
225 * the device poll handler.
227 static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
228 struct file *file, poll_table *wait)
232 mutex_lock(&queue->mutex);
233 ret = vb2_poll(&queue->queue, file, wait);
234 mutex_unlock(&queue->mutex);
239 static int uvc_queue_mmap(struct uvc_video_queue *queue,
240 struct vm_area_struct *vma)
244 mutex_lock(&queue->mutex);
245 ret = vb2_mmap(&queue->queue, vma);
246 mutex_unlock(&queue->mutex);
255 * NO-MMU arch need this function to make mmap() work correctly.
257 static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
262 mutex_lock(&queue->mutex);
263 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
264 mutex_unlock(&queue->mutex);
270 * Cancel the video buffers queue.
272 * Cancelling the queue marks all buffers on the irq queue as erroneous,
273 * wakes them up and removes them from the queue.
275 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
278 * This function acquires the irq spinlock and can be called from interrupt
281 static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
283 struct uvc_buffer *buf;
286 spin_lock_irqsave(&queue->irqlock, flags);
287 while (!list_empty(&queue->irqqueue)) {
288 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
290 list_del(&buf->queue);
291 buf->state = UVC_BUF_STATE_ERROR;
292 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
294 /* This must be protected by the irqlock spinlock to avoid race
295 * conditions between uvc_queue_buffer and the disconnection event that
296 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
297 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
298 * state outside the queue code.
301 queue->flags |= UVC_QUEUE_DISCONNECTED;
302 spin_unlock_irqrestore(&queue->irqlock, flags);
306 * Enable or disable the video buffers queue.
308 * The queue must be enabled before starting video acquisition and must be
309 * disabled after stopping it. This ensures that the video buffers queue
310 * state can be properly initialized before buffers are accessed from the
313 * Enabling the video queue initializes parameters (such as sequence number,
314 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
316 * Disabling the video queue cancels the queue and removes all buffers from
319 * This function can't be called from interrupt context. Use
320 * uvc_queue_cancel() instead.
322 static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
327 mutex_lock(&queue->mutex);
329 ret = vb2_streamon(&queue->queue, queue->queue.type);
336 ret = vb2_streamoff(&queue->queue, queue->queue.type);
340 spin_lock_irqsave(&queue->irqlock, flags);
341 INIT_LIST_HEAD(&queue->irqqueue);
344 * FIXME: We need to clear the DISCONNECTED flag to ensure that
345 * applications will be able to queue buffers for the next
346 * streaming run. However, clearing it here doesn't guarantee
347 * that the device will be reconnected in the meantime.
349 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
350 spin_unlock_irqrestore(&queue->irqlock, flags);
354 mutex_unlock(&queue->mutex);
358 /* called with &queue_irqlock held.. */
359 static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
360 struct uvc_buffer *buf)
362 struct uvc_buffer *nextbuf;
364 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
365 buf->length != buf->bytesused) {
366 buf->state = UVC_BUF_STATE_QUEUED;
367 vb2_set_plane_payload(&buf->buf, 0, 0);
371 list_del(&buf->queue);
372 if (!list_empty(&queue->irqqueue))
373 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
379 * FIXME: with videobuf2, the sequence number or timestamp fields
380 * are valid only for video capture devices and the UVC gadget usually
381 * is a video output device. Keeping these until the specs are clear on
384 buf->buf.v4l2_buf.sequence = queue->sequence++;
385 do_gettimeofday(&buf->buf.v4l2_buf.timestamp);
387 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
388 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
393 static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
395 struct uvc_buffer *buf = NULL;
397 if (!list_empty(&queue->irqqueue))
398 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
401 queue->flags |= UVC_QUEUE_PAUSED;