hdmi: support parse and modify colorimetry.
[firefly-linux-kernel-4.4.55.git] / drivers / base / dma-buf.c
1 /*
2  * Framework for buffer objects that can be shared across devices/subsystems.
3  *
4  * Copyright(C) 2011 Linaro Limited. All rights reserved.
5  * Author: Sumit Semwal <sumit.semwal@ti.com>
6  *
7  * Many thanks to linaro-mm-sig list, and specially
8  * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
9  * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
10  * refining of this idea.
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License version 2 as published by
14  * the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License along with
22  * this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include <linux/fs.h>
26 #include <linux/slab.h>
27 #include <linux/dma-buf.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/export.h>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
32
33 static inline int is_dma_buf_file(struct file *);
34
35 struct dma_buf_list {
36         struct list_head head;
37         struct mutex lock;
38 };
39
40 static struct dma_buf_list db_list;
41
42 static int dma_buf_release(struct inode *inode, struct file *file)
43 {
44         struct dma_buf *dmabuf;
45
46         if (!is_dma_buf_file(file))
47                 return -EINVAL;
48
49         dmabuf = file->private_data;
50
51         BUG_ON(dmabuf->vmapping_counter);
52
53         dmabuf->ops->release(dmabuf);
54
55         mutex_lock(&db_list.lock);
56         list_del(&dmabuf->list_node);
57         mutex_unlock(&db_list.lock);
58
59         kfree(dmabuf);
60         return 0;
61 }
62
63 static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
64 {
65         struct dma_buf *dmabuf;
66
67         if (!is_dma_buf_file(file))
68                 return -EINVAL;
69
70         dmabuf = file->private_data;
71
72         /* check for overflowing the buffer's size */
73         if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
74             dmabuf->size >> PAGE_SHIFT)
75                 return -EINVAL;
76
77         return dmabuf->ops->mmap(dmabuf, vma);
78 }
79
80 static const struct file_operations dma_buf_fops = {
81         .release        = dma_buf_release,
82         .mmap           = dma_buf_mmap_internal,
83 };
84
85 /*
86  * is_dma_buf_file - Check if struct file* is associated with dma_buf
87  */
88 static inline int is_dma_buf_file(struct file *file)
89 {
90         return file->f_op == &dma_buf_fops;
91 }
92
93 #ifdef CONFIG_ARCH_ROCKCHIP
94 int dma_buf_is_dma_buf(struct file *file)
95 {
96         return is_dma_buf_file(file);
97 }
98 #endif
99
100 /**
101  * dma_buf_export_named - Creates a new dma_buf, and associates an anon file
102  * with this buffer, so it can be exported.
103  * Also connect the allocator specific data and ops to the buffer.
104  * Additionally, provide a name string for exporter; useful in debugging.
105  *
106  * @priv:       [in]    Attach private data of allocator to this buffer
107  * @ops:        [in]    Attach allocator-defined dma buf ops to the new buffer.
108  * @size:       [in]    Size of the buffer
109  * @flags:      [in]    mode flags for the file.
110  * @exp_name:   [in]    name of the exporting module - useful for debugging.
111  *
112  * Returns, on success, a newly created dma_buf object, which wraps the
113  * supplied private data and operations for dma_buf_ops. On either missing
114  * ops, or error in allocating struct dma_buf, will return negative error.
115  *
116  */
117 struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops,
118                                 size_t size, int flags, const char *exp_name)
119 {
120         struct dma_buf *dmabuf;
121         struct file *file;
122
123         if (WARN_ON(!priv || !ops
124                           || !ops->map_dma_buf
125                           || !ops->unmap_dma_buf
126                           || !ops->release
127                           || !ops->kmap_atomic
128                           || !ops->kmap
129                           || !ops->mmap)) {
130                 return ERR_PTR(-EINVAL);
131         }
132
133         dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
134         if (dmabuf == NULL)
135                 return ERR_PTR(-ENOMEM);
136
137         dmabuf->priv = priv;
138         dmabuf->ops = ops;
139         dmabuf->size = size;
140         dmabuf->exp_name = exp_name;
141
142         file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
143
144         dmabuf->file = file;
145
146         mutex_init(&dmabuf->lock);
147         INIT_LIST_HEAD(&dmabuf->attachments);
148
149         mutex_lock(&db_list.lock);
150         list_add(&dmabuf->list_node, &db_list.head);
151         mutex_unlock(&db_list.lock);
152
153         return dmabuf;
154 }
155 EXPORT_SYMBOL_GPL(dma_buf_export_named);
156
157
158 /**
159  * dma_buf_fd - returns a file descriptor for the given dma_buf
160  * @dmabuf:     [in]    pointer to dma_buf for which fd is required.
161  * @flags:      [in]    flags to give to fd
162  *
163  * On success, returns an associated 'fd'. Else, returns error.
164  */
165 int dma_buf_fd(struct dma_buf *dmabuf, int flags)
166 {
167         int fd;
168
169         if (!dmabuf || !dmabuf->file)
170                 return -EINVAL;
171
172         fd = get_unused_fd_flags(flags);
173         if (fd < 0)
174                 return fd;
175
176         fd_install(fd, dmabuf->file);
177
178         return fd;
179 }
180 EXPORT_SYMBOL_GPL(dma_buf_fd);
181
182 /**
183  * dma_buf_get - returns the dma_buf structure related to an fd
184  * @fd: [in]    fd associated with the dma_buf to be returned
185  *
186  * On success, returns the dma_buf structure associated with an fd; uses
187  * file's refcounting done by fget to increase refcount. returns ERR_PTR
188  * otherwise.
189  */
190 struct dma_buf *dma_buf_get(int fd)
191 {
192         struct file *file;
193
194         file = fget(fd);
195
196         if (!file)
197                 return ERR_PTR(-EBADF);
198
199         if (!is_dma_buf_file(file)) {
200                 fput(file);
201                 return ERR_PTR(-EINVAL);
202         }
203
204         return file->private_data;
205 }
206 EXPORT_SYMBOL_GPL(dma_buf_get);
207
208 /**
209  * dma_buf_put - decreases refcount of the buffer
210  * @dmabuf:     [in]    buffer to reduce refcount of
211  *
212  * Uses file's refcounting done implicitly by fput()
213  */
214 void dma_buf_put(struct dma_buf *dmabuf)
215 {
216         if (WARN_ON(!dmabuf || !dmabuf->file))
217                 return;
218
219         fput(dmabuf->file);
220 }
221 EXPORT_SYMBOL_GPL(dma_buf_put);
222
223 /**
224  * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
225  * calls attach() of dma_buf_ops to allow device-specific attach functionality
226  * @dmabuf:     [in]    buffer to attach device to.
227  * @dev:        [in]    device to be attached.
228  *
229  * Returns struct dma_buf_attachment * for this attachment; may return negative
230  * error codes.
231  *
232  */
233 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
234                                           struct device *dev)
235 {
236         struct dma_buf_attachment *attach;
237         int ret;
238
239         if (WARN_ON(!dmabuf || !dev))
240                 return ERR_PTR(-EINVAL);
241
242         attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
243         if (attach == NULL)
244                 return ERR_PTR(-ENOMEM);
245
246         attach->dev = dev;
247         attach->dmabuf = dmabuf;
248
249         mutex_lock(&dmabuf->lock);
250
251         if (dmabuf->ops->attach) {
252                 ret = dmabuf->ops->attach(dmabuf, dev, attach);
253                 if (ret)
254                         goto err_attach;
255         }
256         list_add(&attach->node, &dmabuf->attachments);
257
258         mutex_unlock(&dmabuf->lock);
259         return attach;
260
261 err_attach:
262         kfree(attach);
263         mutex_unlock(&dmabuf->lock);
264         return ERR_PTR(ret);
265 }
266 EXPORT_SYMBOL_GPL(dma_buf_attach);
267
268 /**
269  * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
270  * optionally calls detach() of dma_buf_ops for device-specific detach
271  * @dmabuf:     [in]    buffer to detach from.
272  * @attach:     [in]    attachment to be detached; is free'd after this call.
273  *
274  */
275 void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
276 {
277         if (WARN_ON(!dmabuf || !attach))
278                 return;
279
280         mutex_lock(&dmabuf->lock);
281         list_del(&attach->node);
282         if (dmabuf->ops->detach)
283                 dmabuf->ops->detach(dmabuf, attach);
284
285         mutex_unlock(&dmabuf->lock);
286         kfree(attach);
287 }
288 EXPORT_SYMBOL_GPL(dma_buf_detach);
289
290 /**
291  * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
292  * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
293  * dma_buf_ops.
294  * @attach:     [in]    attachment whose scatterlist is to be returned
295  * @direction:  [in]    direction of DMA transfer
296  *
297  * Returns sg_table containing the scatterlist to be returned; may return NULL
298  * or ERR_PTR.
299  *
300  */
301 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
302                                         enum dma_data_direction direction)
303 {
304         struct sg_table *sg_table = ERR_PTR(-EINVAL);
305
306         might_sleep();
307
308         if (WARN_ON(!attach || !attach->dmabuf))
309                 return ERR_PTR(-EINVAL);
310
311         sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
312
313         return sg_table;
314 }
315 EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
316
317 /**
318  * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
319  * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
320  * dma_buf_ops.
321  * @attach:     [in]    attachment to unmap buffer from
322  * @sg_table:   [in]    scatterlist info of the buffer to unmap
323  * @direction:  [in]    direction of DMA transfer
324  *
325  */
326 void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
327                                 struct sg_table *sg_table,
328                                 enum dma_data_direction direction)
329 {
330         might_sleep();
331
332         if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
333                 return;
334
335         attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
336                                                 direction);
337 }
338 EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
339
340
341 /**
342  * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
343  * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
344  * preparations. Coherency is only guaranteed in the specified range for the
345  * specified access direction.
346  * @dmabuf:     [in]    buffer to prepare cpu access for.
347  * @start:      [in]    start of range for cpu access.
348  * @len:        [in]    length of range for cpu access.
349  * @direction:  [in]    length of range for cpu access.
350  *
351  * Can return negative error values, returns 0 on success.
352  */
353 int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
354                              enum dma_data_direction direction)
355 {
356         int ret = 0;
357
358         if (WARN_ON(!dmabuf))
359                 return -EINVAL;
360
361         if (dmabuf->ops->begin_cpu_access)
362                 ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
363
364         return ret;
365 }
366 EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
367
368 /**
369  * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
370  * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
371  * actions. Coherency is only guaranteed in the specified range for the
372  * specified access direction.
373  * @dmabuf:     [in]    buffer to complete cpu access for.
374  * @start:      [in]    start of range for cpu access.
375  * @len:        [in]    length of range for cpu access.
376  * @direction:  [in]    length of range for cpu access.
377  *
378  * This call must always succeed.
379  */
380 void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
381                             enum dma_data_direction direction)
382 {
383         WARN_ON(!dmabuf);
384
385         if (dmabuf->ops->end_cpu_access)
386                 dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
387 }
388 EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
389
390 /**
391  * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
392  * space. The same restrictions as for kmap_atomic and friends apply.
393  * @dmabuf:     [in]    buffer to map page from.
394  * @page_num:   [in]    page in PAGE_SIZE units to map.
395  *
396  * This call must always succeed, any necessary preparations that might fail
397  * need to be done in begin_cpu_access.
398  */
399 void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
400 {
401         WARN_ON(!dmabuf);
402
403         return dmabuf->ops->kmap_atomic(dmabuf, page_num);
404 }
405 EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
406
407 /**
408  * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
409  * @dmabuf:     [in]    buffer to unmap page from.
410  * @page_num:   [in]    page in PAGE_SIZE units to unmap.
411  * @vaddr:      [in]    kernel space pointer obtained from dma_buf_kmap_atomic.
412  *
413  * This call must always succeed.
414  */
415 void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
416                            void *vaddr)
417 {
418         WARN_ON(!dmabuf);
419
420         if (dmabuf->ops->kunmap_atomic)
421                 dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
422 }
423 EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
424
425 /**
426  * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
427  * same restrictions as for kmap and friends apply.
428  * @dmabuf:     [in]    buffer to map page from.
429  * @page_num:   [in]    page in PAGE_SIZE units to map.
430  *
431  * This call must always succeed, any necessary preparations that might fail
432  * need to be done in begin_cpu_access.
433  */
434 void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
435 {
436         WARN_ON(!dmabuf);
437
438         return dmabuf->ops->kmap(dmabuf, page_num);
439 }
440 EXPORT_SYMBOL_GPL(dma_buf_kmap);
441
442 /**
443  * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
444  * @dmabuf:     [in]    buffer to unmap page from.
445  * @page_num:   [in]    page in PAGE_SIZE units to unmap.
446  * @vaddr:      [in]    kernel space pointer obtained from dma_buf_kmap.
447  *
448  * This call must always succeed.
449  */
450 void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
451                     void *vaddr)
452 {
453         WARN_ON(!dmabuf);
454
455         if (dmabuf->ops->kunmap)
456                 dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
457 }
458 EXPORT_SYMBOL_GPL(dma_buf_kunmap);
459
460
461 /**
462  * dma_buf_mmap - Setup up a userspace mmap with the given vma
463  * @dmabuf:     [in]    buffer that should back the vma
464  * @vma:        [in]    vma for the mmap
465  * @pgoff:      [in]    offset in pages where this mmap should start within the
466  *                      dma-buf buffer.
467  *
468  * This function adjusts the passed in vma so that it points at the file of the
469  * dma_buf operation. It alsog adjusts the starting pgoff and does bounds
470  * checking on the size of the vma. Then it calls the exporters mmap function to
471  * set up the mapping.
472  *
473  * Can return negative error values, returns 0 on success.
474  */
475 int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
476                  unsigned long pgoff)
477 {
478         struct file *oldfile;
479         int ret;
480
481         if (WARN_ON(!dmabuf || !vma))
482                 return -EINVAL;
483
484         /* check for offset overflow */
485         if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
486                 return -EOVERFLOW;
487
488         /* check for overflowing the buffer's size */
489         if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
490             dmabuf->size >> PAGE_SHIFT)
491                 return -EINVAL;
492
493         /* readjust the vma */
494         get_file(dmabuf->file);
495         oldfile = vma->vm_file;
496         vma->vm_file = dmabuf->file;
497         vma->vm_pgoff = pgoff;
498
499         ret = dmabuf->ops->mmap(dmabuf, vma);
500         if (ret) {
501                 /* restore old parameters on failure */
502                 vma->vm_file = oldfile;
503                 fput(dmabuf->file);
504         } else {
505                 if (oldfile)
506                         fput(oldfile);
507         }
508         return ret;
509
510 }
511 EXPORT_SYMBOL_GPL(dma_buf_mmap);
512
513 /**
514  * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
515  * address space. Same restrictions as for vmap and friends apply.
516  * @dmabuf:     [in]    buffer to vmap
517  *
518  * This call may fail due to lack of virtual mapping address space.
519  * These calls are optional in drivers. The intended use for them
520  * is for mapping objects linear in kernel space for high use objects.
521  * Please attempt to use kmap/kunmap before thinking about these interfaces.
522  */
523 void *dma_buf_vmap(struct dma_buf *dmabuf)
524 {
525         void *ptr;
526
527         if (WARN_ON(!dmabuf))
528                 return NULL;
529
530         if (!dmabuf->ops->vmap)
531                 return NULL;
532
533         mutex_lock(&dmabuf->lock);
534         if (dmabuf->vmapping_counter) {
535                 dmabuf->vmapping_counter++;
536                 BUG_ON(!dmabuf->vmap_ptr);
537                 ptr = dmabuf->vmap_ptr;
538                 goto out_unlock;
539         }
540
541         BUG_ON(dmabuf->vmap_ptr);
542
543         ptr = dmabuf->ops->vmap(dmabuf);
544         if (IS_ERR_OR_NULL(ptr))
545                 goto out_unlock;
546
547         dmabuf->vmap_ptr = ptr;
548         dmabuf->vmapping_counter = 1;
549
550 out_unlock:
551         mutex_unlock(&dmabuf->lock);
552         return ptr;
553 }
554 EXPORT_SYMBOL_GPL(dma_buf_vmap);
555
556 /**
557  * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
558  * @dmabuf:     [in]    buffer to vunmap
559  * @vaddr:      [in]    vmap to vunmap
560  */
561 void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
562 {
563         if (WARN_ON(!dmabuf))
564                 return;
565
566         BUG_ON(!dmabuf->vmap_ptr);
567         BUG_ON(dmabuf->vmapping_counter == 0);
568         BUG_ON(dmabuf->vmap_ptr != vaddr);
569
570         mutex_lock(&dmabuf->lock);
571         if (--dmabuf->vmapping_counter == 0) {
572                 if (dmabuf->ops->vunmap)
573                         dmabuf->ops->vunmap(dmabuf, vaddr);
574                 dmabuf->vmap_ptr = NULL;
575         }
576         mutex_unlock(&dmabuf->lock);
577 }
578 EXPORT_SYMBOL_GPL(dma_buf_vunmap);
579
580 #ifdef CONFIG_DEBUG_FS
581 static int dma_buf_describe(struct seq_file *s)
582 {
583         int ret;
584         struct dma_buf *buf_obj;
585         struct dma_buf_attachment *attach_obj;
586         int count = 0, attach_count;
587         size_t size = 0;
588
589         ret = mutex_lock_interruptible(&db_list.lock);
590
591         if (ret)
592                 return ret;
593
594         seq_printf(s, "\nDma-buf Objects:\n");
595         seq_printf(s, "\texp_name\tsize\tflags\tmode\tcount\n");
596
597         list_for_each_entry(buf_obj, &db_list.head, list_node) {
598                 ret = mutex_lock_interruptible(&buf_obj->lock);
599
600                 if (ret) {
601                         seq_printf(s,
602                                   "\tERROR locking buffer object: skipping\n");
603                         continue;
604                 }
605
606                 seq_printf(s, "\t");
607
608                 seq_printf(s, "\t%s\t%08zu\t%08x\t%08x\t%08ld\n",
609                                 buf_obj->exp_name, buf_obj->size,
610                                 buf_obj->file->f_flags, buf_obj->file->f_mode,
611                                 (long)(buf_obj->file->f_count.counter));
612
613                 seq_printf(s, "\t\tAttached Devices:\n");
614                 attach_count = 0;
615
616                 list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
617                         seq_printf(s, "\t\t");
618
619                         seq_printf(s, "%s\n", attach_obj->dev->init_name);
620                         attach_count++;
621                 }
622
623                 seq_printf(s, "\n\t\tTotal %d devices attached\n",
624                                 attach_count);
625
626                 count++;
627                 size += buf_obj->size;
628                 mutex_unlock(&buf_obj->lock);
629         }
630
631         seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
632
633         mutex_unlock(&db_list.lock);
634         return 0;
635 }
636
637 static int dma_buf_show(struct seq_file *s, void *unused)
638 {
639         void (*func)(struct seq_file *) = s->private;
640         func(s);
641         return 0;
642 }
643
644 static int dma_buf_debug_open(struct inode *inode, struct file *file)
645 {
646         return single_open(file, dma_buf_show, inode->i_private);
647 }
648
649 static const struct file_operations dma_buf_debug_fops = {
650         .open           = dma_buf_debug_open,
651         .read           = seq_read,
652         .llseek         = seq_lseek,
653         .release        = single_release,
654 };
655
656 static struct dentry *dma_buf_debugfs_dir;
657
658 static int dma_buf_init_debugfs(void)
659 {
660         int err = 0;
661         dma_buf_debugfs_dir = debugfs_create_dir("dma_buf", NULL);
662         if (IS_ERR(dma_buf_debugfs_dir)) {
663                 err = PTR_ERR(dma_buf_debugfs_dir);
664                 dma_buf_debugfs_dir = NULL;
665                 return err;
666         }
667
668         err = dma_buf_debugfs_create_file("bufinfo", dma_buf_describe);
669
670         if (err)
671                 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
672
673         return err;
674 }
675
676 static void dma_buf_uninit_debugfs(void)
677 {
678         if (dma_buf_debugfs_dir)
679                 debugfs_remove_recursive(dma_buf_debugfs_dir);
680 }
681
682 int dma_buf_debugfs_create_file(const char *name,
683                                 int (*write)(struct seq_file *))
684 {
685         struct dentry *d;
686
687         d = debugfs_create_file(name, S_IRUGO, dma_buf_debugfs_dir,
688                         write, &dma_buf_debug_fops);
689
690         if (IS_ERR(d))
691                 return PTR_ERR(d);
692
693         return 0;
694 }
695 #else
696 static inline int dma_buf_init_debugfs(void)
697 {
698         return 0;
699 }
700 static inline void dma_buf_uninit_debugfs(void)
701 {
702 }
703 #endif
704
705 static int __init dma_buf_init(void)
706 {
707         mutex_init(&db_list.lock);
708         INIT_LIST_HEAD(&db_list.head);
709         dma_buf_init_debugfs();
710         return 0;
711 }
712 subsys_initcall(dma_buf_init);
713
714 static void __exit dma_buf_deinit(void)
715 {
716         dma_buf_uninit_debugfs();
717 }
718 __exitcall(dma_buf_deinit);