blob: 31397954c889565d1404d942baefcb09d33e3acd [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
23#include <media/videobuf2-vmalloc.h>
Laurent Pinchartcdda4792010-05-02 20:57:41 +020024
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 *
Bhupesh Sharmad6925222013-03-28 15:11:52 +053033 * 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.
Laurent Pinchartcdda4792010-05-02 20:57:41 +020037 */
38
Bhupesh Sharmad6925222013-03-28 15:11:52 +053039/* -----------------------------------------------------------------------------
40 * videobuf2 queue operations
41 */
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[])
Laurent Pinchartcdda4792010-05-02 20:57:41 +020046{
Bhupesh Sharmad6925222013-03-28 15:11:52 +053047 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
48 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
49
50 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
51 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
52
53 *nplanes = 1;
54
55 sizes[0] = video->imagesize;
56
57 return 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +020058}
59
Bhupesh Sharmad6925222013-03-28 15:11:52 +053060static int uvc_buffer_prepare(struct vb2_buffer *vb)
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030061{
Bhupesh Sharmad6925222013-03-28 15:11:52 +053062 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
63 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030064
Bhupesh Sharmad6925222013-03-28 15:11:52 +053065 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;
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030069 }
70
Bhupesh Sharmad6925222013-03-28 15:11:52 +053071 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
72 return -ENODEV;
73
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);
81
82 return 0;
83}
84
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;
90
91 spin_lock_irqsave(&queue->irqlock, flags);
92
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);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300101 }
102
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530103 spin_unlock_irqrestore(&queue->irqlock, flags);
104}
105
106static struct vb2_ops uvc_queue_qops = {
107 .queue_setup = uvc_queue_setup,
108 .buf_prepare = uvc_buffer_prepare,
109 .buf_queue = uvc_buffer_queue,
110};
111
112static int uvc_queue_init(struct uvc_video_queue *queue,
113 enum v4l2_buf_type type)
114{
115 int ret;
116
117 queue->queue.type = type;
118 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
119 queue->queue.drv_priv = queue;
120 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
121 queue->queue.ops = &uvc_queue_qops;
122 queue->queue.mem_ops = &vb2_vmalloc_memops;
123 ret = vb2_queue_init(&queue->queue);
124 if (ret)
125 return ret;
126
127 mutex_init(&queue->mutex);
128 spin_lock_init(&queue->irqlock);
129 INIT_LIST_HEAD(&queue->irqqueue);
130 queue->flags = 0;
131
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300132 return 0;
133}
134
135/*
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530136 * Free the video buffers.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200137 */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530138static void uvc_free_buffers(struct uvc_video_queue *queue)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200139{
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200140 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530141 vb2_queue_release(&queue->queue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200142 mutex_unlock(&queue->mutex);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200143}
144
145/*
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530146 * Allocate the video buffers.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200147 */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530148static int uvc_alloc_buffers(struct uvc_video_queue *queue,
149 struct v4l2_requestbuffers *rb)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200150{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530151 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200152
153 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530154 ret = vb2_reqbufs(&queue->queue, rb);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200155 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530156
157 return ret ? ret : rb->count;
158}
159
160static int uvc_query_buffer(struct uvc_video_queue *queue,
161 struct v4l2_buffer *buf)
162{
163 int ret;
164
165 mutex_lock(&queue->mutex);
166 ret = vb2_querybuf(&queue->queue, buf);
167 mutex_unlock(&queue->mutex);
168
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200169 return ret;
170}
171
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530172static int uvc_queue_buffer(struct uvc_video_queue *queue,
173 struct v4l2_buffer *buf)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200174{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530175 unsigned long flags;
176 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200177
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530178 mutex_lock(&queue->mutex);
179 ret = vb2_qbuf(&queue->queue, buf);
180 spin_lock_irqsave(&queue->irqlock, flags);
181 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
182 queue->flags &= ~UVC_QUEUE_PAUSED;
183 spin_unlock_irqrestore(&queue->irqlock, flags);
184 mutex_unlock(&queue->mutex);
185
186 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200187}
188
189/*
190 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
191 * available.
192 */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530193static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
194 struct v4l2_buffer *buf, int nonblocking)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200195{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530196 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200197
198 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530199 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200200 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530201
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200202 return ret;
203}
204
205/*
206 * Poll the video queue.
207 *
208 * This function implements video queue polling and is intended to be used by
209 * the device poll handler.
210 */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530211static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
212 struct file *file, poll_table *wait)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200213{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530214 unsigned int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200215
216 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530217 ret = vb2_poll(&queue->queue, file, wait);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200218 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530219
220 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200221}
222
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530223static int uvc_queue_mmap(struct uvc_video_queue *queue,
224 struct vm_area_struct *vma)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200225{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530226 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200227
228 mutex_lock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530229 ret = vb2_mmap(&queue->queue, vma);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200230 mutex_unlock(&queue->mutex);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530231
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200232 return ret;
233}
234
235/*
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300236 * Cancel the video buffers queue.
237 *
238 * Cancelling the queue marks all buffers on the irq queue as erroneous,
239 * wakes them up and removes them from the queue.
240 *
241 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
242 * fail with -ENODEV.
243 *
244 * This function acquires the irq spinlock and can be called from interrupt
245 * context.
246 */
247static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
248{
249 struct uvc_buffer *buf;
250 unsigned long flags;
251
252 spin_lock_irqsave(&queue->irqlock, flags);
253 while (!list_empty(&queue->irqqueue)) {
254 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
255 queue);
256 list_del(&buf->queue);
257 buf->state = UVC_BUF_STATE_ERROR;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530258 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300259 }
260 /* This must be protected by the irqlock spinlock to avoid race
261 * conditions between uvc_queue_buffer and the disconnection event that
262 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
263 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
264 * state outside the queue code.
265 */
266 if (disconnect)
267 queue->flags |= UVC_QUEUE_DISCONNECTED;
268 spin_unlock_irqrestore(&queue->irqlock, flags);
269}
270
271/*
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200272 * Enable or disable the video buffers queue.
273 *
274 * The queue must be enabled before starting video acquisition and must be
275 * disabled after stopping it. This ensures that the video buffers queue
276 * state can be properly initialized before buffers are accessed from the
277 * interrupt handler.
278 *
279 * Enabling the video queue initializes parameters (such as sequence number,
280 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
281 *
282 * Disabling the video queue cancels the queue and removes all buffers from
283 * the main queue.
284 *
285 * This function can't be called from interrupt context. Use
286 * uvc_queue_cancel() instead.
287 */
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300288static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200289{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530290 unsigned long flags;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200291 int ret = 0;
292
293 mutex_lock(&queue->mutex);
294 if (enable) {
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530295 ret = vb2_streamon(&queue->queue, queue->queue.type);
296 if (ret < 0)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200297 goto done;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530298
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200299 queue->sequence = 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200300 queue->buf_used = 0;
301 } else {
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530302 ret = vb2_streamoff(&queue->queue, queue->queue.type);
303 if (ret < 0)
304 goto done;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200305
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530306 spin_lock_irqsave(&queue->irqlock, flags);
307 INIT_LIST_HEAD(&queue->irqqueue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200308
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530309 /*
310 * FIXME: We need to clear the DISCONNECTED flag to ensure that
311 * applications will be able to queue buffers for the next
312 * streaming run. However, clearing it here doesn't guarantee
313 * that the device will be reconnected in the meantime.
314 */
315 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
316 spin_unlock_irqrestore(&queue->irqlock, flags);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200317 }
318
319done:
320 mutex_unlock(&queue->mutex);
321 return ret;
322}
323
Bhupesh Sharma95faf822012-03-22 00:50:37 -0300324/* called with &queue_irqlock held.. */
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530325static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
326 struct uvc_buffer *buf)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200327{
328 struct uvc_buffer *nextbuf;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200329
330 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530331 buf->length != buf->bytesused) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200332 buf->state = UVC_BUF_STATE_QUEUED;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530333 vb2_set_plane_payload(&buf->buf, 0, 0);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200334 return buf;
335 }
336
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200337 list_del(&buf->queue);
338 if (!list_empty(&queue->irqqueue))
339 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
340 queue);
341 else
342 nextbuf = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200343
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530344 /*
345 * FIXME: with videobuf2, the sequence number or timestamp fields
346 * are valid only for video capture devices and the UVC gadget usually
347 * is a video output device. Keeping these until the specs are clear on
348 * this aspect.
349 */
350 buf->buf.v4l2_buf.sequence = queue->sequence++;
351 do_gettimeofday(&buf->buf.v4l2_buf.timestamp);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200352
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530353 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
354 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
355
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200356 return nextbuf;
357}
358
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300359static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200360{
361 struct uvc_buffer *buf = NULL;
362
363 if (!list_empty(&queue->irqqueue))
364 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
365 queue);
366 else
367 queue->flags |= UVC_QUEUE_PAUSED;
368
369 return buf;
370}
371