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>
19 #include <linux/slab.h>
20 #include <linux/interrupt.h>
22 #include <media/videobuf-core.h>
24 #define MAGIC_BUFFER 0x20070728
25 #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
26 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
29 module_param(debug, int, 0644);
31 MODULE_DESCRIPTION("helper module to manage video4linux buffers");
32 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
33 MODULE_LICENSE("GPL");
35 #define dprintk(level, fmt, arg...) if (debug >= level) \
36 printk(KERN_DEBUG "vbuf: " fmt , ## arg)
38 /* --------------------------------------------------------------------- */
40 #define CALL(q, f, arg...) \
41 ( (q->int_ops->f)? q->int_ops->f(arg) : 0)
43 void* videobuf_alloc(struct videobuf_queue* q)
45 struct videobuf_buffer *vb;
47 BUG_ON (q->msize<sizeof(*vb));
49 if (!q->int_ops || !q->int_ops->alloc) {
50 printk(KERN_ERR "No specific ops defined!\n");
54 vb = q->int_ops->alloc(q->msize);
57 init_waitqueue_head(&vb->done);
58 vb->magic = MAGIC_BUFFER;
64 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
67 DECLARE_WAITQUEUE(wait, current);
69 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
70 add_wait_queue(&vb->done, &wait);
71 while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) {
76 set_current_state(intr ? TASK_INTERRUPTIBLE
77 : TASK_UNINTERRUPTIBLE);
78 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
80 set_current_state(TASK_RUNNING);
81 if (intr && signal_pending(current)) {
82 dprintk(1,"buffer waiton: -EINTR\n");
87 remove_wait_queue(&vb->done, &wait);
91 int videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
92 struct v4l2_framebuffer *fbuf)
94 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
95 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
97 /* FIXME: This is required to avoid OOPS on some cases, since mmap_mapper()
98 method should be called before _iolock.
99 On some cases, the mmap_mapper() is called only after scheduling.
101 However, this way is just too dirty! Better to wait for some event.
103 schedule_timeout(HZ);
105 return CALL(q,iolock,q,vb,fbuf);
108 /* --------------------------------------------------------------------- */
111 void videobuf_queue_core_init(struct videobuf_queue* q,
112 struct videobuf_queue_ops *ops,
115 enum v4l2_buf_type type,
116 enum v4l2_field field,
119 struct videobuf_qtype_ops *int_ops)
121 memset(q,0,sizeof(*q));
122 q->irqlock = irqlock;
129 q->int_ops = int_ops;
131 /* All buffer operations are mandatory */
132 BUG_ON (!q->ops->buf_setup);
133 BUG_ON (!q->ops->buf_prepare);
134 BUG_ON (!q->ops->buf_queue);
135 BUG_ON (!q->ops->buf_release);
137 /* Having implementations for abstract methods are mandatory */
138 BUG_ON (!q->int_ops);
140 mutex_init(&q->lock);
141 INIT_LIST_HEAD(&q->stream);
144 int videobuf_queue_is_busy(struct videobuf_queue *q)
148 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
151 dprintk(1,"busy: streaming active\n");
155 dprintk(1,"busy: pending read #1\n");
159 dprintk(1,"busy: pending read #2\n");
162 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
163 if (NULL == q->bufs[i])
165 if (q->bufs[i]->map) {
166 dprintk(1,"busy: buffer #%d mapped\n",i);
169 if (q->bufs[i]->state == STATE_QUEUED) {
170 dprintk(1,"busy: buffer #%d queued\n",i);
173 if (q->bufs[i]->state == STATE_ACTIVE) {
174 dprintk(1,"busy: buffer #%d avtive\n",i);
181 void videobuf_queue_cancel(struct videobuf_queue *q)
183 unsigned long flags=0;
186 /* remove queued buffers from list */
188 spin_lock_irqsave(q->irqlock,flags);
189 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
190 if (NULL == q->bufs[i])
192 if (q->bufs[i]->state == STATE_QUEUED) {
193 list_del(&q->bufs[i]->queue);
194 q->bufs[i]->state = STATE_ERROR;
198 spin_unlock_irqrestore(q->irqlock,flags);
200 /* free all buffers + clear queue */
201 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
202 if (NULL == q->bufs[i])
204 q->ops->buf_release(q,q->bufs[i]);
206 INIT_LIST_HEAD(&q->stream);
209 /* --------------------------------------------------------------------- */
211 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
213 enum v4l2_field field = q->field;
215 BUG_ON(V4L2_FIELD_ANY == field);
217 if (V4L2_FIELD_ALTERNATE == field) {
218 if (V4L2_FIELD_TOP == q->last) {
219 field = V4L2_FIELD_BOTTOM;
220 q->last = V4L2_FIELD_BOTTOM;
222 field = V4L2_FIELD_TOP;
223 q->last = V4L2_FIELD_TOP;
229 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
230 struct videobuf_buffer *vb, enum v4l2_buf_type type)
232 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
233 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
238 b->memory = vb->memory;
240 case V4L2_MEMORY_MMAP:
241 b->m.offset = vb->boff;
242 b->length = vb->bsize;
244 case V4L2_MEMORY_USERPTR:
245 b->m.userptr = vb->baddr;
246 b->length = vb->bsize;
248 case V4L2_MEMORY_OVERLAY:
249 b->m.offset = vb->boff;
255 b->flags |= V4L2_BUF_FLAG_MAPPED;
261 b->flags |= V4L2_BUF_FLAG_QUEUED;
265 b->flags |= V4L2_BUF_FLAG_DONE;
267 case STATE_NEEDS_INIT:
273 if (vb->input != UNSET) {
274 b->flags |= V4L2_BUF_FLAG_INPUT;
275 b->input = vb->input;
278 b->field = vb->field;
279 b->timestamp = vb->ts;
280 b->bytesused = vb->size;
281 b->sequence = vb->field_count >> 1;
284 int videobuf_reqbufs(struct videobuf_queue *q,
285 struct v4l2_requestbuffers *req)
287 unsigned int size,count;
290 if (req->type != q->type) {
291 dprintk(1,"reqbufs: queue type invalid\n");
294 if (req->count < 1) {
295 dprintk(1,"reqbufs: count invalid (%d)\n",req->count);
298 if (req->memory != V4L2_MEMORY_MMAP &&
299 req->memory != V4L2_MEMORY_USERPTR &&
300 req->memory != V4L2_MEMORY_OVERLAY) {
301 dprintk(1,"reqbufs: memory type invalid\n");
305 mutex_lock(&q->lock);
307 dprintk(1,"reqbufs: streaming already exists\n");
311 if (!list_empty(&q->stream)) {
312 dprintk(1,"reqbufs: stream running\n");
318 if (count > VIDEO_MAX_FRAME)
319 count = VIDEO_MAX_FRAME;
321 q->ops->buf_setup(q,&count,&size);
322 size = PAGE_ALIGN(size);
323 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
324 count, size, (count*size)>>PAGE_SHIFT);
326 retval = videobuf_mmap_setup(q,count,size,req->memory);
328 dprintk(1,"reqbufs: mmap setup returned %d\n",retval);
335 mutex_unlock(&q->lock);
339 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
341 if (unlikely(b->type != q->type)) {
342 dprintk(1,"querybuf: Wrong type.\n");
345 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
346 dprintk(1,"querybuf: index out of range.\n");
349 if (unlikely(NULL == q->bufs[b->index])) {
350 dprintk(1,"querybuf: buffer is null.\n");
353 videobuf_status(q,b,q->bufs[b->index],q->type);
357 int videobuf_qbuf(struct videobuf_queue *q,
358 struct v4l2_buffer *b)
360 struct videobuf_buffer *buf;
361 enum v4l2_field field;
362 unsigned long flags=0;
365 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
367 if (b->memory == V4L2_MEMORY_MMAP)
368 down_read(¤t->mm->mmap_sem);
370 mutex_lock(&q->lock);
373 dprintk(1,"qbuf: Reading running...\n");
377 if (b->type != q->type) {
378 dprintk(1,"qbuf: Wrong type.\n");
381 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
382 dprintk(1,"qbuf: index out of range.\n");
385 buf = q->bufs[b->index];
387 dprintk(1,"qbuf: buffer is null.\n");
390 MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
391 if (buf->memory != b->memory) {
392 dprintk(1,"qbuf: memory type is wrong.\n");
395 if (buf->state != STATE_NEEDS_INIT && buf->state != STATE_IDLE) {
396 dprintk(1,"qbuf: buffer is already queued or active.\n");
400 if (b->flags & V4L2_BUF_FLAG_INPUT) {
401 if (b->input >= q->inputs) {
402 dprintk(1,"qbuf: wrong input.\n");
405 buf->input = b->input;
411 case V4L2_MEMORY_MMAP:
412 if (0 == buf->baddr) {
413 dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n");
417 case V4L2_MEMORY_USERPTR:
418 if (b->length < buf->bsize) {
419 dprintk(1,"qbuf: buffer length is not enough\n");
422 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
423 q->ops->buf_release(q,buf);
424 buf->baddr = b->m.userptr;
426 case V4L2_MEMORY_OVERLAY:
427 buf->boff = b->m.offset;
430 dprintk(1,"qbuf: wrong memory type\n");
434 dprintk(1,"qbuf: requesting next field\n");
435 field = videobuf_next_field(q);
436 retval = q->ops->buf_prepare(q,buf,field);
438 dprintk(1,"qbuf: buffer_prepare returned %d\n",retval);
442 list_add_tail(&buf->stream,&q->stream);
445 spin_lock_irqsave(q->irqlock,flags);
446 q->ops->buf_queue(q,buf);
448 spin_unlock_irqrestore(q->irqlock,flags);
450 dprintk(1,"qbuf: succeded\n");
454 mutex_unlock(&q->lock);
456 if (b->memory == V4L2_MEMORY_MMAP)
457 up_read(¤t->mm->mmap_sem);
462 int videobuf_dqbuf(struct videobuf_queue *q,
463 struct v4l2_buffer *b, int nonblocking)
465 struct videobuf_buffer *buf;
468 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
470 mutex_lock(&q->lock);
473 dprintk(1,"dqbuf: Reading running...\n");
477 if (b->type != q->type) {
478 dprintk(1,"dqbuf: Wrong type.\n");
481 if (list_empty(&q->stream)) {
482 dprintk(1,"dqbuf: stream running\n");
485 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
486 retval = videobuf_waiton(buf, nonblocking, 1);
488 dprintk(1,"dqbuf: waiton returned %d\n",retval);
491 switch (buf->state) {
493 dprintk(1,"dqbuf: state is error\n");
496 buf->state = STATE_IDLE;
499 dprintk(1,"dqbuf: state is done\n");
501 buf->state = STATE_IDLE;
504 dprintk(1,"dqbuf: state invalid\n");
508 list_del(&buf->stream);
509 memset(b,0,sizeof(*b));
510 videobuf_status(q,b,buf,q->type);
513 mutex_unlock(&q->lock);
517 int videobuf_streamon(struct videobuf_queue *q)
519 struct videobuf_buffer *buf;
520 unsigned long flags=0;
523 mutex_lock(&q->lock);
532 spin_lock_irqsave(q->irqlock,flags);
533 list_for_each_entry(buf, &q->stream, stream)
534 if (buf->state == STATE_PREPARED)
535 q->ops->buf_queue(q,buf);
537 spin_unlock_irqrestore(q->irqlock,flags);
540 mutex_unlock(&q->lock);
544 int videobuf_streamoff(struct videobuf_queue *q)
546 int retval = -EINVAL;
548 mutex_lock(&q->lock);
551 videobuf_queue_cancel(q);
556 mutex_unlock(&q->lock);
560 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
562 size_t count, loff_t *ppos)
564 enum v4l2_field field;
565 unsigned long flags=0;
568 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
571 q->read_buf = videobuf_alloc(q);
572 if (NULL == q->read_buf)
575 q->read_buf->memory = V4L2_MEMORY_USERPTR;
576 q->read_buf->baddr = (unsigned long)data;
577 q->read_buf->bsize = count;
579 field = videobuf_next_field(q);
580 retval = q->ops->buf_prepare(q,q->read_buf,field);
584 /* start capture & wait */
586 spin_lock_irqsave(q->irqlock,flags);
587 q->ops->buf_queue(q,q->read_buf);
589 spin_unlock_irqrestore(q->irqlock,flags);
590 retval = videobuf_waiton(q->read_buf,0,0);
592 CALL(q,sync,q,q->read_buf);
593 if (STATE_ERROR == q->read_buf->state)
596 retval = q->read_buf->size;
601 q->ops->buf_release(q,q->read_buf);
607 ssize_t videobuf_read_one(struct videobuf_queue *q,
608 char __user *data, size_t count, loff_t *ppos,
611 enum v4l2_field field;
612 unsigned long flags=0;
613 unsigned size, nbufs;
616 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
618 mutex_lock(&q->lock);
621 q->ops->buf_setup(q,&nbufs,&size);
623 if (NULL == q->read_buf &&
626 retval = videobuf_read_zerocopy(q,data,count,ppos);
627 if (retval >= 0 || retval == -EIO)
630 /* fallback to kernel bounce buffer on failures */
633 if (NULL == q->read_buf) {
634 /* need to capture a new frame */
636 q->read_buf = videobuf_alloc(q);
638 dprintk(1,"video alloc=0x%p\n", q->read_buf);
639 if (NULL == q->read_buf)
641 q->read_buf->memory = V4L2_MEMORY_USERPTR;
642 q->read_buf->bsize = count; /* preferred size */
643 field = videobuf_next_field(q);
644 retval = q->ops->buf_prepare(q,q->read_buf,field);
652 spin_lock_irqsave(q->irqlock,flags);
654 q->ops->buf_queue(q,q->read_buf);
656 spin_unlock_irqrestore(q->irqlock,flags);
660 /* wait until capture is done */
661 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
665 CALL(q,sync,q,q->read_buf);
667 if (STATE_ERROR == q->read_buf->state) {
668 /* catch I/O errors */
669 q->ops->buf_release(q,q->read_buf);
676 /* Copy to userspace */
677 retval=CALL(q,video_copy_to_user,q,data,count,nonblocking);
681 q->read_off += retval;
682 if (q->read_off == q->read_buf->size) {
683 /* all data copied, cleanup */
684 q->ops->buf_release(q,q->read_buf);
690 mutex_unlock(&q->lock);
694 int videobuf_read_start(struct videobuf_queue *q)
696 enum v4l2_field field;
697 unsigned long flags=0;
698 unsigned int count = 0, size = 0;
701 q->ops->buf_setup(q,&count,&size);
704 if (count > VIDEO_MAX_FRAME)
705 count = VIDEO_MAX_FRAME;
706 size = PAGE_ALIGN(size);
708 err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
714 for (i = 0; i < count; i++) {
715 field = videobuf_next_field(q);
716 err = q->ops->buf_prepare(q,q->bufs[i],field);
719 list_add_tail(&q->bufs[i]->stream, &q->stream);
722 spin_lock_irqsave(q->irqlock,flags);
723 for (i = 0; i < count; i++)
724 q->ops->buf_queue(q,q->bufs[i]);
726 spin_unlock_irqrestore(q->irqlock,flags);
731 void videobuf_read_stop(struct videobuf_queue *q)
735 videobuf_queue_cancel(q);
736 videobuf_mmap_free(q);
737 INIT_LIST_HEAD(&q->stream);
738 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
739 if (NULL == q->bufs[i])
748 ssize_t videobuf_read_stream(struct videobuf_queue *q,
749 char __user *data, size_t count, loff_t *ppos,
750 int vbihack, int nonblocking)
753 unsigned long flags=0;
755 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
757 dprintk(2,"%s\n",__FUNCTION__);
758 mutex_lock(&q->lock);
763 retval = videobuf_read_start(q);
770 /* get / wait for data */
771 if (NULL == q->read_buf) {
772 q->read_buf = list_entry(q->stream.next,
773 struct videobuf_buffer,
775 list_del(&q->read_buf->stream);
778 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
785 if (q->read_buf->state == STATE_DONE) {
786 rc = CALL (q,copy_stream, q, data + retval, count,
787 retval, vbihack, nonblocking);
797 q->read_off = q->read_buf->size;
802 /* requeue buffer when done with copying */
803 if (q->read_off == q->read_buf->size) {
804 list_add_tail(&q->read_buf->stream,
807 spin_lock_irqsave(q->irqlock,flags);
808 q->ops->buf_queue(q,q->read_buf);
810 spin_unlock_irqrestore(q->irqlock,flags);
818 mutex_unlock(&q->lock);
822 unsigned int videobuf_poll_stream(struct file *file,
823 struct videobuf_queue *q,
826 struct videobuf_buffer *buf = NULL;
829 mutex_lock(&q->lock);
831 if (!list_empty(&q->stream))
832 buf = list_entry(q->stream.next,
833 struct videobuf_buffer, stream);
836 videobuf_read_start(q);
839 } else if (NULL == q->read_buf) {
840 q->read_buf = list_entry(q->stream.next,
841 struct videobuf_buffer,
843 list_del(&q->read_buf->stream);
852 poll_wait(file, &buf->done, wait);
853 if (buf->state == STATE_DONE ||
854 buf->state == STATE_ERROR)
855 rc = POLLIN|POLLRDNORM;
857 mutex_unlock(&q->lock);
861 int videobuf_mmap_setup(struct videobuf_queue *q,
862 unsigned int bcount, unsigned int bsize,
863 enum v4l2_memory memory)
868 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
870 err = videobuf_mmap_free(q);
874 /* Allocate and initialize buffers */
875 for (i = 0; i < bcount; i++) {
876 q->bufs[i] = videobuf_alloc(q);
878 if (q->bufs[i] == NULL)
882 q->bufs[i]->input = UNSET;
883 q->bufs[i]->memory = memory;
884 q->bufs[i]->bsize = bsize;
886 case V4L2_MEMORY_MMAP:
887 q->bufs[i]->boff = bsize * i;
889 case V4L2_MEMORY_USERPTR:
890 case V4L2_MEMORY_OVERLAY:
899 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
905 int videobuf_mmap_free(struct videobuf_queue *q)
913 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
915 rc = CALL(q,mmap_free,q);
919 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
920 if (NULL == q->bufs[i])
922 q->ops->buf_release(q,q->bufs[i]);
930 int videobuf_mmap_mapper(struct videobuf_queue *q,
931 struct vm_area_struct *vma)
935 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
937 mutex_lock(&q->lock);
938 retval=CALL(q,mmap_mapper,q,vma);
939 mutex_unlock(&q->lock);
944 #ifdef CONFIG_VIDEO_V4L1_COMPAT
945 int videobuf_cgmbuf(struct videobuf_queue *q,
946 struct video_mbuf *mbuf, int count)
948 struct v4l2_requestbuffers req;
951 MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
953 memset(&req,0,sizeof(req));
956 req.memory = V4L2_MEMORY_MMAP;
957 rc = videobuf_reqbufs(q,&req);
961 mbuf->frames = req.count;
963 for (i = 0; i < mbuf->frames; i++) {
964 mbuf->offsets[i] = q->bufs[i]->boff;
965 mbuf->size += q->bufs[i]->bsize;
972 /* --------------------------------------------------------------------- */
974 EXPORT_SYMBOL_GPL(videobuf_waiton);
975 EXPORT_SYMBOL_GPL(videobuf_iolock);
977 EXPORT_SYMBOL_GPL(videobuf_alloc);
979 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
980 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
981 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
983 EXPORT_SYMBOL_GPL(videobuf_next_field);
984 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
985 EXPORT_SYMBOL_GPL(videobuf_querybuf);
986 EXPORT_SYMBOL_GPL(videobuf_qbuf);
987 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
988 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
989 EXPORT_SYMBOL_GPL(videobuf_streamon);
990 EXPORT_SYMBOL_GPL(videobuf_streamoff);
992 EXPORT_SYMBOL_GPL(videobuf_read_start);
993 EXPORT_SYMBOL_GPL(videobuf_read_stop);
994 EXPORT_SYMBOL_GPL(videobuf_read_stream);
995 EXPORT_SYMBOL_GPL(videobuf_read_one);
996 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
998 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
999 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1000 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);