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-vmalloc.h>
24 /* ------------------------------------------------------------------------
25 * Video buffers queue management.
27 * Video queues is initialized by uvcg_queue_init(). The function performs
28 * basic initialization of the uvc_video_queue struct and never fails.
30 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
31 * the videobuf2 queue operations by serializing calls to videobuf2 and a
32 * spinlock to protect the IRQ queue that holds the buffers to be processed by
36 /* -----------------------------------------------------------------------------
37 * videobuf2 queue operations
40 static int uvc_queue_setup(struct vb2_queue
*vq
,
41 unsigned int *nbuffers
, unsigned int *nplanes
,
42 unsigned int sizes
[], struct device
*alloc_devs
[])
44 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vq
);
45 struct uvc_video
*video
= container_of(queue
, struct uvc_video
, queue
);
47 if (*nbuffers
> UVC_MAX_VIDEO_BUFFERS
)
48 *nbuffers
= UVC_MAX_VIDEO_BUFFERS
;
52 sizes
[0] = video
->imagesize
;
57 static int uvc_buffer_prepare(struct vb2_buffer
*vb
)
59 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vb
->vb2_queue
);
60 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
61 struct uvc_buffer
*buf
= container_of(vbuf
, struct uvc_buffer
, buf
);
63 if (vb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
64 vb2_get_plane_payload(vb
, 0) > vb2_plane_size(vb
, 0)) {
65 uvc_trace(UVC_TRACE_CAPTURE
, "[E] Bytes used out of bounds.\n");
69 if (unlikely(queue
->flags
& UVC_QUEUE_DISCONNECTED
))
72 buf
->state
= UVC_BUF_STATE_QUEUED
;
73 buf
->mem
= vb2_plane_vaddr(vb
, 0);
74 buf
->length
= vb2_plane_size(vb
, 0);
75 if (vb
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
78 buf
->bytesused
= vb2_get_plane_payload(vb
, 0);
83 static void uvc_buffer_queue(struct vb2_buffer
*vb
)
85 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vb
->vb2_queue
);
86 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
87 struct uvc_buffer
*buf
= container_of(vbuf
, struct uvc_buffer
, buf
);
90 spin_lock_irqsave(&queue
->irqlock
, flags
);
92 if (likely(!(queue
->flags
& UVC_QUEUE_DISCONNECTED
))) {
93 list_add_tail(&buf
->queue
, &queue
->irqqueue
);
95 /* If the device is disconnected return the buffer to userspace
96 * directly. The next QBUF call will fail with -ENODEV.
98 buf
->state
= UVC_BUF_STATE_ERROR
;
99 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
102 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
105 static const struct vb2_ops uvc_queue_qops
= {
106 .queue_setup
= uvc_queue_setup
,
107 .buf_prepare
= uvc_buffer_prepare
,
108 .buf_queue
= uvc_buffer_queue
,
109 .wait_prepare
= vb2_ops_wait_prepare
,
110 .wait_finish
= vb2_ops_wait_finish
,
113 int uvcg_queue_init(struct uvc_video_queue
*queue
, enum v4l2_buf_type type
,
118 queue
->queue
.type
= type
;
119 queue
->queue
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
120 queue
->queue
.drv_priv
= queue
;
121 queue
->queue
.buf_struct_size
= sizeof(struct uvc_buffer
);
122 queue
->queue
.ops
= &uvc_queue_qops
;
123 queue
->queue
.lock
= lock
;
124 queue
->queue
.mem_ops
= &vb2_vmalloc_memops
;
125 queue
->queue
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
126 | V4L2_BUF_FLAG_TSTAMP_SRC_EOF
;
127 ret
= vb2_queue_init(&queue
->queue
);
131 spin_lock_init(&queue
->irqlock
);
132 INIT_LIST_HEAD(&queue
->irqqueue
);
139 * Free the video buffers.
141 void uvcg_free_buffers(struct uvc_video_queue
*queue
)
143 vb2_queue_release(&queue
->queue
);
147 * Allocate the video buffers.
149 int uvcg_alloc_buffers(struct uvc_video_queue
*queue
,
150 struct v4l2_requestbuffers
*rb
)
154 ret
= vb2_reqbufs(&queue
->queue
, rb
);
156 return ret
? ret
: rb
->count
;
159 int uvcg_query_buffer(struct uvc_video_queue
*queue
, struct v4l2_buffer
*buf
)
161 return vb2_querybuf(&queue
->queue
, buf
);
164 int uvcg_queue_buffer(struct uvc_video_queue
*queue
, struct v4l2_buffer
*buf
)
169 ret
= vb2_qbuf(&queue
->queue
, NULL
, buf
);
173 spin_lock_irqsave(&queue
->irqlock
, flags
);
174 ret
= (queue
->flags
& UVC_QUEUE_PAUSED
) != 0;
175 queue
->flags
&= ~UVC_QUEUE_PAUSED
;
176 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
181 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
184 int uvcg_dequeue_buffer(struct uvc_video_queue
*queue
, struct v4l2_buffer
*buf
,
187 return vb2_dqbuf(&queue
->queue
, buf
, nonblocking
);
191 * Poll the video queue.
193 * This function implements video queue polling and is intended to be used by
194 * the device poll handler.
196 __poll_t
uvcg_queue_poll(struct uvc_video_queue
*queue
, struct file
*file
,
199 return vb2_poll(&queue
->queue
, file
, wait
);
202 int uvcg_queue_mmap(struct uvc_video_queue
*queue
, struct vm_area_struct
*vma
)
204 return vb2_mmap(&queue
->queue
, vma
);
211 * NO-MMU arch need this function to make mmap() work correctly.
213 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue
*queue
,
216 return vb2_get_unmapped_area(&queue
->queue
, 0, 0, pgoff
, 0);
221 * Cancel the video buffers queue.
223 * Cancelling the queue marks all buffers on the irq queue as erroneous,
224 * wakes them up and removes them from the queue.
226 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
229 * This function acquires the irq spinlock and can be called from interrupt
232 void uvcg_queue_cancel(struct uvc_video_queue
*queue
, int disconnect
)
234 struct uvc_buffer
*buf
;
237 spin_lock_irqsave(&queue
->irqlock
, flags
);
238 while (!list_empty(&queue
->irqqueue
)) {
239 buf
= list_first_entry(&queue
->irqqueue
, struct uvc_buffer
,
241 list_del(&buf
->queue
);
242 buf
->state
= UVC_BUF_STATE_ERROR
;
243 vb2_buffer_done(&buf
->buf
.vb2_buf
, VB2_BUF_STATE_ERROR
);
245 /* This must be protected by the irqlock spinlock to avoid race
246 * conditions between uvc_queue_buffer and the disconnection event that
247 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
248 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
249 * state outside the queue code.
252 queue
->flags
|= UVC_QUEUE_DISCONNECTED
;
253 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
257 * Enable or disable the video buffers queue.
259 * The queue must be enabled before starting video acquisition and must be
260 * disabled after stopping it. This ensures that the video buffers queue
261 * state can be properly initialized before buffers are accessed from the
264 * Enabling the video queue initializes parameters (such as sequence number,
265 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
267 * Disabling the video queue cancels the queue and removes all buffers from
270 * This function can't be called from interrupt context. Use
271 * uvcg_queue_cancel() instead.
273 int uvcg_queue_enable(struct uvc_video_queue
*queue
, int enable
)
279 ret
= vb2_streamon(&queue
->queue
, queue
->queue
.type
);
286 ret
= vb2_streamoff(&queue
->queue
, queue
->queue
.type
);
290 spin_lock_irqsave(&queue
->irqlock
, flags
);
291 INIT_LIST_HEAD(&queue
->irqqueue
);
294 * FIXME: We need to clear the DISCONNECTED flag to ensure that
295 * applications will be able to queue buffers for the next
296 * streaming run. However, clearing it here doesn't guarantee
297 * that the device will be reconnected in the meantime.
299 queue
->flags
&= ~UVC_QUEUE_DISCONNECTED
;
300 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
306 /* called with &queue_irqlock held.. */
307 struct uvc_buffer
*uvcg_queue_next_buffer(struct uvc_video_queue
*queue
,
308 struct uvc_buffer
*buf
)
310 struct uvc_buffer
*nextbuf
;
312 if ((queue
->flags
& UVC_QUEUE_DROP_INCOMPLETE
) &&
313 buf
->length
!= buf
->bytesused
) {
314 buf
->state
= UVC_BUF_STATE_QUEUED
;
315 vb2_set_plane_payload(&buf
->buf
.vb2_buf
, 0, 0);
319 list_del(&buf
->queue
);
320 if (!list_empty(&queue
->irqqueue
))
321 nextbuf
= list_first_entry(&queue
->irqqueue
, struct uvc_buffer
,
326 buf
->buf
.field
= V4L2_FIELD_NONE
;
327 buf
->buf
.sequence
= queue
->sequence
++;
328 buf
->buf
.vb2_buf
.timestamp
= ktime_get_ns();
330 vb2_set_plane_payload(&buf
->buf
.vb2_buf
, 0, buf
->bytesused
);
331 vb2_buffer_done(&buf
->buf
.vb2_buf
, VB2_BUF_STATE_DONE
);
336 struct uvc_buffer
*uvcg_queue_head(struct uvc_video_queue
*queue
)
338 struct uvc_buffer
*buf
= NULL
;
340 if (!list_empty(&queue
->irqqueue
))
341 buf
= list_first_entry(&queue
->irqqueue
, struct uvc_buffer
,
344 queue
->flags
|= UVC_QUEUE_PAUSED
;