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/malloc.h>
40 #include <linux/string.h>
41 #include <linux/miscdevice.h>
42 #include <linux/init.h>
43 #include <asm/oplib.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
46 #include <asm/openpromio.h>
48 #include <linux/pci.h>
52 /* Private data kept by the driver for each descriptor. */
53 typedef struct openprom_private_data
55 int current_node
; /* Current node for SunOS ioctls. */
56 int lastnode
; /* Last valid node used by BSD ioctls. */
59 /* ID of the PROM node containing all of the EEPROM options. */
60 static int options_node
= 0;
63 * Copy an openpromio structure into kernel space from user space.
64 * This routine does error checking to make sure that all memory
65 * accesses are within bounds. A pointer to the allocated openpromio
66 * structure will be placed in "*opp_p". Return value is the length
67 * of the user supplied buffer.
69 static int copyin(struct openpromio
*info
, struct openpromio
**opp_p
)
76 if (get_user(bufsize
, &info
->oprom_size
))
82 /* If the bufsize is too large, just limit it.
83 * Fix from Jason Rappleye.
85 if (bufsize
> OPROMMAXPARAM
)
86 bufsize
= OPROMMAXPARAM
;
88 if (!(*opp_p
= kmalloc(sizeof(int) + bufsize
+ 1, GFP_KERNEL
)))
90 memset(*opp_p
, 0, sizeof(int) + bufsize
+ 1);
92 if (copy_from_user(&(*opp_p
)->oprom_array
,
93 &info
->oprom_array
, bufsize
)) {
100 static int getstrings(struct openpromio
*info
, struct openpromio
**opp_p
)
108 if (!(*opp_p
= kmalloc(sizeof(int) + OPROMMAXPARAM
+ 1, GFP_KERNEL
)))
111 memset(*opp_p
, 0, sizeof(int) + OPROMMAXPARAM
+ 1);
112 (*opp_p
)->oprom_size
= 0;
115 while ((n
< 2) && (bufsize
< OPROMMAXPARAM
)) {
116 if (get_user(c
, &info
->oprom_array
[bufsize
])) {
122 (*opp_p
)->oprom_array
[bufsize
++] = c
;
132 * Copy an openpromio structure in kernel space back to user space.
134 static int copyout(void *info
, struct openpromio
*opp
, int len
)
136 if (copy_to_user(info
, opp
, len
))
142 * SunOS and Solaris /dev/openprom ioctl calls.
144 static int openprom_sunos_ioctl(struct inode
* inode
, struct file
* file
,
145 unsigned int cmd
, unsigned long arg
, int node
)
147 DATA
*data
= (DATA
*) file
->private_data
;
148 char buffer
[OPROMMAXPARAM
+1], *buf
;
149 struct openpromio
*opp
;
151 int bufsize
, len
, error
= 0;
152 extern char saved_command_line
[];
155 if (cmd
== OPROMSETOPT
)
156 bufsize
= getstrings((void *)arg
, &opp
);
158 bufsize
= copyin((void *)arg
, &opp
);
167 len
= prom_getproplen(node
, opp
->oprom_array
);
168 restore_flags(flags
);
170 if (len
<= 0 || len
> bufsize
) {
171 error
= copyout((void *)arg
, opp
, sizeof(int));
176 len
= prom_getproperty(node
, opp
->oprom_array
, buffer
, bufsize
);
177 restore_flags(flags
);
179 memcpy(opp
->oprom_array
, buffer
, len
);
180 opp
->oprom_array
[len
] = '\0';
181 opp
->oprom_size
= len
;
183 error
= copyout((void *)arg
, opp
, sizeof(int) + bufsize
);
189 buf
= prom_nextprop(node
, opp
->oprom_array
, buffer
);
190 restore_flags(flags
);
193 if (len
== 0 || len
+ 1 > bufsize
) {
194 error
= copyout((void *)arg
, opp
, sizeof(int));
198 memcpy(opp
->oprom_array
, buf
, len
);
199 opp
->oprom_array
[len
] = '\0';
200 opp
->oprom_size
= ++len
;
202 error
= copyout((void *)arg
, opp
, sizeof(int) + bufsize
);
207 buf
= opp
->oprom_array
+ strlen(opp
->oprom_array
) + 1;
208 len
= opp
->oprom_array
+ bufsize
- buf
;
211 error
= prom_setprop(options_node
, opp
->oprom_array
,
213 restore_flags(flags
);
222 if (bufsize
< sizeof(int)) {
227 node
= *((int *) opp
->oprom_array
);
231 case OPROMNEXT
: node
= __prom_getsibling(node
); break;
232 case OPROMCHILD
: node
= __prom_getchild(node
); break;
233 case OPROMSETCUR
: break;
235 restore_flags(flags
);
237 data
->current_node
= node
;
238 *((int *)opp
->oprom_array
) = node
;
239 opp
->oprom_size
= sizeof(int);
241 error
= copyout((void *)arg
, opp
, bufsize
+ sizeof(int));
247 if (bufsize
>= 2*sizeof(int)) {
249 struct pci_dev
*pdev
;
250 struct pcidev_cookie
*pcp
;
251 pdev
= pci_find_slot (((int *) opp
->oprom_array
)[0],
252 ((int *) opp
->oprom_array
)[1]);
255 if (pcp
!= NULL
&& pcp
->prom_node
!= -1 && pcp
->prom_node
) {
256 node
= pcp
->prom_node
;
257 data
->current_node
= node
;
258 *((int *)opp
->oprom_array
) = node
;
259 opp
->oprom_size
= sizeof(int);
260 error
= copyout((void *)arg
, opp
, bufsize
+ sizeof(int));
268 node
= prom_finddevice(opp
->oprom_array
);
269 restore_flags(flags
);
270 data
->current_node
= node
;
271 *((int *)opp
->oprom_array
) = node
;
272 opp
->oprom_size
= sizeof(int);
274 error
= copyout((void *)arg
, opp
, bufsize
+ sizeof(int));
277 case OPROMGETBOOTARGS
:
278 buf
= saved_command_line
;
287 strcpy(opp
->oprom_array
, buf
);
288 opp
->oprom_size
= len
;
290 error
= copyout((void *)arg
, opp
, bufsize
+ sizeof(int));
297 printk(KERN_INFO
"openprom_sunos_ioctl: unimplemented ioctl\n");
302 printk(KERN_INFO
"openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd
, arg
);
312 /* Return nonzero if a specific node is in the PROM device tree. */
313 static int intree(int root
, int node
)
315 for (; root
!= 0; root
= prom_getsibling(root
))
316 if (root
== node
|| intree(prom_getchild(root
),node
))
321 /* Return nonzero if a specific node is "valid". */
322 static int goodnode(int n
, DATA
*data
)
324 if (n
== data
->lastnode
|| n
== prom_root_node
|| n
== options_node
)
326 if (n
== 0 || n
== -1 || !intree(prom_root_node
,n
))
332 /* Copy in a whole string from userspace into kernelspace. */
333 static int copyin_string(char *user
, size_t len
, char **ptr
)
337 tmp
= kmalloc(len
+ 1, GFP_KERNEL
);
341 if(copy_from_user(tmp
, user
, len
)) {
354 * NetBSD /dev/openprom ioctl calls.
356 static int openprom_bsd_ioctl(struct inode
* inode
, struct file
* file
,
357 unsigned int cmd
, unsigned long arg
)
359 DATA
*data
= (DATA
*) file
->private_data
;
362 int error
, node
, len
;
369 if (copy_from_user(&op
, (void *)arg
, sizeof(op
)))
372 if (!goodnode(op
.op_nodeid
,data
))
375 error
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
380 len
= prom_getproplen(op
.op_nodeid
,str
);
381 restore_flags(flags
);
383 if (len
> op
.op_buflen
) {
392 /* Verified by the above copy_from_user */
393 if (__copy_to_user((void *)arg
, &op
,
399 tmp
= kmalloc(len
+ 1, GFP_KERNEL
);
406 prom_getproperty(op
.op_nodeid
, str
, tmp
, len
);
407 restore_flags(flags
);
411 error
= __copy_to_user((void *)arg
, &op
, sizeof(op
));
413 error
= copy_to_user(op
.op_buf
, tmp
, len
);
421 if (copy_from_user(&op
, (void *)arg
, sizeof(op
)))
424 if (!goodnode(op
.op_nodeid
,data
))
427 error
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
432 tmp
= prom_nextprop(op
.op_nodeid
,str
,buffer
);
433 restore_flags(flags
);
437 if (len
> op
.op_buflen
)
442 len
= op
.op_buflen
= 0;
445 error
= verify_area(VERIFY_WRITE
, (void *)arg
, sizeof(op
));
451 error
= verify_area(VERIFY_WRITE
, op
.op_buf
, len
);
457 error
= __copy_to_user((void *)arg
, &op
, sizeof(op
));
458 if (!error
) error
= __copy_to_user(op
.op_buf
, tmp
, len
);
465 if (copy_from_user(&op
, (void *)arg
, sizeof(op
)))
468 if (!goodnode(op
.op_nodeid
,data
))
471 error
= copyin_string(op
.op_name
, op
.op_namelen
, &str
);
475 error
= copyin_string(op
.op_buf
, op
.op_buflen
, &tmp
);
482 len
= prom_setprop(op
.op_nodeid
,str
,tmp
,op
.op_buflen
+1);
483 restore_flags(flags
);
485 if (len
!= op
.op_buflen
)
493 case OPIOCGETOPTNODE
:
494 if (copy_to_user((void *)arg
, &options_node
, sizeof(int)))
500 if (copy_from_user(&node
, (void *)arg
, sizeof(int)))
504 if (cmd
== OPIOCGETNEXT
)
505 node
= __prom_getsibling(node
);
507 node
= __prom_getchild(node
);
508 restore_flags(flags
);
510 if (__copy_to_user((void *)arg
, &node
, sizeof(int)))
517 printk(KERN_INFO
"openprom_bsd_ioctl: cmd 0x%X\n", cmd
);
525 * Handoff control to the correct ioctl handler.
527 static int openprom_ioctl(struct inode
* inode
, struct file
* file
,
528 unsigned int cmd
, unsigned long arg
)
530 DATA
*data
= (DATA
*) file
->private_data
;
536 if ((file
->f_mode
& FMODE_READ
) == 0)
538 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
,
543 if ((file
->f_mode
& FMODE_WRITE
) == 0)
545 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
,
552 if ((file
->f_mode
& FMODE_READ
) == 0)
554 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
,
560 case OPROMGETBOOTARGS
:
564 if ((file
->f_mode
& FMODE_READ
) == 0)
566 return openprom_sunos_ioctl(inode
, file
, cmd
, arg
, 0);
570 case OPIOCGETOPTNODE
:
573 if ((file
->f_mode
& FMODE_READ
) == 0)
575 return openprom_bsd_ioctl(inode
,file
,cmd
,arg
);
578 if ((file
->f_mode
& FMODE_WRITE
) == 0)
580 return openprom_bsd_ioctl(inode
,file
,cmd
,arg
);
584 printk("openprom_ioctl: cmd 0x%X, arg 0x%lX\n", cmd
, arg
);
589 static long long openprom_lseek(struct file
* file
, long long offset
, int origin
)
594 static int openprom_open(struct inode
* inode
, struct file
* file
)
598 data
= (DATA
*) kmalloc(sizeof(DATA
), GFP_KERNEL
);
602 data
->current_node
= prom_root_node
;
603 data
->lastnode
= prom_root_node
;
604 file
->private_data
= (void *)data
;
609 static int openprom_release(struct inode
* inode
, struct file
* file
)
611 kfree(file
->private_data
);
615 static struct file_operations openprom_fops
= {
617 llseek
: openprom_lseek
,
618 ioctl
: openprom_ioctl
,
620 release
: openprom_release
,
623 static struct miscdevice openprom_dev
= {
624 SUN_OPENPROM_MINOR
, "openprom", &openprom_fops
630 int init_module(void)
632 int __init
openprom_init(void)
638 error
= misc_register(&openprom_dev
);
640 printk(KERN_ERR
"openprom: unable to get misc minor\n");
645 options_node
= prom_getchild(prom_root_node
);
646 options_node
= prom_searchsiblings(options_node
,"options");
647 restore_flags(flags
);
649 if (options_node
== 0 || options_node
== -1) {
650 printk(KERN_ERR
"openprom: unable to find options node\n");
651 misc_deregister(&openprom_dev
);
659 void cleanup_module(void)
661 misc_deregister(&openprom_dev
);