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