Skip to content

Commit 27365ac

Browse files
jfischer-noChromeos LUCI
authored andcommitted
samples: hid-mouse: protect report buffer
Use the message queue to pass the new report from the input callback, and use a semaphore to protect the report buffer until it is transferred to the host. (cherry picked from commit 67a31ef) Original-Signed-off-by: Johann Fischer <[email protected]> GitOrigin-RevId: 67a31ef Change-Id: I1cd577c19818038ad57b0a96a3cfd95cfe8f8bfa Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5688009 Tested-by: Yuval Peress <[email protected]> Reviewed-by: Yuval Peress <[email protected]> Tested-by: ChromeOS Prod (Robot) <[email protected]> Commit-Queue: Yuval Peress <[email protected]>
1 parent 9ef2c9d commit 27365ac

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

samples/subsys/usb/hid-mouse/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
1010

1111
CONFIG_GPIO=y
1212
CONFIG_INPUT=y
13+
CONFIG_INPUT_MODE_SYNCHRONOUS=y

samples/subsys/usb/hid-mouse/src/main.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ enum mouse_report_idx {
3737
MOUSE_REPORT_COUNT = 4,
3838
};
3939

40-
static uint8_t __aligned(sizeof(void *)) report[MOUSE_REPORT_COUNT];
41-
static K_SEM_DEFINE(report_sem, 0, 1);
40+
K_MSGQ_DEFINE(mouse_msgq, MOUSE_REPORT_COUNT, 2, 1);
41+
static K_SEM_DEFINE(ep_write_sem, 0, 1);
4242

4343
static inline void status_cb(enum usb_dc_status_code status, const uint8_t *param)
4444
{
@@ -57,9 +57,7 @@ static ALWAYS_INLINE void rwup_if_suspended(void)
5757

5858
static void input_cb(struct input_event *evt)
5959
{
60-
uint8_t tmp[MOUSE_REPORT_COUNT];
61-
62-
(void)memcpy(tmp, report, sizeof(tmp));
60+
static uint8_t tmp[MOUSE_REPORT_COUNT];
6361

6462
switch (evt->code) {
6563
case INPUT_KEY_0:
@@ -88,10 +86,13 @@ static void input_cb(struct input_event *evt)
8886
return;
8987
}
9088

91-
if (memcmp(tmp, report, sizeof(tmp))) {
92-
memcpy(report, tmp, sizeof(report));
93-
k_sem_give(&report_sem);
89+
if (k_msgq_put(&mouse_msgq, tmp, K_NO_WAIT) != 0) {
90+
LOG_ERR("Failed to put new input event");
9491
}
92+
93+
tmp[MOUSE_X_REPORT_IDX] = 0U;
94+
tmp[MOUSE_Y_REPORT_IDX] = 0U;
95+
9596
}
9697

9798
INPUT_CALLBACK_DEFINE(NULL, input_cb);
@@ -120,6 +121,16 @@ static int enable_usb_device_next(void)
120121
}
121122
#endif /* defined(CONFIG_USB_DEVICE_STACK_NEXT) */
122123

124+
static void int_in_ready_cb(const struct device *dev)
125+
{
126+
ARG_UNUSED(dev);
127+
k_sem_give(&ep_write_sem);
128+
}
129+
130+
static const struct hid_ops ops = {
131+
.int_in_ready = int_in_ready_cb,
132+
};
133+
123134
int main(void)
124135
{
125136
const struct device *hid_dev;
@@ -148,7 +159,7 @@ int main(void)
148159

149160
usb_hid_register_device(hid_dev,
150161
hid_report_desc, sizeof(hid_report_desc),
151-
NULL);
162+
&ops);
152163

153164
usb_hid_init(hid_dev);
154165

@@ -163,19 +174,17 @@ int main(void)
163174
}
164175

165176
while (true) {
166-
k_sem_take(&report_sem, K_FOREVER);
177+
uint8_t __aligned(sizeof(void *)) report[MOUSE_REPORT_COUNT];
178+
179+
k_msgq_get(&mouse_msgq, &report, K_FOREVER);
167180

168181
ret = hid_int_ep_write(hid_dev, report, sizeof(report), NULL);
169-
report[MOUSE_X_REPORT_IDX] = 0U;
170-
report[MOUSE_Y_REPORT_IDX] = 0U;
171182
if (ret) {
172183
LOG_ERR("HID write error, %d", ret);
173-
}
174-
175-
/* Toggle LED on sent report */
176-
ret = gpio_pin_toggle(led0.port, led0.pin);
177-
if (ret < 0) {
178-
LOG_ERR("Failed to toggle the LED pin, error: %d", ret);
184+
} else {
185+
k_sem_take(&ep_write_sem, K_FOREVER);
186+
/* Toggle LED on sent report */
187+
(void)gpio_pin_toggle(led0.port, led0.pin);
179188
}
180189
}
181190
return 0;

0 commit comments

Comments
 (0)