blob: 9ac4ffe19014730ce5d43a7faea9f37a66301035 [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 *
31 * Video queues is initialized by uvc_queue_init(). The function performs
32 * 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
129static int uvc_queue_init(struct uvc_video_queue *queue,
130 enum v4l2_buf_type type)
131{
132 int ret;
133
134 queue->queue.type = type;
135 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
136 queue->queue.drv_priv = queue;
137 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
138 queue->queue.ops = &uvc_queue_qops;
139 queue->queue.mem_ops = &vb2_vmalloc_memops;
140 ret = vb2_queue_init(&queue->queue);
141 if (ret)
142 return ret;
143
144 mutex_init(&queue->mutex);
145 spin_lock_init(&queue->irqlock);
146 INIT_LIST_HEAD(&queue->irqqueue);
147 queue->flags = 0;
148
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300149 return 0;
150}
151
152/*
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530153 * Free the video buffers.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200154 */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530155static void uvc_free_buffers(struct uvc_video_queue *queue)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200156{
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200157 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530158 vb2_queue_release(&queue->queue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200159 mutex_unlock(&queue->mutex);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200160}
161
162/*
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530163 * Allocate the video buffers.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200164 */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530165static int uvc_alloc_buffers(struct uvc_video_queue *queue,
166 struct v4l2_requestbuffers *rb)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200167{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530168 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200169
170 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530171 ret = vb2_reqbufs(&queue->queue, rb);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200172 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530173
174 return ret ? ret : rb->count;
175}
176
177static int uvc_query_buffer(struct uvc_video_queue *queue,
178 struct v4l2_buffer *buf)
179{
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
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530189static int uvc_queue_buffer(struct uvc_video_queue *queue,
190 struct v4l2_buffer *buf)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200191{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530192 unsigned long flags;
193 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200194
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530195 mutex_lock(&queue->mutex);
196 ret = vb2_qbuf(&queue->queue, buf);
Laurent Pinchartebe864a2013-04-29 22:18:01 +0200197 if (ret < 0)
198 goto done;
199
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530200 spin_lock_irqsave(&queue->irqlock, flags);
201 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
202 queue->flags &= ~UVC_QUEUE_PAUSED;
203 spin_unlock_irqrestore(&queue->irqlock, flags);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530204
Laurent Pinchartebe864a2013-04-29 22:18:01 +0200205done:
206 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530207 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200208}
209
210/*
211 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
212 * available.
213 */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530214static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
215 struct v4l2_buffer *buf, int nonblocking)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200216{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530217 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200218
219 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530220 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200221 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530222
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200223 return ret;
224}
225
226/*
227 * Poll the video queue.
228 *
229 * This function implements video queue polling and is intended to be used by
230 * the device poll handler.
231 */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530232static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
233 struct file *file, poll_table *wait)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200234{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530235 unsigned int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200236
237 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530238 ret = vb2_poll(&queue->queue, file, wait);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200239 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530240
241 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200242}
243
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530244static int uvc_queue_mmap(struct uvc_video_queue *queue,
245 struct vm_area_struct *vma)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200246{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530247 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200248
249 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530250 ret = vb2_mmap(&queue->queue, vma);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200251 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530252
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200253 return ret;
254}
255
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530256#ifndef CONFIG_MMU
257/*
258 * Get unmapped area.
259 *
260 * NO-MMU arch need this function to make mmap() work correctly.
261 */
262static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
263 unsigned long pgoff)
264{
265 unsigned long ret;
266
267 mutex_lock(&queue->mutex);
268 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
269 mutex_unlock(&queue->mutex);
270 return ret;
271}
272#endif
273
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200274/*
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300275 * Cancel the video buffers queue.
276 *
277 * Cancelling the queue marks all buffers on the irq queue as erroneous,
278 * wakes them up and removes them from the queue.
279 *
280 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
281 * fail with -ENODEV.
282 *
283 * This function acquires the irq spinlock and can be called from interrupt
284 * context.
285 */
286static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
287{
288 struct uvc_buffer *buf;
289 unsigned long flags;
290
291 spin_lock_irqsave(&queue->irqlock, flags);
292 while (!list_empty(&queue->irqqueue)) {
293 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
294 queue);
295 list_del(&buf->queue);
296 buf->state = UVC_BUF_STATE_ERROR;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530297 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300298 }
299 /* This must be protected by the irqlock spinlock to avoid race
300 * conditions between uvc_queue_buffer and the disconnection event that
301 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
302 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
303 * state outside the queue code.
304 */
305 if (disconnect)
306 queue->flags |= UVC_QUEUE_DISCONNECTED;
307 spin_unlock_irqrestore(&queue->irqlock, flags);
308}
309
310/*
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200311 * Enable or disable the video buffers queue.
312 *
313 * The queue must be enabled before starting video acquisition and must be
314 * disabled after stopping it. This ensures that the video buffers queue
315 * state can be properly initialized before buffers are accessed from the
316 * interrupt handler.
317 *
318 * Enabling the video queue initializes parameters (such as sequence number,
319 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
320 *
321 * Disabling the video queue cancels the queue and removes all buffers from
322 * the main queue.
323 *
324 * This function can't be called from interrupt context. Use
325 * uvc_queue_cancel() instead.
326 */
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300327static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200328{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530329 unsigned long flags;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200330 int ret = 0;
331
332 mutex_lock(&queue->mutex);
333 if (enable) {
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530334 ret = vb2_streamon(&queue->queue, queue->queue.type);
335 if (ret < 0)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200336 goto done;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530337
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200338 queue->sequence = 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200339 queue->buf_used = 0;
340 } else {
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530341 ret = vb2_streamoff(&queue->queue, queue->queue.type);
342 if (ret < 0)
343 goto done;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200344
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530345 spin_lock_irqsave(&queue->irqlock, flags);
346 INIT_LIST_HEAD(&queue->irqqueue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200347
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530348 /*
349 * FIXME: We need to clear the DISCONNECTED flag to ensure that
350 * applications will be able to queue buffers for the next
351 * streaming run. However, clearing it here doesn't guarantee
352 * that the device will be reconnected in the meantime.
353 */
354 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
355 spin_unlock_irqrestore(&queue->irqlock, flags);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200356 }
357
358done:
359 mutex_unlock(&queue->mutex);
360 return ret;
361}
362
Bhupesh Sharma95faf822012-03-22 00:50:37 -0300363/* called with &queue_irqlock held.. */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530364static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
365 struct uvc_buffer *buf)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200366{
367 struct uvc_buffer *nextbuf;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200368
369 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530370 buf->length != buf->bytesused) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200371 buf->state = UVC_BUF_STATE_QUEUED;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530372 vb2_set_plane_payload(&buf->buf, 0, 0);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200373 return buf;
374 }
375
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200376 list_del(&buf->queue);
377 if (!list_empty(&queue->irqqueue))
378 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
379 queue);
380 else
381 nextbuf = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200382
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530383 buf->buf.v4l2_buf.sequence = queue->sequence++;
Laurent Pinchart16bf9002014-03-23 16:25:09 +0100384 v4l2_get_timestamp(&buf->buf.v4l2_buf.timestamp);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200385
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530386 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
387 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
388
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200389 return nextbuf;
390}
391
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300392static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200393{
394 struct uvc_buffer *buf = NULL;
395
396 if (!list_empty(&queue->irqqueue))
397 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
398 queue);
399 else
400 queue->flags |= UVC_QUEUE_PAUSED;
401
402 return buf;
403}
404