blob: d7e4bb41bd79a5dcb8ffc21b849156a339654c6e [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/config.h>
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/sched.h>
36#include <linux/errno.h>
37#include <linux/slab.h>
38#include <linux/string.h>
39#include <linux/miscdevice.h>
40#include <linux/init.h>
41#include <linux/fs.h>
42#include <asm/oplib.h>
David S. Miller8e48aec2006-06-25 23:19:30 -070043#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/system.h>
45#include <asm/uaccess.h>
46#include <asm/openpromio.h>
47#ifdef CONFIG_PCI
48#include <linux/pci.h>
49#include <asm/pbm.h>
50#endif
51
David S. Miller8e48aec2006-06-25 23:19:30 -070052MODULE_AUTHOR("Thomas K. Dyas (tdyas@noc.rutgers.edu) and Eddie C. Dost (ecd@skynet.be)");
53MODULE_DESCRIPTION("OPENPROM Configuration Driver");
54MODULE_LICENSE("GPL");
55MODULE_VERSION("1.0");
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* Private data kept by the driver for each descriptor. */
58typedef struct openprom_private_data
59{
David S. Miller8e48aec2006-06-25 23:19:30 -070060 struct device_node *current_node; /* Current node for SunOS ioctls. */
61 struct device_node *lastnode; /* Last valid node used by BSD ioctls. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062} DATA;
63
64/* ID of the PROM node containing all of the EEPROM options. */
David S. Miller8e48aec2006-06-25 23:19:30 -070065static struct device_node *options_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
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 */
74static 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. Miller8e48aec2006-06-25 23:19:30 -070093 if (!(*opp_p = kzalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
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
104static 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. Miller8e48aec2006-06-25 23:19:30 -0700112 if (!(*opp_p = kzalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -ENOMEM;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 (*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 */
137static 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. Miller8e48aec2006-06-25 23:19:30 -0700144static int opromgetprop(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize)
145{
146 void *pval;
147 int len;
148
149 pval = of_get_property(dp, op->oprom_array, &len);
150 if (!pval || len <= 0 || len > bufsize)
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
160static int opromnxtprop(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize)
161{
162 struct property *prop;
163 int len;
164
165 if (op->oprom_array[0] == '\0') {
166 prop = dp->properties;
167 if (!prop)
168 return copyout(argp, op, sizeof(int));
169 len = strlen(prop->name);
170 } else {
171 prop = of_find_property(dp, op->oprom_array, NULL);
172
173 if (!prop ||
174 !prop->next ||
175 (len = strlen(prop->next->name)) + 1 > bufsize)
176 return copyout(argp, op, sizeof(int));
177
178 prop = prop->next;
179 }
180
181 memcpy(op->oprom_array, prop->name, len);
182 op->oprom_array[len] = '\0';
183 op->oprom_size = ++len;
184
185 return copyout(argp, op, sizeof(int) + bufsize);
186}
187
188static int opromsetopt(struct device_node *dp, struct openpromio *op, int bufsize)
189{
190 char *buf = op->oprom_array + strlen(op->oprom_array) + 1;
191 int len = op->oprom_array + bufsize - buf;
192
193 return of_set_property(options_node, op->oprom_array, buf, len);
194}
195
196static int opromnext(void __user *argp, unsigned int cmd, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
197{
198 phandle ph;
199
200 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
201
202 if (bufsize < sizeof(phandle))
203 return -EINVAL;
204
205 ph = *((int *) op->oprom_array);
206 if (ph) {
207 dp = of_find_node_by_phandle(ph);
208 if (!dp)
209 return -EINVAL;
210
211 switch (cmd) {
212 case OPROMNEXT:
213 dp = dp->sibling;
214 break;
215
216 case OPROMCHILD:
217 dp = dp->child;
218 break;
219
220 case OPROMSETCUR:
221 default:
222 break;
223 };
224 } else {
225 /* Sibling of node zero is the root node. */
226 if (cmd != OPROMNEXT)
227 return -EINVAL;
228
229 dp = of_find_node_by_path("/");
230 }
231
232 ph = 0;
233 if (dp)
234 ph = dp->node;
235
236 data->current_node = dp;
237 *((int *) op->oprom_array) = ph;
238 op->oprom_size = sizeof(phandle);
239
240 return copyout(argp, op, bufsize + sizeof(int));
241}
242
243static int oprompci2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
244{
245 int err = -EINVAL;
246
247 if (bufsize >= 2*sizeof(int)) {
248#ifdef CONFIG_PCI
249 struct pci_dev *pdev;
250 struct pcidev_cookie *pcp;
251 pdev = pci_find_slot (((int *) op->oprom_array)[0],
252 ((int *) op->oprom_array)[1]);
253
254 pcp = pdev->sysdata;
255 if (pcp != NULL) {
256 dp = pcp->prom_node;
257 data->current_node = dp;
258 *((int *)op->oprom_array) = dp->node;
259 op->oprom_size = sizeof(int);
260 err = copyout(argp, op, bufsize + sizeof(int));
261 }
262#endif
263 }
264
265 return err;
266}
267
268static int oprompath2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
269{
270 dp = of_find_node_by_path(op->oprom_array);
271 data->current_node = dp;
272 *((int *)op->oprom_array) = dp->node;
273 op->oprom_size = sizeof(int);
274
275 return copyout(argp, op, bufsize + sizeof(int));
276}
277
278static int opromgetbootargs(void __user *argp, struct openpromio *op, int bufsize)
279{
280 char *buf = saved_command_line;
281 int len = strlen(buf);
282
283 if (len > bufsize)
284 return -EINVAL;
285
286 strcpy(op->oprom_array, buf);
287 op->oprom_size = len;
288
289 return copyout(argp, op, bufsize + sizeof(int));
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * SunOS and Solaris /dev/openprom ioctl calls.
294 */
295static int openprom_sunos_ioctl(struct inode * inode, struct file * file,
David S. Miller8e48aec2006-06-25 23:19:30 -0700296 unsigned int cmd, unsigned long arg,
297 struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
David S. Miller8e48aec2006-06-25 23:19:30 -0700299 DATA *data = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct openpromio *opp;
David S. Miller8e48aec2006-06-25 23:19:30 -0700301 int bufsize, error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 static int cnt;
303 void __user *argp = (void __user *)arg;
304
305 if (cmd == OPROMSETOPT)
306 bufsize = getstrings(argp, &opp);
307 else
308 bufsize = copyin(argp, &opp);
309
310 if (bufsize < 0)
311 return bufsize;
312
313 switch (cmd) {
314 case OPROMGETOPT:
315 case OPROMGETPROP:
David S. Miller8e48aec2006-06-25 23:19:30 -0700316 error = opromgetprop(argp, dp, opp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
318
319 case OPROMNXTOPT:
320 case OPROMNXTPROP:
David S. Miller8e48aec2006-06-25 23:19:30 -0700321 error = opromnxtprop(argp, dp, opp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
323
324 case OPROMSETOPT:
325 case OPROMSETOPT2:
David S. Miller8e48aec2006-06-25 23:19:30 -0700326 error = opromsetopt(dp, opp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328
329 case OPROMNEXT:
330 case OPROMCHILD:
331 case OPROMSETCUR:
David S. Miller8e48aec2006-06-25 23:19:30 -0700332 error = opromnext(argp, cmd, dp, opp, bufsize, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 break;
334
335 case OPROMPCI2NODE:
David S. Miller8e48aec2006-06-25 23:19:30 -0700336 error = oprompci2node(argp, dp, opp, bufsize, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 break;
338
339 case OPROMPATH2NODE:
David S. Miller8e48aec2006-06-25 23:19:30 -0700340 error = oprompath2node(argp, dp, opp, bufsize, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 break;
342
343 case OPROMGETBOOTARGS:
David S. Miller8e48aec2006-06-25 23:19:30 -0700344 error = opromgetbootargs(argp, opp, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 break;
346
347 case OPROMU2P:
348 case OPROMGETCONS:
349 case OPROMGETFBNAME:
350 if (cnt++ < 10)
351 printk(KERN_INFO "openprom_sunos_ioctl: unimplemented ioctl\n");
352 error = -EINVAL;
353 break;
354 default:
355 if (cnt++ < 10)
356 printk(KERN_INFO "openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
357 error = -EINVAL;
358 break;
359 }
360
361 kfree(opp);
362 return error;
363}
364
David S. Miller8e48aec2006-06-25 23:19:30 -0700365static struct device_node *get_node(phandle n, DATA *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
David S. Miller8e48aec2006-06-25 23:19:30 -0700367 struct device_node *dp = of_find_node_by_phandle(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
David S. Miller8e48aec2006-06-25 23:19:30 -0700369 if (dp)
370 data->lastnode = dp;
371
372 return dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/* Copy in a whole string from userspace into kernelspace. */
376static int copyin_string(char __user *user, size_t len, char **ptr)
377{
378 char *tmp;
379
380 if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
381 return -EINVAL;
382
383 tmp = kmalloc(len + 1, GFP_KERNEL);
384 if (!tmp)
385 return -ENOMEM;
386
David S. Miller8e48aec2006-06-25 23:19:30 -0700387 if (copy_from_user(tmp, user, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 kfree(tmp);
389 return -EFAULT;
390 }
391
392 tmp[len] = '\0';
393
394 *ptr = tmp;
395
396 return 0;
397}
398
399/*
400 * NetBSD /dev/openprom ioctl calls.
401 */
David S. Miller8e48aec2006-06-25 23:19:30 -0700402static int opiocget(void __user *argp, DATA *data)
403{
404 struct opiocdesc op;
405 struct device_node *dp;
406 char *str;
407 void *pval;
408 int err, len;
409
410 if (copy_from_user(&op, argp, sizeof(op)))
411 return -EFAULT;
412
413 dp = get_node(op.op_nodeid, data);
414
415 err = copyin_string(op.op_name, op.op_namelen, &str);
416 if (err)
417 return err;
418
419 pval = of_get_property(dp, str, &len);
420 err = 0;
421 if (!pval || len > op.op_buflen) {
422 err = -EINVAL;
423 } else {
424 op.op_buflen = len;
425 if (copy_to_user(argp, &op, sizeof(op)) ||
426 copy_to_user(op.op_buf, pval, len))
427 err = -EFAULT;
428 }
429 kfree(str);
430
431 return err;
432}
433
434static int opiocnextprop(void __user *argp, DATA *data)
435{
436 struct opiocdesc op;
437 struct device_node *dp;
438 struct property *prop;
439 char *str;
440 int err, len;
441
442 if (copy_from_user(&op, argp, sizeof(op)))
443 return -EFAULT;
444
445 dp = get_node(op.op_nodeid, data);
446 if (!dp)
447 return -EINVAL;
448
449 err = copyin_string(op.op_name, op.op_namelen, &str);
450 if (err)
451 return err;
452
453 if (str[0] == '\0') {
454 prop = dp->properties;
455 } else {
456 prop = of_find_property(dp, str, NULL);
457 if (prop)
458 prop = prop->next;
459 }
460 kfree(str);
461
462 if (!prop)
463 len = 0;
464 else
465 len = prop->length;
466
467 if (len > op.op_buflen)
468 len = op.op_buflen;
469
470 if (copy_to_user(argp, &op, sizeof(op)))
471 return -EFAULT;
472
473 if (len &&
474 copy_to_user(op.op_buf, prop->value, len))
475 return -EFAULT;
476
477 return 0;
478}
479
480static int opiocset(void __user *argp, DATA *data)
481{
482 struct opiocdesc op;
483 struct device_node *dp;
484 char *str, *tmp;
485 int err;
486
487 if (copy_from_user(&op, argp, sizeof(op)))
488 return -EFAULT;
489
490 dp = get_node(op.op_nodeid, data);
491 if (!dp)
492 return -EINVAL;
493
494 err = copyin_string(op.op_name, op.op_namelen, &str);
495 if (err)
496 return err;
497
498 err = copyin_string(op.op_buf, op.op_buflen, &tmp);
499 if (err) {
500 kfree(str);
501 return err;
502 }
503
504 err = of_set_property(dp, str, tmp, op.op_buflen);
505
506 kfree(str);
507 kfree(tmp);
508
509 return err;
510}
511
512static int opiocgetnext(unsigned int cmd, void __user *argp)
513{
514 struct device_node *dp;
515 phandle nd;
516
517 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
518
519 if (copy_from_user(&nd, argp, sizeof(phandle)))
520 return -EFAULT;
521
522 if (nd == 0) {
523 if (cmd != OPIOCGETNEXT)
524 return -EINVAL;
525 dp = of_find_node_by_path("/");
526 } else {
527 dp = of_find_node_by_phandle(nd);
528 nd = 0;
529 if (dp) {
530 if (cmd == OPIOCGETNEXT)
531 dp = dp->sibling;
532 else
533 dp = dp->child;
534 }
535 }
536 if (dp)
537 nd = dp->node;
538 if (copy_to_user(argp, &nd, sizeof(phandle)))
539 return -EFAULT;
540
541 return 0;
542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544static int openprom_bsd_ioctl(struct inode * inode, struct file * file,
545 unsigned int cmd, unsigned long arg)
546{
547 DATA *data = (DATA *) file->private_data;
548 void __user *argp = (void __user *)arg;
David S. Miller8e48aec2006-06-25 23:19:30 -0700549 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 switch (cmd) {
552 case OPIOCGET:
David S. Miller8e48aec2006-06-25 23:19:30 -0700553 err = opiocget(argp, data);
554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 case OPIOCNEXTPROP:
David S. Miller8e48aec2006-06-25 23:19:30 -0700557 err = opiocnextprop(argp, data);
558 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 case OPIOCSET:
David S. Miller8e48aec2006-06-25 23:19:30 -0700561 err = opiocset(argp, data);
562 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 case OPIOCGETOPTNODE:
David S. Miller8e48aec2006-06-25 23:19:30 -0700565 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
566
567 if (copy_to_user(argp, &options_node->node, sizeof(phandle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -EFAULT;
David S. Miller8e48aec2006-06-25 23:19:30 -0700569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
571
572 case OPIOCGETNEXT:
573 case OPIOCGETCHILD:
David S. Miller8e48aec2006-06-25 23:19:30 -0700574 err = opiocgetnext(cmd, argp);
575 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -EINVAL;
579
David S. Miller8e48aec2006-06-25 23:19:30 -0700580 };
581
582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585
586/*
587 * Handoff control to the correct ioctl handler.
588 */
589static int openprom_ioctl(struct inode * inode, struct file * file,
590 unsigned int cmd, unsigned long arg)
591{
592 DATA *data = (DATA *) file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 switch (cmd) {
595 case OPROMGETOPT:
596 case OPROMNXTOPT:
597 if ((file->f_mode & FMODE_READ) == 0)
598 return -EPERM;
599 return openprom_sunos_ioctl(inode, file, cmd, arg,
600 options_node);
601
602 case OPROMSETOPT:
603 case OPROMSETOPT2:
604 if ((file->f_mode & FMODE_WRITE) == 0)
605 return -EPERM;
606 return openprom_sunos_ioctl(inode, file, cmd, arg,
607 options_node);
608
609 case OPROMNEXT:
610 case OPROMCHILD:
611 case OPROMGETPROP:
612 case OPROMNXTPROP:
613 if ((file->f_mode & FMODE_READ) == 0)
614 return -EPERM;
615 return openprom_sunos_ioctl(inode, file, cmd, arg,
616 data->current_node);
617
618 case OPROMU2P:
619 case OPROMGETCONS:
620 case OPROMGETFBNAME:
621 case OPROMGETBOOTARGS:
622 case OPROMSETCUR:
623 case OPROMPCI2NODE:
624 case OPROMPATH2NODE:
625 if ((file->f_mode & FMODE_READ) == 0)
626 return -EPERM;
627 return openprom_sunos_ioctl(inode, file, cmd, arg, 0);
628
629 case OPIOCGET:
630 case OPIOCNEXTPROP:
631 case OPIOCGETOPTNODE:
632 case OPIOCGETNEXT:
633 case OPIOCGETCHILD:
634 if ((file->f_mode & FMODE_READ) == 0)
635 return -EBADF;
636 return openprom_bsd_ioctl(inode,file,cmd,arg);
637
638 case OPIOCSET:
639 if ((file->f_mode & FMODE_WRITE) == 0)
640 return -EBADF;
641 return openprom_bsd_ioctl(inode,file,cmd,arg);
642
643 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -EINVAL;
David S. Miller8e48aec2006-06-25 23:19:30 -0700645 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Christoph Hellwigb31023f2005-11-07 14:12:47 -0800648static long openprom_compat_ioctl(struct file *file, unsigned int cmd,
649 unsigned long arg)
650{
651 long rval = -ENOTTY;
652
653 /*
654 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
655 * the arg which we'd need to clean up...
656 */
657 switch (cmd) {
658 case OPROMGETOPT:
659 case OPROMSETOPT:
660 case OPROMNXTOPT:
661 case OPROMSETOPT2:
662 case OPROMNEXT:
663 case OPROMCHILD:
664 case OPROMGETPROP:
665 case OPROMNXTPROP:
666 case OPROMU2P:
667 case OPROMGETCONS:
668 case OPROMGETFBNAME:
669 case OPROMGETBOOTARGS:
670 case OPROMSETCUR:
671 case OPROMPCI2NODE:
672 case OPROMPATH2NODE:
Christoph Hellwigb31023f2005-11-07 14:12:47 -0800673 rval = openprom_ioctl(file->f_dentry->d_inode, file, cmd, arg);
Christoph Hellwigb31023f2005-11-07 14:12:47 -0800674 break;
675 }
David S. Millerd5a858b2005-11-08 10:00:13 -0800676
677 return rval;
Christoph Hellwigb31023f2005-11-07 14:12:47 -0800678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680static int openprom_open(struct inode * inode, struct file * file)
681{
682 DATA *data;
683
David S. Miller8e48aec2006-06-25 23:19:30 -0700684 data = kmalloc(sizeof(DATA), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!data)
686 return -ENOMEM;
687
David S. Miller8e48aec2006-06-25 23:19:30 -0700688 data->current_node = of_find_node_by_path("/");
689 data->lastnode = data->current_node;
690 file->private_data = (void *) data;
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
701static struct file_operations openprom_fops = {
702 .owner = THIS_MODULE,
703 .llseek = no_llseek,
704 .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 struct device_node *dp;
719 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
David S. Miller8e48aec2006-06-25 23:19:30 -0700721 err = misc_register(&openprom_dev);
722 if (err)
723 return err;
724
725 dp = of_find_node_by_path("/");
726 dp = dp->child;
727 while (dp) {
728 if (!strcmp(dp->name, "options"))
729 break;
730 dp = dp->sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
David S. Miller8e48aec2006-06-25 23:19:30 -0700732 options_node = dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
David S. Miller8e48aec2006-06-25 23:19:30 -0700734 if (!options_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 misc_deregister(&openprom_dev);
736 return -EIO;
737 }
738
739 return 0;
740}
741
742static void __exit openprom_cleanup(void)
743{
744 misc_deregister(&openprom_dev);
745}
746
747module_init(openprom_init);
748module_exit(openprom_cleanup);