Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 1 | /* |
| 2 | * helper functions for physically contiguous capture buffers |
| 3 | * |
| 4 | * The functions support hardware lacking scatter gather support |
| 5 | * (i.e. the buffers must be linear in physical memory) |
| 6 | * |
| 7 | * Copyright (c) 2008 Magnus Damm |
| 8 | * |
| 9 | * Based on videobuf-vmalloc.c, |
| 10 | * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org> |
| 11 | * |
| 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 |
| 15 | */ |
| 16 | |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
Hans Verkuil | f19ad39 | 2008-07-18 02:02:50 -0300 | [diff] [blame] | 19 | #include <linux/mm.h> |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 20 | #include <linux/pagemap.h> |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 21 | #include <linux/dma-mapping.h> |
Guennadi Liakhovetski | f39c1ab | 2009-11-09 16:11:34 -0300 | [diff] [blame] | 22 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 24 | #include <media/videobuf-dma-contig.h> |
| 25 | |
| 26 | struct videobuf_dma_contig_memory { |
| 27 | u32 magic; |
| 28 | void *vaddr; |
| 29 | dma_addr_t dma_handle; |
| 30 | unsigned long size; |
| 31 | }; |
| 32 | |
| 33 | #define MAGIC_DC_MEM 0x0733ac61 |
Guennadi Liakhovetski | c60f2b5 | 2008-07-17 17:30:47 -0300 | [diff] [blame] | 34 | #define MAGIC_CHECK(is, should) \ |
| 35 | if (unlikely((is) != (should))) { \ |
| 36 | pr_err("magic mismatch: %x expected %x\n", (is), (should)); \ |
| 37 | BUG(); \ |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 38 | } |
| 39 | |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 40 | static int __videobuf_dc_alloc(struct device *dev, |
| 41 | struct videobuf_dma_contig_memory *mem, |
Dan Carpenter | 50fbe32 | 2012-05-24 11:56:41 -0300 | [diff] [blame] | 42 | unsigned long size, gfp_t flags) |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 43 | { |
| 44 | mem->size = size; |
Mauro Carvalho Chehab | cb132cd | 2013-04-17 08:22:15 -0300 | [diff] [blame] | 45 | mem->vaddr = dma_alloc_coherent(dev, mem->size, |
| 46 | &mem->dma_handle, flags); |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 47 | |
| 48 | if (!mem->vaddr) { |
| 49 | dev_err(dev, "memory alloc size %ld failed\n", mem->size); |
| 50 | return -ENOMEM; |
| 51 | } |
| 52 | |
| 53 | dev_dbg(dev, "dma mapped data is at %p (%ld)\n", mem->vaddr, mem->size); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static void __videobuf_dc_free(struct device *dev, |
| 59 | struct videobuf_dma_contig_memory *mem) |
| 60 | { |
Mauro Carvalho Chehab | cb132cd | 2013-04-17 08:22:15 -0300 | [diff] [blame] | 61 | dma_free_coherent(dev, mem->size, mem->vaddr, mem->dma_handle); |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 62 | |
| 63 | mem->vaddr = NULL; |
| 64 | } |
| 65 | |
| 66 | static void videobuf_vm_open(struct vm_area_struct *vma) |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 67 | { |
| 68 | struct videobuf_mapping *map = vma->vm_private_data; |
| 69 | |
Hans Verkuil | cca36e2 | 2014-01-03 08:10:49 -0300 | [diff] [blame] | 70 | dev_dbg(map->q->dev, "vm_open %p [count=%u,vma=%08lx-%08lx]\n", |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 71 | map, map->count, vma->vm_start, vma->vm_end); |
| 72 | |
| 73 | map->count++; |
| 74 | } |
| 75 | |
| 76 | static void videobuf_vm_close(struct vm_area_struct *vma) |
| 77 | { |
| 78 | struct videobuf_mapping *map = vma->vm_private_data; |
| 79 | struct videobuf_queue *q = map->q; |
| 80 | int i; |
| 81 | |
Guennadi Liakhovetski | f35f1bb | 2010-03-23 11:52:11 -0300 | [diff] [blame] | 82 | dev_dbg(q->dev, "vm_close %p [count=%u,vma=%08lx-%08lx]\n", |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 83 | map, map->count, vma->vm_start, vma->vm_end); |
| 84 | |
Hans Verkuil | cca36e2 | 2014-01-03 08:10:49 -0300 | [diff] [blame] | 85 | map->count--; |
| 86 | if (0 == map->count) { |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 87 | struct videobuf_dma_contig_memory *mem; |
| 88 | |
Guennadi Liakhovetski | f35f1bb | 2010-03-23 11:52:11 -0300 | [diff] [blame] | 89 | dev_dbg(q->dev, "munmap %p q=%p\n", map, q); |
Hans Verkuil | cca36e2 | 2014-01-03 08:10:49 -0300 | [diff] [blame] | 90 | videobuf_queue_lock(q); |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 91 | |
| 92 | /* We need first to cancel streams, before unmapping */ |
| 93 | if (q->streaming) |
| 94 | videobuf_queue_cancel(q); |
| 95 | |
| 96 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 97 | if (NULL == q->bufs[i]) |
| 98 | continue; |
| 99 | |
| 100 | if (q->bufs[i]->map != map) |
| 101 | continue; |
| 102 | |
| 103 | mem = q->bufs[i]->priv; |
| 104 | if (mem) { |
| 105 | /* This callback is called only if kernel has |
| 106 | allocated memory and this memory is mmapped. |
| 107 | In this case, memory should be freed, |
| 108 | in order to do memory unmap. |
| 109 | */ |
| 110 | |
| 111 | MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); |
| 112 | |
| 113 | /* vfree is not atomic - can't be |
| 114 | called with IRQ's disabled |
| 115 | */ |
Guennadi Liakhovetski | f35f1bb | 2010-03-23 11:52:11 -0300 | [diff] [blame] | 116 | dev_dbg(q->dev, "buf[%d] freeing %p\n", |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 117 | i, mem->vaddr); |
| 118 | |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 119 | __videobuf_dc_free(q->dev, mem); |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 120 | mem->vaddr = NULL; |
| 121 | } |
| 122 | |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 123 | q->bufs[i]->map = NULL; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 124 | q->bufs[i]->baddr = 0; |
| 125 | } |
| 126 | |
| 127 | kfree(map); |
| 128 | |
Hans Verkuil | cca36e2 | 2014-01-03 08:10:49 -0300 | [diff] [blame] | 129 | videobuf_queue_unlock(q); |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 133 | static const struct vm_operations_struct videobuf_vm_ops = { |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 134 | .open = videobuf_vm_open, |
| 135 | .close = videobuf_vm_close, |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 136 | }; |
| 137 | |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 138 | /** |
| 139 | * videobuf_dma_contig_user_put() - reset pointer to user space buffer |
| 140 | * @mem: per-buffer private videobuf-dma-contig data |
| 141 | * |
| 142 | * This function resets the user space pointer |
| 143 | */ |
| 144 | static void videobuf_dma_contig_user_put(struct videobuf_dma_contig_memory *mem) |
| 145 | { |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 146 | mem->dma_handle = 0; |
| 147 | mem->size = 0; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * videobuf_dma_contig_user_get() - setup user space memory pointer |
| 152 | * @mem: per-buffer private videobuf-dma-contig data |
| 153 | * @vb: video buffer to map |
| 154 | * |
| 155 | * This function validates and sets up a pointer to user space memory. |
| 156 | * Only physically contiguous pfn-mapped memory is accepted. |
| 157 | * |
| 158 | * Returns 0 if successful. |
| 159 | */ |
| 160 | static int videobuf_dma_contig_user_get(struct videobuf_dma_contig_memory *mem, |
| 161 | struct videobuf_buffer *vb) |
| 162 | { |
| 163 | struct mm_struct *mm = current->mm; |
| 164 | struct vm_area_struct *vma; |
| 165 | unsigned long prev_pfn, this_pfn; |
| 166 | unsigned long pages_done, user_address; |
Muralidharan Karicheri | 31bedfa | 2009-12-10 16:47:48 -0300 | [diff] [blame] | 167 | unsigned int offset; |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 168 | int ret; |
| 169 | |
Muralidharan Karicheri | 31bedfa | 2009-12-10 16:47:48 -0300 | [diff] [blame] | 170 | offset = vb->baddr & ~PAGE_MASK; |
| 171 | mem->size = PAGE_ALIGN(vb->size + offset); |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 172 | ret = -EINVAL; |
| 173 | |
| 174 | down_read(&mm->mmap_sem); |
| 175 | |
| 176 | vma = find_vma(mm, vb->baddr); |
| 177 | if (!vma) |
| 178 | goto out_up; |
| 179 | |
| 180 | if ((vb->baddr + mem->size) > vma->vm_end) |
| 181 | goto out_up; |
| 182 | |
| 183 | pages_done = 0; |
| 184 | prev_pfn = 0; /* kill warning */ |
| 185 | user_address = vb->baddr; |
| 186 | |
| 187 | while (pages_done < (mem->size >> PAGE_SHIFT)) { |
| 188 | ret = follow_pfn(vma, user_address, &this_pfn); |
| 189 | if (ret) |
| 190 | break; |
| 191 | |
| 192 | if (pages_done == 0) |
Muralidharan Karicheri | 31bedfa | 2009-12-10 16:47:48 -0300 | [diff] [blame] | 193 | mem->dma_handle = (this_pfn << PAGE_SHIFT) + offset; |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 194 | else if (this_pfn != (prev_pfn + 1)) |
| 195 | ret = -EFAULT; |
| 196 | |
| 197 | if (ret) |
| 198 | break; |
| 199 | |
| 200 | prev_pfn = this_pfn; |
| 201 | user_address += PAGE_SIZE; |
| 202 | pages_done++; |
| 203 | } |
| 204 | |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 205 | out_up: |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 206 | up_read(¤t->mm->mmap_sem); |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
Mauro Carvalho Chehab | cb132cd | 2013-04-17 08:22:15 -0300 | [diff] [blame] | 211 | static struct videobuf_buffer *__videobuf_alloc(size_t size) |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 212 | { |
| 213 | struct videobuf_dma_contig_memory *mem; |
| 214 | struct videobuf_buffer *vb; |
| 215 | |
| 216 | vb = kzalloc(size + sizeof(*mem), GFP_KERNEL); |
| 217 | if (vb) { |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 218 | vb->priv = ((char *)vb) + size; |
| 219 | mem = vb->priv; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 220 | mem->magic = MAGIC_DC_MEM; |
| 221 | } |
| 222 | |
| 223 | return vb; |
| 224 | } |
| 225 | |
Hans Verkuil | 037c75e | 2010-03-28 08:18:37 -0300 | [diff] [blame] | 226 | static void *__videobuf_to_vaddr(struct videobuf_buffer *buf) |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 227 | { |
| 228 | struct videobuf_dma_contig_memory *mem = buf->priv; |
| 229 | |
| 230 | BUG_ON(!mem); |
| 231 | MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); |
| 232 | |
| 233 | return mem->vaddr; |
| 234 | } |
| 235 | |
| 236 | static int __videobuf_iolock(struct videobuf_queue *q, |
| 237 | struct videobuf_buffer *vb, |
| 238 | struct v4l2_framebuffer *fbuf) |
| 239 | { |
| 240 | struct videobuf_dma_contig_memory *mem = vb->priv; |
| 241 | |
| 242 | BUG_ON(!mem); |
| 243 | MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); |
| 244 | |
| 245 | switch (vb->memory) { |
| 246 | case V4L2_MEMORY_MMAP: |
| 247 | dev_dbg(q->dev, "%s memory method MMAP\n", __func__); |
| 248 | |
| 249 | /* All handling should be done by __videobuf_mmap_mapper() */ |
| 250 | if (!mem->vaddr) { |
| 251 | dev_err(q->dev, "memory is not alloced/mmapped.\n"); |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | break; |
| 255 | case V4L2_MEMORY_USERPTR: |
| 256 | dev_dbg(q->dev, "%s memory method USERPTR\n", __func__); |
| 257 | |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 258 | /* handle pointer from user space */ |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 259 | if (vb->baddr) |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 260 | return videobuf_dma_contig_user_get(mem, vb); |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 261 | |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 262 | /* allocate memory for the read() method */ |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 263 | if (__videobuf_dc_alloc(q->dev, mem, PAGE_ALIGN(vb->size), |
| 264 | GFP_KERNEL)) |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 265 | return -ENOMEM; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 266 | break; |
| 267 | case V4L2_MEMORY_OVERLAY: |
| 268 | default: |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 269 | dev_dbg(q->dev, "%s memory method OVERLAY/unknown\n", __func__); |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 270 | return -EINVAL; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 276 | static int __videobuf_mmap_mapper(struct videobuf_queue *q, |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 277 | struct videobuf_buffer *buf, |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 278 | struct vm_area_struct *vma) |
| 279 | { |
| 280 | struct videobuf_dma_contig_memory *mem; |
| 281 | struct videobuf_mapping *map; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 282 | int retval; |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 283 | unsigned long size; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 284 | |
| 285 | dev_dbg(q->dev, "%s\n", __func__); |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 286 | |
| 287 | /* create mapping + update buffer list */ |
| 288 | map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL); |
| 289 | if (!map) |
| 290 | return -ENOMEM; |
| 291 | |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 292 | buf->map = map; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 293 | map->q = q; |
| 294 | |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 295 | buf->baddr = vma->vm_start; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 296 | |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 297 | mem = buf->priv; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 298 | BUG_ON(!mem); |
| 299 | MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); |
| 300 | |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 301 | if (__videobuf_dc_alloc(q->dev, mem, PAGE_ALIGN(buf->bsize), |
| 302 | GFP_KERNEL | __GFP_COMP)) |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 303 | goto error; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 304 | |
| 305 | /* Try to remap memory */ |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 306 | size = vma->vm_end - vma->vm_start; |
Mauro Carvalho Chehab | cb132cd | 2013-04-17 08:22:15 -0300 | [diff] [blame] | 307 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
Fancy Fang | 8a6a547 | 2014-10-09 23:21:22 -0300 | [diff] [blame] | 308 | |
| 309 | /* the "vm_pgoff" is just used in v4l2 to find the |
| 310 | * corresponding buffer data structure which is allocated |
| 311 | * earlier and it does not mean the offset from the physical |
| 312 | * buffer start address as usual. So set it to 0 to pass |
| 313 | * the sanity check in vm_iomap_memory(). |
| 314 | */ |
| 315 | vma->vm_pgoff = 0; |
| 316 | |
Ma Haijun | 29f1cdb | 2014-03-27 08:07:06 -0300 | [diff] [blame] | 317 | retval = vm_iomap_memory(vma, mem->dma_handle, size); |
Mauro Carvalho Chehab | cb132cd | 2013-04-17 08:22:15 -0300 | [diff] [blame] | 318 | if (retval) { |
| 319 | dev_err(q->dev, "mmap: remap failed with error %d. ", |
| 320 | retval); |
| 321 | dma_free_coherent(q->dev, mem->size, |
| 322 | mem->vaddr, mem->dma_handle); |
| 323 | goto error; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 324 | } |
| 325 | |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 326 | vma->vm_ops = &videobuf_vm_ops; |
| 327 | vma->vm_flags |= VM_DONTEXPAND; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 328 | vma->vm_private_data = map; |
| 329 | |
| 330 | dev_dbg(q->dev, "mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n", |
| 331 | map, q, vma->vm_start, vma->vm_end, |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 332 | (long int)buf->bsize, vma->vm_pgoff, buf->i); |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 333 | |
| 334 | videobuf_vm_open(vma); |
| 335 | |
| 336 | return 0; |
| 337 | |
| 338 | error: |
| 339 | kfree(map); |
| 340 | return -ENOMEM; |
| 341 | } |
| 342 | |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 343 | static struct videobuf_qtype_ops qops = { |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 344 | .magic = MAGIC_QTYPE_OPS, |
Mauro Carvalho Chehab | cb132cd | 2013-04-17 08:22:15 -0300 | [diff] [blame] | 345 | .alloc_vb = __videobuf_alloc, |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 346 | .iolock = __videobuf_iolock, |
| 347 | .mmap_mapper = __videobuf_mmap_mapper, |
| 348 | .vaddr = __videobuf_to_vaddr, |
| 349 | }; |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 350 | |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 351 | void videobuf_queue_dma_contig_init(struct videobuf_queue *q, |
Jonathan Corbet | 38a54f3 | 2009-11-17 19:43:41 -0300 | [diff] [blame] | 352 | const struct videobuf_queue_ops *ops, |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 353 | struct device *dev, |
| 354 | spinlock_t *irqlock, |
| 355 | enum v4l2_buf_type type, |
| 356 | enum v4l2_field field, |
| 357 | unsigned int msize, |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 358 | void *priv, |
| 359 | struct mutex *ext_lock) |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 360 | { |
| 361 | videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 362 | priv, &qops, ext_lock); |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 363 | } |
| 364 | EXPORT_SYMBOL_GPL(videobuf_queue_dma_contig_init); |
| 365 | |
| 366 | dma_addr_t videobuf_to_dma_contig(struct videobuf_buffer *buf) |
| 367 | { |
| 368 | struct videobuf_dma_contig_memory *mem = buf->priv; |
| 369 | |
| 370 | BUG_ON(!mem); |
| 371 | MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); |
| 372 | |
| 373 | return mem->dma_handle; |
| 374 | } |
| 375 | EXPORT_SYMBOL_GPL(videobuf_to_dma_contig); |
| 376 | |
| 377 | void videobuf_dma_contig_free(struct videobuf_queue *q, |
| 378 | struct videobuf_buffer *buf) |
| 379 | { |
| 380 | struct videobuf_dma_contig_memory *mem = buf->priv; |
| 381 | |
| 382 | /* mmapped memory can't be freed here, otherwise mmapped region |
| 383 | would be released, while still needed. In this case, the memory |
| 384 | release should happen inside videobuf_vm_close(). |
| 385 | So, it should free memory only if the memory were allocated for |
| 386 | read() operation. |
| 387 | */ |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 388 | if (buf->memory != V4L2_MEMORY_USERPTR) |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 389 | return; |
| 390 | |
| 391 | if (!mem) |
| 392 | return; |
| 393 | |
| 394 | MAGIC_CHECK(mem->magic, MAGIC_DC_MEM); |
| 395 | |
Magnus Damm | 720b17e | 2009-06-16 15:32:36 -0700 | [diff] [blame] | 396 | /* handle user space pointer case */ |
| 397 | if (buf->baddr) { |
| 398 | videobuf_dma_contig_user_put(mem); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | /* read() method */ |
Pawel Osciak | b2b476f | 2010-07-20 13:49:16 -0300 | [diff] [blame] | 403 | if (mem->vaddr) { |
Federico Vaga | a8f3c20 | 2012-04-12 12:39:37 -0300 | [diff] [blame] | 404 | __videobuf_dc_free(q->dev, mem); |
Pawel Osciak | b2b476f | 2010-07-20 13:49:16 -0300 | [diff] [blame] | 405 | mem->vaddr = NULL; |
| 406 | } |
Magnus Damm | 2cc45cf | 2008-07-16 21:33:39 -0300 | [diff] [blame] | 407 | } |
| 408 | EXPORT_SYMBOL_GPL(videobuf_dma_contig_free); |
| 409 | |
| 410 | MODULE_DESCRIPTION("helper module to manage video4linux dma contig buffers"); |
| 411 | MODULE_AUTHOR("Magnus Damm"); |
| 412 | MODULE_LICENSE("GPL"); |