1 // SPDX-License-Identifier: GPL-2.0+
3 * uvc_queue.c -- USB Video Class driver - Buffers management
5 * Copyright (C) 2005-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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 unsigned int req_size;
50 if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
51 *nbuffers = UVC_MAX_VIDEO_BUFFERS;
55 sizes[0] = video->imagesize;
57 req_size = video->ep->maxpacket
58 * max_t(unsigned int, video->ep->maxburst, 1)
61 /* We divide by two, to increase the chance to run
62 * into fewer requests for smaller framesizes.
64 nreq = DIV_ROUND_UP(DIV_ROUND_UP(sizes[0], 2), req_size);
65 nreq = clamp(nreq, 4U, 64U);
66 video->uvc_num_requests = nreq;
71 static int uvc_buffer_prepare(struct vb2_buffer *vb)
73 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
74 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
75 struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
77 if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
78 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
79 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
83 if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
86 buf->state = UVC_BUF_STATE_QUEUED;
88 buf->sgt = vb2_dma_sg_plane_desc(vb, 0);
89 buf->sg = buf->sgt->sgl;
91 buf->mem = vb2_plane_vaddr(vb, 0);
93 buf->length = vb2_plane_size(vb, 0);
94 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
97 buf->bytesused = vb2_get_plane_payload(vb, 0);
102 static void uvc_buffer_queue(struct vb2_buffer *vb)
104 struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
105 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
106 struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
109 spin_lock_irqsave(&queue->irqlock, flags);
111 if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
112 list_add_tail(&buf->queue, &queue->irqqueue);
115 * If the device is disconnected return the buffer to userspace
116 * directly. The next QBUF call will fail with -ENODEV.
118 buf->state = UVC_BUF_STATE_ERROR;
119 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
122 spin_unlock_irqrestore(&queue->irqlock, flags);
125 static const struct vb2_ops uvc_queue_qops = {
126 .queue_setup = uvc_queue_setup,
127 .buf_prepare = uvc_buffer_prepare,
128 .buf_queue = uvc_buffer_queue,
129 .wait_prepare = vb2_ops_wait_prepare,
130 .wait_finish = vb2_ops_wait_finish,
133 int uvcg_queue_init(struct uvc_video_queue *queue, struct device *dev, enum v4l2_buf_type type,
136 struct uvc_video *video = container_of(queue, struct uvc_video, queue);
137 struct usb_composite_dev *cdev = video->uvc->func.config->cdev;
140 queue->queue.type = type;
141 queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
142 queue->queue.drv_priv = queue;
143 queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
144 queue->queue.ops = &uvc_queue_qops;
145 queue->queue.lock = lock;
146 if (cdev->gadget->sg_supported) {
147 queue->queue.mem_ops = &vb2_dma_sg_memops;
150 queue->queue.mem_ops = &vb2_vmalloc_memops;
153 queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY
154 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
155 queue->queue.dev = dev;
157 ret = vb2_queue_init(&queue->queue);
161 spin_lock_init(&queue->irqlock);
162 INIT_LIST_HEAD(&queue->irqqueue);
169 * Free the video buffers.
171 void uvcg_free_buffers(struct uvc_video_queue *queue)
173 vb2_queue_release(&queue->queue);
177 * Allocate the video buffers.
179 int uvcg_alloc_buffers(struct uvc_video_queue *queue,
180 struct v4l2_requestbuffers *rb)
184 ret = vb2_reqbufs(&queue->queue, rb);
186 return ret ? ret : rb->count;
189 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
191 return vb2_querybuf(&queue->queue, buf);
194 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
196 return vb2_qbuf(&queue->queue, NULL, buf);
200 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
203 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
206 return vb2_dqbuf(&queue->queue, buf, nonblocking);
210 * Poll the video queue.
212 * This function implements video queue polling and is intended to be used by
213 * the device poll handler.
215 __poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
218 return vb2_poll(&queue->queue, file, wait);
221 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
223 return vb2_mmap(&queue->queue, vma);
230 * NO-MMU arch need this function to make mmap() work correctly.
232 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
235 return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
240 * Cancel the video buffers queue.
242 * Cancelling the queue marks all buffers on the irq queue as erroneous,
243 * wakes them up and removes them from the queue.
245 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
248 * This function acquires the irq spinlock and can be called from interrupt
251 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
253 struct uvc_buffer *buf;
256 spin_lock_irqsave(&queue->irqlock, flags);
257 while (!list_empty(&queue->irqqueue)) {
258 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
260 list_del(&buf->queue);
261 buf->state = UVC_BUF_STATE_ERROR;
262 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);
307 queue->flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
309 ret = vb2_streamoff(&queue->queue, queue->queue.type);
313 spin_lock_irqsave(&queue->irqlock, flags);
314 INIT_LIST_HEAD(&queue->irqqueue);
317 * FIXME: We need to clear the DISCONNECTED flag to ensure that
318 * applications will be able to queue buffers for the next
319 * streaming run. However, clearing it here doesn't guarantee
320 * that the device will be reconnected in the meantime.
322 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
323 spin_unlock_irqrestore(&queue->irqlock, flags);
329 /* called with &queue_irqlock held.. */
330 void uvcg_complete_buffer(struct uvc_video_queue *queue,
331 struct uvc_buffer *buf)
333 if (queue->flags & UVC_QUEUE_DROP_INCOMPLETE) {
334 queue->flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
335 buf->state = UVC_BUF_STATE_ERROR;
336 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
337 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
341 buf->buf.field = V4L2_FIELD_NONE;
342 buf->buf.sequence = queue->sequence++;
343 buf->buf.vb2_buf.timestamp = ktime_get_ns();
345 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
346 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
349 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
351 struct uvc_buffer *buf = NULL;
353 if (!list_empty(&queue->irqqueue))
354 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,