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-v4l2.h>
24 #include <media/videobuf2-vmalloc.h>
28 /* ------------------------------------------------------------------------
29 * Video buffers queue management.
31 * Video queues is initialized by uvc_queue_init(). The function performs
32 * basic initialization of the uvc_video_queue struct and never fails.
34 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
35 * the videobuf2 queue operations by serializing calls to videobuf2 and a
36 * spinlock to protect the IRQ queue that holds the buffers to be processed by
40 static inline struct uvc_streaming *
41 uvc_queue_to_stream(struct uvc_video_queue *queue)
43 return container_of(queue, struct uvc_streaming, queue);
46 static inline struct uvc_buffer *uvc_vbuf_to_buffer(struct vb2_v4l2_buffer *buf)
48 return container_of(buf, struct uvc_buffer, buf);
52 * Return all queued buffers to videobuf2 in the requested state.
54 * This function must be called with the queue spinlock held.
56 static void uvc_queue_return_buffers(struct uvc_video_queue *queue,
57 enum uvc_buffer_state state)
59 enum vb2_buffer_state vb2_state = state == UVC_BUF_STATE_ERROR
61 : VB2_BUF_STATE_QUEUED;
63 while (!list_empty(&queue->irqqueue)) {
64 struct uvc_buffer *buf = list_first_entry(&queue->irqqueue,
67 list_del(&buf->queue);
69 vb2_buffer_done(&buf->buf.vb2_buf, vb2_state);
73 /* -----------------------------------------------------------------------------
74 * videobuf2 queue operations
77 static int uvc_queue_setup(struct vb2_queue *vq,
78 unsigned int *nbuffers, unsigned int *nplanes,
79 unsigned int sizes[], struct device *alloc_devs[])
81 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
82 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
83 unsigned size = stream->ctrl.dwMaxVideoFrameSize;
86 * When called with plane sizes, validate them. The driver supports
87 * single planar formats only, and requires buffers to be large enough
88 * to store a complete frame.
91 return *nplanes != 1 || sizes[0] < size ? -EINVAL : 0;
98 static int uvc_buffer_prepare(struct vb2_buffer *vb)
100 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
101 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
102 struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
104 if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
105 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
106 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
110 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
113 buf->state = UVC_BUF_STATE_QUEUED;
115 buf->mem = vb2_plane_vaddr(vb, 0);
116 buf->length = vb2_plane_size(vb, 0);
117 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
120 buf->bytesused = vb2_get_plane_payload(vb, 0);
125 static void uvc_buffer_queue(struct vb2_buffer *vb)
127 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
128 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
129 struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
132 spin_lock_irqsave(&queue->irqlock, flags);
133 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
134 list_add_tail(&buf->queue, &queue->irqqueue);
136 /* If the device is disconnected return the buffer to userspace
137 * directly. The next QBUF call will fail with -ENODEV.
139 buf->state = UVC_BUF_STATE_ERROR;
140 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
143 spin_unlock_irqrestore(&queue->irqlock, flags);
146 static void uvc_buffer_finish(struct vb2_buffer *vb)
148 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
149 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
150 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
151 struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
153 if (vb->state == VB2_BUF_STATE_DONE)
154 uvc_video_clock_update(stream, vbuf, buf);
157 static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count)
159 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
160 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
166 ret = uvc_video_enable(stream, 1);
170 spin_lock_irqsave(&queue->irqlock, flags);
171 uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED);
172 spin_unlock_irqrestore(&queue->irqlock, flags);
177 static void uvc_stop_streaming(struct vb2_queue *vq)
179 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
180 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
183 uvc_video_enable(stream, 0);
185 spin_lock_irqsave(&queue->irqlock, flags);
186 uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
187 spin_unlock_irqrestore(&queue->irqlock, flags);
190 static const struct vb2_ops uvc_queue_qops = {
191 .queue_setup = uvc_queue_setup,
192 .buf_prepare = uvc_buffer_prepare,
193 .buf_queue = uvc_buffer_queue,
194 .buf_finish = uvc_buffer_finish,
195 .wait_prepare = vb2_ops_wait_prepare,
196 .wait_finish = vb2_ops_wait_finish,
197 .start_streaming = uvc_start_streaming,
198 .stop_streaming = uvc_stop_streaming,
201 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
206 queue->queue.type = type;
207 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
208 queue->queue.drv_priv = queue;
209 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
210 queue->queue.ops = &uvc_queue_qops;
211 queue->queue.mem_ops = &vb2_vmalloc_memops;
212 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
213 | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
214 queue->queue.lock = &queue->mutex;
215 ret = vb2_queue_init(&queue->queue);
219 mutex_init(&queue->mutex);
220 spin_lock_init(&queue->irqlock);
221 INIT_LIST_HEAD(&queue->irqqueue);
222 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
227 void uvc_queue_release(struct uvc_video_queue *queue)
229 mutex_lock(&queue->mutex);
230 vb2_queue_release(&queue->queue);
231 mutex_unlock(&queue->mutex);
234 /* -----------------------------------------------------------------------------
235 * V4L2 queue operations
238 int uvc_request_buffers(struct uvc_video_queue *queue,
239 struct v4l2_requestbuffers *rb)
243 mutex_lock(&queue->mutex);
244 ret = vb2_reqbufs(&queue->queue, rb);
245 mutex_unlock(&queue->mutex);
247 return ret ? ret : rb->count;
250 int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
254 mutex_lock(&queue->mutex);
255 ret = vb2_querybuf(&queue->queue, buf);
256 mutex_unlock(&queue->mutex);
261 int uvc_create_buffers(struct uvc_video_queue *queue,
262 struct v4l2_create_buffers *cb)
266 mutex_lock(&queue->mutex);
267 ret = vb2_create_bufs(&queue->queue, cb);
268 mutex_unlock(&queue->mutex);
273 int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
277 mutex_lock(&queue->mutex);
278 ret = vb2_qbuf(&queue->queue, buf);
279 mutex_unlock(&queue->mutex);
284 int uvc_export_buffer(struct uvc_video_queue *queue,
285 struct v4l2_exportbuffer *exp)
289 mutex_lock(&queue->mutex);
290 ret = vb2_expbuf(&queue->queue, exp);
291 mutex_unlock(&queue->mutex);
296 int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
301 mutex_lock(&queue->mutex);
302 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
303 mutex_unlock(&queue->mutex);
308 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type)
312 mutex_lock(&queue->mutex);
313 ret = vb2_streamon(&queue->queue, type);
314 mutex_unlock(&queue->mutex);
319 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type)
323 mutex_lock(&queue->mutex);
324 ret = vb2_streamoff(&queue->queue, type);
325 mutex_unlock(&queue->mutex);
330 int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
332 return vb2_mmap(&queue->queue, vma);
336 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
339 return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
343 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
348 mutex_lock(&queue->mutex);
349 ret = vb2_poll(&queue->queue, file, wait);
350 mutex_unlock(&queue->mutex);
355 /* -----------------------------------------------------------------------------
360 * Check if buffers have been allocated.
362 int uvc_queue_allocated(struct uvc_video_queue *queue)
366 mutex_lock(&queue->mutex);
367 allocated = vb2_is_busy(&queue->queue);
368 mutex_unlock(&queue->mutex);
374 * Cancel the video buffers queue.
376 * Cancelling the queue marks all buffers on the irq queue as erroneous,
377 * wakes them up and removes them from the queue.
379 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
382 * This function acquires the irq spinlock and can be called from interrupt
385 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
389 spin_lock_irqsave(&queue->irqlock, flags);
390 uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
391 /* This must be protected by the irqlock spinlock to avoid race
392 * conditions between uvc_buffer_queue and the disconnection event that
393 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
394 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
395 * state outside the queue code.
398 queue->flags |= UVC_QUEUE_DISCONNECTED;
399 spin_unlock_irqrestore(&queue->irqlock, flags);
402 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
403 struct uvc_buffer *buf)
405 struct uvc_buffer *nextbuf;
408 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
410 buf->state = UVC_BUF_STATE_QUEUED;
412 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
416 spin_lock_irqsave(&queue->irqlock, flags);
417 list_del(&buf->queue);
418 if (!list_empty(&queue->irqqueue))
419 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
423 spin_unlock_irqrestore(&queue->irqlock, flags);
425 buf->state = buf->error ? UVC_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
426 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
427 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);