videobuf-vmalloc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * helper functions for vmalloc video4linux capture buffers
  3. *
  4. * The functions expect the hardware being able to scatter gatter
  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
  47. videobuf_vm_close(struct vm_area_struct *vma)
  48. {
  49. struct videobuf_mapping *map = vma->vm_private_data;
  50. struct videobuf_queue *q = map->q;
  51. int i;
  52. dprintk(2,"vm_close %p [count=%u,vma=%08lx-%08lx]\n",map,
  53. map->count,vma->vm_start,vma->vm_end);
  54. map->count--;
  55. if (0 == map->count) {
  56. dprintk(1,"munmap %p q=%p\n",map,q);
  57. mutex_lock(&q->vb_lock);
  58. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  59. if (NULL == q->bufs[i])
  60. continue;
  61. if (q->bufs[i]->map != map)
  62. continue;
  63. q->bufs[i]->map = NULL;
  64. q->bufs[i]->baddr = 0;
  65. }
  66. mutex_unlock(&q->vb_lock);
  67. kfree(map);
  68. }
  69. return;
  70. }
  71. static struct vm_operations_struct videobuf_vm_ops =
  72. {
  73. .open = videobuf_vm_open,
  74. .close = videobuf_vm_close,
  75. };
  76. /* ---------------------------------------------------------------------
  77. * vmalloc handlers for the generic methods
  78. */
  79. /* Allocated area consists on 3 parts:
  80. struct video_buffer
  81. struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
  82. struct videobuf_dma_sg_memory
  83. */
  84. static void *__videobuf_alloc(size_t size)
  85. {
  86. struct videobuf_vmalloc_memory *mem;
  87. struct videobuf_buffer *vb;
  88. vb = kzalloc(size+sizeof(*mem),GFP_KERNEL);
  89. mem = vb->priv = ((char *)vb)+size;
  90. mem->magic=MAGIC_VMAL_MEM;
  91. dprintk(1,"%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
  92. __func__,vb,(long)sizeof(*vb),(long)size-sizeof(*vb),
  93. mem,(long)sizeof(*mem));
  94. return vb;
  95. }
  96. static int __videobuf_iolock (struct videobuf_queue* q,
  97. struct videobuf_buffer *vb,
  98. struct v4l2_framebuffer *fbuf)
  99. {
  100. int pages;
  101. struct videobuf_vmalloc_memory *mem=vb->priv;
  102. BUG_ON(!mem);
  103. MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
  104. pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
  105. /* Currently, doesn't support V4L2_MEMORY_OVERLAY */
  106. if ((vb->memory != V4L2_MEMORY_MMAP) &&
  107. (vb->memory != V4L2_MEMORY_USERPTR) ) {
  108. printk(KERN_ERR "Method currently unsupported.\n");
  109. return -EINVAL;
  110. }
  111. /* FIXME: should be tested with kernel mmap mem */
  112. mem->vmalloc=vmalloc_user (PAGE_ALIGN(vb->size));
  113. if (NULL == mem->vmalloc) {
  114. printk(KERN_ERR "vmalloc (%d pages) failed\n",pages);
  115. return -ENOMEM;
  116. }
  117. dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
  118. (unsigned long)mem->vmalloc,
  119. pages << PAGE_SHIFT);
  120. /* It seems that some kernel versions need to do remap *after*
  121. the mmap() call
  122. */
  123. if (mem->vma) {
  124. int retval=remap_vmalloc_range(mem->vma, mem->vmalloc,0);
  125. kfree(mem->vma);
  126. mem->vma=NULL;
  127. if (retval<0) {
  128. dprintk(1,"mmap app bug: remap_vmalloc_range area %p error %d\n",
  129. mem->vmalloc,retval);
  130. return retval;
  131. }
  132. }
  133. return 0;
  134. }
  135. static int __videobuf_sync(struct videobuf_queue *q,
  136. struct videobuf_buffer *buf)
  137. {
  138. return 0;
  139. }
  140. static int __videobuf_mmap_free(struct videobuf_queue *q)
  141. {
  142. unsigned int i;
  143. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  144. if (q->bufs[i]) {
  145. if (q->bufs[i]->map)
  146. return -EBUSY;
  147. }
  148. }
  149. return 0;
  150. }
  151. static int __videobuf_mmap_mapper(struct videobuf_queue *q,
  152. struct vm_area_struct *vma)
  153. {
  154. struct videobuf_vmalloc_memory *mem;
  155. struct videobuf_mapping *map;
  156. unsigned int first;
  157. int retval;
  158. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  159. if (! (vma->vm_flags & VM_WRITE) || ! (vma->vm_flags & VM_SHARED))
  160. return -EINVAL;
  161. /* look for first buffer to map */
  162. for (first = 0; first < VIDEO_MAX_FRAME; first++) {
  163. if (NULL == q->bufs[first])
  164. continue;
  165. if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
  166. continue;
  167. if (q->bufs[first]->boff == offset)
  168. break;
  169. }
  170. if (VIDEO_MAX_FRAME == first) {
  171. dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
  172. (vma->vm_pgoff << PAGE_SHIFT));
  173. return -EINVAL;
  174. }
  175. /* create mapping + update buffer list */
  176. map = q->bufs[first]->map = kzalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
  177. if (NULL == map)
  178. return -ENOMEM;
  179. map->start = vma->vm_start;
  180. map->end = vma->vm_end;
  181. map->q = q;
  182. q->bufs[first]->baddr = vma->vm_start;
  183. vma->vm_ops = &videobuf_vm_ops;
  184. vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
  185. vma->vm_private_data = map;
  186. mem=q->bufs[first]->priv;
  187. BUG_ON (!mem);
  188. MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
  189. /* Try to remap memory */
  190. retval=remap_vmalloc_range(vma, mem->vmalloc,0);
  191. if (retval<0) {
  192. dprintk(1,"mmap: postponing remap_vmalloc_range\n");
  193. mem->vma=kmalloc(sizeof(*vma),GFP_KERNEL);
  194. if (!mem->vma) {
  195. kfree(map);
  196. q->bufs[first]->map=NULL;
  197. return -ENOMEM;
  198. }
  199. memcpy(mem->vma,vma,sizeof(*vma));
  200. }
  201. dprintk(1,"mmap %p: q=%p %08lx-%08lx (%lx) pgoff %08lx buf %d\n",
  202. map,q,vma->vm_start,vma->vm_end,
  203. (long int) q->bufs[first]->bsize,
  204. vma->vm_pgoff,first);
  205. videobuf_vm_open(vma);
  206. return (0);
  207. }
  208. static int __videobuf_copy_to_user ( struct videobuf_queue *q,
  209. char __user *data, size_t count,
  210. int nonblocking )
  211. {
  212. struct videobuf_vmalloc_memory *mem=q->read_buf->priv;
  213. BUG_ON (!mem);
  214. MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
  215. BUG_ON (!mem->vmalloc);
  216. /* copy to userspace */
  217. if (count > q->read_buf->size - q->read_off)
  218. count = q->read_buf->size - q->read_off;
  219. if (copy_to_user(data, mem->vmalloc+q->read_off, count))
  220. return -EFAULT;
  221. return count;
  222. }
  223. static int __videobuf_copy_stream ( struct videobuf_queue *q,
  224. char __user *data, size_t count, size_t pos,
  225. int vbihack, int nonblocking )
  226. {
  227. unsigned int *fc;
  228. struct videobuf_vmalloc_memory *mem=q->read_buf->priv;
  229. BUG_ON (!mem);
  230. MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
  231. if (vbihack) {
  232. /* dirty, undocumented hack -- pass the frame counter
  233. * within the last four bytes of each vbi data block.
  234. * We need that one to maintain backward compatibility
  235. * to all vbi decoding software out there ... */
  236. fc = (unsigned int*)mem->vmalloc;
  237. fc += (q->read_buf->size>>2) -1;
  238. *fc = q->read_buf->field_count >> 1;
  239. dprintk(1,"vbihack: %d\n",*fc);
  240. }
  241. /* copy stuff using the common method */
  242. count = __videobuf_copy_to_user (q,data,count,nonblocking);
  243. if ( (count==-EFAULT) && (0 == pos) )
  244. return -EFAULT;
  245. return count;
  246. }
  247. static struct videobuf_qtype_ops qops = {
  248. .magic = MAGIC_QTYPE_OPS,
  249. .alloc = __videobuf_alloc,
  250. .iolock = __videobuf_iolock,
  251. .sync = __videobuf_sync,
  252. .mmap_free = __videobuf_mmap_free,
  253. .mmap_mapper = __videobuf_mmap_mapper,
  254. .video_copy_to_user = __videobuf_copy_to_user,
  255. .copy_stream = __videobuf_copy_stream,
  256. };
  257. void videobuf_queue_vmalloc_init(struct videobuf_queue* q,
  258. struct videobuf_queue_ops *ops,
  259. void *dev,
  260. spinlock_t *irqlock,
  261. enum v4l2_buf_type type,
  262. enum v4l2_field field,
  263. unsigned int msize,
  264. void *priv)
  265. {
  266. videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
  267. priv, &qops);
  268. }
  269. EXPORT_SYMBOL_GPL(videobuf_queue_vmalloc_init);
  270. void *videobuf_to_vmalloc (struct videobuf_buffer *buf)
  271. {
  272. struct videobuf_vmalloc_memory *mem=buf->priv;
  273. BUG_ON (!mem);
  274. MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
  275. return mem->vmalloc;
  276. }
  277. EXPORT_SYMBOL_GPL(videobuf_to_vmalloc);
  278. void videobuf_vmalloc_free (struct videobuf_buffer *buf)
  279. {
  280. struct videobuf_vmalloc_memory *mem=buf->priv;
  281. BUG_ON (!mem);
  282. MAGIC_CHECK(mem->magic,MAGIC_VMAL_MEM);
  283. vfree(mem->vmalloc);
  284. mem->vmalloc=NULL;
  285. return;
  286. }
  287. EXPORT_SYMBOL_GPL(videobuf_vmalloc_free);
  288. /*
  289. * Local variables:
  290. * c-basic-offset: 8
  291. * End:
  292. */