xref: /linux/drivers/media/usb/uvc/uvc_queue.c (revision aec2f682d47c54ef434b2d440992626d80b1ebdc)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      uvc_queue.c  --  USB Video Class driver - Buffers management
4  *
5  *      Copyright (C) 2005-2010
6  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  */
8 
9 #include <linux/atomic.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.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>
20 
21 #include "uvcvideo.h"
22 
23 /* ------------------------------------------------------------------------
24  * Video buffers queue management.
25  *
26  * Video queues is initialized by uvc_queue_init(). The function performs
27  * basic initialization of the uvc_video_queue struct and never fails.
28  *
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
32  * the driver.
33  */
34 
35 static inline struct uvc_buffer *uvc_vbuf_to_buffer(struct vb2_v4l2_buffer *buf)
36 {
37 	return container_of(buf, struct uvc_buffer, buf);
38 }
39 
40 /*
41  * Return all queued buffers to videobuf2 in the requested state.
42  *
43  * This function must be called with the queue spinlock held.
44  */
45 static void __uvc_queue_return_buffers(struct uvc_video_queue *queue,
46 				       enum uvc_buffer_state state)
47 {
48 	enum vb2_buffer_state vb2_state = state == UVC_BUF_STATE_ERROR
49 					? VB2_BUF_STATE_ERROR
50 					: VB2_BUF_STATE_QUEUED;
51 
52 	lockdep_assert_held(&queue->irqlock);
53 
54 	while (!list_empty(&queue->irqqueue)) {
55 		struct uvc_buffer *buf = list_first_entry(&queue->irqqueue,
56 							  struct uvc_buffer,
57 							  queue);
58 		list_del(&buf->queue);
59 		buf->state = state;
60 		vb2_buffer_done(&buf->buf.vb2_buf, vb2_state);
61 	}
62 }
63 
64 static void uvc_queue_return_buffers(struct uvc_video_queue *queue,
65 				     enum uvc_buffer_state state)
66 {
67 	spin_lock_irq(&queue->irqlock);
68 	__uvc_queue_return_buffers(queue, state);
69 	spin_unlock_irq(&queue->irqlock);
70 }
71 
72 /* -----------------------------------------------------------------------------
73  * videobuf2 queue operations
74  */
75 
76 static int uvc_queue_setup(struct vb2_queue *vq,
77 			   unsigned int *nbuffers, unsigned int *nplanes,
78 			   unsigned int sizes[], struct device *alloc_devs[])
79 {
80 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
81 	struct uvc_streaming *stream = queue->stream;
82 	unsigned int size;
83 
84 	switch (vq->type) {
85 	case V4L2_BUF_TYPE_META_CAPTURE:
86 		size = stream->meta.buffersize;
87 		break;
88 
89 	default:
90 		size = stream->ctrl.dwMaxVideoFrameSize;
91 		break;
92 	}
93 
94 	/*
95 	 * When called with plane sizes, validate them. The driver supports
96 	 * single planar formats only, and requires buffers to be large enough
97 	 * to store a complete frame.
98 	 */
99 	if (*nplanes)
100 		return *nplanes != 1 || sizes[0] < size ? -EINVAL : 0;
101 
102 	*nplanes = 1;
103 	sizes[0] = size;
104 	return 0;
105 }
106 
107 static int uvc_buffer_prepare(struct vb2_buffer *vb)
108 {
109 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
110 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
111 	struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
112 
113 	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
114 	    vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
115 		uvc_dbg(queue->stream->dev, CAPTURE,
116 			"[E] Bytes used out of bounds\n");
117 		return -EINVAL;
118 	}
119 
120 	if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
121 		return -ENODEV;
122 
123 	buf->state = UVC_BUF_STATE_QUEUED;
124 	buf->error = 0;
125 	buf->mem = vb2_plane_vaddr(vb, 0);
126 	buf->length = vb2_plane_size(vb, 0);
127 	if (vb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
128 		buf->bytesused = 0;
129 	else
130 		buf->bytesused = vb2_get_plane_payload(vb, 0);
131 
132 	return 0;
133 }
134 
135 static void uvc_buffer_queue(struct vb2_buffer *vb)
136 {
137 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
138 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
139 	struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
140 	unsigned long flags;
141 
142 	spin_lock_irqsave(&queue->irqlock, flags);
143 	if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
144 		kref_init(&buf->ref);
145 		list_add_tail(&buf->queue, &queue->irqqueue);
146 	} else {
147 		/*
148 		 * If the device is disconnected return the buffer to userspace
149 		 * directly. The next QBUF call will fail with -ENODEV.
150 		 */
151 		buf->state = UVC_BUF_STATE_ERROR;
152 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
153 	}
154 
155 	spin_unlock_irqrestore(&queue->irqlock, flags);
156 }
157 
158 static void uvc_buffer_finish(struct vb2_buffer *vb)
159 {
160 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
161 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
162 	struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
163 
164 	if (vb->state == VB2_BUF_STATE_DONE)
165 		uvc_video_clock_update(queue->stream, vbuf, buf);
166 }
167 
168 static int uvc_start_streaming_video(struct vb2_queue *vq, unsigned int count)
169 {
170 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
171 	struct uvc_streaming *stream = queue->stream;
172 	int ret;
173 
174 	lockdep_assert_irqs_enabled();
175 
176 	ret = uvc_pm_get(stream->dev);
177 	if (ret)
178 		goto err_buffers;
179 
180 	queue->buf_used = 0;
181 
182 	ret = uvc_video_start_streaming(stream);
183 	if (ret)
184 		goto err_pm;
185 
186 	return 0;
187 
188 err_pm:
189 	uvc_pm_put(stream->dev);
190 err_buffers:
191 	uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED);
192 	return ret;
193 }
194 
195 static void uvc_stop_streaming_video(struct vb2_queue *vq)
196 {
197 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
198 	struct uvc_streaming *stream = queue->stream;
199 
200 	lockdep_assert_irqs_enabled();
201 
202 	uvc_video_stop_streaming(stream);
203 
204 	uvc_pm_put(stream->dev);
205 
206 	uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
207 }
208 
209 static void uvc_stop_streaming_meta(struct vb2_queue *vq)
210 {
211 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
212 
213 	lockdep_assert_irqs_enabled();
214 
215 	uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
216 }
217 
218 static const struct vb2_ops uvc_queue_qops = {
219 	.queue_setup = uvc_queue_setup,
220 	.buf_prepare = uvc_buffer_prepare,
221 	.buf_queue = uvc_buffer_queue,
222 	.buf_finish = uvc_buffer_finish,
223 	.start_streaming = uvc_start_streaming_video,
224 	.stop_streaming = uvc_stop_streaming_video,
225 };
226 
227 static const struct vb2_ops uvc_meta_queue_qops = {
228 	.queue_setup = uvc_queue_setup,
229 	.buf_prepare = uvc_buffer_prepare,
230 	.buf_queue = uvc_buffer_queue,
231 	/*
232 	 * .start_streaming is not provided here. Metadata relies on video
233 	 * streaming being active. If video isn't streaming, then no metadata
234 	 * will arrive either.
235 	 */
236 	.stop_streaming = uvc_stop_streaming_meta,
237 };
238 
239 int uvc_queue_init(struct uvc_streaming *stream, struct uvc_video_queue *queue,
240 		   enum v4l2_buf_type type)
241 {
242 	int ret;
243 
244 	queue->stream = stream;
245 	queue->queue.type = type;
246 	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
247 	queue->queue.drv_priv = queue;
248 	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
249 	queue->queue.mem_ops = &vb2_vmalloc_memops;
250 	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
251 		| V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
252 	queue->queue.lock = &queue->mutex;
253 
254 	switch (type) {
255 	case V4L2_BUF_TYPE_META_CAPTURE:
256 		queue->queue.ops = &uvc_meta_queue_qops;
257 		break;
258 	default:
259 		queue->queue.ops = &uvc_queue_qops;
260 		break;
261 	}
262 
263 	ret = vb2_queue_init(&queue->queue);
264 	if (ret)
265 		return ret;
266 
267 	mutex_init(&queue->mutex);
268 	spin_lock_init(&queue->irqlock);
269 	INIT_LIST_HEAD(&queue->irqqueue);
270 
271 	return 0;
272 }
273 
274 /* -----------------------------------------------------------------------------
275  *
276  */
277 
278 /*
279  * Cancel the video buffers queue.
280  *
281  * Cancelling the queue marks all buffers on the irq queue as erroneous,
282  * wakes them up and removes them from the queue.
283  *
284  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
285  * fail with -ENODEV.
286  *
287  * This function acquires the irq spinlock and can be called from interrupt
288  * context.
289  */
290 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
291 {
292 	unsigned long flags;
293 
294 	spin_lock_irqsave(&queue->irqlock, flags);
295 	__uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
296 	/*
297 	 * This must be protected by the irqlock spinlock to avoid race
298 	 * conditions between uvc_buffer_queue and the disconnection event that
299 	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
300 	 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
301 	 * state outside the queue code.
302 	 */
303 	if (disconnect)
304 		queue->flags |= UVC_QUEUE_DISCONNECTED;
305 	spin_unlock_irqrestore(&queue->irqlock, flags);
306 }
307 
308 /*
309  * uvc_queue_get_current_buffer: Obtain the current working output buffer
310  *
311  * Buffers may span multiple packets, and even URBs, therefore the active buffer
312  * remains on the queue until the EOF marker.
313  */
314 static struct uvc_buffer *
315 __uvc_queue_get_current_buffer(struct uvc_video_queue *queue)
316 {
317 	if (list_empty(&queue->irqqueue))
318 		return NULL;
319 
320 	return list_first_entry(&queue->irqqueue, struct uvc_buffer, queue);
321 }
322 
323 struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue)
324 {
325 	struct uvc_buffer *nextbuf;
326 	unsigned long flags;
327 
328 	spin_lock_irqsave(&queue->irqlock, flags);
329 	nextbuf = __uvc_queue_get_current_buffer(queue);
330 	spin_unlock_irqrestore(&queue->irqlock, flags);
331 
332 	return nextbuf;
333 }
334 
335 /*
336  * uvc_queue_buffer_requeue: Requeue a buffer on our internal irqqueue
337  *
338  * Reuse a buffer through our internal queue without the need to 'prepare'.
339  * The buffer will be returned to userspace through the uvc_buffer_queue call if
340  * the device has been disconnected.
341  */
342 static void uvc_queue_buffer_requeue(struct uvc_video_queue *queue,
343 				     struct uvc_buffer *buf)
344 {
345 	buf->error = 0;
346 	buf->state = UVC_BUF_STATE_QUEUED;
347 	buf->bytesused = 0;
348 	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
349 
350 	uvc_buffer_queue(&buf->buf.vb2_buf);
351 }
352 
353 static void uvc_queue_buffer_complete(struct kref *ref)
354 {
355 	struct uvc_buffer *buf = container_of(ref, struct uvc_buffer, ref);
356 	struct vb2_buffer *vb = &buf->buf.vb2_buf;
357 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
358 
359 	if (buf->error && !uvc_no_drop_param) {
360 		uvc_queue_buffer_requeue(queue, buf);
361 		return;
362 	}
363 
364 	buf->state = buf->error ? UVC_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
365 	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
366 	vb2_buffer_done(&buf->buf.vb2_buf, buf->error ? VB2_BUF_STATE_ERROR :
367 							VB2_BUF_STATE_DONE);
368 }
369 
370 /*
371  * Release a reference on the buffer. Complete the buffer when the last
372  * reference is released.
373  */
374 void uvc_queue_buffer_release(struct uvc_buffer *buf)
375 {
376 	kref_put(&buf->ref, uvc_queue_buffer_complete);
377 }
378 
379 /*
380  * Remove this buffer from the queue. Lifetime will persist while async actions
381  * are still running (if any), and uvc_queue_buffer_release will give the buffer
382  * back to VB2 when all users have completed.
383  */
384 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
385 		struct uvc_buffer *buf)
386 {
387 	struct uvc_buffer *nextbuf;
388 	unsigned long flags;
389 
390 	spin_lock_irqsave(&queue->irqlock, flags);
391 	list_del(&buf->queue);
392 	nextbuf = __uvc_queue_get_current_buffer(queue);
393 	spin_unlock_irqrestore(&queue->irqlock, flags);
394 
395 	uvc_queue_buffer_release(buf);
396 
397 	return nextbuf;
398 }
399