xref: /linux/drivers/media/usb/uvc/uvc_queue.c (revision d83ff0bb828854d9e7172ac5d8d007a7466934c9)
1 /*
2  *      uvc_queue.c  --  USB Video Class driver - Buffers management
3  *
4  *      Copyright (C) 2005-2010
5  *          Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6  *
7  *      This program is free software; you can redistribute it and/or modify
8  *      it under the terms of the GNU General Public License as published by
9  *      the Free Software Foundation; either version 2 of the License, or
10  *      (at your option) any later version.
11  *
12  */
13 
14 #include <linux/atomic.h>
15 #include <linux/kernel.h>
16 #include <linux/mm.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/videodev2.h>
21 #include <linux/vmalloc.h>
22 #include <linux/wait.h>
23 #include <media/videobuf2-vmalloc.h>
24 
25 #include "uvcvideo.h"
26 
27 /* ------------------------------------------------------------------------
28  * Video buffers queue management.
29  *
30  * Video queues is initialized by uvc_queue_init(). The function performs
31  * basic initialization of the uvc_video_queue struct and never fails.
32  *
33  * Video buffers are managed by videobuf2. The driver uses a mutex to protect
34  * the videobuf2 queue operations by serializing calls to videobuf2 and a
35  * spinlock to protect the IRQ queue that holds the buffers to be processed by
36  * the driver.
37  */
38 
39 /* -----------------------------------------------------------------------------
40  * videobuf2 queue operations
41  */
42 
43 static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
44 			   unsigned int *nbuffers, unsigned int *nplanes,
45 			   unsigned int sizes[], void *alloc_ctxs[])
46 {
47 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
48 	struct uvc_streaming *stream =
49 			container_of(queue, struct uvc_streaming, queue);
50 
51 	if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
52 		*nbuffers = UVC_MAX_VIDEO_BUFFERS;
53 
54 	*nplanes = 1;
55 
56 	sizes[0] = stream->ctrl.dwMaxVideoFrameSize;
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_buffer *buf = container_of(vb, struct uvc_buffer, buf);
65 
66 	if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
67 	    vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
68 		uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
69 		return -EINVAL;
70 	}
71 
72 	if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
73 		return -ENODEV;
74 
75 	buf->state = UVC_BUF_STATE_QUEUED;
76 	buf->error = 0;
77 	buf->mem = vb2_plane_vaddr(vb, 0);
78 	buf->length = vb2_plane_size(vb, 0);
79 	if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
80 		buf->bytesused = 0;
81 	else
82 		buf->bytesused = vb2_get_plane_payload(vb, 0);
83 
84 	return 0;
85 }
86 
87 static void uvc_buffer_queue(struct vb2_buffer *vb)
88 {
89 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
90 	struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
91 	unsigned long flags;
92 
93 	spin_lock_irqsave(&queue->irqlock, flags);
94 	if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
95 		list_add_tail(&buf->queue, &queue->irqqueue);
96 	} else {
97 		/* If the device is disconnected return the buffer to userspace
98 		 * directly. The next QBUF call will fail with -ENODEV.
99 		 */
100 		buf->state = UVC_BUF_STATE_ERROR;
101 		vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
102 	}
103 
104 	spin_unlock_irqrestore(&queue->irqlock, flags);
105 }
106 
107 static int uvc_buffer_finish(struct vb2_buffer *vb)
108 {
109 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
110 	struct uvc_streaming *stream =
111 			container_of(queue, struct uvc_streaming, queue);
112 	struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
113 
114 	uvc_video_clock_update(stream, &vb->v4l2_buf, buf);
115 	return 0;
116 }
117 
118 static void uvc_wait_prepare(struct vb2_queue *vq)
119 {
120 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
121 
122 	mutex_unlock(&queue->mutex);
123 }
124 
125 static void uvc_wait_finish(struct vb2_queue *vq)
126 {
127 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
128 
129 	mutex_lock(&queue->mutex);
130 }
131 
132 static struct vb2_ops uvc_queue_qops = {
133 	.queue_setup = uvc_queue_setup,
134 	.buf_prepare = uvc_buffer_prepare,
135 	.buf_queue = uvc_buffer_queue,
136 	.buf_finish = uvc_buffer_finish,
137 	.wait_prepare = uvc_wait_prepare,
138 	.wait_finish = uvc_wait_finish,
139 };
140 
141 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
142 		    int drop_corrupted)
143 {
144 	int ret;
145 
146 	queue->queue.type = type;
147 	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
148 	queue->queue.drv_priv = queue;
149 	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
150 	queue->queue.ops = &uvc_queue_qops;
151 	queue->queue.mem_ops = &vb2_vmalloc_memops;
152 	ret = vb2_queue_init(&queue->queue);
153 	if (ret)
154 		return ret;
155 
156 	mutex_init(&queue->mutex);
157 	spin_lock_init(&queue->irqlock);
158 	INIT_LIST_HEAD(&queue->irqqueue);
159 	queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
160 
161 	return 0;
162 }
163 
164 /* -----------------------------------------------------------------------------
165  * V4L2 queue operations
166  */
167 
168 int uvc_alloc_buffers(struct uvc_video_queue *queue,
169 		      struct v4l2_requestbuffers *rb)
170 {
171 	int ret;
172 
173 	mutex_lock(&queue->mutex);
174 	ret = vb2_reqbufs(&queue->queue, rb);
175 	mutex_unlock(&queue->mutex);
176 
177 	return ret ? ret : rb->count;
178 }
179 
180 void uvc_free_buffers(struct uvc_video_queue *queue)
181 {
182 	mutex_lock(&queue->mutex);
183 	vb2_queue_release(&queue->queue);
184 	mutex_unlock(&queue->mutex);
185 }
186 
187 int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
188 {
189 	int ret;
190 
191 	mutex_lock(&queue->mutex);
192 	ret = vb2_querybuf(&queue->queue, buf);
193 	mutex_unlock(&queue->mutex);
194 
195 	return ret;
196 }
197 
198 int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
199 {
200 	int ret;
201 
202 	mutex_lock(&queue->mutex);
203 	ret = vb2_qbuf(&queue->queue, buf);
204 	mutex_unlock(&queue->mutex);
205 
206 	return ret;
207 }
208 
209 int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
210 		       int nonblocking)
211 {
212 	int ret;
213 
214 	mutex_lock(&queue->mutex);
215 	ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
216 	mutex_unlock(&queue->mutex);
217 
218 	return ret;
219 }
220 
221 int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
222 {
223 	int ret;
224 
225 	mutex_lock(&queue->mutex);
226 	ret = vb2_mmap(&queue->queue, vma);
227 	mutex_unlock(&queue->mutex);
228 
229 	return ret;
230 }
231 
232 #ifndef CONFIG_MMU
233 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
234 		unsigned long pgoff)
235 {
236 	unsigned long ret;
237 
238 	mutex_lock(&queue->mutex);
239 	ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
240 	mutex_unlock(&queue->mutex);
241 	return ret;
242 }
243 #endif
244 
245 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
246 			    poll_table *wait)
247 {
248 	unsigned int ret;
249 
250 	mutex_lock(&queue->mutex);
251 	ret = vb2_poll(&queue->queue, file, wait);
252 	mutex_unlock(&queue->mutex);
253 
254 	return ret;
255 }
256 
257 /* -----------------------------------------------------------------------------
258  *
259  */
260 
261 /*
262  * Check if buffers have been allocated.
263  */
264 int uvc_queue_allocated(struct uvc_video_queue *queue)
265 {
266 	int allocated;
267 
268 	mutex_lock(&queue->mutex);
269 	allocated = vb2_is_busy(&queue->queue);
270 	mutex_unlock(&queue->mutex);
271 
272 	return allocated;
273 }
274 
275 /*
276  * Enable or disable the video buffers queue.
277  *
278  * The queue must be enabled before starting video acquisition and must be
279  * disabled after stopping it. This ensures that the video buffers queue
280  * state can be properly initialized before buffers are accessed from the
281  * interrupt handler.
282  *
283  * Enabling the video queue returns -EBUSY if the queue is already enabled.
284  *
285  * Disabling the video queue cancels the queue and removes all buffers from
286  * the main queue.
287  *
288  * This function can't be called from interrupt context. Use
289  * uvc_queue_cancel() instead.
290  */
291 int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
292 {
293 	unsigned long flags;
294 	int ret;
295 
296 	mutex_lock(&queue->mutex);
297 	if (enable) {
298 		ret = vb2_streamon(&queue->queue, queue->queue.type);
299 		if (ret < 0)
300 			goto done;
301 
302 		queue->buf_used = 0;
303 	} else {
304 		ret = vb2_streamoff(&queue->queue, queue->queue.type);
305 		if (ret < 0)
306 			goto done;
307 
308 		spin_lock_irqsave(&queue->irqlock, flags);
309 		INIT_LIST_HEAD(&queue->irqqueue);
310 		spin_unlock_irqrestore(&queue->irqlock, flags);
311 	}
312 
313 done:
314 	mutex_unlock(&queue->mutex);
315 	return ret;
316 }
317 
318 /*
319  * Cancel the video buffers queue.
320  *
321  * Cancelling the queue marks all buffers on the irq queue as erroneous,
322  * wakes them up and removes them from the queue.
323  *
324  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
325  * fail with -ENODEV.
326  *
327  * This function acquires the irq spinlock and can be called from interrupt
328  * context.
329  */
330 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
331 {
332 	struct uvc_buffer *buf;
333 	unsigned long flags;
334 
335 	spin_lock_irqsave(&queue->irqlock, flags);
336 	while (!list_empty(&queue->irqqueue)) {
337 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
338 				       queue);
339 		list_del(&buf->queue);
340 		buf->state = UVC_BUF_STATE_ERROR;
341 		vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
342 	}
343 	/* This must be protected by the irqlock spinlock to avoid race
344 	 * conditions between uvc_buffer_queue and the disconnection event that
345 	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
346 	 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
347 	 * state outside the queue code.
348 	 */
349 	if (disconnect)
350 		queue->flags |= UVC_QUEUE_DISCONNECTED;
351 	spin_unlock_irqrestore(&queue->irqlock, flags);
352 }
353 
354 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
355 		struct uvc_buffer *buf)
356 {
357 	struct uvc_buffer *nextbuf;
358 	unsigned long flags;
359 
360 	if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
361 		buf->error = 0;
362 		buf->state = UVC_BUF_STATE_QUEUED;
363 		buf->bytesused = 0;
364 		vb2_set_plane_payload(&buf->buf, 0, 0);
365 		return buf;
366 	}
367 
368 	spin_lock_irqsave(&queue->irqlock, flags);
369 	list_del(&buf->queue);
370 	if (!list_empty(&queue->irqqueue))
371 		nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
372 					   queue);
373 	else
374 		nextbuf = NULL;
375 	spin_unlock_irqrestore(&queue->irqlock, flags);
376 
377 	buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
378 	vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
379 	vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
380 
381 	return nextbuf;
382 }
383