Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linux/SPARC PROM Configuration Driver |
| 3 | * Copyright (C) 1996 Thomas K. Dyas (tdyas@noc.rutgers.edu) |
| 4 | * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be) |
| 5 | * |
| 6 | * This character device driver allows user programs to access the |
| 7 | * PROM device tree. It is compatible with the SunOS /dev/openprom |
| 8 | * driver and the NetBSD /dev/openprom driver. The SunOS eeprom |
| 9 | * utility works without any modifications. |
| 10 | * |
| 11 | * The driver uses a minor number under the misc device major. The |
| 12 | * file read/write mode determines the type of access to the PROM. |
| 13 | * Interrupts are disabled whenever the driver calls into the PROM for |
| 14 | * sanity's sake. |
| 15 | */ |
| 16 | |
| 17 | /* This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation; either version 2 of the |
| 20 | * License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, but |
| 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | * General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 30 | */ |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/module.h> |
| 33 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/errno.h> |
| 35 | #include <linux/slab.h> |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/string.h> |
| 38 | #include <linux/miscdevice.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/fs.h> |
| 41 | #include <asm/oplib.h> |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 42 | #include <asm/prom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/uaccess.h> |
| 44 | #include <asm/openpromio.h> |
| 45 | #ifdef CONFIG_PCI |
| 46 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #endif |
| 48 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 49 | MODULE_AUTHOR("Thomas K. Dyas (tdyas@noc.rutgers.edu) and Eddie C. Dost (ecd@skynet.be)"); |
| 50 | MODULE_DESCRIPTION("OPENPROM Configuration Driver"); |
| 51 | MODULE_LICENSE("GPL"); |
| 52 | MODULE_VERSION("1.0"); |
Scott James Remnant | 1c339eb | 2009-03-13 14:30:08 -0700 | [diff] [blame] | 53 | MODULE_ALIAS_MISCDEV(SUN_OPENPROM_MINOR); |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* Private data kept by the driver for each descriptor. */ |
| 56 | typedef struct openprom_private_data |
| 57 | { |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 58 | struct device_node *current_node; /* Current node for SunOS ioctls. */ |
| 59 | struct device_node *lastnode; /* Last valid node used by BSD ioctls. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } DATA; |
| 61 | |
| 62 | /* ID of the PROM node containing all of the EEPROM options. */ |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 63 | static DEFINE_MUTEX(openprom_mutex); |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 64 | static struct device_node *options_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Copy an openpromio structure into kernel space from user space. |
| 68 | * This routine does error checking to make sure that all memory |
| 69 | * accesses are within bounds. A pointer to the allocated openpromio |
| 70 | * structure will be placed in "*opp_p". Return value is the length |
| 71 | * of the user supplied buffer. |
| 72 | */ |
| 73 | static int copyin(struct openpromio __user *info, struct openpromio **opp_p) |
| 74 | { |
| 75 | unsigned int bufsize; |
| 76 | |
| 77 | if (!info || !opp_p) |
| 78 | return -EFAULT; |
| 79 | |
| 80 | if (get_user(bufsize, &info->oprom_size)) |
| 81 | return -EFAULT; |
| 82 | |
| 83 | if (bufsize == 0) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | /* If the bufsize is too large, just limit it. |
| 87 | * Fix from Jason Rappleye. |
| 88 | */ |
| 89 | if (bufsize > OPROMMAXPARAM) |
| 90 | bufsize = OPROMMAXPARAM; |
| 91 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 92 | if (!(*opp_p = kzalloc(sizeof(int) + bufsize + 1, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | if (copy_from_user(&(*opp_p)->oprom_array, |
| 96 | &info->oprom_array, bufsize)) { |
| 97 | kfree(*opp_p); |
| 98 | return -EFAULT; |
| 99 | } |
| 100 | return bufsize; |
| 101 | } |
| 102 | |
| 103 | static int getstrings(struct openpromio __user *info, struct openpromio **opp_p) |
| 104 | { |
| 105 | int n, bufsize; |
| 106 | char c; |
| 107 | |
| 108 | if (!info || !opp_p) |
| 109 | return -EFAULT; |
| 110 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 111 | if (!(*opp_p = kzalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return -ENOMEM; |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | (*opp_p)->oprom_size = 0; |
| 115 | |
| 116 | n = bufsize = 0; |
| 117 | while ((n < 2) && (bufsize < OPROMMAXPARAM)) { |
| 118 | if (get_user(c, &info->oprom_array[bufsize])) { |
| 119 | kfree(*opp_p); |
| 120 | return -EFAULT; |
| 121 | } |
| 122 | if (c == '\0') |
| 123 | n++; |
| 124 | (*opp_p)->oprom_array[bufsize++] = c; |
| 125 | } |
| 126 | if (!n) { |
| 127 | kfree(*opp_p); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | return bufsize; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Copy an openpromio structure in kernel space back to user space. |
| 135 | */ |
| 136 | static int copyout(void __user *info, struct openpromio *opp, int len) |
| 137 | { |
| 138 | if (copy_to_user(info, opp, len)) |
| 139 | return -EFAULT; |
| 140 | return 0; |
| 141 | } |
| 142 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 143 | static int opromgetprop(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize) |
| 144 | { |
Stephen Rothwell | ccf0dec | 2007-03-29 00:49:54 -0700 | [diff] [blame] | 145 | const void *pval; |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 146 | int len; |
| 147 | |
David S. Miller | b9b64e6 | 2006-09-18 01:47:13 -0700 | [diff] [blame] | 148 | if (!dp || |
| 149 | !(pval = of_get_property(dp, op->oprom_array, &len)) || |
| 150 | len <= 0 || len > bufsize) |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 151 | return copyout(argp, op, sizeof(int)); |
| 152 | |
| 153 | memcpy(op->oprom_array, pval, len); |
| 154 | op->oprom_array[len] = '\0'; |
| 155 | op->oprom_size = len; |
| 156 | |
| 157 | return copyout(argp, op, sizeof(int) + bufsize); |
| 158 | } |
| 159 | |
| 160 | static int opromnxtprop(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize) |
| 161 | { |
| 162 | struct property *prop; |
| 163 | int len; |
| 164 | |
David S. Miller | b9b64e6 | 2006-09-18 01:47:13 -0700 | [diff] [blame] | 165 | if (!dp) |
| 166 | return copyout(argp, op, sizeof(int)); |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 167 | if (op->oprom_array[0] == '\0') { |
| 168 | prop = dp->properties; |
| 169 | if (!prop) |
| 170 | return copyout(argp, op, sizeof(int)); |
| 171 | len = strlen(prop->name); |
| 172 | } else { |
| 173 | prop = of_find_property(dp, op->oprom_array, NULL); |
| 174 | |
| 175 | if (!prop || |
| 176 | !prop->next || |
| 177 | (len = strlen(prop->next->name)) + 1 > bufsize) |
| 178 | return copyout(argp, op, sizeof(int)); |
| 179 | |
| 180 | prop = prop->next; |
| 181 | } |
| 182 | |
| 183 | memcpy(op->oprom_array, prop->name, len); |
| 184 | op->oprom_array[len] = '\0'; |
| 185 | op->oprom_size = ++len; |
| 186 | |
| 187 | return copyout(argp, op, sizeof(int) + bufsize); |
| 188 | } |
| 189 | |
| 190 | static int opromsetopt(struct device_node *dp, struct openpromio *op, int bufsize) |
| 191 | { |
| 192 | char *buf = op->oprom_array + strlen(op->oprom_array) + 1; |
| 193 | int len = op->oprom_array + bufsize - buf; |
| 194 | |
| 195 | return of_set_property(options_node, op->oprom_array, buf, len); |
| 196 | } |
| 197 | |
| 198 | static int opromnext(void __user *argp, unsigned int cmd, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data) |
| 199 | { |
| 200 | phandle ph; |
| 201 | |
| 202 | BUILD_BUG_ON(sizeof(phandle) != sizeof(int)); |
| 203 | |
| 204 | if (bufsize < sizeof(phandle)) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | ph = *((int *) op->oprom_array); |
| 208 | if (ph) { |
| 209 | dp = of_find_node_by_phandle(ph); |
| 210 | if (!dp) |
| 211 | return -EINVAL; |
| 212 | |
| 213 | switch (cmd) { |
| 214 | case OPROMNEXT: |
| 215 | dp = dp->sibling; |
| 216 | break; |
| 217 | |
| 218 | case OPROMCHILD: |
| 219 | dp = dp->child; |
| 220 | break; |
| 221 | |
| 222 | case OPROMSETCUR: |
| 223 | default: |
| 224 | break; |
Peter Senna Tschudin | da20116 | 2012-09-12 07:03:12 +0000 | [diff] [blame] | 225 | } |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 226 | } else { |
| 227 | /* Sibling of node zero is the root node. */ |
| 228 | if (cmd != OPROMNEXT) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | dp = of_find_node_by_path("/"); |
| 232 | } |
| 233 | |
| 234 | ph = 0; |
| 235 | if (dp) |
Grant Likely | 6016a36 | 2010-01-28 14:06:53 -0700 | [diff] [blame] | 236 | ph = dp->phandle; |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 237 | |
| 238 | data->current_node = dp; |
| 239 | *((int *) op->oprom_array) = ph; |
| 240 | op->oprom_size = sizeof(phandle); |
| 241 | |
| 242 | return copyout(argp, op, bufsize + sizeof(int)); |
| 243 | } |
| 244 | |
| 245 | static int oprompci2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data) |
| 246 | { |
| 247 | int err = -EINVAL; |
| 248 | |
| 249 | if (bufsize >= 2*sizeof(int)) { |
| 250 | #ifdef CONFIG_PCI |
| 251 | struct pci_dev *pdev; |
David S. Miller | fa449bd | 2007-04-25 16:01:51 -0700 | [diff] [blame] | 252 | struct device_node *dp; |
| 253 | |
Alan Cox | 7e9f334 | 2007-04-23 22:50:53 -0700 | [diff] [blame] | 254 | pdev = pci_get_bus_and_slot (((int *) op->oprom_array)[0], |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 255 | ((int *) op->oprom_array)[1]); |
| 256 | |
David S. Miller | fa449bd | 2007-04-25 16:01:51 -0700 | [diff] [blame] | 257 | dp = pci_device_to_OF_node(pdev); |
| 258 | data->current_node = dp; |
Grant Likely | 6016a36 | 2010-01-28 14:06:53 -0700 | [diff] [blame] | 259 | *((int *)op->oprom_array) = dp->phandle; |
David S. Miller | fa449bd | 2007-04-25 16:01:51 -0700 | [diff] [blame] | 260 | op->oprom_size = sizeof(int); |
| 261 | err = copyout(argp, op, bufsize + sizeof(int)); |
| 262 | |
Alan Cox | 7e9f334 | 2007-04-23 22:50:53 -0700 | [diff] [blame] | 263 | pci_dev_put(pdev); |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 264 | #endif |
| 265 | } |
| 266 | |
| 267 | return err; |
| 268 | } |
| 269 | |
| 270 | static int oprompath2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data) |
| 271 | { |
David S. Miller | b9b64e6 | 2006-09-18 01:47:13 -0700 | [diff] [blame] | 272 | phandle ph = 0; |
| 273 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 274 | dp = of_find_node_by_path(op->oprom_array); |
David S. Miller | b9b64e6 | 2006-09-18 01:47:13 -0700 | [diff] [blame] | 275 | if (dp) |
Grant Likely | 6016a36 | 2010-01-28 14:06:53 -0700 | [diff] [blame] | 276 | ph = dp->phandle; |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 277 | data->current_node = dp; |
David S. Miller | b9b64e6 | 2006-09-18 01:47:13 -0700 | [diff] [blame] | 278 | *((int *)op->oprom_array) = ph; |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 279 | op->oprom_size = sizeof(int); |
| 280 | |
| 281 | return copyout(argp, op, bufsize + sizeof(int)); |
| 282 | } |
| 283 | |
| 284 | static int opromgetbootargs(void __user *argp, struct openpromio *op, int bufsize) |
| 285 | { |
| 286 | char *buf = saved_command_line; |
| 287 | int len = strlen(buf); |
| 288 | |
| 289 | if (len > bufsize) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | strcpy(op->oprom_array, buf); |
| 293 | op->oprom_size = len; |
| 294 | |
| 295 | return copyout(argp, op, bufsize + sizeof(int)); |
| 296 | } |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | /* |
| 299 | * SunOS and Solaris /dev/openprom ioctl calls. |
| 300 | */ |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 301 | static long openprom_sunos_ioctl(struct file * file, |
| 302 | unsigned int cmd, unsigned long arg, |
| 303 | struct device_node *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 305 | DATA *data = file->private_data; |
David S. Miller | 32e5897 | 2009-06-16 03:46:54 -0700 | [diff] [blame] | 306 | struct openpromio *opp = NULL; |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 307 | int bufsize, error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | static int cnt; |
| 309 | void __user *argp = (void __user *)arg; |
| 310 | |
| 311 | if (cmd == OPROMSETOPT) |
| 312 | bufsize = getstrings(argp, &opp); |
| 313 | else |
| 314 | bufsize = copyin(argp, &opp); |
| 315 | |
| 316 | if (bufsize < 0) |
| 317 | return bufsize; |
| 318 | |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 319 | mutex_lock(&openprom_mutex); |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | switch (cmd) { |
| 322 | case OPROMGETOPT: |
| 323 | case OPROMGETPROP: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 324 | error = opromgetprop(argp, dp, opp, bufsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | break; |
| 326 | |
| 327 | case OPROMNXTOPT: |
| 328 | case OPROMNXTPROP: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 329 | error = opromnxtprop(argp, dp, opp, bufsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | case OPROMSETOPT: |
| 333 | case OPROMSETOPT2: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 334 | error = opromsetopt(dp, opp, bufsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | break; |
| 336 | |
| 337 | case OPROMNEXT: |
| 338 | case OPROMCHILD: |
| 339 | case OPROMSETCUR: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 340 | error = opromnext(argp, cmd, dp, opp, bufsize, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | break; |
| 342 | |
| 343 | case OPROMPCI2NODE: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 344 | error = oprompci2node(argp, dp, opp, bufsize, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | break; |
| 346 | |
| 347 | case OPROMPATH2NODE: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 348 | error = oprompath2node(argp, dp, opp, bufsize, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | break; |
| 350 | |
| 351 | case OPROMGETBOOTARGS: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 352 | error = opromgetbootargs(argp, opp, bufsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | break; |
| 354 | |
| 355 | case OPROMU2P: |
| 356 | case OPROMGETCONS: |
| 357 | case OPROMGETFBNAME: |
| 358 | if (cnt++ < 10) |
| 359 | printk(KERN_INFO "openprom_sunos_ioctl: unimplemented ioctl\n"); |
| 360 | error = -EINVAL; |
| 361 | break; |
| 362 | default: |
| 363 | if (cnt++ < 10) |
| 364 | printk(KERN_INFO "openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg); |
| 365 | error = -EINVAL; |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | kfree(opp); |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 370 | mutex_unlock(&openprom_mutex); |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return error; |
| 373 | } |
| 374 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 375 | static struct device_node *get_node(phandle n, DATA *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 377 | struct device_node *dp = of_find_node_by_phandle(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 379 | if (dp) |
| 380 | data->lastnode = dp; |
| 381 | |
| 382 | return dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* Copy in a whole string from userspace into kernelspace. */ |
| 386 | static int copyin_string(char __user *user, size_t len, char **ptr) |
| 387 | { |
| 388 | char *tmp; |
| 389 | |
| 390 | if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0) |
| 391 | return -EINVAL; |
| 392 | |
Al Viro | 16e5c1f | 2015-12-24 00:06:05 -0500 | [diff] [blame] | 393 | tmp = memdup_user_nul(user, len); |
| 394 | if (IS_ERR(tmp)) |
| 395 | return PTR_ERR(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | *ptr = tmp; |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * NetBSD /dev/openprom ioctl calls. |
| 404 | */ |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 405 | static int opiocget(void __user *argp, DATA *data) |
| 406 | { |
| 407 | struct opiocdesc op; |
| 408 | struct device_node *dp; |
| 409 | char *str; |
Stephen Rothwell | ccf0dec | 2007-03-29 00:49:54 -0700 | [diff] [blame] | 410 | const void *pval; |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 411 | int err, len; |
| 412 | |
| 413 | if (copy_from_user(&op, argp, sizeof(op))) |
| 414 | return -EFAULT; |
| 415 | |
| 416 | dp = get_node(op.op_nodeid, data); |
| 417 | |
| 418 | err = copyin_string(op.op_name, op.op_namelen, &str); |
| 419 | if (err) |
| 420 | return err; |
| 421 | |
| 422 | pval = of_get_property(dp, str, &len); |
| 423 | err = 0; |
| 424 | if (!pval || len > op.op_buflen) { |
| 425 | err = -EINVAL; |
| 426 | } else { |
| 427 | op.op_buflen = len; |
| 428 | if (copy_to_user(argp, &op, sizeof(op)) || |
| 429 | copy_to_user(op.op_buf, pval, len)) |
| 430 | err = -EFAULT; |
| 431 | } |
| 432 | kfree(str); |
| 433 | |
| 434 | return err; |
| 435 | } |
| 436 | |
| 437 | static int opiocnextprop(void __user *argp, DATA *data) |
| 438 | { |
| 439 | struct opiocdesc op; |
| 440 | struct device_node *dp; |
| 441 | struct property *prop; |
| 442 | char *str; |
| 443 | int err, len; |
| 444 | |
| 445 | if (copy_from_user(&op, argp, sizeof(op))) |
| 446 | return -EFAULT; |
| 447 | |
| 448 | dp = get_node(op.op_nodeid, data); |
| 449 | if (!dp) |
| 450 | return -EINVAL; |
| 451 | |
| 452 | err = copyin_string(op.op_name, op.op_namelen, &str); |
| 453 | if (err) |
| 454 | return err; |
| 455 | |
| 456 | if (str[0] == '\0') { |
| 457 | prop = dp->properties; |
| 458 | } else { |
| 459 | prop = of_find_property(dp, str, NULL); |
| 460 | if (prop) |
| 461 | prop = prop->next; |
| 462 | } |
| 463 | kfree(str); |
| 464 | |
| 465 | if (!prop) |
| 466 | len = 0; |
| 467 | else |
| 468 | len = prop->length; |
| 469 | |
| 470 | if (len > op.op_buflen) |
| 471 | len = op.op_buflen; |
| 472 | |
| 473 | if (copy_to_user(argp, &op, sizeof(op))) |
| 474 | return -EFAULT; |
| 475 | |
| 476 | if (len && |
| 477 | copy_to_user(op.op_buf, prop->value, len)) |
| 478 | return -EFAULT; |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static int opiocset(void __user *argp, DATA *data) |
| 484 | { |
| 485 | struct opiocdesc op; |
| 486 | struct device_node *dp; |
| 487 | char *str, *tmp; |
| 488 | int err; |
| 489 | |
| 490 | if (copy_from_user(&op, argp, sizeof(op))) |
| 491 | return -EFAULT; |
| 492 | |
| 493 | dp = get_node(op.op_nodeid, data); |
| 494 | if (!dp) |
| 495 | return -EINVAL; |
| 496 | |
| 497 | err = copyin_string(op.op_name, op.op_namelen, &str); |
| 498 | if (err) |
| 499 | return err; |
| 500 | |
| 501 | err = copyin_string(op.op_buf, op.op_buflen, &tmp); |
| 502 | if (err) { |
| 503 | kfree(str); |
| 504 | return err; |
| 505 | } |
| 506 | |
| 507 | err = of_set_property(dp, str, tmp, op.op_buflen); |
| 508 | |
| 509 | kfree(str); |
| 510 | kfree(tmp); |
| 511 | |
| 512 | return err; |
| 513 | } |
| 514 | |
| 515 | static int opiocgetnext(unsigned int cmd, void __user *argp) |
| 516 | { |
| 517 | struct device_node *dp; |
| 518 | phandle nd; |
| 519 | |
| 520 | BUILD_BUG_ON(sizeof(phandle) != sizeof(int)); |
| 521 | |
| 522 | if (copy_from_user(&nd, argp, sizeof(phandle))) |
| 523 | return -EFAULT; |
| 524 | |
| 525 | if (nd == 0) { |
| 526 | if (cmd != OPIOCGETNEXT) |
| 527 | return -EINVAL; |
| 528 | dp = of_find_node_by_path("/"); |
| 529 | } else { |
| 530 | dp = of_find_node_by_phandle(nd); |
| 531 | nd = 0; |
| 532 | if (dp) { |
| 533 | if (cmd == OPIOCGETNEXT) |
| 534 | dp = dp->sibling; |
| 535 | else |
| 536 | dp = dp->child; |
| 537 | } |
| 538 | } |
| 539 | if (dp) |
Grant Likely | 6016a36 | 2010-01-28 14:06:53 -0700 | [diff] [blame] | 540 | nd = dp->phandle; |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 541 | if (copy_to_user(argp, &nd, sizeof(phandle))) |
| 542 | return -EFAULT; |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 547 | static int openprom_bsd_ioctl(struct file * file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | unsigned int cmd, unsigned long arg) |
| 549 | { |
Joe Perches | 33cfe65 | 2010-07-12 21:16:04 -0700 | [diff] [blame] | 550 | DATA *data = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | void __user *argp = (void __user *)arg; |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 552 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 554 | mutex_lock(&openprom_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | switch (cmd) { |
| 556 | case OPIOCGET: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 557 | err = opiocget(argp, data); |
| 558 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | case OPIOCNEXTPROP: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 561 | err = opiocnextprop(argp, data); |
| 562 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | case OPIOCSET: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 565 | err = opiocset(argp, data); |
| 566 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
| 568 | case OPIOCGETOPTNODE: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 569 | BUILD_BUG_ON(sizeof(phandle) != sizeof(int)); |
| 570 | |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 571 | err = 0; |
Grant Likely | 6016a36 | 2010-01-28 14:06:53 -0700 | [diff] [blame] | 572 | if (copy_to_user(argp, &options_node->phandle, sizeof(phandle))) |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 573 | err = -EFAULT; |
| 574 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
| 576 | case OPIOCGETNEXT: |
| 577 | case OPIOCGETCHILD: |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 578 | err = opiocgetnext(cmd, argp); |
| 579 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | default: |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 582 | err = -EINVAL; |
| 583 | break; |
Peter Senna Tschudin | da20116 | 2012-09-12 07:03:12 +0000 | [diff] [blame] | 584 | } |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 585 | mutex_unlock(&openprom_mutex); |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 586 | |
| 587 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
| 591 | /* |
| 592 | * Handoff control to the correct ioctl handler. |
| 593 | */ |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 594 | static long openprom_ioctl(struct file * file, |
| 595 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
Joe Perches | 33cfe65 | 2010-07-12 21:16:04 -0700 | [diff] [blame] | 597 | DATA *data = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
| 599 | switch (cmd) { |
| 600 | case OPROMGETOPT: |
| 601 | case OPROMNXTOPT: |
| 602 | if ((file->f_mode & FMODE_READ) == 0) |
| 603 | return -EPERM; |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 604 | return openprom_sunos_ioctl(file, cmd, arg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | options_node); |
| 606 | |
| 607 | case OPROMSETOPT: |
| 608 | case OPROMSETOPT2: |
| 609 | if ((file->f_mode & FMODE_WRITE) == 0) |
| 610 | return -EPERM; |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 611 | return openprom_sunos_ioctl(file, cmd, arg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | options_node); |
| 613 | |
| 614 | case OPROMNEXT: |
| 615 | case OPROMCHILD: |
| 616 | case OPROMGETPROP: |
| 617 | case OPROMNXTPROP: |
| 618 | if ((file->f_mode & FMODE_READ) == 0) |
| 619 | return -EPERM; |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 620 | return openprom_sunos_ioctl(file, cmd, arg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | data->current_node); |
| 622 | |
| 623 | case OPROMU2P: |
| 624 | case OPROMGETCONS: |
| 625 | case OPROMGETFBNAME: |
| 626 | case OPROMGETBOOTARGS: |
| 627 | case OPROMSETCUR: |
| 628 | case OPROMPCI2NODE: |
| 629 | case OPROMPATH2NODE: |
| 630 | if ((file->f_mode & FMODE_READ) == 0) |
| 631 | return -EPERM; |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 632 | return openprom_sunos_ioctl(file, cmd, arg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | case OPIOCGET: |
| 635 | case OPIOCNEXTPROP: |
| 636 | case OPIOCGETOPTNODE: |
| 637 | case OPIOCGETNEXT: |
| 638 | case OPIOCGETCHILD: |
| 639 | if ((file->f_mode & FMODE_READ) == 0) |
| 640 | return -EBADF; |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 641 | return openprom_bsd_ioctl(file,cmd,arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
| 643 | case OPIOCSET: |
| 644 | if ((file->f_mode & FMODE_WRITE) == 0) |
| 645 | return -EBADF; |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 646 | return openprom_bsd_ioctl(file,cmd,arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | return -EINVAL; |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 650 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Christoph Hellwig | b31023f | 2005-11-07 14:12:47 -0800 | [diff] [blame] | 653 | static long openprom_compat_ioctl(struct file *file, unsigned int cmd, |
| 654 | unsigned long arg) |
| 655 | { |
| 656 | long rval = -ENOTTY; |
| 657 | |
| 658 | /* |
| 659 | * SunOS/Solaris only, the NetBSD one's have embedded pointers in |
| 660 | * the arg which we'd need to clean up... |
| 661 | */ |
| 662 | switch (cmd) { |
| 663 | case OPROMGETOPT: |
| 664 | case OPROMSETOPT: |
| 665 | case OPROMNXTOPT: |
| 666 | case OPROMSETOPT2: |
| 667 | case OPROMNEXT: |
| 668 | case OPROMCHILD: |
| 669 | case OPROMGETPROP: |
| 670 | case OPROMNXTPROP: |
| 671 | case OPROMU2P: |
| 672 | case OPROMGETCONS: |
| 673 | case OPROMGETFBNAME: |
| 674 | case OPROMGETBOOTARGS: |
| 675 | case OPROMSETCUR: |
| 676 | case OPROMPCI2NODE: |
| 677 | case OPROMPATH2NODE: |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 678 | rval = openprom_ioctl(file, cmd, arg); |
Christoph Hellwig | b31023f | 2005-11-07 14:12:47 -0800 | [diff] [blame] | 679 | break; |
| 680 | } |
David S. Miller | d5a858b | 2005-11-08 10:00:13 -0800 | [diff] [blame] | 681 | |
| 682 | return rval; |
Christoph Hellwig | b31023f | 2005-11-07 14:12:47 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | static int openprom_open(struct inode * inode, struct file * file) |
| 686 | { |
| 687 | DATA *data; |
| 688 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 689 | data = kmalloc(sizeof(DATA), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | if (!data) |
| 691 | return -ENOMEM; |
| 692 | |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 693 | mutex_lock(&openprom_mutex); |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 694 | data->current_node = of_find_node_by_path("/"); |
| 695 | data->lastnode = data->current_node; |
| 696 | file->private_data = (void *) data; |
Arnd Bergmann | a3108ca | 2010-07-20 23:36:39 -0700 | [diff] [blame] | 697 | mutex_unlock(&openprom_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static int openprom_release(struct inode * inode, struct file * file) |
| 703 | { |
| 704 | kfree(file->private_data); |
| 705 | return 0; |
| 706 | } |
| 707 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 708 | static const struct file_operations openprom_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | .owner = THIS_MODULE, |
| 710 | .llseek = no_llseek, |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 711 | .unlocked_ioctl = openprom_ioctl, |
David S. Miller | d5a858b | 2005-11-08 10:00:13 -0800 | [diff] [blame] | 712 | .compat_ioctl = openprom_compat_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | .open = openprom_open, |
| 714 | .release = openprom_release, |
| 715 | }; |
| 716 | |
| 717 | static struct miscdevice openprom_dev = { |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 718 | .minor = SUN_OPENPROM_MINOR, |
| 719 | .name = "openprom", |
| 720 | .fops = &openprom_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | }; |
| 722 | |
| 723 | static int __init openprom_init(void) |
| 724 | { |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 725 | struct device_node *dp; |
| 726 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 728 | err = misc_register(&openprom_dev); |
| 729 | if (err) |
| 730 | return err; |
| 731 | |
| 732 | dp = of_find_node_by_path("/"); |
| 733 | dp = dp->child; |
| 734 | while (dp) { |
| 735 | if (!strcmp(dp->name, "options")) |
| 736 | break; |
| 737 | dp = dp->sibling; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 739 | options_node = dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
David S. Miller | 8e48aec | 2006-06-25 23:19:30 -0700 | [diff] [blame] | 741 | if (!options_node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | misc_deregister(&openprom_dev); |
| 743 | return -EIO; |
| 744 | } |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static void __exit openprom_cleanup(void) |
| 750 | { |
| 751 | misc_deregister(&openprom_dev); |
| 752 | } |
| 753 | |
| 754 | module_init(openprom_init); |
| 755 | module_exit(openprom_cleanup); |