From a46b7ad8b85e10aa8aaed94c58f61fb92d20429c Mon Sep 17 00:00:00 2001 From: skiinder Date: Fri, 20 Mar 2026 15:50:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(usb=5Fhid):=20=E6=94=AF=E6=8C=81HID?= =?UTF-8?q?=E4=BE=9B=E5=BA=94=E5=95=86=E6=8A=A5=E5=91=8A=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=B9=B6=E5=A2=9E=E5=8A=A0=E8=BE=93=E5=87=BA=E6=8A=A5=E5=91=8A?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持HID供应商特定报告类型的处理,在USB HID模块中添加了对REPORT_ID_VENDOR 的支持,并相应地修改了设备覆盖文件中的输出报告大小配置。 功能变更包括: - 在app.overlay中将out-report-size从8增加到31以支持更大的报告 - 添加hid_vendor_mask_event.h头文件引入 - 实现try_extract_vendor_mask函数用于解析供应商特定掩码数据 - 在hid_stub_set_report和hid_stub_output_report函数中添加供应商掩码处理逻辑 - 更新handle_hid_tx_event函数以允许REPORT_ID_VENDOR类型的报告 --- app.overlay | 2 +- src/modules/usb_hid_module.c | 48 +++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/app.overlay b/app.overlay index 7b57426..d4f648b 100644 --- a/app.overlay +++ b/app.overlay @@ -22,7 +22,7 @@ protocol-code = "none"; in-report-size = <31>; in-polling-period-us = <1000>; - out-report-size = <8>; + out-report-size = <31>; out-polling-period-us = <1000>; }; diff --git a/src/modules/usb_hid_module.c b/src/modules/usb_hid_module.c index 5e128e2..1e823a0 100644 --- a/src/modules/usb_hid_module.c +++ b/src/modules/usb_hid_module.c @@ -17,6 +17,7 @@ #include "hid_protocol_event.h" #include "hid_tx_done_event.h" #include "hid_tx_event.h" +#include "hid_vendor_mask_event.h" #include "keyboard_led_event.h" #include "mode_event.h" @@ -164,6 +165,33 @@ static bool try_extract_led_mask(const struct device *dev, return true; } +static bool try_extract_vendor_mask(const struct device *dev, + uint16_t len, + const uint8_t *buf, + const uint8_t **mask_data, + size_t *mask_len) +{ + if ((buf == NULL) || (len < 1U)) { + return false; + } + + if (dev != g_usb_hid.nkro.dev) { + return false; + } + + if (buf[0] != REPORT_ID_VENDOR) { + return false; + } + + if ((len - 1U) != HID_VENDOR_PAYLOAD_SIZE) { + return false; + } + + *mask_data = &buf[1]; + *mask_len = len - 1U; + return true; +} + static int hid_stub_get_report(const struct device *dev, uint8_t type, uint8_t id, uint16_t len, uint8_t *buf) @@ -184,6 +212,14 @@ static int hid_stub_set_report(const struct device *dev, ARG_UNUSED(id); if (!should_handle_led_input_from_dev(dev)) { + const uint8_t *mask_data; + size_t mask_len; + + if (try_extract_vendor_mask(dev, len, buf, &mask_data, &mask_len)) { + LOG_INF("hid_stub_set_report vendor mask len=%u", mask_len); + hid_vendor_mask_event_submit(mask_data, mask_len); + } + return 0; } @@ -260,6 +296,14 @@ static void hid_stub_input_done(const struct device *dev, const uint8_t *report) static void hid_stub_output_report(const struct device *dev, uint16_t len, const uint8_t *buf) { if (!should_handle_led_input_from_dev(dev)) { + const uint8_t *mask_data; + size_t mask_len; + + if (try_extract_vendor_mask(dev, len, buf, &mask_data, &mask_len)) { + LOG_INF("hid_stub_output_report vendor mask len=%u", mask_len); + hid_vendor_mask_event_submit(mask_data, mask_len); + } + return; } @@ -668,7 +712,9 @@ static bool handle_hid_tx_event(const struct hid_tx_event *event) return false; } - if ((report_id != REPORT_ID_KEYBOARD) && (report_id != REPORT_ID_CONSUMER)) { + if ((report_id != REPORT_ID_KEYBOARD) && + (report_id != REPORT_ID_CONSUMER) && + (report_id != REPORT_ID_VENDOR)) { submit_usb_tx_done(HID_TX_KIND_REPORT, false); return false; }