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 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)
188 return vb2_qbuf(&queue->queue, NULL, buf);
192 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
195 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
198 return vb2_dqbuf(&queue->queue, buf, nonblocking);
202 * Poll the video queue.
204 * This function implements video queue polling and is intended to be used by
205 * the device poll handler.
207 __poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
210 return vb2_poll(&queue->queue, file, wait);
213 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
215 return vb2_mmap(&queue->queue, vma);
222 * NO-MMU arch need this function to make mmap() work correctly.
224 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
227 return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
232 * Cancel the video buffers queue.
234 * Cancelling the queue marks all buffers on the irq queue as erroneous,
235 * wakes them up and removes them from the queue.
237 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
240 * This function acquires the irq spinlock and can be called from interrupt
243 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
245 struct uvc_buffer *buf;
248 spin_lock_irqsave(&queue->irqlock, flags);
249 while (!list_empty(&queue->irqqueue)) {
250 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
252 list_del(&buf->queue);
253 buf->state = UVC_BUF_STATE_ERROR;
254 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
258 /* This must be protected by the irqlock spinlock to avoid race
259 * conditions between uvc_queue_buffer and the disconnection event that
260 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
261 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
262 * state outside the queue code.
265 queue->flags |= UVC_QUEUE_DISCONNECTED;
266 spin_unlock_irqrestore(&queue->irqlock, flags);
270 * Enable or disable the video buffers queue.
272 * The queue must be enabled before starting video acquisition and must be
273 * disabled after stopping it. This ensures that the video buffers queue
274 * state can be properly initialized before buffers are accessed from the
277 * Enabling the video queue initializes parameters (such as sequence number,
278 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
280 * Disabling the video queue cancels the queue and removes all buffers from
283 * This function can't be called from interrupt context. Use
284 * uvcg_queue_cancel() instead.
286 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
292 ret = vb2_streamon(&queue->queue, queue->queue.type);
299 ret = vb2_streamoff(&queue->queue, queue->queue.type);
303 spin_lock_irqsave(&queue->irqlock, flags);
304 INIT_LIST_HEAD(&queue->irqqueue);
307 * FIXME: We need to clear the DISCONNECTED flag to ensure that
308 * applications will be able to queue buffers for the next
309 * streaming run. However, clearing it here doesn't guarantee
310 * that the device will be reconnected in the meantime.
312 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
313 spin_unlock_irqrestore(&queue->irqlock, flags);
319 /* called with &queue_irqlock held.. */
320 void uvcg_complete_buffer(struct uvc_video_queue *queue,
321 struct uvc_buffer *buf)
323 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
324 buf->length != buf->bytesused) {
325 buf->state = UVC_BUF_STATE_QUEUED;
326 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
330 buf->buf.field = V4L2_FIELD_NONE;
331 buf->buf.sequence = queue->sequence++;
332 buf->buf.vb2_buf.timestamp = ktime_get_ns();
334 vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
335 vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
338 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
340 struct uvc_buffer *buf = NULL;
342 if (!list_empty(&queue->irqqueue))
343 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,