logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

os_detection.c (8677B)


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