videobuf-vmalloc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * helper functions for vmalloc video4linux capture buffers
  3. *
  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.
  8. *
  9. * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/slab.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pci.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/pagemap.h>
  23. #include <asm/page.h>
  24. #include <asm/pgtable.h>
  25. #include <media/videobuf-vmalloc.h>
  26. #define MAGIC_DMABUF 0x17760309
  27. #define MAGIC_VMAL_MEM 0x18221223
  28. #define MAGIC_CHECK(is, should) \
  29. if (unlikely((is) != (should))) { \
  30. printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \
  31. is, should); \
  32. BUG(); \
  33. }
  34. static int debug;
  35. module_param(debug, int, 0644);
  36. MODULE_DESCRIPTION("helper module to manage video4linux vmalloc buffers");
  37. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  38. MODULE_LICENSE("GPL");
  39. #define dprintk(level, fmt, arg...) \
  40. if (debug >= level) \
  41. printk(KERN_DEBUG "vbuf-vmalloc: " fmt , ## arg)
  42. /***************************************************************************/
  43. static void videobuf_vm_open(struct vm_area_struct *vma)
  44. {
  45. struct videobuf_mapping *map = vma->vm_private_data;
  46. dprintk(2, "vm_open %p [count=%u,vma=%08lx-%08lx]\n", map,
  47. map->count, vma->vm_start, vma->vm_end);
  48. map->count++;
  49. }
  50. static void videobuf_vm_close(struct vm_area_struct *vma)
  51. {
  52. struct videobuf_mapping *map = vma->vm_private_data;
  53. struct videobuf_queue *q = map->q;
  54. int i;
  55. dprintk(2, "vm_close %p [count=%u,vma=%08lx-%08lx]\n", map,
  56. map->count, vma->vm_start, vma->vm_end);
  57. map->count--;
  58. if (0 == map->count) {
  59. struct videobuf_vmalloc_memory *mem;
  60. dprintk(1, "munmap %p q=%p\n", map, q);
  61. videobuf_queue_lock(q);
  62. /* We need first to cancel streams, before unmapping */
  63. if (q->streaming)
  64. videobuf_queue_cancel(q);
  65. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  66. if (NULL == q->bufs[i])
  67. continue;
  68. if (q->bufs[i]->map != map)
  69. continue;
  70. mem = q->bufs[i]->priv;
  71. if (mem) {
  72. /* This callback is called only if kernel has
  73. allocated memory and this memory is mmapped.
  74. In this case, memory should be freed,
  75. in order to do memory unmap.
  76. */
  77. MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
  78. /* vfree is not atomic - can't be
  79. called with IRQ's disabled
  80. */
  81. dprintk(1, "%s: buf[%d] freeing (%p)\n",
  82. __func__, i, mem->vaddr);
  83. vfree(mem->vaddr);
  84. mem->vaddr = NULL;
  85. }
  86. q->bufs[i]->map = NULL;
  87. q->bufs[i]->baddr = 0;
  88. }
  89. kfree(map);
  90. videobuf_queue_unlock(q);
  91. }
  92. return;
  93. }
  94. static const struct vm_operations_struct videobuf_vm_ops = {
  95. .open = videobuf_vm_open,
  96. .close = videobuf_vm_close,
  97. };
  98. /* ---------------------------------------------------------------------
  99. * vmalloc handlers for the generic methods
  100. */
  101. /* Allocated area consists on 3 parts:
  102. struct video_buffer
  103. struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
  104. struct videobuf_dma_sg_memory
  105. */
  106. static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
  107. {
  108. struct videobuf_vmalloc_memory *mem;
  109. struct videobuf_buffer *vb;
  110. vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
  111. if (!vb)
  112. return vb;
  113. mem = vb->priv = ((char *)vb) + size;
  114. mem->magic = MAGIC_VMAL_MEM;
  115. dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
  116. __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
  117. mem, (long)sizeof(*mem));
  118. return vb;
  119. }
  120. static int __videobuf_iolock(struct videobuf_queue *q,
  121. struct videobuf_buffer *vb,
  122. struct v4l2_framebuffer *fbuf)
  123. {
  124. struct videobuf_vmalloc_memory *mem = vb->priv;
  125. int pages;
  126. BUG_ON(!mem);
  127. MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
  128. switch (vb->memory) {
  129. case V4L2_MEMORY_MMAP:
  130. dprintk(1, "%s memory method MMAP\n", __func__);
  131. /* All handling should be done by __videobuf_mmap_mapper() */
  132. if (!mem->vaddr) {
  133. printk(KERN_ERR "memory is not alloced/mmapped.\n");
  134. return -EINVAL;
  135. }
  136. break;
  137. case V4L2_MEMORY_USERPTR:
  138. pages = PAGE_ALIGN(vb->size);
  139. dprintk(1, "%s memory method USERPTR\n", __func__);
  140. if (vb->baddr) {
  141. printk(KERN_ERR "USERPTR is currently not supported\n");
  142. return -EINVAL;
  143. }
  144. /* The only USERPTR currently supported is the one needed for
  145. * read() method.
  146. */
  147. mem->vaddr = vmalloc_user(pages);
  148. if (!mem->vaddr) {
  149. printk(KERN_ERR "vmalloc (%d pages) failed\n", pages);
  150. return -ENOMEM;
  151. }
  152. dprintk(1, "vmalloc is at addr %p (%d pages)\n",
  153. mem->vaddr, pages);
  154. #if 0
  155. int rc;
  156. /* Kernel userptr is used also by read() method. In this case,
  157. there's no need to remap, since data will be copied to user
  158. */
  159. if (!vb->baddr)
  160. return 0;
  161. /* FIXME: to properly support USERPTR, remap should occur.
  162. The code below won't work, since mem->vma = NULL
  163. */
  164. /* Try to remap memory */
  165. rc = remap_vmalloc_range(mem->vma, (void *)vb->baddr, 0);
  166. if (rc < 0) {
  167. printk(KERN_ERR "mmap: remap failed with error %d", rc);
  168. return -ENOMEM;
  169. }
  170. #endif
  171. break;
  172. case V4L2_MEMORY_OVERLAY:
  173. default:
  174. dprintk(1, "%s memory method OVERLAY/unknown\n", __func__);
  175. /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
  176. printk(KERN_ERR "Memory method currently unsupported.\n");
  177. return -EINVAL;
  178. }
  179. return 0;
  180. }
  181. static int __videobuf_mmap_mapper(struct videobuf_queue *q,
  182. struct videobuf_buffer *buf,
  183. struct vm_area_struct *vma)
  184. {
  185. struct videobuf_vmalloc_memory *mem;
  186. struct videobuf_mapping *map;
  187. int retval, pages;
  188. dprintk(1, "%s\n", __func__);
  189. /* create mapping + update buffer list */
  190. map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
  191. if (NULL == map)
  192. return -ENOMEM;
  193. buf->map = map;
  194. map->q = q;
  195. buf->baddr = vma->vm_start;
  196. mem = buf->priv;
  197. BUG_ON(!mem);
  198. MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
  199. pages = PAGE_ALIGN(vma->vm_end - vma->vm_start);
  200. mem->vaddr = vmalloc_user(pages);
  201. if (!mem->vaddr) {
  202. printk(KERN_ERR "vmalloc (%d pages) failed\n", pages);
  203. goto error;
  204. }
  205. dprintk(1, "vmalloc is at addr %p (%d pages)\n", mem->vaddr, pages);
  206. /* Try to remap memory */
  207. retval = remap_vmalloc_range(vma, mem->vaddr, 0);
  208. if (retval < 0) {
  209. printk(KERN_ERR "mmap: remap failed with error %d. ", retval);
  210. vfree(mem->vaddr);
  211. goto error;
  212. }
  213. vma->vm_ops = &videobuf_vm_ops;
  214. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  215. vma->vm_private_data = map;
  216. dprintk(1, "mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
  217. map, q, vma->vm_start, vma->vm_end,
  218. (long int)buf->bsize,
  219. vma->vm_pgoff, buf->i);
  220. videobuf_vm_open(vma);
  221. return 0;
  222. error:
  223. mem = NULL;
  224. kfree(map);
  225. return -ENOMEM;
  226. }
  227. static struct videobuf_qtype_ops qops = {
  228. .magic = MAGIC_QTYPE_OPS,
  229. .alloc_vb = __videobuf_alloc_vb,
  230. .iolock = __videobuf_iolock,
  231. .mmap_mapper = __videobuf_mmap_mapper,
  232. .vaddr = videobuf_to_vmalloc,
  233. };
  234. void videobuf_queue_vmalloc_init(struct videobuf_queue *q,
  235. const struct videobuf_queue_ops *ops,
  236. struct device *dev,
  237. spinlock_t *irqlock,
  238. enum v4l2_buf_type type,
  239. enum v4l2_field field,
  240. unsigned int msize,
  241. void *priv,
  242. struct mutex *ext_lock)
  243. {
  244. videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
  245. priv, &qops, ext_lock);
  246. }
  247. EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
  248. void *videobuf_to_vmalloc(struct videobuf_buffer *buf)
  249. {
  250. struct videobuf_vmalloc_memory *mem = buf->priv;
  251. BUG_ON(!mem);
  252. MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
  253. return mem->vaddr;
  254. }
  255. EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
  256. void videobuf_vmalloc_free(struct videobuf_buffer *buf)
  257. {
  258. struct videobuf_vmalloc_memory *mem = buf->priv;
  259. /* mmapped memory can't be freed here, otherwise mmapped region
  260. would be released, while still needed. In this case, the memory
  261. release should happen inside videobuf_vm_close().
  262. So, it should free memory only if the memory were allocated for
  263. read() operation.
  264. */
  265. if ((buf->memory != V4L2_MEMORY_USERPTR) || buf->baddr)
  266. return;
  267. if (!mem)
  268. return;
  269. MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
  270. vfree(mem->vaddr);
  271. mem->vaddr = NULL;
  272. return;
  273. }
  274. EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);