Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / sbus / char / openprom.c
blob239e108b8ed1e16cb45d51fa89591418a5410bac
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 #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/smp_lock.h>
43 #include <linux/init.h>
44 #include <linux/fs.h>
45 #include <asm/oplib.h>
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
48 #include <asm/openpromio.h>
49 #ifdef CONFIG_PCI
50 #include <linux/pci.h>
51 #include <asm/pbm.h>
52 #endif
54 /* Private data kept by the driver for each descriptor. */
55 typedef struct openprom_private_data
57 int current_node; /* Current node for SunOS ioctls. */
58 int lastnode; /* Last valid node used by BSD ioctls. */
59 } DATA;
61 /* ID of the PROM node containing all of the EEPROM options. */
62 static int options_node = 0;
65 * Copy an openpromio structure into kernel space from user space.
66 * This routine does error checking to make sure that all memory
67 * accesses are within bounds. A pointer to the allocated openpromio
68 * structure will be placed in "*opp_p". Return value is the length
69 * of the user supplied buffer.
71 static int copyin(struct openpromio __user *info, struct openpromio **opp_p)
73 unsigned int bufsize;
75 if (!info || !opp_p)
76 return -EFAULT;
78 if (get_user(bufsize, &info->oprom_size))
79 return -EFAULT;
81 if (bufsize == 0)
82 return -EINVAL;
84 /* If the bufsize is too large, just limit it.
85 * Fix from Jason Rappleye.
87 if (bufsize > OPROMMAXPARAM)
88 bufsize = OPROMMAXPARAM;
90 if (!(*opp_p = kmalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
91 return -ENOMEM;
92 memset(*opp_p, 0, sizeof(int) + bufsize + 1);
94 if (copy_from_user(&(*opp_p)->oprom_array,
95 &info->oprom_array, bufsize)) {
96 kfree(*opp_p);
97 return -EFAULT;
99 return bufsize;
102 static int getstrings(struct openpromio __user *info, struct openpromio **opp_p)
104 int n, bufsize;
105 char c;
107 if (!info || !opp_p)
108 return -EFAULT;
110 if (!(*opp_p = kmalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
111 return -ENOMEM;
113 memset(*opp_p, 0, sizeof(int) + OPROMMAXPARAM + 1);
114 (*opp_p)->oprom_size = 0;
116 n = bufsize = 0;
117 while ((n < 2) && (bufsize < OPROMMAXPARAM)) {
118 if (get_user(c, &info->oprom_array[bufsize])) {
119 kfree(*opp_p);
120 return -EFAULT;
122 if (c == '\0')
123 n++;
124 (*opp_p)->oprom_array[bufsize++] = c;
126 if (!n) {
127 kfree(*opp_p);
128 return -EINVAL;
130 return bufsize;
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))
139 return -EFAULT;
140 return 0;
144 * SunOS and Solaris /dev/openprom ioctl calls.
146 static int openprom_sunos_ioctl(struct inode * inode, struct file * file,
147 unsigned int cmd, unsigned long arg, int node)
149 DATA *data = (DATA *) file->private_data;
150 char buffer[OPROMMAXPARAM+1], *buf;
151 struct openpromio *opp;
152 int bufsize, len, error = 0;
153 static int cnt;
154 void __user *argp = (void __user *)arg;
156 if (cmd == OPROMSETOPT)
157 bufsize = getstrings(argp, &opp);
158 else
159 bufsize = copyin(argp, &opp);
161 if (bufsize < 0)
162 return bufsize;
164 switch (cmd) {
165 case OPROMGETOPT:
166 case OPROMGETPROP:
167 len = prom_getproplen(node, opp->oprom_array);
169 if (len <= 0 || len > bufsize) {
170 error = copyout(argp, opp, sizeof(int));
171 break;
174 len = prom_getproperty(node, opp->oprom_array, buffer, bufsize);
176 memcpy(opp->oprom_array, buffer, len);
177 opp->oprom_array[len] = '\0';
178 opp->oprom_size = len;
180 error = copyout(argp, opp, sizeof(int) + bufsize);
181 break;
183 case OPROMNXTOPT:
184 case OPROMNXTPROP:
185 buf = prom_nextprop(node, opp->oprom_array, buffer);
187 len = strlen(buf);
188 if (len == 0 || len + 1 > bufsize) {
189 error = copyout(argp, opp, sizeof(int));
190 break;
193 memcpy(opp->oprom_array, buf, len);
194 opp->oprom_array[len] = '\0';
195 opp->oprom_size = ++len;
197 error = copyout(argp, opp, sizeof(int) + bufsize);
198 break;
200 case OPROMSETOPT:
201 case OPROMSETOPT2:
202 buf = opp->oprom_array + strlen(opp->oprom_array) + 1;
203 len = opp->oprom_array + bufsize - buf;
205 error = prom_setprop(options_node, opp->oprom_array,
206 buf, len);
208 if (error < 0)
209 error = -EINVAL;
210 break;
212 case OPROMNEXT:
213 case OPROMCHILD:
214 case OPROMSETCUR:
215 if (bufsize < sizeof(int)) {
216 error = -EINVAL;
217 break;
220 node = *((int *) opp->oprom_array);
222 switch (cmd) {
223 case OPROMNEXT: node = __prom_getsibling(node); break;
224 case OPROMCHILD: node = __prom_getchild(node); break;
225 case OPROMSETCUR: break;
228 data->current_node = node;
229 *((int *)opp->oprom_array) = node;
230 opp->oprom_size = sizeof(int);
232 error = copyout(argp, opp, bufsize + sizeof(int));
233 break;
235 case OPROMPCI2NODE:
236 error = -EINVAL;
238 if (bufsize >= 2*sizeof(int)) {
239 #ifdef CONFIG_PCI
240 struct pci_dev *pdev;
241 struct pcidev_cookie *pcp;
242 pdev = pci_find_slot (((int *) opp->oprom_array)[0],
243 ((int *) opp->oprom_array)[1]);
245 pcp = pdev->sysdata;
246 if (pcp != NULL && pcp->prom_node != -1 && pcp->prom_node) {
247 node = pcp->prom_node;
248 data->current_node = node;
249 *((int *)opp->oprom_array) = node;
250 opp->oprom_size = sizeof(int);
251 error = copyout(argp, opp, bufsize + sizeof(int));
253 #endif
255 break;
257 case OPROMPATH2NODE:
258 node = prom_finddevice(opp->oprom_array);
259 data->current_node = node;
260 *((int *)opp->oprom_array) = node;
261 opp->oprom_size = sizeof(int);
263 error = copyout(argp, opp, bufsize + sizeof(int));
264 break;
266 case OPROMGETBOOTARGS:
267 buf = saved_command_line;
269 len = strlen(buf);
271 if (len > bufsize) {
272 error = -EINVAL;
273 break;
276 strcpy(opp->oprom_array, buf);
277 opp->oprom_size = len;
279 error = copyout(argp, opp, bufsize + sizeof(int));
280 break;
282 case OPROMU2P:
283 case OPROMGETCONS:
284 case OPROMGETFBNAME:
285 if (cnt++ < 10)
286 printk(KERN_INFO "openprom_sunos_ioctl: unimplemented ioctl\n");
287 error = -EINVAL;
288 break;
289 default:
290 if (cnt++ < 10)
291 printk(KERN_INFO "openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
292 error = -EINVAL;
293 break;
296 kfree(opp);
297 return error;
301 /* Return nonzero if a specific node is in the PROM device tree. */
302 static int intree(int root, int node)
304 for (; root != 0; root = prom_getsibling(root))
305 if (root == node || intree(prom_getchild(root),node))
306 return 1;
307 return 0;
310 /* Return nonzero if a specific node is "valid". */
311 static int goodnode(int n, DATA *data)
313 if (n == data->lastnode || n == prom_root_node || n == options_node)
314 return 1;
315 if (n == 0 || n == -1 || !intree(prom_root_node,n))
316 return 0;
317 data->lastnode = n;
318 return 1;
321 /* Copy in a whole string from userspace into kernelspace. */
322 static int copyin_string(char __user *user, size_t len, char **ptr)
324 char *tmp;
326 if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
327 return -EINVAL;
329 tmp = kmalloc(len + 1, GFP_KERNEL);
330 if (!tmp)
331 return -ENOMEM;
333 if(copy_from_user(tmp, user, len)) {
334 kfree(tmp);
335 return -EFAULT;
338 tmp[len] = '\0';
340 *ptr = tmp;
342 return 0;
346 * NetBSD /dev/openprom ioctl calls.
348 static int openprom_bsd_ioctl(struct inode * inode, struct file * file,
349 unsigned int cmd, unsigned long arg)
351 DATA *data = (DATA *) file->private_data;
352 void __user *argp = (void __user *)arg;
353 struct opiocdesc op;
354 int error, node, len;
355 char *str, *tmp;
356 char buffer[64];
357 static int cnt;
359 switch (cmd) {
360 case OPIOCGET:
361 if (copy_from_user(&op, argp, sizeof(op)))
362 return -EFAULT;
364 if (!goodnode(op.op_nodeid,data))
365 return -EINVAL;
367 error = copyin_string(op.op_name, op.op_namelen, &str);
368 if (error)
369 return error;
371 len = prom_getproplen(op.op_nodeid,str);
373 if (len > op.op_buflen) {
374 kfree(str);
375 return -ENOMEM;
378 op.op_buflen = len;
380 if (len <= 0) {
381 kfree(str);
382 /* Verified by the above copy_from_user */
383 if (__copy_to_user(argp, &op,
384 sizeof(op)))
385 return -EFAULT;
386 return 0;
389 tmp = kmalloc(len + 1, GFP_KERNEL);
390 if (!tmp) {
391 kfree(str);
392 return -ENOMEM;
395 cnt = prom_getproperty(op.op_nodeid, str, tmp, len);
396 if (cnt <= 0) {
397 error = -EINVAL;
398 } else {
399 tmp[len] = '\0';
401 if (__copy_to_user(argp, &op, sizeof(op)) != 0 ||
402 copy_to_user(op.op_buf, tmp, len) != 0)
403 error = -EFAULT;
406 kfree(tmp);
407 kfree(str);
409 return error;
411 case OPIOCNEXTPROP:
412 if (copy_from_user(&op, argp, sizeof(op)))
413 return -EFAULT;
415 if (!goodnode(op.op_nodeid,data))
416 return -EINVAL;
418 error = copyin_string(op.op_name, op.op_namelen, &str);
419 if (error)
420 return error;
422 tmp = prom_nextprop(op.op_nodeid,str,buffer);
424 if (tmp) {
425 len = strlen(tmp);
426 if (len > op.op_buflen)
427 len = op.op_buflen;
428 else
429 op.op_buflen = len;
430 } else {
431 len = op.op_buflen = 0;
434 if (!access_ok(VERIFY_WRITE, argp, sizeof(op))) {
435 kfree(str);
436 return -EFAULT;
439 if (!access_ok(VERIFY_WRITE, op.op_buf, len)) {
440 kfree(str);
441 return -EFAULT;
444 error = __copy_to_user(argp, &op, sizeof(op));
445 if (!error) error = __copy_to_user(op.op_buf, tmp, len);
447 kfree(str);
449 return error;
451 case OPIOCSET:
452 if (copy_from_user(&op, argp, sizeof(op)))
453 return -EFAULT;
455 if (!goodnode(op.op_nodeid,data))
456 return -EINVAL;
458 error = copyin_string(op.op_name, op.op_namelen, &str);
459 if (error)
460 return error;
462 error = copyin_string(op.op_buf, op.op_buflen, &tmp);
463 if (error) {
464 kfree(str);
465 return error;
468 len = prom_setprop(op.op_nodeid,str,tmp,op.op_buflen+1);
470 if (len != op.op_buflen)
471 return -EINVAL;
473 kfree(str);
474 kfree(tmp);
476 return 0;
478 case OPIOCGETOPTNODE:
479 if (copy_to_user(argp, &options_node, sizeof(int)))
480 return -EFAULT;
481 return 0;
483 case OPIOCGETNEXT:
484 case OPIOCGETCHILD:
485 if (copy_from_user(&node, argp, sizeof(int)))
486 return -EFAULT;
488 if (cmd == OPIOCGETNEXT)
489 node = __prom_getsibling(node);
490 else
491 node = __prom_getchild(node);
493 if (__copy_to_user(argp, &node, sizeof(int)))
494 return -EFAULT;
496 return 0;
498 default:
499 if (cnt++ < 10)
500 printk(KERN_INFO "openprom_bsd_ioctl: cmd 0x%X\n", cmd);
501 return -EINVAL;
508 * Handoff control to the correct ioctl handler.
510 static int openprom_ioctl(struct inode * inode, struct file * file,
511 unsigned int cmd, unsigned long arg)
513 DATA *data = (DATA *) file->private_data;
514 static int cnt;
516 switch (cmd) {
517 case OPROMGETOPT:
518 case OPROMNXTOPT:
519 if ((file->f_mode & FMODE_READ) == 0)
520 return -EPERM;
521 return openprom_sunos_ioctl(inode, file, cmd, arg,
522 options_node);
524 case OPROMSETOPT:
525 case OPROMSETOPT2:
526 if ((file->f_mode & FMODE_WRITE) == 0)
527 return -EPERM;
528 return openprom_sunos_ioctl(inode, file, cmd, arg,
529 options_node);
531 case OPROMNEXT:
532 case OPROMCHILD:
533 case OPROMGETPROP:
534 case OPROMNXTPROP:
535 if ((file->f_mode & FMODE_READ) == 0)
536 return -EPERM;
537 return openprom_sunos_ioctl(inode, file, cmd, arg,
538 data->current_node);
540 case OPROMU2P:
541 case OPROMGETCONS:
542 case OPROMGETFBNAME:
543 case OPROMGETBOOTARGS:
544 case OPROMSETCUR:
545 case OPROMPCI2NODE:
546 case OPROMPATH2NODE:
547 if ((file->f_mode & FMODE_READ) == 0)
548 return -EPERM;
549 return openprom_sunos_ioctl(inode, file, cmd, arg, 0);
551 case OPIOCGET:
552 case OPIOCNEXTPROP:
553 case OPIOCGETOPTNODE:
554 case OPIOCGETNEXT:
555 case OPIOCGETCHILD:
556 if ((file->f_mode & FMODE_READ) == 0)
557 return -EBADF;
558 return openprom_bsd_ioctl(inode,file,cmd,arg);
560 case OPIOCSET:
561 if ((file->f_mode & FMODE_WRITE) == 0)
562 return -EBADF;
563 return openprom_bsd_ioctl(inode,file,cmd,arg);
565 default:
566 if (cnt++ < 10)
567 printk("openprom_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
568 return -EINVAL;
572 static long openprom_compat_ioctl(struct file *file, unsigned int cmd,
573 unsigned long arg)
575 long rval = -ENOTTY;
578 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
579 * the arg which we'd need to clean up...
581 switch (cmd) {
582 case OPROMGETOPT:
583 case OPROMSETOPT:
584 case OPROMNXTOPT:
585 case OPROMSETOPT2:
586 case OPROMNEXT:
587 case OPROMCHILD:
588 case OPROMGETPROP:
589 case OPROMNXTPROP:
590 case OPROMU2P:
591 case OPROMGETCONS:
592 case OPROMGETFBNAME:
593 case OPROMGETBOOTARGS:
594 case OPROMSETCUR:
595 case OPROMPCI2NODE:
596 case OPROMPATH2NODE:
597 lock_kernel();
598 rval = openprom_ioctl(file->f_dentry->d_inode, file, cmd, arg);
599 lock_kernel();
600 break;
603 return rval;
606 static int openprom_open(struct inode * inode, struct file * file)
608 DATA *data;
610 data = (DATA *) kmalloc(sizeof(DATA), GFP_KERNEL);
611 if (!data)
612 return -ENOMEM;
614 data->current_node = prom_root_node;
615 data->lastnode = prom_root_node;
616 file->private_data = (void *)data;
618 return 0;
621 static int openprom_release(struct inode * inode, struct file * file)
623 kfree(file->private_data);
624 return 0;
627 static struct file_operations openprom_fops = {
628 .owner = THIS_MODULE,
629 .llseek = no_llseek,
630 .ioctl = openprom_ioctl,
631 .compat_ioctl = openprom_compat_ioctl,
632 .open = openprom_open,
633 .release = openprom_release,
636 static struct miscdevice openprom_dev = {
637 SUN_OPENPROM_MINOR, "openprom", &openprom_fops
640 static int __init openprom_init(void)
642 int error;
644 error = misc_register(&openprom_dev);
645 if (error) {
646 printk(KERN_ERR "openprom: unable to get misc minor\n");
647 return error;
650 options_node = prom_getchild(prom_root_node);
651 options_node = prom_searchsiblings(options_node,"options");
653 if (options_node == 0 || options_node == -1) {
654 printk(KERN_ERR "openprom: unable to find options node\n");
655 misc_deregister(&openprom_dev);
656 return -EIO;
659 return 0;
662 static void __exit openprom_cleanup(void)
664 misc_deregister(&openprom_dev);
667 module_init(openprom_init);
668 module_exit(openprom_cleanup);
669 MODULE_LICENSE("GPL");