videobuf-dma-sg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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@kernel.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@kernel.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/mm.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@kernel.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(array_size(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(array_size(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_array(dma->nr_pages, sizeof(struct page *),
  154. GFP_KERNEL);
  155. if (NULL == dma->pages)
  156. return -ENOMEM;
  157. if (rw == READ)
  158. flags |= FOLL_WRITE;
  159. dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n",
  160. data, size, dma->nr_pages);
  161. err = get_user_pages_longterm(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_longterm: err=%d [%d]\n", err,
  166. dma->nr_pages);
  167. return err < 0 ? err : -EINVAL;
  168. }
  169. return 0;
  170. }
  171. static int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
  172. unsigned long data, unsigned long size)
  173. {
  174. int ret;
  175. down_read(&current->mm->mmap_sem);
  176. ret = videobuf_dma_init_user_locked(dma, direction, data, size);
  177. up_read(&current->mm->mmap_sem);
  178. return ret;
  179. }
  180. static int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
  181. int nr_pages)
  182. {
  183. int i;
  184. dprintk(1, "init kernel [%d pages]\n", nr_pages);
  185. dma->direction = direction;
  186. dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages),
  187. GFP_KERNEL);
  188. if (!dma->vaddr_pages)
  189. return -ENOMEM;
  190. dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL);
  191. if (!dma->dma_addr) {
  192. kfree(dma->vaddr_pages);
  193. return -ENOMEM;
  194. }
  195. for (i = 0; i < nr_pages; i++) {
  196. void *addr;
  197. addr = dma_alloc_coherent(dma->dev, PAGE_SIZE,
  198. &(dma->dma_addr[i]), GFP_KERNEL);
  199. if (addr == NULL)
  200. goto out_free_pages;
  201. dma->vaddr_pages[i] = virt_to_page(addr);
  202. }
  203. dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP,
  204. PAGE_KERNEL);
  205. if (NULL == dma->vaddr) {
  206. dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages);
  207. goto out_free_pages;
  208. }
  209. dprintk(1, "vmalloc is at addr %p, size=%d\n",
  210. dma->vaddr, 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->nr_pages, 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. if (dma->direction == DMA_FROM_DEVICE)
  299. set_page_dirty_lock(dma->pages[i]);
  300. put_page(dma->pages[i]);
  301. }
  302. kfree(dma->pages);
  303. dma->pages = NULL;
  304. }
  305. if (dma->dma_addr) {
  306. for (i = 0; i < dma->nr_pages; i++) {
  307. void *addr;
  308. addr = page_address(dma->vaddr_pages[i]);
  309. dma_free_coherent(dma->dev, PAGE_SIZE, addr,
  310. dma->dma_addr[i]);
  311. }
  312. kfree(dma->dma_addr);
  313. dma->dma_addr = NULL;
  314. kfree(dma->vaddr_pages);
  315. dma->vaddr_pages = NULL;
  316. vunmap(dma->vaddr);
  317. dma->vaddr = NULL;
  318. }
  319. if (dma->bus_addr)
  320. dma->bus_addr = 0;
  321. dma->direction = DMA_NONE;
  322. return 0;
  323. }
  324. EXPORT_SYMBOL_GPL(videobuf_dma_free);
  325. /* --------------------------------------------------------------------- */
  326. static void videobuf_vm_open(struct vm_area_struct *vma)
  327. {
  328. struct videobuf_mapping *map = vma->vm_private_data;
  329. dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map,
  330. map->count, vma->vm_start, vma->vm_end);
  331. map->count++;
  332. }
  333. static void videobuf_vm_close(struct vm_area_struct *vma)
  334. {
  335. struct videobuf_mapping *map = vma->vm_private_data;
  336. struct videobuf_queue *q = map->q;
  337. struct videobuf_dma_sg_memory *mem;
  338. int i;
  339. dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map,
  340. map->count, vma->vm_start, vma->vm_end);
  341. map->count--;
  342. if (0 == map->count) {
  343. dprintk(1, "munmap %p q=%p\n", map, q);
  344. videobuf_queue_lock(q);
  345. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  346. if (NULL == q->bufs[i])
  347. continue;
  348. mem = q->bufs[i]->priv;
  349. if (!mem)
  350. continue;
  351. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  352. if (q->bufs[i]->map != map)
  353. continue;
  354. q->bufs[i]->map = NULL;
  355. q->bufs[i]->baddr = 0;
  356. q->ops->buf_release(q, q->bufs[i]);
  357. }
  358. videobuf_queue_unlock(q);
  359. kfree(map);
  360. }
  361. return;
  362. }
  363. /*
  364. * Get a anonymous page for the mapping. Make sure we can DMA to that
  365. * memory location with 32bit PCI devices (i.e. don't use highmem for
  366. * now ...). Bounce buffers don't work very well for the data rates
  367. * video capture has.
  368. */
  369. static vm_fault_t videobuf_vm_fault(struct vm_fault *vmf)
  370. {
  371. struct vm_area_struct *vma = vmf->vma;
  372. struct page *page;
  373. dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n",
  374. vmf->address, vma->vm_start, vma->vm_end);
  375. page = alloc_page(GFP_USER | __GFP_DMA32);
  376. if (!page)
  377. return VM_FAULT_OOM;
  378. clear_user_highpage(page, vmf->address);
  379. vmf->page = page;
  380. return 0;
  381. }
  382. static const struct vm_operations_struct videobuf_vm_ops = {
  383. .open = videobuf_vm_open,
  384. .close = videobuf_vm_close,
  385. .fault = videobuf_vm_fault,
  386. };
  387. /* ---------------------------------------------------------------------
  388. * SG handlers for the generic methods
  389. */
  390. /* Allocated area consists on 3 parts:
  391. struct video_buffer
  392. struct <driver>_buffer (cx88_buffer, saa7134_buf, ...)
  393. struct videobuf_dma_sg_memory
  394. */
  395. static struct videobuf_buffer *__videobuf_alloc_vb(size_t size)
  396. {
  397. struct videobuf_dma_sg_memory *mem;
  398. struct videobuf_buffer *vb;
  399. vb = kzalloc(size + sizeof(*mem), GFP_KERNEL);
  400. if (!vb)
  401. return vb;
  402. mem = vb->priv = ((char *)vb) + size;
  403. mem->magic = MAGIC_SG_MEM;
  404. videobuf_dma_init(&mem->dma);
  405. dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n",
  406. __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb),
  407. mem, (long)sizeof(*mem));
  408. return vb;
  409. }
  410. static void *__videobuf_to_vaddr(struct videobuf_buffer *buf)
  411. {
  412. struct videobuf_dma_sg_memory *mem = buf->priv;
  413. BUG_ON(!mem);
  414. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  415. return mem->dma.vaddr;
  416. }
  417. static int __videobuf_iolock(struct videobuf_queue *q,
  418. struct videobuf_buffer *vb,
  419. struct v4l2_framebuffer *fbuf)
  420. {
  421. int err, pages;
  422. dma_addr_t bus;
  423. struct videobuf_dma_sg_memory *mem = vb->priv;
  424. BUG_ON(!mem);
  425. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  426. if (!mem->dma.dev)
  427. mem->dma.dev = q->dev;
  428. else
  429. WARN_ON(mem->dma.dev != q->dev);
  430. switch (vb->memory) {
  431. case V4L2_MEMORY_MMAP:
  432. case V4L2_MEMORY_USERPTR:
  433. if (0 == vb->baddr) {
  434. /* no userspace addr -- kernel bounce buffer */
  435. pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
  436. err = videobuf_dma_init_kernel(&mem->dma,
  437. DMA_FROM_DEVICE,
  438. pages);
  439. if (0 != err)
  440. return err;
  441. } else if (vb->memory == V4L2_MEMORY_USERPTR) {
  442. /* dma directly to userspace */
  443. err = videobuf_dma_init_user(&mem->dma,
  444. DMA_FROM_DEVICE,
  445. vb->baddr, vb->bsize);
  446. if (0 != err)
  447. return err;
  448. } else {
  449. /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP
  450. buffers can only be called from videobuf_qbuf
  451. we take current->mm->mmap_sem there, to prevent
  452. locking inversion, so don't take it here */
  453. err = videobuf_dma_init_user_locked(&mem->dma,
  454. DMA_FROM_DEVICE,
  455. vb->baddr, vb->bsize);
  456. if (0 != err)
  457. return err;
  458. }
  459. break;
  460. case V4L2_MEMORY_OVERLAY:
  461. if (NULL == fbuf)
  462. return -EINVAL;
  463. /* FIXME: need sanity checks for vb->boff */
  464. /*
  465. * Using a double cast to avoid compiler warnings when
  466. * building for PAE. Compiler doesn't like direct casting
  467. * of a 32 bit ptr to 64 bit integer.
  468. */
  469. bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
  470. pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
  471. err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE,
  472. bus, pages);
  473. if (0 != err)
  474. return err;
  475. break;
  476. default:
  477. BUG();
  478. }
  479. err = videobuf_dma_map(q->dev, &mem->dma);
  480. if (0 != err)
  481. return err;
  482. return 0;
  483. }
  484. static int __videobuf_sync(struct videobuf_queue *q,
  485. struct videobuf_buffer *buf)
  486. {
  487. struct videobuf_dma_sg_memory *mem = buf->priv;
  488. BUG_ON(!mem || !mem->dma.sglen);
  489. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  490. MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF);
  491. dma_sync_sg_for_cpu(q->dev, mem->dma.sglist,
  492. mem->dma.nr_pages, mem->dma.direction);
  493. return 0;
  494. }
  495. static int __videobuf_mmap_mapper(struct videobuf_queue *q,
  496. struct videobuf_buffer *buf,
  497. struct vm_area_struct *vma)
  498. {
  499. struct videobuf_dma_sg_memory *mem = buf->priv;
  500. struct videobuf_mapping *map;
  501. unsigned int first, last, size = 0, i;
  502. int retval;
  503. retval = -EINVAL;
  504. BUG_ON(!mem);
  505. MAGIC_CHECK(mem->magic, MAGIC_SG_MEM);
  506. /* look for first buffer to map */
  507. for (first = 0; first < VIDEO_MAX_FRAME; first++) {
  508. if (buf == q->bufs[first]) {
  509. size = PAGE_ALIGN(q->bufs[first]->bsize);
  510. break;
  511. }
  512. }
  513. /* paranoia, should never happen since buf is always valid. */
  514. if (!size) {
  515. dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n",
  516. (vma->vm_pgoff << PAGE_SHIFT));
  517. goto done;
  518. }
  519. last = first;
  520. /* create mapping + update buffer list */
  521. retval = -ENOMEM;
  522. map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL);
  523. if (NULL == map)
  524. goto done;
  525. size = 0;
  526. for (i = first; i <= last; i++) {
  527. if (NULL == q->bufs[i])
  528. continue;
  529. q->bufs[i]->map = map;
  530. q->bufs[i]->baddr = vma->vm_start + size;
  531. size += PAGE_ALIGN(q->bufs[i]->bsize);
  532. }
  533. map->count = 1;
  534. map->q = q;
  535. vma->vm_ops = &videobuf_vm_ops;
  536. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  537. vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
  538. vma->vm_private_data = map;
  539. dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
  540. map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last);
  541. retval = 0;
  542. done:
  543. return retval;
  544. }
  545. static struct videobuf_qtype_ops sg_ops = {
  546. .magic = MAGIC_QTYPE_OPS,
  547. .alloc_vb = __videobuf_alloc_vb,
  548. .iolock = __videobuf_iolock,
  549. .sync = __videobuf_sync,
  550. .mmap_mapper = __videobuf_mmap_mapper,
  551. .vaddr = __videobuf_to_vaddr,
  552. };
  553. void *videobuf_sg_alloc(size_t size)
  554. {
  555. struct videobuf_queue q;
  556. /* Required to make generic handler to call __videobuf_alloc */
  557. q.int_ops = &sg_ops;
  558. q.msize = size;
  559. return videobuf_alloc_vb(&q);
  560. }
  561. EXPORT_SYMBOL_GPL(videobuf_sg_alloc);
  562. void videobuf_queue_sg_init(struct videobuf_queue *q,
  563. const struct videobuf_queue_ops *ops,
  564. struct device *dev,
  565. spinlock_t *irqlock,
  566. enum v4l2_buf_type type,
  567. enum v4l2_field field,
  568. unsigned int msize,
  569. void *priv,
  570. struct mutex *ext_lock)
  571. {
  572. videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize,
  573. priv, &sg_ops, ext_lock);
  574. }
  575. EXPORT_SYMBOL_GPL(videobuf_queue_sg_init);