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 (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 
19 #include <media/v4l2-common.h>
20 #include <media/videobuf2-dma-sg.h>
21 #include <media/videobuf2-vmalloc.h>
22 
23 #include "uvc.h"
24 
25 /* ------------------------------------------------------------------------
26  * Video buffers queue management.
27  *
28  * Video queues is initialized by uvcg_queue_init(). The function performs
29  * basic initialization of the uvc_video_queue struct and never fails.
30  *
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
34  * the driver.
35  */
36 
37 /* -----------------------------------------------------------------------------
38  * videobuf2 queue operations
39  */
40 
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[])
44 {
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;
48 
49 	if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
50 		*nbuffers = UVC_MAX_VIDEO_BUFFERS;
51 
52 	*nplanes = 1;
53 
54 	sizes[0] = video->imagesize;
55 
56 	if (cdev->gadget->speed < USB_SPEED_SUPER)
57 		video->uvc_num_requests = 4;
58 	else
59 		video->uvc_num_requests = 64;
60 
61 	return 0;
62 }
63 
64 static int uvc_buffer_prepare(struct vb2_buffer *vb)
65 {
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);
69 
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");
73 		return -EINVAL;
74 	}
75 
76 	if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
77 		return -ENODEV;
78 
79 	buf->state = UVC_BUF_STATE_QUEUED;
80 	if (queue->use_sg) {
81 		buf->sgt = vb2_dma_sg_plane_desc(vb, 0);
82 		buf->sg = buf->sgt->sgl;
83 	} else {
84 		buf->mem = vb2_plane_vaddr(vb, 0);
85 	}
86 	buf->length = vb2_plane_size(vb, 0);
87 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
88 		buf->bytesused = 0;
89 	else
90 		buf->bytesused = vb2_get_plane_payload(vb, 0);
91 
92 	return 0;
93 }
94 
95 static void uvc_buffer_queue(struct vb2_buffer *vb)
96 {
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);
100 	unsigned long flags;
101 
102 	spin_lock_irqsave(&queue->irqlock, flags);
103 
104 	if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
105 		list_add_tail(&buf->queue, &queue->irqqueue);
106 	} else {
107 		/* If the device is disconnected return the buffer to userspace
108 		 * directly. The next QBUF call will fail with -ENODEV.
109 		 */
110 		buf->state = UVC_BUF_STATE_ERROR;
111 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
112 	}
113 
114 	spin_unlock_irqrestore(&queue->irqlock, flags);
115 }
116 
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,
123 };
124 
125 int uvcg_queue_init(struct uvc_video_queue *queue, struct device *dev, enum v4l2_buf_type type,
126 		    struct mutex *lock)
127 {
128 	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
129 	struct usb_composite_dev *cdev = video->uvc->func.config->cdev;
130 	int ret;
131 
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;
140 		queue->use_sg = 1;
141 	} else {
142 		queue->queue.mem_ops = &vb2_vmalloc_memops;
143 	}
144 
145 	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY
146 				     | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
147 	queue->queue.dev = dev;
148 
149 	ret = vb2_queue_init(&queue->queue);
150 	if (ret)
151 		return ret;
152 
153 	spin_lock_init(&queue->irqlock);
154 	INIT_LIST_HEAD(&queue->irqqueue);
155 	queue->flags = 0;
156 
157 	return 0;
158 }
159 
160 /*
161  * Free the video buffers.
162  */
163 void uvcg_free_buffers(struct uvc_video_queue *queue)
164 {
165 	vb2_queue_release(&queue->queue);
166 }
167 
168 /*
169  * Allocate the video buffers.
170  */
171 int uvcg_alloc_buffers(struct uvc_video_queue *queue,
172 			      struct v4l2_requestbuffers *rb)
173 {
174 	int ret;
175 
176 	ret = vb2_reqbufs(&queue->queue, rb);
177 
178 	return ret ? ret : rb->count;
179 }
180 
181 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
182 {
183 	return vb2_querybuf(&queue->queue, buf);
184 }
185 
186 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
187 {
188 	return vb2_qbuf(&queue->queue, NULL, buf);
189 }
190 
191 /*
192  * Dequeue a video buffer. If nonblocking is false, block until a buffer is
193  * available.
194  */
195 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
196 			int nonblocking)
197 {
198 	return vb2_dqbuf(&queue->queue, buf, nonblocking);
199 }
200 
201 /*
202  * Poll the video queue.
203  *
204  * This function implements video queue polling and is intended to be used by
205  * the device poll handler.
206  */
207 __poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
208 			     poll_table *wait)
209 {
210 	return vb2_poll(&queue->queue, file, wait);
211 }
212 
213 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
214 {
215 	return vb2_mmap(&queue->queue, vma);
216 }
217 
218 #ifndef CONFIG_MMU
219 /*
220  * Get unmapped area.
221  *
222  * NO-MMU arch need this function to make mmap() work correctly.
223  */
224 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
225 					   unsigned long pgoff)
226 {
227 	return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
228 }
229 #endif
230 
231 /*
232  * Cancel the video buffers queue.
233  *
234  * Cancelling the queue marks all buffers on the irq queue as erroneous,
235  * wakes them up and removes them from the queue.
236  *
237  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
238  * fail with -ENODEV.
239  *
240  * This function acquires the irq spinlock and can be called from interrupt
241  * context.
242  */
243 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
244 {
245 	struct uvc_buffer *buf;
246 	unsigned long flags;
247 
248 	spin_lock_irqsave(&queue->irqlock, flags);
249 	while (!list_empty(&queue->irqqueue)) {
250 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
251 				       queue);
252 		list_del(&buf->queue);
253 		buf->state = UVC_BUF_STATE_ERROR;
254 		vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
255 	}
256 	queue->buf_used = 0;
257 
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.
263 	 */
264 	if (disconnect)
265 		queue->flags |= UVC_QUEUE_DISCONNECTED;
266 	spin_unlock_irqrestore(&queue->irqlock, flags);
267 }
268 
269 /*
270  * Enable or disable the video buffers queue.
271  *
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
275  * interrupt handler.
276  *
277  * Enabling the video queue initializes parameters (such as sequence number,
278  * sync pattern, ...). If the queue is already enabled, return -EBUSY.
279  *
280  * Disabling the video queue cancels the queue and removes all buffers from
281  * the main queue.
282  *
283  * This function can't be called from interrupt context. Use
284  * uvcg_queue_cancel() instead.
285  */
286 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
287 {
288 	unsigned long flags;
289 	int ret = 0;
290 
291 	if (enable) {
292 		ret = vb2_streamon(&queue->queue, queue->queue.type);
293 		if (ret < 0)
294 			return ret;
295 
296 		queue->sequence = 0;
297 		queue->buf_used = 0;
298 	} else {
299 		ret = vb2_streamoff(&queue->queue, queue->queue.type);
300 		if (ret < 0)
301 			return ret;
302 
303 		spin_lock_irqsave(&queue->irqlock, flags);
304 		INIT_LIST_HEAD(&queue->irqqueue);
305 
306 		/*
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.
311 		 */
312 		queue->flags &= ~UVC_QUEUE_DISCONNECTED;
313 		spin_unlock_irqrestore(&queue->irqlock, flags);
314 	}
315 
316 	return ret;
317 }
318 
319 /* called with &queue_irqlock held.. */
320 void uvcg_complete_buffer(struct uvc_video_queue *queue,
321 					  struct uvc_buffer *buf)
322 {
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);
327 		return;
328 	}
329 
330 	buf->buf.field = V4L2_FIELD_NONE;
331 	buf->buf.sequence = queue->sequence++;
332 	buf->buf.vb2_buf.timestamp = ktime_get_ns();
333 
334 	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
335 	vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
336 }
337 
338 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
339 {
340 	struct uvc_buffer *buf = NULL;
341 
342 	if (!list_empty(&queue->irqqueue))
343 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
344 				       queue);
345 
346 	return buf;
347 }
348 
349