CHROMIUM: dma-buf/kds: allow KDS to be compiled out if dma-buf is enabled
[cascardo/linux.git] / drivers / base / dma-buf.c
index 09e6878..4defa66 100644 (file)
 #include <linux/dma-buf.h>
 #include <linux/anon_inodes.h>
 #include <linux/export.h>
+#ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
 #include <linux/poll.h>
 #include <linux/sched.h>
+#endif
 
 static inline int is_dma_buf_file(struct file *);
 
@@ -42,8 +44,10 @@ static int dma_buf_release(struct inode *inode, struct file *file)
        dmabuf = file->private_data;
 
        dmabuf->ops->release(dmabuf);
+#ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
        kds_callback_term(&dmabuf->kds_cb);
        kds_resource_term(&dmabuf->kds);
+#endif
        kfree(dmabuf);
        return 0;
 }
@@ -65,7 +69,7 @@ static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
        return dmabuf->ops->mmap(dmabuf, vma);
 }
 
-
+#ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
 static void dma_buf_kds_cb_fn(void *param1, void *param2)
 {
        struct kds_resource_set **rset_ptr = param1;
@@ -146,11 +150,14 @@ static unsigned int dma_buf_poll(struct file *file,
        }
        return ret;
 }
+#endif
 
 static const struct file_operations dma_buf_fops = {
        .release        = dma_buf_release,
        .mmap           = dma_buf_mmap_internal,
+#ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
        .poll           = dma_buf_poll,
+#endif
 };
 
 /*
@@ -207,10 +214,12 @@ struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
        mutex_init(&dmabuf->lock);
        INIT_LIST_HEAD(&dmabuf->attachments);
 
+#ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
        init_waitqueue_head(&dmabuf->wq_exclusive);
        init_waitqueue_head(&dmabuf->wq_shared);
        kds_resource_init(&dmabuf->kds);
        kds_callback_init(&dmabuf->kds_cb, 1, dma_buf_kds_cb_fn);
+#endif
 
        return dmabuf;
 }
@@ -536,6 +545,9 @@ EXPORT_SYMBOL_GPL(dma_buf_kunmap);
 int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
                 unsigned long pgoff)
 {
+       struct file *oldfile;
+       int ret;
+
        if (WARN_ON(!dmabuf || !vma))
                return -EINVAL;
 
@@ -549,15 +561,21 @@ int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
                return -EINVAL;
 
        /* readjust the vma */
-       if (vma->vm_file)
-               fput(vma->vm_file);
-
+       get_file(dmabuf->file);
+       oldfile = vma->vm_file;
        vma->vm_file = dmabuf->file;
-       get_file(vma->vm_file);
-
        vma->vm_pgoff = pgoff;
 
-       return dmabuf->ops->mmap(dmabuf, vma);
+       ret = dmabuf->ops->mmap(dmabuf, vma);
+       if (ret) {
+               /* restore old parameters on failure */
+               vma->vm_file = oldfile;
+               fput(dmabuf->file);
+       } else {
+               if (oldfile)
+                       fput(oldfile);
+       }
+       return ret;
 }
 EXPORT_SYMBOL_GPL(dma_buf_mmap);