[media] uvcvideo: Implement vb2 queue start and stop stream operations
[linux-2.6/btrfs-unstable.git] / drivers / media / usb / uvc / uvc_queue.c
blob758247048ee1a7a2e17ab64ce847fd9e4f7d9c50
1 /*
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>
16 #include <linux/mm.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>
25 #include "uvcvideo.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
36 * the driver.
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)
58 return -EINVAL;
60 *nplanes = 1;
62 sizes[0] = fmt ? fmt->fmt.pix.sizeimage
63 : stream->ctrl.dwMaxVideoFrameSize;
65 return 0;
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");
76 return -EINVAL;
79 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
80 return -ENODEV;
82 buf->state = UVC_BUF_STATE_QUEUED;
83 buf->error = 0;
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)
87 buf->bytesused = 0;
88 else
89 buf->bytesused = vb2_get_plane_payload(vb, 0);
91 return 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);
98 unsigned long flags;
100 spin_lock_irqsave(&queue->irqlock, flags);
101 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
102 list_add_tail(&buf->queue, &queue->irqqueue);
103 } else {
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);
143 queue->buf_used = 0;
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);
152 unsigned long flags;
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,
173 int drop_corrupted)
175 int ret;
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);
186 if (ret)
187 return ret;
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;
194 return 0;
197 /* -----------------------------------------------------------------------------
198 * V4L2 queue operations
201 int uvc_alloc_buffers(struct uvc_video_queue *queue,
202 struct v4l2_requestbuffers *rb)
204 int ret;
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)
222 int ret;
224 mutex_lock(&queue->mutex);
225 ret = vb2_querybuf(&queue->queue, buf);
226 mutex_unlock(&queue->mutex);
228 return ret;
231 int uvc_create_buffers(struct uvc_video_queue *queue,
232 struct v4l2_create_buffers *cb)
234 int ret;
236 mutex_lock(&queue->mutex);
237 ret = vb2_create_bufs(&queue->queue, cb);
238 mutex_unlock(&queue->mutex);
240 return ret;
243 int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
245 int ret;
247 mutex_lock(&queue->mutex);
248 ret = vb2_qbuf(&queue->queue, buf);
249 mutex_unlock(&queue->mutex);
251 return ret;
254 int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
255 int nonblocking)
257 int ret;
259 mutex_lock(&queue->mutex);
260 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
261 mutex_unlock(&queue->mutex);
263 return ret;
266 int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
268 int ret;
270 mutex_lock(&queue->mutex);
271 ret = vb2_mmap(&queue->queue, vma);
272 mutex_unlock(&queue->mutex);
274 return ret;
277 #ifndef CONFIG_MMU
278 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
279 unsigned long pgoff)
281 unsigned long ret;
283 mutex_lock(&queue->mutex);
284 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
285 mutex_unlock(&queue->mutex);
286 return ret;
288 #endif
290 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
291 poll_table *wait)
293 unsigned int ret;
295 mutex_lock(&queue->mutex);
296 ret = vb2_poll(&queue->queue, file, wait);
297 mutex_unlock(&queue->mutex);
299 return ret;
302 /* -----------------------------------------------------------------------------
307 * Check if buffers have been allocated.
309 int uvc_queue_allocated(struct uvc_video_queue *queue)
311 int allocated;
313 mutex_lock(&queue->mutex);
314 allocated = vb2_is_busy(&queue->queue);
315 mutex_unlock(&queue->mutex);
317 return allocated;
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
326 * interrupt handler.
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
331 * the main queue.
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)
338 int ret;
340 mutex_lock(&queue->mutex);
342 if (enable)
343 ret = vb2_streamon(&queue->queue, queue->queue.type);
344 else
345 ret = vb2_streamoff(&queue->queue, queue->queue.type);
347 mutex_unlock(&queue->mutex);
348 return ret;
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
358 * fail with -ENODEV.
360 * This function acquires the irq spinlock and can be called from interrupt
361 * context.
363 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
365 struct uvc_buffer *buf;
366 unsigned long flags;
368 spin_lock_irqsave(&queue->irqlock, flags);
369 while (!list_empty(&queue->irqqueue)) {
370 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
371 queue);
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.
382 if (disconnect)
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;
391 unsigned long flags;
393 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
394 buf->error = 0;
395 buf->state = UVC_BUF_STATE_QUEUED;
396 buf->bytesused = 0;
397 vb2_set_plane_payload(&buf->buf, 0, 0);
398 return buf;
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,
405 queue);
406 else
407 nextbuf = NULL;
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);
414 return nextbuf;