You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
1.9 KiB
87 lines
1.9 KiB
4 years ago
|
/*
|
||
|
* Copyright (c) 2020 The ZMK Contributors
|
||
|
*
|
||
|
* SPDX-License-Identifier: MIT
|
||
|
*/
|
||
5 years ago
|
|
||
|
#include <device.h>
|
||
5 years ago
|
#include <init.h>
|
||
5 years ago
|
|
||
|
#include <usb/usb_device.h>
|
||
|
#include <usb/class/usb_hid.h>
|
||
5 years ago
|
#include <dt-bindings/zmk/keys.h>
|
||
|
|
||
5 years ago
|
#include <zmk/hid.h>
|
||
|
#include <zmk/keymap.h>
|
||
5 years ago
|
|
||
5 years ago
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||
5 years ago
|
|
||
4 years ago
|
static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN;
|
||
5 years ago
|
|
||
4 years ago
|
#ifdef CONFIG_ZMK_USB
|
||
|
|
||
5 years ago
|
static struct device *hid_dev;
|
||
5 years ago
|
|
||
4 years ago
|
static K_SEM_DEFINE(hid_sem, 1, 1);
|
||
|
|
||
4 years ago
|
static void in_ready_cb(void) { k_sem_give(&hid_sem); }
|
||
4 years ago
|
|
||
4 years ago
|
static const struct hid_ops ops = {
|
||
|
.int_in_ready = in_ready_cb,
|
||
4 years ago
|
};
|
||
|
|
||
4 years ago
|
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;
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
#endif /* CONFIG_ZMK_USB */
|
||
|
|
||
|
enum usb_dc_status_code zmk_usb_get_status() { return usb_status; }
|
||
5 years ago
|
|
||
4 years ago
|
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) {
|
||
4 years ago
|
int usb_enable_ret;
|
||
5 years ago
|
|
||
4 years ago
|
#ifdef CONFIG_ZMK_USB
|
||
4 years ago
|
hid_dev = device_get_binding("HID_0");
|
||
|
if (hid_dev == NULL) {
|
||
|
LOG_ERR("Unable to locate HID device");
|
||
|
return -EINVAL;
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), &ops);
|
||
5 years ago
|
|
||
4 years ago
|
usb_hid_init(hid_dev);
|
||
5 years ago
|
|
||
4 years ago
|
#endif /* CONFIG_ZMK_USB */
|
||
|
|
||
|
usb_enable_ret = usb_enable(usb_status_cb);
|
||
5 years ago
|
|
||
4 years ago
|
if (usb_enable_ret != 0) {
|
||
|
LOG_ERR("Unable to enable USB");
|
||
|
return -EINVAL;
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
return 0;
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
SYS_INIT(zmk_usb_init, APPLICATION, CONFIG_ZMK_USB_INIT_PRIORITY);
|