Align VUSB suspend protocol logic (#22688)

This commit is contained in:
Joel Challis 2024-02-01 15:33:57 +00:00 committed by GitHub
parent 6761936fcd
commit 79020519b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -22,7 +22,7 @@
#include "keyboard.h" #include "keyboard.h"
#include "host.h" #include "host.h"
#include "timer.h" #include "timer.h"
#include "print.h" #include "debug.h"
#include "suspend.h" #include "suspend.h"
#include "wait.h" #include "wait.h"
#include "sendchar.h" #include "sendchar.h"
@ -53,7 +53,7 @@ static void initForUsbConnectivity(void) {
usbDeviceConnect(); usbDeviceConnect();
} }
static void vusb_send_remote_wakeup(void) { static inline void vusb_send_remote_wakeup(void) {
cli(); cli();
uint8_t ddr_orig = USBDDR; uint8_t ddr_orig = USBDDR;
@ -72,9 +72,7 @@ static void vusb_send_remote_wakeup(void) {
bool vusb_suspended = false; bool vusb_suspended = false;
static void vusb_suspend(void) { static inline void vusb_suspend(void) {
vusb_suspended = true;
#ifdef SLEEP_LED_ENABLE #ifdef SLEEP_LED_ENABLE
sleep_led_enable(); sleep_led_enable();
#endif #endif
@ -82,16 +80,13 @@ static void vusb_suspend(void) {
suspend_power_down(); suspend_power_down();
} }
#if USB_COUNT_SOF static inline void vusb_wakeup(void) {
static void vusb_wakeup(void) {
vusb_suspended = false;
suspend_wakeup_init(); suspend_wakeup_init();
# ifdef SLEEP_LED_ENABLE #ifdef SLEEP_LED_ENABLE
sleep_led_disable(); sleep_led_disable();
# endif
}
#endif #endif
}
/** \brief Setup USB /** \brief Setup USB
* *
@ -125,49 +120,67 @@ void protocol_post_init(void) {
wait_ms(50); wait_ms(50);
} }
void protocol_task(void) { static inline bool should_do_suspend(void) {
#if USB_COUNT_SOF #if USB_COUNT_SOF
if (usbSofCount != 0) { if (usbSofCount != 0) {
usbSofCount = 0; usbSofCount = 0;
sof_timer = timer_read(); sof_timer = timer_read();
if (vusb_suspended) { vusb_suspended = false;
vusb_wakeup();
}
} else { } else {
// Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1) // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1)
if (!vusb_suspended && timer_elapsed(sof_timer) > 5) { if (!vusb_suspended && timer_elapsed(sof_timer) > 5) {
vusb_suspend(); vusb_suspended = true;
} }
} }
#endif #endif
if (vusb_suspended) { return vusb_suspended;
vusb_suspend(); }
if (suspend_wakeup_condition()) {
vusb_send_remote_wakeup();
}
} else {
usbPoll();
// TODO: configuration process is inconsistent. it sometime fails. void protocol_task(void) {
// To prevent failing to configure NOT scan keyboard during configuration #if !defined(NO_USB_STARTUP_CHECK)
if (usbConfiguration && usbInterruptIsReady()) { if (should_do_suspend()) {
keyboard_task(); 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 #ifdef RAW_ENABLE
usbPoll(); usbPoll();
if (usbConfiguration && usbInterruptIsReady4()) { if (usbConfiguration && usbInterruptIsReady4()) {
raw_hid_task(); raw_hid_task();
} }
#endif #endif
#ifdef CONSOLE_ENABLE #ifdef CONSOLE_ENABLE
usbPoll(); usbPoll();
if (usbConfiguration && usbInterruptIsReady3()) { if (usbConfiguration && usbInterruptIsReady3()) {
console_task(); console_task();
}
#endif
} }
#endif
} }