4 Copyright (C) 2006-2009 Jonathan Zarate
11 #include <sys/types.h>
20 const char *led_names
[] = { "wlan", "diag", "white", "amber", "dmz", "aoss", "bridge", "mystery" };
28 void gpio_write(uint32_t bit
, int en
)
33 if ((f
= open("/dev/gpio/control", O_RDWR
)) < 0) return;
34 read(f
, &r
, sizeof(r
));
36 write(f
, &r
, sizeof(r
));
39 if ((f
= open("/dev/gpio/outen", O_RDWR
)) < 0) return;
40 read(f
, &r
, sizeof(r
));
42 write(f
, &r
, sizeof(r
));
45 if ((f
= open("/dev/gpio/out", O_RDWR
)) < 0) return;
46 read(f
, &r
, sizeof(r
));
49 write(f
, &r
, sizeof(r
));
53 uint32_t gpio_read(void)
58 if ((f
= open("/dev/gpio/in", O_RDONLY
)) < 0) return ~0;
59 if (read(f
, &r
, sizeof(r
)) != sizeof(r
)) r
= ~0;
66 int nvget_gpio(const char *name
, int *gpio
, int *inv
)
71 if (((p
= nvram_get(name
)) != NULL
) && (*p
)) {
72 n
= strtoul(p
, NULL
, 0);
73 if ((n
& 0xFFFFFF70) == 0) {
75 *inv
= ((n
& 0x80) != 0);
84 int led(int which
, int mode
)
86 // WLAN DIAG WHITE AMBER DMZ AOSS BRIDG MYST
87 // ----- ----- ----- ----- ----- ----- ----- -----
88 static int wrt54g
[] = { 0, 1, 2, 3, 7, 255, 255, 255 };
89 static int wrtsl
[] = { 255, 1, 5, 7, 0, 255, 255, 255 };
90 static int whrg54
[] = { 2, 7, 255, 255, 255, 6, 1, 3 };
91 static int wbr2g54
[] = { 255, -1, 255, 255, 255, -6, 255, 255 };
92 static int wzrg54
[] = { 2, 7, 255, 255, 255, 6, 255, 255 };
93 static int wr850g1
[] = { 7, 3, 255, 255, 255, 255, 255, 255 };
94 static int wr850g2
[] = { 0, 1, 255, 255, 255, 255, 255, 255 };
95 static int wtr54gs
[] = { 1, -1, 255, 255, 255, 255, 255, 255 };
100 if ((which
< 0) || (which
>= LED_COUNT
)) return 0;
102 switch (nvram_match("led_override", "1") ? MODEL_UNKNOWN
: get_model()) {
104 if (check_hw_type() == HW_BCM4702
) {
106 if ((which
!= LED_DIAG
) && (which
!= LED_DMZ
)) return 0;
107 if (mode
!= LED_PROBE
) {
108 if (f_read_string("/proc/sys/diag", s
, sizeof(s
)) > 0) {
109 b
= (which
== LED_DMZ
) ? 1 : 4;
111 sprintf(s
, "%u", mode
? (n
| b
) : (n
& ~b
));
112 f_write_string("/proc/sys/diag", s
, 0, 0);
120 if (!supports(SUP_WHAM_LED
)) return 0;
128 case MODEL_WRTSL54GS
:
139 case MODEL_WZRRSG54HP
:
141 case MODEL_WHR2A54G54
:
147 case MODEL_WHR2A54G54:
148 if (which != LED_DIAG) return 0;
153 if (which
!= LED_DIAG
) return 0;
167 if (which
!= LED_DIAG
) return 0;
168 b
= -1; // power light
171 if (which
!= LED_DIAG
) return 0;
172 b
= -5; // power light
175 if (which
!= LED_DIAG
) return 0;
176 b
= 0; // Invert power light as diag indicator
177 if (mode
!= LED_PROBE
) mode
= !mode
;
184 if (which
!= LED_DIAG
) return 0;
188 if (which
!= LED_DIAG
) return 0;
192 sprintf(s
, "led_%s", led_names
[which
]);
193 if (nvget_gpio(s
, &b
, &n
)) {
194 if ((mode
!= LED_PROBE
) && (n
)) mode
= !mode
;
201 if (b
== -99) b
= 1; // -0 substitute
204 else if (mode
!= LED_PROBE
) {
210 if (mode
!= LED_PROBE
) {
211 gpio_write(1 << b
, mode
);