blob: 8ea8b3b227b497108815118a9f38f297f81b6df3 [file] [log] [blame]
Laurent Pinchartcdda4792010-05-02 20:57:41 +02001/*
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.
Laurent Pinchartcdda4792010-05-02 20:57:41 +020011 */
12
Bhupesh Sharmad6925222013-03-28 15:11:52 +053013#include <linux/atomic.h>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020014#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>
Bhupesh Sharmad6925222013-03-28 15:11:52 +053022
Laurent Pinchart16bf9002014-03-23 16:25:09 +010023#include <media/v4l2-common.h>
Bhupesh Sharmad6925222013-03-28 15:11:52 +053024#include <media/videobuf2-vmalloc.h>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020025
26#include "uvc.h"
27
28/* ------------------------------------------------------------------------
29 * Video buffers queue management.
30 *
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +030031 * Video queues is initialized by uvcg_queue_init(). The function performs
Laurent Pinchartcdda4792010-05-02 20:57:41 +020032 * basic initialization of the uvc_video_queue struct and never fails.
33 *
Bhupesh Sharmad6925222013-03-28 15:11:52 +053034 * 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.
Laurent Pinchartcdda4792010-05-02 20:57:41 +020038 */
39
Bhupesh Sharmad6925222013-03-28 15:11:52 +053040/* -----------------------------------------------------------------------------
41 * videobuf2 queue operations
42 */
43
44static 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[])
Laurent Pinchartcdda4792010-05-02 20:57:41 +020047{
Bhupesh Sharmad6925222013-03-28 15:11:52 +053048 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
49 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
50
51 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
52 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
53
54 *nplanes = 1;
55
56 sizes[0] = video->imagesize;
57
58 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +020059}
60
Bhupesh Sharmad6925222013-03-28 15:11:52 +053061static int uvc_buffer_prepare(struct vb2_buffer *vb)
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030062{
Bhupesh Sharmad6925222013-03-28 15:11:52 +053063 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
64 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030065
Bhupesh Sharmad6925222013-03-28 15:11:52 +053066 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;
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030070 }
71
Bhupesh Sharmad6925222013-03-28 15:11:52 +053072 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
73 return -ENODEV;
74
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);
82
83 return 0;
84}
85
86static void uvc_buffer_queue(struct vb2_buffer *vb)
87{
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;
91
92 spin_lock_irqsave(&queue->irqlock, flags);
93
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.
99 */
100 buf->state = UVC_BUF_STATE_ERROR;
101 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300102 }
103
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530104 spin_unlock_irqrestore(&queue->irqlock, flags);
105}
106
Michael Grzeschika2cc81d2013-02-09 00:54:54 +0100107static void uvc_wait_prepare(struct vb2_queue *vq)
108{
109 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
110
111 mutex_unlock(&queue->mutex);
112}
113
114static void uvc_wait_finish(struct vb2_queue *vq)
115{
116 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
117
118 mutex_lock(&queue->mutex);
119}
120
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530121static struct vb2_ops uvc_queue_qops = {
122 .queue_setup = uvc_queue_setup,
123 .buf_prepare = uvc_buffer_prepare,
124 .buf_queue = uvc_buffer_queue,
Michael Grzeschika2cc81d2013-02-09 00:54:54 +0100125 .wait_prepare = uvc_wait_prepare,
126 .wait_finish = uvc_wait_finish,
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530127};
128
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300129int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type)
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530130{
131 int ret;
132
133 queue->queue.type = type;
Philipp Zabelee7ec7f2014-08-21 16:54:44 +0200134 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530135 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;
Laurent Pinchartc9e44b52014-03-23 16:19:50 +0100139 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
140 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530141 ret = vb2_queue_init(&queue->queue);
142 if (ret)
143 return ret;
144
145 mutex_init(&queue->mutex);
146 spin_lock_init(&queue->irqlock);
147 INIT_LIST_HEAD(&queue->irqqueue);
148 queue->flags = 0;
149
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300150 return 0;
151}
152
153/*
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530154 * Free the video buffers.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200155 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300156void uvcg_free_buffers(struct uvc_video_queue *queue)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200157{
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200158 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530159 vb2_queue_release(&queue->queue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200160 mutex_unlock(&queue->mutex);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200161}
162
163/*
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530164 * Allocate the video buffers.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200165 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300166int uvcg_alloc_buffers(struct uvc_video_queue *queue,
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300167 struct v4l2_requestbuffers *rb)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200168{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530169 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200170
171 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530172 ret = vb2_reqbufs(&queue->queue, rb);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200173 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530174
175 return ret ? ret : rb->count;
176}
177
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300178int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530179{
180 int ret;
181
182 mutex_lock(&queue->mutex);
183 ret = vb2_querybuf(&queue->queue, buf);
184 mutex_unlock(&queue->mutex);
185
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200186 return ret;
187}
188
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300189int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200190{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530191 unsigned long flags;
192 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200193
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530194 mutex_lock(&queue->mutex);
195 ret = vb2_qbuf(&queue->queue, buf);
Laurent Pinchartebe864a2013-04-29 22:18:01 +0200196 if (ret < 0)
197 goto done;
198
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530199 spin_lock_irqsave(&queue->irqlock, flags);
200 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
201 queue->flags &= ~UVC_QUEUE_PAUSED;
202 spin_unlock_irqrestore(&queue->irqlock, flags);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530203
Laurent Pinchartebe864a2013-04-29 22:18:01 +0200204done:
205 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530206 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200207}
208
209/*
210 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
211 * available.
212 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300213int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
214 int nonblocking)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200215{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530216 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200217
218 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530219 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200220 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530221
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200222 return ret;
223}
224
225/*
226 * Poll the video queue.
227 *
228 * This function implements video queue polling and is intended to be used by
229 * the device poll handler.
230 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300231unsigned int uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
232 poll_table *wait)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200233{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530234 unsigned int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200235
236 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530237 ret = vb2_poll(&queue->queue, file, wait);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200238 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530239
240 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200241}
242
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300243int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200244{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530245 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200246
247 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530248 ret = vb2_mmap(&queue->queue, vma);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200249 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530250
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200251 return ret;
252}
253
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530254#ifndef CONFIG_MMU
255/*
256 * Get unmapped area.
257 *
258 * NO-MMU arch need this function to make mmap() work correctly.
259 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300260unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
261 unsigned long pgoff)
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530262{
263 unsigned long ret;
264
265 mutex_lock(&queue->mutex);
266 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
267 mutex_unlock(&queue->mutex);
268 return ret;
269}
270#endif
271
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200272/*
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300273 * Cancel the video buffers queue.
274 *
275 * Cancelling the queue marks all buffers on the irq queue as erroneous,
276 * wakes them up and removes them from the queue.
277 *
278 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
279 * fail with -ENODEV.
280 *
281 * This function acquires the irq spinlock and can be called from interrupt
282 * context.
283 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300284void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300285{
286 struct uvc_buffer *buf;
287 unsigned long flags;
288
289 spin_lock_irqsave(&queue->irqlock, flags);
290 while (!list_empty(&queue->irqqueue)) {
291 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
292 queue);
293 list_del(&buf->queue);
294 buf->state = UVC_BUF_STATE_ERROR;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530295 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300296 }
297 /* This must be protected by the irqlock spinlock to avoid race
298 * conditions between uvc_queue_buffer and the disconnection event that
299 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
300 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
301 * state outside the queue code.
302 */
303 if (disconnect)
304 queue->flags |= UVC_QUEUE_DISCONNECTED;
305 spin_unlock_irqrestore(&queue->irqlock, flags);
306}
307
308/*
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200309 * Enable or disable the video buffers queue.
310 *
311 * The queue must be enabled before starting video acquisition and must be
312 * disabled after stopping it. This ensures that the video buffers queue
313 * state can be properly initialized before buffers are accessed from the
314 * interrupt handler.
315 *
316 * Enabling the video queue initializes parameters (such as sequence number,
317 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
318 *
319 * Disabling the video queue cancels the queue and removes all buffers from
320 * the main queue.
321 *
322 * This function can't be called from interrupt context. Use
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300323 * uvcg_queue_cancel() instead.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200324 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300325int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200326{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530327 unsigned long flags;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200328 int ret = 0;
329
330 mutex_lock(&queue->mutex);
331 if (enable) {
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530332 ret = vb2_streamon(&queue->queue, queue->queue.type);
333 if (ret < 0)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200334 goto done;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530335
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200336 queue->sequence = 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200337 queue->buf_used = 0;
338 } else {
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530339 ret = vb2_streamoff(&queue->queue, queue->queue.type);
340 if (ret < 0)
341 goto done;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200342
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530343 spin_lock_irqsave(&queue->irqlock, flags);
344 INIT_LIST_HEAD(&queue->irqqueue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200345
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530346 /*
347 * FIXME: We need to clear the DISCONNECTED flag to ensure that
348 * applications will be able to queue buffers for the next
349 * streaming run. However, clearing it here doesn't guarantee
350 * that the device will be reconnected in the meantime.
351 */
352 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
353 spin_unlock_irqrestore(&queue->irqlock, flags);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200354 }
355
356done:
357 mutex_unlock(&queue->mutex);
358 return ret;
359}
360
Bhupesh Sharma95faf822012-03-22 00:50:37 -0300361/* called with &queue_irqlock held.. */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300362struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue,
363 struct uvc_buffer *buf)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200364{
365 struct uvc_buffer *nextbuf;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200366
367 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530368 buf->length != buf->bytesused) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200369 buf->state = UVC_BUF_STATE_QUEUED;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530370 vb2_set_plane_payload(&buf->buf, 0, 0);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200371 return buf;
372 }
373
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200374 list_del(&buf->queue);
375 if (!list_empty(&queue->irqqueue))
376 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
377 queue);
378 else
379 nextbuf = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200380
Laurent Pinchartf17388c2014-03-23 16:25:09 +0100381 buf->buf.v4l2_buf.field = V4L2_FIELD_NONE;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530382 buf->buf.v4l2_buf.sequence = queue->sequence++;
Laurent Pinchart16bf9002014-03-23 16:25:09 +0100383 v4l2_get_timestamp(&buf->buf.v4l2_buf.timestamp);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200384
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530385 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
386 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
387
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200388 return nextbuf;
389}
390
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300391struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200392{
393 struct uvc_buffer *buf = NULL;
394
395 if (!list_empty(&queue->irqqueue))
396 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
397 queue);
398 else
399 queue->flags |= UVC_QUEUE_PAUSED;
400
401 return buf;
402}
403