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>
24 struct ion_allocation_data allocation;
25 struct ion_heap_query query;
28 static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg)
33 case ION_IOC_HEAP_QUERY:
34 ret = arg->query.reserved0 != 0;
35 ret |= arg->query.reserved1 != 0;
36 ret |= arg->query.reserved2 != 0;
42 return ret ? -EINVAL : 0;
45 /* fix up the cases where the ioctl direction bits are incorrect */
46 static unsigned int ion_ioctl_dir(unsigned int cmd)
54 long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
58 union ion_ioctl_arg data;
60 dir = ion_ioctl_dir(cmd);
62 if (_IOC_SIZE(cmd) > sizeof(data))
66 * The copy_from_user is unconditional here for both read and write
67 * to do the validate. If there is no write for the ioctl, the
70 if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
73 ret = validate_ioctl_arg(cmd, &data);
75 pr_warn_once("%s: ioctl validate failed\n", __func__);
79 if (!(dir & _IOC_WRITE))
80 memset(&data, 0, sizeof(data));
87 fd = ion_alloc(data.allocation.len,
88 data.allocation.heap_id_mask,
89 data.allocation.flags);
93 data.allocation.fd = fd;
97 case ION_IOC_HEAP_QUERY:
98 ret = ion_query_heaps(&data.query);
104 if (dir & _IOC_READ) {
105 if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd)))