Linux v2.6.15-rc7
[pohmelfs.git] / drivers / sbus / char / openprom.c
blob383a95f34a0da4e8adec682df992786d572efe27
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 prom_getproperty(op.op_nodeid, str, tmp, len);
397 tmp[len] = '\0';
399 if (__copy_to_user(argp, &op, sizeof(op)) != 0
400 || copy_to_user(op.op_buf, tmp, len) != 0)
401 error = -EFAULT;
403 kfree(tmp);
404 kfree(str);
406 return error;
408 case OPIOCNEXTPROP:
409 if (copy_from_user(&op, argp, sizeof(op)))
410 return -EFAULT;
412 if (!goodnode(op.op_nodeid,data))
413 return -EINVAL;
415 error = copyin_string(op.op_name, op.op_namelen, &str);
416 if (error)
417 return error;
419 tmp = prom_nextprop(op.op_nodeid,str,buffer);
421 if (tmp) {
422 len = strlen(tmp);
423 if (len > op.op_buflen)
424 len = op.op_buflen;
425 else
426 op.op_buflen = len;
427 } else {
428 len = op.op_buflen = 0;
431 if (!access_ok(VERIFY_WRITE, argp, sizeof(op))) {
432 kfree(str);
433 return -EFAULT;
436 if (!access_ok(VERIFY_WRITE, op.op_buf, len)) {
437 kfree(str);
438 return -EFAULT;
441 error = __copy_to_user(argp, &op, sizeof(op));
442 if (!error) error = __copy_to_user(op.op_buf, tmp, len);
444 kfree(str);
446 return error;
448 case OPIOCSET:
449 if (copy_from_user(&op, argp, sizeof(op)))
450 return -EFAULT;
452 if (!goodnode(op.op_nodeid,data))
453 return -EINVAL;
455 error = copyin_string(op.op_name, op.op_namelen, &str);
456 if (error)
457 return error;
459 error = copyin_string(op.op_buf, op.op_buflen, &tmp);
460 if (error) {
461 kfree(str);
462 return error;
465 len = prom_setprop(op.op_nodeid,str,tmp,op.op_buflen+1);
467 if (len != op.op_buflen)
468 return -EINVAL;
470 kfree(str);
471 kfree(tmp);
473 return 0;
475 case OPIOCGETOPTNODE:
476 if (copy_to_user(argp, &options_node, sizeof(int)))
477 return -EFAULT;
478 return 0;
480 case OPIOCGETNEXT:
481 case OPIOCGETCHILD:
482 if (copy_from_user(&node, argp, sizeof(int)))
483 return -EFAULT;
485 if (cmd == OPIOCGETNEXT)
486 node = __prom_getsibling(node);
487 else
488 node = __prom_getchild(node);
490 if (__copy_to_user(argp, &node, sizeof(int)))
491 return -EFAULT;
493 return 0;
495 default:
496 if (cnt++ < 10)
497 printk(KERN_INFO "openprom_bsd_ioctl: cmd 0x%X\n", cmd);
498 return -EINVAL;
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;
511 static int cnt;
513 switch (cmd) {
514 case OPROMGETOPT:
515 case OPROMNXTOPT:
516 if ((file->f_mode & FMODE_READ) == 0)
517 return -EPERM;
518 return openprom_sunos_ioctl(inode, file, cmd, arg,
519 options_node);
521 case OPROMSETOPT:
522 case OPROMSETOPT2:
523 if ((file->f_mode & FMODE_WRITE) == 0)
524 return -EPERM;
525 return openprom_sunos_ioctl(inode, file, cmd, arg,
526 options_node);
528 case OPROMNEXT:
529 case OPROMCHILD:
530 case OPROMGETPROP:
531 case OPROMNXTPROP:
532 if ((file->f_mode & FMODE_READ) == 0)
533 return -EPERM;
534 return openprom_sunos_ioctl(inode, file, cmd, arg,
535 data->current_node);
537 case OPROMU2P:
538 case OPROMGETCONS:
539 case OPROMGETFBNAME:
540 case OPROMGETBOOTARGS:
541 case OPROMSETCUR:
542 case OPROMPCI2NODE:
543 case OPROMPATH2NODE:
544 if ((file->f_mode & FMODE_READ) == 0)
545 return -EPERM;
546 return openprom_sunos_ioctl(inode, file, cmd, arg, 0);
548 case OPIOCGET:
549 case OPIOCNEXTPROP:
550 case OPIOCGETOPTNODE:
551 case OPIOCGETNEXT:
552 case OPIOCGETCHILD:
553 if ((file->f_mode & FMODE_READ) == 0)
554 return -EBADF;
555 return openprom_bsd_ioctl(inode,file,cmd,arg);
557 case OPIOCSET:
558 if ((file->f_mode & FMODE_WRITE) == 0)
559 return -EBADF;
560 return openprom_bsd_ioctl(inode,file,cmd,arg);
562 default:
563 if (cnt++ < 10)
564 printk("openprom_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
565 return -EINVAL;
569 static long openprom_compat_ioctl(struct file *file, unsigned int cmd,
570 unsigned long arg)
572 long rval = -ENOTTY;
575 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
576 * the arg which we'd need to clean up...
578 switch (cmd) {
579 case OPROMGETOPT:
580 case OPROMSETOPT:
581 case OPROMNXTOPT:
582 case OPROMSETOPT2:
583 case OPROMNEXT:
584 case OPROMCHILD:
585 case OPROMGETPROP:
586 case OPROMNXTPROP:
587 case OPROMU2P:
588 case OPROMGETCONS:
589 case OPROMGETFBNAME:
590 case OPROMGETBOOTARGS:
591 case OPROMSETCUR:
592 case OPROMPCI2NODE:
593 case OPROMPATH2NODE:
594 lock_kernel();
595 rval = openprom_ioctl(file->f_dentry->d_inode, file, cmd, arg);
596 lock_kernel();
597 break;
600 return rval;
603 static int openprom_open(struct inode * inode, struct file * file)
605 DATA *data;
607 data = (DATA *) kmalloc(sizeof(DATA), GFP_KERNEL);
608 if (!data)
609 return -ENOMEM;
611 data->current_node = prom_root_node;
612 data->lastnode = prom_root_node;
613 file->private_data = (void *)data;
615 return 0;
618 static int openprom_release(struct inode * inode, struct file * file)
620 kfree(file->private_data);
621 return 0;
624 static struct file_operations openprom_fops = {
625 .owner = THIS_MODULE,
626 .llseek = no_llseek,
627 .ioctl = openprom_ioctl,
628 .compat_ioctl = openprom_compat_ioctl,
629 .open = openprom_open,
630 .release = openprom_release,
633 static struct miscdevice openprom_dev = {
634 SUN_OPENPROM_MINOR, "openprom", &openprom_fops
637 static int __init openprom_init(void)
639 int error;
641 error = misc_register(&openprom_dev);
642 if (error) {
643 printk(KERN_ERR "openprom: unable to get misc minor\n");
644 return error;
647 options_node = prom_getchild(prom_root_node);
648 options_node = prom_searchsiblings(options_node,"options");
650 if (options_node == 0 || options_node == -1) {
651 printk(KERN_ERR "openprom: unable to find options node\n");
652 misc_deregister(&openprom_dev);
653 return -EIO;
656 return 0;
659 static void __exit openprom_cleanup(void)
661 misc_deregister(&openprom_dev);
664 module_init(openprom_init);
665 module_exit(openprom_cleanup);
666 MODULE_LICENSE("GPL");