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 
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 	return 0;
56 }
57 
58 static int uvc_buffer_prepare(struct vb2_buffer *vb)
59 {
60 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
61 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
62 	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
63 
64 	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
65 	    vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
66 		uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
67 		return -EINVAL;
68 	}
69 
70 	if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
71 		return -ENODEV;
72 
73 	buf->state = UVC_BUF_STATE_QUEUED;
74 	if (queue->use_sg) {
75 		buf->sgt = vb2_dma_sg_plane_desc(vb, 0);
76 		buf->sg = buf->sgt->sgl;
77 	} else {
78 		buf->mem = vb2_plane_vaddr(vb, 0);
79 	}
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 		/*
102 		 * If the device is disconnected return the buffer to userspace
103 		 * directly. The next QBUF call will fail with -ENODEV.
104 		 */
105 		buf->state = UVC_BUF_STATE_ERROR;
106 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
107 	}
108 
109 	spin_unlock_irqrestore(&queue->irqlock, flags);
110 }
111 
112 static const struct vb2_ops uvc_queue_qops = {
113 	.queue_setup = uvc_queue_setup,
114 	.buf_prepare = uvc_buffer_prepare,
115 	.buf_queue = uvc_buffer_queue,
116 	.wait_prepare = vb2_ops_wait_prepare,
117 	.wait_finish = vb2_ops_wait_finish,
118 };
119 
120 int uvcg_queue_init(struct uvc_video_queue *queue, struct device *dev, enum v4l2_buf_type type,
121 		    struct mutex *lock)
122 {
123 	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
124 	struct usb_composite_dev *cdev = video->uvc->func.config->cdev;
125 	int ret;
126 
127 	queue->queue.type = type;
128 	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
129 	queue->queue.drv_priv = queue;
130 	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
131 	queue->queue.ops = &uvc_queue_qops;
132 	queue->queue.lock = lock;
133 	if (cdev->gadget->sg_supported) {
134 		queue->queue.mem_ops = &vb2_dma_sg_memops;
135 		queue->use_sg = 1;
136 	} else {
137 		queue->queue.mem_ops = &vb2_vmalloc_memops;
138 	}
139 
140 	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY
141 				     | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
142 	queue->queue.dev = dev;
143 
144 	ret = vb2_queue_init(&queue->queue);
145 	if (ret)
146 		return ret;
147 
148 	spin_lock_init(&queue->irqlock);
149 	INIT_LIST_HEAD(&queue->irqqueue);
150 	queue->flags = 0;
151 
152 	return 0;
153 }
154 
155 /*
156  * Free the video buffers.
157  */
158 void uvcg_free_buffers(struct uvc_video_queue *queue)
159 {
160 	vb2_queue_release(&queue->queue);
161 }
162 
163 /*
164  * Allocate the video buffers.
165  */
166 int uvcg_alloc_buffers(struct uvc_video_queue *queue,
167 			      struct v4l2_requestbuffers *rb)
168 {
169 	int ret;
170 
171 	ret = vb2_reqbufs(&queue->queue, rb);
172 
173 	return ret ? ret : rb->count;
174 }
175 
176 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
177 {
178 	return vb2_querybuf(&queue->queue, buf);
179 }
180 
181 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
182 {
183 	return vb2_qbuf(&queue->queue, NULL, buf);
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 	queue->buf_used = 0;
252 
253 	/*
254 	 * This must be protected by the irqlock spinlock to avoid race
255 	 * conditions between uvc_queue_buffer and the disconnection event that
256 	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
257 	 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
258 	 * state outside the queue code.
259 	 */
260 	if (disconnect)
261 		queue->flags |= UVC_QUEUE_DISCONNECTED;
262 	spin_unlock_irqrestore(&queue->irqlock, flags);
263 }
264 
265 /*
266  * Enable or disable the video buffers queue.
267  *
268  * The queue must be enabled before starting video acquisition and must be
269  * disabled after stopping it. This ensures that the video buffers queue
270  * state can be properly initialized before buffers are accessed from the
271  * interrupt handler.
272  *
273  * Enabling the video queue initializes parameters (such as sequence number,
274  * sync pattern, ...). If the queue is already enabled, return -EBUSY.
275  *
276  * Disabling the video queue cancels the queue and removes all buffers from
277  * the main queue.
278  *
279  * This function can't be called from interrupt context. Use
280  * uvcg_queue_cancel() instead.
281  */
282 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
283 {
284 	unsigned long flags;
285 	int ret = 0;
286 
287 	if (enable) {
288 		ret = vb2_streamon(&queue->queue, queue->queue.type);
289 		if (ret < 0)
290 			return ret;
291 
292 		queue->sequence = 0;
293 		queue->buf_used = 0;
294 		queue->flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
295 	} else {
296 		ret = vb2_streamoff(&queue->queue, queue->queue.type);
297 		if (ret < 0)
298 			return ret;
299 
300 		spin_lock_irqsave(&queue->irqlock, flags);
301 		INIT_LIST_HEAD(&queue->irqqueue);
302 
303 		/*
304 		 * FIXME: We need to clear the DISCONNECTED flag to ensure that
305 		 * applications will be able to queue buffers for the next
306 		 * streaming run. However, clearing it here doesn't guarantee
307 		 * that the device will be reconnected in the meantime.
308 		 */
309 		queue->flags &= ~UVC_QUEUE_DISCONNECTED;
310 		spin_unlock_irqrestore(&queue->irqlock, flags);
311 	}
312 
313 	return ret;
314 }
315 
316 /* called with &queue_irqlock held.. */
317 void uvcg_complete_buffer(struct uvc_video_queue *queue,
318 					  struct uvc_buffer *buf)
319 {
320 	if (queue->flags & UVC_QUEUE_DROP_INCOMPLETE) {
321 		queue->flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
322 		buf->state = UVC_BUF_STATE_ERROR;
323 		vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
324 		vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
325 		return;
326 	}
327 
328 	buf->buf.field = V4L2_FIELD_NONE;
329 	buf->buf.sequence = queue->sequence++;
330 	buf->buf.vb2_buf.timestamp = ktime_get_ns();
331 
332 	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
333 	vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
334 }
335 
336 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
337 {
338 	struct uvc_buffer *buf = NULL;
339 
340 	if (!list_empty(&queue->irqqueue))
341 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
342 				       queue);
343 
344 	return buf;
345 }
346 
347