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