From 0d7edbbcbe46e2b917ba0c30fbe7fe1d09428def Mon Sep 17 00:00:00 2001 From: lokher Date: Thu, 8 Dec 2022 23:56:36 +0800 Subject: Fixed NKRO issue caused by HID_SET_PROTOCOL on Chibios platform (#17588) --- tmk_core/protocol/chibios/usb_main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'tmk_core/protocol/chibios/usb_main.c') diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 62a11faff7..094c14d1b3 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -665,8 +665,7 @@ static bool usb_request_hook_cb(USBDriver *usbp) { if ((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */ keyboard_protocol = ((usbp->setup[2]) != 0x00); /* LSB(wValue) */ #ifdef NKRO_ENABLE - keymap_config.nkro = !!keyboard_protocol; - if (!keymap_config.nkro && keyboard_idle) { + if (!keyboard_protocol && keyboard_idle) { #else /* NKRO_ENABLE */ if (keyboard_idle) { #endif /* NKRO_ENABLE */ -- cgit v1.2.3 From 85ee55ff3becd29e1590376c3462c6a99c976fa0 Mon Sep 17 00:00:00 2001 From: Ruslan Sayfutdinov Date: Thu, 8 Dec 2022 16:45:30 +0000 Subject: Detect host OS based on USB fingerprint (#18463) Co-authored-by: Drashna Jaelre Co-authored-by: Nick Brassel --- tmk_core/protocol/chibios/usb_main.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'tmk_core/protocol/chibios/usb_main.c') diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 094c14d1b3..5d36900bdd 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -111,9 +111,10 @@ uint8_t extra_report_blank[3] = {0}; static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t wIndex) { (void)usbp; static USBDescriptor desc; - uint16_t wValue = ((uint16_t)dtype << 8) | dindex; - desc.ud_string = NULL; - desc.ud_size = get_usb_descriptor(wValue, wIndex, (const void **const) & desc.ud_string); + uint16_t wValue = ((uint16_t)dtype << 8) | dindex; + uint16_t wLength = ((uint16_t)usbp->setup[7] << 8) | usbp->setup[6]; + desc.ud_string = NULL; + desc.ud_size = get_usb_descriptor(wValue, wIndex, wLength, (const void **const) & desc.ud_string); if (desc.ud_string == NULL) return NULL; else @@ -585,7 +586,8 @@ static uint16_t get_hword(uint8_t *p) { */ static uint8_t set_report_buf[2] __attribute__((aligned(4))); -static void set_led_transfer_cb(USBDriver *usbp) { + +static void set_led_transfer_cb(USBDriver *usbp) { if (usbp->setup[6] == 2) { /* LSB(wLength) */ uint8_t report_id = set_report_buf[0]; if ((report_id == REPORT_ID_KEYBOARD) || (report_id == REPORT_ID_NKRO)) { -- cgit v1.2.3 From a23333eb586943c94dc9ab2d69cecc1c2ffc8e8e Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Thu, 8 Dec 2022 12:08:36 -0500 Subject: Return USB HID GET_REPORT requests (#14814) Co-authored-by: Sergey Vlasov Co-authored-by: Nick Brassel --- tmk_core/protocol/chibios/usb_main.c | 55 ++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 12 deletions(-) (limited to 'tmk_core/protocol/chibios/usb_main.c') diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 5d36900bdd..3ffea0a712 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -76,12 +76,24 @@ static void keyboard_idle_timer_cb(void *arg); #endif report_keyboard_t keyboard_report_sent = {{0}}; -#ifdef MOUSE_ENABLE -report_mouse_t mouse_report_blank = {0}; -#endif /* MOUSE_ENABLE */ +report_mouse_t mouse_report_sent = {0}; + +union { + uint8_t report_id; + report_keyboard_t keyboard; #ifdef EXTRAKEY_ENABLE -uint8_t extra_report_blank[3] = {0}; -#endif /* EXTRAKEY_ENABLE */ + report_extra_t extra; +#endif +#ifdef MOUSE_ENABLE + report_mouse_t mouse; +#endif +#ifdef DIGITIZER_ENABLE + report_digitizer_t digitizer; +#endif +#ifdef JOYSTICK_ENABLE + joystick_report_t joystick; +#endif +} universal_report_blank = {0}; /* --------------------------------------------------------- * Descriptors and USB driver objects @@ -114,7 +126,7 @@ static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype uint16_t wValue = ((uint16_t)dtype << 8) | dindex; uint16_t wLength = ((uint16_t)usbp->setup[7] << 8) | usbp->setup[6]; desc.ud_string = NULL; - desc.ud_size = get_usb_descriptor(wValue, wIndex, wLength, (const void **const) & desc.ud_string); + desc.ud_size = get_usb_descriptor(wValue, wIndex, wLength, (const void **const)&desc.ud_string); if (desc.ud_string == NULL) return NULL; else @@ -616,20 +628,38 @@ static bool usb_request_hook_cb(USBDriver *usbp) { switch (usbp->setup[1]) { /* bRequest */ case HID_GET_REPORT: switch (usbp->setup[4]) { /* LSB(wIndex) (check MSB==0?) */ +#ifndef KEYBOARD_SHARED_EP case KEYBOARD_INTERFACE: - usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent), NULL); + usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, KEYBOARD_REPORT_SIZE, NULL); return TRUE; break; - +#endif #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP) case MOUSE_INTERFACE: - usbSetupTransfer(usbp, (uint8_t *)&mouse_report_blank, sizeof(mouse_report_blank), NULL); + usbSetupTransfer(usbp, (uint8_t *)&mouse_report_sent, sizeof(mouse_report_sent), NULL); return TRUE; - break; + break; #endif - +#ifdef SHARED_EP_ENABLE + case SHARED_INTERFACE: +# ifdef KEYBOARD_SHARED_EP + if (usbp->setup[2] == REPORT_ID_KEYBOARD) { + usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, KEYBOARD_REPORT_SIZE, NULL); + return TRUE; + break; + } +# endif +# ifdef MOUSE_SHARED_EP + if (usbp->setup[2] == REPORT_ID_MOUSE) { + usbSetupTransfer(usbp, (uint8_t *)&mouse_report_sent, sizeof(mouse_report_sent), NULL); + return TRUE; + break; + } +# endif +#endif /* SHARED_EP_ENABLE */ default: - usbSetupTransfer(usbp, NULL, 0, NULL); + universal_report_blank.report_id = usbp->setup[2]; + usbSetupTransfer(usbp, (uint8_t *)&universal_report_blank, usbp->setup[6], NULL); return TRUE; break; } @@ -922,6 +952,7 @@ void send_mouse(report_mouse_t *report) { } } usbStartTransmitI(&USB_DRIVER, MOUSE_IN_EPNUM, (uint8_t *)report, sizeof(report_mouse_t)); + mouse_report_sent = *report; osalSysUnlock(); } -- cgit v1.2.3 From 1786932d9fce138fea30f08031153ef98be75ec3 Mon Sep 17 00:00:00 2001 From: QMK Bot Date: Thu, 8 Dec 2022 09:10:52 -0800 Subject: [CI] Format code according to conventions (#19265) --- tmk_core/protocol/chibios/usb_main.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'tmk_core/protocol/chibios/usb_main.c') diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 3ffea0a712..47edd7cb8c 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -76,10 +76,10 @@ static void keyboard_idle_timer_cb(void *arg); #endif report_keyboard_t keyboard_report_sent = {{0}}; -report_mouse_t mouse_report_sent = {0}; +report_mouse_t mouse_report_sent = {0}; union { - uint8_t report_id; + uint8_t report_id; report_keyboard_t keyboard; #ifdef EXTRAKEY_ENABLE report_extra_t extra; @@ -126,7 +126,7 @@ static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype uint16_t wValue = ((uint16_t)dtype << 8) | dindex; uint16_t wLength = ((uint16_t)usbp->setup[7] << 8) | usbp->setup[6]; desc.ud_string = NULL; - desc.ud_size = get_usb_descriptor(wValue, wIndex, wLength, (const void **const)&desc.ud_string); + desc.ud_size = get_usb_descriptor(wValue, wIndex, wLength, (const void **const) & desc.ud_string); if (desc.ud_string == NULL) return NULL; else @@ -638,24 +638,24 @@ static bool usb_request_hook_cb(USBDriver *usbp) { case MOUSE_INTERFACE: usbSetupTransfer(usbp, (uint8_t *)&mouse_report_sent, sizeof(mouse_report_sent), NULL); return TRUE; - break; + break; #endif #ifdef SHARED_EP_ENABLE case SHARED_INTERFACE: -# ifdef KEYBOARD_SHARED_EP +# ifdef KEYBOARD_SHARED_EP if (usbp->setup[2] == REPORT_ID_KEYBOARD) { usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, KEYBOARD_REPORT_SIZE, NULL); return TRUE; break; } -# endif -# ifdef MOUSE_SHARED_EP +# endif +# ifdef MOUSE_SHARED_EP if (usbp->setup[2] == REPORT_ID_MOUSE) { usbSetupTransfer(usbp, (uint8_t *)&mouse_report_sent, sizeof(mouse_report_sent), NULL); return TRUE; - break; + break; } -# endif +# endif #endif /* SHARED_EP_ENABLE */ default: universal_report_blank.report_id = usbp->setup[2]; -- cgit v1.2.3 From a92071494a95175b44a1a3912110c9432f63f3ed Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 15 Jan 2023 19:40:52 +1100 Subject: usb_main.c: remove `CH_KERNEL_MAJOR` check (#19597) --- tmk_core/protocol/chibios/usb_main.c | 8 -------- 1 file changed, 8 deletions(-) (limited to 'tmk_core/protocol/chibios/usb_main.c') diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 47edd7cb8c..34d4af7363 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -69,11 +69,7 @@ uint8_t keyboard_led_state = 0; volatile uint16_t keyboard_idle_count = 0; static virtual_timer_t keyboard_idle_timer; -#if CH_KERNEL_MAJOR >= 7 static void keyboard_idle_timer_cb(struct ch_virtual_timer *, void *arg); -#elif CH_KERNEL_MAJOR <= 6 -static void keyboard_idle_timer_cb(void *arg); -#endif report_keyboard_t keyboard_report_sent = {{0}}; report_mouse_t mouse_report_sent = {0}; @@ -827,12 +823,8 @@ __attribute__((weak)) void restart_usb_driver(USBDriver *usbp) { /* Idle requests timer code * callback (called from ISR, unlocked state) */ -#if CH_KERNEL_MAJOR >= 7 static void keyboard_idle_timer_cb(struct ch_virtual_timer *timer, void *arg) { (void)timer; -#elif CH_KERNEL_MAJOR <= 6 -static void keyboard_idle_timer_cb(void *arg) { -#endif USBDriver *usbp = (USBDriver *)arg; osalSysLockFromISR(); -- cgit v1.2.3 From 955829bfd0ceb835faf6731eb40b1a641f39197a Mon Sep 17 00:00:00 2001 From: Sergey Vlasov Date: Mon, 16 Jan 2023 02:29:29 +0300 Subject: Fix joystick build for ChibiOS (#19602) `joystick_report_t` was renamed to `report_joystick_t`, but apparently one place in the code was missed. --- tmk_core/protocol/chibios/usb_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tmk_core/protocol/chibios/usb_main.c') diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 34d4af7363..cd4b137d83 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -87,7 +87,7 @@ union { report_digitizer_t digitizer; #endif #ifdef JOYSTICK_ENABLE - joystick_report_t joystick; + report_joystick_t joystick; #endif } universal_report_blank = {0}; -- cgit v1.2.3 From 7acc3f444993c77ad65836933c4daecbae57d034 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 17 Jan 2023 12:54:26 +1100 Subject: ChibiOS: Consolidate report sending code (#19607) --- tmk_core/protocol/chibios/usb_main.c | 191 ++++++++--------------------------- 1 file changed, 40 insertions(+), 151 deletions(-) (limited to 'tmk_core/protocol/chibios/usb_main.c') diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index cd4b137d83..b14ca30c1a 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -859,100 +859,61 @@ uint8_t keyboard_leds(void) { return keyboard_led_state; } -/* prepare and start sending a report IN - * not callable from ISR or locked state */ -void send_keyboard(report_keyboard_t *report) { - osalSysLock(); - if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - goto unlock; - } - -#ifdef NKRO_ENABLE - if (keymap_config.nkro && keyboard_protocol) { /* NKRO protocol */ - /* need to wait until the previous packet has made it through */ - /* can rewrite this using the synchronous API, then would wait - * until *after* the packet has been transmitted. I think - * this is more efficient */ - /* busy wait, should be short and not very common */ - if (usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) { - /* Need to either suspend, or loop and call unlock/lock during - * every iteration - otherwise the system will remain locked, - * no interrupts served, so USB not going through as well. - * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - osalThreadSuspendS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread); - - /* after osalThreadSuspendS returns USB status might have changed */ - if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - goto unlock; - } - } - usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(struct nkro_report)); - } else -#endif /* NKRO_ENABLE */ - { /* regular protocol */ - /* need to wait until the previous packet has made it through */ - /* busy wait, should be short and not very common */ - if (usbGetTransmitStatusI(&USB_DRIVER, KEYBOARD_IN_EPNUM)) { - /* Need to either suspend, or loop and call unlock/lock during - * every iteration - otherwise the system will remain locked, - * no interrupts served, so USB not going through as well. - * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - osalThreadSuspendS(&(&USB_DRIVER)->epc[KEYBOARD_IN_EPNUM]->in_state->thread); - - /* after osalThreadSuspendS returns USB status might have changed */ - if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - goto unlock; - } - } - uint8_t *data, size; - if (keyboard_protocol) { - data = (uint8_t *)report; - size = KEYBOARD_REPORT_SIZE; - } else { /* boot protocol */ - data = &report->mods; - size = 8; - } - usbStartTransmitI(&USB_DRIVER, KEYBOARD_IN_EPNUM, data, size); - } - keyboard_report_sent = *report; - -unlock: - osalSysUnlock(); -} - -/* --------------------------------------------------------- - * Mouse functions - * --------------------------------------------------------- - */ - -#ifdef MOUSE_ENABLE -void send_mouse(report_mouse_t *report) { +void send_report(uint8_t endpoint, void *report, size_t size) { osalSysLock(); if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { osalSysUnlock(); return; } - if (usbGetTransmitStatusI(&USB_DRIVER, MOUSE_IN_EPNUM)) { + if (usbGetTransmitStatusI(&USB_DRIVER, endpoint)) { /* Need to either suspend, or loop and call unlock/lock during * every iteration - otherwise the system will remain locked, * no interrupts served, so USB not going through as well. * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - if (osalThreadSuspendTimeoutS(&(&USB_DRIVER)->epc[MOUSE_IN_EPNUM]->in_state->thread, TIME_MS2I(10)) == MSG_TIMEOUT) { + if (osalThreadSuspendTimeoutS(&(&USB_DRIVER)->epc[endpoint]->in_state->thread, TIME_MS2I(10)) == MSG_TIMEOUT) { osalSysUnlock(); return; } } - usbStartTransmitI(&USB_DRIVER, MOUSE_IN_EPNUM, (uint8_t *)report, sizeof(report_mouse_t)); - mouse_report_sent = *report; + usbStartTransmitI(&USB_DRIVER, endpoint, report, size); osalSysUnlock(); } -#else /* MOUSE_ENABLE */ +/* prepare and start sending a report IN + * not callable from ISR or locked state */ +void send_keyboard(report_keyboard_t *report) { + uint8_t ep = KEYBOARD_IN_EPNUM; + size_t size = KEYBOARD_REPORT_SIZE; + + /* If we're in Boot Protocol, don't send any report ID or other funky fields */ + if (!keyboard_protocol) { + send_report(ep, &report->mods, 8); + } else { +#ifdef NKRO_ENABLE + if (keymap_config.nkro) { + ep = SHARED_IN_EPNUM; + size = sizeof(struct nkro_report); + } +#endif + + send_report(ep, report, size); + } + + keyboard_report_sent = *report; +} + +/* --------------------------------------------------------- + * Mouse functions + * --------------------------------------------------------- + */ + void send_mouse(report_mouse_t *report) { - (void)report; +#ifdef MOUSE_ENABLE + send_report(MOUSE_IN_EPNUM, report, sizeof(report_mouse_t)); + mouse_report_sent = *report; +#endif } -#endif /* MOUSE_ENABLE */ /* --------------------------------------------------------- * Extrakey functions @@ -961,97 +922,25 @@ void send_mouse(report_mouse_t *report) { void send_extra(report_extra_t *report) { #ifdef EXTRAKEY_ENABLE - osalSysLock(); - if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - osalSysUnlock(); - return; - } - - if (usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) { - /* Need to either suspend, or loop and call unlock/lock during - * every iteration - otherwise the system will remain locked, - * no interrupts served, so USB not going through as well. - * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - if (osalThreadSuspendTimeoutS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread, TIME_MS2I(10)) == MSG_TIMEOUT) { - osalSysUnlock(); - return; - } - } - - usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(report_extra_t)); - osalSysUnlock(); + send_report(SHARED_IN_EPNUM, report, sizeof(report_extra_t)); #endif } void send_programmable_button(report_programmable_button_t *report) { #ifdef PROGRAMMABLE_BUTTON_ENABLE - osalSysLock(); - if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - osalSysUnlock(); - return; - } - - if (usbGetTransmitStatusI(&USB_DRIVER, SHARED_IN_EPNUM)) { - /* Need to either suspend, or loop and call unlock/lock during - * every iteration - otherwise the system will remain locked, - * no interrupts served, so USB not going through as well. - * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - if (osalThreadSuspendTimeoutS(&(&USB_DRIVER)->epc[SHARED_IN_EPNUM]->in_state->thread, TIME_MS2I(10)) == MSG_TIMEOUT) { - osalSysUnlock(); - return; - } - } - - usbStartTransmitI(&USB_DRIVER, SHARED_IN_EPNUM, (uint8_t *)report, sizeof(report_programmable_button_t)); - osalSysUnlock(); + send_report(SHARED_IN_EPNUM, report, sizeof(report_programmable_button_t)); #endif } void send_joystick(report_joystick_t *report) { #ifdef JOYSTICK_ENABLE - osalSysLock(); - if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - osalSysUnlock(); - return; - } - - if (usbGetTransmitStatusI(&USB_DRIVER, JOYSTICK_IN_EPNUM)) { - /* Need to either suspend, or loop and call unlock/lock during - * every iteration - otherwise the system will remain locked, - * no interrupts served, so USB not going through as well. - * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - if (osalThreadSuspendTimeoutS(&(&USB_DRIVER)->epc[JOYSTICK_IN_EPNUM]->in_state->thread, TIME_MS2I(10)) == MSG_TIMEOUT) { - osalSysUnlock(); - return; - } - } - - usbStartTransmitI(&USB_DRIVER, JOYSTICK_IN_EPNUM, (uint8_t *)report, sizeof(report_joystick_t)); - osalSysUnlock(); + send_report(JOYSTICK_IN_EPNUM, report, sizeof(report_joystick_t)); #endif } void send_digitizer(report_digitizer_t *report) { #ifdef DIGITIZER_ENABLE - osalSysLock(); - if (usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) { - osalSysUnlock(); - return; - } - - if (usbGetTransmitStatusI(&USB_DRIVER, DIGITIZER_IN_EPNUM)) { - /* Need to either suspend, or loop and call unlock/lock during - * every iteration - otherwise the system will remain locked, - * no interrupts served, so USB not going through as well. - * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ - if (osalThreadSuspendTimeoutS(&(&USB_DRIVER)->epc[DIGITIZER_IN_EPNUM]->in_state->thread, TIME_MS2I(10)) == MSG_TIMEOUT) { - osalSysUnlock(); - return; - } - } - - usbStartTransmitI(&USB_DRIVER, DIGITIZER_IN_EPNUM, (uint8_t *)report, sizeof(report_digitizer_t)); - osalSysUnlock(); + send_report(DIGITIZER_IN_EPNUM, report, sizeof(report_digitizer_t)); #endif } -- cgit v1.2.3