usb_main.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*
  2. * (c) 2015 flabberast <s3+flabbergast@sdfeu.org>
  3. *
  4. * Based on the following work:
  5. * - Guillaume Duc's raw hid example (MIT License)
  6. * https://github.com/guiduc/usb-hid-chibios-example
  7. * - PJRC Teensy examples (MIT License)
  8. * https://www.pjrc.com/teensy/usb_keyboard.html
  9. * - hasu's TMK keyboard code (GPL v2 and some code Modified BSD)
  10. * https://github.com/tmk/tmk_keyboard/
  11. * - ChibiOS demo code (Apache 2.0 License)
  12. * http://www.chibios.org
  13. *
  14. * Since some GPL'd code is used, this work is licensed under
  15. * GPL v2 or later.
  16. */
  17. /*
  18. * Implementation notes:
  19. *
  20. * USBEndpointConfig - Configured using explicit order instead of struct member name.
  21. * This is due to ChibiOS hal LLD differences, which is dependent on hardware,
  22. * "USBv1" devices have `ep_buffers` and "OTGv1" have `in_multiplier`.
  23. * Given `USBv1/hal_usb_lld.h` marks the field as "not currently used" this code file
  24. * makes the assumption this is safe to avoid littering with preprocessor directives.
  25. */
  26. #include <ch.h>
  27. #include <hal.h>
  28. #include <string.h>
  29. #include "usb_main.h"
  30. #include "host.h"
  31. #include "debug.h"
  32. #include "suspend.h"
  33. #ifdef SLEEP_LED_ENABLE
  34. # include "sleep_led.h"
  35. # include "led.h"
  36. #endif
  37. #include "wait.h"
  38. #include "usb_descriptor.h"
  39. #include "usb_driver.h"
  40. #ifdef NKRO_ENABLE
  41. # include "keycode_config.h"
  42. extern keymap_config_t keymap_config;
  43. #endif
  44. #ifdef JOYSTICK_ENABLE
  45. # include "joystick.h"
  46. #endif
  47. /* ---------------------------------------------------------
  48. * Global interface variables and declarations
  49. * ---------------------------------------------------------
  50. */
  51. #ifndef usb_lld_connect_bus
  52. # define usb_lld_connect_bus(usbp)
  53. #endif
  54. #ifndef usb_lld_disconnect_bus
  55. # define usb_lld_disconnect_bus(usbp)
  56. #endif
  57. uint8_t keyboard_idle __attribute__((aligned(2))) = 0;
  58. uint8_t keyboard_protocol __attribute__((aligned(2))) = 1;
  59. uint8_t keyboard_led_state = 0;
  60. volatile uint16_t keyboard_idle_count = 0;
  61. static virtual_timer_t keyboard_idle_timer;
  62. static void keyboard_idle_timer_cb(void *arg);
  63. report_keyboard_t keyboard_report_sent = {{0}};
  64. #ifdef MOUSE_ENABLE
  65. report_mouse_t mouse_report_blank = {0};
  66. #endif /* MOUSE_ENABLE */
  67. #ifdef EXTRAKEY_ENABLE
  68. uint8_t extra_report_blank[3] = {0};
  69. #endif /* EXTRAKEY_ENABLE */
  70. /* ---------------------------------------------------------
  71. * Descriptors and USB driver objects
  72. * ---------------------------------------------------------
  73. */
  74. /* HID specific constants */
  75. #define HID_GET_REPORT 0x01
  76. #define HID_GET_IDLE 0x02
  77. #define HID_GET_PROTOCOL 0x03
  78. #define HID_SET_REPORT 0x09
  79. #define HID_SET_IDLE 0x0A
  80. #define HID_SET_PROTOCOL 0x0B
  81. /*
  82. * Handles the GET_DESCRIPTOR callback
  83. *
  84. * Returns the proper descriptor
  85. */
  86. static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t wIndex) {
  87. (void)usbp;
  88. static USBDescriptor desc;
  89. uint16_t wValue = ((uint16_t)dtype << 8) | dindex;
  90. desc.ud_string = NULL;
  91. desc.ud_size = get_usb_descriptor(wValue, wIndex, (const void **const) & desc.ud_string);
  92. if (desc.ud_string == NULL)
  93. return NULL;
  94. else
  95. return &desc;
  96. }
  97. #ifndef KEYBOARD_SHARED_EP
  98. /* keyboard endpoint state structure */
  99. static USBInEndpointState kbd_ep_state;
  100. /* keyboard endpoint initialization structure (IN) - see USBEndpointConfig comment at top of file */
  101. static const USBEndpointConfig kbd_ep_config = {
  102. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
  103. NULL, /* SETUP packet notification callback */
  104. kbd_in_cb, /* IN notification callback */
  105. NULL, /* OUT notification callback */
  106. KEYBOARD_EPSIZE, /* IN maximum packet size */
  107. 0, /* OUT maximum packet size */
  108. &kbd_ep_state, /* IN Endpoint state */
  109. NULL, /* OUT endpoint state */
  110. 2, /* IN multiplier */
  111. NULL /* SETUP buffer (not a SETUP endpoint) */
  112. };
  113. #endif
  114. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  115. /* mouse endpoint state structure */
  116. static USBInEndpointState mouse_ep_state;
  117. /* mouse endpoint initialization structure (IN) - see USBEndpointConfig comment at top of file */
  118. static const USBEndpointConfig mouse_ep_config = {
  119. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
  120. NULL, /* SETUP packet notification callback */
  121. mouse_in_cb, /* IN notification callback */
  122. NULL, /* OUT notification callback */
  123. MOUSE_EPSIZE, /* IN maximum packet size */
  124. 0, /* OUT maximum packet size */
  125. &mouse_ep_state, /* IN Endpoint state */
  126. NULL, /* OUT endpoint state */
  127. 2, /* IN multiplier */
  128. NULL /* SETUP buffer (not a SETUP endpoint) */
  129. };
  130. #endif
  131. #ifdef SHARED_EP_ENABLE
  132. /* shared endpoint state structure */
  133. static USBInEndpointState shared_ep_state;
  134. /* shared endpoint initialization structure (IN) - see USBEndpointConfig comment at top of file */
  135. static const USBEndpointConfig shared_ep_config = {
  136. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
  137. NULL, /* SETUP packet notification callback */
  138. shared_in_cb, /* IN notification callback */
  139. NULL, /* OUT notification callback */
  140. SHARED_EPSIZE, /* IN maximum packet size */
  141. 0, /* OUT maximum packet size */
  142. &shared_ep_state, /* IN Endpoint state */
  143. NULL, /* OUT endpoint state */
  144. 2, /* IN multiplier */
  145. NULL /* SETUP buffer (not a SETUP endpoint) */
  146. };
  147. #endif
  148. #if STM32_USB_USE_OTG1
  149. typedef struct {
  150. size_t queue_capacity_in;
  151. size_t queue_capacity_out;
  152. USBInEndpointState in_ep_state;
  153. USBOutEndpointState out_ep_state;
  154. USBInEndpointState int_ep_state;
  155. USBEndpointConfig inout_ep_config;
  156. USBEndpointConfig int_ep_config;
  157. const QMKUSBConfig config;
  158. QMKUSBDriver driver;
  159. } usb_driver_config_t;
  160. #else
  161. typedef struct {
  162. size_t queue_capacity_in;
  163. size_t queue_capacity_out;
  164. USBInEndpointState in_ep_state;
  165. USBOutEndpointState out_ep_state;
  166. USBInEndpointState int_ep_state;
  167. USBEndpointConfig in_ep_config;
  168. USBEndpointConfig out_ep_config;
  169. USBEndpointConfig int_ep_config;
  170. const QMKUSBConfig config;
  171. QMKUSBDriver driver;
  172. } usb_driver_config_t;
  173. #endif
  174. #if STM32_USB_USE_OTG1
  175. /* Reusable initialization structure - see USBEndpointConfig comment at top of file */
  176. # define QMK_USB_DRIVER_CONFIG(stream, notification, fixedsize) \
  177. { \
  178. .queue_capacity_in = stream##_IN_CAPACITY, .queue_capacity_out = stream##_OUT_CAPACITY, \
  179. .inout_ep_config = \
  180. { \
  181. stream##_IN_MODE, /* Interrupt EP */ \
  182. NULL, /* SETUP packet notification callback */ \
  183. qmkusbDataTransmitted, /* IN notification callback */ \
  184. qmkusbDataReceived, /* OUT notification callback */ \
  185. stream##_EPSIZE, /* IN maximum packet size */ \
  186. stream##_EPSIZE, /* OUT maximum packet size */ \
  187. NULL, /* IN Endpoint state */ \
  188. NULL, /* OUT endpoint state */ \
  189. 2, /* IN multiplier */ \
  190. NULL /* SETUP buffer (not a SETUP endpoint) */ \
  191. }, \
  192. .int_ep_config = \
  193. { \
  194. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ \
  195. NULL, /* SETUP packet notification callback */ \
  196. qmkusbInterruptTransmitted, /* IN notification callback */ \
  197. NULL, /* OUT notification callback */ \
  198. CDC_NOTIFICATION_EPSIZE, /* IN maximum packet size */ \
  199. 0, /* OUT maximum packet size */ \
  200. NULL, /* IN Endpoint state */ \
  201. NULL, /* OUT endpoint state */ \
  202. 2, /* IN multiplier */ \
  203. NULL, /* SETUP buffer (not a SETUP endpoint) */ \
  204. }, \
  205. .config = { \
  206. .usbp = &USB_DRIVER, \
  207. .bulk_in = stream##_IN_EPNUM, \
  208. .bulk_out = stream##_OUT_EPNUM, \
  209. .int_in = notification, \
  210. .in_buffers = stream##_IN_CAPACITY, \
  211. .out_buffers = stream##_OUT_CAPACITY, \
  212. .in_size = stream##_EPSIZE, \
  213. .out_size = stream##_EPSIZE, \
  214. .fixed_size = fixedsize, \
  215. .ib = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_IN_CAPACITY, stream##_EPSIZE)]){}, \
  216. .ob = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_OUT_CAPACITY, stream##_EPSIZE)]){}, \
  217. } \
  218. }
  219. #else
  220. /* Reusable initialization structure - see USBEndpointConfig comment at top of file */
  221. # define QMK_USB_DRIVER_CONFIG(stream, notification, fixedsize) \
  222. { \
  223. .queue_capacity_in = stream##_IN_CAPACITY, .queue_capacity_out = stream##_OUT_CAPACITY, \
  224. .in_ep_config = \
  225. { \
  226. stream##_IN_MODE, /* Interrupt EP */ \
  227. NULL, /* SETUP packet notification callback */ \
  228. qmkusbDataTransmitted, /* IN notification callback */ \
  229. NULL, /* OUT notification callback */ \
  230. stream##_EPSIZE, /* IN maximum packet size */ \
  231. 0, /* OUT maximum packet size */ \
  232. NULL, /* IN Endpoint state */ \
  233. NULL, /* OUT endpoint state */ \
  234. 2, /* IN multiplier */ \
  235. NULL /* SETUP buffer (not a SETUP endpoint) */ \
  236. }, \
  237. .out_ep_config = \
  238. { \
  239. stream##_OUT_MODE, /* Interrupt EP */ \
  240. NULL, /* SETUP packet notification callback */ \
  241. NULL, /* IN notification callback */ \
  242. qmkusbDataReceived, /* OUT notification callback */ \
  243. 0, /* IN maximum packet size */ \
  244. stream##_EPSIZE, /* OUT maximum packet size */ \
  245. NULL, /* IN Endpoint state */ \
  246. NULL, /* OUT endpoint state */ \
  247. 2, /* IN multiplier */ \
  248. NULL, /* SETUP buffer (not a SETUP endpoint) */ \
  249. }, \
  250. .int_ep_config = \
  251. { \
  252. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ \
  253. NULL, /* SETUP packet notification callback */ \
  254. qmkusbInterruptTransmitted, /* IN notification callback */ \
  255. NULL, /* OUT notification callback */ \
  256. CDC_NOTIFICATION_EPSIZE, /* IN maximum packet size */ \
  257. 0, /* OUT maximum packet size */ \
  258. NULL, /* IN Endpoint state */ \
  259. NULL, /* OUT endpoint state */ \
  260. 2, /* IN multiplier */ \
  261. NULL, /* SETUP buffer (not a SETUP endpoint) */ \
  262. }, \
  263. .config = { \
  264. .usbp = &USB_DRIVER, \
  265. .bulk_in = stream##_IN_EPNUM, \
  266. .bulk_out = stream##_OUT_EPNUM, \
  267. .int_in = notification, \
  268. .in_buffers = stream##_IN_CAPACITY, \
  269. .out_buffers = stream##_OUT_CAPACITY, \
  270. .in_size = stream##_EPSIZE, \
  271. .out_size = stream##_EPSIZE, \
  272. .fixed_size = fixedsize, \
  273. .ib = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_IN_CAPACITY, stream##_EPSIZE)]){}, \
  274. .ob = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_OUT_CAPACITY, stream##_EPSIZE)]){}, \
  275. } \
  276. }
  277. #endif
  278. typedef struct {
  279. union {
  280. struct {
  281. #ifdef CONSOLE_ENABLE
  282. usb_driver_config_t console_driver;
  283. #endif
  284. #ifdef RAW_ENABLE
  285. usb_driver_config_t raw_driver;
  286. #endif
  287. #ifdef MIDI_ENABLE
  288. usb_driver_config_t midi_driver;
  289. #endif
  290. #ifdef VIRTSER_ENABLE
  291. usb_driver_config_t serial_driver;
  292. #endif
  293. #ifdef JOYSTICK_ENABLE
  294. usb_driver_config_t joystick_driver;
  295. #endif
  296. };
  297. usb_driver_config_t array[0];
  298. };
  299. } usb_driver_configs_t;
  300. static usb_driver_configs_t drivers = {
  301. #ifdef CONSOLE_ENABLE
  302. # define CONSOLE_IN_CAPACITY 4
  303. # define CONSOLE_OUT_CAPACITY 4
  304. # define CONSOLE_IN_MODE USB_EP_MODE_TYPE_INTR
  305. # define CONSOLE_OUT_MODE USB_EP_MODE_TYPE_INTR
  306. .console_driver = QMK_USB_DRIVER_CONFIG(CONSOLE, 0, true),
  307. #endif
  308. #ifdef RAW_ENABLE
  309. # define RAW_IN_CAPACITY 4
  310. # define RAW_OUT_CAPACITY 4
  311. # define RAW_IN_MODE USB_EP_MODE_TYPE_INTR
  312. # define RAW_OUT_MODE USB_EP_MODE_TYPE_INTR
  313. .raw_driver = QMK_USB_DRIVER_CONFIG(RAW, 0, false),
  314. #endif
  315. #ifdef MIDI_ENABLE
  316. # define MIDI_STREAM_IN_CAPACITY 4
  317. # define MIDI_STREAM_OUT_CAPACITY 4
  318. # define MIDI_STREAM_IN_MODE USB_EP_MODE_TYPE_BULK
  319. # define MIDI_STREAM_OUT_MODE USB_EP_MODE_TYPE_BULK
  320. .midi_driver = QMK_USB_DRIVER_CONFIG(MIDI_STREAM, 0, false),
  321. #endif
  322. #ifdef VIRTSER_ENABLE
  323. # define CDC_IN_CAPACITY 4
  324. # define CDC_OUT_CAPACITY 4
  325. # define CDC_IN_MODE USB_EP_MODE_TYPE_BULK
  326. # define CDC_OUT_MODE USB_EP_MODE_TYPE_BULK
  327. .serial_driver = QMK_USB_DRIVER_CONFIG(CDC, CDC_NOTIFICATION_EPNUM, false),
  328. #endif
  329. #ifdef JOYSTICK_ENABLE
  330. # define JOYSTICK_IN_CAPACITY 4
  331. # define JOYSTICK_OUT_CAPACITY 4
  332. # define JOYSTICK_IN_MODE USB_EP_MODE_TYPE_BULK
  333. # define JOYSTICK_OUT_MODE USB_EP_MODE_TYPE_BULK
  334. .joystick_driver = QMK_USB_DRIVER_CONFIG(JOYSTICK, 0, false),
  335. #endif
  336. };
  337. #define NUM_USB_DRIVERS (sizeof(drivers) / sizeof(usb_driver_config_t))
  338. /* ---------------------------------------------------------
  339. * USB driver functions
  340. * ---------------------------------------------------------
  341. */
  342. #define USB_EVENT_QUEUE_SIZE 16
  343. usbevent_t event_queue[USB_EVENT_QUEUE_SIZE];
  344. uint8_t event_queue_head;
  345. uint8_t event_queue_tail;
  346. void usb_event_queue_init(void) {
  347. // Initialise the event queue
  348. memset(&event_queue, 0, sizeof(event_queue));
  349. event_queue_head = 0;
  350. event_queue_tail = 0;
  351. }
  352. static inline bool usb_event_queue_enqueue(usbevent_t event) {
  353. uint8_t next = (event_queue_head + 1) % USB_EVENT_QUEUE_SIZE;
  354. if (next == event_queue_tail) {
  355. return false;
  356. }
  357. event_queue[event_queue_head] = event;
  358. event_queue_head = next;
  359. return true;
  360. }
  361. static inline bool usb_event_queue_dequeue(usbevent_t *event) {
  362. if (event_queue_head == event_queue_tail) {
  363. return false;
  364. }
  365. *event = event_queue[event_queue_tail];
  366. event_queue_tail = (event_queue_tail + 1) % USB_EVENT_QUEUE_SIZE;
  367. return true;
  368. }
  369. static inline void usb_event_suspend_handler(void) {
  370. #ifdef SLEEP_LED_ENABLE
  371. sleep_led_enable();
  372. #endif /* SLEEP_LED_ENABLE */
  373. }
  374. static inline void usb_event_wakeup_handler(void) {
  375. suspend_wakeup_init();
  376. #ifdef SLEEP_LED_ENABLE
  377. sleep_led_disable();
  378. // NOTE: converters may not accept this
  379. led_set(host_keyboard_leds());
  380. #endif /* SLEEP_LED_ENABLE */
  381. }
  382. bool last_suspend_state = false;
  383. void usb_event_queue_task(void) {
  384. usbevent_t event;
  385. while (usb_event_queue_dequeue(&event)) {
  386. switch (event) {
  387. case USB_EVENT_SUSPEND:
  388. last_suspend_state = true;
  389. usb_event_suspend_handler();
  390. break;
  391. case USB_EVENT_WAKEUP:
  392. last_suspend_state = false;
  393. usb_event_wakeup_handler();
  394. break;
  395. default:
  396. // Nothing to do, we don't handle it.
  397. break;
  398. }
  399. }
  400. }
  401. /* Handles the USB driver global events
  402. * TODO: maybe disable some things when connection is lost? */
  403. static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
  404. switch (event) {
  405. case USB_EVENT_ADDRESS:
  406. return;
  407. case USB_EVENT_CONFIGURED:
  408. osalSysLockFromISR();
  409. /* Enable the endpoints specified into the configuration. */
  410. #ifndef KEYBOARD_SHARED_EP
  411. usbInitEndpointI(usbp, KEYBOARD_IN_EPNUM, &kbd_ep_config);
  412. #endif
  413. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  414. usbInitEndpointI(usbp, MOUSE_IN_EPNUM, &mouse_ep_config);
  415. #endif
  416. #ifdef SHARED_EP_ENABLE
  417. usbInitEndpointI(usbp, SHARED_IN_EPNUM, &shared_ep_config);
  418. #endif
  419. for (int i = 0; i < NUM_USB_DRIVERS; i++) {
  420. #if STM32_USB_USE_OTG1
  421. usbInitEndpointI(usbp, drivers.array[i].config.bulk_in, &drivers.array[i].inout_ep_config);
  422. #else
  423. usbInitEndpointI(usbp, drivers.array[i].config.bulk_in, &drivers.array[i].in_ep_config);
  424. usbInitEndpointI(usbp, drivers.array[i].config.bulk_out, &drivers.array[i].out_ep_config);
  425. #endif
  426. if (drivers.array[i].config.int_in) {
  427. usbInitEndpointI(usbp, drivers.array[i].config.int_in, &drivers.array[i].int_ep_config);
  428. }
  429. qmkusbConfigureHookI(&drivers.array[i].driver);
  430. }
  431. osalSysUnlockFromISR();
  432. if (last_suspend_state) {
  433. usb_event_queue_enqueue(USB_EVENT_WAKEUP);
  434. }
  435. return;
  436. case USB_EVENT_SUSPEND:
  437. usb_event_queue_enqueue(USB_EVENT_SUSPEND);
  438. /* Falls into.*/
  439. case USB_EVENT_UNCONFIGURED:
  440. /* Falls into.*/
  441. case USB_EVENT_RESET:
  442. for (int i = 0; i < NUM_USB_DRIVERS; i++) {
  443. chSysLockFromISR();
  444. /* Disconnection event on suspend.*/
  445. qmkusbSuspendHookI(&drivers.array[i].driver);
  446. chSysUnlockFromISR();
  447. }
  448. return;
  449. case USB_EVENT_WAKEUP:
  450. // TODO: from ISR! print("[W]");
  451. for (int i = 0; i < NUM_USB_DRIVERS; i++) {
  452. chSysLockFromISR();
  453. /* Disconnection event on suspend.*/
  454. qmkusbWakeupHookI(&drivers.array[i].driver);
  455. chSysUnlockFromISR();
  456. }
  457. usb_event_queue_enqueue(USB_EVENT_WAKEUP);
  458. return;
  459. case USB_EVENT_STALLED:
  460. return;
  461. }
  462. }
  463. /* Function used locally in os/hal/src/usb.c for getting descriptors
  464. * need it here for HID descriptor */
  465. static uint16_t get_hword(uint8_t *p) {
  466. uint16_t hw;
  467. hw = (uint16_t)*p++;
  468. hw |= (uint16_t)*p << 8U;
  469. return hw;
  470. }
  471. /*
  472. * Appendix G: HID Request Support Requirements
  473. *
  474. * The following table enumerates the requests that need to be supported by various types of HID class devices.
  475. * Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  476. * ------------------------------------------------------------------------------------------
  477. * Boot Mouse Required Optional Optional Optional Required Required
  478. * Non-Boot Mouse Required Optional Optional Optional Optional Optional
  479. * Boot Keyboard Required Optional Required Required Required Required
  480. * Non-Boot Keybrd Required Optional Required Required Optional Optional
  481. * Other Device Required Optional Optional Optional Optional Optional
  482. */
  483. static uint8_t set_report_buf[2] __attribute__((aligned(4)));
  484. static void set_led_transfer_cb(USBDriver *usbp) {
  485. if (usbp->setup[6] == 2) { /* LSB(wLength) */
  486. uint8_t report_id = set_report_buf[0];
  487. if ((report_id == REPORT_ID_KEYBOARD) || (report_id == REPORT_ID_NKRO)) {
  488. keyboard_led_state = set_report_buf[1];
  489. }
  490. } else {
  491. keyboard_led_state = set_report_buf[0];
  492. }
  493. }
  494. /* Callback for SETUP request on the endpoint 0 (control) */
  495. static bool usb_request_hook_cb(USBDriver *usbp) {
  496. const USBDescriptor *dp;
  497. /* usbp->setup fields:
  498. * 0: bmRequestType (bitmask)
  499. * 1: bRequest
  500. * 2,3: (LSB,MSB) wValue
  501. * 4,5: (LSB,MSB) wIndex
  502. * 6,7: (LSB,MSB) wLength (number of bytes to transfer if there is a data phase) */
  503. /* Handle HID class specific requests */
  504. if (((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) && ((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE)) {
  505. switch (usbp->setup[0] & USB_RTYPE_DIR_MASK) {
  506. case USB_RTYPE_DIR_DEV2HOST:
  507. switch (usbp->setup[1]) { /* bRequest */
  508. case HID_GET_REPORT:
  509. switch (usbp->setup[4]) { /* LSB(wIndex) (check MSB==0?) */
  510. case KEYBOARD_INTERFACE:
  511. usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent), NULL);
  512. return TRUE;
  513. break;
  514. #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
  515. case MOUSE_INTERFACE:
  516. usbSetupTransfer(usbp, (uint8_t *)&mouse_report_blank, sizeof(mouse_report_blank), NULL);
  517. return TRUE;
  518. break;
  519. #endif
  520. default:
  521. usbSetupTransfer(usbp, NULL, 0, NULL);
  522. return TRUE;
  523. break;
  524. }
  525. break;
  526. case HID_GET_PROTOCOL:
  527. if ((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */
  528. usbSetupTransfer(usbp, &keyboard_protocol, 1, NULL);
  529. return TRUE;
  530. }
  531. break;
  532. case HID_GET_IDLE:
  533. usbSetupTransfer(usbp, &keyboard_idle, 1, NULL);
  534. return TRUE;
  535. break;
  536. }
  537. break;
  538. case USB_RTYPE_DIR_HOST2DEV:
  539. switch (usbp->setup[1]) { /* bRequest */
  540. case HID_SET_REPORT:
  541. switch (usbp->setup[4]) { /* LSB(wIndex) (check MSB==0?) */
  542. case KEYBOARD_INTERFACE:
  543. #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
  544. case SHARED_INTERFACE:
  545. #endif
  546. usbSetupTransfer(usbp, set_report_buf, sizeof(set_report_buf), set_led_transfer_cb);
  547. return TRUE;
  548. break;
  549. }
  550. break;
  551. case HID_SET_PROTOCOL:
  552. if ((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */
  553. keyboard_protocol = ((usbp->setup[2]) != 0x00); /* LSB(wValue) */
  554. #ifdef NKRO_ENABLE
  555. keymap_config.nkro = !!keyboard_protocol;
  556. if (!keymap_config.nkro && keyboard_idle) {
  557. #else /* NKRO_ENABLE */
  558. if (keyboard_idle) {
  559. #endif /* NKRO_ENABLE */
  560. /* arm the idle timer if boot protocol & idle */
  561. osalSysLockFromISR();
  562. chVTSetI(&keyboard_idle_timer, 4 * TIME_MS2I(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
  563. osalSysUnlockFromISR();
  564. }
  565. }
  566. usbSetupTransfer(usbp, NULL, 0, NULL);
  567. return TRUE;
  568. break;
  569. case HID_SET_IDLE:
  570. keyboard_idle = usbp->setup[3]; /* MSB(wValue) */
  571. /* arm the timer */
  572. #ifdef NKRO_ENABLE
  573. if (!keymap_config.nkro && keyboard_idle) {
  574. #else /* NKRO_ENABLE */
  575. if (keyboard_idle) {
  576. #endif /* NKRO_ENABLE */
  577. osalSysLockFromISR();
  578. chVTSetI(&keyboard_idle_timer, 4 * TIME_MS2I(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
  579. osalSysUnlockFromISR();
  580. }
  581. usbSetupTransfer(usbp, NULL, 0, NULL);
  582. return TRUE;
  583. break;
  584. }
  585. break;
  586. }
  587. }
  588. /* Handle the Get_Descriptor Request for HID class (not handled by the default hook) */
  589. if ((usbp->setup[0] == 0x81) && (usbp->setup[1] == USB_REQ_GET_DESCRIPTOR)) {
  590. dp = usbp->config->get_descriptor_cb(usbp, usbp->setup[3], usbp->setup[2], get_hword(&usbp->setup[4]));
  591. if (dp == NULL) return FALSE;
  592. usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL);
  593. return TRUE;
  594. }
  595. for (int i = 0; i < NUM_USB_DRIVERS; i++) {
  596. if (drivers.array[i].config.int_in) {
  597. // NOTE: Assumes that we only have one serial driver
  598. return qmkusbRequestsHook(usbp);
  599. }
  600. }
  601. return FALSE;
  602. }
  603. /* Start-of-frame callback */
  604. static void usb_sof_cb(USBDriver *usbp) {
  605. kbd_sof_cb(usbp);
  606. osalSysLockFromISR();
  607. for (int i = 0; i < NUM_USB_DRIVERS; i++) {
  608. qmkusbSOFHookI(&drivers.array[i].driver);
  609. }
  610. osalSysUnlockFromISR();
  611. }
  612. /* USB driver configuration */
  613. static const USBConfig usbcfg = {
  614. usb_event_cb, /* USB events callback */
  615. usb_get_descriptor_cb, /* Device GET_DESCRIPTOR request callback */
  616. usb_request_hook_cb, /* Requests hook callback */
  617. usb_sof_cb /* Start Of Frame callback */
  618. };
  619. /*
  620. * Initialize the USB driver
  621. */
  622. void init_usb_driver(USBDriver *usbp) {
  623. for (int i = 0; i < NUM_USB_DRIVERS; i++) {
  624. #if STM32_USB_USE_OTG1
  625. QMKUSBDriver *driver = &drivers.array[i].driver;
  626. drivers.array[i].inout_ep_config.in_state = &drivers.array[i].in_ep_state;
  627. drivers.array[i].inout_ep_config.out_state = &drivers.array[i].out_ep_state;
  628. drivers.array[i].int_ep_config.in_state = &drivers.array[i].int_ep_state;
  629. qmkusbObjectInit(driver, &drivers.array[i].config);
  630. qmkusbStart(driver, &drivers.array[i].config);
  631. #else
  632. QMKUSBDriver *driver = &drivers.array[i].driver;
  633. drivers.array[i].in_ep_config.in_state = &drivers.array[i].in_ep_state;
  634. drivers.array[i].out_ep_config.out_state = &drivers.array[i].out_ep_state;
  635. drivers.array[i].int_ep_config.in_state = &drivers.array[i].int_ep_state;
  636. qmkusbObjectInit(driver, &drivers.array[i].config);
  637. qmkusbStart(driver, &drivers.array[i].config);
  638. #endif
  639. }
  640. /*
  641. * Activates the USB driver and then the USB bus pull-up on D+.
  642. * Note, a delay is inserted in order to not have to disconnect the cable
  643. * after a reset.
  644. */
  645. usbDisconnectBus(usbp);
  646. wait_ms(1500);
  647. usbStart(usbp, &usbcfg);
  648. usbConnectBus(usbp);
  649. chVTObjectInit(&keyboard_idle_timer);
  650. }
  651. __attribute__((weak)) void restart_usb_driver(USBDriver *usbp) {
  652. usbStop(usbp);
  653. usbDisconnectBus(usbp);
  654. #if USB_SUSPEND_WAKEUP_DELAY > 0
  655. // Some hubs, kvm switches, and monitors do
  656. // weird things, with USB device state bouncing
  657. // around wildly on wakeup, yielding race
  658. // conditions that can corrupt the keyboard state.
  659. //
  660. // Pause for a while to let things settle...
  661. wait_ms(USB_SUSPEND_WAKEUP_DELAY);
  662. #endif
  663. usbStart(usbp, &usbcfg);
  664. usbConnectBus(usbp);
  665. }
  666. /* ---------------------------------------------------------
  667. * Keyboard functions
  668. * ---------------------------------------------------------
  669. */
  670. /* keyboard IN callback hander (a kbd report has made it IN) */
  671. #ifndef KEYBOARD_SHARED_EP
  672. void kbd_in_cb(USBDriver *usbp, usbep_t ep) {
  673. /* STUB */
  674. (void)usbp;
  675. (void)ep;
  676. }
  677. #endif
  678. /* start-of-frame handler
  679. * TODO: i guess it would be better to re-implement using timers,
  680. * so that this is not going to have to be checked every 1ms */
  681. void kbd_sof_cb(USBDriver *usbp) { (void)usbp; }
  682. /* Idle requests timer code
  683. * callback (called from ISR, unlocked state) */
  684. static void keyboard_idle_timer_cb(void *arg) {
  685. USBDriver *usbp = (USBDriver *)arg;
  686. osalSysLockFromISR();
  687. /* check that the states of things are as they're supposed to */
  688. if (usbGetDriverStateI(usbp) != USB_ACTIVE) {
  689. /* do not rearm the timer, should be enabled on IDLE request */
  690. osalSysUnlockFromISR();
  691. return;
  692. }
  693. #ifdef NKRO_ENABLE
  694. if (!keymap_config.nkro && keyboard_idle && keyboard_protocol) {
  695. #else /* NKRO_ENABLE */
  696. if (keyboard_idle && keyboard_protocol) {
  697. #endif /* NKRO_ENABLE */
  698. /* TODO: are we sure we want the KBD_ENDPOINT? */
  699. if (!usbGetTransmitStatusI(usbp, KEYBOARD_IN_EPNUM)) {
  700. usbStartTransmitI(usbp, KEYBOARD_IN_EPNUM, (uint8_t *)&keyboard_report_sent, KEYBOARD_EPSIZE);
  701. }
  702. /* rearm the timer */
  703. chVTSetI(&keyboard_idle_timer, 4 * TIME_MS2I(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
  704. }
  705. /* do not rearm the timer if the condition above fails
  706. * it should be enabled again on either IDLE or SET_PROTOCOL requests */
  707. osalSysUnlockFromISR();
  708. }
  709. /* LED status */
  710. uint8_t keyboard_leds(void) { return keyboard_led_state; }
  711. /* prepare and start sending a report IN
  712. * not callable from ISR or locked state */
  713. void send_keyboard(report_keyboard_t *report) {
  714. osalSysLock();
  715. if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  716. goto unlock;
  717. }
  718. #ifdef NKRO_ENABLE
  719. if (keymap_config.nkro && keyboard_protocol) { /* NKRO protocol */
  720. /* need to wait until the previous packet has made it through */
  721. /* can rewrite this using the synchronous API, then would wait
  722. * until *after* the packet has been transmitted. I think
  723. * this is more efficient */
  724. /* busy wait, should be short and not very common */
  725. if (usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) {
  726. /* Need to either suspend, or loop and call unlock/lock during
  727. * every iteration - otherwise the system will remain locked,
  728. * no interrupts served, so USB not going through as well.
  729. * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */
  730. osalThreadSuspendS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread);
  731. /* after osalThreadSuspendS returns USB status might have changed */
  732. if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  733. goto unlock;
  734. }
  735. }
  736. usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(struct nkro_report));
  737. } else
  738. #endif /* NKRO_ENABLE */
  739. { /* regular protocol */
  740. /* need to wait until the previous packet has made it through */
  741. /* busy wait, should be short and not very common */
  742. if (usbGetTransmitStatusI(&USB_DRIVER, KEYBOARD_IN_EPNUM)) {
  743. /* Need to either suspend, or loop and call unlock/lock during
  744. * every iteration - otherwise the system will remain locked,
  745. * no interrupts served, so USB not going through as well.
  746. * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */
  747. osalThreadSuspendS(&(&USB_DRIVER)->epc[KEYBOARD_IN_EPNUM]->in_state->thread);
  748. /* after osalThreadSuspendS returns USB status might have changed */
  749. if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  750. goto unlock;
  751. }
  752. }
  753. uint8_t *data, size;
  754. if (keyboard_protocol) {
  755. data = (uint8_t *)report;
  756. size = KEYBOARD_REPORT_SIZE;
  757. } else { /* boot protocol */
  758. data = &report->mods;
  759. size = 8;
  760. }
  761. usbStartTransmitI(&USB_DRIVER, KEYBOARD_IN_EPNUM, data, size);
  762. }
  763. keyboard_report_sent = *report;
  764. unlock:
  765. osalSysUnlock();
  766. }
  767. /* ---------------------------------------------------------
  768. * Mouse functions
  769. * ---------------------------------------------------------
  770. */
  771. #ifdef MOUSE_ENABLE
  772. # ifndef MOUSE_SHARED_EP
  773. /* mouse IN callback hander (a mouse report has made it IN) */
  774. void mouse_in_cb(USBDriver *usbp, usbep_t ep) {
  775. (void)usbp;
  776. (void)ep;
  777. }
  778. # endif
  779. void send_mouse(report_mouse_t *report) {
  780. osalSysLock();
  781. if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  782. osalSysUnlock();
  783. return;
  784. }
  785. if (usbGetTransmitStatusI(&USB_DRIVER, MOUSE_IN_EPNUM)) {
  786. /* Need to either suspend, or loop and call unlock/lock during
  787. * every iteration - otherwise the system will remain locked,
  788. * no interrupts served, so USB not going through as well.
  789. * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */
  790. if (osalThreadSuspendTimeoutS(&(&USB_DRIVER)->epc[MOUSE_IN_EPNUM]->in_state->thread, TIME_MS2I(10)) == MSG_TIMEOUT) {
  791. osalSysUnlock();
  792. return;
  793. }
  794. }
  795. usbStartTransmitI(&USB_DRIVER, MOUSE_IN_EPNUM, (uint8_t *)report, sizeof(report_mouse_t));
  796. osalSysUnlock();
  797. }
  798. #else /* MOUSE_ENABLE */
  799. void send_mouse(report_mouse_t *report) { (void)report; }
  800. #endif /* MOUSE_ENABLE */
  801. /* ---------------------------------------------------------
  802. * Shared EP functions
  803. * ---------------------------------------------------------
  804. */
  805. #ifdef SHARED_EP_ENABLE
  806. /* shared IN callback hander */
  807. void shared_in_cb(USBDriver *usbp, usbep_t ep) {
  808. /* STUB */
  809. (void)usbp;
  810. (void)ep;
  811. }
  812. #endif
  813. /* ---------------------------------------------------------
  814. * Extrakey functions
  815. * ---------------------------------------------------------
  816. */
  817. #ifdef EXTRAKEY_ENABLE
  818. static void send_extra(uint8_t report_id, uint16_t data) {
  819. osalSysLock();
  820. if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  821. osalSysUnlock();
  822. return;
  823. }
  824. static report_extra_t report;
  825. report = (report_extra_t){.report_id = report_id, .usage = data};
  826. usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)&report, sizeof(report_extra_t));
  827. osalSysUnlock();
  828. }
  829. #endif
  830. void send_system(uint16_t data) {
  831. #ifdef EXTRAKEY_ENABLE
  832. send_extra(REPORT_ID_SYSTEM, data);
  833. #endif
  834. }
  835. void send_consumer(uint16_t data) {
  836. #ifdef EXTRAKEY_ENABLE
  837. send_extra(REPORT_ID_CONSUMER, data);
  838. #endif
  839. }
  840. /* ---------------------------------------------------------
  841. * Console functions
  842. * ---------------------------------------------------------
  843. */
  844. #ifdef CONSOLE_ENABLE
  845. int8_t sendchar(uint8_t c) {
  846. static bool timed_out = false;
  847. /* The `timed_out` state is an approximation of the ideal `is_listener_disconnected?` state.
  848. *
  849. * When a 5ms timeout write has timed out, hid_listen is most likely not running, or not
  850. * listening to this keyboard, so we go into the timed_out state. In this state we assume
  851. * that hid_listen is most likely not gonna be connected to us any time soon, so it would
  852. * be wasteful to write follow-up characters with a 5ms timeout, it would all add up and
  853. * unncecessarily slow down the firmware. However instead of just dropping the characters,
  854. * we write them with a TIME_IMMEDIATE timeout, which is a zero timeout,
  855. * and this will succeed only if hid_listen gets connected again. When a write with
  856. * TIME_IMMEDIATE timeout succeeds, we know that hid_listen is listening to us again, and
  857. * we can go back to the timed_out = false state, and following writes will be executed
  858. * with a 5ms timeout. The reason we don't just send all characters with the TIME_IMMEDIATE
  859. * timeout is that this could cause bytes to be lost even if hid_listen is running, if there
  860. * is a lot of data being sent over the console.
  861. *
  862. * This logic will work correctly as long as hid_listen is able to receive at least 200
  863. * bytes per second. On a heavily overloaded machine that's so overloaded that it's
  864. * unusable, and constantly swapping, hid_listen might have trouble receiving 200 bytes per
  865. * second, so some bytes might be lost on the console.
  866. */
  867. const sysinterval_t timeout = timed_out ? TIME_IMMEDIATE : TIME_MS2I(5);
  868. const size_t result = chnWriteTimeout(&drivers.console_driver.driver, &c, 1, timeout);
  869. timed_out = (result == 0);
  870. return result;
  871. }
  872. // Just a dummy function for now, this could be exposed as a weak function
  873. // Or connected to the actual QMK console
  874. static void console_receive(uint8_t *data, uint8_t length) {
  875. (void)data;
  876. (void)length;
  877. }
  878. void console_task(void) {
  879. uint8_t buffer[CONSOLE_EPSIZE];
  880. size_t size = 0;
  881. do {
  882. size_t size = chnReadTimeout(&drivers.console_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE);
  883. if (size > 0) {
  884. console_receive(buffer, size);
  885. }
  886. } while (size > 0);
  887. }
  888. #endif /* CONSOLE_ENABLE */
  889. #ifdef RAW_ENABLE
  890. void raw_hid_send(uint8_t *data, uint8_t length) {
  891. // TODO: implement variable size packet
  892. if (length != RAW_EPSIZE) {
  893. return;
  894. }
  895. chnWrite(&drivers.raw_driver.driver, data, length);
  896. }
  897. __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) {
  898. // Users should #include "raw_hid.h" in their own code
  899. // and implement this function there. Leave this as weak linkage
  900. // so users can opt to not handle data coming in.
  901. }
  902. void raw_hid_task(void) {
  903. uint8_t buffer[RAW_EPSIZE];
  904. size_t size = 0;
  905. do {
  906. size_t size = chnReadTimeout(&drivers.raw_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE);
  907. if (size > 0) {
  908. raw_hid_receive(buffer, size);
  909. }
  910. } while (size > 0);
  911. }
  912. #endif
  913. #ifdef MIDI_ENABLE
  914. void send_midi_packet(MIDI_EventPacket_t *event) { chnWrite(&drivers.midi_driver.driver, (uint8_t *)event, sizeof(MIDI_EventPacket_t)); }
  915. bool recv_midi_packet(MIDI_EventPacket_t *const event) {
  916. size_t size = chnReadTimeout(&drivers.midi_driver.driver, (uint8_t *)event, sizeof(MIDI_EventPacket_t), TIME_IMMEDIATE);
  917. return size == sizeof(MIDI_EventPacket_t);
  918. }
  919. void midi_ep_task(void) {
  920. uint8_t buffer[MIDI_STREAM_EPSIZE];
  921. size_t size = 0;
  922. do {
  923. size_t size = chnReadTimeout(&drivers.midi_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE);
  924. if (size > 0) {
  925. MIDI_EventPacket_t event;
  926. recv_midi_packet(&event);
  927. }
  928. } while (size > 0);
  929. }
  930. #endif
  931. #ifdef VIRTSER_ENABLE
  932. void virtser_send(const uint8_t byte) { chnWrite(&drivers.serial_driver.driver, &byte, 1); }
  933. __attribute__((weak)) void virtser_recv(uint8_t c) {
  934. // Ignore by default
  935. }
  936. void virtser_task(void) {
  937. uint8_t numBytesReceived = 0;
  938. uint8_t buffer[16];
  939. do {
  940. numBytesReceived = chnReadTimeout(&drivers.serial_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE);
  941. for (int i = 0; i < numBytesReceived; i++) {
  942. virtser_recv(buffer[i]);
  943. }
  944. } while (numBytesReceived > 0);
  945. }
  946. #endif
  947. #ifdef JOYSTICK_ENABLE
  948. void send_joystick_packet(joystick_t *joystick) {
  949. static joystick_report_t rep;
  950. rep = (joystick_report_t) {
  951. # if JOYSTICK_AXES_COUNT > 0
  952. .axes =
  953. { joystick->axes[0],
  954. # if JOYSTICK_AXES_COUNT >= 2
  955. joystick->axes[1],
  956. # endif
  957. # if JOYSTICK_AXES_COUNT >= 3
  958. joystick->axes[2],
  959. # endif
  960. # if JOYSTICK_AXES_COUNT >= 4
  961. joystick->axes[3],
  962. # endif
  963. # if JOYSTICK_AXES_COUNT >= 5
  964. joystick->axes[4],
  965. # endif
  966. # if JOYSTICK_AXES_COUNT >= 6
  967. joystick->axes[5],
  968. # endif
  969. },
  970. # endif // JOYSTICK_AXES_COUNT>0
  971. # if JOYSTICK_BUTTON_COUNT > 0
  972. .buttons = {
  973. joystick->buttons[0],
  974. # if JOYSTICK_BUTTON_COUNT > 8
  975. joystick->buttons[1],
  976. # endif
  977. # if JOYSTICK_BUTTON_COUNT > 16
  978. joystick->buttons[2],
  979. # endif
  980. # if JOYSTICK_BUTTON_COUNT > 24
  981. joystick->buttons[3],
  982. # endif
  983. }
  984. # endif // JOYSTICK_BUTTON_COUNT>0
  985. };
  986. // chnWrite(&drivers.joystick_driver.driver, (uint8_t *)&rep, sizeof(rep));
  987. osalSysLock();
  988. if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  989. osalSysUnlock();
  990. return;
  991. }
  992. usbStartTransmitI(&USB_DRIVER, JOYSTICK_IN_EPNUM, (uint8_t *)&rep, sizeof(joystick_report_t));
  993. osalSysUnlock();
  994. }
  995. #endif