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 #define PROMLIB_INTERNAL
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/errno.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/miscdevice.h>
42 #include <linux/smp_lock.h>
43 #include <linux/init.h>
45 #include <asm/oplib.h>
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
48 #include <asm/openpromio.h>
50 #include <linux/pci.h>
54 /* Private data kept by the driver for each descriptor. */
55 typedef struct openprom_private_data
57 int current_node
; /* Current node for SunOS ioctls. */
58 int lastnode
; /* Last valid node used by BSD ioctls. */
61 /* ID of the PROM node containing all of the EEPROM options. */
62 static int options_node
= 0;
65 * Copy an openpromio structure into kernel space from user space.
66 * This routine does error checking to make sure that all memory
67 * accesses are within bounds. A pointer to the allocated openpromio
68 * structure will be placed in "*opp_p". Return value is the length
69 * of the user supplied buffer.
71 static int copyin(struct openpromio __user
*info
, struct openpromio
**opp_p
)
78 if (get_user(bufsize
, &info
->oprom_size
))
84 /* If the bufsize is too large, just limit it.
85 * Fix from Jason Rappleye.
87 if (bufsize
> OPROMMAXPARAM
)
88 bufsize
= OPROMMAXPARAM
;
90 if (!(*opp_p
= kmalloc(sizeof(int) + bufsize
+ 1, GFP_KERNEL
)))
92 memset(*opp_p
, 0, sizeof(int) + bufsize
+ 1);
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
= kmalloc(sizeof(int) + OPROMMAXPARAM
+ 1, GFP_KERNEL
)))
113 memset(*opp_p
, 0, sizeof(int) + OPROMMAXPARAM
+ 1);
114 (*opp_p
)->oprom_size
= 0;
117 while ((n
< 2) && (bufsize
< OPROMMAXPARAM
)) {
118 if (get_user(c
, &info
->oprom_array
[bufsize
])) {
124 (*opp_p
)->oprom_array
[bufsize
++] = c
;
134 * Copy an openpromio structure in kernel space back to user space.
136 static int copyout(void __user
*info
, struct openpromio
*opp
, int len
)
138 if (copy_to_user(info
, opp
, len
))
144 * SunOS and Solaris /dev/openprom ioctl calls.
146 static int openprom_sunos_ioctl(struct inode
* inode
, struct file
* file
,
147 unsigned int cmd
, unsigned long arg
, int node
)
149 DATA
*data
= (DATA
*) file
->private_data
;
150 char buffer
[OPROMMAXPARAM
+1], *buf
;
151 struct openpromio
*opp
;
152 int bufsize
, len
, error
= 0;
154 void __user
*argp
= (void __user
*)arg
;
156 if (cmd
== OPROMSETOPT
)
157 bufsize
= getstrings(argp
, &opp
);
159 bufsize
= copyin(argp
, &opp
);
167 len
= prom_getproplen(node
, opp
->oprom_array
);
169 if (len
<= 0 || len
> bufsize
) {
170 error
= copyout(argp
, opp
, sizeof(int));
174 len
= prom_getproperty(node
, opp
->oprom_array
, buffer
, bufsize
);
176 memcpy(opp
->oprom_array
, buffer
, len
);
177 opp
->oprom_array
[len
] = '\0';
178 opp
->oprom_size
= len
;
180 error
= copyout(argp
, opp
, sizeof(int) + bufsize
);
185 buf
= prom_nextprop(node
, opp
->oprom_array
, buffer
);
188 if (len
== 0 || len
+ 1 > bufsize
) {
189 error
= copyout(argp
, opp
, sizeof(int));
193 memcpy(opp
->oprom_array
, buf
, len
);
194 opp
->oprom_array
[len
] = '\0';
195 opp
->oprom_size
= ++len
;
197 error
= copyout(argp
, opp
, sizeof(int) + bufsize
);
202 buf
= opp
->oprom_array
+ strlen(opp
->oprom_array
) + 1;
203 len
= opp
->oprom_array
+ bufsize
- buf
;
205 error
= prom_setprop(options_node
, opp
->oprom_array
,
215 if (bufsize
< sizeof(int)) {
220 node
= *((int *) opp
->oprom_array
);
223 case OPROMNEXT
: node
= __prom_getsibling(node
); break;
224 case OPROMCHILD
: node
= __prom_getchild(node
); break;
225 case OPROMSETCUR
: break;
228 data
->current_node
= node
;
229 *((int *)opp
->oprom_array
) = node
;
230 opp
->oprom_size
= sizeof(int);
232 error
= copyout(argp
, opp
, bufsize
+ sizeof(int));
238 if (bufsize
>= 2*sizeof(int)) {
240 struct pci_dev
*pdev
;
241 struct pcidev_cookie
*pcp
;
242 pdev
= pci_find_slot (((int *) opp
->oprom_array
)[0],
243 ((int *) opp
->oprom_array
)[1]);
246 if (pcp
!= NULL
&& pcp
->prom_node
!= -1 && pcp
->prom_node
) {
247 node
= pcp
->prom_node
;
248 data
->current_node
= node
;
249 *((int *)opp
->oprom_array
) = node
;
250 opp
->oprom_size
= sizeof(int);
251 error
= copyout(argp
, opp
, bufsize
+ sizeof(int));
258 node
= prom_finddevice(opp
->oprom_array
);
259 data
->current_node
= node
;
260 *((int *)opp
->oprom_array
) = node
;
261 opp
->oprom_size
= sizeof(int);
263 error
= copyout(argp
, opp
, bufsize
+ sizeof(int));
266 case OPROMGETBOOTARGS
:
267 buf
= saved_command_line
;
276 strcpy(opp
->oprom_array
, buf
);
277 opp
->oprom_size
= len
;
279 error
= copyout(argp
, opp
, bufsize
+ sizeof(int));
286 printk(KERN_INFO
"openprom_sunos_ioctl: unimplemented ioctl\n");
291 printk(KERN_INFO
"openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd
, arg
);
301 /* Return nonzero if a specific node is in the PROM device tree. */
302 static int intree(int root
, int node
)
304 for (; root
!= 0; root
= prom_getsibling(root
))
305 if (root
== node
|| intree(prom_getchild(root
),node
))
310 /* Return nonzero if a specific node is "valid". */
311 static int goodnode(int n
, DATA
*data
)
313 if (n
== data
->lastnode
|| n
== prom_root_node
|| n
== options_node
)
315 if (n
== 0 || n
== -1 || !intree(prom_root_node
,n
))
321 /* Copy in a whole string from userspace into kernelspace. */
322 static int copyin_string(char __user
*user
, size_t len
, char **ptr
)
326 if ((ssize_t
)len
< 0 || (ssize_t
)(len
+ 1) < 0)
329 tmp
= kmalloc(len
+ 1, GFP_KERNEL
);
333 if(copy_from_user(tmp
, user
, len
)) {
346 * NetBSD /dev/openprom ioctl calls.
348 static int openprom_bsd_ioctl(struct inode
* inode
, struct file
* file
,
349 unsigned int cmd
, unsigned long arg
)
351 DATA
*data
= (DATA
*) file
->private_data
;
352 void __user
*argp
= (void __user
*)arg
;
354 int error
, node
, len
;
361 if (copy_from_user(&op
, argp
, sizeof(op
)))
364 if (!goodnode(op
.op_nodeid
,data
))
367 error
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
371 len
= prom_getproplen(op
.op_nodeid
,str
);
373 if (len
> op
.op_buflen
) {
382 /* Verified by the above copy_from_user */
383 if (__copy_to_user(argp
, &op
,
389 tmp
= kmalloc(len
+ 1, GFP_KERNEL
);
395 cnt
= prom_getproperty(op
.op_nodeid
, str
, tmp
, len
);
401 if (__copy_to_user(argp
, &op
, sizeof(op
)) != 0 ||
402 copy_to_user(op
.op_buf
, tmp
, len
) != 0)
412 if (copy_from_user(&op
, argp
, sizeof(op
)))
415 if (!goodnode(op
.op_nodeid
,data
))
418 error
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
422 tmp
= prom_nextprop(op
.op_nodeid
,str
,buffer
);
426 if (len
> op
.op_buflen
)
431 len
= op
.op_buflen
= 0;
434 if (!access_ok(VERIFY_WRITE
, argp
, sizeof(op
))) {
439 if (!access_ok(VERIFY_WRITE
, op
.op_buf
, len
)) {
444 error
= __copy_to_user(argp
, &op
, sizeof(op
));
445 if (!error
) error
= __copy_to_user(op
.op_buf
, tmp
, len
);
452 if (copy_from_user(&op
, argp
, sizeof(op
)))
455 if (!goodnode(op
.op_nodeid
,data
))
458 error
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
462 error
= copyin_string(op
.op_buf
, op
.op_buflen
, &tmp
);
468 len
= prom_setprop(op
.op_nodeid
,str
,tmp
,op
.op_buflen
+1);
470 if (len
!= op
.op_buflen
)
478 case OPIOCGETOPTNODE
:
479 if (copy_to_user(argp
, &options_node
, sizeof(int)))
485 if (copy_from_user(&node
, argp
, sizeof(int)))
488 if (cmd
== OPIOCGETNEXT
)
489 node
= __prom_getsibling(node
);
491 node
= __prom_getchild(node
);
493 if (__copy_to_user(argp
, &node
, sizeof(int)))
500 printk(KERN_INFO
"openprom_bsd_ioctl: cmd 0x%X\n", cmd
);
508 * Handoff control to the correct ioctl handler.
510 static int openprom_ioctl(struct inode
* inode
, struct file
* file
,
511 unsigned int cmd
, unsigned long arg
)
513 DATA
*data
= (DATA
*) file
->private_data
;
519 if ((file
->f_mode
& FMODE_READ
) == 0)
521 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
,
526 if ((file
->f_mode
& FMODE_WRITE
) == 0)
528 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
,
535 if ((file
->f_mode
& FMODE_READ
) == 0)
537 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
,
543 case OPROMGETBOOTARGS
:
547 if ((file
->f_mode
& FMODE_READ
) == 0)
549 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
, 0);
553 case OPIOCGETOPTNODE
:
556 if ((file
->f_mode
& FMODE_READ
) == 0)
558 return openprom_bsd_ioctl(inode
,file
,cmd
,arg
);
561 if ((file
->f_mode
& FMODE_WRITE
) == 0)
563 return openprom_bsd_ioctl(inode
,file
,cmd
,arg
);
567 printk("openprom_ioctl: cmd 0x%X, arg 0x%lX\n", cmd
, arg
);
572 static long openprom_compat_ioctl(struct file
*file
, unsigned int cmd
,
578 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
579 * the arg which we'd need to clean up...
593 case OPROMGETBOOTARGS
:
598 rval
= openprom_ioctl(file
->f_dentry
->d_inode
, file
, cmd
, arg
);
606 static int openprom_open(struct inode
* inode
, struct file
* file
)
610 data
= (DATA
*) kmalloc(sizeof(DATA
), GFP_KERNEL
);
614 data
->current_node
= prom_root_node
;
615 data
->lastnode
= prom_root_node
;
616 file
->private_data
= (void *)data
;
621 static int openprom_release(struct inode
* inode
, struct file
* file
)
623 kfree(file
->private_data
);
627 static struct file_operations openprom_fops
= {
628 .owner
= THIS_MODULE
,
630 .ioctl
= openprom_ioctl
,
631 .compat_ioctl
= openprom_compat_ioctl
,
632 .open
= openprom_open
,
633 .release
= openprom_release
,
636 static struct miscdevice openprom_dev
= {
637 SUN_OPENPROM_MINOR
, "openprom", &openprom_fops
640 static int __init
openprom_init(void)
644 error
= misc_register(&openprom_dev
);
646 printk(KERN_ERR
"openprom: unable to get misc minor\n");
650 options_node
= prom_getchild(prom_root_node
);
651 options_node
= prom_searchsiblings(options_node
,"options");
653 if (options_node
== 0 || options_node
== -1) {
654 printk(KERN_ERR
"openprom: unable to find options node\n");
655 misc_deregister(&openprom_dev
);
662 static void __exit
openprom_cleanup(void)
664 misc_deregister(&openprom_dev
);
667 module_init(openprom_init
);
668 module_exit(openprom_cleanup
);
669 MODULE_LICENSE("GPL");