fix(usb): USB state changes in system thhread.

* Use a `k_work` to send usb status events from the system
  workqueue thread, not the USB work thread.
This commit is contained in:
Peter Johanson 2021-08-02 17:00:56 -04:00 committed by Pete Johanson
parent cf5c56c4f1
commit 9d34cf561e

View file

@ -19,6 +19,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN; static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN;
static void raise_usb_status_changed_event(struct k_work *_work) {
ZMK_EVENT_RAISE(new_zmk_usb_conn_state_changed(
(struct zmk_usb_conn_state_changed){.conn_state = zmk_usb_get_conn_state()}));
}
K_WORK_DEFINE(usb_status_notifier_work, raise_usb_status_changed_event);
#ifdef CONFIG_ZMK_USB #ifdef CONFIG_ZMK_USB
static const struct device *hid_dev; static const struct device *hid_dev;
@ -54,14 +61,10 @@ int zmk_usb_hid_send_report(const uint8_t *report, size_t len) {
#endif /* CONFIG_ZMK_USB */ #endif /* CONFIG_ZMK_USB */
static void raise_usb_status_changed_event() {
ZMK_EVENT_RAISE(new_zmk_usb_conn_state_changed(
(struct zmk_usb_conn_state_changed){.conn_state = zmk_usb_get_conn_state()}));
}
enum usb_dc_status_code zmk_usb_get_status() { return usb_status; } enum usb_dc_status_code zmk_usb_get_status() { return usb_status; }
enum zmk_usb_conn_state zmk_usb_get_conn_state() { enum zmk_usb_conn_state zmk_usb_get_conn_state() {
LOG_DBG("state: %d", usb_status);
switch (usb_status) { switch (usb_status) {
case USB_DC_DISCONNECTED: case USB_DC_DISCONNECTED:
case USB_DC_UNKNOWN: case USB_DC_UNKNOWN:
@ -78,7 +81,7 @@ enum zmk_usb_conn_state zmk_usb_get_conn_state() {
void usb_status_cb(enum usb_dc_status_code status, const uint8_t *params) { void usb_status_cb(enum usb_dc_status_code status, const uint8_t *params) {
usb_status = status; usb_status = status;
raise_usb_status_changed_event(); k_work_submit(&usb_status_notifier_work);
}; };
static int zmk_usb_init(const struct device *_arg) { static int zmk_usb_init(const struct device *_arg) {