1
0

usb_main.c 46 KB

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