Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux...
[firefly-linux-kernel-4.4.55.git] / drivers / usb / gadget / function / uvc_queue.c
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 void uvc_wait_prepare(struct vb2_queue *vq)
108 {
109         struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
110
111         mutex_unlock(&queue->mutex);
112 }
113
114 static void uvc_wait_finish(struct vb2_queue *vq)
115 {
116         struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
117
118         mutex_lock(&queue->mutex);
119 }
120
121 static struct vb2_ops uvc_queue_qops = {
122         .queue_setup = uvc_queue_setup,
123         .buf_prepare = uvc_buffer_prepare,
124         .buf_queue = uvc_buffer_queue,
125         .wait_prepare = uvc_wait_prepare,
126         .wait_finish = uvc_wait_finish,
127 };
128
129 int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type)
130 {
131         int ret;
132
133         queue->queue.type = type;
134         queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
135         queue->queue.drv_priv = queue;
136         queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
137         queue->queue.ops = &uvc_queue_qops;
138         queue->queue.mem_ops = &vb2_vmalloc_memops;
139         queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
140                                      | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
141         ret = vb2_queue_init(&queue->queue);
142         if (ret)
143                 return ret;
144
145         mutex_init(&queue->mutex);
146         spin_lock_init(&queue->irqlock);
147         INIT_LIST_HEAD(&queue->irqqueue);
148         queue->flags = 0;
149
150         return 0;
151 }
152
153 /*
154  * Free the video buffers.
155  */
156 void uvcg_free_buffers(struct uvc_video_queue *queue)
157 {
158         mutex_lock(&queue->mutex);
159         vb2_queue_release(&queue->queue);
160         mutex_unlock(&queue->mutex);
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         mutex_lock(&queue->mutex);
172         ret = vb2_reqbufs(&queue->queue, rb);
173         mutex_unlock(&queue->mutex);
174
175         return ret ? ret : rb->count;
176 }
177
178 int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
179 {
180         int ret;
181
182         mutex_lock(&queue->mutex);
183         ret = vb2_querybuf(&queue->queue, buf);
184         mutex_unlock(&queue->mutex);
185
186         return ret;
187 }
188
189 int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
190 {
191         unsigned long flags;
192         int ret;
193
194         mutex_lock(&queue->mutex);
195         ret = vb2_qbuf(&queue->queue, buf);
196         if (ret < 0)
197                 goto done;
198
199         spin_lock_irqsave(&queue->irqlock, flags);
200         ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
201         queue->flags &= ~UVC_QUEUE_PAUSED;
202         spin_unlock_irqrestore(&queue->irqlock, flags);
203
204 done:
205         mutex_unlock(&queue->mutex);
206         return ret;
207 }
208
209 /*
210  * Dequeue a video buffer. If nonblocking is false, block until a buffer is
211  * available.
212  */
213 int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
214                         int nonblocking)
215 {
216         int ret;
217
218         mutex_lock(&queue->mutex);
219         ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
220         mutex_unlock(&queue->mutex);
221
222         return ret;
223 }
224
225 /*
226  * Poll the video queue.
227  *
228  * This function implements video queue polling and is intended to be used by
229  * the device poll handler.
230  */
231 unsigned int uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
232                              poll_table *wait)
233 {
234         unsigned int ret;
235
236         mutex_lock(&queue->mutex);
237         ret = vb2_poll(&queue->queue, file, wait);
238         mutex_unlock(&queue->mutex);
239
240         return ret;
241 }
242
243 int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
244 {
245         int ret;
246
247         mutex_lock(&queue->mutex);
248         ret = vb2_mmap(&queue->queue, vma);
249         mutex_unlock(&queue->mutex);
250
251         return ret;
252 }
253
254 #ifndef CONFIG_MMU
255 /*
256  * Get unmapped area.
257  *
258  * NO-MMU arch need this function to make mmap() work correctly.
259  */
260 unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
261                                            unsigned long pgoff)
262 {
263         unsigned long ret;
264
265         mutex_lock(&queue->mutex);
266         ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
267         mutex_unlock(&queue->mutex);
268         return ret;
269 }
270 #endif
271
272 /*
273  * Cancel the video buffers queue.
274  *
275  * Cancelling the queue marks all buffers on the irq queue as erroneous,
276  * wakes them up and removes them from the queue.
277  *
278  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
279  * fail with -ENODEV.
280  *
281  * This function acquires the irq spinlock and can be called from interrupt
282  * context.
283  */
284 void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
285 {
286         struct uvc_buffer *buf;
287         unsigned long flags;
288
289         spin_lock_irqsave(&queue->irqlock, flags);
290         while (!list_empty(&queue->irqqueue)) {
291                 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
292                                        queue);
293                 list_del(&buf->queue);
294                 buf->state = UVC_BUF_STATE_ERROR;
295                 vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
296         }
297         /* This must be protected by the irqlock spinlock to avoid race
298          * conditions between uvc_queue_buffer and the disconnection event that
299          * could result in an interruptible wait in uvc_dequeue_buffer. Do not
300          * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
301          * state outside the queue code.
302          */
303         if (disconnect)
304                 queue->flags |= UVC_QUEUE_DISCONNECTED;
305         spin_unlock_irqrestore(&queue->irqlock, flags);
306 }
307
308 /*
309  * Enable or disable the video buffers queue.
310  *
311  * The queue must be enabled before starting video acquisition and must be
312  * disabled after stopping it. This ensures that the video buffers queue
313  * state can be properly initialized before buffers are accessed from the
314  * interrupt handler.
315  *
316  * Enabling the video queue initializes parameters (such as sequence number,
317  * sync pattern, ...). If the queue is already enabled, return -EBUSY.
318  *
319  * Disabling the video queue cancels the queue and removes all buffers from
320  * the main queue.
321  *
322  * This function can't be called from interrupt context. Use
323  * uvcg_queue_cancel() instead.
324  */
325 int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
326 {
327         unsigned long flags;
328         int ret = 0;
329
330         mutex_lock(&queue->mutex);
331         if (enable) {
332                 ret = vb2_streamon(&queue->queue, queue->queue.type);
333                 if (ret < 0)
334                         goto done;
335
336                 queue->sequence = 0;
337                 queue->buf_used = 0;
338         } else {
339                 ret = vb2_streamoff(&queue->queue, queue->queue.type);
340                 if (ret < 0)
341                         goto done;
342
343                 spin_lock_irqsave(&queue->irqlock, flags);
344                 INIT_LIST_HEAD(&queue->irqqueue);
345
346                 /*
347                  * FIXME: We need to clear the DISCONNECTED flag to ensure that
348                  * applications will be able to queue buffers for the next
349                  * streaming run. However, clearing it here doesn't guarantee
350                  * that the device will be reconnected in the meantime.
351                  */
352                 queue->flags &= ~UVC_QUEUE_DISCONNECTED;
353                 spin_unlock_irqrestore(&queue->irqlock, flags);
354         }
355
356 done:
357         mutex_unlock(&queue->mutex);
358         return ret;
359 }
360
361 /* called with &queue_irqlock held.. */
362 struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue,
363                                           struct uvc_buffer *buf)
364 {
365         struct uvc_buffer *nextbuf;
366
367         if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
368              buf->length != buf->bytesused) {
369                 buf->state = UVC_BUF_STATE_QUEUED;
370                 vb2_set_plane_payload(&buf->buf, 0, 0);
371                 return buf;
372         }
373
374         list_del(&buf->queue);
375         if (!list_empty(&queue->irqqueue))
376                 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
377                                            queue);
378         else
379                 nextbuf = NULL;
380
381         buf->buf.v4l2_buf.field = V4L2_FIELD_NONE;
382         buf->buf.v4l2_buf.sequence = queue->sequence++;
383         v4l2_get_timestamp(&buf->buf.v4l2_buf.timestamp);
384
385         vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
386         vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
387
388         return nextbuf;
389 }
390
391 struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
392 {
393         struct uvc_buffer *buf = NULL;
394
395         if (!list_empty(&queue->irqqueue))
396                 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
397                                        queue);
398         else
399                 queue->flags |= UVC_QUEUE_PAUSED;
400
401         return buf;
402 }
403