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)
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>
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.
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
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
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
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
);
109 mutex_lock(&queue
->mutex
);
111 ret
= microdia_free_buffers(queue
);
115 /* Bail out if no buffers should be allocated. */
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
) {
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
),
136 if (mem
!= NULL
&& buffer
!= NULL
)
144 if (mem
== NULL
|| buffer
== NULL
) {
146 vfree(queue
->scratch
);
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
);
165 queue
->count
= nbuffers
;
166 queue
->buf_size
= bufsize
;
170 mutex_unlock(&queue
->mutex
);
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
)
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)
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
;
194 ClearPageReserved(vmalloc_to_page((void *)addr
));
200 vfree(queue
->scratch
);
201 kfree(queue
->buffer
);
202 INIT_LIST_HEAD(&queue
->mainqueue
);
203 INIT_LIST_HEAD(&queue
->irqqueue
);
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
;
223 case MICRODIA_BUF_STATE_QUEUED
:
224 case MICRODIA_BUF_STATE_ACTIVE
:
225 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
227 case MICRODIA_BUF_STATE_IDLE
:
233 int microdia_query_buffer(struct microdia_video_queue
*queue
,
234 struct v4l2_buffer
*v4l2_buf
)
238 mutex_lock(&queue
->mutex
);
239 if (v4l2_buf
->index
>= queue
->count
) {
244 __microdia_query_buffer(&queue
->buffer
[v4l2_buf
->index
], v4l2_buf
);
247 mutex_unlock(&queue
->mutex
);
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
;
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
,
272 mutex_lock(&queue
->mutex
);
273 if (v4l2_buf
->index
>= queue
->count
) {
274 UDIA_ERROR("[E] Out of range index.\n");
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
);
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
);
295 mutex_unlock(&queue
->mutex
);
299 static int microdia_queue_waiton(struct microdia_buffer
*buf
, int nonblocking
)
302 return (buf
->state
!= MICRODIA_BUF_STATE_QUEUED
&&
303 buf
->state
!= MICRODIA_BUF_STATE_ACTIVE
)
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
316 int microdia_dequeue_buffer(struct microdia_video_queue
*queue
,
317 struct v4l2_buffer
*v4l2_buf
, int nonblocking
)
319 struct microdia_buffer
*buf
;
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
,
330 mutex_lock(&queue
->mutex
);
331 if (list_empty(&queue
->mainqueue
)) {
332 UDIA_ERROR("[E] Empty buffer queue.\n");
337 buf
= list_first_entry(&queue
->mainqueue
, struct microdia_buffer
,
340 ret
= microdia_queue_waiton(buf
, nonblocking
);
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");
351 case MICRODIA_BUF_STATE_DONE
:
352 buf
->state
= MICRODIA_BUF_STATE_IDLE
;
355 case MICRODIA_BUF_STATE_IDLE
:
356 case MICRODIA_BUF_STATE_QUEUED
:
357 case MICRODIA_BUF_STATE_ACTIVE
:
359 UDIA_ERROR("[E] Invalid buffer state %u "
360 "(driver bug?).\n", buf
->state
);
365 list_del(&buf
->stream
);
366 __microdia_query_buffer(buf
, v4l2_buf
);
369 mutex_unlock(&queue
->mutex
);
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
)) {
390 buf
= list_first_entry(&queue
->mainqueue
, struct microdia_buffer
,
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
;
399 mutex_unlock(&queue
->mutex
);
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
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
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
)
425 mutex_lock(&queue
->mutex
);
427 if (queue
->streaming
) {
432 queue
->streaming
= 1;
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;
444 mutex_unlock(&queue
->mutex
);
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
457 void microdia_queue_cancel(struct microdia_video_queue
*queue
)
459 struct microdia_buffer
*buf
;
462 spin_lock_irqsave(&queue
->irqlock
, flags
);
463 while (!list_empty(&queue
->irqqueue
)) {
464 buf
= list_first_entry(&queue
->irqqueue
, struct microdia_buffer
,
466 list_del(&buf
->queue
);
467 buf
->state
= MICRODIA_BUF_STATE_ERROR
;
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
;
480 if (queue
->drop_incomplete
&& queue
->frame_size
!= buf
->buf
.bytesused
) {
481 buf
->state
= MICRODIA_BUF_STATE_QUEUED
;
482 buf
->buf
.bytesused
= 0;
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
);
493 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
495 buf
->buf
.sequence
= queue
->sequence
++;
496 do_gettimeofday(&buf
->buf
.timestamp
);