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