This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
/
rtkswitch.c
468 lines (385 loc) · 9.3 KB
/
rtkswitch.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* 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; either version 2 of
* the License, or (at your option) any later version.
*
* 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., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <bcmnvram.h>
#include <utils.h>
#include <shutils.h>
#include <shared.h>
#include <linux/config.h>
#include <ralink_gpio.h>
#include <ralink.h>
#define RTKSWITCH_DEV "/dev/rtkswitch"
struct trafficCount_t
{
long long rxByteCount;
long long txByteCount;
};
int rtkswitch_ioctl(int val, int val2)
{
int fd;
int value = 0;
int *p = NULL, invalid_ioctl_cmd = 0;
struct trafficCount_t portTraffic;
fd = open(RTKSWITCH_DEV, O_RDONLY);
if (fd < 0) {
perror(RTKSWITCH_DEV);
return -1;
}
switch (val) {
case 0: /* check WAN port phy status */
case 3: /* check LAN ports phy status */
case 6:
case 8:
case 9:
case 10:
case 19: /* Set WAN port txDelay */
case 20: /* Set WAN port rxDelay */
case 22:
case 23:
case 24:
case 25:
case 36: /* Set Vlan VID. */
case 37: /* Set Vlan PRIO. */
case 38: /* Initialize VLAN. Cherry Cho added in 2011/7/15. */
case 39: /* Create VLAN. Cherry Cho added in 2011/7/15. */
case 40:
case 99:
case 100:
case 109: /* Set specific ext port txDelay */
case 110: /* Set specific ext port rxDelay */
p = &value;
value = (unsigned int)val2;
break;
case 29:/* Set VoIP port. Cherry Cho added in 2011/6/30. */
val = 34; /* call ioctl 34 instead. */
p = &value;
value = (unsigned int)val2;
break;
case 2: /* show state of RTL8367RB GMAC1 */
case 7:
case 11:
case 14: /* power up LAN port(s) */
case 15: /* power down LAN port(s) */
case 16: /* power up all ports */
case 17: /* power down all ports */
case 21:
#ifdef RTN56U
case 26: /* WAN port force 1G mode */
#endif
case 27:
case 41: /* check realtek switch normal */
case 114: /* power up WAN port(s) */
case 115: /* power down WAN port(s) */
p = NULL;
break;
case 42:
case 43:
p = (int*) &portTraffic;
break;
default:
invalid_ioctl_cmd = 1;
break;
}
if (invalid_ioctl_cmd) {
printf("wrong ioctl cmd: %d\n", val);
close(fd);
return 0;
}
if (ioctl(fd, val, p) < 0) {
perror("rtkswitch ioctl");
close(fd);
return -1;
}
if (val == 0 || val == 3)
printf("return: %x\n", value);
else if(val == 42 || val == 43)
printf("rx/tx: %lld/%lld\n", portTraffic.rxByteCount, portTraffic.txByteCount);
close(fd);
return 0;
}
int config_rtkswitch(int argc, char *argv[])
{
int val;
int val2 = 0;
char *cmd = NULL;
char *cmd2 = NULL;
if (argc >= 2)
cmd = argv[1];
else
return -1;
if (argc >= 3)
cmd2 = argv[2];
val = (int) strtol(cmd, NULL, 0);
if (cmd2)
val2 = (int) strtol(cmd2, NULL, 0);
return rtkswitch_ioctl(val, val2);
}
typedef struct {
unsigned int idx;
unsigned int value;
} asus_gpio_info;
int ralink_gpio_write_bit(int idx, int value)
{
int fd;
asus_gpio_info info;
fd = open(RTKSWITCH_DEV, O_RDONLY);
if (fd < 0) {
perror(RTKSWITCH_DEV);
return -1;
}
info.idx = idx;
info.value = value;
if (ioctl(fd, RALINK_GPIO_WRITE_BIT, &info) < 0) {
perror("ioctl");
close(fd);
return -1;
}
close(fd);
return 0;
}
int ralink_gpio_read_bit(int idx)
{
int fd;
int value;
fd = open(RTKSWITCH_DEV, O_RDONLY);
if (fd < 0) {
perror(RTKSWITCH_DEV);
return -1;
}
value = idx;
if (ioctl(fd, RALINK_GPIO_READ_BIT, &value) < 0) {
perror("ioctl");
close(fd);
return -1;
}
close(fd);
return value;
}
int ralink_gpio_init(unsigned int idx, int dir)
{
int fd;
asus_gpio_info info;
fd = open(RTKSWITCH_DEV, O_RDONLY);
if (fd < 0) {
perror(RTKSWITCH_DEV);
return -1;
}
info.idx = idx;
info.value = dir;
if (ioctl(fd, RALINK_GPIO_SET_DIR, &info) < 0) {
perror("ioctl");
close(fd);
return -1;
}
close(fd);
return 0;
}
// a wrapper to broadcom world
// bit 1: LED_POWER
// bit 6: BTN_RESET
// bit 8: BTN_WPS
uint32_t ralink_gpio_read(void)
{
uint32_t r = 0;
if (ralink_gpio_read_bit(RA_BTN_RESET))
r |= (1 << 6);
if (ralink_gpio_read_bit(RA_BTN_WPS))
r |= (1 << 8);
return r;
}
int ralink_gpio_write(uint32_t bit, int en)
{
if (bit & (1 << 1)) {
if (!en)
ralink_gpio_write_bit(RA_LED_POWER, RA_LED_OFF);
else
ralink_gpio_write_bit(RA_LED_POWER, RA_LED_ON);
}
return 0;
}
unsigned int rtkswitch_wanPort_phyStatus(int wan_unit)
{
int fd;
unsigned int value;
fd = open(RTKSWITCH_DEV, O_RDONLY);
if (fd < 0) {
perror(RTKSWITCH_DEV);
return -1;
}
if (ioctl(fd, 0, &value) < 0) {
perror("ioctl");
close(fd);
return -1;
}
close(fd);
return value;
}
unsigned int rtkswitch_lanPorts_phyStatus(void)
{
int fd;
unsigned int value;
fd = open(RTKSWITCH_DEV, O_RDONLY);
if (fd < 0) {
perror(RTKSWITCH_DEV);
return -1;
}
if (ioctl(fd, 3, &value) < 0) {
perror("ioctl");
close(fd);
return -1;
}
close(fd);
return value;
}
unsigned int rtkswitch_WanPort_phySpeed(void)
{
int fd;
unsigned int value;
fd = open(RTKSWITCH_DEV, O_RDONLY);
if (fd < 0) {
perror(RTKSWITCH_DEV);
return -1;
}
if (ioctl(fd, 13, &value) < 0) {
perror("ioctl");
close(fd);
return -1;
}
close(fd);
return value;
}
int rtkswitch_WanPort_linkUp(void)
{
eval("rtkswitch", "114");
return 0;
}
int rtkswitch_WanPort_linkDown(void)
{
eval("rtkswitch", "115");
return 0;
}
int rtkswitch_LanPort_linkUp(void)
{
eval("rtkswitch", "14");
return 0;
}
int rtkswitch_LanPort_linkDown(void)
{
eval("rtkswitch", "15");
return 0;
}
int rtkswitch_AllPort_linkUp(void)
{
eval("rtkswitch", "16");
return 0;
}
int rtkswitch_AllPort_linkDown(void)
{
eval("rtkswitch", "17");
return 0;
}
int rtkswitch_Reset_Storm_Control(void)
{
eval("rtkswitch", "21");
return 0;
}
typedef struct {
unsigned int link[5];
unsigned int speed[5];
} phyState;
int rtkswitch_AllPort_phyState(void)
{
int fd;
char buf[32];
int porder_56u[5] = {4,3,2,1,0};
int *o = porder_56u;
const char *portMark = "W0=%C;L1=%C;L2=%C;L3=%C;L4=%C;";
#ifdef RTCONFIG_DSL
int porder_dsl[5] = {0,1,2,3,4};
o = porder_dsl;
#endif
if (get_model() == MODEL_RTN65U)
portMark = "W0=%C;L4=%C;L3=%C;L2=%C;L1=%C;";
fd = open(RTKSWITCH_DEV, O_RDONLY);
if (fd < 0) {
perror(RTKSWITCH_DEV);
return -1;
}
phyState pS;
pS.link[0] = pS.link[1] = pS.link[2] = pS.link[3] = pS.link[4] = 0;
pS.speed[0] = pS.speed[1] = pS.speed[2] = pS.speed[3] = pS.speed[4] = 0;
if (ioctl(fd, 18, &pS) < 0) {
perror("rtkswitch ioctl");
close(fd);
return -1;
}
close(fd);
sprintf(buf, portMark,
(pS.link[o[0]] == 1) ? (pS.speed[o[0]] == 2) ? 'G' : 'M': 'X',
(pS.link[o[1]] == 1) ? (pS.speed[o[1]] == 2) ? 'G' : 'M': 'X',
(pS.link[o[2]] == 1) ? (pS.speed[o[2]] == 2) ? 'G' : 'M': 'X',
(pS.link[o[3]] == 1) ? (pS.speed[o[3]] == 2) ? 'G' : 'M': 'X',
(pS.link[o[4]] == 1) ? (pS.speed[o[4]] == 2) ? 'G' : 'M': 'X');
puts(buf);
return 0;
}
#if 0
void usage(char *cmd)
{
printf("Usage: %s 1 - set (SCK, SD) as (0, 1)\n", cmd);
printf(" %s 2 - set (SCK, SD) as (1, 0)\n", cmd);
printf(" %s 3 - set (SCK, SD) as (1, 1)\n", cmd);
printf(" %s 0 - set (SCK, SD) as (0, 0)\n", cmd);
printf(" %s 4 - init vlan\n", cmd);
printf(" %s 5 - set cpu port 0 0\n", cmd);
printf(" %s 6 - set cpu port 0 1\n", cmd);
printf(" %s 7 - set cpu port 1 0\n", cmd);
printf(" %s 8 - set cpu port 1 1\n", cmd);
printf(" %s 10 - set vlan entry, no cpu port, but mbr\n", cmd);
printf(" %s 11 - set vlan entry, no cpu port, no mbr\n", cmd);
printf(" %s 15 - set vlan PVID, no cpu port\n", cmd);
printf(" %s 20 - set vlan entry, with cpu port\n", cmd);
printf(" %s 21 - set vlan entry, with cpu port and no cpu port in untag sets\n", cmd);
printf(" %s 25 - set vlan PVID, with cpu port\n", cmd);
printf(" %s 26 - set vlan PVID, not set cpu port\n", cmd);
printf(" %s 90 - accept all frmaes\n", cmd);
printf(" %s 66 - setup default vlan\n", cmd);
printf(" %s 61 - setup vlan type1\n", cmd);
printf(" %s 62 - setup vlan type2\n", cmd);
printf(" %s 63 - setup vlan type3\n", cmd);
printf(" %s 64 - setup vlan type4\n", cmd);
printf(" %s 65 - setup vlan type34\n", cmd);
printf(" %s 70 - disable multicast snooping\n", cmd);
printf(" %s 81 - setRtctTesting on port x\n", cmd);
printf(" %s 82 - getRtctResult on port x\n", cmd);
printf(" %s 83 - setGreenEthernet x(green, powsav)\n", cmd);
printf(" %s 84 - setAsicGreenFeature x(txGreen, rxGreen)\n", cmd);
printf(" %s 85 - getAsicGreenFeature\n", cmd);
printf(" %s 86 - enable GreenEthernet on port x\n", cmd);
printf(" %s 87 - disable GreenEthernet on port x\n", cmd);
printf(" %s 88 - getAsicPowerSaving x\n", cmd);
printf(" %s 50 - getPortLinkStatus x\n", cmd);
exit(0);
}
#endif