ofw_network_subr.c revision 1.1 1 1.1 thorpej /* $NetBSD: ofw_network_subr.c,v 1.1 1998/07/22 22:04:14 thorpej 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.1 thorpej
40 1.1 thorpej #include <sys/param.h>
41 1.1 thorpej #include <sys/systm.h>
42 1.1 thorpej #include <sys/malloc.h>
43 1.1 thorpej #include <sys/socket.h>
44 1.1 thorpej
45 1.1 thorpej #include <net/if.h>
46 1.1 thorpej #include <net/if_media.h>
47 1.1 thorpej
48 1.1 thorpej #include <dev/ofw/openfirm.h>
49 1.1 thorpej
50 1.1 thorpej #define OFW_MAX_STACK_BUF_SIZE 256
51 1.1 thorpej #define OFW_PATH_BUF_SIZE 512
52 1.1 thorpej
53 1.1 thorpej struct table_entry {
54 1.1 thorpej const char *t_string;
55 1.1 thorpej int t_value;
56 1.1 thorpej };
57 1.1 thorpej
58 1.1 thorpej int of_network_parse_network_type __P((const char *));
59 1.1 thorpej
60 1.1 thorpej /*
61 1.1 thorpej * int of_network_decode_media(phandle, nmediap, defmediap)
62 1.1 thorpej *
63 1.1 thorpej * This routine decodes the OFW properties `supported-network-types'
64 1.1 thorpej * and `chosen-network-type'.
65 1.1 thorpej *
66 1.1 thorpej * Arguments:
67 1.1 thorpej * phandle OFW phandle of device whos network media properties
68 1.1 thorpej * are to be decoded.
69 1.1 thorpej * nmediap Pointer to an integer which will be initialized
70 1.1 thorpej * with the number of returned media words.
71 1.1 thorpej * defmediap Pointer to an integer which will be initialized
72 1.1 thorpej * with the default network media.
73 1.1 thorpej *
74 1.1 thorpej * Return Values:
75 1.1 thorpej * An array of integers, allocated with malloc(), containing the
76 1.1 thorpej * decoded media values. The number of elements in the array will
77 1.1 thorpej * be stored in the location pointed to by the `nmediap' argument.
78 1.1 thorpej * The default media will be stored in the location pointed to by
79 1.1 thorpej * the `defmediap' argument.
80 1.1 thorpej *
81 1.1 thorpej * Side Effects:
82 1.1 thorpej * None.
83 1.1 thorpej */
84 1.1 thorpej int *
85 1.1 thorpej of_network_decode_media(phandle, nmediap, defmediap)
86 1.1 thorpej int phandle, *nmediap, *defmediap;
87 1.1 thorpej {
88 1.1 thorpej int i, len, count, med, *rv = NULL;
89 1.1 thorpej char *buf = NULL, *cp, *ncp;
90 1.1 thorpej
91 1.1 thorpej len = OF_getproplen(phandle, "supported-network-types");
92 1.1 thorpej if (len <= 0)
93 1.1 thorpej return (NULL);
94 1.1 thorpej
95 1.1 thorpej buf = malloc(len, M_TEMP, M_WAITOK);
96 1.1 thorpej
97 1.1 thorpej /* `supported-network-types' should not change. */
98 1.1 thorpej if (OF_getprop(phandle, "supported-network-types", buf, len) != len)
99 1.1 thorpej goto bad;
100 1.1 thorpej
101 1.1 thorpej /*
102 1.1 thorpej * Count the number of entries in the array. This is kind of tricky,
103 1.1 thorpej * because they're variable-length strings, yuck.
104 1.1 thorpej */
105 1.1 thorpej for (count = 0, cp = buf; cp <= (buf + len); cp++) {
106 1.1 thorpej /*
107 1.1 thorpej * If we encounter nul, that marks the end of a string,
108 1.1 thorpej * and thus one complete media description.
109 1.1 thorpej */
110 1.1 thorpej if (*cp == '\0')
111 1.1 thorpej count++;
112 1.1 thorpej }
113 1.1 thorpej
114 1.1 thorpej /* Sanity. */
115 1.1 thorpej if (count == 0)
116 1.1 thorpej goto bad;
117 1.1 thorpej
118 1.1 thorpej /* Allocate the return value array. */
119 1.1 thorpej rv = malloc(count * sizeof(int), M_DEVBUF, M_WAITOK);
120 1.1 thorpej
121 1.1 thorpej /*
122 1.1 thorpej * Parse each media string. If we get -1 back from the parser,
123 1.1 thorpej * back off the count by one, to skip the bad entry.
124 1.1 thorpej */
125 1.1 thorpej for (i = 0, cp = buf; cp <= (buf + len) && i < count; ) {
126 1.1 thorpej /*
127 1.1 thorpej * Find the next string now, as we may chop
128 1.1 thorpej * the current one up in the parser.
129 1.1 thorpej */
130 1.1 thorpej for (ncp = cp; *ncp != '\0'; ncp++)
131 1.1 thorpej /* ...skip to the nul... */ ;
132 1.1 thorpej ncp++; /* ...and now past it. */
133 1.1 thorpej
134 1.1 thorpej med = of_network_parse_network_type(cp);
135 1.1 thorpej if (med == -1)
136 1.1 thorpej count--;
137 1.1 thorpej else {
138 1.1 thorpej rv[i] = med;
139 1.1 thorpej i++;
140 1.1 thorpej }
141 1.1 thorpej cp = ncp;
142 1.1 thorpej }
143 1.1 thorpej
144 1.1 thorpej /* Sanity... */
145 1.1 thorpej if (count == 0)
146 1.1 thorpej goto bad;
147 1.1 thorpej
148 1.1 thorpej /*
149 1.1 thorpej * We now have the `supported-media-types' property decoded.
150 1.1 thorpej * Next step is to decode the `chosen-media-type' property,
151 1.1 thorpej * if it exists.
152 1.1 thorpej */
153 1.1 thorpej free(buf, M_TEMP);
154 1.1 thorpej buf = NULL;
155 1.1 thorpej len = OF_getproplen(phandle, "chosen-network-type");
156 1.1 thorpej if (len <= 0) {
157 1.1 thorpej /* Property does not exist. */
158 1.1 thorpej *defmediap = -1;
159 1.1 thorpej goto done;
160 1.1 thorpej }
161 1.1 thorpej
162 1.1 thorpej buf = malloc(len, M_TEMP, M_WAITOK);
163 1.1 thorpej if (OF_getprop(phandle, "chosen-network-type", buf, len) != len) {
164 1.1 thorpej /* Something went wrong... */
165 1.1 thorpej *defmediap = -1;
166 1.1 thorpej goto done;
167 1.1 thorpej }
168 1.1 thorpej
169 1.1 thorpej *defmediap = of_network_parse_network_type(buf);
170 1.1 thorpej
171 1.1 thorpej done:
172 1.1 thorpej if (buf != NULL)
173 1.1 thorpej free(buf, M_TEMP);
174 1.1 thorpej *nmediap = count;
175 1.1 thorpej return (rv);
176 1.1 thorpej
177 1.1 thorpej bad:
178 1.1 thorpej if (rv != NULL)
179 1.1 thorpej free(rv, M_DEVBUF);
180 1.1 thorpej if (buf != NULL)
181 1.1 thorpej free(buf, M_TEMP);
182 1.1 thorpej return (NULL);
183 1.1 thorpej }
184 1.1 thorpej
185 1.1 thorpej int
186 1.1 thorpej of_network_parse_network_type(cp)
187 1.1 thorpej const char *cp;
188 1.1 thorpej {
189 1.1 thorpej /*
190 1.1 thorpej * We could tokenize this, but that would be a pain in
191 1.1 thorpej * the neck given how the media are described. If this
192 1.1 thorpej * table grows any larger, we may want to consider doing
193 1.1 thorpej * that.
194 1.1 thorpej *
195 1.1 thorpej * Oh yes, we also only support combinations that actually
196 1.1 thorpej * make sense.
197 1.1 thorpej */
198 1.1 thorpej static const struct table_entry mediatab[] = {
199 1.1 thorpej { "ethernet,10,rj45,half",
200 1.1 thorpej IFM_ETHER|IFM_10_T },
201 1.1 thorpej { "ethernet,10,rj45,full",
202 1.1 thorpej IFM_ETHER|IFM_10_T|IFM_FDX },
203 1.1 thorpej { "ethernet,10,aui,half",
204 1.1 thorpej IFM_ETHER|IFM_10_5, },
205 1.1 thorpej { "ethernet,10,bnc,half",
206 1.1 thorpej IFM_ETHER|IFM_10_2, },
207 1.1 thorpej { "ethernet,100,rj45,half",
208 1.1 thorpej IFM_ETHER|IFM_100_TX },
209 1.1 thorpej { "ethernet,100,rj45,full",
210 1.1 thorpej IFM_ETHER|IFM_100_TX|IFM_FDX },
211 1.1 thorpej { NULL, -1 },
212 1.1 thorpej };
213 1.1 thorpej int i;
214 1.1 thorpej
215 1.1 thorpej for (i = 0; mediatab[i].t_string != NULL; i++) {
216 1.1 thorpej if (strcmp(cp, mediatab[i].t_string) == 0)
217 1.1 thorpej return (mediatab[i].t_value);
218 1.1 thorpej }
219 1.1 thorpej
220 1.1 thorpej /* Not found. */
221 1.1 thorpej return (-1);
222 1.1 thorpej }
223