blob: 050879a2ddef28c7c0156359e3942cf483bdffaa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
33#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/errno.h>
35#include <linux/slab.h>
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070036#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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. Miller8e48aec2006-06-25 23:19:30 -070042#include <asm/prom.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080043#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/openpromio.h>
45#ifdef CONFIG_PCI
46#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#endif
48
David S. Miller8e48aec2006-06-25 23:19:30 -070049MODULE_AUTHOR("Thomas K. Dyas (tdyas@noc.rutgers.edu) and Eddie C. Dost (ecd@skynet.be)");
50MODULE_DESCRIPTION("OPENPROM Configuration Driver");
51MODULE_LICENSE("GPL");
52MODULE_VERSION("1.0");
Scott James Remnant1c339eb2009-03-13 14:30:08 -070053MODULE_ALIAS_MISCDEV(SUN_OPENPROM_MINOR);
David S. Miller8e48aec2006-06-25 23:19:30 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* Private data kept by the driver for each descriptor. */
56typedef struct openprom_private_data
57{
David S. Miller8e48aec2006-06-25 23:19:30 -070058 struct device_node *current_node; /* Current node for SunOS ioctls. */
59 struct device_node *lastnode; /* Last valid node used by BSD ioctls. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060} DATA;
61
62/* ID of the PROM node containing all of the EEPROM options. */
Arnd Bergmanna3108ca2010-07-20 23:36:39 -070063static DEFINE_MUTEX(openprom_mutex);
David S. Miller8e48aec2006-06-25 23:19:30 -070064static struct device_node *options_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
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 */
73static 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. Miller8e48aec2006-06-25 23:19:30 -070092 if (!(*opp_p = kzalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
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
103static 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. Miller8e48aec2006-06-25 23:19:30 -0700111 if (!(*opp_p = kzalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -ENOMEM;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 (*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 */
136static 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. Miller8e48aec2006-06-25 23:19:30 -0700143static int opromgetprop(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize)
144{
Stephen Rothwellccf0dec2007-03-29 00:49:54 -0700145 const void *pval;
David S. Miller8e48aec2006-06-25 23:19:30 -0700146 int len;
147
David S. Millerb9b64e62006-09-18 01:47:13 -0700148 if (!dp ||
149 !(pval = of_get_property(dp, op->oprom_array, &len)) ||
150 len <= 0 || len > bufsize)
David S. Miller8e48aec2006-06-25 23:19:30 -0700151 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
160static 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. Millerb9b64e62006-09-18 01:47:13 -0700165 if (!dp)
166 return copyout(argp, op, sizeof(int));
David S. Miller8e48aec2006-06-25 23:19:30 -0700167 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
190static 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
198static 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 Tschudinda201162012-09-12 07:03:12 +0000225 }
David S. Miller8e48aec2006-06-25 23:19:30 -0700226 } 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 Likely6016a362010-01-28 14:06:53 -0700236 ph = dp->phandle;
David S. Miller8e48aec2006-06-25 23:19:30 -0700237
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
245static 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. Millerfa449bd2007-04-25 16:01:51 -0700252 struct device_node *dp;
253
Sinan Kayac7923732017-12-19 00:37:56 -0500254 pdev = pci_get_domain_bus_and_slot(0,
255 ((int *) op->oprom_array)[0],
256 ((int *) op->oprom_array)[1]);
David S. Miller8e48aec2006-06-25 23:19:30 -0700257
David S. Millerfa449bd2007-04-25 16:01:51 -0700258 dp = pci_device_to_OF_node(pdev);
259 data->current_node = dp;
Grant Likely6016a362010-01-28 14:06:53 -0700260 *((int *)op->oprom_array) = dp->phandle;
David S. Millerfa449bd2007-04-25 16:01:51 -0700261 op->oprom_size = sizeof(int);
262 err = copyout(argp, op, bufsize + sizeof(int));
263
Alan Cox7e9f3342007-04-23 22:50:53 -0700264 pci_dev_put(pdev);
David S. Miller8e48aec2006-06-25 23:19:30 -0700265#endif
266 }
267
268 return err;
269}
270
271static int oprompath2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
272{
David S. Millerb9b64e62006-09-18 01:47:13 -0700273 phandle ph = 0;
274
David S. Miller8e48aec2006-06-25 23:19:30 -0700275 dp = of_find_node_by_path(op->oprom_array);
David S. Millerb9b64e62006-09-18 01:47:13 -0700276 if (dp)
Grant Likely6016a362010-01-28 14:06:53 -0700277 ph = dp->phandle;
David S. Miller8e48aec2006-06-25 23:19:30 -0700278 data->current_node = dp;
David S. Millerb9b64e62006-09-18 01:47:13 -0700279 *((int *)op->oprom_array) = ph;
David S. Miller8e48aec2006-06-25 23:19:30 -0700280 op->oprom_size = sizeof(int);
281
282 return copyout(argp, op, bufsize + sizeof(int));
283}
284
285static 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 Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * SunOS and Solaris /dev/openprom ioctl calls.
301 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200302static long openprom_sunos_ioctl(struct file * file,
303 unsigned int cmd, unsigned long arg,
304 struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
David S. Miller8e48aec2006-06-25 23:19:30 -0700306 DATA *data = file->private_data;
David S. Miller32e58972009-06-16 03:46:54 -0700307 struct openpromio *opp = NULL;
David S. Miller8e48aec2006-06-25 23:19:30 -0700308 int bufsize, error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 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 Bergmanna3108ca2010-07-20 23:36:39 -0700320 mutex_lock(&openprom_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 switch (cmd) {
323 case OPROMGETOPT:
324 case OPROMGETPROP:
David S. Miller8e48aec2006-06-25 23:19:30 -0700325 error = opromgetprop(argp, dp, opp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 break;
327
328 case OPROMNXTOPT:
329 case OPROMNXTPROP:
David S. Miller8e48aec2006-06-25 23:19:30 -0700330 error = opromnxtprop(argp, dp, opp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 break;
332
333 case OPROMSETOPT:
334 case OPROMSETOPT2:
David S. Miller8e48aec2006-06-25 23:19:30 -0700335 error = opromsetopt(dp, opp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
337
338 case OPROMNEXT:
339 case OPROMCHILD:
340 case OPROMSETCUR:
David S. Miller8e48aec2006-06-25 23:19:30 -0700341 error = opromnext(argp, cmd, dp, opp, bufsize, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
343
344 case OPROMPCI2NODE:
David S. Miller8e48aec2006-06-25 23:19:30 -0700345 error = oprompci2node(argp, dp, opp, bufsize, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 break;
347
348 case OPROMPATH2NODE:
David S. Miller8e48aec2006-06-25 23:19:30 -0700349 error = oprompath2node(argp, dp, opp, bufsize, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351
352 case OPROMGETBOOTARGS:
David S. Miller8e48aec2006-06-25 23:19:30 -0700353 error = opromgetbootargs(argp, opp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 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 Bergmanna3108ca2010-07-20 23:36:39 -0700371 mutex_unlock(&openprom_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return error;
374}
375
David S. Miller8e48aec2006-06-25 23:19:30 -0700376static struct device_node *get_node(phandle n, DATA *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
David S. Miller8e48aec2006-06-25 23:19:30 -0700378 struct device_node *dp = of_find_node_by_phandle(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David S. Miller8e48aec2006-06-25 23:19:30 -0700380 if (dp)
381 data->lastnode = dp;
382
383 return dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386/* Copy in a whole string from userspace into kernelspace. */
Sam Ravnborg21916a42016-04-24 15:24:33 +0200387static char * copyin_string(char __user *user, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
Sam Ravnborg21916a42016-04-24 15:24:33 +0200390 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Sam Ravnborg21916a42016-04-24 15:24:33 +0200392 return memdup_user_nul(user, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/*
396 * NetBSD /dev/openprom ioctl calls.
397 */
David S. Miller8e48aec2006-06-25 23:19:30 -0700398static int opiocget(void __user *argp, DATA *data)
399{
400 struct opiocdesc op;
401 struct device_node *dp;
402 char *str;
Stephen Rothwellccf0dec2007-03-29 00:49:54 -0700403 const void *pval;
David S. Miller8e48aec2006-06-25 23:19:30 -0700404 int err, len;
405
406 if (copy_from_user(&op, argp, sizeof(op)))
407 return -EFAULT;
408
409 dp = get_node(op.op_nodeid, data);
410
Sam Ravnborg21916a42016-04-24 15:24:33 +0200411 str = copyin_string(op.op_name, op.op_namelen);
412 if (IS_ERR(str))
413 return PTR_ERR(str);
David S. Miller8e48aec2006-06-25 23:19:30 -0700414
415 pval = of_get_property(dp, str, &len);
416 err = 0;
417 if (!pval || len > op.op_buflen) {
418 err = -EINVAL;
419 } else {
420 op.op_buflen = len;
421 if (copy_to_user(argp, &op, sizeof(op)) ||
422 copy_to_user(op.op_buf, pval, len))
423 err = -EFAULT;
424 }
425 kfree(str);
426
427 return err;
428}
429
430static int opiocnextprop(void __user *argp, DATA *data)
431{
432 struct opiocdesc op;
433 struct device_node *dp;
434 struct property *prop;
435 char *str;
Sam Ravnborg21916a42016-04-24 15:24:33 +0200436 int len;
David S. Miller8e48aec2006-06-25 23:19:30 -0700437
438 if (copy_from_user(&op, argp, sizeof(op)))
439 return -EFAULT;
440
441 dp = get_node(op.op_nodeid, data);
442 if (!dp)
443 return -EINVAL;
444
Sam Ravnborg21916a42016-04-24 15:24:33 +0200445 str = copyin_string(op.op_name, op.op_namelen);
446 if (IS_ERR(str))
447 return PTR_ERR(str);
David S. Miller8e48aec2006-06-25 23:19:30 -0700448
449 if (str[0] == '\0') {
450 prop = dp->properties;
451 } else {
452 prop = of_find_property(dp, str, NULL);
453 if (prop)
454 prop = prop->next;
455 }
456 kfree(str);
457
458 if (!prop)
459 len = 0;
460 else
461 len = prop->length;
462
463 if (len > op.op_buflen)
464 len = op.op_buflen;
465
466 if (copy_to_user(argp, &op, sizeof(op)))
467 return -EFAULT;
468
469 if (len &&
470 copy_to_user(op.op_buf, prop->value, len))
471 return -EFAULT;
472
473 return 0;
474}
475
476static int opiocset(void __user *argp, DATA *data)
477{
478 struct opiocdesc op;
479 struct device_node *dp;
480 char *str, *tmp;
481 int err;
482
483 if (copy_from_user(&op, argp, sizeof(op)))
484 return -EFAULT;
485
486 dp = get_node(op.op_nodeid, data);
487 if (!dp)
488 return -EINVAL;
489
Sam Ravnborg21916a42016-04-24 15:24:33 +0200490 str = copyin_string(op.op_name, op.op_namelen);
491 if (IS_ERR(str))
492 return PTR_ERR(str);
David S. Miller8e48aec2006-06-25 23:19:30 -0700493
Sam Ravnborg21916a42016-04-24 15:24:33 +0200494 tmp = copyin_string(op.op_buf, op.op_buflen);
495 if (IS_ERR(tmp)) {
David S. Miller8e48aec2006-06-25 23:19:30 -0700496 kfree(str);
Sam Ravnborg21916a42016-04-24 15:24:33 +0200497 return PTR_ERR(tmp);
David S. Miller8e48aec2006-06-25 23:19:30 -0700498 }
499
500 err = of_set_property(dp, str, tmp, op.op_buflen);
501
502 kfree(str);
503 kfree(tmp);
504
505 return err;
506}
507
508static int opiocgetnext(unsigned int cmd, void __user *argp)
509{
510 struct device_node *dp;
511 phandle nd;
512
513 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
514
515 if (copy_from_user(&nd, argp, sizeof(phandle)))
516 return -EFAULT;
517
518 if (nd == 0) {
519 if (cmd != OPIOCGETNEXT)
520 return -EINVAL;
521 dp = of_find_node_by_path("/");
522 } else {
523 dp = of_find_node_by_phandle(nd);
524 nd = 0;
525 if (dp) {
526 if (cmd == OPIOCGETNEXT)
527 dp = dp->sibling;
528 else
529 dp = dp->child;
530 }
531 }
532 if (dp)
Grant Likely6016a362010-01-28 14:06:53 -0700533 nd = dp->phandle;
David S. Miller8e48aec2006-06-25 23:19:30 -0700534 if (copy_to_user(argp, &nd, sizeof(phandle)))
535 return -EFAULT;
536
537 return 0;
538}
539
Arnd Bergmann55929332010-04-27 00:24:05 +0200540static int openprom_bsd_ioctl(struct file * file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 unsigned int cmd, unsigned long arg)
542{
Joe Perches33cfe652010-07-12 21:16:04 -0700543 DATA *data = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 void __user *argp = (void __user *)arg;
David S. Miller8e48aec2006-06-25 23:19:30 -0700545 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700547 mutex_lock(&openprom_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 switch (cmd) {
549 case OPIOCGET:
David S. Miller8e48aec2006-06-25 23:19:30 -0700550 err = opiocget(argp, data);
551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 case OPIOCNEXTPROP:
David S. Miller8e48aec2006-06-25 23:19:30 -0700554 err = opiocnextprop(argp, data);
555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 case OPIOCSET:
David S. Miller8e48aec2006-06-25 23:19:30 -0700558 err = opiocset(argp, data);
559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 case OPIOCGETOPTNODE:
David S. Miller8e48aec2006-06-25 23:19:30 -0700562 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
563
Arnd Bergmann55929332010-04-27 00:24:05 +0200564 err = 0;
Grant Likely6016a362010-01-28 14:06:53 -0700565 if (copy_to_user(argp, &options_node->phandle, sizeof(phandle)))
Arnd Bergmann55929332010-04-27 00:24:05 +0200566 err = -EFAULT;
567 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 case OPIOCGETNEXT:
570 case OPIOCGETCHILD:
David S. Miller8e48aec2006-06-25 23:19:30 -0700571 err = opiocgetnext(cmd, argp);
572 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 default:
Arnd Bergmann55929332010-04-27 00:24:05 +0200575 err = -EINVAL;
576 break;
Peter Senna Tschudinda201162012-09-12 07:03:12 +0000577 }
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700578 mutex_unlock(&openprom_mutex);
David S. Miller8e48aec2006-06-25 23:19:30 -0700579
580 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583
584/*
585 * Handoff control to the correct ioctl handler.
586 */
Arnd Bergmann55929332010-04-27 00:24:05 +0200587static long openprom_ioctl(struct file * file,
588 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Joe Perches33cfe652010-07-12 21:16:04 -0700590 DATA *data = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 switch (cmd) {
593 case OPROMGETOPT:
594 case OPROMNXTOPT:
595 if ((file->f_mode & FMODE_READ) == 0)
596 return -EPERM;
Arnd Bergmann55929332010-04-27 00:24:05 +0200597 return openprom_sunos_ioctl(file, cmd, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 options_node);
599
600 case OPROMSETOPT:
601 case OPROMSETOPT2:
602 if ((file->f_mode & FMODE_WRITE) == 0)
603 return -EPERM;
Arnd Bergmann55929332010-04-27 00:24:05 +0200604 return openprom_sunos_ioctl(file, cmd, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 options_node);
606
607 case OPROMNEXT:
608 case OPROMCHILD:
609 case OPROMGETPROP:
610 case OPROMNXTPROP:
611 if ((file->f_mode & FMODE_READ) == 0)
612 return -EPERM;
Arnd Bergmann55929332010-04-27 00:24:05 +0200613 return openprom_sunos_ioctl(file, cmd, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 data->current_node);
615
616 case OPROMU2P:
617 case OPROMGETCONS:
618 case OPROMGETFBNAME:
619 case OPROMGETBOOTARGS:
620 case OPROMSETCUR:
621 case OPROMPCI2NODE:
622 case OPROMPATH2NODE:
623 if ((file->f_mode & FMODE_READ) == 0)
624 return -EPERM;
Arnd Bergmann55929332010-04-27 00:24:05 +0200625 return openprom_sunos_ioctl(file, cmd, arg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 case OPIOCGET:
628 case OPIOCNEXTPROP:
629 case OPIOCGETOPTNODE:
630 case OPIOCGETNEXT:
631 case OPIOCGETCHILD:
632 if ((file->f_mode & FMODE_READ) == 0)
633 return -EBADF;
Arnd Bergmann55929332010-04-27 00:24:05 +0200634 return openprom_bsd_ioctl(file,cmd,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 case OPIOCSET:
637 if ((file->f_mode & FMODE_WRITE) == 0)
638 return -EBADF;
Arnd Bergmann55929332010-04-27 00:24:05 +0200639 return openprom_bsd_ioctl(file,cmd,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return -EINVAL;
David S. Miller8e48aec2006-06-25 23:19:30 -0700643 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Christoph Hellwigb31023f2005-11-07 14:12:47 -0800646static long openprom_compat_ioctl(struct file *file, unsigned int cmd,
647 unsigned long arg)
648{
649 long rval = -ENOTTY;
650
651 /*
652 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
653 * the arg which we'd need to clean up...
654 */
655 switch (cmd) {
656 case OPROMGETOPT:
657 case OPROMSETOPT:
658 case OPROMNXTOPT:
659 case OPROMSETOPT2:
660 case OPROMNEXT:
661 case OPROMCHILD:
662 case OPROMGETPROP:
663 case OPROMNXTPROP:
664 case OPROMU2P:
665 case OPROMGETCONS:
666 case OPROMGETFBNAME:
667 case OPROMGETBOOTARGS:
668 case OPROMSETCUR:
669 case OPROMPCI2NODE:
670 case OPROMPATH2NODE:
Arnd Bergmann55929332010-04-27 00:24:05 +0200671 rval = openprom_ioctl(file, cmd, arg);
Christoph Hellwigb31023f2005-11-07 14:12:47 -0800672 break;
673 }
David S. Millerd5a858b2005-11-08 10:00:13 -0800674
675 return rval;
Christoph Hellwigb31023f2005-11-07 14:12:47 -0800676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678static int openprom_open(struct inode * inode, struct file * file)
679{
680 DATA *data;
681
David S. Miller8e48aec2006-06-25 23:19:30 -0700682 data = kmalloc(sizeof(DATA), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (!data)
684 return -ENOMEM;
685
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700686 mutex_lock(&openprom_mutex);
David S. Miller8e48aec2006-06-25 23:19:30 -0700687 data->current_node = of_find_node_by_path("/");
688 data->lastnode = data->current_node;
689 file->private_data = (void *) data;
Arnd Bergmanna3108ca2010-07-20 23:36:39 -0700690 mutex_unlock(&openprom_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 return 0;
693}
694
695static int openprom_release(struct inode * inode, struct file * file)
696{
697 kfree(file->private_data);
698 return 0;
699}
700
Arjan van de Ven00977a52007-02-12 00:55:34 -0800701static const struct file_operations openprom_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 .owner = THIS_MODULE,
703 .llseek = no_llseek,
Arnd Bergmann55929332010-04-27 00:24:05 +0200704 .unlocked_ioctl = openprom_ioctl,
David S. Millerd5a858b2005-11-08 10:00:13 -0800705 .compat_ioctl = openprom_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 .open = openprom_open,
707 .release = openprom_release,
708};
709
710static struct miscdevice openprom_dev = {
David S. Miller8e48aec2006-06-25 23:19:30 -0700711 .minor = SUN_OPENPROM_MINOR,
712 .name = "openprom",
713 .fops = &openprom_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714};
715
716static int __init openprom_init(void)
717{
David S. Miller8e48aec2006-06-25 23:19:30 -0700718 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
David S. Miller8e48aec2006-06-25 23:19:30 -0700720 err = misc_register(&openprom_dev);
721 if (err)
722 return err;
723
Rob Herringdf58f372018-08-29 15:03:37 -0500724 options_node = of_get_child_by_name(of_find_node_by_path("/"), "options");
David S. Miller8e48aec2006-06-25 23:19:30 -0700725 if (!options_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 misc_deregister(&openprom_dev);
727 return -EIO;
728 }
729
730 return 0;
731}
732
733static void __exit openprom_cleanup(void)
734{
735 misc_deregister(&openprom_dev);
736}
737
738module_init(openprom_init);
739module_exit(openprom_cleanup);