ofw_network_subr.c revision 1.9 1 1.9 thorpej /* $NetBSD: ofw_network_subr.c,v 1.9 2021/01/24 20:09:03 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.9 thorpej * Copyright (c) 1998, 2021 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.9 thorpej __KERNEL_RCSID(0, "$NetBSD: ofw_network_subr.c,v 1.9 2021/01/24 20:09:03 thorpej 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.9 thorpej #include <sys/device.h>
41 1.1 thorpej
42 1.1 thorpej #include <net/if.h>
43 1.1 thorpej #include <net/if_media.h>
44 1.1 thorpej
45 1.1 thorpej #include <dev/ofw/openfirm.h>
46 1.1 thorpej
47 1.9 thorpej static const struct device_compatible_entry media_compat[] = {
48 1.9 thorpej { .compat = "ethernet,10,rj45,half",
49 1.9 thorpej .value = IFM_ETHER | IFM_10_T },
50 1.1 thorpej
51 1.9 thorpej { .compat = "ethernet,10,rj45,full",
52 1.9 thorpej .value = IFM_ETHER | IFM_10_T | IFM_FDX },
53 1.9 thorpej
54 1.9 thorpej { .compat = "ethernet,10,aui,half",
55 1.9 thorpej .value = IFM_ETHER | IFM_10_5 },
56 1.9 thorpej
57 1.9 thorpej { .compat = "ethernet,10,bnc,half",
58 1.9 thorpej .value = IFM_ETHER | IFM_10_2 },
59 1.9 thorpej
60 1.9 thorpej { .compat = "ethernet,100,rj45,half",
61 1.9 thorpej .value = IFM_ETHER | IFM_100_TX },
62 1.9 thorpej
63 1.9 thorpej { .compat = "ethernet,100,rj45,full",
64 1.9 thorpej .value = IFM_ETHER | IFM_100_TX | IFM_FDX },
65 1.9 thorpej
66 1.9 thorpej { 0 }
67 1.1 thorpej };
68 1.1 thorpej
69 1.1 thorpej /*
70 1.1 thorpej * int of_network_decode_media(phandle, nmediap, defmediap)
71 1.1 thorpej *
72 1.1 thorpej * This routine decodes the OFW properties `supported-network-types'
73 1.1 thorpej * and `chosen-network-type'.
74 1.1 thorpej *
75 1.1 thorpej * Arguments:
76 1.1 thorpej * phandle OFW phandle of device whos network media properties
77 1.1 thorpej * are to be decoded.
78 1.1 thorpej * nmediap Pointer to an integer which will be initialized
79 1.1 thorpej * with the number of returned media words.
80 1.1 thorpej * defmediap Pointer to an integer which will be initialized
81 1.1 thorpej * with the default network media.
82 1.1 thorpej *
83 1.1 thorpej * Return Values:
84 1.1 thorpej * An array of integers, allocated with malloc(), containing the
85 1.1 thorpej * decoded media values. The number of elements in the array will
86 1.1 thorpej * be stored in the location pointed to by the `nmediap' argument.
87 1.1 thorpej * The default media will be stored in the location pointed to by
88 1.1 thorpej * the `defmediap' argument.
89 1.1 thorpej *
90 1.1 thorpej * Side Effects:
91 1.1 thorpej * None.
92 1.1 thorpej */
93 1.1 thorpej int *
94 1.7 dsl of_network_decode_media(int phandle, int *nmediap, int *defmediap)
95 1.1 thorpej {
96 1.9 thorpej const struct device_compatible_entry *dce;
97 1.9 thorpej int nmedia, len, *rv = NULL;
98 1.9 thorpej char *sl = NULL;
99 1.9 thorpej const char *cp;
100 1.9 thorpej size_t cursor;
101 1.9 thorpej unsigned int count;
102 1.1 thorpej
103 1.1 thorpej len = OF_getproplen(phandle, "supported-network-types");
104 1.1 thorpej if (len <= 0)
105 1.1 thorpej return (NULL);
106 1.1 thorpej
107 1.9 thorpej sl = malloc(len, M_TEMP, M_WAITOK);
108 1.1 thorpej
109 1.1 thorpej /* `supported-network-types' should not change. */
110 1.9 thorpej if (OF_getprop(phandle, "supported-network-types", sl, len) != len)
111 1.1 thorpej goto bad;
112 1.1 thorpej
113 1.9 thorpej count = strlist_count(sl, len);
114 1.1 thorpej
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.9 thorpej /* Parse each media string. */
122 1.9 thorpej for (nmedia = 0, cursor = 0;
123 1.9 thorpej (cp = strlist_next(sl, len, &cursor)) != NULL; ) {
124 1.9 thorpej dce = device_compatible_lookup(&cp, 1, media_compat);
125 1.9 thorpej if (dce != NULL) {
126 1.9 thorpej rv[nmedia++] = (int)dce->value;
127 1.1 thorpej }
128 1.1 thorpej }
129 1.1 thorpej /* Sanity... */
130 1.9 thorpej if (nmedia == 0)
131 1.1 thorpej goto bad;
132 1.1 thorpej
133 1.9 thorpej free(sl, M_TEMP);
134 1.9 thorpej sl = NULL;
135 1.9 thorpej
136 1.1 thorpej /*
137 1.1 thorpej * We now have the `supported-media-types' property decoded.
138 1.1 thorpej * Next step is to decode the `chosen-media-type' property,
139 1.1 thorpej * if it exists.
140 1.1 thorpej */
141 1.9 thorpej *defmediap = -1;
142 1.1 thorpej len = OF_getproplen(phandle, "chosen-network-type");
143 1.1 thorpej if (len <= 0) {
144 1.1 thorpej /* Property does not exist. */
145 1.1 thorpej *defmediap = -1;
146 1.1 thorpej goto done;
147 1.1 thorpej }
148 1.1 thorpej
149 1.9 thorpej sl = malloc(len, M_TEMP, M_WAITOK);
150 1.9 thorpej if (OF_getprop(phandle, "chosen-network-type", sl, len) != len) {
151 1.1 thorpej /* Something went wrong... */
152 1.1 thorpej goto done;
153 1.1 thorpej }
154 1.1 thorpej
155 1.9 thorpej cp = sl;
156 1.9 thorpej dce = device_compatible_lookup(&cp, 1, media_compat);
157 1.9 thorpej if (dce != NULL) {
158 1.9 thorpej *defmediap = (int)dce->value;
159 1.9 thorpej }
160 1.1 thorpej
161 1.1 thorpej done:
162 1.9 thorpej if (sl != NULL)
163 1.9 thorpej free(sl, M_TEMP);
164 1.9 thorpej *nmediap = nmedia;
165 1.1 thorpej return (rv);
166 1.1 thorpej
167 1.1 thorpej bad:
168 1.1 thorpej if (rv != NULL)
169 1.1 thorpej free(rv, M_DEVBUF);
170 1.9 thorpej if (sl != NULL)
171 1.9 thorpej free(sl, M_TEMP);
172 1.1 thorpej return (NULL);
173 1.1 thorpej }
174