summaryrefslogtreecommitdiff
path: root/tmk_core
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2024-02-01 15:33:57 +0000
committerGitHub <noreply@github.com>2024-02-01 15:33:57 +0000
commit79020519b4393c8a4f37c9b5fbd214a0f71192d4 (patch)
treea59acc96c2c8085d842ff5d30fba4e44a8778dc2 /tmk_core
parent6761936fcd28d21df7e6dde27584d75d0df22262 (diff)
Align VUSB suspend protocol logic (#22688)
Diffstat (limited to 'tmk_core')
-rw-r--r--tmk_core/protocol/vusb/protocol.c89
1 files 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 @@
22#include "keyboard.h" 22#include "keyboard.h"
23#include "host.h" 23#include "host.h"
24#include "timer.h" 24#include "timer.h"
25#include "print.h" 25#include "debug.h"
26#include "suspend.h" 26#include "suspend.h"
27#include "wait.h" 27#include "wait.h"
28#include "sendchar.h" 28#include "sendchar.h"
@@ -53,7 +53,7 @@ static void initForUsbConnectivity(void) {
53 usbDeviceConnect(); 53 usbDeviceConnect();
54} 54}
55 55
56static void vusb_send_remote_wakeup(void) { 56static inline void vusb_send_remote_wakeup(void) {
57 cli(); 57 cli();
58 58
59 uint8_t ddr_orig = USBDDR; 59 uint8_t ddr_orig = USBDDR;
@@ -72,9 +72,7 @@ static void vusb_send_remote_wakeup(void) {
72 72
73bool vusb_suspended = false; 73bool vusb_suspended = false;
74 74
75static void vusb_suspend(void) { 75static inline void vusb_suspend(void) {
76 vusb_suspended = true;
77
78#ifdef SLEEP_LED_ENABLE 76#ifdef SLEEP_LED_ENABLE
79 sleep_led_enable(); 77 sleep_led_enable();
80#endif 78#endif
@@ -82,16 +80,13 @@ static void vusb_suspend(void) {
82 suspend_power_down(); 80 suspend_power_down();
83} 81}
84 82
85#if USB_COUNT_SOF 83static inline void vusb_wakeup(void) {
86static void vusb_wakeup(void) {
87 vusb_suspended = false;
88 suspend_wakeup_init(); 84 suspend_wakeup_init();
89 85
90# ifdef SLEEP_LED_ENABLE 86#ifdef SLEEP_LED_ENABLE
91 sleep_led_disable(); 87 sleep_led_disable();
92# endif
93}
94#endif 88#endif
89}
95 90
96/** \brief Setup USB 91/** \brief Setup USB
97 * 92 *
@@ -125,49 +120,67 @@ void protocol_post_init(void) {
125 wait_ms(50); 120 wait_ms(50);
126} 121}
127 122
128void protocol_task(void) { 123static inline bool should_do_suspend(void) {
129#if USB_COUNT_SOF 124#if USB_COUNT_SOF
130 if (usbSofCount != 0) { 125 if (usbSofCount != 0) {
131 usbSofCount = 0; 126 usbSofCount = 0;
132 sof_timer = timer_read(); 127 sof_timer = timer_read();
133 if (vusb_suspended) { 128 vusb_suspended = false;
134 vusb_wakeup();
135 }
136 } else { 129 } else {
137 // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1) 130 // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
138 if (!vusb_suspended && timer_elapsed(sof_timer) > 5) { 131 if (!vusb_suspended && timer_elapsed(sof_timer) > 5) {
139 vusb_suspend(); 132 vusb_suspended = true;
140 } 133 }
141 } 134 }
142#endif 135#endif
143 if (vusb_suspended) { 136 return vusb_suspended;
144 vusb_suspend(); 137}
145 if (suspend_wakeup_condition()) {
146 vusb_send_remote_wakeup();
147 }
148 } else {
149 usbPoll();
150 138
151 // TODO: configuration process is inconsistent. it sometime fails. 139void protocol_task(void) {
152 // To prevent failing to configure NOT scan keyboard during configuration 140#if !defined(NO_USB_STARTUP_CHECK)
153 if (usbConfiguration && usbInterruptIsReady()) { 141 if (should_do_suspend()) {
154 keyboard_task(); 142 dprintln("suspending keyboard");
143 while (should_do_suspend()) {
144 vusb_suspend();
145 if (suspend_wakeup_condition()) {
146 vusb_send_remote_wakeup();
147
148# if USB_SUSPEND_WAKEUP_DELAY > 0
149 // Some hubs, kvm switches, and monitors do
150 // weird things, with USB device state bouncing
151 // around wildly on wakeup, yielding race
152 // conditions that can corrupt the keyboard state.
153 //
154 // Pause for a while to let things settle...
155 wait_ms(USB_SUSPEND_WAKEUP_DELAY);
156# endif
157 }
155 } 158 }
159 vusb_wakeup();
160 }
161#endif
162
163 usbPoll();
164
165 // TODO: configuration process is inconsistent. it sometime fails.
166 // To prevent failing to configure NOT scan keyboard during configuration
167 if (usbConfiguration && usbInterruptIsReady()) {
168 keyboard_task();
169 }
156 170
157#ifdef RAW_ENABLE 171#ifdef RAW_ENABLE
158 usbPoll(); 172 usbPoll();
159 173
160 if (usbConfiguration && usbInterruptIsReady4()) { 174 if (usbConfiguration && usbInterruptIsReady4()) {
161 raw_hid_task(); 175 raw_hid_task();
162 } 176 }
163#endif 177#endif
164 178
165#ifdef CONSOLE_ENABLE 179#ifdef CONSOLE_ENABLE
166 usbPoll(); 180 usbPoll();
167 181
168 if (usbConfiguration && usbInterruptIsReady3()) { 182 if (usbConfiguration && usbInterruptIsReady3()) {
169 console_task(); 183 console_task();
170 }
171#endif
172 } 184 }
185#endif
173} 186}