You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.7 KiB

  1. /* Copyright 2022 Ruslan Sayfutdinov (@KapJI)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "os_detection.h"
  17. #include <string.h>
  18. #include "timer.h"
  19. #ifdef OS_DETECTION_KEYBOARD_RESET
  20. # include "quantum.h"
  21. #endif
  22. #ifdef OS_DETECTION_DEBUG_ENABLE
  23. # include "eeconfig.h"
  24. # include "eeprom.h"
  25. # include "print.h"
  26. # define STORED_USB_SETUPS 50
  27. # define EEPROM_USER_OFFSET (uint8_t*)EECONFIG_SIZE
  28. static uint16_t usb_setups[STORED_USB_SETUPS];
  29. #endif
  30. #ifndef OS_DETECTION_DEBOUNCE
  31. # define OS_DETECTION_DEBOUNCE 200
  32. #endif
  33. // 2s should always be more than enough (otherwise, you may have other issues)
  34. #if OS_DETECTION_DEBOUNCE > 2000
  35. # undef OS_DETECTION_DEBOUNCE
  36. # define OS_DETECTION_DEBOUNCE 2000
  37. #endif
  38. struct setups_data_t {
  39. uint8_t count;
  40. uint8_t cnt_02;
  41. uint8_t cnt_04;
  42. uint8_t cnt_ff;
  43. uint16_t last_wlength;
  44. };
  45. struct setups_data_t setups_data = {
  46. .count = 0,
  47. .cnt_02 = 0,
  48. .cnt_04 = 0,
  49. .cnt_ff = 0,
  50. };
  51. static volatile os_variant_t detected_os = OS_UNSURE;
  52. static os_variant_t reported_os = OS_UNSURE;
  53. // we need to be able to report OS_UNSURE if that is the stable result of the guesses
  54. static bool first_report = true;
  55. // to react on USB state changes
  56. static volatile enum usb_device_state current_usb_device_state = USB_DEVICE_STATE_INIT;
  57. static enum usb_device_state reported_usb_device_state = USB_DEVICE_STATE_INIT;
  58. // the OS detection might be unstable for a while, "debounce" it
  59. static volatile bool debouncing = false;
  60. static volatile fast_timer_t last_time;
  61. void os_detection_task(void) {
  62. if (current_usb_device_state == USB_DEVICE_STATE_CONFIGURED) {
  63. // debouncing goes for both the detected OS as well as the USB state
  64. if (debouncing && timer_elapsed_fast(last_time) >= OS_DETECTION_DEBOUNCE) {
  65. debouncing = false;
  66. reported_usb_device_state = current_usb_device_state;
  67. if (detected_os != reported_os || first_report) {
  68. first_report = false;
  69. reported_os = detected_os;
  70. process_detected_host_os_kb(detected_os);
  71. }
  72. }
  73. }
  74. #ifdef OS_DETECTION_KEYBOARD_RESET
  75. // resetting the keyboard on the USB device state change callback results in instability, so delegate that to this task
  76. // only take action if it's been stable at least once, to avoid issues with some KVMs
  77. else if (current_usb_device_state == USB_DEVICE_STATE_INIT && reported_usb_device_state != USB_DEVICE_STATE_INIT) {
  78. soft_reset_keyboard();
  79. }
  80. #endif
  81. }
  82. __attribute__((weak)) bool process_detected_host_os_kb(os_variant_t detected_os) {
  83. return process_detected_host_os_user(detected_os);
  84. }
  85. __attribute__((weak)) bool process_detected_host_os_user(os_variant_t detected_os) {
  86. return true;
  87. }
  88. // Some collected sequences of wLength can be found in tests.
  89. void process_wlength(const uint16_t w_length) {
  90. #ifdef OS_DETECTION_DEBUG_ENABLE
  91. usb_setups[setups_data.count] = w_length;
  92. #endif
  93. setups_data.count++;
  94. setups_data.last_wlength = w_length;
  95. if (w_length == 0x2) {
  96. setups_data.cnt_02++;
  97. } else if (w_length == 0x4) {
  98. setups_data.cnt_04++;
  99. } else if (w_length == 0xFF) {
  100. setups_data.cnt_ff++;
  101. }
  102. // now try to make a guess
  103. os_variant_t guessed = OS_UNSURE;
  104. if (setups_data.count >= 3) {
  105. if (setups_data.cnt_ff >= 2 && setups_data.cnt_04 >= 1) {
  106. guessed = OS_WINDOWS;
  107. } else if (setups_data.count == setups_data.cnt_ff) {
  108. // Linux has 3 packets with 0xFF.
  109. guessed = OS_LINUX;
  110. } else if (setups_data.count == 5 && setups_data.last_wlength == 0xFF && setups_data.cnt_ff == 1 && setups_data.cnt_02 == 2) {
  111. guessed = OS_MACOS;
  112. } else if (setups_data.count == 4 && setups_data.cnt_ff == 0 && setups_data.cnt_02 == 2) {
  113. // iOS and iPadOS don't have the last 0xFF packet.
  114. guessed = OS_IOS;
  115. } else if (setups_data.cnt_ff == 0 && setups_data.cnt_02 == 3 && setups_data.cnt_04 == 1) {
  116. // This is actually PS5.
  117. guessed = OS_LINUX;
  118. } else if (setups_data.cnt_ff >= 1 && setups_data.cnt_02 == 0 && setups_data.cnt_04 == 0) {
  119. // This is actually Quest 2 or Nintendo Switch.
  120. guessed = OS_LINUX;
  121. }
  122. }
  123. // only replace the guessed value if not unsure
  124. if (guessed != OS_UNSURE) {
  125. detected_os = guessed;
  126. }
  127. // whatever the result, debounce
  128. last_time = timer_read_fast();
  129. debouncing = true;
  130. }
  131. os_variant_t detected_host_os(void) {
  132. return detected_os;
  133. }
  134. void erase_wlength_data(void) {
  135. memset(&setups_data, 0, sizeof(setups_data));
  136. detected_os = OS_UNSURE;
  137. reported_os = OS_UNSURE;
  138. current_usb_device_state = USB_DEVICE_STATE_INIT;
  139. reported_usb_device_state = USB_DEVICE_STATE_INIT;
  140. debouncing = false;
  141. first_report = true;
  142. }
  143. void os_detection_notify_usb_device_state_change(enum usb_device_state usb_device_state) {
  144. // treat this like any other source of instability
  145. current_usb_device_state = usb_device_state;
  146. last_time = timer_read_fast();
  147. debouncing = true;
  148. }
  149. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
  150. void slave_update_detected_host_os(os_variant_t os) {
  151. detected_os = os;
  152. last_time = timer_read_fast();
  153. debouncing = true;
  154. }
  155. #endif
  156. #ifdef OS_DETECTION_DEBUG_ENABLE
  157. void print_stored_setups(void) {
  158. # ifdef CONSOLE_ENABLE
  159. uint8_t cnt = eeprom_read_byte(EEPROM_USER_OFFSET);
  160. for (uint16_t i = 0; i < cnt; ++i) {
  161. uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
  162. xprintf("i: %d, wLength: 0x%02X\n", i, eeprom_read_word(addr));
  163. }
  164. # endif
  165. }
  166. void store_setups_in_eeprom(void) {
  167. eeprom_update_byte(EEPROM_USER_OFFSET, setups_data.count);
  168. for (uint16_t i = 0; i < setups_data.count; ++i) {
  169. uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
  170. eeprom_update_word(addr, usb_setups[i]);
  171. }
  172. }
  173. #endif // OS_DETECTION_DEBUG_ENABLE