usb: musb dma: add cppi41 dma driver
[linux-2.6-block.git] / drivers / usb / gadget / uvc_queue.c
CommitLineData
cdda479f
LP
1/*
2 * uvc_queue.c -- USB Video Class driver - Buffers management
3 *
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
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.
cdda479f
LP
11 */
12
d6925225 13#include <linux/atomic.h>
cdda479f
LP
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>
d6925225
BS
22
23#include <media/videobuf2-vmalloc.h>
cdda479f
LP
24
25#include "uvc.h"
26
27/* ------------------------------------------------------------------------
28 * Video buffers queue management.
29 *
30 * Video queues is initialized by uvc_queue_init(). The function performs
31 * basic initialization of the uvc_video_queue struct and never fails.
32 *
d6925225
BS
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.
cdda479f
LP
37 */
38
d6925225
BS
39/* -----------------------------------------------------------------------------
40 * videobuf2 queue operations
5d9955f8 41 */
d6925225
BS
42
43static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
44 unsigned int *nbuffers, unsigned int *nplanes,
45 unsigned int sizes[], void *alloc_ctxs[])
5d9955f8 46{
d6925225
BS
47 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
48 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
5d9955f8 49
d6925225
BS
50 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
51 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
5d9955f8 52
d6925225
BS
53 *nplanes = 1;
54
55 sizes[0] = video->imagesize;
5d9955f8
LP
56
57 return 0;
58}
59
d6925225 60static int uvc_buffer_prepare(struct vb2_buffer *vb)
cdda479f 61{
d6925225
BS
62 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
63 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
cdda479f 64
d6925225
BS
65 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
66 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
67 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
68 return -EINVAL;
69 }
cdda479f 70
d6925225
BS
71 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
72 return -ENODEV;
cdda479f 73
d6925225
BS
74 buf->state = UVC_BUF_STATE_QUEUED;
75 buf->mem = vb2_plane_vaddr(vb, 0);
76 buf->length = vb2_plane_size(vb, 0);
77 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
78 buf->bytesused = 0;
79 else
80 buf->bytesused = vb2_get_plane_payload(vb, 0);
cdda479f 81
d6925225
BS
82 return 0;
83}
cdda479f 84
d6925225
BS
85static void uvc_buffer_queue(struct vb2_buffer *vb)
86{
87 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
88 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
89 unsigned long flags;
cdda479f 90
d6925225 91 spin_lock_irqsave(&queue->irqlock, flags);
cdda479f 92
d6925225
BS
93 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
94 list_add_tail(&buf->queue, &queue->irqqueue);
95 } else {
96 /* If the device is disconnected return the buffer to userspace
97 * directly. The next QBUF call will fail with -ENODEV.
98 */
99 buf->state = UVC_BUF_STATE_ERROR;
100 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
cdda479f
LP
101 }
102
d6925225 103 spin_unlock_irqrestore(&queue->irqlock, flags);
cdda479f
LP
104}
105
a2cc81d3
MG
106static void uvc_wait_prepare(struct vb2_queue *vq)
107{
108 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
109
110 mutex_unlock(&queue->mutex);
111}
112
113static void uvc_wait_finish(struct vb2_queue *vq)
114{
115 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
116
117 mutex_lock(&queue->mutex);
118}
119
d6925225
BS
120static struct vb2_ops uvc_queue_qops = {
121 .queue_setup = uvc_queue_setup,
122 .buf_prepare = uvc_buffer_prepare,
123 .buf_queue = uvc_buffer_queue,
a2cc81d3
MG
124 .wait_prepare = uvc_wait_prepare,
125 .wait_finish = uvc_wait_finish,
d6925225 126};
cdda479f 127
d6925225
BS
128static int uvc_queue_init(struct uvc_video_queue *queue,
129 enum v4l2_buf_type type)
cdda479f 130{
d6925225 131 int ret;
cdda479f 132
d6925225
BS
133 queue->queue.type = type;
134 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
135 queue->queue.drv_priv = queue;
136 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
137 queue->queue.ops = &uvc_queue_qops;
138 queue->queue.mem_ops = &vb2_vmalloc_memops;
139 ret = vb2_queue_init(&queue->queue);
140 if (ret)
141 return ret;
142
143 mutex_init(&queue->mutex);
144 spin_lock_init(&queue->irqlock);
145 INIT_LIST_HEAD(&queue->irqqueue);
146 queue->flags = 0;
cdda479f 147
d6925225
BS
148 return 0;
149}
cdda479f 150
d6925225
BS
151/*
152 * Free the video buffers.
153 */
154static void uvc_free_buffers(struct uvc_video_queue *queue)
155{
156 mutex_lock(&queue->mutex);
157 vb2_queue_release(&queue->queue);
cdda479f 158 mutex_unlock(&queue->mutex);
cdda479f
LP
159}
160
161/*
d6925225 162 * Allocate the video buffers.
cdda479f 163 */
d6925225
BS
164static int uvc_alloc_buffers(struct uvc_video_queue *queue,
165 struct v4l2_requestbuffers *rb)
cdda479f 166{
d6925225 167 int ret;
cdda479f 168
d6925225
BS
169 mutex_lock(&queue->mutex);
170 ret = vb2_reqbufs(&queue->queue, rb);
171 mutex_unlock(&queue->mutex);
cdda479f 172
d6925225
BS
173 return ret ? ret : rb->count;
174}
cdda479f 175
d6925225
BS
176static int uvc_query_buffer(struct uvc_video_queue *queue,
177 struct v4l2_buffer *buf)
178{
179 int ret;
cdda479f 180
d6925225
BS
181 mutex_lock(&queue->mutex);
182 ret = vb2_querybuf(&queue->queue, buf);
183 mutex_unlock(&queue->mutex);
cdda479f 184
d6925225
BS
185 return ret;
186}
cdda479f 187
d6925225
BS
188static int uvc_queue_buffer(struct uvc_video_queue *queue,
189 struct v4l2_buffer *buf)
190{
191 unsigned long flags;
192 int ret;
cdda479f 193
d6925225
BS
194 mutex_lock(&queue->mutex);
195 ret = vb2_qbuf(&queue->queue, buf);
cdda479f 196 spin_lock_irqsave(&queue->irqlock, flags);
cdda479f
LP
197 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
198 queue->flags &= ~UVC_QUEUE_PAUSED;
cdda479f 199 spin_unlock_irqrestore(&queue->irqlock, flags);
cdda479f 200 mutex_unlock(&queue->mutex);
cdda479f 201
d6925225 202 return ret;
cdda479f
LP
203}
204
205/*
206 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
207 * available.
208 */
d6925225
BS
209static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
210 struct v4l2_buffer *buf, int nonblocking)
cdda479f 211{
d6925225 212 int ret;
cdda479f
LP
213
214 mutex_lock(&queue->mutex);
d6925225 215 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
cdda479f 216 mutex_unlock(&queue->mutex);
d6925225 217
cdda479f
LP
218 return ret;
219}
220
221/*
222 * Poll the video queue.
223 *
224 * This function implements video queue polling and is intended to be used by
225 * the device poll handler.
226 */
d6925225
BS
227static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
228 struct file *file, poll_table *wait)
cdda479f 229{
d6925225 230 unsigned int ret;
cdda479f
LP
231
232 mutex_lock(&queue->mutex);
d6925225 233 ret = vb2_poll(&queue->queue, file, wait);
cdda479f 234 mutex_unlock(&queue->mutex);
cdda479f 235
d6925225 236 return ret;
cdda479f
LP
237}
238
d6925225
BS
239static int uvc_queue_mmap(struct uvc_video_queue *queue,
240 struct vm_area_struct *vma)
cdda479f 241{
d6925225 242 int ret;
cdda479f
LP
243
244 mutex_lock(&queue->mutex);
d6925225 245 ret = vb2_mmap(&queue->queue, vma);
cdda479f 246 mutex_unlock(&queue->mutex);
d6925225 247
cdda479f
LP
248 return ret;
249}
250
2f1d5706
BS
251#ifndef CONFIG_MMU
252/*
253 * Get unmapped area.
254 *
255 * NO-MMU arch need this function to make mmap() work correctly.
256 */
257static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
258 unsigned long pgoff)
259{
260 unsigned long ret;
261
262 mutex_lock(&queue->mutex);
263 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
264 mutex_unlock(&queue->mutex);
265 return ret;
266}
267#endif
268
5d9955f8
LP
269/*
270 * Cancel the video buffers queue.
271 *
272 * Cancelling the queue marks all buffers on the irq queue as erroneous,
273 * wakes them up and removes them from the queue.
274 *
275 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
276 * fail with -ENODEV.
277 *
278 * This function acquires the irq spinlock and can be called from interrupt
279 * context.
280 */
281static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
282{
283 struct uvc_buffer *buf;
284 unsigned long flags;
285
286 spin_lock_irqsave(&queue->irqlock, flags);
287 while (!list_empty(&queue->irqqueue)) {
288 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
289 queue);
290 list_del(&buf->queue);
291 buf->state = UVC_BUF_STATE_ERROR;
d6925225 292 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
5d9955f8
LP
293 }
294 /* This must be protected by the irqlock spinlock to avoid race
295 * conditions between uvc_queue_buffer and the disconnection event that
296 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
297 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
298 * state outside the queue code.
299 */
300 if (disconnect)
301 queue->flags |= UVC_QUEUE_DISCONNECTED;
302 spin_unlock_irqrestore(&queue->irqlock, flags);
303}
304
cdda479f
LP
305/*
306 * Enable or disable the video buffers queue.
307 *
308 * The queue must be enabled before starting video acquisition and must be
309 * disabled after stopping it. This ensures that the video buffers queue
310 * state can be properly initialized before buffers are accessed from the
311 * interrupt handler.
312 *
313 * Enabling the video queue initializes parameters (such as sequence number,
314 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
315 *
316 * Disabling the video queue cancels the queue and removes all buffers from
317 * the main queue.
318 *
319 * This function can't be called from interrupt context. Use
320 * uvc_queue_cancel() instead.
321 */
5d9955f8 322static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
cdda479f 323{
d6925225 324 unsigned long flags;
cdda479f
LP
325 int ret = 0;
326
327 mutex_lock(&queue->mutex);
328 if (enable) {
d6925225
BS
329 ret = vb2_streamon(&queue->queue, queue->queue.type);
330 if (ret < 0)
cdda479f 331 goto done;
d6925225 332
cdda479f 333 queue->sequence = 0;
cdda479f
LP
334 queue->buf_used = 0;
335 } else {
d6925225
BS
336 ret = vb2_streamoff(&queue->queue, queue->queue.type);
337 if (ret < 0)
338 goto done;
cdda479f 339
d6925225
BS
340 spin_lock_irqsave(&queue->irqlock, flags);
341 INIT_LIST_HEAD(&queue->irqqueue);
cdda479f 342
d6925225
BS
343 /*
344 * FIXME: We need to clear the DISCONNECTED flag to ensure that
345 * applications will be able to queue buffers for the next
346 * streaming run. However, clearing it here doesn't guarantee
347 * that the device will be reconnected in the meantime.
348 */
349 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
350 spin_unlock_irqrestore(&queue->irqlock, flags);
cdda479f
LP
351 }
352
353done:
354 mutex_unlock(&queue->mutex);
355 return ret;
356}
357
95faf82b 358/* called with &queue_irqlock held.. */
d6925225
BS
359static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
360 struct uvc_buffer *buf)
cdda479f
LP
361{
362 struct uvc_buffer *nextbuf;
cdda479f
LP
363
364 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
d6925225 365 buf->length != buf->bytesused) {
cdda479f 366 buf->state = UVC_BUF_STATE_QUEUED;
d6925225 367 vb2_set_plane_payload(&buf->buf, 0, 0);
cdda479f
LP
368 return buf;
369 }
370
cdda479f
LP
371 list_del(&buf->queue);
372 if (!list_empty(&queue->irqqueue))
373 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
374 queue);
375 else
376 nextbuf = NULL;
cdda479f 377
d6925225
BS
378 /*
379 * FIXME: with videobuf2, the sequence number or timestamp fields
380 * are valid only for video capture devices and the UVC gadget usually
381 * is a video output device. Keeping these until the specs are clear on
382 * this aspect.
383 */
384 buf->buf.v4l2_buf.sequence = queue->sequence++;
385 do_gettimeofday(&buf->buf.v4l2_buf.timestamp);
386
387 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
388 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
cdda479f 389
cdda479f
LP
390 return nextbuf;
391}
392
5d9955f8 393static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
cdda479f
LP
394{
395 struct uvc_buffer *buf = NULL;
396
397 if (!list_empty(&queue->irqqueue))
398 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
399 queue);
400 else
401 queue->flags |= UVC_QUEUE_PAUSED;
402
403 return buf;
404}
405