NFC: microread: Auto-select core module
[linux-2.6/btrfs-unstable.git] / drivers / usb / gadget / function / uvc_queue.c
blobd617c39a00527414ea2561c3332a7c508f7b7815
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.
13 #include <linux/atomic.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/videodev2.h>
20 #include <linux/vmalloc.h>
21 #include <linux/wait.h>
23 #include <media/v4l2-common.h>
24 #include <media/videobuf2-vmalloc.h>
26 #include "uvc.h"
28 /* ------------------------------------------------------------------------
29 * Video buffers queue management.
31 * Video queues is initialized by uvcg_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
37 * the driver.
40 /* -----------------------------------------------------------------------------
41 * videobuf2 queue operations
44 static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
45 unsigned int *nbuffers, unsigned int *nplanes,
46 unsigned int sizes[], void *alloc_ctxs[])
48 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
49 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
51 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
52 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
54 *nplanes = 1;
56 sizes[0] = video->imagesize;
58 return 0;
61 static int uvc_buffer_prepare(struct vb2_buffer *vb)
63 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
64 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
66 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
67 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
68 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
69 return -EINVAL;
72 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
73 return -ENODEV;
75 buf->state = UVC_BUF_STATE_QUEUED;
76 buf->mem = vb2_plane_vaddr(vb, 0);
77 buf->length = vb2_plane_size(vb, 0);
78 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
79 buf->bytesused = 0;
80 else
81 buf->bytesused = vb2_get_plane_payload(vb, 0);
83 return 0;
86 static void uvc_buffer_queue(struct vb2_buffer *vb)
88 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
89 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
90 unsigned long flags;
92 spin_lock_irqsave(&queue->irqlock, flags);
94 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
95 list_add_tail(&buf->queue, &queue->irqqueue);
96 } else {
97 /* If the device is disconnected return the buffer to userspace
98 * directly. The next QBUF call will fail with -ENODEV.
100 buf->state = UVC_BUF_STATE_ERROR;
101 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
104 spin_unlock_irqrestore(&queue->irqlock, flags);
107 static struct vb2_ops uvc_queue_qops = {
108 .queue_setup = uvc_queue_setup,
109 .buf_prepare = uvc_buffer_prepare,
110 .buf_queue = uvc_buffer_queue,
111 .wait_prepare = vb2_ops_wait_prepare,
112 .wait_finish = vb2_ops_wait_finish,
115 int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
116 struct mutex *lock)
118 int ret;
120 queue->queue.type = type;
121 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
122 queue->queue.drv_priv = queue;
123 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
124 queue->queue.ops = &uvc_queue_qops;
125 queue->queue.lock = lock;
126 queue->queue.mem_ops = &vb2_vmalloc_memops;
127 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
128 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
129 ret = vb2_queue_init(&queue->queue);
130 if (ret)
131 return ret;
133 spin_lock_init(&queue->irqlock);
134 INIT_LIST_HEAD(&queue->irqqueue);
135 queue->flags = 0;
137 return 0;
141 * Free the video buffers.
143 void uvcg_free_buffers(struct uvc_video_queue *queue)
145 vb2_queue_release(&queue->queue);
149 * Allocate the video buffers.
151 int uvcg_alloc_buffers(struct uvc_video_queue *queue,
152 struct v4l2_requestbuffers *rb)
154 int ret;
156 ret = vb2_reqbufs(&queue->queue, rb);
158 return ret ? ret : rb->count;
161 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
163 return vb2_querybuf(&queue->queue, buf);
166 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
168 unsigned long flags;
169 int ret;
171 ret = vb2_qbuf(&queue->queue, buf);
172 if (ret < 0)
173 return ret;
175 spin_lock_irqsave(&queue->irqlock, flags);
176 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
177 queue->flags &= ~UVC_QUEUE_PAUSED;
178 spin_unlock_irqrestore(&queue->irqlock, flags);
179 return ret;
183 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
184 * available.
186 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
187 int nonblocking)
189 return vb2_dqbuf(&queue->queue, buf, nonblocking);
193 * Poll the video queue.
195 * This function implements video queue polling and is intended to be used by
196 * the device poll handler.
198 unsigned int uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
199 poll_table *wait)
201 return vb2_poll(&queue->queue, file, wait);
204 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
206 return vb2_mmap(&queue->queue, vma);
209 #ifndef CONFIG_MMU
211 * Get unmapped area.
213 * NO-MMU arch need this function to make mmap() work correctly.
215 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
216 unsigned long pgoff)
218 return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
220 #endif
223 * Cancel the video buffers queue.
225 * Cancelling the queue marks all buffers on the irq queue as erroneous,
226 * wakes them up and removes them from the queue.
228 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
229 * fail with -ENODEV.
231 * This function acquires the irq spinlock and can be called from interrupt
232 * context.
234 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
236 struct uvc_buffer *buf;
237 unsigned long flags;
239 spin_lock_irqsave(&queue->irqlock, flags);
240 while (!list_empty(&queue->irqqueue)) {
241 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
242 queue);
243 list_del(&buf->queue);
244 buf->state = UVC_BUF_STATE_ERROR;
245 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
247 /* This must be protected by the irqlock spinlock to avoid race
248 * conditions between uvc_queue_buffer and the disconnection event that
249 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
250 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
251 * state outside the queue code.
253 if (disconnect)
254 queue->flags |= UVC_QUEUE_DISCONNECTED;
255 spin_unlock_irqrestore(&queue->irqlock, flags);
259 * Enable or disable the video buffers queue.
261 * The queue must be enabled before starting video acquisition and must be
262 * disabled after stopping it. This ensures that the video buffers queue
263 * state can be properly initialized before buffers are accessed from the
264 * interrupt handler.
266 * Enabling the video queue initializes parameters (such as sequence number,
267 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
269 * Disabling the video queue cancels the queue and removes all buffers from
270 * the main queue.
272 * This function can't be called from interrupt context. Use
273 * uvcg_queue_cancel() instead.
275 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
277 unsigned long flags;
278 int ret = 0;
280 if (enable) {
281 ret = vb2_streamon(&queue->queue, queue->queue.type);
282 if (ret < 0)
283 return ret;
285 queue->sequence = 0;
286 queue->buf_used = 0;
287 } else {
288 ret = vb2_streamoff(&queue->queue, queue->queue.type);
289 if (ret < 0)
290 return ret;
292 spin_lock_irqsave(&queue->irqlock, flags);
293 INIT_LIST_HEAD(&queue->irqqueue);
296 * FIXME: We need to clear the DISCONNECTED flag to ensure that
297 * applications will be able to queue buffers for the next
298 * streaming run. However, clearing it here doesn't guarantee
299 * that the device will be reconnected in the meantime.
301 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
302 spin_unlock_irqrestore(&queue->irqlock, flags);
305 return ret;
308 /* called with &queue_irqlock held.. */
309 struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue,
310 struct uvc_buffer *buf)
312 struct uvc_buffer *nextbuf;
314 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
315 buf->length != buf->bytesused) {
316 buf->state = UVC_BUF_STATE_QUEUED;
317 vb2_set_plane_payload(&buf->buf, 0, 0);
318 return buf;
321 list_del(&buf->queue);
322 if (!list_empty(&queue->irqqueue))
323 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
324 queue);
325 else
326 nextbuf = NULL;
328 buf->buf.v4l2_buf.field = V4L2_FIELD_NONE;
329 buf->buf.v4l2_buf.sequence = queue->sequence++;
330 v4l2_get_timestamp(&buf->buf.v4l2_buf.timestamp);
332 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
333 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
335 return nextbuf;
338 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
340 struct uvc_buffer *buf = NULL;
342 if (!list_empty(&queue->irqqueue))
343 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
344 queue);
345 else
346 queue->flags |= UVC_QUEUE_PAUSED;
348 return buf;