videobuf-dma-sg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*
  2. * helper functions for SG DMA 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. * Highly based on video-buf written originally by:
  12. * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
  13. * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
  14. * (c) 2006 Ted Walther and John Sokol
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2
  19. */
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/scatterlist.h>
  30. #include <asm/page.h>
  31. #include <asm/pgtable.h>
  32. #include <media/videobuf-dma-sg.h>
  33. #define MAGIC_DMABUF 0x19721112
  34. #define MAGIC_SG_MEM 0x17890714
  35. #define MAGIC_CHECK(is, should) \
  36. if (unlikely((is) != (should))) { \
  37. printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \
  38. is, should); \
  39. BUG(); \
  40. }
  41. static int debug;
  42. module_param(debug, int, 0644);
  43. MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers");
  44. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  45. MODULE_LICENSE("GPL");
  46. #define dprintk(level, fmt, arg...) \
  47. if (debug >= level) \
  48. printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg)
  49. /* --------------------------------------------------------------------- */
  50. /*
  51. * Return a scatterlist for some page-aligned vmalloc()'ed memory
  52. * block (NULL on errors). Memory for the scatterlist is allocated
  53. * using kmalloc. The caller must free the memory.
  54. */
  55. static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt,
  56. int nr_pages)
  57. {
  58. struct scatterlist *sglist;
  59. struct page *pg;
  60. int i;
  61. sglist = vzalloc(nr_pages * sizeof(*sglist));
  62. if (NULL == sglist)
  63. return NULL;
  64. sg_init_table(sglist, nr_pages);
  65. for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
  66. pg = vmalloc_to_page(virt);
  67. if (NULL == pg)
  68. goto err;
  69. BUG_ON(PageHighMem(pg));
  70. sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
  71. }
  72. return sglist;
  73. err:
  74. vfree(sglist);
  75. return NULL;
  76. }
  77. /*
  78. * Return a scatterlist for a an array of userpages (NULL on errors).
  79. * Memory for the scatterlist is allocated using kmalloc. The caller
  80. * must free the memory.
  81. */
  82. static struct scatterlist *videobuf_pages_to_sg(struct page **pages,
  83. int nr_pages, int offset, size_t size)
  84. {
  85. struct scatterlist *sglist;
  86. int i;
  87. if (NULL == pages[0])
  88. return NULL;
  89. sglist = vmalloc(nr_pages * sizeof(*sglist));
  90. if (NULL == sglist)
  91. return NULL;
  92. sg_init_table(sglist, nr_pages);
  93. if (PageHighMem(pages[0]))
  94. /* DMA to highmem pages might not work */
  95. goto highmem;
  96. sg_set_page(&sglist[0], pages[0],
  97. min_t(size_t, PAGE_SIZE - offset, size), offset);
  98. size -= min_t(size_t, PAGE_SIZE - offset, size);
  99. for (i = 1; i < nr_pages; i++) {
  100. if (NULL == pages[i])
  101. goto nopage;
  102. if (PageHighMem(pages[i]))
  103. goto highmem;
  104. sg_set_page(&sglist[i], pages[i], min_t(size_t, PAGE_SIZE, size), 0);
  105. size -= min_t(size_t, PAGE_SIZE, size);
  106. }
  107. return sglist;
  108. nopage:
  109. dprintk(2, "sgl: oops - no page\n");
  110. vfree(sglist);
  111. return NULL;
  112. highmem:
  113. dprintk(2, "sgl: oops - highmem page\n");
  114. vfree(sglist);
  115. return NULL;
  116. }
  117. /* --------------------------------------------------------------------- */
  118. struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf)
  119. {
  120. struct videobuf_dma_sg_memory *mem = buf->priv;
  121. BUG_ON(!mem);
  122. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  123. return &mem->dma;
  124. }
  125. EXPORT_SYMBOL_GPL(videobuf_to_dma);
  126. static void videobuf_dma_init(struct videobuf_dmabuf *dma)
  127. {
  128. memset(dma, 0, sizeof(*dma));
  129. dma->magic = MAGIC_DMABUF;
  130. }
  131. static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma,
  132. int direction, unsigned long data, unsigned long size)
  133. {
  134. unsigned long first, last;
  135. int err, rw = 0;
  136. unsigned int flags = FOLL_FORCE;
  137. dma->direction = direction;
  138. switch (dma->direction) {
  139. case DMA_FROM_DEVICE:
  140. rw = READ;
  141. break;
  142. case DMA_TO_DEVICE:
  143. rw = WRITE;
  144. break;
  145. default:
  146. BUG();
  147. }
  148. first = (data & PAGE_MASK) >> PAGE_SHIFT;
  149. last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
  150. dma->offset = data & ~PAGE_MASK;
  151. dma->size = size;
  152. dma->nr_pages = last-first+1;
  153. dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL);
  154. if (NULL == dma->pages)
  155. return -ENOMEM;
  156. if (rw == READ)
  157. flags |= FOLL_WRITE;
  158. dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
  159. data, size, dma->nr_pages);
  160. err = get_user_pages(current, current->mm,
  161. data & PAGE_MASK, dma->nr_pages,
  162. flags, dma->pages, NULL);
  163. if (err != dma->nr_pages) {
  164. dma->nr_pages = (err >= 0) ? err : 0;
  165. dprintk(1, "get_user_pages: err=%d [%d]\n", err, dma->nr_pages);
  166. return err < 0 ? err : -EINVAL;
  167. }
  168. return 0;
  169. }
  170. static int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
  171. unsigned long data, unsigned long size)
  172. {
  173. int ret;
  174. down_read(&current->mm->mmap_sem);
  175. ret = videobuf_dma_init_user_locked(dma, direction, data, size);
  176. up_read(&current->mm->mmap_sem);
  177. return ret;
  178. }
  179. static int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
  180. int nr_pages)
  181. {
  182. int i;
  183. dprintk(1, "init kernel [%d pages]\n", nr_pages);
  184. dma->direction = direction;
  185. dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages),
  186. GFP_KERNEL);
  187. if (!dma->vaddr_pages)
  188. return -ENOMEM;
  189. dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL);
  190. if (!dma->dma_addr) {
  191. kfree(dma->vaddr_pages);
  192. return -ENOMEM;
  193. }
  194. for (i = 0; i < nr_pages; i++) {
  195. void *addr;
  196. addr = dma_alloc_coherent(dma->dev, PAGE_SIZE,
  197. &(dma->dma_addr[i]), GFP_KERNEL);
  198. if (addr == NULL)
  199. goto out_free_pages;
  200. dma->vaddr_pages[i] = virt_to_page(addr);
  201. }
  202. dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP,
  203. PAGE_KERNEL);
  204. if (NULL == dma->vaddr) {
  205. dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
  206. goto out_free_pages;
  207. }
  208. dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n",
  209. (unsigned long)dma->vaddr,
  210. nr_pages << PAGE_SHIFT);
  211. memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
  212. dma->nr_pages = nr_pages;
  213. return 0;
  214. out_free_pages:
  215. while (i > 0) {
  216. void *addr;
  217. i--;
  218. addr = page_address(dma->vaddr_pages[i]);
  219. dma_free_coherent(dma->dev, PAGE_SIZE, addr, dma->dma_addr[i]);
  220. }
  221. kfree(dma->dma_addr);
  222. dma->dma_addr = NULL;
  223. kfree(dma->vaddr_pages);
  224. dma->vaddr_pages = NULL;
  225. return -ENOMEM;
  226. }
  227. static int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
  228. dma_addr_t addr, int nr_pages)
  229. {
  230. dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n",
  231. nr_pages, (unsigned long)addr);
  232. dma->direction = direction;
  233. if (0 == addr)
  234. return -EINVAL;
  235. dma->bus_addr = addr;
  236. dma->nr_pages = nr_pages;
  237. return 0;
  238. }
  239. static int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma)
  240. {
  241. MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
  242. BUG_ON(0 == dma->nr_pages);
  243. if (dma->pages) {
  244. dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
  245. dma->offset, dma->size);
  246. }
  247. if (dma->vaddr) {
  248. dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr,
  249. dma->nr_pages);
  250. }
  251. if (dma->bus_addr) {
  252. dma->sglist = vmalloc(sizeof(*dma->sglist));
  253. if (NULL != dma->sglist) {
  254. dma->sglen = 1;
  255. sg_dma_address(&dma->sglist[0]) = dma->bus_addr
  256. & PAGE_MASK;
  257. dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
  258. sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
  259. }
  260. }
  261. if (NULL == dma->sglist) {
  262. dprintk(1, "scatterlist is NULL\n");
  263. return -ENOMEM;
  264. }
  265. if (!dma->bus_addr) {
  266. dma->sglen = dma_map_sg(dev, dma->sglist,
  267. dma->nr_pages, dma->direction);
  268. if (0 == dma->sglen) {
  269. printk(KERN_WARNING
  270. "%s: videobuf_map_sg failed\n", __func__);
  271. vfree(dma->sglist);
  272. dma->sglist = NULL;
  273. dma->sglen = 0;
  274. return -ENOMEM;
  275. }
  276. }
  277. return 0;
  278. }
  279. int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma)
  280. {
  281. MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
  282. if (!dma->sglen)
  283. return 0;
  284. dma_unmap_sg(dev, dma->sglist, dma->sglen, dma->direction);
  285. vfree(dma->sglist);
  286. dma->sglist = NULL;
  287. dma->sglen = 0;
  288. return 0;
  289. }
  290. EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
  291. int videobuf_dma_free(struct videobuf_dmabuf *dma)
  292. {
  293. int i;
  294. MAGIC_CHECK(dma->magic, MAGIC_DMABUF);
  295. BUG_ON(dma->sglen);
  296. if (dma->pages) {
  297. for (i = 0; i < dma->nr_pages; i++)
  298. page_cache_release(dma->pages[i]);
  299. kfree(dma->pages);
  300. dma->pages = NULL;
  301. }
  302. if (dma->dma_addr) {
  303. for (i = 0; i < dma->nr_pages; i++) {
  304. void *addr;
  305. addr = page_address(dma->vaddr_pages[i]);
  306. dma_free_coherent(dma->dev, PAGE_SIZE, addr,
  307. dma->dma_addr[i]);
  308. }
  309. kfree(dma->dma_addr);
  310. dma->dma_addr = NULL;
  311. kfree(dma->vaddr_pages);
  312. dma->vaddr_pages = NULL;
  313. vunmap(dma->vaddr);
  314. dma->vaddr = NULL;
  315. }
  316. if (dma->bus_addr)
  317. dma->bus_addr = 0;
  318. dma->direction = DMA_NONE;
  319. return 0;
  320. }
  321. EXPORT_SYMBOL_GPL(videobuf_dma_free);
  322. /* --------------------------------------------------------------------- */
  323. static void videobuf_vm_open(struct vm_area_struct *vma)
  324. {
  325. struct videobuf_mapping *map = vma->vm_private_data;
  326. dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
  327. map->count, vma->vm_start, vma->vm_end);
  328. map->count++;
  329. }
  330. static void videobuf_vm_close(struct vm_area_struct *vma)
  331. {
  332. struct videobuf_mapping *map = vma->vm_private_data;
  333. struct videobuf_queue *q = map->q;
  334. struct videobuf_dma_sg_memory *mem;
  335. int i;
  336. dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
  337. map->count, vma->vm_start, vma->vm_end);
  338. map->count--;
  339. if (0 == map->count) {
  340. dprintk(1, "munmap %p q=%p\n", map, q);
  341. videobuf_queue_lock(q);
  342. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  343. if (NULL == q->bufs[i])
  344. continue;
  345. mem = q->bufs[i]->priv;
  346. if (!mem)
  347. continue;
  348. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  349. if (q->bufs[i]->map != map)
  350. continue;
  351. q->bufs[i]->map = NULL;
  352. q->bufs[i]->baddr = 0;
  353. q->ops->buf_release(q, q->bufs[i]);
  354. }
  355. videobuf_queue_unlock(q);
  356. kfree(map);
  357. }
  358. return;
  359. }
  360. /*
  361. * Get a anonymous page for the mapping. Make sure we can DMA to that
  362. * memory location with 32bit PCI devices (i.e. don't use highmem for
  363. * now ...). Bounce buffers don't work very well for the data rates
  364. * video capture has.
  365. */
  366. static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  367. {
  368. struct page *page;
  369. dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
  370. (unsigned long)vmf->virtual_address,
  371. vma->vm_start, vma->vm_end);
  372. page = alloc_page(GFP_USER | __GFP_DMA32);
  373. if (!page)
  374. return VM_FAULT_OOM;
  375. clear_user_highpage(page, (unsigned long)vmf->virtual_address);
  376. vmf->page = page;
  377. return 0;
  378. }
  379. static const struct vm_operations_struct videobuf_vm_ops = {
  380. .open = videobuf_vm_open,
  381. .close = videobuf_vm_close,
  382. .fault = videobuf_vm_fault,
  383. };
  384. /* ---------------------------------------------------------------------
  385. * SG handlers for the generic methods
  386. */
  387. /* Allocated area consists on 3 parts:
  388. struct video_buffer
  389. struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
  390. struct videobuf_dma_sg_memory
  391. */
  392. static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
  393. {
  394. struct videobuf_dma_sg_memory *mem;
  395. struct videobuf_buffer *vb;
  396. vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
  397. if (!vb)
  398. return vb;
  399. mem = vb->priv = ((char *)vb) + size;
  400. mem->magic = MAGIC_SG_MEM;
  401. videobuf_dma_init(&mem->dma);
  402. dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
  403. __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
  404. mem, (long)sizeof(*mem));
  405. return vb;
  406. }
  407. static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
  408. {
  409. struct videobuf_dma_sg_memory *mem = buf->priv;
  410. BUG_ON(!mem);
  411. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  412. return mem->dma.vaddr;
  413. }
  414. static int __videobuf_iolock(struct videobuf_queue *q,
  415. struct videobuf_buffer *vb,
  416. struct v4l2_framebuffer *fbuf)
  417. {
  418. int err, pages;
  419. dma_addr_t bus;
  420. struct videobuf_dma_sg_memory *mem = vb->priv;
  421. BUG_ON(!mem);
  422. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  423. if (!mem->dma.dev)
  424. mem->dma.dev = q->dev;
  425. else
  426. WARN_ON(mem->dma.dev != q->dev);
  427. switch (vb->memory) {
  428. case V4L2_MEMORY_MMAP:
  429. case V4L2_MEMORY_USERPTR:
  430. if (0 == vb->baddr) {
  431. /* no userspace addr -- kernel bounce buffer */
  432. pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
  433. err = videobuf_dma_init_kernel(&mem->dma,
  434. DMA_FROM_DEVICE,
  435. pages);
  436. if (0 != err)
  437. return err;
  438. } else if (vb->memory == V4L2_MEMORY_USERPTR) {
  439. /* dma directly to userspace */
  440. err = videobuf_dma_init_user(&mem->dma,
  441. DMA_FROM_DEVICE,
  442. vb->baddr, vb->bsize);
  443. if (0 != err)
  444. return err;
  445. } else {
  446. /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
  447. buffers can only be called from videobuf_qbuf
  448. we take current->mm->mmap_sem there, to prevent
  449. locking inversion, so don't take it here */
  450. err = videobuf_dma_init_user_locked(&mem->dma,
  451. DMA_FROM_DEVICE,
  452. vb->baddr, vb->bsize);
  453. if (0 != err)
  454. return err;
  455. }
  456. break;
  457. case V4L2_MEMORY_OVERLAY:
  458. if (NULL == fbuf)
  459. return -EINVAL;
  460. /* FIXME: need sanity checks for vb->boff */
  461. /*
  462. * Using a double cast to avoid compiler warnings when
  463. * building for PAE. Compiler doesn't like direct casting
  464. * of a 32 bit ptr to 64 bit integer.
  465. */
  466. bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
  467. pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
  468. err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
  469. bus, pages);
  470. if (0 != err)
  471. return err;
  472. break;
  473. default:
  474. BUG();
  475. }
  476. err = videobuf_dma_map(q->dev, &mem->dma);
  477. if (0 != err)
  478. return err;
  479. return 0;
  480. }
  481. static int __videobuf_sync(struct videobuf_queue *q,
  482. struct videobuf_buffer *buf)
  483. {
  484. struct videobuf_dma_sg_memory *mem = buf->priv;
  485. BUG_ON(!mem || !mem->dma.sglen);
  486. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  487. MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
  488. dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
  489. mem->dma.sglen, mem->dma.direction);
  490. return 0;
  491. }
  492. static int __videobuf_mmap_mapper(struct videobuf_queue *q,
  493. struct videobuf_buffer *buf,
  494. struct vm_area_struct *vma)
  495. {
  496. struct videobuf_dma_sg_memory *mem = buf->priv;
  497. struct videobuf_mapping *map;
  498. unsigned int first, last, size = 0, i;
  499. int retval;
  500. retval = -EINVAL;
  501. BUG_ON(!mem);
  502. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  503. /* look for first buffer to map */
  504. for (first = 0; first < VIDEO_MAX_FRAME; first++) {
  505. if (buf == q->bufs[first]) {
  506. size = PAGE_ALIGN(q->bufs[first]->bsize);
  507. break;
  508. }
  509. }
  510. /* paranoia, should never happen since buf is always valid. */
  511. if (!size) {
  512. dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
  513. (vma->vm_pgoff << PAGE_SHIFT));
  514. goto done;
  515. }
  516. last = first;
  517. /* create mapping + update buffer list */
  518. retval = -ENOMEM;
  519. map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
  520. if (NULL == map)
  521. goto done;
  522. size = 0;
  523. for (i = first; i <= last; i++) {
  524. if (NULL == q->bufs[i])
  525. continue;
  526. q->bufs[i]->map = map;
  527. q->bufs[i]->baddr = vma->vm_start + size;
  528. size += PAGE_ALIGN(q->bufs[i]->bsize);
  529. }
  530. map->count = 1;
  531. map->q = q;
  532. vma->vm_ops = &videobuf_vm_ops;
  533. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  534. vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
  535. vma->vm_private_data = map;
  536. dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
  537. map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
  538. retval = 0;
  539. done:
  540. return retval;
  541. }
  542. static struct videobuf_qtype_ops sg_ops = {
  543. .magic = MAGIC_QTYPE_OPS,
  544. .alloc_vb = __videobuf_alloc_vb,
  545. .iolock = __videobuf_iolock,
  546. .sync = __videobuf_sync,
  547. .mmap_mapper = __videobuf_mmap_mapper,
  548. .vaddr = __videobuf_to_vaddr,
  549. };
  550. void *videobuf_sg_alloc(size_t size)
  551. {
  552. struct videobuf_queue q;
  553. /* Required to make generic handler to call __videobuf_alloc */
  554. q.int_ops = &sg_ops;
  555. q.msize = size;
  556. return videobuf_alloc_vb(&q);
  557. }
  558. EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
  559. void videobuf_queue_sg_init(struct videobuf_queue *q,
  560. const struct videobuf_queue_ops *ops,
  561. struct device *dev,
  562. spinlock_t *irqlock,
  563. enum v4l2_buf_type type,
  564. enum v4l2_field field,
  565. unsigned int msize,
  566. void *priv,
  567. struct mutex *ext_lock)
  568. {
  569. videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
  570. priv, &sg_ops, ext_lock);
  571. }
  572. EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);