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