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)
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.
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
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.
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.
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.
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/errno.h>
35 #include <linux/slab.h>
36 #include <linux/mutex.h>
37 #include <linux/string.h>
38 #include <linux/miscdevice.h>
39 #include <linux/init.h>
41 #include <asm/oplib.h>
43 #include <asm/system.h>
44 #include <asm/uaccess.h>
45 #include <asm/openpromio.h>
47 #include <linux/pci.h>
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");
54 MODULE_ALIAS_MISCDEV(SUN_OPENPROM_MINOR
);
56 /* Private data kept by the driver for each descriptor. */
57 typedef struct openprom_private_data
59 struct device_node
*current_node
; /* Current node for SunOS ioctls. */
60 struct device_node
*lastnode
; /* Last valid node used by BSD ioctls. */
63 /* ID of the PROM node containing all of the EEPROM options. */
64 static DEFINE_MUTEX(openprom_mutex
);
65 static struct device_node
*options_node
;
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.
74 static int copyin(struct openpromio __user
*info
, struct openpromio
**opp_p
)
81 if (get_user(bufsize
, &info
->oprom_size
))
87 /* If the bufsize is too large, just limit it.
88 * Fix from Jason Rappleye.
90 if (bufsize
> OPROMMAXPARAM
)
91 bufsize
= OPROMMAXPARAM
;
93 if (!(*opp_p
= kzalloc(sizeof(int) + bufsize
+ 1, GFP_KERNEL
)))
96 if (copy_from_user(&(*opp_p
)->oprom_array
,
97 &info
->oprom_array
, bufsize
)) {
104 static int getstrings(struct openpromio __user
*info
, struct openpromio
**opp_p
)
112 if (!(*opp_p
= kzalloc(sizeof(int) + OPROMMAXPARAM
+ 1, GFP_KERNEL
)))
115 (*opp_p
)->oprom_size
= 0;
118 while ((n
< 2) && (bufsize
< OPROMMAXPARAM
)) {
119 if (get_user(c
, &info
->oprom_array
[bufsize
])) {
125 (*opp_p
)->oprom_array
[bufsize
++] = c
;
135 * Copy an openpromio structure in kernel space back to user space.
137 static int copyout(void __user
*info
, struct openpromio
*opp
, int len
)
139 if (copy_to_user(info
, opp
, len
))
144 static int opromgetprop(void __user
*argp
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
)
150 !(pval
= of_get_property(dp
, op
->oprom_array
, &len
)) ||
151 len
<= 0 || len
> bufsize
)
152 return copyout(argp
, op
, sizeof(int));
154 memcpy(op
->oprom_array
, pval
, len
);
155 op
->oprom_array
[len
] = '\0';
156 op
->oprom_size
= len
;
158 return copyout(argp
, op
, sizeof(int) + bufsize
);
161 static int opromnxtprop(void __user
*argp
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
)
163 struct property
*prop
;
167 return copyout(argp
, op
, sizeof(int));
168 if (op
->oprom_array
[0] == '\0') {
169 prop
= dp
->properties
;
171 return copyout(argp
, op
, sizeof(int));
172 len
= strlen(prop
->name
);
174 prop
= of_find_property(dp
, op
->oprom_array
, NULL
);
178 (len
= strlen(prop
->next
->name
)) + 1 > bufsize
)
179 return copyout(argp
, op
, sizeof(int));
184 memcpy(op
->oprom_array
, prop
->name
, len
);
185 op
->oprom_array
[len
] = '\0';
186 op
->oprom_size
= ++len
;
188 return copyout(argp
, op
, sizeof(int) + bufsize
);
191 static int opromsetopt(struct device_node
*dp
, struct openpromio
*op
, int bufsize
)
193 char *buf
= op
->oprom_array
+ strlen(op
->oprom_array
) + 1;
194 int len
= op
->oprom_array
+ bufsize
- buf
;
196 return of_set_property(options_node
, op
->oprom_array
, buf
, len
);
199 static int opromnext(void __user
*argp
, unsigned int cmd
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
, DATA
*data
)
203 BUILD_BUG_ON(sizeof(phandle
) != sizeof(int));
205 if (bufsize
< sizeof(phandle
))
208 ph
= *((int *) op
->oprom_array
);
210 dp
= of_find_node_by_phandle(ph
);
228 /* Sibling of node zero is the root node. */
229 if (cmd
!= OPROMNEXT
)
232 dp
= of_find_node_by_path("/");
239 data
->current_node
= dp
;
240 *((int *) op
->oprom_array
) = ph
;
241 op
->oprom_size
= sizeof(phandle
);
243 return copyout(argp
, op
, bufsize
+ sizeof(int));
246 static int oprompci2node(void __user
*argp
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
, DATA
*data
)
250 if (bufsize
>= 2*sizeof(int)) {
252 struct pci_dev
*pdev
;
253 struct device_node
*dp
;
255 pdev
= pci_get_bus_and_slot (((int *) op
->oprom_array
)[0],
256 ((int *) op
->oprom_array
)[1]);
258 dp
= pci_device_to_OF_node(pdev
);
259 data
->current_node
= dp
;
260 *((int *)op
->oprom_array
) = dp
->phandle
;
261 op
->oprom_size
= sizeof(int);
262 err
= copyout(argp
, op
, bufsize
+ sizeof(int));
271 static int oprompath2node(void __user
*argp
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
, DATA
*data
)
275 dp
= of_find_node_by_path(op
->oprom_array
);
278 data
->current_node
= dp
;
279 *((int *)op
->oprom_array
) = ph
;
280 op
->oprom_size
= sizeof(int);
282 return copyout(argp
, op
, bufsize
+ sizeof(int));
285 static int opromgetbootargs(void __user
*argp
, struct openpromio
*op
, int bufsize
)
287 char *buf
= saved_command_line
;
288 int len
= strlen(buf
);
293 strcpy(op
->oprom_array
, buf
);
294 op
->oprom_size
= len
;
296 return copyout(argp
, op
, bufsize
+ sizeof(int));
300 * SunOS and Solaris /dev/openprom ioctl calls.
302 static long openprom_sunos_ioctl(struct file
* file
,
303 unsigned int cmd
, unsigned long arg
,
304 struct device_node
*dp
)
306 DATA
*data
= file
->private_data
;
307 struct openpromio
*opp
= NULL
;
308 int bufsize
, error
= 0;
310 void __user
*argp
= (void __user
*)arg
;
312 if (cmd
== OPROMSETOPT
)
313 bufsize
= getstrings(argp
, &opp
);
315 bufsize
= copyin(argp
, &opp
);
320 mutex_lock(&openprom_mutex
);
325 error
= opromgetprop(argp
, dp
, opp
, bufsize
);
330 error
= opromnxtprop(argp
, dp
, opp
, bufsize
);
335 error
= opromsetopt(dp
, opp
, bufsize
);
341 error
= opromnext(argp
, cmd
, dp
, opp
, bufsize
, data
);
345 error
= oprompci2node(argp
, dp
, opp
, bufsize
, data
);
349 error
= oprompath2node(argp
, dp
, opp
, bufsize
, data
);
352 case OPROMGETBOOTARGS
:
353 error
= opromgetbootargs(argp
, opp
, bufsize
);
360 printk(KERN_INFO
"openprom_sunos_ioctl: unimplemented ioctl\n");
365 printk(KERN_INFO
"openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd
, arg
);
371 mutex_unlock(&openprom_mutex
);
376 static struct device_node
*get_node(phandle n
, DATA
*data
)
378 struct device_node
*dp
= of_find_node_by_phandle(n
);
386 /* Copy in a whole string from userspace into kernelspace. */
387 static int copyin_string(char __user
*user
, size_t len
, char **ptr
)
391 if ((ssize_t
)len
< 0 || (ssize_t
)(len
+ 1) < 0)
394 tmp
= kmalloc(len
+ 1, GFP_KERNEL
);
398 if (copy_from_user(tmp
, user
, len
)) {
411 * NetBSD /dev/openprom ioctl calls.
413 static int opiocget(void __user
*argp
, DATA
*data
)
416 struct device_node
*dp
;
421 if (copy_from_user(&op
, argp
, sizeof(op
)))
424 dp
= get_node(op
.op_nodeid
, data
);
426 err
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
430 pval
= of_get_property(dp
, str
, &len
);
432 if (!pval
|| len
> op
.op_buflen
) {
436 if (copy_to_user(argp
, &op
, sizeof(op
)) ||
437 copy_to_user(op
.op_buf
, pval
, len
))
445 static int opiocnextprop(void __user
*argp
, DATA
*data
)
448 struct device_node
*dp
;
449 struct property
*prop
;
453 if (copy_from_user(&op
, argp
, sizeof(op
)))
456 dp
= get_node(op
.op_nodeid
, data
);
460 err
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
464 if (str
[0] == '\0') {
465 prop
= dp
->properties
;
467 prop
= of_find_property(dp
, str
, NULL
);
478 if (len
> op
.op_buflen
)
481 if (copy_to_user(argp
, &op
, sizeof(op
)))
485 copy_to_user(op
.op_buf
, prop
->value
, len
))
491 static int opiocset(void __user
*argp
, DATA
*data
)
494 struct device_node
*dp
;
498 if (copy_from_user(&op
, argp
, sizeof(op
)))
501 dp
= get_node(op
.op_nodeid
, data
);
505 err
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
509 err
= copyin_string(op
.op_buf
, op
.op_buflen
, &tmp
);
515 err
= of_set_property(dp
, str
, tmp
, op
.op_buflen
);
523 static int opiocgetnext(unsigned int cmd
, void __user
*argp
)
525 struct device_node
*dp
;
528 BUILD_BUG_ON(sizeof(phandle
) != sizeof(int));
530 if (copy_from_user(&nd
, argp
, sizeof(phandle
)))
534 if (cmd
!= OPIOCGETNEXT
)
536 dp
= of_find_node_by_path("/");
538 dp
= of_find_node_by_phandle(nd
);
541 if (cmd
== OPIOCGETNEXT
)
549 if (copy_to_user(argp
, &nd
, sizeof(phandle
)))
555 static int openprom_bsd_ioctl(struct file
* file
,
556 unsigned int cmd
, unsigned long arg
)
558 DATA
*data
= file
->private_data
;
559 void __user
*argp
= (void __user
*)arg
;
562 mutex_lock(&openprom_mutex
);
565 err
= opiocget(argp
, data
);
569 err
= opiocnextprop(argp
, data
);
573 err
= opiocset(argp
, data
);
576 case OPIOCGETOPTNODE
:
577 BUILD_BUG_ON(sizeof(phandle
) != sizeof(int));
580 if (copy_to_user(argp
, &options_node
->phandle
, sizeof(phandle
)))
586 err
= opiocgetnext(cmd
, argp
);
593 mutex_unlock(&openprom_mutex
);
600 * Handoff control to the correct ioctl handler.
602 static long openprom_ioctl(struct file
* file
,
603 unsigned int cmd
, unsigned long arg
)
605 DATA
*data
= file
->private_data
;
610 if ((file
->f_mode
& FMODE_READ
) == 0)
612 return openprom_sunos_ioctl(file
, cmd
, arg
,
617 if ((file
->f_mode
& FMODE_WRITE
) == 0)
619 return openprom_sunos_ioctl(file
, cmd
, arg
,
626 if ((file
->f_mode
& FMODE_READ
) == 0)
628 return openprom_sunos_ioctl(file
, cmd
, arg
,
634 case OPROMGETBOOTARGS
:
638 if ((file
->f_mode
& FMODE_READ
) == 0)
640 return openprom_sunos_ioctl(file
, cmd
, arg
, NULL
);
644 case OPIOCGETOPTNODE
:
647 if ((file
->f_mode
& FMODE_READ
) == 0)
649 return openprom_bsd_ioctl(file
,cmd
,arg
);
652 if ((file
->f_mode
& FMODE_WRITE
) == 0)
654 return openprom_bsd_ioctl(file
,cmd
,arg
);
661 static long openprom_compat_ioctl(struct file
*file
, unsigned int cmd
,
667 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
668 * the arg which we'd need to clean up...
682 case OPROMGETBOOTARGS
:
686 rval
= openprom_ioctl(file
, cmd
, arg
);
693 static int openprom_open(struct inode
* inode
, struct file
* file
)
697 data
= kmalloc(sizeof(DATA
), GFP_KERNEL
);
701 mutex_lock(&openprom_mutex
);
702 data
->current_node
= of_find_node_by_path("/");
703 data
->lastnode
= data
->current_node
;
704 file
->private_data
= (void *) data
;
705 mutex_unlock(&openprom_mutex
);
710 static int openprom_release(struct inode
* inode
, struct file
* file
)
712 kfree(file
->private_data
);
716 static const struct file_operations openprom_fops
= {
717 .owner
= THIS_MODULE
,
719 .unlocked_ioctl
= openprom_ioctl
,
720 .compat_ioctl
= openprom_compat_ioctl
,
721 .open
= openprom_open
,
722 .release
= openprom_release
,
725 static struct miscdevice openprom_dev
= {
726 .minor
= SUN_OPENPROM_MINOR
,
728 .fops
= &openprom_fops
,
731 static int __init
openprom_init(void)
733 struct device_node
*dp
;
736 err
= misc_register(&openprom_dev
);
740 dp
= of_find_node_by_path("/");
743 if (!strcmp(dp
->name
, "options"))
750 misc_deregister(&openprom_dev
);
757 static void __exit
openprom_cleanup(void)
759 misc_deregister(&openprom_dev
);
762 module_init(openprom_init
);
763 module_exit(openprom_cleanup
);