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/smp_lock.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");
55 /* Private data kept by the driver for each descriptor. */
56 typedef struct openprom_private_data
58 struct device_node
*current_node
; /* Current node for SunOS ioctls. */
59 struct device_node
*lastnode
; /* Last valid node used by BSD ioctls. */
62 /* ID of the PROM node containing all of the EEPROM options. */
63 static struct device_node
*options_node
;
66 * Copy an openpromio structure into kernel space from user space.
67 * This routine does error checking to make sure that all memory
68 * accesses are within bounds. A pointer to the allocated openpromio
69 * structure will be placed in "*opp_p". Return value is the length
70 * of the user supplied buffer.
72 static int copyin(struct openpromio __user
*info
, struct openpromio
**opp_p
)
79 if (get_user(bufsize
, &info
->oprom_size
))
85 /* If the bufsize is too large, just limit it.
86 * Fix from Jason Rappleye.
88 if (bufsize
> OPROMMAXPARAM
)
89 bufsize
= OPROMMAXPARAM
;
91 if (!(*opp_p
= kzalloc(sizeof(int) + bufsize
+ 1, GFP_KERNEL
)))
94 if (copy_from_user(&(*opp_p
)->oprom_array
,
95 &info
->oprom_array
, bufsize
)) {
102 static int getstrings(struct openpromio __user
*info
, struct openpromio
**opp_p
)
110 if (!(*opp_p
= kzalloc(sizeof(int) + OPROMMAXPARAM
+ 1, GFP_KERNEL
)))
113 (*opp_p
)->oprom_size
= 0;
116 while ((n
< 2) && (bufsize
< OPROMMAXPARAM
)) {
117 if (get_user(c
, &info
->oprom_array
[bufsize
])) {
123 (*opp_p
)->oprom_array
[bufsize
++] = c
;
133 * Copy an openpromio structure in kernel space back to user space.
135 static int copyout(void __user
*info
, struct openpromio
*opp
, int len
)
137 if (copy_to_user(info
, opp
, len
))
142 static int opromgetprop(void __user
*argp
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
)
148 !(pval
= of_get_property(dp
, op
->oprom_array
, &len
)) ||
149 len
<= 0 || len
> bufsize
)
150 return copyout(argp
, op
, sizeof(int));
152 memcpy(op
->oprom_array
, pval
, len
);
153 op
->oprom_array
[len
] = '\0';
154 op
->oprom_size
= len
;
156 return copyout(argp
, op
, sizeof(int) + bufsize
);
159 static int opromnxtprop(void __user
*argp
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
)
161 struct property
*prop
;
165 return copyout(argp
, op
, sizeof(int));
166 if (op
->oprom_array
[0] == '\0') {
167 prop
= dp
->properties
;
169 return copyout(argp
, op
, sizeof(int));
170 len
= strlen(prop
->name
);
172 prop
= of_find_property(dp
, op
->oprom_array
, NULL
);
176 (len
= strlen(prop
->next
->name
)) + 1 > bufsize
)
177 return copyout(argp
, op
, sizeof(int));
182 memcpy(op
->oprom_array
, prop
->name
, len
);
183 op
->oprom_array
[len
] = '\0';
184 op
->oprom_size
= ++len
;
186 return copyout(argp
, op
, sizeof(int) + bufsize
);
189 static int opromsetopt(struct device_node
*dp
, struct openpromio
*op
, int bufsize
)
191 char *buf
= op
->oprom_array
+ strlen(op
->oprom_array
) + 1;
192 int len
= op
->oprom_array
+ bufsize
- buf
;
194 return of_set_property(options_node
, op
->oprom_array
, buf
, len
);
197 static int opromnext(void __user
*argp
, unsigned int cmd
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
, DATA
*data
)
201 BUILD_BUG_ON(sizeof(phandle
) != sizeof(int));
203 if (bufsize
< sizeof(phandle
))
206 ph
= *((int *) op
->oprom_array
);
208 dp
= of_find_node_by_phandle(ph
);
226 /* Sibling of node zero is the root node. */
227 if (cmd
!= OPROMNEXT
)
230 dp
= of_find_node_by_path("/");
237 data
->current_node
= dp
;
238 *((int *) op
->oprom_array
) = ph
;
239 op
->oprom_size
= sizeof(phandle
);
241 return copyout(argp
, op
, bufsize
+ sizeof(int));
244 static int oprompci2node(void __user
*argp
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
, DATA
*data
)
248 if (bufsize
>= 2*sizeof(int)) {
250 struct pci_dev
*pdev
;
251 struct device_node
*dp
;
253 pdev
= pci_get_bus_and_slot (((int *) op
->oprom_array
)[0],
254 ((int *) op
->oprom_array
)[1]);
256 dp
= pci_device_to_OF_node(pdev
);
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));
269 static int oprompath2node(void __user
*argp
, struct device_node
*dp
, struct openpromio
*op
, int bufsize
, DATA
*data
)
273 dp
= of_find_node_by_path(op
->oprom_array
);
276 data
->current_node
= dp
;
277 *((int *)op
->oprom_array
) = ph
;
278 op
->oprom_size
= sizeof(int);
280 return copyout(argp
, op
, bufsize
+ sizeof(int));
283 static int opromgetbootargs(void __user
*argp
, struct openpromio
*op
, int bufsize
)
285 char *buf
= saved_command_line
;
286 int len
= strlen(buf
);
291 strcpy(op
->oprom_array
, buf
);
292 op
->oprom_size
= len
;
294 return copyout(argp
, op
, bufsize
+ sizeof(int));
298 * SunOS and Solaris /dev/openprom ioctl calls.
300 static int openprom_sunos_ioctl(struct inode
* inode
, struct file
* file
,
301 unsigned int cmd
, unsigned long arg
,
302 struct device_node
*dp
)
304 DATA
*data
= file
->private_data
;
305 struct openpromio
*opp
;
306 int bufsize
, error
= 0;
308 void __user
*argp
= (void __user
*)arg
;
310 if (cmd
== OPROMSETOPT
)
311 bufsize
= getstrings(argp
, &opp
);
313 bufsize
= copyin(argp
, &opp
);
321 error
= opromgetprop(argp
, dp
, opp
, bufsize
);
326 error
= opromnxtprop(argp
, dp
, opp
, bufsize
);
331 error
= opromsetopt(dp
, opp
, bufsize
);
337 error
= opromnext(argp
, cmd
, dp
, opp
, bufsize
, data
);
341 error
= oprompci2node(argp
, dp
, opp
, bufsize
, data
);
345 error
= oprompath2node(argp
, dp
, opp
, bufsize
, data
);
348 case OPROMGETBOOTARGS
:
349 error
= opromgetbootargs(argp
, opp
, bufsize
);
356 printk(KERN_INFO
"openprom_sunos_ioctl: unimplemented ioctl\n");
361 printk(KERN_INFO
"openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd
, arg
);
370 static struct device_node
*get_node(phandle n
, DATA
*data
)
372 struct device_node
*dp
= of_find_node_by_phandle(n
);
380 /* Copy in a whole string from userspace into kernelspace. */
381 static int copyin_string(char __user
*user
, size_t len
, char **ptr
)
385 if ((ssize_t
)len
< 0 || (ssize_t
)(len
+ 1) < 0)
388 tmp
= kmalloc(len
+ 1, GFP_KERNEL
);
392 if (copy_from_user(tmp
, user
, len
)) {
405 * NetBSD /dev/openprom ioctl calls.
407 static int opiocget(void __user
*argp
, DATA
*data
)
410 struct device_node
*dp
;
415 if (copy_from_user(&op
, argp
, sizeof(op
)))
418 dp
= get_node(op
.op_nodeid
, data
);
420 err
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
424 pval
= of_get_property(dp
, str
, &len
);
426 if (!pval
|| len
> op
.op_buflen
) {
430 if (copy_to_user(argp
, &op
, sizeof(op
)) ||
431 copy_to_user(op
.op_buf
, pval
, len
))
439 static int opiocnextprop(void __user
*argp
, DATA
*data
)
442 struct device_node
*dp
;
443 struct property
*prop
;
447 if (copy_from_user(&op
, argp
, sizeof(op
)))
450 dp
= get_node(op
.op_nodeid
, data
);
454 err
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
458 if (str
[0] == '\0') {
459 prop
= dp
->properties
;
461 prop
= of_find_property(dp
, str
, NULL
);
472 if (len
> op
.op_buflen
)
475 if (copy_to_user(argp
, &op
, sizeof(op
)))
479 copy_to_user(op
.op_buf
, prop
->value
, len
))
485 static int opiocset(void __user
*argp
, DATA
*data
)
488 struct device_node
*dp
;
492 if (copy_from_user(&op
, argp
, sizeof(op
)))
495 dp
= get_node(op
.op_nodeid
, data
);
499 err
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
503 err
= copyin_string(op
.op_buf
, op
.op_buflen
, &tmp
);
509 err
= of_set_property(dp
, str
, tmp
, op
.op_buflen
);
517 static int opiocgetnext(unsigned int cmd
, void __user
*argp
)
519 struct device_node
*dp
;
522 BUILD_BUG_ON(sizeof(phandle
) != sizeof(int));
524 if (copy_from_user(&nd
, argp
, sizeof(phandle
)))
528 if (cmd
!= OPIOCGETNEXT
)
530 dp
= of_find_node_by_path("/");
532 dp
= of_find_node_by_phandle(nd
);
535 if (cmd
== OPIOCGETNEXT
)
543 if (copy_to_user(argp
, &nd
, sizeof(phandle
)))
549 static int openprom_bsd_ioctl(struct inode
* inode
, struct file
* file
,
550 unsigned int cmd
, unsigned long arg
)
552 DATA
*data
= (DATA
*) file
->private_data
;
553 void __user
*argp
= (void __user
*)arg
;
558 err
= opiocget(argp
, data
);
562 err
= opiocnextprop(argp
, data
);
566 err
= opiocset(argp
, data
);
569 case OPIOCGETOPTNODE
:
570 BUILD_BUG_ON(sizeof(phandle
) != sizeof(int));
572 if (copy_to_user(argp
, &options_node
->node
, sizeof(phandle
)))
579 err
= opiocgetnext(cmd
, argp
);
592 * Handoff control to the correct ioctl handler.
594 static int openprom_ioctl(struct inode
* inode
, struct file
* file
,
595 unsigned int cmd
, unsigned long arg
)
597 DATA
*data
= (DATA
*) file
->private_data
;
602 if ((file
->f_mode
& FMODE_READ
) == 0)
604 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
,
609 if ((file
->f_mode
& FMODE_WRITE
) == 0)
611 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
,
618 if ((file
->f_mode
& FMODE_READ
) == 0)
620 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
,
626 case OPROMGETBOOTARGS
:
630 if ((file
->f_mode
& FMODE_READ
) == 0)
632 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
, NULL
);
636 case OPIOCGETOPTNODE
:
639 if ((file
->f_mode
& FMODE_READ
) == 0)
641 return openprom_bsd_ioctl(inode
,file
,cmd
,arg
);
644 if ((file
->f_mode
& FMODE_WRITE
) == 0)
646 return openprom_bsd_ioctl(inode
,file
,cmd
,arg
);
653 static long openprom_compat_ioctl(struct file
*file
, unsigned int cmd
,
659 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
660 * the arg which we'd need to clean up...
674 case OPROMGETBOOTARGS
:
678 rval
= openprom_ioctl(file
->f_path
.dentry
->d_inode
, file
, cmd
, arg
);
685 static int openprom_open(struct inode
* inode
, struct file
* file
)
689 data
= kmalloc(sizeof(DATA
), GFP_KERNEL
);
694 data
->current_node
= of_find_node_by_path("/");
695 data
->lastnode
= data
->current_node
;
696 file
->private_data
= (void *) data
;
702 static int openprom_release(struct inode
* inode
, struct file
* file
)
704 kfree(file
->private_data
);
708 static const struct file_operations openprom_fops
= {
709 .owner
= THIS_MODULE
,
711 .ioctl
= openprom_ioctl
,
712 .compat_ioctl
= openprom_compat_ioctl
,
713 .open
= openprom_open
,
714 .release
= openprom_release
,
717 static struct miscdevice openprom_dev
= {
718 .minor
= SUN_OPENPROM_MINOR
,
720 .fops
= &openprom_fops
,
723 static int __init
openprom_init(void)
725 struct device_node
*dp
;
728 err
= misc_register(&openprom_dev
);
732 dp
= of_find_node_by_path("/");
735 if (!strcmp(dp
->name
, "options"))
742 misc_deregister(&openprom_dev
);
749 static void __exit
openprom_cleanup(void)
751 misc_deregister(&openprom_dev
);
754 module_init(openprom_init
);
755 module_exit(openprom_cleanup
);