Home | History | Annotate | Line # | Download | only in ofw
ofw_subr.c revision 1.12.2.1
      1  1.12.2.1      mjf /*	$NetBSD: ofw_subr.c,v 1.12.2.1 2007/11/19 00:48:04 mjf Exp $	*/
      2       1.1      cgd 
      3       1.1      cgd /*
      4       1.1      cgd  * Copyright 1998
      5       1.1      cgd  * Digital Equipment Corporation. All rights reserved.
      6       1.1      cgd  *
      7       1.1      cgd  * This software is furnished under license and may be used and
      8       1.1      cgd  * copied only in accordance with the following terms and conditions.
      9       1.1      cgd  * Subject to these conditions, you may download, copy, install,
     10       1.1      cgd  * use, modify and distribute this software in source and/or binary
     11       1.1      cgd  * form. No title or ownership is transferred hereby.
     12       1.1      cgd  *
     13       1.1      cgd  * 1) Any source code used, modified or distributed must reproduce
     14       1.1      cgd  *    and retain this copyright notice and list of conditions as
     15       1.1      cgd  *    they appear in the source file.
     16       1.1      cgd  *
     17       1.1      cgd  * 2) No right is granted to use any trade name, trademark, or logo of
     18       1.1      cgd  *    Digital Equipment Corporation. Neither the "Digital Equipment
     19       1.1      cgd  *    Corporation" name nor any trademark or logo of Digital Equipment
     20       1.1      cgd  *    Corporation may be used to endorse or promote products derived
     21       1.1      cgd  *    from this software without the prior written permission of
     22       1.1      cgd  *    Digital Equipment Corporation.
     23       1.1      cgd  *
     24       1.1      cgd  * 3) This software is provided "AS-IS" and any express or implied
     25       1.1      cgd  *    warranties, including but not limited to, any implied warranties
     26       1.1      cgd  *    of merchantability, fitness for a particular purpose, or
     27       1.1      cgd  *    non-infringement are disclaimed. In no event shall DIGITAL be
     28       1.1      cgd  *    liable for any damages whatsoever, and in particular, DIGITAL
     29       1.1      cgd  *    shall not be liable for special, indirect, consequential, or
     30       1.1      cgd  *    incidental damages or damages for lost profits, loss of
     31       1.1      cgd  *    revenue or loss of use, whether such damages arise in contract,
     32       1.1      cgd  *    negligence, tort, under statute, in equity, at law or otherwise,
     33       1.1      cgd  *    even if advised of the possibility of such damage.
     34       1.1      cgd  */
     35       1.7    lukem 
     36       1.7    lukem #include <sys/cdefs.h>
     37  1.12.2.1      mjf __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.12.2.1 2007/11/19 00:48:04 mjf Exp $");
     38       1.1      cgd 
     39       1.2      cgd #include <sys/param.h>
     40       1.2      cgd #include <sys/systm.h>
     41       1.2      cgd #include <sys/malloc.h>
     42       1.2      cgd #include <dev/ofw/openfirm.h>
     43       1.2      cgd 
     44       1.3      cgd #define	OFW_MAX_STACK_BUF_SIZE	256
     45       1.3      cgd #define	OFW_PATH_BUF_SIZE	512
     46       1.3      cgd 
     47       1.1      cgd /*
     48       1.3      cgd  * int of_decode_int(p)
     49       1.3      cgd  *
     50       1.3      cgd  * This routine converts OFW encoded-int datums
     51       1.3      cgd  * into the integer format of the host machine.
     52       1.1      cgd  *
     53       1.3      cgd  * It is primarily used to convert integer properties
     54       1.3      cgd  * returned by the OF_getprop routine.
     55       1.2      cgd  *
     56       1.2      cgd  * Arguments:
     57       1.2      cgd  *	p		pointer to unsigned char array which is an
     58       1.2      cgd  *			OFW-encoded integer.
     59       1.2      cgd  *
     60       1.2      cgd  * Return Value:
     61       1.2      cgd  *	Decoded integer value of argument p.
     62       1.3      cgd  *
     63       1.3      cgd  * Side Effects:
     64       1.3      cgd  *	None.
     65       1.1      cgd  */
     66       1.1      cgd int
     67       1.1      cgd of_decode_int(p)
     68       1.1      cgd 	const unsigned char *p;
     69       1.1      cgd {
     70       1.1      cgd 	unsigned int i = *p++ << 8;
     71       1.1      cgd 	i = (i + *p++) << 8;
     72       1.1      cgd 	i = (i + *p++) << 8;
     73       1.1      cgd 	return (i + *p);
     74       1.2      cgd }
     75       1.2      cgd 
     76       1.2      cgd /*
     77       1.3      cgd  * int of_compatible(phandle, strings)
     78       1.3      cgd  *
     79       1.2      cgd  * This routine checks an OFW node's "compatible" entry to see if
     80       1.2      cgd  * it matches any of the provided strings.
     81       1.2      cgd  *
     82       1.2      cgd  * It should be used when determining whether a driver can drive
     83       1.2      cgd  * a partcular device.
     84       1.2      cgd  *
     85       1.2      cgd  * Arguments:
     86       1.2      cgd  *	phandle		OFW phandle of device to be checked for
     87       1.2      cgd  *			compatibility.
     88       1.2      cgd  *	strings		Array of containing expected "compatibility"
     89       1.2      cgd  *			property values, presence of any of which
     90       1.2      cgd  *			indicates compatibility.
     91       1.2      cgd  *
     92       1.2      cgd  * Return Value:
     93       1.8      wiz  *	-1 if none of the strings are found in phandle's "compatibility"
     94       1.2      cgd  *	property, or the index of the string in "strings" of the first
     95       1.2      cgd  *	string found in phandle's "compatibility" property.
     96       1.3      cgd  *
     97       1.3      cgd  * Side Effects:
     98       1.3      cgd  *	None.
     99       1.2      cgd  */
    100       1.2      cgd int
    101      1.12  garbled of_compatible(int phandle, const char * const *strings)
    102       1.2      cgd {
    103       1.2      cgd 	int len, allocated, rv;
    104       1.2      cgd 	char *buf;
    105       1.2      cgd 	const char *sp, *nsp;
    106       1.2      cgd 
    107       1.2      cgd 	len = OF_getproplen(phandle, "compatible");
    108       1.2      cgd 	if (len <= 0)
    109       1.2      cgd 		return (-1);
    110       1.2      cgd 
    111       1.3      cgd 	if (len > OFW_MAX_STACK_BUF_SIZE) {
    112       1.2      cgd 		buf = malloc(len, M_TEMP, M_WAITOK);
    113       1.2      cgd 		allocated = 1;
    114       1.2      cgd 	} else {
    115       1.2      cgd 		buf = alloca(len);
    116       1.2      cgd 		allocated = 0;
    117       1.2      cgd 	}
    118       1.2      cgd 
    119       1.2      cgd 	/* 'compatible' size should not change. */
    120       1.2      cgd 	if (OF_getprop(phandle, "compatible", buf, len) != len) {
    121       1.2      cgd 		rv = -1;
    122       1.2      cgd 		goto out;
    123       1.2      cgd 	}
    124       1.2      cgd 
    125       1.2      cgd 	sp = buf;
    126       1.2      cgd 	while (len && (nsp = memchr(sp, 0, len)) != NULL) {
    127       1.2      cgd 		/* look for a match among the strings provided */
    128       1.2      cgd 		for (rv = 0; strings[rv] != NULL; rv++)
    129       1.2      cgd 			if (strcmp(sp, strings[rv]) == 0)
    130       1.2      cgd 				goto out;
    131       1.2      cgd 
    132       1.2      cgd 		nsp++;			/* skip over NUL char */
    133       1.2      cgd 		len -= (nsp - sp);
    134       1.2      cgd 		sp = nsp;
    135       1.2      cgd 	}
    136       1.2      cgd 	rv = -1;
    137       1.2      cgd 
    138       1.2      cgd out:
    139       1.2      cgd 	if (allocated)
    140       1.2      cgd 		free(buf, M_TEMP);
    141       1.2      cgd 	return (rv);
    142      1.10    perry 
    143       1.3      cgd }
    144       1.3      cgd 
    145       1.3      cgd /*
    146       1.4      cgd  * int of_packagename(phandle, buf, bufsize)
    147       1.3      cgd  *
    148       1.3      cgd  * This routine places the last component of an OFW node's name
    149       1.3      cgd  * into a user-provided buffer.
    150       1.3      cgd  *
    151       1.3      cgd  * It can be used during autoconfiguration to make printing of
    152       1.3      cgd  * device names more informative.
    153       1.3      cgd  *
    154       1.3      cgd  * Arguments:
    155       1.3      cgd  *	phandle		OFW phandle of device whose name name is
    156       1.3      cgd  *			desired.
    157       1.3      cgd  *	buf		Buffer to contain device name, provided by
    158       1.3      cgd  *			caller.  (For now, must be at least 4
    159       1.3      cgd  *			bytes long.)
    160       1.3      cgd  *	bufsize		Length of buffer referenced by 'buf', in
    161       1.3      cgd  *			bytes.
    162       1.3      cgd  *
    163       1.3      cgd  * Return Value:
    164       1.3      cgd  *	-1 if the device path name could not be obtained or would
    165       1.3      cgd  *	not fit in the allocated temporary buffer, or zero otherwise
    166       1.6    soren  *	(meaning that the leaf node name was successfully extracted).
    167       1.3      cgd  *
    168       1.3      cgd  * Side Effects:
    169       1.3      cgd  *	If the leaf node name was successfully extracted, 'buf' is
    170       1.3      cgd  *	filled in with at most 'bufsize' bytes of the leaf node
    171       1.3      cgd  *	name.  If the leaf node was not successfully extracted, a
    172       1.3      cgd  *	somewhat meaningful string is placed in the buffer.  In
    173       1.3      cgd  *	either case, the contents of 'buf' will be NUL-terminated.
    174       1.3      cgd  */
    175       1.3      cgd int
    176      1.12  garbled of_packagename(int phandle, char *buf, int bufsize)
    177       1.3      cgd {
    178       1.3      cgd 	char *pbuf;
    179       1.3      cgd 	const char *lastslash;
    180       1.3      cgd 	int l, rv;
    181       1.3      cgd 
    182       1.3      cgd 	pbuf = malloc(OFW_PATH_BUF_SIZE, M_TEMP, M_WAITOK);
    183       1.3      cgd 	l = OF_package_to_path(phandle, pbuf, OFW_PATH_BUF_SIZE);
    184       1.3      cgd 
    185       1.3      cgd 	/* check that we could get the name, and that it's not too long. */
    186       1.3      cgd 	if (l < 0 ||
    187       1.3      cgd 	    (l == OFW_PATH_BUF_SIZE && pbuf[OFW_PATH_BUF_SIZE - 1] != '\0')) {
    188       1.3      cgd 		if (bufsize >= 25)
    189       1.9   itojun 			snprintf(buf, bufsize, "??? (phandle 0x%x)", phandle);
    190       1.3      cgd 		else if (bufsize >= 4)
    191       1.9   itojun 			strlcpy(buf, "???", bufsize);
    192       1.3      cgd 		else
    193       1.4      cgd 			panic("of_packagename: bufsize = %d is silly",
    194       1.4      cgd 			    bufsize);
    195       1.3      cgd 		rv = -1;
    196       1.3      cgd 	} else {
    197       1.5  mycroft 		pbuf[l] = '\0';
    198       1.3      cgd 		lastslash = strrchr(pbuf, '/');
    199       1.9   itojun 		strlcpy(buf, (lastslash == NULL) ? pbuf : (lastslash + 1),
    200       1.3      cgd 		    bufsize);
    201       1.3      cgd 		rv = 0;
    202       1.3      cgd 	}
    203       1.3      cgd 
    204       1.3      cgd 	free(pbuf, M_TEMP);
    205       1.3      cgd 	return (rv);
    206       1.1      cgd }
    207      1.12  garbled 
    208      1.12  garbled /*
    209  1.12.2.1      mjf  * Find the first child of a given node that matches name. Does not recurse.
    210      1.12  garbled  */
    211      1.12  garbled int
    212      1.12  garbled of_find_firstchild_byname(int node, const char *name)
    213      1.12  garbled {
    214      1.12  garbled 	char namex[32];
    215      1.12  garbled 	int nn;
    216      1.12  garbled 
    217      1.12  garbled 	for (nn = OF_child(node); nn; nn = OF_peer(nn)) {
    218      1.12  garbled 		memset(namex, 0, sizeof(namex));
    219      1.12  garbled 		if (OF_getprop(nn, "name", namex, sizeof(namex)) == -1)
    220      1.12  garbled 			continue;
    221      1.12  garbled 		if (strcmp(name, namex) == 0)
    222      1.12  garbled 			return nn;
    223      1.12  garbled 	}
    224      1.12  garbled 	return -1;
    225      1.12  garbled }
    226  1.12.2.1      mjf 
    227  1.12.2.1      mjf /*
    228  1.12.2.1      mjf  * Find a give node by name.  Recurses, and seems to walk upwards too.
    229  1.12.2.1      mjf  */
    230  1.12.2.1      mjf 
    231  1.12.2.1      mjf int
    232  1.12.2.1      mjf of_getnode_byname(int start, const char *target)
    233  1.12.2.1      mjf {
    234  1.12.2.1      mjf 	int node, next;
    235  1.12.2.1      mjf 	char name[64];
    236  1.12.2.1      mjf 
    237  1.12.2.1      mjf 	if (start == 0)
    238  1.12.2.1      mjf 		start = OF_peer(0);
    239  1.12.2.1      mjf 
    240  1.12.2.1      mjf 	for (node = start; node; node = next) {
    241  1.12.2.1      mjf 		memset(name, 0, sizeof name);
    242  1.12.2.1      mjf 		OF_getprop(node, "name", name, sizeof name - 1);
    243  1.12.2.1      mjf 		if (strcmp(name, target) == 0)
    244  1.12.2.1      mjf 			break;
    245  1.12.2.1      mjf 
    246  1.12.2.1      mjf 		if ((next = OF_child(node)) != 0)
    247  1.12.2.1      mjf 			continue;
    248  1.12.2.1      mjf 
    249  1.12.2.1      mjf 		while (node) {
    250  1.12.2.1      mjf 			if ((next = OF_peer(node)) != 0)
    251  1.12.2.1      mjf 				break;
    252  1.12.2.1      mjf 			node = OF_parent(node);
    253  1.12.2.1      mjf 		}
    254  1.12.2.1      mjf 	}
    255  1.12.2.1      mjf 
    256  1.12.2.1      mjf 	/* XXX is this correct? */
    257  1.12.2.1      mjf 	return node;
    258  1.12.2.1      mjf }
    259  1.12.2.1      mjf 
    260  1.12.2.1      mjf /*
    261  1.12.2.1      mjf  * Create a uint32_t integer property from an OFW node property.
    262  1.12.2.1      mjf  */
    263  1.12.2.1      mjf 
    264  1.12.2.1      mjf boolean_t
    265  1.12.2.1      mjf of_to_uint32_prop(prop_dictionary_t dict, int node, const char *ofname,
    266  1.12.2.1      mjf     const char *propname)
    267  1.12.2.1      mjf {
    268  1.12.2.1      mjf 	uint32_t prop;
    269  1.12.2.1      mjf 
    270  1.12.2.1      mjf 	if (OF_getprop(node, ofname, &prop, sizeof(prop)) != sizeof(prop))
    271  1.12.2.1      mjf 		return FALSE;
    272  1.12.2.1      mjf 
    273  1.12.2.1      mjf 	return(prop_dictionary_set_uint32(dict, propname, prop));
    274  1.12.2.1      mjf }
    275  1.12.2.1      mjf 
    276  1.12.2.1      mjf /*
    277  1.12.2.1      mjf  * Create a data property from an OFW node property.  Max size of 256bytes.
    278  1.12.2.1      mjf  */
    279  1.12.2.1      mjf 
    280  1.12.2.1      mjf boolean_t
    281  1.12.2.1      mjf of_to_dataprop(prop_dictionary_t dict, int node, const char *ofname,
    282  1.12.2.1      mjf     const char *propname)
    283  1.12.2.1      mjf {
    284  1.12.2.1      mjf 	prop_data_t data;
    285  1.12.2.1      mjf 	int len;
    286  1.12.2.1      mjf 	uint8_t prop[256];
    287  1.12.2.1      mjf 
    288  1.12.2.1      mjf 	len = OF_getprop(node, ofname, prop, 256);
    289  1.12.2.1      mjf 	if (len < 1)
    290  1.12.2.1      mjf 		return FALSE;
    291  1.12.2.1      mjf 
    292  1.12.2.1      mjf 	data = prop_data_create_data(prop, len);
    293  1.12.2.1      mjf 	return(prop_dictionary_set(dict, propname, data));
    294  1.12.2.1      mjf }
    295