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)
54 #define CALLPTR(q, f, arg...) \
55 ((q->int_ops->f) ? q->int_ops->f(arg) : NULL)
57 struct videobuf_buffer
*videobuf_alloc_vb(struct videobuf_queue
*q
)
59 struct videobuf_buffer
*vb
;
61 BUG_ON(q
->msize
< sizeof(*vb
));
63 if (!q
->int_ops
|| !q
->int_ops
->alloc_vb
) {
64 printk(KERN_ERR
"No specific ops defined!\n");
68 vb
= q
->int_ops
->alloc_vb(q
->msize
);
70 init_waitqueue_head(&vb
->done
);
71 vb
->magic
= MAGIC_BUFFER
;
76 EXPORT_SYMBOL_GPL(videobuf_alloc_vb
);
78 static int is_state_active_or_queued(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
83 spin_lock_irqsave(q
->irqlock
, flags
);
84 rc
= vb
->state
!= VIDEOBUF_ACTIVE
&& vb
->state
!= VIDEOBUF_QUEUED
;
85 spin_unlock_irqrestore(q
->irqlock
, flags
);
89 int videobuf_waiton(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
90 int non_blocking
, int intr
)
95 MAGIC_CHECK(vb
->magic
, MAGIC_BUFFER
);
98 if (is_state_active_or_queued(q
, vb
))
103 is_ext_locked
= q
->ext_lock
&& mutex_is_locked(q
->ext_lock
);
105 /* Release vdev lock to prevent this wait from blocking outside access to
108 mutex_unlock(q
->ext_lock
);
110 ret
= wait_event_interruptible(vb
->done
, is_state_active_or_queued(q
, vb
));
112 wait_event(vb
->done
, is_state_active_or_queued(q
, vb
));
115 mutex_lock(q
->ext_lock
);
119 EXPORT_SYMBOL_GPL(videobuf_waiton
);
121 int videobuf_iolock(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
122 struct v4l2_framebuffer
*fbuf
)
124 MAGIC_CHECK(vb
->magic
, MAGIC_BUFFER
);
125 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
127 return CALL(q
, iolock
, q
, vb
, fbuf
);
129 EXPORT_SYMBOL_GPL(videobuf_iolock
);
131 void *videobuf_queue_to_vaddr(struct videobuf_queue
*q
,
132 struct videobuf_buffer
*buf
)
134 if (q
->int_ops
->vaddr
)
135 return q
->int_ops
->vaddr(buf
);
138 EXPORT_SYMBOL_GPL(videobuf_queue_to_vaddr
);
140 /* --------------------------------------------------------------------- */
143 void videobuf_queue_core_init(struct videobuf_queue
*q
,
144 const struct videobuf_queue_ops
*ops
,
147 enum v4l2_buf_type type
,
148 enum v4l2_field field
,
151 struct videobuf_qtype_ops
*int_ops
,
152 struct mutex
*ext_lock
)
155 memset(q
, 0, sizeof(*q
));
156 q
->irqlock
= irqlock
;
157 q
->ext_lock
= ext_lock
;
164 q
->int_ops
= int_ops
;
166 /* All buffer operations are mandatory */
167 BUG_ON(!q
->ops
->buf_setup
);
168 BUG_ON(!q
->ops
->buf_prepare
);
169 BUG_ON(!q
->ops
->buf_queue
);
170 BUG_ON(!q
->ops
->buf_release
);
172 /* Lock is mandatory for queue_cancel to work */
175 /* Having implementations for abstract methods are mandatory */
178 mutex_init(&q
->vb_lock
);
179 init_waitqueue_head(&q
->wait
);
180 INIT_LIST_HEAD(&q
->stream
);
182 EXPORT_SYMBOL_GPL(videobuf_queue_core_init
);
184 /* Locking: Only usage in bttv unsafe find way to remove */
185 int videobuf_queue_is_busy(struct videobuf_queue
*q
)
189 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
192 dprintk(1, "busy: streaming active\n");
196 dprintk(1, "busy: pending read #1\n");
200 dprintk(1, "busy: pending read #2\n");
203 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
204 if (NULL
== q
->bufs
[i
])
206 if (q
->bufs
[i
]->map
) {
207 dprintk(1, "busy: buffer #%d mapped\n", i
);
210 if (q
->bufs
[i
]->state
== VIDEOBUF_QUEUED
) {
211 dprintk(1, "busy: buffer #%d queued\n", i
);
214 if (q
->bufs
[i
]->state
== VIDEOBUF_ACTIVE
) {
215 dprintk(1, "busy: buffer #%d avtive\n", i
);
221 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy
);
224 * __videobuf_free() - free all the buffers and their control structures
226 * This function can only be called if streaming/reading is off, i.e. no buffers
227 * are under control of the driver.
229 /* Locking: Caller holds q->vb_lock */
230 static int __videobuf_free(struct videobuf_queue
*q
)
234 dprintk(1, "%s\n", __func__
);
238 if (q
->streaming
|| q
->reading
) {
239 dprintk(1, "Cannot free buffers when streaming or reading\n");
243 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
245 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++)
246 if (q
->bufs
[i
] && q
->bufs
[i
]->map
) {
247 dprintk(1, "Cannot free mmapped buffers\n");
251 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
252 if (NULL
== q
->bufs
[i
])
254 q
->ops
->buf_release(q
, q
->bufs
[i
]);
262 /* Locking: Caller holds q->vb_lock */
263 void videobuf_queue_cancel(struct videobuf_queue
*q
)
265 unsigned long flags
= 0;
270 wake_up_interruptible_sync(&q
->wait
);
272 /* remove queued buffers from list */
273 spin_lock_irqsave(q
->irqlock
, flags
);
274 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
275 if (NULL
== q
->bufs
[i
])
277 if (q
->bufs
[i
]->state
== VIDEOBUF_QUEUED
) {
278 list_del(&q
->bufs
[i
]->queue
);
279 q
->bufs
[i
]->state
= VIDEOBUF_ERROR
;
280 wake_up_all(&q
->bufs
[i
]->done
);
283 spin_unlock_irqrestore(q
->irqlock
, flags
);
285 /* free all buffers + clear queue */
286 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
287 if (NULL
== q
->bufs
[i
])
289 q
->ops
->buf_release(q
, q
->bufs
[i
]);
291 INIT_LIST_HEAD(&q
->stream
);
293 EXPORT_SYMBOL_GPL(videobuf_queue_cancel
);
295 /* --------------------------------------------------------------------- */
297 /* Locking: Caller holds q->vb_lock */
298 enum v4l2_field
videobuf_next_field(struct videobuf_queue
*q
)
300 enum v4l2_field field
= q
->field
;
302 BUG_ON(V4L2_FIELD_ANY
== field
);
304 if (V4L2_FIELD_ALTERNATE
== field
) {
305 if (V4L2_FIELD_TOP
== q
->last
) {
306 field
= V4L2_FIELD_BOTTOM
;
307 q
->last
= V4L2_FIELD_BOTTOM
;
309 field
= V4L2_FIELD_TOP
;
310 q
->last
= V4L2_FIELD_TOP
;
315 EXPORT_SYMBOL_GPL(videobuf_next_field
);
317 /* Locking: Caller holds q->vb_lock */
318 static void videobuf_status(struct videobuf_queue
*q
, struct v4l2_buffer
*b
,
319 struct videobuf_buffer
*vb
, enum v4l2_buf_type type
)
321 MAGIC_CHECK(vb
->magic
, MAGIC_BUFFER
);
322 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
327 b
->memory
= vb
->memory
;
329 case V4L2_MEMORY_MMAP
:
330 b
->m
.offset
= vb
->boff
;
331 b
->length
= vb
->bsize
;
333 case V4L2_MEMORY_USERPTR
:
334 b
->m
.userptr
= vb
->baddr
;
335 b
->length
= vb
->bsize
;
337 case V4L2_MEMORY_OVERLAY
:
338 b
->m
.offset
= vb
->boff
;
340 case V4L2_MEMORY_DMABUF
:
341 /* DMABUF is not handled in videobuf framework */
345 b
->flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
347 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
350 case VIDEOBUF_PREPARED
:
351 case VIDEOBUF_QUEUED
:
352 case VIDEOBUF_ACTIVE
:
353 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
356 b
->flags
|= V4L2_BUF_FLAG_ERROR
;
359 b
->flags
|= V4L2_BUF_FLAG_DONE
;
361 case VIDEOBUF_NEEDS_INIT
:
367 b
->field
= vb
->field
;
368 b
->timestamp
= vb
->ts
;
369 b
->bytesused
= vb
->size
;
370 b
->sequence
= vb
->field_count
>> 1;
373 int videobuf_mmap_free(struct videobuf_queue
*q
)
376 videobuf_queue_lock(q
);
377 ret
= __videobuf_free(q
);
378 videobuf_queue_unlock(q
);
381 EXPORT_SYMBOL_GPL(videobuf_mmap_free
);
383 /* Locking: Caller holds q->vb_lock */
384 int __videobuf_mmap_setup(struct videobuf_queue
*q
,
385 unsigned int bcount
, unsigned int bsize
,
386 enum v4l2_memory memory
)
391 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
393 err
= __videobuf_free(q
);
397 /* Allocate and initialize buffers */
398 for (i
= 0; i
< bcount
; i
++) {
399 q
->bufs
[i
] = videobuf_alloc_vb(q
);
401 if (NULL
== q
->bufs
[i
])
405 q
->bufs
[i
]->memory
= memory
;
406 q
->bufs
[i
]->bsize
= bsize
;
408 case V4L2_MEMORY_MMAP
:
409 q
->bufs
[i
]->boff
= PAGE_ALIGN(bsize
) * i
;
411 case V4L2_MEMORY_USERPTR
:
412 case V4L2_MEMORY_OVERLAY
:
413 case V4L2_MEMORY_DMABUF
:
422 dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i
, bsize
);
426 EXPORT_SYMBOL_GPL(__videobuf_mmap_setup
);
428 int videobuf_mmap_setup(struct videobuf_queue
*q
,
429 unsigned int bcount
, unsigned int bsize
,
430 enum v4l2_memory memory
)
433 videobuf_queue_lock(q
);
434 ret
= __videobuf_mmap_setup(q
, bcount
, bsize
, memory
);
435 videobuf_queue_unlock(q
);
438 EXPORT_SYMBOL_GPL(videobuf_mmap_setup
);
440 int videobuf_reqbufs(struct videobuf_queue
*q
,
441 struct v4l2_requestbuffers
*req
)
443 unsigned int size
, count
;
446 if (req
->memory
!= V4L2_MEMORY_MMAP
&&
447 req
->memory
!= V4L2_MEMORY_USERPTR
&&
448 req
->memory
!= V4L2_MEMORY_OVERLAY
) {
449 dprintk(1, "reqbufs: memory type invalid\n");
453 videobuf_queue_lock(q
);
454 if (req
->type
!= q
->type
) {
455 dprintk(1, "reqbufs: queue type invalid\n");
461 dprintk(1, "reqbufs: streaming already exists\n");
465 if (!list_empty(&q
->stream
)) {
466 dprintk(1, "reqbufs: stream running\n");
471 if (req
->count
== 0) {
472 dprintk(1, "reqbufs: count invalid (%d)\n", req
->count
);
473 retval
= __videobuf_free(q
);
478 if (count
> VIDEO_MAX_FRAME
)
479 count
= VIDEO_MAX_FRAME
;
481 q
->ops
->buf_setup(q
, &count
, &size
);
482 dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
484 (unsigned int)((count
* PAGE_ALIGN(size
)) >> PAGE_SHIFT
));
486 retval
= __videobuf_mmap_setup(q
, count
, size
, req
->memory
);
488 dprintk(1, "reqbufs: mmap setup returned %d\n", retval
);
496 videobuf_queue_unlock(q
);
499 EXPORT_SYMBOL_GPL(videobuf_reqbufs
);
501 int videobuf_querybuf(struct videobuf_queue
*q
, struct v4l2_buffer
*b
)
505 videobuf_queue_lock(q
);
506 if (unlikely(b
->type
!= q
->type
)) {
507 dprintk(1, "querybuf: Wrong type.\n");
510 if (unlikely(b
->index
>= VIDEO_MAX_FRAME
)) {
511 dprintk(1, "querybuf: index out of range.\n");
514 if (unlikely(NULL
== q
->bufs
[b
->index
])) {
515 dprintk(1, "querybuf: buffer is null.\n");
519 videobuf_status(q
, b
, q
->bufs
[b
->index
], q
->type
);
523 videobuf_queue_unlock(q
);
526 EXPORT_SYMBOL_GPL(videobuf_querybuf
);
528 int videobuf_qbuf(struct videobuf_queue
*q
, struct v4l2_buffer
*b
)
530 struct videobuf_buffer
*buf
;
531 enum v4l2_field field
;
532 unsigned long flags
= 0;
535 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
537 if (b
->memory
== V4L2_MEMORY_MMAP
)
538 down_read(¤t
->mm
->mmap_sem
);
540 videobuf_queue_lock(q
);
543 dprintk(1, "qbuf: Reading running...\n");
547 if (b
->type
!= q
->type
) {
548 dprintk(1, "qbuf: Wrong type.\n");
551 if (b
->index
>= VIDEO_MAX_FRAME
) {
552 dprintk(1, "qbuf: index out of range.\n");
555 buf
= q
->bufs
[b
->index
];
557 dprintk(1, "qbuf: buffer is null.\n");
560 MAGIC_CHECK(buf
->magic
, MAGIC_BUFFER
);
561 if (buf
->memory
!= b
->memory
) {
562 dprintk(1, "qbuf: memory type is wrong.\n");
565 if (buf
->state
!= VIDEOBUF_NEEDS_INIT
&& buf
->state
!= VIDEOBUF_IDLE
) {
566 dprintk(1, "qbuf: buffer is already queued or active.\n");
571 case V4L2_MEMORY_MMAP
:
572 if (0 == buf
->baddr
) {
573 dprintk(1, "qbuf: mmap requested "
574 "but buffer addr is zero!\n");
577 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT
578 || q
->type
== V4L2_BUF_TYPE_VBI_OUTPUT
579 || q
->type
== V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
580 || q
->type
== V4L2_BUF_TYPE_SDR_OUTPUT
) {
581 buf
->size
= b
->bytesused
;
582 buf
->field
= b
->field
;
583 buf
->ts
= b
->timestamp
;
586 case V4L2_MEMORY_USERPTR
:
587 if (b
->length
< buf
->bsize
) {
588 dprintk(1, "qbuf: buffer length is not enough\n");
591 if (VIDEOBUF_NEEDS_INIT
!= buf
->state
&&
592 buf
->baddr
!= b
->m
.userptr
)
593 q
->ops
->buf_release(q
, buf
);
594 buf
->baddr
= b
->m
.userptr
;
596 case V4L2_MEMORY_OVERLAY
:
597 buf
->boff
= b
->m
.offset
;
600 dprintk(1, "qbuf: wrong memory type\n");
604 dprintk(1, "qbuf: requesting next field\n");
605 field
= videobuf_next_field(q
);
606 retval
= q
->ops
->buf_prepare(q
, buf
, field
);
608 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval
);
612 list_add_tail(&buf
->stream
, &q
->stream
);
614 spin_lock_irqsave(q
->irqlock
, flags
);
615 q
->ops
->buf_queue(q
, buf
);
616 spin_unlock_irqrestore(q
->irqlock
, flags
);
618 dprintk(1, "qbuf: succeeded\n");
620 wake_up_interruptible_sync(&q
->wait
);
623 videobuf_queue_unlock(q
);
625 if (b
->memory
== V4L2_MEMORY_MMAP
)
626 up_read(¤t
->mm
->mmap_sem
);
630 EXPORT_SYMBOL_GPL(videobuf_qbuf
);
632 /* Locking: Caller holds q->vb_lock */
633 static int stream_next_buffer_check_queue(struct videobuf_queue
*q
, int noblock
)
639 dprintk(1, "next_buffer: Not streaming\n");
644 if (list_empty(&q
->stream
)) {
647 dprintk(2, "next_buffer: no buffers to dequeue\n");
650 dprintk(2, "next_buffer: waiting on buffer\n");
652 /* Drop lock to avoid deadlock with qbuf */
653 videobuf_queue_unlock(q
);
655 /* Checking list_empty and streaming is safe without
656 * locks because we goto checks to validate while
657 * holding locks before proceeding */
658 retval
= wait_event_interruptible(q
->wait
,
659 !list_empty(&q
->stream
) || !q
->streaming
);
660 videobuf_queue_lock(q
);
675 /* Locking: Caller holds q->vb_lock */
676 static int stream_next_buffer(struct videobuf_queue
*q
,
677 struct videobuf_buffer
**vb
, int nonblocking
)
680 struct videobuf_buffer
*buf
= NULL
;
682 retval
= stream_next_buffer_check_queue(q
, nonblocking
);
686 buf
= list_entry(q
->stream
.next
, struct videobuf_buffer
, stream
);
687 retval
= videobuf_waiton(q
, buf
, nonblocking
, 1);
696 int videobuf_dqbuf(struct videobuf_queue
*q
,
697 struct v4l2_buffer
*b
, int nonblocking
)
699 struct videobuf_buffer
*buf
= NULL
;
702 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
704 memset(b
, 0, sizeof(*b
));
705 videobuf_queue_lock(q
);
707 retval
= stream_next_buffer(q
, &buf
, nonblocking
);
709 dprintk(1, "dqbuf: next_buffer error: %i\n", retval
);
713 switch (buf
->state
) {
715 dprintk(1, "dqbuf: state is error\n");
718 dprintk(1, "dqbuf: state is done\n");
721 dprintk(1, "dqbuf: state invalid\n");
725 CALL(q
, sync
, q
, buf
);
726 videobuf_status(q
, b
, buf
, q
->type
);
727 list_del(&buf
->stream
);
728 buf
->state
= VIDEOBUF_IDLE
;
729 b
->flags
&= ~V4L2_BUF_FLAG_DONE
;
731 videobuf_queue_unlock(q
);
734 EXPORT_SYMBOL_GPL(videobuf_dqbuf
);
736 int videobuf_streamon(struct videobuf_queue
*q
)
738 struct videobuf_buffer
*buf
;
739 unsigned long flags
= 0;
742 videobuf_queue_lock(q
);
750 spin_lock_irqsave(q
->irqlock
, flags
);
751 list_for_each_entry(buf
, &q
->stream
, stream
)
752 if (buf
->state
== VIDEOBUF_PREPARED
)
753 q
->ops
->buf_queue(q
, buf
);
754 spin_unlock_irqrestore(q
->irqlock
, flags
);
756 wake_up_interruptible_sync(&q
->wait
);
758 videobuf_queue_unlock(q
);
761 EXPORT_SYMBOL_GPL(videobuf_streamon
);
763 /* Locking: Caller holds q->vb_lock */
764 static int __videobuf_streamoff(struct videobuf_queue
*q
)
769 videobuf_queue_cancel(q
);
774 int videobuf_streamoff(struct videobuf_queue
*q
)
778 videobuf_queue_lock(q
);
779 retval
= __videobuf_streamoff(q
);
780 videobuf_queue_unlock(q
);
784 EXPORT_SYMBOL_GPL(videobuf_streamoff
);
786 /* Locking: Caller holds q->vb_lock */
787 static ssize_t
videobuf_read_zerocopy(struct videobuf_queue
*q
,
789 size_t count
, loff_t
*ppos
)
791 enum v4l2_field field
;
792 unsigned long flags
= 0;
795 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
798 q
->read_buf
= videobuf_alloc_vb(q
);
799 if (NULL
== q
->read_buf
)
802 q
->read_buf
->memory
= V4L2_MEMORY_USERPTR
;
803 q
->read_buf
->baddr
= (unsigned long)data
;
804 q
->read_buf
->bsize
= count
;
806 field
= videobuf_next_field(q
);
807 retval
= q
->ops
->buf_prepare(q
, q
->read_buf
, field
);
811 /* start capture & wait */
812 spin_lock_irqsave(q
->irqlock
, flags
);
813 q
->ops
->buf_queue(q
, q
->read_buf
);
814 spin_unlock_irqrestore(q
->irqlock
, flags
);
815 retval
= videobuf_waiton(q
, q
->read_buf
, 0, 0);
817 CALL(q
, sync
, q
, q
->read_buf
);
818 if (VIDEOBUF_ERROR
== q
->read_buf
->state
)
821 retval
= q
->read_buf
->size
;
826 q
->ops
->buf_release(q
, q
->read_buf
);
832 static int __videobuf_copy_to_user(struct videobuf_queue
*q
,
833 struct videobuf_buffer
*buf
,
834 char __user
*data
, size_t count
,
837 void *vaddr
= CALLPTR(q
, vaddr
, buf
);
839 /* copy to userspace */
840 if (count
> buf
->size
- q
->read_off
)
841 count
= buf
->size
- q
->read_off
;
843 if (copy_to_user(data
, vaddr
+ q
->read_off
, count
))
849 static int __videobuf_copy_stream(struct videobuf_queue
*q
,
850 struct videobuf_buffer
*buf
,
851 char __user
*data
, size_t count
, size_t pos
,
852 int vbihack
, int nonblocking
)
854 unsigned int *fc
= CALLPTR(q
, vaddr
, buf
);
857 /* dirty, undocumented hack -- pass the frame counter
858 * within the last four bytes of each vbi data block.
859 * We need that one to maintain backward compatibility
860 * to all vbi decoding software out there ... */
861 fc
+= (buf
->size
>> 2) - 1;
862 *fc
= buf
->field_count
>> 1;
863 dprintk(1, "vbihack: %d\n", *fc
);
866 /* copy stuff using the common method */
867 count
= __videobuf_copy_to_user(q
, buf
, data
, count
, nonblocking
);
869 if ((count
== -EFAULT
) && (pos
== 0))
875 ssize_t
videobuf_read_one(struct videobuf_queue
*q
,
876 char __user
*data
, size_t count
, loff_t
*ppos
,
879 enum v4l2_field field
;
880 unsigned long flags
= 0;
881 unsigned size
= 0, nbufs
= 1;
884 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
886 videobuf_queue_lock(q
);
888 q
->ops
->buf_setup(q
, &nbufs
, &size
);
890 if (NULL
== q
->read_buf
&&
893 retval
= videobuf_read_zerocopy(q
, data
, count
, ppos
);
894 if (retval
>= 0 || retval
== -EIO
)
897 /* fallback to kernel bounce buffer on failures */
900 if (NULL
== q
->read_buf
) {
901 /* need to capture a new frame */
903 q
->read_buf
= videobuf_alloc_vb(q
);
905 dprintk(1, "video alloc=0x%p\n", q
->read_buf
);
906 if (NULL
== q
->read_buf
)
908 q
->read_buf
->memory
= V4L2_MEMORY_USERPTR
;
909 q
->read_buf
->bsize
= count
; /* preferred size */
910 field
= videobuf_next_field(q
);
911 retval
= q
->ops
->buf_prepare(q
, q
->read_buf
, field
);
919 spin_lock_irqsave(q
->irqlock
, flags
);
920 q
->ops
->buf_queue(q
, q
->read_buf
);
921 spin_unlock_irqrestore(q
->irqlock
, flags
);
926 /* wait until capture is done */
927 retval
= videobuf_waiton(q
, q
->read_buf
, nonblocking
, 1);
931 CALL(q
, sync
, q
, q
->read_buf
);
933 if (VIDEOBUF_ERROR
== q
->read_buf
->state
) {
934 /* catch I/O errors */
935 q
->ops
->buf_release(q
, q
->read_buf
);
942 /* Copy to userspace */
943 retval
= __videobuf_copy_to_user(q
, q
->read_buf
, data
, count
, nonblocking
);
947 q
->read_off
+= retval
;
948 if (q
->read_off
== q
->read_buf
->size
) {
949 /* all data copied, cleanup */
950 q
->ops
->buf_release(q
, q
->read_buf
);
956 videobuf_queue_unlock(q
);
959 EXPORT_SYMBOL_GPL(videobuf_read_one
);
961 /* Locking: Caller holds q->vb_lock */
962 static int __videobuf_read_start(struct videobuf_queue
*q
)
964 enum v4l2_field field
;
965 unsigned long flags
= 0;
966 unsigned int count
= 0, size
= 0;
969 q
->ops
->buf_setup(q
, &count
, &size
);
972 if (count
> VIDEO_MAX_FRAME
)
973 count
= VIDEO_MAX_FRAME
;
974 size
= PAGE_ALIGN(size
);
976 err
= __videobuf_mmap_setup(q
, count
, size
, V4L2_MEMORY_USERPTR
);
982 for (i
= 0; i
< count
; i
++) {
983 field
= videobuf_next_field(q
);
984 err
= q
->ops
->buf_prepare(q
, q
->bufs
[i
], field
);
987 list_add_tail(&q
->bufs
[i
]->stream
, &q
->stream
);
989 spin_lock_irqsave(q
->irqlock
, flags
);
990 for (i
= 0; i
< count
; i
++)
991 q
->ops
->buf_queue(q
, q
->bufs
[i
]);
992 spin_unlock_irqrestore(q
->irqlock
, flags
);
997 static void __videobuf_read_stop(struct videobuf_queue
*q
)
1001 videobuf_queue_cancel(q
);
1003 INIT_LIST_HEAD(&q
->stream
);
1004 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
1005 if (NULL
== q
->bufs
[i
])
1013 int videobuf_read_start(struct videobuf_queue
*q
)
1017 videobuf_queue_lock(q
);
1018 rc
= __videobuf_read_start(q
);
1019 videobuf_queue_unlock(q
);
1023 EXPORT_SYMBOL_GPL(videobuf_read_start
);
1025 void videobuf_read_stop(struct videobuf_queue
*q
)
1027 videobuf_queue_lock(q
);
1028 __videobuf_read_stop(q
);
1029 videobuf_queue_unlock(q
);
1031 EXPORT_SYMBOL_GPL(videobuf_read_stop
);
1033 void videobuf_stop(struct videobuf_queue
*q
)
1035 videobuf_queue_lock(q
);
1038 __videobuf_streamoff(q
);
1041 __videobuf_read_stop(q
);
1043 videobuf_queue_unlock(q
);
1045 EXPORT_SYMBOL_GPL(videobuf_stop
);
1047 ssize_t
videobuf_read_stream(struct videobuf_queue
*q
,
1048 char __user
*data
, size_t count
, loff_t
*ppos
,
1049 int vbihack
, int nonblocking
)
1052 unsigned long flags
= 0;
1054 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
1056 dprintk(2, "%s\n", __func__
);
1057 videobuf_queue_lock(q
);
1062 retval
= __videobuf_read_start(q
);
1069 /* get / wait for data */
1070 if (NULL
== q
->read_buf
) {
1071 q
->read_buf
= list_entry(q
->stream
.next
,
1072 struct videobuf_buffer
,
1074 list_del(&q
->read_buf
->stream
);
1077 rc
= videobuf_waiton(q
, q
->read_buf
, nonblocking
, 1);
1084 if (q
->read_buf
->state
== VIDEOBUF_DONE
) {
1085 rc
= __videobuf_copy_stream(q
, q
->read_buf
, data
+ retval
, count
,
1086 retval
, vbihack
, nonblocking
);
1096 q
->read_off
= q
->read_buf
->size
;
1101 /* requeue buffer when done with copying */
1102 if (q
->read_off
== q
->read_buf
->size
) {
1103 list_add_tail(&q
->read_buf
->stream
,
1105 spin_lock_irqsave(q
->irqlock
, flags
);
1106 q
->ops
->buf_queue(q
, q
->read_buf
);
1107 spin_unlock_irqrestore(q
->irqlock
, flags
);
1115 videobuf_queue_unlock(q
);
1118 EXPORT_SYMBOL_GPL(videobuf_read_stream
);
1120 unsigned int videobuf_poll_stream(struct file
*file
,
1121 struct videobuf_queue
*q
,
1124 unsigned long req_events
= poll_requested_events(wait
);
1125 struct videobuf_buffer
*buf
= NULL
;
1126 unsigned int rc
= 0;
1128 videobuf_queue_lock(q
);
1130 if (!list_empty(&q
->stream
))
1131 buf
= list_entry(q
->stream
.next
,
1132 struct videobuf_buffer
, stream
);
1133 } else if (req_events
& (POLLIN
| POLLRDNORM
)) {
1135 __videobuf_read_start(q
);
1138 } else if (NULL
== q
->read_buf
) {
1139 q
->read_buf
= list_entry(q
->stream
.next
,
1140 struct videobuf_buffer
,
1142 list_del(&q
->read_buf
->stream
);
1151 poll_wait(file
, &buf
->done
, wait
);
1152 if (buf
->state
== VIDEOBUF_DONE
||
1153 buf
->state
== VIDEOBUF_ERROR
) {
1155 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
1156 case V4L2_BUF_TYPE_VBI_OUTPUT
:
1157 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT
:
1158 case V4L2_BUF_TYPE_SDR_OUTPUT
:
1159 rc
= POLLOUT
| POLLWRNORM
;
1162 rc
= POLLIN
| POLLRDNORM
;
1167 videobuf_queue_unlock(q
);
1170 EXPORT_SYMBOL_GPL(videobuf_poll_stream
);
1172 int videobuf_mmap_mapper(struct videobuf_queue
*q
, struct vm_area_struct
*vma
)
1177 MAGIC_CHECK(q
->int_ops
->magic
, MAGIC_QTYPE_OPS
);
1179 if (!(vma
->vm_flags
& VM_WRITE
) || !(vma
->vm_flags
& VM_SHARED
)) {
1180 dprintk(1, "mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
1184 videobuf_queue_lock(q
);
1185 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
1186 struct videobuf_buffer
*buf
= q
->bufs
[i
];
1188 if (buf
&& buf
->memory
== V4L2_MEMORY_MMAP
&&
1189 buf
->boff
== (vma
->vm_pgoff
<< PAGE_SHIFT
)) {
1190 rc
= CALL(q
, mmap_mapper
, q
, buf
, vma
);
1194 videobuf_queue_unlock(q
);
1198 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper
);