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