Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hid): Eliminate data race in USB pathway causing dropped keys #2257

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion app/src/usb_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: MIT
*/

#include <zephyr/sys/util.h>

#include <zephyr/device.h>
#include <zephyr/init.h>

Expand All @@ -18,11 +20,28 @@
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
#include <zmk/event_manager.h>

#if IS_ENABLED(CONFIG_ZMK_USB_BOOT)
#define HID_BOOT_REPORT_SIZE sizeof(zmk_hid_boot_report_t)
#else
#define HID_BOOT_REPORT_SIZE 0
#endif

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
#define HID_MOUSE_REPORT_SIZE sizeof(struct zmk_hid_mouse_report)
#else
#define HID_MOUSE_REPORT_SIZE 0
#endif

#define HID_EP_WRITE_BUF_SIZE \
MAX(MAX(HID_BOOT_REPORT_SIZE, sizeof(struct zmk_hid_keyboard_report)), \
MAX(sizeof(struct zmk_hid_consumer_report), HID_MOUSE_REPORT_SIZE))
Comment on lines +23 to +37
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative here is define the buffer size to be a certain fixed constant and then add compile time checks that the buffer is large enough to handle any possible response.


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 +156,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