4 Copyright (C) 2006-2009 Jonathan Zarate
11 #include <sys/types.h>
21 const char *led_names
[] = { "wlan", "diag", "white", "amber", "dmz", "aoss", "bridge", "usb" };
30 #include <sys/ioctl.h>
31 #include <linux_gpio.h>
33 static int _gpio_ioctl(int f
, int gpioreg
, unsigned int mask
, unsigned int val
)
35 struct gpio_ioctl gpio
;
40 if (ioctl(f
, gpioreg
, &gpio
) < 0) {
41 _dprintf("Invalid gpioreg %d\n", gpioreg
);
47 static int _gpio_open()
49 int f
= open("/dev/gpio", O_RDWR
);
51 _dprintf ("Failed to open /dev/gpio\n");
55 int gpio_open(uint32_t mask
)
61 if ((f
>= 0) && mask
) {
62 for (i
= 0; i
<= 15; i
++) {
64 if ((mask
& bit
) == bit
) {
65 _gpio_ioctl(f
, GPIO_IOC_RESERVE
, bit
, bit
);
66 _gpio_ioctl(f
, GPIO_IOC_OUTEN
, bit
, 0);
76 void gpio_write(uint32_t bit
, int en
)
80 if ((f
= gpio_open(0)) < 0) return;
82 _gpio_ioctl(f
, GPIO_IOC_RESERVE
, bit
, bit
);
83 _gpio_ioctl(f
, GPIO_IOC_OUTEN
, bit
, bit
);
84 _gpio_ioctl(f
, GPIO_IOC_OUT
, bit
, en
? bit
: 0);
88 uint32_t _gpio_read(int f
)
91 // r = _gpio_ioctl(f, GPIO_IOC_IN, 0xFFFF, 0);
92 r
= _gpio_ioctl(f
, GPIO_IOC_IN
, 0x07FF, 0);
97 uint32_t gpio_read(void)
102 if ((f
= gpio_open(0)) < 0) return ~0;
110 int gpio_open(uint32_t mask
)
112 int f
= open(DEV_GPIO(in
), O_RDONLY
|O_SYNC
);
114 _dprintf ("Failed to open %s\n", DEV_GPIO(in
));
118 void gpio_write(uint32_t bit
, int en
)
123 if ((f
= open(DEV_GPIO(control
), O_RDWR
)) < 0) return;
124 read(f
, &r
, sizeof(r
));
126 write(f
, &r
, sizeof(r
));
129 if ((f
= open(DEV_GPIO(outen
), O_RDWR
)) < 0) return;
130 read(f
, &r
, sizeof(r
));
132 write(f
, &r
, sizeof(r
));
135 if ((f
= open(DEV_GPIO(out
), O_RDWR
)) < 0) return;
136 read(f
, &r
, sizeof(r
));
139 write(f
, &r
, sizeof(r
));
143 uint32_t _gpio_read(int f
)
146 return (read(f
, &v
, sizeof(v
)) == sizeof(v
)) ? v
: ~0;
149 uint32_t gpio_read(void)
154 if ((f
= open(DEV_GPIO(in
), O_RDONLY
)) < 0) return ~0;
162 int nvget_gpio(const char *name
, int *gpio
, int *inv
)
167 if (((p
= nvram_get(name
)) != NULL
) && (*p
)) {
168 n
= strtoul(p
, NULL
, 0);
169 if ((n
& 0xFFFFFF70) == 0) {
171 *inv
= ((n
& 0x80) != 0);
180 int do_led(int which
, int mode
)
182 // WLAN DIAG WHITE AMBER DMZ AOSS BRIDG MYST/USB
183 // ----- ----- ----- ----- ----- ----- ----- -----
184 static int wrt54g
[] = { 255, 1, 2, 3, 7, 255, 255, 255 };
185 static int wrtsl
[] = { 255, 1, 5, 7, 0, 255, 255, 255 };
186 static int whrg54
[] = { 2, 7, 255, 255, 255, 6, 1, 3 };
187 static int wbr2g54
[] = { 255, -1, 255, 255, 255, -6, 255, 255 };
188 static int wzrg54
[] = { 2, 7, 255, 255, 255, 6, 255, 255 };
189 static int wr850g1
[] = { 7, 3, 255, 255, 255, 255, 255, 255 };
190 static int wr850g2
[] = { 0, 1, 255, 255, 255, 255, 255, 255 };
191 static int wtr54gs
[] = { 1, -1, 255, 255, 255, 255, 255, 255 };
192 static int dir320
[] = { -99, 1, 4, 3, 255, 255, 255, -5 };
193 static int h618b
[] = { 255, -1, 255, 255, 255, -5, -3, -4 };
194 static int wl1600gl
[] = { 1, -5, 0, 255, 255, 2, 255, 255 };
195 static int wrt310nv1
[] = { 255, 1, 9, 3, 255, 255, 255, 255 };
196 static int wrt160nv1
[] = { 255, 1, 5, 3, 255, 255, 255, 255 };
198 static int wnr3500
[] = { 255, 255, 2, 255, 255, -1, 255, 255 };
199 static int wnr2000v2
[] = { 255, 255, 255, 255, 255, -7, 255, 255 };
200 static int f7d
[] = { 255, 255, 255, 255, 12, 13, 255, 14 };
201 static int wrt160nv3
[] = { 255, 1, 4, 2, 255, 255, 255, 255 };
202 static int wrt320n
[] = { 255, 2, 3, 4, 255, 255, 255, 255 };
203 static int wrt610nv2
[] = { 255, 5, 3, 0, 255, 255, 255, -7 };
204 static int e4200
[] = { 255, 5, -3, 255, 255, 255, 255, 255 };
205 static int rtn10u
[] = { 255, 255, 255, 255, 255, -7, 255, 8 };
206 static int rtn66u
[] = { 255, -12, 255, 255, 255, 255, 255, 15 };
211 int b
= 255, c
= 255;
214 if ((which
< 0) || (which
>= LED_COUNT
)) return ret
;
216 switch (nvram_match("led_override", "1") ? MODEL_UNKNOWN
: get_model()) {
218 if (check_hw_type() == HW_BCM4702
) {
220 if ((which
!= LED_DIAG
) && (which
!= LED_DMZ
)) return ret
;
221 b
= (which
== LED_DMZ
) ? 1 : 4;
222 if (mode
!= LED_PROBE
) {
223 if (f_read_string("/proc/sys/diag", s
, sizeof(s
)) > 0) {
225 sprintf(s
, "%u", mode
? (n
| b
) : (n
& ~b
));
226 f_write_string("/proc/sys/diag", s
, 0, 0);
234 if (!supports(SUP_WHAM_LED
)) return ret
;
242 case MODEL_WRTSL54GS
:
253 case MODEL_WZRRSG54HP
:
255 case MODEL_WHR2A54G54
:
261 case MODEL_WHR2A54G54:
262 if (which != LED_DIAG) return ret;
267 if (which
!= LED_DIAG
) return ret
;
281 if (which
!= LED_DIAG
) return ret
;
282 b
= -1; // power light
285 if (which
!= LED_DIAG
) return ret
;
286 b
= -5; // power light
297 case MODEL_WL500GPv2
:
300 if (which
!= LED_DIAG
) return ret
;
301 b
= -99; // Invert power light as diag indicator
305 if (which
!= LED_DIAG
) return ret
;
306 b
= -2; // power light
310 if (which
!= LED_DIAG
) return ret
;
311 b
= -1; // power light
317 if (which
== LED_DIAG
) {
318 // power led gpio: 0x03 - green, 0x07 - amber
324 case MODEL_WNR2000v2
:
325 if (which
== LED_DIAG
) {
326 // power led gpio: 0x01 - green, 0x02 - amber
330 b
= wnr2000v2
[which
];
336 case MODEL_F5D8235v3
:
337 if (which
== LED_DIAG
) {
338 // power led gpio: 10 - green, 11 - red
339 b
= (mode
) ? 11 : -10;
340 c
= (mode
) ? -10 : 11;
344 case MODEL_WRT160Nv3
:
345 b
= wrt160nv3
[which
];
350 case MODEL_WRT610Nv2
:
351 b
= wrt610nv2
[which
];
365 if (which
!= LED_DIAG
) return ret
;
369 if (which
!= LED_DIAG
) return ret
;
373 if (which
!= LED_DIAG
) return ret
;
376 case MODEL_WRT310Nv1
:
377 b
= wrt310nv1
[which
];
379 case MODEL_WRT160Nv1
:
380 b
= wrt160nv1
[which
];
383 sprintf(s
, "led_%s", led_names
[which
]);
384 if (nvget_gpio(s
, &b
, &n
)) {
385 if ((mode
!= LED_PROBE
) && (n
)) mode
= !mode
;
386 ret
= (n
) ? b
: ((b
) ? -b
: -99);
394 if (b
== -99) b
= 0; // -0 substitute
397 else if (mode
!= LED_PROBE
) {
403 if (mode
!= LED_PROBE
) {
404 gpio_write(1 << b
, mode
);
411 if (c
< 16) gpio_write(1 << c
, mode
);