2 * generic helper functions for handling video4linux capture buffers
4 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
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
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
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
24 #include <media/videobuf-core.h>
26 #define MAGIC_BUFFER 0x20070728
27 #define MAGIC_CHECK(is, should) \
29 if (unlikely((is) != (should))) { \
31 "magic mismatch: %x (expected %x)\n", \
38 module_param(debug, int, 0644);
40 MODULE_DESCRIPTION("helper module to manage video4linux buffers");
41 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
42 MODULE_LICENSE("GPL");
44 #define dprintk(level, fmt, arg...) \
47 printk(KERN_DEBUG "vbuf: " fmt, ## arg); \
50 /* --------------------------------------------------------------------- */
52 #define CALL(q, f, arg...) \
53 ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
55 void *videobuf_alloc(struct videobuf_queue *q)
57 struct videobuf_buffer *vb;
59 BUG_ON(q->msize < sizeof(*vb));
61 if (!q->int_ops || !q->int_ops->alloc) {
62 printk(KERN_ERR "No specific ops defined!\n");
66 vb = q->int_ops->alloc(q->msize);
68 init_waitqueue_head(&vb->done);
69 vb->magic = MAGIC_BUFFER;
74 EXPORT_SYMBOL_GPL(videobuf_alloc);
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)
80 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
90 return wait_event_interruptible(vb->done, WAITON_CONDITION);
92 wait_event(vb->done, WAITON_CONDITION);
96 EXPORT_SYMBOL_GPL(videobuf_waiton);
98 int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
99 struct v4l2_framebuffer *fbuf)
101 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
102 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
104 return CALL(q, iolock, q, vb, fbuf);
106 EXPORT_SYMBOL_GPL(videobuf_iolock);
108 void *videobuf_queue_to_vmalloc(struct videobuf_queue *q,
109 struct videobuf_buffer *buf)
111 if (q->int_ops->vmalloc)
112 return q->int_ops->vmalloc(buf);
116 EXPORT_SYMBOL_GPL(videobuf_queue_to_vmalloc);
118 /* --------------------------------------------------------------------- */
121 void videobuf_queue_core_init(struct videobuf_queue *q,
122 const struct videobuf_queue_ops *ops,
125 enum v4l2_buf_type type,
126 enum v4l2_field field,
129 struct videobuf_qtype_ops *int_ops)
132 memset(q, 0, sizeof(*q));
133 q->irqlock = irqlock;
140 q->int_ops = int_ops;
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);
148 /* Lock is mandatory for queue_cancel to work */
151 /* Having implementations for abstract methods are mandatory */
154 mutex_init(&q->vb_lock);
155 init_waitqueue_head(&q->wait);
156 INIT_LIST_HEAD(&q->stream);
158 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
160 /* Locking: Only usage in bttv unsafe find way to remove */
161 int videobuf_queue_is_busy(struct videobuf_queue *q)
165 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
168 dprintk(1, "busy: streaming active\n");
172 dprintk(1, "busy: pending read #1\n");
176 dprintk(1, "busy: pending read #2\n");
179 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
180 if (NULL == q->bufs[i])
182 if (q->bufs[i]->map) {
183 dprintk(1, "busy: buffer #%d mapped\n", i);
186 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
187 dprintk(1, "busy: buffer #%d queued\n", i);
190 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
191 dprintk(1, "busy: buffer #%d avtive\n", i);
197 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
199 /* Locking: Caller holds q->vb_lock */
200 void videobuf_queue_cancel(struct videobuf_queue *q)
202 unsigned long flags = 0;
207 wake_up_interruptible_sync(&q->wait);
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])
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);
220 spin_unlock_irqrestore(q->irqlock, flags);
222 /* free all buffers + clear queue */
223 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
224 if (NULL == q->bufs[i])
226 q->ops->buf_release(q, q->bufs[i]);
228 INIT_LIST_HEAD(&q->stream);
230 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
232 /* --------------------------------------------------------------------- */
234 /* Locking: Caller holds q->vb_lock */
235 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
237 enum v4l2_field field = q->field;
239 BUG_ON(V4L2_FIELD_ANY == field);
241 if (V4L2_FIELD_ALTERNATE == field) {
242 if (V4L2_FIELD_TOP == q->last) {
243 field = V4L2_FIELD_BOTTOM;
244 q->last = V4L2_FIELD_BOTTOM;
246 field = V4L2_FIELD_TOP;
247 q->last = V4L2_FIELD_TOP;
252 EXPORT_SYMBOL_GPL(videobuf_next_field);
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)
258 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
259 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
264 b->memory = vb->memory;
266 case V4L2_MEMORY_MMAP:
267 b->m.offset = vb->boff;
268 b->length = vb->bsize;
270 case V4L2_MEMORY_USERPTR:
271 b->m.userptr = vb->baddr;
272 b->length = vb->bsize;
274 case V4L2_MEMORY_OVERLAY:
275 b->m.offset = vb->boff;
281 b->flags |= V4L2_BUF_FLAG_MAPPED;
284 case VIDEOBUF_PREPARED:
285 case VIDEOBUF_QUEUED:
286 case VIDEOBUF_ACTIVE:
287 b->flags |= V4L2_BUF_FLAG_QUEUED;
291 b->flags |= V4L2_BUF_FLAG_DONE;
293 case VIDEOBUF_NEEDS_INIT:
299 if (vb->input != UNSET) {
300 b->flags |= V4L2_BUF_FLAG_INPUT;
301 b->input = vb->input;
304 b->field = vb->field;
305 b->timestamp = vb->ts;
306 b->bytesused = vb->size;
307 b->sequence = vb->field_count >> 1;
310 /* Locking: Caller holds q->vb_lock */
311 static int __videobuf_mmap_free(struct videobuf_queue *q)
319 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
321 rc = CALL(q, mmap_free, q);
328 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
329 if (NULL == q->bufs[i])
331 q->ops->buf_release(q, q->bufs[i]);
339 int videobuf_mmap_free(struct videobuf_queue *q)
342 mutex_lock(&q->vb_lock);
343 ret = __videobuf_mmap_free(q);
344 mutex_unlock(&q->vb_lock);
347 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
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)
357 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
359 err = __videobuf_mmap_free(q);
363 /* Allocate and initialize buffers */
364 for (i = 0; i < bcount; i++) {
365 q->bufs[i] = videobuf_alloc(q);
367 if (NULL == q->bufs[i])
371 q->bufs[i]->input = UNSET;
372 q->bufs[i]->memory = memory;
373 q->bufs[i]->bsize = bsize;
375 case V4L2_MEMORY_MMAP:
376 q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
378 case V4L2_MEMORY_USERPTR:
379 case V4L2_MEMORY_OVERLAY:
388 dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
392 EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
394 int videobuf_mmap_setup(struct videobuf_queue *q,
395 unsigned int bcount, unsigned int bsize,
396 enum v4l2_memory memory)
399 mutex_lock(&q->vb_lock);
400 ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
401 mutex_unlock(&q->vb_lock);
404 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
406 int videobuf_reqbufs(struct videobuf_queue *q,
407 struct v4l2_requestbuffers *req)
409 unsigned int size, count;
412 if (req->count < 1) {
413 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
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");
424 mutex_lock(&q->vb_lock);
425 if (req->type != q->type) {
426 dprintk(1, "reqbufs: queue type invalid\n");
432 dprintk(1, "reqbufs: streaming already exists\n");
436 if (!list_empty(&q->stream)) {
437 dprintk(1, "reqbufs: stream running\n");
443 if (count > VIDEO_MAX_FRAME)
444 count = VIDEO_MAX_FRAME;
446 q->ops->buf_setup(q, &count, &size);
447 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
449 (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
451 retval = __videobuf_mmap_setup(q, count, size, req->memory);
453 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
461 mutex_unlock(&q->vb_lock);
464 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
466 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
470 mutex_lock(&q->vb_lock);
471 if (unlikely(b->type != q->type)) {
472 dprintk(1, "querybuf: Wrong type.\n");
475 if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
476 dprintk(1, "querybuf: index out of range.\n");
479 if (unlikely(NULL == q->bufs[b->index])) {
480 dprintk(1, "querybuf: buffer is null.\n");
484 videobuf_status(q, b, q->bufs[b->index], q->type);
488 mutex_unlock(&q->vb_lock);
491 EXPORT_SYMBOL_GPL(videobuf_querybuf);
493 int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
495 struct videobuf_buffer *buf;
496 enum v4l2_field field;
497 unsigned long flags = 0;
500 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
502 if (b->memory == V4L2_MEMORY_MMAP)
503 down_read(¤t->mm->mmap_sem);
505 mutex_lock(&q->vb_lock);
508 dprintk(1, "qbuf: Reading running...\n");
512 if (b->type != q->type) {
513 dprintk(1, "qbuf: Wrong type.\n");
516 if (b->index >= VIDEO_MAX_FRAME) {
517 dprintk(1, "qbuf: index out of range.\n");
520 buf = q->bufs[b->index];
522 dprintk(1, "qbuf: buffer is null.\n");
525 MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
526 if (buf->memory != b->memory) {
527 dprintk(1, "qbuf: memory type is wrong.\n");
530 if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
531 dprintk(1, "qbuf: buffer is already queued or active.\n");
535 if (b->flags & V4L2_BUF_FLAG_INPUT) {
536 if (b->input >= q->inputs) {
537 dprintk(1, "qbuf: wrong input.\n");
540 buf->input = b->input;
546 case V4L2_MEMORY_MMAP:
547 if (0 == buf->baddr) {
548 dprintk(1, "qbuf: mmap requested "
549 "but buffer addr is zero!\n");
553 case V4L2_MEMORY_USERPTR:
554 if (b->length < buf->bsize) {
555 dprintk(1, "qbuf: buffer length is not enough\n");
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;
563 case V4L2_MEMORY_OVERLAY:
564 buf->boff = b->m.offset;
567 dprintk(1, "qbuf: wrong memory type\n");
571 dprintk(1, "qbuf: requesting next field\n");
572 field = videobuf_next_field(q);
573 retval = q->ops->buf_prepare(q, buf, field);
575 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
579 list_add_tail(&buf->stream, &q->stream);
581 spin_lock_irqsave(q->irqlock, flags);
582 q->ops->buf_queue(q, buf);
583 spin_unlock_irqrestore(q->irqlock, flags);
585 dprintk(1, "qbuf: succeeded\n");
587 wake_up_interruptible_sync(&q->wait);
590 mutex_unlock(&q->vb_lock);
592 if (b->memory == V4L2_MEMORY_MMAP)
593 up_read(¤t->mm->mmap_sem);
597 EXPORT_SYMBOL_GPL(videobuf_qbuf);
599 /* Locking: Caller holds q->vb_lock */
600 static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
606 dprintk(1, "next_buffer: Not streaming\n");
611 if (list_empty(&q->stream)) {
614 dprintk(2, "next_buffer: no buffers to dequeue\n");
617 dprintk(2, "next_buffer: waiting on buffer\n");
619 /* Drop lock to avoid deadlock with qbuf */
620 mutex_unlock(&q->vb_lock);
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);
642 /* Locking: Caller holds q->vb_lock */
643 static int stream_next_buffer(struct videobuf_queue *q,
644 struct videobuf_buffer **vb, int nonblocking)
647 struct videobuf_buffer *buf = NULL;
649 retval = stream_next_buffer_check_queue(q, nonblocking);
653 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
654 retval = videobuf_waiton(buf, nonblocking, 1);
663 int videobuf_dqbuf(struct videobuf_queue *q,
664 struct v4l2_buffer *b, int nonblocking)
666 struct videobuf_buffer *buf = NULL;
669 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
671 mutex_lock(&q->vb_lock);
673 retval = stream_next_buffer(q, &buf, nonblocking);
675 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
679 switch (buf->state) {
681 dprintk(1, "dqbuf: state is error\n");
683 CALL(q, sync, q, buf);
684 buf->state = VIDEOBUF_IDLE;
687 dprintk(1, "dqbuf: state is done\n");
688 CALL(q, sync, q, buf);
689 buf->state = VIDEOBUF_IDLE;
692 dprintk(1, "dqbuf: state invalid\n");
696 list_del(&buf->stream);
697 memset(b, 0, sizeof(*b));
698 videobuf_status(q, b, buf, q->type);
700 mutex_unlock(&q->vb_lock);
703 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
705 int videobuf_streamon(struct videobuf_queue *q)
707 struct videobuf_buffer *buf;
708 unsigned long flags = 0;
711 mutex_lock(&q->vb_lock);
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);
725 wake_up_interruptible_sync(&q->wait);
727 mutex_unlock(&q->vb_lock);
730 EXPORT_SYMBOL_GPL(videobuf_streamon);
732 /* Locking: Caller holds q->vb_lock */
733 static int __videobuf_streamoff(struct videobuf_queue *q)
738 videobuf_queue_cancel(q);
743 int videobuf_streamoff(struct videobuf_queue *q)
747 mutex_lock(&q->vb_lock);
748 retval = __videobuf_streamoff(q);
749 mutex_unlock(&q->vb_lock);
753 EXPORT_SYMBOL_GPL(videobuf_streamoff);
755 /* Locking: Caller holds q->vb_lock */
756 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
758 size_t count, loff_t *ppos)
760 enum v4l2_field field;
761 unsigned long flags = 0;
764 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
767 q->read_buf = videobuf_alloc(q);
768 if (NULL == q->read_buf)
771 q->read_buf->memory = V4L2_MEMORY_USERPTR;
772 q->read_buf->baddr = (unsigned long)data;
773 q->read_buf->bsize = count;
775 field = videobuf_next_field(q);
776 retval = q->ops->buf_prepare(q, q->read_buf, field);
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);
786 CALL(q, sync, q, q->read_buf);
787 if (VIDEOBUF_ERROR == q->read_buf->state)
790 retval = q->read_buf->size;
795 q->ops->buf_release(q, q->read_buf);
801 ssize_t videobuf_read_one(struct videobuf_queue *q,
802 char __user *data, size_t count, loff_t *ppos,
805 enum v4l2_field field;
806 unsigned long flags = 0;
807 unsigned size = 0, nbufs = 1;
810 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
812 mutex_lock(&q->vb_lock);
814 q->ops->buf_setup(q, &nbufs, &size);
816 if (NULL == q->read_buf &&
819 retval = videobuf_read_zerocopy(q, data, count, ppos);
820 if (retval >= 0 || retval == -EIO)
823 /* fallback to kernel bounce buffer on failures */
826 if (NULL == q->read_buf) {
827 /* need to capture a new frame */
829 q->read_buf = videobuf_alloc(q);
831 dprintk(1, "video alloc=0x%p\n", q->read_buf);
832 if (NULL == q->read_buf)
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);
845 spin_lock_irqsave(q->irqlock, flags);
846 q->ops->buf_queue(q, q->read_buf);
847 spin_unlock_irqrestore(q->irqlock, flags);
852 /* wait until capture is done */
853 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
857 CALL(q, sync, q, q->read_buf);
859 if (VIDEOBUF_ERROR == q->read_buf->state) {
860 /* catch I/O errors */
861 q->ops->buf_release(q, q->read_buf);
868 /* Copy to userspace */
869 retval = CALL(q, video_copy_to_user, q, data, count, nonblocking);
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);
882 mutex_unlock(&q->vb_lock);
885 EXPORT_SYMBOL_GPL(videobuf_read_one);
887 /* Locking: Caller holds q->vb_lock */
888 static int __videobuf_read_start(struct videobuf_queue *q)
890 enum v4l2_field field;
891 unsigned long flags = 0;
892 unsigned int count = 0, size = 0;
895 q->ops->buf_setup(q, &count, &size);
898 if (count > VIDEO_MAX_FRAME)
899 count = VIDEO_MAX_FRAME;
900 size = PAGE_ALIGN(size);
902 err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
908 for (i = 0; i < count; i++) {
909 field = videobuf_next_field(q);
910 err = q->ops->buf_prepare(q, q->bufs[i], field);
913 list_add_tail(&q->bufs[i]->stream, &q->stream);
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);
923 static void __videobuf_read_stop(struct videobuf_queue *q)
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])
939 int videobuf_read_start(struct videobuf_queue *q)
943 mutex_lock(&q->vb_lock);
944 rc = __videobuf_read_start(q);
945 mutex_unlock(&q->vb_lock);
949 EXPORT_SYMBOL_GPL(videobuf_read_start);
951 void videobuf_read_stop(struct videobuf_queue *q)
953 mutex_lock(&q->vb_lock);
954 __videobuf_read_stop(q);
955 mutex_unlock(&q->vb_lock);
957 EXPORT_SYMBOL_GPL(videobuf_read_stop);
959 void videobuf_stop(struct videobuf_queue *q)
961 mutex_lock(&q->vb_lock);
964 __videobuf_streamoff(q);
967 __videobuf_read_stop(q);
969 mutex_unlock(&q->vb_lock);
971 EXPORT_SYMBOL_GPL(videobuf_stop);
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)
978 unsigned long flags = 0;
980 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
982 dprintk(2, "%s\n", __func__);
983 mutex_lock(&q->vb_lock);
988 retval = __videobuf_read_start(q);
995 /* get / wait for data */
996 if (NULL == q->read_buf) {
997 q->read_buf = list_entry(q->stream.next,
998 struct videobuf_buffer,
1000 list_del(&q->read_buf->stream);
1003 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
1010 if (q->read_buf->state == VIDEOBUF_DONE) {
1011 rc = CALL(q, copy_stream, q, data + retval, count,
1012 retval, vbihack, nonblocking);
1022 q->read_off = q->read_buf->size;
1027 /* requeue buffer when done with copying */
1028 if (q->read_off == q->read_buf->size) {
1029 list_add_tail(&q->read_buf->stream,
1031 spin_lock_irqsave(q->irqlock, flags);
1032 q->ops->buf_queue(q, q->read_buf);
1033 spin_unlock_irqrestore(q->irqlock, flags);
1041 mutex_unlock(&q->vb_lock);
1044 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1046 unsigned int videobuf_poll_stream(struct file *file,
1047 struct videobuf_queue *q,
1050 struct videobuf_buffer *buf = NULL;
1051 unsigned int rc = 0;
1053 mutex_lock(&q->vb_lock);
1055 if (!list_empty(&q->stream))
1056 buf = list_entry(q->stream.next,
1057 struct videobuf_buffer, stream);
1060 __videobuf_read_start(q);
1063 } else if (NULL == q->read_buf) {
1064 q->read_buf = list_entry(q->stream.next,
1065 struct videobuf_buffer,
1067 list_del(&q->read_buf->stream);
1076 poll_wait(file, &buf->done, wait);
1077 if (buf->state == VIDEOBUF_DONE ||
1078 buf->state == VIDEOBUF_ERROR)
1079 rc = POLLIN|POLLRDNORM;
1081 mutex_unlock(&q->vb_lock);
1084 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1086 int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
1090 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1092 mutex_lock(&q->vb_lock);
1093 retval = CALL(q, mmap_mapper, q, vma);
1095 mutex_unlock(&q->vb_lock);
1099 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1101 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1102 int videobuf_cgmbuf(struct videobuf_queue *q,
1103 struct video_mbuf *mbuf, int count)
1105 struct v4l2_requestbuffers req;
1108 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1110 memset(&req, 0, sizeof(req));
1113 req.memory = V4L2_MEMORY_MMAP;
1114 rc = videobuf_reqbufs(q, &req);
1118 mbuf->frames = req.count;
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);
1127 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);