usb_main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. uint8_t _Alignas(2) keyboard_idle = 0;
  48. uint8_t _Alignas(2) keyboard_protocol = 1;
  49. uint8_t keyboard_led_state = 0;
  50. static bool __attribute__((__unused__)) send_report_buffered(usb_endpoint_in_lut_t endpoint, void *report, size_t size);
  51. static void __attribute__((__unused__)) flush_report_buffered(usb_endpoint_in_lut_t endpoint, bool padded);
  52. static bool __attribute__((__unused__)) receive_report(usb_endpoint_out_lut_t endpoint, void *report, size_t size);
  53. /* ---------------------------------------------------------
  54. * Descriptors and USB driver objects
  55. * ---------------------------------------------------------
  56. */
  57. /* USB Low Level driver specific endpoint fields */
  58. #if !defined(usb_lld_endpoint_fields)
  59. # define usb_lld_endpoint_fields \
  60. 2, /* IN multiplier */ \
  61. NULL, /* SETUP buffer (not a SETUP endpoint) */
  62. #endif
  63. /*
  64. * Handles the GET_DESCRIPTOR callback
  65. *
  66. * Returns the proper descriptor
  67. */
  68. static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t wIndex) {
  69. usb_control_request_t *setup = (usb_control_request_t *)usbp->setup;
  70. static USBDescriptor descriptor;
  71. descriptor.ud_string = NULL;
  72. descriptor.ud_size = get_usb_descriptor(setup->wValue.word, setup->wIndex, setup->wLength, (const void **const) & descriptor.ud_string);
  73. if (descriptor.ud_string == NULL) {
  74. return NULL;
  75. }
  76. return &descriptor;
  77. }
  78. /* ---------------------------------------------------------
  79. * USB driver functions
  80. * ---------------------------------------------------------
  81. */
  82. #define USB_EVENT_QUEUE_SIZE 16
  83. usbevent_t event_queue[USB_EVENT_QUEUE_SIZE];
  84. uint8_t event_queue_head;
  85. uint8_t event_queue_tail;
  86. void usb_event_queue_init(void) {
  87. // Initialise the event queue
  88. memset(&event_queue, 0, sizeof(event_queue));
  89. event_queue_head = 0;
  90. event_queue_tail = 0;
  91. }
  92. static inline bool usb_event_queue_enqueue(usbevent_t event) {
  93. uint8_t next = (event_queue_head + 1) % USB_EVENT_QUEUE_SIZE;
  94. if (next == event_queue_tail) {
  95. return false;
  96. }
  97. event_queue[event_queue_head] = event;
  98. event_queue_head = next;
  99. return true;
  100. }
  101. static inline bool usb_event_queue_dequeue(usbevent_t *event) {
  102. if (event_queue_head == event_queue_tail) {
  103. return false;
  104. }
  105. *event = event_queue[event_queue_tail];
  106. event_queue_tail = (event_queue_tail + 1) % USB_EVENT_QUEUE_SIZE;
  107. return true;
  108. }
  109. static inline void usb_event_suspend_handler(void) {
  110. usb_device_state_set_suspend(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
  111. #ifdef SLEEP_LED_ENABLE
  112. sleep_led_enable();
  113. #endif /* SLEEP_LED_ENABLE */
  114. }
  115. static inline void usb_event_wakeup_handler(void) {
  116. suspend_wakeup_init();
  117. usb_device_state_set_resume(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
  118. #ifdef SLEEP_LED_ENABLE
  119. sleep_led_disable();
  120. // NOTE: converters may not accept this
  121. led_set(host_keyboard_leds());
  122. #endif /* SLEEP_LED_ENABLE */
  123. }
  124. bool last_suspend_state = false;
  125. void usb_event_queue_task(void) {
  126. usbevent_t event;
  127. while (usb_event_queue_dequeue(&event)) {
  128. switch (event) {
  129. case USB_EVENT_SUSPEND:
  130. last_suspend_state = true;
  131. usb_event_suspend_handler();
  132. break;
  133. case USB_EVENT_WAKEUP:
  134. last_suspend_state = false;
  135. usb_event_wakeup_handler();
  136. break;
  137. case USB_EVENT_CONFIGURED:
  138. usb_device_state_set_configuration(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
  139. break;
  140. case USB_EVENT_UNCONFIGURED:
  141. usb_device_state_set_configuration(false, 0);
  142. break;
  143. case USB_EVENT_RESET:
  144. usb_device_state_set_reset();
  145. break;
  146. default:
  147. // Nothing to do, we don't handle it.
  148. break;
  149. }
  150. }
  151. }
  152. /* Handles the USB driver global events. */
  153. static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
  154. switch (event) {
  155. case USB_EVENT_ADDRESS:
  156. return;
  157. case USB_EVENT_CONFIGURED:
  158. osalSysLockFromISR();
  159. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  160. usb_endpoint_in_configure_cb(&usb_endpoints_in[i]);
  161. }
  162. for (int i = 0; i < USB_ENDPOINT_OUT_COUNT; i++) {
  163. usb_endpoint_out_configure_cb(&usb_endpoints_out[i]);
  164. }
  165. osalSysUnlockFromISR();
  166. if (last_suspend_state) {
  167. usb_event_queue_enqueue(USB_EVENT_WAKEUP);
  168. }
  169. usb_event_queue_enqueue(USB_EVENT_CONFIGURED);
  170. return;
  171. case USB_EVENT_SUSPEND:
  172. /* Falls into.*/
  173. case USB_EVENT_UNCONFIGURED:
  174. /* Falls into.*/
  175. case USB_EVENT_RESET:
  176. usb_event_queue_enqueue(event);
  177. chSysLockFromISR();
  178. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  179. usb_endpoint_in_suspend_cb(&usb_endpoints_in[i]);
  180. }
  181. for (int i = 0; i < USB_ENDPOINT_OUT_COUNT; i++) {
  182. usb_endpoint_out_suspend_cb(&usb_endpoints_out[i]);
  183. }
  184. chSysUnlockFromISR();
  185. return;
  186. case USB_EVENT_WAKEUP:
  187. chSysLockFromISR();
  188. for (int i = 0; i < USB_ENDPOINT_IN_COUNT; i++) {
  189. usb_endpoint_in_wakeup_cb(&usb_endpoints_in[i]);
  190. }
  191. for (int i = 0; i < USB_ENDPOINT_OUT_COUNT; i++) {
  192. usb_endpoint_out_wakeup_cb(&usb_endpoints_out[i]);
  193. }
  194. chSysUnlockFromISR();
  195. usb_event_queue_enqueue(USB_EVENT_WAKEUP);
  196. return;
  197. case USB_EVENT_STALLED:
  198. return;
  199. }
  200. }
  201. /*
  202. * Appendix G: HID Request Support Requirements
  203. *
  204. * The following table enumerates the requests that need to be supported by various types of HID class devices.
  205. * Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  206. * ------------------------------------------------------------------------------------------
  207. * Boot Mouse Required Optional Optional Optional Required Required
  208. * Non-Boot Mouse Required Optional Optional Optional Optional Optional
  209. * Boot Keyboard Required Optional Required Required Required Required
  210. * Non-Boot Keybrd Required Optional Required Required Optional Optional
  211. * Other Device Required Optional Optional Optional Optional Optional
  212. */
  213. static uint8_t _Alignas(4) set_report_buf[2];
  214. static void set_led_transfer_cb(USBDriver *usbp) {
  215. usb_control_request_t *setup = (usb_control_request_t *)usbp->setup;
  216. if (setup->wLength == 2) {
  217. uint8_t report_id = set_report_buf[0];
  218. if ((report_id == REPORT_ID_KEYBOARD) || (report_id == REPORT_ID_NKRO)) {
  219. keyboard_led_state = set_report_buf[1];
  220. }
  221. } else {
  222. keyboard_led_state = set_report_buf[0];
  223. }
  224. }
  225. static bool usb_requests_hook_cb(USBDriver *usbp) {
  226. usb_control_request_t *setup = (usb_control_request_t *)usbp->setup;
  227. /* Handle HID class specific requests */
  228. if ((setup->bmRequestType & (USB_RTYPE_TYPE_MASK | USB_RTYPE_RECIPIENT_MASK)) == (USB_RTYPE_TYPE_CLASS | USB_RTYPE_RECIPIENT_INTERFACE)) {
  229. switch (setup->bmRequestType & USB_RTYPE_DIR_MASK) {
  230. case USB_RTYPE_DIR_DEV2HOST:
  231. switch (setup->bRequest) {
  232. case HID_REQ_GetReport:
  233. return usb_get_report_cb(usbp);
  234. case HID_REQ_GetProtocol:
  235. if (setup->wIndex == KEYBOARD_INTERFACE) {
  236. usbSetupTransfer(usbp, &keyboard_protocol, sizeof(uint8_t), 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. keyboard_protocol = setup->wValue.word;
  258. }
  259. usbSetupTransfer(usbp, NULL, 0, NULL);
  260. return true;
  261. case HID_REQ_SetIdle:
  262. keyboard_idle = 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. /* LED status */
  345. uint8_t keyboard_leds(void) {
  346. return keyboard_led_state;
  347. }
  348. /**
  349. * @brief Send a report to the host, the report is enqueued into an output
  350. * queue and send once the USB endpoint becomes empty.
  351. *
  352. * @param endpoint USB IN endpoint to send the report from
  353. * @param report pointer to the report
  354. * @param size size of the report
  355. * @return true Success
  356. * @return false Failure
  357. */
  358. bool send_report(usb_endpoint_in_lut_t endpoint, void *report, size_t size) {
  359. return usb_endpoint_in_send(&usb_endpoints_in[endpoint], (uint8_t *)report, size, TIME_MS2I(100), false);
  360. }
  361. /**
  362. * @brief Send a report to the host, but delay the sending until the size of
  363. * endpoint report is reached or the incompletely filled buffer is flushed with
  364. * a call to `flush_report_buffered`. This is useful if the report is being
  365. * updated frequently. The complete report is then enqueued into an output
  366. * queue and send once the USB endpoint becomes empty.
  367. *
  368. * @param endpoint USB IN endpoint to send the report from
  369. * @param report pointer to the report
  370. * @param size size of the report
  371. * @return true Success
  372. * @return false Failure
  373. */
  374. static bool send_report_buffered(usb_endpoint_in_lut_t endpoint, void *report, size_t size) {
  375. return usb_endpoint_in_send(&usb_endpoints_in[endpoint], (uint8_t *)report, size, TIME_MS2I(100), true);
  376. }
  377. /** @brief Flush all buffered reports which were enqueued with a call to
  378. * `send_report_buffered` that haven't been send. If necessary the buffered
  379. * report can be padded with zeros up to the endpoints maximum size.
  380. *
  381. * @param endpoint USB IN endpoint to flush the reports from
  382. * @param padded Pad the buffered report with zeros up to the endpoints maximum size
  383. */
  384. static void flush_report_buffered(usb_endpoint_in_lut_t endpoint, bool padded) {
  385. usb_endpoint_in_flush(&usb_endpoints_in[endpoint], padded);
  386. }
  387. /**
  388. * @brief Receive a report from the host.
  389. *
  390. * @param endpoint USB OUT endpoint to receive the report from
  391. * @param report pointer to the report
  392. * @param size size of the report
  393. * @return true Success
  394. * @return false Failure
  395. */
  396. static bool receive_report(usb_endpoint_out_lut_t endpoint, void *report, size_t size) {
  397. return usb_endpoint_out_receive(&usb_endpoints_out[endpoint], (uint8_t *)report, size, TIME_IMMEDIATE);
  398. }
  399. void send_keyboard(report_keyboard_t *report) {
  400. /* If we're in Boot Protocol, don't send any report ID or other funky fields */
  401. if (!keyboard_protocol) {
  402. send_report(USB_ENDPOINT_IN_KEYBOARD, &report->mods, 8);
  403. } else {
  404. send_report(USB_ENDPOINT_IN_KEYBOARD, report, KEYBOARD_REPORT_SIZE);
  405. }
  406. }
  407. void send_nkro(report_nkro_t *report) {
  408. #ifdef NKRO_ENABLE
  409. send_report(USB_ENDPOINT_IN_SHARED, report, sizeof(report_nkro_t));
  410. #endif
  411. }
  412. /* ---------------------------------------------------------
  413. * Mouse functions
  414. * ---------------------------------------------------------
  415. */
  416. void send_mouse(report_mouse_t *report) {
  417. #ifdef MOUSE_ENABLE
  418. send_report(USB_ENDPOINT_IN_MOUSE, report, sizeof(report_mouse_t));
  419. #endif
  420. }
  421. /* ---------------------------------------------------------
  422. * Extrakey functions
  423. * ---------------------------------------------------------
  424. */
  425. void send_extra(report_extra_t *report) {
  426. #ifdef EXTRAKEY_ENABLE
  427. send_report(USB_ENDPOINT_IN_SHARED, report, sizeof(report_extra_t));
  428. #endif
  429. }
  430. void send_programmable_button(report_programmable_button_t *report) {
  431. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  432. send_report(USB_ENDPOINT_IN_SHARED, report, sizeof(report_programmable_button_t));
  433. #endif
  434. }
  435. void send_joystick(report_joystick_t *report) {
  436. #ifdef JOYSTICK_ENABLE
  437. send_report(USB_ENDPOINT_IN_JOYSTICK, report, sizeof(report_joystick_t));
  438. #endif
  439. }
  440. void send_digitizer(report_digitizer_t *report) {
  441. #ifdef DIGITIZER_ENABLE
  442. send_report(USB_ENDPOINT_IN_DIGITIZER, report, sizeof(report_digitizer_t));
  443. #endif
  444. }
  445. /* ---------------------------------------------------------
  446. * Console functions
  447. * ---------------------------------------------------------
  448. */
  449. #ifdef CONSOLE_ENABLE
  450. int8_t sendchar(uint8_t c) {
  451. return (int8_t)send_report_buffered(USB_ENDPOINT_IN_CONSOLE, &c, sizeof(uint8_t));
  452. }
  453. void console_task(void) {
  454. flush_report_buffered(USB_ENDPOINT_IN_CONSOLE, true);
  455. }
  456. #endif /* CONSOLE_ENABLE */
  457. #ifdef RAW_ENABLE
  458. void raw_hid_send(uint8_t *data, uint8_t length) {
  459. if (length != RAW_EPSIZE) {
  460. return;
  461. }
  462. send_report(USB_ENDPOINT_IN_RAW, data, length);
  463. }
  464. __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) {
  465. // Users should #include "raw_hid.h" in their own code
  466. // and implement this function there. Leave this as weak linkage
  467. // so users can opt to not handle data coming in.
  468. }
  469. void raw_hid_task(void) {
  470. uint8_t buffer[RAW_EPSIZE];
  471. while (receive_report(USB_ENDPOINT_OUT_RAW, buffer, sizeof(buffer))) {
  472. raw_hid_receive(buffer, sizeof(buffer));
  473. }
  474. }
  475. #endif
  476. #ifdef MIDI_ENABLE
  477. void send_midi_packet(MIDI_EventPacket_t *event) {
  478. send_report(USB_ENDPOINT_IN_MIDI, (uint8_t *)event, sizeof(MIDI_EventPacket_t));
  479. }
  480. bool recv_midi_packet(MIDI_EventPacket_t *const event) {
  481. return receive_report(USB_ENDPOINT_OUT_MIDI, (uint8_t *)event, sizeof(MIDI_EventPacket_t));
  482. }
  483. #endif
  484. #ifdef VIRTSER_ENABLE
  485. # include "hal_usb_cdc.h"
  486. /**
  487. * @brief CDC serial driver configuration structure. Set to 9600 baud, 1 stop bit, no parity, 8 data bits.
  488. */
  489. static cdc_linecoding_t linecoding = {{0x00, 0x96, 0x00, 0x00}, LC_STOP_1, LC_PARITY_NONE, 8};
  490. bool virtser_usb_request_cb(USBDriver *usbp) {
  491. if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) { /* bmRequestType */
  492. if (usbp->setup[4] == CCI_INTERFACE) { /* wIndex (LSB) */
  493. switch (usbp->setup[1]) { /* bRequest */
  494. case CDC_GET_LINE_CODING:
  495. usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
  496. return true;
  497. case CDC_SET_LINE_CODING:
  498. usbSetupTransfer(usbp, (uint8_t *)&linecoding, sizeof(linecoding), NULL);
  499. return true;
  500. case CDC_SET_CONTROL_LINE_STATE:
  501. /* Nothing to do, there are no control lines.*/
  502. usbSetupTransfer(usbp, NULL, 0, NULL);
  503. return true;
  504. default:
  505. return false;
  506. }
  507. }
  508. }
  509. return false;
  510. }
  511. void virtser_init(void) {}
  512. void virtser_send(const uint8_t byte) {
  513. send_report_buffered(USB_ENDPOINT_IN_CDC_DATA, (void *)&byte, sizeof(byte));
  514. }
  515. __attribute__((weak)) void virtser_recv(uint8_t c) {
  516. // Ignore by default
  517. }
  518. void virtser_task(void) {
  519. uint8_t buffer[CDC_EPSIZE];
  520. while (receive_report(USB_ENDPOINT_OUT_CDC_DATA, buffer, sizeof(buffer))) {
  521. for (int i = 0; i < sizeof(buffer); i++) {
  522. virtser_recv(buffer[i]);
  523. }
  524. }
  525. flush_report_buffered(USB_ENDPOINT_IN_CDC_DATA, false);
  526. }
  527. #endif