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/init.h>
44 #include <asm/oplib.h>
45 #include <asm/system.h>
46 #include <asm/uaccess.h>
47 #include <asm/openpromio.h>
49 #include <linux/pci.h>
53 /* Private data kept by the driver for each descriptor. */
54 typedef struct openprom_private_data
56 int current_node; /* Current node for SunOS ioctls. */
57 int lastnode; /* Last valid node used by BSD ioctls. */
60 /* ID of the PROM node containing all of the EEPROM options. */
61 static int options_node = 0;
64 * Copy an openpromio structure into kernel space from user space.
65 * This routine does error checking to make sure that all memory
66 * accesses are within bounds. A pointer to the allocated openpromio
67 * structure will be placed in "*opp_p". Return value is the length
68 * of the user supplied buffer.
70 static int copyin(struct openpromio *info, struct openpromio **opp_p)
77 if (get_user(bufsize, &info->oprom_size))
83 /* If the bufsize is too large, just limit it.
84 * Fix from Jason Rappleye.
86 if (bufsize > OPROMMAXPARAM)
87 bufsize = OPROMMAXPARAM;
89 if (!(*opp_p = kmalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
91 memset(*opp_p, 0, sizeof(int) + bufsize + 1);
93 if (copy_from_user(&(*opp_p)->oprom_array,
94 &info->oprom_array, bufsize)) {
101 static int getstrings(struct openpromio *info, struct openpromio **opp_p)
109 if (!(*opp_p = kmalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
112 memset(*opp_p, 0, sizeof(int) + OPROMMAXPARAM + 1);
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 *info, struct openpromio *opp, int len)
137 if (copy_to_user(info, opp, len))
143 * SunOS and Solaris /dev/openprom ioctl calls.
145 static int openprom_sunos_ioctl(struct inode * inode, struct file * file,
146 unsigned int cmd, unsigned long arg, int node)
148 DATA *data = (DATA *) file->private_data;
149 char buffer[OPROMMAXPARAM+1], *buf;
150 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);
166 len = prom_getproplen(node, opp->oprom_array);
168 if (len <= 0 || len > bufsize) {
169 error = copyout((void *)arg, opp, sizeof(int));
173 len = prom_getproperty(node, opp->oprom_array, buffer, bufsize);
175 memcpy(opp->oprom_array, buffer, len);
176 opp->oprom_array[len] = '\0';
177 opp->oprom_size = len;
179 error = copyout((void *)arg, opp, sizeof(int) + bufsize);
184 buf = prom_nextprop(node, opp->oprom_array, buffer);
187 if (len == 0 || len + 1 > bufsize) {
188 error = copyout((void *)arg, opp, sizeof(int));
192 memcpy(opp->oprom_array, buf, len);
193 opp->oprom_array[len] = '\0';
194 opp->oprom_size = ++len;
196 error = copyout((void *)arg, opp, sizeof(int) + bufsize);
201 buf = opp->oprom_array + strlen(opp->oprom_array) + 1;
202 len = opp->oprom_array + bufsize - buf;
204 error = prom_setprop(options_node, opp->oprom_array,
214 if (bufsize < sizeof(int)) {
219 node = *((int *) opp->oprom_array);
222 case OPROMNEXT: node = __prom_getsibling(node); break;
223 case OPROMCHILD: node = __prom_getchild(node); break;
224 case OPROMSETCUR: break;
227 data->current_node = node;
228 *((int *)opp->oprom_array) = node;
229 opp->oprom_size = sizeof(int);
231 error = copyout((void *)arg, opp, bufsize + sizeof(int));
237 if (bufsize >= 2*sizeof(int)) {
239 struct pci_dev *pdev;
240 struct pcidev_cookie *pcp;
241 pdev = pci_find_slot (((int *) opp->oprom_array)[0],
242 ((int *) opp->oprom_array)[1]);
245 if (pcp != NULL && pcp->prom_node != -1 && pcp->prom_node) {
246 node = pcp->prom_node;
247 data->current_node = node;
248 *((int *)opp->oprom_array) = node;
249 opp->oprom_size = sizeof(int);
250 error = copyout((void *)arg, opp, bufsize + sizeof(int));
257 node = prom_finddevice(opp->oprom_array);
258 data->current_node = node;
259 *((int *)opp->oprom_array) = node;
260 opp->oprom_size = sizeof(int);
262 error = copyout((void *)arg, opp, bufsize + sizeof(int));
265 case OPROMGETBOOTARGS:
266 buf = saved_command_line;
275 strcpy(opp->oprom_array, buf);
276 opp->oprom_size = len;
278 error = copyout((void *)arg, opp, bufsize + sizeof(int));
285 printk(KERN_INFO "openprom_sunos_ioctl: unimplemented ioctl\n");
290 printk(KERN_INFO "openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
300 /* Return nonzero if a specific node is in the PROM device tree. */
301 static int intree(int root, int node)
303 for (; root != 0; root = prom_getsibling(root))
304 if (root == node || intree(prom_getchild(root),node))
309 /* Return nonzero if a specific node is "valid". */
310 static int goodnode(int n, DATA *data)
312 if (n == data->lastnode || n == prom_root_node || n == options_node)
314 if (n == 0 || n == -1 || !intree(prom_root_node,n))
320 /* Copy in a whole string from userspace into kernelspace. */
321 static int copyin_string(char *user, size_t len, char **ptr)
325 if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
328 tmp = kmalloc(len + 1, GFP_KERNEL);
332 if(copy_from_user(tmp, user, len)) {
345 * NetBSD /dev/openprom ioctl calls.
347 static int openprom_bsd_ioctl(struct inode * inode, struct file * file,
348 unsigned int cmd, unsigned long arg)
350 DATA *data = (DATA *) file->private_data;
352 int error, node, len;
359 if (copy_from_user(&op, (void *)arg, sizeof(op)))
362 if (!goodnode(op.op_nodeid,data))
365 error = copyin_string(op.op_name, op.op_namelen, &str);
369 len = prom_getproplen(op.op_nodeid,str);
371 if (len > op.op_buflen) {
380 /* Verified by the above copy_from_user */
381 if (__copy_to_user((void *)arg, &op,
387 tmp = kmalloc(len + 1, GFP_KERNEL);
393 prom_getproperty(op.op_nodeid, str, tmp, len);
397 if (__copy_to_user((void *)arg, &op, sizeof(op)) != 0
398 || copy_to_user(op.op_buf, tmp, len) != 0)
407 if (copy_from_user(&op, (void *)arg, sizeof(op)))
410 if (!goodnode(op.op_nodeid,data))
413 error = copyin_string(op.op_name, op.op_namelen, &str);
417 tmp = prom_nextprop(op.op_nodeid,str,buffer);
421 if (len > op.op_buflen)
426 len = op.op_buflen = 0;
429 error = verify_area(VERIFY_WRITE, (void *)arg, sizeof(op));
435 error = verify_area(VERIFY_WRITE, op.op_buf, len);
441 error = __copy_to_user((void *)arg, &op, sizeof(op));
442 if (!error) error = __copy_to_user(op.op_buf, tmp, len);
449 if (copy_from_user(&op, (void *)arg, sizeof(op)))
452 if (!goodnode(op.op_nodeid,data))
455 error = copyin_string(op.op_name, op.op_namelen, &str);
459 error = copyin_string(op.op_buf, op.op_buflen, &tmp);
465 len = prom_setprop(op.op_nodeid,str,tmp,op.op_buflen+1);
467 if (len != op.op_buflen)
475 case OPIOCGETOPTNODE:
476 if (copy_to_user((void *)arg, &options_node, sizeof(int)))
482 if (copy_from_user(&node, (void *)arg, sizeof(int)))
485 if (cmd == OPIOCGETNEXT)
486 node = __prom_getsibling(node);
488 node = __prom_getchild(node);
490 if (__copy_to_user((void *)arg, &node, sizeof(int)))
497 printk(KERN_INFO "openprom_bsd_ioctl: cmd 0x%X\n", cmd);
505 * Handoff control to the correct ioctl handler.
507 static int openprom_ioctl(struct inode * inode, struct file * file,
508 unsigned int cmd, unsigned long arg)
510 DATA *data = (DATA *) file->private_data;
516 if ((file->f_mode & FMODE_READ) == 0)
518 return openprom_sunos_ioctl(inode, file, cmd, arg,
523 if ((file->f_mode & FMODE_WRITE) == 0)
525 return openprom_sunos_ioctl(inode, file, cmd, arg,
532 if ((file->f_mode & FMODE_READ) == 0)
534 return openprom_sunos_ioctl(inode, file, cmd, arg,
540 case OPROMGETBOOTARGS:
544 if ((file->f_mode & FMODE_READ) == 0)
546 return openprom_sunos_ioctl(inode, file, cmd, arg, 0);
550 case OPIOCGETOPTNODE:
553 if ((file->f_mode & FMODE_READ) == 0)
555 return openprom_bsd_ioctl(inode,file,cmd,arg);
558 if ((file->f_mode & FMODE_WRITE) == 0)
560 return openprom_bsd_ioctl(inode,file,cmd,arg);
564 printk("openprom_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
569 static int openprom_open(struct inode * inode, struct file * file)
573 data = (DATA *) kmalloc(sizeof(DATA), GFP_KERNEL);
577 data->current_node = prom_root_node;
578 data->lastnode = prom_root_node;
579 file->private_data = (void *)data;
584 static int openprom_release(struct inode * inode, struct file * file)
586 kfree(file->private_data);
590 static struct file_operations openprom_fops = {
591 .owner = THIS_MODULE,
593 .ioctl = openprom_ioctl,
594 .open = openprom_open,
595 .release = openprom_release,
598 static struct miscdevice openprom_dev = {
599 SUN_OPENPROM_MINOR, "openprom", &openprom_fops
602 static int __init openprom_init(void)
606 error = misc_register(&openprom_dev);
608 printk(KERN_ERR "openprom: unable to get misc minor\n");
612 options_node = prom_getchild(prom_root_node);
613 options_node = prom_searchsiblings(options_node,"options");
615 if (options_node == 0 || options_node == -1) {
616 printk(KERN_ERR "openprom: unable to find options node\n");
617 misc_deregister(&openprom_dev);
624 static void __exit openprom_cleanup(void)
626 misc_deregister(&openprom_dev);
629 module_init(openprom_init);
630 module_exit(openprom_cleanup);
631 MODULE_LICENSE("GPL");