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-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 		/*
108 		 * If the device is disconnected return the buffer to userspace
109 		 * directly. The next QBUF call will fail with -ENODEV.
110 		 */
111 		buf->state = UVC_BUF_STATE_ERROR;
112 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
113 	}
114 
115 	spin_unlock_irqrestore(&queue->irqlock, flags);
116 }
117 
118 static const struct vb2_ops uvc_queue_qops = {
119 	.queue_setup = uvc_queue_setup,
120 	.buf_prepare = uvc_buffer_prepare,
121 	.buf_queue = uvc_buffer_queue,
122 	.wait_prepare = vb2_ops_wait_prepare,
123 	.wait_finish = vb2_ops_wait_finish,
124 };
125 
126 int uvcg_queue_init(struct uvc_video_queue *queue, struct device *dev, enum v4l2_buf_type type,
127 		    struct mutex *lock)
128 {
129 	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
130 	struct usb_composite_dev *cdev = video->uvc->func.config->cdev;
131 	int ret;
132 
133 	queue->queue.type = type;
134 	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
135 	queue->queue.drv_priv = queue;
136 	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
137 	queue->queue.ops = &uvc_queue_qops;
138 	queue->queue.lock = lock;
139 	if (cdev->gadget->sg_supported) {
140 		queue->queue.mem_ops = &vb2_dma_sg_memops;
141 		queue->use_sg = 1;
142 	} else {
143 		queue->queue.mem_ops = &vb2_vmalloc_memops;
144 	}
145 
146 	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY
147 				     | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
148 	queue->queue.dev = dev;
149 
150 	ret = vb2_queue_init(&queue->queue);
151 	if (ret)
152 		return ret;
153 
154 	spin_lock_init(&queue->irqlock);
155 	INIT_LIST_HEAD(&queue->irqqueue);
156 	queue->flags = 0;
157 
158 	return 0;
159 }
160 
161 /*
162  * Free the video buffers.
163  */
164 void uvcg_free_buffers(struct uvc_video_queue *queue)
165 {
166 	vb2_queue_release(&queue->queue);
167 }
168 
169 /*
170  * Allocate the video buffers.
171  */
172 int uvcg_alloc_buffers(struct uvc_video_queue *queue,
173 			      struct v4l2_requestbuffers *rb)
174 {
175 	int ret;
176 
177 	ret = vb2_reqbufs(&queue->queue, rb);
178 
179 	return ret ? ret : rb->count;
180 }
181 
182 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
183 {
184 	return vb2_querybuf(&queue->queue, buf);
185 }
186 
187 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
188 {
189 	return vb2_qbuf(&queue->queue, NULL, buf);
190 }
191 
192 /*
193  * Dequeue a video buffer. If nonblocking is false, block until a buffer is
194  * available.
195  */
196 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
197 			int nonblocking)
198 {
199 	return vb2_dqbuf(&queue->queue, buf, nonblocking);
200 }
201 
202 /*
203  * Poll the video queue.
204  *
205  * This function implements video queue polling and is intended to be used by
206  * the device poll handler.
207  */
208 __poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
209 			     poll_table *wait)
210 {
211 	return vb2_poll(&queue->queue, file, wait);
212 }
213 
214 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
215 {
216 	return vb2_mmap(&queue->queue, vma);
217 }
218 
219 #ifndef CONFIG_MMU
220 /*
221  * Get unmapped area.
222  *
223  * NO-MMU arch need this function to make mmap() work correctly.
224  */
225 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
226 					   unsigned long pgoff)
227 {
228 	return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
229 }
230 #endif
231 
232 /*
233  * Cancel the video buffers queue.
234  *
235  * Cancelling the queue marks all buffers on the irq queue as erroneous,
236  * wakes them up and removes them from the queue.
237  *
238  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
239  * fail with -ENODEV.
240  *
241  * This function acquires the irq spinlock and can be called from interrupt
242  * context.
243  */
244 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
245 {
246 	struct uvc_buffer *buf;
247 	unsigned long flags;
248 
249 	spin_lock_irqsave(&queue->irqlock, flags);
250 	while (!list_empty(&queue->irqqueue)) {
251 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
252 				       queue);
253 		list_del(&buf->queue);
254 		buf->state = UVC_BUF_STATE_ERROR;
255 		vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
256 	}
257 	queue->buf_used = 0;
258 
259 	/*
260 	 * This must be protected by the irqlock spinlock to avoid race
261 	 * conditions between uvc_queue_buffer and the disconnection event that
262 	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
263 	 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
264 	 * state outside the queue code.
265 	 */
266 	if (disconnect)
267 		queue->flags |= UVC_QUEUE_DISCONNECTED;
268 	spin_unlock_irqrestore(&queue->irqlock, flags);
269 }
270 
271 /*
272  * Enable or disable the video buffers queue.
273  *
274  * The queue must be enabled before starting video acquisition and must be
275  * disabled after stopping it. This ensures that the video buffers queue
276  * state can be properly initialized before buffers are accessed from the
277  * interrupt handler.
278  *
279  * Enabling the video queue initializes parameters (such as sequence number,
280  * sync pattern, ...). If the queue is already enabled, return -EBUSY.
281  *
282  * Disabling the video queue cancels the queue and removes all buffers from
283  * the main queue.
284  *
285  * This function can't be called from interrupt context. Use
286  * uvcg_queue_cancel() instead.
287  */
288 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
289 {
290 	unsigned long flags;
291 	int ret = 0;
292 
293 	if (enable) {
294 		ret = vb2_streamon(&queue->queue, queue->queue.type);
295 		if (ret < 0)
296 			return ret;
297 
298 		queue->sequence = 0;
299 		queue->buf_used = 0;
300 	} else {
301 		ret = vb2_streamoff(&queue->queue, queue->queue.type);
302 		if (ret < 0)
303 			return ret;
304 
305 		spin_lock_irqsave(&queue->irqlock, flags);
306 		INIT_LIST_HEAD(&queue->irqqueue);
307 
308 		/*
309 		 * FIXME: We need to clear the DISCONNECTED flag to ensure that
310 		 * applications will be able to queue buffers for the next
311 		 * streaming run. However, clearing it here doesn't guarantee
312 		 * that the device will be reconnected in the meantime.
313 		 */
314 		queue->flags &= ~UVC_QUEUE_DISCONNECTED;
315 		spin_unlock_irqrestore(&queue->irqlock, flags);
316 	}
317 
318 	return ret;
319 }
320 
321 /* called with &queue_irqlock held.. */
322 void uvcg_complete_buffer(struct uvc_video_queue *queue,
323 					  struct uvc_buffer *buf)
324 {
325 	if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
326 	     buf->length != buf->bytesused) {
327 		buf->state = UVC_BUF_STATE_QUEUED;
328 		vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
329 		return;
330 	}
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 
340 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
341 {
342 	struct uvc_buffer *buf = NULL;
343 
344 	if (!list_empty(&queue->irqqueue))
345 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
346 				       queue);
347 
348 	return buf;
349 }
350 
351