From 25c8a269982ae7414ef0e407900c0f793971e625 Mon Sep 17 00:00:00 2001 From: Kellen Carey Date: Sun, 16 Aug 2020 12:49:08 -0700 Subject: wait before sending HID report --- app/src/usb_hid.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'app/src/usb_hid.c') diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c index 4c6dd4b..07c3d5f 100644 --- a/app/src/usb_hid.c +++ b/app/src/usb_hid.c @@ -1,4 +1,5 @@ +#include #include #include @@ -15,6 +16,18 @@ static enum usb_dc_status_code usb_status; static struct device *hid_dev; +static K_SEM_DEFINE(hid_sem, 1, 1); + +static void in_ready_cb(void) +{ + k_sem_give(&hid_sem); +} + +static const struct hid_ops ops = +{ + .int_in_ready = in_ready_cb, +}; + int zmk_usb_hid_send_report(const u8_t *report, size_t len) { if (usb_status == USB_DC_SUSPEND) @@ -22,6 +35,7 @@ int zmk_usb_hid_send_report(const u8_t *report, size_t len) return usb_wakeup_request(); } + k_sem_take(&hid_sem, K_FOREVER); return hid_int_ep_write(hid_dev, report, len, NULL); } @@ -43,7 +57,7 @@ static int zmk_usb_hid_init(struct device *_arg) usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), - NULL); + &ops); usb_hid_init(hid_dev); -- cgit v1.2.3 From 5b4e5a30c41ea6edfa592735fcf607c44b0b3243 Mon Sep 17 00:00:00 2001 From: Kellen Carey Date: Sun, 16 Aug 2020 12:51:06 -0700 Subject: remove unnecessary include --- app/src/usb_hid.c | 1 - 1 file changed, 1 deletion(-) (limited to 'app/src/usb_hid.c') diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c index 07c3d5f..40511b0 100644 --- a/app/src/usb_hid.c +++ b/app/src/usb_hid.c @@ -1,5 +1,4 @@ -#include #include #include -- cgit v1.2.3 From e5ba03f0881658514c24b1227c514ed57c0c7937 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 17 Aug 2020 23:23:30 -0400 Subject: Revert "Merge pull request #93 from careyk007/main" This reverts commit 8cd8933c87aadd2ce7b31c1367bfaad81fc2a36b, reversing changes made to 3f1dfbaad1a867f59c13814a517e03dfce4d4223. --- app/src/usb_hid.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) (limited to 'app/src/usb_hid.c') diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c index 40511b0..4c6dd4b 100644 --- a/app/src/usb_hid.c +++ b/app/src/usb_hid.c @@ -15,18 +15,6 @@ static enum usb_dc_status_code usb_status; static struct device *hid_dev; -static K_SEM_DEFINE(hid_sem, 1, 1); - -static void in_ready_cb(void) -{ - k_sem_give(&hid_sem); -} - -static const struct hid_ops ops = -{ - .int_in_ready = in_ready_cb, -}; - int zmk_usb_hid_send_report(const u8_t *report, size_t len) { if (usb_status == USB_DC_SUSPEND) @@ -34,7 +22,6 @@ int zmk_usb_hid_send_report(const u8_t *report, size_t len) return usb_wakeup_request(); } - k_sem_take(&hid_sem, K_FOREVER); return hid_int_ep_write(hid_dev, report, len, NULL); } @@ -56,7 +43,7 @@ static int zmk_usb_hid_init(struct device *_arg) usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), - &ops); + NULL); usb_hid_init(hid_dev); -- cgit v1.2.3 From 66c4b7ebb0ec914089a8f08905d832b16493dd73 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 17 Aug 2020 22:17:59 -0400 Subject: fix(usb): Restore write semaphore, release it on write failures. --- app/src/usb_hid.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'app/src/usb_hid.c') diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c index 4c6dd4b..784fc25 100644 --- a/app/src/usb_hid.c +++ b/app/src/usb_hid.c @@ -11,18 +11,42 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -static enum usb_dc_status_code usb_status; +static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN; static struct device *hid_dev; +static K_SEM_DEFINE(hid_sem, 1, 1); + +static void in_ready_cb(void) +{ + k_sem_give(&hid_sem); +} + +static const struct hid_ops ops = +{ + .int_in_ready = in_ready_cb, +}; + int zmk_usb_hid_send_report(const u8_t *report, size_t len) { - if (usb_status == USB_DC_SUSPEND) - { + switch(usb_status) { + case USB_DC_SUSPEND: return usb_wakeup_request(); + case USB_DC_ERROR: + case USB_DC_RESET: + case USB_DC_DISCONNECTED: + case USB_DC_UNKNOWN: + return -ENODEV; + default: + k_sem_take(&hid_sem, K_MSEC(30)); + int err = hid_int_ep_write(hid_dev, report, len, NULL); + + if (err) { + k_sem_give(&hid_sem); + } + + return err; } - - return hid_int_ep_write(hid_dev, report, len, NULL); } void usb_hid_status_cb(enum usb_dc_status_code status, const u8_t *params) @@ -43,7 +67,7 @@ static int zmk_usb_hid_init(struct device *_arg) usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), - NULL); + &ops); usb_hid_init(hid_dev); -- cgit v1.2.3