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>
26 #include <asm/atomic.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.
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
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
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
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
);
110 mutex_lock(&queue
->mutex
);
112 ret
= microdia_free_buffers(queue
);
116 /* Bail out if no buffers should be allocated. */
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
) {
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
),
137 if (mem
!= NULL
&& buffer
!= NULL
)
145 if (mem
== NULL
|| buffer
== NULL
) {
147 vfree(queue
->scratch
);
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
);
166 queue
->count
= nbuffers
;
167 queue
->buf_size
= bufsize
;
171 mutex_unlock(&queue
->mutex
);
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
)
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)
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
;
195 ClearPageReserved(vmalloc_to_page((void *)addr
));
201 vfree(queue
->scratch
);
202 kfree(queue
->buffer
);
203 INIT_LIST_HEAD(&queue
->mainqueue
);
204 INIT_LIST_HEAD(&queue
->irqqueue
);
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
;
224 case MICRODIA_BUF_STATE_QUEUED
:
225 case MICRODIA_BUF_STATE_ACTIVE
:
226 v4l2_buf
->flags
|= V4L2_BUF_FLAG_QUEUED
;
228 case MICRODIA_BUF_STATE_IDLE
:
234 int microdia_query_buffer(struct microdia_video_queue
*queue
,
235 struct v4l2_buffer
*v4l2_buf
)
239 mutex_lock(&queue
->mutex
);
240 if (v4l2_buf
->index
>= queue
->count
) {
245 __microdia_query_buffer(&queue
->buffer
[v4l2_buf
->index
], v4l2_buf
);
248 mutex_unlock(&queue
->mutex
);
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
;
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
,
273 mutex_lock(&queue
->mutex
);
274 if (v4l2_buf
->index
>= queue
->count
) {
275 UDIA_ERROR("[E] Out of range index.\n");
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
);
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
);
296 mutex_unlock(&queue
->mutex
);
300 static int microdia_queue_waiton(struct microdia_buffer
*buf
, int nonblocking
)
303 return (buf
->state
!= MICRODIA_BUF_STATE_QUEUED
&&
304 buf
->state
!= MICRODIA_BUF_STATE_ACTIVE
)
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
317 int microdia_dequeue_buffer(struct microdia_video_queue
*queue
,
318 struct v4l2_buffer
*v4l2_buf
, int nonblocking
)
320 struct microdia_buffer
*buf
;
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
,
331 mutex_lock(&queue
->mutex
);
332 if (list_empty(&queue
->mainqueue
)) {
333 UDIA_ERROR("[E] Empty buffer queue.\n");
338 buf
= list_first_entry(&queue
->mainqueue
, struct microdia_buffer
,
341 ret
= microdia_queue_waiton(buf
, nonblocking
);
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");
352 case MICRODIA_BUF_STATE_DONE
:
353 buf
->state
= MICRODIA_BUF_STATE_IDLE
;
356 case MICRODIA_BUF_STATE_IDLE
:
357 case MICRODIA_BUF_STATE_QUEUED
:
358 case MICRODIA_BUF_STATE_ACTIVE
:
360 UDIA_ERROR("[E] Invalid buffer state %u "
361 "(driver bug?).\n", buf
->state
);
366 list_del(&buf
->stream
);
367 __microdia_query_buffer(buf
, v4l2_buf
);
370 mutex_unlock(&queue
->mutex
);
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
)) {
391 buf
= list_first_entry(&queue
->mainqueue
, struct microdia_buffer
,
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
;
400 mutex_unlock(&queue
->mutex
);
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
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
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
)
426 mutex_lock(&queue
->mutex
);
428 if (queue
->streaming
) {
433 queue
->streaming
= 1;
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;
445 mutex_unlock(&queue
->mutex
);
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
458 void microdia_queue_cancel(struct microdia_video_queue
*queue
)
460 struct microdia_buffer
*buf
;
463 spin_lock_irqsave(&queue
->irqlock
, flags
);
464 while (!list_empty(&queue
->irqqueue
)) {
465 buf
= list_first_entry(&queue
->irqqueue
, struct microdia_buffer
,
467 list_del(&buf
->queue
);
468 buf
->state
= MICRODIA_BUF_STATE_ERROR
;
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
;
481 if (queue
->drop_incomplete
&& queue
->frame_size
!= buf
->buf
.bytesused
) {
482 buf
->state
= MICRODIA_BUF_STATE_QUEUED
;
483 buf
->buf
.bytesused
= 0;
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
);
494 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
496 buf
->buf
.sequence
= queue
->sequence
++;
497 do_gettimeofday(&buf
->buf
.timestamp
);