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/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
82 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 * Free the video buffers.
94 * This function must be called with the queue lock held.
96 static int uvc_free_buffers(struct uvc_video_queue *queue)
100 for (i = 0; i < queue->count; ++i) {
101 if (queue->buffer[i].vma_use_count != 0)
114 * Allocate the video buffers.
116 * Pages are reserved to make sure they will not be swapped, as they will be
117 * filled in the URB completion handler.
119 * Buffers will be individually mapped, so they must all be page aligned.
122 uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
123 unsigned int buflength)
125 unsigned int bufsize = PAGE_ALIGN(buflength);
130 if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
131 nbuffers = UVC_MAX_VIDEO_BUFFERS;
133 mutex_lock(&queue->mutex);
135 if ((ret = uvc_free_buffers(queue)) < 0)
138 /* Bail out if no buffers should be allocated. */
142 /* Decrement the number of buffers until allocation succeeds. */
143 for (; nbuffers > 0; --nbuffers) {
144 mem = vmalloc_32(nbuffers * bufsize);
154 for (i = 0; i < nbuffers; ++i) {
155 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
156 queue->buffer[i].buf.index = i;
157 queue->buffer[i].buf.m.offset = i * bufsize;
158 queue->buffer[i].buf.length = buflength;
159 queue->buffer[i].buf.type = queue->type;
160 queue->buffer[i].buf.sequence = 0;
161 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
162 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
163 queue->buffer[i].buf.flags = 0;
164 init_waitqueue_head(&queue->buffer[i].wait);
168 queue->count = nbuffers;
169 queue->buf_size = bufsize;
173 mutex_unlock(&queue->mutex);
177 static void __uvc_query_buffer(struct uvc_buffer *buf,
178 struct v4l2_buffer *v4l2_buf)
180 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
182 if (buf->vma_use_count)
183 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
185 switch (buf->state) {
186 case UVC_BUF_STATE_ERROR:
187 case UVC_BUF_STATE_DONE:
188 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
190 case UVC_BUF_STATE_QUEUED:
191 case UVC_BUF_STATE_ACTIVE:
192 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
194 case UVC_BUF_STATE_IDLE:
201 uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf)
205 mutex_lock(&queue->mutex);
206 if (v4l2_buf->index >= queue->count) {
211 __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
214 mutex_unlock(&queue->mutex);
219 * Queue a video buffer. Attempting to queue a buffer that has already been
220 * queued will return -EINVAL.
223 uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf)
225 struct uvc_buffer *buf;
229 uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
231 if (v4l2_buf->type != queue->type ||
232 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
233 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
234 "and/or memory (%u).\n", v4l2_buf->type,
239 mutex_lock(&queue->mutex);
240 if (v4l2_buf->index >= queue->count) {
241 uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
246 buf = &queue->buffer[v4l2_buf->index];
247 if (buf->state != UVC_BUF_STATE_IDLE) {
248 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
249 "(%u).\n", buf->state);
254 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
255 v4l2_buf->bytesused > buf->buf.length) {
256 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
261 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
262 buf->buf.bytesused = 0;
264 buf->buf.bytesused = v4l2_buf->bytesused;
266 spin_lock_irqsave(&queue->irqlock, flags);
267 if (queue->flags & UVC_QUEUE_DISCONNECTED) {
268 spin_unlock_irqrestore(&queue->irqlock, flags);
272 buf->state = UVC_BUF_STATE_QUEUED;
274 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
275 queue->flags &= ~UVC_QUEUE_PAUSED;
277 list_add_tail(&buf->stream, &queue->mainqueue);
278 list_add_tail(&buf->queue, &queue->irqqueue);
279 spin_unlock_irqrestore(&queue->irqlock, flags);
282 mutex_unlock(&queue->mutex);
286 static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
289 return (buf->state != UVC_BUF_STATE_QUEUED &&
290 buf->state != UVC_BUF_STATE_ACTIVE)
294 return wait_event_interruptible(buf->wait,
295 buf->state != UVC_BUF_STATE_QUEUED &&
296 buf->state != UVC_BUF_STATE_ACTIVE);
300 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
304 uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf,
307 struct uvc_buffer *buf;
310 if (v4l2_buf->type != queue->type ||
311 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
312 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
313 "and/or memory (%u).\n", v4l2_buf->type,
318 mutex_lock(&queue->mutex);
319 if (list_empty(&queue->mainqueue)) {
320 uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
325 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
326 if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
329 uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
330 buf->buf.index, buf->state, buf->buf.bytesused);
332 switch (buf->state) {
333 case UVC_BUF_STATE_ERROR:
334 uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
335 "(transmission error).\n");
337 case UVC_BUF_STATE_DONE:
338 buf->state = UVC_BUF_STATE_IDLE;
341 case UVC_BUF_STATE_IDLE:
342 case UVC_BUF_STATE_QUEUED:
343 case UVC_BUF_STATE_ACTIVE:
345 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
346 "(driver bug?).\n", buf->state);
351 list_del(&buf->stream);
352 __uvc_query_buffer(buf, v4l2_buf);
355 mutex_unlock(&queue->mutex);
360 * Poll the video queue.
362 * This function implements video queue polling and is intended to be used by
363 * the device poll handler.
366 uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
369 struct uvc_buffer *buf;
370 unsigned int mask = 0;
372 mutex_lock(&queue->mutex);
373 if (list_empty(&queue->mainqueue))
376 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
378 poll_wait(file, &buf->wait, wait);
379 if (buf->state == UVC_BUF_STATE_DONE ||
380 buf->state == UVC_BUF_STATE_ERROR)
381 mask |= POLLOUT | POLLWRNORM;
384 mutex_unlock(&queue->mutex);
391 static void uvc_vm_open(struct vm_area_struct *vma)
393 struct uvc_buffer *buffer = vma->vm_private_data;
394 buffer->vma_use_count++;
397 static void uvc_vm_close(struct vm_area_struct *vma)
399 struct uvc_buffer *buffer = vma->vm_private_data;
400 buffer->vma_use_count--;
403 static struct vm_operations_struct uvc_vm_ops = {
405 .close = uvc_vm_close,
409 * Memory-map a buffer.
411 * This function implements video buffer memory mapping and is intended to be
412 * used by the device mmap handler.
415 uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
417 struct uvc_buffer *uninitialized_var(buffer);
419 unsigned long addr, start, size;
423 start = vma->vm_start;
424 size = vma->vm_end - vma->vm_start;
426 mutex_lock(&queue->mutex);
428 for (i = 0; i < queue->count; ++i) {
429 buffer = &queue->buffer[i];
430 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
434 if (i == queue->count || size != queue->buf_size) {
440 * VM_IO marks the area as being an mmaped region for I/O to a
441 * device. It also prevents the region from being core dumped.
443 vma->vm_flags |= VM_IO;
445 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
447 page = vmalloc_to_page((void *)addr);
448 if ((ret = vm_insert_page(vma, start, page)) < 0)
456 vma->vm_ops = &uvc_vm_ops;
457 vma->vm_private_data = buffer;
461 mutex_unlock(&queue->mutex);
466 * Cancel the video buffers queue.
468 * Cancelling the queue marks all buffers on the irq queue as erroneous,
469 * wakes them up and removes them from the queue.
471 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
474 * This function acquires the irq spinlock and can be called from interrupt
477 static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
479 struct uvc_buffer *buf;
482 spin_lock_irqsave(&queue->irqlock, flags);
483 while (!list_empty(&queue->irqqueue)) {
484 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
486 list_del(&buf->queue);
487 buf->state = UVC_BUF_STATE_ERROR;
490 /* This must be protected by the irqlock spinlock to avoid race
491 * conditions between uvc_queue_buffer and the disconnection event that
492 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
493 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
494 * state outside the queue code.
497 queue->flags |= UVC_QUEUE_DISCONNECTED;
498 spin_unlock_irqrestore(&queue->irqlock, flags);
502 * Enable or disable the video buffers queue.
504 * The queue must be enabled before starting video acquisition and must be
505 * disabled after stopping it. This ensures that the video buffers queue
506 * state can be properly initialized before buffers are accessed from the
509 * Enabling the video queue initializes parameters (such as sequence number,
510 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
512 * Disabling the video queue cancels the queue and removes all buffers from
515 * This function can't be called from interrupt context. Use
516 * uvc_queue_cancel() instead.
518 static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
523 mutex_lock(&queue->mutex);
525 if (uvc_queue_streaming(queue)) {
530 queue->flags |= UVC_QUEUE_STREAMING;
533 uvc_queue_cancel(queue, 0);
534 INIT_LIST_HEAD(&queue->mainqueue);
536 for (i = 0; i < queue->count; ++i)
537 queue->buffer[i].state = UVC_BUF_STATE_IDLE;
539 queue->flags &= ~UVC_QUEUE_STREAMING;
543 mutex_unlock(&queue->mutex);
547 static struct uvc_buffer *
548 uvc_queue_next_buffer(struct uvc_video_queue *queue, struct uvc_buffer *buf)
550 struct uvc_buffer *nextbuf;
553 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
554 buf->buf.length != buf->buf.bytesused) {
555 buf->state = UVC_BUF_STATE_QUEUED;
556 buf->buf.bytesused = 0;
560 spin_lock_irqsave(&queue->irqlock, flags);
561 list_del(&buf->queue);
562 if (!list_empty(&queue->irqqueue))
563 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
567 spin_unlock_irqrestore(&queue->irqlock, flags);
569 buf->buf.sequence = queue->sequence++;
570 do_gettimeofday(&buf->buf.timestamp);
576 static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
578 struct uvc_buffer *buf = NULL;
580 if (!list_empty(&queue->irqqueue))
581 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
584 queue->flags |= UVC_QUEUE_PAUSED;