1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
18 #include <media/videobuf2-v4l2.h>
19 #include <media/videobuf2-vmalloc.h>
23 /* ------------------------------------------------------------------------
24 * Video buffers queue management.
26 * Video queues is initialized by uvc_queue_init(). The function performs
27 * basic initialization of the uvc_video_queue struct and never fails.
29 * Video buffers are managed by videobuf2. The driver uses a mutex to protect
30 * the videobuf2 queue operations by serializing calls to videobuf2 and a
31 * spinlock to protect the IRQ queue that holds the buffers to be processed by
35 static inline struct uvc_streaming
*
36 uvc_queue_to_stream(struct uvc_video_queue
*queue
)
38 return container_of(queue
, struct uvc_streaming
, queue
);
41 static inline struct uvc_buffer
*uvc_vbuf_to_buffer(struct vb2_v4l2_buffer
*buf
)
43 return container_of(buf
, struct uvc_buffer
, buf
);
47 * Return all queued buffers to videobuf2 in the requested state.
49 * This function must be called with the queue spinlock held.
51 static void uvc_queue_return_buffers(struct uvc_video_queue
*queue
,
52 enum uvc_buffer_state state
)
54 enum vb2_buffer_state vb2_state
= state
== UVC_BUF_STATE_ERROR
56 : VB2_BUF_STATE_QUEUED
;
58 while (!list_empty(&queue
->irqqueue
)) {
59 struct uvc_buffer
*buf
= list_first_entry(&queue
->irqqueue
,
62 list_del(&buf
->queue
);
64 vb2_buffer_done(&buf
->buf
.vb2_buf
, vb2_state
);
68 /* -----------------------------------------------------------------------------
69 * videobuf2 queue operations
72 static int uvc_queue_setup(struct vb2_queue
*vq
,
73 unsigned int *nbuffers
, unsigned int *nplanes
,
74 unsigned int sizes
[], struct device
*alloc_devs
[])
76 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vq
);
77 struct uvc_streaming
*stream
;
81 case V4L2_BUF_TYPE_META_CAPTURE
:
82 size
= UVC_METADATA_BUF_SIZE
;
86 stream
= uvc_queue_to_stream(queue
);
87 size
= stream
->ctrl
.dwMaxVideoFrameSize
;
92 * When called with plane sizes, validate them. The driver supports
93 * single planar formats only, and requires buffers to be large enough
94 * to store a complete frame.
97 return *nplanes
!= 1 || sizes
[0] < size
? -EINVAL
: 0;
104 static int uvc_buffer_prepare(struct vb2_buffer
*vb
)
106 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
107 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vb
->vb2_queue
);
108 struct uvc_buffer
*buf
= uvc_vbuf_to_buffer(vbuf
);
110 if (vb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
&&
111 vb2_get_plane_payload(vb
, 0) > vb2_plane_size(vb
, 0)) {
112 uvc_trace(UVC_TRACE_CAPTURE
, "[E] Bytes used out of bounds.\n");
116 if (unlikely(queue
->flags
& UVC_QUEUE_DISCONNECTED
))
119 buf
->state
= UVC_BUF_STATE_QUEUED
;
121 buf
->mem
= vb2_plane_vaddr(vb
, 0);
122 buf
->length
= vb2_plane_size(vb
, 0);
123 if (vb
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
126 buf
->bytesused
= vb2_get_plane_payload(vb
, 0);
131 static void uvc_buffer_queue(struct vb2_buffer
*vb
)
133 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
134 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vb
->vb2_queue
);
135 struct uvc_buffer
*buf
= uvc_vbuf_to_buffer(vbuf
);
138 spin_lock_irqsave(&queue
->irqlock
, flags
);
139 if (likely(!(queue
->flags
& UVC_QUEUE_DISCONNECTED
))) {
140 kref_init(&buf
->ref
);
141 list_add_tail(&buf
->queue
, &queue
->irqqueue
);
143 /* If the device is disconnected return the buffer to userspace
144 * directly. The next QBUF call will fail with -ENODEV.
146 buf
->state
= UVC_BUF_STATE_ERROR
;
147 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
150 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
153 static void uvc_buffer_finish(struct vb2_buffer
*vb
)
155 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
156 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vb
->vb2_queue
);
157 struct uvc_streaming
*stream
= uvc_queue_to_stream(queue
);
158 struct uvc_buffer
*buf
= uvc_vbuf_to_buffer(vbuf
);
160 if (vb
->state
== VB2_BUF_STATE_DONE
)
161 uvc_video_clock_update(stream
, vbuf
, buf
);
164 static int uvc_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
166 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vq
);
167 struct uvc_streaming
*stream
= uvc_queue_to_stream(queue
);
170 lockdep_assert_irqs_enabled();
174 ret
= uvc_video_start_streaming(stream
);
178 spin_lock_irq(&queue
->irqlock
);
179 uvc_queue_return_buffers(queue
, UVC_BUF_STATE_QUEUED
);
180 spin_unlock_irq(&queue
->irqlock
);
185 static void uvc_stop_streaming(struct vb2_queue
*vq
)
187 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vq
);
189 lockdep_assert_irqs_enabled();
191 if (vq
->type
!= V4L2_BUF_TYPE_META_CAPTURE
)
192 uvc_video_stop_streaming(uvc_queue_to_stream(queue
));
194 spin_lock_irq(&queue
->irqlock
);
195 uvc_queue_return_buffers(queue
, UVC_BUF_STATE_ERROR
);
196 spin_unlock_irq(&queue
->irqlock
);
199 static const struct vb2_ops uvc_queue_qops
= {
200 .queue_setup
= uvc_queue_setup
,
201 .buf_prepare
= uvc_buffer_prepare
,
202 .buf_queue
= uvc_buffer_queue
,
203 .buf_finish
= uvc_buffer_finish
,
204 .wait_prepare
= vb2_ops_wait_prepare
,
205 .wait_finish
= vb2_ops_wait_finish
,
206 .start_streaming
= uvc_start_streaming
,
207 .stop_streaming
= uvc_stop_streaming
,
210 static const struct vb2_ops uvc_meta_queue_qops
= {
211 .queue_setup
= uvc_queue_setup
,
212 .buf_prepare
= uvc_buffer_prepare
,
213 .buf_queue
= uvc_buffer_queue
,
214 .wait_prepare
= vb2_ops_wait_prepare
,
215 .wait_finish
= vb2_ops_wait_finish
,
216 .stop_streaming
= uvc_stop_streaming
,
219 int uvc_queue_init(struct uvc_video_queue
*queue
, enum v4l2_buf_type type
,
224 queue
->queue
.type
= type
;
225 queue
->queue
.io_modes
= VB2_MMAP
| VB2_USERPTR
;
226 queue
->queue
.drv_priv
= queue
;
227 queue
->queue
.buf_struct_size
= sizeof(struct uvc_buffer
);
228 queue
->queue
.mem_ops
= &vb2_vmalloc_memops
;
229 queue
->queue
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
230 | V4L2_BUF_FLAG_TSTAMP_SRC_SOE
;
231 queue
->queue
.lock
= &queue
->mutex
;
234 case V4L2_BUF_TYPE_META_CAPTURE
:
235 queue
->queue
.ops
= &uvc_meta_queue_qops
;
238 queue
->queue
.io_modes
|= VB2_DMABUF
;
239 queue
->queue
.ops
= &uvc_queue_qops
;
243 ret
= vb2_queue_init(&queue
->queue
);
247 mutex_init(&queue
->mutex
);
248 spin_lock_init(&queue
->irqlock
);
249 INIT_LIST_HEAD(&queue
->irqqueue
);
250 queue
->flags
= drop_corrupted
? UVC_QUEUE_DROP_CORRUPTED
: 0;
255 void uvc_queue_release(struct uvc_video_queue
*queue
)
257 mutex_lock(&queue
->mutex
);
258 vb2_queue_release(&queue
->queue
);
259 mutex_unlock(&queue
->mutex
);
262 /* -----------------------------------------------------------------------------
263 * V4L2 queue operations
266 int uvc_request_buffers(struct uvc_video_queue
*queue
,
267 struct v4l2_requestbuffers
*rb
)
271 mutex_lock(&queue
->mutex
);
272 ret
= vb2_reqbufs(&queue
->queue
, rb
);
273 mutex_unlock(&queue
->mutex
);
275 return ret
? ret
: rb
->count
;
278 int uvc_query_buffer(struct uvc_video_queue
*queue
, struct v4l2_buffer
*buf
)
282 mutex_lock(&queue
->mutex
);
283 ret
= vb2_querybuf(&queue
->queue
, buf
);
284 mutex_unlock(&queue
->mutex
);
289 int uvc_create_buffers(struct uvc_video_queue
*queue
,
290 struct v4l2_create_buffers
*cb
)
294 mutex_lock(&queue
->mutex
);
295 ret
= vb2_create_bufs(&queue
->queue
, cb
);
296 mutex_unlock(&queue
->mutex
);
301 int uvc_queue_buffer(struct uvc_video_queue
*queue
,
302 struct media_device
*mdev
, struct v4l2_buffer
*buf
)
306 mutex_lock(&queue
->mutex
);
307 ret
= vb2_qbuf(&queue
->queue
, mdev
, buf
);
308 mutex_unlock(&queue
->mutex
);
313 int uvc_export_buffer(struct uvc_video_queue
*queue
,
314 struct v4l2_exportbuffer
*exp
)
318 mutex_lock(&queue
->mutex
);
319 ret
= vb2_expbuf(&queue
->queue
, exp
);
320 mutex_unlock(&queue
->mutex
);
325 int uvc_dequeue_buffer(struct uvc_video_queue
*queue
, struct v4l2_buffer
*buf
,
330 mutex_lock(&queue
->mutex
);
331 ret
= vb2_dqbuf(&queue
->queue
, buf
, nonblocking
);
332 mutex_unlock(&queue
->mutex
);
337 int uvc_queue_streamon(struct uvc_video_queue
*queue
, enum v4l2_buf_type type
)
341 mutex_lock(&queue
->mutex
);
342 ret
= vb2_streamon(&queue
->queue
, type
);
343 mutex_unlock(&queue
->mutex
);
348 int uvc_queue_streamoff(struct uvc_video_queue
*queue
, enum v4l2_buf_type type
)
352 mutex_lock(&queue
->mutex
);
353 ret
= vb2_streamoff(&queue
->queue
, type
);
354 mutex_unlock(&queue
->mutex
);
359 int uvc_queue_mmap(struct uvc_video_queue
*queue
, struct vm_area_struct
*vma
)
361 return vb2_mmap(&queue
->queue
, vma
);
365 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue
*queue
,
368 return vb2_get_unmapped_area(&queue
->queue
, 0, 0, pgoff
, 0);
372 __poll_t
uvc_queue_poll(struct uvc_video_queue
*queue
, struct file
*file
,
377 mutex_lock(&queue
->mutex
);
378 ret
= vb2_poll(&queue
->queue
, file
, wait
);
379 mutex_unlock(&queue
->mutex
);
384 /* -----------------------------------------------------------------------------
389 * Check if buffers have been allocated.
391 int uvc_queue_allocated(struct uvc_video_queue
*queue
)
395 mutex_lock(&queue
->mutex
);
396 allocated
= vb2_is_busy(&queue
->queue
);
397 mutex_unlock(&queue
->mutex
);
403 * Cancel the video buffers queue.
405 * Cancelling the queue marks all buffers on the irq queue as erroneous,
406 * wakes them up and removes them from the queue.
408 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
411 * This function acquires the irq spinlock and can be called from interrupt
414 void uvc_queue_cancel(struct uvc_video_queue
*queue
, int disconnect
)
418 spin_lock_irqsave(&queue
->irqlock
, flags
);
419 uvc_queue_return_buffers(queue
, UVC_BUF_STATE_ERROR
);
420 /* This must be protected by the irqlock spinlock to avoid race
421 * conditions between uvc_buffer_queue and the disconnection event that
422 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
423 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
424 * state outside the queue code.
427 queue
->flags
|= UVC_QUEUE_DISCONNECTED
;
428 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
432 * uvc_queue_get_current_buffer: Obtain the current working output buffer
434 * Buffers may span multiple packets, and even URBs, therefore the active buffer
435 * remains on the queue until the EOF marker.
437 static struct uvc_buffer
*
438 __uvc_queue_get_current_buffer(struct uvc_video_queue
*queue
)
440 if (list_empty(&queue
->irqqueue
))
443 return list_first_entry(&queue
->irqqueue
, struct uvc_buffer
, queue
);
446 struct uvc_buffer
*uvc_queue_get_current_buffer(struct uvc_video_queue
*queue
)
448 struct uvc_buffer
*nextbuf
;
451 spin_lock_irqsave(&queue
->irqlock
, flags
);
452 nextbuf
= __uvc_queue_get_current_buffer(queue
);
453 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
459 * uvc_queue_buffer_requeue: Requeue a buffer on our internal irqqueue
461 * Reuse a buffer through our internal queue without the need to 'prepare'.
462 * The buffer will be returned to userspace through the uvc_buffer_queue call if
463 * the device has been disconnected.
465 static void uvc_queue_buffer_requeue(struct uvc_video_queue
*queue
,
466 struct uvc_buffer
*buf
)
469 buf
->state
= UVC_BUF_STATE_QUEUED
;
471 vb2_set_plane_payload(&buf
->buf
.vb2_buf
, 0, 0);
473 uvc_buffer_queue(&buf
->buf
.vb2_buf
);
476 static void uvc_queue_buffer_complete(struct kref
*ref
)
478 struct uvc_buffer
*buf
= container_of(ref
, struct uvc_buffer
, ref
);
479 struct vb2_buffer
*vb
= &buf
->buf
.vb2_buf
;
480 struct uvc_video_queue
*queue
= vb2_get_drv_priv(vb
->vb2_queue
);
482 if ((queue
->flags
& UVC_QUEUE_DROP_CORRUPTED
) && buf
->error
) {
483 uvc_queue_buffer_requeue(queue
, buf
);
487 buf
->state
= buf
->error
? UVC_BUF_STATE_ERROR
: UVC_BUF_STATE_DONE
;
488 vb2_set_plane_payload(&buf
->buf
.vb2_buf
, 0, buf
->bytesused
);
489 vb2_buffer_done(&buf
->buf
.vb2_buf
, VB2_BUF_STATE_DONE
);
493 * Release a reference on the buffer. Complete the buffer when the last
494 * reference is released.
496 void uvc_queue_buffer_release(struct uvc_buffer
*buf
)
498 kref_put(&buf
->ref
, uvc_queue_buffer_complete
);
502 * Remove this buffer from the queue. Lifetime will persist while async actions
503 * are still running (if any), and uvc_queue_buffer_release will give the buffer
504 * back to VB2 when all users have completed.
506 struct uvc_buffer
*uvc_queue_next_buffer(struct uvc_video_queue
*queue
,
507 struct uvc_buffer
*buf
)
509 struct uvc_buffer
*nextbuf
;
512 spin_lock_irqsave(&queue
->irqlock
, flags
);
513 list_del(&buf
->queue
);
514 nextbuf
= __uvc_queue_get_current_buffer(queue
);
515 spin_unlock_irqrestore(&queue
->irqlock
, flags
);
517 uvc_queue_buffer_release(buf
);