Correcting code style for the rest
[microdia.git] / microdia-queue.c
blobad56fc7aebe97a269c237808e6d4ccde9ad36f6e
1 /*
2 * microdia_queue.c -- Microdia Webcam driver - Buffers management
3 * Original code from UVC webcam driver
5 * Copyright (C) 2005-2008
6 * Laurent Pinchart (laurent.pinchart@skynet.be)
7 * Copyright (C) 2008
8 * Brian Johnson (brijohn@gmail.com)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/version.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/usb.h>
22 #include <linux/videodev.h>
23 #include <linux/vmalloc.h>
24 #include <linux/wait.h>
25 #include <asm/atomic.h>
27 #include "microdia.h"
29 /* ------------------------------------------------------------------------
30 * Video buffers queue management.
32 * Video queues is initialized by microdia_queue_init(). The function performs
33 * basic initialization of the microdia_video_queue struct and never fails.
35 * Video buffer allocation and freeing are performed by microdia_alloc_buffers
36 * and microdia_free_buffers respectively. The former acquires the video queue
37 * lock, while the later must be called with the lock held (so that allocation
38 * can free previously allocated buffers). Trying to free buffers that are
39 * mapped to user space will return -EBUSY.
41 * Video buffers are managed using two queues. However, unlike most USB video
42 * drivers which use an in queue and an out queue, we use a main queue which
43 * holds all queued buffers (both 'empty' and 'done' buffers), and an irq
44 * queue which holds empty buffers. This design (copied from video-buf)
45 * minimizes locking in interrupt, as only one queue is shared between
46 * interrupt and user contexts.
48 * Use cases
49 * ---------
51 * Unless stated otherwise, all operations which modify the irq buffers queue
52 * are protected by the irq spinlock.
54 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
56 * The buffers are added to the main and irq queues. Both operations are
57 * protected by the queue lock, and the latert is protected by the irq
58 * spinlock as well.
60 * The completion handler fetches a buffer from the irq queue and fills it
61 * with video data. If no buffer is available (irq queue empty), the handler
62 * returns immediately.
64 * When the buffer is full, the completion handler removes it from the irq
65 * queue, marks it as ready (MICRODIA_BUF_STATE_DONE) and wake its wait
66 * queue. At that point, any process waiting on the buffer will be woken up.
67 * If a process tries to dequeue a buffer after it has been marked ready,
68 * the dequeing will succeed immediately.
70 * 2. Buffers are queued, user is waiting on a buffer and the device gets
71 * disconnected.
73 * When the device is disconnected, the kernel calls the completion handler
74 * with an appropriate status code. The handler marks all buffers in the
75 * irq queue as being erroneous (MICRODIA_BUF_STATE_ERROR) and wakes them up
76 * so that any process waiting on a buffer gets woken up.
78 * Waking up up the first buffer on the irq list is not enough, as the
79 * process waiting on the buffer might restart the dequeue operation
80 * immediately.
84 void microdia_queue_init(struct microdia_video_queue *queue)
86 mutex_init(&queue->mutex);
87 spin_lock_init(&queue->irqlock);
88 INIT_LIST_HEAD(&queue->mainqueue);
89 INIT_LIST_HEAD(&queue->irqqueue);
93 * Allocate the video buffers.
95 * Pages are reserved to make sure they will not be swaped, as they will be
96 * filled in URB completion handler.
98 * Buffers will be individually mapped, so they must all be page aligned.
100 int microdia_alloc_buffers(struct microdia_video_queue *queue,
101 unsigned int nbuffers, unsigned int buflength)
103 unsigned int bufsize = PAGE_ALIGN(buflength);
104 unsigned int i;
105 void *mem = NULL;
106 void *buffer = NULL;
107 int ret;
109 mutex_lock(&queue->mutex);
111 ret = microdia_free_buffers(queue);
112 if (ret < 0)
113 goto done;
115 /* Bail out if no buffers should be allocated. */
116 if (nbuffers == 0)
117 goto done;
119 if (nbuffers < queue->min_buffers)
120 nbuffers = queue->min_buffers;
121 else if (nbuffers > queue->max_buffers)
122 nbuffers = queue->max_buffers;
124 queue->scratch = vmalloc_32(bufsize);
126 if (queue->scratch == NULL) {
127 ret = -ENOMEM;
128 goto done;
131 /* Decrement the number of buffers until allocation succeeds. */
132 for (; nbuffers >= queue->min_buffers; --nbuffers) {
133 mem = vmalloc_32(nbuffers * bufsize);
134 buffer = kzalloc(nbuffers * sizeof(struct microdia_buffer),
135 GFP_KERNEL);
136 if (mem != NULL && buffer != NULL)
137 break;
138 if (mem != NULL)
139 vfree(mem);
140 if (buffer != NULL)
141 kfree(buffer);
144 if (mem == NULL || buffer == NULL) {
145 ret = -ENOMEM;
146 vfree(queue->scratch);
147 goto done;
149 queue->buffer = buffer;
151 for (i = 0; i < nbuffers; ++i) {
152 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
153 queue->buffer[i].buf.index = i;
154 queue->buffer[i].buf.m.offset = i * bufsize;
155 queue->buffer[i].buf.length = buflength;
156 queue->buffer[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
157 queue->buffer[i].buf.sequence = 0;
158 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
159 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
160 queue->buffer[i].buf.flags = 0;
161 init_waitqueue_head(&queue->buffer[i].wait);
164 queue->mem = mem;
165 queue->count = nbuffers;
166 queue->buf_size = bufsize;
167 ret = nbuffers;
169 done:
170 mutex_unlock(&queue->mutex);
171 return ret;
175 * Free the video buffers.
177 * This function must be called with the queue lock held.
179 int microdia_free_buffers(struct microdia_video_queue *queue)
181 unsigned int i;
182 UDIA_INFO("Freeing %d v4l2 buffers\n", queue->count);
184 for (i = 0; i < queue->count; ++i) {
185 if (queue->buffer[i].vma_use_count != 0)
186 return -EBUSY;
189 if (queue->count) {
190 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 15)
191 unsigned long addr = (unsigned long)queue->mem;
192 size_t size = queue->count * queue->buf_size;
193 while (size > 0) {
194 ClearPageReserved(vmalloc_to_page((void *)addr));
195 addr += PAGE_SIZE;
196 size -= PAGE_SIZE;
198 #endif
199 vfree(queue->mem);
200 vfree(queue->scratch);
201 kfree(queue->buffer);
202 INIT_LIST_HEAD(&queue->mainqueue);
203 INIT_LIST_HEAD(&queue->irqqueue);
204 queue->count = 0;
207 return 0;
210 static void __microdia_query_buffer(struct microdia_buffer *buf,
211 struct v4l2_buffer *v4l2_buf)
213 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
215 if (buf->vma_use_count)
216 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
218 switch (buf->state) {
219 case MICRODIA_BUF_STATE_ERROR:
220 case MICRODIA_BUF_STATE_DONE:
221 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
222 break;
223 case MICRODIA_BUF_STATE_QUEUED:
224 case MICRODIA_BUF_STATE_ACTIVE:
225 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
226 break;
227 case MICRODIA_BUF_STATE_IDLE:
228 default:
229 break;
233 int microdia_query_buffer(struct microdia_video_queue *queue,
234 struct v4l2_buffer *v4l2_buf)
236 int ret = 0;
238 mutex_lock(&queue->mutex);
239 if (v4l2_buf->index >= queue->count) {
240 ret = -EINVAL;
241 goto done;
244 __microdia_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
246 done:
247 mutex_unlock(&queue->mutex);
248 return ret;
252 * Queue a video buffer. Attempting to queue a buffer that has already been
253 * queued will return -EINVAL.
255 int microdia_queue_buffer(struct microdia_video_queue *queue,
256 struct v4l2_buffer *v4l2_buf)
258 struct microdia_buffer *buf;
259 unsigned long flags;
260 int ret = 0;
262 UDIA_DEBUG("Queuing buffer %u.\n", v4l2_buf->index);
264 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
265 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
266 UDIA_ERROR("[E] Invalid buffer type (%u) "
267 "and/or memory (%u).\n", v4l2_buf->type,
268 v4l2_buf->memory);
269 return -EINVAL;
272 mutex_lock(&queue->mutex);
273 if (v4l2_buf->index >= queue->count) {
274 UDIA_ERROR("[E] Out of range index.\n");
275 ret = -EINVAL;
276 goto done;
279 buf = &queue->buffer[v4l2_buf->index];
280 if (buf->state != MICRODIA_BUF_STATE_IDLE) {
281 UDIA_ERROR("[E] Invalid buffer state "
282 "(%u).\n", buf->state);
283 ret = -EINVAL;
284 goto done;
287 buf->state = MICRODIA_BUF_STATE_QUEUED;
288 buf->buf.bytesused = 0;
289 list_add_tail(&buf->stream, &queue->mainqueue);
290 spin_lock_irqsave(&queue->irqlock, flags);
291 list_add_tail(&buf->queue, &queue->irqqueue);
292 spin_unlock_irqrestore(&queue->irqlock, flags);
294 done:
295 mutex_unlock(&queue->mutex);
296 return ret;
299 static int microdia_queue_waiton(struct microdia_buffer *buf, int nonblocking)
301 if (nonblocking) {
302 return (buf->state != MICRODIA_BUF_STATE_QUEUED &&
303 buf->state != MICRODIA_BUF_STATE_ACTIVE)
304 ? 0 : -EAGAIN;
307 return wait_event_interruptible(buf->wait,
308 buf->state != MICRODIA_BUF_STATE_QUEUED &&
309 buf->state != MICRODIA_BUF_STATE_ACTIVE);
313 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
314 * available.
316 int microdia_dequeue_buffer(struct microdia_video_queue *queue,
317 struct v4l2_buffer *v4l2_buf, int nonblocking)
319 struct microdia_buffer *buf;
320 int ret = 0;
322 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
323 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
324 UDIA_ERROR("[E] Invalid buffer type (%u) "
325 "and/or memory (%u).\n", v4l2_buf->type,
326 v4l2_buf->memory);
327 return -EINVAL;
330 mutex_lock(&queue->mutex);
331 if (list_empty(&queue->mainqueue)) {
332 UDIA_ERROR("[E] Empty buffer queue.\n");
333 ret = -EINVAL;
334 goto done;
337 buf = list_first_entry(&queue->mainqueue, struct microdia_buffer,
338 stream);
340 ret = microdia_queue_waiton(buf, nonblocking);
341 if (ret < 0)
342 goto done;
344 UDIA_DEBUG("Dequeuing buffer %u (%u, %u bytes).\n",
345 buf->buf.index, buf->state, buf->buf.bytesused);
347 switch (buf->state) {
348 case MICRODIA_BUF_STATE_ERROR:
349 UDIA_WARNING("[W] Corrupted data (transmission error).\n");
350 ret = -EIO;
351 case MICRODIA_BUF_STATE_DONE:
352 buf->state = MICRODIA_BUF_STATE_IDLE;
353 break;
355 case MICRODIA_BUF_STATE_IDLE:
356 case MICRODIA_BUF_STATE_QUEUED:
357 case MICRODIA_BUF_STATE_ACTIVE:
358 default:
359 UDIA_ERROR("[E] Invalid buffer state %u "
360 "(driver bug?).\n", buf->state);
361 ret = -EINVAL;
362 goto done;
365 list_del(&buf->stream);
366 __microdia_query_buffer(buf, v4l2_buf);
368 done:
369 mutex_unlock(&queue->mutex);
370 return ret;
374 * Poll the video queue.
376 * This function implements video queue polling and is intended to be used by
377 * the device poll handler.
379 unsigned int microdia_queue_poll(struct microdia_video_queue *queue,
380 struct file *file, poll_table *wait)
382 struct microdia_buffer *buf;
383 unsigned int mask = 0;
385 mutex_lock(&queue->mutex);
386 if (list_empty(&queue->mainqueue)) {
387 mask |= POLLERR;
388 goto done;
390 buf = list_first_entry(&queue->mainqueue, struct microdia_buffer,
391 stream);
393 poll_wait(file, &buf->wait, wait);
394 if (buf->state == MICRODIA_BUF_STATE_DONE ||
395 buf->state == MICRODIA_BUF_STATE_ERROR)
396 mask |= POLLIN | POLLRDNORM;
398 done:
399 mutex_unlock(&queue->mutex);
400 return mask;
404 * Enable or disable the video buffers queue.
406 * The queue must be enabled before starting video acquisition and must be
407 * disabled after stopping it. This ensures that the video buffers queue
408 * state can be properly initialized before buffers are accessed from the
409 * interrupt handler.
411 * Enabling the video queue initializes parameters (such as sequence number,
412 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
414 * Disabling the video queue cancels the queue and removes all buffers from
415 * the main queue.
417 * This function can't be called from interrupt context. Use
418 * microdia_queue_cancel() instead.
420 int microdia_queue_enable(struct microdia_video_queue *queue, int enable)
422 unsigned int i;
423 int ret = 0;
425 mutex_lock(&queue->mutex);
426 if (enable) {
427 if (queue->streaming) {
428 ret = -EBUSY;
429 goto done;
431 queue->sequence = 0;
432 queue->streaming = 1;
433 } else {
434 microdia_queue_cancel(queue);
435 INIT_LIST_HEAD(&queue->mainqueue);
437 for (i = 0; i < queue->count; ++i)
438 queue->buffer[i].state = MICRODIA_BUF_STATE_IDLE;
440 queue->streaming = 0;
443 done:
444 mutex_unlock(&queue->mutex);
445 return ret;
449 * Cancel the video buffers queue.
451 * Cancelling the queue marks all buffers on the irq queue as erroneous,
452 * wakes them up and remove them from the queue.
454 * This function acquires the irq spinlock and can be called from interrupt
455 * context.
457 void microdia_queue_cancel(struct microdia_video_queue *queue)
459 struct microdia_buffer *buf;
460 unsigned long flags;
462 spin_lock_irqsave(&queue->irqlock, flags);
463 while (!list_empty(&queue->irqqueue)) {
464 buf = list_first_entry(&queue->irqqueue, struct microdia_buffer,
465 queue);
466 list_del(&buf->queue);
467 buf->state = MICRODIA_BUF_STATE_ERROR;
468 wake_up(&buf->wait);
470 spin_unlock_irqrestore(&queue->irqlock, flags);
473 struct microdia_buffer *microdia_queue_next_buffer(
474 struct microdia_video_queue *queue,
475 struct microdia_buffer *buf)
477 struct microdia_buffer *nextbuf;
478 unsigned long flags;
480 if (queue->drop_incomplete && queue->frame_size != buf->buf.bytesused) {
481 buf->state = MICRODIA_BUF_STATE_QUEUED;
482 buf->buf.bytesused = 0;
483 return buf;
486 spin_lock_irqsave(&queue->irqlock, flags);
487 list_del(&buf->queue);
488 if (!list_empty(&queue->irqqueue))
489 nextbuf = list_first_entry(&queue->irqqueue,
490 struct microdia_buffer, queue);
491 else
492 nextbuf = NULL;
493 spin_unlock_irqrestore(&queue->irqlock, flags);
495 buf->buf.sequence = queue->sequence++;
496 do_gettimeofday(&buf->buf.timestamp);
498 wake_up(&buf->wait);
499 return nextbuf;