xref: /linux/drivers/media/usb/uvc/uvc_queue.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
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-v4l2.h>
24 #include <media/videobuf2-vmalloc.h>
25 
26 #include "uvcvideo.h"
27 
28 /* ------------------------------------------------------------------------
29  * Video buffers queue management.
30  *
31  * Video queues is initialized by uvc_queue_init(). The function performs
32  * basic initialization of the uvc_video_queue struct and never fails.
33  *
34  * Video buffers are managed by videobuf2. The driver uses a mutex to protect
35  * the videobuf2 queue operations by serializing calls to videobuf2 and a
36  * spinlock to protect the IRQ queue that holds the buffers to be processed by
37  * the driver.
38  */
39 
40 static inline struct uvc_streaming *
41 uvc_queue_to_stream(struct uvc_video_queue *queue)
42 {
43 	return container_of(queue, struct uvc_streaming, queue);
44 }
45 
46 static inline struct uvc_buffer *uvc_vbuf_to_buffer(struct vb2_v4l2_buffer *buf)
47 {
48 	return container_of(buf, struct uvc_buffer, buf);
49 }
50 
51 /*
52  * Return all queued buffers to videobuf2 in the requested state.
53  *
54  * This function must be called with the queue spinlock held.
55  */
56 static void uvc_queue_return_buffers(struct uvc_video_queue *queue,
57 			       enum uvc_buffer_state state)
58 {
59 	enum vb2_buffer_state vb2_state = state == UVC_BUF_STATE_ERROR
60 					? VB2_BUF_STATE_ERROR
61 					: VB2_BUF_STATE_QUEUED;
62 
63 	while (!list_empty(&queue->irqqueue)) {
64 		struct uvc_buffer *buf = list_first_entry(&queue->irqqueue,
65 							  struct uvc_buffer,
66 							  queue);
67 		list_del(&buf->queue);
68 		buf->state = state;
69 		vb2_buffer_done(&buf->buf.vb2_buf, vb2_state);
70 	}
71 }
72 
73 /* -----------------------------------------------------------------------------
74  * videobuf2 queue operations
75  */
76 
77 static int uvc_queue_setup(struct vb2_queue *vq,
78 			   unsigned int *nbuffers, unsigned int *nplanes,
79 			   unsigned int sizes[], struct device *alloc_devs[])
80 {
81 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
82 	struct uvc_streaming *stream = uvc_queue_to_stream(queue);
83 	unsigned size = stream->ctrl.dwMaxVideoFrameSize;
84 
85 	/* Make sure the image size is large enough. */
86 	if (*nplanes)
87 		return sizes[0] < size ? -EINVAL : 0;
88 	*nplanes = 1;
89 	sizes[0] = size;
90 	return 0;
91 }
92 
93 static int uvc_buffer_prepare(struct vb2_buffer *vb)
94 {
95 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
96 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
97 	struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
98 
99 	if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
100 	    vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
101 		uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
102 		return -EINVAL;
103 	}
104 
105 	if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
106 		return -ENODEV;
107 
108 	buf->state = UVC_BUF_STATE_QUEUED;
109 	buf->error = 0;
110 	buf->mem = vb2_plane_vaddr(vb, 0);
111 	buf->length = vb2_plane_size(vb, 0);
112 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
113 		buf->bytesused = 0;
114 	else
115 		buf->bytesused = vb2_get_plane_payload(vb, 0);
116 
117 	return 0;
118 }
119 
120 static void uvc_buffer_queue(struct vb2_buffer *vb)
121 {
122 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
123 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
124 	struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
125 	unsigned long flags;
126 
127 	spin_lock_irqsave(&queue->irqlock, flags);
128 	if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
129 		list_add_tail(&buf->queue, &queue->irqqueue);
130 	} else {
131 		/* If the device is disconnected return the buffer to userspace
132 		 * directly. The next QBUF call will fail with -ENODEV.
133 		 */
134 		buf->state = UVC_BUF_STATE_ERROR;
135 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
136 	}
137 
138 	spin_unlock_irqrestore(&queue->irqlock, flags);
139 }
140 
141 static void uvc_buffer_finish(struct vb2_buffer *vb)
142 {
143 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
144 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
145 	struct uvc_streaming *stream = uvc_queue_to_stream(queue);
146 	struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
147 
148 	if (vb->state == VB2_BUF_STATE_DONE)
149 		uvc_video_clock_update(stream, vbuf, buf);
150 }
151 
152 static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count)
153 {
154 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
155 	struct uvc_streaming *stream = uvc_queue_to_stream(queue);
156 	unsigned long flags;
157 	int ret;
158 
159 	queue->buf_used = 0;
160 
161 	ret = uvc_video_enable(stream, 1);
162 	if (ret == 0)
163 		return 0;
164 
165 	spin_lock_irqsave(&queue->irqlock, flags);
166 	uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED);
167 	spin_unlock_irqrestore(&queue->irqlock, flags);
168 
169 	return ret;
170 }
171 
172 static void uvc_stop_streaming(struct vb2_queue *vq)
173 {
174 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
175 	struct uvc_streaming *stream = uvc_queue_to_stream(queue);
176 	unsigned long flags;
177 
178 	uvc_video_enable(stream, 0);
179 
180 	spin_lock_irqsave(&queue->irqlock, flags);
181 	uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
182 	spin_unlock_irqrestore(&queue->irqlock, flags);
183 }
184 
185 static const struct vb2_ops uvc_queue_qops = {
186 	.queue_setup = uvc_queue_setup,
187 	.buf_prepare = uvc_buffer_prepare,
188 	.buf_queue = uvc_buffer_queue,
189 	.buf_finish = uvc_buffer_finish,
190 	.wait_prepare = vb2_ops_wait_prepare,
191 	.wait_finish = vb2_ops_wait_finish,
192 	.start_streaming = uvc_start_streaming,
193 	.stop_streaming = uvc_stop_streaming,
194 };
195 
196 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
197 		    int drop_corrupted)
198 {
199 	int ret;
200 
201 	queue->queue.type = type;
202 	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
203 	queue->queue.drv_priv = queue;
204 	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
205 	queue->queue.ops = &uvc_queue_qops;
206 	queue->queue.mem_ops = &vb2_vmalloc_memops;
207 	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
208 		| V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
209 	queue->queue.lock = &queue->mutex;
210 	ret = vb2_queue_init(&queue->queue);
211 	if (ret)
212 		return ret;
213 
214 	mutex_init(&queue->mutex);
215 	spin_lock_init(&queue->irqlock);
216 	INIT_LIST_HEAD(&queue->irqqueue);
217 	queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
218 
219 	return 0;
220 }
221 
222 void uvc_queue_release(struct uvc_video_queue *queue)
223 {
224 	mutex_lock(&queue->mutex);
225 	vb2_queue_release(&queue->queue);
226 	mutex_unlock(&queue->mutex);
227 }
228 
229 /* -----------------------------------------------------------------------------
230  * V4L2 queue operations
231  */
232 
233 int uvc_request_buffers(struct uvc_video_queue *queue,
234 			struct v4l2_requestbuffers *rb)
235 {
236 	int ret;
237 
238 	mutex_lock(&queue->mutex);
239 	ret = vb2_reqbufs(&queue->queue, rb);
240 	mutex_unlock(&queue->mutex);
241 
242 	return ret ? ret : rb->count;
243 }
244 
245 int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
246 {
247 	int ret;
248 
249 	mutex_lock(&queue->mutex);
250 	ret = vb2_querybuf(&queue->queue, buf);
251 	mutex_unlock(&queue->mutex);
252 
253 	return ret;
254 }
255 
256 int uvc_create_buffers(struct uvc_video_queue *queue,
257 		       struct v4l2_create_buffers *cb)
258 {
259 	int ret;
260 
261 	mutex_lock(&queue->mutex);
262 	ret = vb2_create_bufs(&queue->queue, cb);
263 	mutex_unlock(&queue->mutex);
264 
265 	return ret;
266 }
267 
268 int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
269 {
270 	int ret;
271 
272 	mutex_lock(&queue->mutex);
273 	ret = vb2_qbuf(&queue->queue, buf);
274 	mutex_unlock(&queue->mutex);
275 
276 	return ret;
277 }
278 
279 int uvc_export_buffer(struct uvc_video_queue *queue,
280 		      struct v4l2_exportbuffer *exp)
281 {
282 	int ret;
283 
284 	mutex_lock(&queue->mutex);
285 	ret = vb2_expbuf(&queue->queue, exp);
286 	mutex_unlock(&queue->mutex);
287 
288 	return ret;
289 }
290 
291 int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
292 		       int nonblocking)
293 {
294 	int ret;
295 
296 	mutex_lock(&queue->mutex);
297 	ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
298 	mutex_unlock(&queue->mutex);
299 
300 	return ret;
301 }
302 
303 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type)
304 {
305 	int ret;
306 
307 	mutex_lock(&queue->mutex);
308 	ret = vb2_streamon(&queue->queue, type);
309 	mutex_unlock(&queue->mutex);
310 
311 	return ret;
312 }
313 
314 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type)
315 {
316 	int ret;
317 
318 	mutex_lock(&queue->mutex);
319 	ret = vb2_streamoff(&queue->queue, type);
320 	mutex_unlock(&queue->mutex);
321 
322 	return ret;
323 }
324 
325 int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
326 {
327 	return vb2_mmap(&queue->queue, vma);
328 }
329 
330 #ifndef CONFIG_MMU
331 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
332 		unsigned long pgoff)
333 {
334 	return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
335 }
336 #endif
337 
338 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
339 			    poll_table *wait)
340 {
341 	unsigned int ret;
342 
343 	mutex_lock(&queue->mutex);
344 	ret = vb2_poll(&queue->queue, file, wait);
345 	mutex_unlock(&queue->mutex);
346 
347 	return ret;
348 }
349 
350 /* -----------------------------------------------------------------------------
351  *
352  */
353 
354 /*
355  * Check if buffers have been allocated.
356  */
357 int uvc_queue_allocated(struct uvc_video_queue *queue)
358 {
359 	int allocated;
360 
361 	mutex_lock(&queue->mutex);
362 	allocated = vb2_is_busy(&queue->queue);
363 	mutex_unlock(&queue->mutex);
364 
365 	return allocated;
366 }
367 
368 /*
369  * Cancel the video buffers queue.
370  *
371  * Cancelling the queue marks all buffers on the irq queue as erroneous,
372  * wakes them up and removes them from the queue.
373  *
374  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
375  * fail with -ENODEV.
376  *
377  * This function acquires the irq spinlock and can be called from interrupt
378  * context.
379  */
380 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
381 {
382 	unsigned long flags;
383 
384 	spin_lock_irqsave(&queue->irqlock, flags);
385 	uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
386 	/* This must be protected by the irqlock spinlock to avoid race
387 	 * conditions between uvc_buffer_queue and the disconnection event that
388 	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
389 	 * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
390 	 * state outside the queue code.
391 	 */
392 	if (disconnect)
393 		queue->flags |= UVC_QUEUE_DISCONNECTED;
394 	spin_unlock_irqrestore(&queue->irqlock, flags);
395 }
396 
397 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
398 		struct uvc_buffer *buf)
399 {
400 	struct uvc_buffer *nextbuf;
401 	unsigned long flags;
402 
403 	if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
404 		buf->error = 0;
405 		buf->state = UVC_BUF_STATE_QUEUED;
406 		buf->bytesused = 0;
407 		vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
408 		return buf;
409 	}
410 
411 	spin_lock_irqsave(&queue->irqlock, flags);
412 	list_del(&buf->queue);
413 	if (!list_empty(&queue->irqqueue))
414 		nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
415 					   queue);
416 	else
417 		nextbuf = NULL;
418 	spin_unlock_irqrestore(&queue->irqlock, flags);
419 
420 	buf->state = buf->error ? UVC_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
421 	vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
422 	vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
423 
424 	return nextbuf;
425 }
426