videobuf-vmalloc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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) if (unlikely((is) != (should))) \
  29. { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
  30. static int debug;
  31. module_param(debug, int, 0644);
  32. MODULE_DESCRIPTION("helper module to manage video4linux vmalloc buffers");
  33. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  34. MODULE_LICENSE("GPL");
  35. #define dprintk(level, fmt, arg...) if (debug >= level) \
  36. printk(KERN_DEBUG "vbuf-vmalloc: " fmt , ## arg)
  37. /***************************************************************************/
  38. static void
  39. videobuf_vm_open(struct vm_area_struct *vma)
  40. {
  41. struct videobuf_mapping *map = vma->vm_private_data;
  42. dprintk(2,"vm_open %p [count=%u,vma=%08lx-%08lx]\n",map,
  43. map->count,vma->vm_start,vma->vm_end);
  44. map->count++;
  45. }
  46. static void videobuf_vm_close(struct vm_area_struct *vma)
  47. {
  48. struct videobuf_mapping *map = vma->vm_private_data;
  49. struct videobuf_queue *q = map->q;
  50. int i;
  51. dprintk(2,"vm_close %p [count=%u,vma=%08lx-%08lx]\n", map,
  52. map->count, vma->vm_start, vma->vm_end);
  53. map->count--;
  54. if (0 == map->count) {
  55. struct videobuf_vmalloc_memory *mem;
  56. dprintk(1, "munmap %p q=%p\n", map, q);
  57. mutex_lock(&q->vb_lock);
  58. /* We need first to cancel streams, before unmapping */
  59. if (q->streaming)
  60. videobuf_queue_cancel(q);
  61. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  62. if (NULL == q->bufs[i])
  63. continue;
  64. if (q->bufs[i]->map != map)
  65. continue;
  66. mem = q->bufs[i]->priv;
  67. if (mem) {
  68. /* This callback is called only if kernel has
  69. allocated memory and this memory is mmapped.
  70. In this case, memory should be freed,
  71. in order to do memory unmap.
  72. */
  73. MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
  74. /* vfree is not atomic - can't be
  75. called with IRQ's disabled
  76. */
  77. dprintk(1, "%s: buf[%d] freeing (%p)\n",
  78. __func__, i, mem->vmalloc);
  79. vfree(mem->vmalloc);
  80. mem->vmalloc = NULL;
  81. }
  82. q->bufs[i]->map = NULL;
  83. q->bufs[i]->baddr = 0;
  84. }
  85. kfree(map);
  86. mutex_unlock(&q->vb_lock);
  87. }
  88. return;
  89. }
  90. static const struct vm_operations_struct videobuf_vm_ops =
  91. {
  92. .open = videobuf_vm_open,
  93. .close = videobuf_vm_close,
  94. };
  95. /* ---------------------------------------------------------------------
  96. * vmalloc handlers for the generic methods
  97. */
  98. /* Allocated area consists on 3 parts:
  99. struct video_buffer
  100. struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
  101. struct videobuf_dma_sg_memory
  102. */
  103. static void *__videobuf_alloc(size_t size)
  104. {
  105. struct videobuf_vmalloc_memory *mem;
  106. struct videobuf_buffer *vb;
  107. vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
  108. mem = vb->priv = ((char *)vb)+size;
  109. mem->magic=MAGIC_VMAL_MEM;
  110. dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
  111. __func__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
  112. mem,(long)sizeof(*mem));
  113. return vb;
  114. }
  115. static int __videobuf_iolock (struct videobuf_queue* q,
  116. struct videobuf_buffer *vb,
  117. struct v4l2_framebuffer *fbuf)
  118. {
  119. struct videobuf_vmalloc_memory *mem = vb->priv;
  120. int pages;
  121. BUG_ON(!mem);
  122. MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
  123. switch (vb->memory) {
  124. case V4L2_MEMORY_MMAP:
  125. dprintk(1, "%s memory method MMAP\n", __func__);
  126. /* All handling should be done by __videobuf_mmap_mapper() */
  127. if (!mem->vmalloc) {
  128. printk(KERN_ERR "memory is not alloced/mmapped.\n");
  129. return -EINVAL;
  130. }
  131. break;
  132. case V4L2_MEMORY_USERPTR:
  133. pages = PAGE_ALIGN(vb->size);
  134. dprintk(1, "%s memory method USERPTR\n", __func__);
  135. #if 1
  136. if (vb->baddr) {
  137. printk(KERN_ERR "USERPTR is currently not supported\n");
  138. return -EINVAL;
  139. }
  140. #endif
  141. /* The only USERPTR currently supported is the one needed for
  142. read() method.
  143. */
  144. mem->vmalloc = vmalloc_user(pages);
  145. if (!mem->vmalloc) {
  146. printk(KERN_ERR "vmalloc (%d pages) failed\n", pages);
  147. return -ENOMEM;
  148. }
  149. dprintk(1, "vmalloc is at addr %p (%d pages)\n",
  150. mem->vmalloc, pages);
  151. #if 0
  152. int rc;
  153. /* Kernel userptr is used also by read() method. In this case,
  154. there's no need to remap, since data will be copied to user
  155. */
  156. if (!vb->baddr)
  157. return 0;
  158. /* FIXME: to properly support USERPTR, remap should occur.
  159. The code below won't work, since mem->vma = NULL
  160. */
  161. /* Try to remap memory */
  162. rc = remap_vmalloc_range(mem->vma, (void *)vb->baddr, 0);
  163. if (rc < 0) {
  164. printk(KERN_ERR "mmap: remap failed with error %d. ", rc);
  165. return -ENOMEM;
  166. }
  167. #endif
  168. break;
  169. case V4L2_MEMORY_OVERLAY:
  170. default:
  171. dprintk(1, "%s memory method OVERLAY/unknown\n", __func__);
  172. /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
  173. printk(KERN_ERR "Memory method currently unsupported.\n");
  174. return -EINVAL;
  175. }
  176. return 0;
  177. }
  178. static int __videobuf_sync(struct videobuf_queue *q,
  179. struct videobuf_buffer *buf)
  180. {
  181. return 0;
  182. }
  183. static int __videobuf_mmap_free(struct videobuf_queue *q)
  184. {
  185. unsigned int i;
  186. dprintk(1, "%s\n", __func__);
  187. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  188. if (q->bufs[i]) {
  189. if (q->bufs[i]->map)
  190. return -EBUSY;
  191. }
  192. }
  193. return 0;
  194. }
  195. static int __videobuf_mmap_mapper(struct videobuf_queue *q,
  196. struct vm_area_struct *vma)
  197. {
  198. struct videobuf_vmalloc_memory *mem;
  199. struct videobuf_mapping *map;
  200. unsigned int first;
  201. int retval, pages;
  202. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  203. dprintk(1, "%s\n", __func__);
  204. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
  205. return -EINVAL;
  206. /* look for first buffer to map */
  207. for (first = 0; first < VIDEO_MAX_FRAME; first++) {
  208. if (NULL == q->bufs[first])
  209. continue;
  210. if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
  211. continue;
  212. if (q->bufs[first]->boff == offset)
  213. break;
  214. }
  215. if (VIDEO_MAX_FRAME == first) {
  216. dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
  217. (vma->vm_pgoff << PAGE_SHIFT));
  218. return -EINVAL;
  219. }
  220. /* create mapping + update buffer list */
  221. map = kzalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
  222. if (NULL == map)
  223. return -ENOMEM;
  224. q->bufs[first]->map = map;
  225. map->start = vma->vm_start;
  226. map->end = vma->vm_end;
  227. map->q = q;
  228. q->bufs[first]->baddr = vma->vm_start;
  229. mem = q->bufs[first]->priv;
  230. BUG_ON(!mem);
  231. MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
  232. pages = PAGE_ALIGN(vma->vm_end - vma->vm_start);
  233. mem->vmalloc = vmalloc_user(pages);
  234. if (!mem->vmalloc) {
  235. printk(KERN_ERR "vmalloc (%d pages) failed\n", pages);
  236. goto error;
  237. }
  238. dprintk(1, "vmalloc is at addr %p (%d pages)\n",
  239. mem->vmalloc, pages);
  240. /* Try to remap memory */
  241. retval = remap_vmalloc_range(vma, mem->vmalloc, 0);
  242. if (retval < 0) {
  243. printk(KERN_ERR "mmap: remap failed with error %d. ", retval);
  244. vfree(mem->vmalloc);
  245. goto error;
  246. }
  247. vma->vm_ops = &videobuf_vm_ops;
  248. vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
  249. vma->vm_private_data = map;
  250. dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
  251. map, q, vma->vm_start, vma->vm_end,
  252. (long int) q->bufs[first]->bsize,
  253. vma->vm_pgoff, first);
  254. videobuf_vm_open(vma);
  255. return 0;
  256. error:
  257. mem = NULL;
  258. kfree(map);
  259. return -ENOMEM;
  260. }
  261. static int __videobuf_copy_to_user ( struct videobuf_queue *q,
  262. char __user *data, size_t count,
  263. int nonblocking )
  264. {
  265. struct videobuf_vmalloc_memory *mem=q->read_buf->priv;
  266. BUG_ON (!mem);
  267. MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
  268. BUG_ON (!mem->vmalloc);
  269. /* copy to userspace */
  270. if (count > q->read_buf->size - q->read_off)
  271. count = q->read_buf->size - q->read_off;
  272. if (copy_to_user(data, mem->vmalloc+q->read_off, count))
  273. return -EFAULT;
  274. return count;
  275. }
  276. static int __videobuf_copy_stream ( struct videobuf_queue *q,
  277. char __user *data, size_t count, size_t pos,
  278. int vbihack, int nonblocking )
  279. {
  280. unsigned int *fc;
  281. struct videobuf_vmalloc_memory *mem=q->read_buf->priv;
  282. BUG_ON (!mem);
  283. MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
  284. if (vbihack) {
  285. /* dirty, undocumented hack -- pass the frame counter
  286. * within the last four bytes of each vbi data block.
  287. * We need that one to maintain backward compatibility
  288. * to all vbi decoding software out there ... */
  289. fc = (unsigned int*)mem->vmalloc;
  290. fc += (q->read_buf->size>>2) -1;
  291. *fc = q->read_buf->field_count >> 1;
  292. dprintk(1,"vbihack: %d\n",*fc);
  293. }
  294. /* copy stuff using the common method */
  295. count = __videobuf_copy_to_user (q,data,count,nonblocking);
  296. if ( (count==-EFAULT) && (0 == pos) )
  297. return -EFAULT;
  298. return count;
  299. }
  300. static struct videobuf_qtype_ops qops = {
  301. .magic = MAGIC_QTYPE_OPS,
  302. .alloc = __videobuf_alloc,
  303. .iolock = __videobuf_iolock,
  304. .sync = __videobuf_sync,
  305. .mmap_free = __videobuf_mmap_free,
  306. .mmap_mapper = __videobuf_mmap_mapper,
  307. .video_copy_to_user = __videobuf_copy_to_user,
  308. .copy_stream = __videobuf_copy_stream,
  309. .vmalloc = videobuf_to_vmalloc,
  310. };
  311. void videobuf_queue_vmalloc_init(struct videobuf_queue* q,
  312. const struct videobuf_queue_ops *ops,
  313. struct device *dev,
  314. spinlock_t *irqlock,
  315. enum v4l2_buf_type type,
  316. enum v4l2_field field,
  317. unsigned int msize,
  318. void *priv)
  319. {
  320. videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
  321. priv, &qops);
  322. }
  323. EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
  324. void *videobuf_to_vmalloc (struct videobuf_buffer *buf)
  325. {
  326. struct videobuf_vmalloc_memory *mem=buf->priv;
  327. BUG_ON (!mem);
  328. MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
  329. return mem->vmalloc;
  330. }
  331. EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
  332. void videobuf_vmalloc_free (struct videobuf_buffer *buf)
  333. {
  334. struct videobuf_vmalloc_memory *mem = buf->priv;
  335. /* mmapped memory can't be freed here, otherwise mmapped region
  336. would be released, while still needed. In this case, the memory
  337. release should happen inside videobuf_vm_close().
  338. So, it should free memory only if the memory were allocated for
  339. read() operation.
  340. */
  341. if ((buf->memory != V4L2_MEMORY_USERPTR) || buf->baddr)
  342. return;
  343. if (!mem)
  344. return;
  345. MAGIC_CHECK(mem->magic, MAGIC_VMAL_MEM);
  346. vfree(mem->vmalloc);
  347. mem->vmalloc = NULL;
  348. return;
  349. }
  350. EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);
  351. /*
  352. * Local variables:
  353. * c-basic-offset: 8
  354. * End:
  355. */