blob: 268be579aa7237efb4fc07f0d4a7c6b0b5ed9eac [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
107static struct vb2_ops uvc_queue_qops = {
108 .queue_setup = uvc_queue_setup,
109 .buf_prepare = uvc_buffer_prepare,
110 .buf_queue = uvc_buffer_queue,
111};
112
113void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
114 int drop_corrupted)
115{
116 queue->queue.type = type;
117 queue->queue.io_modes = VB2_MMAP;
118 queue->queue.drv_priv = queue;
119 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
120 queue->queue.ops = &uvc_queue_qops;
121 queue->queue.mem_ops = &vb2_vmalloc_memops;
122 vb2_queue_init(&queue->queue);
123
124 mutex_init(&queue->mutex);
125 spin_lock_init(&queue->irqlock);
126 INIT_LIST_HEAD(&queue->irqqueue);
127 queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
128}
129
130/* -----------------------------------------------------------------------------
131 * V4L2 queue operations
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300132 */
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300133
134int uvc_alloc_buffers(struct uvc_video_queue *queue,
135 struct v4l2_requestbuffers *rb)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300136{
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300137 int ret;
138
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300139 mutex_lock(&queue->mutex);
140 ret = vb2_reqbufs(&queue->queue, rb);
141 mutex_unlock(&queue->mutex);
142
143 return ret ? ret : rb->count;
144}
145
146void uvc_free_buffers(struct uvc_video_queue *queue)
147{
148 mutex_lock(&queue->mutex);
149 vb2_queue_release(&queue->queue);
150 mutex_unlock(&queue->mutex);
151}
152
153int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
154{
155 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300156
157 mutex_lock(&queue->mutex);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300158 ret = vb2_querybuf(&queue->queue, buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300159 mutex_unlock(&queue->mutex);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300160
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300161 return ret;
162}
163
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300164int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
165{
166 int ret;
167
168 mutex_lock(&queue->mutex);
169 ret = vb2_qbuf(&queue->queue, buf);
170 mutex_unlock(&queue->mutex);
171
172 return ret;
173}
174
175int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
176 int nonblocking)
177{
178 int ret;
179
180 mutex_lock(&queue->mutex);
181 ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
182 mutex_unlock(&queue->mutex);
183
184 return ret;
185}
186
187int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
188{
189 int ret;
190
191 mutex_lock(&queue->mutex);
192 ret = vb2_mmap(&queue->queue, vma);
193 mutex_unlock(&queue->mutex);
194
195 return ret;
196}
197
198unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
199 poll_table *wait)
200{
201 unsigned int ret;
202
203 mutex_lock(&queue->mutex);
204 ret = vb2_poll(&queue->queue, file, wait);
205 mutex_unlock(&queue->mutex);
206
207 return ret;
208}
209
210/* -----------------------------------------------------------------------------
211 *
212 */
213
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300214/*
Laurent Pinchart23ff6042009-06-04 09:26:39 -0300215 * Check if buffers have been allocated.
216 */
217int uvc_queue_allocated(struct uvc_video_queue *queue)
218{
219 int allocated;
220
221 mutex_lock(&queue->mutex);
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300222 allocated = vb2_is_busy(&queue->queue);
Laurent Pinchart23ff6042009-06-04 09:26:39 -0300223 mutex_unlock(&queue->mutex);
224
225 return allocated;
226}
227
Bob Liu72969442011-04-29 07:11:35 -0300228#ifndef CONFIG_MMU
229/*
230 * Get unmapped area.
231 *
232 * NO-MMU arch need this function to make mmap() work correctly.
233 */
234unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
235 unsigned long pgoff)
236{
237 struct uvc_buffer *buffer;
238 unsigned int i;
239 unsigned long ret;
240
241 mutex_lock(&queue->mutex);
242 for (i = 0; i < queue->count; ++i) {
243 buffer = &queue->buffer[i];
244 if ((buffer->buf.m.offset >> PAGE_SHIFT) == pgoff)
245 break;
246 }
247 if (i == queue->count) {
248 ret = -EINVAL;
249 goto done;
250 }
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300251 ret = (unsigned long)buf->mem;
Bob Liu72969442011-04-29 07:11:35 -0300252done:
253 mutex_unlock(&queue->mutex);
254 return ret;
255}
256#endif
257
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300258/*
259 * Enable or disable the video buffers queue.
260 *
261 * The queue must be enabled before starting video acquisition and must be
262 * disabled after stopping it. This ensures that the video buffers queue
263 * state can be properly initialized before buffers are accessed from the
264 * interrupt handler.
265 *
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300266 * Enabling the video queue returns -EBUSY if the queue is already enabled.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300267 *
268 * Disabling the video queue cancels the queue and removes all buffers from
269 * the main queue.
270 *
271 * This function can't be called from interrupt context. Use
272 * uvc_queue_cancel() instead.
273 */
274int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
275{
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300276 unsigned long flags;
277 int ret;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300278
279 mutex_lock(&queue->mutex);
280 if (enable) {
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300281 ret = vb2_streamon(&queue->queue, queue->queue.type);
282 if (ret < 0)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300283 goto done;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300284
Laurent Pinchartff924202008-12-28 22:32:29 -0300285 queue->buf_used = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300286 } else {
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300287 ret = vb2_streamoff(&queue->queue, queue->queue.type);
288 if (ret < 0)
289 goto done;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300290
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300291 spin_lock_irqsave(&queue->irqlock, flags);
292 INIT_LIST_HEAD(&queue->irqqueue);
293 spin_unlock_irqrestore(&queue->irqlock, flags);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300294 }
295
296done:
297 mutex_unlock(&queue->mutex);
298 return ret;
299}
300
301/*
302 * Cancel the video buffers queue.
303 *
304 * Cancelling the queue marks all buffers on the irq queue as erroneous,
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300305 * wakes them up and removes them from the queue.
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300306 *
307 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
308 * fail with -ENODEV.
309 *
310 * This function acquires the irq spinlock and can be called from interrupt
311 * context.
312 */
313void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
314{
315 struct uvc_buffer *buf;
316 unsigned long flags;
317
318 spin_lock_irqsave(&queue->irqlock, flags);
319 while (!list_empty(&queue->irqqueue)) {
320 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
321 queue);
322 list_del(&buf->queue);
323 buf->state = UVC_BUF_STATE_ERROR;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300324 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300325 }
326 /* This must be protected by the irqlock spinlock to avoid race
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300327 * conditions between uvc_buffer_queue and the disconnection event that
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300328 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300329 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300330 * state outside the queue code.
331 */
332 if (disconnect)
333 queue->flags |= UVC_QUEUE_DISCONNECTED;
334 spin_unlock_irqrestore(&queue->irqlock, flags);
335}
336
337struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
338 struct uvc_buffer *buf)
339{
340 struct uvc_buffer *nextbuf;
341 unsigned long flags;
342
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300343 if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
344 buf->error = 0;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300345 buf->state = UVC_BUF_STATE_QUEUED;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300346 vb2_set_plane_payload(&buf->buf, 0, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300347 return buf;
348 }
349
350 spin_lock_irqsave(&queue->irqlock, flags);
351 list_del(&buf->queue);
352 if (!list_empty(&queue->irqqueue))
353 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
354 queue);
355 else
356 nextbuf = NULL;
357 spin_unlock_irqrestore(&queue->irqlock, flags);
358
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300359 buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
360 vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
361 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
362
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300363 return nextbuf;
364}