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