Home | History | Annotate | Line # | Download | only in ofw
ofw_subr.c revision 1.9
      1 /*	$NetBSD: ofw_subr.c,v 1.9 2004/04/22 00:17:12 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright 1998
      5  * Digital Equipment Corporation. All rights reserved.
      6  *
      7  * This software is furnished under license and may be used and
      8  * copied only in accordance with the following terms and conditions.
      9  * Subject to these conditions, you may download, copy, install,
     10  * use, modify and distribute this software in source and/or binary
     11  * form. No title or ownership is transferred hereby.
     12  *
     13  * 1) Any source code used, modified or distributed must reproduce
     14  *    and retain this copyright notice and list of conditions as
     15  *    they appear in the source file.
     16  *
     17  * 2) No right is granted to use any trade name, trademark, or logo of
     18  *    Digital Equipment Corporation. Neither the "Digital Equipment
     19  *    Corporation" name nor any trademark or logo of Digital Equipment
     20  *    Corporation may be used to endorse or promote products derived
     21  *    from this software without the prior written permission of
     22  *    Digital Equipment Corporation.
     23  *
     24  * 3) This software is provided "AS-IS" and any express or implied
     25  *    warranties, including but not limited to, any implied warranties
     26  *    of merchantability, fitness for a particular purpose, or
     27  *    non-infringement are disclaimed. In no event shall DIGITAL be
     28  *    liable for any damages whatsoever, and in particular, DIGITAL
     29  *    shall not be liable for special, indirect, consequential, or
     30  *    incidental damages or damages for lost profits, loss of
     31  *    revenue or loss of use, whether such damages arise in contract,
     32  *    negligence, tort, under statute, in equity, at law or otherwise,
     33  *    even if advised of the possibility of such damage.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.9 2004/04/22 00:17:12 itojun Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/malloc.h>
     42 #include <dev/ofw/openfirm.h>
     43 
     44 #define	OFW_MAX_STACK_BUF_SIZE	256
     45 #define	OFW_PATH_BUF_SIZE	512
     46 
     47 /*
     48  * int of_decode_int(p)
     49  *
     50  * This routine converts OFW encoded-int datums
     51  * into the integer format of the host machine.
     52  *
     53  * It is primarily used to convert integer properties
     54  * returned by the OF_getprop routine.
     55  *
     56  * Arguments:
     57  *	p		pointer to unsigned char array which is an
     58  *			OFW-encoded integer.
     59  *
     60  * Return Value:
     61  *	Decoded integer value of argument p.
     62  *
     63  * Side Effects:
     64  *	None.
     65  */
     66 int
     67 of_decode_int(p)
     68 	const unsigned char *p;
     69 {
     70 	unsigned int i = *p++ << 8;
     71 	i = (i + *p++) << 8;
     72 	i = (i + *p++) << 8;
     73 	return (i + *p);
     74 }
     75 
     76 /*
     77  * int of_compatible(phandle, strings)
     78  *
     79  * This routine checks an OFW node's "compatible" entry to see if
     80  * it matches any of the provided strings.
     81  *
     82  * It should be used when determining whether a driver can drive
     83  * a partcular device.
     84  *
     85  * Arguments:
     86  *	phandle		OFW phandle of device to be checked for
     87  *			compatibility.
     88  *	strings		Array of containing expected "compatibility"
     89  *			property values, presence of any of which
     90  *			indicates compatibility.
     91  *
     92  * Return Value:
     93  *	-1 if none of the strings are found in phandle's "compatibility"
     94  *	property, or the index of the string in "strings" of the first
     95  *	string found in phandle's "compatibility" property.
     96  *
     97  * Side Effects:
     98  *	None.
     99  */
    100 int
    101 of_compatible(phandle, strings)
    102 	int phandle;
    103 	const char * const *strings;
    104 {
    105 	int len, allocated, rv;
    106 	char *buf;
    107 	const char *sp, *nsp;
    108 
    109 	len = OF_getproplen(phandle, "compatible");
    110 	if (len <= 0)
    111 		return (-1);
    112 
    113 	if (len > OFW_MAX_STACK_BUF_SIZE) {
    114 		buf = malloc(len, M_TEMP, M_WAITOK);
    115 		allocated = 1;
    116 	} else {
    117 		buf = alloca(len);
    118 		allocated = 0;
    119 	}
    120 
    121 	/* 'compatible' size should not change. */
    122 	if (OF_getprop(phandle, "compatible", buf, len) != len) {
    123 		rv = -1;
    124 		goto out;
    125 	}
    126 
    127 	sp = buf;
    128 	while (len && (nsp = memchr(sp, 0, len)) != NULL) {
    129 		/* look for a match among the strings provided */
    130 		for (rv = 0; strings[rv] != NULL; rv++)
    131 			if (strcmp(sp, strings[rv]) == 0)
    132 				goto out;
    133 
    134 		nsp++;			/* skip over NUL char */
    135 		len -= (nsp - sp);
    136 		sp = nsp;
    137 	}
    138 	rv = -1;
    139 
    140 out:
    141 	if (allocated)
    142 		free(buf, M_TEMP);
    143 	return (rv);
    144 
    145 }
    146 
    147 /*
    148  * int of_packagename(phandle, buf, bufsize)
    149  *
    150  * This routine places the last component of an OFW node's name
    151  * into a user-provided buffer.
    152  *
    153  * It can be used during autoconfiguration to make printing of
    154  * device names more informative.
    155  *
    156  * Arguments:
    157  *	phandle		OFW phandle of device whose name name is
    158  *			desired.
    159  *	buf		Buffer to contain device name, provided by
    160  *			caller.  (For now, must be at least 4
    161  *			bytes long.)
    162  *	bufsize		Length of buffer referenced by 'buf', in
    163  *			bytes.
    164  *
    165  * Return Value:
    166  *	-1 if the device path name could not be obtained or would
    167  *	not fit in the allocated temporary buffer, or zero otherwise
    168  *	(meaning that the leaf node name was successfully extracted).
    169  *
    170  * Side Effects:
    171  *	If the leaf node name was successfully extracted, 'buf' is
    172  *	filled in with at most 'bufsize' bytes of the leaf node
    173  *	name.  If the leaf node was not successfully extracted, a
    174  *	somewhat meaningful string is placed in the buffer.  In
    175  *	either case, the contents of 'buf' will be NUL-terminated.
    176  */
    177 int
    178 of_packagename(phandle, buf, bufsize)
    179 	int phandle;
    180 	char *buf;
    181 	int bufsize;
    182 {
    183 	char *pbuf;
    184 	const char *lastslash;
    185 	int l, rv;
    186 
    187 	pbuf = malloc(OFW_PATH_BUF_SIZE, M_TEMP, M_WAITOK);
    188 	l = OF_package_to_path(phandle, pbuf, OFW_PATH_BUF_SIZE);
    189 
    190 	/* check that we could get the name, and that it's not too long. */
    191 	if (l < 0 ||
    192 	    (l == OFW_PATH_BUF_SIZE && pbuf[OFW_PATH_BUF_SIZE - 1] != '\0')) {
    193 		if (bufsize >= 25)
    194 			snprintf(buf, bufsize, "??? (phandle 0x%x)", phandle);
    195 		else if (bufsize >= 4)
    196 			strlcpy(buf, "???", bufsize);
    197 		else
    198 			panic("of_packagename: bufsize = %d is silly",
    199 			    bufsize);
    200 		rv = -1;
    201 	} else {
    202 		pbuf[l] = '\0';
    203 		lastslash = strrchr(pbuf, '/');
    204 		strlcpy(buf, (lastslash == NULL) ? pbuf : (lastslash + 1),
    205 		    bufsize);
    206 		rv = 0;
    207 	}
    208 
    209 	free(pbuf, M_TEMP);
    210 	return (rv);
    211 }
    212