Home | History | Annotate | Line # | Download | only in ofw
openfirmio.c revision 1.2.2.2
      1  1.2.2.2  bouyer /*	$NetBSD: openfirmio.c,v 1.2.2.2 2000/11/22 16:03:57 bouyer Exp $ */
      2  1.2.2.2  bouyer 
      3  1.2.2.2  bouyer /*
      4  1.2.2.2  bouyer  * Copyright (c) 1992, 1993
      5  1.2.2.2  bouyer  *	The Regents of the University of California.  All rights reserved.
      6  1.2.2.2  bouyer  *
      7  1.2.2.2  bouyer  * This software was developed by the Computer Systems Engineering group
      8  1.2.2.2  bouyer  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.2.2.2  bouyer  * contributed to Berkeley.
     10  1.2.2.2  bouyer  *
     11  1.2.2.2  bouyer  * All advertising materials mentioning features or use of this software
     12  1.2.2.2  bouyer  * must display the following acknowledgement:
     13  1.2.2.2  bouyer  *	This product includes software developed by the University of
     14  1.2.2.2  bouyer  *	California, Lawrence Berkeley Laboratory.
     15  1.2.2.2  bouyer  *
     16  1.2.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     17  1.2.2.2  bouyer  * modification, are permitted provided that the following conditions
     18  1.2.2.2  bouyer  * are met:
     19  1.2.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     20  1.2.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     21  1.2.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.2.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     23  1.2.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     24  1.2.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     25  1.2.2.2  bouyer  *    must display the following acknowledgement:
     26  1.2.2.2  bouyer  *	This product includes software developed by the University of
     27  1.2.2.2  bouyer  *	California, Berkeley and its contributors.
     28  1.2.2.2  bouyer  * 4. Neither the name of the University nor the names of its contributors
     29  1.2.2.2  bouyer  *    may be used to endorse or promote products derived from this software
     30  1.2.2.2  bouyer  *    without specific prior written permission.
     31  1.2.2.2  bouyer  *
     32  1.2.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  1.2.2.2  bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  1.2.2.2  bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  1.2.2.2  bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  1.2.2.2  bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  1.2.2.2  bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  1.2.2.2  bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  1.2.2.2  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  1.2.2.2  bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  1.2.2.2  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  1.2.2.2  bouyer  * SUCH DAMAGE.
     43  1.2.2.2  bouyer  *
     44  1.2.2.2  bouyer  *	@(#)openfirm.c	8.1 (Berkeley) 6/11/93
     45  1.2.2.2  bouyer  */
     46  1.2.2.2  bouyer 
     47  1.2.2.2  bouyer #include <sys/param.h>
     48  1.2.2.2  bouyer #include <sys/systm.h>
     49  1.2.2.2  bouyer #include <sys/errno.h>
     50  1.2.2.2  bouyer #include <sys/fcntl.h>
     51  1.2.2.2  bouyer #include <sys/ioctl.h>
     52  1.2.2.2  bouyer #include <sys/malloc.h>
     53  1.2.2.2  bouyer #include <sys/conf.h>
     54  1.2.2.2  bouyer #include <sys/device.h>
     55  1.2.2.2  bouyer 
     56  1.2.2.2  bouyer #include <dev/ofw/openfirm.h>
     57  1.2.2.2  bouyer #include <dev/ofw/openfirmio.h>
     58  1.2.2.2  bouyer 
     59  1.2.2.2  bouyer static	int lastnode;			/* speed hack */
     60  1.2.2.2  bouyer 
     61  1.2.2.2  bouyer static int openfirmcheckid (int, int);
     62  1.2.2.2  bouyer static int openfirmgetstr (int, char *, char **);
     63  1.2.2.2  bouyer 
     64  1.2.2.2  bouyer void openfirmattach (int);
     65  1.2.2.2  bouyer 
     66  1.2.2.2  bouyer void
     67  1.2.2.2  bouyer openfirmattach(int num)
     68  1.2.2.2  bouyer {
     69  1.2.2.2  bouyer 	/* nothing */
     70  1.2.2.2  bouyer }
     71  1.2.2.2  bouyer 
     72  1.2.2.2  bouyer int
     73  1.2.2.2  bouyer openfirmopen(dev_t dev, int flags, int mode, struct proc *p)
     74  1.2.2.2  bouyer {
     75  1.2.2.2  bouyer 	return (0);
     76  1.2.2.2  bouyer }
     77  1.2.2.2  bouyer 
     78  1.2.2.2  bouyer int
     79  1.2.2.2  bouyer openfirmclose(dev_t dev, int flags, int mode, struct proc *p)
     80  1.2.2.2  bouyer {
     81  1.2.2.2  bouyer 
     82  1.2.2.2  bouyer 	return (0);
     83  1.2.2.2  bouyer }
     84  1.2.2.2  bouyer 
     85  1.2.2.2  bouyer /*
     86  1.2.2.2  bouyer  * Verify target ID is valid (exists in the OPENPROM tree), as
     87  1.2.2.2  bouyer  * listed from node ID sid forward.
     88  1.2.2.2  bouyer  */
     89  1.2.2.2  bouyer static int
     90  1.2.2.2  bouyer openfirmcheckid(int sid, int tid)
     91  1.2.2.2  bouyer {
     92  1.2.2.2  bouyer 
     93  1.2.2.2  bouyer 	for (; sid != 0; sid = OF_peer(sid))
     94  1.2.2.2  bouyer 		if (sid == tid || openfirmcheckid(OF_child(sid), tid))
     95  1.2.2.2  bouyer 			return (1);
     96  1.2.2.2  bouyer 
     97  1.2.2.2  bouyer 	return (0);
     98  1.2.2.2  bouyer }
     99  1.2.2.2  bouyer 
    100  1.2.2.2  bouyer static int
    101  1.2.2.2  bouyer openfirmgetstr(int len, char *user, char **cpp)
    102  1.2.2.2  bouyer {
    103  1.2.2.2  bouyer 	int error;
    104  1.2.2.2  bouyer 	char *cp;
    105  1.2.2.2  bouyer 
    106  1.2.2.2  bouyer 	/* Reject obvious bogus requests */
    107  1.2.2.2  bouyer 	if ((u_int)len > (8 * 1024) - 1)
    108  1.2.2.2  bouyer 		return (ENAMETOOLONG);
    109  1.2.2.2  bouyer 
    110  1.2.2.2  bouyer 	*cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
    111  1.2.2.2  bouyer 	error = copyin(user, cp, len);
    112  1.2.2.2  bouyer 	cp[len] = '\0';
    113  1.2.2.2  bouyer 	return (error);
    114  1.2.2.2  bouyer }
    115  1.2.2.2  bouyer 
    116  1.2.2.2  bouyer int
    117  1.2.2.2  bouyer openfirmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
    118  1.2.2.2  bouyer {
    119  1.2.2.2  bouyer 	struct ofiocdesc *of;
    120  1.2.2.2  bouyer 	int node, len, ok, error, s;
    121  1.2.2.2  bouyer 	char *name, *value;
    122  1.2.2.2  bouyer 
    123  1.2.2.2  bouyer 	if (cmd == OFIOCGETOPTNODE) {
    124  1.2.2.2  bouyer 		s = splhigh();
    125  1.2.2.2  bouyer 		*(int *) data = OF_finddevice("/options");
    126  1.2.2.2  bouyer 		splx(s);
    127  1.2.2.2  bouyer 		return (0);
    128  1.2.2.2  bouyer 	}
    129  1.2.2.2  bouyer 
    130  1.2.2.2  bouyer 	/* Verify node id */
    131  1.2.2.2  bouyer 	of = (struct ofiocdesc *)data;
    132  1.2.2.2  bouyer 	node = of->of_nodeid;
    133  1.2.2.2  bouyer 	if (node != 0 && node != lastnode) {
    134  1.2.2.2  bouyer 		/* Not an easy one, must search for it */
    135  1.2.2.2  bouyer 		s = splhigh();
    136  1.2.2.2  bouyer 		ok = openfirmcheckid(OF_peer(0), node);
    137  1.2.2.2  bouyer 		splx(s);
    138  1.2.2.2  bouyer 		if (!ok)
    139  1.2.2.2  bouyer 			return (EINVAL);
    140  1.2.2.2  bouyer 		lastnode = node;
    141  1.2.2.2  bouyer 	}
    142  1.2.2.2  bouyer 
    143  1.2.2.2  bouyer 	name = value = NULL;
    144  1.2.2.2  bouyer 	error = 0;
    145  1.2.2.2  bouyer 	switch (cmd) {
    146  1.2.2.2  bouyer 
    147  1.2.2.2  bouyer 	case OFIOCGET:
    148  1.2.2.2  bouyer 		if ((flags & FREAD) == 0)
    149  1.2.2.2  bouyer 			return (EBADF);
    150  1.2.2.2  bouyer 		if (node == 0)
    151  1.2.2.2  bouyer 			return (EINVAL);
    152  1.2.2.2  bouyer 		error = openfirmgetstr(of->of_namelen, of->of_name, &name);
    153  1.2.2.2  bouyer 		if (error)
    154  1.2.2.2  bouyer 			break;
    155  1.2.2.2  bouyer 		s = splhigh();
    156  1.2.2.2  bouyer 		len = OF_getproplen(node, name);
    157  1.2.2.2  bouyer 		splx(s);
    158  1.2.2.2  bouyer 		if (len > of->of_buflen) {
    159  1.2.2.2  bouyer 			error = ENOMEM;
    160  1.2.2.2  bouyer 			break;
    161  1.2.2.2  bouyer 		}
    162  1.2.2.2  bouyer 		of->of_buflen = len;
    163  1.2.2.2  bouyer 		/* -1 means no entry; 0 means no value */
    164  1.2.2.2  bouyer 		if (len <= 0)
    165  1.2.2.2  bouyer 			break;
    166  1.2.2.2  bouyer 		value = malloc(len, M_TEMP, M_WAITOK);
    167  1.2.2.2  bouyer 		if (value == NULL) {
    168  1.2.2.2  bouyer 			error = ENOMEM;
    169  1.2.2.2  bouyer 			break;
    170  1.2.2.2  bouyer 		}
    171  1.2.2.2  bouyer 		s = splhigh();
    172  1.2.2.2  bouyer 		len = OF_getprop(node, name, (void *)value, len);
    173  1.2.2.2  bouyer 		splx(s);
    174  1.2.2.2  bouyer 		error = copyout(value, of->of_buf, len);
    175  1.2.2.2  bouyer 		break;
    176  1.2.2.2  bouyer 
    177  1.2.2.2  bouyer #if 0
    178  1.2.2.2  bouyer 	case OFIOCSET:
    179  1.2.2.2  bouyer 		if ((flags & FWRITE) == 0)
    180  1.2.2.2  bouyer 			return (EBADF);
    181  1.2.2.2  bouyer 		if (node == 0)
    182  1.2.2.2  bouyer 			return (EINVAL);
    183  1.2.2.2  bouyer 		error = openfirmgetstr(of->of_namelen, of->of_name, &name);
    184  1.2.2.2  bouyer 		if (error)
    185  1.2.2.2  bouyer 			break;
    186  1.2.2.2  bouyer 		error = openfirmgetstr(of->of_buflen, of->of_buf, &value);
    187  1.2.2.2  bouyer 		if (error)
    188  1.2.2.2  bouyer 			break;
    189  1.2.2.2  bouyer 		s = splhigh();
    190  1.2.2.2  bouyer 		len = OF_setprop(node, name, value, of->of_buflen + 1);
    191  1.2.2.2  bouyer 		splx(s);
    192  1.2.2.2  bouyer 		if (len != of->of_buflen)
    193  1.2.2.2  bouyer 			error = EINVAL;
    194  1.2.2.2  bouyer 		break;
    195  1.2.2.2  bouyer #endif
    196  1.2.2.2  bouyer 
    197  1.2.2.2  bouyer 	case OFIOCNEXTPROP: {
    198  1.2.2.2  bouyer 		char newname[32];
    199  1.2.2.2  bouyer 		if ((flags & FREAD) == 0)
    200  1.2.2.2  bouyer 			return (EBADF);
    201  1.2.2.2  bouyer 		if (node == 0)
    202  1.2.2.2  bouyer 			return (EINVAL);
    203  1.2.2.2  bouyer 		if (of->of_namelen != 0) {
    204  1.2.2.2  bouyer 			error = openfirmgetstr(of->of_namelen, of->of_name,
    205  1.2.2.2  bouyer 			    &name);
    206  1.2.2.2  bouyer 			if (error)
    207  1.2.2.2  bouyer 				break;
    208  1.2.2.2  bouyer 		}
    209  1.2.2.2  bouyer 		s = splhigh();
    210  1.2.2.2  bouyer 		ok = OF_nextprop(node, name, newname);
    211  1.2.2.2  bouyer 		splx(s);
    212  1.2.2.2  bouyer 		if (ok == 0) {
    213  1.2.2.2  bouyer 			error = ENOENT;
    214  1.2.2.2  bouyer 			break;
    215  1.2.2.2  bouyer 		}
    216  1.2.2.2  bouyer 		if (ok == -1) {
    217  1.2.2.2  bouyer 			error = EINVAL;
    218  1.2.2.2  bouyer 			break;
    219  1.2.2.2  bouyer 		}
    220  1.2.2.2  bouyer 		len = strlen(newname);
    221  1.2.2.2  bouyer 		if (len > of->of_buflen)
    222  1.2.2.2  bouyer 			len = of->of_buflen;
    223  1.2.2.2  bouyer 		else
    224  1.2.2.2  bouyer 			of->of_buflen = len;
    225  1.2.2.2  bouyer 		error = copyout(newname, of->of_buf, len);
    226  1.2.2.2  bouyer 		break;
    227  1.2.2.2  bouyer 	}
    228  1.2.2.2  bouyer 
    229  1.2.2.2  bouyer 	case OFIOCGETNEXT:
    230  1.2.2.2  bouyer 		if ((flags & FREAD) == 0)
    231  1.2.2.2  bouyer 			return (EBADF);
    232  1.2.2.2  bouyer 		s = splhigh();
    233  1.2.2.2  bouyer 		node = OF_peer(node);
    234  1.2.2.2  bouyer 		splx(s);
    235  1.2.2.2  bouyer 		*(int *)data = lastnode = node;
    236  1.2.2.2  bouyer 		break;
    237  1.2.2.2  bouyer 
    238  1.2.2.2  bouyer 	case OFIOCGETCHILD:
    239  1.2.2.2  bouyer 		if ((flags & FREAD) == 0)
    240  1.2.2.2  bouyer 			return (EBADF);
    241  1.2.2.2  bouyer 		if (node == 0)
    242  1.2.2.2  bouyer 			return (EINVAL);
    243  1.2.2.2  bouyer 		s = splhigh();
    244  1.2.2.2  bouyer 		node = OF_child(node);
    245  1.2.2.2  bouyer 		splx(s);
    246  1.2.2.2  bouyer 		*(int *)data = lastnode = node;
    247  1.2.2.2  bouyer 		break;
    248  1.2.2.2  bouyer 
    249  1.2.2.2  bouyer 	case OFIOCFINDDEVICE:
    250  1.2.2.2  bouyer 		if ((flags & FREAD) == 0)
    251  1.2.2.2  bouyer 			return (EBADF);
    252  1.2.2.2  bouyer 		error = openfirmgetstr(of->of_namelen, of->of_name, &name);
    253  1.2.2.2  bouyer 		if (error)
    254  1.2.2.2  bouyer 			break;
    255  1.2.2.2  bouyer 		node = OF_finddevice(name);
    256  1.2.2.2  bouyer 		if (node == 0 || node == -1) {
    257  1.2.2.2  bouyer 			error = ENOENT;
    258  1.2.2.2  bouyer 			break;
    259  1.2.2.2  bouyer 		}
    260  1.2.2.2  bouyer 		of->of_nodeid = lastnode = node;
    261  1.2.2.2  bouyer 		break;
    262  1.2.2.2  bouyer 
    263  1.2.2.2  bouyer 	default:
    264  1.2.2.2  bouyer 		return (ENOTTY);
    265  1.2.2.2  bouyer 	}
    266  1.2.2.2  bouyer 
    267  1.2.2.2  bouyer 	if (name)
    268  1.2.2.2  bouyer 		free(name, M_TEMP);
    269  1.2.2.2  bouyer 	if (value)
    270  1.2.2.2  bouyer 		free(value, M_TEMP);
    271  1.2.2.2  bouyer 
    272  1.2.2.2  bouyer 	return (error);
    273  1.2.2.2  bouyer }
    274