forked from rorei/bpi_ledset
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bpi_ledset.c
361 lines (314 loc) · 9.74 KB
/
bpi_ledset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Configure the blue led on BananaPi
*
* based on Roman Reichel's code, which itself is
*
* based on mii-tool from David A. Hinds <dhinds@pcmcia.sourceforge.org>
*
* http://sourceforge.net/projects/net-tools
*
* which itself is based on mii-diag by Donald Becker <becker@scyld.com>
*
* Copyright (C) 2014 Roman Reichel <roman.reichel@posteo.de>
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation.
*
* */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <time.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <linux/mii.h>
static int verbose = 1;
static int skfd = -1;
static struct ifreq ifr;
static int mdio_read(int skfd, int location) {
struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
mii->reg_num = location;
if (ioctl(skfd, SIOCGMIIREG, &ifr) < 0) {
fprintf(stderr, "SIOCGMIIREG on %s failed: %s\n", ifr.ifr_name, strerror(errno));
return -1;
}
return mii->val_out;
}
static void mdio_write(int skfd, int location, int value) {
struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
mii->reg_num = location;
mii->val_in = value;
if (ioctl(skfd, SIOCSMIIREG, &ifr) < 0) {
fprintf(stderr, "SIOCGMIIREG on %s failed: %s\n", ifr.ifr_name, strerror(errno));
}
}
enum {
FUNC_10MBPS,
FUNC_100MBPS,
FUNC_1000MBPS,
FUNC_ACTIVITY,
FUNC_COUNT
};
#define CFG_10MBPS (1 << FUNC_10MBPS)
#define CFG_100MBPS (1 << FUNC_100MBPS)
#define CFG_1000MBPS (1 << FUNC_1000MBPS)
#define CFG_ACTIVITY (1 << FUNC_ACTIVITY)
enum {
LED_BLUE,
LED_YELLOW,
LED_GREEN,
LED_COUNT
};
typedef struct {
int reg26_clear;
int reg26_set;
int reg28_clear;
int reg28_set;
} led_config_t;
typedef struct {
int func_mask;
int func_value;
} func_config_t;
static const char *led_name[] = {
[LED_BLUE] = "blue",
[LED_YELLOW] = "yellow",
[LED_GREEN] = "green",
};
static const char *func_name[] = {
[FUNC_10MBPS] = "10Mbps",
[FUNC_100MBPS] = "100Mbps",
[FUNC_1000MBPS] = "1000Mbps",
[FUNC_ACTIVITY] = "Active (Tx/Rx)",
};
static const led_config_t led_config[LED_COUNT][FUNC_COUNT] = {
[LED_BLUE] = {
[FUNC_10MBPS] = {.reg26_set = 0 , .reg28_set = 1 << 8},
[FUNC_100MBPS] = {.reg26_set = 0 , .reg28_set = 1 << 9},
[FUNC_1000MBPS] = {.reg26_set = 0 , .reg28_set = 1 << 10},
[FUNC_ACTIVITY] = {.reg26_set = 1 << 6, .reg28_set = 0},
},
[LED_YELLOW] = {
[FUNC_10MBPS] = {.reg26_set = 0 , .reg28_set = 1 << 4},
[FUNC_100MBPS] = {.reg26_set = 0 , .reg28_set = 1 << 5},
[FUNC_1000MBPS] = {.reg26_set = 0 , .reg28_set = 1 << 6},
[FUNC_ACTIVITY] = {.reg26_set = 1 << 5, .reg28_set = 0},
},
[LED_GREEN] = {
[FUNC_10MBPS] = {.reg26_set = 0 , .reg28_set = 1 << 0},
[FUNC_100MBPS] = {.reg26_set = 0 , .reg28_set = 1 << 1},
[FUNC_1000MBPS] = {.reg26_set = 0 , .reg28_set = 1 << 2},
[FUNC_ACTIVITY] = {.reg26_set = 1 << 4, .reg28_set = 0},
},
};
static void led_config_apply(led_config_t *lcp, int set, const led_config_t *lf) {
if (set) {
lcp->reg26_clear |= lf->reg26_clear;
lcp->reg26_set |= lf->reg26_set;
lcp->reg28_clear |= lf->reg28_clear;
lcp->reg28_set |= lf->reg28_set;
} else {
lcp->reg26_clear |= lf->reg26_set;
lcp->reg26_set |= lf->reg26_clear;
lcp->reg28_clear |= lf->reg28_set;
lcp->reg28_set |= lf->reg28_clear;
}
}
static void led_mask_val(led_config_t *lcp, int led, const func_config_t *cfg) {
if (cfg->func_mask & CFG_10MBPS) led_config_apply(lcp, !!(cfg->func_value & CFG_10MBPS), &led_config[led][FUNC_10MBPS]);
if (cfg->func_mask & CFG_100MBPS) led_config_apply(lcp, !!(cfg->func_value & CFG_100MBPS), &led_config[led][FUNC_100MBPS]);
if (cfg->func_mask & CFG_1000MBPS) led_config_apply(lcp, !!(cfg->func_value & CFG_1000MBPS), &led_config[led][FUNC_1000MBPS]);
if (cfg->func_mask & CFG_ACTIVITY) led_config_apply(lcp, !!(cfg->func_value & CFG_ACTIVITY), &led_config[led][FUNC_ACTIVITY]);
}
static void led_show(int reg26, int reg28) {
int led, func;
const led_config_t *lf;
int f;
const char *name;
if (verbose >= 3)
printf("[reg26 = 0x%04x, reg28 = 0x%04x]\n", reg26, reg28);
for (led = 0; led < LED_COUNT; led++) {
name = led_name[led];
printf("%s:%*c", name, 8 - strlen(name), ' ');
f = 0;
for (func = 0; func < FUNC_COUNT; func++) {
lf = &led_config[led][func];
if ((reg26 & (lf->reg26_clear | lf->reg26_set)) == lf->reg26_set &&
(reg28 & (lf->reg28_clear | lf->reg28_set)) == lf->reg28_set) {
printf("%s%s", f ? " | " : "", func_name[func]);
f = 1;
}
}
printf(f ? "\n" : "disabled\n");
}
}
static void led_set(const func_config_t *blue, const func_config_t *yellow, const func_config_t *green) {
led_config_t lc;
int reg26_old, reg28_old;
int reg26_new, reg28_new;
/* Parse configuration request */
lc.reg26_clear = 0;
lc.reg26_set = 0;
lc.reg28_clear = 0;
lc.reg28_set = 0;
led_mask_val(&lc, LED_BLUE, blue);
led_mask_val(&lc, LED_YELLOW, yellow);
led_mask_val(&lc, LED_GREEN, green);
/* Configure the phy */
mdio_write(skfd, 0x1f, 0x0007);
mdio_write(skfd, 0x1e, 0x002c);
reg26_old = mdio_read(skfd, 26);
reg26_new = (reg26_old & ~lc.reg26_clear) | lc.reg26_set;
if (reg26_new != reg26_old)
mdio_write(skfd, 26, reg26_new);
reg28_old = mdio_read(skfd, 28);
reg28_new = (reg28_old & ~lc.reg28_clear) | lc.reg28_set;
if (reg28_new != reg28_old)
mdio_write(skfd, 28, reg28_new);
mdio_write(skfd, 0x1f, 0x0000);
/* Show results */
if (reg26_new != reg26_old || reg28_new != reg28_old) {
/* Configuration has been changed */
if (verbose >= 2) {
printf("Old phy led configuration:\n");
led_show(reg26_old, reg28_old);
printf("\n");
}
if (verbose >= 1)
printf("Phy led configuration changed\n");
if (verbose >= 2) {
printf("\nNew phy led configuration:\n");
led_show(reg26_new, reg28_new);
}
} else {
/* No configuration change */
if (lc.reg26_clear || lc.reg26_set || lc.reg28_clear || lc.reg28_set) {
/* Request matches current config */
if (verbose >= 1)
printf("Phy led configuration already set\n");
if (verbose >= 2) {
printf("\nPhy led configuration:\n");
led_show(reg26_new, reg28_new);
}
} else {
/* No request: show current config */
if (verbose >= 1) {
printf("Phy led configuration:\n");
led_show(reg26_new, reg28_new);
}
}
}
}
#if 0
static void led_on() {
int reg, val;
reg = 26;
val = mdio_read(skfd, reg);
//printf("reg %d is: 0x%x\n", reg, val);
mdio_write(skfd, reg, val | (0x0040));
reg = 28;
val = mdio_read(skfd, reg);
//printf("reg %d is: 0x%x\n", reg, val);
mdio_write(skfd, reg, val | (0x0700));
printf("phy led enabled\n");
}
static void led_off() {
int reg, val;
reg = 26;
val = mdio_read(skfd, reg);
//printf("reg %d is: 0x%x\n", reg, val);
mdio_write(skfd, reg, val & ~(0x0040));
reg = 28;
val = mdio_read(skfd, reg);
//printf("reg %d is: 0x%x\n", reg, val);
mdio_write(skfd, reg, val & ~(0x0700));
printf("phy led disabled\n");
}
#endif
int main(int argc, char** argv) {
func_config_t blue, yellow, green, *ledp;
int val;
int argn;
const char *p;
if (argc < 2) {
fprintf(stderr, "Usage: bpi_ledset <if> [qv] [blmha] [ylmha] [glmha]\n"
" q Quiet (no informational messages)\n"
" v Verbose (extra informational messages)\n"
"\n"
" b Blue led configuration\n"
" y Yellow led configuration\n"
" g Green led configuration\n"
"\n"
" l Switch on when linked at 10Mbps\n"
" m Switch on when linked at 100Mbps\n"
" h Switch on when linked at 1000Mbps\n"
" a Blink with activity (Tx/Rx)\n"
"\n"
"\n"
"Example1: show phy leds status\n"
" bpi_ledset eth0\n"
"\n"
"Example2: disable all phy leds\n"
" bpi_ledset eth0 b y g\n"
"\n"
"Example3: disable blue led, yellow led for 1000Mbps, green led for link and activity\n"
" bpi_ledset eth0 b yh glmha\n"
"\n"
"Example4: disable blue led\n"
" bpi_ledset eth0 b\n"
);
return 1;
}
blue.func_mask = blue.func_value = 0;
yellow.func_mask = yellow.func_value = 0;
green.func_mask = green.func_value = 0;
for (argn =2; argn < argc; argn++) {
ledp = NULL;
for (p = argv[argn]; *p; p++)
switch (*p) {
case 'q': verbose = 0; break;
case 'v': verbose = verbose < 3 ? verbose + 1 : 3; break;
case 'b': ledp = &blue; ledp->func_mask = ~0; ledp->func_value = 0; break;
case 'y': ledp = &yellow; ledp->func_mask = ~0; ledp->func_value = 0; break;
case 'g': ledp = &green; ledp->func_mask = ~0; ledp->func_value = 0; break;
case 'l': if (ledp) {ledp->func_mask |= CFG_10MBPS; ledp->func_value |= CFG_10MBPS;} break;
case 'm': if (ledp) {ledp->func_mask |= CFG_100MBPS; ledp->func_value |= CFG_100MBPS;} break;
case 'h': if (ledp) {ledp->func_mask |= CFG_1000MBPS; ledp->func_value |= CFG_1000MBPS;} break;
case 'a': if (ledp) {ledp->func_mask |= CFG_ACTIVITY; ledp->func_value |= CFG_ACTIVITY;} break;
}
}
if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(1);
}
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) {
fprintf(stderr, "SIOCGMIIPHY on %s failed: %s\n", argv[1], strerror(errno));
close(skfd);
return 1;
}
mdio_write(skfd, 0x1f, 0x0000);
val = mdio_read(skfd, 2);
if ((val & 0xFFFF) != 0x001c) {
close(skfd);
fprintf(stderr, "unexpected PHYID1: 0x%x\n", val);
return 1;
}
val = mdio_read(skfd, 3);
if ((val & 0xFC00) != 0xc800) {
close(skfd);
fprintf(stderr, "unexpected PHYID2: 0x%x\n", val);
return 1;
}
led_set(&blue, &yellow, &green);
close(skfd);
return 0;
}