4 Copyright (C) 2006-2009 Jonathan Zarate
9 #include <sys/reboot.h>
17 static int get_btn(const char *name
, uint32_t *bit
, uint32_t *pushed
)
22 if (nvget_gpio(name
, &gpio
, &inv
)) {
24 *pushed
= inv
? 0 : *bit
;
30 int buttons_main(int argc
, char *argv
[])
38 uint32_t reset_pushed
;
41 int brau_count_stable
;
49 ses_mask
= ses_pushed
= 0;
56 switch (nvram_get_int("btn_override") ? MODEL_UNKNOWN
: get_model()) {
74 case MODEL_WHR2A54G54
:
77 reset_mask
= reset_pushed
= 1 << 4;
82 reset_mask
= reset_pushed
= 1 << 4;
85 reset_mask
= reset_pushed
= 1 << 7;
86 ses_mask
= ses_pushed
= 1 << 4;
92 case MODEL_WZRRSG54HP
:
94 reset_mask
= reset_pushed
= 1 << 4;
99 reset_mask
= reset_pushed
= 1 << 7;
111 reset_mask
= reset_pushed
= 1 << 0;
112 ses_mask
= ses_pushed
= 1 << 4;
115 reset_mask
= reset_pushed
= 1 << 6;
116 ses_mask
= ses_pushed
= 1 << 7;
121 ses_mask
= 1 << 6; // WLAN button on H618B
123 case MODEL_WL500GPv2
:
129 //? reset_mask = reset_pushed = 1 << 7;
132 reset_mask
= reset_pushed
= 1 << 7;
147 brau_mask
= (1 << 4) | (1 << 5) | (1 << 6);
158 case MODEL_WNR2000v2
:
167 case MODEL_F5D8235v3
:
172 case MODEL_WRT160Nv3
:
181 case MODEL_WRT610Nv2
:
192 case MODEL_WRT160Nv1
:
197 case MODEL_WRT310Nv1
:
202 get_btn("btn_ses", &ses_mask
, &ses_pushed
);
203 if (!get_btn("btn_reset", &reset_mask
, &reset_pushed
)) {
204 // fprintf(stderr, "Not supported.\n");
209 mask
= reset_mask
| ses_mask
| brau_mask
;
212 cprintf("reset_mask=0x%X reset_pushed=0x%X\n", reset_mask
, reset_pushed
);
213 cprintf("ses_mask=0x%X\n", ses_mask
);
214 cprintf("brau_mask=0x%X\n", brau_mask
);
215 cprintf("ses_led=%d\n", ses_led
);
217 if (fork() != 0) return 0;
221 signal(SIGCHLD
, chld_reap
);
223 if ((gf
= gpio_open(mask
)) < 0) return 1;
226 brau_count_stable
= 0;
229 if (((gpio
= _gpio_read(gf
)) == ~0) || (last
== (gpio
&= mask
) && !brau_flag
) || (check_action() != ACT_IDLE
)) {
231 cprintf("gpio = %X\n", gpio
);
237 if ((gpio
& reset_mask
) == reset_pushed
) {
239 cprintf("reset down\n");
247 if (++count
== 3) led(LED_DIAG
, 1);
248 } while (((gpio
= _gpio_read(gf
)) != ~0) && ((gpio
& reset_mask
) == reset_pushed
));
251 cprintf("reset count = %d\n", count
);
254 eval("mtd-erase", "-d", "nvram");
255 //nvram_set("restore_defaults", "1");
262 set_action(ACT_REBOOT
);
269 if ((ses_mask
) && ((gpio
& ses_mask
) == ses_pushed
)) {
272 // syslog(LOG_DEBUG, "ses-pushed: gpio=x%X, pushed=x%X, mask=x%X, count=%d", gpio, ses_pushed, ses_mask, count);
274 led(ses_led
, LED_ON
);
276 led(ses_led
, LED_OFF
);
279 } while (((gpio
= _gpio_read(gf
)) != ~0) && ((gpio
& ses_mask
) == ses_pushed
));
282 if ((ses_led
== LED_DMZ
) && (nvram_get_int("dmz_enable") > 0)) led(LED_DMZ
, 1);
284 // syslog(LOG_DEBUG, "ses-released: gpio=x%X, pushed=x%X, mask=x%X, count=%d", gpio, ses_pushed, ses_mask, count);
285 syslog(LOG_INFO
, "SES pushed. Count was %d.", count
);
287 if ((count
!= 3) && (count
!= 7) && (count
!= 11)) {
298 cprintf("ses func=%d\n", n
);
300 sprintf(s
, "sesx_b%d", n
);
301 // syslog(LOG_DEBUG, "ses-func: count=%d %s='%s'", count, s, nvram_safe_get(s));
302 if ((p
= nvram_get(s
)) != NULL
) {
304 case '1': // toggle wl
305 nvram_set("rrules_radio", "-1");
306 eval("radio", "toggle");
311 case '3': // shutdown
314 case '4': // run a script
315 sprintf(s
, "%d", count
);
316 run_nvscript("sesx_script", s
, 2);
319 case '5': // !!TB: unmount all USB drives
320 add_remove_usbhost("-2", 0);
333 last
= (gpio
& brau_mask
);
334 if (brau_state
!= last
) {
335 brau_flag
= (brau_state
!= ~0); // set to 1 to run at startup
337 brau_count_stable
= 0;
339 else if (brau_flag
&& ++brau_count_stable
> 2) { // stable for 2+ seconds
341 switch (nvram_get_int("btn_override") ? MODEL_UNKNOWN
: get_model()) {
344 p
= (brau_state
& (1 << 4)) ? "ap" : (brau_state
& (1 << 5)) ? "repeater" : "router";
348 p
= brau_state
? "auto" : "bridge";
351 nvram_set("brau_state", p
);
353 cprintf("bridge/auto state = %s\n", p
);
355 run_nvscript("script_brau", p
, 2);