diff options
author | Andre Brait <[email protected]> | 2024-10-06 10:50:39 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-10-06 21:50:39 +1300 |
commit | 7f475b590a1c497ca77c818cd295a94c606b7e6e (patch) | |
tree | c75ac7e9383990217f6c5dec0b34c515d05f8c9f /quantum/os_detection.c | |
parent | a322107cb7b347a8c2020fcab5b9505c328daa24 (diff) | |
download | qmk_firmware-7f475b590a1c497ca77c818cd295a94c606b7e6e.tar.gz qmk_firmware-7f475b590a1c497ca77c818cd295a94c606b7e6e.zip |
Tweak OS detect, add OS_DETECTION_SINGLE_REPORT (#24379)
* Default OS_DETECTION_DEBOUNCE bumped from 200ms to 250ms
* Add OS_DETECTION_SINGLE_REPORT to prevent undesired multiple reports
* Prevents random stability issues on ARM MacBooks after switching via KVM
* Works for every device I could test, including ARM MacBooks
* Disabled by default to keep current behavior
* Add Troubleshooting section on documentation
* Tweak reset logic to prevent a freeze with some KVMs
The USB stack on ARM MacBooks is more similar to that of iOS and,
for some reason, it seems to like sending packets that influence
the OS detection and results in a second OS_MACOS report being sent
at a random period of time after plugging the keyboard back. This
does not always happen and the consequences of this vary based on
what the user is doing in the callback, but since this is not
obvious and it's hard to debug, I've decided to add a flag for
those affected by such issue. The stability issue I had in mine was
a combination of factors and I found the actual cause being my own
bad math when changing the default layer, but this change alone is
also confirmed to fix it. Lastly, soem KVMs seem to leave the USB
controlled in a suspended state when cold-booting Windows, meaning
the keyboard would hang and the reset logic would not work. This
tunes it so that it can get out of such state. Also retested for
compatibility with my old KVM to ensure the logic works for both.
Diffstat (limited to 'quantum/os_detection.c')
-rw-r--r-- | quantum/os_detection.c | 54 |
1 files changed, 33 insertions, 21 deletions
diff --git a/quantum/os_detection.c b/quantum/os_detection.c index 96b026e247..99ffe1927a 100644 --- a/quantum/os_detection.c +++ b/quantum/os_detection.c @@ -34,7 +34,7 @@ static uint16_t usb_setups[STORED_USB_SETUPS]; #endif #ifndef OS_DETECTION_DEBOUNCE -# define OS_DETECTION_DEBOUNCE 200 +# define OS_DETECTION_DEBOUNCE 250 #endif // 2s should always be more than enough (otherwise, you may have other issues) @@ -59,25 +59,40 @@ struct setups_data_t setups_data = { }; static volatile os_variant_t detected_os = OS_UNSURE; -static os_variant_t reported_os = OS_UNSURE; +static volatile os_variant_t reported_os = OS_UNSURE; // we need to be able to report OS_UNSURE if that is the stable result of the guesses -static bool first_report = true; +static volatile bool first_report = true; // to react on USB state changes -static volatile enum usb_device_state current_usb_device_state = USB_DEVICE_STATE_INIT; -static enum usb_device_state reported_usb_device_state = USB_DEVICE_STATE_INIT; +static volatile enum usb_device_state current_usb_device_state = USB_DEVICE_STATE_NO_INIT; +static volatile enum usb_device_state maxprev_usb_device_state = USB_DEVICE_STATE_NO_INIT; // the OS detection might be unstable for a while, "debounce" it static volatile bool debouncing = false; -static volatile fast_timer_t last_time; +static volatile fast_timer_t last_time = 0; void os_detection_task(void) { +#ifdef OS_DETECTION_KEYBOARD_RESET + // resetting the keyboard on the USB device state change callback results in instability, so delegate that to this task + // only take action if it's been stable at least once, to avoid issues with some KVMs + if (current_usb_device_state <= USB_DEVICE_STATE_INIT && maxprev_usb_device_state >= USB_DEVICE_STATE_CONFIGURED) { + if (debouncing && timer_elapsed_fast(last_time) >= OS_DETECTION_DEBOUNCE) { + soft_reset_keyboard(); + } + return; + } +#endif +#ifdef OS_DETECTION_SINGLE_REPORT + if (!first_report) { + return; + } +#endif if (current_usb_device_state == USB_DEVICE_STATE_CONFIGURED) { // debouncing goes for both the detected OS as well as the USB state if (debouncing && timer_elapsed_fast(last_time) >= OS_DETECTION_DEBOUNCE) { - debouncing = false; - reported_usb_device_state = current_usb_device_state; + debouncing = false; + last_time = 0; if (detected_os != reported_os || first_report) { first_report = false; reported_os = detected_os; @@ -85,13 +100,6 @@ void os_detection_task(void) { } } } -#ifdef OS_DETECTION_KEYBOARD_RESET - // resetting the keyboard on the USB device state change callback results in instability, so delegate that to this task - // only take action if it's been stable at least once, to avoid issues with some KVMs - else if (current_usb_device_state == USB_DEVICE_STATE_INIT && reported_usb_device_state != USB_DEVICE_STATE_INIT) { - soft_reset_keyboard(); - } -#endif } __attribute__((weak)) bool process_detected_host_os_kb(os_variant_t detected_os) { @@ -155,16 +163,20 @@ os_variant_t detected_host_os(void) { void erase_wlength_data(void) { memset(&setups_data, 0, sizeof(setups_data)); - detected_os = OS_UNSURE; - reported_os = OS_UNSURE; - current_usb_device_state = USB_DEVICE_STATE_INIT; - reported_usb_device_state = USB_DEVICE_STATE_INIT; - debouncing = false; - first_report = true; + detected_os = OS_UNSURE; + reported_os = OS_UNSURE; + current_usb_device_state = USB_DEVICE_STATE_NO_INIT; + maxprev_usb_device_state = USB_DEVICE_STATE_NO_INIT; + debouncing = false; + last_time = 0; + first_report = true; } void os_detection_notify_usb_device_state_change(enum usb_device_state usb_device_state) { // treat this like any other source of instability + if (maxprev_usb_device_state < current_usb_device_state) { + maxprev_usb_device_state = current_usb_device_state; + } current_usb_device_state = usb_device_state; last_time = timer_read_fast(); debouncing = true; |