usb_main.c 20 KB

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