2 * helper functions for physically contiguous capture buffers
4 * The functions support hardware lacking scatter gather support
5 * (i.e. the buffers must be linear in physical memory)
7 * Copyright (c) 2008 Magnus Damm
9 * Based on videobuf-vmalloc.c,
10 * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2
17 #include <linux/init.h>
18 #include <linux/module.h>
20 #include <linux/pagemap.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <media/videobuf-dma-contig.h>
26 struct videobuf_dma_contig_memory
{
29 dma_addr_t dma_handle
;
34 #define MAGIC_DC_MEM 0x0733ac61
35 #define MAGIC_CHECK(is, should) \
36 if (unlikely((is) != (should))) { \
37 pr_err("magic mismatch: %x expected %x\n", (is), (should)); \
42 videobuf_vm_open(struct vm_area_struct
*vma
)
44 struct videobuf_mapping
*map
= vma
->vm_private_data
;
46 dev_dbg(map
->q
->dev
, "vm_open %p [count=%u,vma=%08lx-%08lx]\n",
47 map
, map
->count
, vma
->vm_start
, vma
->vm_end
);
52 static void videobuf_vm_close(struct vm_area_struct
*vma
)
54 struct videobuf_mapping
*map
= vma
->vm_private_data
;
55 struct videobuf_queue
*q
= map
->q
;
58 dev_dbg(map
->q
->dev
, "vm_close %p [count=%u,vma=%08lx-%08lx]\n",
59 map
, map
->count
, vma
->vm_start
, vma
->vm_end
);
62 if (0 == map
->count
) {
63 struct videobuf_dma_contig_memory
*mem
;
65 dev_dbg(map
->q
->dev
, "munmap %p q=%p\n", map
, q
);
66 mutex_lock(&q
->vb_lock
);
68 /* We need first to cancel streams, before unmapping */
70 videobuf_queue_cancel(q
);
72 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
73 if (NULL
== q
->bufs
[i
])
76 if (q
->bufs
[i
]->map
!= map
)
79 mem
= q
->bufs
[i
]->priv
;
81 /* This callback is called only if kernel has
82 allocated memory and this memory is mmapped.
83 In this case, memory should be freed,
84 in order to do memory unmap.
87 MAGIC_CHECK(mem
->magic
, MAGIC_DC_MEM
);
89 /* vfree is not atomic - can't be
90 called with IRQ's disabled
92 dev_dbg(map
->q
->dev
, "buf[%d] freeing %p\n",
95 dma_free_coherent(q
->dev
, mem
->size
,
96 mem
->vaddr
, mem
->dma_handle
);
100 q
->bufs
[i
]->map
= NULL
;
101 q
->bufs
[i
]->baddr
= 0;
106 mutex_unlock(&q
->vb_lock
);
110 static const struct vm_operations_struct videobuf_vm_ops
= {
111 .open
= videobuf_vm_open
,
112 .close
= videobuf_vm_close
,
116 * videobuf_dma_contig_user_put() - reset pointer to user space buffer
117 * @mem: per-buffer private videobuf-dma-contig data
119 * This function resets the user space pointer
121 static void videobuf_dma_contig_user_put(struct videobuf_dma_contig_memory
*mem
)
129 * videobuf_dma_contig_user_get() - setup user space memory pointer
130 * @mem: per-buffer private videobuf-dma-contig data
131 * @vb: video buffer to map
133 * This function validates and sets up a pointer to user space memory.
134 * Only physically contiguous pfn-mapped memory is accepted.
136 * Returns 0 if successful.
138 static int videobuf_dma_contig_user_get(struct videobuf_dma_contig_memory
*mem
,
139 struct videobuf_buffer
*vb
)
141 struct mm_struct
*mm
= current
->mm
;
142 struct vm_area_struct
*vma
;
143 unsigned long prev_pfn
, this_pfn
;
144 unsigned long pages_done
, user_address
;
148 offset
= vb
->baddr
& ~PAGE_MASK
;
149 mem
->size
= PAGE_ALIGN(vb
->size
+ offset
);
153 down_read(&mm
->mmap_sem
);
155 vma
= find_vma(mm
, vb
->baddr
);
159 if ((vb
->baddr
+ mem
->size
) > vma
->vm_end
)
163 prev_pfn
= 0; /* kill warning */
164 user_address
= vb
->baddr
;
166 while (pages_done
< (mem
->size
>> PAGE_SHIFT
)) {
167 ret
= follow_pfn(vma
, user_address
, &this_pfn
);
172 mem
->dma_handle
= (this_pfn
<< PAGE_SHIFT
) + offset
;
173 else if (this_pfn
!= (prev_pfn
+ 1))
180 user_address
+= PAGE_SIZE
;
188 up_read(¤t
->mm
->mmap_sem
);
193 static void *__videobuf_alloc(size_t size
)
195 struct videobuf_dma_contig_memory
*mem
;
196 struct videobuf_buffer
*vb
;
198 vb
= kzalloc(size
+ sizeof(*mem
), GFP_KERNEL
);
200 mem
= vb
->priv
= ((char *)vb
) + size
;
201 mem
->magic
= MAGIC_DC_MEM
;
207 static void *__videobuf_to_vmalloc(struct videobuf_buffer
*buf
)
209 struct videobuf_dma_contig_memory
*mem
= buf
->priv
;
212 MAGIC_CHECK(mem
->magic
, MAGIC_DC_MEM
);
217 static int __videobuf_iolock(struct videobuf_queue
*q
,
218 struct videobuf_buffer
*vb
,
219 struct v4l2_framebuffer
*fbuf
)
221 struct videobuf_dma_contig_memory
*mem
= vb
->priv
;
224 MAGIC_CHECK(mem
->magic
, MAGIC_DC_MEM
);
226 switch (vb
->memory
) {
227 case V4L2_MEMORY_MMAP
:
228 dev_dbg(q
->dev
, "%s memory method MMAP\n", __func__
);
230 /* All handling should be done by __videobuf_mmap_mapper() */
232 dev_err(q
->dev
, "memory is not alloced/mmapped.\n");
236 case V4L2_MEMORY_USERPTR
:
237 dev_dbg(q
->dev
, "%s memory method USERPTR\n", __func__
);
239 /* handle pointer from user space */
241 return videobuf_dma_contig_user_get(mem
, vb
);
243 /* allocate memory for the read() method */
244 mem
->size
= PAGE_ALIGN(vb
->size
);
245 mem
->vaddr
= dma_alloc_coherent(q
->dev
, mem
->size
,
246 &mem
->dma_handle
, GFP_KERNEL
);
248 dev_err(q
->dev
, "dma_alloc_coherent %ld failed\n",
253 dev_dbg(q
->dev
, "dma_alloc_coherent data is at %p (%ld)\n",
254 mem
->vaddr
, mem
->size
);
256 case V4L2_MEMORY_OVERLAY
:
258 dev_dbg(q
->dev
, "%s memory method OVERLAY/unknown\n",
266 static int __videobuf_mmap_free(struct videobuf_queue
*q
)
270 dev_dbg(q
->dev
, "%s\n", __func__
);
271 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++) {
272 if (q
->bufs
[i
] && q
->bufs
[i
]->map
)
279 static int __videobuf_mmap_mapper(struct videobuf_queue
*q
,
280 struct vm_area_struct
*vma
)
282 struct videobuf_dma_contig_memory
*mem
;
283 struct videobuf_mapping
*map
;
286 unsigned long size
, offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
288 dev_dbg(q
->dev
, "%s\n", __func__
);
289 if (!(vma
->vm_flags
& VM_WRITE
) || !(vma
->vm_flags
& VM_SHARED
))
292 /* look for first buffer to map */
293 for (first
= 0; first
< VIDEO_MAX_FRAME
; first
++) {
297 if (V4L2_MEMORY_MMAP
!= q
->bufs
[first
]->memory
)
299 if (q
->bufs
[first
]->boff
== offset
)
302 if (VIDEO_MAX_FRAME
== first
) {
303 dev_dbg(q
->dev
, "invalid user space offset [offset=0x%lx]\n",
308 /* create mapping + update buffer list */
309 map
= kzalloc(sizeof(struct videobuf_mapping
), GFP_KERNEL
);
313 q
->bufs
[first
]->map
= map
;
314 map
->start
= vma
->vm_start
;
315 map
->end
= vma
->vm_end
;
318 q
->bufs
[first
]->baddr
= vma
->vm_start
;
320 mem
= q
->bufs
[first
]->priv
;
322 MAGIC_CHECK(mem
->magic
, MAGIC_DC_MEM
);
324 mem
->size
= PAGE_ALIGN(q
->bufs
[first
]->bsize
);
325 mem
->vaddr
= dma_alloc_coherent(q
->dev
, mem
->size
,
326 &mem
->dma_handle
, GFP_KERNEL
);
328 dev_err(q
->dev
, "dma_alloc_coherent size %ld failed\n",
332 dev_dbg(q
->dev
, "dma_alloc_coherent data is at addr %p (size %ld)\n",
333 mem
->vaddr
, mem
->size
);
335 /* Try to remap memory */
337 size
= vma
->vm_end
- vma
->vm_start
;
338 size
= (size
< mem
->size
) ? size
: mem
->size
;
340 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
341 retval
= remap_pfn_range(vma
, vma
->vm_start
,
342 mem
->dma_handle
>> PAGE_SHIFT
,
343 size
, vma
->vm_page_prot
);
345 dev_err(q
->dev
, "mmap: remap failed with error %d. ", retval
);
346 dma_free_coherent(q
->dev
, mem
->size
,
347 mem
->vaddr
, mem
->dma_handle
);
351 vma
->vm_ops
= &videobuf_vm_ops
;
352 vma
->vm_flags
|= VM_DONTEXPAND
;
353 vma
->vm_private_data
= map
;
355 dev_dbg(q
->dev
, "mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
356 map
, q
, vma
->vm_start
, vma
->vm_end
,
357 (long int) q
->bufs
[first
]->bsize
,
358 vma
->vm_pgoff
, first
);
360 videobuf_vm_open(vma
);
369 static int __videobuf_copy_to_user(struct videobuf_queue
*q
,
370 char __user
*data
, size_t count
,
373 struct videobuf_dma_contig_memory
*mem
= q
->read_buf
->priv
;
377 MAGIC_CHECK(mem
->magic
, MAGIC_DC_MEM
);
380 /* copy to userspace */
381 if (count
> q
->read_buf
->size
- q
->read_off
)
382 count
= q
->read_buf
->size
- q
->read_off
;
386 if (copy_to_user(data
, vaddr
+ q
->read_off
, count
))
392 static int __videobuf_copy_stream(struct videobuf_queue
*q
,
393 char __user
*data
, size_t count
, size_t pos
,
394 int vbihack
, int nonblocking
)
397 struct videobuf_dma_contig_memory
*mem
= q
->read_buf
->priv
;
400 MAGIC_CHECK(mem
->magic
, MAGIC_DC_MEM
);
403 /* dirty, undocumented hack -- pass the frame counter
404 * within the last four bytes of each vbi data block.
405 * We need that one to maintain backward compatibility
406 * to all vbi decoding software out there ... */
407 fc
= (unsigned int *)mem
->vaddr
;
408 fc
+= (q
->read_buf
->size
>> 2) - 1;
409 *fc
= q
->read_buf
->field_count
>> 1;
410 dev_dbg(q
->dev
, "vbihack: %d\n", *fc
);
413 /* copy stuff using the common method */
414 count
= __videobuf_copy_to_user(q
, data
, count
, nonblocking
);
416 if ((count
== -EFAULT
) && (pos
== 0))
422 static struct videobuf_qtype_ops qops
= {
423 .magic
= MAGIC_QTYPE_OPS
,
425 .alloc
= __videobuf_alloc
,
426 .iolock
= __videobuf_iolock
,
427 .mmap_free
= __videobuf_mmap_free
,
428 .mmap_mapper
= __videobuf_mmap_mapper
,
429 .video_copy_to_user
= __videobuf_copy_to_user
,
430 .copy_stream
= __videobuf_copy_stream
,
431 .vmalloc
= __videobuf_to_vmalloc
,
434 void videobuf_queue_dma_contig_init(struct videobuf_queue
*q
,
435 const struct videobuf_queue_ops
*ops
,
438 enum v4l2_buf_type type
,
439 enum v4l2_field field
,
443 videobuf_queue_core_init(q
, ops
, dev
, irqlock
, type
, field
, msize
,
446 EXPORT_SYMBOL_GPL(videobuf_queue_dma_contig_init
);
448 dma_addr_t
videobuf_to_dma_contig(struct videobuf_buffer
*buf
)
450 struct videobuf_dma_contig_memory
*mem
= buf
->priv
;
453 MAGIC_CHECK(mem
->magic
, MAGIC_DC_MEM
);
455 return mem
->dma_handle
;
457 EXPORT_SYMBOL_GPL(videobuf_to_dma_contig
);
459 void videobuf_dma_contig_free(struct videobuf_queue
*q
,
460 struct videobuf_buffer
*buf
)
462 struct videobuf_dma_contig_memory
*mem
= buf
->priv
;
464 /* mmapped memory can't be freed here, otherwise mmapped region
465 would be released, while still needed. In this case, the memory
466 release should happen inside videobuf_vm_close().
467 So, it should free memory only if the memory were allocated for
470 if (buf
->memory
!= V4L2_MEMORY_USERPTR
)
476 MAGIC_CHECK(mem
->magic
, MAGIC_DC_MEM
);
478 /* handle user space pointer case */
480 videobuf_dma_contig_user_put(mem
);
485 dma_free_coherent(q
->dev
, mem
->size
, mem
->vaddr
, mem
->dma_handle
);
488 EXPORT_SYMBOL_GPL(videobuf_dma_contig_free
);
490 MODULE_DESCRIPTION("helper module to manage video4linux dma contig buffers");
491 MODULE_AUTHOR("Magnus Damm");
492 MODULE_LICENSE("GPL");