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