videobuf-vmalloc.c 8.8 KB

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