Home | History | Annotate | Line # | Download | only in ofw
ofw_network_subr.c revision 1.5.8.1
      1  1.5.8.1    skrll /*	$NetBSD: ofw_network_subr.c,v 1.5.8.1 2009/04/28 07:35:55 skrll Exp $	*/
      2      1.1  thorpej 
      3      1.1  thorpej /*-
      4      1.1  thorpej  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5      1.1  thorpej  * All rights reserved.
      6      1.1  thorpej  *
      7      1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9      1.1  thorpej  * NASA Ames Research Center.
     10      1.1  thorpej  *
     11      1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12      1.1  thorpej  * modification, are permitted provided that the following conditions
     13      1.1  thorpej  * are met:
     14      1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15      1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16      1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18      1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19      1.1  thorpej  *
     20      1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21      1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22      1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23      1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24      1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25      1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26      1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27      1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28      1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29      1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30      1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31      1.1  thorpej  */
     32      1.2    lukem 
     33      1.2    lukem #include <sys/cdefs.h>
     34  1.5.8.1    skrll __KERNEL_RCSID(0, "$NetBSD: ofw_network_subr.c,v 1.5.8.1 2009/04/28 07:35:55 skrll Exp $");
     35      1.1  thorpej 
     36      1.1  thorpej #include <sys/param.h>
     37      1.1  thorpej #include <sys/systm.h>
     38      1.1  thorpej #include <sys/malloc.h>
     39      1.1  thorpej #include <sys/socket.h>
     40      1.1  thorpej 
     41      1.1  thorpej #include <net/if.h>
     42      1.1  thorpej #include <net/if_media.h>
     43      1.1  thorpej 
     44      1.1  thorpej #include <dev/ofw/openfirm.h>
     45      1.1  thorpej 
     46      1.1  thorpej #define	OFW_MAX_STACK_BUF_SIZE	256
     47      1.1  thorpej #define	OFW_PATH_BUF_SIZE	512
     48      1.1  thorpej 
     49      1.1  thorpej struct table_entry {
     50      1.1  thorpej 	const char *t_string;
     51      1.1  thorpej 	int t_value;
     52      1.1  thorpej };
     53      1.1  thorpej 
     54      1.3    perry int	of_network_parse_network_type(const char *);
     55      1.1  thorpej 
     56      1.1  thorpej /*
     57      1.1  thorpej  * int of_network_decode_media(phandle, nmediap, defmediap)
     58      1.1  thorpej  *
     59      1.1  thorpej  * This routine decodes the OFW properties `supported-network-types'
     60      1.1  thorpej  * and `chosen-network-type'.
     61      1.1  thorpej  *
     62      1.1  thorpej  * Arguments:
     63      1.1  thorpej  *	phandle		OFW phandle of device whos network media properties
     64      1.1  thorpej  *			are to be decoded.
     65      1.1  thorpej  *	nmediap		Pointer to an integer which will be initialized
     66      1.1  thorpej  *			with the number of returned media words.
     67      1.1  thorpej  *	defmediap	Pointer to an integer which will be initialized
     68      1.1  thorpej  *			with the default network media.
     69      1.1  thorpej  *
     70      1.1  thorpej  * Return Values:
     71      1.1  thorpej  *	An array of integers, allocated with malloc(), containing the
     72      1.1  thorpej  *	decoded media values.  The number of elements in the array will
     73      1.1  thorpej  *	be stored in the location pointed to by the `nmediap' argument.
     74      1.1  thorpej  *	The default media will be stored in the location pointed to by
     75      1.1  thorpej  *	the `defmediap' argument.
     76      1.1  thorpej  *
     77      1.1  thorpej  * Side Effects:
     78      1.1  thorpej  *	None.
     79      1.1  thorpej  */
     80      1.1  thorpej int *
     81  1.5.8.1    skrll of_network_decode_media(int phandle, int *nmediap, int *defmediap)
     82      1.1  thorpej {
     83      1.1  thorpej 	int i, len, count, med, *rv = NULL;
     84      1.1  thorpej 	char *buf = NULL, *cp, *ncp;
     85      1.1  thorpej 
     86      1.1  thorpej 	len = OF_getproplen(phandle, "supported-network-types");
     87      1.1  thorpej 	if (len <= 0)
     88      1.1  thorpej 		return (NULL);
     89      1.1  thorpej 
     90      1.1  thorpej 	buf = malloc(len, M_TEMP, M_WAITOK);
     91      1.1  thorpej 
     92      1.1  thorpej 	/* `supported-network-types' should not change. */
     93      1.1  thorpej 	if (OF_getprop(phandle, "supported-network-types", buf, len) != len)
     94      1.1  thorpej 		goto bad;
     95      1.1  thorpej 
     96      1.1  thorpej 	/*
     97      1.1  thorpej 	 * Count the number of entries in the array.  This is kind of tricky,
     98      1.1  thorpej 	 * because they're variable-length strings, yuck.
     99      1.1  thorpej 	 */
    100      1.1  thorpej 	for (count = 0, cp = buf; cp <= (buf + len); cp++) {
    101      1.1  thorpej 		/*
    102      1.1  thorpej 		 * If we encounter nul, that marks the end of a string,
    103      1.1  thorpej 		 * and thus one complete media description.
    104      1.1  thorpej 		 */
    105      1.1  thorpej 		if (*cp == '\0')
    106      1.1  thorpej 			count++;
    107      1.1  thorpej 	}
    108      1.1  thorpej 
    109      1.1  thorpej 	/* Sanity. */
    110      1.1  thorpej 	if (count == 0)
    111      1.1  thorpej 		goto bad;
    112      1.1  thorpej 
    113      1.1  thorpej 	/* Allocate the return value array. */
    114      1.1  thorpej 	rv = malloc(count * sizeof(int), M_DEVBUF, M_WAITOK);
    115      1.1  thorpej 
    116      1.1  thorpej 	/*
    117      1.1  thorpej 	 * Parse each media string.  If we get -1 back from the parser,
    118      1.1  thorpej 	 * back off the count by one, to skip the bad entry.
    119      1.1  thorpej 	 */
    120      1.1  thorpej 	for (i = 0, cp = buf; cp <= (buf + len) && i < count; ) {
    121      1.1  thorpej 		/*
    122      1.1  thorpej 		 * Find the next string now, as we may chop
    123      1.1  thorpej 		 * the current one up in the parser.
    124      1.1  thorpej 		 */
    125      1.1  thorpej 		for (ncp = cp; *ncp != '\0'; ncp++)
    126      1.1  thorpej 			/* ...skip to the nul... */ ;
    127      1.1  thorpej 		ncp++;	/* ...and now past it. */
    128      1.1  thorpej 
    129      1.1  thorpej 		med = of_network_parse_network_type(cp);
    130      1.1  thorpej 		if (med == -1)
    131      1.1  thorpej 			count--;
    132      1.1  thorpej 		else {
    133      1.1  thorpej 			rv[i] = med;
    134      1.1  thorpej 			i++;
    135      1.1  thorpej 		}
    136      1.1  thorpej 		cp = ncp;
    137      1.1  thorpej 	}
    138      1.1  thorpej 
    139      1.1  thorpej 	/* Sanity... */
    140      1.1  thorpej 	if (count == 0)
    141      1.1  thorpej 		goto bad;
    142      1.1  thorpej 
    143      1.1  thorpej 	/*
    144      1.1  thorpej 	 * We now have the `supported-media-types' property decoded.
    145      1.1  thorpej 	 * Next step is to decode the `chosen-media-type' property,
    146      1.1  thorpej 	 * if it exists.
    147      1.1  thorpej 	 */
    148      1.1  thorpej 	free(buf, M_TEMP);
    149      1.1  thorpej 	buf = NULL;
    150      1.1  thorpej 	len = OF_getproplen(phandle, "chosen-network-type");
    151      1.1  thorpej 	if (len <= 0) {
    152      1.1  thorpej 		/* Property does not exist. */
    153      1.1  thorpej 		*defmediap = -1;
    154      1.1  thorpej 		goto done;
    155      1.1  thorpej 	}
    156      1.1  thorpej 
    157      1.1  thorpej 	buf = malloc(len, M_TEMP, M_WAITOK);
    158      1.1  thorpej 	if (OF_getprop(phandle, "chosen-network-type", buf, len) != len) {
    159      1.1  thorpej 		/* Something went wrong... */
    160      1.1  thorpej 		*defmediap = -1;
    161      1.1  thorpej 		goto done;
    162      1.1  thorpej 	}
    163      1.1  thorpej 
    164      1.1  thorpej 	*defmediap = of_network_parse_network_type(buf);
    165      1.1  thorpej 
    166      1.1  thorpej  done:
    167      1.1  thorpej 	if (buf != NULL)
    168      1.1  thorpej 		free(buf, M_TEMP);
    169      1.1  thorpej 	*nmediap = count;
    170      1.1  thorpej 	return (rv);
    171      1.1  thorpej 
    172      1.1  thorpej  bad:
    173      1.1  thorpej 	if (rv != NULL)
    174      1.1  thorpej 		free(rv, M_DEVBUF);
    175      1.1  thorpej 	if (buf != NULL)
    176      1.1  thorpej 		free(buf, M_TEMP);
    177      1.1  thorpej 	return (NULL);
    178      1.1  thorpej }
    179      1.1  thorpej 
    180      1.1  thorpej int
    181  1.5.8.1    skrll of_network_parse_network_type(const char *cp)
    182      1.1  thorpej {
    183      1.1  thorpej 	/*
    184      1.1  thorpej 	 * We could tokenize this, but that would be a pain in
    185      1.1  thorpej 	 * the neck given how the media are described.  If this
    186      1.1  thorpej 	 * table grows any larger, we may want to consider doing
    187      1.1  thorpej 	 * that.
    188      1.1  thorpej 	 *
    189      1.1  thorpej 	 * Oh yes, we also only support combinations that actually
    190      1.1  thorpej 	 * make sense.
    191      1.1  thorpej 	 */
    192      1.1  thorpej 	static const struct table_entry mediatab[] = {
    193      1.1  thorpej 		{ "ethernet,10,rj45,half",
    194      1.1  thorpej 		  IFM_ETHER|IFM_10_T },
    195      1.1  thorpej 		{ "ethernet,10,rj45,full",
    196      1.1  thorpej 		  IFM_ETHER|IFM_10_T|IFM_FDX },
    197      1.1  thorpej 		{ "ethernet,10,aui,half",
    198      1.1  thorpej 		  IFM_ETHER|IFM_10_5, },
    199      1.1  thorpej 		{ "ethernet,10,bnc,half",
    200      1.1  thorpej 		  IFM_ETHER|IFM_10_2, },
    201      1.1  thorpej 		{ "ethernet,100,rj45,half",
    202      1.1  thorpej 		  IFM_ETHER|IFM_100_TX },
    203      1.1  thorpej 		{ "ethernet,100,rj45,full",
    204      1.1  thorpej 		  IFM_ETHER|IFM_100_TX|IFM_FDX },
    205      1.1  thorpej 		{ NULL, -1 },
    206      1.1  thorpej 	};
    207      1.1  thorpej 	int i;
    208      1.1  thorpej 
    209      1.1  thorpej 	for (i = 0; mediatab[i].t_string != NULL; i++) {
    210      1.1  thorpej 		if (strcmp(cp, mediatab[i].t_string) == 0)
    211      1.1  thorpej 			return (mediatab[i].t_value);
    212      1.1  thorpej 	}
    213      1.1  thorpej 
    214      1.1  thorpej 	/* Not found. */
    215      1.1  thorpej 	return (-1);
    216      1.1  thorpej }
    217