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