2 * $Id: video-buf.c,v 1.18 2005/02/24 13:32:30 kraxel Exp $
4 * generic helper functions for video4linux capture buffers, to handle
5 * memory management and PCI DMA. Right now bttv + saa7134 use it.
7 * The functions expect the hardware being able to scatter gatter
8 * (i.e. the buffers are not linear in physical memory, but fragmented
9 * into PAGE_SIZE chunks). They also assume the driver does not need
10 * to touch the video data (thus it is probably not useful for USB 1.1
11 * as data often must be uncompressed by the drivers).
13 * (c) 2001-2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/vmalloc.h>
25 #include <linux/pagemap.h>
26 #include <linux/slab.h>
27 #include <linux/pci.h>
28 #include <linux/interrupt.h>
30 #include <asm/pgtable.h>
32 #include <media/video-buf.h>
34 #define MAGIC_DMABUF 0x19721112
35 #define MAGIC_BUFFER 0x20040302
36 #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
37 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
40 module_param(debug
, int, 0644);
42 MODULE_DESCRIPTION("helper module to manage video4linux pci dma buffers");
43 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
44 MODULE_LICENSE("GPL");
46 #define dprintk(level, fmt, arg...) if (debug >= level) \
47 printk(KERN_DEBUG "vbuf: " fmt , ## arg)
50 videobuf_vmalloc_to_sg(unsigned char *virt
, int nr_pages
)
52 struct scatterlist
*sglist
;
56 sglist
= kmalloc(sizeof(struct scatterlist
)*nr_pages
, GFP_KERNEL
);
59 memset(sglist
,0,sizeof(struct scatterlist
)*nr_pages
);
60 for (i
= 0; i
< nr_pages
; i
++, virt
+= PAGE_SIZE
) {
61 pg
= vmalloc_to_page(virt
);
67 sglist
[i
].length
= PAGE_SIZE
;
77 videobuf_pages_to_sg(struct page
**pages
, int nr_pages
, int offset
)
79 struct scatterlist
*sglist
;
84 sglist
= kmalloc(sizeof(*sglist
) * nr_pages
, GFP_KERNEL
);
87 memset(sglist
, 0, sizeof(*sglist
) * nr_pages
);
91 if (PageHighMem(pages
[0]))
92 /* DMA to highmem pages might not work */
94 sglist
[0].page
= pages
[0];
95 sglist
[0].offset
= offset
;
96 sglist
[0].length
= PAGE_SIZE
- offset
;
97 for (i
= 1; i
< nr_pages
; i
++) {
100 if (PageHighMem(pages
[i
]))
102 sglist
[i
].page
= pages
[i
];
103 sglist
[i
].length
= PAGE_SIZE
;
108 dprintk(2,"sgl: oops - no page\n");
113 dprintk(2,"sgl: oops - highmem page\n");
118 /* --------------------------------------------------------------------- */
120 void videobuf_dma_init(struct videobuf_dmabuf
*dma
)
122 memset(dma
,0,sizeof(*dma
));
123 dma
->magic
= MAGIC_DMABUF
;
126 int videobuf_dma_init_user(struct videobuf_dmabuf
*dma
, int direction
,
127 unsigned long data
, unsigned long size
)
129 unsigned long first
,last
;
132 dma
->direction
= direction
;
133 switch (dma
->direction
) {
134 case PCI_DMA_FROMDEVICE
: rw
= READ
; break;
135 case PCI_DMA_TODEVICE
: rw
= WRITE
; break;
139 first
= (data
& PAGE_MASK
) >> PAGE_SHIFT
;
140 last
= ((data
+size
-1) & PAGE_MASK
) >> PAGE_SHIFT
;
141 dma
->offset
= data
& ~PAGE_MASK
;
142 dma
->nr_pages
= last
-first
+1;
143 dma
->pages
= kmalloc(dma
->nr_pages
* sizeof(struct page
*),
145 if (NULL
== dma
->pages
)
147 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
148 data
,size
,dma
->nr_pages
);
150 down_read(¤t
->mm
->mmap_sem
);
151 err
= get_user_pages(current
,current
->mm
,
152 data
& PAGE_MASK
, dma
->nr_pages
,
153 rw
== READ
, 1, /* force */
155 up_read(¤t
->mm
->mmap_sem
);
156 if (err
!= dma
->nr_pages
) {
157 dma
->nr_pages
= (err
>= 0) ? err
: 0;
158 dprintk(1,"get_user_pages: err=%d [%d]\n",err
,dma
->nr_pages
);
159 return err
< 0 ? err
: -EINVAL
;
164 int videobuf_dma_init_kernel(struct videobuf_dmabuf
*dma
, int direction
,
167 dprintk(1,"init kernel [%d pages]\n",nr_pages
);
168 dma
->direction
= direction
;
169 dma
->vmalloc
= vmalloc_32(nr_pages
<< PAGE_SHIFT
);
170 if (NULL
== dma
->vmalloc
) {
171 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages
);
174 memset(dma
->vmalloc
,0,nr_pages
<< PAGE_SHIFT
);
175 dma
->nr_pages
= nr_pages
;
179 int videobuf_dma_init_overlay(struct videobuf_dmabuf
*dma
, int direction
,
180 dma_addr_t addr
, int nr_pages
)
182 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
183 nr_pages
,(unsigned long)addr
);
184 dma
->direction
= direction
;
188 dma
->bus_addr
= addr
;
189 dma
->nr_pages
= nr_pages
;
193 int videobuf_dma_pci_map(struct pci_dev
*dev
, struct videobuf_dmabuf
*dma
)
195 MAGIC_CHECK(dma
->magic
,MAGIC_DMABUF
);
196 BUG_ON(0 == dma
->nr_pages
);
199 dma
->sglist
= videobuf_pages_to_sg(dma
->pages
, dma
->nr_pages
,
203 dma
->sglist
= videobuf_vmalloc_to_sg
204 (dma
->vmalloc
,dma
->nr_pages
);
207 dma
->sglist
= kmalloc(sizeof(struct scatterlist
), GFP_KERNEL
);
208 if (NULL
!= dma
->sglist
) {
210 sg_dma_address(&dma
->sglist
[0]) = dma
->bus_addr
& PAGE_MASK
;
211 dma
->sglist
[0].offset
= dma
->bus_addr
& ~PAGE_MASK
;
212 sg_dma_len(&dma
->sglist
[0]) = dma
->nr_pages
* PAGE_SIZE
;
215 if (NULL
== dma
->sglist
) {
216 dprintk(1,"scatterlist is NULL\n");
220 if (!dma
->bus_addr
) {
221 dma
->sglen
= pci_map_sg(dev
,dma
->sglist
,dma
->nr_pages
,
223 if (0 == dma
->sglen
) {
225 "%s: pci_map_sg failed\n",__FUNCTION__
);
235 int videobuf_dma_pci_sync(struct pci_dev
*dev
, struct videobuf_dmabuf
*dma
)
237 MAGIC_CHECK(dma
->magic
,MAGIC_DMABUF
);
241 pci_dma_sync_sg_for_cpu(dev
,dma
->sglist
,dma
->nr_pages
,dma
->direction
);
245 int videobuf_dma_pci_unmap(struct pci_dev
*dev
, struct videobuf_dmabuf
*dma
)
247 MAGIC_CHECK(dma
->magic
,MAGIC_DMABUF
);
252 pci_unmap_sg(dev
,dma
->sglist
,dma
->nr_pages
,dma
->direction
);
259 int videobuf_dma_free(struct videobuf_dmabuf
*dma
)
261 MAGIC_CHECK(dma
->magic
,MAGIC_DMABUF
);
266 for (i
=0; i
< dma
->nr_pages
; i
++)
267 page_cache_release(dma
->pages
[i
]);
278 dma
->direction
= PCI_DMA_NONE
;
282 /* --------------------------------------------------------------------- */
284 void* videobuf_alloc(unsigned int size
)
286 struct videobuf_buffer
*vb
;
288 vb
= kmalloc(size
,GFP_KERNEL
);
291 videobuf_dma_init(&vb
->dma
);
292 init_waitqueue_head(&vb
->done
);
293 vb
->magic
= MAGIC_BUFFER
;
298 int videobuf_waiton(struct videobuf_buffer
*vb
, int non_blocking
, int intr
)
301 DECLARE_WAITQUEUE(wait
, current
);
303 MAGIC_CHECK(vb
->magic
,MAGIC_BUFFER
);
304 add_wait_queue(&vb
->done
, &wait
);
305 while (vb
->state
== STATE_ACTIVE
|| vb
->state
== STATE_QUEUED
) {
310 set_current_state(intr
? TASK_INTERRUPTIBLE
311 : TASK_UNINTERRUPTIBLE
);
312 if (vb
->state
== STATE_ACTIVE
|| vb
->state
== STATE_QUEUED
)
314 set_current_state(TASK_RUNNING
);
315 if (intr
&& signal_pending(current
)) {
316 dprintk(1,"buffer waiton: -EINTR\n");
321 remove_wait_queue(&vb
->done
, &wait
);
326 videobuf_iolock(struct pci_dev
*pci
, struct videobuf_buffer
*vb
,
327 struct v4l2_framebuffer
*fbuf
)
332 MAGIC_CHECK(vb
->magic
,MAGIC_BUFFER
);
333 switch (vb
->memory
) {
334 case V4L2_MEMORY_MMAP
:
335 case V4L2_MEMORY_USERPTR
:
336 if (0 == vb
->baddr
) {
337 /* no userspace addr -- kernel bounce buffer */
338 pages
= PAGE_ALIGN(vb
->size
) >> PAGE_SHIFT
;
339 err
= videobuf_dma_init_kernel(&vb
->dma
,PCI_DMA_FROMDEVICE
,
344 /* dma directly to userspace */
345 err
= videobuf_dma_init_user(&vb
->dma
,PCI_DMA_FROMDEVICE
,
346 vb
->baddr
,vb
->bsize
);
351 case V4L2_MEMORY_OVERLAY
:
354 /* FIXME: need sanity checks for vb->boff */
355 bus
= (dma_addr_t
)fbuf
->base
+ vb
->boff
;
356 pages
= PAGE_ALIGN(vb
->size
) >> PAGE_SHIFT
;
357 err
= videobuf_dma_init_overlay(&vb
->dma
,PCI_DMA_FROMDEVICE
,
365 err
= videobuf_dma_pci_map(pci
,&vb
->dma
);
372 /* --------------------------------------------------------------------- */
374 void videobuf_queue_init(struct videobuf_queue
* q
,
375 struct videobuf_queue_ops
*ops
,
378 enum v4l2_buf_type type
,
379 enum v4l2_field field
,
383 memset(q
,0,sizeof(*q
));
384 q
->irqlock
= irqlock
;
392 init_MUTEX(&q
->lock
);
393 INIT_LIST_HEAD(&q
->stream
);
397 videobuf_queue_is_busy(struct videobuf_queue
*q
)
402 dprintk(1,"busy: streaming active\n");
406 dprintk(1,"busy: pending read #1\n");
410 dprintk(1,"busy: pending read #2\n");
413 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
414 if (NULL
== q
->bufs
[i
])
416 if (q
->bufs
[i
]->map
) {
417 dprintk(1,"busy: buffer #%d mapped\n",i
);
420 if (q
->bufs
[i
]->state
== STATE_QUEUED
) {
421 dprintk(1,"busy: buffer #%d queued\n",i
);
424 if (q
->bufs
[i
]->state
== STATE_ACTIVE
) {
425 dprintk(1,"busy: buffer #%d avtive\n",i
);
433 videobuf_queue_cancel(struct videobuf_queue
*q
)
438 /* remove queued buffers from list */
439 spin_lock_irqsave(q
->irqlock
,flags
);
440 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
441 if (NULL
== q
->bufs
[i
])
443 if (q
->bufs
[i
]->state
== STATE_QUEUED
) {
444 list_del(&q
->bufs
[i
]->queue
);
445 q
->bufs
[i
]->state
= STATE_ERROR
;
448 spin_unlock_irqrestore(q
->irqlock
,flags
);
450 /* free all buffers + clear queue */
451 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
452 if (NULL
== q
->bufs
[i
])
454 q
->ops
->buf_release(q
,q
->bufs
[i
]);
456 INIT_LIST_HEAD(&q
->stream
);
459 /* --------------------------------------------------------------------- */
462 videobuf_next_field(struct videobuf_queue
*q
)
464 enum v4l2_field field
= q
->field
;
466 BUG_ON(V4L2_FIELD_ANY
== field
);
468 if (V4L2_FIELD_ALTERNATE
== field
) {
469 if (V4L2_FIELD_TOP
== q
->last
) {
470 field
= V4L2_FIELD_BOTTOM
;
471 q
->last
= V4L2_FIELD_BOTTOM
;
473 field
= V4L2_FIELD_TOP
;
474 q
->last
= V4L2_FIELD_TOP
;
481 videobuf_status(struct v4l2_buffer
*b
, struct videobuf_buffer
*vb
,
482 enum v4l2_buf_type type
)
484 MAGIC_CHECK(vb
->magic
,MAGIC_BUFFER
);
489 b
->memory
= vb
->memory
;
491 case V4L2_MEMORY_MMAP
:
492 b
->m
.offset
= vb
->boff
;
493 b
->length
= vb
->bsize
;
495 case V4L2_MEMORY_USERPTR
:
496 b
->m
.userptr
= vb
->baddr
;
497 b
->length
= vb
->bsize
;
499 case V4L2_MEMORY_OVERLAY
:
500 b
->m
.offset
= vb
->boff
;
506 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
512 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
516 b
->flags
|= V4L2_BUF_FLAG_DONE
;
518 case STATE_NEEDS_INIT
:
524 if (vb
->input
!= UNSET
) {
525 b
->flags
|= V4L2_BUF_FLAG_INPUT
;
526 b
->input
= vb
->input
;
529 b
->field
= vb
->field
;
530 b
->timestamp
= vb
->ts
;
531 b
->bytesused
= vb
->size
;
532 b
->sequence
= vb
->field_count
>> 1;
536 videobuf_reqbufs(struct videobuf_queue
*q
,
537 struct v4l2_requestbuffers
*req
)
539 unsigned int size
,count
;
542 if (req
->type
!= q
->type
)
546 if (req
->memory
!= V4L2_MEMORY_MMAP
&&
547 req
->memory
!= V4L2_MEMORY_USERPTR
&&
548 req
->memory
!= V4L2_MEMORY_OVERLAY
)
553 if (!list_empty(&q
->stream
))
558 if (count
> VIDEO_MAX_FRAME
)
559 count
= VIDEO_MAX_FRAME
;
561 q
->ops
->buf_setup(q
,&count
,&size
);
562 size
= PAGE_ALIGN(size
);
563 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
564 count
, size
, (count
*size
)>>PAGE_SHIFT
);
566 retval
= videobuf_mmap_setup(q
,count
,size
,req
->memory
);
578 videobuf_querybuf(struct videobuf_queue
*q
, struct v4l2_buffer
*b
)
580 if (unlikely(b
->type
!= q
->type
))
582 if (unlikely(b
->index
< 0 || b
->index
>= VIDEO_MAX_FRAME
))
584 if (unlikely(NULL
== q
->bufs
[b
->index
]))
586 videobuf_status(b
,q
->bufs
[b
->index
],q
->type
);
591 videobuf_qbuf(struct videobuf_queue
*q
,
592 struct v4l2_buffer
*b
)
594 struct videobuf_buffer
*buf
;
595 enum v4l2_field field
;
604 if (b
->type
!= q
->type
)
606 if (b
->index
< 0 || b
->index
>= VIDEO_MAX_FRAME
)
608 buf
= q
->bufs
[b
->index
];
611 MAGIC_CHECK(buf
->magic
,MAGIC_BUFFER
);
612 if (buf
->memory
!= b
->memory
)
614 if (buf
->state
== STATE_QUEUED
||
615 buf
->state
== STATE_ACTIVE
)
618 if (b
->flags
& V4L2_BUF_FLAG_INPUT
) {
619 if (b
->input
>= q
->inputs
)
621 buf
->input
= b
->input
;
627 case V4L2_MEMORY_MMAP
:
631 case V4L2_MEMORY_USERPTR
:
632 if (b
->length
< buf
->bsize
)
634 if (STATE_NEEDS_INIT
!= buf
->state
&& buf
->baddr
!= b
->m
.userptr
)
635 q
->ops
->buf_release(q
,buf
);
636 buf
->baddr
= b
->m
.userptr
;
638 case V4L2_MEMORY_OVERLAY
:
639 buf
->boff
= b
->m
.offset
;
645 field
= videobuf_next_field(q
);
646 retval
= q
->ops
->buf_prepare(q
,buf
,field
);
650 list_add_tail(&buf
->stream
,&q
->stream
);
652 spin_lock_irqsave(q
->irqlock
,flags
);
653 q
->ops
->buf_queue(q
,buf
);
654 spin_unlock_irqrestore(q
->irqlock
,flags
);
664 videobuf_dqbuf(struct videobuf_queue
*q
,
665 struct v4l2_buffer
*b
, int nonblocking
)
667 struct videobuf_buffer
*buf
;
675 if (b
->type
!= q
->type
)
677 if (list_empty(&q
->stream
))
679 buf
= list_entry(q
->stream
.next
, struct videobuf_buffer
, stream
);
680 retval
= videobuf_waiton(buf
, nonblocking
, 1);
683 switch (buf
->state
) {
688 videobuf_dma_pci_sync(q
->pci
,&buf
->dma
);
689 buf
->state
= STATE_IDLE
;
695 list_del(&buf
->stream
);
696 memset(b
,0,sizeof(*b
));
697 videobuf_status(b
,buf
,q
->type
);
704 int videobuf_streamon(struct videobuf_queue
*q
)
706 struct videobuf_buffer
*buf
;
707 struct list_head
*list
;
719 spin_lock_irqsave(q
->irqlock
,flags
);
720 list_for_each(list
,&q
->stream
) {
721 buf
= list_entry(list
, struct videobuf_buffer
, stream
);
722 if (buf
->state
== STATE_PREPARED
)
723 q
->ops
->buf_queue(q
,buf
);
725 spin_unlock_irqrestore(q
->irqlock
,flags
);
732 int videobuf_streamoff(struct videobuf_queue
*q
)
734 int retval
= -EINVAL
;
739 videobuf_queue_cancel(q
);
749 videobuf_read_zerocopy(struct videobuf_queue
*q
, char __user
*data
,
750 size_t count
, loff_t
*ppos
)
752 enum v4l2_field field
;
758 q
->read_buf
= videobuf_alloc(q
->msize
);
759 if (NULL
== q
->read_buf
)
762 q
->read_buf
->memory
= V4L2_MEMORY_USERPTR
;
763 q
->read_buf
->baddr
= (unsigned long)data
;
764 q
->read_buf
->bsize
= count
;
765 field
= videobuf_next_field(q
);
766 retval
= q
->ops
->buf_prepare(q
,q
->read_buf
,field
);
770 /* start capture & wait */
771 spin_lock_irqsave(q
->irqlock
,flags
);
772 q
->ops
->buf_queue(q
,q
->read_buf
);
773 spin_unlock_irqrestore(q
->irqlock
,flags
);
774 retval
= videobuf_waiton(q
->read_buf
,0,0);
776 videobuf_dma_pci_sync(q
->pci
,&q
->read_buf
->dma
);
777 if (STATE_ERROR
== q
->read_buf
->state
)
780 retval
= q
->read_buf
->size
;
785 q
->ops
->buf_release(q
,q
->read_buf
);
791 ssize_t
videobuf_read_one(struct videobuf_queue
*q
,
792 char __user
*data
, size_t count
, loff_t
*ppos
,
795 enum v4l2_field field
;
797 unsigned size
, nbufs
, bytes
;
803 q
->ops
->buf_setup(q
,&nbufs
,&size
);
804 if (NULL
== q
->read_buf
&&
807 retval
= videobuf_read_zerocopy(q
,data
,count
,ppos
);
808 if (retval
>= 0 || retval
== -EIO
)
811 /* fallback to kernel bounce buffer on failures */
814 if (NULL
== q
->read_buf
) {
815 /* need to capture a new frame */
817 q
->read_buf
= videobuf_alloc(q
->msize
);
818 if (NULL
== q
->read_buf
)
820 q
->read_buf
->memory
= V4L2_MEMORY_USERPTR
;
821 field
= videobuf_next_field(q
);
822 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
);
831 /* wait until capture is done */
832 retval
= videobuf_waiton(q
->read_buf
, nonblocking
, 1);
835 videobuf_dma_pci_sync(q
->pci
,&q
->read_buf
->dma
);
837 if (STATE_ERROR
== q
->read_buf
->state
) {
838 /* catch I/O errors */
839 q
->ops
->buf_release(q
,q
->read_buf
);
846 /* copy to userspace */
848 if (bytes
> q
->read_buf
->size
- q
->read_off
)
849 bytes
= q
->read_buf
->size
- q
->read_off
;
851 if (copy_to_user(data
, q
->read_buf
->dma
.vmalloc
+q
->read_off
, bytes
))
855 q
->read_off
+= bytes
;
856 if (q
->read_off
== q
->read_buf
->size
) {
857 /* all data copied, cleanup */
858 q
->ops
->buf_release(q
,q
->read_buf
);
868 int videobuf_read_start(struct videobuf_queue
*q
)
870 enum v4l2_field field
;
872 int count
= 0, size
= 0;
875 q
->ops
->buf_setup(q
,&count
,&size
);
878 if (count
> VIDEO_MAX_FRAME
)
879 count
= VIDEO_MAX_FRAME
;
880 size
= PAGE_ALIGN(size
);
882 err
= videobuf_mmap_setup(q
, count
, size
, V4L2_MEMORY_USERPTR
);
885 for (i
= 0; i
< count
; i
++) {
886 field
= videobuf_next_field(q
);
887 err
= q
->ops
->buf_prepare(q
,q
->bufs
[i
],field
);
890 list_add_tail(&q
->bufs
[i
]->stream
, &q
->stream
);
892 spin_lock_irqsave(q
->irqlock
,flags
);
893 for (i
= 0; i
< count
; i
++)
894 q
->ops
->buf_queue(q
,q
->bufs
[i
]);
895 spin_unlock_irqrestore(q
->irqlock
,flags
);
900 void videobuf_read_stop(struct videobuf_queue
*q
)
904 videobuf_queue_cancel(q
);
905 videobuf_mmap_free(q
);
906 INIT_LIST_HEAD(&q
->stream
);
907 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
908 if (NULL
== q
->bufs
[i
])
917 ssize_t
videobuf_read_stream(struct videobuf_queue
*q
,
918 char __user
*data
, size_t count
, loff_t
*ppos
,
919 int vbihack
, int nonblocking
)
921 unsigned int *fc
, bytes
;
925 dprintk(2,"%s\n",__FUNCTION__
);
931 retval
= videobuf_read_start(q
);
938 /* get / wait for data */
939 if (NULL
== q
->read_buf
) {
940 q
->read_buf
= list_entry(q
->stream
.next
,
941 struct videobuf_buffer
,
943 list_del(&q
->read_buf
->stream
);
946 err
= videobuf_waiton(q
->read_buf
, nonblocking
, 1);
953 if (q
->read_buf
->state
== STATE_DONE
) {
955 /* dirty, undocumented hack -- pass the frame counter
956 * within the last four bytes of each vbi data block.
957 * We need that one to maintain backward compatibility
958 * to all vbi decoding software out there ... */
959 fc
= (unsigned int*)q
->read_buf
->dma
.vmalloc
;
960 fc
+= (q
->read_buf
->size
>>2) -1;
961 *fc
= q
->read_buf
->field_count
>> 1;
962 dprintk(1,"vbihack: %d\n",*fc
);
967 if (bytes
> q
->read_buf
->size
- q
->read_off
)
968 bytes
= q
->read_buf
->size
- q
->read_off
;
969 if (copy_to_user(data
+ retval
,
970 q
->read_buf
->dma
.vmalloc
+ q
->read_off
,
978 q
->read_off
+= bytes
;
981 q
->read_off
= q
->read_buf
->size
;
986 /* requeue buffer when done with copying */
987 if (q
->read_off
== q
->read_buf
->size
) {
988 list_add_tail(&q
->read_buf
->stream
,
990 spin_lock_irqsave(q
->irqlock
,flags
);
991 q
->ops
->buf_queue(q
,q
->read_buf
);
992 spin_unlock_irqrestore(q
->irqlock
,flags
);
1004 unsigned int videobuf_poll_stream(struct file
*file
,
1005 struct videobuf_queue
*q
,
1008 struct videobuf_buffer
*buf
= NULL
;
1009 unsigned int rc
= 0;
1013 if (!list_empty(&q
->stream
))
1014 buf
= list_entry(q
->stream
.next
,
1015 struct videobuf_buffer
, stream
);
1018 videobuf_read_start(q
);
1021 } else if (NULL
== q
->read_buf
) {
1022 q
->read_buf
= list_entry(q
->stream
.next
,
1023 struct videobuf_buffer
,
1025 list_del(&q
->read_buf
->stream
);
1034 poll_wait(file
, &buf
->done
, wait
);
1035 if (buf
->state
== STATE_DONE
||
1036 buf
->state
== STATE_ERROR
)
1037 rc
= POLLIN
|POLLRDNORM
;
1043 /* --------------------------------------------------------------------- */
1046 videobuf_vm_open(struct vm_area_struct
*vma
)
1048 struct videobuf_mapping
*map
= vma
->vm_private_data
;
1050 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map
,
1051 map
->count
,vma
->vm_start
,vma
->vm_end
);
1056 videobuf_vm_close(struct vm_area_struct
*vma
)
1058 struct videobuf_mapping
*map
= vma
->vm_private_data
;
1059 struct videobuf_queue
*q
= map
->q
;
1062 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map
,
1063 map
->count
,vma
->vm_start
,vma
->vm_end
);
1066 if (0 == map
->count
) {
1067 dprintk(1,"munmap %p q=%p\n",map
,q
);
1069 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
1070 if (NULL
== q
->bufs
[i
])
1074 if (q
->bufs
[i
]->map
!= map
)
1076 q
->bufs
[i
]->map
= NULL
;
1077 q
->bufs
[i
]->baddr
= 0;
1078 q
->ops
->buf_release(q
,q
->bufs
[i
]);
1087 * Get a anonymous page for the mapping. Make sure we can DMA to that
1088 * memory location with 32bit PCI devices (i.e. don't use highmem for
1089 * now ...). Bounce buffers don't work very well for the data rates
1090 * video capture has.
1093 videobuf_vm_nopage(struct vm_area_struct
*vma
, unsigned long vaddr
,
1098 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1099 vaddr
,vma
->vm_start
,vma
->vm_end
);
1100 if (vaddr
> vma
->vm_end
)
1101 return NOPAGE_SIGBUS
;
1102 page
= alloc_page(GFP_USER
);
1105 clear_user_page(page_address(page
), vaddr
, page
);
1107 *type
= VM_FAULT_MINOR
;
1111 static struct vm_operations_struct videobuf_vm_ops
=
1113 .open
= videobuf_vm_open
,
1114 .close
= videobuf_vm_close
,
1115 .nopage
= videobuf_vm_nopage
,
1118 int videobuf_mmap_setup(struct videobuf_queue
*q
,
1119 unsigned int bcount
, unsigned int bsize
,
1120 enum v4l2_memory memory
)
1125 err
= videobuf_mmap_free(q
);
1129 for (i
= 0; i
< bcount
; i
++) {
1130 q
->bufs
[i
] = videobuf_alloc(q
->msize
);
1132 q
->bufs
[i
]->input
= UNSET
;
1133 q
->bufs
[i
]->memory
= memory
;
1134 q
->bufs
[i
]->bsize
= bsize
;
1136 case V4L2_MEMORY_MMAP
:
1137 q
->bufs
[i
]->boff
= bsize
* i
;
1139 case V4L2_MEMORY_USERPTR
:
1140 case V4L2_MEMORY_OVERLAY
:
1145 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1150 int videobuf_mmap_free(struct videobuf_queue
*q
)
1154 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++)
1155 if (q
->bufs
[i
] && q
->bufs
[i
]->map
)
1157 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
1158 if (NULL
== q
->bufs
[i
])
1160 q
->ops
->buf_release(q
,q
->bufs
[i
]);
1167 int videobuf_mmap_mapper(struct videobuf_queue
*q
,
1168 struct vm_area_struct
*vma
)
1170 struct videobuf_mapping
*map
;
1171 unsigned int first
,last
,size
,i
;
1176 if (!(vma
->vm_flags
& VM_WRITE
)) {
1177 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1180 if (!(vma
->vm_flags
& VM_SHARED
)) {
1181 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1185 /* look for first buffer to map */
1186 for (first
= 0; first
< VIDEO_MAX_FRAME
; first
++) {
1187 if (NULL
== q
->bufs
[first
])
1189 if (V4L2_MEMORY_MMAP
!= q
->bufs
[first
]->memory
)
1191 if (q
->bufs
[first
]->boff
== (vma
->vm_pgoff
<< PAGE_SHIFT
))
1194 if (VIDEO_MAX_FRAME
== first
) {
1195 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1196 (vma
->vm_pgoff
<< PAGE_SHIFT
));
1200 /* look for last buffer to map */
1201 for (size
= 0, last
= first
; last
< VIDEO_MAX_FRAME
; last
++) {
1202 if (NULL
== q
->bufs
[last
])
1204 if (V4L2_MEMORY_MMAP
!= q
->bufs
[last
]->memory
)
1206 if (q
->bufs
[last
]->map
) {
1210 size
+= q
->bufs
[last
]->bsize
;
1211 if (size
== (vma
->vm_end
- vma
->vm_start
))
1214 if (VIDEO_MAX_FRAME
== last
) {
1215 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1216 (vma
->vm_end
- vma
->vm_start
));
1220 /* create mapping + update buffer list */
1222 map
= kmalloc(sizeof(struct videobuf_mapping
),GFP_KERNEL
);
1225 for (size
= 0, i
= first
; i
<= last
; size
+= q
->bufs
[i
++]->bsize
) {
1226 q
->bufs
[i
]->map
= map
;
1227 q
->bufs
[i
]->baddr
= vma
->vm_start
+ size
;
1230 map
->start
= vma
->vm_start
;
1231 map
->end
= vma
->vm_end
;
1233 vma
->vm_ops
= &videobuf_vm_ops
;
1234 vma
->vm_flags
|= VM_DONTEXPAND
| VM_RESERVED
;
1235 vma
->vm_flags
&= ~VM_IO
; /* using shared anonymous pages */
1236 vma
->vm_private_data
= map
;
1237 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1238 map
,q
,vma
->vm_start
,vma
->vm_end
,vma
->vm_pgoff
,first
,last
);
1246 /* --------------------------------------------------------------------- */
1248 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg
);
1250 EXPORT_SYMBOL_GPL(videobuf_dma_init
);
1251 EXPORT_SYMBOL_GPL(videobuf_dma_init_user
);
1252 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel
);
1253 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay
);
1254 EXPORT_SYMBOL_GPL(videobuf_dma_pci_map
);
1255 EXPORT_SYMBOL_GPL(videobuf_dma_pci_sync
);
1256 EXPORT_SYMBOL_GPL(videobuf_dma_pci_unmap
);
1257 EXPORT_SYMBOL_GPL(videobuf_dma_free
);
1259 EXPORT_SYMBOL_GPL(videobuf_alloc
);
1260 EXPORT_SYMBOL_GPL(videobuf_waiton
);
1261 EXPORT_SYMBOL_GPL(videobuf_iolock
);
1263 EXPORT_SYMBOL_GPL(videobuf_queue_init
);
1264 EXPORT_SYMBOL_GPL(videobuf_queue_cancel
);
1265 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy
);
1267 EXPORT_SYMBOL_GPL(videobuf_next_field
);
1268 EXPORT_SYMBOL_GPL(videobuf_status
);
1269 EXPORT_SYMBOL_GPL(videobuf_reqbufs
);
1270 EXPORT_SYMBOL_GPL(videobuf_querybuf
);
1271 EXPORT_SYMBOL_GPL(videobuf_qbuf
);
1272 EXPORT_SYMBOL_GPL(videobuf_dqbuf
);
1273 EXPORT_SYMBOL_GPL(videobuf_streamon
);
1274 EXPORT_SYMBOL_GPL(videobuf_streamoff
);
1276 EXPORT_SYMBOL_GPL(videobuf_read_start
);
1277 EXPORT_SYMBOL_GPL(videobuf_read_stop
);
1278 EXPORT_SYMBOL_GPL(videobuf_read_stream
);
1279 EXPORT_SYMBOL_GPL(videobuf_read_one
);
1280 EXPORT_SYMBOL_GPL(videobuf_poll_stream
);
1282 EXPORT_SYMBOL_GPL(videobuf_mmap_setup
);
1283 EXPORT_SYMBOL_GPL(videobuf_mmap_free
);
1284 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper
);