Merge branch 'semaphore' of git://git.kernel.org/pub/scm/linux/kernel/git/willy/misc
[pandora-kernel.git] / drivers / media / video / uvc / uvc_queue.c
1 /*
2  *      uvc_queue.c  --  USB Video Class driver - Buffers management
3  *
4  *      Copyright (C) 2005-2008
5  *          Laurent Pinchart (laurent.pinchart@skynet.be)
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/kernel.h>
15 #include <linux/version.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 <asm/atomic.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 buffer allocation and freeing are performed by uvc_alloc_buffers and
34  * uvc_free_buffers respectively. The former acquires the video queue lock,
35  * while the later must be called with the lock held (so that allocation can
36  * free previously allocated buffers). Trying to free buffers that are mapped
37  * to user space will return -EBUSY.
38  *
39  * Video buffers are managed using two queues. However, unlike most USB video
40  * drivers which use an in queue and an out queue, we use a main queue which
41  * holds all queued buffers (both 'empty' and 'done' buffers), and an irq
42  * queue which holds empty buffers. This design (copied from video-buf)
43  * minimizes locking in interrupt, as only one queue is shared between
44  * interrupt and user contexts.
45  *
46  * Use cases
47  * ---------
48  *
49  * Unless stated otherwise, all operations which modify the irq buffers queue
50  * are protected by the irq spinlock.
51  *
52  * 1. The user queues the buffers, starts streaming and dequeues a buffer.
53  *
54  *    The buffers are added to the main and irq queues. Both operations are
55  *    protected by the queue lock, and the latert is protected by the irq
56  *    spinlock as well.
57  *
58  *    The completion handler fetches a buffer from the irq queue and fills it
59  *    with video data. If no buffer is available (irq queue empty), the handler
60  *    returns immediately.
61  *
62  *    When the buffer is full, the completion handler removes it from the irq
63  *    queue, marks it as ready (UVC_BUF_STATE_DONE) and wake its wait queue.
64  *    At that point, any process waiting on the buffer will be woken up. If a
65  *    process tries to dequeue a buffer after it has been marked ready, the
66  *    dequeing will succeed immediately.
67  *
68  * 2. Buffers are queued, user is waiting on a buffer and the device gets
69  *    disconnected.
70  *
71  *    When the device is disconnected, the kernel calls the completion handler
72  *    with an appropriate status code. The handler marks all buffers in the
73  *    irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
74  *    that any process waiting on a buffer gets woken up.
75  *
76  *    Waking up up the first buffer on the irq list is not enough, as the
77  *    process waiting on the buffer might restart the dequeue operation
78  *    immediately.
79  *
80  */
81
82 void uvc_queue_init(struct uvc_video_queue *queue)
83 {
84         mutex_init(&queue->mutex);
85         spin_lock_init(&queue->irqlock);
86         INIT_LIST_HEAD(&queue->mainqueue);
87         INIT_LIST_HEAD(&queue->irqqueue);
88 }
89
90 /*
91  * Allocate the video buffers.
92  *
93  * Pages are reserved to make sure they will not be swaped, as they will be
94  * filled in URB completion handler.
95  *
96  * Buffers will be individually mapped, so they must all be page aligned.
97  */
98 int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
99                 unsigned int buflength)
100 {
101         unsigned int bufsize = PAGE_ALIGN(buflength);
102         unsigned int i;
103         void *mem = NULL;
104         int ret;
105
106         if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
107                 nbuffers = UVC_MAX_VIDEO_BUFFERS;
108
109         mutex_lock(&queue->mutex);
110
111         if ((ret = uvc_free_buffers(queue)) < 0)
112                 goto done;
113
114         /* Bail out if no buffers should be allocated. */
115         if (nbuffers == 0)
116                 goto done;
117
118         /* Decrement the number of buffers until allocation succeeds. */
119         for (; nbuffers > 0; --nbuffers) {
120                 mem = vmalloc_32(nbuffers * bufsize);
121                 if (mem != NULL)
122                         break;
123         }
124
125         if (mem == NULL) {
126                 ret = -ENOMEM;
127                 goto done;
128         }
129
130         for (i = 0; i < nbuffers; ++i) {
131                 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
132                 queue->buffer[i].buf.index = i;
133                 queue->buffer[i].buf.m.offset = i * bufsize;
134                 queue->buffer[i].buf.length = buflength;
135                 queue->buffer[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
136                 queue->buffer[i].buf.sequence = 0;
137                 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
138                 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
139                 queue->buffer[i].buf.flags = 0;
140                 init_waitqueue_head(&queue->buffer[i].wait);
141         }
142
143         queue->mem = mem;
144         queue->count = nbuffers;
145         queue->buf_size = bufsize;
146         ret = nbuffers;
147
148 done:
149         mutex_unlock(&queue->mutex);
150         return ret;
151 }
152
153 /*
154  * Free the video buffers.
155  *
156  * This function must be called with the queue lock held.
157  */
158 int uvc_free_buffers(struct uvc_video_queue *queue)
159 {
160         unsigned int i;
161
162         for (i = 0; i < queue->count; ++i) {
163                 if (queue->buffer[i].vma_use_count != 0)
164                         return -EBUSY;
165         }
166
167         if (queue->count) {
168                 vfree(queue->mem);
169                 queue->count = 0;
170         }
171
172         return 0;
173 }
174
175 static void __uvc_query_buffer(struct uvc_buffer *buf,
176                 struct v4l2_buffer *v4l2_buf)
177 {
178         memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
179
180         if (buf->vma_use_count)
181                 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
182
183         switch (buf->state) {
184         case UVC_BUF_STATE_ERROR:
185         case UVC_BUF_STATE_DONE:
186                 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
187                 break;
188         case UVC_BUF_STATE_QUEUED:
189         case UVC_BUF_STATE_ACTIVE:
190                 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
191                 break;
192         case UVC_BUF_STATE_IDLE:
193         default:
194                 break;
195         }
196 }
197
198 int uvc_query_buffer(struct uvc_video_queue *queue,
199                 struct v4l2_buffer *v4l2_buf)
200 {
201         int ret = 0;
202
203         mutex_lock(&queue->mutex);
204         if (v4l2_buf->index >= queue->count) {
205                 ret = -EINVAL;
206                 goto done;
207         }
208
209         __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
210
211 done:
212        mutex_unlock(&queue->mutex);
213        return ret;
214 }
215
216 /*
217  * Queue a video buffer. Attempting to queue a buffer that has already been
218  * queued will return -EINVAL.
219  */
220 int uvc_queue_buffer(struct uvc_video_queue *queue,
221         struct v4l2_buffer *v4l2_buf)
222 {
223         struct uvc_buffer *buf;
224         unsigned long flags;
225         int ret = 0;
226
227         uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
228
229         if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
230             v4l2_buf->memory != V4L2_MEMORY_MMAP) {
231                 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
232                         "and/or memory (%u).\n", v4l2_buf->type,
233                         v4l2_buf->memory);
234                 return -EINVAL;
235         }
236
237         mutex_lock(&queue->mutex);
238         if (v4l2_buf->index >= queue->count)  {
239                 uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
240                 ret = -EINVAL;
241                 goto done;
242         }
243
244         buf = &queue->buffer[v4l2_buf->index];
245         if (buf->state != UVC_BUF_STATE_IDLE) {
246                 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
247                         "(%u).\n", buf->state);
248                 ret = -EINVAL;
249                 goto done;
250         }
251
252         spin_lock_irqsave(&queue->irqlock, flags);
253         if (queue->flags & UVC_QUEUE_DISCONNECTED) {
254                 spin_unlock_irqrestore(&queue->irqlock, flags);
255                 ret = -ENODEV;
256                 goto done;
257         }
258         buf->state = UVC_BUF_STATE_QUEUED;
259         buf->buf.bytesused = 0;
260         list_add_tail(&buf->stream, &queue->mainqueue);
261         list_add_tail(&buf->queue, &queue->irqqueue);
262         spin_unlock_irqrestore(&queue->irqlock, flags);
263
264 done:
265         mutex_unlock(&queue->mutex);
266         return ret;
267 }
268
269 static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
270 {
271         if (nonblocking) {
272                 return (buf->state != UVC_BUF_STATE_QUEUED &&
273                         buf->state != UVC_BUF_STATE_ACTIVE)
274                         ? 0 : -EAGAIN;
275         }
276
277         return wait_event_interruptible(buf->wait,
278                 buf->state != UVC_BUF_STATE_QUEUED &&
279                 buf->state != UVC_BUF_STATE_ACTIVE);
280 }
281
282 /*
283  * Dequeue a video buffer. If nonblocking is false, block until a buffer is
284  * available.
285  */
286 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
287                 struct v4l2_buffer *v4l2_buf, int nonblocking)
288 {
289         struct uvc_buffer *buf;
290         int ret = 0;
291
292         if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
293             v4l2_buf->memory != V4L2_MEMORY_MMAP) {
294                 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
295                         "and/or memory (%u).\n", v4l2_buf->type,
296                         v4l2_buf->memory);
297                 return -EINVAL;
298         }
299
300         mutex_lock(&queue->mutex);
301         if (list_empty(&queue->mainqueue)) {
302                 uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
303                 ret = -EINVAL;
304                 goto done;
305         }
306
307         buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
308         if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
309                 goto done;
310
311         uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
312                 buf->buf.index, buf->state, buf->buf.bytesused);
313
314         switch (buf->state) {
315         case UVC_BUF_STATE_ERROR:
316                 uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
317                         "(transmission error).\n");
318                 ret = -EIO;
319         case UVC_BUF_STATE_DONE:
320                 buf->state = UVC_BUF_STATE_IDLE;
321                 break;
322
323         case UVC_BUF_STATE_IDLE:
324         case UVC_BUF_STATE_QUEUED:
325         case UVC_BUF_STATE_ACTIVE:
326         default:
327                 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
328                         "(driver bug?).\n", buf->state);
329                 ret = -EINVAL;
330                 goto done;
331         }
332
333         list_del(&buf->stream);
334         __uvc_query_buffer(buf, v4l2_buf);
335
336 done:
337         mutex_unlock(&queue->mutex);
338         return ret;
339 }
340
341 /*
342  * Poll the video queue.
343  *
344  * This function implements video queue polling and is intended to be used by
345  * the device poll handler.
346  */
347 unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
348                 poll_table *wait)
349 {
350         struct uvc_buffer *buf;
351         unsigned int mask = 0;
352
353         mutex_lock(&queue->mutex);
354         if (list_empty(&queue->mainqueue)) {
355                 mask |= POLLERR;
356                 goto done;
357         }
358         buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
359
360         poll_wait(file, &buf->wait, wait);
361         if (buf->state == UVC_BUF_STATE_DONE ||
362             buf->state == UVC_BUF_STATE_ERROR)
363                 mask |= POLLIN | POLLRDNORM;
364
365 done:
366         mutex_unlock(&queue->mutex);
367         return mask;
368 }
369
370 /*
371  * Enable or disable the video buffers queue.
372  *
373  * The queue must be enabled before starting video acquisition and must be
374  * disabled after stopping it. This ensures that the video buffers queue
375  * state can be properly initialized before buffers are accessed from the
376  * interrupt handler.
377  *
378  * Enabling the video queue initializes parameters (such as sequence number,
379  * sync pattern, ...). If the queue is already enabled, return -EBUSY.
380  *
381  * Disabling the video queue cancels the queue and removes all buffers from
382  * the main queue.
383  *
384  * This function can't be called from interrupt context. Use
385  * uvc_queue_cancel() instead.
386  */
387 int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
388 {
389         unsigned int i;
390         int ret = 0;
391
392         mutex_lock(&queue->mutex);
393         if (enable) {
394                 if (uvc_queue_streaming(queue)) {
395                         ret = -EBUSY;
396                         goto done;
397                 }
398                 queue->sequence = 0;
399                 queue->flags |= UVC_QUEUE_STREAMING;
400         } else {
401                 uvc_queue_cancel(queue, 0);
402                 INIT_LIST_HEAD(&queue->mainqueue);
403
404                 for (i = 0; i < queue->count; ++i)
405                         queue->buffer[i].state = UVC_BUF_STATE_IDLE;
406
407                 queue->flags &= ~UVC_QUEUE_STREAMING;
408         }
409
410 done:
411         mutex_unlock(&queue->mutex);
412         return ret;
413 }
414
415 /*
416  * Cancel the video buffers queue.
417  *
418  * Cancelling the queue marks all buffers on the irq queue as erroneous,
419  * wakes them up and remove them from the queue.
420  *
421  * If the disconnect parameter is set, further calls to uvc_queue_buffer will
422  * fail with -ENODEV.
423  *
424  * This function acquires the irq spinlock and can be called from interrupt
425  * context.
426  */
427 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
428 {
429         struct uvc_buffer *buf;
430         unsigned long flags;
431
432         spin_lock_irqsave(&queue->irqlock, flags);
433         while (!list_empty(&queue->irqqueue)) {
434                 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
435                                        queue);
436                 list_del(&buf->queue);
437                 buf->state = UVC_BUF_STATE_ERROR;
438                 wake_up(&buf->wait);
439         }
440         /* This must be protected by the irqlock spinlock to avoid race
441          * conditions between uvc_queue_buffer and the disconnection event that
442          * could result in an interruptible wait in uvc_dequeue_buffer. Do not
443          * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
444          * state outside the queue code.
445          */
446         if (disconnect)
447                 queue->flags |= UVC_QUEUE_DISCONNECTED;
448         spin_unlock_irqrestore(&queue->irqlock, flags);
449 }
450
451 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
452                 struct uvc_buffer *buf)
453 {
454         struct uvc_buffer *nextbuf;
455         unsigned long flags;
456
457         if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
458             buf->buf.length != buf->buf.bytesused) {
459                 buf->state = UVC_BUF_STATE_QUEUED;
460                 buf->buf.bytesused = 0;
461                 return buf;
462         }
463
464         spin_lock_irqsave(&queue->irqlock, flags);
465         list_del(&buf->queue);
466         if (!list_empty(&queue->irqqueue))
467                 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
468                                            queue);
469         else
470                 nextbuf = NULL;
471         spin_unlock_irqrestore(&queue->irqlock, flags);
472
473         buf->buf.sequence = queue->sequence++;
474         do_gettimeofday(&buf->buf.timestamp);
475
476         wake_up(&buf->wait);
477         return nextbuf;
478 }
479