3 * generic helper functions for video4linux capture buffers, to handle
4 * memory management and PCI DMA. Right now bttv + saa7134 use it.
6 * The functions expect the hardware being able to scatter gatter
7 * (i.e. the buffers are not linear in physical memory, but fragmented
8 * into PAGE_SIZE chunks). They also assume the driver does not need
9 * to touch the video data (thus it is probably not useful for USB 1.1
10 * as data often must be uncompressed by the drivers).
12 * (c) 2001-2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/vmalloc.h>
24 #include <linux/pagemap.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
29 #include <asm/pgtable.h>
31 #include <media/video-buf.h>
33 #define MAGIC_DMABUF 0x19721112
34 #define MAGIC_BUFFER 0x20040302
35 #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
36 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
39 module_param(debug
, int, 0644);
41 MODULE_DESCRIPTION("helper module to manage video4linux pci dma buffers");
42 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
43 MODULE_LICENSE("GPL");
45 #define dprintk(level, fmt, arg...) if (debug >= level) \
46 printk(KERN_DEBUG "vbuf: " fmt , ## arg)
49 videobuf_vmalloc_to_sg(unsigned char *virt
, int nr_pages
)
51 struct scatterlist
*sglist
;
55 sglist
= kmalloc(sizeof(struct scatterlist
)*nr_pages
, GFP_KERNEL
);
58 memset(sglist
,0,sizeof(struct scatterlist
)*nr_pages
);
59 for (i
= 0; i
< nr_pages
; i
++, virt
+= PAGE_SIZE
) {
60 pg
= vmalloc_to_page(virt
);
66 sglist
[i
].length
= PAGE_SIZE
;
76 videobuf_pages_to_sg(struct page
**pages
, int nr_pages
, int offset
)
78 struct scatterlist
*sglist
;
83 sglist
= kmalloc(sizeof(*sglist
) * nr_pages
, GFP_KERNEL
);
86 memset(sglist
, 0, sizeof(*sglist
) * nr_pages
);
90 if (PageHighMem(pages
[0]))
91 /* DMA to highmem pages might not work */
93 sglist
[0].page
= pages
[0];
94 sglist
[0].offset
= offset
;
95 sglist
[0].length
= PAGE_SIZE
- offset
;
96 for (i
= 1; i
< nr_pages
; i
++) {
99 if (PageHighMem(pages
[i
]))
101 sglist
[i
].page
= pages
[i
];
102 sglist
[i
].length
= PAGE_SIZE
;
107 dprintk(2,"sgl: oops - no page\n");
112 dprintk(2,"sgl: oops - highmem page\n");
117 /* --------------------------------------------------------------------- */
119 void videobuf_dma_init(struct videobuf_dmabuf
*dma
)
121 memset(dma
,0,sizeof(*dma
));
122 dma
->magic
= MAGIC_DMABUF
;
125 int videobuf_dma_init_user(struct videobuf_dmabuf
*dma
, int direction
,
126 unsigned long data
, unsigned long size
)
128 unsigned long first
,last
;
131 dma
->direction
= direction
;
132 switch (dma
->direction
) {
133 case PCI_DMA_FROMDEVICE
: rw
= READ
; break;
134 case PCI_DMA_TODEVICE
: rw
= WRITE
; break;
138 first
= (data
& PAGE_MASK
) >> PAGE_SHIFT
;
139 last
= ((data
+size
-1) & PAGE_MASK
) >> PAGE_SHIFT
;
140 dma
->offset
= data
& ~PAGE_MASK
;
141 dma
->nr_pages
= last
-first
+1;
142 dma
->pages
= kmalloc(dma
->nr_pages
* sizeof(struct page
*),
144 if (NULL
== dma
->pages
)
146 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
147 data
,size
,dma
->nr_pages
);
149 down_read(¤t
->mm
->mmap_sem
);
150 err
= get_user_pages(current
,current
->mm
,
151 data
& PAGE_MASK
, dma
->nr_pages
,
152 rw
== READ
, 1, /* force */
154 up_read(¤t
->mm
->mmap_sem
);
155 if (err
!= dma
->nr_pages
) {
156 dma
->nr_pages
= (err
>= 0) ? err
: 0;
157 dprintk(1,"get_user_pages: err=%d [%d]\n",err
,dma
->nr_pages
);
158 return err
< 0 ? err
: -EINVAL
;
163 int videobuf_dma_init_kernel(struct videobuf_dmabuf
*dma
, int direction
,
166 dprintk(1,"init kernel [%d pages]\n",nr_pages
);
167 dma
->direction
= direction
;
168 dma
->vmalloc
= vmalloc_32(nr_pages
<< PAGE_SHIFT
);
169 if (NULL
== dma
->vmalloc
) {
170 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages
);
173 memset(dma
->vmalloc
,0,nr_pages
<< PAGE_SHIFT
);
174 dma
->nr_pages
= nr_pages
;
178 int videobuf_dma_init_overlay(struct videobuf_dmabuf
*dma
, int direction
,
179 dma_addr_t addr
, int nr_pages
)
181 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
182 nr_pages
,(unsigned long)addr
);
183 dma
->direction
= direction
;
187 dma
->bus_addr
= addr
;
188 dma
->nr_pages
= nr_pages
;
192 int videobuf_dma_pci_map(struct pci_dev
*dev
, struct videobuf_dmabuf
*dma
)
194 MAGIC_CHECK(dma
->magic
,MAGIC_DMABUF
);
195 BUG_ON(0 == dma
->nr_pages
);
198 dma
->sglist
= videobuf_pages_to_sg(dma
->pages
, dma
->nr_pages
,
202 dma
->sglist
= videobuf_vmalloc_to_sg
203 (dma
->vmalloc
,dma
->nr_pages
);
206 dma
->sglist
= kmalloc(sizeof(struct scatterlist
), GFP_KERNEL
);
207 if (NULL
!= dma
->sglist
) {
209 sg_dma_address(&dma
->sglist
[0]) = dma
->bus_addr
& PAGE_MASK
;
210 dma
->sglist
[0].offset
= dma
->bus_addr
& ~PAGE_MASK
;
211 sg_dma_len(&dma
->sglist
[0]) = dma
->nr_pages
* PAGE_SIZE
;
214 if (NULL
== dma
->sglist
) {
215 dprintk(1,"scatterlist is NULL\n");
219 if (!dma
->bus_addr
) {
220 dma
->sglen
= pci_map_sg(dev
,dma
->sglist
,dma
->nr_pages
,
222 if (0 == dma
->sglen
) {
224 "%s: pci_map_sg failed\n",__FUNCTION__
);
234 int videobuf_dma_pci_sync(struct pci_dev
*dev
, struct videobuf_dmabuf
*dma
)
236 MAGIC_CHECK(dma
->magic
,MAGIC_DMABUF
);
240 pci_dma_sync_sg_for_cpu(dev
,dma
->sglist
,dma
->nr_pages
,dma
->direction
);
244 int videobuf_dma_pci_unmap(struct pci_dev
*dev
, struct videobuf_dmabuf
*dma
)
246 MAGIC_CHECK(dma
->magic
,MAGIC_DMABUF
);
251 pci_unmap_sg(dev
,dma
->sglist
,dma
->nr_pages
,dma
->direction
);
258 int videobuf_dma_free(struct videobuf_dmabuf
*dma
)
260 MAGIC_CHECK(dma
->magic
,MAGIC_DMABUF
);
265 for (i
=0; i
< dma
->nr_pages
; i
++)
266 page_cache_release(dma
->pages
[i
]);
277 dma
->direction
= PCI_DMA_NONE
;
281 /* --------------------------------------------------------------------- */
283 void* videobuf_alloc(unsigned int size
)
285 struct videobuf_buffer
*vb
;
287 vb
= kmalloc(size
,GFP_KERNEL
);
290 videobuf_dma_init(&vb
->dma
);
291 init_waitqueue_head(&vb
->done
);
292 vb
->magic
= MAGIC_BUFFER
;
297 int videobuf_waiton(struct videobuf_buffer
*vb
, int non_blocking
, int intr
)
300 DECLARE_WAITQUEUE(wait
, current
);
302 MAGIC_CHECK(vb
->magic
,MAGIC_BUFFER
);
303 add_wait_queue(&vb
->done
, &wait
);
304 while (vb
->state
== STATE_ACTIVE
|| vb
->state
== STATE_QUEUED
) {
309 set_current_state(intr
? TASK_INTERRUPTIBLE
310 : TASK_UNINTERRUPTIBLE
);
311 if (vb
->state
== STATE_ACTIVE
|| vb
->state
== STATE_QUEUED
)
313 set_current_state(TASK_RUNNING
);
314 if (intr
&& signal_pending(current
)) {
315 dprintk(1,"buffer waiton: -EINTR\n");
320 remove_wait_queue(&vb
->done
, &wait
);
325 videobuf_iolock(struct pci_dev
*pci
, struct videobuf_buffer
*vb
,
326 struct v4l2_framebuffer
*fbuf
)
331 MAGIC_CHECK(vb
->magic
,MAGIC_BUFFER
);
332 switch (vb
->memory
) {
333 case V4L2_MEMORY_MMAP
:
334 case V4L2_MEMORY_USERPTR
:
335 if (0 == vb
->baddr
) {
336 /* no userspace addr -- kernel bounce buffer */
337 pages
= PAGE_ALIGN(vb
->size
) >> PAGE_SHIFT
;
338 err
= videobuf_dma_init_kernel(&vb
->dma
,PCI_DMA_FROMDEVICE
,
343 /* dma directly to userspace */
344 err
= videobuf_dma_init_user(&vb
->dma
,PCI_DMA_FROMDEVICE
,
345 vb
->baddr
,vb
->bsize
);
350 case V4L2_MEMORY_OVERLAY
:
353 /* FIXME: need sanity checks for vb->boff */
354 bus
= (dma_addr_t
)fbuf
->base
+ vb
->boff
;
355 pages
= PAGE_ALIGN(vb
->size
) >> PAGE_SHIFT
;
356 err
= videobuf_dma_init_overlay(&vb
->dma
,PCI_DMA_FROMDEVICE
,
364 err
= videobuf_dma_pci_map(pci
,&vb
->dma
);
371 /* --------------------------------------------------------------------- */
373 void videobuf_queue_init(struct videobuf_queue
* q
,
374 struct videobuf_queue_ops
*ops
,
377 enum v4l2_buf_type type
,
378 enum v4l2_field field
,
382 memset(q
,0,sizeof(*q
));
383 q
->irqlock
= irqlock
;
391 init_MUTEX(&q
->lock
);
392 INIT_LIST_HEAD(&q
->stream
);
396 videobuf_queue_is_busy(struct videobuf_queue
*q
)
401 dprintk(1,"busy: streaming active\n");
405 dprintk(1,"busy: pending read #1\n");
409 dprintk(1,"busy: pending read #2\n");
412 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
413 if (NULL
== q
->bufs
[i
])
415 if (q
->bufs
[i
]->map
) {
416 dprintk(1,"busy: buffer #%d mapped\n",i
);
419 if (q
->bufs
[i
]->state
== STATE_QUEUED
) {
420 dprintk(1,"busy: buffer #%d queued\n",i
);
423 if (q
->bufs
[i
]->state
== STATE_ACTIVE
) {
424 dprintk(1,"busy: buffer #%d avtive\n",i
);
432 videobuf_queue_cancel(struct videobuf_queue
*q
)
437 /* remove queued buffers from list */
438 spin_lock_irqsave(q
->irqlock
,flags
);
439 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
440 if (NULL
== q
->bufs
[i
])
442 if (q
->bufs
[i
]->state
== STATE_QUEUED
) {
443 list_del(&q
->bufs
[i
]->queue
);
444 q
->bufs
[i
]->state
= STATE_ERROR
;
447 spin_unlock_irqrestore(q
->irqlock
,flags
);
449 /* free all buffers + clear queue */
450 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
451 if (NULL
== q
->bufs
[i
])
453 q
->ops
->buf_release(q
,q
->bufs
[i
]);
455 INIT_LIST_HEAD(&q
->stream
);
458 /* --------------------------------------------------------------------- */
461 videobuf_next_field(struct videobuf_queue
*q
)
463 enum v4l2_field field
= q
->field
;
465 BUG_ON(V4L2_FIELD_ANY
== field
);
467 if (V4L2_FIELD_ALTERNATE
== field
) {
468 if (V4L2_FIELD_TOP
== q
->last
) {
469 field
= V4L2_FIELD_BOTTOM
;
470 q
->last
= V4L2_FIELD_BOTTOM
;
472 field
= V4L2_FIELD_TOP
;
473 q
->last
= V4L2_FIELD_TOP
;
480 videobuf_status(struct v4l2_buffer
*b
, struct videobuf_buffer
*vb
,
481 enum v4l2_buf_type type
)
483 MAGIC_CHECK(vb
->magic
,MAGIC_BUFFER
);
488 b
->memory
= vb
->memory
;
490 case V4L2_MEMORY_MMAP
:
491 b
->m
.offset
= vb
->boff
;
492 b
->length
= vb
->bsize
;
494 case V4L2_MEMORY_USERPTR
:
495 b
->m
.userptr
= vb
->baddr
;
496 b
->length
= vb
->bsize
;
498 case V4L2_MEMORY_OVERLAY
:
499 b
->m
.offset
= vb
->boff
;
505 b
->flags
|= V4L2_BUF_FLAG_MAPPED
;
511 b
->flags
|= V4L2_BUF_FLAG_QUEUED
;
515 b
->flags
|= V4L2_BUF_FLAG_DONE
;
517 case STATE_NEEDS_INIT
:
523 if (vb
->input
!= UNSET
) {
524 b
->flags
|= V4L2_BUF_FLAG_INPUT
;
525 b
->input
= vb
->input
;
528 b
->field
= vb
->field
;
529 b
->timestamp
= vb
->ts
;
530 b
->bytesused
= vb
->size
;
531 b
->sequence
= vb
->field_count
>> 1;
535 videobuf_reqbufs(struct videobuf_queue
*q
,
536 struct v4l2_requestbuffers
*req
)
538 unsigned int size
,count
;
541 if (req
->type
!= q
->type
)
545 if (req
->memory
!= V4L2_MEMORY_MMAP
&&
546 req
->memory
!= V4L2_MEMORY_USERPTR
&&
547 req
->memory
!= V4L2_MEMORY_OVERLAY
)
552 if (!list_empty(&q
->stream
))
557 if (count
> VIDEO_MAX_FRAME
)
558 count
= VIDEO_MAX_FRAME
;
560 q
->ops
->buf_setup(q
,&count
,&size
);
561 size
= PAGE_ALIGN(size
);
562 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
563 count
, size
, (count
*size
)>>PAGE_SHIFT
);
565 retval
= videobuf_mmap_setup(q
,count
,size
,req
->memory
);
577 videobuf_querybuf(struct videobuf_queue
*q
, struct v4l2_buffer
*b
)
579 if (unlikely(b
->type
!= q
->type
))
581 if (unlikely(b
->index
< 0 || b
->index
>= VIDEO_MAX_FRAME
))
583 if (unlikely(NULL
== q
->bufs
[b
->index
]))
585 videobuf_status(b
,q
->bufs
[b
->index
],q
->type
);
590 videobuf_qbuf(struct videobuf_queue
*q
,
591 struct v4l2_buffer
*b
)
593 struct videobuf_buffer
*buf
;
594 enum v4l2_field field
;
603 if (b
->type
!= q
->type
)
605 if (b
->index
< 0 || b
->index
>= VIDEO_MAX_FRAME
)
607 buf
= q
->bufs
[b
->index
];
610 MAGIC_CHECK(buf
->magic
,MAGIC_BUFFER
);
611 if (buf
->memory
!= b
->memory
)
613 if (buf
->state
== STATE_QUEUED
||
614 buf
->state
== STATE_ACTIVE
)
617 if (b
->flags
& V4L2_BUF_FLAG_INPUT
) {
618 if (b
->input
>= q
->inputs
)
620 buf
->input
= b
->input
;
626 case V4L2_MEMORY_MMAP
:
630 case V4L2_MEMORY_USERPTR
:
631 if (b
->length
< buf
->bsize
)
633 if (STATE_NEEDS_INIT
!= buf
->state
&& buf
->baddr
!= b
->m
.userptr
)
634 q
->ops
->buf_release(q
,buf
);
635 buf
->baddr
= b
->m
.userptr
;
637 case V4L2_MEMORY_OVERLAY
:
638 buf
->boff
= b
->m
.offset
;
644 field
= videobuf_next_field(q
);
645 retval
= q
->ops
->buf_prepare(q
,buf
,field
);
649 list_add_tail(&buf
->stream
,&q
->stream
);
651 spin_lock_irqsave(q
->irqlock
,flags
);
652 q
->ops
->buf_queue(q
,buf
);
653 spin_unlock_irqrestore(q
->irqlock
,flags
);
663 videobuf_dqbuf(struct videobuf_queue
*q
,
664 struct v4l2_buffer
*b
, int nonblocking
)
666 struct videobuf_buffer
*buf
;
674 if (b
->type
!= q
->type
)
676 if (list_empty(&q
->stream
))
678 buf
= list_entry(q
->stream
.next
, struct videobuf_buffer
, stream
);
679 retval
= videobuf_waiton(buf
, nonblocking
, 1);
682 switch (buf
->state
) {
687 videobuf_dma_pci_sync(q
->pci
,&buf
->dma
);
688 buf
->state
= STATE_IDLE
;
694 list_del(&buf
->stream
);
695 memset(b
,0,sizeof(*b
));
696 videobuf_status(b
,buf
,q
->type
);
703 int videobuf_streamon(struct videobuf_queue
*q
)
705 struct videobuf_buffer
*buf
;
706 struct list_head
*list
;
718 spin_lock_irqsave(q
->irqlock
,flags
);
719 list_for_each(list
,&q
->stream
) {
720 buf
= list_entry(list
, struct videobuf_buffer
, stream
);
721 if (buf
->state
== STATE_PREPARED
)
722 q
->ops
->buf_queue(q
,buf
);
724 spin_unlock_irqrestore(q
->irqlock
,flags
);
731 int videobuf_streamoff(struct videobuf_queue
*q
)
733 int retval
= -EINVAL
;
738 videobuf_queue_cancel(q
);
748 videobuf_read_zerocopy(struct videobuf_queue
*q
, char __user
*data
,
749 size_t count
, loff_t
*ppos
)
751 enum v4l2_field field
;
756 q
->read_buf
= videobuf_alloc(q
->msize
);
757 if (NULL
== q
->read_buf
)
760 q
->read_buf
->memory
= V4L2_MEMORY_USERPTR
;
761 q
->read_buf
->baddr
= (unsigned long)data
;
762 q
->read_buf
->bsize
= count
;
763 field
= videobuf_next_field(q
);
764 retval
= q
->ops
->buf_prepare(q
,q
->read_buf
,field
);
768 /* start capture & wait */
769 spin_lock_irqsave(q
->irqlock
,flags
);
770 q
->ops
->buf_queue(q
,q
->read_buf
);
771 spin_unlock_irqrestore(q
->irqlock
,flags
);
772 retval
= videobuf_waiton(q
->read_buf
,0,0);
774 videobuf_dma_pci_sync(q
->pci
,&q
->read_buf
->dma
);
775 if (STATE_ERROR
== q
->read_buf
->state
)
778 retval
= q
->read_buf
->size
;
783 q
->ops
->buf_release(q
,q
->read_buf
);
789 ssize_t
videobuf_read_one(struct videobuf_queue
*q
,
790 char __user
*data
, size_t count
, loff_t
*ppos
,
793 enum v4l2_field field
;
795 unsigned size
, nbufs
, bytes
;
801 q
->ops
->buf_setup(q
,&nbufs
,&size
);
802 if (NULL
== q
->read_buf
&&
805 retval
= videobuf_read_zerocopy(q
,data
,count
,ppos
);
806 if (retval
>= 0 || retval
== -EIO
)
809 /* fallback to kernel bounce buffer on failures */
812 if (NULL
== q
->read_buf
) {
813 /* need to capture a new frame */
815 q
->read_buf
= videobuf_alloc(q
->msize
);
816 if (NULL
== q
->read_buf
)
818 q
->read_buf
->memory
= V4L2_MEMORY_USERPTR
;
819 q
->read_buf
->bsize
= count
; /* preferred size */
820 field
= videobuf_next_field(q
);
821 retval
= q
->ops
->buf_prepare(q
,q
->read_buf
,field
);
827 spin_lock_irqsave(q
->irqlock
,flags
);
828 q
->ops
->buf_queue(q
,q
->read_buf
);
829 spin_unlock_irqrestore(q
->irqlock
,flags
);
833 /* wait until capture is done */
834 retval
= videobuf_waiton(q
->read_buf
, nonblocking
, 1);
837 videobuf_dma_pci_sync(q
->pci
,&q
->read_buf
->dma
);
839 if (STATE_ERROR
== q
->read_buf
->state
) {
840 /* catch I/O errors */
841 q
->ops
->buf_release(q
,q
->read_buf
);
848 /* copy to userspace */
850 if (bytes
> q
->read_buf
->size
- q
->read_off
)
851 bytes
= q
->read_buf
->size
- q
->read_off
;
853 if (copy_to_user(data
, q
->read_buf
->dma
.vmalloc
+q
->read_off
, bytes
))
857 q
->read_off
+= bytes
;
858 if (q
->read_off
== q
->read_buf
->size
) {
859 /* all data copied, cleanup */
860 q
->ops
->buf_release(q
,q
->read_buf
);
870 int videobuf_read_start(struct videobuf_queue
*q
)
872 enum v4l2_field field
;
874 int count
= 0, size
= 0;
877 q
->ops
->buf_setup(q
,&count
,&size
);
880 if (count
> VIDEO_MAX_FRAME
)
881 count
= VIDEO_MAX_FRAME
;
882 size
= PAGE_ALIGN(size
);
884 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 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 ssize_t
videobuf_read_stream(struct videobuf_queue
*q
,
920 char __user
*data
, size_t count
, loff_t
*ppos
,
921 int vbihack
, int nonblocking
)
923 unsigned int *fc
, bytes
;
927 dprintk(2,"%s\n",__FUNCTION__
);
933 retval
= videobuf_read_start(q
);
940 /* get / wait for data */
941 if (NULL
== q
->read_buf
) {
942 q
->read_buf
= list_entry(q
->stream
.next
,
943 struct videobuf_buffer
,
945 list_del(&q
->read_buf
->stream
);
948 err
= videobuf_waiton(q
->read_buf
, nonblocking
, 1);
955 if (q
->read_buf
->state
== STATE_DONE
) {
957 /* dirty, undocumented hack -- pass the frame counter
958 * within the last four bytes of each vbi data block.
959 * We need that one to maintain backward compatibility
960 * to all vbi decoding software out there ... */
961 fc
= (unsigned int*)q
->read_buf
->dma
.vmalloc
;
962 fc
+= (q
->read_buf
->size
>>2) -1;
963 *fc
= q
->read_buf
->field_count
>> 1;
964 dprintk(1,"vbihack: %d\n",*fc
);
969 if (bytes
> q
->read_buf
->size
- q
->read_off
)
970 bytes
= q
->read_buf
->size
- q
->read_off
;
971 if (copy_to_user(data
+ retval
,
972 q
->read_buf
->dma
.vmalloc
+ q
->read_off
,
980 q
->read_off
+= bytes
;
983 q
->read_off
= q
->read_buf
->size
;
988 /* requeue buffer when done with copying */
989 if (q
->read_off
== q
->read_buf
->size
) {
990 list_add_tail(&q
->read_buf
->stream
,
992 spin_lock_irqsave(q
->irqlock
,flags
);
993 q
->ops
->buf_queue(q
,q
->read_buf
);
994 spin_unlock_irqrestore(q
->irqlock
,flags
);
1006 unsigned int videobuf_poll_stream(struct file
*file
,
1007 struct videobuf_queue
*q
,
1010 struct videobuf_buffer
*buf
= NULL
;
1011 unsigned int rc
= 0;
1015 if (!list_empty(&q
->stream
))
1016 buf
= list_entry(q
->stream
.next
,
1017 struct videobuf_buffer
, stream
);
1020 videobuf_read_start(q
);
1023 } else if (NULL
== q
->read_buf
) {
1024 q
->read_buf
= list_entry(q
->stream
.next
,
1025 struct videobuf_buffer
,
1027 list_del(&q
->read_buf
->stream
);
1036 poll_wait(file
, &buf
->done
, wait
);
1037 if (buf
->state
== STATE_DONE
||
1038 buf
->state
== STATE_ERROR
)
1039 rc
= POLLIN
|POLLRDNORM
;
1045 /* --------------------------------------------------------------------- */
1048 videobuf_vm_open(struct vm_area_struct
*vma
)
1050 struct videobuf_mapping
*map
= vma
->vm_private_data
;
1052 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map
,
1053 map
->count
,vma
->vm_start
,vma
->vm_end
);
1058 videobuf_vm_close(struct vm_area_struct
*vma
)
1060 struct videobuf_mapping
*map
= vma
->vm_private_data
;
1061 struct videobuf_queue
*q
= map
->q
;
1064 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map
,
1065 map
->count
,vma
->vm_start
,vma
->vm_end
);
1068 if (0 == map
->count
) {
1069 dprintk(1,"munmap %p q=%p\n",map
,q
);
1071 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
1072 if (NULL
== q
->bufs
[i
])
1076 if (q
->bufs
[i
]->map
!= map
)
1078 q
->bufs
[i
]->map
= NULL
;
1079 q
->bufs
[i
]->baddr
= 0;
1080 q
->ops
->buf_release(q
,q
->bufs
[i
]);
1089 * Get a anonymous page for the mapping. Make sure we can DMA to that
1090 * memory location with 32bit PCI devices (i.e. don't use highmem for
1091 * now ...). Bounce buffers don't work very well for the data rates
1092 * video capture has.
1095 videobuf_vm_nopage(struct vm_area_struct
*vma
, unsigned long vaddr
,
1100 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1101 vaddr
,vma
->vm_start
,vma
->vm_end
);
1102 if (vaddr
> vma
->vm_end
)
1103 return NOPAGE_SIGBUS
;
1104 page
= alloc_page(GFP_USER
);
1107 clear_user_page(page_address(page
), vaddr
, page
);
1109 *type
= VM_FAULT_MINOR
;
1113 static struct vm_operations_struct videobuf_vm_ops
=
1115 .open
= videobuf_vm_open
,
1116 .close
= videobuf_vm_close
,
1117 .nopage
= videobuf_vm_nopage
,
1120 int videobuf_mmap_setup(struct videobuf_queue
*q
,
1121 unsigned int bcount
, unsigned int bsize
,
1122 enum v4l2_memory memory
)
1127 err
= videobuf_mmap_free(q
);
1131 for (i
= 0; i
< bcount
; i
++) {
1132 q
->bufs
[i
] = videobuf_alloc(q
->msize
);
1134 q
->bufs
[i
]->input
= UNSET
;
1135 q
->bufs
[i
]->memory
= memory
;
1136 q
->bufs
[i
]->bsize
= bsize
;
1138 case V4L2_MEMORY_MMAP
:
1139 q
->bufs
[i
]->boff
= bsize
* i
;
1141 case V4L2_MEMORY_USERPTR
:
1142 case V4L2_MEMORY_OVERLAY
:
1147 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1152 int videobuf_mmap_free(struct videobuf_queue
*q
)
1156 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++)
1157 if (q
->bufs
[i
] && q
->bufs
[i
]->map
)
1159 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
1160 if (NULL
== q
->bufs
[i
])
1162 q
->ops
->buf_release(q
,q
->bufs
[i
]);
1169 int videobuf_mmap_mapper(struct videobuf_queue
*q
,
1170 struct vm_area_struct
*vma
)
1172 struct videobuf_mapping
*map
;
1173 unsigned int first
,last
,size
,i
;
1178 if (!(vma
->vm_flags
& VM_WRITE
)) {
1179 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1182 if (!(vma
->vm_flags
& VM_SHARED
)) {
1183 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1187 /* look for first buffer to map */
1188 for (first
= 0; first
< VIDEO_MAX_FRAME
; first
++) {
1189 if (NULL
== q
->bufs
[first
])
1191 if (V4L2_MEMORY_MMAP
!= q
->bufs
[first
]->memory
)
1193 if (q
->bufs
[first
]->boff
== (vma
->vm_pgoff
<< PAGE_SHIFT
))
1196 if (VIDEO_MAX_FRAME
== first
) {
1197 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1198 (vma
->vm_pgoff
<< PAGE_SHIFT
));
1202 /* look for last buffer to map */
1203 for (size
= 0, last
= first
; last
< VIDEO_MAX_FRAME
; last
++) {
1204 if (NULL
== q
->bufs
[last
])
1206 if (V4L2_MEMORY_MMAP
!= q
->bufs
[last
]->memory
)
1208 if (q
->bufs
[last
]->map
) {
1212 size
+= q
->bufs
[last
]->bsize
;
1213 if (size
== (vma
->vm_end
- vma
->vm_start
))
1216 if (VIDEO_MAX_FRAME
== last
) {
1217 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1218 (vma
->vm_end
- vma
->vm_start
));
1222 /* create mapping + update buffer list */
1224 map
= kmalloc(sizeof(struct videobuf_mapping
),GFP_KERNEL
);
1227 for (size
= 0, i
= first
; i
<= last
; size
+= q
->bufs
[i
++]->bsize
) {
1228 q
->bufs
[i
]->map
= map
;
1229 q
->bufs
[i
]->baddr
= vma
->vm_start
+ size
;
1232 map
->start
= vma
->vm_start
;
1233 map
->end
= vma
->vm_end
;
1235 vma
->vm_ops
= &videobuf_vm_ops
;
1236 vma
->vm_flags
|= VM_DONTEXPAND
| VM_RESERVED
;
1237 vma
->vm_flags
&= ~VM_IO
; /* using shared anonymous pages */
1238 vma
->vm_private_data
= map
;
1239 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1240 map
,q
,vma
->vm_start
,vma
->vm_end
,vma
->vm_pgoff
,first
,last
);
1248 /* --------------------------------------------------------------------- */
1250 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg
);
1252 EXPORT_SYMBOL_GPL(videobuf_dma_init
);
1253 EXPORT_SYMBOL_GPL(videobuf_dma_init_user
);
1254 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel
);
1255 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay
);
1256 EXPORT_SYMBOL_GPL(videobuf_dma_pci_map
);
1257 EXPORT_SYMBOL_GPL(videobuf_dma_pci_sync
);
1258 EXPORT_SYMBOL_GPL(videobuf_dma_pci_unmap
);
1259 EXPORT_SYMBOL_GPL(videobuf_dma_free
);
1261 EXPORT_SYMBOL_GPL(videobuf_alloc
);
1262 EXPORT_SYMBOL_GPL(videobuf_waiton
);
1263 EXPORT_SYMBOL_GPL(videobuf_iolock
);
1265 EXPORT_SYMBOL_GPL(videobuf_queue_init
);
1266 EXPORT_SYMBOL_GPL(videobuf_queue_cancel
);
1267 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy
);
1269 EXPORT_SYMBOL_GPL(videobuf_next_field
);
1270 EXPORT_SYMBOL_GPL(videobuf_status
);
1271 EXPORT_SYMBOL_GPL(videobuf_reqbufs
);
1272 EXPORT_SYMBOL_GPL(videobuf_querybuf
);
1273 EXPORT_SYMBOL_GPL(videobuf_qbuf
);
1274 EXPORT_SYMBOL_GPL(videobuf_dqbuf
);
1275 EXPORT_SYMBOL_GPL(videobuf_streamon
);
1276 EXPORT_SYMBOL_GPL(videobuf_streamoff
);
1278 EXPORT_SYMBOL_GPL(videobuf_read_start
);
1279 EXPORT_SYMBOL_GPL(videobuf_read_stop
);
1280 EXPORT_SYMBOL_GPL(videobuf_read_stream
);
1281 EXPORT_SYMBOL_GPL(videobuf_read_one
);
1282 EXPORT_SYMBOL_GPL(videobuf_poll_stream
);
1284 EXPORT_SYMBOL_GPL(videobuf_mmap_setup
);
1285 EXPORT_SYMBOL_GPL(videobuf_mmap_free
);
1286 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper
);