1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *	uvc_queue.c  --  USB Video Class driver - Buffers management
4  *
5  *	Copyright (C) 2005-2010
6  *	    Laurent Pinchart ([email protected])
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 
19 #include <media/v4l2-common.h>
20 #include <media/videobuf2-vmalloc.h>
21 
22 #include "uvc.h"
23 
24 /* ------------------------------------------------------------------------
25  * Video buffers queue management.
26  *
27  * Video queues is initialized by uvcg_queue_init(). The function performs
28  * basic initialization of the uvc_video_queue struct and never fails.
29  *
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
33  * the driver.
34  */
35 
36 /* -----------------------------------------------------------------------------
37  * videobuf2 queue operations
38  */
39 
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[])
43 {
44 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
45 	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
46 	struct usb_composite_dev *cdev = video->uvc->func.config->cdev;
47 
48 	if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
49 		*nbuffers = UVC_MAX_VIDEO_BUFFERS;
50 
51 	*nplanes = 1;
52 
53 	sizes[0] = video->imagesize;
54 
55 	if (cdev->gadget->speed < USB_SPEED_SUPER)
56 		video->uvc_num_requests = 4;
57 	else
58 		video->uvc_num_requests = 64;
59 
60 	return 0;
61 }
62 
63 static int uvc_buffer_prepare(struct vb2_buffer *vb)
64 {
65 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
66 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
67 	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
68 
69 	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
70 	    vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
71 		uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
72 		return -EINVAL;
73 	}
74 
75 	if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
76 		return -ENODEV;
77 
78 	buf->state = UVC_BUF_STATE_QUEUED;
79 	buf->mem = vb2_plane_vaddr(vb, 0);
80 	buf->length = vb2_plane_size(vb, 0);
81 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
82 		buf->bytesused = 0;
83 	else
84 		buf->bytesused = vb2_get_plane_payload(vb, 0);
85 
86 	return 0;
87 }
88 
89 static void uvc_buffer_queue(struct vb2_buffer *vb)
90 {
91 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
92 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
93 	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
94 	unsigned long flags;
95 
96 	spin_lock_irqsave(&queue->irqlock, flags);
97 
98 	if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
99 		list_add_tail(&buf->queue, &queue->irqqueue);
100 	} else {
101 		/* If the device is disconnected return the buffer to userspace
102 		 * directly. The next QBUF call will fail with -ENODEV.
103 		 */
104 		buf->state = UVC_BUF_STATE_ERROR;
105 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
106 	}
107 
108 	spin_unlock_irqrestore(&queue->irqlock, flags);
109 }
110 
111 static const struct vb2_ops uvc_queue_qops = {
112 	.queue_setup = uvc_queue_setup,
113 	.buf_prepare = uvc_buffer_prepare,
114 	.buf_queue = uvc_buffer_queue,
115 	.wait_prepare = vb2_ops_wait_prepare,
116 	.wait_finish = vb2_ops_wait_finish,
117 };
118 
119 int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
120 		    struct mutex *lock)
121 {
122 	int ret;
123 
124 	queue->queue.type = type;
125 	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
126 	queue->queue.drv_priv = queue;
127 	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
128 	queue->queue.ops = &uvc_queue_qops;
129 	queue->queue.lock = lock;
130 	queue->queue.mem_ops = &vb2_vmalloc_memops;
131 	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
132 				     | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
133 	ret = vb2_queue_init(&queue->queue);
134 	if (ret)
135 		return ret;
136 
137 	spin_lock_init(&queue->irqlock);
138 	INIT_LIST_HEAD(&queue->irqqueue);
139 	queue->flags = 0;
140 
141 	return 0;
142 }
143 
144 /*
145  * Free the video buffers.
146  */
147 void uvcg_free_buffers(struct uvc_video_queue *queue)
148 {
149 	vb2_queue_release(&queue->queue);
150 }
151 
152 /*
153  * Allocate the video buffers.
154  */
155 int uvcg_alloc_buffers(struct uvc_video_queue *queue,
156 			      struct v4l2_requestbuffers *rb)
157 {
158 	int ret;
159 
160 	ret = vb2_reqbufs(&queue->queue, rb);
161 
162 	return ret ? ret : rb->count;
163 }
164 
165 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
166 {
167 	return vb2_querybuf(&queue->queue, buf);
168 }
169 
170 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
171 {
172 	unsigned long flags;
173 	int ret;
174 
175 	ret = vb2_qbuf(&queue->queue, NULL, buf);
176 	if (ret < 0)
177 		return ret;
178 
179 	spin_lock_irqsave(&queue->irqlock, flags);
180 	ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
181 	queue->flags &= ~UVC_QUEUE_PAUSED;
182 	spin_unlock_irqrestore(&queue->irqlock, flags);
183 	return ret;
184 }
185 
186 /*
187  * Dequeue a video buffer. If nonblocking is false, block until a buffer is
188  * available.
189  */
190 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
191 			int nonblocking)
192 {
193 	return vb2_dqbuf(&queue->queue, buf, nonblocking);
194 }
195 
196 /*
197  * Poll the video queue.
198  *
199  * This function implements video queue polling and is intended to be used by
200  * the device poll handler.
201  */
202 __poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
203 			     poll_table *wait)
204 {
205 	return vb2_poll(&queue->queue, file, wait);
206 }
207 
208 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
209 {
210 	return vb2_mmap(&queue->queue, vma);
211 }
212 
213 #ifndef CONFIG_MMU
214 /*
215  * Get unmapped area.
216  *
217  * NO-MMU arch need this function to make mmap() work correctly.
218  */
219 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
220 					   unsigned long pgoff)
221 {
222 	return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
223 }
224 #endif
225 
226 /*
227  * Cancel the video buffers queue.
228  *
229  * Cancelling the queue marks all buffers on the irq queue as erroneous,
230  * wakes them up and removes them from the queue.
231  *
232  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
233  * fail with -ENODEV.
234  *
235  * This function acquires the irq spinlock and can be called from interrupt
236  * context.
237  */
238 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
239 {
240 	struct uvc_buffer *buf;
241 	unsigned long flags;
242 
243 	spin_lock_irqsave(&queue->irqlock, flags);
244 	while (!list_empty(&queue->irqqueue)) {
245 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
246 				       queue);
247 		list_del(&buf->queue);
248 		buf->state = UVC_BUF_STATE_ERROR;
249 		vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
250 	}
251 	/* This must be protected by the irqlock spinlock to avoid race
252 	 * conditions between uvc_queue_buffer and the disconnection event that
253 	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
254 	 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
255 	 * state outside the queue code.
256 	 */
257 	if (disconnect)
258 		queue->flags |= UVC_QUEUE_DISCONNECTED;
259 	spin_unlock_irqrestore(&queue->irqlock, flags);
260 }
261 
262 /*
263  * Enable or disable the video buffers queue.
264  *
265  * The queue must be enabled before starting video acquisition and must be
266  * disabled after stopping it. This ensures that the video buffers queue
267  * state can be properly initialized before buffers are accessed from the
268  * interrupt handler.
269  *
270  * Enabling the video queue initializes parameters (such as sequence number,
271  * sync pattern, ...). If the queue is already enabled, return -EBUSY.
272  *
273  * Disabling the video queue cancels the queue and removes all buffers from
274  * the main queue.
275  *
276  * This function can't be called from interrupt context. Use
277  * uvcg_queue_cancel() instead.
278  */
279 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
280 {
281 	unsigned long flags;
282 	int ret = 0;
283 
284 	if (enable) {
285 		ret = vb2_streamon(&queue->queue, queue->queue.type);
286 		if (ret < 0)
287 			return ret;
288 
289 		queue->sequence = 0;
290 		queue->buf_used = 0;
291 	} else {
292 		ret = vb2_streamoff(&queue->queue, queue->queue.type);
293 		if (ret < 0)
294 			return ret;
295 
296 		spin_lock_irqsave(&queue->irqlock, flags);
297 		INIT_LIST_HEAD(&queue->irqqueue);
298 
299 		/*
300 		 * FIXME: We need to clear the DISCONNECTED flag to ensure that
301 		 * applications will be able to queue buffers for the next
302 		 * streaming run. However, clearing it here doesn't guarantee
303 		 * that the device will be reconnected in the meantime.
304 		 */
305 		queue->flags &= ~UVC_QUEUE_DISCONNECTED;
306 		spin_unlock_irqrestore(&queue->irqlock, flags);
307 	}
308 
309 	return ret;
310 }
311 
312 /* called with &queue_irqlock held.. */
313 struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue,
314 					  struct uvc_buffer *buf)
315 {
316 	struct uvc_buffer *nextbuf;
317 
318 	if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
319 	     buf->length != buf->bytesused) {
320 		buf->state = UVC_BUF_STATE_QUEUED;
321 		vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
322 		return buf;
323 	}
324 
325 	list_del(&buf->queue);
326 	if (!list_empty(&queue->irqqueue))
327 		nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
328 					   queue);
329 	else
330 		nextbuf = NULL;
331 
332 	buf->buf.field = V4L2_FIELD_NONE;
333 	buf->buf.sequence = queue->sequence++;
334 	buf->buf.vb2_buf.timestamp = ktime_get_ns();
335 
336 	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
337 	vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
338 
339 	return nextbuf;
340 }
341 
342 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
343 {
344 	struct uvc_buffer *buf = NULL;
345 
346 	if (!list_empty(&queue->irqqueue))
347 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
348 				       queue);
349 	else
350 		queue->flags |= UVC_QUEUE_PAUSED;
351 
352 	return buf;
353 }
354 
355