2 * uvc_queue.c -- USB Video Class driver - Buffers management
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/atomic.h>
15 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 #include <media/videobuf2-vmalloc.h>
27 /* ------------------------------------------------------------------------
28 * Video buffers queue management.
30 * Video queues is initialized by uvc_queue_init(). The function performs
31 * basic initialization of the uvc_video_queue struct and never fails.
33 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
34 * the videobuf2 queue operations by serializing calls to videobuf2 and a
35 * spinlock to protect the IRQ queue that holds the buffers to be processed by
39 static inline struct uvc_streaming *
40 uvc_queue_to_stream(struct uvc_video_queue *queue)
42 return container_of(queue, struct uvc_streaming, queue);
45 /* -----------------------------------------------------------------------------
46 * videobuf2 queue operations
49 static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
50 unsigned int *nbuffers, unsigned int *nplanes,
51 unsigned int sizes[], void *alloc_ctxs[])
53 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
54 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
56 /* Make sure the image size is large enough. */
57 if (fmt && fmt->fmt.pix.sizeimage < stream->ctrl.dwMaxVideoFrameSize)
62 sizes[0] = fmt ? fmt->fmt.pix.sizeimage
63 : stream->ctrl.dwMaxVideoFrameSize;
68 static int uvc_buffer_prepare(struct vb2_buffer *vb)
70 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
71 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
73 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
74 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
75 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
79 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
82 buf->state = UVC_BUF_STATE_QUEUED;
84 buf->mem = vb2_plane_vaddr(vb, 0);
85 buf->length = vb2_plane_size(vb, 0);
86 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
89 buf->bytesused = vb2_get_plane_payload(vb, 0);
94 static void uvc_buffer_queue(struct vb2_buffer *vb)
96 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
97 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
100 spin_lock_irqsave(&queue->irqlock, flags);
101 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
102 list_add_tail(&buf->queue, &queue->irqqueue);
104 /* If the device is disconnected return the buffer to userspace
105 * directly. The next QBUF call will fail with -ENODEV.
107 buf->state = UVC_BUF_STATE_ERROR;
108 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
111 spin_unlock_irqrestore(&queue->irqlock, flags);
114 static void uvc_buffer_finish(struct vb2_buffer *vb)
116 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
117 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
118 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
120 if (vb->state == VB2_BUF_STATE_DONE)
121 uvc_video_clock_update(stream, &vb->v4l2_buf, buf);
124 static void uvc_wait_prepare(struct vb2_queue *vq)
126 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
128 mutex_unlock(&queue->mutex);
131 static void uvc_wait_finish(struct vb2_queue *vq)
133 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
135 mutex_lock(&queue->mutex);
138 static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count)
140 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
141 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
145 return uvc_video_enable(stream, 1);
148 static void uvc_stop_streaming(struct vb2_queue *vq)
150 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
151 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
154 uvc_video_enable(stream, 0);
156 spin_lock_irqsave(&queue->irqlock, flags);
157 INIT_LIST_HEAD(&queue->irqqueue);
158 spin_unlock_irqrestore(&queue->irqlock, flags);
161 static struct vb2_ops uvc_queue_qops = {
162 .queue_setup = uvc_queue_setup,
163 .buf_prepare = uvc_buffer_prepare,
164 .buf_queue = uvc_buffer_queue,
165 .buf_finish = uvc_buffer_finish,
166 .wait_prepare = uvc_wait_prepare,
167 .wait_finish = uvc_wait_finish,
168 .start_streaming = uvc_start_streaming,
169 .stop_streaming = uvc_stop_streaming,
172 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
177 queue->queue.type = type;
178 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
179 queue->queue.drv_priv = queue;
180 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
181 queue->queue.ops = &uvc_queue_qops;
182 queue->queue.mem_ops = &vb2_vmalloc_memops;
183 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
184 | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
185 ret = vb2_queue_init(&queue->queue);
189 mutex_init(&queue->mutex);
190 spin_lock_init(&queue->irqlock);
191 INIT_LIST_HEAD(&queue->irqqueue);
192 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
197 /* -----------------------------------------------------------------------------
198 * V4L2 queue operations
201 int uvc_alloc_buffers(struct uvc_video_queue *queue,
202 struct v4l2_requestbuffers *rb)
206 mutex_lock(&queue->mutex);
207 ret = vb2_reqbufs(&queue->queue, rb);
208 mutex_unlock(&queue->mutex);
210 return ret ? ret : rb->count;
213 void uvc_free_buffers(struct uvc_video_queue *queue)
215 mutex_lock(&queue->mutex);
216 vb2_queue_release(&queue->queue);
217 mutex_unlock(&queue->mutex);
220 int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
224 mutex_lock(&queue->mutex);
225 ret = vb2_querybuf(&queue->queue, buf);
226 mutex_unlock(&queue->mutex);
231 int uvc_create_buffers(struct uvc_video_queue *queue,
232 struct v4l2_create_buffers *cb)
236 mutex_lock(&queue->mutex);
237 ret = vb2_create_bufs(&queue->queue, cb);
238 mutex_unlock(&queue->mutex);
243 int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
247 mutex_lock(&queue->mutex);
248 ret = vb2_qbuf(&queue->queue, buf);
249 mutex_unlock(&queue->mutex);
254 int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
259 mutex_lock(&queue->mutex);
260 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
261 mutex_unlock(&queue->mutex);
266 int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
270 mutex_lock(&queue->mutex);
271 ret = vb2_mmap(&queue->queue, vma);
272 mutex_unlock(&queue->mutex);
278 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
283 mutex_lock(&queue->mutex);
284 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
285 mutex_unlock(&queue->mutex);
290 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
295 mutex_lock(&queue->mutex);
296 ret = vb2_poll(&queue->queue, file, wait);
297 mutex_unlock(&queue->mutex);
302 /* -----------------------------------------------------------------------------
307 * Check if buffers have been allocated.
309 int uvc_queue_allocated(struct uvc_video_queue *queue)
313 mutex_lock(&queue->mutex);
314 allocated = vb2_is_busy(&queue->queue);
315 mutex_unlock(&queue->mutex);
321 * Enable or disable the video buffers queue.
323 * The queue must be enabled before starting video acquisition and must be
324 * disabled after stopping it. This ensures that the video buffers queue
325 * state can be properly initialized before buffers are accessed from the
328 * Enabling the video queue returns -EBUSY if the queue is already enabled.
330 * Disabling the video queue cancels the queue and removes all buffers from
333 * This function can't be called from interrupt context. Use
334 * uvc_queue_cancel() instead.
336 int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
340 mutex_lock(&queue->mutex);
343 ret = vb2_streamon(&queue->queue, queue->queue.type);
345 ret = vb2_streamoff(&queue->queue, queue->queue.type);
347 mutex_unlock(&queue->mutex);
352 * Cancel the video buffers queue.
354 * Cancelling the queue marks all buffers on the irq queue as erroneous,
355 * wakes them up and removes them from the queue.
357 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
360 * This function acquires the irq spinlock and can be called from interrupt
363 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
365 struct uvc_buffer *buf;
368 spin_lock_irqsave(&queue->irqlock, flags);
369 while (!list_empty(&queue->irqqueue)) {
370 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
372 list_del(&buf->queue);
373 buf->state = UVC_BUF_STATE_ERROR;
374 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
376 /* This must be protected by the irqlock spinlock to avoid race
377 * conditions between uvc_buffer_queue and the disconnection event that
378 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
379 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
380 * state outside the queue code.
383 queue->flags |= UVC_QUEUE_DISCONNECTED;
384 spin_unlock_irqrestore(&queue->irqlock, flags);
387 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
388 struct uvc_buffer *buf)
390 struct uvc_buffer *nextbuf;
393 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
395 buf->state = UVC_BUF_STATE_QUEUED;
397 vb2_set_plane_payload(&buf->buf, 0, 0);
401 spin_lock_irqsave(&queue->irqlock, flags);
402 list_del(&buf->queue);
403 if (!list_empty(&queue->irqqueue))
404 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
408 spin_unlock_irqrestore(&queue->irqlock, flags);
410 buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
411 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
412 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);