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/config.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/errno.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/miscdevice.h>
40 #include <linux/init.h>
42 #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 MODULE_AUTHOR("Thomas K. Dyas (tdyas@noc.rutgers.edu) and Eddie C. Dost (ecd@skynet.be)");
53 MODULE_DESCRIPTION("OPENPROM Configuration Driver");
54 MODULE_LICENSE("GPL");
55 MODULE_VERSION("1.0");
57 /* Private data kept by the driver for each descriptor. */
58 typedef struct openprom_private_data
60 struct device_node *current_node; /* Current node for SunOS ioctls. */
61 struct device_node *lastnode; /* Last valid node used by BSD ioctls. */
64 /* ID of the PROM node containing all of the EEPROM options. */
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)
149 pval = of_get_property(dp, op->oprom_array, &len);
150 if (!pval || len <= 0 || len > bufsize)
151 return copyout(argp, op, sizeof(int));
153 memcpy(op->oprom_array, pval, len);
154 op->oprom_array[len] = '\0';
155 op->oprom_size = len;
157 return copyout(argp, op, sizeof(int) + bufsize);
160 static int opromnxtprop(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize)
162 struct property *prop;
165 if (op->oprom_array[0] == '\0') {
166 prop = dp->properties;
168 return copyout(argp, op, sizeof(int));
169 len = strlen(prop->name);
171 prop = of_find_property(dp, op->oprom_array, NULL);
175 (len = strlen(prop->next->name)) + 1 > bufsize)
176 return copyout(argp, op, sizeof(int));
181 memcpy(op->oprom_array, prop->name, len);
182 op->oprom_array[len] = '\0';
183 op->oprom_size = ++len;
185 return copyout(argp, op, sizeof(int) + bufsize);
188 static int opromsetopt(struct device_node *dp, struct openpromio *op, int bufsize)
190 char *buf = op->oprom_array + strlen(op->oprom_array) + 1;
191 int len = op->oprom_array + bufsize - buf;
193 return of_set_property(options_node, op->oprom_array, buf, len);
196 static int opromnext(void __user *argp, unsigned int cmd, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
200 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
202 if (bufsize < sizeof(phandle))
205 ph = *((int *) op->oprom_array);
207 dp = of_find_node_by_phandle(ph);
225 /* Sibling of node zero is the root node. */
226 if (cmd != OPROMNEXT)
229 dp = of_find_node_by_path("/");
236 data->current_node = dp;
237 *((int *) op->oprom_array) = ph;
238 op->oprom_size = sizeof(phandle);
240 return copyout(argp, op, bufsize + sizeof(int));
243 static int oprompci2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
247 if (bufsize >= 2*sizeof(int)) {
249 struct pci_dev *pdev;
250 struct pcidev_cookie *pcp;
251 pdev = pci_find_slot (((int *) op->oprom_array)[0],
252 ((int *) op->oprom_array)[1]);
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));
268 static int oprompath2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
270 dp = of_find_node_by_path(op->oprom_array);
271 data->current_node = dp;
272 *((int *)op->oprom_array) = dp->node;
273 op->oprom_size = sizeof(int);
275 return copyout(argp, op, bufsize + sizeof(int));
278 static int opromgetbootargs(void __user *argp, struct openpromio *op, int bufsize)
280 char *buf = saved_command_line;
281 int len = strlen(buf);
286 strcpy(op->oprom_array, buf);
287 op->oprom_size = len;
289 return copyout(argp, op, bufsize + sizeof(int));
293 * SunOS and Solaris /dev/openprom ioctl calls.
295 static int openprom_sunos_ioctl(struct inode * inode, struct file * file,
296 unsigned int cmd, unsigned long arg,
297 struct device_node *dp)
299 DATA *data = file->private_data;
300 struct openpromio *opp;
301 int bufsize, error = 0;
303 void __user *argp = (void __user *)arg;
305 if (cmd == OPROMSETOPT)
306 bufsize = getstrings(argp, &opp);
308 bufsize = copyin(argp, &opp);
316 error = opromgetprop(argp, dp, opp, bufsize);
321 error = opromnxtprop(argp, dp, opp, bufsize);
326 error = opromsetopt(dp, opp, bufsize);
332 error = opromnext(argp, cmd, dp, opp, bufsize, data);
336 error = oprompci2node(argp, dp, opp, bufsize, data);
340 error = oprompath2node(argp, dp, opp, bufsize, data);
343 case OPROMGETBOOTARGS:
344 error = opromgetbootargs(argp, opp, bufsize);
351 printk(KERN_INFO "openprom_sunos_ioctl: unimplemented ioctl\n");
356 printk(KERN_INFO "openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
365 static struct device_node *get_node(phandle n, DATA *data)
367 struct device_node *dp = of_find_node_by_phandle(n);
375 /* Copy in a whole string from userspace into kernelspace. */
376 static int copyin_string(char __user *user, size_t len, char **ptr)
380 if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
383 tmp = kmalloc(len + 1, GFP_KERNEL);
387 if (copy_from_user(tmp, user, len)) {
400 * NetBSD /dev/openprom ioctl calls.
402 static int opiocget(void __user *argp, DATA *data)
405 struct device_node *dp;
410 if (copy_from_user(&op, argp, sizeof(op)))
413 dp = get_node(op.op_nodeid, data);
415 err = copyin_string(op.op_name, op.op_namelen, &str);
419 pval = of_get_property(dp, str, &len);
421 if (!pval || len > op.op_buflen) {
425 if (copy_to_user(argp, &op, sizeof(op)) ||
426 copy_to_user(op.op_buf, pval, len))
434 static int opiocnextprop(void __user *argp, DATA *data)
437 struct device_node *dp;
438 struct property *prop;
442 if (copy_from_user(&op, argp, sizeof(op)))
445 dp = get_node(op.op_nodeid, data);
449 err = copyin_string(op.op_name, op.op_namelen, &str);
453 if (str[0] == '\0') {
454 prop = dp->properties;
456 prop = of_find_property(dp, str, NULL);
467 if (len > op.op_buflen)
470 if (copy_to_user(argp, &op, sizeof(op)))
474 copy_to_user(op.op_buf, prop->value, len))
480 static int opiocset(void __user *argp, DATA *data)
483 struct device_node *dp;
487 if (copy_from_user(&op, argp, sizeof(op)))
490 dp = get_node(op.op_nodeid, data);
494 err = copyin_string(op.op_name, op.op_namelen, &str);
498 err = copyin_string(op.op_buf, op.op_buflen, &tmp);
504 err = of_set_property(dp, str, tmp, op.op_buflen);
512 static int opiocgetnext(unsigned int cmd, void __user *argp)
514 struct device_node *dp;
517 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
519 if (copy_from_user(&nd, argp, sizeof(phandle)))
523 if (cmd != OPIOCGETNEXT)
525 dp = of_find_node_by_path("/");
527 dp = of_find_node_by_phandle(nd);
530 if (cmd == OPIOCGETNEXT)
538 if (copy_to_user(argp, &nd, sizeof(phandle)))
544 static int openprom_bsd_ioctl(struct inode * inode, struct file * file,
545 unsigned int cmd, unsigned long arg)
547 DATA *data = (DATA *) file->private_data;
548 void __user *argp = (void __user *)arg;
553 err = opiocget(argp, data);
557 err = opiocnextprop(argp, data);
561 err = opiocset(argp, data);
564 case OPIOCGETOPTNODE:
565 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
567 if (copy_to_user(argp, &options_node->node, sizeof(phandle)))
574 err = opiocgetnext(cmd, argp);
587 * Handoff control to the correct ioctl handler.
589 static int openprom_ioctl(struct inode * inode, struct file * file,
590 unsigned int cmd, unsigned long arg)
592 DATA *data = (DATA *) file->private_data;
597 if ((file->f_mode & FMODE_READ) == 0)
599 return openprom_sunos_ioctl(inode, file, cmd, arg,
604 if ((file->f_mode & FMODE_WRITE) == 0)
606 return openprom_sunos_ioctl(inode, file, cmd, arg,
613 if ((file->f_mode & FMODE_READ) == 0)
615 return openprom_sunos_ioctl(inode, file, cmd, arg,
621 case OPROMGETBOOTARGS:
625 if ((file->f_mode & FMODE_READ) == 0)
627 return openprom_sunos_ioctl(inode, file, cmd, arg, 0);
631 case OPIOCGETOPTNODE:
634 if ((file->f_mode & FMODE_READ) == 0)
636 return openprom_bsd_ioctl(inode,file,cmd,arg);
639 if ((file->f_mode & FMODE_WRITE) == 0)
641 return openprom_bsd_ioctl(inode,file,cmd,arg);
648 static long openprom_compat_ioctl(struct file *file, unsigned int cmd,
654 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
655 * the arg which we'd need to clean up...
669 case OPROMGETBOOTARGS:
673 rval = openprom_ioctl(file->f_dentry->d_inode, file, cmd, arg);
680 static int openprom_open(struct inode * inode, struct file * file)
684 data = kmalloc(sizeof(DATA), GFP_KERNEL);
688 data->current_node = of_find_node_by_path("/");
689 data->lastnode = data->current_node;
690 file->private_data = (void *) data;
695 static int openprom_release(struct inode * inode, struct file * file)
697 kfree(file->private_data);
701 static struct file_operations openprom_fops = {
702 .owner = THIS_MODULE,
704 .ioctl = openprom_ioctl,
705 .compat_ioctl = openprom_compat_ioctl,
706 .open = openprom_open,
707 .release = openprom_release,
710 static struct miscdevice openprom_dev = {
711 .minor = SUN_OPENPROM_MINOR,
713 .fops = &openprom_fops,
716 static int __init openprom_init(void)
718 struct device_node *dp;
721 err = misc_register(&openprom_dev);
725 dp = of_find_node_by_path("/");
728 if (!strcmp(dp->name, "options"))
735 misc_deregister(&openprom_dev);
742 static void __exit openprom_cleanup(void)
744 misc_deregister(&openprom_dev);
747 module_init(openprom_init);
748 module_exit(openprom_cleanup);