blob: 32c18229863bb38f8cf41687321876260020dddc [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_queue.c -- USB Video Class driver - Buffers management
3 *
Laurent Pinchart11fc5ba2010-09-20 06:10:10 -03004 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03006 *
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.
11 *
12 */
13
14#include <linux/kernel.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070015#include <linux/mm.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030016#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>
23
24#include "uvcvideo.h"
25
26/* ------------------------------------------------------------------------
27 * Video buffers queue management.
28 *
29 * Video queues is initialized by uvc_queue_init(). The function performs
30 * basic initialization of the uvc_video_queue struct and never fails.
31 *
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.
37 *
38 * Video buffers are managed using two queues. However, unlike most USB video
Laurent Pinchart2c2d2642009-01-03 19:12:40 -030039 * 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
43 * contexts.
Laurent Pinchartc0efd232008-06-30 15:04:50 -030044 *
45 * Use cases
46 * ---------
47 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -030048 * Unless stated otherwise, all operations that modify the irq buffers queue
Laurent Pinchartc0efd232008-06-30 15:04:50 -030049 * are protected by the irq spinlock.
50 *
51 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
52 *
53 * The buffers are added to the main and irq queues. Both operations are
Laurent Pinchart2c2d2642009-01-03 19:12:40 -030054 * protected by the queue lock, and the later is protected by the irq
Laurent Pinchartc0efd232008-06-30 15:04:50 -030055 * spinlock as well.
56 *
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.
60 *
61 * When the buffer is full, the completion handler removes it from the irq
Laurent Pinchartd7c0d432009-12-16 21:20:45 -030062 * queue, marks it as done (UVC_BUF_STATE_DONE) and wakes its wait queue.
Laurent Pinchartc0efd232008-06-30 15:04:50 -030063 * At that point, any process waiting on the buffer will be woken up. If a
Laurent Pinchartd7c0d432009-12-16 21:20:45 -030064 * process tries to dequeue a buffer after it has been marked done, the
Laurent Pinchartc0efd232008-06-30 15:04:50 -030065 * dequeing will succeed immediately.
66 *
67 * 2. Buffers are queued, user is waiting on a buffer and the device gets
68 * disconnected.
69 *
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.
74 *
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
77 * immediately.
78 *
79 */
80
Laurent Pinchart9bde9f22010-06-17 06:52:37 -030081void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
82 int drop_corrupted)
Laurent Pinchartc0efd232008-06-30 15:04:50 -030083{
84 mutex_init(&queue->mutex);
85 spin_lock_init(&queue->irqlock);
86 INIT_LIST_HEAD(&queue->mainqueue);
87 INIT_LIST_HEAD(&queue->irqqueue);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -030088 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
Laurent Pinchartff924202008-12-28 22:32:29 -030089 queue->type = type;
Laurent Pinchartc0efd232008-06-30 15:04:50 -030090}
91
92/*
Laurent Pinchart8e815e172010-11-21 14:46:44 -030093 * Free the video buffers.
94 *
95 * This function must be called with the queue lock held.
96 */
97static int __uvc_free_buffers(struct uvc_video_queue *queue)
98{
99 unsigned int i;
100
101 for (i = 0; i < queue->count; ++i) {
102 if (queue->buffer[i].vma_use_count != 0)
103 return -EBUSY;
104 }
105
106 if (queue->count) {
107 vfree(queue->mem);
108 queue->count = 0;
109 }
110
111 return 0;
112}
113
114int uvc_free_buffers(struct uvc_video_queue *queue)
115{
116 int ret;
117
118 mutex_lock(&queue->mutex);
119 ret = __uvc_free_buffers(queue);
120 mutex_unlock(&queue->mutex);
121
122 return ret;
123}
124
125/*
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300126 * Allocate the video buffers.
127 *
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300128 * Pages are reserved to make sure they will not be swapped, as they will be
129 * filled in the URB completion handler.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300130 *
131 * Buffers will be individually mapped, so they must all be page aligned.
132 */
133int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
134 unsigned int buflength)
135{
136 unsigned int bufsize = PAGE_ALIGN(buflength);
137 unsigned int i;
138 void *mem = NULL;
139 int ret;
140
141 if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
142 nbuffers = UVC_MAX_VIDEO_BUFFERS;
143
144 mutex_lock(&queue->mutex);
145
Laurent Pinchart8e815e172010-11-21 14:46:44 -0300146 if ((ret = __uvc_free_buffers(queue)) < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300147 goto done;
148
149 /* Bail out if no buffers should be allocated. */
150 if (nbuffers == 0)
151 goto done;
152
153 /* Decrement the number of buffers until allocation succeeds. */
154 for (; nbuffers > 0; --nbuffers) {
155 mem = vmalloc_32(nbuffers * bufsize);
156 if (mem != NULL)
157 break;
158 }
159
160 if (mem == NULL) {
161 ret = -ENOMEM;
162 goto done;
163 }
164
165 for (i = 0; i < nbuffers; ++i) {
166 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
167 queue->buffer[i].buf.index = i;
168 queue->buffer[i].buf.m.offset = i * bufsize;
169 queue->buffer[i].buf.length = buflength;
Laurent Pinchartff924202008-12-28 22:32:29 -0300170 queue->buffer[i].buf.type = queue->type;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300171 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
172 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
173 queue->buffer[i].buf.flags = 0;
174 init_waitqueue_head(&queue->buffer[i].wait);
175 }
176
177 queue->mem = mem;
178 queue->count = nbuffers;
179 queue->buf_size = bufsize;
180 ret = nbuffers;
181
182done:
183 mutex_unlock(&queue->mutex);
184 return ret;
185}
186
187/*
Laurent Pinchart23ff6042009-06-04 09:26:39 -0300188 * Check if buffers have been allocated.
189 */
190int uvc_queue_allocated(struct uvc_video_queue *queue)
191{
192 int allocated;
193
194 mutex_lock(&queue->mutex);
195 allocated = queue->count != 0;
196 mutex_unlock(&queue->mutex);
197
198 return allocated;
199}
200
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300201static void __uvc_query_buffer(struct uvc_buffer *buf,
202 struct v4l2_buffer *v4l2_buf)
203{
204 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
205
206 if (buf->vma_use_count)
207 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
208
209 switch (buf->state) {
210 case UVC_BUF_STATE_ERROR:
211 case UVC_BUF_STATE_DONE:
212 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
213 break;
214 case UVC_BUF_STATE_QUEUED:
215 case UVC_BUF_STATE_ACTIVE:
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300216 case UVC_BUF_STATE_READY:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300217 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
218 break;
219 case UVC_BUF_STATE_IDLE:
220 default:
221 break;
222 }
223}
224
225int uvc_query_buffer(struct uvc_video_queue *queue,
226 struct v4l2_buffer *v4l2_buf)
227{
228 int ret = 0;
229
230 mutex_lock(&queue->mutex);
231 if (v4l2_buf->index >= queue->count) {
232 ret = -EINVAL;
233 goto done;
234 }
235
236 __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
237
238done:
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300239 mutex_unlock(&queue->mutex);
240 return ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300241}
242
243/*
244 * Queue a video buffer. Attempting to queue a buffer that has already been
245 * queued will return -EINVAL.
246 */
247int uvc_queue_buffer(struct uvc_video_queue *queue,
248 struct v4l2_buffer *v4l2_buf)
249{
250 struct uvc_buffer *buf;
251 unsigned long flags;
252 int ret = 0;
253
254 uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
255
Laurent Pinchartff924202008-12-28 22:32:29 -0300256 if (v4l2_buf->type != queue->type ||
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300257 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
258 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
259 "and/or memory (%u).\n", v4l2_buf->type,
260 v4l2_buf->memory);
261 return -EINVAL;
262 }
263
264 mutex_lock(&queue->mutex);
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300265 if (v4l2_buf->index >= queue->count) {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300266 uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
267 ret = -EINVAL;
268 goto done;
269 }
270
271 buf = &queue->buffer[v4l2_buf->index];
272 if (buf->state != UVC_BUF_STATE_IDLE) {
273 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
274 "(%u).\n", buf->state);
275 ret = -EINVAL;
276 goto done;
277 }
278
Laurent Pinchartff924202008-12-28 22:32:29 -0300279 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
280 v4l2_buf->bytesused > buf->buf.length) {
281 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
282 ret = -EINVAL;
283 goto done;
284 }
285
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300286 spin_lock_irqsave(&queue->irqlock, flags);
287 if (queue->flags & UVC_QUEUE_DISCONNECTED) {
288 spin_unlock_irqrestore(&queue->irqlock, flags);
289 ret = -ENODEV;
290 goto done;
291 }
292 buf->state = UVC_BUF_STATE_QUEUED;
Laurent Pinchartff924202008-12-28 22:32:29 -0300293 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
294 buf->buf.bytesused = 0;
295 else
296 buf->buf.bytesused = v4l2_buf->bytesused;
297
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300298 list_add_tail(&buf->stream, &queue->mainqueue);
299 list_add_tail(&buf->queue, &queue->irqqueue);
300 spin_unlock_irqrestore(&queue->irqlock, flags);
301
302done:
303 mutex_unlock(&queue->mutex);
304 return ret;
305}
306
307static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
308{
309 if (nonblocking) {
310 return (buf->state != UVC_BUF_STATE_QUEUED &&
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300311 buf->state != UVC_BUF_STATE_ACTIVE &&
312 buf->state != UVC_BUF_STATE_READY)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300313 ? 0 : -EAGAIN;
314 }
315
316 return wait_event_interruptible(buf->wait,
317 buf->state != UVC_BUF_STATE_QUEUED &&
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300318 buf->state != UVC_BUF_STATE_ACTIVE &&
319 buf->state != UVC_BUF_STATE_READY);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300320}
321
322/*
323 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
324 * available.
325 */
326int uvc_dequeue_buffer(struct uvc_video_queue *queue,
327 struct v4l2_buffer *v4l2_buf, int nonblocking)
328{
329 struct uvc_buffer *buf;
330 int ret = 0;
331
Laurent Pinchartff924202008-12-28 22:32:29 -0300332 if (v4l2_buf->type != queue->type ||
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300333 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
334 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
335 "and/or memory (%u).\n", v4l2_buf->type,
336 v4l2_buf->memory);
337 return -EINVAL;
338 }
339
340 mutex_lock(&queue->mutex);
341 if (list_empty(&queue->mainqueue)) {
342 uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
343 ret = -EINVAL;
344 goto done;
345 }
346
347 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
348 if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
349 goto done;
350
351 uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
352 buf->buf.index, buf->state, buf->buf.bytesused);
353
354 switch (buf->state) {
355 case UVC_BUF_STATE_ERROR:
356 uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
357 "(transmission error).\n");
358 ret = -EIO;
359 case UVC_BUF_STATE_DONE:
360 buf->state = UVC_BUF_STATE_IDLE;
361 break;
362
363 case UVC_BUF_STATE_IDLE:
364 case UVC_BUF_STATE_QUEUED:
365 case UVC_BUF_STATE_ACTIVE:
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300366 case UVC_BUF_STATE_READY:
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300367 default:
368 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
369 "(driver bug?).\n", buf->state);
370 ret = -EINVAL;
371 goto done;
372 }
373
374 list_del(&buf->stream);
375 __uvc_query_buffer(buf, v4l2_buf);
376
377done:
378 mutex_unlock(&queue->mutex);
379 return ret;
380}
381
382/*
383 * Poll the video queue.
384 *
385 * This function implements video queue polling and is intended to be used by
386 * the device poll handler.
387 */
388unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
389 poll_table *wait)
390{
391 struct uvc_buffer *buf;
392 unsigned int mask = 0;
393
394 mutex_lock(&queue->mutex);
395 if (list_empty(&queue->mainqueue)) {
396 mask |= POLLERR;
397 goto done;
398 }
399 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
400
401 poll_wait(file, &buf->wait, wait);
402 if (buf->state == UVC_BUF_STATE_DONE ||
Laurent Pincharted3de602010-03-31 12:29:26 -0300403 buf->state == UVC_BUF_STATE_ERROR) {
404 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
405 mask |= POLLIN | POLLRDNORM;
406 else
407 mask |= POLLOUT | POLLWRNORM;
408 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300409
410done:
411 mutex_unlock(&queue->mutex);
412 return mask;
413}
414
415/*
416 * Enable or disable the video buffers queue.
417 *
418 * The queue must be enabled before starting video acquisition and must be
419 * disabled after stopping it. This ensures that the video buffers queue
420 * state can be properly initialized before buffers are accessed from the
421 * interrupt handler.
422 *
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300423 * Enabling the video queue returns -EBUSY if the queue is already enabled.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300424 *
425 * Disabling the video queue cancels the queue and removes all buffers from
426 * the main queue.
427 *
428 * This function can't be called from interrupt context. Use
429 * uvc_queue_cancel() instead.
430 */
431int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
432{
433 unsigned int i;
434 int ret = 0;
435
436 mutex_lock(&queue->mutex);
437 if (enable) {
438 if (uvc_queue_streaming(queue)) {
439 ret = -EBUSY;
440 goto done;
441 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300442 queue->flags |= UVC_QUEUE_STREAMING;
Laurent Pinchartff924202008-12-28 22:32:29 -0300443 queue->buf_used = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300444 } else {
445 uvc_queue_cancel(queue, 0);
446 INIT_LIST_HEAD(&queue->mainqueue);
447
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300448 for (i = 0; i < queue->count; ++i) {
449 queue->buffer[i].error = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300450 queue->buffer[i].state = UVC_BUF_STATE_IDLE;
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300451 }
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300452
453 queue->flags &= ~UVC_QUEUE_STREAMING;
454 }
455
456done:
457 mutex_unlock(&queue->mutex);
458 return ret;
459}
460
461/*
462 * Cancel the video buffers queue.
463 *
464 * Cancelling the queue marks all buffers on the irq queue as erroneous,
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300465 * wakes them up and removes them from the queue.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300466 *
467 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
468 * fail with -ENODEV.
469 *
470 * This function acquires the irq spinlock and can be called from interrupt
471 * context.
472 */
473void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
474{
475 struct uvc_buffer *buf;
476 unsigned long flags;
477
478 spin_lock_irqsave(&queue->irqlock, flags);
479 while (!list_empty(&queue->irqqueue)) {
480 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
481 queue);
482 list_del(&buf->queue);
483 buf->state = UVC_BUF_STATE_ERROR;
484 wake_up(&buf->wait);
485 }
486 /* This must be protected by the irqlock spinlock to avoid race
487 * conditions between uvc_queue_buffer and the disconnection event that
488 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
489 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
490 * state outside the queue code.
491 */
492 if (disconnect)
493 queue->flags |= UVC_QUEUE_DISCONNECTED;
494 spin_unlock_irqrestore(&queue->irqlock, flags);
495}
496
497struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
498 struct uvc_buffer *buf)
499{
500 struct uvc_buffer *nextbuf;
501 unsigned long flags;
502
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300503 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
504 buf->error = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300505 buf->state = UVC_BUF_STATE_QUEUED;
506 buf->buf.bytesused = 0;
507 return buf;
508 }
509
510 spin_lock_irqsave(&queue->irqlock, flags);
511 list_del(&buf->queue);
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300512 buf->error = 0;
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300513 buf->state = UVC_BUF_STATE_DONE;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300514 if (!list_empty(&queue->irqqueue))
515 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
516 queue);
517 else
518 nextbuf = NULL;
519 spin_unlock_irqrestore(&queue->irqlock, flags);
520
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300521 wake_up(&buf->wait);
522 return nextbuf;
523}
Hans Verkuilf87086e2008-07-18 00:50:58 -0300524