videobuf-vmalloc.c 9.1 KB

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