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