63d7043502d683c9ba5bc9fea3290b8bc8a58ad0
[safe/jmp/linux-2.6] / drivers / media / video / videobuf-core.c
1 /*
2  * generic helper functions for handling video4linux capture buffers
3  *
4  * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
5  *
6  * Highly based on video-buf written originally by:
7  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
8  * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
9  * (c) 2006 Ted Walther and John Sokol
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2
14  */
15
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/mm.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23
24 #include <media/videobuf-core.h>
25
26 #define MAGIC_BUFFER 0x20070728
27 #define MAGIC_CHECK(is, should)                                         \
28         do {                                                            \
29                 if (unlikely((is) != (should))) {                       \
30                         printk(KERN_ERR                                 \
31                                 "magic mismatch: %x (expected %x)\n",   \
32                                         is, should);                    \
33                         BUG();                                          \
34                 }                                                       \
35         } while (0)
36
37 static int debug;
38 module_param(debug, int, 0644);
39
40 MODULE_DESCRIPTION("helper module to manage video4linux buffers");
41 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
42 MODULE_LICENSE("GPL");
43
44 #define dprintk(level, fmt, arg...)                                     \
45         do {                                                            \
46                 if (debug >= level)                                     \
47                         printk(KERN_DEBUG "vbuf: " fmt, ## arg);        \
48         } while (0)
49
50 /* --------------------------------------------------------------------- */
51
52 #define CALL(q, f, arg...)                                              \
53         ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
54
55 void *videobuf_alloc(struct videobuf_queue *q)
56 {
57         struct videobuf_buffer *vb;
58
59         BUG_ON(q->msize < sizeof(*vb));
60
61         if (!q->int_ops || !q->int_ops->alloc) {
62                 printk(KERN_ERR "No specific ops defined!\n");
63                 BUG();
64         }
65
66         vb = q->int_ops->alloc(q->msize);
67         if (NULL != vb) {
68                 init_waitqueue_head(&vb->done);
69                 vb->magic = MAGIC_BUFFER;
70         }
71
72         return vb;
73 }
74 EXPORT_SYMBOL_GPL(videobuf_alloc);
75
76 #define WAITON_CONDITION (vb->state != VIDEOBUF_ACTIVE &&\
77                                 vb->state != VIDEOBUF_QUEUED)
78 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
79 {
80         MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
81
82         if (non_blocking) {
83                 if (WAITON_CONDITION)
84                         return 0;
85                 else
86                         return -EAGAIN;
87         }
88
89         if (intr)
90                 return wait_event_interruptible(vb->done, WAITON_CONDITION);
91         else
92                 wait_event(vb->done, WAITON_CONDITION);
93
94         return 0;
95 }
96 EXPORT_SYMBOL_GPL(videobuf_waiton);
97
98 int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
99                     struct v4l2_framebuffer *fbuf)
100 {
101         MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
102         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
103
104         return CALL(q, iolock, q, vb, fbuf);
105 }
106 EXPORT_SYMBOL_GPL(videobuf_iolock);
107
108 void *videobuf_queue_to_vmalloc(struct videobuf_queue *q,
109                                 struct videobuf_buffer *buf)
110 {
111         if (q->int_ops->vmalloc)
112                 return q->int_ops->vmalloc(buf);
113         else
114                 return NULL;
115 }
116 EXPORT_SYMBOL_GPL(videobuf_queue_to_vmalloc);
117
118 /* --------------------------------------------------------------------- */
119
120
121 void videobuf_queue_core_init(struct videobuf_queue *q,
122                          const struct videobuf_queue_ops *ops,
123                          struct device *dev,
124                          spinlock_t *irqlock,
125                          enum v4l2_buf_type type,
126                          enum v4l2_field field,
127                          unsigned int msize,
128                          void *priv,
129                          struct videobuf_qtype_ops *int_ops)
130 {
131         BUG_ON(!q);
132         memset(q, 0, sizeof(*q));
133         q->irqlock   = irqlock;
134         q->dev       = dev;
135         q->type      = type;
136         q->field     = field;
137         q->msize     = msize;
138         q->ops       = ops;
139         q->priv_data = priv;
140         q->int_ops   = int_ops;
141
142         /* All buffer operations are mandatory */
143         BUG_ON(!q->ops->buf_setup);
144         BUG_ON(!q->ops->buf_prepare);
145         BUG_ON(!q->ops->buf_queue);
146         BUG_ON(!q->ops->buf_release);
147
148         /* Lock is mandatory for queue_cancel to work */
149         BUG_ON(!irqlock);
150
151         /* Having implementations for abstract methods are mandatory */
152         BUG_ON(!q->int_ops);
153
154         mutex_init(&q->vb_lock);
155         init_waitqueue_head(&q->wait);
156         INIT_LIST_HEAD(&q->stream);
157 }
158 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
159
160 /* Locking: Only usage in bttv unsafe find way to remove */
161 int videobuf_queue_is_busy(struct videobuf_queue *q)
162 {
163         int i;
164
165         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
166
167         if (q->streaming) {
168                 dprintk(1, "busy: streaming active\n");
169                 return 1;
170         }
171         if (q->reading) {
172                 dprintk(1, "busy: pending read #1\n");
173                 return 1;
174         }
175         if (q->read_buf) {
176                 dprintk(1, "busy: pending read #2\n");
177                 return 1;
178         }
179         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
180                 if (NULL == q->bufs[i])
181                         continue;
182                 if (q->bufs[i]->map) {
183                         dprintk(1, "busy: buffer #%d mapped\n", i);
184                         return 1;
185                 }
186                 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
187                         dprintk(1, "busy: buffer #%d queued\n", i);
188                         return 1;
189                 }
190                 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
191                         dprintk(1, "busy: buffer #%d avtive\n", i);
192                         return 1;
193                 }
194         }
195         return 0;
196 }
197 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
198
199 /* Locking: Caller holds q->vb_lock */
200 void videobuf_queue_cancel(struct videobuf_queue *q)
201 {
202         unsigned long flags = 0;
203         int i;
204
205         q->streaming = 0;
206         q->reading  = 0;
207         wake_up_interruptible_sync(&q->wait);
208
209         /* remove queued buffers from list */
210         spin_lock_irqsave(q->irqlock, flags);
211         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
212                 if (NULL == q->bufs[i])
213                         continue;
214                 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
215                         list_del(&q->bufs[i]->queue);
216                         q->bufs[i]->state = VIDEOBUF_ERROR;
217                         wake_up_all(&q->bufs[i]->done);
218                 }
219         }
220         spin_unlock_irqrestore(q->irqlock, flags);
221
222         /* free all buffers + clear queue */
223         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
224                 if (NULL == q->bufs[i])
225                         continue;
226                 q->ops->buf_release(q, q->bufs[i]);
227         }
228         INIT_LIST_HEAD(&q->stream);
229 }
230 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
231
232 /* --------------------------------------------------------------------- */
233
234 /* Locking: Caller holds q->vb_lock */
235 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
236 {
237         enum v4l2_field field = q->field;
238
239         BUG_ON(V4L2_FIELD_ANY == field);
240
241         if (V4L2_FIELD_ALTERNATE == field) {
242                 if (V4L2_FIELD_TOP == q->last) {
243                         field   = V4L2_FIELD_BOTTOM;
244                         q->last = V4L2_FIELD_BOTTOM;
245                 } else {
246                         field   = V4L2_FIELD_TOP;
247                         q->last = V4L2_FIELD_TOP;
248                 }
249         }
250         return field;
251 }
252 EXPORT_SYMBOL_GPL(videobuf_next_field);
253
254 /* Locking: Caller holds q->vb_lock */
255 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
256                             struct videobuf_buffer *vb, enum v4l2_buf_type type)
257 {
258         MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
259         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
260
261         b->index    = vb->i;
262         b->type     = type;
263
264         b->memory   = vb->memory;
265         switch (b->memory) {
266         case V4L2_MEMORY_MMAP:
267                 b->m.offset  = vb->boff;
268                 b->length    = vb->bsize;
269                 break;
270         case V4L2_MEMORY_USERPTR:
271                 b->m.userptr = vb->baddr;
272                 b->length    = vb->bsize;
273                 break;
274         case V4L2_MEMORY_OVERLAY:
275                 b->m.offset  = vb->boff;
276                 break;
277         }
278
279         b->flags    = 0;
280         if (vb->map)
281                 b->flags |= V4L2_BUF_FLAG_MAPPED;
282
283         switch (vb->state) {
284         case VIDEOBUF_PREPARED:
285         case VIDEOBUF_QUEUED:
286         case VIDEOBUF_ACTIVE:
287                 b->flags |= V4L2_BUF_FLAG_QUEUED;
288                 break;
289         case VIDEOBUF_DONE:
290         case VIDEOBUF_ERROR:
291                 b->flags |= V4L2_BUF_FLAG_DONE;
292                 break;
293         case VIDEOBUF_NEEDS_INIT:
294         case VIDEOBUF_IDLE:
295                 /* nothing */
296                 break;
297         }
298
299         if (vb->input != UNSET) {
300                 b->flags |= V4L2_BUF_FLAG_INPUT;
301                 b->input  = vb->input;
302         }
303
304         b->field     = vb->field;
305         b->timestamp = vb->ts;
306         b->bytesused = vb->size;
307         b->sequence  = vb->field_count >> 1;
308 }
309
310 /* Locking: Caller holds q->vb_lock */
311 static int __videobuf_mmap_free(struct videobuf_queue *q)
312 {
313         int i;
314         int rc;
315
316         if (!q)
317                 return 0;
318
319         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
320
321         rc = CALL(q, mmap_free, q);
322
323         q->is_mmapped = 0;
324
325         if (rc < 0)
326                 return rc;
327
328         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
329                 if (NULL == q->bufs[i])
330                         continue;
331                 q->ops->buf_release(q, q->bufs[i]);
332                 kfree(q->bufs[i]);
333                 q->bufs[i] = NULL;
334         }
335
336         return rc;
337 }
338
339 int videobuf_mmap_free(struct videobuf_queue *q)
340 {
341         int ret;
342         mutex_lock(&q->vb_lock);
343         ret = __videobuf_mmap_free(q);
344         mutex_unlock(&q->vb_lock);
345         return ret;
346 }
347 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
348
349 /* Locking: Caller holds q->vb_lock */
350 int __videobuf_mmap_setup(struct videobuf_queue *q,
351                         unsigned int bcount, unsigned int bsize,
352                         enum v4l2_memory memory)
353 {
354         unsigned int i;
355         int err;
356
357         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
358
359         err = __videobuf_mmap_free(q);
360         if (0 != err)
361                 return err;
362
363         /* Allocate and initialize buffers */
364         for (i = 0; i < bcount; i++) {
365                 q->bufs[i] = videobuf_alloc(q);
366
367                 if (NULL == q->bufs[i])
368                         break;
369
370                 q->bufs[i]->i      = i;
371                 q->bufs[i]->input  = UNSET;
372                 q->bufs[i]->memory = memory;
373                 q->bufs[i]->bsize  = bsize;
374                 switch (memory) {
375                 case V4L2_MEMORY_MMAP:
376                         q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
377                         break;
378                 case V4L2_MEMORY_USERPTR:
379                 case V4L2_MEMORY_OVERLAY:
380                         /* nothing */
381                         break;
382                 }
383         }
384
385         if (!i)
386                 return -ENOMEM;
387
388         dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
389
390         return i;
391 }
392 EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
393
394 int videobuf_mmap_setup(struct videobuf_queue *q,
395                         unsigned int bcount, unsigned int bsize,
396                         enum v4l2_memory memory)
397 {
398         int ret;
399         mutex_lock(&q->vb_lock);
400         ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
401         mutex_unlock(&q->vb_lock);
402         return ret;
403 }
404 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
405
406 int videobuf_reqbufs(struct videobuf_queue *q,
407                  struct v4l2_requestbuffers *req)
408 {
409         unsigned int size, count;
410         int retval;
411
412         if (req->count < 1) {
413                 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
414                 return -EINVAL;
415         }
416
417         if (req->memory != V4L2_MEMORY_MMAP     &&
418             req->memory != V4L2_MEMORY_USERPTR  &&
419             req->memory != V4L2_MEMORY_OVERLAY) {
420                 dprintk(1, "reqbufs: memory type invalid\n");
421                 return -EINVAL;
422         }
423
424         mutex_lock(&q->vb_lock);
425         if (req->type != q->type) {
426                 dprintk(1, "reqbufs: queue type invalid\n");
427                 retval = -EINVAL;
428                 goto done;
429         }
430
431         if (q->streaming) {
432                 dprintk(1, "reqbufs: streaming already exists\n");
433                 retval = -EBUSY;
434                 goto done;
435         }
436         if (!list_empty(&q->stream)) {
437                 dprintk(1, "reqbufs: stream running\n");
438                 retval = -EBUSY;
439                 goto done;
440         }
441
442         count = req->count;
443         if (count > VIDEO_MAX_FRAME)
444                 count = VIDEO_MAX_FRAME;
445         size = 0;
446         q->ops->buf_setup(q, &count, &size);
447         dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
448                 count, size,
449                 (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
450
451         retval = __videobuf_mmap_setup(q, count, size, req->memory);
452         if (retval < 0) {
453                 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
454                 goto done;
455         }
456
457         req->count = retval;
458         retval = 0;
459
460  done:
461         mutex_unlock(&q->vb_lock);
462         return retval;
463 }
464 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
465
466 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
467 {
468         int ret = -EINVAL;
469
470         mutex_lock(&q->vb_lock);
471         if (unlikely(b->type != q->type)) {
472                 dprintk(1, "querybuf: Wrong type.\n");
473                 goto done;
474         }
475         if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
476                 dprintk(1, "querybuf: index out of range.\n");
477                 goto done;
478         }
479         if (unlikely(NULL == q->bufs[b->index])) {
480                 dprintk(1, "querybuf: buffer is null.\n");
481                 goto done;
482         }
483
484         videobuf_status(q, b, q->bufs[b->index], q->type);
485
486         ret = 0;
487 done:
488         mutex_unlock(&q->vb_lock);
489         return ret;
490 }
491 EXPORT_SYMBOL_GPL(videobuf_querybuf);
492
493 int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
494 {
495         struct videobuf_buffer *buf;
496         enum v4l2_field field;
497         unsigned long flags = 0;
498         int retval;
499
500         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
501
502         if (b->memory == V4L2_MEMORY_MMAP)
503                 down_read(&current->mm->mmap_sem);
504
505         mutex_lock(&q->vb_lock);
506         retval = -EBUSY;
507         if (q->reading) {
508                 dprintk(1, "qbuf: Reading running...\n");
509                 goto done;
510         }
511         retval = -EINVAL;
512         if (b->type != q->type) {
513                 dprintk(1, "qbuf: Wrong type.\n");
514                 goto done;
515         }
516         if (b->index >= VIDEO_MAX_FRAME) {
517                 dprintk(1, "qbuf: index out of range.\n");
518                 goto done;
519         }
520         buf = q->bufs[b->index];
521         if (NULL == buf) {
522                 dprintk(1, "qbuf: buffer is null.\n");
523                 goto done;
524         }
525         MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
526         if (buf->memory != b->memory) {
527                 dprintk(1, "qbuf: memory type is wrong.\n");
528                 goto done;
529         }
530         if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
531                 dprintk(1, "qbuf: buffer is already queued or active.\n");
532                 goto done;
533         }
534
535         if (b->flags & V4L2_BUF_FLAG_INPUT) {
536                 if (b->input >= q->inputs) {
537                         dprintk(1, "qbuf: wrong input.\n");
538                         goto done;
539                 }
540                 buf->input = b->input;
541         } else {
542                 buf->input = UNSET;
543         }
544
545         switch (b->memory) {
546         case V4L2_MEMORY_MMAP:
547                 if (0 == buf->baddr) {
548                         dprintk(1, "qbuf: mmap requested "
549                                    "but buffer addr is zero!\n");
550                         goto done;
551                 }
552                 break;
553         case V4L2_MEMORY_USERPTR:
554                 if (b->length < buf->bsize) {
555                         dprintk(1, "qbuf: buffer length is not enough\n");
556                         goto done;
557                 }
558                 if (VIDEOBUF_NEEDS_INIT != buf->state &&
559                     buf->baddr != b->m.userptr)
560                         q->ops->buf_release(q, buf);
561                 buf->baddr = b->m.userptr;
562                 break;
563         case V4L2_MEMORY_OVERLAY:
564                 buf->boff = b->m.offset;
565                 break;
566         default:
567                 dprintk(1, "qbuf: wrong memory type\n");
568                 goto done;
569         }
570
571         dprintk(1, "qbuf: requesting next field\n");
572         field = videobuf_next_field(q);
573         retval = q->ops->buf_prepare(q, buf, field);
574         if (0 != retval) {
575                 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
576                 goto done;
577         }
578
579         list_add_tail(&buf->stream, &q->stream);
580         if (q->streaming) {
581                 spin_lock_irqsave(q->irqlock, flags);
582                 q->ops->buf_queue(q, buf);
583                 spin_unlock_irqrestore(q->irqlock, flags);
584         }
585         dprintk(1, "qbuf: succeeded\n");
586         retval = 0;
587         wake_up_interruptible_sync(&q->wait);
588
589 done:
590         mutex_unlock(&q->vb_lock);
591
592         if (b->memory == V4L2_MEMORY_MMAP)
593                 up_read(&current->mm->mmap_sem);
594
595         return retval;
596 }
597 EXPORT_SYMBOL_GPL(videobuf_qbuf);
598
599 /* Locking: Caller holds q->vb_lock */
600 static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
601 {
602         int retval;
603
604 checks:
605         if (!q->streaming) {
606                 dprintk(1, "next_buffer: Not streaming\n");
607                 retval = -EINVAL;
608                 goto done;
609         }
610
611         if (list_empty(&q->stream)) {
612                 if (noblock) {
613                         retval = -EAGAIN;
614                         dprintk(2, "next_buffer: no buffers to dequeue\n");
615                         goto done;
616                 } else {
617                         dprintk(2, "next_buffer: waiting on buffer\n");
618
619                         /* Drop lock to avoid deadlock with qbuf */
620                         mutex_unlock(&q->vb_lock);
621
622                         /* Checking list_empty and streaming is safe without
623                          * locks because we goto checks to validate while
624                          * holding locks before proceeding */
625                         retval = wait_event_interruptible(q->wait,
626                                 !list_empty(&q->stream) || !q->streaming);
627                         mutex_lock(&q->vb_lock);
628
629                         if (retval)
630                                 goto done;
631
632                         goto checks;
633                 }
634         }
635
636         retval = 0;
637
638 done:
639         return retval;
640 }
641
642 /* Locking: Caller holds q->vb_lock */
643 static int stream_next_buffer(struct videobuf_queue *q,
644                         struct videobuf_buffer **vb, int nonblocking)
645 {
646         int retval;
647         struct videobuf_buffer *buf = NULL;
648
649         retval = stream_next_buffer_check_queue(q, nonblocking);
650         if (retval)
651                 goto done;
652
653         buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
654         retval = videobuf_waiton(buf, nonblocking, 1);
655         if (retval < 0)
656                 goto done;
657
658         *vb = buf;
659 done:
660         return retval;
661 }
662
663 int videobuf_dqbuf(struct videobuf_queue *q,
664                    struct v4l2_buffer *b, int nonblocking)
665 {
666         struct videobuf_buffer *buf = NULL;
667         int retval;
668
669         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
670
671         mutex_lock(&q->vb_lock);
672
673         retval = stream_next_buffer(q, &buf, nonblocking);
674         if (retval < 0) {
675                 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
676                 goto done;
677         }
678
679         switch (buf->state) {
680         case VIDEOBUF_ERROR:
681                 dprintk(1, "dqbuf: state is error\n");
682                 retval = -EIO;
683                 CALL(q, sync, q, buf);
684                 buf->state = VIDEOBUF_IDLE;
685                 break;
686         case VIDEOBUF_DONE:
687                 dprintk(1, "dqbuf: state is done\n");
688                 CALL(q, sync, q, buf);
689                 buf->state = VIDEOBUF_IDLE;
690                 break;
691         default:
692                 dprintk(1, "dqbuf: state invalid\n");
693                 retval = -EINVAL;
694                 goto done;
695         }
696         list_del(&buf->stream);
697         memset(b, 0, sizeof(*b));
698         videobuf_status(q, b, buf, q->type);
699 done:
700         mutex_unlock(&q->vb_lock);
701         return retval;
702 }
703 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
704
705 int videobuf_streamon(struct videobuf_queue *q)
706 {
707         struct videobuf_buffer *buf;
708         unsigned long flags = 0;
709         int retval;
710
711         mutex_lock(&q->vb_lock);
712         retval = -EBUSY;
713         if (q->reading)
714                 goto done;
715         retval = 0;
716         if (q->streaming)
717                 goto done;
718         q->streaming = 1;
719         spin_lock_irqsave(q->irqlock, flags);
720         list_for_each_entry(buf, &q->stream, stream)
721                 if (buf->state == VIDEOBUF_PREPARED)
722                         q->ops->buf_queue(q, buf);
723         spin_unlock_irqrestore(q->irqlock, flags);
724
725         wake_up_interruptible_sync(&q->wait);
726 done:
727         mutex_unlock(&q->vb_lock);
728         return retval;
729 }
730 EXPORT_SYMBOL_GPL(videobuf_streamon);
731
732 /* Locking: Caller holds q->vb_lock */
733 static int __videobuf_streamoff(struct videobuf_queue *q)
734 {
735         if (!q->streaming)
736                 return -EINVAL;
737
738         videobuf_queue_cancel(q);
739
740         return 0;
741 }
742
743 int videobuf_streamoff(struct videobuf_queue *q)
744 {
745         int retval;
746
747         mutex_lock(&q->vb_lock);
748         retval = __videobuf_streamoff(q);
749         mutex_unlock(&q->vb_lock);
750
751         return retval;
752 }
753 EXPORT_SYMBOL_GPL(videobuf_streamoff);
754
755 /* Locking: Caller holds q->vb_lock */
756 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
757                                       char __user *data,
758                                       size_t count, loff_t *ppos)
759 {
760         enum v4l2_field field;
761         unsigned long flags = 0;
762         int retval;
763
764         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
765
766         /* setup stuff */
767         q->read_buf = videobuf_alloc(q);
768         if (NULL == q->read_buf)
769                 return -ENOMEM;
770
771         q->read_buf->memory = V4L2_MEMORY_USERPTR;
772         q->read_buf->baddr  = (unsigned long)data;
773         q->read_buf->bsize  = count;
774
775         field = videobuf_next_field(q);
776         retval = q->ops->buf_prepare(q, q->read_buf, field);
777         if (0 != retval)
778                 goto done;
779
780         /* start capture & wait */
781         spin_lock_irqsave(q->irqlock, flags);
782         q->ops->buf_queue(q, q->read_buf);
783         spin_unlock_irqrestore(q->irqlock, flags);
784         retval = videobuf_waiton(q->read_buf, 0, 0);
785         if (0 == retval) {
786                 CALL(q, sync, q, q->read_buf);
787                 if (VIDEOBUF_ERROR == q->read_buf->state)
788                         retval = -EIO;
789                 else
790                         retval = q->read_buf->size;
791         }
792
793 done:
794         /* cleanup */
795         q->ops->buf_release(q, q->read_buf);
796         kfree(q->read_buf);
797         q->read_buf = NULL;
798         return retval;
799 }
800
801 ssize_t videobuf_read_one(struct videobuf_queue *q,
802                           char __user *data, size_t count, loff_t *ppos,
803                           int nonblocking)
804 {
805         enum v4l2_field field;
806         unsigned long flags = 0;
807         unsigned size = 0, nbufs = 1;
808         int retval;
809
810         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
811
812         mutex_lock(&q->vb_lock);
813
814         q->ops->buf_setup(q, &nbufs, &size);
815
816         if (NULL == q->read_buf  &&
817             count >= size        &&
818             !nonblocking) {
819                 retval = videobuf_read_zerocopy(q, data, count, ppos);
820                 if (retval >= 0  ||  retval == -EIO)
821                         /* ok, all done */
822                         goto done;
823                 /* fallback to kernel bounce buffer on failures */
824         }
825
826         if (NULL == q->read_buf) {
827                 /* need to capture a new frame */
828                 retval = -ENOMEM;
829                 q->read_buf = videobuf_alloc(q);
830
831                 dprintk(1, "video alloc=0x%p\n", q->read_buf);
832                 if (NULL == q->read_buf)
833                         goto done;
834                 q->read_buf->memory = V4L2_MEMORY_USERPTR;
835                 q->read_buf->bsize = count; /* preferred size */
836                 field = videobuf_next_field(q);
837                 retval = q->ops->buf_prepare(q, q->read_buf, field);
838
839                 if (0 != retval) {
840                         kfree(q->read_buf);
841                         q->read_buf = NULL;
842                         goto done;
843                 }
844
845                 spin_lock_irqsave(q->irqlock, flags);
846                 q->ops->buf_queue(q, q->read_buf);
847                 spin_unlock_irqrestore(q->irqlock, flags);
848
849                 q->read_off = 0;
850         }
851
852         /* wait until capture is done */
853         retval = videobuf_waiton(q->read_buf, nonblocking, 1);
854         if (0 != retval)
855                 goto done;
856
857         CALL(q, sync, q, q->read_buf);
858
859         if (VIDEOBUF_ERROR == q->read_buf->state) {
860                 /* catch I/O errors */
861                 q->ops->buf_release(q, q->read_buf);
862                 kfree(q->read_buf);
863                 q->read_buf = NULL;
864                 retval = -EIO;
865                 goto done;
866         }
867
868         /* Copy to userspace */
869         retval = CALL(q, video_copy_to_user, q, data, count, nonblocking);
870         if (retval < 0)
871                 goto done;
872
873         q->read_off += retval;
874         if (q->read_off == q->read_buf->size) {
875                 /* all data copied, cleanup */
876                 q->ops->buf_release(q, q->read_buf);
877                 kfree(q->read_buf);
878                 q->read_buf = NULL;
879         }
880
881 done:
882         mutex_unlock(&q->vb_lock);
883         return retval;
884 }
885 EXPORT_SYMBOL_GPL(videobuf_read_one);
886
887 /* Locking: Caller holds q->vb_lock */
888 static int __videobuf_read_start(struct videobuf_queue *q)
889 {
890         enum v4l2_field field;
891         unsigned long flags = 0;
892         unsigned int count = 0, size = 0;
893         int err, i;
894
895         q->ops->buf_setup(q, &count, &size);
896         if (count < 2)
897                 count = 2;
898         if (count > VIDEO_MAX_FRAME)
899                 count = VIDEO_MAX_FRAME;
900         size = PAGE_ALIGN(size);
901
902         err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
903         if (err < 0)
904                 return err;
905
906         count = err;
907
908         for (i = 0; i < count; i++) {
909                 field = videobuf_next_field(q);
910                 err = q->ops->buf_prepare(q, q->bufs[i], field);
911                 if (err)
912                         return err;
913                 list_add_tail(&q->bufs[i]->stream, &q->stream);
914         }
915         spin_lock_irqsave(q->irqlock, flags);
916         for (i = 0; i < count; i++)
917                 q->ops->buf_queue(q, q->bufs[i]);
918         spin_unlock_irqrestore(q->irqlock, flags);
919         q->reading = 1;
920         return 0;
921 }
922
923 static void __videobuf_read_stop(struct videobuf_queue *q)
924 {
925         int i;
926
927         videobuf_queue_cancel(q);
928         __videobuf_mmap_free(q);
929         INIT_LIST_HEAD(&q->stream);
930         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
931                 if (NULL == q->bufs[i])
932                         continue;
933                 kfree(q->bufs[i]);
934                 q->bufs[i] = NULL;
935         }
936         q->read_buf = NULL;
937 }
938
939 int videobuf_read_start(struct videobuf_queue *q)
940 {
941         int rc;
942
943         mutex_lock(&q->vb_lock);
944         rc = __videobuf_read_start(q);
945         mutex_unlock(&q->vb_lock);
946
947         return rc;
948 }
949 EXPORT_SYMBOL_GPL(videobuf_read_start);
950
951 void videobuf_read_stop(struct videobuf_queue *q)
952 {
953         mutex_lock(&q->vb_lock);
954         __videobuf_read_stop(q);
955         mutex_unlock(&q->vb_lock);
956 }
957 EXPORT_SYMBOL_GPL(videobuf_read_stop);
958
959 void videobuf_stop(struct videobuf_queue *q)
960 {
961         mutex_lock(&q->vb_lock);
962
963         if (q->streaming)
964                 __videobuf_streamoff(q);
965
966         if (q->reading)
967                 __videobuf_read_stop(q);
968
969         mutex_unlock(&q->vb_lock);
970 }
971 EXPORT_SYMBOL_GPL(videobuf_stop);
972
973 ssize_t videobuf_read_stream(struct videobuf_queue *q,
974                              char __user *data, size_t count, loff_t *ppos,
975                              int vbihack, int nonblocking)
976 {
977         int rc, retval;
978         unsigned long flags = 0;
979
980         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
981
982         dprintk(2, "%s\n", __func__);
983         mutex_lock(&q->vb_lock);
984         retval = -EBUSY;
985         if (q->streaming)
986                 goto done;
987         if (!q->reading) {
988                 retval = __videobuf_read_start(q);
989                 if (retval < 0)
990                         goto done;
991         }
992
993         retval = 0;
994         while (count > 0) {
995                 /* get / wait for data */
996                 if (NULL == q->read_buf) {
997                         q->read_buf = list_entry(q->stream.next,
998                                                  struct videobuf_buffer,
999                                                  stream);
1000                         list_del(&q->read_buf->stream);
1001                         q->read_off = 0;
1002                 }
1003                 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
1004                 if (rc < 0) {
1005                         if (0 == retval)
1006                                 retval = rc;
1007                         break;
1008                 }
1009
1010                 if (q->read_buf->state == VIDEOBUF_DONE) {
1011                         rc = CALL(q, copy_stream, q, data + retval, count,
1012                                         retval, vbihack, nonblocking);
1013                         if (rc < 0) {
1014                                 retval = rc;
1015                                 break;
1016                         }
1017                         retval      += rc;
1018                         count       -= rc;
1019                         q->read_off += rc;
1020                 } else {
1021                         /* some error */
1022                         q->read_off = q->read_buf->size;
1023                         if (0 == retval)
1024                                 retval = -EIO;
1025                 }
1026
1027                 /* requeue buffer when done with copying */
1028                 if (q->read_off == q->read_buf->size) {
1029                         list_add_tail(&q->read_buf->stream,
1030                                       &q->stream);
1031                         spin_lock_irqsave(q->irqlock, flags);
1032                         q->ops->buf_queue(q, q->read_buf);
1033                         spin_unlock_irqrestore(q->irqlock, flags);
1034                         q->read_buf = NULL;
1035                 }
1036                 if (retval < 0)
1037                         break;
1038         }
1039
1040 done:
1041         mutex_unlock(&q->vb_lock);
1042         return retval;
1043 }
1044 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1045
1046 unsigned int videobuf_poll_stream(struct file *file,
1047                                   struct videobuf_queue *q,
1048                                   poll_table *wait)
1049 {
1050         struct videobuf_buffer *buf = NULL;
1051         unsigned int rc = 0;
1052
1053         mutex_lock(&q->vb_lock);
1054         if (q->streaming) {
1055                 if (!list_empty(&q->stream))
1056                         buf = list_entry(q->stream.next,
1057                                          struct videobuf_buffer, stream);
1058         } else {
1059                 if (!q->reading)
1060                         __videobuf_read_start(q);
1061                 if (!q->reading) {
1062                         rc = POLLERR;
1063                 } else if (NULL == q->read_buf) {
1064                         q->read_buf = list_entry(q->stream.next,
1065                                                  struct videobuf_buffer,
1066                                                  stream);
1067                         list_del(&q->read_buf->stream);
1068                         q->read_off = 0;
1069                 }
1070                 buf = q->read_buf;
1071         }
1072         if (!buf)
1073                 rc = POLLERR;
1074
1075         if (0 == rc) {
1076                 poll_wait(file, &buf->done, wait);
1077                 if (buf->state == VIDEOBUF_DONE ||
1078                     buf->state == VIDEOBUF_ERROR)
1079                         rc = POLLIN|POLLRDNORM;
1080         }
1081         mutex_unlock(&q->vb_lock);
1082         return rc;
1083 }
1084 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1085
1086 int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
1087 {
1088         int retval;
1089
1090         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1091
1092         mutex_lock(&q->vb_lock);
1093         retval = CALL(q, mmap_mapper, q, vma);
1094         q->is_mmapped = 1;
1095         mutex_unlock(&q->vb_lock);
1096
1097         return retval;
1098 }
1099 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1100
1101 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1102 int videobuf_cgmbuf(struct videobuf_queue *q,
1103                     struct video_mbuf *mbuf, int count)
1104 {
1105         struct v4l2_requestbuffers req;
1106         int rc, i;
1107
1108         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1109
1110         memset(&req, 0, sizeof(req));
1111         req.type   = q->type;
1112         req.count  = count;
1113         req.memory = V4L2_MEMORY_MMAP;
1114         rc = videobuf_reqbufs(q, &req);
1115         if (rc < 0)
1116                 return rc;
1117
1118         mbuf->frames = req.count;
1119         mbuf->size   = 0;
1120         for (i = 0; i < mbuf->frames; i++) {
1121                 mbuf->offsets[i]  = q->bufs[i]->boff;
1122                 mbuf->size       += PAGE_ALIGN(q->bufs[i]->bsize);
1123         }
1124
1125         return 0;
1126 }
1127 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
1128 #endif
1129