]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/media/usb/uvc/uvc_queue.c
uvcvideo: Prevent new URBs being processed at stream stop
[zynq/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-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         /*
86          * When called with plane sizes, validate them. The driver supports
87          * single planar formats only, and requires buffers to be large enough
88          * to store a complete frame.
89          */
90         if (*nplanes)
91                 return *nplanes != 1 || sizes[0] < size ? -EINVAL : 0;
92
93         *nplanes = 1;
94         sizes[0] = size;
95         return 0;
96 }
97
98 static int uvc_buffer_prepare(struct vb2_buffer *vb)
99 {
100         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
101         struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
102         struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
103
104         if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
105             vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
106                 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
107                 return -EINVAL;
108         }
109
110         if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
111                 return -ENODEV;
112
113         buf->state = UVC_BUF_STATE_QUEUED;
114         buf->error = 0;
115         buf->mem = vb2_plane_vaddr(vb, 0);
116         buf->length = vb2_plane_size(vb, 0);
117         if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
118                 buf->bytesused = 0;
119         else
120                 buf->bytesused = vb2_get_plane_payload(vb, 0);
121
122         return 0;
123 }
124
125 static void uvc_buffer_queue(struct vb2_buffer *vb)
126 {
127         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
128         struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
129         struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
130         unsigned long flags;
131
132         spin_lock_irqsave(&queue->irqlock, flags);
133         if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
134                 kref_init(&buf->ref);
135                 list_add_tail(&buf->queue, &queue->irqqueue);
136         } else {
137                 /* If the device is disconnected return the buffer to userspace
138                  * directly. The next QBUF call will fail with -ENODEV.
139                  */
140                 buf->state = UVC_BUF_STATE_ERROR;
141                 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
142         }
143
144         spin_unlock_irqrestore(&queue->irqlock, flags);
145 }
146
147 static void uvc_buffer_finish(struct vb2_buffer *vb)
148 {
149         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
150         struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
151         struct uvc_streaming *stream = uvc_queue_to_stream(queue);
152         struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
153
154         if (vb->state == VB2_BUF_STATE_DONE)
155                 uvc_video_clock_update(stream, vbuf, buf);
156 }
157
158 static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count)
159 {
160         struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
161         struct uvc_streaming *stream = uvc_queue_to_stream(queue);
162         unsigned long flags;
163         int ret;
164
165         queue->buf_used = 0;
166
167         ret = uvc_video_enable(stream, 1);
168         if (ret == 0)
169                 return 0;
170
171         spin_lock_irqsave(&queue->irqlock, flags);
172         uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED);
173         spin_unlock_irqrestore(&queue->irqlock, flags);
174
175         return ret;
176 }
177
178 static void uvc_stop_streaming(struct vb2_queue *vq)
179 {
180         struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
181         struct uvc_streaming *stream = uvc_queue_to_stream(queue);
182
183         /* Prevent new buffers coming in. */
184         spin_lock_irq(&queue->irqlock);
185         queue->flags |= UVC_QUEUE_STOPPING;
186         spin_unlock_irq(&queue->irqlock);
187
188         /*
189          * All pending work should be completed before disabling the stream, as
190          * all URBs will be free'd during uvc_video_enable(s, 0).
191          */
192         flush_workqueue(stream->async_wq);
193
194         uvc_video_enable(stream, 0);
195
196         spin_lock_irq(&queue->irqlock);
197         uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
198         queue->flags &= ~UVC_QUEUE_STOPPING;
199         spin_unlock_irq(&queue->irqlock);
200 }
201
202 static const struct vb2_ops uvc_queue_qops = {
203         .queue_setup = uvc_queue_setup,
204         .buf_prepare = uvc_buffer_prepare,
205         .buf_queue = uvc_buffer_queue,
206         .buf_finish = uvc_buffer_finish,
207         .wait_prepare = vb2_ops_wait_prepare,
208         .wait_finish = vb2_ops_wait_finish,
209         .start_streaming = uvc_start_streaming,
210         .stop_streaming = uvc_stop_streaming,
211 };
212
213 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
214                     int drop_corrupted)
215 {
216         int ret;
217
218         queue->queue.type = type;
219         queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
220         queue->queue.drv_priv = queue;
221         queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
222         queue->queue.ops = &uvc_queue_qops;
223         queue->queue.mem_ops = &vb2_vmalloc_memops;
224         queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
225                 | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
226         queue->queue.lock = &queue->mutex;
227         ret = vb2_queue_init(&queue->queue);
228         if (ret)
229                 return ret;
230
231         mutex_init(&queue->mutex);
232         spin_lock_init(&queue->irqlock);
233         INIT_LIST_HEAD(&queue->irqqueue);
234         queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
235
236         return 0;
237 }
238
239 void uvc_queue_release(struct uvc_video_queue *queue)
240 {
241         mutex_lock(&queue->mutex);
242         vb2_queue_release(&queue->queue);
243         mutex_unlock(&queue->mutex);
244 }
245
246 /* -----------------------------------------------------------------------------
247  * V4L2 queue operations
248  */
249
250 int uvc_request_buffers(struct uvc_video_queue *queue,
251                         struct v4l2_requestbuffers *rb)
252 {
253         int ret;
254
255         mutex_lock(&queue->mutex);
256         ret = vb2_reqbufs(&queue->queue, rb);
257         mutex_unlock(&queue->mutex);
258
259         return ret ? ret : rb->count;
260 }
261
262 int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
263 {
264         int ret;
265
266         mutex_lock(&queue->mutex);
267         ret = vb2_querybuf(&queue->queue, buf);
268         mutex_unlock(&queue->mutex);
269
270         return ret;
271 }
272
273 int uvc_create_buffers(struct uvc_video_queue *queue,
274                        struct v4l2_create_buffers *cb)
275 {
276         int ret;
277
278         mutex_lock(&queue->mutex);
279         ret = vb2_create_bufs(&queue->queue, cb);
280         mutex_unlock(&queue->mutex);
281
282         return ret;
283 }
284
285 int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
286 {
287         int ret;
288
289         mutex_lock(&queue->mutex);
290         ret = vb2_qbuf(&queue->queue, buf);
291         mutex_unlock(&queue->mutex);
292
293         return ret;
294 }
295
296 int uvc_export_buffer(struct uvc_video_queue *queue,
297                       struct v4l2_exportbuffer *exp)
298 {
299         int ret;
300
301         mutex_lock(&queue->mutex);
302         ret = vb2_expbuf(&queue->queue, exp);
303         mutex_unlock(&queue->mutex);
304
305         return ret;
306 }
307
308 int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
309                        int nonblocking)
310 {
311         int ret;
312
313         mutex_lock(&queue->mutex);
314         ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
315         mutex_unlock(&queue->mutex);
316
317         return ret;
318 }
319
320 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type)
321 {
322         int ret;
323
324         mutex_lock(&queue->mutex);
325         ret = vb2_streamon(&queue->queue, type);
326         mutex_unlock(&queue->mutex);
327
328         return ret;
329 }
330
331 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type)
332 {
333         int ret;
334
335         mutex_lock(&queue->mutex);
336         ret = vb2_streamoff(&queue->queue, type);
337         mutex_unlock(&queue->mutex);
338
339         return ret;
340 }
341
342 int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
343 {
344         return vb2_mmap(&queue->queue, vma);
345 }
346
347 #ifndef CONFIG_MMU
348 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
349                 unsigned long pgoff)
350 {
351         return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
352 }
353 #endif
354
355 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
356                             poll_table *wait)
357 {
358         unsigned int ret;
359
360         mutex_lock(&queue->mutex);
361         ret = vb2_poll(&queue->queue, file, wait);
362         mutex_unlock(&queue->mutex);
363
364         return ret;
365 }
366
367 /* -----------------------------------------------------------------------------
368  *
369  */
370
371 /*
372  * Check if buffers have been allocated.
373  */
374 int uvc_queue_allocated(struct uvc_video_queue *queue)
375 {
376         int allocated;
377
378         mutex_lock(&queue->mutex);
379         allocated = vb2_is_busy(&queue->queue);
380         mutex_unlock(&queue->mutex);
381
382         return allocated;
383 }
384
385 /*
386  * Cancel the video buffers queue.
387  *
388  * Cancelling the queue marks all buffers on the irq queue as erroneous,
389  * wakes them up and removes them from the queue.
390  *
391  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
392  * fail with -ENODEV.
393  *
394  * This function acquires the irq spinlock and can be called from interrupt
395  * context.
396  */
397 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
398 {
399         unsigned long flags;
400
401         spin_lock_irqsave(&queue->irqlock, flags);
402         uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
403         /* This must be protected by the irqlock spinlock to avoid race
404          * conditions between uvc_buffer_queue and the disconnection event that
405          * could result in an interruptible wait in uvc_dequeue_buffer. Do not
406          * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
407          * state outside the queue code.
408          */
409         if (disconnect)
410                 queue->flags |= UVC_QUEUE_DISCONNECTED;
411         spin_unlock_irqrestore(&queue->irqlock, flags);
412 }
413
414 /*
415  * uvc_queue_get_current_buffer: Obtain the current working output buffer
416  *
417  * Buffers may span multiple packets, and even URBs, therefore the active buffer
418  * remains on the queue until the EOF marker.
419  */
420 static struct uvc_buffer *
421 __uvc_queue_get_current_buffer(struct uvc_video_queue *queue)
422 {
423         if (!list_empty(&queue->irqqueue))
424                 return list_first_entry(&queue->irqqueue, struct uvc_buffer,
425                                         queue);
426         else
427                 return NULL;
428 }
429
430 struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue)
431 {
432         struct uvc_buffer *nextbuf;
433         unsigned long flags;
434
435         spin_lock_irqsave(&queue->irqlock, flags);
436         nextbuf = __uvc_queue_get_current_buffer(queue);
437         spin_unlock_irqrestore(&queue->irqlock, flags);
438
439         return nextbuf;
440 }
441
442 /*
443  * uvc_queue_requeue: Requeue a buffer on our internal irqqueue
444  *
445  * Reuse a buffer through our internal queue without the need to 'prepare'
446  * The buffer will be returned to userspace through the uvc_buffer_queue call if
447  * the device has been disconnected
448  */
449 static void uvc_queue_requeue(struct uvc_video_queue *queue,
450                 struct uvc_buffer *buf)
451 {
452         buf->error = 0;
453         buf->state = UVC_BUF_STATE_QUEUED;
454         buf->bytesused = 0;
455         vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
456
457         uvc_buffer_queue(&buf->buf.vb2_buf);
458 }
459
460 static void uvc_queue_buffer_complete(struct kref *ref)
461 {
462         struct uvc_buffer *buf = container_of(ref, struct uvc_buffer, ref);
463         struct vb2_buffer *vb = &buf->buf.vb2_buf;
464         struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
465
466         if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
467                 uvc_queue_requeue(queue, buf);
468                 return;
469         }
470
471         buf->state = buf->error ? UVC_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
472         vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
473         vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
474 }
475
476 /*
477  * Release a reference on the buffer. Complete the buffer when the last
478  * reference is released
479  */
480 void uvc_queue_buffer_release(struct uvc_buffer *buf)
481 {
482         kref_put(&buf->ref, uvc_queue_buffer_complete);
483 }
484
485 /*
486  * Remove this buffer from the queue. Lifetime will persist while async actions
487  * are still running (if any), and uvc_queue_buffer_release will give the buffer
488  * back to VB2 when all users have completed.
489  */
490 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
491                 struct uvc_buffer *buf)
492 {
493         struct uvc_buffer *nextbuf;
494         unsigned long flags;
495
496         spin_lock_irqsave(&queue->irqlock, flags);
497         list_del(&buf->queue);
498         nextbuf = __uvc_queue_get_current_buffer(queue);
499         spin_unlock_irqrestore(&queue->irqlock, flags);
500
501         uvc_queue_buffer_release(buf);
502
503         return nextbuf;
504 }