From bbf5a5905a973d6d23457d2e7bf15bf6bac234c3 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 4 Oct 2020 18:18:44 -0400 Subject: refactor(usb): Report USB status w/o HID output. --- app/src/endpoints.c | 2 +- app/src/usb.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ app/src/usb_hid.c | 79 ------------------------------------------------ 3 files changed, 87 insertions(+), 80 deletions(-) create mode 100644 app/src/usb.c delete mode 100644 app/src/usb_hid.c (limited to 'app/src') diff --git a/app/src/endpoints.c b/app/src/endpoints.c index ae78587..79d294e 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include diff --git a/app/src/usb.c b/app/src/usb.c new file mode 100644 index 0000000..434b3d4 --- /dev/null +++ b/app/src/usb.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +#include +#include +#include + +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN; + +#ifdef CONFIG_ZMK_USB + +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) { + 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; + } +} + +#endif /* CONFIG_ZMK_USB */ + +enum usb_dc_status_code zmk_usb_get_status() { return usb_status; } + +void usb_status_cb(enum usb_dc_status_code status, const u8_t *params) { usb_status = status; }; + +static int zmk_usb_init(struct device *_arg) { + int usb_enable_ret; + +#ifdef CONFIG_ZMK_USB + hid_dev = device_get_binding("HID_0"); + if (hid_dev == NULL) { + LOG_ERR("Unable to locate HID device"); + return -EINVAL; + } + + usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), &ops); + + usb_hid_init(hid_dev); + +#endif /* CONFIG_ZMK_USB */ + + usb_enable_ret = usb_enable(usb_status_cb); + + if (usb_enable_ret != 0) { + LOG_ERR("Unable to enable USB"); + return -EINVAL; + } + + return 0; +} + +SYS_INIT(zmk_usb_init, APPLICATION, CONFIG_ZMK_USB_INIT_PRIORITY); diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c deleted file mode 100644 index 64addae..0000000 --- a/app/src/usb_hid.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2020 The ZMK Contributors - * - * SPDX-License-Identifier: MIT - */ - -#include -#include - -#include -#include -#include - -#include -#include - -LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); - -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, -}; - -enum usb_dc_status_code zmk_usb_hid_get_status() { return usb_status; } - -int zmk_usb_hid_send_report(const u8_t *report, size_t len) { - 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; - } -} - -void usb_hid_status_cb(enum usb_dc_status_code status, const u8_t *params) { usb_status = status; }; - -static int zmk_usb_hid_init(struct device *_arg) { - int usb_enable_ret; - - hid_dev = device_get_binding("HID_0"); - if (hid_dev == NULL) { - LOG_ERR("Unable to locate HID device"); - return -EINVAL; - } - - usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), &ops); - - usb_hid_init(hid_dev); - - usb_enable_ret = usb_enable(usb_hid_status_cb); - - if (usb_enable_ret != 0) { - LOG_ERR("Unable to enable USB"); - return -EINVAL; - } - - return 0; -} - -SYS_INIT(zmk_usb_hid_init, APPLICATION, CONFIG_ZMK_USB_INIT_PRIORITY); -- cgit v1.2.3