2020-05-06 06:09:05 +10:00
|
|
|
|
|
|
|
#include <device.h>
|
|
|
|
|
|
|
|
#include <usb/usb_device.h>
|
|
|
|
#include <usb/class/usb_hid.h>
|
2020-05-13 03:22:07 +10:00
|
|
|
#include <dt-bindings/zmk/keys.h>
|
|
|
|
|
2020-05-18 23:11:46 +10:00
|
|
|
#include <zmk/hid.h>
|
|
|
|
#include <zmk/keymap.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
|
|
|
|
|
|
|
static enum usb_dc_status_code usb_status;
|
|
|
|
|
|
|
|
static struct device *hid_dev;
|
2020-05-13 03:22:07 +10:00
|
|
|
|
2020-05-15 23:41:06 +10:00
|
|
|
int zmk_usb_hid_send_report(const struct zmk_hid_report *report)
|
2020-05-06 06:09:05 +10:00
|
|
|
{
|
2020-05-13 03:22:07 +10:00
|
|
|
if (usb_status == USB_DC_SUSPEND)
|
|
|
|
{
|
2020-05-06 06:09:05 +10:00
|
|
|
return usb_wakeup_request();
|
|
|
|
}
|
|
|
|
|
2020-05-15 23:41:06 +10:00
|
|
|
return hid_int_ep_write(hid_dev, (u8_t *)report, sizeof(struct zmk_hid_report), NULL);
|
2020-05-06 06:09:05 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_hid_status_cb(enum usb_dc_status_code status, const u8_t *params)
|
|
|
|
{
|
|
|
|
usb_status = status;
|
|
|
|
};
|
|
|
|
|
|
|
|
int zmk_usb_hid_init()
|
|
|
|
{
|
|
|
|
int usb_enable_ret;
|
|
|
|
|
|
|
|
hid_dev = device_get_binding("HID_0");
|
2020-05-13 03:22:07 +10:00
|
|
|
if (hid_dev == NULL)
|
|
|
|
{
|
2020-05-06 06:09:05 +10:00
|
|
|
LOG_ERR("Unable to locate HID device");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_hid_register_device(hid_dev,
|
2020-05-15 23:41:06 +10:00
|
|
|
zmk_hid_report_desc, sizeof(zmk_hid_report_desc),
|
2020-05-13 03:22:07 +10:00
|
|
|
NULL);
|
2020-05-06 06:09:05 +10:00
|
|
|
|
|
|
|
usb_hid_init(hid_dev);
|
|
|
|
|
|
|
|
usb_enable_ret = usb_enable(usb_hid_status_cb);
|
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
if (usb_enable_ret != 0)
|
|
|
|
{
|
2020-05-06 06:09:05 +10:00
|
|
|
LOG_ERR("Unable to enable USB");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|