2 * helper functions for SG DMA video4linux capture buffers
4 * The functions expect the hardware being able to scatter gather
5 * (i.e. the buffers are not linear in physical memory, but fragmented
6 * into PAGE_SIZE chunks). They also assume the driver does not need
7 * to touch the video data.
9 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
11 * Highly based on video-buf written originally by:
12 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
13 * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
14 * (c) 2006 Ted Walther and John Sokol
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/vmalloc.h>
29 #include <linux/pagemap.h>
30 #include <linux/scatterlist.h>
32 #include <asm/pgtable.h>
34 #include <media/videobuf-dma-sg.h>
36 #define MAGIC_DMABUF 0x19721112
37 #define MAGIC_SG_MEM 0x17890714
39 #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
40 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
43 module_param(debug, int, 0644);
45 MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
46 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
47 MODULE_LICENSE("GPL");
49 #define dprintk(level, fmt, arg...) if (debug >= level) \
50 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
52 /* --------------------------------------------------------------------- */
55 videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
57 struct scatterlist *sglist;
61 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
64 sg_init_table(sglist, nr_pages);
65 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
66 pg = vmalloc_to_page(virt);
69 BUG_ON(PageHighMem(pg));
70 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
80 videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
82 struct scatterlist *sglist;
87 sglist = kmalloc(nr_pages * sizeof(*sglist), GFP_KERNEL);
90 sg_init_table(sglist, nr_pages);
92 if (PageHighMem(pages[0]))
93 /* DMA to highmem pages might not work */
95 sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset);
96 for (i = 1; i < nr_pages; i++) {
99 if (PageHighMem(pages[i]))
101 sg_set_page(&sglist[i], pages[i], PAGE_SIZE, 0);
106 dprintk(2,"sgl: oops - no page\n");
111 dprintk(2,"sgl: oops - highmem page\n");
116 /* --------------------------------------------------------------------- */
118 struct videobuf_dmabuf *videobuf_to_dma (struct videobuf_buffer *buf)
120 struct videobuf_dma_sg_memory *mem = buf->priv;
123 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
128 void videobuf_dma_init(struct videobuf_dmabuf *dma)
130 memset(dma,0,sizeof(*dma));
131 dma->magic = MAGIC_DMABUF;
134 static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
135 int direction, unsigned long data, unsigned long size)
137 unsigned long first,last;
140 dma->direction = direction;
141 switch (dma->direction) {
142 case DMA_FROM_DEVICE:
152 first = (data & PAGE_MASK) >> PAGE_SHIFT;
153 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
154 dma->offset = data & ~PAGE_MASK;
155 dma->nr_pages = last-first+1;
156 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
158 if (NULL == dma->pages)
160 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
161 data,size,dma->nr_pages);
163 err = get_user_pages(current,current->mm,
164 data & PAGE_MASK, dma->nr_pages,
165 rw == READ, 1, /* force */
168 if (err != dma->nr_pages) {
169 dma->nr_pages = (err >= 0) ? err : 0;
170 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
171 return err < 0 ? err : -EINVAL;
176 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
177 unsigned long data, unsigned long size)
180 down_read(¤t->mm->mmap_sem);
181 ret = videobuf_dma_init_user_locked(dma, direction, data, size);
182 up_read(¤t->mm->mmap_sem);
187 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
190 dprintk(1,"init kernel [%d pages]\n",nr_pages);
191 dma->direction = direction;
192 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
193 if (NULL == dma->vmalloc) {
194 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
197 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
198 (unsigned long)dma->vmalloc,
199 nr_pages << PAGE_SHIFT);
200 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
201 dma->nr_pages = nr_pages;
205 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
206 dma_addr_t addr, int nr_pages)
208 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
209 nr_pages,(unsigned long)addr);
210 dma->direction = direction;
214 dma->bus_addr = addr;
215 dma->nr_pages = nr_pages;
219 int videobuf_dma_map(struct videobuf_queue* q, struct videobuf_dmabuf *dma)
221 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
222 BUG_ON(0 == dma->nr_pages);
225 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
229 dma->sglist = videobuf_vmalloc_to_sg
230 (dma->vmalloc,dma->nr_pages);
233 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
234 if (NULL != dma->sglist) {
236 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
237 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
238 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
241 if (NULL == dma->sglist) {
242 dprintk(1,"scatterlist is NULL\n");
245 if (!dma->bus_addr) {
246 dma->sglen = dma_map_sg(q->dev, dma->sglist,
247 dma->nr_pages, dma->direction);
248 if (0 == dma->sglen) {
250 "%s: videobuf_map_sg failed\n",__func__);
260 int videobuf_dma_sync(struct videobuf_queue *q, struct videobuf_dmabuf *dma)
262 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
265 dma_sync_sg_for_cpu(q->dev, dma->sglist, dma->nr_pages, dma->direction);
269 int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
271 MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
275 dma_unmap_sg(q->dev, dma->sglist, dma->nr_pages, dma->direction);
283 int videobuf_dma_free(struct videobuf_dmabuf *dma)
285 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
290 for (i=0; i < dma->nr_pages; i++)
291 page_cache_release(dma->pages[i]);
302 dma->direction = DMA_NONE;
306 /* --------------------------------------------------------------------- */
308 int videobuf_sg_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
310 struct videobuf_queue q;
314 return videobuf_dma_map(&q, dma);
317 int videobuf_sg_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
319 struct videobuf_queue q;
323 return videobuf_dma_unmap(&q, dma);
326 /* --------------------------------------------------------------------- */
329 videobuf_vm_open(struct vm_area_struct *vma)
331 struct videobuf_mapping *map = vma->vm_private_data;
333 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
334 map->count,vma->vm_start,vma->vm_end);
339 videobuf_vm_close(struct vm_area_struct *vma)
341 struct videobuf_mapping *map = vma->vm_private_data;
342 struct videobuf_queue *q = map->q;
343 struct videobuf_dma_sg_memory *mem;
346 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
347 map->count,vma->vm_start,vma->vm_end);
350 if (0 == map->count) {
351 dprintk(1,"munmap %p q=%p\n",map,q);
352 mutex_lock(&q->vb_lock);
353 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
354 if (NULL == q->bufs[i])
356 mem=q->bufs[i]->priv;
361 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
363 if (q->bufs[i]->map != map)
365 q->bufs[i]->map = NULL;
366 q->bufs[i]->baddr = 0;
367 q->ops->buf_release(q,q->bufs[i]);
369 mutex_unlock(&q->vb_lock);
376 * Get a anonymous page for the mapping. Make sure we can DMA to that
377 * memory location with 32bit PCI devices (i.e. don't use highmem for
378 * now ...). Bounce buffers don't work very well for the data rates
382 videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
386 dprintk(3,"fault: fault @ %08lx [vma %08lx-%08lx]\n",
387 (unsigned long)vmf->virtual_address,vma->vm_start,vma->vm_end);
388 page = alloc_page(GFP_USER | __GFP_DMA32);
391 clear_user_page(page_address(page), (unsigned long)vmf->virtual_address,
397 static struct vm_operations_struct videobuf_vm_ops =
399 .open = videobuf_vm_open,
400 .close = videobuf_vm_close,
401 .fault = videobuf_vm_fault,
404 /* ---------------------------------------------------------------------
405 * SG handlers for the generic methods
408 /* Allocated area consists on 3 parts:
410 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
411 struct videobuf_dma_sg_memory
414 static void *__videobuf_alloc(size_t size)
416 struct videobuf_dma_sg_memory *mem;
417 struct videobuf_buffer *vb;
419 vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
421 mem = vb->priv = ((char *)vb)+size;
422 mem->magic=MAGIC_SG_MEM;
424 videobuf_dma_init(&mem->dma);
426 dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
427 __func__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
428 mem,(long)sizeof(*mem));
433 static void *__videobuf_to_vmalloc (struct videobuf_buffer *buf)
435 struct videobuf_dma_sg_memory *mem = buf->priv;
438 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
440 return mem->dma.vmalloc;
443 static int __videobuf_iolock (struct videobuf_queue* q,
444 struct videobuf_buffer *vb,
445 struct v4l2_framebuffer *fbuf)
449 struct videobuf_dma_sg_memory *mem = vb->priv;
452 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
454 switch (vb->memory) {
455 case V4L2_MEMORY_MMAP:
456 case V4L2_MEMORY_USERPTR:
457 if (0 == vb->baddr) {
458 /* no userspace addr -- kernel bounce buffer */
459 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
460 err = videobuf_dma_init_kernel( &mem->dma,
465 } else if (vb->memory == V4L2_MEMORY_USERPTR) {
466 /* dma directly to userspace */
467 err = videobuf_dma_init_user( &mem->dma,
469 vb->baddr,vb->bsize );
473 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
474 buffers can only be called from videobuf_qbuf
475 we take current->mm->mmap_sem there, to prevent
476 locking inversion, so don't take it here */
478 err = videobuf_dma_init_user_locked(&mem->dma,
480 vb->baddr, vb->bsize);
485 case V4L2_MEMORY_OVERLAY:
488 /* FIXME: need sanity checks for vb->boff */
490 * Using a double cast to avoid compiler warnings when
491 * building for PAE. Compiler doesn't like direct casting
492 * of a 32 bit ptr to 64 bit integer.
494 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
495 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
496 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
504 err = videobuf_dma_map(q, &mem->dma);
511 static int __videobuf_sync(struct videobuf_queue *q,
512 struct videobuf_buffer *buf)
514 struct videobuf_dma_sg_memory *mem = buf->priv;
516 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
518 return videobuf_dma_sync(q,&mem->dma);
521 static int __videobuf_mmap_free(struct videobuf_queue *q)
525 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
535 static int __videobuf_mmap_mapper(struct videobuf_queue *q,
536 struct vm_area_struct *vma)
538 struct videobuf_dma_sg_memory *mem;
539 struct videobuf_mapping *map;
540 unsigned int first,last,size,i;
544 if (!(vma->vm_flags & VM_WRITE)) {
545 dprintk(1,"mmap app bug: PROT_WRITE please\n");
548 if (!(vma->vm_flags & VM_SHARED)) {
549 dprintk(1,"mmap app bug: MAP_SHARED please\n");
553 /* This function maintains backwards compatibility with V4L1 and will
554 * map more than one buffer if the vma length is equal to the combined
555 * size of multiple buffers than it will map them together. See
556 * VIDIOCGMBUF in the v4l spec
558 * TODO: Allow drivers to specify if they support this mode
561 /* look for first buffer to map */
562 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
563 if (NULL == q->bufs[first])
565 mem=q->bufs[first]->priv;
567 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
569 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
571 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
574 if (VIDEO_MAX_FRAME == first) {
575 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
576 (vma->vm_pgoff << PAGE_SHIFT));
580 /* look for last buffer to map */
581 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
582 if (NULL == q->bufs[last])
584 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
586 if (q->bufs[last]->map) {
590 size += q->bufs[last]->bsize;
591 if (size == (vma->vm_end - vma->vm_start))
594 if (VIDEO_MAX_FRAME == last) {
595 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
596 (vma->vm_end - vma->vm_start));
600 /* create mapping + update buffer list */
602 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
607 for (i = first; i <= last; i++) {
608 if (NULL == q->bufs[i])
610 q->bufs[i]->map = map;
611 q->bufs[i]->baddr = vma->vm_start + size;
612 size += q->bufs[i]->bsize;
616 map->start = vma->vm_start;
617 map->end = vma->vm_end;
619 vma->vm_ops = &videobuf_vm_ops;
620 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
621 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
622 vma->vm_private_data = map;
623 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
624 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
631 static int __videobuf_copy_to_user ( struct videobuf_queue *q,
632 char __user *data, size_t count,
635 struct videobuf_dma_sg_memory *mem = q->read_buf->priv;
637 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
639 /* copy to userspace */
640 if (count > q->read_buf->size - q->read_off)
641 count = q->read_buf->size - q->read_off;
643 if (copy_to_user(data, mem->dma.vmalloc+q->read_off, count))
649 static int __videobuf_copy_stream ( struct videobuf_queue *q,
650 char __user *data, size_t count, size_t pos,
651 int vbihack, int nonblocking )
654 struct videobuf_dma_sg_memory *mem = q->read_buf->priv;
656 MAGIC_CHECK(mem->magic,MAGIC_SG_MEM);
659 /* dirty, undocumented hack -- pass the frame counter
660 * within the last four bytes of each vbi data block.
661 * We need that one to maintain backward compatibility
662 * to all vbi decoding software out there ... */
663 fc = (unsigned int*)mem->dma.vmalloc;
664 fc += (q->read_buf->size>>2) -1;
665 *fc = q->read_buf->field_count >> 1;
666 dprintk(1,"vbihack: %d\n",*fc);
669 /* copy stuff using the common method */
670 count = __videobuf_copy_to_user (q,data,count,nonblocking);
672 if ( (count==-EFAULT) && (0 == pos) )
678 static struct videobuf_qtype_ops sg_ops = {
679 .magic = MAGIC_QTYPE_OPS,
681 .alloc = __videobuf_alloc,
682 .iolock = __videobuf_iolock,
683 .sync = __videobuf_sync,
684 .mmap_free = __videobuf_mmap_free,
685 .mmap_mapper = __videobuf_mmap_mapper,
686 .video_copy_to_user = __videobuf_copy_to_user,
687 .copy_stream = __videobuf_copy_stream,
688 .vmalloc = __videobuf_to_vmalloc,
691 void *videobuf_sg_alloc(size_t size)
693 struct videobuf_queue q;
695 /* Required to make generic handler to call __videobuf_alloc */
700 return videobuf_alloc(&q);
703 void videobuf_queue_sg_init(struct videobuf_queue* q,
704 struct videobuf_queue_ops *ops,
707 enum v4l2_buf_type type,
708 enum v4l2_field field,
712 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
716 /* --------------------------------------------------------------------- */
718 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
720 EXPORT_SYMBOL_GPL(videobuf_to_dma);
721 EXPORT_SYMBOL_GPL(videobuf_dma_init);
722 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
723 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
724 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
725 EXPORT_SYMBOL_GPL(videobuf_dma_map);
726 EXPORT_SYMBOL_GPL(videobuf_dma_sync);
727 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
728 EXPORT_SYMBOL_GPL(videobuf_dma_free);
730 EXPORT_SYMBOL_GPL(videobuf_sg_dma_map);
731 EXPORT_SYMBOL_GPL(videobuf_sg_dma_unmap);
732 EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
734 EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);