Starting the process of seperating bridge-sensor for 6240
[microdia.git] / microdia-queue.c
blob2bd0fdce4b17b1c63fa226b89710dcad00a4e67e
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 <linux/mm.h>
26 #include <asm/atomic.h>
28 #include "microdia.h"
30 /* ------------------------------------------------------------------------
31 * Video buffers queue management.
33 * Video queues is initialized by microdia_queue_init(). The function performs
34 * basic initialization of the microdia_video_queue struct and never fails.
36 * Video buffer allocation and freeing are performed by microdia_alloc_buffers
37 * and microdia_free_buffers respectively. The former acquires the video queue
38 * lock, while the later must be called with the lock held (so that allocation
39 * can free previously allocated buffers). Trying to free buffers that are
40 * mapped to user space will return -EBUSY.
42 * Video buffers are managed using two queues. However, unlike most USB video
43 * drivers which use an in queue and an out queue, we use a main queue which
44 * holds all queued buffers (both 'empty' and 'done' buffers), and an irq
45 * queue which holds empty buffers. This design (copied from video-buf)
46 * minimizes locking in interrupt, as only one queue is shared between
47 * interrupt and user contexts.
49 * Use cases
50 * ---------
52 * Unless stated otherwise, all operations which modify the irq buffers queue
53 * are protected by the irq spinlock.
55 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
57 * The buffers are added to the main and irq queues. Both operations are
58 * protected by the queue lock, and the latert is protected by the irq
59 * spinlock as well.
61 * The completion handler fetches a buffer from the irq queue and fills it
62 * with video data. If no buffer is available (irq queue empty), the handler
63 * returns immediately.
65 * When the buffer is full, the completion handler removes it from the irq
66 * queue, marks it as ready (MICRODIA_BUF_STATE_DONE) and wake its wait
67 * queue. At that point, any process waiting on the buffer will be woken up.
68 * If a process tries to dequeue a buffer after it has been marked ready,
69 * the dequeing will succeed immediately.
71 * 2. Buffers are queued, user is waiting on a buffer and the device gets
72 * disconnected.
74 * When the device is disconnected, the kernel calls the completion handler
75 * with an appropriate status code. The handler marks all buffers in the
76 * irq queue as being erroneous (MICRODIA_BUF_STATE_ERROR) and wakes them up
77 * so that any process waiting on a buffer gets woken up.
79 * Waking up up the first buffer on the irq list is not enough, as the
80 * process waiting on the buffer might restart the dequeue operation
81 * immediately.
85 void microdia_queue_init(struct microdia_video_queue *queue)
87 mutex_init(&queue->mutex);
88 spin_lock_init(&queue->irqlock);
89 INIT_LIST_HEAD(&queue->mainqueue);
90 INIT_LIST_HEAD(&queue->irqqueue);
94 * Allocate the video buffers.
96 * Pages are reserved to make sure they will not be swaped, as they will be
97 * filled in URB completion handler.
99 * Buffers will be individually mapped, so they must all be page aligned.
101 int microdia_alloc_buffers(struct microdia_video_queue *queue,
102 unsigned int nbuffers, unsigned int buflength)
104 unsigned int bufsize = PAGE_ALIGN(buflength);
105 unsigned int i;
106 void *mem = NULL;
107 void *buffer = NULL;
108 int ret;
110 mutex_lock(&queue->mutex);
112 ret = microdia_free_buffers(queue);
113 if (ret < 0)
114 goto done;
116 /* Bail out if no buffers should be allocated. */
117 if (nbuffers == 0)
118 goto done;
120 if (nbuffers < queue->min_buffers)
121 nbuffers = queue->min_buffers;
122 else if (nbuffers > queue->max_buffers)
123 nbuffers = queue->max_buffers;
125 queue->scratch = vmalloc_32(bufsize);
127 if (queue->scratch == NULL) {
128 ret = -ENOMEM;
129 goto done;
132 /* Decrement the number of buffers until allocation succeeds. */
133 for (; nbuffers >= queue->min_buffers; --nbuffers) {
134 mem = vmalloc_32(nbuffers * bufsize);
135 buffer = kzalloc(nbuffers * sizeof(struct microdia_buffer),
136 GFP_KERNEL);
137 if (mem != NULL && buffer != NULL)
138 break;
139 if (mem != NULL)
140 vfree(mem);
141 if (buffer != NULL)
142 kfree(buffer);
145 if (mem == NULL || buffer == NULL) {
146 ret = -ENOMEM;
147 vfree(queue->scratch);
148 goto done;
150 queue->buffer = buffer;
152 for (i = 0; i < nbuffers; ++i) {
153 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
154 queue->buffer[i].buf.index = i;
155 queue->buffer[i].buf.m.offset = i * bufsize;
156 queue->buffer[i].buf.length = buflength;
157 queue->buffer[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
158 queue->buffer[i].buf.sequence = 0;
159 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
160 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
161 queue->buffer[i].buf.flags = 0;
162 init_waitqueue_head(&queue->buffer[i].wait);
165 queue->mem = mem;
166 queue->count = nbuffers;
167 queue->buf_size = bufsize;
168 ret = nbuffers;
170 done:
171 mutex_unlock(&queue->mutex);
172 return ret;
176 * Free the video buffers.
178 * This function must be called with the queue lock held.
180 int microdia_free_buffers(struct microdia_video_queue *queue)
182 unsigned int i;
183 UDIA_INFO("Freeing %d v4l2 buffers\n", queue->count);
185 for (i = 0; i < queue->count; ++i) {
186 if (queue->buffer[i].vma_use_count != 0)
187 return -EBUSY;
190 if (queue->count) {
191 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 15)
192 unsigned long addr = (unsigned long)queue->mem;
193 size_t size = queue->count * queue->buf_size;
194 while (size > 0) {
195 ClearPageReserved(vmalloc_to_page((void *)addr));
196 addr += PAGE_SIZE;
197 size -= PAGE_SIZE;
199 #endif
200 vfree(queue->mem);
201 vfree(queue->scratch);
202 kfree(queue->buffer);
203 INIT_LIST_HEAD(&queue->mainqueue);
204 INIT_LIST_HEAD(&queue->irqqueue);
205 queue->count = 0;
208 return 0;
211 static void __microdia_query_buffer(struct microdia_buffer *buf,
212 struct v4l2_buffer *v4l2_buf)
214 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
216 if (buf->vma_use_count)
217 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
219 switch (buf->state) {
220 case MICRODIA_BUF_STATE_ERROR:
221 case MICRODIA_BUF_STATE_DONE:
222 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
223 break;
224 case MICRODIA_BUF_STATE_QUEUED:
225 case MICRODIA_BUF_STATE_ACTIVE:
226 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
227 break;
228 case MICRODIA_BUF_STATE_IDLE:
229 default:
230 break;
234 int microdia_query_buffer(struct microdia_video_queue *queue,
235 struct v4l2_buffer *v4l2_buf)
237 int ret = 0;
239 mutex_lock(&queue->mutex);
240 if (v4l2_buf->index >= queue->count) {
241 ret = -EINVAL;
242 goto done;
245 __microdia_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
247 done:
248 mutex_unlock(&queue->mutex);
249 return ret;
253 * Queue a video buffer. Attempting to queue a buffer that has already been
254 * queued will return -EINVAL.
256 int microdia_queue_buffer(struct microdia_video_queue *queue,
257 struct v4l2_buffer *v4l2_buf)
259 struct microdia_buffer *buf;
260 unsigned long flags;
261 int ret = 0;
263 UDIA_DEBUG("Queuing buffer %u.\n", v4l2_buf->index);
265 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
266 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
267 UDIA_ERROR("[E] Invalid buffer type (%u) "
268 "and/or memory (%u).\n", v4l2_buf->type,
269 v4l2_buf->memory);
270 return -EINVAL;
273 mutex_lock(&queue->mutex);
274 if (v4l2_buf->index >= queue->count) {
275 UDIA_ERROR("[E] Out of range index.\n");
276 ret = -EINVAL;
277 goto done;
280 buf = &queue->buffer[v4l2_buf->index];
281 if (buf->state != MICRODIA_BUF_STATE_IDLE) {
282 UDIA_ERROR("[E] Invalid buffer state "
283 "(%u).\n", buf->state);
284 ret = -EINVAL;
285 goto done;
288 buf->state = MICRODIA_BUF_STATE_QUEUED;
289 buf->buf.bytesused = 0;
290 list_add_tail(&buf->stream, &queue->mainqueue);
291 spin_lock_irqsave(&queue->irqlock, flags);
292 list_add_tail(&buf->queue, &queue->irqqueue);
293 spin_unlock_irqrestore(&queue->irqlock, flags);
295 done:
296 mutex_unlock(&queue->mutex);
297 return ret;
300 static int microdia_queue_waiton(struct microdia_buffer *buf, int nonblocking)
302 if (nonblocking) {
303 return (buf->state != MICRODIA_BUF_STATE_QUEUED &&
304 buf->state != MICRODIA_BUF_STATE_ACTIVE)
305 ? 0 : -EAGAIN;
308 return wait_event_interruptible(buf->wait,
309 buf->state != MICRODIA_BUF_STATE_QUEUED &&
310 buf->state != MICRODIA_BUF_STATE_ACTIVE);
314 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
315 * available.
317 int microdia_dequeue_buffer(struct microdia_video_queue *queue,
318 struct v4l2_buffer *v4l2_buf, int nonblocking)
320 struct microdia_buffer *buf;
321 int ret = 0;
323 if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
324 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
325 UDIA_ERROR("[E] Invalid buffer type (%u) "
326 "and/or memory (%u).\n", v4l2_buf->type,
327 v4l2_buf->memory);
328 return -EINVAL;
331 mutex_lock(&queue->mutex);
332 if (list_empty(&queue->mainqueue)) {
333 UDIA_ERROR("[E] Empty buffer queue.\n");
334 ret = -EINVAL;
335 goto done;
338 buf = list_first_entry(&queue->mainqueue, struct microdia_buffer,
339 stream);
341 ret = microdia_queue_waiton(buf, nonblocking);
342 if (ret < 0)
343 goto done;
345 UDIA_DEBUG("Dequeuing buffer %u (%u, %u bytes).\n",
346 buf->buf.index, buf->state, buf->buf.bytesused);
348 switch (buf->state) {
349 case MICRODIA_BUF_STATE_ERROR:
350 UDIA_WARNING("[W] Corrupted data (transmission error).\n");
351 ret = -EIO;
352 case MICRODIA_BUF_STATE_DONE:
353 buf->state = MICRODIA_BUF_STATE_IDLE;
354 break;
356 case MICRODIA_BUF_STATE_IDLE:
357 case MICRODIA_BUF_STATE_QUEUED:
358 case MICRODIA_BUF_STATE_ACTIVE:
359 default:
360 UDIA_ERROR("[E] Invalid buffer state %u "
361 "(driver bug?).\n", buf->state);
362 ret = -EINVAL;
363 goto done;
366 list_del(&buf->stream);
367 __microdia_query_buffer(buf, v4l2_buf);
369 done:
370 mutex_unlock(&queue->mutex);
371 return ret;
375 * Poll the video queue.
377 * This function implements video queue polling and is intended to be used by
378 * the device poll handler.
380 unsigned int microdia_queue_poll(struct microdia_video_queue *queue,
381 struct file *file, poll_table *wait)
383 struct microdia_buffer *buf;
384 unsigned int mask = 0;
386 mutex_lock(&queue->mutex);
387 if (list_empty(&queue->mainqueue)) {
388 mask |= POLLERR;
389 goto done;
391 buf = list_first_entry(&queue->mainqueue, struct microdia_buffer,
392 stream);
394 poll_wait(file, &buf->wait, wait);
395 if (buf->state == MICRODIA_BUF_STATE_DONE ||
396 buf->state == MICRODIA_BUF_STATE_ERROR)
397 mask |= POLLIN | POLLRDNORM;
399 done:
400 mutex_unlock(&queue->mutex);
401 return mask;
405 * Enable or disable the video buffers queue.
407 * The queue must be enabled before starting video acquisition and must be
408 * disabled after stopping it. This ensures that the video buffers queue
409 * state can be properly initialized before buffers are accessed from the
410 * interrupt handler.
412 * Enabling the video queue initializes parameters (such as sequence number,
413 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
415 * Disabling the video queue cancels the queue and removes all buffers from
416 * the main queue.
418 * This function can't be called from interrupt context. Use
419 * microdia_queue_cancel() instead.
421 int microdia_queue_enable(struct microdia_video_queue *queue, int enable)
423 unsigned int i;
424 int ret = 0;
426 mutex_lock(&queue->mutex);
427 if (enable) {
428 if (queue->streaming) {
429 ret = -EBUSY;
430 goto done;
432 queue->sequence = 0;
433 queue->streaming = 1;
434 } else {
435 microdia_queue_cancel(queue);
436 INIT_LIST_HEAD(&queue->mainqueue);
438 for (i = 0; i < queue->count; ++i)
439 queue->buffer[i].state = MICRODIA_BUF_STATE_IDLE;
441 queue->streaming = 0;
444 done:
445 mutex_unlock(&queue->mutex);
446 return ret;
450 * Cancel the video buffers queue.
452 * Cancelling the queue marks all buffers on the irq queue as erroneous,
453 * wakes them up and remove them from the queue.
455 * This function acquires the irq spinlock and can be called from interrupt
456 * context.
458 void microdia_queue_cancel(struct microdia_video_queue *queue)
460 struct microdia_buffer *buf;
461 unsigned long flags;
463 spin_lock_irqsave(&queue->irqlock, flags);
464 while (!list_empty(&queue->irqqueue)) {
465 buf = list_first_entry(&queue->irqqueue, struct microdia_buffer,
466 queue);
467 list_del(&buf->queue);
468 buf->state = MICRODIA_BUF_STATE_ERROR;
469 wake_up(&buf->wait);
471 spin_unlock_irqrestore(&queue->irqlock, flags);
474 struct microdia_buffer *microdia_queue_next_buffer(
475 struct microdia_video_queue *queue,
476 struct microdia_buffer *buf)
478 struct microdia_buffer *nextbuf;
479 unsigned long flags;
481 if (queue->drop_incomplete && queue->frame_size != buf->buf.bytesused) {
482 buf->state = MICRODIA_BUF_STATE_QUEUED;
483 buf->buf.bytesused = 0;
484 return buf;
487 spin_lock_irqsave(&queue->irqlock, flags);
488 list_del(&buf->queue);
489 if (!list_empty(&queue->irqqueue))
490 nextbuf = list_first_entry(&queue->irqqueue,
491 struct microdia_buffer, queue);
492 else
493 nextbuf = NULL;
494 spin_unlock_irqrestore(&queue->irqlock, flags);
496 buf->buf.sequence = queue->sequence++;
497 do_gettimeofday(&buf->buf.timestamp);
499 wake_up(&buf->wait);
500 return nextbuf;