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)
318 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
320 for (i = 0; i < VIDEO_MAX_FRAME; i++)
321 if (q->bufs[i] && q->bufs[i]->map)
324 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
325 if (NULL == q->bufs[i])
327 q->ops->buf_release(q, q->bufs[i]);
335 int videobuf_mmap_free(struct videobuf_queue *q)
338 mutex_lock(&q->vb_lock);
339 ret = __videobuf_mmap_free(q);
340 mutex_unlock(&q->vb_lock);
343 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
345 /* Locking: Caller holds q->vb_lock */
346 int __videobuf_mmap_setup(struct videobuf_queue *q,
347 unsigned int bcount, unsigned int bsize,
348 enum v4l2_memory memory)
353 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
355 err = __videobuf_mmap_free(q);
359 /* Allocate and initialize buffers */
360 for (i = 0; i < bcount; i++) {
361 q->bufs[i] = videobuf_alloc(q);
363 if (NULL == q->bufs[i])
367 q->bufs[i]->input = UNSET;
368 q->bufs[i]->memory = memory;
369 q->bufs[i]->bsize = bsize;
371 case V4L2_MEMORY_MMAP:
372 q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
374 case V4L2_MEMORY_USERPTR:
375 case V4L2_MEMORY_OVERLAY:
384 dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
388 EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
390 int videobuf_mmap_setup(struct videobuf_queue *q,
391 unsigned int bcount, unsigned int bsize,
392 enum v4l2_memory memory)
395 mutex_lock(&q->vb_lock);
396 ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
397 mutex_unlock(&q->vb_lock);
400 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
402 int videobuf_reqbufs(struct videobuf_queue *q,
403 struct v4l2_requestbuffers *req)
405 unsigned int size, count;
408 if (req->count < 1) {
409 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
413 if (req->memory != V4L2_MEMORY_MMAP &&
414 req->memory != V4L2_MEMORY_USERPTR &&
415 req->memory != V4L2_MEMORY_OVERLAY) {
416 dprintk(1, "reqbufs: memory type invalid\n");
420 mutex_lock(&q->vb_lock);
421 if (req->type != q->type) {
422 dprintk(1, "reqbufs: queue type invalid\n");
428 dprintk(1, "reqbufs: streaming already exists\n");
432 if (!list_empty(&q->stream)) {
433 dprintk(1, "reqbufs: stream running\n");
439 if (count > VIDEO_MAX_FRAME)
440 count = VIDEO_MAX_FRAME;
442 q->ops->buf_setup(q, &count, &size);
443 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
445 (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
447 retval = __videobuf_mmap_setup(q, count, size, req->memory);
449 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
457 mutex_unlock(&q->vb_lock);
460 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
462 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
466 mutex_lock(&q->vb_lock);
467 if (unlikely(b->type != q->type)) {
468 dprintk(1, "querybuf: Wrong type.\n");
471 if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
472 dprintk(1, "querybuf: index out of range.\n");
475 if (unlikely(NULL == q->bufs[b->index])) {
476 dprintk(1, "querybuf: buffer is null.\n");
480 videobuf_status(q, b, q->bufs[b->index], q->type);
484 mutex_unlock(&q->vb_lock);
487 EXPORT_SYMBOL_GPL(videobuf_querybuf);
489 int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
491 struct videobuf_buffer *buf;
492 enum v4l2_field field;
493 unsigned long flags = 0;
496 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
498 if (b->memory == V4L2_MEMORY_MMAP)
499 down_read(¤t->mm->mmap_sem);
501 mutex_lock(&q->vb_lock);
504 dprintk(1, "qbuf: Reading running...\n");
508 if (b->type != q->type) {
509 dprintk(1, "qbuf: Wrong type.\n");
512 if (b->index >= VIDEO_MAX_FRAME) {
513 dprintk(1, "qbuf: index out of range.\n");
516 buf = q->bufs[b->index];
518 dprintk(1, "qbuf: buffer is null.\n");
521 MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
522 if (buf->memory != b->memory) {
523 dprintk(1, "qbuf: memory type is wrong.\n");
526 if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
527 dprintk(1, "qbuf: buffer is already queued or active.\n");
531 if (b->flags & V4L2_BUF_FLAG_INPUT) {
532 if (b->input >= q->inputs) {
533 dprintk(1, "qbuf: wrong input.\n");
536 buf->input = b->input;
542 case V4L2_MEMORY_MMAP:
543 if (0 == buf->baddr) {
544 dprintk(1, "qbuf: mmap requested "
545 "but buffer addr is zero!\n");
549 case V4L2_MEMORY_USERPTR:
550 if (b->length < buf->bsize) {
551 dprintk(1, "qbuf: buffer length is not enough\n");
554 if (VIDEOBUF_NEEDS_INIT != buf->state &&
555 buf->baddr != b->m.userptr)
556 q->ops->buf_release(q, buf);
557 buf->baddr = b->m.userptr;
559 case V4L2_MEMORY_OVERLAY:
560 buf->boff = b->m.offset;
563 dprintk(1, "qbuf: wrong memory type\n");
567 dprintk(1, "qbuf: requesting next field\n");
568 field = videobuf_next_field(q);
569 retval = q->ops->buf_prepare(q, buf, field);
571 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
575 list_add_tail(&buf->stream, &q->stream);
577 spin_lock_irqsave(q->irqlock, flags);
578 q->ops->buf_queue(q, buf);
579 spin_unlock_irqrestore(q->irqlock, flags);
581 dprintk(1, "qbuf: succeeded\n");
583 wake_up_interruptible_sync(&q->wait);
586 mutex_unlock(&q->vb_lock);
588 if (b->memory == V4L2_MEMORY_MMAP)
589 up_read(¤t->mm->mmap_sem);
593 EXPORT_SYMBOL_GPL(videobuf_qbuf);
595 /* Locking: Caller holds q->vb_lock */
596 static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
602 dprintk(1, "next_buffer: Not streaming\n");
607 if (list_empty(&q->stream)) {
610 dprintk(2, "next_buffer: no buffers to dequeue\n");
613 dprintk(2, "next_buffer: waiting on buffer\n");
615 /* Drop lock to avoid deadlock with qbuf */
616 mutex_unlock(&q->vb_lock);
618 /* Checking list_empty and streaming is safe without
619 * locks because we goto checks to validate while
620 * holding locks before proceeding */
621 retval = wait_event_interruptible(q->wait,
622 !list_empty(&q->stream) || !q->streaming);
623 mutex_lock(&q->vb_lock);
638 /* Locking: Caller holds q->vb_lock */
639 static int stream_next_buffer(struct videobuf_queue *q,
640 struct videobuf_buffer **vb, int nonblocking)
643 struct videobuf_buffer *buf = NULL;
645 retval = stream_next_buffer_check_queue(q, nonblocking);
649 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
650 retval = videobuf_waiton(buf, nonblocking, 1);
659 int videobuf_dqbuf(struct videobuf_queue *q,
660 struct v4l2_buffer *b, int nonblocking)
662 struct videobuf_buffer *buf = NULL;
665 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
667 mutex_lock(&q->vb_lock);
669 retval = stream_next_buffer(q, &buf, nonblocking);
671 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
675 switch (buf->state) {
677 dprintk(1, "dqbuf: state is error\n");
679 CALL(q, sync, q, buf);
680 buf->state = VIDEOBUF_IDLE;
683 dprintk(1, "dqbuf: state is done\n");
684 CALL(q, sync, q, buf);
685 buf->state = VIDEOBUF_IDLE;
688 dprintk(1, "dqbuf: state invalid\n");
692 list_del(&buf->stream);
693 memset(b, 0, sizeof(*b));
694 videobuf_status(q, b, buf, q->type);
696 mutex_unlock(&q->vb_lock);
699 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
701 int videobuf_streamon(struct videobuf_queue *q)
703 struct videobuf_buffer *buf;
704 unsigned long flags = 0;
707 mutex_lock(&q->vb_lock);
715 spin_lock_irqsave(q->irqlock, flags);
716 list_for_each_entry(buf, &q->stream, stream)
717 if (buf->state == VIDEOBUF_PREPARED)
718 q->ops->buf_queue(q, buf);
719 spin_unlock_irqrestore(q->irqlock, flags);
721 wake_up_interruptible_sync(&q->wait);
723 mutex_unlock(&q->vb_lock);
726 EXPORT_SYMBOL_GPL(videobuf_streamon);
728 /* Locking: Caller holds q->vb_lock */
729 static int __videobuf_streamoff(struct videobuf_queue *q)
734 videobuf_queue_cancel(q);
739 int videobuf_streamoff(struct videobuf_queue *q)
743 mutex_lock(&q->vb_lock);
744 retval = __videobuf_streamoff(q);
745 mutex_unlock(&q->vb_lock);
749 EXPORT_SYMBOL_GPL(videobuf_streamoff);
751 /* Locking: Caller holds q->vb_lock */
752 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
754 size_t count, loff_t *ppos)
756 enum v4l2_field field;
757 unsigned long flags = 0;
760 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
763 q->read_buf = videobuf_alloc(q);
764 if (NULL == q->read_buf)
767 q->read_buf->memory = V4L2_MEMORY_USERPTR;
768 q->read_buf->baddr = (unsigned long)data;
769 q->read_buf->bsize = count;
771 field = videobuf_next_field(q);
772 retval = q->ops->buf_prepare(q, q->read_buf, field);
776 /* start capture & wait */
777 spin_lock_irqsave(q->irqlock, flags);
778 q->ops->buf_queue(q, q->read_buf);
779 spin_unlock_irqrestore(q->irqlock, flags);
780 retval = videobuf_waiton(q->read_buf, 0, 0);
782 CALL(q, sync, q, q->read_buf);
783 if (VIDEOBUF_ERROR == q->read_buf->state)
786 retval = q->read_buf->size;
791 q->ops->buf_release(q, q->read_buf);
797 ssize_t videobuf_read_one(struct videobuf_queue *q,
798 char __user *data, size_t count, loff_t *ppos,
801 enum v4l2_field field;
802 unsigned long flags = 0;
803 unsigned size = 0, nbufs = 1;
806 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
808 mutex_lock(&q->vb_lock);
810 q->ops->buf_setup(q, &nbufs, &size);
812 if (NULL == q->read_buf &&
815 retval = videobuf_read_zerocopy(q, data, count, ppos);
816 if (retval >= 0 || retval == -EIO)
819 /* fallback to kernel bounce buffer on failures */
822 if (NULL == q->read_buf) {
823 /* need to capture a new frame */
825 q->read_buf = videobuf_alloc(q);
827 dprintk(1, "video alloc=0x%p\n", q->read_buf);
828 if (NULL == q->read_buf)
830 q->read_buf->memory = V4L2_MEMORY_USERPTR;
831 q->read_buf->bsize = count; /* preferred size */
832 field = videobuf_next_field(q);
833 retval = q->ops->buf_prepare(q, q->read_buf, field);
841 spin_lock_irqsave(q->irqlock, flags);
842 q->ops->buf_queue(q, q->read_buf);
843 spin_unlock_irqrestore(q->irqlock, flags);
848 /* wait until capture is done */
849 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
853 CALL(q, sync, q, q->read_buf);
855 if (VIDEOBUF_ERROR == q->read_buf->state) {
856 /* catch I/O errors */
857 q->ops->buf_release(q, q->read_buf);
864 /* Copy to userspace */
865 retval = CALL(q, video_copy_to_user, q, data, count, nonblocking);
869 q->read_off += retval;
870 if (q->read_off == q->read_buf->size) {
871 /* all data copied, cleanup */
872 q->ops->buf_release(q, q->read_buf);
878 mutex_unlock(&q->vb_lock);
881 EXPORT_SYMBOL_GPL(videobuf_read_one);
883 /* Locking: Caller holds q->vb_lock */
884 static int __videobuf_read_start(struct videobuf_queue *q)
886 enum v4l2_field field;
887 unsigned long flags = 0;
888 unsigned int count = 0, size = 0;
891 q->ops->buf_setup(q, &count, &size);
894 if (count > VIDEO_MAX_FRAME)
895 count = VIDEO_MAX_FRAME;
896 size = PAGE_ALIGN(size);
898 err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
904 for (i = 0; i < count; i++) {
905 field = videobuf_next_field(q);
906 err = q->ops->buf_prepare(q, q->bufs[i], field);
909 list_add_tail(&q->bufs[i]->stream, &q->stream);
911 spin_lock_irqsave(q->irqlock, flags);
912 for (i = 0; i < count; i++)
913 q->ops->buf_queue(q, q->bufs[i]);
914 spin_unlock_irqrestore(q->irqlock, flags);
919 static void __videobuf_read_stop(struct videobuf_queue *q)
923 videobuf_queue_cancel(q);
924 __videobuf_mmap_free(q);
925 INIT_LIST_HEAD(&q->stream);
926 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
927 if (NULL == q->bufs[i])
935 int videobuf_read_start(struct videobuf_queue *q)
939 mutex_lock(&q->vb_lock);
940 rc = __videobuf_read_start(q);
941 mutex_unlock(&q->vb_lock);
945 EXPORT_SYMBOL_GPL(videobuf_read_start);
947 void videobuf_read_stop(struct videobuf_queue *q)
949 mutex_lock(&q->vb_lock);
950 __videobuf_read_stop(q);
951 mutex_unlock(&q->vb_lock);
953 EXPORT_SYMBOL_GPL(videobuf_read_stop);
955 void videobuf_stop(struct videobuf_queue *q)
957 mutex_lock(&q->vb_lock);
960 __videobuf_streamoff(q);
963 __videobuf_read_stop(q);
965 mutex_unlock(&q->vb_lock);
967 EXPORT_SYMBOL_GPL(videobuf_stop);
969 ssize_t videobuf_read_stream(struct videobuf_queue *q,
970 char __user *data, size_t count, loff_t *ppos,
971 int vbihack, int nonblocking)
974 unsigned long flags = 0;
976 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
978 dprintk(2, "%s\n", __func__);
979 mutex_lock(&q->vb_lock);
984 retval = __videobuf_read_start(q);
991 /* get / wait for data */
992 if (NULL == q->read_buf) {
993 q->read_buf = list_entry(q->stream.next,
994 struct videobuf_buffer,
996 list_del(&q->read_buf->stream);
999 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
1006 if (q->read_buf->state == VIDEOBUF_DONE) {
1007 rc = CALL(q, copy_stream, q, data + retval, count,
1008 retval, vbihack, nonblocking);
1018 q->read_off = q->read_buf->size;
1023 /* requeue buffer when done with copying */
1024 if (q->read_off == q->read_buf->size) {
1025 list_add_tail(&q->read_buf->stream,
1027 spin_lock_irqsave(q->irqlock, flags);
1028 q->ops->buf_queue(q, q->read_buf);
1029 spin_unlock_irqrestore(q->irqlock, flags);
1037 mutex_unlock(&q->vb_lock);
1040 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1042 unsigned int videobuf_poll_stream(struct file *file,
1043 struct videobuf_queue *q,
1046 struct videobuf_buffer *buf = NULL;
1047 unsigned int rc = 0;
1049 mutex_lock(&q->vb_lock);
1051 if (!list_empty(&q->stream))
1052 buf = list_entry(q->stream.next,
1053 struct videobuf_buffer, stream);
1056 __videobuf_read_start(q);
1059 } else if (NULL == q->read_buf) {
1060 q->read_buf = list_entry(q->stream.next,
1061 struct videobuf_buffer,
1063 list_del(&q->read_buf->stream);
1072 poll_wait(file, &buf->done, wait);
1073 if (buf->state == VIDEOBUF_DONE ||
1074 buf->state == VIDEOBUF_ERROR)
1075 rc = POLLIN|POLLRDNORM;
1077 mutex_unlock(&q->vb_lock);
1080 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1082 int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
1086 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1088 mutex_lock(&q->vb_lock);
1089 retval = CALL(q, mmap_mapper, q, vma);
1090 mutex_unlock(&q->vb_lock);
1094 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1096 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1097 int videobuf_cgmbuf(struct videobuf_queue *q,
1098 struct video_mbuf *mbuf, int count)
1100 struct v4l2_requestbuffers req;
1103 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1105 memset(&req, 0, sizeof(req));
1108 req.memory = V4L2_MEMORY_MMAP;
1109 rc = videobuf_reqbufs(q, &req);
1113 mbuf->frames = req.count;
1115 for (i = 0; i < mbuf->frames; i++) {
1116 mbuf->offsets[i] = q->bufs[i]->boff;
1117 mbuf->size += PAGE_ALIGN(q->bufs[i]->bsize);
1122 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);