2 * uvc_queue.c -- USB Video Class driver - Buffers management
4 * Copyright (C) 2005-2009
5 * Laurent Pinchart (laurent.pinchart@skynet.be)
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/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>
22 #include <asm/atomic.h>
26 /* ------------------------------------------------------------------------
27 * Video buffers queue management.
29 * Video queues is initialized by uvc_queue_init(). The function performs
30 * basic initialization of the uvc_video_queue struct and never fails.
32 * Video buffer allocation and freeing are performed by uvc_alloc_buffers and
33 * uvc_free_buffers respectively. The former acquires the video queue lock,
34 * while the later must be called with the lock held (so that allocation can
35 * free previously allocated buffers). Trying to free buffers that are mapped
36 * to user space will return -EBUSY.
38 * Video buffers are managed using two queues. However, unlike most USB video
39 * drivers that use an in queue and an out queue, we use a main queue to hold
40 * all queued buffers (both 'empty' and 'done' buffers), and an irq queue to
41 * hold empty buffers. This design (copied from video-buf) minimizes locking
42 * in interrupt, as only one queue is shared between interrupt and user
48 * Unless stated otherwise, all operations that modify the irq buffers queue
49 * are protected by the irq spinlock.
51 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
53 * The buffers are added to the main and irq queues. Both operations are
54 * protected by the queue lock, and the later is protected by the irq
57 * The completion handler fetches a buffer from the irq queue and fills it
58 * with video data. If no buffer is available (irq queue empty), the handler
59 * returns immediately.
61 * When the buffer is full, the completion handler removes it from the irq
62 * queue, marks it as ready (UVC_BUF_STATE_DONE) and wakes its wait queue.
63 * At that point, any process waiting on the buffer will be woken up. If a
64 * process tries to dequeue a buffer after it has been marked ready, the
65 * dequeing will succeed immediately.
67 * 2. Buffers are queued, user is waiting on a buffer and the device gets
70 * When the device is disconnected, the kernel calls the completion handler
71 * with an appropriate status code. The handler marks all buffers in the
72 * irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
73 * that any process waiting on a buffer gets woken up.
75 * Waking up up the first buffer on the irq list is not enough, as the
76 * process waiting on the buffer might restart the dequeue operation
81 void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type)
83 mutex_init(&queue->mutex);
84 spin_lock_init(&queue->irqlock);
85 INIT_LIST_HEAD(&queue->mainqueue);
86 INIT_LIST_HEAD(&queue->irqqueue);
91 * Allocate the video buffers.
93 * Pages are reserved to make sure they will not be swapped, as they will be
94 * filled in the URB completion handler.
96 * Buffers will be individually mapped, so they must all be page aligned.
98 int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
99 unsigned int buflength)
101 unsigned int bufsize = PAGE_ALIGN(buflength);
106 if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
107 nbuffers = UVC_MAX_VIDEO_BUFFERS;
109 mutex_lock(&queue->mutex);
111 if ((ret = uvc_free_buffers(queue)) < 0)
114 /* Bail out if no buffers should be allocated. */
118 /* Decrement the number of buffers until allocation succeeds. */
119 for (; nbuffers > 0; --nbuffers) {
120 mem = vmalloc_32(nbuffers * bufsize);
130 for (i = 0; i < nbuffers; ++i) {
131 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
132 queue->buffer[i].buf.index = i;
133 queue->buffer[i].buf.m.offset = i * bufsize;
134 queue->buffer[i].buf.length = buflength;
135 queue->buffer[i].buf.type = queue->type;
136 queue->buffer[i].buf.sequence = 0;
137 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
138 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
139 queue->buffer[i].buf.flags = 0;
140 init_waitqueue_head(&queue->buffer[i].wait);
144 queue->count = nbuffers;
145 queue->buf_size = bufsize;
149 mutex_unlock(&queue->mutex);
154 * Free the video buffers.
156 * This function must be called with the queue lock held.
158 int uvc_free_buffers(struct uvc_video_queue *queue)
162 for (i = 0; i < queue->count; ++i) {
163 if (queue->buffer[i].vma_use_count != 0)
176 * Check if buffers have been allocated.
178 int uvc_queue_allocated(struct uvc_video_queue *queue)
182 mutex_lock(&queue->mutex);
183 allocated = queue->count != 0;
184 mutex_unlock(&queue->mutex);
189 static void __uvc_query_buffer(struct uvc_buffer *buf,
190 struct v4l2_buffer *v4l2_buf)
192 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
194 if (buf->vma_use_count)
195 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
197 switch (buf->state) {
198 case UVC_BUF_STATE_ERROR:
199 case UVC_BUF_STATE_DONE:
200 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
202 case UVC_BUF_STATE_QUEUED:
203 case UVC_BUF_STATE_ACTIVE:
204 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
206 case UVC_BUF_STATE_IDLE:
212 int uvc_query_buffer(struct uvc_video_queue *queue,
213 struct v4l2_buffer *v4l2_buf)
217 mutex_lock(&queue->mutex);
218 if (v4l2_buf->index >= queue->count) {
223 __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
226 mutex_unlock(&queue->mutex);
231 * Queue a video buffer. Attempting to queue a buffer that has already been
232 * queued will return -EINVAL.
234 int uvc_queue_buffer(struct uvc_video_queue *queue,
235 struct v4l2_buffer *v4l2_buf)
237 struct uvc_buffer *buf;
241 uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
243 if (v4l2_buf->type != queue->type ||
244 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
245 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
246 "and/or memory (%u).\n", v4l2_buf->type,
251 mutex_lock(&queue->mutex);
252 if (v4l2_buf->index >= queue->count) {
253 uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
258 buf = &queue->buffer[v4l2_buf->index];
259 if (buf->state != UVC_BUF_STATE_IDLE) {
260 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
261 "(%u).\n", buf->state);
266 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
267 v4l2_buf->bytesused > buf->buf.length) {
268 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
273 spin_lock_irqsave(&queue->irqlock, flags);
274 if (queue->flags & UVC_QUEUE_DISCONNECTED) {
275 spin_unlock_irqrestore(&queue->irqlock, flags);
279 buf->state = UVC_BUF_STATE_QUEUED;
280 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
281 buf->buf.bytesused = 0;
283 buf->buf.bytesused = v4l2_buf->bytesused;
285 list_add_tail(&buf->stream, &queue->mainqueue);
286 list_add_tail(&buf->queue, &queue->irqqueue);
287 spin_unlock_irqrestore(&queue->irqlock, flags);
290 mutex_unlock(&queue->mutex);
294 static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
297 return (buf->state != UVC_BUF_STATE_QUEUED &&
298 buf->state != UVC_BUF_STATE_ACTIVE)
302 return wait_event_interruptible(buf->wait,
303 buf->state != UVC_BUF_STATE_QUEUED &&
304 buf->state != UVC_BUF_STATE_ACTIVE);
308 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
311 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
312 struct v4l2_buffer *v4l2_buf, int nonblocking)
314 struct uvc_buffer *buf;
317 if (v4l2_buf->type != queue->type ||
318 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
319 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
320 "and/or memory (%u).\n", v4l2_buf->type,
325 mutex_lock(&queue->mutex);
326 if (list_empty(&queue->mainqueue)) {
327 uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
332 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
333 if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
336 uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
337 buf->buf.index, buf->state, buf->buf.bytesused);
339 switch (buf->state) {
340 case UVC_BUF_STATE_ERROR:
341 uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
342 "(transmission error).\n");
344 case UVC_BUF_STATE_DONE:
345 buf->state = UVC_BUF_STATE_IDLE;
348 case UVC_BUF_STATE_IDLE:
349 case UVC_BUF_STATE_QUEUED:
350 case UVC_BUF_STATE_ACTIVE:
352 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
353 "(driver bug?).\n", buf->state);
358 list_del(&buf->stream);
359 __uvc_query_buffer(buf, v4l2_buf);
362 mutex_unlock(&queue->mutex);
367 * Poll the video queue.
369 * This function implements video queue polling and is intended to be used by
370 * the device poll handler.
372 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
375 struct uvc_buffer *buf;
376 unsigned int mask = 0;
378 mutex_lock(&queue->mutex);
379 if (list_empty(&queue->mainqueue)) {
383 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
385 poll_wait(file, &buf->wait, wait);
386 if (buf->state == UVC_BUF_STATE_DONE ||
387 buf->state == UVC_BUF_STATE_ERROR)
388 mask |= POLLIN | POLLRDNORM;
391 mutex_unlock(&queue->mutex);
396 * Enable or disable the video buffers queue.
398 * The queue must be enabled before starting video acquisition and must be
399 * disabled after stopping it. This ensures that the video buffers queue
400 * state can be properly initialized before buffers are accessed from the
403 * Enabling the video queue initializes parameters (such as sequence number,
404 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
406 * Disabling the video queue cancels the queue and removes all buffers from
409 * This function can't be called from interrupt context. Use
410 * uvc_queue_cancel() instead.
412 int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
417 mutex_lock(&queue->mutex);
419 if (uvc_queue_streaming(queue)) {
424 queue->flags |= UVC_QUEUE_STREAMING;
427 uvc_queue_cancel(queue, 0);
428 INIT_LIST_HEAD(&queue->mainqueue);
430 for (i = 0; i < queue->count; ++i)
431 queue->buffer[i].state = UVC_BUF_STATE_IDLE;
433 queue->flags &= ~UVC_QUEUE_STREAMING;
437 mutex_unlock(&queue->mutex);
442 * Cancel the video buffers queue.
444 * Cancelling the queue marks all buffers on the irq queue as erroneous,
445 * wakes them up and removes them from the queue.
447 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
450 * This function acquires the irq spinlock and can be called from interrupt
453 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
455 struct uvc_buffer *buf;
458 spin_lock_irqsave(&queue->irqlock, flags);
459 while (!list_empty(&queue->irqqueue)) {
460 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
462 list_del(&buf->queue);
463 buf->state = UVC_BUF_STATE_ERROR;
466 /* This must be protected by the irqlock spinlock to avoid race
467 * conditions between uvc_queue_buffer and the disconnection event that
468 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
469 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
470 * state outside the queue code.
473 queue->flags |= UVC_QUEUE_DISCONNECTED;
474 spin_unlock_irqrestore(&queue->irqlock, flags);
477 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
478 struct uvc_buffer *buf)
480 struct uvc_buffer *nextbuf;
483 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
484 buf->buf.length != buf->buf.bytesused) {
485 buf->state = UVC_BUF_STATE_QUEUED;
486 buf->buf.bytesused = 0;
490 spin_lock_irqsave(&queue->irqlock, flags);
491 list_del(&buf->queue);
492 if (!list_empty(&queue->irqqueue))
493 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
497 spin_unlock_irqrestore(&queue->irqlock, flags);
499 buf->buf.sequence = queue->sequence++;
500 do_gettimeofday(&buf->buf.timestamp);