From 79020519b4393c8a4f37c9b5fbd214a0f71192d4 Mon Sep 17 00:00:00 2001
From: Joel Challis <git@zvecr.com>
Date: Thu, 1 Feb 2024 15:33:57 +0000
Subject: [PATCH] Align VUSB suspend protocol logic (#22688)

---
 tmk_core/protocol/vusb/protocol.c | 89 ++++++++++++++++++-------------
 1 file changed, 51 insertions(+), 38 deletions(-)

diff --git a/tmk_core/protocol/vusb/protocol.c b/tmk_core/protocol/vusb/protocol.c
index 1f64561274..6178d48ef2 100644
--- a/tmk_core/protocol/vusb/protocol.c
+++ b/tmk_core/protocol/vusb/protocol.c
@@ -22,7 +22,7 @@
 #include "keyboard.h"
 #include "host.h"
 #include "timer.h"
-#include "print.h"
+#include "debug.h"
 #include "suspend.h"
 #include "wait.h"
 #include "sendchar.h"
@@ -53,7 +53,7 @@ static void initForUsbConnectivity(void) {
     usbDeviceConnect();
 }
 
-static void vusb_send_remote_wakeup(void) {
+static inline void vusb_send_remote_wakeup(void) {
     cli();
 
     uint8_t ddr_orig = USBDDR;
@@ -72,9 +72,7 @@ static void vusb_send_remote_wakeup(void) {
 
 bool vusb_suspended = false;
 
-static void vusb_suspend(void) {
-    vusb_suspended = true;
-
+static inline void vusb_suspend(void) {
 #ifdef SLEEP_LED_ENABLE
     sleep_led_enable();
 #endif
@@ -82,16 +80,13 @@ static void vusb_suspend(void) {
     suspend_power_down();
 }
 
-#if USB_COUNT_SOF
-static void vusb_wakeup(void) {
-    vusb_suspended = false;
+static inline void vusb_wakeup(void) {
     suspend_wakeup_init();
 
-#    ifdef SLEEP_LED_ENABLE
+#ifdef SLEEP_LED_ENABLE
     sleep_led_disable();
-#    endif
-}
 #endif
+}
 
 /** \brief Setup USB
  *
@@ -125,49 +120,67 @@ void protocol_post_init(void) {
     wait_ms(50);
 }
 
-void protocol_task(void) {
+static inline bool should_do_suspend(void) {
 #if USB_COUNT_SOF
     if (usbSofCount != 0) {
-        usbSofCount = 0;
-        sof_timer   = timer_read();
-        if (vusb_suspended) {
-            vusb_wakeup();
-        }
+        usbSofCount    = 0;
+        sof_timer      = timer_read();
+        vusb_suspended = false;
     } else {
         // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
         if (!vusb_suspended && timer_elapsed(sof_timer) > 5) {
-            vusb_suspend();
+            vusb_suspended = true;
         }
     }
 #endif
-    if (vusb_suspended) {
-        vusb_suspend();
-        if (suspend_wakeup_condition()) {
-            vusb_send_remote_wakeup();
-        }
-    } else {
-        usbPoll();
+    return vusb_suspended;
+}
 
-        // TODO: configuration process is inconsistent. it sometime fails.
-        // To prevent failing to configure NOT scan keyboard during configuration
-        if (usbConfiguration && usbInterruptIsReady()) {
-            keyboard_task();
+void protocol_task(void) {
+#if !defined(NO_USB_STARTUP_CHECK)
+    if (should_do_suspend()) {
+        dprintln("suspending keyboard");
+        while (should_do_suspend()) {
+            vusb_suspend();
+            if (suspend_wakeup_condition()) {
+                vusb_send_remote_wakeup();
+
+#    if USB_SUSPEND_WAKEUP_DELAY > 0
+                // Some hubs, kvm switches, and monitors do
+                // weird things, with USB device state bouncing
+                // around wildly on wakeup, yielding race
+                // conditions that can corrupt the keyboard state.
+                //
+                // Pause for a while to let things settle...
+                wait_ms(USB_SUSPEND_WAKEUP_DELAY);
+#    endif
+            }
         }
+        vusb_wakeup();
+    }
+#endif
+
+    usbPoll();
+
+    // TODO: configuration process is inconsistent. it sometime fails.
+    // To prevent failing to configure NOT scan keyboard during configuration
+    if (usbConfiguration && usbInterruptIsReady()) {
+        keyboard_task();
+    }
 
 #ifdef RAW_ENABLE
-        usbPoll();
+    usbPoll();
 
-        if (usbConfiguration && usbInterruptIsReady4()) {
-            raw_hid_task();
-        }
+    if (usbConfiguration && usbInterruptIsReady4()) {
+        raw_hid_task();
+    }
 #endif
 
 #ifdef CONSOLE_ENABLE
-        usbPoll();
+    usbPoll();
 
-        if (usbConfiguration && usbInterruptIsReady3()) {
-            console_task();
-        }
-#endif
+    if (usbConfiguration && usbInterruptIsReady3()) {
+        console_task();
     }
+#endif
 }