From c3f91cd3e9ed9b37743cb646fd4a4c5b6b87c050 Mon Sep 17 00:00:00 2001 From: Keeley Hoek Date: Sun, 7 Apr 2024 06:57:06 -0400 Subject: [PATCH] fix(hid): Eliminate data race in USB pathway causing dropped keys Closes #2253 --- app/src/usb_hid.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c index cd3ef9203910..a6997010bef6 100644 --- a/app/src/usb_hid.c +++ b/app/src/usb_hid.c @@ -4,6 +4,8 @@ * SPDX-License-Identifier: MIT */ +#include + #include #include @@ -18,11 +20,16 @@ #endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS) #include +#define HID_EP_WRITE_BUF_SIZE \ + MAX(MAX(sizeof(zmk_hid_boot_report_t), sizeof(struct zmk_hid_keyboard_report)), \ + MAX(sizeof(struct zmk_hid_consumer_report), sizeof(struct zmk_hid_mouse_report))) + LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static const struct device *hid_dev; static K_SEM_DEFINE(hid_sem, 1, 1); +static uint8_t hid_ep_write_buf[HID_EP_WRITE_BUF_SIZE]; static void in_ready_cb(const struct device *dev) { k_sem_give(&hid_sem); } @@ -137,7 +144,8 @@ static int zmk_usb_hid_send_report(const uint8_t *report, size_t len) { return -ENODEV; default: k_sem_take(&hid_sem, K_MSEC(30)); - int err = hid_int_ep_write(hid_dev, report, len, NULL); + memcpy(hid_ep_write_buf, report, len); + int err = hid_int_ep_write(hid_dev, hid_ep_write_buf, len, NULL); if (err) { k_sem_give(&hid_sem);