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 = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
58 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
59 pg = vmalloc_to_page(virt);
62 BUG_ON(PageHighMem(pg));
64 sglist[i].length = PAGE_SIZE;
74 videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
76 struct scatterlist *sglist;
81 sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
87 if (PageHighMem(pages[0]))
88 /* DMA to highmem pages might not work */
90 sglist[0].page = pages[0];
91 sglist[0].offset = offset;
92 sglist[0].length = PAGE_SIZE - offset;
93 for (i = 1; i < nr_pages; i++) {
96 if (PageHighMem(pages[i]))
98 sglist[i].page = pages[i];
99 sglist[i].length = PAGE_SIZE;
104 dprintk(2,"sgl: oops - no page\n");
109 dprintk(2,"sgl: oops - highmem page\n");
114 /* --------------------------------------------------------------------- */
116 void videobuf_dma_init(struct videobuf_dmabuf *dma)
118 memset(dma,0,sizeof(*dma));
119 dma->magic = MAGIC_DMABUF;
122 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
123 unsigned long data, unsigned long size)
125 unsigned long first,last;
128 dma->direction = direction;
129 switch (dma->direction) {
130 case PCI_DMA_FROMDEVICE: rw = READ; break;
131 case PCI_DMA_TODEVICE: rw = WRITE; break;
135 first = (data & PAGE_MASK) >> PAGE_SHIFT;
136 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
137 dma->offset = data & ~PAGE_MASK;
138 dma->nr_pages = last-first+1;
139 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
141 if (NULL == dma->pages)
143 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
144 data,size,dma->nr_pages);
146 down_read(¤t->mm->mmap_sem);
147 err = get_user_pages(current,current->mm,
148 data & PAGE_MASK, dma->nr_pages,
149 rw == READ, 1, /* force */
151 up_read(¤t->mm->mmap_sem);
152 if (err != dma->nr_pages) {
153 dma->nr_pages = (err >= 0) ? err : 0;
154 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
155 return err < 0 ? err : -EINVAL;
160 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
163 dprintk(1,"init kernel [%d pages]\n",nr_pages);
164 dma->direction = direction;
165 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
166 if (NULL == dma->vmalloc) {
167 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
170 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
171 dma->nr_pages = nr_pages;
175 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
176 dma_addr_t addr, int nr_pages)
178 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
179 nr_pages,(unsigned long)addr);
180 dma->direction = direction;
184 dma->bus_addr = addr;
185 dma->nr_pages = nr_pages;
189 int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma)
191 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
192 BUG_ON(0 == dma->nr_pages);
195 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
199 dma->sglist = videobuf_vmalloc_to_sg
200 (dma->vmalloc,dma->nr_pages);
203 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
204 if (NULL != dma->sglist) {
206 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
207 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
208 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
211 if (NULL == dma->sglist) {
212 dprintk(1,"scatterlist is NULL\n");
216 if (!dma->bus_addr) {
217 dma->sglen = pci_map_sg(dev,dma->sglist,dma->nr_pages,
219 if (0 == dma->sglen) {
221 "%s: pci_map_sg failed\n",__FUNCTION__);
231 int videobuf_dma_pci_sync(struct pci_dev *dev, struct videobuf_dmabuf *dma)
233 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
237 pci_dma_sync_sg_for_cpu(dev,dma->sglist,dma->nr_pages,dma->direction);
241 int videobuf_dma_pci_unmap(struct pci_dev *dev, struct videobuf_dmabuf *dma)
243 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
248 pci_unmap_sg(dev,dma->sglist,dma->nr_pages,dma->direction);
255 int videobuf_dma_free(struct videobuf_dmabuf *dma)
257 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
262 for (i=0; i < dma->nr_pages; i++)
263 page_cache_release(dma->pages[i]);
274 dma->direction = PCI_DMA_NONE;
278 /* --------------------------------------------------------------------- */
280 void* videobuf_alloc(unsigned int size)
282 struct videobuf_buffer *vb;
284 vb = kzalloc(size,GFP_KERNEL);
286 videobuf_dma_init(&vb->dma);
287 init_waitqueue_head(&vb->done);
288 vb->magic = MAGIC_BUFFER;
293 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
296 DECLARE_WAITQUEUE(wait, current);
298 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
299 add_wait_queue(&vb->done, &wait);
300 while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) {
305 set_current_state(intr ? TASK_INTERRUPTIBLE
306 : TASK_UNINTERRUPTIBLE);
307 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
309 set_current_state(TASK_RUNNING);
310 if (intr && signal_pending(current)) {
311 dprintk(1,"buffer waiton: -EINTR\n");
316 remove_wait_queue(&vb->done, &wait);
321 videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb,
322 struct v4l2_framebuffer *fbuf)
327 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
328 switch (vb->memory) {
329 case V4L2_MEMORY_MMAP:
330 case V4L2_MEMORY_USERPTR:
331 if (0 == vb->baddr) {
332 /* no userspace addr -- kernel bounce buffer */
333 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
334 err = videobuf_dma_init_kernel(&vb->dma,PCI_DMA_FROMDEVICE,
339 /* dma directly to userspace */
340 err = videobuf_dma_init_user(&vb->dma,PCI_DMA_FROMDEVICE,
341 vb->baddr,vb->bsize);
346 case V4L2_MEMORY_OVERLAY:
349 /* FIXME: need sanity checks for vb->boff */
350 bus = (dma_addr_t)fbuf->base + vb->boff;
351 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
352 err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE,
360 err = videobuf_dma_pci_map(pci,&vb->dma);
367 /* --------------------------------------------------------------------- */
369 void videobuf_queue_init(struct videobuf_queue* q,
370 struct videobuf_queue_ops *ops,
373 enum v4l2_buf_type type,
374 enum v4l2_field field,
378 memset(q,0,sizeof(*q));
379 q->irqlock = irqlock;
387 mutex_init(&q->lock);
388 INIT_LIST_HEAD(&q->stream);
392 videobuf_queue_is_busy(struct videobuf_queue *q)
397 dprintk(1,"busy: streaming active\n");
401 dprintk(1,"busy: pending read #1\n");
405 dprintk(1,"busy: pending read #2\n");
408 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
409 if (NULL == q->bufs[i])
411 if (q->bufs[i]->map) {
412 dprintk(1,"busy: buffer #%d mapped\n",i);
415 if (q->bufs[i]->state == STATE_QUEUED) {
416 dprintk(1,"busy: buffer #%d queued\n",i);
419 if (q->bufs[i]->state == STATE_ACTIVE) {
420 dprintk(1,"busy: buffer #%d avtive\n",i);
428 videobuf_queue_cancel(struct videobuf_queue *q)
430 unsigned long flags=0;
433 /* remove queued buffers from list */
434 spin_lock_irqsave(q->irqlock,flags);
435 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
436 if (NULL == q->bufs[i])
438 if (q->bufs[i]->state == STATE_QUEUED) {
439 list_del(&q->bufs[i]->queue);
440 q->bufs[i]->state = STATE_ERROR;
443 spin_unlock_irqrestore(q->irqlock,flags);
445 /* free all buffers + clear queue */
446 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
447 if (NULL == q->bufs[i])
449 q->ops->buf_release(q,q->bufs[i]);
451 INIT_LIST_HEAD(&q->stream);
454 /* --------------------------------------------------------------------- */
457 videobuf_next_field(struct videobuf_queue *q)
459 enum v4l2_field field = q->field;
461 BUG_ON(V4L2_FIELD_ANY == field);
463 if (V4L2_FIELD_ALTERNATE == field) {
464 if (V4L2_FIELD_TOP == q->last) {
465 field = V4L2_FIELD_BOTTOM;
466 q->last = V4L2_FIELD_BOTTOM;
468 field = V4L2_FIELD_TOP;
469 q->last = V4L2_FIELD_TOP;
476 videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
477 enum v4l2_buf_type type)
479 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
484 b->memory = vb->memory;
486 case V4L2_MEMORY_MMAP:
487 b->m.offset = vb->boff;
488 b->length = vb->bsize;
490 case V4L2_MEMORY_USERPTR:
491 b->m.userptr = vb->baddr;
492 b->length = vb->bsize;
494 case V4L2_MEMORY_OVERLAY:
495 b->m.offset = vb->boff;
501 b->flags |= V4L2_BUF_FLAG_MAPPED;
507 b->flags |= V4L2_BUF_FLAG_QUEUED;
511 b->flags |= V4L2_BUF_FLAG_DONE;
513 case STATE_NEEDS_INIT:
519 if (vb->input != UNSET) {
520 b->flags |= V4L2_BUF_FLAG_INPUT;
521 b->input = vb->input;
524 b->field = vb->field;
525 b->timestamp = vb->ts;
526 b->bytesused = vb->size;
527 b->sequence = vb->field_count >> 1;
531 videobuf_reqbufs(struct videobuf_queue *q,
532 struct v4l2_requestbuffers *req)
534 unsigned int size,count;
537 if (req->type != q->type)
541 if (req->memory != V4L2_MEMORY_MMAP &&
542 req->memory != V4L2_MEMORY_USERPTR &&
543 req->memory != V4L2_MEMORY_OVERLAY)
548 if (!list_empty(&q->stream))
551 mutex_lock(&q->lock);
553 if (count > VIDEO_MAX_FRAME)
554 count = VIDEO_MAX_FRAME;
556 q->ops->buf_setup(q,&count,&size);
557 size = PAGE_ALIGN(size);
558 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
559 count, size, (count*size)>>PAGE_SHIFT);
561 retval = videobuf_mmap_setup(q,count,size,req->memory);
568 mutex_unlock(&q->lock);
573 videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
575 if (unlikely(b->type != q->type))
577 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME))
579 if (unlikely(NULL == q->bufs[b->index]))
581 videobuf_status(b,q->bufs[b->index],q->type);
586 videobuf_qbuf(struct videobuf_queue *q,
587 struct v4l2_buffer *b)
589 struct videobuf_buffer *buf;
590 enum v4l2_field field;
591 unsigned long flags=0;
594 mutex_lock(&q->lock);
599 if (b->type != q->type)
601 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME)
603 buf = q->bufs[b->index];
606 MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
607 if (buf->memory != b->memory)
609 if (buf->state == STATE_QUEUED ||
610 buf->state == STATE_ACTIVE)
613 if (b->flags & V4L2_BUF_FLAG_INPUT) {
614 if (b->input >= q->inputs)
616 buf->input = b->input;
622 case V4L2_MEMORY_MMAP:
626 case V4L2_MEMORY_USERPTR:
627 if (b->length < buf->bsize)
629 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
630 q->ops->buf_release(q,buf);
631 buf->baddr = b->m.userptr;
633 case V4L2_MEMORY_OVERLAY:
634 buf->boff = b->m.offset;
640 field = videobuf_next_field(q);
641 retval = q->ops->buf_prepare(q,buf,field);
645 list_add_tail(&buf->stream,&q->stream);
647 spin_lock_irqsave(q->irqlock,flags);
648 q->ops->buf_queue(q,buf);
649 spin_unlock_irqrestore(q->irqlock,flags);
654 mutex_unlock(&q->lock);
659 videobuf_dqbuf(struct videobuf_queue *q,
660 struct v4l2_buffer *b, int nonblocking)
662 struct videobuf_buffer *buf;
665 mutex_lock(&q->lock);
670 if (b->type != q->type)
672 if (list_empty(&q->stream))
674 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
675 retval = videobuf_waiton(buf, nonblocking, 1);
678 switch (buf->state) {
683 videobuf_dma_pci_sync(q->pci,&buf->dma);
684 buf->state = STATE_IDLE;
690 list_del(&buf->stream);
691 memset(b,0,sizeof(*b));
692 videobuf_status(b,buf,q->type);
695 mutex_unlock(&q->lock);
699 int videobuf_streamon(struct videobuf_queue *q)
701 struct videobuf_buffer *buf;
702 struct list_head *list;
703 unsigned long flags=0;
706 mutex_lock(&q->lock);
714 spin_lock_irqsave(q->irqlock,flags);
715 list_for_each(list,&q->stream) {
716 buf = list_entry(list, struct videobuf_buffer, stream);
717 if (buf->state == STATE_PREPARED)
718 q->ops->buf_queue(q,buf);
720 spin_unlock_irqrestore(q->irqlock,flags);
723 mutex_unlock(&q->lock);
727 int videobuf_streamoff(struct videobuf_queue *q)
729 int retval = -EINVAL;
731 mutex_lock(&q->lock);
734 videobuf_queue_cancel(q);
739 mutex_unlock(&q->lock);
744 videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
745 size_t count, loff_t *ppos)
747 enum v4l2_field field;
748 unsigned long flags=0;
752 q->read_buf = videobuf_alloc(q->msize);
753 if (NULL == q->read_buf)
756 q->read_buf->memory = V4L2_MEMORY_USERPTR;
757 q->read_buf->baddr = (unsigned long)data;
758 q->read_buf->bsize = count;
759 field = videobuf_next_field(q);
760 retval = q->ops->buf_prepare(q,q->read_buf,field);
764 /* start capture & wait */
765 spin_lock_irqsave(q->irqlock,flags);
766 q->ops->buf_queue(q,q->read_buf);
767 spin_unlock_irqrestore(q->irqlock,flags);
768 retval = videobuf_waiton(q->read_buf,0,0);
770 videobuf_dma_pci_sync(q->pci,&q->read_buf->dma);
771 if (STATE_ERROR == q->read_buf->state)
774 retval = q->read_buf->size;
779 q->ops->buf_release(q,q->read_buf);
785 ssize_t videobuf_read_one(struct videobuf_queue *q,
786 char __user *data, size_t count, loff_t *ppos,
789 enum v4l2_field field;
790 unsigned long flags=0;
791 unsigned size, nbufs, bytes;
794 mutex_lock(&q->lock);
797 q->ops->buf_setup(q,&nbufs,&size);
798 if (NULL == q->read_buf &&
801 retval = videobuf_read_zerocopy(q,data,count,ppos);
802 if (retval >= 0 || retval == -EIO)
805 /* fallback to kernel bounce buffer on failures */
808 if (NULL == q->read_buf) {
809 /* need to capture a new frame */
811 q->read_buf = videobuf_alloc(q->msize);
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);
823 spin_lock_irqsave(q->irqlock,flags);
824 q->ops->buf_queue(q,q->read_buf);
825 spin_unlock_irqrestore(q->irqlock,flags);
829 /* wait until capture is done */
830 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
833 videobuf_dma_pci_sync(q->pci,&q->read_buf->dma);
835 if (STATE_ERROR == q->read_buf->state) {
836 /* catch I/O errors */
837 q->ops->buf_release(q,q->read_buf);
844 /* copy to userspace */
846 if (bytes > q->read_buf->size - q->read_off)
847 bytes = q->read_buf->size - q->read_off;
849 if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes))
853 q->read_off += bytes;
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->lock);
866 int videobuf_read_start(struct videobuf_queue *q)
868 enum v4l2_field field;
869 unsigned long flags=0;
870 int count = 0, size = 0;
873 q->ops->buf_setup(q,&count,&size);
876 if (count > VIDEO_MAX_FRAME)
877 count = VIDEO_MAX_FRAME;
878 size = PAGE_ALIGN(size);
880 err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
883 for (i = 0; i < count; i++) {
884 field = videobuf_next_field(q);
885 err = q->ops->buf_prepare(q,q->bufs[i],field);
888 list_add_tail(&q->bufs[i]->stream, &q->stream);
890 spin_lock_irqsave(q->irqlock,flags);
891 for (i = 0; i < count; i++)
892 q->ops->buf_queue(q,q->bufs[i]);
893 spin_unlock_irqrestore(q->irqlock,flags);
898 void videobuf_read_stop(struct videobuf_queue *q)
902 videobuf_queue_cancel(q);
903 videobuf_mmap_free(q);
904 INIT_LIST_HEAD(&q->stream);
905 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
906 if (NULL == q->bufs[i])
915 ssize_t videobuf_read_stream(struct videobuf_queue *q,
916 char __user *data, size_t count, loff_t *ppos,
917 int vbihack, int nonblocking)
919 unsigned int *fc, bytes;
921 unsigned long flags=0;
923 dprintk(2,"%s\n",__FUNCTION__);
924 mutex_lock(&q->lock);
929 retval = videobuf_read_start(q);
936 /* get / wait for data */
937 if (NULL == q->read_buf) {
938 q->read_buf = list_entry(q->stream.next,
939 struct videobuf_buffer,
941 list_del(&q->read_buf->stream);
944 err = videobuf_waiton(q->read_buf, nonblocking, 1);
951 if (q->read_buf->state == STATE_DONE) {
953 /* dirty, undocumented hack -- pass the frame counter
954 * within the last four bytes of each vbi data block.
955 * We need that one to maintain backward compatibility
956 * to all vbi decoding software out there ... */
957 fc = (unsigned int*)q->read_buf->dma.vmalloc;
958 fc += (q->read_buf->size>>2) -1;
959 *fc = q->read_buf->field_count >> 1;
960 dprintk(1,"vbihack: %d\n",*fc);
965 if (bytes > q->read_buf->size - q->read_off)
966 bytes = q->read_buf->size - q->read_off;
967 if (copy_to_user(data + retval,
968 q->read_buf->dma.vmalloc + q->read_off,
976 q->read_off += bytes;
979 q->read_off = q->read_buf->size;
984 /* requeue buffer when done with copying */
985 if (q->read_off == q->read_buf->size) {
986 list_add_tail(&q->read_buf->stream,
988 spin_lock_irqsave(q->irqlock,flags);
989 q->ops->buf_queue(q,q->read_buf);
990 spin_unlock_irqrestore(q->irqlock,flags);
998 mutex_unlock(&q->lock);
1002 unsigned int videobuf_poll_stream(struct file *file,
1003 struct videobuf_queue *q,
1006 struct videobuf_buffer *buf = NULL;
1007 unsigned int rc = 0;
1009 mutex_lock(&q->lock);
1011 if (!list_empty(&q->stream))
1012 buf = list_entry(q->stream.next,
1013 struct videobuf_buffer, stream);
1016 videobuf_read_start(q);
1019 } else if (NULL == q->read_buf) {
1020 q->read_buf = list_entry(q->stream.next,
1021 struct videobuf_buffer,
1023 list_del(&q->read_buf->stream);
1032 poll_wait(file, &buf->done, wait);
1033 if (buf->state == STATE_DONE ||
1034 buf->state == STATE_ERROR)
1035 rc = POLLIN|POLLRDNORM;
1037 mutex_unlock(&q->lock);
1041 /* --------------------------------------------------------------------- */
1044 videobuf_vm_open(struct vm_area_struct *vma)
1046 struct videobuf_mapping *map = vma->vm_private_data;
1048 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
1049 map->count,vma->vm_start,vma->vm_end);
1054 videobuf_vm_close(struct vm_area_struct *vma)
1056 struct videobuf_mapping *map = vma->vm_private_data;
1057 struct videobuf_queue *q = map->q;
1060 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1061 map->count,vma->vm_start,vma->vm_end);
1064 if (0 == map->count) {
1065 dprintk(1,"munmap %p q=%p\n",map,q);
1066 mutex_lock(&q->lock);
1067 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1068 if (NULL == q->bufs[i])
1072 if (q->bufs[i]->map != map)
1074 q->bufs[i]->map = NULL;
1075 q->bufs[i]->baddr = 0;
1076 q->ops->buf_release(q,q->bufs[i]);
1078 mutex_unlock(&q->lock);
1085 * Get a anonymous page for the mapping. Make sure we can DMA to that
1086 * memory location with 32bit PCI devices (i.e. don't use highmem for
1087 * now ...). Bounce buffers don't work very well for the data rates
1088 * video capture has.
1091 videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1096 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1097 vaddr,vma->vm_start,vma->vm_end);
1098 if (vaddr > vma->vm_end)
1099 return NOPAGE_SIGBUS;
1100 page = alloc_page(GFP_USER);
1103 clear_user_page(page_address(page), vaddr, page);
1105 *type = VM_FAULT_MINOR;
1109 static struct vm_operations_struct videobuf_vm_ops =
1111 .open = videobuf_vm_open,
1112 .close = videobuf_vm_close,
1113 .nopage = videobuf_vm_nopage,
1116 int videobuf_mmap_setup(struct videobuf_queue *q,
1117 unsigned int bcount, unsigned int bsize,
1118 enum v4l2_memory memory)
1123 err = videobuf_mmap_free(q);
1127 for (i = 0; i < bcount; i++) {
1128 q->bufs[i] = videobuf_alloc(q->msize);
1130 q->bufs[i]->input = UNSET;
1131 q->bufs[i]->memory = memory;
1132 q->bufs[i]->bsize = bsize;
1134 case V4L2_MEMORY_MMAP:
1135 q->bufs[i]->boff = bsize * i;
1137 case V4L2_MEMORY_USERPTR:
1138 case V4L2_MEMORY_OVERLAY:
1143 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1148 int videobuf_mmap_free(struct videobuf_queue *q)
1152 for (i = 0; i < VIDEO_MAX_FRAME; i++)
1153 if (q->bufs[i] && q->bufs[i]->map)
1155 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1156 if (NULL == q->bufs[i])
1158 q->ops->buf_release(q,q->bufs[i]);
1165 int videobuf_mmap_mapper(struct videobuf_queue *q,
1166 struct vm_area_struct *vma)
1168 struct videobuf_mapping *map;
1169 unsigned int first,last,size,i;
1172 mutex_lock(&q->lock);
1174 if (!(vma->vm_flags & VM_WRITE)) {
1175 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1178 if (!(vma->vm_flags & VM_SHARED)) {
1179 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1183 /* look for first buffer to map */
1184 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1185 if (NULL == q->bufs[first])
1187 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
1189 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1192 if (VIDEO_MAX_FRAME == first) {
1193 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1194 (vma->vm_pgoff << PAGE_SHIFT));
1198 /* look for last buffer to map */
1199 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1200 if (NULL == q->bufs[last])
1202 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
1204 if (q->bufs[last]->map) {
1208 size += q->bufs[last]->bsize;
1209 if (size == (vma->vm_end - vma->vm_start))
1212 if (VIDEO_MAX_FRAME == last) {
1213 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1214 (vma->vm_end - vma->vm_start));
1218 /* create mapping + update buffer list */
1220 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
1223 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
1224 q->bufs[i]->map = map;
1225 q->bufs[i]->baddr = vma->vm_start + size;
1228 map->start = vma->vm_start;
1229 map->end = vma->vm_end;
1231 vma->vm_ops = &videobuf_vm_ops;
1232 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
1233 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
1234 vma->vm_private_data = map;
1235 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1236 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
1240 mutex_unlock(&q->lock);
1244 /* --------------------------------------------------------------------- */
1246 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1248 EXPORT_SYMBOL_GPL(videobuf_dma_init);
1249 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1250 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1251 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
1252 EXPORT_SYMBOL_GPL(videobuf_dma_pci_map);
1253 EXPORT_SYMBOL_GPL(videobuf_dma_pci_sync);
1254 EXPORT_SYMBOL_GPL(videobuf_dma_pci_unmap);
1255 EXPORT_SYMBOL_GPL(videobuf_dma_free);
1257 EXPORT_SYMBOL_GPL(videobuf_alloc);
1258 EXPORT_SYMBOL_GPL(videobuf_waiton);
1259 EXPORT_SYMBOL_GPL(videobuf_iolock);
1261 EXPORT_SYMBOL_GPL(videobuf_queue_init);
1262 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1263 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1265 EXPORT_SYMBOL_GPL(videobuf_next_field);
1266 EXPORT_SYMBOL_GPL(videobuf_status);
1267 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1268 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1269 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1270 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1271 EXPORT_SYMBOL_GPL(videobuf_streamon);
1272 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1274 EXPORT_SYMBOL_GPL(videobuf_read_start);
1275 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1276 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1277 EXPORT_SYMBOL_GPL(videobuf_read_one);
1278 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1280 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1281 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1282 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);