GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / staging / android / ion / ion-ioctl.c
1 /*
2  *
3  * Copyright (C) 2011 Google, Inc.
4  *
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.
8  *
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.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/file.h>
18 #include <linux/fs.h>
19 #include <linux/uaccess.h>
20
21 #include "ion.h"
22
23 union ion_ioctl_arg {
24         struct ion_allocation_data allocation;
25         struct ion_heap_query query;
26 };
27
28 static int validate_ioctl_arg(unsigned int cmd, union ion_ioctl_arg *arg)
29 {
30         int ret = 0;
31
32         switch (cmd) {
33         case ION_IOC_HEAP_QUERY:
34                 ret = arg->query.reserved0 != 0;
35                 ret |= arg->query.reserved1 != 0;
36                 ret |= arg->query.reserved2 != 0;
37                 break;
38         default:
39                 break;
40         }
41
42         return ret ? -EINVAL : 0;
43 }
44
45 /* fix up the cases where the ioctl direction bits are incorrect */
46 static unsigned int ion_ioctl_dir(unsigned int cmd)
47 {
48         switch (cmd) {
49         default:
50                 return _IOC_DIR(cmd);
51         }
52 }
53
54 long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
55 {
56         int ret = 0;
57         unsigned int dir;
58         union ion_ioctl_arg data;
59
60         dir = ion_ioctl_dir(cmd);
61
62         if (_IOC_SIZE(cmd) > sizeof(data))
63                 return -EINVAL;
64
65         /*
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
68          * buffer is cleared
69          */
70         if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
71                 return -EFAULT;
72
73         ret = validate_ioctl_arg(cmd, &data);
74         if (ret) {
75                 pr_warn_once("%s: ioctl validate failed\n", __func__);
76                 return ret;
77         }
78
79         if (!(dir & _IOC_WRITE))
80                 memset(&data, 0, sizeof(data));
81
82         switch (cmd) {
83         case ION_IOC_ALLOC:
84         {
85                 int fd;
86
87                 fd = ion_alloc(data.allocation.len,
88                                data.allocation.heap_id_mask,
89                                data.allocation.flags);
90                 if (fd < 0)
91                         return fd;
92
93                 data.allocation.fd = fd;
94
95                 break;
96         }
97         case ION_IOC_HEAP_QUERY:
98                 ret = ion_query_heaps(&data.query);
99                 break;
100         default:
101                 return -ENOTTY;
102         }
103
104         if (dir & _IOC_READ) {
105                 if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd)))
106                         return -EFAULT;
107         }
108         return ret;
109 }