os_detection.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 250
  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 volatile 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 volatile bool first_report = true;
  55. // to react on USB state changes
  56. static volatile struct usb_device_state current_usb_device_state = {.configure_state = USB_DEVICE_STATE_NO_INIT};
  57. static volatile struct usb_device_state maxprev_usb_device_state = {.configure_state = USB_DEVICE_STATE_NO_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 = 0;
  61. void os_detection_task(void) {
  62. #ifdef OS_DETECTION_KEYBOARD_RESET
  63. // resetting the keyboard on the USB device state change callback results in instability, so delegate that to this task
  64. // only take action if it's been stable at least once, to avoid issues with some KVMs
  65. if (current_usb_device_state.configure_state <= USB_DEVICE_STATE_INIT && maxprev_usb_device_state.configure_state >= USB_DEVICE_STATE_CONFIGURED) {
  66. if (debouncing && timer_elapsed_fast(last_time) >= OS_DETECTION_DEBOUNCE) {
  67. soft_reset_keyboard();
  68. }
  69. return;
  70. }
  71. #endif
  72. #ifdef OS_DETECTION_SINGLE_REPORT
  73. if (!first_report) {
  74. return;
  75. }
  76. #endif
  77. if (current_usb_device_state.configure_state == USB_DEVICE_STATE_CONFIGURED) {
  78. // debouncing goes for both the detected OS as well as the USB state
  79. if (debouncing && timer_elapsed_fast(last_time) >= OS_DETECTION_DEBOUNCE) {
  80. debouncing = false;
  81. last_time = 0;
  82. if (detected_os != reported_os || first_report) {
  83. first_report = false;
  84. reported_os = detected_os;
  85. process_detected_host_os_kb(detected_os);
  86. }
  87. }
  88. }
  89. }
  90. __attribute__((weak)) bool process_detected_host_os_kb(os_variant_t detected_os) {
  91. return process_detected_host_os_user(detected_os);
  92. }
  93. __attribute__((weak)) bool process_detected_host_os_user(os_variant_t detected_os) {
  94. return true;
  95. }
  96. // Some collected sequences of wLength can be found in tests.
  97. void process_wlength(const uint16_t w_length) {
  98. #ifdef OS_DETECTION_DEBUG_ENABLE
  99. usb_setups[setups_data.count] = w_length;
  100. #endif
  101. setups_data.count++;
  102. setups_data.last_wlength = w_length;
  103. if (w_length == 0x2) {
  104. setups_data.cnt_02++;
  105. } else if (w_length == 0x4) {
  106. setups_data.cnt_04++;
  107. } else if (w_length == 0xFF) {
  108. setups_data.cnt_ff++;
  109. }
  110. // now try to make a guess
  111. os_variant_t guessed = OS_UNSURE;
  112. if (setups_data.count >= 3) {
  113. if (setups_data.cnt_ff >= 2 && setups_data.cnt_04 >= 1) {
  114. guessed = OS_WINDOWS;
  115. } else if (setups_data.count == setups_data.cnt_ff) {
  116. // Linux has 3 packets with 0xFF.
  117. guessed = OS_LINUX;
  118. } else if (setups_data.count == 5 && setups_data.last_wlength == 0xFF && setups_data.cnt_ff == 1 && setups_data.cnt_02 == 2) {
  119. guessed = OS_MACOS;
  120. } else if (setups_data.count == 4 && setups_data.cnt_ff == 0 && setups_data.cnt_02 == 2) {
  121. // iOS and iPadOS don't have the last 0xFF packet.
  122. guessed = OS_IOS;
  123. } else if (setups_data.cnt_ff == 0 && setups_data.cnt_02 == 3 && setups_data.cnt_04 == 1) {
  124. // This is actually PS5.
  125. guessed = OS_LINUX;
  126. } else if (setups_data.cnt_ff >= 1 && setups_data.cnt_02 == 0 && setups_data.cnt_04 == 0) {
  127. // This is actually Quest 2 or Nintendo Switch.
  128. guessed = OS_LINUX;
  129. }
  130. }
  131. // only replace the guessed value if not unsure
  132. if (guessed != OS_UNSURE) {
  133. detected_os = guessed;
  134. }
  135. // whatever the result, debounce
  136. last_time = timer_read_fast();
  137. debouncing = true;
  138. }
  139. os_variant_t detected_host_os(void) {
  140. return detected_os;
  141. }
  142. void erase_wlength_data(void) {
  143. memset(&setups_data, 0, sizeof(setups_data));
  144. detected_os = OS_UNSURE;
  145. reported_os = OS_UNSURE;
  146. current_usb_device_state.configure_state = USB_DEVICE_STATE_NO_INIT;
  147. maxprev_usb_device_state.configure_state = USB_DEVICE_STATE_NO_INIT;
  148. debouncing = false;
  149. last_time = 0;
  150. first_report = true;
  151. }
  152. void os_detection_notify_usb_device_state_change(struct usb_device_state usb_device_state) {
  153. // treat this like any other source of instability
  154. if (maxprev_usb_device_state.configure_state < current_usb_device_state.configure_state) {
  155. maxprev_usb_device_state.configure_state = current_usb_device_state.configure_state;
  156. }
  157. current_usb_device_state = usb_device_state;
  158. last_time = timer_read_fast();
  159. debouncing = true;
  160. }
  161. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_DETECTED_OS_ENABLE)
  162. void slave_update_detected_host_os(os_variant_t os) {
  163. detected_os = os;
  164. last_time = timer_read_fast();
  165. debouncing = true;
  166. }
  167. #endif
  168. #ifdef OS_DETECTION_DEBUG_ENABLE
  169. void print_stored_setups(void) {
  170. # ifdef CONSOLE_ENABLE
  171. uint8_t cnt = eeprom_read_byte(EEPROM_USER_OFFSET);
  172. for (uint16_t i = 0; i < cnt; ++i) {
  173. uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
  174. xprintf("i: %d, wLength: 0x%02X\n", i, eeprom_read_word(addr));
  175. }
  176. # endif
  177. }
  178. void store_setups_in_eeprom(void) {
  179. eeprom_update_byte(EEPROM_USER_OFFSET, setups_data.count);
  180. for (uint16_t i = 0; i < setups_data.count; ++i) {
  181. uint16_t* addr = (uint16_t*)EEPROM_USER_OFFSET + i * sizeof(uint16_t) + sizeof(uint8_t);
  182. eeprom_update_word(addr, usb_setups[i]);
  183. }
  184. }
  185. #endif // OS_DETECTION_DEBUG_ENABLE