Newer
Older
/*
* ethernet.c - file implements library APIs
*
* Copyright (C) 2018 iopsys Software Solutions AB. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <stdbool.h>
#include <linux/sockios.h>
#include <linux/mii.h>
#include "ethernet.h"
#ifdef IOPSYS_BROADCOM
extern const struct eth_ops bcm_eth_ops;
#else
extern const struct eth_ops ethsw_ops;
#endif
const struct eth_ops *eth_ops[] = {
#ifdef IOPSYS_BROADCOM
&bcm_eth_ops,
#else
ðsw_ops,
#endif
};
const struct eth_ops *get_eth_driver(char *name)
{
int i;
for (i = 0; i < sizeof(eth_ops)/sizeof(eth_ops[0]); i++)
if (!strncmp(eth_ops[i]->name, name,
strlen(eth_ops[i]->name)))
return eth_ops[i];
return NULL;
}
int eth_up(char *name)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->up)
return eth->up(name);
return -1;
}
int eth_down(char *name)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->down)
return eth->down(name);
return -1;
}
int eth_get_link_settings(char *name, struct eth_link *link)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->get_link_settings)
return eth->get_link_settings(name, link);
return -1;
}
int eth_set_link_settings(char *name, struct eth_link link)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->set_link_settings)
return eth->set_link_settings(name, link);
return -1;
}
int eth_get_operstate(char *name, enum eth_operstate *op)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->get_operstate)
return eth->get_operstate(name, op);
return -1;
}
int eth_set_operstate(char *name, enum eth_operstate op)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->set_operstate)
return eth->set_operstate(name, op);
return -1;
}
int eth_get_stats(char *name, struct eth_counters *c)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->get_stats)
return eth->get_stats(name, c);
return -1;
}
int eth_poweron_phy(char *name, struct eth_phy p)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->poweron_phy)
return eth->poweron_phy(name, p);
return -1;
}
int eth_poweroff_phy(char *name, struct eth_phy p)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->poweroff_phy)
return eth->poweroff_phy(name, p);
return -1;
}
int eth_reset_phy(char *name, int phy_id)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->reset_phy)
return eth->reset_phy(name, phy_id);
else
return eth_mii_reset_phy(name, phy_id);
}
int eth_get_phy_id(char *name, int port, int *phy_id)
{
const struct eth_ops *eth = get_eth_driver(name);
if (eth && eth->get_phy_id)
return eth->get_phy_id(name, port, phy_id);
else
return eth_mii_get_phy_id(name, port, phy_id);
}
int eth_ioctl(char *name, int cmd, void *in, int len)
{
struct ifreq ifr;
int s;
strncpy(ifr.ifr_name, name, IFNAMSIZ);
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
return -1;
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
fprintf(stderr, "SIOCGIFINDEX failed!\n");
close(s);
return -1;
}
if (len && in)
ifr.ifr_data = (caddr_t)in;
if (ioctl(s, cmd, &ifr) < 0) {
close(s);
return -1;
}
close(s);
return 0;
}
int eth_mii_get_phy_id(char *ifname, int port, int *phy_id)
{
struct mii_ioctl_data *mii;
struct ifreq ifr;
int s;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
return -1;
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
fprintf(stderr, "SIOCGIFINDEX failed!\n");
close(s);
return -1;
}
mii = if_mii(&ifr);
memset(mii, 0, sizeof(struct mii_ioctl_data));
mii->val_in = port;
if (ioctl(s, SIOCGMIIPHY, &ifr) < 0) {
fprintf(stderr, "SIOCGMIIPHY failed!\n");
close(s);
return -1;
}
if (phy_id)
*phy_id = mii->phy_id;
//fprintf(stderr, "%s: phy_id = %d\n", ifname, *phy_id);
close(s);
return 0;
}
static int eth_mii_ioctl(char *name, int cmd, int phy_id, int reg,
int in, int *out)
{
struct mii_ioctl_data *mii;
struct ifreq ifr;
int s;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
return -1;
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, name, IFNAMSIZ);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
fprintf(stderr, "SIOCGIFINDEX failed!\n");
close(s);
return -1;
}
mii = if_mii(&ifr);
memset(mii, 0, sizeof(struct mii_ioctl_data));
//PHYID_2_MII_IOCTL(phy_id, mii);
mii->phy_id = phy_id;
mii->reg_num = reg;
mii->val_in = in;
if (ioctl(s, cmd, &ifr) < 0) {
fprintf(stderr, "MII cmd on %s failed\n", ifr.ifr_name);
close(s);
return -1;
}
if (out)
*out = mii->val_out; // FIXME?
close(s);
return 0;
}
int eth_mii_read(char *name, int phy_id, int reg, int *out)
{
return eth_mii_ioctl(name, SIOCGMIIREG, phy_id, reg, 0, out);
}
int eth_mii_write(char *name, int phy_id, int reg, int in)
{
return eth_mii_ioctl(name, SIOCGMIIREG, phy_id, reg, in, NULL);
}
int eth_mii_reset_phy(char *name, int phy_id)
{
return eth_mii_ioctl(name, SIOCSMIIREG, phy_id,
MII_BMCR, BMCR_RESET, NULL);
}