usb_main.c 48 KB

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