diff options
| author | Ryan <fauxpark@gmail.com> | 2023-11-26 22:50:21 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-26 22:50:21 +1100 |
| commit | cbf538aaaae837971a5b54aaddb9f92b3ac0a8c0 (patch) | |
| tree | 0cf64edc522a0bd989da29369924449dd9bc7a93 | |
| parent | 3a74fa5bf7f8dd26f240cbed14ce76049c0f613f (diff) | |
V-USB: Add generic `send_report()` function (#22323)
| -rw-r--r-- | tmk_core/protocol/vusb/protocol.c | 1 | ||||
| -rw-r--r-- | tmk_core/protocol/vusb/vusb.c | 143 | ||||
| -rw-r--r-- | tmk_core/protocol/vusb/vusb.h | 1 |
3 files changed, 53 insertions, 92 deletions
diff --git a/tmk_core/protocol/vusb/protocol.c b/tmk_core/protocol/vusb/protocol.c index ae99680ce4..1f64561274 100644 --- a/tmk_core/protocol/vusb/protocol.c +++ b/tmk_core/protocol/vusb/protocol.c | |||
| @@ -153,7 +153,6 @@ void protocol_task(void) { | |||
| 153 | if (usbConfiguration && usbInterruptIsReady()) { | 153 | if (usbConfiguration && usbInterruptIsReady()) { |
| 154 | keyboard_task(); | 154 | keyboard_task(); |
| 155 | } | 155 | } |
| 156 | vusb_transfer_keyboard(); | ||
| 157 | 156 | ||
| 158 | #ifdef RAW_ENABLE | 157 | #ifdef RAW_ENABLE |
| 159 | usbPoll(); | 158 | usbPoll(); |
diff --git a/tmk_core/protocol/vusb/vusb.c b/tmk_core/protocol/vusb/vusb.c index 3502d9e644..1750afc562 100644 --- a/tmk_core/protocol/vusb/vusb.c +++ b/tmk_core/protocol/vusb/vusb.c | |||
| @@ -89,40 +89,51 @@ static uint8_t keyboard_led_state = 0; | |||
| 89 | uint8_t keyboard_idle = 0; | 89 | uint8_t keyboard_idle = 0; |
| 90 | uint8_t keyboard_protocol = 1; | 90 | uint8_t keyboard_protocol = 1; |
| 91 | 91 | ||
| 92 | /* Keyboard report send buffer */ | ||
| 93 | #define KBUF_SIZE 16 | ||
| 94 | static report_keyboard_t kbuf[KBUF_SIZE]; | ||
| 95 | static uint8_t kbuf_head = 0; | ||
| 96 | static uint8_t kbuf_tail = 0; | ||
| 97 | |||
| 98 | static report_keyboard_t keyboard_report_sent; | 92 | static report_keyboard_t keyboard_report_sent; |
| 99 | 93 | ||
| 100 | #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10 | 94 | static void send_report_fragment(uint8_t endpoint, void *data, size_t size) { |
| 101 | 95 | for (uint8_t retries = 5; retries > 0; retries--) { | |
| 102 | /* transfer keyboard report from buffer */ | 96 | switch (endpoint) { |
| 103 | void vusb_transfer_keyboard(void) { | 97 | case 1: |
| 104 | for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) { | 98 | if (usbInterruptIsReady()) { |
| 105 | if (usbInterruptIsReady()) { | 99 | usbSetInterrupt(data, size); |
| 106 | if (kbuf_head != kbuf_tail) { | 100 | return; |
| 107 | #ifndef KEYBOARD_SHARED_EP | ||
| 108 | usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t)); | ||
| 109 | #else | ||
| 110 | // Ugly hack! :( | ||
| 111 | usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t) - 1); | ||
| 112 | while (!usbInterruptIsReady()) { | ||
| 113 | usbPoll(); | ||
| 114 | } | 101 | } |
| 115 | usbSetInterrupt((void *)(&(kbuf[kbuf_tail].keys[5])), 1); | 102 | break; |
| 116 | #endif | 103 | case USB_CFG_EP3_NUMBER: |
| 117 | kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE; | 104 | if (usbInterruptIsReady3()) { |
| 118 | if (debug_keyboard) { | 105 | usbSetInterrupt3(data, size); |
| 119 | dprintf("V-USB: kbuf[%d->%d](%02X)\n", kbuf_tail, kbuf_head, (kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail)); | 106 | return; |
| 120 | } | 107 | } |
| 121 | } | 108 | break; |
| 122 | break; | 109 | case USB_CFG_EP4_NUMBER: |
| 110 | if (usbInterruptIsReady4()) { | ||
| 111 | usbSetInterrupt4(data, size); | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | break; | ||
| 115 | default: | ||
| 116 | return; | ||
| 123 | } | 117 | } |
| 118 | |||
| 124 | usbPoll(); | 119 | usbPoll(); |
| 125 | wait_ms(1); | 120 | wait_ms(5); |
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | static void send_report(uint8_t endpoint, void *report, size_t size) { | ||
| 125 | uint8_t *temp = (uint8_t *)report; | ||
| 126 | |||
| 127 | // Send as many full packets as possible | ||
| 128 | for (uint8_t i = 0; i < size / 8; i++) { | ||
| 129 | send_report_fragment(endpoint, temp, 8); | ||
| 130 | temp += 8; | ||
| 131 | } | ||
| 132 | |||
| 133 | // Send any data left over | ||
| 134 | uint8_t remainder = size % 8; | ||
| 135 | if (remainder) { | ||
| 136 | send_report_fragment(endpoint, temp, remainder); | ||
| 126 | } | 137 | } |
| 127 | } | 138 | } |
| 128 | 139 | ||
| @@ -141,18 +152,7 @@ void raw_hid_send(uint8_t *data, uint8_t length) { | |||
| 141 | return; | 152 | return; |
| 142 | } | 153 | } |
| 143 | 154 | ||
| 144 | uint8_t *temp = data; | 155 | send_report(4, data, 32); |
| 145 | for (uint8_t i = 0; i < 4; i++) { | ||
| 146 | while (!usbInterruptIsReady4()) { | ||
| 147 | usbPoll(); | ||
| 148 | } | ||
| 149 | usbSetInterrupt4(temp, 8); | ||
| 150 | temp += 8; | ||
| 151 | } | ||
| 152 | while (!usbInterruptIsReady4()) { | ||
| 153 | usbPoll(); | ||
| 154 | } | ||
| 155 | usbSetInterrupt4(0, 0); | ||
| 156 | } | 156 | } |
| 157 | 157 | ||
| 158 | __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) { | 158 | __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) { |
| @@ -181,19 +181,6 @@ int8_t sendchar(uint8_t c) { | |||
| 181 | return 0; | 181 | return 0; |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | static inline bool usbSendData3(char *data, uint8_t len) { | ||
| 185 | uint8_t retries = 5; | ||
| 186 | while (!usbInterruptIsReady3()) { | ||
| 187 | if (!(retries--)) { | ||
| 188 | return false; | ||
| 189 | } | ||
| 190 | usbPoll(); | ||
| 191 | } | ||
| 192 | |||
| 193 | usbSetInterrupt3((unsigned char *)data, len); | ||
| 194 | return true; | ||
| 195 | } | ||
| 196 | |||
| 197 | void console_task(void) { | 184 | void console_task(void) { |
| 198 | if (!usbConfiguration) { | 185 | if (!usbConfiguration) { |
| 199 | return; | 186 | return; |
| @@ -210,16 +197,7 @@ void console_task(void) { | |||
| 210 | send_buf[send_buf_count++] = rbuf_dequeue(); | 197 | send_buf[send_buf_count++] = rbuf_dequeue(); |
| 211 | } | 198 | } |
| 212 | 199 | ||
| 213 | char *temp = send_buf; | 200 | send_report(3, send_buf, CONSOLE_BUFFER_SIZE); |
| 214 | for (uint8_t i = 0; i < 4; i++) { | ||
| 215 | if (!usbSendData3(temp, 8)) { | ||
| 216 | break; | ||
| 217 | } | ||
| 218 | temp += 8; | ||
| 219 | } | ||
| 220 | |||
| 221 | usbSendData3(0, 0); | ||
| 222 | usbPoll(); | ||
| 223 | } | 201 | } |
| 224 | #endif | 202 | #endif |
| 225 | 203 | ||
| @@ -243,17 +221,12 @@ static uint8_t keyboard_leds(void) { | |||
| 243 | } | 221 | } |
| 244 | 222 | ||
| 245 | static void send_keyboard(report_keyboard_t *report) { | 223 | static void send_keyboard(report_keyboard_t *report) { |
| 246 | uint8_t next = (kbuf_head + 1) % KBUF_SIZE; | 224 | if (!keyboard_protocol) { |
| 247 | if (next != kbuf_tail) { | 225 | send_report(1, &report->mods, 8); |
| 248 | kbuf[kbuf_head] = *report; | ||
| 249 | kbuf_head = next; | ||
| 250 | } else { | 226 | } else { |
| 251 | dprint("kbuf: full\n"); | 227 | send_report(1, report, sizeof(report_keyboard_t)); |
| 252 | } | 228 | } |
| 253 | 229 | ||
| 254 | // NOTE: send key strokes of Macro | ||
| 255 | usbPoll(); | ||
| 256 | vusb_transfer_keyboard(); | ||
| 257 | keyboard_report_sent = *report; | 230 | keyboard_report_sent = *report; |
| 258 | } | 231 | } |
| 259 | 232 | ||
| @@ -262,50 +235,40 @@ static void send_nkro(report_nkro_t *report) { | |||
| 262 | } | 235 | } |
| 263 | 236 | ||
| 264 | #ifndef KEYBOARD_SHARED_EP | 237 | #ifndef KEYBOARD_SHARED_EP |
| 265 | # define usbInterruptIsReadyShared usbInterruptIsReady3 | 238 | # define MOUSE_IN_EPNUM 3 |
| 266 | # define usbSetInterruptShared usbSetInterrupt3 | 239 | # define SHARED_IN_EPNUM 3 |
| 267 | #else | 240 | #else |
| 268 | # define usbInterruptIsReadyShared usbInterruptIsReady | 241 | # define MOUSE_IN_EPNUM 1 |
| 269 | # define usbSetInterruptShared usbSetInterrupt | 242 | # define SHARED_IN_EPNUM 1 |
| 270 | #endif | 243 | #endif |
| 271 | 244 | ||
| 272 | static void send_mouse(report_mouse_t *report) { | 245 | static void send_mouse(report_mouse_t *report) { |
| 273 | #ifdef MOUSE_ENABLE | 246 | #ifdef MOUSE_ENABLE |
| 274 | if (usbInterruptIsReadyShared()) { | 247 | send_report(MOUSE_IN_EPNUM, report, sizeof(report_mouse_t)); |
| 275 | usbSetInterruptShared((void *)report, sizeof(report_mouse_t)); | ||
| 276 | } | ||
| 277 | #endif | 248 | #endif |
| 278 | } | 249 | } |
| 279 | 250 | ||
| 280 | static void send_extra(report_extra_t *report) { | 251 | static void send_extra(report_extra_t *report) { |
| 281 | #ifdef EXTRAKEY_ENABLE | 252 | #ifdef EXTRAKEY_ENABLE |
| 282 | if (usbInterruptIsReadyShared()) { | 253 | send_report(SHARED_IN_EPNUM, report, sizeof(report_extra_t)); |
| 283 | usbSetInterruptShared((void *)report, sizeof(report_extra_t)); | ||
| 284 | } | ||
| 285 | #endif | 254 | #endif |
| 286 | } | 255 | } |
| 287 | 256 | ||
| 288 | void send_joystick(report_joystick_t *report) { | 257 | void send_joystick(report_joystick_t *report) { |
| 289 | #ifdef JOYSTICK_ENABLE | 258 | #ifdef JOYSTICK_ENABLE |
| 290 | if (usbInterruptIsReadyShared()) { | 259 | send_report(SHARED_IN_EPNUM, report, sizeof(report_joystick_t)); |
| 291 | usbSetInterruptShared((void *)report, sizeof(report_joystick_t)); | ||
| 292 | } | ||
| 293 | #endif | 260 | #endif |
| 294 | } | 261 | } |
| 295 | 262 | ||
| 296 | void send_digitizer(report_digitizer_t *report) { | 263 | void send_digitizer(report_digitizer_t *report) { |
| 297 | #ifdef DIGITIZER_ENABLE | 264 | #ifdef DIGITIZER_ENABLE |
| 298 | if (usbInterruptIsReadyShared()) { | 265 | send_report(SHARED_IN_EPNUM, report, sizeof(report_digitizer_t)); |
| 299 | usbSetInterruptShared((void *)report, sizeof(report_digitizer_t)); | ||
| 300 | } | ||
| 301 | #endif | 266 | #endif |
| 302 | } | 267 | } |
| 303 | 268 | ||
| 304 | void send_programmable_button(report_programmable_button_t *report) { | 269 | void send_programmable_button(report_programmable_button_t *report) { |
| 305 | #ifdef PROGRAMMABLE_BUTTON_ENABLE | 270 | #ifdef PROGRAMMABLE_BUTTON_ENABLE |
| 306 | if (usbInterruptIsReadyShared()) { | 271 | send_report(SHARED_IN_EPNUM, report, sizeof(report_programmable_button_t)); |
| 307 | usbSetInterruptShared((void *)report, sizeof(report_programmable_button_t)); | ||
| 308 | } | ||
| 309 | #endif | 272 | #endif |
| 310 | } | 273 | } |
| 311 | 274 | ||
diff --git a/tmk_core/protocol/vusb/vusb.h b/tmk_core/protocol/vusb/vusb.h index c5cb27ded6..ae17e5e014 100644 --- a/tmk_core/protocol/vusb/vusb.h +++ b/tmk_core/protocol/vusb/vusb.h | |||
| @@ -121,4 +121,3 @@ typedef struct usbConfigurationDescriptor { | |||
| 121 | extern bool vusb_suspended; | 121 | extern bool vusb_suspended; |
| 122 | 122 | ||
| 123 | host_driver_t *vusb_driver(void); | 123 | host_driver_t *vusb_driver(void); |
| 124 | void vusb_transfer_keyboard(void); | ||
