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 struct videobuf_buffer *videobuf_alloc_vb(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_vb) {
62 printk(KERN_ERR "No specific ops defined!\n");
66 vb = q->int_ops->alloc_vb(q->msize);
68 init_waitqueue_head(&vb->done);
69 vb->magic = MAGIC_BUFFER;
74 EXPORT_SYMBOL_GPL(videobuf_alloc_vb);
76 static int is_state_active_or_queued(struct videobuf_queue *q, struct videobuf_buffer *vb)
81 spin_lock_irqsave(q->irqlock, flags);
82 rc = vb->state != VIDEOBUF_ACTIVE && vb->state != VIDEOBUF_QUEUED;
83 spin_unlock_irqrestore(q->irqlock, flags);
87 int videobuf_waiton(struct videobuf_queue *q, struct videobuf_buffer *vb,
88 int non_blocking, int intr)
93 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
96 if (is_state_active_or_queued(q, vb))
101 is_ext_locked = q->ext_lock && mutex_is_locked(q->ext_lock);
103 /* Release vdev lock to prevent this wait from blocking outside access to
106 mutex_unlock(q->ext_lock);
108 ret = wait_event_interruptible(vb->done, is_state_active_or_queued(q, vb));
110 wait_event(vb->done, is_state_active_or_queued(q, vb));
113 mutex_lock(q->ext_lock);
117 EXPORT_SYMBOL_GPL(videobuf_waiton);
119 int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
120 struct v4l2_framebuffer *fbuf)
122 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
123 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
125 return CALL(q, iolock, q, vb, fbuf);
127 EXPORT_SYMBOL_GPL(videobuf_iolock);
129 void *videobuf_queue_to_vaddr(struct videobuf_queue *q,
130 struct videobuf_buffer *buf)
132 if (q->int_ops->vaddr)
133 return q->int_ops->vaddr(buf);
136 EXPORT_SYMBOL_GPL(videobuf_queue_to_vaddr);
138 /* --------------------------------------------------------------------- */
141 void videobuf_queue_core_init(struct videobuf_queue *q,
142 const struct videobuf_queue_ops *ops,
145 enum v4l2_buf_type type,
146 enum v4l2_field field,
149 struct videobuf_qtype_ops *int_ops,
150 struct mutex *ext_lock)
153 memset(q, 0, sizeof(*q));
154 q->irqlock = irqlock;
155 q->ext_lock = ext_lock;
162 q->int_ops = int_ops;
164 /* All buffer operations are mandatory */
165 BUG_ON(!q->ops->buf_setup);
166 BUG_ON(!q->ops->buf_prepare);
167 BUG_ON(!q->ops->buf_queue);
168 BUG_ON(!q->ops->buf_release);
170 /* Lock is mandatory for queue_cancel to work */
173 /* Having implementations for abstract methods are mandatory */
176 mutex_init(&q->vb_lock);
177 init_waitqueue_head(&q->wait);
178 INIT_LIST_HEAD(&q->stream);
180 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
182 /* Locking: Only usage in bttv unsafe find way to remove */
183 int videobuf_queue_is_busy(struct videobuf_queue *q)
187 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
190 dprintk(1, "busy: streaming active\n");
194 dprintk(1, "busy: pending read #1\n");
198 dprintk(1, "busy: pending read #2\n");
201 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
202 if (NULL == q->bufs[i])
204 if (q->bufs[i]->map) {
205 dprintk(1, "busy: buffer #%d mapped\n", i);
208 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
209 dprintk(1, "busy: buffer #%d queued\n", i);
212 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
213 dprintk(1, "busy: buffer #%d avtive\n", i);
219 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
222 * __videobuf_free() - free all the buffers and their control structures
224 * This function can only be called if streaming/reading is off, i.e. no buffers
225 * are under control of the driver.
227 /* Locking: Caller holds q->vb_lock */
228 static int __videobuf_free(struct videobuf_queue *q)
232 dprintk(1, "%s\n", __func__);
236 if (q->streaming || q->reading) {
237 dprintk(1, "Cannot free buffers when streaming or reading\n");
241 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
243 for (i = 0; i < VIDEO_MAX_FRAME; i++)
244 if (q->bufs[i] && q->bufs[i]->map) {
245 dprintk(1, "Cannot free mmapped buffers\n");
249 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
250 if (NULL == q->bufs[i])
252 q->ops->buf_release(q, q->bufs[i]);
260 /* Locking: Caller holds q->vb_lock */
261 void videobuf_queue_cancel(struct videobuf_queue *q)
263 unsigned long flags = 0;
268 wake_up_interruptible_sync(&q->wait);
270 /* remove queued buffers from list */
271 spin_lock_irqsave(q->irqlock, flags);
272 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
273 if (NULL == q->bufs[i])
275 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
276 list_del(&q->bufs[i]->queue);
277 q->bufs[i]->state = VIDEOBUF_ERROR;
278 wake_up_all(&q->bufs[i]->done);
281 spin_unlock_irqrestore(q->irqlock, flags);
283 /* free all buffers + clear queue */
284 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
285 if (NULL == q->bufs[i])
287 q->ops->buf_release(q, q->bufs[i]);
289 INIT_LIST_HEAD(&q->stream);
291 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
293 /* --------------------------------------------------------------------- */
295 /* Locking: Caller holds q->vb_lock */
296 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
298 enum v4l2_field field = q->field;
300 BUG_ON(V4L2_FIELD_ANY == field);
302 if (V4L2_FIELD_ALTERNATE == field) {
303 if (V4L2_FIELD_TOP == q->last) {
304 field = V4L2_FIELD_BOTTOM;
305 q->last = V4L2_FIELD_BOTTOM;
307 field = V4L2_FIELD_TOP;
308 q->last = V4L2_FIELD_TOP;
313 EXPORT_SYMBOL_GPL(videobuf_next_field);
315 /* Locking: Caller holds q->vb_lock */
316 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
317 struct videobuf_buffer *vb, enum v4l2_buf_type type)
319 MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
320 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
325 b->memory = vb->memory;
327 case V4L2_MEMORY_MMAP:
328 b->m.offset = vb->boff;
329 b->length = vb->bsize;
331 case V4L2_MEMORY_USERPTR:
332 b->m.userptr = vb->baddr;
333 b->length = vb->bsize;
335 case V4L2_MEMORY_OVERLAY:
336 b->m.offset = vb->boff;
342 b->flags |= V4L2_BUF_FLAG_MAPPED;
345 case VIDEOBUF_PREPARED:
346 case VIDEOBUF_QUEUED:
347 case VIDEOBUF_ACTIVE:
348 b->flags |= V4L2_BUF_FLAG_QUEUED;
351 b->flags |= V4L2_BUF_FLAG_ERROR;
354 b->flags |= V4L2_BUF_FLAG_DONE;
356 case VIDEOBUF_NEEDS_INIT:
362 b->field = vb->field;
363 b->timestamp = vb->ts;
364 b->bytesused = vb->size;
365 b->sequence = vb->field_count >> 1;
368 int videobuf_mmap_free(struct videobuf_queue *q)
371 videobuf_queue_lock(q);
372 ret = __videobuf_free(q);
373 videobuf_queue_unlock(q);
376 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
378 /* Locking: Caller holds q->vb_lock */
379 int __videobuf_mmap_setup(struct videobuf_queue *q,
380 unsigned int bcount, unsigned int bsize,
381 enum v4l2_memory memory)
386 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
388 err = __videobuf_free(q);
392 /* Allocate and initialize buffers */
393 for (i = 0; i < bcount; i++) {
394 q->bufs[i] = videobuf_alloc_vb(q);
396 if (NULL == q->bufs[i])
400 q->bufs[i]->memory = memory;
401 q->bufs[i]->bsize = bsize;
403 case V4L2_MEMORY_MMAP:
404 q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
406 case V4L2_MEMORY_USERPTR:
407 case V4L2_MEMORY_OVERLAY:
416 dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
420 EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
422 int videobuf_mmap_setup(struct videobuf_queue *q,
423 unsigned int bcount, unsigned int bsize,
424 enum v4l2_memory memory)
427 videobuf_queue_lock(q);
428 ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
429 videobuf_queue_unlock(q);
432 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
434 int videobuf_reqbufs(struct videobuf_queue *q,
435 struct v4l2_requestbuffers *req)
437 unsigned int size, count;
440 if (req->count < 1) {
441 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
445 if (req->memory != V4L2_MEMORY_MMAP &&
446 req->memory != V4L2_MEMORY_USERPTR &&
447 req->memory != V4L2_MEMORY_OVERLAY) {
448 dprintk(1, "reqbufs: memory type invalid\n");
452 videobuf_queue_lock(q);
453 if (req->type != q->type) {
454 dprintk(1, "reqbufs: queue type invalid\n");
460 dprintk(1, "reqbufs: streaming already exists\n");
464 if (!list_empty(&q->stream)) {
465 dprintk(1, "reqbufs: stream running\n");
471 if (count > VIDEO_MAX_FRAME)
472 count = VIDEO_MAX_FRAME;
474 q->ops->buf_setup(q, &count, &size);
475 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
477 (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
479 retval = __videobuf_mmap_setup(q, count, size, req->memory);
481 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
489 videobuf_queue_unlock(q);
492 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
494 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
498 videobuf_queue_lock(q);
499 if (unlikely(b->type != q->type)) {
500 dprintk(1, "querybuf: Wrong type.\n");
503 if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
504 dprintk(1, "querybuf: index out of range.\n");
507 if (unlikely(NULL == q->bufs[b->index])) {
508 dprintk(1, "querybuf: buffer is null.\n");
512 videobuf_status(q, b, q->bufs[b->index], q->type);
516 videobuf_queue_unlock(q);
519 EXPORT_SYMBOL_GPL(videobuf_querybuf);
521 int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
523 struct videobuf_buffer *buf;
524 enum v4l2_field field;
525 unsigned long flags = 0;
528 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
530 if (b->memory == V4L2_MEMORY_MMAP)
531 down_read(¤t->mm->mmap_sem);
533 videobuf_queue_lock(q);
536 dprintk(1, "qbuf: Reading running...\n");
540 if (b->type != q->type) {
541 dprintk(1, "qbuf: Wrong type.\n");
544 if (b->index >= VIDEO_MAX_FRAME) {
545 dprintk(1, "qbuf: index out of range.\n");
548 buf = q->bufs[b->index];
550 dprintk(1, "qbuf: buffer is null.\n");
553 MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
554 if (buf->memory != b->memory) {
555 dprintk(1, "qbuf: memory type is wrong.\n");
558 if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
559 dprintk(1, "qbuf: buffer is already queued or active.\n");
564 case V4L2_MEMORY_MMAP:
565 if (0 == buf->baddr) {
566 dprintk(1, "qbuf: mmap requested "
567 "but buffer addr is zero!\n");
570 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT
571 || q->type == V4L2_BUF_TYPE_VBI_OUTPUT
572 || q->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
573 buf->size = b->bytesused;
574 buf->field = b->field;
575 buf->ts = b->timestamp;
578 case V4L2_MEMORY_USERPTR:
579 if (b->length < buf->bsize) {
580 dprintk(1, "qbuf: buffer length is not enough\n");
583 if (VIDEOBUF_NEEDS_INIT != buf->state &&
584 buf->baddr != b->m.userptr)
585 q->ops->buf_release(q, buf);
586 buf->baddr = b->m.userptr;
588 case V4L2_MEMORY_OVERLAY:
589 buf->boff = b->m.offset;
592 dprintk(1, "qbuf: wrong memory type\n");
596 dprintk(1, "qbuf: requesting next field\n");
597 field = videobuf_next_field(q);
598 retval = q->ops->buf_prepare(q, buf, field);
600 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
604 list_add_tail(&buf->stream, &q->stream);
606 spin_lock_irqsave(q->irqlock, flags);
607 q->ops->buf_queue(q, buf);
608 spin_unlock_irqrestore(q->irqlock, flags);
610 dprintk(1, "qbuf: succeeded\n");
612 wake_up_interruptible_sync(&q->wait);
615 videobuf_queue_unlock(q);
617 if (b->memory == V4L2_MEMORY_MMAP)
618 up_read(¤t->mm->mmap_sem);
622 EXPORT_SYMBOL_GPL(videobuf_qbuf);
624 /* Locking: Caller holds q->vb_lock */
625 static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
631 dprintk(1, "next_buffer: Not streaming\n");
636 if (list_empty(&q->stream)) {
639 dprintk(2, "next_buffer: no buffers to dequeue\n");
642 dprintk(2, "next_buffer: waiting on buffer\n");
644 /* Drop lock to avoid deadlock with qbuf */
645 videobuf_queue_unlock(q);
647 /* Checking list_empty and streaming is safe without
648 * locks because we goto checks to validate while
649 * holding locks before proceeding */
650 retval = wait_event_interruptible(q->wait,
651 !list_empty(&q->stream) || !q->streaming);
652 videobuf_queue_lock(q);
667 /* Locking: Caller holds q->vb_lock */
668 static int stream_next_buffer(struct videobuf_queue *q,
669 struct videobuf_buffer **vb, int nonblocking)
672 struct videobuf_buffer *buf = NULL;
674 retval = stream_next_buffer_check_queue(q, nonblocking);
678 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
679 retval = videobuf_waiton(q, buf, nonblocking, 1);
688 int videobuf_dqbuf(struct videobuf_queue *q,
689 struct v4l2_buffer *b, int nonblocking)
691 struct videobuf_buffer *buf = NULL;
694 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
696 memset(b, 0, sizeof(*b));
697 videobuf_queue_lock(q);
699 retval = stream_next_buffer(q, &buf, nonblocking);
701 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
705 switch (buf->state) {
707 dprintk(1, "dqbuf: state is error\n");
710 dprintk(1, "dqbuf: state is done\n");
713 dprintk(1, "dqbuf: state invalid\n");
717 CALL(q, sync, q, buf);
718 videobuf_status(q, b, buf, q->type);
719 list_del(&buf->stream);
720 buf->state = VIDEOBUF_IDLE;
721 b->flags &= ~V4L2_BUF_FLAG_DONE;
723 videobuf_queue_unlock(q);
726 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
728 int videobuf_streamon(struct videobuf_queue *q)
730 struct videobuf_buffer *buf;
731 unsigned long flags = 0;
734 videobuf_queue_lock(q);
742 spin_lock_irqsave(q->irqlock, flags);
743 list_for_each_entry(buf, &q->stream, stream)
744 if (buf->state == VIDEOBUF_PREPARED)
745 q->ops->buf_queue(q, buf);
746 spin_unlock_irqrestore(q->irqlock, flags);
748 wake_up_interruptible_sync(&q->wait);
750 videobuf_queue_unlock(q);
753 EXPORT_SYMBOL_GPL(videobuf_streamon);
755 /* Locking: Caller holds q->vb_lock */
756 static int __videobuf_streamoff(struct videobuf_queue *q)
761 videobuf_queue_cancel(q);
766 int videobuf_streamoff(struct videobuf_queue *q)
770 videobuf_queue_lock(q);
771 retval = __videobuf_streamoff(q);
772 videobuf_queue_unlock(q);
776 EXPORT_SYMBOL_GPL(videobuf_streamoff);
778 /* Locking: Caller holds q->vb_lock */
779 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
781 size_t count, loff_t *ppos)
783 enum v4l2_field field;
784 unsigned long flags = 0;
787 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
790 q->read_buf = videobuf_alloc_vb(q);
791 if (NULL == q->read_buf)
794 q->read_buf->memory = V4L2_MEMORY_USERPTR;
795 q->read_buf->baddr = (unsigned long)data;
796 q->read_buf->bsize = count;
798 field = videobuf_next_field(q);
799 retval = q->ops->buf_prepare(q, q->read_buf, field);
803 /* start capture & wait */
804 spin_lock_irqsave(q->irqlock, flags);
805 q->ops->buf_queue(q, q->read_buf);
806 spin_unlock_irqrestore(q->irqlock, flags);
807 retval = videobuf_waiton(q, q->read_buf, 0, 0);
809 CALL(q, sync, q, q->read_buf);
810 if (VIDEOBUF_ERROR == q->read_buf->state)
813 retval = q->read_buf->size;
818 q->ops->buf_release(q, q->read_buf);
824 static int __videobuf_copy_to_user(struct videobuf_queue *q,
825 struct videobuf_buffer *buf,
826 char __user *data, size_t count,
829 void *vaddr = CALL(q, vaddr, buf);
831 /* copy to userspace */
832 if (count > buf->size - q->read_off)
833 count = buf->size - q->read_off;
835 if (copy_to_user(data, vaddr + q->read_off, count))
841 static int __videobuf_copy_stream(struct videobuf_queue *q,
842 struct videobuf_buffer *buf,
843 char __user *data, size_t count, size_t pos,
844 int vbihack, int nonblocking)
846 unsigned int *fc = CALL(q, vaddr, buf);
849 /* dirty, undocumented hack -- pass the frame counter
850 * within the last four bytes of each vbi data block.
851 * We need that one to maintain backward compatibility
852 * to all vbi decoding software out there ... */
853 fc += (buf->size >> 2) - 1;
854 *fc = buf->field_count >> 1;
855 dprintk(1, "vbihack: %d\n", *fc);
858 /* copy stuff using the common method */
859 count = __videobuf_copy_to_user(q, buf, data, count, nonblocking);
861 if ((count == -EFAULT) && (pos == 0))
867 ssize_t videobuf_read_one(struct videobuf_queue *q,
868 char __user *data, size_t count, loff_t *ppos,
871 enum v4l2_field field;
872 unsigned long flags = 0;
873 unsigned size = 0, nbufs = 1;
876 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
878 videobuf_queue_lock(q);
880 q->ops->buf_setup(q, &nbufs, &size);
882 if (NULL == q->read_buf &&
885 retval = videobuf_read_zerocopy(q, data, count, ppos);
886 if (retval >= 0 || retval == -EIO)
889 /* fallback to kernel bounce buffer on failures */
892 if (NULL == q->read_buf) {
893 /* need to capture a new frame */
895 q->read_buf = videobuf_alloc_vb(q);
897 dprintk(1, "video alloc=0x%p\n", q->read_buf);
898 if (NULL == q->read_buf)
900 q->read_buf->memory = V4L2_MEMORY_USERPTR;
901 q->read_buf->bsize = count; /* preferred size */
902 field = videobuf_next_field(q);
903 retval = q->ops->buf_prepare(q, q->read_buf, field);
911 spin_lock_irqsave(q->irqlock, flags);
912 q->ops->buf_queue(q, q->read_buf);
913 spin_unlock_irqrestore(q->irqlock, flags);
918 /* wait until capture is done */
919 retval = videobuf_waiton(q, q->read_buf, nonblocking, 1);
923 CALL(q, sync, q, q->read_buf);
925 if (VIDEOBUF_ERROR == q->read_buf->state) {
926 /* catch I/O errors */
927 q->ops->buf_release(q, q->read_buf);
934 /* Copy to userspace */
935 retval = __videobuf_copy_to_user(q, q->read_buf, data, count, nonblocking);
939 q->read_off += retval;
940 if (q->read_off == q->read_buf->size) {
941 /* all data copied, cleanup */
942 q->ops->buf_release(q, q->read_buf);
948 videobuf_queue_unlock(q);
951 EXPORT_SYMBOL_GPL(videobuf_read_one);
953 /* Locking: Caller holds q->vb_lock */
954 static int __videobuf_read_start(struct videobuf_queue *q)
956 enum v4l2_field field;
957 unsigned long flags = 0;
958 unsigned int count = 0, size = 0;
961 q->ops->buf_setup(q, &count, &size);
964 if (count > VIDEO_MAX_FRAME)
965 count = VIDEO_MAX_FRAME;
966 size = PAGE_ALIGN(size);
968 err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
974 for (i = 0; i < count; i++) {
975 field = videobuf_next_field(q);
976 err = q->ops->buf_prepare(q, q->bufs[i], field);
979 list_add_tail(&q->bufs[i]->stream, &q->stream);
981 spin_lock_irqsave(q->irqlock, flags);
982 for (i = 0; i < count; i++)
983 q->ops->buf_queue(q, q->bufs[i]);
984 spin_unlock_irqrestore(q->irqlock, flags);
989 static void __videobuf_read_stop(struct videobuf_queue *q)
993 videobuf_queue_cancel(q);
995 INIT_LIST_HEAD(&q->stream);
996 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
997 if (NULL == q->bufs[i])
1005 int videobuf_read_start(struct videobuf_queue *q)
1009 videobuf_queue_lock(q);
1010 rc = __videobuf_read_start(q);
1011 videobuf_queue_unlock(q);
1015 EXPORT_SYMBOL_GPL(videobuf_read_start);
1017 void videobuf_read_stop(struct videobuf_queue *q)
1019 videobuf_queue_lock(q);
1020 __videobuf_read_stop(q);
1021 videobuf_queue_unlock(q);
1023 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1025 void videobuf_stop(struct videobuf_queue *q)
1027 videobuf_queue_lock(q);
1030 __videobuf_streamoff(q);
1033 __videobuf_read_stop(q);
1035 videobuf_queue_unlock(q);
1037 EXPORT_SYMBOL_GPL(videobuf_stop);
1039 ssize_t videobuf_read_stream(struct videobuf_queue *q,
1040 char __user *data, size_t count, loff_t *ppos,
1041 int vbihack, int nonblocking)
1044 unsigned long flags = 0;
1046 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1048 dprintk(2, "%s\n", __func__);
1049 videobuf_queue_lock(q);
1054 retval = __videobuf_read_start(q);
1061 /* get / wait for data */
1062 if (NULL == q->read_buf) {
1063 q->read_buf = list_entry(q->stream.next,
1064 struct videobuf_buffer,
1066 list_del(&q->read_buf->stream);
1069 rc = videobuf_waiton(q, q->read_buf, nonblocking, 1);
1076 if (q->read_buf->state == VIDEOBUF_DONE) {
1077 rc = __videobuf_copy_stream(q, q->read_buf, data + retval, count,
1078 retval, vbihack, nonblocking);
1088 q->read_off = q->read_buf->size;
1093 /* requeue buffer when done with copying */
1094 if (q->read_off == q->read_buf->size) {
1095 list_add_tail(&q->read_buf->stream,
1097 spin_lock_irqsave(q->irqlock, flags);
1098 q->ops->buf_queue(q, q->read_buf);
1099 spin_unlock_irqrestore(q->irqlock, flags);
1107 videobuf_queue_unlock(q);
1110 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1112 unsigned int videobuf_poll_stream(struct file *file,
1113 struct videobuf_queue *q,
1116 unsigned long req_events = poll_requested_events(wait);
1117 struct videobuf_buffer *buf = NULL;
1118 unsigned int rc = 0;
1120 videobuf_queue_lock(q);
1122 if (!list_empty(&q->stream))
1123 buf = list_entry(q->stream.next,
1124 struct videobuf_buffer, stream);
1125 } else if (req_events & (POLLIN | POLLRDNORM)) {
1127 __videobuf_read_start(q);
1130 } else if (NULL == q->read_buf) {
1131 q->read_buf = list_entry(q->stream.next,
1132 struct videobuf_buffer,
1134 list_del(&q->read_buf->stream);
1143 poll_wait(file, &buf->done, wait);
1144 if (buf->state == VIDEOBUF_DONE ||
1145 buf->state == VIDEOBUF_ERROR) {
1147 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1148 case V4L2_BUF_TYPE_VBI_OUTPUT:
1149 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1150 rc = POLLOUT | POLLWRNORM;
1153 rc = POLLIN | POLLRDNORM;
1158 videobuf_queue_unlock(q);
1161 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1163 int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
1168 MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1170 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED)) {
1171 dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
1175 videobuf_queue_lock(q);
1176 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1177 struct videobuf_buffer *buf = q->bufs[i];
1179 if (buf && buf->memory == V4L2_MEMORY_MMAP &&
1180 buf->boff == (vma->vm_pgoff << PAGE_SHIFT)) {
1181 rc = CALL(q, mmap_mapper, q, buf, vma);
1185 videobuf_queue_unlock(q);
1189 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);