summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2022-10-16 03:53:57 +1100
committerGitHub <noreply@github.com>2022-10-15 12:53:57 -0400
commit19e2dd742b6c8e54c5751b39cf910e0f4fbb2a76 (patch)
treea035234591a7ea6ef1a0db9e2de6fb6ea3668973
parent7c0116a3ec47d39f85bf61076fec05317557bb5f (diff)
LUFA: Consolidate report sending code (#18629)
-rw-r--r--tmk_core/protocol/lufa/lufa.c161
1 files changed, 43 insertions, 118 deletions
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c
index f27256a0e8..af49ed5909 100644
--- a/tmk_core/protocol/lufa/lufa.c
+++ b/tmk_core/protocol/lufa/lufa.c
@@ -87,6 +87,23 @@ static void send_mouse(report_mouse_t *report);
87static void send_extra(report_extra_t *report); 87static void send_extra(report_extra_t *report);
88host_driver_t lufa_driver = {keyboard_leds, send_keyboard, send_mouse, send_extra}; 88host_driver_t lufa_driver = {keyboard_leds, send_keyboard, send_mouse, send_extra};
89 89
90void send_report(uint8_t endpoint, void *report, size_t size) {
91 uint8_t timeout = 255;
92
93 if (USB_DeviceState != DEVICE_STATE_Configured) return;
94
95 Endpoint_SelectEndpoint(endpoint);
96
97 /* Check if write ready for a polling interval around 10ms */
98 while (timeout-- && !Endpoint_IsReadWriteAllowed()) {
99 _delay_us(40);
100 }
101 if (!Endpoint_IsReadWriteAllowed()) return;
102
103 Endpoint_Write_Stream_LE(report, size, NULL);
104 Endpoint_ClearIN();
105}
106
90#ifdef VIRTSER_ENABLE 107#ifdef VIRTSER_ENABLE
91// clang-format off 108// clang-format off
92 109
@@ -121,30 +138,8 @@ USB_ClassInfo_CDC_Device_t cdc_device = {
121 * FIXME: Needs doc 138 * FIXME: Needs doc
122 */ 139 */
123void raw_hid_send(uint8_t *data, uint8_t length) { 140void raw_hid_send(uint8_t *data, uint8_t length) {
124 // TODO: implement variable size packet 141 if (length != RAW_EPSIZE) return;
125 if (length != RAW_EPSIZE) { 142 send_report(RAW_IN_EPNUM, data, RAW_EPSIZE);
126 return;
127 }
128
129 if (USB_DeviceState != DEVICE_STATE_Configured) {
130 return;
131 }
132
133 // TODO: decide if we allow calls to raw_hid_send() in the middle
134 // of other endpoint usage.
135 uint8_t ep = Endpoint_GetCurrentEndpoint();
136
137 Endpoint_SelectEndpoint(RAW_IN_EPNUM);
138
139 // Check to see if the host is ready to accept another packet
140 if (Endpoint_IsINReady()) {
141 // Write data
142 Endpoint_Write_Stream_LE(data, RAW_EPSIZE, NULL);
143 // Finalize the stream transfer to send the last packet
144 Endpoint_ClearIN();
145 }
146
147 Endpoint_SelectEndpoint(ep);
148} 143}
149 144
150/** \brief Raw HID Receive 145/** \brief Raw HID Receive
@@ -250,29 +245,6 @@ static void Console_Task(void) {
250#endif 245#endif
251 246
252/******************************************************************************* 247/*******************************************************************************
253 * Joystick
254 ******************************************************************************/
255void send_joystick(report_joystick_t *report) {
256#ifdef JOYSTICK_ENABLE
257 uint8_t timeout = 255;
258
259 /* Select the Joystick Report Endpoint */
260 Endpoint_SelectEndpoint(JOYSTICK_IN_EPNUM);
261
262 /* Check if write ready for a polling interval around 10ms */
263 while (timeout-- && !Endpoint_IsReadWriteAllowed())
264 _delay_us(40);
265 if (!Endpoint_IsReadWriteAllowed()) return;
266
267 /* Write Joystick Report Data */
268 Endpoint_Write_Stream_LE(report, sizeof(report_joystick_t), NULL);
269
270 /* Finalize the stream transfer to send the last packet */
271 Endpoint_ClearIN();
272#endif
273}
274
275/*******************************************************************************
276 * USB Events 248 * USB Events
277 ******************************************************************************/ 249 ******************************************************************************/
278/* 250/*
@@ -587,32 +559,23 @@ static uint8_t keyboard_leds(void) {
587 * FIXME: Needs doc 559 * FIXME: Needs doc
588 */ 560 */
589static void send_keyboard(report_keyboard_t *report) { 561static void send_keyboard(report_keyboard_t *report) {
590 uint8_t timeout = 255;
591
592 /* Select the Keyboard Report Endpoint */ 562 /* Select the Keyboard Report Endpoint */
593 uint8_t ep = KEYBOARD_IN_EPNUM; 563 uint8_t ep = KEYBOARD_IN_EPNUM;
594 uint8_t size = KEYBOARD_REPORT_SIZE; 564 uint8_t size = KEYBOARD_REPORT_SIZE;
595#ifdef NKRO_ENABLE
596 if (keyboard_protocol && keymap_config.nkro) {
597 ep = SHARED_IN_EPNUM;
598 size = sizeof(struct nkro_report);
599 }
600#endif
601 Endpoint_SelectEndpoint(ep);
602 /* Check if write ready for a polling interval around 10ms */
603 while (timeout-- && !Endpoint_IsReadWriteAllowed())
604 _delay_us(40);
605 if (!Endpoint_IsReadWriteAllowed()) return;
606 565
607 /* If we're in Boot Protocol, don't send any report ID or other funky fields */ 566 /* If we're in Boot Protocol, don't send any report ID or other funky fields */
608 if (!keyboard_protocol) { 567 if (!keyboard_protocol) {
609 Endpoint_Write_Stream_LE(&report->mods, 8, NULL); 568 send_report(ep, &report->mods, 8);
610 } else { 569 } else {
611 Endpoint_Write_Stream_LE(report, size, NULL); 570#ifdef NKRO_ENABLE
612 } 571 if (keymap_config.nkro) {
572 ep = SHARED_IN_EPNUM;
573 size = sizeof(struct nkro_report);
574 }
575#endif
613 576
614 /* Finalize the stream transfer to send the last packet */ 577 send_report(ep, report, size);
615 Endpoint_ClearIN(); 578 }
616 579
617 keyboard_report_sent = *report; 580 keyboard_report_sent = *report;
618} 581}
@@ -623,55 +586,35 @@ static void send_keyboard(report_keyboard_t *report) {
623 */ 586 */
624static void send_mouse(report_mouse_t *report) { 587static void send_mouse(report_mouse_t *report) {
625#ifdef MOUSE_ENABLE 588#ifdef MOUSE_ENABLE
626 uint8_t timeout = 255; 589 send_report(MOUSE_IN_EPNUM, report, sizeof(report_mouse_t));
627
628 /* Select the Mouse Report Endpoint */
629 Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);
630
631 /* Check if write ready for a polling interval around 10ms */
632 while (timeout-- && !Endpoint_IsReadWriteAllowed())
633 _delay_us(40);
634 if (!Endpoint_IsReadWriteAllowed()) return;
635
636 /* Write Mouse Report Data */
637 Endpoint_Write_Stream_LE(report, sizeof(report_mouse_t), NULL);
638
639 /* Finalize the stream transfer to send the last packet */
640 Endpoint_ClearIN();
641#endif 590#endif
642} 591}
643 592
644#if defined(EXTRAKEY_ENABLE) || defined(PROGRAMMABLE_BUTTON_ENABLE)
645static void send_report(void *report, size_t size) {
646 uint8_t timeout = 255;
647
648 if (USB_DeviceState != DEVICE_STATE_Configured) return;
649
650 Endpoint_SelectEndpoint(SHARED_IN_EPNUM);
651
652 /* Check if write ready for a polling interval around 10ms */
653 while (timeout-- && !Endpoint_IsReadWriteAllowed())
654 _delay_us(40);
655 if (!Endpoint_IsReadWriteAllowed()) return;
656
657 Endpoint_Write_Stream_LE(report, size, NULL);
658 Endpoint_ClearIN();
659}
660#endif
661
662/** \brief Send Extra 593/** \brief Send Extra
663 * 594 *
664 * FIXME: Needs doc 595 * FIXME: Needs doc
665 */ 596 */
666static void send_extra(report_extra_t *report) { 597static void send_extra(report_extra_t *report) {
667#ifdef EXTRAKEY_ENABLE 598#ifdef EXTRAKEY_ENABLE
668 send_report(report, sizeof(report_extra_t)); 599 send_report(SHARED_IN_EPNUM, report, sizeof(report_extra_t));
600#endif
601}
602
603void send_joystick(report_joystick_t *report) {
604#ifdef JOYSTICK_ENABLE
605 send_report(JOYSTICK_IN_EPNUM, report, sizeof(report_joystick_t));
669#endif 606#endif
670} 607}
671 608
672void send_programmable_button(report_programmable_button_t *report) { 609void send_programmable_button(report_programmable_button_t *report) {
673#ifdef PROGRAMMABLE_BUTTON_ENABLE 610#ifdef PROGRAMMABLE_BUTTON_ENABLE
674 send_report(report, sizeof(report_programmable_button_t)); 611 send_report(SHARED_IN_EPNUM, report, sizeof(report_programmable_button_t));
612#endif
613}
614
615void send_digitizer(report_digitizer_t *report) {
616#ifdef DIGITIZER_ENABLE
617 send_report(DIGITIZER_IN_EPNUM, report, sizeof(report_digitizer_t));
675#endif 618#endif
676} 619}
677 620
@@ -844,24 +787,6 @@ void virtser_send(const uint8_t byte) {
844} 787}
845#endif 788#endif
846 789
847void send_digitizer(report_digitizer_t *report) {
848#ifdef DIGITIZER_ENABLE
849 uint8_t timeout = 255;
850
851 if (USB_DeviceState != DEVICE_STATE_Configured) return;
852
853 Endpoint_SelectEndpoint(DIGITIZER_IN_EPNUM);
854
855 /* Check if write ready for a polling interval around 10ms */
856 while (timeout-- && !Endpoint_IsReadWriteAllowed())
857 _delay_us(40);
858 if (!Endpoint_IsReadWriteAllowed()) return;
859
860 Endpoint_Write_Stream_LE(report, sizeof(report_digitizer_t), NULL);
861 Endpoint_ClearIN();
862#endif
863}
864
865/******************************************************************************* 790/*******************************************************************************
866 * main 791 * main
867 ******************************************************************************/ 792 ******************************************************************************/