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) do { \
26 if (unlikely((is) != (should))) { \
27 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", is, should); \
31 module_param(debug
, int, 0644);
33 MODULE_DESCRIPTION("helper module to manage video4linux buffers");
34 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
35 MODULE_LICENSE("GPL");
37 #define dprintk(level, fmt, arg...) do { \
39 printk(KERN_DEBUG "vbuf: " fmt , ## arg); } while (0)
41 /* --------------------------------------------------------------------- */
43 #define CALL(q, f, arg...) \
44 ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
46 void *videobuf_alloc(struct videobuf_queue
*q
)
48 struct videobuf_buffer
*vb
;
50 BUG_ON(q
->msize
< sizeof(*vb
));
52 if (!q
->int_ops
|| !q
->int_ops
->alloc
) {
53 printk(KERN_ERR
"No specific ops defined!\n");
57 vb
= q
->int_ops
->alloc(q
->msize
);
60 init_waitqueue_head(&vb
->done
);
61 vb
->magic
= MAGIC_BUFFER
;
67 #define WAITON_CONDITION (vb->state != VIDEOBUF_ACTIVE &&\
68 vb->state != VIDEOBUF_QUEUED)
69 int videobuf_waiton(struct videobuf_buffer
*vb
, int non_blocking
, int intr
)
71 MAGIC_CHECK(vb
->magic
, MAGIC_BUFFER
);
81 return wait_event_interruptible(vb
->done
, WAITON_CONDITION
);
83 wait_event(vb
->done
, WAITON_CONDITION
);
88 int videobuf_iolock(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
89 struct v4l2_framebuffer
*fbuf
)
91 MAGIC_CHECK(vb
->magic
, MAGIC_BUFFER
);
92 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
94 return CALL(q
, iolock
, q
, vb
, fbuf
);
97 void *videobuf_queue_to_vmalloc (struct videobuf_queue
*q
,
98 struct videobuf_buffer
*buf
)
100 if (q
->int_ops
->vmalloc
)
101 return q
->int_ops
->vmalloc(buf
);
105 EXPORT_SYMBOL_GPL(videobuf_queue_to_vmalloc
);
107 /* --------------------------------------------------------------------- */
110 void videobuf_queue_core_init(struct videobuf_queue
*q
,
111 struct videobuf_queue_ops
*ops
,
114 enum v4l2_buf_type type
,
115 enum v4l2_field field
,
118 struct videobuf_qtype_ops
*int_ops
)
120 memset(q
, 0, sizeof(*q
));
121 q
->irqlock
= irqlock
;
128 q
->int_ops
= int_ops
;
130 /* All buffer operations are mandatory */
131 BUG_ON(!q
->ops
->buf_setup
);
132 BUG_ON(!q
->ops
->buf_prepare
);
133 BUG_ON(!q
->ops
->buf_queue
);
134 BUG_ON(!q
->ops
->buf_release
);
136 /* Lock is mandatory for queue_cancel to work */
139 /* Having implementations for abstract methods are mandatory */
142 mutex_init(&q
->vb_lock
);
143 init_waitqueue_head(&q
->wait
);
144 INIT_LIST_HEAD(&q
->stream
);
147 /* Locking: Only usage in bttv unsafe find way to remove */
148 int videobuf_queue_is_busy(struct videobuf_queue
*q
)
152 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
155 dprintk(1, "busy: streaming active\n");
159 dprintk(1, "busy: pending read #1\n");
163 dprintk(1, "busy: pending read #2\n");
166 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
167 if (NULL
== q
->bufs
[i
])
169 if (q
->bufs
[i
]->map
) {
170 dprintk(1, "busy: buffer #%d mapped\n", i
);
173 if (q
->bufs
[i
]->state
== VIDEOBUF_QUEUED
) {
174 dprintk(1, "busy: buffer #%d queued\n", i
);
177 if (q
->bufs
[i
]->state
== VIDEOBUF_ACTIVE
) {
178 dprintk(1, "busy: buffer #%d avtive\n", i
);
185 /* Locking: Caller holds q->vb_lock */
186 void videobuf_queue_cancel(struct videobuf_queue
*q
)
188 unsigned long flags
= 0;
193 wake_up_interruptible_sync(&q
->wait
);
195 /* remove queued buffers from list */
196 spin_lock_irqsave(q
->irqlock
, flags
);
197 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
198 if (NULL
== q
->bufs
[i
])
200 if (q
->bufs
[i
]->state
== VIDEOBUF_QUEUED
) {
201 list_del(&q
->bufs
[i
]->queue
);
202 q
->bufs
[i
]->state
= VIDEOBUF_ERROR
;
203 wake_up_all(&q
->bufs
[i
]->done
);
206 spin_unlock_irqrestore(q
->irqlock
, flags
);
208 /* free all buffers + clear queue */
209 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
210 if (NULL
== q
->bufs
[i
])
212 q
->ops
->buf_release(q
, q
->bufs
[i
]);
214 INIT_LIST_HEAD(&q
->stream
);
217 /* --------------------------------------------------------------------- */
219 /* Locking: Caller holds q->vb_lock */
220 enum v4l2_field
videobuf_next_field(struct videobuf_queue
*q
)
222 enum v4l2_field field
= q
->field
;
224 BUG_ON(V4L2_FIELD_ANY
== field
);
226 if (V4L2_FIELD_ALTERNATE
== field
) {
227 if (V4L2_FIELD_TOP
== q
->last
) {
228 field
= V4L2_FIELD_BOTTOM
;
229 q
->last
= V4L2_FIELD_BOTTOM
;
231 field
= V4L2_FIELD_TOP
;
232 q
->last
= V4L2_FIELD_TOP
;
238 /* Locking: Caller holds q->vb_lock */
239 static void videobuf_status(struct videobuf_queue
*q
, struct v4l2_buffer
*b
,
240 struct videobuf_buffer
*vb
, enum v4l2_buf_type type
)
242 MAGIC_CHECK(vb
->magic
, MAGIC_BUFFER
);
243 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
248 b
->memory
= vb
->memory
;
250 case V4L2_MEMORY_MMAP
:
251 b
->m
.offset
= vb
->boff
;
252 b
->length
= vb
->bsize
;
254 case V4L2_MEMORY_USERPTR
:
255 b
->m
.userptr
= vb
->baddr
;
256 b
->length
= vb
->bsize
;
258 case V4L2_MEMORY_OVERLAY
:
259 b
->m
.offset
= vb
->boff
;
265 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
268 case VIDEOBUF_PREPARED
:
269 case VIDEOBUF_QUEUED
:
270 case VIDEOBUF_ACTIVE
:
271 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
275 b
->flags
|= V4L2_BUF_FLAG_DONE
;
277 case VIDEOBUF_NEEDS_INIT
:
283 if (vb
->input
!= UNSET
) {
284 b
->flags
|= V4L2_BUF_FLAG_INPUT
;
285 b
->input
= vb
->input
;
288 b
->field
= vb
->field
;
289 b
->timestamp
= vb
->ts
;
290 b
->bytesused
= vb
->size
;
291 b
->sequence
= vb
->field_count
>> 1;
294 /* Locking: Caller holds q->vb_lock */
295 static int __videobuf_mmap_free(struct videobuf_queue
*q
)
303 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
306 rc
= CALL(q
, mmap_free
, q
);
313 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
314 if (NULL
== q
->bufs
[i
])
316 q
->ops
->buf_release(q
, q
->bufs
[i
]);
324 int videobuf_mmap_free(struct videobuf_queue
*q
)
327 mutex_lock(&q
->vb_lock
);
328 ret
= __videobuf_mmap_free(q
);
329 mutex_unlock(&q
->vb_lock
);
333 /* Locking: Caller holds q->vb_lock */
334 static int __videobuf_mmap_setup(struct videobuf_queue
*q
,
335 unsigned int bcount
, unsigned int bsize
,
336 enum v4l2_memory memory
)
341 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
343 err
= __videobuf_mmap_free(q
);
347 /* Allocate and initialize buffers */
348 for (i
= 0; i
< bcount
; i
++) {
349 q
->bufs
[i
] = videobuf_alloc(q
);
351 if (q
->bufs
[i
] == NULL
)
355 q
->bufs
[i
]->input
= UNSET
;
356 q
->bufs
[i
]->memory
= memory
;
357 q
->bufs
[i
]->bsize
= bsize
;
359 case V4L2_MEMORY_MMAP
:
360 q
->bufs
[i
]->boff
= bsize
* i
;
362 case V4L2_MEMORY_USERPTR
:
363 case V4L2_MEMORY_OVERLAY
:
372 dprintk(1, "mmap setup: %d buffers, %d bytes each\n",
378 int videobuf_mmap_setup(struct videobuf_queue
*q
,
379 unsigned int bcount
, unsigned int bsize
,
380 enum v4l2_memory memory
)
383 mutex_lock(&q
->vb_lock
);
384 ret
= __videobuf_mmap_setup(q
, bcount
, bsize
, memory
);
385 mutex_unlock(&q
->vb_lock
);
389 int videobuf_reqbufs(struct videobuf_queue
*q
,
390 struct v4l2_requestbuffers
*req
)
392 unsigned int size
, count
;
395 if (req
->count
< 1) {
396 dprintk(1, "reqbufs: count invalid (%d)\n", req
->count
);
400 if (req
->memory
!= V4L2_MEMORY_MMAP
&&
401 req
->memory
!= V4L2_MEMORY_USERPTR
&&
402 req
->memory
!= V4L2_MEMORY_OVERLAY
) {
403 dprintk(1, "reqbufs: memory type invalid\n");
407 mutex_lock(&q
->vb_lock
);
408 if (req
->type
!= q
->type
) {
409 dprintk(1, "reqbufs: queue type invalid\n");
415 dprintk(1, "reqbufs: streaming already exists\n");
419 if (!list_empty(&q
->stream
)) {
420 dprintk(1, "reqbufs: stream running\n");
426 if (count
> VIDEO_MAX_FRAME
)
427 count
= VIDEO_MAX_FRAME
;
429 q
->ops
->buf_setup(q
, &count
, &size
);
430 size
= PAGE_ALIGN(size
);
431 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
432 count
, size
, (count
*size
)>>PAGE_SHIFT
);
434 retval
= __videobuf_mmap_setup(q
, count
, size
, req
->memory
);
436 dprintk(1, "reqbufs: mmap setup returned %d\n", retval
);
443 mutex_unlock(&q
->vb_lock
);
447 int videobuf_querybuf(struct videobuf_queue
*q
, struct v4l2_buffer
*b
)
451 mutex_lock(&q
->vb_lock
);
452 if (unlikely(b
->type
!= q
->type
)) {
453 dprintk(1, "querybuf: Wrong type.\n");
456 if (unlikely(b
->index
< 0 || b
->index
>= VIDEO_MAX_FRAME
)) {
457 dprintk(1, "querybuf: index out of range.\n");
460 if (unlikely(NULL
== q
->bufs
[b
->index
])) {
461 dprintk(1, "querybuf: buffer is null.\n");
465 videobuf_status(q
, b
, q
->bufs
[b
->index
], q
->type
);
469 mutex_unlock(&q
->vb_lock
);
473 int videobuf_qbuf(struct videobuf_queue
*q
,
474 struct v4l2_buffer
*b
)
476 struct videobuf_buffer
*buf
;
477 enum v4l2_field field
;
478 unsigned long flags
= 0;
481 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
483 if (b
->memory
== V4L2_MEMORY_MMAP
)
484 down_read(¤t
->mm
->mmap_sem
);
486 mutex_lock(&q
->vb_lock
);
489 dprintk(1, "qbuf: Reading running...\n");
493 if (b
->type
!= q
->type
) {
494 dprintk(1, "qbuf: Wrong type.\n");
497 if (b
->index
< 0 || b
->index
>= VIDEO_MAX_FRAME
) {
498 dprintk(1, "qbuf: index out of range.\n");
501 buf
= q
->bufs
[b
->index
];
503 dprintk(1, "qbuf: buffer is null.\n");
506 MAGIC_CHECK(buf
->magic
, MAGIC_BUFFER
);
507 if (buf
->memory
!= b
->memory
) {
508 dprintk(1, "qbuf: memory type is wrong.\n");
511 if (buf
->state
!= VIDEOBUF_NEEDS_INIT
&& buf
->state
!= VIDEOBUF_IDLE
) {
512 dprintk(1, "qbuf: buffer is already queued or active.\n");
516 if (b
->flags
& V4L2_BUF_FLAG_INPUT
) {
517 if (b
->input
>= q
->inputs
) {
518 dprintk(1, "qbuf: wrong input.\n");
521 buf
->input
= b
->input
;
527 case V4L2_MEMORY_MMAP
:
528 if (0 == buf
->baddr
) {
529 dprintk(1, "qbuf: mmap requested "
530 "but buffer addr is zero!\n");
534 case V4L2_MEMORY_USERPTR
:
535 if (b
->length
< buf
->bsize
) {
536 dprintk(1, "qbuf: buffer length is not enough\n");
539 if (VIDEOBUF_NEEDS_INIT
!= buf
->state
&&
540 buf
->baddr
!= b
->m
.userptr
)
541 q
->ops
->buf_release(q
, buf
);
542 buf
->baddr
= b
->m
.userptr
;
544 case V4L2_MEMORY_OVERLAY
:
545 buf
->boff
= b
->m
.offset
;
548 dprintk(1, "qbuf: wrong memory type\n");
552 dprintk(1, "qbuf: requesting next field\n");
553 field
= videobuf_next_field(q
);
554 retval
= q
->ops
->buf_prepare(q
, buf
, field
);
556 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval
);
560 list_add_tail(&buf
->stream
, &q
->stream
);
562 spin_lock_irqsave(q
->irqlock
, flags
);
563 q
->ops
->buf_queue(q
, buf
);
564 spin_unlock_irqrestore(q
->irqlock
, flags
);
566 dprintk(1, "qbuf: succeded\n");
568 wake_up_interruptible_sync(&q
->wait
);
571 mutex_unlock(&q
->vb_lock
);
573 if (b
->memory
== V4L2_MEMORY_MMAP
)
574 up_read(¤t
->mm
->mmap_sem
);
580 /* Locking: Caller holds q->vb_lock */
581 static int stream_next_buffer_check_queue(struct videobuf_queue
*q
, int noblock
)
587 dprintk(1, "next_buffer: Not streaming\n");
592 if (list_empty(&q
->stream
)) {
595 dprintk(2, "next_buffer: no buffers to dequeue\n");
598 dprintk(2, "next_buffer: waiting on buffer\n");
600 /* Drop lock to avoid deadlock with qbuf */
601 mutex_unlock(&q
->vb_lock
);
603 /* Checking list_empty and streaming is safe without
604 * locks because we goto checks to validate while
605 * holding locks before proceeding */
606 retval
= wait_event_interruptible(q
->wait
,
607 !list_empty(&q
->stream
) || !q
->streaming
);
608 mutex_lock(&q
->vb_lock
);
624 /* Locking: Caller holds q->vb_lock */
625 static int stream_next_buffer(struct videobuf_queue
*q
,
626 struct videobuf_buffer
**vb
, int nonblocking
)
629 struct videobuf_buffer
*buf
= NULL
;
631 retval
= stream_next_buffer_check_queue(q
, nonblocking
);
635 buf
= list_entry(q
->stream
.next
, struct videobuf_buffer
, stream
);
636 retval
= videobuf_waiton(buf
, nonblocking
, 1);
645 int videobuf_dqbuf(struct videobuf_queue
*q
,
646 struct v4l2_buffer
*b
, int nonblocking
)
648 struct videobuf_buffer
*buf
= NULL
;
651 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
653 mutex_lock(&q
->vb_lock
);
655 retval
= stream_next_buffer(q
, &buf
, nonblocking
);
657 dprintk(1, "dqbuf: next_buffer error: %i\n", retval
);
661 switch (buf
->state
) {
663 dprintk(1, "dqbuf: state is error\n");
665 CALL(q
, sync
, q
, buf
);
666 buf
->state
= VIDEOBUF_IDLE
;
669 dprintk(1, "dqbuf: state is done\n");
670 CALL(q
, sync
, q
, buf
);
671 buf
->state
= VIDEOBUF_IDLE
;
674 dprintk(1, "dqbuf: state invalid\n");
678 list_del(&buf
->stream
);
679 memset(b
, 0, sizeof(*b
));
680 videobuf_status(q
, b
, buf
, q
->type
);
683 mutex_unlock(&q
->vb_lock
);
687 int videobuf_streamon(struct videobuf_queue
*q
)
689 struct videobuf_buffer
*buf
;
690 unsigned long flags
= 0;
693 mutex_lock(&q
->vb_lock
);
701 spin_lock_irqsave(q
->irqlock
, flags
);
702 list_for_each_entry(buf
, &q
->stream
, stream
)
703 if (buf
->state
== VIDEOBUF_PREPARED
)
704 q
->ops
->buf_queue(q
, buf
);
705 spin_unlock_irqrestore(q
->irqlock
, flags
);
707 wake_up_interruptible_sync(&q
->wait
);
709 mutex_unlock(&q
->vb_lock
);
713 /* Locking: Caller holds q->vb_lock */
714 static int __videobuf_streamoff(struct videobuf_queue
*q
)
719 videobuf_queue_cancel(q
);
724 int videobuf_streamoff(struct videobuf_queue
*q
)
728 mutex_lock(&q
->vb_lock
);
729 retval
= __videobuf_streamoff(q
);
730 mutex_unlock(&q
->vb_lock
);
735 /* Locking: Caller holds q->vb_lock */
736 static ssize_t
videobuf_read_zerocopy(struct videobuf_queue
*q
,
738 size_t count
, loff_t
*ppos
)
740 enum v4l2_field field
;
741 unsigned long flags
= 0;
744 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
747 q
->read_buf
= videobuf_alloc(q
);
748 if (NULL
== q
->read_buf
)
751 q
->read_buf
->memory
= V4L2_MEMORY_USERPTR
;
752 q
->read_buf
->baddr
= (unsigned long)data
;
753 q
->read_buf
->bsize
= count
;
755 field
= videobuf_next_field(q
);
756 retval
= q
->ops
->buf_prepare(q
, q
->read_buf
, field
);
760 /* start capture & wait */
761 spin_lock_irqsave(q
->irqlock
, flags
);
762 q
->ops
->buf_queue(q
, q
->read_buf
);
763 spin_unlock_irqrestore(q
->irqlock
, flags
);
764 retval
= videobuf_waiton(q
->read_buf
, 0, 0);
766 CALL(q
, sync
, q
, q
->read_buf
);
767 if (VIDEOBUF_ERROR
== q
->read_buf
->state
)
770 retval
= q
->read_buf
->size
;
775 q
->ops
->buf_release(q
, q
->read_buf
);
781 ssize_t
videobuf_read_one(struct videobuf_queue
*q
,
782 char __user
*data
, size_t count
, loff_t
*ppos
,
785 enum v4l2_field field
;
786 unsigned long flags
= 0;
787 unsigned size
= 0, nbufs
= 1;
790 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
792 mutex_lock(&q
->vb_lock
);
794 q
->ops
->buf_setup(q
, &nbufs
, &size
);
796 if (NULL
== q
->read_buf
&&
799 retval
= videobuf_read_zerocopy(q
, data
, count
, ppos
);
800 if (retval
>= 0 || retval
== -EIO
)
803 /* fallback to kernel bounce buffer on failures */
806 if (NULL
== q
->read_buf
) {
807 /* need to capture a new frame */
809 q
->read_buf
= videobuf_alloc(q
);
811 dprintk(1, "video alloc=0x%p\n", q
->read_buf
);
812 if (NULL
== q
->read_buf
)
814 q
->read_buf
->memory
= V4L2_MEMORY_USERPTR
;
815 q
->read_buf
->bsize
= count
; /* preferred size */
816 field
= videobuf_next_field(q
);
817 retval
= q
->ops
->buf_prepare(q
, q
->read_buf
, field
);
825 spin_lock_irqsave(q
->irqlock
, flags
);
826 q
->ops
->buf_queue(q
, q
->read_buf
);
827 spin_unlock_irqrestore(q
->irqlock
, flags
);
832 /* wait until capture is done */
833 retval
= videobuf_waiton(q
->read_buf
, nonblocking
, 1);
837 CALL(q
, sync
, q
, q
->read_buf
);
839 if (VIDEOBUF_ERROR
== q
->read_buf
->state
) {
840 /* catch I/O errors */
841 q
->ops
->buf_release(q
, q
->read_buf
);
848 /* Copy to userspace */
849 retval
= CALL(q
, video_copy_to_user
, q
, data
, count
, nonblocking
);
853 q
->read_off
+= retval
;
854 if (q
->read_off
== q
->read_buf
->size
) {
855 /* all data copied, cleanup */
856 q
->ops
->buf_release(q
, q
->read_buf
);
862 mutex_unlock(&q
->vb_lock
);
866 /* Locking: Caller holds q->vb_lock */
867 static int __videobuf_read_start(struct videobuf_queue
*q
)
869 enum v4l2_field field
;
870 unsigned long flags
= 0;
871 unsigned int count
= 0, size
= 0;
874 q
->ops
->buf_setup(q
, &count
, &size
);
877 if (count
> VIDEO_MAX_FRAME
)
878 count
= VIDEO_MAX_FRAME
;
879 size
= PAGE_ALIGN(size
);
881 err
= __videobuf_mmap_setup(q
, count
, size
, V4L2_MEMORY_USERPTR
);
887 for (i
= 0; i
< count
; i
++) {
888 field
= videobuf_next_field(q
);
889 err
= q
->ops
->buf_prepare(q
, q
->bufs
[i
], field
);
892 list_add_tail(&q
->bufs
[i
]->stream
, &q
->stream
);
894 spin_lock_irqsave(q
->irqlock
, flags
);
895 for (i
= 0; i
< count
; i
++)
896 q
->ops
->buf_queue(q
, q
->bufs
[i
]);
897 spin_unlock_irqrestore(q
->irqlock
, flags
);
902 static void __videobuf_read_stop(struct videobuf_queue
*q
)
906 videobuf_queue_cancel(q
);
907 __videobuf_mmap_free(q
);
908 INIT_LIST_HEAD(&q
->stream
);
909 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
910 if (NULL
== q
->bufs
[i
])
919 int videobuf_read_start(struct videobuf_queue
*q
)
923 mutex_lock(&q
->vb_lock
);
924 rc
= __videobuf_read_start(q
);
925 mutex_unlock(&q
->vb_lock
);
930 void videobuf_read_stop(struct videobuf_queue
*q
)
932 mutex_lock(&q
->vb_lock
);
933 __videobuf_read_stop(q
);
934 mutex_unlock(&q
->vb_lock
);
937 void videobuf_stop(struct videobuf_queue
*q
)
939 mutex_lock(&q
->vb_lock
);
942 __videobuf_streamoff(q
);
945 __videobuf_read_stop(q
);
947 mutex_unlock(&q
->vb_lock
);
951 ssize_t
videobuf_read_stream(struct videobuf_queue
*q
,
952 char __user
*data
, size_t count
, loff_t
*ppos
,
953 int vbihack
, int nonblocking
)
956 unsigned long flags
= 0;
958 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
960 dprintk(2, "%s\n", __func__
);
961 mutex_lock(&q
->vb_lock
);
966 retval
= __videobuf_read_start(q
);
973 /* get / wait for data */
974 if (NULL
== q
->read_buf
) {
975 q
->read_buf
= list_entry(q
->stream
.next
,
976 struct videobuf_buffer
,
978 list_del(&q
->read_buf
->stream
);
981 rc
= videobuf_waiton(q
->read_buf
, nonblocking
, 1);
988 if (q
->read_buf
->state
== VIDEOBUF_DONE
) {
989 rc
= CALL(q
, copy_stream
, q
, data
+ retval
, count
,
990 retval
, vbihack
, nonblocking
);
1000 q
->read_off
= q
->read_buf
->size
;
1005 /* requeue buffer when done with copying */
1006 if (q
->read_off
== q
->read_buf
->size
) {
1007 list_add_tail(&q
->read_buf
->stream
,
1009 spin_lock_irqsave(q
->irqlock
, flags
);
1010 q
->ops
->buf_queue(q
, q
->read_buf
);
1011 spin_unlock_irqrestore(q
->irqlock
, flags
);
1019 mutex_unlock(&q
->vb_lock
);
1023 unsigned int videobuf_poll_stream(struct file
*file
,
1024 struct videobuf_queue
*q
,
1027 struct videobuf_buffer
*buf
= NULL
;
1028 unsigned int rc
= 0;
1030 mutex_lock(&q
->vb_lock
);
1032 if (!list_empty(&q
->stream
))
1033 buf
= list_entry(q
->stream
.next
,
1034 struct videobuf_buffer
, stream
);
1037 __videobuf_read_start(q
);
1040 } else if (NULL
== q
->read_buf
) {
1041 q
->read_buf
= list_entry(q
->stream
.next
,
1042 struct videobuf_buffer
,
1044 list_del(&q
->read_buf
->stream
);
1053 poll_wait(file
, &buf
->done
, wait
);
1054 if (buf
->state
== VIDEOBUF_DONE
||
1055 buf
->state
== VIDEOBUF_ERROR
)
1056 rc
= POLLIN
|POLLRDNORM
;
1058 mutex_unlock(&q
->vb_lock
);
1062 int videobuf_mmap_mapper(struct videobuf_queue
*q
,
1063 struct vm_area_struct
*vma
)
1067 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
1069 mutex_lock(&q
->vb_lock
);
1070 retval
= CALL(q
, mmap_mapper
, q
, vma
);
1072 mutex_unlock(&q
->vb_lock
);
1077 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1078 int videobuf_cgmbuf(struct videobuf_queue
*q
,
1079 struct video_mbuf
*mbuf
, int count
)
1081 struct v4l2_requestbuffers req
;
1084 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
1086 memset(&req
, 0, sizeof(req
));
1089 req
.memory
= V4L2_MEMORY_MMAP
;
1090 rc
= videobuf_reqbufs(q
, &req
);
1094 mbuf
->frames
= req
.count
;
1096 for (i
= 0; i
< mbuf
->frames
; i
++) {
1097 mbuf
->offsets
[i
] = q
->bufs
[i
]->boff
;
1098 mbuf
->size
+= q
->bufs
[i
]->bsize
;
1103 EXPORT_SYMBOL_GPL(videobuf_cgmbuf
);
1106 /* --------------------------------------------------------------------- */
1108 EXPORT_SYMBOL_GPL(videobuf_waiton
);
1109 EXPORT_SYMBOL_GPL(videobuf_iolock
);
1111 EXPORT_SYMBOL_GPL(videobuf_alloc
);
1113 EXPORT_SYMBOL_GPL(videobuf_queue_core_init
);
1114 EXPORT_SYMBOL_GPL(videobuf_queue_cancel
);
1115 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy
);
1117 EXPORT_SYMBOL_GPL(videobuf_next_field
);
1118 EXPORT_SYMBOL_GPL(videobuf_reqbufs
);
1119 EXPORT_SYMBOL_GPL(videobuf_querybuf
);
1120 EXPORT_SYMBOL_GPL(videobuf_qbuf
);
1121 EXPORT_SYMBOL_GPL(videobuf_dqbuf
);
1122 EXPORT_SYMBOL_GPL(videobuf_streamon
);
1123 EXPORT_SYMBOL_GPL(videobuf_streamoff
);
1125 EXPORT_SYMBOL_GPL(videobuf_read_start
);
1126 EXPORT_SYMBOL_GPL(videobuf_read_stop
);
1127 EXPORT_SYMBOL_GPL(videobuf_stop
);
1128 EXPORT_SYMBOL_GPL(videobuf_read_stream
);
1129 EXPORT_SYMBOL_GPL(videobuf_read_one
);
1130 EXPORT_SYMBOL_GPL(videobuf_poll_stream
);
1132 EXPORT_SYMBOL_GPL(videobuf_mmap_setup
);
1133 EXPORT_SYMBOL_GPL(videobuf_mmap_free
);
1134 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper
);