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>
|
|
|
|
|
|
|
|
#include "keymap.h"
|
2020-05-06 06:09:05 +10:00
|
|
|
|
|
|
|
LOG_MODULE_REGISTER(zmk_usb_hid, CONFIG_ZMK_USB_HID_LOG_LEVEL);
|
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
#define MAX_KEYCODE KC_APP
|
|
|
|
|
|
|
|
static const u8_t hid_report_desc[] = {
|
|
|
|
/* USAGE_PAGE (Generic Desktop) */
|
|
|
|
HID_GI_USAGE_PAGE,
|
|
|
|
USAGE_GEN_DESKTOP,
|
|
|
|
/* USAGE (Keyboard) */
|
|
|
|
HID_LI_USAGE,
|
|
|
|
USAGE_GEN_DESKTOP_KEYBOARD,
|
|
|
|
/* COLLECTION (Application) */
|
|
|
|
HID_MI_COLLECTION,
|
|
|
|
COLLECTION_APPLICATION,
|
|
|
|
/* USAGE_PAGE (Keypad) */
|
|
|
|
HID_GI_USAGE_PAGE,
|
|
|
|
USAGE_GEN_DESKTOP_KEYPAD,
|
|
|
|
/* USAGE_MINIMUM (Keyboard LeftControl) */
|
|
|
|
HID_LI_USAGE_MIN(1),
|
|
|
|
0xE0,
|
|
|
|
/* USAGE_MAXIMUM (Keyboard Right GUI) */
|
|
|
|
HID_LI_USAGE_MAX(1),
|
|
|
|
0xE7,
|
|
|
|
/* LOGICAL_MINIMUM (0) */
|
|
|
|
HID_GI_LOGICAL_MIN(1),
|
|
|
|
0x00,
|
|
|
|
/* LOGICAL_MAXIMUM (1) */
|
|
|
|
HID_GI_LOGICAL_MAX(1),
|
|
|
|
0x01,
|
|
|
|
/* REPORT_SIZE (1) */
|
|
|
|
HID_GI_REPORT_SIZE,
|
|
|
|
0x01,
|
|
|
|
/* REPORT_COUNT (8) */
|
|
|
|
HID_GI_REPORT_COUNT,
|
|
|
|
0x08,
|
|
|
|
/* INPUT (Data,Var,Abs) */
|
|
|
|
HID_MI_INPUT,
|
|
|
|
0x02,
|
|
|
|
/* USAGE_PAGE (Keypad) */
|
|
|
|
HID_GI_USAGE_PAGE,
|
|
|
|
USAGE_GEN_DESKTOP_KEYPAD,
|
|
|
|
/* REPORT_SIZE (8) */
|
|
|
|
HID_GI_REPORT_SIZE,
|
|
|
|
0x08,
|
|
|
|
/* REPORT_COUNT (1) */
|
|
|
|
HID_GI_REPORT_COUNT,
|
|
|
|
0x07,
|
|
|
|
/* INPUT (Cnst,Var,Abs) */
|
|
|
|
HID_MI_INPUT,
|
|
|
|
0x03,
|
|
|
|
|
|
|
|
/* USAGE_PAGE (Keypad) */
|
|
|
|
HID_GI_USAGE_PAGE,
|
|
|
|
USAGE_GEN_DESKTOP_KEYPAD,
|
|
|
|
/* LOGICAL_MINIMUM (0) */
|
|
|
|
HID_GI_LOGICAL_MIN(1),
|
|
|
|
0x00,
|
|
|
|
/* LOGICAL_MAXIMUM (101) */
|
|
|
|
HID_GI_LOGICAL_MAX(1),
|
|
|
|
0x01,
|
|
|
|
/* USAGE_MINIMUM (Reserved) */
|
|
|
|
HID_LI_USAGE_MIN(1),
|
|
|
|
0x00,
|
|
|
|
/* USAGE_MAXIMUM (Keyboard Application) */
|
|
|
|
HID_LI_USAGE_MAX(1),
|
|
|
|
MAX_KEYCODE,
|
|
|
|
/* REPORT_SIZE (8) */
|
|
|
|
HID_GI_REPORT_SIZE,
|
|
|
|
0x01,
|
|
|
|
/* REPORT_COUNT (6) */
|
|
|
|
HID_GI_REPORT_COUNT,
|
|
|
|
MAX_KEYCODE + 1,
|
|
|
|
/* INPUT (Data,Ary,Abs) */
|
|
|
|
HID_MI_INPUT,
|
|
|
|
0x02,
|
|
|
|
/* USAGE_PAGE (Keypad) */
|
|
|
|
HID_GI_USAGE_PAGE,
|
|
|
|
USAGE_GEN_DESKTOP_KEYPAD,
|
|
|
|
/* REPORT_SIZE (8) */
|
|
|
|
HID_GI_REPORT_SIZE,
|
|
|
|
0x02,
|
|
|
|
/* REPORT_COUNT (6) */
|
|
|
|
HID_GI_REPORT_COUNT,
|
|
|
|
0x01,
|
|
|
|
/* INPUT (Cnst,Var,Abs) */
|
|
|
|
HID_MI_INPUT,
|
|
|
|
0x03,
|
|
|
|
/* END_COLLECTION */
|
|
|
|
HID_MI_COLLECTION_END,
|
|
|
|
};
|
|
|
|
|
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
|
|
|
struct boot_report
|
|
|
|
{
|
|
|
|
u8_t modifiers;
|
|
|
|
u8_t _unused;
|
|
|
|
u8_t keys[6];
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct extended_report
|
|
|
|
{
|
|
|
|
u8_t keys[13];
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct hid_report
|
|
|
|
{
|
|
|
|
struct boot_report boot;
|
|
|
|
struct extended_report extended;
|
|
|
|
} __packed report;
|
2020-05-06 06:09:05 +10:00
|
|
|
|
|
|
|
#define KEY_OFFSET 0x02
|
|
|
|
#define MAX_KEYS 6
|
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
#define TOGGLE_BOOT_KEY(match, val) \
|
|
|
|
for (int idx = 0; idx < MAX_KEYS; idx++) \
|
|
|
|
{ \
|
|
|
|
if (report.boot.keys[idx + KEY_OFFSET] != match) \
|
|
|
|
{ \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
report.boot.keys[idx + KEY_OFFSET] = val; \
|
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TOGGLE_EXT_KEY(code, val) WRITE_BIT(report.extended.keys[code / 8], code % 8, val)
|
|
|
|
|
|
|
|
int zmk_usb_hid_press_key(zmk_key code)
|
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-13 03:22:07 +10:00
|
|
|
if (code > MAX_KEYCODE)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
TOGGLE_BOOT_KEY(0U, code);
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
TOGGLE_EXT_KEY(code, true);
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
return hid_int_ep_write(hid_dev, (u8_t *)&report, sizeof(report), NULL);
|
2020-05-06 06:09:05 +10:00
|
|
|
}
|
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
int zmk_usb_hid_release_key(zmk_key code)
|
2020-05-06 06:09:05 +10:00
|
|
|
{
|
2020-05-13 03:22:07 +10:00
|
|
|
if (code > MAX_KEYCODE)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
TOGGLE_BOOT_KEY(code, 0U);
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
TOGGLE_EXT_KEY(code, false);
|
2020-05-06 06:09:05 +10:00
|
|
|
|
2020-05-13 03:22:07 +10:00
|
|
|
return hid_int_ep_write(hid_dev, (u8_t *)&report, sizeof(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-13 03:22:07 +10:00
|
|
|
hid_report_desc, sizeof(hid_report_desc),
|
|
|
|
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;
|
|
|
|
}
|