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