Skip to content

Commit

Permalink
fix(hid): Eliminate data race in USB pathway causing dropped keys
Browse files Browse the repository at this point in the history
Closes #2253
  • Loading branch information
khoek committed Apr 7, 2024
1 parent 7d5aa0c commit 8454120
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/src/usb_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
#include <zmk/event_manager.h>

#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); }

Expand Down Expand Up @@ -137,7 +142,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);
Expand Down

0 comments on commit 8454120

Please sign in to comment.