Restores USB audio functionality of SN9C202 webcams
[microdia.git] / microdia-queue.c
blob0f78b237c52197eb1dcc630a44b28772597cc037
1 /**
2 * @file microdia-queue.c
3 * @author Brian Johnson
5 * @brief Buffer management
7 * @note Original code from UVC webcam driver
8 * @note Copyright (C) 2005-2008 Laurent Pinchart (laurent.pinchart@skynet.be)
9 * @note Copyright (C) 2008 Brian Johnson (brijohn@gmail.com)
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * ------------------------------------------------------------------------
18 * Video buffers queue management.
20 * Video queues is initialized by microdia_queue_init(). The function performs
21 * basic initialization of the microdia_video_queue struct and never fails.
23 * Video buffer allocation and freeing are performed by microdia_alloc_buffers()
24 * and microdia_free_buffers() respectively. The former acquires the video queue
25 * lock, while the later must be called with the lock held (so that allocation
26 * can free previously allocated buffers). Trying to free buffers that are
27 * mapped to user space will return -EBUSY.
29 * Video buffers are managed using two queues. However, unlike most USB video
30 * drivers which use an in queue and an out queue, we use a main queue which
31 * holds all queued buffers (both 'empty' and 'done' buffers), and an irq
32 * queue which holds empty buffers. This design (copied from video-buf)
33 * minimizes locking in interrupt, as only one queue is shared between
34 * interrupt and user contexts.
36 * Use cases
37 * ---------
39 * Unless stated otherwise, all operations which modify the irq buffers queue
40 * are protected by the irq spinlock.
42 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
44 * The buffers are added to the main and irq queues. Both operations are
45 * protected by the queue lock, and the latert is protected by the irq
46 * spinlock as well.
48 * The completion handler fetches a buffer from the irq queue and fills it
49 * with video data. If no buffer is available (irq queue empty), the handler
50 * returns immediately.
52 * When the buffer is full, the completion handler removes it from the irq
53 * queue, marks it as ready (MICRODIA_BUF_STATE_DONE) and wake its wait
54 * queue. At that point, any process waiting on the buffer will be woken up.
55 * If a process tries to dequeue a buffer after it has been marked ready,
56 * the dequeing will succeed immediately.
58 * 2. Buffers are queued, user is waiting on a buffer and the device gets
59 * disconnected.
61 * When the device is disconnected, the kernel calls the completion handler
62 * with an appropriate status code. The handler marks all buffers in the
63 * irq queue as being erroneous (MICRODIA_BUF_STATE_ERROR) and wakes them up
64 * so that any process waiting on a buffer gets woken up.
66 * Waking up up the first buffer on the irq list is not enough, as the
67 * process waiting on the buffer might restart the dequeue operation
68 * immediately.
72 #include <linux/kernel.h>
73 #include <linux/version.h>
74 #include <linux/list.h>
75 #include <linux/module.h>
76 #include <linux/usb.h>
77 #include <linux/videodev.h>
78 #include <linux/vmalloc.h>
79 #include <linux/wait.h>
80 #include <linux/mm.h>
81 #include <asm/atomic.h>
83 #include "microdia.h"
85 /**
86 * @param queue
89 void microdia_queue_init(struct microdia_video_queue *queue)
91 mutex_init(&queue->mutex);
92 spin_lock_init(&queue->irqlock);
93 INIT_LIST_HEAD(&queue->mainqueue);
94 INIT_LIST_HEAD(&queue->irqqueue);
97 /**
98 * @brief Allocate the video buffers.
100 * @param queue
101 * @param nbuffers
102 * @param buflength
104 * Pages are reserved to make sure they will not be swaped, as they will be
105 * filled in URB completion handler.
107 * Buffers will be individually mapped, so they must all be page aligned.
109 int microdia_alloc_buffers(struct microdia_video_queue *queue,
110 unsigned int nbuffers, unsigned int buflength)
112 unsigned int bufsize = PAGE_ALIGN(buflength);
113 unsigned int i;
114 void *mem = NULL;
115 void *buffer = NULL;
116 int ret;
118 mutex_lock(&queue->mutex);
120 ret = microdia_free_buffers(queue);
121 if (ret < 0)
122 goto done;
124 /* Bail out if no buffers should be allocated. */
125 if (nbuffers == 0)
126 goto done;
128 if (nbuffers < queue->min_buffers)
129 nbuffers = queue->min_buffers;
130 else if (nbuffers > queue->max_buffers)
131 nbuffers = queue->max_buffers;
133 queue->scratch = vmalloc_32(bufsize);
135 if (queue->scratch == NULL) {
136 ret = -ENOMEM;
137 goto done;
140 /* Decrement the number of buffers until allocation succeeds. */
141 for (; nbuffers >= queue->min_buffers; --nbuffers) {
142 mem = vmalloc_32(nbuffers * bufsize);
143 buffer = kzalloc(nbuffers * sizeof(struct microdia_buffer),
144 GFP_KERNEL);
145 if (mem != NULL && buffer != NULL)
146 break;
147 if (mem != NULL)
148 vfree(mem);
149 if (buffer != NULL)
150 kfree(buffer);
153 if (mem == NULL || buffer == NULL) {
154 ret = -ENOMEM;
155 vfree(queue->scratch);
156 goto done;
158 queue->buffer = buffer;
160 for (i = 0; i < nbuffers; ++i) {
161 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
162 queue->buffer[i].buf.index = i;
163 queue->buffer[i].buf.m.offset = i * bufsize;
164 queue->buffer[i].buf.length = buflength;
165 queue->buffer[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
166 queue->buffer[i].buf.sequence = 0;
167 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
168 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
169 queue->buffer[i].buf.flags = 0;
170 init_waitqueue_head(&queue->buffer[i].wait);
173 queue->mem = mem;
174 queue->count = nbuffers;
175 queue->buf_size = bufsize;
176 ret = nbuffers;
178 done:
179 mutex_unlock(&queue->mutex);
180 return ret;
184 * @brief Free the video buffers.
186 * @param queue
188 * This function must be called with the queue lock held.
190 int microdia_free_buffers(struct microdia_video_queue *queue)
192 unsigned int i;
193 UDIA_INFO("Freeing %d v4l2 buffers\n", queue->count);
195 for (i = 0; i < queue->count; ++i) {
196 if (queue->buffer[i].vma_use_count != 0)
197 return -EBUSY;
200 if (queue->count) {
201 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 15)
202 unsigned long addr = (unsigned long)queue->mem;
203 size_t size = queue->count * queue->buf_size;
204 while (size > 0) {
205 ClearPageReserved(vmalloc_to_page((void *)addr));
206 addr += PAGE_SIZE;
207 size -= PAGE_SIZE;
209 #endif
210 vfree(queue->mem);
211 vfree(queue->scratch);
212 kfree(queue->buffer);
213 INIT_LIST_HEAD(&queue->mainqueue);
214 INIT_LIST_HEAD(&queue->irqqueue);
215 queue->count = 0;
218 return 0;
222 * @param buf
223 * @param v4l2_buf
226 static void __microdia_query_buffer(struct microdia_buffer *buf,
227 struct v4l2_buffer *v4l2_buf)
229 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
231 if (buf->vma_use_count)
232 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
234 switch (buf->state) {
235 case MICRODIA_BUF_STATE_ERROR:
236 case MICRODIA_BUF_STATE_DONE:
237 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
238 break;
239 case MICRODIA_BUF_STATE_QUEUED:
240 case MICRODIA_BUF_STATE_ACTIVE:
241 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
242 break;
243 case MICRODIA_BUF_STATE_IDLE:
244 default:
245 break;
250 * @param queue
251 * @param v4l2_buf
253 * @return 0 or negative error code
256 int microdia_query_buffer(struct microdia_video_queue *queue,
257 struct v4l2_buffer *v4l2_buf)
259 int ret = 0;
261 mutex_lock(&queue->mutex);
262 if (v4l2_buf->index >= queue->count) {
263 ret = -EINVAL;
264 goto done;
267 __microdia_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
269 done:
270 mutex_unlock(&queue->mutex);
271 return ret;
275 * @brief Queue a video buffer.
277 * @param queue
278 * @param v4l2_buf
280 * @return 0 or negative error code
282 * Attempting to queue a buffer that has already been
283 * queued will return -EINVAL.
285 int microdia_queue_buffer(struct microdia_video_queue *queue,
286 struct v4l2_buffer *v4l2_buf)
288 struct microdia_buffer *buf;
289 unsigned long flags;
290 int ret = 0;
292 UDIA_DEBUG("Queuing buffer %u.\n", v4l2_buf->index);
294 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
295 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
296 UDIA_ERROR("[E] Invalid buffer type (%u) "
297 "and/or memory (%u).\n", v4l2_buf->type,
298 v4l2_buf->memory);
299 return -EINVAL;
302 mutex_lock(&queue->mutex);
303 if (v4l2_buf->index >= queue->count) {
304 UDIA_ERROR("[E] Out of range index.\n");
305 ret = -EINVAL;
306 goto done;
309 buf = &queue->buffer[v4l2_buf->index];
310 if (buf->state != MICRODIA_BUF_STATE_IDLE) {
311 UDIA_ERROR("[E] Invalid buffer state "
312 "(%u).\n", buf->state);
313 ret = -EINVAL;
314 goto done;
317 buf->state = MICRODIA_BUF_STATE_QUEUED;
318 buf->buf.bytesused = 0;
319 list_add_tail(&buf->stream, &queue->mainqueue);
320 spin_lock_irqsave(&queue->irqlock, flags);
321 list_add_tail(&buf->queue, &queue->irqqueue);
322 spin_unlock_irqrestore(&queue->irqlock, flags);
324 done:
325 mutex_unlock(&queue->mutex);
326 return ret;
330 * @param buf
331 * @param nonblocking
334 static int microdia_queue_waiton(struct microdia_buffer *buf, int nonblocking)
336 if (nonblocking) {
337 return (buf->state != MICRODIA_BUF_STATE_QUEUED &&
338 buf->state != MICRODIA_BUF_STATE_ACTIVE)
339 ? 0 : -EAGAIN;
342 return wait_event_interruptible(buf->wait,
343 buf->state != MICRODIA_BUF_STATE_QUEUED &&
344 buf->state != MICRODIA_BUF_STATE_ACTIVE);
348 * @brief Dequeue a video buffer.
350 * @param queue
351 * @param v4l2_buf
352 * @param nonblocking
354 * @return 0 or negative error code
356 * If nonblocking is false, block until a buffer is
357 * available.
359 int microdia_dequeue_buffer(struct microdia_video_queue *queue,
360 struct v4l2_buffer *v4l2_buf, int nonblocking)
362 struct microdia_buffer *buf;
363 int ret = 0;
365 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
366 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
367 UDIA_ERROR("[E] Invalid buffer type (%u) "
368 "and/or memory (%u).\n", v4l2_buf->type,
369 v4l2_buf->memory);
370 return -EINVAL;
373 mutex_lock(&queue->mutex);
374 if (list_empty(&queue->mainqueue)) {
375 UDIA_ERROR("[E] Empty buffer queue.\n");
376 ret = -EINVAL;
377 goto done;
380 buf = list_first_entry(&queue->mainqueue, struct microdia_buffer,
381 stream);
383 ret = microdia_queue_waiton(buf, nonblocking);
384 if (ret < 0)
385 goto done;
387 UDIA_DEBUG("Dequeuing buffer %u (%u, %u bytes).\n",
388 buf->buf.index, buf->state, buf->buf.bytesused);
390 switch (buf->state) {
391 case MICRODIA_BUF_STATE_ERROR:
392 UDIA_WARNING("[W] Corrupted data (transmission error).\n");
393 ret = -EIO;
394 case MICRODIA_BUF_STATE_DONE:
395 buf->state = MICRODIA_BUF_STATE_IDLE;
396 break;
398 case MICRODIA_BUF_STATE_IDLE:
399 case MICRODIA_BUF_STATE_QUEUED:
400 case MICRODIA_BUF_STATE_ACTIVE:
401 default:
402 UDIA_ERROR("[E] Invalid buffer state %u "
403 "(driver bug?).\n", buf->state);
404 ret = -EINVAL;
405 goto done;
408 list_del(&buf->stream);
409 __microdia_query_buffer(buf, v4l2_buf);
411 done:
412 mutex_unlock(&queue->mutex);
413 return ret;
417 * @brief Poll the video queue.
419 * @param queue
420 * @param file
421 * @param wait
423 * This function implements video queue polling and is intended to be used by
424 * the device poll handler.
426 unsigned int microdia_queue_poll(struct microdia_video_queue *queue,
427 struct file *file, poll_table *wait)
429 struct microdia_buffer *buf;
430 unsigned int mask = 0;
432 mutex_lock(&queue->mutex);
433 if (list_empty(&queue->mainqueue)) {
434 mask |= POLLERR;
435 goto done;
437 buf = list_first_entry(&queue->mainqueue, struct microdia_buffer,
438 stream);
440 poll_wait(file, &buf->wait, wait);
441 if (buf->state == MICRODIA_BUF_STATE_DONE ||
442 buf->state == MICRODIA_BUF_STATE_ERROR)
443 mask |= POLLIN | POLLRDNORM;
445 done:
446 mutex_unlock(&queue->mutex);
447 return mask;
451 * @brief Enable or disable the video buffers queue.
453 * @param queue
454 * @param enable
456 * @return 0 or negative error code
458 * The queue must be enabled before starting video acquisition and must be
459 * disabled after stopping it. This ensures that the video buffers queue
460 * state can be properly initialized before buffers are accessed from the
461 * interrupt handler.
463 * Enabling the video queue initializes parameters (such as sequence number,
464 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
466 * Disabling the video queue cancels the queue and removes all buffers from
467 * the main queue.
469 * This function can't be called from interrupt context. Use
470 * microdia_queue_cancel() instead.
472 int microdia_queue_enable(struct microdia_video_queue *queue, int enable)
474 unsigned int i;
475 int ret = 0;
477 mutex_lock(&queue->mutex);
478 if (enable) {
479 if (queue->streaming) {
480 ret = -EBUSY;
481 goto done;
483 queue->sequence = 0;
484 queue->streaming = 1;
485 } else {
486 microdia_queue_cancel(queue);
487 INIT_LIST_HEAD(&queue->mainqueue);
489 for (i = 0; i < queue->count; ++i)
490 queue->buffer[i].state = MICRODIA_BUF_STATE_IDLE;
492 queue->streaming = 0;
495 done:
496 mutex_unlock(&queue->mutex);
497 return ret;
501 * @brief Cancel the video buffers queue.
503 * @param queue
505 * Cancelling the queue marks all buffers on the irq queue as erroneous,
506 * wakes them up and remove them from the queue.
508 * This function acquires the irq spinlock and can be called from interrupt
509 * context.
511 void microdia_queue_cancel(struct microdia_video_queue *queue)
513 struct microdia_buffer *buf;
514 unsigned long flags;
516 spin_lock_irqsave(&queue->irqlock, flags);
517 while (!list_empty(&queue->irqqueue)) {
518 buf = list_first_entry(&queue->irqqueue, struct microdia_buffer,
519 queue);
520 list_del(&buf->queue);
521 buf->state = MICRODIA_BUF_STATE_ERROR;
522 wake_up(&buf->wait);
524 spin_unlock_irqrestore(&queue->irqlock, flags);
528 * @param queue
529 * @param buf
532 struct microdia_buffer *microdia_queue_next_buffer(
533 struct microdia_video_queue *queue,
534 struct microdia_buffer *buf)
536 struct microdia_buffer *nextbuf;
537 unsigned long flags;
539 if (queue->drop_incomplete && queue->frame_size != buf->buf.bytesused) {
540 buf->state = MICRODIA_BUF_STATE_QUEUED;
541 buf->buf.bytesused = 0;
542 return buf;
545 spin_lock_irqsave(&queue->irqlock, flags);
546 list_del(&buf->queue);
547 if (!list_empty(&queue->irqqueue))
548 nextbuf = list_first_entry(&queue->irqqueue,
549 struct microdia_buffer, queue);
550 else
551 nextbuf = NULL;
552 spin_unlock_irqrestore(&queue->irqlock, flags);
554 buf->buf.sequence = queue->sequence++;
555 do_gettimeofday(&buf->buf.timestamp);
557 wake_up(&buf->wait);
558 return nextbuf;