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 #include <linux/atomic.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/videodev2.h>
20 #include <linux/vmalloc.h>
21 #include <linux/wait.h>
22 
23 #include <media/v4l2-common.h>
24 #include <media/videobuf2-vmalloc.h>
25 
26 #include "uvc.h"
27 
28 /* ------------------------------------------------------------------------
29  * Video buffers queue management.
30  *
31  * Video queues is initialized by uvcg_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 /* -----------------------------------------------------------------------------
41  * videobuf2 queue operations
42  */
43 
44 static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
45 			   unsigned int *nbuffers, unsigned int *nplanes,
46 			   unsigned int sizes[], void *alloc_ctxs[])
47 {
48 	struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
49 	struct uvc_video *video = container_of(queue, struct uvc_video, queue);
50 
51 	if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
52 		*nbuffers = UVC_MAX_VIDEO_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_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->mem = vb2_plane_vaddr(vb, 0);
77 	buf->length = vb2_plane_size(vb, 0);
78 	if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
79 		buf->bytesused = 0;
80 	else
81 		buf->bytesused = vb2_get_plane_payload(vb, 0);
82 
83 	return 0;
84 }
85 
86 static void uvc_buffer_queue(struct vb2_buffer *vb)
87 {
88 	struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
89 	struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
90 	unsigned long flags;
91 
92 	spin_lock_irqsave(&queue->irqlock, flags);
93 
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 struct vb2_ops uvc_queue_qops = {
108 	.queue_setup = uvc_queue_setup,
109 	.buf_prepare = uvc_buffer_prepare,
110 	.buf_queue = uvc_buffer_queue,
111 	.wait_prepare = vb2_ops_wait_prepare,
112 	.wait_finish = vb2_ops_wait_finish,
113 };
114 
115 int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
116 		    struct mutex *lock)
117 {
118 	int ret;
119 
120 	queue->queue.type = type;
121 	queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
122 	queue->queue.drv_priv = queue;
123 	queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
124 	queue->queue.ops = &uvc_queue_qops;
125 	queue->queue.lock = lock;
126 	queue->queue.mem_ops = &vb2_vmalloc_memops;
127 	queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
128 				     | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
129 	ret = vb2_queue_init(&queue->queue);
130 	if (ret)
131 		return ret;
132 
133 	spin_lock_init(&queue->irqlock);
134 	INIT_LIST_HEAD(&queue->irqqueue);
135 	queue->flags = 0;
136 
137 	return 0;
138 }
139 
140 /*
141  * Free the video buffers.
142  */
143 void uvcg_free_buffers(struct uvc_video_queue *queue)
144 {
145 	vb2_queue_release(&queue->queue);
146 }
147 
148 /*
149  * Allocate the video buffers.
150  */
151 int uvcg_alloc_buffers(struct uvc_video_queue *queue,
152 			      struct v4l2_requestbuffers *rb)
153 {
154 	int ret;
155 
156 	ret = vb2_reqbufs(&queue->queue, rb);
157 
158 	return ret ? ret : rb->count;
159 }
160 
161 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
162 {
163 	return vb2_querybuf(&queue->queue, buf);
164 }
165 
166 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
167 {
168 	unsigned long flags;
169 	int ret;
170 
171 	ret = vb2_qbuf(&queue->queue, buf);
172 	if (ret < 0)
173 		return ret;
174 
175 	spin_lock_irqsave(&queue->irqlock, flags);
176 	ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
177 	queue->flags &= ~UVC_QUEUE_PAUSED;
178 	spin_unlock_irqrestore(&queue->irqlock, flags);
179 	return ret;
180 }
181 
182 /*
183  * Dequeue a video buffer. If nonblocking is false, block until a buffer is
184  * available.
185  */
186 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
187 			int nonblocking)
188 {
189 	return vb2_dqbuf(&queue->queue, buf, nonblocking);
190 }
191 
192 /*
193  * Poll the video queue.
194  *
195  * This function implements video queue polling and is intended to be used by
196  * the device poll handler.
197  */
198 unsigned int uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
199 			     poll_table *wait)
200 {
201 	return vb2_poll(&queue->queue, file, wait);
202 }
203 
204 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
205 {
206 	return vb2_mmap(&queue->queue, vma);
207 }
208 
209 #ifndef CONFIG_MMU
210 /*
211  * Get unmapped area.
212  *
213  * NO-MMU arch need this function to make mmap() work correctly.
214  */
215 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
216 					   unsigned long pgoff)
217 {
218 	return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
219 }
220 #endif
221 
222 /*
223  * Cancel the video buffers queue.
224  *
225  * Cancelling the queue marks all buffers on the irq queue as erroneous,
226  * wakes them up and removes them from the queue.
227  *
228  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
229  * fail with -ENODEV.
230  *
231  * This function acquires the irq spinlock and can be called from interrupt
232  * context.
233  */
234 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
235 {
236 	struct uvc_buffer *buf;
237 	unsigned long flags;
238 
239 	spin_lock_irqsave(&queue->irqlock, flags);
240 	while (!list_empty(&queue->irqqueue)) {
241 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
242 				       queue);
243 		list_del(&buf->queue);
244 		buf->state = UVC_BUF_STATE_ERROR;
245 		vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
246 	}
247 	/* This must be protected by the irqlock spinlock to avoid race
248 	 * conditions between uvc_queue_buffer and the disconnection event that
249 	 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
250 	 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
251 	 * state outside the queue code.
252 	 */
253 	if (disconnect)
254 		queue->flags |= UVC_QUEUE_DISCONNECTED;
255 	spin_unlock_irqrestore(&queue->irqlock, flags);
256 }
257 
258 /*
259  * Enable or disable the video buffers queue.
260  *
261  * The queue must be enabled before starting video acquisition and must be
262  * disabled after stopping it. This ensures that the video buffers queue
263  * state can be properly initialized before buffers are accessed from the
264  * interrupt handler.
265  *
266  * Enabling the video queue initializes parameters (such as sequence number,
267  * sync pattern, ...). If the queue is already enabled, return -EBUSY.
268  *
269  * Disabling the video queue cancels the queue and removes all buffers from
270  * the main queue.
271  *
272  * This function can't be called from interrupt context. Use
273  * uvcg_queue_cancel() instead.
274  */
275 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
276 {
277 	unsigned long flags;
278 	int ret = 0;
279 
280 	if (enable) {
281 		ret = vb2_streamon(&queue->queue, queue->queue.type);
282 		if (ret < 0)
283 			return ret;
284 
285 		queue->sequence = 0;
286 		queue->buf_used = 0;
287 	} else {
288 		ret = vb2_streamoff(&queue->queue, queue->queue.type);
289 		if (ret < 0)
290 			return ret;
291 
292 		spin_lock_irqsave(&queue->irqlock, flags);
293 		INIT_LIST_HEAD(&queue->irqqueue);
294 
295 		/*
296 		 * FIXME: We need to clear the DISCONNECTED flag to ensure that
297 		 * applications will be able to queue buffers for the next
298 		 * streaming run. However, clearing it here doesn't guarantee
299 		 * that the device will be reconnected in the meantime.
300 		 */
301 		queue->flags &= ~UVC_QUEUE_DISCONNECTED;
302 		spin_unlock_irqrestore(&queue->irqlock, flags);
303 	}
304 
305 	return ret;
306 }
307 
308 /* called with &queue_irqlock held.. */
309 struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue,
310 					  struct uvc_buffer *buf)
311 {
312 	struct uvc_buffer *nextbuf;
313 
314 	if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
315 	     buf->length != buf->bytesused) {
316 		buf->state = UVC_BUF_STATE_QUEUED;
317 		vb2_set_plane_payload(&buf->buf, 0, 0);
318 		return buf;
319 	}
320 
321 	list_del(&buf->queue);
322 	if (!list_empty(&queue->irqqueue))
323 		nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
324 					   queue);
325 	else
326 		nextbuf = NULL;
327 
328 	buf->buf.v4l2_buf.field = V4L2_FIELD_NONE;
329 	buf->buf.v4l2_buf.sequence = queue->sequence++;
330 	v4l2_get_timestamp(&buf->buf.v4l2_buf.timestamp);
331 
332 	vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
333 	vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
334 
335 	return nextbuf;
336 }
337 
338 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
339 {
340 	struct uvc_buffer *buf = NULL;
341 
342 	if (!list_empty(&queue->irqqueue))
343 		buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
344 				       queue);
345 	else
346 		queue->flags |= UVC_QUEUE_PAUSED;
347 
348 	return buf;
349 }
350 
351