usb_main.c 20 KB

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