ofw_network_subr.c revision 1.6 1 1.6 dsl /* $NetBSD: ofw_network_subr.c,v 1.6 2009/03/14 15:36:19 dsl 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.6 dsl __KERNEL_RCSID(0, "$NetBSD: ofw_network_subr.c,v 1.6 2009/03/14 15:36:19 dsl 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.1 thorpej of_network_decode_media(phandle, nmediap, defmediap)
82 1.1 thorpej int phandle, *nmediap, *defmediap;
83 1.1 thorpej {
84 1.1 thorpej int i, len, count, med, *rv = NULL;
85 1.1 thorpej char *buf = NULL, *cp, *ncp;
86 1.1 thorpej
87 1.1 thorpej len = OF_getproplen(phandle, "supported-network-types");
88 1.1 thorpej if (len <= 0)
89 1.1 thorpej return (NULL);
90 1.1 thorpej
91 1.1 thorpej buf = malloc(len, M_TEMP, M_WAITOK);
92 1.1 thorpej
93 1.1 thorpej /* `supported-network-types' should not change. */
94 1.1 thorpej if (OF_getprop(phandle, "supported-network-types", buf, len) != len)
95 1.1 thorpej goto bad;
96 1.1 thorpej
97 1.1 thorpej /*
98 1.1 thorpej * Count the number of entries in the array. This is kind of tricky,
99 1.1 thorpej * because they're variable-length strings, yuck.
100 1.1 thorpej */
101 1.1 thorpej for (count = 0, cp = buf; cp <= (buf + len); cp++) {
102 1.1 thorpej /*
103 1.1 thorpej * If we encounter nul, that marks the end of a string,
104 1.1 thorpej * and thus one complete media description.
105 1.1 thorpej */
106 1.1 thorpej if (*cp == '\0')
107 1.1 thorpej count++;
108 1.1 thorpej }
109 1.1 thorpej
110 1.1 thorpej /* Sanity. */
111 1.1 thorpej if (count == 0)
112 1.1 thorpej goto bad;
113 1.1 thorpej
114 1.1 thorpej /* Allocate the return value array. */
115 1.1 thorpej rv = malloc(count * sizeof(int), M_DEVBUF, M_WAITOK);
116 1.1 thorpej
117 1.1 thorpej /*
118 1.1 thorpej * Parse each media string. If we get -1 back from the parser,
119 1.1 thorpej * back off the count by one, to skip the bad entry.
120 1.1 thorpej */
121 1.1 thorpej for (i = 0, cp = buf; cp <= (buf + len) && i < count; ) {
122 1.1 thorpej /*
123 1.1 thorpej * Find the next string now, as we may chop
124 1.1 thorpej * the current one up in the parser.
125 1.1 thorpej */
126 1.1 thorpej for (ncp = cp; *ncp != '\0'; ncp++)
127 1.1 thorpej /* ...skip to the nul... */ ;
128 1.1 thorpej ncp++; /* ...and now past it. */
129 1.1 thorpej
130 1.1 thorpej med = of_network_parse_network_type(cp);
131 1.1 thorpej if (med == -1)
132 1.1 thorpej count--;
133 1.1 thorpej else {
134 1.1 thorpej rv[i] = med;
135 1.1 thorpej i++;
136 1.1 thorpej }
137 1.1 thorpej cp = ncp;
138 1.1 thorpej }
139 1.1 thorpej
140 1.1 thorpej /* Sanity... */
141 1.1 thorpej if (count == 0)
142 1.1 thorpej goto bad;
143 1.1 thorpej
144 1.1 thorpej /*
145 1.1 thorpej * We now have the `supported-media-types' property decoded.
146 1.1 thorpej * Next step is to decode the `chosen-media-type' property,
147 1.1 thorpej * if it exists.
148 1.1 thorpej */
149 1.1 thorpej free(buf, M_TEMP);
150 1.1 thorpej buf = NULL;
151 1.1 thorpej len = OF_getproplen(phandle, "chosen-network-type");
152 1.1 thorpej if (len <= 0) {
153 1.1 thorpej /* Property does not exist. */
154 1.1 thorpej *defmediap = -1;
155 1.1 thorpej goto done;
156 1.1 thorpej }
157 1.1 thorpej
158 1.1 thorpej buf = malloc(len, M_TEMP, M_WAITOK);
159 1.1 thorpej if (OF_getprop(phandle, "chosen-network-type", buf, len) != len) {
160 1.1 thorpej /* Something went wrong... */
161 1.1 thorpej *defmediap = -1;
162 1.1 thorpej goto done;
163 1.1 thorpej }
164 1.1 thorpej
165 1.1 thorpej *defmediap = of_network_parse_network_type(buf);
166 1.1 thorpej
167 1.1 thorpej done:
168 1.1 thorpej if (buf != NULL)
169 1.1 thorpej free(buf, M_TEMP);
170 1.1 thorpej *nmediap = count;
171 1.1 thorpej return (rv);
172 1.1 thorpej
173 1.1 thorpej bad:
174 1.1 thorpej if (rv != NULL)
175 1.1 thorpej free(rv, M_DEVBUF);
176 1.1 thorpej if (buf != NULL)
177 1.1 thorpej free(buf, M_TEMP);
178 1.1 thorpej return (NULL);
179 1.1 thorpej }
180 1.1 thorpej
181 1.1 thorpej int
182 1.6 dsl of_network_parse_network_type(const char *cp)
183 1.1 thorpej {
184 1.1 thorpej /*
185 1.1 thorpej * We could tokenize this, but that would be a pain in
186 1.1 thorpej * the neck given how the media are described. If this
187 1.1 thorpej * table grows any larger, we may want to consider doing
188 1.1 thorpej * that.
189 1.1 thorpej *
190 1.1 thorpej * Oh yes, we also only support combinations that actually
191 1.1 thorpej * make sense.
192 1.1 thorpej */
193 1.1 thorpej static const struct table_entry mediatab[] = {
194 1.1 thorpej { "ethernet,10,rj45,half",
195 1.1 thorpej IFM_ETHER|IFM_10_T },
196 1.1 thorpej { "ethernet,10,rj45,full",
197 1.1 thorpej IFM_ETHER|IFM_10_T|IFM_FDX },
198 1.1 thorpej { "ethernet,10,aui,half",
199 1.1 thorpej IFM_ETHER|IFM_10_5, },
200 1.1 thorpej { "ethernet,10,bnc,half",
201 1.1 thorpej IFM_ETHER|IFM_10_2, },
202 1.1 thorpej { "ethernet,100,rj45,half",
203 1.1 thorpej IFM_ETHER|IFM_100_TX },
204 1.1 thorpej { "ethernet,100,rj45,full",
205 1.1 thorpej IFM_ETHER|IFM_100_TX|IFM_FDX },
206 1.1 thorpej { NULL, -1 },
207 1.1 thorpej };
208 1.1 thorpej int i;
209 1.1 thorpej
210 1.1 thorpej for (i = 0; mediatab[i].t_string != NULL; i++) {
211 1.1 thorpej if (strcmp(cp, mediatab[i].t_string) == 0)
212 1.1 thorpej return (mediatab[i].t_value);
213 1.1 thorpej }
214 1.1 thorpej
215 1.1 thorpej /* Not found. */
216 1.1 thorpej return (-1);
217 1.1 thorpej }
218