blob: 6377e9fee6e5988fdc679b7b708d038ede0e710c [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
Hans Verkuildf9ecb02015-10-28 00:50:37 -020044static int uvc_queue_setup(struct vb2_queue *vq,
Bhupesh Sharmad6925222013-03-28 15:11:52 +053045 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -030046 unsigned int sizes[], struct device *alloc_devs[])
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);
Junghak Sung2d700712015-09-22 10:30:30 -030064 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
65 struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030066
Junghak Sung2d700712015-09-22 10:30:30 -030067 if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
Bhupesh Sharmad6925222013-03-28 15:11:52 +053068 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
69 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
70 return -EINVAL;
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030071 }
72
Bhupesh Sharmad6925222013-03-28 15:11:52 +053073 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
74 return -ENODEV;
75
76 buf->state = UVC_BUF_STATE_QUEUED;
77 buf->mem = vb2_plane_vaddr(vb, 0);
78 buf->length = vb2_plane_size(vb, 0);
Junghak Sung2d700712015-09-22 10:30:30 -030079 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Bhupesh Sharmad6925222013-03-28 15:11:52 +053080 buf->bytesused = 0;
81 else
82 buf->bytesused = vb2_get_plane_payload(vb, 0);
83
84 return 0;
85}
86
87static void uvc_buffer_queue(struct vb2_buffer *vb)
88{
89 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -030090 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
91 struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
Bhupesh Sharmad6925222013-03-28 15:11:52 +053092 unsigned long flags;
93
94 spin_lock_irqsave(&queue->irqlock, flags);
95
96 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
97 list_add_tail(&buf->queue, &queue->irqqueue);
98 } else {
99 /* If the device is disconnected return the buffer to userspace
100 * directly. The next QBUF call will fail with -ENODEV.
101 */
102 buf->state = UVC_BUF_STATE_ERROR;
Junghak Sung2d700712015-09-22 10:30:30 -0300103 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300104 }
105
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530106 spin_unlock_irqrestore(&queue->irqlock, flags);
107}
108
109static struct vb2_ops uvc_queue_qops = {
110 .queue_setup = uvc_queue_setup,
111 .buf_prepare = uvc_buffer_prepare,
112 .buf_queue = uvc_buffer_queue,
Hans Verkuild8e96c42015-02-17 05:44:06 -0300113 .wait_prepare = vb2_ops_wait_prepare,
114 .wait_finish = vb2_ops_wait_finish,
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530115};
116
Hans Verkuild8e96c42015-02-17 05:44:06 -0300117int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
118 struct mutex *lock)
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530119{
120 int ret;
121
122 queue->queue.type = type;
Philipp Zabelee7ec7f2014-08-21 16:54:44 +0200123 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530124 queue->queue.drv_priv = queue;
125 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
126 queue->queue.ops = &uvc_queue_qops;
Hans Verkuild8e96c42015-02-17 05:44:06 -0300127 queue->queue.lock = lock;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530128 queue->queue.mem_ops = &vb2_vmalloc_memops;
Laurent Pinchartc9e44b52014-03-23 16:19:50 +0100129 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
130 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530131 ret = vb2_queue_init(&queue->queue);
132 if (ret)
133 return ret;
134
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530135 spin_lock_init(&queue->irqlock);
136 INIT_LIST_HEAD(&queue->irqqueue);
137 queue->flags = 0;
138
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300139 return 0;
140}
141
142/*
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530143 * Free the video buffers.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200144 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300145void uvcg_free_buffers(struct uvc_video_queue *queue)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200146{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530147 vb2_queue_release(&queue->queue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200148}
149
150/*
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530151 * Allocate the video buffers.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200152 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300153int uvcg_alloc_buffers(struct uvc_video_queue *queue,
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300154 struct v4l2_requestbuffers *rb)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200155{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530156 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200157
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530158 ret = vb2_reqbufs(&queue->queue, rb);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530159
160 return ret ? ret : rb->count;
161}
162
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300163int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530164{
Hans Verkuild8e96c42015-02-17 05:44:06 -0300165 return vb2_querybuf(&queue->queue, buf);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200166}
167
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300168int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200169{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530170 unsigned long flags;
171 int ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200172
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530173 ret = vb2_qbuf(&queue->queue, buf);
Laurent Pinchartebe864a2013-04-29 22:18:01 +0200174 if (ret < 0)
Hans Verkuild8e96c42015-02-17 05:44:06 -0300175 return ret;
Laurent Pinchartebe864a2013-04-29 22:18:01 +0200176
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530177 spin_lock_irqsave(&queue->irqlock, flags);
178 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
179 queue->flags &= ~UVC_QUEUE_PAUSED;
180 spin_unlock_irqrestore(&queue->irqlock, flags);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530181 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200182}
183
184/*
185 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
186 * available.
187 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300188int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
189 int nonblocking)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200190{
Hans Verkuild8e96c42015-02-17 05:44:06 -0300191 return vb2_dqbuf(&queue->queue, buf, nonblocking);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200192}
193
194/*
195 * Poll the video queue.
196 *
197 * This function implements video queue polling and is intended to be used by
198 * the device poll handler.
199 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300200unsigned int uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
201 poll_table *wait)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200202{
Hans Verkuild8e96c42015-02-17 05:44:06 -0300203 return vb2_poll(&queue->queue, file, wait);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200204}
205
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300206int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200207{
Hans Verkuild8e96c42015-02-17 05:44:06 -0300208 return vb2_mmap(&queue->queue, vma);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200209}
210
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530211#ifndef CONFIG_MMU
212/*
213 * Get unmapped area.
214 *
215 * NO-MMU arch need this function to make mmap() work correctly.
216 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300217unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
218 unsigned long pgoff)
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530219{
Hans Verkuild8e96c42015-02-17 05:44:06 -0300220 return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
Bhupesh Sharma2f1d5702013-03-28 15:11:53 +0530221}
222#endif
223
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200224/*
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300225 * Cancel the video buffers queue.
226 *
227 * Cancelling the queue marks all buffers on the irq queue as erroneous,
228 * wakes them up and removes them from the queue.
229 *
230 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
231 * fail with -ENODEV.
232 *
233 * This function acquires the irq spinlock and can be called from interrupt
234 * context.
235 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300236void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300237{
238 struct uvc_buffer *buf;
239 unsigned long flags;
240
241 spin_lock_irqsave(&queue->irqlock, flags);
242 while (!list_empty(&queue->irqqueue)) {
243 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
244 queue);
245 list_del(&buf->queue);
246 buf->state = UVC_BUF_STATE_ERROR;
Junghak Sung2d700712015-09-22 10:30:30 -0300247 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
Laurent Pinchart5d9955f2010-07-10 16:13:05 -0300248 }
249 /* This must be protected by the irqlock spinlock to avoid race
250 * conditions between uvc_queue_buffer and the disconnection event that
251 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
252 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
253 * state outside the queue code.
254 */
255 if (disconnect)
256 queue->flags |= UVC_QUEUE_DISCONNECTED;
257 spin_unlock_irqrestore(&queue->irqlock, flags);
258}
259
260/*
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200261 * Enable or disable the video buffers queue.
262 *
263 * The queue must be enabled before starting video acquisition and must be
264 * disabled after stopping it. This ensures that the video buffers queue
265 * state can be properly initialized before buffers are accessed from the
266 * interrupt handler.
267 *
268 * Enabling the video queue initializes parameters (such as sequence number,
269 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
270 *
271 * Disabling the video queue cancels the queue and removes all buffers from
272 * the main queue.
273 *
274 * This function can't be called from interrupt context. Use
Andrzej Pietrasiewicz7ea95b12014-09-09 02:02:08 +0300275 * uvcg_queue_cancel() instead.
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200276 */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300277int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200278{
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530279 unsigned long flags;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200280 int ret = 0;
281
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200282 if (enable) {
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530283 ret = vb2_streamon(&queue->queue, queue->queue.type);
284 if (ret < 0)
Hans Verkuild8e96c42015-02-17 05:44:06 -0300285 return ret;
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530286
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200287 queue->sequence = 0;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200288 queue->buf_used = 0;
289 } else {
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530290 ret = vb2_streamoff(&queue->queue, queue->queue.type);
291 if (ret < 0)
Hans Verkuild8e96c42015-02-17 05:44:06 -0300292 return ret;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200293
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530294 spin_lock_irqsave(&queue->irqlock, flags);
295 INIT_LIST_HEAD(&queue->irqqueue);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200296
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530297 /*
298 * FIXME: We need to clear the DISCONNECTED flag to ensure that
299 * applications will be able to queue buffers for the next
300 * streaming run. However, clearing it here doesn't guarantee
301 * that the device will be reconnected in the meantime.
302 */
303 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
304 spin_unlock_irqrestore(&queue->irqlock, flags);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200305 }
306
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200307 return ret;
308}
309
Bhupesh Sharma95faf822012-03-22 00:50:37 -0300310/* called with &queue_irqlock held.. */
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300311struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue,
312 struct uvc_buffer *buf)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200313{
314 struct uvc_buffer *nextbuf;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200315
316 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530317 buf->length != buf->bytesused) {
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200318 buf->state = UVC_BUF_STATE_QUEUED;
Junghak Sung2d700712015-09-22 10:30:30 -0300319 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200320 return buf;
321 }
322
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200323 list_del(&buf->queue);
324 if (!list_empty(&queue->irqqueue))
325 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
326 queue);
327 else
328 nextbuf = NULL;
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200329
Junghak Sung2d700712015-09-22 10:30:30 -0300330 buf->buf.field = V4L2_FIELD_NONE;
331 buf->buf.sequence = queue->sequence++;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200332 buf->buf.vb2_buf.timestamp = ktime_get_ns();
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200333
Junghak Sung2d700712015-09-22 10:30:30 -0300334 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
335 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
Bhupesh Sharmad6925222013-03-28 15:11:52 +0530336
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200337 return nextbuf;
338}
339
Andrzej Pietrasiewicz3a83c162014-09-09 02:02:09 +0300340struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
Laurent Pinchartcdda4792010-05-02 20:57:41 +0200341{
342 struct uvc_buffer *buf = NULL;
343
344 if (!list_empty(&queue->irqqueue))
345 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
346 queue);
347 else
348 queue->flags |= UVC_QUEUE_PAUSED;
349
350 return buf;
351}
352