dma-buf: pass flags into dma_buf_fd.
[linux-2.6.git] / drivers / base / dma-buf.c
blob3c8c0232ce431285c5737fa1b1e521c2a76dfc51
1 /*
2 * Framework for buffer objects that can be shared across devices/subsystems.
4 * Copyright(C) 2011 Linaro Limited. All rights reserved.
5 * Author: Sumit Semwal <sumit.semwal@ti.com>
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.
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.
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.
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/>.
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>
31 static inline int is_dma_buf_file(struct file *);
33 static int dma_buf_release(struct inode *inode, struct file *file)
35 struct dma_buf *dmabuf;
37 if (!is_dma_buf_file(file))
38 return -EINVAL;
40 dmabuf = file->private_data;
42 dmabuf->ops->release(dmabuf);
43 kfree(dmabuf);
44 return 0;
47 static const struct file_operations dma_buf_fops = {
48 .release = dma_buf_release,
52 * is_dma_buf_file - Check if struct file* is associated with dma_buf
54 static inline int is_dma_buf_file(struct file *file)
56 return file->f_op == &dma_buf_fops;
59 /**
60 * dma_buf_export - Creates a new dma_buf, and associates an anon file
61 * with this buffer, so it can be exported.
62 * Also connect the allocator specific data and ops to the buffer.
64 * @priv: [in] Attach private data of allocator to this buffer
65 * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
66 * @size: [in] Size of the buffer
67 * @flags: [in] mode flags for the file.
69 * Returns, on success, a newly created dma_buf object, which wraps the
70 * supplied private data and operations for dma_buf_ops. On either missing
71 * ops, or error in allocating struct dma_buf, will return negative error.
74 struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
75 size_t size, int flags)
77 struct dma_buf *dmabuf;
78 struct file *file;
80 if (WARN_ON(!priv || !ops
81 || !ops->map_dma_buf
82 || !ops->unmap_dma_buf
83 || !ops->release)) {
84 return ERR_PTR(-EINVAL);
87 dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
88 if (dmabuf == NULL)
89 return ERR_PTR(-ENOMEM);
91 dmabuf->priv = priv;
92 dmabuf->ops = ops;
93 dmabuf->size = size;
95 file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
97 dmabuf->file = file;
99 mutex_init(&dmabuf->lock);
100 INIT_LIST_HEAD(&dmabuf->attachments);
102 return dmabuf;
104 EXPORT_SYMBOL_GPL(dma_buf_export);
108 * dma_buf_fd - returns a file descriptor for the given dma_buf
109 * @dmabuf: [in] pointer to dma_buf for which fd is required.
110 * @flags: [in] flags to give to fd
112 * On success, returns an associated 'fd'. Else, returns error.
114 int dma_buf_fd(struct dma_buf *dmabuf, int flags)
116 int error, fd;
118 if (!dmabuf || !dmabuf->file)
119 return -EINVAL;
121 error = get_unused_fd_flags(flags);
122 if (error < 0)
123 return error;
124 fd = error;
126 fd_install(fd, dmabuf->file);
128 return fd;
130 EXPORT_SYMBOL_GPL(dma_buf_fd);
133 * dma_buf_get - returns the dma_buf structure related to an fd
134 * @fd: [in] fd associated with the dma_buf to be returned
136 * On success, returns the dma_buf structure associated with an fd; uses
137 * file's refcounting done by fget to increase refcount. returns ERR_PTR
138 * otherwise.
140 struct dma_buf *dma_buf_get(int fd)
142 struct file *file;
144 file = fget(fd);
146 if (!file)
147 return ERR_PTR(-EBADF);
149 if (!is_dma_buf_file(file)) {
150 fput(file);
151 return ERR_PTR(-EINVAL);
154 return file->private_data;
156 EXPORT_SYMBOL_GPL(dma_buf_get);
159 * dma_buf_put - decreases refcount of the buffer
160 * @dmabuf: [in] buffer to reduce refcount of
162 * Uses file's refcounting done implicitly by fput()
164 void dma_buf_put(struct dma_buf *dmabuf)
166 if (WARN_ON(!dmabuf || !dmabuf->file))
167 return;
169 fput(dmabuf->file);
171 EXPORT_SYMBOL_GPL(dma_buf_put);
174 * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
175 * calls attach() of dma_buf_ops to allow device-specific attach functionality
176 * @dmabuf: [in] buffer to attach device to.
177 * @dev: [in] device to be attached.
179 * Returns struct dma_buf_attachment * for this attachment; may return negative
180 * error codes.
183 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
184 struct device *dev)
186 struct dma_buf_attachment *attach;
187 int ret;
189 if (WARN_ON(!dmabuf || !dev))
190 return ERR_PTR(-EINVAL);
192 attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
193 if (attach == NULL)
194 return ERR_PTR(-ENOMEM);
196 attach->dev = dev;
197 attach->dmabuf = dmabuf;
199 mutex_lock(&dmabuf->lock);
201 if (dmabuf->ops->attach) {
202 ret = dmabuf->ops->attach(dmabuf, dev, attach);
203 if (ret)
204 goto err_attach;
206 list_add(&attach->node, &dmabuf->attachments);
208 mutex_unlock(&dmabuf->lock);
209 return attach;
211 err_attach:
212 kfree(attach);
213 mutex_unlock(&dmabuf->lock);
214 return ERR_PTR(ret);
216 EXPORT_SYMBOL_GPL(dma_buf_attach);
219 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
220 * optionally calls detach() of dma_buf_ops for device-specific detach
221 * @dmabuf: [in] buffer to detach from.
222 * @attach: [in] attachment to be detached; is free'd after this call.
225 void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
227 if (WARN_ON(!dmabuf || !attach))
228 return;
230 mutex_lock(&dmabuf->lock);
231 list_del(&attach->node);
232 if (dmabuf->ops->detach)
233 dmabuf->ops->detach(dmabuf, attach);
235 mutex_unlock(&dmabuf->lock);
236 kfree(attach);
238 EXPORT_SYMBOL_GPL(dma_buf_detach);
241 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
242 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
243 * dma_buf_ops.
244 * @attach: [in] attachment whose scatterlist is to be returned
245 * @direction: [in] direction of DMA transfer
247 * Returns sg_table containing the scatterlist to be returned; may return NULL
248 * or ERR_PTR.
251 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
252 enum dma_data_direction direction)
254 struct sg_table *sg_table = ERR_PTR(-EINVAL);
256 might_sleep();
258 if (WARN_ON(!attach || !attach->dmabuf))
259 return ERR_PTR(-EINVAL);
261 mutex_lock(&attach->dmabuf->lock);
262 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
263 mutex_unlock(&attach->dmabuf->lock);
265 return sg_table;
267 EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
270 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
271 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
272 * dma_buf_ops.
273 * @attach: [in] attachment to unmap buffer from
274 * @sg_table: [in] scatterlist info of the buffer to unmap
275 * @direction: [in] direction of DMA transfer
278 void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
279 struct sg_table *sg_table,
280 enum dma_data_direction direction)
282 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
283 return;
285 mutex_lock(&attach->dmabuf->lock);
286 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
287 direction);
288 mutex_unlock(&attach->dmabuf->lock);
291 EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);