1 // SPDX-License-Identifier: GPL-2.0+
3 * uvc_queue.c -- USB Video Class driver - Buffers management
5 * Copyright (C) 2005-2010
9 #include <linux/atomic.h>
10 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/usb.h>
15 #include <linux/videodev2.h>
16 #include <linux/vmalloc.h>
17 #include <linux/wait.h>
19 #include <media/v4l2-common.h>
20 #include <media/videobuf2-dma-sg.h>
21 #include <media/videobuf2-vmalloc.h>
25 /* ------------------------------------------------------------------------
26 * Video buffers queue management.
28 * Video queues is initialized by uvcg_queue_init(). The function performs
29 * basic initialization of the uvc_video_queue struct and never fails.
31 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
32 * the videobuf2 queue operations by serializing calls to videobuf2 and a
33 * spinlock to protect the IRQ queue that holds the buffers to be processed by
37 /* -----------------------------------------------------------------------------
38 * videobuf2 queue operations
41 static int uvc_queue_setup(struct vb2_queue *vq,
42 unsigned int *nbuffers, unsigned int *nplanes,
43 unsigned int sizes[], struct device *alloc_devs[])
45 struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
46 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
47 struct usb_composite_dev *cdev = video->uvc->func.config->cdev;
49 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
50 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
54 sizes[0] = video->imagesize;
56 if (cdev->gadget->speed < USB_SPEED_SUPER)
57 video->uvc_num_requests = 4;
59 video->uvc_num_requests = 64;
64 static int uvc_buffer_prepare(struct vb2_buffer *vb)
66 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
67 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
68 struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
70 if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
71 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
72 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
76 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
79 buf->state = UVC_BUF_STATE_QUEUED;
81 buf->sgt = vb2_dma_sg_plane_desc(vb, 0);
82 buf->sg = buf->sgt->sgl;
84 buf->mem = vb2_plane_vaddr(vb, 0);
86 buf->length = vb2_plane_size(vb, 0);
87 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
90 buf->bytesused = vb2_get_plane_payload(vb, 0);
95 static void uvc_buffer_queue(struct vb2_buffer *vb)
97 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
98 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
99 struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
102 spin_lock_irqsave(&queue->irqlock, flags);
104 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
105 list_add_tail(&buf->queue, &queue->irqqueue);
107 /* If the device is disconnected return the buffer to userspace
108 * directly. The next QBUF call will fail with -ENODEV.
110 buf->state = UVC_BUF_STATE_ERROR;
111 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
114 spin_unlock_irqrestore(&queue->irqlock, flags);
117 static const struct vb2_ops uvc_queue_qops = {
118 .queue_setup = uvc_queue_setup,
119 .buf_prepare = uvc_buffer_prepare,
120 .buf_queue = uvc_buffer_queue,
121 .wait_prepare = vb2_ops_wait_prepare,
122 .wait_finish = vb2_ops_wait_finish,
125 int uvcg_queue_init(struct uvc_video_queue *queue, struct device *dev, enum v4l2_buf_type type,
128 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
129 struct usb_composite_dev *cdev = video->uvc->func.config->cdev;
132 queue->queue.type = type;
133 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
134 queue->queue.drv_priv = queue;
135 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
136 queue->queue.ops = &uvc_queue_qops;
137 queue->queue.lock = lock;
138 if (cdev->gadget->sg_supported) {
139 queue->queue.mem_ops = &vb2_dma_sg_memops;
142 queue->queue.mem_ops = &vb2_vmalloc_memops;
145 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY
146 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
147 queue->queue.dev = dev;
149 ret = vb2_queue_init(&queue->queue);
153 spin_lock_init(&queue->irqlock);
154 INIT_LIST_HEAD(&queue->irqqueue);
161 * Free the video buffers.
163 void uvcg_free_buffers(struct uvc_video_queue *queue)
165 vb2_queue_release(&queue->queue);
169 * Allocate the video buffers.
171 int uvcg_alloc_buffers(struct uvc_video_queue *queue,
172 struct v4l2_requestbuffers *rb)
176 ret = vb2_reqbufs(&queue->queue, rb);
178 return ret ? ret : rb->count;
181 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
183 return vb2_querybuf(&queue->queue, buf);
186 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
191 ret = vb2_qbuf(&queue->queue, NULL, buf);
195 spin_lock_irqsave(&queue->irqlock, flags);
196 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
197 queue->flags &= ~UVC_QUEUE_PAUSED;
198 spin_unlock_irqrestore(&queue->irqlock, flags);
203 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
206 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
209 return vb2_dqbuf(&queue->queue, buf, nonblocking);
213 * Poll the video queue.
215 * This function implements video queue polling and is intended to be used by
216 * the device poll handler.
218 __poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
221 return vb2_poll(&queue->queue, file, wait);
224 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
226 return vb2_mmap(&queue->queue, vma);
233 * NO-MMU arch need this function to make mmap() work correctly.
235 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
238 return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
243 * Cancel the video buffers queue.
245 * Cancelling the queue marks all buffers on the irq queue as erroneous,
246 * wakes them up and removes them from the queue.
248 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
251 * This function acquires the irq spinlock and can be called from interrupt
254 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
256 struct uvc_buffer *buf;
259 spin_lock_irqsave(&queue->irqlock, flags);
260 while (!list_empty(&queue->irqqueue)) {
261 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
263 list_del(&buf->queue);
264 buf->state = UVC_BUF_STATE_ERROR;
265 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
267 /* This must be protected by the irqlock spinlock to avoid race
268 * conditions between uvc_queue_buffer and the disconnection event that
269 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
270 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
271 * state outside the queue code.
274 queue->flags |= UVC_QUEUE_DISCONNECTED;
275 spin_unlock_irqrestore(&queue->irqlock, flags);
279 * Enable or disable the video buffers queue.
281 * The queue must be enabled before starting video acquisition and must be
282 * disabled after stopping it. This ensures that the video buffers queue
283 * state can be properly initialized before buffers are accessed from the
286 * Enabling the video queue initializes parameters (such as sequence number,
287 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
289 * Disabling the video queue cancels the queue and removes all buffers from
292 * This function can't be called from interrupt context. Use
293 * uvcg_queue_cancel() instead.
295 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
301 ret = vb2_streamon(&queue->queue, queue->queue.type);
308 ret = vb2_streamoff(&queue->queue, queue->queue.type);
312 spin_lock_irqsave(&queue->irqlock, flags);
313 INIT_LIST_HEAD(&queue->irqqueue);
316 * FIXME: We need to clear the DISCONNECTED flag to ensure that
317 * applications will be able to queue buffers for the next
318 * streaming run. However, clearing it here doesn't guarantee
319 * that the device will be reconnected in the meantime.
321 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
322 spin_unlock_irqrestore(&queue->irqlock, flags);
328 /* called with &queue_irqlock held.. */
329 struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue,
330 struct uvc_buffer *buf)
332 struct uvc_buffer *nextbuf;
334 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
335 buf->length != buf->bytesused) {
336 buf->state = UVC_BUF_STATE_QUEUED;
337 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
341 list_del(&buf->queue);
342 if (!list_empty(&queue->irqqueue))
343 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
348 buf->buf.field = V4L2_FIELD_NONE;
349 buf->buf.sequence = queue->sequence++;
350 buf->buf.vb2_buf.timestamp = ktime_get_ns();
352 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
353 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
358 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
360 struct uvc_buffer *buf = NULL;
362 if (!list_empty(&queue->irqqueue))
363 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
366 queue->flags |= UVC_QUEUE_PAUSED;