[PATCH] pgdat allocation for new node add (generic alloc node_data)
[linux-2.6/x86.git] / drivers / sbus / char / openprom.c
blobd7e4bb41bd79a5dcb8ffc21b849156a339654c6e
1 /*
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
14 * sanity's sake.
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>
41 #include <linux/fs.h>
42 #include <asm/oplib.h>
43 #include <asm/prom.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
46 #include <asm/openpromio.h>
47 #ifdef CONFIG_PCI
48 #include <linux/pci.h>
49 #include <asm/pbm.h>
50 #endif
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. */
62 } DATA;
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)
76 unsigned int bufsize;
78 if (!info || !opp_p)
79 return -EFAULT;
81 if (get_user(bufsize, &info->oprom_size))
82 return -EFAULT;
84 if (bufsize == 0)
85 return -EINVAL;
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)))
94 return -ENOMEM;
96 if (copy_from_user(&(*opp_p)->oprom_array,
97 &info->oprom_array, bufsize)) {
98 kfree(*opp_p);
99 return -EFAULT;
101 return bufsize;
104 static int getstrings(struct openpromio __user *info, struct openpromio **opp_p)
106 int n, bufsize;
107 char c;
109 if (!info || !opp_p)
110 return -EFAULT;
112 if (!(*opp_p = kzalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
113 return -ENOMEM;
115 (*opp_p)->oprom_size = 0;
117 n = bufsize = 0;
118 while ((n < 2) && (bufsize < OPROMMAXPARAM)) {
119 if (get_user(c, &info->oprom_array[bufsize])) {
120 kfree(*opp_p);
121 return -EFAULT;
123 if (c == '\0')
124 n++;
125 (*opp_p)->oprom_array[bufsize++] = c;
127 if (!n) {
128 kfree(*opp_p);
129 return -EINVAL;
131 return bufsize;
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))
140 return -EFAULT;
141 return 0;
144 static int opromgetprop(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize)
146 void *pval;
147 int len;
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;
163 int len;
165 if (op->oprom_array[0] == '\0') {
166 prop = dp->properties;
167 if (!prop)
168 return copyout(argp, op, sizeof(int));
169 len = strlen(prop->name);
170 } else {
171 prop = of_find_property(dp, op->oprom_array, NULL);
173 if (!prop ||
174 !prop->next ||
175 (len = strlen(prop->next->name)) + 1 > bufsize)
176 return copyout(argp, op, sizeof(int));
178 prop = prop->next;
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)
198 phandle ph;
200 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
202 if (bufsize < sizeof(phandle))
203 return -EINVAL;
205 ph = *((int *) op->oprom_array);
206 if (ph) {
207 dp = of_find_node_by_phandle(ph);
208 if (!dp)
209 return -EINVAL;
211 switch (cmd) {
212 case OPROMNEXT:
213 dp = dp->sibling;
214 break;
216 case OPROMCHILD:
217 dp = dp->child;
218 break;
220 case OPROMSETCUR:
221 default:
222 break;
224 } else {
225 /* Sibling of node zero is the root node. */
226 if (cmd != OPROMNEXT)
227 return -EINVAL;
229 dp = of_find_node_by_path("/");
232 ph = 0;
233 if (dp)
234 ph = dp->node;
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)
245 int err = -EINVAL;
247 if (bufsize >= 2*sizeof(int)) {
248 #ifdef CONFIG_PCI
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]);
254 pcp = pdev->sysdata;
255 if (pcp != NULL) {
256 dp = pcp->prom_node;
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));
262 #endif
265 return err;
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);
283 if (len > bufsize)
284 return -EINVAL;
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;
302 static int cnt;
303 void __user *argp = (void __user *)arg;
305 if (cmd == OPROMSETOPT)
306 bufsize = getstrings(argp, &opp);
307 else
308 bufsize = copyin(argp, &opp);
310 if (bufsize < 0)
311 return bufsize;
313 switch (cmd) {
314 case OPROMGETOPT:
315 case OPROMGETPROP:
316 error = opromgetprop(argp, dp, opp, bufsize);
317 break;
319 case OPROMNXTOPT:
320 case OPROMNXTPROP:
321 error = opromnxtprop(argp, dp, opp, bufsize);
322 break;
324 case OPROMSETOPT:
325 case OPROMSETOPT2:
326 error = opromsetopt(dp, opp, bufsize);
327 break;
329 case OPROMNEXT:
330 case OPROMCHILD:
331 case OPROMSETCUR:
332 error = opromnext(argp, cmd, dp, opp, bufsize, data);
333 break;
335 case OPROMPCI2NODE:
336 error = oprompci2node(argp, dp, opp, bufsize, data);
337 break;
339 case OPROMPATH2NODE:
340 error = oprompath2node(argp, dp, opp, bufsize, data);
341 break;
343 case OPROMGETBOOTARGS:
344 error = opromgetbootargs(argp, opp, bufsize);
345 break;
347 case OPROMU2P:
348 case OPROMGETCONS:
349 case OPROMGETFBNAME:
350 if (cnt++ < 10)
351 printk(KERN_INFO "openprom_sunos_ioctl: unimplemented ioctl\n");
352 error = -EINVAL;
353 break;
354 default:
355 if (cnt++ < 10)
356 printk(KERN_INFO "openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
357 error = -EINVAL;
358 break;
361 kfree(opp);
362 return error;
365 static struct device_node *get_node(phandle n, DATA *data)
367 struct device_node *dp = of_find_node_by_phandle(n);
369 if (dp)
370 data->lastnode = dp;
372 return dp;
375 /* Copy in a whole string from userspace into kernelspace. */
376 static int copyin_string(char __user *user, size_t len, char **ptr)
378 char *tmp;
380 if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
381 return -EINVAL;
383 tmp = kmalloc(len + 1, GFP_KERNEL);
384 if (!tmp)
385 return -ENOMEM;
387 if (copy_from_user(tmp, user, len)) {
388 kfree(tmp);
389 return -EFAULT;
392 tmp[len] = '\0';
394 *ptr = tmp;
396 return 0;
400 * NetBSD /dev/openprom ioctl calls.
402 static int opiocget(void __user *argp, DATA *data)
404 struct opiocdesc op;
405 struct device_node *dp;
406 char *str;
407 void *pval;
408 int err, len;
410 if (copy_from_user(&op, argp, sizeof(op)))
411 return -EFAULT;
413 dp = get_node(op.op_nodeid, data);
415 err = copyin_string(op.op_name, op.op_namelen, &str);
416 if (err)
417 return err;
419 pval = of_get_property(dp, str, &len);
420 err = 0;
421 if (!pval || len > op.op_buflen) {
422 err = -EINVAL;
423 } else {
424 op.op_buflen = len;
425 if (copy_to_user(argp, &op, sizeof(op)) ||
426 copy_to_user(op.op_buf, pval, len))
427 err = -EFAULT;
429 kfree(str);
431 return err;
434 static int opiocnextprop(void __user *argp, DATA *data)
436 struct opiocdesc op;
437 struct device_node *dp;
438 struct property *prop;
439 char *str;
440 int err, len;
442 if (copy_from_user(&op, argp, sizeof(op)))
443 return -EFAULT;
445 dp = get_node(op.op_nodeid, data);
446 if (!dp)
447 return -EINVAL;
449 err = copyin_string(op.op_name, op.op_namelen, &str);
450 if (err)
451 return err;
453 if (str[0] == '\0') {
454 prop = dp->properties;
455 } else {
456 prop = of_find_property(dp, str, NULL);
457 if (prop)
458 prop = prop->next;
460 kfree(str);
462 if (!prop)
463 len = 0;
464 else
465 len = prop->length;
467 if (len > op.op_buflen)
468 len = op.op_buflen;
470 if (copy_to_user(argp, &op, sizeof(op)))
471 return -EFAULT;
473 if (len &&
474 copy_to_user(op.op_buf, prop->value, len))
475 return -EFAULT;
477 return 0;
480 static int opiocset(void __user *argp, DATA *data)
482 struct opiocdesc op;
483 struct device_node *dp;
484 char *str, *tmp;
485 int err;
487 if (copy_from_user(&op, argp, sizeof(op)))
488 return -EFAULT;
490 dp = get_node(op.op_nodeid, data);
491 if (!dp)
492 return -EINVAL;
494 err = copyin_string(op.op_name, op.op_namelen, &str);
495 if (err)
496 return err;
498 err = copyin_string(op.op_buf, op.op_buflen, &tmp);
499 if (err) {
500 kfree(str);
501 return err;
504 err = of_set_property(dp, str, tmp, op.op_buflen);
506 kfree(str);
507 kfree(tmp);
509 return err;
512 static int opiocgetnext(unsigned int cmd, void __user *argp)
514 struct device_node *dp;
515 phandle nd;
517 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
519 if (copy_from_user(&nd, argp, sizeof(phandle)))
520 return -EFAULT;
522 if (nd == 0) {
523 if (cmd != OPIOCGETNEXT)
524 return -EINVAL;
525 dp = of_find_node_by_path("/");
526 } else {
527 dp = of_find_node_by_phandle(nd);
528 nd = 0;
529 if (dp) {
530 if (cmd == OPIOCGETNEXT)
531 dp = dp->sibling;
532 else
533 dp = dp->child;
536 if (dp)
537 nd = dp->node;
538 if (copy_to_user(argp, &nd, sizeof(phandle)))
539 return -EFAULT;
541 return 0;
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;
549 int err;
551 switch (cmd) {
552 case OPIOCGET:
553 err = opiocget(argp, data);
554 break;
556 case OPIOCNEXTPROP:
557 err = opiocnextprop(argp, data);
558 break;
560 case OPIOCSET:
561 err = opiocset(argp, data);
562 break;
564 case OPIOCGETOPTNODE:
565 BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
567 if (copy_to_user(argp, &options_node->node, sizeof(phandle)))
568 return -EFAULT;
570 return 0;
572 case OPIOCGETNEXT:
573 case OPIOCGETCHILD:
574 err = opiocgetnext(cmd, argp);
575 break;
577 default:
578 return -EINVAL;
582 return err;
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;
594 switch (cmd) {
595 case OPROMGETOPT:
596 case OPROMNXTOPT:
597 if ((file->f_mode & FMODE_READ) == 0)
598 return -EPERM;
599 return openprom_sunos_ioctl(inode, file, cmd, arg,
600 options_node);
602 case OPROMSETOPT:
603 case OPROMSETOPT2:
604 if ((file->f_mode & FMODE_WRITE) == 0)
605 return -EPERM;
606 return openprom_sunos_ioctl(inode, file, cmd, arg,
607 options_node);
609 case OPROMNEXT:
610 case OPROMCHILD:
611 case OPROMGETPROP:
612 case OPROMNXTPROP:
613 if ((file->f_mode & FMODE_READ) == 0)
614 return -EPERM;
615 return openprom_sunos_ioctl(inode, file, cmd, arg,
616 data->current_node);
618 case OPROMU2P:
619 case OPROMGETCONS:
620 case OPROMGETFBNAME:
621 case OPROMGETBOOTARGS:
622 case OPROMSETCUR:
623 case OPROMPCI2NODE:
624 case OPROMPATH2NODE:
625 if ((file->f_mode & FMODE_READ) == 0)
626 return -EPERM;
627 return openprom_sunos_ioctl(inode, file, cmd, arg, 0);
629 case OPIOCGET:
630 case OPIOCNEXTPROP:
631 case OPIOCGETOPTNODE:
632 case OPIOCGETNEXT:
633 case OPIOCGETCHILD:
634 if ((file->f_mode & FMODE_READ) == 0)
635 return -EBADF;
636 return openprom_bsd_ioctl(inode,file,cmd,arg);
638 case OPIOCSET:
639 if ((file->f_mode & FMODE_WRITE) == 0)
640 return -EBADF;
641 return openprom_bsd_ioctl(inode,file,cmd,arg);
643 default:
644 return -EINVAL;
648 static long openprom_compat_ioctl(struct file *file, unsigned int cmd,
649 unsigned long arg)
651 long rval = -ENOTTY;
654 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
655 * the arg which we'd need to clean up...
657 switch (cmd) {
658 case OPROMGETOPT:
659 case OPROMSETOPT:
660 case OPROMNXTOPT:
661 case OPROMSETOPT2:
662 case OPROMNEXT:
663 case OPROMCHILD:
664 case OPROMGETPROP:
665 case OPROMNXTPROP:
666 case OPROMU2P:
667 case OPROMGETCONS:
668 case OPROMGETFBNAME:
669 case OPROMGETBOOTARGS:
670 case OPROMSETCUR:
671 case OPROMPCI2NODE:
672 case OPROMPATH2NODE:
673 rval = openprom_ioctl(file->f_dentry->d_inode, file, cmd, arg);
674 break;
677 return rval;
680 static int openprom_open(struct inode * inode, struct file * file)
682 DATA *data;
684 data = kmalloc(sizeof(DATA), GFP_KERNEL);
685 if (!data)
686 return -ENOMEM;
688 data->current_node = of_find_node_by_path("/");
689 data->lastnode = data->current_node;
690 file->private_data = (void *) data;
692 return 0;
695 static int openprom_release(struct inode * inode, struct file * file)
697 kfree(file->private_data);
698 return 0;
701 static struct file_operations openprom_fops = {
702 .owner = THIS_MODULE,
703 .llseek = no_llseek,
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,
712 .name = "openprom",
713 .fops = &openprom_fops,
716 static int __init openprom_init(void)
718 struct device_node *dp;
719 int err;
721 err = misc_register(&openprom_dev);
722 if (err)
723 return err;
725 dp = of_find_node_by_path("/");
726 dp = dp->child;
727 while (dp) {
728 if (!strcmp(dp->name, "options"))
729 break;
730 dp = dp->sibling;
732 options_node = dp;
734 if (!options_node) {
735 misc_deregister(&openprom_dev);
736 return -EIO;
739 return 0;
742 static void __exit openprom_cleanup(void)
744 misc_deregister(&openprom_dev);
747 module_init(openprom_init);
748 module_exit(openprom_cleanup);