blob: 5577381b5bf057357c6c4f591e4eb09269c2eb38 [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001/*
2 * uvc_queue.c -- USB Video Class driver - Buffers management
3 *
Laurent Pinchart11fc5ba2010-09-20 06:10:10 -03004 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
Laurent Pinchartc0efd232008-06-30 15:04:50 -03006 *
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
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030014#include <linux/atomic.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030015#include <linux/kernel.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070016#include <linux/mm.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030017#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>
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030023#include <media/videobuf2-vmalloc.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030024
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 *
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030033 * 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 Pinchartc0efd232008-06-30 15:04:50 -030037 */
38
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030039/* -----------------------------------------------------------------------------
40 * videobuf2 queue operations
Laurent Pinchart8e815e172010-11-21 14:46:44 -030041 */
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030042
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 Pinchart8e815e172010-11-21 14:46:44 -030046{
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030047 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
48 struct uvc_streaming *stream =
49 container_of(queue, struct uvc_streaming, queue);
Laurent Pinchart8e815e172010-11-21 14:46:44 -030050
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030051 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
52 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
Laurent Pinchart8e815e172010-11-21 14:46:44 -030053
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030054 *nplanes = 1;
55
56 sizes[0] = stream->ctrl.dwMaxVideoFrameSize;
Laurent Pinchart8e815e172010-11-21 14:46:44 -030057
58 return 0;
59}
60
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030061static int uvc_buffer_prepare(struct vb2_buffer *vb)
Laurent Pinchart8e815e172010-11-21 14:46:44 -030062{
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030063 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 Pinchart8e815e172010-11-21 14:46:44 -030065
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030066 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;
70 }
Laurent Pinchart8e815e172010-11-21 14:46:44 -030071
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030072 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
73 return -ENODEV;
74
75 buf->state = UVC_BUF_STATE_QUEUED;
76 buf->error = 0;
77 buf->mem = vb2_plane_vaddr(vb, 0);
78 buf->length = vb2_plane_size(vb, 0);
79 if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
80 buf->bytesused = 0;
81 else
82 buf->bytesused = vb2_get_plane_payload(vb, 0);
83
84 return 0;
Laurent Pinchart8e815e172010-11-21 14:46:44 -030085}
86
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030087static void uvc_buffer_queue(struct vb2_buffer *vb)
88{
89 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
90 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
91 unsigned long flags;
92
93 spin_lock_irqsave(&queue->irqlock, flags);
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);
102 }
103
104 spin_unlock_irqrestore(&queue->irqlock, flags);
105}
106
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300107static int uvc_buffer_finish(struct vb2_buffer *vb)
108{
109 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
110 struct uvc_streaming *stream =
111 container_of(queue, struct uvc_streaming, queue);
112 struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
113
114 uvc_video_clock_update(stream, &vb->v4l2_buf, buf);
115 return 0;
116}
117
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300118static struct vb2_ops uvc_queue_qops = {
119 .queue_setup = uvc_queue_setup,
120 .buf_prepare = uvc_buffer_prepare,
121 .buf_queue = uvc_buffer_queue,
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300122 .buf_finish = uvc_buffer_finish,
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300123};
124
125void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
126 int drop_corrupted)
127{
128 queue->queue.type = type;
Javier Martinab86e9e2012-01-02 11:12:23 -0300129 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300130 queue->queue.drv_priv = queue;
131 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
132 queue->queue.ops = &uvc_queue_qops;
133 queue->queue.mem_ops = &vb2_vmalloc_memops;
134 vb2_queue_init(&queue->queue);
135
136 mutex_init(&queue->mutex);
137 spin_lock_init(&queue->irqlock);
138 INIT_LIST_HEAD(&queue->irqqueue);
139 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
140}
141
142/* -----------------------------------------------------------------------------
143 * V4L2 queue operations
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300144 */
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300145
146int uvc_alloc_buffers(struct uvc_video_queue *queue,
147 struct v4l2_requestbuffers *rb)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300148{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300149 int ret;
150
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300151 mutex_lock(&queue->mutex);
152 ret = vb2_reqbufs(&queue->queue, rb);
153 mutex_unlock(&queue->mutex);
154
155 return ret ? ret : rb->count;
156}
157
158void uvc_free_buffers(struct uvc_video_queue *queue)
159{
160 mutex_lock(&queue->mutex);
161 vb2_queue_release(&queue->queue);
162 mutex_unlock(&queue->mutex);
163}
164
165int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
166{
167 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300168
169 mutex_lock(&queue->mutex);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300170 ret = vb2_querybuf(&queue->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300171 mutex_unlock(&queue->mutex);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300172
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300173 return ret;
174}
175
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300176int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
177{
178 int ret;
179
180 mutex_lock(&queue->mutex);
181 ret = vb2_qbuf(&queue->queue, buf);
182 mutex_unlock(&queue->mutex);
183
184 return ret;
185}
186
187int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
188 int nonblocking)
189{
190 int ret;
191
192 mutex_lock(&queue->mutex);
193 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
194 mutex_unlock(&queue->mutex);
195
196 return ret;
197}
198
199int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
200{
201 int ret;
202
203 mutex_lock(&queue->mutex);
204 ret = vb2_mmap(&queue->queue, vma);
205 mutex_unlock(&queue->mutex);
206
207 return ret;
208}
209
Laurent Pinchart4742c822012-04-30 08:19:10 -0300210#ifndef CONFIG_MMU
211unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
212 unsigned long pgoff)
213{
214 unsigned long ret;
215
216 mutex_lock(&queue->mutex);
217 ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
218 mutex_unlock(&queue->mutex);
219 return ret;
220}
221#endif
222
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300223unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
224 poll_table *wait)
225{
226 unsigned int ret;
227
228 mutex_lock(&queue->mutex);
229 ret = vb2_poll(&queue->queue, file, wait);
230 mutex_unlock(&queue->mutex);
231
232 return ret;
233}
234
235/* -----------------------------------------------------------------------------
236 *
237 */
238
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300239/*
Laurent Pinchart23ff6042009-06-04 09:26:39 -0300240 * Check if buffers have been allocated.
241 */
242int uvc_queue_allocated(struct uvc_video_queue *queue)
243{
244 int allocated;
245
246 mutex_lock(&queue->mutex);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300247 allocated = vb2_is_busy(&queue->queue);
Laurent Pinchart23ff6042009-06-04 09:26:39 -0300248 mutex_unlock(&queue->mutex);
249
250 return allocated;
251}
252
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300253/*
254 * Enable or disable the video buffers queue.
255 *
256 * The queue must be enabled before starting video acquisition and must be
257 * disabled after stopping it. This ensures that the video buffers queue
258 * state can be properly initialized before buffers are accessed from the
259 * interrupt handler.
260 *
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300261 * Enabling the video queue returns -EBUSY if the queue is already enabled.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300262 *
263 * Disabling the video queue cancels the queue and removes all buffers from
264 * the main queue.
265 *
266 * This function can't be called from interrupt context. Use
267 * uvc_queue_cancel() instead.
268 */
269int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
270{
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300271 unsigned long flags;
272 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300273
274 mutex_lock(&queue->mutex);
275 if (enable) {
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300276 ret = vb2_streamon(&queue->queue, queue->queue.type);
277 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300278 goto done;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300279
Laurent Pinchartff924202008-12-28 22:32:29 -0300280 queue->buf_used = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300281 } else {
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300282 ret = vb2_streamoff(&queue->queue, queue->queue.type);
283 if (ret < 0)
284 goto done;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300285
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300286 spin_lock_irqsave(&queue->irqlock, flags);
287 INIT_LIST_HEAD(&queue->irqqueue);
288 spin_unlock_irqrestore(&queue->irqlock, flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300289 }
290
291done:
292 mutex_unlock(&queue->mutex);
293 return ret;
294}
295
296/*
297 * Cancel the video buffers queue.
298 *
299 * Cancelling the queue marks all buffers on the irq queue as erroneous,
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300300 * wakes them up and removes them from the queue.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300301 *
302 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
303 * fail with -ENODEV.
304 *
305 * This function acquires the irq spinlock and can be called from interrupt
306 * context.
307 */
308void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
309{
310 struct uvc_buffer *buf;
311 unsigned long flags;
312
313 spin_lock_irqsave(&queue->irqlock, flags);
314 while (!list_empty(&queue->irqqueue)) {
315 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
316 queue);
317 list_del(&buf->queue);
318 buf->state = UVC_BUF_STATE_ERROR;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300319 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300320 }
321 /* This must be protected by the irqlock spinlock to avoid race
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300322 * conditions between uvc_buffer_queue and the disconnection event that
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300323 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300324 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300325 * state outside the queue code.
326 */
327 if (disconnect)
328 queue->flags |= UVC_QUEUE_DISCONNECTED;
329 spin_unlock_irqrestore(&queue->irqlock, flags);
330}
331
332struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
333 struct uvc_buffer *buf)
334{
335 struct uvc_buffer *nextbuf;
336 unsigned long flags;
337
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300338 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
339 buf->error = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300340 buf->state = UVC_BUF_STATE_QUEUED;
Jayakrishnan Memana8a3f0ed2012-07-15 10:54:03 -0300341 buf->bytesused = 0;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300342 vb2_set_plane_payload(&buf->buf, 0, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300343 return buf;
344 }
345
346 spin_lock_irqsave(&queue->irqlock, flags);
347 list_del(&buf->queue);
348 if (!list_empty(&queue->irqqueue))
349 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
350 queue);
351 else
352 nextbuf = NULL;
353 spin_unlock_irqrestore(&queue->irqlock, flags);
354
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300355 buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
356 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
357 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
358
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300359 return nextbuf;
360}