xref: /linux/drivers/sbus/char/openprom.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
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)
5  *
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.
10  *
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.
15  */
16 
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.
21  *
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.
26  *
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.
30  */
31 
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
51 
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");
56 
57 /* Private data kept by the driver for each descriptor. */
58 typedef struct openprom_private_data
59 {
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;
63 
64 /* ID of the PROM node containing all of the EEPROM options. */
65 static struct device_node *options_node;
66 
67 /*
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.
73  */
74 static int copyin(struct openpromio __user *info, struct openpromio **opp_p)
75 {
76 	unsigned int bufsize;
77 
78 	if (!info || !opp_p)
79 		return -EFAULT;
80 
81 	if (get_user(bufsize, &info->oprom_size))
82 		return -EFAULT;
83 
84 	if (bufsize == 0)
85 		return -EINVAL;
86 
87 	/* If the bufsize is too large, just limit it.
88 	 * Fix from Jason Rappleye.
89 	 */
90 	if (bufsize > OPROMMAXPARAM)
91 		bufsize = OPROMMAXPARAM;
92 
93 	if (!(*opp_p = kzalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
94 		return -ENOMEM;
95 
96 	if (copy_from_user(&(*opp_p)->oprom_array,
97 			   &info->oprom_array, bufsize)) {
98 		kfree(*opp_p);
99 		return -EFAULT;
100 	}
101 	return bufsize;
102 }
103 
104 static int getstrings(struct openpromio __user *info, struct openpromio **opp_p)
105 {
106 	int n, bufsize;
107 	char c;
108 
109 	if (!info || !opp_p)
110 		return -EFAULT;
111 
112 	if (!(*opp_p = kzalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
113 		return -ENOMEM;
114 
115 	(*opp_p)->oprom_size = 0;
116 
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;
122 		}
123 		if (c == '\0')
124 			n++;
125 		(*opp_p)->oprom_array[bufsize++] = c;
126 	}
127 	if (!n) {
128 		kfree(*opp_p);
129 		return -EINVAL;
130 	}
131 	return bufsize;
132 }
133 
134 /*
135  * Copy an openpromio structure in kernel space back to user space.
136  */
137 static int copyout(void __user *info, struct openpromio *opp, int len)
138 {
139 	if (copy_to_user(info, opp, len))
140 		return -EFAULT;
141 	return 0;
142 }
143 
144 static int opromgetprop(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize)
145 {
146 	void *pval;
147 	int len;
148 
149 	pval = of_get_property(dp, op->oprom_array, &len);
150 	if (!pval || len <= 0 || len > bufsize)
151 		return copyout(argp, op, sizeof(int));
152 
153 	memcpy(op->oprom_array, pval, len);
154 	op->oprom_array[len] = '\0';
155 	op->oprom_size = len;
156 
157 	return copyout(argp, op, sizeof(int) + bufsize);
158 }
159 
160 static int opromnxtprop(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize)
161 {
162 	struct property *prop;
163 	int len;
164 
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);
172 
173 		if (!prop ||
174 		    !prop->next ||
175 		    (len = strlen(prop->next->name)) + 1 > bufsize)
176 			return copyout(argp, op, sizeof(int));
177 
178 		prop = prop->next;
179 	}
180 
181 	memcpy(op->oprom_array, prop->name, len);
182 	op->oprom_array[len] = '\0';
183 	op->oprom_size = ++len;
184 
185 	return copyout(argp, op, sizeof(int) + bufsize);
186 }
187 
188 static int opromsetopt(struct device_node *dp, struct openpromio *op, int bufsize)
189 {
190 	char *buf = op->oprom_array + strlen(op->oprom_array) + 1;
191 	int len = op->oprom_array + bufsize - buf;
192 
193 	return of_set_property(options_node, op->oprom_array, buf, len);
194 }
195 
196 static int opromnext(void __user *argp, unsigned int cmd, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
197 {
198 	phandle ph;
199 
200 	BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
201 
202 	if (bufsize < sizeof(phandle))
203 		return -EINVAL;
204 
205 	ph = *((int *) op->oprom_array);
206 	if (ph) {
207 		dp = of_find_node_by_phandle(ph);
208 		if (!dp)
209 			return -EINVAL;
210 
211 		switch (cmd) {
212 		case OPROMNEXT:
213 			dp = dp->sibling;
214 			break;
215 
216 		case OPROMCHILD:
217 			dp = dp->child;
218 			break;
219 
220 		case OPROMSETCUR:
221 		default:
222 			break;
223 		};
224 	} else {
225 		/* Sibling of node zero is the root node.  */
226 		if (cmd != OPROMNEXT)
227 			return -EINVAL;
228 
229 		dp = of_find_node_by_path("/");
230 	}
231 
232 	ph = 0;
233 	if (dp)
234 		ph = dp->node;
235 
236 	data->current_node = dp;
237 	*((int *) op->oprom_array) = ph;
238 	op->oprom_size = sizeof(phandle);
239 
240 	return copyout(argp, op, bufsize + sizeof(int));
241 }
242 
243 static int oprompci2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
244 {
245 	int err = -EINVAL;
246 
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]);
253 
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));
261 		}
262 #endif
263 	}
264 
265 	return err;
266 }
267 
268 static int oprompath2node(void __user *argp, struct device_node *dp, struct openpromio *op, int bufsize, DATA *data)
269 {
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);
274 
275 	return copyout(argp, op, bufsize + sizeof(int));
276 }
277 
278 static int opromgetbootargs(void __user *argp, struct openpromio *op, int bufsize)
279 {
280 	char *buf = saved_command_line;
281 	int len = strlen(buf);
282 
283 	if (len > bufsize)
284 		return -EINVAL;
285 
286 	strcpy(op->oprom_array, buf);
287 	op->oprom_size = len;
288 
289 	return copyout(argp, op, bufsize + sizeof(int));
290 }
291 
292 /*
293  *	SunOS and Solaris /dev/openprom ioctl calls.
294  */
295 static int openprom_sunos_ioctl(struct inode * inode, struct file * file,
296 				unsigned int cmd, unsigned long arg,
297 				struct device_node *dp)
298 {
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;
304 
305 	if (cmd == OPROMSETOPT)
306 		bufsize = getstrings(argp, &opp);
307 	else
308 		bufsize = copyin(argp, &opp);
309 
310 	if (bufsize < 0)
311 		return bufsize;
312 
313 	switch (cmd) {
314 	case OPROMGETOPT:
315 	case OPROMGETPROP:
316 		error = opromgetprop(argp, dp, opp, bufsize);
317 		break;
318 
319 	case OPROMNXTOPT:
320 	case OPROMNXTPROP:
321 		error = opromnxtprop(argp, dp, opp, bufsize);
322 		break;
323 
324 	case OPROMSETOPT:
325 	case OPROMSETOPT2:
326 		error = opromsetopt(dp, opp, bufsize);
327 		break;
328 
329 	case OPROMNEXT:
330 	case OPROMCHILD:
331 	case OPROMSETCUR:
332 		error = opromnext(argp, cmd, dp, opp, bufsize, data);
333 		break;
334 
335 	case OPROMPCI2NODE:
336 		error = oprompci2node(argp, dp, opp, bufsize, data);
337 		break;
338 
339 	case OPROMPATH2NODE:
340 		error = oprompath2node(argp, dp, opp, bufsize, data);
341 		break;
342 
343 	case OPROMGETBOOTARGS:
344 		error = opromgetbootargs(argp, opp, bufsize);
345 		break;
346 
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;
359 	}
360 
361 	kfree(opp);
362 	return error;
363 }
364 
365 static struct device_node *get_node(phandle n, DATA *data)
366 {
367 	struct device_node *dp = of_find_node_by_phandle(n);
368 
369 	if (dp)
370 		data->lastnode = dp;
371 
372 	return dp;
373 }
374 
375 /* Copy in a whole string from userspace into kernelspace. */
376 static int copyin_string(char __user *user, size_t len, char **ptr)
377 {
378 	char *tmp;
379 
380 	if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
381 		return -EINVAL;
382 
383 	tmp = kmalloc(len + 1, GFP_KERNEL);
384 	if (!tmp)
385 		return -ENOMEM;
386 
387 	if (copy_from_user(tmp, user, len)) {
388 		kfree(tmp);
389 		return -EFAULT;
390 	}
391 
392 	tmp[len] = '\0';
393 
394 	*ptr = tmp;
395 
396 	return 0;
397 }
398 
399 /*
400  *	NetBSD /dev/openprom ioctl calls.
401  */
402 static int opiocget(void __user *argp, DATA *data)
403 {
404 	struct opiocdesc op;
405 	struct device_node *dp;
406 	char *str;
407 	void *pval;
408 	int err, len;
409 
410 	if (copy_from_user(&op, argp, sizeof(op)))
411 		return -EFAULT;
412 
413 	dp = get_node(op.op_nodeid, data);
414 
415 	err = copyin_string(op.op_name, op.op_namelen, &str);
416 	if (err)
417 		return err;
418 
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;
428 	}
429 	kfree(str);
430 
431 	return err;
432 }
433 
434 static int opiocnextprop(void __user *argp, DATA *data)
435 {
436 	struct opiocdesc op;
437 	struct device_node *dp;
438 	struct property *prop;
439 	char *str;
440 	int err, len;
441 
442 	if (copy_from_user(&op, argp, sizeof(op)))
443 		return -EFAULT;
444 
445 	dp = get_node(op.op_nodeid, data);
446 	if (!dp)
447 		return -EINVAL;
448 
449 	err = copyin_string(op.op_name, op.op_namelen, &str);
450 	if (err)
451 		return err;
452 
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;
459 	}
460 	kfree(str);
461 
462 	if (!prop)
463 		len = 0;
464 	else
465 		len = prop->length;
466 
467 	if (len > op.op_buflen)
468 		len = op.op_buflen;
469 
470 	if (copy_to_user(argp, &op, sizeof(op)))
471 		return -EFAULT;
472 
473 	if (len &&
474 	    copy_to_user(op.op_buf, prop->value, len))
475 		return -EFAULT;
476 
477 	return 0;
478 }
479 
480 static int opiocset(void __user *argp, DATA *data)
481 {
482 	struct opiocdesc op;
483 	struct device_node *dp;
484 	char *str, *tmp;
485 	int err;
486 
487 	if (copy_from_user(&op, argp, sizeof(op)))
488 		return -EFAULT;
489 
490 	dp = get_node(op.op_nodeid, data);
491 	if (!dp)
492 		return -EINVAL;
493 
494 	err = copyin_string(op.op_name, op.op_namelen, &str);
495 	if (err)
496 		return err;
497 
498 	err = copyin_string(op.op_buf, op.op_buflen, &tmp);
499 	if (err) {
500 		kfree(str);
501 		return err;
502 	}
503 
504 	err = of_set_property(dp, str, tmp, op.op_buflen);
505 
506 	kfree(str);
507 	kfree(tmp);
508 
509 	return err;
510 }
511 
512 static int opiocgetnext(unsigned int cmd, void __user *argp)
513 {
514 	struct device_node *dp;
515 	phandle nd;
516 
517 	BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
518 
519 	if (copy_from_user(&nd, argp, sizeof(phandle)))
520 		return -EFAULT;
521 
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;
534 		}
535 	}
536 	if (dp)
537 		nd = dp->node;
538 	if (copy_to_user(argp, &nd, sizeof(phandle)))
539 		return -EFAULT;
540 
541 	return 0;
542 }
543 
544 static int openprom_bsd_ioctl(struct inode * inode, struct file * file,
545 			      unsigned int cmd, unsigned long arg)
546 {
547 	DATA *data = (DATA *) file->private_data;
548 	void __user *argp = (void __user *)arg;
549 	int err;
550 
551 	switch (cmd) {
552 	case OPIOCGET:
553 		err = opiocget(argp, data);
554 		break;
555 
556 	case OPIOCNEXTPROP:
557 		err = opiocnextprop(argp, data);
558 		break;
559 
560 	case OPIOCSET:
561 		err = opiocset(argp, data);
562 		break;
563 
564 	case OPIOCGETOPTNODE:
565 		BUILD_BUG_ON(sizeof(phandle) != sizeof(int));
566 
567 		if (copy_to_user(argp, &options_node->node, sizeof(phandle)))
568 			return -EFAULT;
569 
570 		return 0;
571 
572 	case OPIOCGETNEXT:
573 	case OPIOCGETCHILD:
574 		err = opiocgetnext(cmd, argp);
575 		break;
576 
577 	default:
578 		return -EINVAL;
579 
580 	};
581 
582 	return err;
583 }
584 
585 
586 /*
587  *	Handoff control to the correct ioctl handler.
588  */
589 static int openprom_ioctl(struct inode * inode, struct file * file,
590 			  unsigned int cmd, unsigned long arg)
591 {
592 	DATA *data = (DATA *) file->private_data;
593 
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);
601 
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);
608 
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);
617 
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);
628 
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);
637 
638 	case OPIOCSET:
639 		if ((file->f_mode & FMODE_WRITE) == 0)
640 			return -EBADF;
641 		return openprom_bsd_ioctl(inode,file,cmd,arg);
642 
643 	default:
644 		return -EINVAL;
645 	};
646 }
647 
648 static long openprom_compat_ioctl(struct file *file, unsigned int cmd,
649 		unsigned long arg)
650 {
651 	long rval = -ENOTTY;
652 
653 	/*
654 	 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
655 	 * the arg which we'd need to clean up...
656 	 */
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;
675 	}
676 
677 	return rval;
678 }
679 
680 static int openprom_open(struct inode * inode, struct file * file)
681 {
682 	DATA *data;
683 
684 	data = kmalloc(sizeof(DATA), GFP_KERNEL);
685 	if (!data)
686 		return -ENOMEM;
687 
688 	data->current_node = of_find_node_by_path("/");
689 	data->lastnode = data->current_node;
690 	file->private_data = (void *) data;
691 
692 	return 0;
693 }
694 
695 static int openprom_release(struct inode * inode, struct file * file)
696 {
697 	kfree(file->private_data);
698 	return 0;
699 }
700 
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,
708 };
709 
710 static struct miscdevice openprom_dev = {
711 	.minor		= SUN_OPENPROM_MINOR,
712 	.name		= "openprom",
713 	.fops		= &openprom_fops,
714 };
715 
716 static int __init openprom_init(void)
717 {
718 	struct device_node *dp;
719 	int err;
720 
721 	err = misc_register(&openprom_dev);
722 	if (err)
723 		return err;
724 
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;
731 	}
732 	options_node = dp;
733 
734 	if (!options_node) {
735 		misc_deregister(&openprom_dev);
736 		return -EIO;
737 	}
738 
739 	return 0;
740 }
741 
742 static void __exit openprom_cleanup(void)
743 {
744 	misc_deregister(&openprom_dev);
745 }
746 
747 module_init(openprom_init);
748 module_exit(openprom_cleanup);
749