zmk/app/src/usb_hid.c
Andrew Childs 843d324525
Boot protocol support
Based on PR#1140 with several changes as described in this comment: https://github.com/zmkfirmware/zmk/pull/1140#issuecomment-1237655563

* Minimize the api changes for bluetooth by keeping
  zmk_hid_get_keyboard_report() and returning the .body like before.
* Keeping the logic about "full" vs "body" entirely within the usb code path.
* The endpoint now calls either zmk_usb_hid_send_keyboard_report() and
  zmk_usb_hid_send_consumer_report() instead of zmk_usb_hid_send_report(...).
    - These functions now internally dispatch on protocol to either
      zmk_hid_get_keyboard_report() or a new function for boot reports
      zmk_hid_get_boot_report().
    - There's a change here from the PR version in the behavior of get_report():
      when in boot protocol, don't include the report id. I believe this is
      correct, in that implicit boot protocol report descriptor does not use
      multiple reports, so any boot protocol report should not include the report
      id field.
* Use a single definition of a boot report, used for regular reports in
  non-6KRO, and for rollover in all branches.
* Handle gaps in the zmk report when producing a boot report in HKRO mode. For
  .example, if it was 8KRO, it would be possible to have the state 0 0 0 0 0 0 0
  17 (by pressing 8 keys, and letting go of the first 7). Copying the first 6
  bytes would not show up the single pressed key.
2022-11-25 20:13:24 +09:00

172 lines
5.1 KiB
C

/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <device.h>
#include <init.h>
#include <usb/usb_device.h>
#include <usb/class/usb_hid.h>
#include <zmk/usb.h>
#include <zmk/hid.h>
#include <zmk/keymap.h>
#include <zmk/led_indicators.h>
#include <zmk/event_manager.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
static const struct device *hid_dev;
static K_SEM_DEFINE(hid_sem, 1, 1);
static void in_ready_cb(const struct device *dev) { k_sem_give(&hid_sem); }
#define HID_GET_REPORT_TYPE_MASK 0xff00
#define HID_GET_REPORT_ID_MASK 0x00ff
#define HID_REPORT_TYPE_INPUT 0x100
#define HID_REPORT_TYPE_OUTPUT 0x200
#define HID_REPORT_TYPE_FEATURE 0x300
#if IS_ENABLED(CONFIG_ZMK_USB_BOOT)
static uint8_t hid_protocol = HID_PROTOCOL_REPORT;
static void set_proto_cb(const struct device *dev, uint8_t protocol) { hid_protocol = protocol; }
void zmk_usb_hid_set_protocol(uint8_t protocol) { hid_protocol = protocol; }
#endif /* IS_ENABLED(CONFIG_ZMK_USB_BOOT) */
static uint8_t *get_keyboard_report(size_t *len) {
if (hid_protocol == HID_PROTOCOL_REPORT) {
struct zmk_hid_keyboard_report *report = zmk_hid_get_keyboard_report();
*len = sizeof(*report);
return (uint8_t *)report;
}
#if IS_ENABLED(CONFIG_ZMK_USB_BOOT)
zmk_hid_boot_report_t *boot_report = zmk_hid_get_boot_report();
*len = sizeof(*boot_report);
return (uint8_t *)boot_report;
#endif
}
static int get_report_cb(const struct device *dev, struct usb_setup_packet *setup, int32_t *len,
uint8_t **data) {
/*
* 7.2.1 of the HID v1.11 spec is unclear about handling requests for reports that do not exist
* For requested reports that aren't input reports, return -ENOTSUP like the Zephyr subsys does
*/
if ((setup->wValue & HID_GET_REPORT_TYPE_MASK) != HID_REPORT_TYPE_INPUT) {
LOG_ERR("Unsupported report type %d requested", (setup->wValue & HID_GET_REPORT_TYPE_MASK)
<< 8);
return -ENOTSUP;
}
switch (setup->wValue & HID_GET_REPORT_ID_MASK) {
case HID_REPORT_ID_KEYBOARD: {
*data = get_keyboard_report(len);
break;
}
case HID_REPORT_ID_CONSUMER: {
struct zmk_hid_consumer_report *report = zmk_hid_get_consumer_report();
*data = (uint8_t *)report;
*len = sizeof(*report);
break;
}
default:
LOG_ERR("Invalid report ID %d requested", setup->wValue & HID_GET_REPORT_ID_MASK);
return -EINVAL;
}
return 0;
}
static int set_report_cb(const struct device *dev, struct usb_setup_packet *setup, int32_t *len,
uint8_t **data) {
if ((setup->wValue & HID_GET_REPORT_TYPE_MASK) != HID_REPORT_TYPE_OUTPUT) {
LOG_ERR("Unsupported report type %d requested",
(setup->wValue & HID_GET_REPORT_TYPE_MASK) >> 8);
return -ENOTSUP;
}
switch (setup->wValue & HID_GET_REPORT_ID_MASK) {
case HID_REPORT_ID_LEDS:
if (*len != sizeof(struct zmk_hid_led_report)) {
LOG_ERR("LED set report is malformed: length=%d", *len);
} else {
struct zmk_hid_led_report *report = (struct zmk_hid_led_report *)*data;
zmk_leds_process_report(&report->body, ZMK_ENDPOINT_USB, 0);
}
break;
default:
LOG_ERR("Invalid report ID %d requested", setup->wValue & HID_GET_REPORT_ID_MASK);
return -EINVAL;
}
return 0;
}
static const struct hid_ops ops = {
#if IS_ENABLED(CONFIG_ZMK_USB_BOOT)
.protocol_change = set_proto_cb,
#endif
.int_in_ready = in_ready_cb,
.get_report = get_report_cb,
.set_report = set_report_cb,
};
static int zmk_usb_hid_send_report(const uint8_t *report, size_t len) {
switch (zmk_usb_get_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;
}
}
int zmk_usb_hid_send_keyboard_report() {
size_t len;
uint8_t *report = get_keyboard_report(&len);
return zmk_usb_hid_send_report(report, len);
}
int zmk_usb_hid_send_consumer_report() {
#if IS_ENABLED(CONFIG_ZMK_USB_BOOT)
if (hid_protocol == HID_PROTOCOL_BOOT) {
return -ENOTSUP;
}
#endif /* IS_ENABLED(CONFIG_ZMK_USB_BOOT) */
struct zmk_hid_consumer_report *report = zmk_hid_get_consumer_report();
return zmk_usb_hid_send_report((uint8_t *)report, sizeof(*report));
}
static int zmk_usb_hid_init(const struct device *_arg) {
hid_dev = device_get_binding("HID_0");
if (hid_dev == NULL) {
LOG_ERR("Unable to locate HID device");
return -EINVAL;
}
usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), &ops);
usb_hid_init(hid_dev);
return 0;
}
SYS_INIT(zmk_usb_hid_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);