xref: /OK3568_Linux_fs/kernel/drivers/usb/gadget/function/uvc_queue.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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-vmalloc.h>
21 
22 #include "uvc.h"
23 #include "u_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 
uvc_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])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 #if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI)
48 	struct uvc_device *uvc = container_of(video, struct uvc_device, video);
49 	struct f_uvc_opts *opts = fi_to_f_uvc_opts(uvc->func.fi);
50 #endif
51 	unsigned int req_size;
52 	unsigned int nreq;
53 
54 	if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
55 		*nbuffers = UVC_MAX_VIDEO_BUFFERS;
56 
57 	*nplanes = 1;
58 
59 	sizes[0] = video->imagesize;
60 
61 #if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI)
62 	if (opts && opts->uvc_num_request > 0) {
63 		video->uvc_num_requests = opts->uvc_num_request;
64 		return 0;
65 	}
66 #endif
67 
68 	req_size = video->ep->maxpacket
69 		 * max_t(unsigned int, video->ep->maxburst, 1)
70 		 * (video->ep->mult);
71 
72 	/* We divide by two, to increase the chance to run
73 	 * into fewer requests for smaller framesizes.
74 	 */
75 	nreq = DIV_ROUND_UP(DIV_ROUND_UP(sizes[0], 2), req_size);
76 	nreq = clamp(nreq, 4U, 64U);
77 	video->uvc_num_requests = nreq;
78 
79 	return 0;
80 }
81 
82 #if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI)
83 /*
84  * uvc_dma_buf_phys_to_virt - Get the physical address of the dma_buf and
85  * translate it to virtual address.
86  *
87  * @dbuf: the dma_buf of vb2_plane
88  * @dev: the device to the actual usb controller
89  *
90  * This function is used for dma buf allocated by Contiguous Memory Allocator.
91  *
92  * Returns:
93  * The virtual addresses of the dma_buf.
94  */
uvc_dma_buf_phys_to_virt(struct uvc_device * uvc,struct dma_buf * dbuf)95 static void *uvc_dma_buf_phys_to_virt(struct uvc_device *uvc,
96 				      struct dma_buf *dbuf)
97 {
98 	struct usb_gadget *gadget = uvc->func.config->cdev->gadget;
99 	struct dma_buf_attachment *attachment;
100 	struct sg_table *table;
101 	struct scatterlist *sgl;
102 	dma_addr_t phys = 0;
103 	int i;
104 
105 	attachment = dma_buf_attach(dbuf, gadget->dev.parent);
106 	if (IS_ERR(attachment))
107 		return ERR_PTR(-ENOMEM);
108 
109 	table = dma_buf_map_attachment(attachment, DMA_BIDIRECTIONAL);
110 	if (IS_ERR(table)) {
111 		dma_buf_detach(dbuf, attachment);
112 		return ERR_PTR(-ENOMEM);
113 	}
114 
115 	for_each_sgtable_sg(table, sgl, i)
116 		phys = sg_phys(sgl);
117 
118 	dma_buf_unmap_attachment(attachment, table, DMA_BIDIRECTIONAL);
119 	dma_buf_detach(dbuf, attachment);
120 
121 	if (i > 1) {
122 		uvcg_err(&uvc->func, "Not support mult sgl for uvc zero copy\n");
123 		return ERR_PTR(-ENOMEM);
124 	}
125 
126 	return phys_to_virt(phys);
127 }
128 
uvc_buffer_mem_prepare(struct vb2_buffer * vb,struct uvc_video_queue * queue)129 static void *uvc_buffer_mem_prepare(struct vb2_buffer *vb,
130 				    struct uvc_video_queue *queue)
131 {
132 	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
133 	struct uvc_device *uvc = container_of(video, struct uvc_device, video);
134 	struct f_uvc_opts *opts = fi_to_f_uvc_opts(uvc->func.fi);
135 	void *mem;
136 
137 	if (!opts->uvc_zero_copy || video->fcc == V4L2_PIX_FMT_YUYV)
138 		return (vb2_plane_vaddr(vb, 0) + vb2_plane_data_offset(vb, 0));
139 
140 	mem = uvc_dma_buf_phys_to_virt(uvc, vb->planes[0].dbuf);
141 	if (IS_ERR(mem))
142 		return ERR_PTR(-ENOMEM);
143 
144 	return (mem + vb2_plane_data_offset(vb, 0));
145 }
146 #endif
147 
uvc_buffer_prepare(struct vb2_buffer * vb)148 static int uvc_buffer_prepare(struct vb2_buffer *vb)
149 {
150 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
151 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
152 	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
153 
154 	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
155 	    vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
156 		uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
157 		return -EINVAL;
158 	}
159 
160 	if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
161 		return -ENODEV;
162 
163 	buf->state = UVC_BUF_STATE_QUEUED;
164 #if defined(CONFIG_ARCH_ROCKCHIP) && defined(CONFIG_NO_GKI)
165 	buf->mem = uvc_buffer_mem_prepare(vb, queue);
166 	if (IS_ERR(buf->mem))
167 		return -ENOMEM;
168 #else
169 	buf->mem = vb2_plane_vaddr(vb, 0);
170 #endif
171 	buf->length = vb2_plane_size(vb, 0);
172 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
173 		buf->bytesused = 0;
174 	else
175 		buf->bytesused = vb2_get_plane_payload(vb, 0);
176 
177 	return 0;
178 }
179 
uvc_buffer_queue(struct vb2_buffer * vb)180 static void uvc_buffer_queue(struct vb2_buffer *vb)
181 {
182 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
183 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
184 	struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
185 	unsigned long flags;
186 
187 	spin_lock_irqsave(&queue->irqlock, flags);
188 
189 	if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
190 		list_add_tail(&buf->queue, &queue->irqqueue);
191 	} else {
192 		/* If the device is disconnected return the buffer to userspace
193 		 * directly. The next QBUF call will fail with -ENODEV.
194 		 */
195 		buf->state = UVC_BUF_STATE_ERROR;
196 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
197 	}
198 
199 	spin_unlock_irqrestore(&queue->irqlock, flags);
200 }
201 
202 static const struct vb2_ops uvc_queue_qops = {
203 	.queue_setup = uvc_queue_setup,
204 	.buf_prepare = uvc_buffer_prepare,
205 	.buf_queue = uvc_buffer_queue,
206 	.wait_prepare = vb2_ops_wait_prepare,
207 	.wait_finish = vb2_ops_wait_finish,
208 };
209 
uvcg_queue_init(struct uvc_video_queue * queue,enum v4l2_buf_type type,struct mutex * lock)210 int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
211 		    struct mutex *lock)
212 {
213 	int ret;
214 
215 	queue->queue.type = type;
216 	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
217 	queue->queue.drv_priv = queue;
218 	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
219 	queue->queue.ops = &uvc_queue_qops;
220 	queue->queue.lock = lock;
221 	queue->queue.mem_ops = &vb2_vmalloc_memops;
222 	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
223 				     | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
224 	/*
225 	 * For rockchip platform, the userspace uvc application
226 	 * use bytesused == 0 as a way to indicate that the data
227 	 * is all zero and unused.
228 	 */
229 #ifdef CONFIG_ARCH_ROCKCHIP
230 	queue->queue.allow_zero_bytesused = 1;
231 #endif
232 	ret = vb2_queue_init(&queue->queue);
233 	if (ret)
234 		return ret;
235 
236 	spin_lock_init(&queue->irqlock);
237 	INIT_LIST_HEAD(&queue->irqqueue);
238 	queue->flags = 0;
239 
240 	return 0;
241 }
242 
243 /*
244  * Free the video buffers.
245  */
uvcg_free_buffers(struct uvc_video_queue * queue)246 void uvcg_free_buffers(struct uvc_video_queue *queue)
247 {
248 	vb2_queue_release(&queue->queue);
249 }
250 
251 /*
252  * Allocate the video buffers.
253  */
uvcg_alloc_buffers(struct uvc_video_queue * queue,struct v4l2_requestbuffers * rb)254 int uvcg_alloc_buffers(struct uvc_video_queue *queue,
255 			      struct v4l2_requestbuffers *rb)
256 {
257 	int ret;
258 
259 	ret = vb2_reqbufs(&queue->queue, rb);
260 
261 	return ret ? ret : rb->count;
262 }
263 
uvcg_query_buffer(struct uvc_video_queue * queue,struct v4l2_buffer * buf)264 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
265 {
266 	return vb2_querybuf(&queue->queue, buf);
267 }
268 
uvcg_queue_buffer(struct uvc_video_queue * queue,struct v4l2_buffer * buf)269 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
270 {
271 	return vb2_qbuf(&queue->queue, NULL, buf);
272 }
273 
274 /*
275  * Dequeue a video buffer. If nonblocking is false, block until a buffer is
276  * available.
277  */
uvcg_dequeue_buffer(struct uvc_video_queue * queue,struct v4l2_buffer * buf,int nonblocking)278 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
279 			int nonblocking)
280 {
281 	return vb2_dqbuf(&queue->queue, buf, nonblocking);
282 }
283 
284 /*
285  * Poll the video queue.
286  *
287  * This function implements video queue polling and is intended to be used by
288  * the device poll handler.
289  */
uvcg_queue_poll(struct uvc_video_queue * queue,struct file * file,poll_table * wait)290 __poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
291 			     poll_table *wait)
292 {
293 	return vb2_poll(&queue->queue, file, wait);
294 }
295 
uvcg_queue_mmap(struct uvc_video_queue * queue,struct vm_area_struct * vma)296 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
297 {
298 	return vb2_mmap(&queue->queue, vma);
299 }
300 
301 #ifndef CONFIG_MMU
302 /*
303  * Get unmapped area.
304  *
305  * NO-MMU arch need this function to make mmap() work correctly.
306  */
uvcg_queue_get_unmapped_area(struct uvc_video_queue * queue,unsigned long pgoff)307 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
308 					   unsigned long pgoff)
309 {
310 	return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
311 }
312 #endif
313 
314 /*
315  * Cancel the video buffers queue.
316  *
317  * Cancelling the queue marks all buffers on the irq queue as erroneous,
318  * wakes them up and removes them from the queue.
319  *
320  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
321  * fail with -ENODEV.
322  *
323  * This function acquires the irq spinlock and can be called from interrupt
324  * context.
325  */
uvcg_queue_cancel(struct uvc_video_queue * queue,int disconnect)326 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
327 {
328 	struct uvc_buffer *buf;
329 	unsigned long flags;
330 
331 	spin_lock_irqsave(&queue->irqlock, flags);
332 	while (!list_empty(&queue->irqqueue)) {
333 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
334 				       queue);
335 		list_del(&buf->queue);
336 		buf->state = UVC_BUF_STATE_ERROR;
337 		vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
338 	}
339 	queue->buf_used = 0;
340 
341 	/* This must be protected by the irqlock spinlock to avoid race
342 	 * conditions between uvc_queue_buffer and the disconnection event that
343 	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
344 	 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
345 	 * state outside the queue code.
346 	 */
347 	if (disconnect)
348 		queue->flags |= UVC_QUEUE_DISCONNECTED;
349 	spin_unlock_irqrestore(&queue->irqlock, flags);
350 }
351 
352 /*
353  * Enable or disable the video buffers queue.
354  *
355  * The queue must be enabled before starting video acquisition and must be
356  * disabled after stopping it. This ensures that the video buffers queue
357  * state can be properly initialized before buffers are accessed from the
358  * interrupt handler.
359  *
360  * Enabling the video queue initializes parameters (such as sequence number,
361  * sync pattern, ...). If the queue is already enabled, return -EBUSY.
362  *
363  * Disabling the video queue cancels the queue and removes all buffers from
364  * the main queue.
365  *
366  * This function can't be called from interrupt context. Use
367  * uvcg_queue_cancel() instead.
368  */
uvcg_queue_enable(struct uvc_video_queue * queue,int enable)369 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
370 {
371 	unsigned long flags;
372 	int ret = 0;
373 
374 	if (enable) {
375 		ret = vb2_streamon(&queue->queue, queue->queue.type);
376 		if (ret < 0)
377 			return ret;
378 
379 		queue->sequence = 0;
380 		queue->buf_used = 0;
381 	} else {
382 		ret = vb2_streamoff(&queue->queue, queue->queue.type);
383 		if (ret < 0)
384 			return ret;
385 
386 		spin_lock_irqsave(&queue->irqlock, flags);
387 		INIT_LIST_HEAD(&queue->irqqueue);
388 
389 		/*
390 		 * FIXME: We need to clear the DISCONNECTED flag to ensure that
391 		 * applications will be able to queue buffers for the next
392 		 * streaming run. However, clearing it here doesn't guarantee
393 		 * that the device will be reconnected in the meantime.
394 		 */
395 		queue->flags &= ~UVC_QUEUE_DISCONNECTED;
396 		spin_unlock_irqrestore(&queue->irqlock, flags);
397 	}
398 
399 	return ret;
400 }
401 
402 /* called with &queue_irqlock held.. */
uvcg_queue_next_buffer(struct uvc_video_queue * queue,struct uvc_buffer * buf)403 struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue,
404 					  struct uvc_buffer *buf)
405 {
406 	struct uvc_buffer *nextbuf;
407 
408 	if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
409 	     buf->length != buf->bytesused) {
410 		buf->state = UVC_BUF_STATE_QUEUED;
411 		vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
412 		return buf;
413 	}
414 
415 	list_del(&buf->queue);
416 	if (!list_empty(&queue->irqqueue))
417 		nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
418 					   queue);
419 	else
420 		nextbuf = NULL;
421 
422 	buf->buf.field = V4L2_FIELD_NONE;
423 	buf->buf.sequence = queue->sequence++;
424 	buf->buf.vb2_buf.timestamp = ktime_get_ns();
425 
426 	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
427 	vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
428 
429 	return nextbuf;
430 }
431 
uvcg_queue_head(struct uvc_video_queue * queue)432 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
433 {
434 	struct uvc_buffer *buf = NULL;
435 
436 	if (!list_empty(&queue->irqqueue))
437 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
438 				       queue);
439 
440 	return buf;
441 }
442 
443