4 Copyright (C) 2006-2009 Jonathan Zarate
12 #include <sys/types.h>
22 const char *led_names
[] = { "wlan", "diag", "white", "amber", "dmz", "aoss", "bridge", "usb", "5g"};
23 const char *led_modes
[] = { "Off", "On", "Blink", "Probe"};
32 #include <sys/ioctl.h>
33 #include <linux_gpio.h>
35 static int _gpio_ioctl(int f
, int gpioreg
, unsigned int mask
, unsigned int val
)
37 struct gpio_ioctl gpio
;
42 if (ioctl(f
, gpioreg
, &gpio
) < 0) {
43 _dprintf("Invalid gpioreg %d\n", gpioreg
);
49 static int _gpio_open()
51 int f
= open("/dev/gpio", O_RDWR
);
53 _dprintf ("Failed to open /dev/gpio\n");
57 int gpio_open(uint32_t mask
)
63 if ((f
>= 0) && mask
) {
64 for (i
= 0; i
<= 15; i
++) {
66 if ((mask
& bit
) == bit
) {
67 _gpio_ioctl(f
, GPIO_IOC_RESERVE
, bit
, bit
);
68 _gpio_ioctl(f
, GPIO_IOC_OUTEN
, bit
, 0);
78 void gpio_write(uint32_t bit
, int en
)
82 if ((f
= gpio_open(0)) < 0) return;
84 _gpio_ioctl(f
, GPIO_IOC_RESERVE
, bit
, bit
);
85 _gpio_ioctl(f
, GPIO_IOC_OUTEN
, bit
, bit
);
86 _gpio_ioctl(f
, GPIO_IOC_OUT
, bit
, en
? bit
: 0);
90 uint32_t _gpio_read(int f
)
93 // r = _gpio_ioctl(f, GPIO_IOC_IN, 0xFFFF, 0);
94 r
= _gpio_ioctl(f
, GPIO_IOC_IN
, 0x07FF, 0);
99 uint32_t gpio_read(void)
104 if ((f
= gpio_open(0)) < 0) return ~0;
112 int gpio_open(uint32_t mask
)
114 int f
= open(DEV_GPIO(in
), O_RDONLY
|O_SYNC
);
116 _dprintf ("Failed to open %s\n", DEV_GPIO(in
));
120 void gpio_write(uint32_t bit
, int en
)
125 if ((f
= open(DEV_GPIO(control
), O_RDWR
)) < 0) return;
126 read(f
, &r
, sizeof(r
));
128 write(f
, &r
, sizeof(r
));
131 if ((f
= open(DEV_GPIO(outen
), O_RDWR
)) < 0) return;
132 read(f
, &r
, sizeof(r
));
134 write(f
, &r
, sizeof(r
));
137 if ((f
= open(DEV_GPIO(out
), O_RDWR
)) < 0) return;
138 read(f
, &r
, sizeof(r
));
141 write(f
, &r
, sizeof(r
));
145 uint32_t _gpio_read(int f
)
148 return (read(f
, &v
, sizeof(v
)) == sizeof(v
)) ? v
: ~0;
151 uint32_t gpio_read(void)
156 if ((f
= open(DEV_GPIO(in
), O_RDONLY
)) < 0) return ~0;
164 int nvget_gpio(const char *name
, int *gpio
, int *inv
)
169 if (((p
= nvram_get(name
)) != NULL
) && (*p
)) {
170 n
= strtoul(p
, NULL
, 0);
171 if ((n
& 0xFFFFFF70) == 0) {
173 *inv
= ((n
& 0x80) != 0);
181 // Routine to write to shift register
182 // Note that the controls are active low, but input as high = on
183 void gpio_write_shiftregister(unsigned int led_status
, int clk
, int data
, int max_shifts
)
187 gpio_write(1 << data
, 1); /* set data to 1 to start (disable) */
188 gpio_write(1 << clk
, 0); /* and clear clock ... */
190 for (i
= max_shifts
; i
>= 0; i
--) {
191 if (led_status
& (1 << i
))
192 gpio_write(1 << data
, 0); /* on, pull low (active low) */
194 gpio_write(1 << data
, 1); /* off, pull high (active low) */
196 gpio_write(1 << clk
, 1); /* pull high to trigger */
197 gpio_write(1 << clk
, 0); /* reset to low -> finish clock cycle*/
202 /* strBits: convert binary value to string (binary file representation) */
204 char * strBits(int input
, int binarySize
)
209 if (binarySize
> 0) {
214 for(i
= 0; i
< binarySize
; i
++) {
215 if (input
& (1 << ((binarySize
-1)-i
)))
221 strConvert
[binarySize
] = '\0';
222 return (char *)strConvert
;
228 // All bit positions start at zero, given shifts (and shift logic). 255 means a particular LED is not avaiable / accessible
229 int do_led(int which
, int mode
)
231 // WLAN DIAG WHITE AMBER DMZ AOSS BRIDG MYST/USB 5G
232 // ----- ----- ----- ----- ----- ----- ----- ----- --
233 static int wrt54g
[] = { 255, 1, 2, 3, 7, 255, 255, 255, 255};
234 static int wrtsl
[] = { 255, 1, 5, 7, 0, 255, 255, 255, 255};
235 static int whrg54
[] = { 2, 7, 255, 255, 255, 6, 1, 3 , 255};
236 static int wbr2g54
[] = { 255, -1, 255, 255, 255, -6, 255, 255, 255};
237 static int wzrg54
[] = { 2, 7, 255, 255, 255, 6, 255, 255, 255};
238 static int wr850g1
[] = { 7, 3, 255, 255, 255, 255, 255, 255, 255};
239 static int wr850g2
[] = { 0, 1, 255, 255, 255, 255, 255, 255, 255};
240 static int wtr54gs
[] = { 1, -1, 255, 255, 255, 255, 255, 255, 255};
241 static int dir320
[] = { -99, 1, 4, 3, 255, 255, 255, -5, 255};
242 static int h618b
[] = { 255, -1, 255, 255, 255, -5, -3, -4, 255};
243 static int wl1600gl
[] = { 1, -5, 0, 255, 255, 2, 255, 255, 255};
244 static int wrt310nv1
[] = { 255, 1, 9, 3, 255, 255, 255, 255, 255};
245 static int wrt160nv1
[] = { 255, 1, 5, 3, 255, 255, 255, 255, 255};
247 static int wnr3500
[] = { 255, 255, 2, 255, 255, -1, 255, 255, 255};
248 static int wnr2000v2
[] = { 255, 255, 255, 255, 255, -7, 255, 255, 255};
249 static int wndr4000
[] = { 3, 1, 0, 1, 255, 7, 255, 5, 4};
250 static int wndr3400
[] = { -9, -7, -3, -7, 255, 255, 255, 2, -99}; // Note: 5 = Switch, 4 = Reset button, 8 = SES button
251 static int f7d
[] = { 255, 255, 255, 255, 12, 13, 255, 14, 255};
252 static int wrt160nv3
[] = { 255, 1, 4, 2, 255, 255, 255, 255, 255};
253 static int e900
[] = { 255, -6, 8, 255, 255, 255, 255, 255, 255};
254 static int e1000v2
[] = { 255, -6, 8, 7, 255, 255, 255, 255, 255};
255 static int e3200
[] = { 255, -3, 255, 255, 255, 255, 255, 255, 255};
256 static int wrt320n
[] = { 255, 2, 3, 4, 255, 255, 255, 255, 255};
257 static int wrt610nv2
[] = { 255, 5, 3, 0, 255, 255, 255, -7, 255};
258 static int e4200
[] = { 255, 5, -3, 255, 255, 255, 255, 255, 255};
259 static int rtn10u
[] = { 255, 255, 255, 255, 255, -7, 255, -8, 255};
260 static int rtn10p
[] = { 255, -6, 255, 255, 255, -7, 255, 255, 255};
261 static int rtn12b1
[] = { -5, 255, 255, 255, 255, 255, 255, 225, 255};
262 static int rtn15u
[] = { 1, 255, 3, 255, 255, 255, 255, -9, 255};
263 static int rtn53
[] = { 0, -17, 255, 255, 255, 255, 255, 255, 255};
264 static int l600n
[] = { 255, 255, 255, 255, 255, -7, 255, -8, 255};
265 static int dir620c1
[] = { -6, -8, 255, 255, 255, -7, 255, 255, 255};
266 static int rtn66u
[] = { 255, -12, 255, 255, 255, 255, 255, 15, 13};
267 static int w1800r
[] = { 255, -13, 255, 255, 255, 255, 255, -12, -5};
268 static int d1800h
[] = { -12, -13, 8, 255, 255, -10, 255, 15, 11};
269 static int tdn6
[] = { 255, -6, 8, 255, 255, 255, 255, 255, 255};
270 // WLAN DIAG WHITE AMBER DMZ AOSS BRIDG MYST/USB 5G
272 FILE *fileExtGPIOstatus
; // For WNDR4000, keep track of extended bit status (shift register), as cannot read from HW!
273 unsigned int intExtendedLEDStatus
; // Status of Extended LED's (shift register on WNDR4000) ... and WNDR3700v3, it's the same!
278 int b
= 255, c
= 255;
281 if ((which
< 0) || (which
>= LED_COUNT
)) return ret
;
283 switch (nvram_match("led_override", "1") ? MODEL_UNKNOWN
: get_model()) {
285 if (check_hw_type() == HW_BCM4702
) {
287 if ((which
!= LED_DIAG
) && (which
!= LED_DMZ
)) return ret
;
288 b
= (which
== LED_DMZ
) ? 1 : 4;
289 if (mode
!= LED_PROBE
) {
290 if (f_read_string("/proc/sys/diag", s
, sizeof(s
)) > 0) {
292 sprintf(s
, "%u", mode
? (n
| b
) : (n
& ~b
));
293 f_write_string("/proc/sys/diag", s
, 0, 0);
301 if (!supports(SUP_WHAM_LED
)) return ret
;
309 case MODEL_WRTSL54GS
:
320 case MODEL_WZRRSG54HP
:
322 case MODEL_WHR2A54G54
:
328 case MODEL_WHR2A54G54:
329 if (which != LED_DIAG) return ret;
334 if (which
!= LED_DIAG
) return ret
;
348 if (which
!= LED_DIAG
) return ret
;
349 b
= -1; // power light
352 if (which
!= LED_DIAG
) return ret
;
353 b
= -5; // power light
364 case MODEL_WL500GPv2
:
368 if (which
!= LED_DIAG
) return ret
;
369 b
= -99; // Invert power light as diag indicator
373 if (which
!= LED_DIAG
) return ret
;
374 b
= -2; // power light
378 if (which
!= LED_DIAG
) return ret
;
379 b
= -1; // power light
395 if (which
== LED_DIAG
) {
396 // power led gpio: 0x02 - white, 0x13 - red
403 case MODEL_WNR3500LV2
:
404 if (which
== LED_DIAG
) {
405 // power led gpio: 0x03 - green, 0x07 - amber
411 case MODEL_WNR2000v2
:
412 if (which
== LED_DIAG
) {
413 // power led gpio: 0x01 - green, 0x02 - amber
417 b
= wnr2000v2
[which
];
420 case MODEL_WNDR3700v3
:
421 // Special Case, shift register control ... so write accordingly. Syslog below for debugging, turn back on if needed.
423 //syslog(LOG_INFO, "WNDR4000 Shift Register (do_led): Bit Name = %s, which = %d, b = %d, Mode = %s\n", led_names[which], which, b, led_modes[mode]);
424 if ((mode
== LED_ON
) || (mode
== LED_OFF
)) {
426 // Read bit-mask from file, for tracking / updates (as this process is called clean each LED update, so cannot use static variable!)
427 if (fileExtGPIOstatus
= fopen("/tmp/.ext_led_value", "rb")) {
428 fscanf(fileExtGPIOstatus
, "Shift Register Status: 0x%x\n", &intExtendedLEDStatus
);
429 fclose(fileExtGPIOstatus
);
430 //syslog(LOG_INFO, "WNDR4000 Shift Register (do_led): Read Shift Register status from file, intExtendedLEDStatus = %s\n", strBits(intExtendedLEDStatus, 8));
432 // Read Error (tracking file) - set all LED's to off
433 syslog(LOG_INFO
, "WNDR4000 Shift Register (do_led): Error Reading /tmp/.ext_led_value, set state to all OFF\n");
434 intExtendedLEDStatus
= 0x00;
437 if (mode
== LED_ON
) {
438 // Bitwise OR, turn corresponding bit on
439 intExtendedLEDStatus
|= (1 << b
);
440 //syslog(LOG_INFO, "WNDR4000 Shift Register (do_led): Mode = LED_ON (%d), Bitwise OR = %s\n", mode, strBits((1 << b), 8));
442 // Bitwise AND, with bitwise inverted shift ... so turn bit off
443 intExtendedLEDStatus
&= (~(1 << b
));
444 //syslog(LOG_INFO, "WNDR4000 Shift Register (do_led): Mode = LED_OFF (%d), Bitwise AND = %s\n", mode, strBits((~(1 << b)), 8));
447 // And write to LEDs (Shift Register)
448 //syslog(LOG_INFO, "WNDR4000 Shift Register (do_led): Writing to Shift Register, intExtendedLEDStatus = %s\n", strBits(intExtendedLEDStatus, 8));
449 gpio_write_shiftregister(intExtendedLEDStatus
, 7, 6, 7);
450 // Write bit-mask to file, for tracking / updates (as this process is called clean each LED update, so cannot use static variable!)
451 if (fileExtGPIOstatus
= fopen("/tmp/.ext_led_value", "wb")) {
452 fprintf(fileExtGPIOstatus
, "Shift Register Status: 0x%x\n", intExtendedLEDStatus
);
453 fprintf(fileExtGPIOstatus
, "Shift Register Status: 0b%s\n", strBits(intExtendedLEDStatus
, 8));
454 fclose(fileExtGPIOstatus
);
458 // Return GPIO "pin" (shift register location)
462 case MODEL_WNDR3400v2
:
469 case MODEL_F5D8235v3
:
470 if (which
== LED_DIAG
) {
471 // power led gpio: 10 - green, 11 - red
472 b
= (mode
) ? 11 : -10;
473 c
= (mode
) ? -10 : 11;
489 case MODEL_WRT160Nv3
:
490 b
= wrt160nv3
[which
];
495 case MODEL_WRT610Nv2
:
496 b
= wrt610nv2
[which
];
515 case MODEL_TDN60
: //bwq518
525 if (which
!= LED_DIAG
) return ret
;
529 if (which
!= LED_DIAG
) return ret
;
533 if (which
!= LED_DIAG
) return ret
;
536 case MODEL_WRT310Nv1
:
537 b
= wrt310nv1
[which
];
539 case MODEL_WRT160Nv1
:
540 b
= wrt160nv1
[which
];
543 sprintf(s
, "led_%s", led_names
[which
]);
544 if (nvget_gpio(s
, &b
, &n
)) {
545 if ((mode
!= LED_PROBE
) && (n
)) mode
= !mode
;
546 ret
= (n
) ? b
: ((b
) ? -b
: -99);
554 if (b
== -99) b
= 0; // -0 substitute
557 else if (mode
!= LED_PROBE
) {
563 if (mode
!= LED_PROBE
) {
564 gpio_write(1 << b
, mode
);
571 if (c
< 16) gpio_write(1 << c
, mode
);