Home | History | Annotate | Line # | Download | only in ofw
ofw_subr.c revision 1.8
      1  1.8      wiz /*	$NetBSD: ofw_subr.c,v 1.8 2003/01/06 13:26:27 wiz 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.8      wiz __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.8 2003/01/06 13:26:27 wiz 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.2      cgd of_compatible(phandle, strings)
    102  1.2      cgd 	int phandle;
    103  1.2      cgd 	const char * const *strings;
    104  1.2      cgd {
    105  1.2      cgd 	int len, allocated, rv;
    106  1.2      cgd 	char *buf;
    107  1.2      cgd 	const char *sp, *nsp;
    108  1.2      cgd 
    109  1.2      cgd 	len = OF_getproplen(phandle, "compatible");
    110  1.2      cgd 	if (len <= 0)
    111  1.2      cgd 		return (-1);
    112  1.2      cgd 
    113  1.3      cgd 	if (len > OFW_MAX_STACK_BUF_SIZE) {
    114  1.2      cgd 		buf = malloc(len, M_TEMP, M_WAITOK);
    115  1.2      cgd 		allocated = 1;
    116  1.2      cgd 	} else {
    117  1.2      cgd 		buf = alloca(len);
    118  1.2      cgd 		allocated = 0;
    119  1.2      cgd 	}
    120  1.2      cgd 
    121  1.2      cgd 	/* 'compatible' size should not change. */
    122  1.2      cgd 	if (OF_getprop(phandle, "compatible", buf, len) != len) {
    123  1.2      cgd 		rv = -1;
    124  1.2      cgd 		goto out;
    125  1.2      cgd 	}
    126  1.2      cgd 
    127  1.2      cgd 	sp = buf;
    128  1.2      cgd 	while (len && (nsp = memchr(sp, 0, len)) != NULL) {
    129  1.2      cgd 		/* look for a match among the strings provided */
    130  1.2      cgd 		for (rv = 0; strings[rv] != NULL; rv++)
    131  1.2      cgd 			if (strcmp(sp, strings[rv]) == 0)
    132  1.2      cgd 				goto out;
    133  1.2      cgd 
    134  1.2      cgd 		nsp++;			/* skip over NUL char */
    135  1.2      cgd 		len -= (nsp - sp);
    136  1.2      cgd 		sp = nsp;
    137  1.2      cgd 	}
    138  1.2      cgd 	rv = -1;
    139  1.2      cgd 
    140  1.2      cgd out:
    141  1.2      cgd 	if (allocated)
    142  1.2      cgd 		free(buf, M_TEMP);
    143  1.2      cgd 	return (rv);
    144  1.2      cgd 
    145  1.3      cgd }
    146  1.3      cgd 
    147  1.3      cgd /*
    148  1.4      cgd  * int of_packagename(phandle, buf, bufsize)
    149  1.3      cgd  *
    150  1.3      cgd  * This routine places the last component of an OFW node's name
    151  1.3      cgd  * into a user-provided buffer.
    152  1.3      cgd  *
    153  1.3      cgd  * It can be used during autoconfiguration to make printing of
    154  1.3      cgd  * device names more informative.
    155  1.3      cgd  *
    156  1.3      cgd  * Arguments:
    157  1.3      cgd  *	phandle		OFW phandle of device whose name name is
    158  1.3      cgd  *			desired.
    159  1.3      cgd  *	buf		Buffer to contain device name, provided by
    160  1.3      cgd  *			caller.  (For now, must be at least 4
    161  1.3      cgd  *			bytes long.)
    162  1.3      cgd  *	bufsize		Length of buffer referenced by 'buf', in
    163  1.3      cgd  *			bytes.
    164  1.3      cgd  *
    165  1.3      cgd  * Return Value:
    166  1.3      cgd  *	-1 if the device path name could not be obtained or would
    167  1.3      cgd  *	not fit in the allocated temporary buffer, or zero otherwise
    168  1.6    soren  *	(meaning that the leaf node name was successfully extracted).
    169  1.3      cgd  *
    170  1.3      cgd  * Side Effects:
    171  1.3      cgd  *	If the leaf node name was successfully extracted, 'buf' is
    172  1.3      cgd  *	filled in with at most 'bufsize' bytes of the leaf node
    173  1.3      cgd  *	name.  If the leaf node was not successfully extracted, a
    174  1.3      cgd  *	somewhat meaningful string is placed in the buffer.  In
    175  1.3      cgd  *	either case, the contents of 'buf' will be NUL-terminated.
    176  1.3      cgd  */
    177  1.3      cgd int
    178  1.4      cgd of_packagename(phandle, buf, bufsize)
    179  1.3      cgd 	int phandle;
    180  1.3      cgd 	char *buf;
    181  1.3      cgd 	int bufsize;
    182  1.3      cgd {
    183  1.3      cgd 	char *pbuf;
    184  1.3      cgd 	const char *lastslash;
    185  1.3      cgd 	int l, rv;
    186  1.3      cgd 
    187  1.3      cgd 	pbuf = malloc(OFW_PATH_BUF_SIZE, M_TEMP, M_WAITOK);
    188  1.3      cgd 	l = OF_package_to_path(phandle, pbuf, OFW_PATH_BUF_SIZE);
    189  1.3      cgd 
    190  1.3      cgd 	/* check that we could get the name, and that it's not too long. */
    191  1.3      cgd 	if (l < 0 ||
    192  1.3      cgd 	    (l == OFW_PATH_BUF_SIZE && pbuf[OFW_PATH_BUF_SIZE - 1] != '\0')) {
    193  1.3      cgd 		/* XXX should use snprintf! */
    194  1.3      cgd 		if (bufsize >= 25)
    195  1.3      cgd 			sprintf(buf, "??? (phandle 0x%x)", phandle);
    196  1.3      cgd 		else if (bufsize >= 4)
    197  1.3      cgd 			strcpy(buf, "???");
    198  1.3      cgd 		else
    199  1.4      cgd 			panic("of_packagename: bufsize = %d is silly",
    200  1.4      cgd 			    bufsize);
    201  1.3      cgd 		rv = -1;
    202  1.3      cgd 	} else {
    203  1.5  mycroft 		pbuf[l] = '\0';
    204  1.3      cgd 		lastslash = strrchr(pbuf, '/');
    205  1.3      cgd 		strncpy(buf, (lastslash == NULL) ? pbuf : (lastslash + 1),
    206  1.3      cgd 		    bufsize);
    207  1.3      cgd 		buf[bufsize - 1] = '\0'; /* in case it's fills the buffer. */
    208  1.3      cgd 		rv = 0;
    209  1.3      cgd 	}
    210  1.3      cgd 
    211  1.3      cgd 	free(pbuf, M_TEMP);
    212  1.3      cgd 	return (rv);
    213  1.1      cgd }
    214