[media] uvcvideo: Add function to convert from queue to stream
[deliverable/linux.git] / drivers / media / usb / uvc / uvc_queue.c
CommitLineData
c0efd232
LP
1/*
2 * uvc_queue.c -- USB Video Class driver - Buffers management
3 *
11fc5baf
LP
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
c0efd232
LP
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.
11 *
12 */
13
6998b6fb 14#include <linux/atomic.h>
c0efd232 15#include <linux/kernel.h>
27ac792c 16#include <linux/mm.h>
c0efd232
LP
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>
6998b6fb 23#include <media/videobuf2-vmalloc.h>
c0efd232
LP
24
25#include "uvcvideo.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 *
6998b6fb
LP
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.
c0efd232
LP
37 */
38
bc75d5a0
LP
39static inline struct uvc_streaming *
40uvc_queue_to_stream(struct uvc_video_queue *queue)
41{
42 return container_of(queue, struct uvc_streaming, queue);
43}
44
6998b6fb
LP
45/* -----------------------------------------------------------------------------
46 * videobuf2 queue operations
8e815e17 47 */
6998b6fb
LP
48
49static 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[])
8e815e17 52{
6998b6fb 53 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
bc75d5a0 54 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
8e815e17 55
bddb9d0e
LP
56 /* Make sure the image size is large enough. */
57 if (fmt && fmt->fmt.pix.sizeimage < stream->ctrl.dwMaxVideoFrameSize)
58 return -EINVAL;
59
6998b6fb
LP
60 *nplanes = 1;
61
bddb9d0e
LP
62 sizes[0] = fmt ? fmt->fmt.pix.sizeimage
63 : stream->ctrl.dwMaxVideoFrameSize;
8e815e17
LP
64
65 return 0;
66}
67
6998b6fb 68static int uvc_buffer_prepare(struct vb2_buffer *vb)
8e815e17 69{
6998b6fb
LP
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);
8e815e17 72
6998b6fb
LP
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;
77 }
c0efd232 78
6998b6fb
LP
79 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
80 return -ENODEV;
c0efd232 81
6998b6fb
LP
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);
c0efd232 90
6998b6fb
LP
91 return 0;
92}
c0efd232 93
6998b6fb
LP
94static void uvc_buffer_queue(struct vb2_buffer *vb)
95{
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;
c0efd232 99
6998b6fb
LP
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.
106 */
107 buf->state = UVC_BUF_STATE_ERROR;
108 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
c0efd232
LP
109 }
110
6998b6fb
LP
111 spin_unlock_irqrestore(&queue->irqlock, flags);
112}
c0efd232 113
06470642 114static void uvc_buffer_finish(struct vb2_buffer *vb)
66847ef0
LP
115{
116 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
bc75d5a0 117 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
66847ef0
LP
118 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
119
9c0863b1
HV
120 if (vb->state == VB2_BUF_STATE_DONE)
121 uvc_video_clock_update(stream, &vb->v4l2_buf, buf);
66847ef0
LP
122}
123
05c79d0d
LP
124static void uvc_wait_prepare(struct vb2_queue *vq)
125{
126 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
127
128 mutex_unlock(&queue->mutex);
129}
130
131static void uvc_wait_finish(struct vb2_queue *vq)
132{
133 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
134
135 mutex_lock(&queue->mutex);
136}
137
6998b6fb
LP
138static struct vb2_ops uvc_queue_qops = {
139 .queue_setup = uvc_queue_setup,
140 .buf_prepare = uvc_buffer_prepare,
141 .buf_queue = uvc_buffer_queue,
66847ef0 142 .buf_finish = uvc_buffer_finish,
05c79d0d
LP
143 .wait_prepare = uvc_wait_prepare,
144 .wait_finish = uvc_wait_finish,
6998b6fb 145};
c0efd232 146
5712661d 147int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
6998b6fb
LP
148 int drop_corrupted)
149{
5712661d
EG
150 int ret;
151
6998b6fb 152 queue->queue.type = type;
ccc4c539 153 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
6998b6fb
LP
154 queue->queue.drv_priv = queue;
155 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
156 queue->queue.ops = &uvc_queue_qops;
157 queue->queue.mem_ops = &vb2_vmalloc_memops;
c767492a
SA
158 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
159 | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
5712661d
EG
160 ret = vb2_queue_init(&queue->queue);
161 if (ret)
162 return ret;
c0efd232 163
6998b6fb
LP
164 mutex_init(&queue->mutex);
165 spin_lock_init(&queue->irqlock);
166 INIT_LIST_HEAD(&queue->irqqueue);
167 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
5712661d
EG
168
169 return 0;
c0efd232
LP
170}
171
6998b6fb
LP
172/* -----------------------------------------------------------------------------
173 * V4L2 queue operations
23ff6043 174 */
6998b6fb
LP
175
176int uvc_alloc_buffers(struct uvc_video_queue *queue,
177 struct v4l2_requestbuffers *rb)
23ff6043 178{
6998b6fb 179 int ret;
23ff6043
LP
180
181 mutex_lock(&queue->mutex);
6998b6fb 182 ret = vb2_reqbufs(&queue->queue, rb);
23ff6043
LP
183 mutex_unlock(&queue->mutex);
184
6998b6fb 185 return ret ? ret : rb->count;
23ff6043
LP
186}
187
6998b6fb 188void uvc_free_buffers(struct uvc_video_queue *queue)
c0efd232 189{
6998b6fb
LP
190 mutex_lock(&queue->mutex);
191 vb2_queue_release(&queue->queue);
192 mutex_unlock(&queue->mutex);
c0efd232
LP
193}
194
6998b6fb 195int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
c0efd232 196{
6998b6fb 197 int ret;
c0efd232
LP
198
199 mutex_lock(&queue->mutex);
6998b6fb 200 ret = vb2_querybuf(&queue->queue, buf);
2c2d264b 201 mutex_unlock(&queue->mutex);
6998b6fb 202
2c2d264b 203 return ret;
c0efd232
LP
204}
205
6e9179e2
PZ
206int uvc_create_buffers(struct uvc_video_queue *queue,
207 struct v4l2_create_buffers *cb)
208{
209 int ret;
210
211 mutex_lock(&queue->mutex);
212 ret = vb2_create_bufs(&queue->queue, cb);
213 mutex_unlock(&queue->mutex);
214
215 return ret;
216}
217
6998b6fb 218int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
c0efd232 219{
6998b6fb 220 int ret;
c0efd232
LP
221
222 mutex_lock(&queue->mutex);
6998b6fb 223 ret = vb2_qbuf(&queue->queue, buf);
c0efd232 224 mutex_unlock(&queue->mutex);
c0efd232 225
6998b6fb 226 return ret;
c0efd232
LP
227}
228
6998b6fb
LP
229int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
230 int nonblocking)
c0efd232 231{
6998b6fb 232 int ret;
c0efd232
LP
233
234 mutex_lock(&queue->mutex);
6998b6fb 235 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
c0efd232 236 mutex_unlock(&queue->mutex);
c0efd232 237
6998b6fb 238 return ret;
4aa27597
LP
239}
240
4aa27597
LP
241int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
242{
6998b6fb 243 int ret;
4aa27597
LP
244
245 mutex_lock(&queue->mutex);
6998b6fb
LP
246 ret = vb2_mmap(&queue->queue, vma);
247 mutex_unlock(&queue->mutex);
4aa27597 248
6998b6fb
LP
249 return ret;
250}
4aa27597 251
4742c82e
LP
252#ifndef CONFIG_MMU
253unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
254 unsigned long pgoff)
255{
256 unsigned long ret;
257
258 mutex_lock(&queue->mutex);
259 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
260 mutex_unlock(&queue->mutex);
261 return ret;
262}
263#endif
264
6998b6fb
LP
265unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
266 poll_table *wait)
267{
268 unsigned int ret;
4aa27597 269
6998b6fb
LP
270 mutex_lock(&queue->mutex);
271 ret = vb2_poll(&queue->queue, file, wait);
4aa27597 272 mutex_unlock(&queue->mutex);
6998b6fb 273
4aa27597
LP
274 return ret;
275}
276
6998b6fb 277/* -----------------------------------------------------------------------------
c0efd232 278 *
c0efd232 279 */
6998b6fb
LP
280
281/*
282 * Check if buffers have been allocated.
283 */
284int uvc_queue_allocated(struct uvc_video_queue *queue)
c0efd232 285{
6998b6fb 286 int allocated;
c0efd232
LP
287
288 mutex_lock(&queue->mutex);
6998b6fb 289 allocated = vb2_is_busy(&queue->queue);
c0efd232 290 mutex_unlock(&queue->mutex);
6998b6fb
LP
291
292 return allocated;
c0efd232
LP
293}
294
295/*
296 * Enable or disable the video buffers queue.
297 *
298 * The queue must be enabled before starting video acquisition and must be
299 * disabled after stopping it. This ensures that the video buffers queue
300 * state can be properly initialized before buffers are accessed from the
301 * interrupt handler.
302 *
650b95fe 303 * Enabling the video queue returns -EBUSY if the queue is already enabled.
c0efd232
LP
304 *
305 * Disabling the video queue cancels the queue and removes all buffers from
306 * the main queue.
307 *
308 * This function can't be called from interrupt context. Use
309 * uvc_queue_cancel() instead.
310 */
311int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
312{
6998b6fb
LP
313 unsigned long flags;
314 int ret;
c0efd232
LP
315
316 mutex_lock(&queue->mutex);
317 if (enable) {
6998b6fb
LP
318 ret = vb2_streamon(&queue->queue, queue->queue.type);
319 if (ret < 0)
c0efd232 320 goto done;
6998b6fb 321
ff924203 322 queue->buf_used = 0;
c0efd232 323 } else {
6998b6fb
LP
324 ret = vb2_streamoff(&queue->queue, queue->queue.type);
325 if (ret < 0)
326 goto done;
c0efd232 327
6998b6fb
LP
328 spin_lock_irqsave(&queue->irqlock, flags);
329 INIT_LIST_HEAD(&queue->irqqueue);
330 spin_unlock_irqrestore(&queue->irqlock, flags);
c0efd232
LP
331 }
332
333done:
334 mutex_unlock(&queue->mutex);
335 return ret;
336}
337
338/*
339 * Cancel the video buffers queue.
340 *
341 * Cancelling the queue marks all buffers on the irq queue as erroneous,
2c2d264b 342 * wakes them up and removes them from the queue.
c0efd232
LP
343 *
344 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
345 * fail with -ENODEV.
346 *
347 * This function acquires the irq spinlock and can be called from interrupt
348 * context.
349 */
350void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
351{
352 struct uvc_buffer *buf;
353 unsigned long flags;
354
355 spin_lock_irqsave(&queue->irqlock, flags);
356 while (!list_empty(&queue->irqqueue)) {
357 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
358 queue);
359 list_del(&buf->queue);
360 buf->state = UVC_BUF_STATE_ERROR;
6998b6fb 361 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
c0efd232
LP
362 }
363 /* This must be protected by the irqlock spinlock to avoid race
6998b6fb 364 * conditions between uvc_buffer_queue and the disconnection event that
c0efd232 365 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
6998b6fb 366 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
c0efd232
LP
367 * state outside the queue code.
368 */
369 if (disconnect)
370 queue->flags |= UVC_QUEUE_DISCONNECTED;
371 spin_unlock_irqrestore(&queue->irqlock, flags);
372}
373
374struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
375 struct uvc_buffer *buf)
376{
377 struct uvc_buffer *nextbuf;
378 unsigned long flags;
379
9bde9f26
LP
380 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
381 buf->error = 0;
c0efd232 382 buf->state = UVC_BUF_STATE_QUEUED;
8a3f0ede 383 buf->bytesused = 0;
6998b6fb 384 vb2_set_plane_payload(&buf->buf, 0, 0);
c0efd232
LP
385 return buf;
386 }
387
388 spin_lock_irqsave(&queue->irqlock, flags);
389 list_del(&buf->queue);
390 if (!list_empty(&queue->irqqueue))
391 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
392 queue);
393 else
394 nextbuf = NULL;
395 spin_unlock_irqrestore(&queue->irqlock, flags);
396
6998b6fb
LP
397 buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
398 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
399 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
400
c0efd232
LP
401 return nextbuf;
402}
This page took 0.792599 seconds and 5 git commands to generate.