Skip to content

Commit 0aaf72a

Browse files
spidey3drashna
authored andcommitted
Address wake from sleep instability (qmk#11450)
* resolve race condition between suspend and wake in LUFA * avoid multiple calls to suspend_power_down() / suspend_wakeup_init() * Remove duplicate suspend_power_down_kb() call * pause on wakeup to wait for USB state to settle * need the repeated suspend_power_down() (that's where the sleep is) * more efficient implementation * fine tune the pause after sending wakeup * speculative chibios version of pause-after-wake * make wakeup delay configurable, and adjust value * better location for wakeup delay
1 parent 9ee0271 commit 0aaf72a

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

tmk_core/common/avr/suspend.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ static void power_down(uint8_t wdto) {
110110
rgblight_disable_noeeprom();
111111
}
112112
# endif
113-
suspend_power_down_kb();
114113

115114
// TODO: more power saving
116115
// See PicoPower application note

tmk_core/common/suspend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ void suspend_wakeup_init_user(void);
1212
void suspend_wakeup_init_kb(void);
1313
void suspend_power_down_user(void);
1414
void suspend_power_down_kb(void);
15+
16+
#ifndef USB_SUSPEND_WAKEUP_DELAY
17+
# define USB_SUSPEND_WAKEUP_DELAY 200
18+
#endif

tmk_core/protocol/chibios/usb_main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,17 @@ void init_usb_driver(USBDriver *usbp) {
701701
void restart_usb_driver(USBDriver *usbp) {
702702
usbStop(usbp);
703703
usbDisconnectBus(usbp);
704+
705+
#if USB_SUSPEND_WAKEUP_DELAY > 0
706+
// Some hubs, kvm switches, and monitors do
707+
// weird things, with USB device state bouncing
708+
// around wildly on wakeup, yielding race
709+
// conditions that can corrupt the keyboard state.
710+
//
711+
// Pause for a while to let things settle...
712+
wait_ms(USB_SUSPEND_WAKEUP_DELAY);
713+
#endif
714+
704715
usbStart(usbp, &usbcfg);
705716
usbConnectBus(usbp);
706717
}

tmk_core/protocol/lufa/lufa.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,9 @@ void EVENT_USB_Device_Suspend() {
498498
*/
499499
void EVENT_USB_Device_WakeUp() {
500500
print("[W]");
501+
#if defined(NO_USB_STARTUP_CHECK)
501502
suspend_wakeup_init();
503+
#endif
502504

503505
#ifdef SLEEP_LED_ENABLE
504506
sleep_led_disable();
@@ -1184,12 +1186,26 @@ int main(void) {
11841186
print("Keyboard start.\n");
11851187
while (1) {
11861188
#if !defined(NO_USB_STARTUP_CHECK)
1187-
while (USB_DeviceState == DEVICE_STATE_Suspended) {
1189+
if (USB_DeviceState == DEVICE_STATE_Suspended) {
11881190
print("[s]");
1189-
suspend_power_down();
1190-
if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
1191-
USB_Device_SendRemoteWakeup();
1191+
while (USB_DeviceState == DEVICE_STATE_Suspended) {
1192+
suspend_power_down();
1193+
if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) {
1194+
USB_Device_SendRemoteWakeup();
1195+
clear_keyboard();
1196+
1197+
# if USB_SUSPEND_WAKEUP_DELAY > 0
1198+
// Some hubs, kvm switches, and monitors do
1199+
// weird things, with USB device state bouncing
1200+
// around wildly on wakeup, yielding race
1201+
// conditions that can corrupt the keyboard state.
1202+
//
1203+
// Pause for a while to let things settle...
1204+
wait_ms(USB_SUSPEND_WAKEUP_DELAY);
1205+
# endif
1206+
}
11921207
}
1208+
suspend_wakeup_init();
11931209
}
11941210
#endif
11951211

0 commit comments

Comments
 (0)