Home | History | Annotate | Line # | Download | only in ofw
ofw_subr.c revision 1.2
      1 /*	$NetBSD: ofw_subr.c,v 1.2 1998/01/28 00:01:01 cgd 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/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/malloc.h>
     39 #include <dev/ofw/openfirm.h>
     40 
     41 /*
     42  *  This routine converts OFW encoded-int datums
     43  *  into the integer format of the host machine.
     44  *
     45  *  It is primarily used to convert integer properties
     46  *  returned by the OF_getprop routine.
     47  *
     48  * Arguments:
     49  *	p		pointer to unsigned char array which is an
     50  *			OFW-encoded integer.
     51  *
     52  * Return Value:
     53  *	Decoded integer value of argument p.
     54  */
     55 int
     56 of_decode_int(p)
     57 	const unsigned char *p;
     58 {
     59 	unsigned int i = *p++ << 8;
     60 	i = (i + *p++) << 8;
     61 	i = (i + *p++) << 8;
     62 	return (i + *p);
     63 }
     64 
     65 /*
     66  * This routine checks an OFW node's "compatible" entry to see if
     67  * it matches any of the provided strings.
     68  *
     69  * It should be used when determining whether a driver can drive
     70  * a partcular device.
     71  *
     72  *
     73  * Arguments:
     74  *	phandle		OFW phandle of device to be checked for
     75  *			compatibility.
     76  *	strings		Array of containing expected "compatibility"
     77  *			property values, presence of any of which
     78  *			indicates compatibility.
     79  *
     80  * Return Value:
     81  *	-1 if none of the strings are found in phandle's "compatiblity"
     82  *	property, or the index of the string in "strings" of the first
     83  *	string found in phandle's "compatibility" property.
     84  */
     85 int
     86 of_compatible(phandle, strings)
     87 	int phandle;
     88 	const char * const *strings;
     89 {
     90 	int len, allocated, rv;
     91 	char *buf;
     92 	const char *sp, *nsp;
     93 
     94 	len = OF_getproplen(phandle, "compatible");
     95 	if (len <= 0)
     96 		return (-1);
     97 
     98 	if (len > 256) {
     99 		buf = malloc(len, M_TEMP, M_WAITOK);
    100 		allocated = 1;
    101 	} else {
    102 		buf = alloca(len);
    103 		allocated = 0;
    104 	}
    105 
    106 	/* 'compatible' size should not change. */
    107 	if (OF_getprop(phandle, "compatible", buf, len) != len) {
    108 		rv = -1;
    109 		goto out;
    110 	}
    111 
    112 	sp = buf;
    113 	while (len && (nsp = memchr(sp, 0, len)) != NULL) {
    114 		/* look for a match among the strings provided */
    115 		for (rv = 0; strings[rv] != NULL; rv++)
    116 			if (strcmp(sp, strings[rv]) == 0)
    117 				goto out;
    118 
    119 		nsp++;			/* skip over NUL char */
    120 		len -= (nsp - sp);
    121 		sp = nsp;
    122 	}
    123 	rv = -1;
    124 
    125 out:
    126 	if (allocated)
    127 		free(buf, M_TEMP);
    128 	return (rv);
    129 
    130 }
    131