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/mutex.h>
37 #include <linux/string.h>
38 #include <linux/miscdevice.h>
39 #include <linux/init.h>
41 #include <asm/oplib.h>
43 #include <linux/uaccess.h>
44 #include <asm/openpromio.h>
46 #include <linux/pci.h>
49 MODULE_AUTHOR("Thomas K. Dyas (tdyas@noc.rutgers.edu) and Eddie C. Dost (ecd@skynet.be)");
50 MODULE_DESCRIPTION("OPENPROM Configuration Driver");
51 MODULE_LICENSE("GPL");
52 MODULE_VERSION("1.0");
53 MODULE_ALIAS_MISCDEV(SUN_OPENPROM_MINOR);
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 DEFINE_MUTEX(openprom_mutex);
64 static struct device_node *options_node;
67 * Copy an openpromio structure into kernel space from user space.
68 * This routine does error checking to make sure that all memory
69 * accesses are within bounds. A pointer to the allocated openpromio
70 * structure will be placed in "*opp_p". Return value is the length
71 * of the user supplied buffer.
73 static int copyin(struct openpromio __user *info, struct openpromio **opp_p)
80 if (get_user(bufsize, &info->oprom_size))
86 /* If the bufsize is too large, just limit it.
87 * Fix from Jason Rappleye.
89 if (bufsize > OPROMMAXPARAM)
90 bufsize = OPROMMAXPARAM;
92 if (!(*opp_p = kzalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
95 if (copy_from_user(&(*opp_p)->oprom_array,
96 &info->oprom_array, bufsize)) {
103 static int getstrings(struct openpromio __user *info, struct openpromio **opp_p)
111 if (!(*opp_p = kzalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
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))
143 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 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;
166 return copyout(argp, op, sizeof(int));
167 if (op->oprom_array[0] == '\0') {
168 prop = dp->properties;
170 return copyout(argp, op, sizeof(int));
171 len = strlen(prop->name);
173 prop = of_find_property(dp, op->oprom_array, NULL);
177 (len = strlen(prop->next->name)) + 1 > bufsize)
178 return copyout(argp, op, sizeof(int));
183 memcpy(op->oprom_array, prop->name, len);
184 op->oprom_array[len] = '\0';
185 op->oprom_size = ++len;
187 return copyout(argp, op, sizeof(int) + bufsize);
190 static int opromsetopt(struct device_node *dp, struct openpromio *op, int bufsize)
192 char *buf = op->oprom_array + strlen(op->oprom_array) + 1;
193 int len = op->oprom_array + bufsize - buf;
195 return of_set_property(options_node, op->oprom_array, buf, len);
198 static int opromnext(void __user *argp, unsigned int cmd, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
202 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
204 if (bufsize < sizeof(phandle))
207 ph = *((int *) op->oprom_array);
209 dp = of_find_node_by_phandle(ph);
227 /* Sibling of node zero is the root node. */
228 if (cmd != OPROMNEXT)
231 dp = of_find_node_by_path("/");
238 data->current_node = dp;
239 *((int *) op->oprom_array) = ph;
240 op->oprom_size = sizeof(phandle);
242 return copyout(argp, op, bufsize + sizeof(int));
245 static int oprompci2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
249 if (bufsize >= 2*sizeof(int)) {
251 struct pci_dev *pdev;
252 struct device_node *dp;
254 pdev = pci_get_bus_and_slot (((int *) op->oprom_array)[0],
255 ((int *) op->oprom_array)[1]);
257 dp = pci_device_to_OF_node(pdev);
258 data->current_node = dp;
259 *((int *)op->oprom_array) = dp->phandle;
260 op->oprom_size = sizeof(int);
261 err = copyout(argp, op, bufsize + sizeof(int));
270 static int oprompath2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
274 dp = of_find_node_by_path(op->oprom_array);
277 data->current_node = dp;
278 *((int *)op->oprom_array) = ph;
279 op->oprom_size = sizeof(int);
281 return copyout(argp, op, bufsize + sizeof(int));
284 static int opromgetbootargs(void __user *argp, struct openpromio *op, int bufsize)
286 char *buf = saved_command_line;
287 int len = strlen(buf);
292 strcpy(op->oprom_array, buf);
293 op->oprom_size = len;
295 return copyout(argp, op, bufsize + sizeof(int));
299 * SunOS and Solaris /dev/openprom ioctl calls.
301 static long openprom_sunos_ioctl(struct file * file,
302 unsigned int cmd, unsigned long arg,
303 struct device_node *dp)
305 DATA *data = file->private_data;
306 struct openpromio *opp = NULL;
307 int bufsize, error = 0;
309 void __user *argp = (void __user *)arg;
311 if (cmd == OPROMSETOPT)
312 bufsize = getstrings(argp, &opp);
314 bufsize = copyin(argp, &opp);
319 mutex_lock(&openprom_mutex);
324 error = opromgetprop(argp, dp, opp, bufsize);
329 error = opromnxtprop(argp, dp, opp, bufsize);
334 error = opromsetopt(dp, opp, bufsize);
340 error = opromnext(argp, cmd, dp, opp, bufsize, data);
344 error = oprompci2node(argp, dp, opp, bufsize, data);
348 error = oprompath2node(argp, dp, opp, bufsize, data);
351 case OPROMGETBOOTARGS:
352 error = opromgetbootargs(argp, opp, bufsize);
359 printk(KERN_INFO "openprom_sunos_ioctl: unimplemented ioctl\n");
364 printk(KERN_INFO "openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
370 mutex_unlock(&openprom_mutex);
375 static struct device_node *get_node(phandle n, DATA *data)
377 struct device_node *dp = of_find_node_by_phandle(n);
385 /* Copy in a whole string from userspace into kernelspace. */
386 static char * copyin_string(char __user *user, size_t len)
388 if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
389 return ERR_PTR(-EINVAL);
391 return memdup_user_nul(user, len);
395 * NetBSD /dev/openprom ioctl calls.
397 static int opiocget(void __user *argp, DATA *data)
400 struct device_node *dp;
405 if (copy_from_user(&op, argp, sizeof(op)))
408 dp = get_node(op.op_nodeid, data);
410 str = copyin_string(op.op_name, op.op_namelen);
414 pval = of_get_property(dp, str, &len);
416 if (!pval || len > op.op_buflen) {
420 if (copy_to_user(argp, &op, sizeof(op)) ||
421 copy_to_user(op.op_buf, pval, len))
429 static int opiocnextprop(void __user *argp, DATA *data)
432 struct device_node *dp;
433 struct property *prop;
437 if (copy_from_user(&op, argp, sizeof(op)))
440 dp = get_node(op.op_nodeid, data);
444 str = copyin_string(op.op_name, op.op_namelen);
448 if (str[0] == '\0') {
449 prop = dp->properties;
451 prop = of_find_property(dp, str, NULL);
462 if (len > op.op_buflen)
465 if (copy_to_user(argp, &op, sizeof(op)))
469 copy_to_user(op.op_buf, prop->value, len))
475 static int opiocset(void __user *argp, DATA *data)
478 struct device_node *dp;
482 if (copy_from_user(&op, argp, sizeof(op)))
485 dp = get_node(op.op_nodeid, data);
489 str = copyin_string(op.op_name, op.op_namelen);
493 tmp = copyin_string(op.op_buf, op.op_buflen);
499 err = of_set_property(dp, str, tmp, op.op_buflen);
507 static int opiocgetnext(unsigned int cmd, void __user *argp)
509 struct device_node *dp;
512 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
514 if (copy_from_user(&nd, argp, sizeof(phandle)))
518 if (cmd != OPIOCGETNEXT)
520 dp = of_find_node_by_path("/");
522 dp = of_find_node_by_phandle(nd);
525 if (cmd == OPIOCGETNEXT)
533 if (copy_to_user(argp, &nd, sizeof(phandle)))
539 static int openprom_bsd_ioctl(struct file * file,
540 unsigned int cmd, unsigned long arg)
542 DATA *data = file->private_data;
543 void __user *argp = (void __user *)arg;
546 mutex_lock(&openprom_mutex);
549 err = opiocget(argp, data);
553 err = opiocnextprop(argp, data);
557 err = opiocset(argp, data);
560 case OPIOCGETOPTNODE:
561 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
564 if (copy_to_user(argp, &options_node->phandle, sizeof(phandle)))
570 err = opiocgetnext(cmd, argp);
577 mutex_unlock(&openprom_mutex);
584 * Handoff control to the correct ioctl handler.
586 static long openprom_ioctl(struct file * file,
587 unsigned int cmd, unsigned long arg)
589 DATA *data = file->private_data;
594 if ((file->f_mode & FMODE_READ) == 0)
596 return openprom_sunos_ioctl(file, cmd, arg,
601 if ((file->f_mode & FMODE_WRITE) == 0)
603 return openprom_sunos_ioctl(file, cmd, arg,
610 if ((file->f_mode & FMODE_READ) == 0)
612 return openprom_sunos_ioctl(file, cmd, arg,
618 case OPROMGETBOOTARGS:
622 if ((file->f_mode & FMODE_READ) == 0)
624 return openprom_sunos_ioctl(file, cmd, arg, NULL);
628 case OPIOCGETOPTNODE:
631 if ((file->f_mode & FMODE_READ) == 0)
633 return openprom_bsd_ioctl(file,cmd,arg);
636 if ((file->f_mode & FMODE_WRITE) == 0)
638 return openprom_bsd_ioctl(file,cmd,arg);
645 static long openprom_compat_ioctl(struct file *file, unsigned int cmd,
651 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
652 * the arg which we'd need to clean up...
666 case OPROMGETBOOTARGS:
670 rval = openprom_ioctl(file, cmd, arg);
677 static int openprom_open(struct inode * inode, struct file * file)
681 data = kmalloc(sizeof(DATA), GFP_KERNEL);
685 mutex_lock(&openprom_mutex);
686 data->current_node = of_find_node_by_path("/");
687 data->lastnode = data->current_node;
688 file->private_data = (void *) data;
689 mutex_unlock(&openprom_mutex);
694 static int openprom_release(struct inode * inode, struct file * file)
696 kfree(file->private_data);
700 static const struct file_operations openprom_fops = {
701 .owner = THIS_MODULE,
703 .unlocked_ioctl = openprom_ioctl,
704 .compat_ioctl = openprom_compat_ioctl,
705 .open = openprom_open,
706 .release = openprom_release,
709 static struct miscdevice openprom_dev = {
710 .minor = SUN_OPENPROM_MINOR,
712 .fops = &openprom_fops,
715 static int __init openprom_init(void)
717 struct device_node *dp;
720 err = misc_register(&openprom_dev);
724 dp = of_find_node_by_path("/");
727 if (!strcmp(dp->name, "options"))
734 misc_deregister(&openprom_dev);
741 static void __exit openprom_cleanup(void)
743 misc_deregister(&openprom_dev);
746 module_init(openprom_init);
747 module_exit(openprom_cleanup);