usb_main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. // Copyright 2023 Stefan Kerkmann
  2. // Copyright 2020-2021 Ryan (@fauxpark)
  3. // Copyright 2020 Nick Brassel (@tzarc)
  4. // Copyright 2020 a-chol
  5. // Copyright 2020 xyzz
  6. // Copyright 2020 Joel Challis (@zvecr)
  7. // Copyright 2020 George (@goshdarnharris)
  8. // Copyright 2018 James Laird-Wah
  9. // Copyright 2018 Drashna Jaelre (@drashna)
  10. // Copyright 2016 Fredizzimo
  11. // Copyright 2016 Giovanni Di Sirio
  12. // SPDX-License-Identifier: GPL-3.0-or-later OR Apache-2.0
  13. #include <ch.h>
  14. #include <hal.h>
  15. #include <string.h>
  16. #include "usb_main.h"
  17. #include "usb_report_handling.h"
  18. #include "host.h"
  19. #include "suspend.h"
  20. #include "timer.h"
  21. #include "wait.h"
  22. #include "usb_endpoints.h"
  23. #include "usb_device_state.h"
  24. #include "usb_descriptor.h"
  25. #include "usb_driver.h"
  26. #include "usb_types.h"
  27. #ifdef RAW_ENABLE
  28. # include "raw_hid.h"
  29. #endif
  30. #ifdef NKRO_ENABLE
  31. # include "keycode_config.h"
  32. extern keymap_config_t keymap_config;
  33. #endif
  34. #ifdef XAP_ENABLE
  35. # include "xap.h"
  36. #endif
  37. /* ---------------------------------------------------------
  38. * Global interface variables and declarations
  39. * ---------------------------------------------------------
  40. */
  41. #ifndef usb_lld_connect_bus
  42. # define usb_lld_connect_bus(usbp)
  43. #endif
  44. #ifndef usb_lld_disconnect_bus
  45. # define usb_lld_disconnect_bus(usbp)
  46. #endif
  47. extern usb_endpoint_in_t usb_endpoints_in[USB_ENDPOINT_IN_COUNT];
  48. extern usb_endpoint_out_t usb_endpoints_out[USB_ENDPOINT_OUT_COUNT];
  49. static bool __attribute__((__unused__)) send_report_buffered(usb_endpoint_in_lut_t endpoint, void *report, size_t size);
  50. static void __attribute__((__unused__)) flush_report_buffered(usb_endpoint_in_lut_t endpoint, bool padded);
  51. static bool __attribute__((__unused__)) receive_report(usb_endpoint_out_lut_t endpoint, void *report, size_t size);
  52. /* ---------------------------------------------------------
  53. * Descriptors and USB driver objects
  54. * ---------------------------------------------------------
  55. */
  56. /* USB Low Level driver specific endpoint fields */
  57. #if !defined(usb_lld_endpoint_fields)
  58. # define usb_lld_endpoint_fields \
  59. 2, /* IN multiplier */ \
  60. NULL, /* SETUP buffer (not a SETUP endpoint) */
  61. #endif
  62. /*
  63. * Handles the GET_DESCRIPTOR callback
  64. *
  65. * Returns the proper descriptor
  66. */
  67. static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t wIndex) {
  68. usb_control_request_t *setup = (usb_control_request_t *)usbp->setup;
  69. static USBDescriptor descriptor;
  70. descriptor.ud_string = NULL;
  71. descriptor.ud_size = get_usb_descriptor(setup->wValue.word, setup->wIndex, setup->wLength, (const void **const)&descriptor.ud_string);
  72. if (descriptor.ud_string == NULL) {
  73. return NULL;
  74. }
  75. return &descriptor;
  76. }
  77. /* ---------------------------------------------------------
  78. * USB driver functions
  79. * ---------------------------------------------------------
  80. */
  81. #define USB_EVENT_QUEUE_SIZE 16
  82. usbevent_t event_queue[USB_EVENT_QUEUE_SIZE];
  83. uint8_t event_queue_head;
  84. uint8_t event_queue_tail;
  85. void usb_event_queue_init(void) {
  86. // Initialise the event queue
  87. memset(&event_queue, 0, sizeof(event_queue));
  88. event_queue_head = 0;
  89. event_queue_tail = 0;
  90. }
  91. static inline bool usb_event_queue_enqueue(usbevent_t event) {
  92. uint8_t next = (event_queue_head + 1) % USB_EVENT_QUEUE_SIZE;
  93. if (next == event_queue_tail) {
  94. return false;
  95. }
  96. event_queue[event_queue_head] = event;
  97. event_queue_head = next;
  98. return true;
  99. }
  100. static inline bool usb_event_queue_dequeue(usbevent_t *event) {
  101. if (event_queue_head == event_queue_tail) {
  102. return false;
  103. }
  104. *event = event_queue[event_queue_tail];
  105. event_queue_tail = (event_queue_tail + 1) % USB_EVENT_QUEUE_SIZE;
  106. return true;
  107. }
  108. static inline void usb_event_suspend_handler(void) {
  109. usb_device_state_set_suspend(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
  110. }
  111. static inline void usb_event_wakeup_handler(void) {
  112. suspend_wakeup_init();
  113. usb_device_state_set_resume(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
  114. }
  115. bool last_suspend_state = false;
  116. void usb_event_queue_task(void) {
  117. usbevent_t event;
  118. while (usb_event_queue_dequeue(&event)) {
  119. switch (event) {
  120. case USB_EVENT_SUSPEND:
  121. last_suspend_state = true;
  122. usb_event_suspend_handler();
  123. break;
  124. case USB_EVENT_WAKEUP:
  125. last_suspend_state = false;
  126. usb_event_wakeup_handler();
  127. break;
  128. case USB_EVENT_CONFIGURED:
  129. usb_device_state_set_configuration(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
  130. break;
  131. case USB_EVENT_UNCONFIGURED:
  132. usb_device_state_set_configuration(false, 0);
  133. break;
  134. case USB_EVENT_RESET:
  135. usb_device_state_set_reset();
  136. usb_device_state_set_protocol(USB_PROTOCOL_REPORT);
  137. break;
  138. default:
  139. // Nothing to do, we don't handle it.
  140. break;
  141. }
  142. }
  143. }
  144. /* Handles the USB driver global events. */
  145. static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
  146. switch (event) {
  147. case USB_EVENT_ADDRESS:
  148. return;
  149. case USB_EVENT_CONFIGURED:
  150. osalSysLockFromISR();
  151. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  152. usb_endpoint_in_configure_cb(&usb_endpoints_in[i]);
  153. }
  154. for (int i = 0; i < USB_ENDPOINT_OUT_COUNT; i++) {
  155. usb_endpoint_out_configure_cb(&usb_endpoints_out[i]);
  156. }
  157. osalSysUnlockFromISR();
  158. if (last_suspend_state) {
  159. usb_event_queue_enqueue(USB_EVENT_WAKEUP);
  160. }
  161. usb_event_queue_enqueue(USB_EVENT_CONFIGURED);
  162. return;
  163. case USB_EVENT_SUSPEND:
  164. /* Falls into.*/
  165. case USB_EVENT_UNCONFIGURED:
  166. /* Falls into.*/
  167. case USB_EVENT_RESET:
  168. usb_event_queue_enqueue(event);
  169. chSysLockFromISR();
  170. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  171. usb_endpoint_in_suspend_cb(&usb_endpoints_in[i]);
  172. }
  173. for (int i = 0; i < USB_ENDPOINT_OUT_COUNT; i++) {
  174. usb_endpoint_out_suspend_cb(&usb_endpoints_out[i]);
  175. }
  176. chSysUnlockFromISR();
  177. return;
  178. case USB_EVENT_WAKEUP:
  179. chSysLockFromISR();
  180. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  181. usb_endpoint_in_wakeup_cb(&usb_endpoints_in[i]);
  182. }
  183. for (int i = 0; i < USB_ENDPOINT_OUT_COUNT; i++) {
  184. usb_endpoint_out_wakeup_cb(&usb_endpoints_out[i]);
  185. }
  186. chSysUnlockFromISR();
  187. usb_event_queue_enqueue(USB_EVENT_WAKEUP);
  188. return;
  189. case USB_EVENT_STALLED:
  190. return;
  191. }
  192. }
  193. /*
  194. * Appendix G: HID Request Support Requirements
  195. *
  196. * The following table enumerates the requests that need to be supported by various types of HID class devices.
  197. * Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  198. * ------------------------------------------------------------------------------------------
  199. * Boot Mouse Required Optional Optional Optional Required Required
  200. * Non-Boot Mouse Required Optional Optional Optional Optional Optional
  201. * Boot Keyboard Required Optional Required Required Required Required
  202. * Non-Boot Keybrd Required Optional Required Required Optional Optional
  203. * Other Device Required Optional Optional Optional Optional Optional
  204. */
  205. static uint8_t _Alignas(4) set_report_buf[2];
  206. static void set_led_transfer_cb(USBDriver *usbp) {
  207. usb_control_request_t *setup = (usb_control_request_t *)usbp->setup;
  208. if (setup->wLength == 2) {
  209. uint8_t report_id = set_report_buf[0];
  210. if ((report_id == REPORT_ID_KEYBOARD) || (report_id == REPORT_ID_NKRO)) {
  211. usb_device_state_set_leds(set_report_buf[1]);
  212. }
  213. } else {
  214. usb_device_state_set_leds(set_report_buf[0]);
  215. }
  216. }
  217. static bool usb_requests_hook_cb(USBDriver *usbp) {
  218. usb_control_request_t *setup = (usb_control_request_t *)usbp->setup;
  219. /* Handle HID class specific requests */
  220. if ((setup->bmRequestType & (USB_RTYPE_TYPE_MASK | USB_RTYPE_RECIPIENT_MASK)) == (USB_RTYPE_TYPE_CLASS | USB_RTYPE_RECIPIENT_INTERFACE)) {
  221. switch (setup->bmRequestType & USB_RTYPE_DIR_MASK) {
  222. case USB_RTYPE_DIR_DEV2HOST:
  223. switch (setup->bRequest) {
  224. case HID_REQ_GetReport:
  225. return usb_get_report_cb(usbp);
  226. case HID_REQ_GetProtocol:
  227. if (setup->wIndex == KEYBOARD_INTERFACE) {
  228. static uint8_t keyboard_protocol;
  229. keyboard_protocol = usb_device_state_get_protocol();
  230. usbSetupTransfer(usbp, &keyboard_protocol, sizeof(keyboard_protocol), NULL);
  231. return true;
  232. }
  233. break;
  234. case HID_REQ_GetIdle:
  235. return usb_get_idle_cb(usbp);
  236. }
  237. case USB_RTYPE_DIR_HOST2DEV:
  238. switch (setup->bRequest) {
  239. case HID_REQ_SetReport:
  240. switch (setup->wIndex) {
  241. case KEYBOARD_INTERFACE:
  242. #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
  243. case SHARED_INTERFACE:
  244. #endif
  245. usbSetupTransfer(usbp, set_report_buf, sizeof(set_report_buf), set_led_transfer_cb);
  246. return true;
  247. }
  248. break;
  249. case HID_REQ_SetProtocol:
  250. if (setup->wIndex == KEYBOARD_INTERFACE) {
  251. usb_device_state_set_protocol(setup->wValue.lbyte);
  252. }
  253. usbSetupTransfer(usbp, NULL, 0, NULL);
  254. return true;
  255. case HID_REQ_SetIdle:
  256. usb_device_state_set_idle_rate(setup->wValue.hbyte);
  257. return usb_set_idle_cb(usbp);
  258. }
  259. break;
  260. }
  261. }
  262. /* Handle the Get_Descriptor Request for HID class, which is not handled by
  263. * the ChibiOS USB driver */
  264. 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)) {
  265. const USBDescriptor *descriptor = usbp->config->get_descriptor_cb(usbp, setup->wValue.lbyte, setup->wValue.hbyte, setup->wIndex);
  266. if (descriptor == NULL) {
  267. return false;
  268. }
  269. usbSetupTransfer(usbp, (uint8_t *)descriptor->ud_string, descriptor->ud_size, NULL);
  270. return true;
  271. }
  272. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  273. if (usb_endpoints_in[i].usb_requests_cb != NULL) {
  274. if (usb_endpoints_in[i].usb_requests_cb(usbp)) {
  275. return true;
  276. }
  277. }
  278. }
  279. return false;
  280. }
  281. static const USBConfig usbcfg = {
  282. usb_event_cb, /* USB events callback */
  283. usb_get_descriptor_cb, /* Device GET_DESCRIPTOR request callback */
  284. usb_requests_hook_cb, /* Requests hook callback */
  285. };
  286. void init_usb_driver(USBDriver *usbp) {
  287. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  288. usb_endpoint_in_init(&usb_endpoints_in[i]);
  289. usb_endpoint_in_start(&usb_endpoints_in[i]);
  290. }
  291. for (int i = 0; i < USB_ENDPOINT_OUT_COUNT; i++) {
  292. usb_endpoint_out_init(&usb_endpoints_out[i]);
  293. usb_endpoint_out_start(&usb_endpoints_out[i]);
  294. }
  295. /*
  296. * Activates the USB driver and then the USB bus pull-up on D+.
  297. * Note, a delay is inserted in order to not have to disconnect the cable
  298. * after a reset.
  299. */
  300. usbDisconnectBus(usbp);
  301. usbStop(usbp);
  302. wait_ms(50);
  303. usbStart(usbp, &usbcfg);
  304. usbConnectBus(usbp);
  305. }
  306. __attribute__((weak)) void restart_usb_driver(USBDriver *usbp) {
  307. usbDisconnectBus(usbp);
  308. usbStop(usbp);
  309. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  310. usb_endpoint_in_stop(&usb_endpoints_in[i]);
  311. }
  312. for (int i = 0; i < USB_ENDPOINT_OUT_COUNT; i++) {
  313. usb_endpoint_out_stop(&usb_endpoints_out[i]);
  314. }
  315. wait_ms(50);
  316. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  317. usb_endpoint_in_init(&usb_endpoints_in[i]);
  318. usb_endpoint_in_start(&usb_endpoints_in[i]);
  319. }
  320. for (int i = 0; i < USB_ENDPOINT_OUT_COUNT; i++) {
  321. usb_endpoint_out_init(&usb_endpoints_out[i]);
  322. usb_endpoint_out_start(&usb_endpoints_out[i]);
  323. }
  324. usbStart(usbp, &usbcfg);
  325. usbConnectBus(usbp);
  326. }
  327. /* ---------------------------------------------------------
  328. * Keyboard functions
  329. * ---------------------------------------------------------
  330. */
  331. /**
  332. * @brief Send a report to the host, the report is enqueued into an output
  333. * queue and send once the USB endpoint becomes empty.
  334. *
  335. * @param endpoint USB IN endpoint to send the report from
  336. * @param report pointer to the report
  337. * @param size size of the report
  338. * @return true Success
  339. * @return false Failure
  340. */
  341. bool send_report(usb_endpoint_in_lut_t endpoint, void *report, size_t size) {
  342. return usb_endpoint_in_send(&usb_endpoints_in[endpoint], (uint8_t *)report, size, TIME_MS2I(100), false);
  343. }
  344. /**
  345. * @brief Send a report to the host, but delay the sending until the size of
  346. * endpoint report is reached or the incompletely filled buffer is flushed with
  347. * a call to `flush_report_buffered`. This is useful if the report is being
  348. * updated frequently. The complete report is then enqueued into an output
  349. * queue and send once the USB endpoint becomes empty.
  350. *
  351. * @param endpoint USB IN endpoint to send the report from
  352. * @param report pointer to the report
  353. * @param size size of the report
  354. * @return true Success
  355. * @return false Failure
  356. */
  357. static bool send_report_buffered(usb_endpoint_in_lut_t endpoint, void *report, size_t size) {
  358. return usb_endpoint_in_send(&usb_endpoints_in[endpoint], (uint8_t *)report, size, TIME_MS2I(100), true);
  359. }
  360. /** @brief Flush all buffered reports which were enqueued with a call to
  361. * `send_report_buffered` that haven't been send. If necessary the buffered
  362. * report can be padded with zeros up to the endpoints maximum size.
  363. *
  364. * @param endpoint USB IN endpoint to flush the reports from
  365. * @param padded Pad the buffered report with zeros up to the endpoints maximum size
  366. */
  367. static void flush_report_buffered(usb_endpoint_in_lut_t endpoint, bool padded) {
  368. usb_endpoint_in_flush(&usb_endpoints_in[endpoint], padded);
  369. }
  370. /**
  371. * @brief Receive a report from the host.
  372. *
  373. * @param endpoint USB OUT endpoint to receive the report from
  374. * @param report pointer to the report
  375. * @param size size of the report
  376. * @return true Success
  377. * @return false Failure
  378. */
  379. static bool receive_report(usb_endpoint_out_lut_t endpoint, void *report, size_t size) {
  380. return usb_endpoint_out_receive(&usb_endpoints_out[endpoint], (uint8_t *)report, size, TIME_IMMEDIATE);
  381. }
  382. void send_keyboard(report_keyboard_t *report) {
  383. /* If we're in Boot Protocol, don't send any report ID or other funky fields */
  384. if (usb_device_state_get_protocol() == USB_PROTOCOL_BOOT) {
  385. send_report(USB_ENDPOINT_IN_KEYBOARD, &report->mods, 8);
  386. } else {
  387. send_report(USB_ENDPOINT_IN_KEYBOARD, report, KEYBOARD_REPORT_SIZE);
  388. }
  389. }
  390. void send_nkro(report_nkro_t *report) {
  391. #ifdef NKRO_ENABLE
  392. send_report(USB_ENDPOINT_IN_SHARED, report, sizeof(report_nkro_t));
  393. #endif
  394. }
  395. /* ---------------------------------------------------------
  396. * Mouse functions
  397. * ---------------------------------------------------------
  398. */
  399. void send_mouse(report_mouse_t *report) {
  400. #ifdef MOUSE_ENABLE
  401. send_report(USB_ENDPOINT_IN_MOUSE, report, sizeof(report_mouse_t));
  402. #endif
  403. }
  404. /* ---------------------------------------------------------
  405. * Extrakey functions
  406. * ---------------------------------------------------------
  407. */
  408. void send_extra(report_extra_t *report) {
  409. #ifdef EXTRAKEY_ENABLE
  410. send_report(USB_ENDPOINT_IN_SHARED, report, sizeof(report_extra_t));
  411. #endif
  412. }
  413. void send_programmable_button(report_programmable_button_t *report) {
  414. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  415. send_report(USB_ENDPOINT_IN_SHARED, report, sizeof(report_programmable_button_t));
  416. #endif
  417. }
  418. void send_joystick(report_joystick_t *report) {
  419. #ifdef JOYSTICK_ENABLE
  420. send_report(USB_ENDPOINT_IN_JOYSTICK, report, sizeof(report_joystick_t));
  421. #endif
  422. }
  423. void send_digitizer(report_digitizer_t *report) {
  424. #ifdef DIGITIZER_ENABLE
  425. send_report(USB_ENDPOINT_IN_DIGITIZER, report, sizeof(report_digitizer_t));
  426. #endif
  427. }
  428. void send_plover_hid(report_plover_hid_t *report) {
  429. #ifdef PLOVER_HID_ENABLE
  430. send_report(USB_ENDPOINT_IN_PLOVER_HID, report, sizeof(report_plover_hid_t));
  431. #endif
  432. }
  433. /* ---------------------------------------------------------
  434. * Console functions
  435. * ---------------------------------------------------------
  436. */
  437. #ifdef CONSOLE_ENABLE
  438. int8_t sendchar(uint8_t c) {
  439. return (int8_t)send_report_buffered(USB_ENDPOINT_IN_CONSOLE, &c, sizeof(uint8_t));
  440. }
  441. void console_task(void) {
  442. flush_report_buffered(USB_ENDPOINT_IN_CONSOLE, true);
  443. }
  444. #endif /* CONSOLE_ENABLE */
  445. #ifdef RAW_ENABLE
  446. void send_raw_hid(uint8_t *data, uint8_t length) {
  447. if (length != RAW_EPSIZE) {
  448. return;
  449. }
  450. send_report(USB_ENDPOINT_IN_RAW, data, length);
  451. }
  452. void raw_hid_task(void) {
  453. uint8_t buffer[RAW_EPSIZE];
  454. while (receive_report(USB_ENDPOINT_OUT_RAW, buffer, sizeof(buffer))) {
  455. raw_hid_receive(buffer, sizeof(buffer));
  456. }
  457. }
  458. #endif
  459. #ifdef XAP_ENABLE
  460. extern void xap_receive(xap_token_t token, const uint8_t *data, size_t length);
  461. void xap_send_base(uint8_t *data, uint8_t length) {
  462. if (length != XAP_EPSIZE) {
  463. return;
  464. }
  465. send_report(USB_ENDPOINT_IN_XAP, data, length);
  466. }
  467. void xap_send(xap_token_t token, xap_response_flags_t response_flags, const void *data, size_t length) {
  468. uint8_t rdata[XAP_EPSIZE] = {0};
  469. xap_response_header_t *header = (xap_response_header_t *)&rdata[0];
  470. header->token = token;
  471. if (length > (XAP_EPSIZE - sizeof(xap_response_header_t))) response_flags &= ~(XAP_RESPONSE_FLAG_SUCCESS);
  472. header->flags = response_flags;
  473. if (response_flags & (XAP_RESPONSE_FLAG_SUCCESS)) {
  474. header->length = (uint8_t)length;
  475. if (data != NULL) {
  476. memcpy(&rdata[sizeof(xap_response_header_t)], data, length);
  477. }
  478. }
  479. xap_send_base(rdata, sizeof(rdata));
  480. }
  481. void xap_broadcast(uint8_t type, const void *data, size_t length) {
  482. uint8_t rdata[XAP_EPSIZE] = {0};
  483. xap_broadcast_header_t *header = (xap_broadcast_header_t *)&rdata[0];
  484. header->token = XAP_BROADCAST_TOKEN;
  485. header->type = type;
  486. if (length > (XAP_EPSIZE - sizeof(xap_broadcast_header_t))) return;
  487. header->length = (uint8_t)length;
  488. if (data != NULL) {
  489. memcpy(&rdata[sizeof(xap_broadcast_header_t)], data, length);
  490. }
  491. xap_send_base(rdata, sizeof(rdata));
  492. }
  493. void xap_receive_base(const void *data) {
  494. const uint8_t *u8data = (const uint8_t *)data;
  495. xap_request_header_t *header = (xap_request_header_t *)&u8data[0];
  496. if (header->length <= (XAP_EPSIZE - sizeof(xap_request_header_t))) {
  497. xap_receive(header->token, &u8data[sizeof(xap_request_header_t)], header->length);
  498. }
  499. }
  500. void xap_task(void) {
  501. uint8_t buffer[XAP_EPSIZE];
  502. while (receive_report(USB_ENDPOINT_OUT_XAP, buffer, sizeof(buffer))) {
  503. xap_receive_base(buffer);
  504. }
  505. }
  506. #endif // XAP_ENABLE
  507. #ifdef MIDI_ENABLE
  508. void send_midi_packet(MIDI_EventPacket_t *event) {
  509. send_report(USB_ENDPOINT_IN_MIDI, (uint8_t *)event, sizeof(MIDI_EventPacket_t));
  510. }
  511. bool recv_midi_packet(MIDI_EventPacket_t *const event) {
  512. return receive_report(USB_ENDPOINT_OUT_MIDI, (uint8_t *)event, sizeof(MIDI_EventPacket_t));
  513. }
  514. #endif
  515. #ifdef VIRTSER_ENABLE
  516. # include "hal_usb_cdc.h"
  517. /**
  518. * @brief CDC serial driver configuration structure. Set to 9600 baud, 1 stop bit, no parity, 8 data bits.
  519. */
  520. static cdc_linecoding_t linecoding = {{0x00, 0x96, 0x00, 0x00}, LC_STOP_1, LC_PARITY_NONE, 8};
  521. bool virtser_usb_request_cb(USBDriver *usbp) {
  522. if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) { /* bmRequestType */
  523. if (usbp->setup[4] == CCI_INTERFACE) { /* wIndex (LSB) */
  524. switch (usbp->setup[1]) { /* bRequest */
  525. case CDC_GET_LINE_CODING:
  526. usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
  527. return true;
  528. case CDC_SET_LINE_CODING:
  529. usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
  530. return true;
  531. case CDC_SET_CONTROL_LINE_STATE:
  532. /* Nothing to do, there are no control lines.*/
  533. usbSetupTransfer(usbp, NULL, 0, NULL);
  534. return true;
  535. default:
  536. return false;
  537. }
  538. }
  539. }
  540. return false;
  541. }
  542. void virtser_init(void) {}
  543. void virtser_send(const uint8_t byte) {
  544. send_report_buffered(USB_ENDPOINT_IN_CDC_DATA, (void *)&byte, sizeof(byte));
  545. }
  546. __attribute__((weak)) void virtser_recv(uint8_t c) {
  547. // Ignore by default
  548. }
  549. void virtser_task(void) {
  550. uint8_t buffer[CDC_EPSIZE];
  551. size_t received;
  552. while ((received = usb_endpoint_out_receive_bytes(&usb_endpoints_out[USB_ENDPOINT_OUT_CDC_DATA], buffer, sizeof(buffer), TIME_IMMEDIATE)) > 0) {
  553. for (size_t i = 0; i < received; i++) {
  554. virtser_recv(buffer[i]);
  555. }
  556. }
  557. flush_report_buffered(USB_ENDPOINT_IN_CDC_DATA, false);
  558. }
  559. #endif