17 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
26 #define MAGIC_BUFFER 0x20070728
27 #define MAGIC_CHECK(is, should) \
29 if (unlikely((is) != (should))) { \
31 "magic mismatch: %x (expected %x)\n", \
44 #define dprintk(level, fmt, arg...) \
47 printk(KERN_DEBUG "vbuf: " fmt, ## arg); \
52 #define CALL(q, f, arg...) \
53 ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
83 spin_unlock_irqrestore(q->
irqlock, flags);
96 if (is_state_active_or_queued(q, vb))
125 return CALL(q, iolock, q, vb, fbuf);
150 struct mutex *ext_lock)
178 INIT_LIST_HEAD(&q->
stream);
190 dprintk(1,
"busy: streaming active\n");
194 dprintk(1,
"busy: pending read #1\n");
198 dprintk(1,
"busy: pending read #2\n");
204 if (q->
bufs[i]->map) {
205 dprintk(1,
"busy: buffer #%d mapped\n", i);
209 dprintk(1,
"busy: buffer #%d queued\n", i);
213 dprintk(1,
"busy: buffer #%d avtive\n", i);
237 dprintk(1,
"Cannot free buffers when streaming or reading\n");
244 if (q->
bufs[i] && q->
bufs[i]->map) {
245 dprintk(1,
"Cannot free mmapped buffers\n");
252 q->
ops->buf_release(q, q->
bufs[i]);
263 unsigned long flags = 0;
281 spin_unlock_irqrestore(q->
irqlock, flags);
287 q->
ops->buf_release(q, q->
bufs[i]);
289 INIT_LIST_HEAD(&q->
stream);
371 videobuf_queue_lock(q);
372 ret = __videobuf_free(q);
373 videobuf_queue_unlock(q);
388 err = __videobuf_free(q);
393 for (i = 0; i <
bcount; i++) {
416 dprintk(1,
"mmap setup: %d buffers, %d bytes each\n", i, bsize);
427 videobuf_queue_lock(q);
429 videobuf_queue_unlock(q);
440 if (req->
count < 1) {
441 dprintk(1,
"reqbufs: count invalid (%d)\n", req->
count);
448 dprintk(1,
"reqbufs: memory type invalid\n");
452 videobuf_queue_lock(q);
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)
474 q->
ops->buf_setup(q, &count, &size);
475 dprintk(1,
"reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
481 dprintk(1,
"reqbufs: mmap setup returned %d\n", retval);
489 videobuf_queue_unlock(q);
498 videobuf_queue_lock(q);
500 dprintk(1,
"querybuf: Wrong type.\n");
504 dprintk(1,
"querybuf: index out of range.\n");
508 dprintk(1,
"querybuf: buffer is null.\n");
516 videobuf_queue_unlock(q);
525 unsigned long flags = 0;
533 videobuf_queue_lock(q);
536 dprintk(1,
"qbuf: Reading running...\n");
541 dprintk(1,
"qbuf: Wrong type.\n");
544 if (b->
index >= VIDEO_MAX_FRAME) {
545 dprintk(1,
"qbuf: index out of range.\n");
550 dprintk(1,
"qbuf: buffer is null.\n");
555 dprintk(1,
"qbuf: memory type is wrong.\n");
559 dprintk(1,
"qbuf: buffer is already queued or active.\n");
565 if (0 == buf->
baddr) {
566 dprintk(1,
"qbuf: mmap requested "
567 "but buffer addr is zero!\n");
580 dprintk(1,
"qbuf: buffer length is not enough\n");
585 q->
ops->buf_release(q, buf);
592 dprintk(1,
"qbuf: wrong memory type\n");
596 dprintk(1,
"qbuf: requesting next field\n");
598 retval = q->
ops->buf_prepare(q, buf, field);
600 dprintk(1,
"qbuf: buffer_prepare returned %d\n", retval);
607 q->
ops->buf_queue(q, buf);
608 spin_unlock_irqrestore(q->
irqlock, flags);
610 dprintk(1,
"qbuf: succeeded\n");
615 videobuf_queue_unlock(q);
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");
645 videobuf_queue_unlock(q);
652 videobuf_queue_lock(q);
674 retval = stream_next_buffer_check_queue(q, nonblocking);
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");
718 videobuf_status(q, b, buf, q->
type);
723 videobuf_queue_unlock(q);
731 unsigned long flags = 0;
734 videobuf_queue_lock(q);
745 q->
ops->buf_queue(q, buf);
746 spin_unlock_irqrestore(q->
irqlock, flags);
750 videobuf_queue_unlock(q);
770 videobuf_queue_lock(q);
771 retval = __videobuf_streamoff(q);
772 videobuf_queue_unlock(q);
781 size_t count, loff_t *ppos)
784 unsigned long flags = 0;
799 retval = q->
ops->buf_prepare(q, q->
read_buf, field);
806 spin_unlock_irqrestore(q->
irqlock, flags);
826 char __user *data,
size_t count,
843 char __user *data,
size_t count,
size_t pos,
844 int vbihack,
int nonblocking)
846 unsigned int *
fc =
CALL(q, vaddr, buf);
853 fc += (buf->
size >> 2) - 1;
855 dprintk(1,
"vbihack: %d\n", *fc);
859 count = __videobuf_copy_to_user(q, buf, data, count, nonblocking);
861 if ((count == -
EFAULT) && (pos == 0))
868 char __user *data,
size_t count, loff_t *ppos,
872 unsigned long flags = 0;
873 unsigned size = 0, nbufs = 1;
878 videobuf_queue_lock(q);
880 q->
ops->buf_setup(q, &nbufs, &size);
885 retval = videobuf_read_zerocopy(q, data, count, ppos);
886 if (retval >= 0 || retval == -
EIO)
903 retval = q->
ops->buf_prepare(q, q->
read_buf, field);
913 spin_unlock_irqrestore(q->
irqlock, flags);
935 retval = __videobuf_copy_to_user(q, q->
read_buf, data, count, nonblocking);
948 videobuf_queue_unlock(q);
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)
974 for (i = 0; i <
count; i++) {
976 err = q->
ops->buf_prepare(q, q->
bufs[i], field);
982 for (i = 0; i <
count; i++)
983 q->
ops->buf_queue(q, q->
bufs[i]);
984 spin_unlock_irqrestore(q->
irqlock, flags);
995 INIT_LIST_HEAD(&q->
stream);
1009 videobuf_queue_lock(q);
1010 rc = __videobuf_read_start(q);
1011 videobuf_queue_unlock(q);
1019 videobuf_queue_lock(q);
1020 __videobuf_read_stop(q);
1021 videobuf_queue_unlock(q);
1027 videobuf_queue_lock(q);
1030 __videobuf_streamoff(q);
1033 __videobuf_read_stop(q);
1035 videobuf_queue_unlock(q);
1040 char __user *data,
size_t count, loff_t *ppos,
1041 int vbihack,
int nonblocking)
1044 unsigned long flags = 0;
1049 videobuf_queue_lock(q);
1054 retval = __videobuf_read_start(q);
1077 rc = __videobuf_copy_stream(q, q->
read_buf, data + retval, count,
1078 retval, vbihack, nonblocking);
1099 spin_unlock_irqrestore(q->
irqlock, flags);
1107 videobuf_queue_unlock(q);
1116 unsigned long req_events = poll_requested_events(wait);
1118 unsigned int rc = 0;
1120 videobuf_queue_lock(q);
1122 if (!list_empty(&q->
stream))
1127 __videobuf_read_start(q);
1143 poll_wait(file, &buf->
done, wait);
1158 videobuf_queue_unlock(q);
1171 dprintk(1,
"mmap appl bug: PROT_WRITE and MAP_SHARED are required\n");
1175 videobuf_queue_lock(q);
1181 rc =
CALL(q, mmap_mapper, q, buf, vma);
1185 videobuf_queue_unlock(q);