2020-09-10 13:57:44 +10:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
2020-05-06 06:09:05 +10:00
|
|
|
|
|
|
|
#include <device.h>
|
2020-07-08 00:20:23 +10:00
|
|
|
#include <init.h>
|
2020-05-06 06:09:05 +10:00
|
|
|
|
|
|
|
#include <usb/usb_device.h>
|
|
|
|
#include <usb/class/usb_hid.h>
|
2020-05-13 03:22:07 +10:00
|
|
|
|
2020-05-18 23:11:46 +10:00
|
|
|
#include <zmk/hid.h>
|
|
|
|
#include <zmk/keymap.h>
|
2020-12-23 02:56:00 +11:00
|
|
|
#include <zmk/event_manager.h>
|
|
|
|
#include <zmk/events/usb_conn_state_changed.h>
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-05-25 08:22:16 +10:00
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-08-18 12:17:59 +10:00
|
|
|
static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN;
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2021-08-03 07:00:56 +10:00
|
|
|
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);
|
|
|
|
|
2020-10-05 09:18:44 +11:00
|
|
|
#ifdef CONFIG_ZMK_USB
|
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static const struct device *hid_dev;
|
2020-05-13 03:22:07 +10:00
|
|
|
|
2020-08-18 12:17:59 +10:00
|
|
|
static K_SEM_DEFINE(hid_sem, 1, 1);
|
|
|
|
|
2020-11-20 09:58:03 +11:00
|
|
|
static void in_ready_cb(const struct device *dev) { k_sem_give(&hid_sem); }
|
2020-08-18 12:17:59 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static const struct hid_ops ops = {
|
|
|
|
.int_in_ready = in_ready_cb,
|
2020-08-18 12:17:59 +10:00
|
|
|
};
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
int zmk_usb_hid_send_report(const uint8_t *report, size_t len) {
|
2020-09-14 12:53:24 +10:00
|
|
|
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;
|
|
|
|
}
|
2020-05-06 06:09:05 +10:00
|
|
|
}
|
|
|
|
|
2020-10-05 09:18:44 +11:00
|
|
|
#endif /* CONFIG_ZMK_USB */
|
|
|
|
|
|
|
|
enum usb_dc_status_code zmk_usb_get_status() { return usb_status; }
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-10-12 09:03:21 +11:00
|
|
|
enum zmk_usb_conn_state zmk_usb_get_conn_state() {
|
2021-08-03 07:00:56 +10:00
|
|
|
LOG_DBG("state: %d", usb_status);
|
2020-10-12 09:03:21 +11:00
|
|
|
switch (usb_status) {
|
|
|
|
case USB_DC_DISCONNECTED:
|
|
|
|
case USB_DC_UNKNOWN:
|
|
|
|
return ZMK_USB_CONN_NONE;
|
|
|
|
|
|
|
|
case USB_DC_ERROR:
|
|
|
|
case USB_DC_RESET:
|
|
|
|
return ZMK_USB_CONN_POWERED;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ZMK_USB_CONN_HID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
void usb_status_cb(enum usb_dc_status_code status, const uint8_t *params) {
|
2020-10-12 09:03:21 +11:00
|
|
|
usb_status = status;
|
2021-08-03 07:00:56 +10:00
|
|
|
k_work_submit(&usb_status_notifier_work);
|
2020-10-12 09:03:21 +11:00
|
|
|
};
|
2020-10-05 09:18:44 +11:00
|
|
|
|
2020-12-11 06:31:51 +11:00
|
|
|
static int zmk_usb_init(const struct device *_arg) {
|
2020-09-14 12:53:24 +10:00
|
|
|
int usb_enable_ret;
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-10-05 09:18:44 +11:00
|
|
|
#ifdef CONFIG_ZMK_USB
|
2020-09-14 12:53:24 +10:00
|
|
|
hid_dev = device_get_binding("HID_0");
|
|
|
|
if (hid_dev == NULL) {
|
|
|
|
LOG_ERR("Unable to locate HID device");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), &ops);
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
usb_hid_init(hid_dev);
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-10-05 09:18:44 +11:00
|
|
|
#endif /* CONFIG_ZMK_USB */
|
|
|
|
|
|
|
|
usb_enable_ret = usb_enable(usb_status_cb);
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
if (usb_enable_ret != 0) {
|
|
|
|
LOG_ERR("Unable to enable USB");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
return 0;
|
2020-05-06 06:09:05 +10:00
|
|
|
}
|
2020-07-08 00:20:23 +10:00
|
|
|
|
2020-10-05 09:18:44 +11:00
|
|
|
SYS_INIT(zmk_usb_init, APPLICATION, CONFIG_ZMK_USB_INIT_PRIORITY);
|