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