2 * uvc_queue.c -- USB Video Class driver - Buffers management
4 * Copyright (C) 2005-2008
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>
15 #include <linux/version.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 <asm/atomic.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 buffer allocation and freeing are performed by uvc_alloc_buffers and
34 * uvc_free_buffers respectively. The former acquires the video queue lock,
35 * while the later must be called with the lock held (so that allocation can
36 * free previously allocated buffers). Trying to free buffers that are mapped
37 * to user space will return -EBUSY.
39 * Video buffers are managed using two queues. However, unlike most USB video
40 * drivers which use an in queue and an out queue, we use a main queue which
41 * holds all queued buffers (both 'empty' and 'done' buffers), and an irq
42 * queue which holds empty buffers. This design (copied from video-buf)
43 * minimizes locking in interrupt, as only one queue is shared between
44 * interrupt and user contexts.
49 * Unless stated otherwise, all operations which modify the irq buffers queue
50 * are protected by the irq spinlock.
52 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
54 * The buffers are added to the main and irq queues. Both operations are
55 * protected by the queue lock, and the latert is protected by the irq
58 * The completion handler fetches a buffer from the irq queue and fills it
59 * with video data. If no buffer is available (irq queue empty), the handler
60 * returns immediately.
62 * When the buffer is full, the completion handler removes it from the irq
63 * queue, marks it as ready (UVC_BUF_STATE_DONE) and wake its wait queue.
64 * At that point, any process waiting on the buffer will be woken up. If a
65 * process tries to dequeue a buffer after it has been marked ready, the
66 * dequeing will succeed immediately.
68 * 2. Buffers are queued, user is waiting on a buffer and the device gets
71 * When the device is disconnected, the kernel calls the completion handler
72 * with an appropriate status code. The handler marks all buffers in the
73 * irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
74 * that any process waiting on a buffer gets woken up.
76 * Waking up up the first buffer on the irq list is not enough, as the
77 * process waiting on the buffer might restart the dequeue operation
82 void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type)
84 mutex_init(&queue->mutex);
85 spin_lock_init(&queue->irqlock);
86 INIT_LIST_HEAD(&queue->mainqueue);
87 INIT_LIST_HEAD(&queue->irqqueue);
92 * Allocate the video buffers.
94 * Pages are reserved to make sure they will not be swaped, as they will be
95 * filled in URB completion handler.
97 * Buffers will be individually mapped, so they must all be page aligned.
99 int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
100 unsigned int buflength)
102 unsigned int bufsize = PAGE_ALIGN(buflength);
107 if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
108 nbuffers = UVC_MAX_VIDEO_BUFFERS;
110 mutex_lock(&queue->mutex);
112 if ((ret = uvc_free_buffers(queue)) < 0)
115 /* Bail out if no buffers should be allocated. */
119 /* Decrement the number of buffers until allocation succeeds. */
120 for (; nbuffers > 0; --nbuffers) {
121 mem = vmalloc_32(nbuffers * bufsize);
131 for (i = 0; i < nbuffers; ++i) {
132 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
133 queue->buffer[i].buf.index = i;
134 queue->buffer[i].buf.m.offset = i * bufsize;
135 queue->buffer[i].buf.length = buflength;
136 queue->buffer[i].buf.type = queue->type;
137 queue->buffer[i].buf.sequence = 0;
138 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
139 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
140 queue->buffer[i].buf.flags = 0;
141 init_waitqueue_head(&queue->buffer[i].wait);
145 queue->count = nbuffers;
146 queue->buf_size = bufsize;
150 mutex_unlock(&queue->mutex);
155 * Free the video buffers.
157 * This function must be called with the queue lock held.
159 int uvc_free_buffers(struct uvc_video_queue *queue)
163 for (i = 0; i < queue->count; ++i) {
164 if (queue->buffer[i].vma_use_count != 0)
176 static void __uvc_query_buffer(struct uvc_buffer *buf,
177 struct v4l2_buffer *v4l2_buf)
179 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
181 if (buf->vma_use_count)
182 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
184 switch (buf->state) {
185 case UVC_BUF_STATE_ERROR:
186 case UVC_BUF_STATE_DONE:
187 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
189 case UVC_BUF_STATE_QUEUED:
190 case UVC_BUF_STATE_ACTIVE:
191 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
193 case UVC_BUF_STATE_IDLE:
199 int uvc_query_buffer(struct uvc_video_queue *queue,
200 struct v4l2_buffer *v4l2_buf)
204 mutex_lock(&queue->mutex);
205 if (v4l2_buf->index >= queue->count) {
210 __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
213 mutex_unlock(&queue->mutex);
218 * Queue a video buffer. Attempting to queue a buffer that has already been
219 * queued will return -EINVAL.
221 int uvc_queue_buffer(struct uvc_video_queue *queue,
222 struct v4l2_buffer *v4l2_buf)
224 struct uvc_buffer *buf;
228 uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
230 if (v4l2_buf->type != queue->type ||
231 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
232 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
233 "and/or memory (%u).\n", v4l2_buf->type,
238 mutex_lock(&queue->mutex);
239 if (v4l2_buf->index >= queue->count) {
240 uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
245 buf = &queue->buffer[v4l2_buf->index];
246 if (buf->state != UVC_BUF_STATE_IDLE) {
247 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
248 "(%u).\n", buf->state);
253 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
254 v4l2_buf->bytesused > buf->buf.length) {
255 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
260 spin_lock_irqsave(&queue->irqlock, flags);
261 if (queue->flags & UVC_QUEUE_DISCONNECTED) {
262 spin_unlock_irqrestore(&queue->irqlock, flags);
266 buf->state = UVC_BUF_STATE_QUEUED;
267 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
268 buf->buf.bytesused = 0;
270 buf->buf.bytesused = v4l2_buf->bytesused;
272 list_add_tail(&buf->stream, &queue->mainqueue);
273 list_add_tail(&buf->queue, &queue->irqqueue);
274 spin_unlock_irqrestore(&queue->irqlock, flags);
277 mutex_unlock(&queue->mutex);
281 static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
284 return (buf->state != UVC_BUF_STATE_QUEUED &&
285 buf->state != UVC_BUF_STATE_ACTIVE)
289 return wait_event_interruptible(buf->wait,
290 buf->state != UVC_BUF_STATE_QUEUED &&
291 buf->state != UVC_BUF_STATE_ACTIVE);
295 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
298 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
299 struct v4l2_buffer *v4l2_buf, int nonblocking)
301 struct uvc_buffer *buf;
304 if (v4l2_buf->type != queue->type ||
305 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
306 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
307 "and/or memory (%u).\n", v4l2_buf->type,
312 mutex_lock(&queue->mutex);
313 if (list_empty(&queue->mainqueue)) {
314 uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
319 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
320 if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
323 uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
324 buf->buf.index, buf->state, buf->buf.bytesused);
326 switch (buf->state) {
327 case UVC_BUF_STATE_ERROR:
328 uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
329 "(transmission error).\n");
331 case UVC_BUF_STATE_DONE:
332 buf->state = UVC_BUF_STATE_IDLE;
335 case UVC_BUF_STATE_IDLE:
336 case UVC_BUF_STATE_QUEUED:
337 case UVC_BUF_STATE_ACTIVE:
339 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
340 "(driver bug?).\n", buf->state);
345 list_del(&buf->stream);
346 __uvc_query_buffer(buf, v4l2_buf);
349 mutex_unlock(&queue->mutex);
354 * Poll the video queue.
356 * This function implements video queue polling and is intended to be used by
357 * the device poll handler.
359 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
362 struct uvc_buffer *buf;
363 unsigned int mask = 0;
365 mutex_lock(&queue->mutex);
366 if (list_empty(&queue->mainqueue)) {
370 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
372 poll_wait(file, &buf->wait, wait);
373 if (buf->state == UVC_BUF_STATE_DONE ||
374 buf->state == UVC_BUF_STATE_ERROR)
375 mask |= POLLIN | POLLRDNORM;
378 mutex_unlock(&queue->mutex);
383 * Enable or disable the video buffers queue.
385 * The queue must be enabled before starting video acquisition and must be
386 * disabled after stopping it. This ensures that the video buffers queue
387 * state can be properly initialized before buffers are accessed from the
390 * Enabling the video queue initializes parameters (such as sequence number,
391 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
393 * Disabling the video queue cancels the queue and removes all buffers from
396 * This function can't be called from interrupt context. Use
397 * uvc_queue_cancel() instead.
399 int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
404 mutex_lock(&queue->mutex);
406 if (uvc_queue_streaming(queue)) {
411 queue->flags |= UVC_QUEUE_STREAMING;
414 uvc_queue_cancel(queue, 0);
415 INIT_LIST_HEAD(&queue->mainqueue);
417 for (i = 0; i < queue->count; ++i)
418 queue->buffer[i].state = UVC_BUF_STATE_IDLE;
420 queue->flags &= ~UVC_QUEUE_STREAMING;
424 mutex_unlock(&queue->mutex);
429 * Cancel the video buffers queue.
431 * Cancelling the queue marks all buffers on the irq queue as erroneous,
432 * wakes them up and remove them from the queue.
434 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
437 * This function acquires the irq spinlock and can be called from interrupt
440 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
442 struct uvc_buffer *buf;
445 spin_lock_irqsave(&queue->irqlock, flags);
446 while (!list_empty(&queue->irqqueue)) {
447 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
449 list_del(&buf->queue);
450 buf->state = UVC_BUF_STATE_ERROR;
453 /* This must be protected by the irqlock spinlock to avoid race
454 * conditions between uvc_queue_buffer and the disconnection event that
455 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
456 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
457 * state outside the queue code.
460 queue->flags |= UVC_QUEUE_DISCONNECTED;
461 spin_unlock_irqrestore(&queue->irqlock, flags);
464 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
465 struct uvc_buffer *buf)
467 struct uvc_buffer *nextbuf;
470 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
471 buf->buf.length != buf->buf.bytesused) {
472 buf->state = UVC_BUF_STATE_QUEUED;
473 buf->buf.bytesused = 0;
477 spin_lock_irqsave(&queue->irqlock, flags);
478 list_del(&buf->queue);
479 if (!list_empty(&queue->irqqueue))
480 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
484 spin_unlock_irqrestore(&queue->irqlock, flags);
486 buf->buf.sequence = queue->sequence++;
487 do_gettimeofday(&buf->buf.timestamp);