1 /* $NetBSD: ofw_network_subr.c,v 1.6 2009/03/14 15:36:19 dsl Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ofw_network_subr.c,v 1.6 2009/03/14 15:36:19 dsl Exp $");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
42 #include <net/if_media.h>
44 #include <dev/ofw/openfirm.h>
46 #define OFW_MAX_STACK_BUF_SIZE 256
47 #define OFW_PATH_BUF_SIZE 512
54 int of_network_parse_network_type(const char *);
57 * int of_network_decode_media(phandle, nmediap, defmediap)
59 * This routine decodes the OFW properties `supported-network-types'
60 * and `chosen-network-type'.
63 * phandle OFW phandle of device whos network media properties
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.
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.
81 of_network_decode_media(int phandle
, int *nmediap
, int *defmediap
)
83 int i
, len
, count
, med
, *rv
= NULL
;
84 char *buf
= NULL
, *cp
, *ncp
;
86 len
= OF_getproplen(phandle
, "supported-network-types");
90 buf
= malloc(len
, M_TEMP
, M_WAITOK
);
92 /* `supported-network-types' should not change. */
93 if (OF_getprop(phandle
, "supported-network-types", buf
, len
) != len
)
97 * Count the number of entries in the array. This is kind of tricky,
98 * because they're variable-length strings, yuck.
100 for (count
= 0, cp
= buf
; cp
<= (buf
+ len
); cp
++) {
102 * If we encounter nul, that marks the end of a string,
103 * and thus one complete media description.
113 /* Allocate the return value array. */
114 rv
= malloc(count
* sizeof(int), M_DEVBUF
, M_WAITOK
);
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.
120 for (i
= 0, cp
= buf
; cp
<= (buf
+ len
) && i
< count
; ) {
122 * Find the next string now, as we may chop
123 * the current one up in the parser.
125 for (ncp
= cp
; *ncp
!= '\0'; ncp
++)
126 /* ...skip to the nul... */ ;
127 ncp
++; /* ...and now past it. */
129 med
= of_network_parse_network_type(cp
);
144 * We now have the `supported-media-types' property decoded.
145 * Next step is to decode the `chosen-media-type' property,
150 len
= OF_getproplen(phandle
, "chosen-network-type");
152 /* Property does not exist. */
157 buf
= malloc(len
, M_TEMP
, M_WAITOK
);
158 if (OF_getprop(phandle
, "chosen-network-type", buf
, len
) != len
) {
159 /* Something went wrong... */
164 *defmediap
= of_network_parse_network_type(buf
);
181 of_network_parse_network_type(const char *cp
)
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
189 * Oh yes, we also only support combinations that actually
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
},
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
);