[media] vb2-dma-sg: add support for dmabuf exports
[linux-2.6-microblaze.git] / drivers / media / v4l2-core / videobuf2-dma-sg.c
1 /*
2  * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  *
6  * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/mm.h>
15 #include <linux/scatterlist.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19
20 #include <media/videobuf2-core.h>
21 #include <media/videobuf2-memops.h>
22 #include <media/videobuf2-dma-sg.h>
23
24 static int debug;
25 module_param(debug, int, 0644);
26
27 #define dprintk(level, fmt, arg...)                                     \
28         do {                                                            \
29                 if (debug >= level)                                     \
30                         printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg);  \
31         } while (0)
32
33 struct vb2_dma_sg_conf {
34         struct device           *dev;
35 };
36
37 struct vb2_dma_sg_buf {
38         struct device                   *dev;
39         void                            *vaddr;
40         struct page                     **pages;
41         int                             offset;
42         enum dma_data_direction         dma_dir;
43         struct sg_table                 sg_table;
44         /*
45          * This will point to sg_table when used with the MMAP or USERPTR
46          * memory model, and to the dma_buf sglist when used with the
47          * DMABUF memory model.
48          */
49         struct sg_table                 *dma_sgt;
50         size_t                          size;
51         unsigned int                    num_pages;
52         atomic_t                        refcount;
53         struct vb2_vmarea_handler       handler;
54         struct vm_area_struct           *vma;
55
56         struct dma_buf_attachment       *db_attach;
57 };
58
59 static void vb2_dma_sg_put(void *buf_priv);
60
61 static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
62                 gfp_t gfp_flags)
63 {
64         unsigned int last_page = 0;
65         int size = buf->size;
66
67         while (size > 0) {
68                 struct page *pages;
69                 int order;
70                 int i;
71
72                 order = get_order(size);
73                 /* Dont over allocate*/
74                 if ((PAGE_SIZE << order) > size)
75                         order--;
76
77                 pages = NULL;
78                 while (!pages) {
79                         pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
80                                         __GFP_NOWARN | gfp_flags, order);
81                         if (pages)
82                                 break;
83
84                         if (order == 0) {
85                                 while (last_page--)
86                                         __free_page(buf->pages[last_page]);
87                                 return -ENOMEM;
88                         }
89                         order--;
90                 }
91
92                 split_page(pages, order);
93                 for (i = 0; i < (1 << order); i++)
94                         buf->pages[last_page++] = &pages[i];
95
96                 size -= PAGE_SIZE << order;
97         }
98
99         return 0;
100 }
101
102 static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size,
103                               enum dma_data_direction dma_dir, gfp_t gfp_flags)
104 {
105         struct vb2_dma_sg_conf *conf = alloc_ctx;
106         struct vb2_dma_sg_buf *buf;
107         struct sg_table *sgt;
108         int ret;
109         int num_pages;
110
111         if (WARN_ON(alloc_ctx == NULL))
112                 return NULL;
113         buf = kzalloc(sizeof *buf, GFP_KERNEL);
114         if (!buf)
115                 return NULL;
116
117         buf->vaddr = NULL;
118         buf->dma_dir = dma_dir;
119         buf->offset = 0;
120         buf->size = size;
121         /* size is already page aligned */
122         buf->num_pages = size >> PAGE_SHIFT;
123         buf->dma_sgt = &buf->sg_table;
124
125         buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
126                              GFP_KERNEL);
127         if (!buf->pages)
128                 goto fail_pages_array_alloc;
129
130         ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
131         if (ret)
132                 goto fail_pages_alloc;
133
134         ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
135                         buf->num_pages, 0, size, GFP_KERNEL);
136         if (ret)
137                 goto fail_table_alloc;
138
139         /* Prevent the device from being released while the buffer is used */
140         buf->dev = get_device(conf->dev);
141
142         sgt = &buf->sg_table;
143         if (dma_map_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir) == 0)
144                 goto fail_map;
145         dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
146
147         buf->handler.refcount = &buf->refcount;
148         buf->handler.put = vb2_dma_sg_put;
149         buf->handler.arg = buf;
150
151         atomic_inc(&buf->refcount);
152
153         dprintk(1, "%s: Allocated buffer of %d pages\n",
154                 __func__, buf->num_pages);
155         return buf;
156
157 fail_map:
158         put_device(buf->dev);
159         sg_free_table(buf->dma_sgt);
160 fail_table_alloc:
161         num_pages = buf->num_pages;
162         while (num_pages--)
163                 __free_page(buf->pages[num_pages]);
164 fail_pages_alloc:
165         kfree(buf->pages);
166 fail_pages_array_alloc:
167         kfree(buf);
168         return NULL;
169 }
170
171 static void vb2_dma_sg_put(void *buf_priv)
172 {
173         struct vb2_dma_sg_buf *buf = buf_priv;
174         struct sg_table *sgt = &buf->sg_table;
175         int i = buf->num_pages;
176
177         if (atomic_dec_and_test(&buf->refcount)) {
178                 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
179                         buf->num_pages);
180                 dma_unmap_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
181                 if (buf->vaddr)
182                         vm_unmap_ram(buf->vaddr, buf->num_pages);
183                 sg_free_table(buf->dma_sgt);
184                 while (--i >= 0)
185                         __free_page(buf->pages[i]);
186                 kfree(buf->pages);
187                 put_device(buf->dev);
188                 kfree(buf);
189         }
190 }
191
192 static void vb2_dma_sg_prepare(void *buf_priv)
193 {
194         struct vb2_dma_sg_buf *buf = buf_priv;
195         struct sg_table *sgt = buf->dma_sgt;
196
197         /* DMABUF exporter will flush the cache for us */
198         if (buf->db_attach)
199                 return;
200
201         dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
202 }
203
204 static void vb2_dma_sg_finish(void *buf_priv)
205 {
206         struct vb2_dma_sg_buf *buf = buf_priv;
207         struct sg_table *sgt = buf->dma_sgt;
208
209         /* DMABUF exporter will flush the cache for us */
210         if (buf->db_attach)
211                 return;
212
213         dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
214 }
215
216 static inline int vma_is_io(struct vm_area_struct *vma)
217 {
218         return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
219 }
220
221 static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
222                                     unsigned long size,
223                                     enum dma_data_direction dma_dir)
224 {
225         struct vb2_dma_sg_conf *conf = alloc_ctx;
226         struct vb2_dma_sg_buf *buf;
227         unsigned long first, last;
228         int num_pages_from_user;
229         struct vm_area_struct *vma;
230         struct sg_table *sgt;
231
232         buf = kzalloc(sizeof *buf, GFP_KERNEL);
233         if (!buf)
234                 return NULL;
235
236         buf->vaddr = NULL;
237         buf->dev = conf->dev;
238         buf->dma_dir = dma_dir;
239         buf->offset = vaddr & ~PAGE_MASK;
240         buf->size = size;
241         buf->dma_sgt = &buf->sg_table;
242
243         first = (vaddr           & PAGE_MASK) >> PAGE_SHIFT;
244         last  = ((vaddr + size - 1) & PAGE_MASK) >> PAGE_SHIFT;
245         buf->num_pages = last - first + 1;
246
247         buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
248                              GFP_KERNEL);
249         if (!buf->pages)
250                 goto userptr_fail_alloc_pages;
251
252         vma = find_vma(current->mm, vaddr);
253         if (!vma) {
254                 dprintk(1, "no vma for address %lu\n", vaddr);
255                 goto userptr_fail_find_vma;
256         }
257
258         if (vma->vm_end < vaddr + size) {
259                 dprintk(1, "vma at %lu is too small for %lu bytes\n",
260                         vaddr, size);
261                 goto userptr_fail_find_vma;
262         }
263
264         buf->vma = vb2_get_vma(vma);
265         if (!buf->vma) {
266                 dprintk(1, "failed to copy vma\n");
267                 goto userptr_fail_find_vma;
268         }
269
270         if (vma_is_io(buf->vma)) {
271                 for (num_pages_from_user = 0;
272                      num_pages_from_user < buf->num_pages;
273                      ++num_pages_from_user, vaddr += PAGE_SIZE) {
274                         unsigned long pfn;
275
276                         if (follow_pfn(vma, vaddr, &pfn)) {
277                                 dprintk(1, "no page for address %lu\n", vaddr);
278                                 break;
279                         }
280                         buf->pages[num_pages_from_user] = pfn_to_page(pfn);
281                 }
282         } else
283                 num_pages_from_user = get_user_pages(current, current->mm,
284                                              vaddr & PAGE_MASK,
285                                              buf->num_pages,
286                                              buf->dma_dir == DMA_FROM_DEVICE,
287                                              1, /* force */
288                                              buf->pages,
289                                              NULL);
290
291         if (num_pages_from_user != buf->num_pages)
292                 goto userptr_fail_get_user_pages;
293
294         if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
295                         buf->num_pages, buf->offset, size, 0))
296                 goto userptr_fail_alloc_table_from_pages;
297
298         sgt = &buf->sg_table;
299         if (dma_map_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir) == 0)
300                 goto userptr_fail_map;
301         dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
302         return buf;
303
304 userptr_fail_map:
305         sg_free_table(&buf->sg_table);
306 userptr_fail_alloc_table_from_pages:
307 userptr_fail_get_user_pages:
308         dprintk(1, "get_user_pages requested/got: %d/%d]\n",
309                 buf->num_pages, num_pages_from_user);
310         if (!vma_is_io(buf->vma))
311                 while (--num_pages_from_user >= 0)
312                         put_page(buf->pages[num_pages_from_user]);
313         vb2_put_vma(buf->vma);
314 userptr_fail_find_vma:
315         kfree(buf->pages);
316 userptr_fail_alloc_pages:
317         kfree(buf);
318         return NULL;
319 }
320
321 /*
322  * @put_userptr: inform the allocator that a USERPTR buffer will no longer
323  *               be used
324  */
325 static void vb2_dma_sg_put_userptr(void *buf_priv)
326 {
327         struct vb2_dma_sg_buf *buf = buf_priv;
328         struct sg_table *sgt = &buf->sg_table;
329         int i = buf->num_pages;
330
331         dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
332                __func__, buf->num_pages);
333         dma_unmap_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
334         if (buf->vaddr)
335                 vm_unmap_ram(buf->vaddr, buf->num_pages);
336         sg_free_table(buf->dma_sgt);
337         while (--i >= 0) {
338                 if (buf->dma_dir == DMA_FROM_DEVICE)
339                         set_page_dirty_lock(buf->pages[i]);
340                 if (!vma_is_io(buf->vma))
341                         put_page(buf->pages[i]);
342         }
343         kfree(buf->pages);
344         vb2_put_vma(buf->vma);
345         kfree(buf);
346 }
347
348 static void *vb2_dma_sg_vaddr(void *buf_priv)
349 {
350         struct vb2_dma_sg_buf *buf = buf_priv;
351
352         BUG_ON(!buf);
353
354         if (!buf->vaddr) {
355                 if (buf->db_attach)
356                         buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
357                 else
358                         buf->vaddr = vm_map_ram(buf->pages,
359                                         buf->num_pages, -1, PAGE_KERNEL);
360         }
361
362         /* add offset in case userptr is not page-aligned */
363         return buf->vaddr ? buf->vaddr + buf->offset : NULL;
364 }
365
366 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
367 {
368         struct vb2_dma_sg_buf *buf = buf_priv;
369
370         return atomic_read(&buf->refcount);
371 }
372
373 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
374 {
375         struct vb2_dma_sg_buf *buf = buf_priv;
376         unsigned long uaddr = vma->vm_start;
377         unsigned long usize = vma->vm_end - vma->vm_start;
378         int i = 0;
379
380         if (!buf) {
381                 printk(KERN_ERR "No memory to map\n");
382                 return -EINVAL;
383         }
384
385         do {
386                 int ret;
387
388                 ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
389                 if (ret) {
390                         printk(KERN_ERR "Remapping memory, error: %d\n", ret);
391                         return ret;
392                 }
393
394                 uaddr += PAGE_SIZE;
395                 usize -= PAGE_SIZE;
396         } while (usize > 0);
397
398
399         /*
400          * Use common vm_area operations to track buffer refcount.
401          */
402         vma->vm_private_data    = &buf->handler;
403         vma->vm_ops             = &vb2_common_vm_ops;
404
405         vma->vm_ops->open(vma);
406
407         return 0;
408 }
409
410 /*********************************************/
411 /*         DMABUF ops for exporters          */
412 /*********************************************/
413
414 struct vb2_dma_sg_attachment {
415         struct sg_table sgt;
416         enum dma_data_direction dma_dir;
417 };
418
419 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
420         struct dma_buf_attachment *dbuf_attach)
421 {
422         struct vb2_dma_sg_attachment *attach;
423         unsigned int i;
424         struct scatterlist *rd, *wr;
425         struct sg_table *sgt;
426         struct vb2_dma_sg_buf *buf = dbuf->priv;
427         int ret;
428
429         attach = kzalloc(sizeof(*attach), GFP_KERNEL);
430         if (!attach)
431                 return -ENOMEM;
432
433         sgt = &attach->sgt;
434         /* Copy the buf->base_sgt scatter list to the attachment, as we can't
435          * map the same scatter list to multiple attachments at the same time.
436          */
437         ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
438         if (ret) {
439                 kfree(attach);
440                 return -ENOMEM;
441         }
442
443         rd = buf->dma_sgt->sgl;
444         wr = sgt->sgl;
445         for (i = 0; i < sgt->orig_nents; ++i) {
446                 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
447                 rd = sg_next(rd);
448                 wr = sg_next(wr);
449         }
450
451         attach->dma_dir = DMA_NONE;
452         dbuf_attach->priv = attach;
453
454         return 0;
455 }
456
457 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
458         struct dma_buf_attachment *db_attach)
459 {
460         struct vb2_dma_sg_attachment *attach = db_attach->priv;
461         struct sg_table *sgt;
462
463         if (!attach)
464                 return;
465
466         sgt = &attach->sgt;
467
468         /* release the scatterlist cache */
469         if (attach->dma_dir != DMA_NONE)
470                 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
471                         attach->dma_dir);
472         sg_free_table(sgt);
473         kfree(attach);
474         db_attach->priv = NULL;
475 }
476
477 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
478         struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
479 {
480         struct vb2_dma_sg_attachment *attach = db_attach->priv;
481         /* stealing dmabuf mutex to serialize map/unmap operations */
482         struct mutex *lock = &db_attach->dmabuf->lock;
483         struct sg_table *sgt;
484         int ret;
485
486         mutex_lock(lock);
487
488         sgt = &attach->sgt;
489         /* return previously mapped sg table */
490         if (attach->dma_dir == dma_dir) {
491                 mutex_unlock(lock);
492                 return sgt;
493         }
494
495         /* release any previous cache */
496         if (attach->dma_dir != DMA_NONE) {
497                 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
498                         attach->dma_dir);
499                 attach->dma_dir = DMA_NONE;
500         }
501
502         /* mapping to the client with new direction */
503         ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dma_dir);
504         if (ret <= 0) {
505                 pr_err("failed to map scatterlist\n");
506                 mutex_unlock(lock);
507                 return ERR_PTR(-EIO);
508         }
509
510         attach->dma_dir = dma_dir;
511
512         mutex_unlock(lock);
513
514         return sgt;
515 }
516
517 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
518         struct sg_table *sgt, enum dma_data_direction dma_dir)
519 {
520         /* nothing to be done here */
521 }
522
523 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
524 {
525         /* drop reference obtained in vb2_dma_sg_get_dmabuf */
526         vb2_dma_sg_put(dbuf->priv);
527 }
528
529 static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
530 {
531         struct vb2_dma_sg_buf *buf = dbuf->priv;
532
533         return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
534 }
535
536 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
537 {
538         struct vb2_dma_sg_buf *buf = dbuf->priv;
539
540         return vb2_dma_sg_vaddr(buf);
541 }
542
543 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
544         struct vm_area_struct *vma)
545 {
546         return vb2_dma_sg_mmap(dbuf->priv, vma);
547 }
548
549 static struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
550         .attach = vb2_dma_sg_dmabuf_ops_attach,
551         .detach = vb2_dma_sg_dmabuf_ops_detach,
552         .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
553         .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
554         .kmap = vb2_dma_sg_dmabuf_ops_kmap,
555         .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap,
556         .vmap = vb2_dma_sg_dmabuf_ops_vmap,
557         .mmap = vb2_dma_sg_dmabuf_ops_mmap,
558         .release = vb2_dma_sg_dmabuf_ops_release,
559 };
560
561 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
562 {
563         struct vb2_dma_sg_buf *buf = buf_priv;
564         struct dma_buf *dbuf;
565
566         if (WARN_ON(!buf->dma_sgt))
567                 return NULL;
568
569         dbuf = dma_buf_export(buf, &vb2_dma_sg_dmabuf_ops, buf->size, flags, NULL);
570         if (IS_ERR(dbuf))
571                 return NULL;
572
573         /* dmabuf keeps reference to vb2 buffer */
574         atomic_inc(&buf->refcount);
575
576         return dbuf;
577 }
578
579 /*********************************************/
580 /*       callbacks for DMABUF buffers        */
581 /*********************************************/
582
583 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
584 {
585         struct vb2_dma_sg_buf *buf = mem_priv;
586         struct sg_table *sgt;
587
588         if (WARN_ON(!buf->db_attach)) {
589                 pr_err("trying to pin a non attached buffer\n");
590                 return -EINVAL;
591         }
592
593         if (WARN_ON(buf->dma_sgt)) {
594                 pr_err("dmabuf buffer is already pinned\n");
595                 return 0;
596         }
597
598         /* get the associated scatterlist for this buffer */
599         sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
600         if (IS_ERR(sgt)) {
601                 pr_err("Error getting dmabuf scatterlist\n");
602                 return -EINVAL;
603         }
604
605         buf->dma_sgt = sgt;
606         buf->vaddr = NULL;
607
608         return 0;
609 }
610
611 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
612 {
613         struct vb2_dma_sg_buf *buf = mem_priv;
614         struct sg_table *sgt = buf->dma_sgt;
615
616         if (WARN_ON(!buf->db_attach)) {
617                 pr_err("trying to unpin a not attached buffer\n");
618                 return;
619         }
620
621         if (WARN_ON(!sgt)) {
622                 pr_err("dmabuf buffer is already unpinned\n");
623                 return;
624         }
625
626         if (buf->vaddr) {
627                 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
628                 buf->vaddr = NULL;
629         }
630         dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
631
632         buf->dma_sgt = NULL;
633 }
634
635 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
636 {
637         struct vb2_dma_sg_buf *buf = mem_priv;
638
639         /* if vb2 works correctly you should never detach mapped buffer */
640         if (WARN_ON(buf->dma_sgt))
641                 vb2_dma_sg_unmap_dmabuf(buf);
642
643         /* detach this attachment */
644         dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
645         kfree(buf);
646 }
647
648 static void *vb2_dma_sg_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
649         unsigned long size, enum dma_data_direction dma_dir)
650 {
651         struct vb2_dma_sg_conf *conf = alloc_ctx;
652         struct vb2_dma_sg_buf *buf;
653         struct dma_buf_attachment *dba;
654
655         if (dbuf->size < size)
656                 return ERR_PTR(-EFAULT);
657
658         buf = kzalloc(sizeof(*buf), GFP_KERNEL);
659         if (!buf)
660                 return ERR_PTR(-ENOMEM);
661
662         buf->dev = conf->dev;
663         /* create attachment for the dmabuf with the user device */
664         dba = dma_buf_attach(dbuf, buf->dev);
665         if (IS_ERR(dba)) {
666                 pr_err("failed to attach dmabuf\n");
667                 kfree(buf);
668                 return dba;
669         }
670
671         buf->dma_dir = dma_dir;
672         buf->size = size;
673         buf->db_attach = dba;
674
675         return buf;
676 }
677
678 static void *vb2_dma_sg_cookie(void *buf_priv)
679 {
680         struct vb2_dma_sg_buf *buf = buf_priv;
681
682         return buf->dma_sgt;
683 }
684
685 const struct vb2_mem_ops vb2_dma_sg_memops = {
686         .alloc          = vb2_dma_sg_alloc,
687         .put            = vb2_dma_sg_put,
688         .get_userptr    = vb2_dma_sg_get_userptr,
689         .put_userptr    = vb2_dma_sg_put_userptr,
690         .prepare        = vb2_dma_sg_prepare,
691         .finish         = vb2_dma_sg_finish,
692         .vaddr          = vb2_dma_sg_vaddr,
693         .mmap           = vb2_dma_sg_mmap,
694         .num_users      = vb2_dma_sg_num_users,
695         .get_dmabuf     = vb2_dma_sg_get_dmabuf,
696         .map_dmabuf     = vb2_dma_sg_map_dmabuf,
697         .unmap_dmabuf   = vb2_dma_sg_unmap_dmabuf,
698         .attach_dmabuf  = vb2_dma_sg_attach_dmabuf,
699         .detach_dmabuf  = vb2_dma_sg_detach_dmabuf,
700         .cookie         = vb2_dma_sg_cookie,
701 };
702 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
703
704 void *vb2_dma_sg_init_ctx(struct device *dev)
705 {
706         struct vb2_dma_sg_conf *conf;
707
708         conf = kzalloc(sizeof(*conf), GFP_KERNEL);
709         if (!conf)
710                 return ERR_PTR(-ENOMEM);
711
712         conf->dev = dev;
713
714         return conf;
715 }
716 EXPORT_SYMBOL_GPL(vb2_dma_sg_init_ctx);
717
718 void vb2_dma_sg_cleanup_ctx(void *alloc_ctx)
719 {
720         if (!IS_ERR_OR_NULL(alloc_ctx))
721                 kfree(alloc_ctx);
722 }
723 EXPORT_SYMBOL_GPL(vb2_dma_sg_cleanup_ctx);
724
725 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
726 MODULE_AUTHOR("Andrzej Pietrasiewicz");
727 MODULE_LICENSE("GPL");