3 * Copyright (C) 2011 Google, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/file.h>
19 #include <linux/uaccess.h>
23 #include "compat_ion.h"
26 struct ion_fd_data fd;
27 struct ion_allocation_data allocation;
28 struct ion_handle_data handle;
29 struct ion_custom_data custom;
30 struct ion_heap_query query;
33 static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg)
38 case ION_IOC_HEAP_QUERY:
39 ret = arg->query.reserved0 != 0;
40 ret |= arg->query.reserved1 != 0;
41 ret |= arg->query.reserved2 != 0;
47 return ret ? -EINVAL : 0;
50 /* fix up the cases where the ioctl direction bits are incorrect */
51 static unsigned int ion_ioctl_dir(unsigned int cmd)
62 long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
64 struct ion_client *client = filp->private_data;
65 struct ion_device *dev = client->dev;
66 struct ion_handle *cleanup_handle = NULL;
69 union ion_ioctl_arg data;
71 dir = ion_ioctl_dir(cmd);
73 if (_IOC_SIZE(cmd) > sizeof(data))
77 * The copy_from_user is unconditional here for both read and write
78 * to do the validate. If there is no write for the ioctl, the
81 if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
84 ret = validate_ioctl_arg(cmd, &data);
85 if (WARN_ON_ONCE(ret))
88 if (!(dir & _IOC_WRITE))
89 memset(&data, 0, sizeof(data));
94 struct ion_handle *handle;
96 handle = ion_alloc(client, data.allocation.len,
97 data.allocation.heap_id_mask,
98 data.allocation.flags);
100 return PTR_ERR(handle);
102 data.allocation.handle = handle->id;
104 cleanup_handle = handle;
109 struct ion_handle *handle;
111 mutex_lock(&client->lock);
112 handle = ion_handle_get_by_id_nolock(client,
114 if (IS_ERR(handle)) {
115 mutex_unlock(&client->lock);
116 return PTR_ERR(handle);
118 ion_free_nolock(client, handle);
119 ion_handle_put_nolock(handle);
120 mutex_unlock(&client->lock);
126 struct ion_handle *handle;
128 handle = ion_handle_get_by_id(client, data.handle.handle);
130 return PTR_ERR(handle);
131 data.fd.fd = ion_share_dma_buf_fd(client, handle);
132 ion_handle_put(handle);
139 struct ion_handle *handle;
141 handle = ion_import_dma_buf_fd(client, data.fd.fd);
143 ret = PTR_ERR(handle);
145 data.handle.handle = handle->id;
150 if (!dev->custom_ioctl)
152 ret = dev->custom_ioctl(client, data.custom.cmd,
156 case ION_IOC_HEAP_QUERY:
157 ret = ion_query_heaps(client, &data.query);
163 if (dir & _IOC_READ) {
164 if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd))) {
166 ion_free(client, cleanup_handle);