From 587339b73370feefb572d36d8659b9b1195d63b4 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Wed, 17 Dec 2025 20:43:15 +0800 Subject: [PATCH] feat(class/hid): add hid report parse api Signed-off-by: sakumisu <1203593632@qq.com> --- class/hid/usb_hid.h | 219 ++++++++++----------- class/hid/usbh_hid.c | 451 ++++++++++++++++++++++++++++++++++++++++--- class/hid/usbh_hid.h | 81 ++++++++ 3 files changed, 606 insertions(+), 145 deletions(-) diff --git a/class/hid/usb_hid.h b/class/hid/usb_hid.h index c96e3836..b2bd5713 100644 --- a/class/hid/usb_hid.h +++ b/class/hid/usb_hid.h @@ -35,7 +35,7 @@ #define HID_REPORT_OUTPUT 0x02 #define HID_REPORT_FEATURE 0x03 -/* HID Descriptor ***********************************************************/ +/* HID Descriptor */ #define HID_COUNTRY_NONE 0x00 /* Not Supported */ #define HID_COUNTRY_ARABIC 0x01 /* Arabic */ @@ -74,99 +74,114 @@ #define HID_COUNTRY_YUGOSLAVIA 0x34 /* Yugoslavia */ #define HID_COUNTRY_TURKISHF 0x35 /* Turkish-F */ -/* HID report items */ -#define HID_REPORT_ITEM_SIZE_MASK 0x03 -#define HID_REPORT_ITEM_SIZE_0 0x00 /* No data follows */ -#define HID_REPORT_ITEM_SIZE_1 0x01 /* 1 byte of data follows */ -#define HID_REPORT_ITEM_SIZE_2 0x02 /* 2 bytes of data follow */ -#define HID_REPORT_ITEM_SIZE_4 0x03 /* 4 bytes of data follow */ -#define HID_REPORT_ITEM_TYPE_MASK 0x0c -#define HID_REPORT_ITEM_TYPE_MAIN 0x00 -#define HID_REPORT_ITEM_TYPE_GLOBAL 0x04 -#define HID_REPORT_ITEM_TYPE_LOCAL 0x08 -#define HID_REPORT_ITEM_TAG_MASK 0xf0 +/* HID report See specification at + * https://www.usb.org/sites/default/files/hid1_11.pdf + * https://www.usb.org/sites/default/files/hut1_22.pdf + */ + +#define HID_SIZE_MASK (0x3 << 0) +#define HID_TYPE_MASK (0x3 << 2) +#define HID_TAG_MASK (0xF << 4) + +#define HID_ITEMTYPE_MAIN (0x0 << 2) +#define HID_ITEMTYPE_GLOBAL (0x1 << 2) +#define HID_ITEMTYPE_LOCAL (0x2 << 2) +#define HID_ITEMTYPE_LONG (0x3 << 2) /* Main Items (HID 6.2.2.4) */ -#define HID_MAIN_ITEM_CONSTANT (1 << 0) /* Constant(1) vs Data(0) */ -#define HID_MAIN_ITEM_VARIABLE (1 << 1) /* Variable(1) vs Array(0) */ -#define HID_MAIN_ITEM_RELATIVE (1 << 2) /* Relative(1) vs Absolute(0) */ -#define HID_MAIN_ITEM_WRAP (1 << 3) /* Wrap(1) vs No Wrap(0) */ -#define HID_MAIN_ITEM_NONLINEAR (1 << 4) /* Non Linear(1) vs Linear(0) */ -#define HID_MAIN_ITEM_NOPREFERRED (1 << 5) /* No Preferred (1) vs Preferred State(0) */ -#define HID_MAIN_ITEM_NULLSTATE (1 << 6) /* Null state(1) vs No Null position(0) */ -#define HID_MAIN_ITEM_VOLATILE (1 << 7) /* Volatile(1) vs Non volatile(0) */ -#define HID_MAIN_ITEM_BUFFEREDBYTES (1 << 8) /* Buffered Bytes(1) vs Bit Field(0) */ +#define HID_MAINITEM_TAG_INPUT (0x08 << 4) +#define HID_MAINITEM_TAG_OUTPUT (0x09 << 4) +#define HID_MAINITEM_TAG_COLLECTION (0x0a << 4) +#define HID_MAINITEM_TAG_FEATURE (0x0b << 4) +#define HID_MAINITEM_TAG_ENDCOLLECTION (0x0c << 4) -#define HID_MAIN_ITEM_SIZE(pfx) ((pfx)&HID_REPORT_ITEM_SIZE_MASK) -#define HID_MAIN_ITEM_INPUT_PREFIX 0x80 -#define HID_MAIN_ITEM_INPUT_CONSTANT HID_MAIN_ITEM_CONSTANT -#define HID_MAIN_ITEM_INPUT_VARIABLE HID_MAIN_ITEM_VARIABLE -#define HID_MAIN_ITEM_INPUT_RELATIVE HID_MAIN_ITEM_RELATIVE -#define HID_MAIN_ITEM_INPUT_WRAP HID_MAIN_ITEM_WRAP -#define HID_MAIN_ITEM_INPUT_NONLINEAR HID_MAIN_ITEM_NONLINEAR -#define HID_MAIN_ITEM_INPUT_NOPREFERRED HID_MAIN_ITEM_NOPREFERRED -#define HID_MAIN_ITEM_INPUT_NULLSTATE HID_MAIN_ITEM_NULLSTATE -#define HID_MAIN_ITEM_INPUT_BUFFEREDBYTES HID_MAIN_ITEM_BUFFEREDBYTES +#define HID_MAINITEM_CONSTANT (1 << 0) /* Constant(1) vs Data(0) */ +#define HID_MAINITEM_VARIABLE (1 << 1) /* Variable(1) vs Array(0) */ +#define HID_MAINITEM_RELATIVE (1 << 2) /* Relative(1) vs Absolute(0) */ +#define HID_MAINITEM_WRAP (1 << 3) /* Wrap(1) vs No Wrap(0) */ +#define HID_MAINITEM_NONLINEAR (1 << 4) /* Non Linear(1) vs Linear(0) */ +#define HID_MAINITEM_NOPREFERRED (1 << 5) /* No Preferred (1) vs Preferred State(0) */ +#define HID_MAINITEM_NULLSTATE (1 << 6) /* Null state(1) vs No Null position(0) */ +#define HID_MAINITEM_VOLATILE (1 << 7) /* Volatile(1) vs Non volatile(0) */ +#define HID_MAINITEM_BUFFEREDBYTES (1 << 8) /* Buffered Bytes(1) vs Bit Field(0) */ -#define HID_MAIN_ITEM_OUTPUT_PREFIX 0x90 -#define HID_MAIN_ITEM_OUTPUT_CONSTANT HID_MAIN_ITEM_CONSTANT -#define HID_MAIN_ITEM_OUTPUT_VARIABLE HID_MAIN_ITEM_VARIABLE -#define HID_MAIN_ITEM_OUTPUT_RELATIVE HID_MAIN_ITEM_RELATIVE -#define HID_MAIN_ITEM_OUTPUT_WRAP HID_MAIN_ITEM_WRAP -#define HID_MAIN_ITEM_OUTPUT_NONLINEAR HID_MAIN_ITEM_NONLINEAR -#define HID_MAIN_ITEM_OUTPUT_NOPREFERRED HID_MAIN_ITEM_NOPREFERRED -#define HID_MAIN_ITEM_OUTPUT_NULLSTATE HID_MAIN_ITEM_NULLSTATE -#define HID_MAIN_ITEM_OUTPUT_VOLATILE HID_MAIN_ITEM_VOLATILE -#define HID_MAIN_ITEM_OUTPUT_BUFFEREDBYTES HID_MAIN_ITEM_BUFFEREDBYTES - -#define HID_MAIN_ITEM_FEATURE_PREFIX 0xb0 -#define HID_MAIN_ITEM_FEATURE_CONSTANT HID_MAIN_ITEM_CONSTANT -#define HID_MAIN_ITEM_FEATURE_VARIABLE HID_MAIN_ITEM_VARIABLE -#define HID_MAIN_ITEM_FEATURE_RELATIVE HID_MAIN_ITEM_RELATIVE -#define HID_MAIN_ITEM_FEATURE_WRAP HID_MAIN_ITEM_WRAP -#define HID_MAIN_ITEM_FEATURE_NONLINEAR HID_MAIN_ITEM_NONLINEAR -#define HID_MAIN_ITEM_FEATURE_NOPREFERRED HID_MAIN_ITEM_NOPREFERRED -#define HID_MAIN_ITEM_FEATURE_NULLSTATE HID_MAIN_ITEM_NULLSTATE -#define HID_MAIN_ITEM_FEATURE_VOLATILE HID_MAIN_ITEM_VOLATILE -#define HID_MAIN_ITEM_FEATURE_BUFFEREDBYTES HID_MAIN_ITEM_BUFFEREDBYTES - -#define HID_MAIN_ITEM_COLLECTION_PREFIX 0xa0 -#define HID_MAIN_ITEM_COLLECTION_PHYSICAL 0x00 /* Physical (group of axes) */ -#define HID_MAIN_ITEM_COLLECTION_APPL 0x01 /* Application (mouse, keyboard) */ -#define HID_MAIN_ITEM_COLLECTION_LOGICAL 0x02 /* Logical (interrelated data) */ -#define HID_MAIN_ITEM_COLLECTION_REPORT 0x03 /* Report */ -#define HID_MAIN_ITEM_COLLECTION_ARRAY 0x04 /* Named Array */ -#define HID_MAIN_ITEM_COLLECTION_SWITCH 0x05 /* Usage Switch */ -#define HID_MAIN_ITEM_COLLECTION_MODIFIER 0x06 /* Usage Modifier */ -#define HID_MAIN_ITEM_ENDCOLLECTION_PREFIX 0xc0 +#define HID_MAINITEM_COLLECTION_PHYSICAL 0x00 /* Physical (group of axes) */ +#define HID_MAINITEM_COLLECTION_APPL 0x01 /* Application (mouse, keyboard) */ +#define HID_MAINITEM_COLLECTION_LOGICAL 0x02 /* Logical (interrelated data) */ +#define HID_MAINITEM_COLLECTION_REPORT 0x03 /* Report */ +#define HID_MAINITEM_COLLECTION_ARRAY 0x04 /* Named Array */ +#define HID_MAINITEM_COLLECTION_SWITCH 0x05 /* Usage Switch */ +#define HID_MAINITEM_COLLECTION_MODIFIER 0x06 /* Usage Modifier */ /* Global Items (HID 6.2.2.7) */ -#define HID_GLOBAL_ITEM_SIZE(pfx) ((pfx)&HID_REPORT_ITEM_SIZE_MASK) -#define HID_GLOBAL_ITEM_USAGEPAGE_PREFIX 0x04 /* Usage Page */ -#define HID_GLOBAL_ITEM_LOGICALMIN_PREFIX 0x14 /* Logical Minimum */ -#define HID_GLOBAL_ITEM_LOGICALMAX_PREFIX 0x24 /* Logical Maximum */ -#define HID_GLOBAL_ITEM_PHYSICALMIN_PREFIX 0x34 /* Physical Minimum */ -#define HID_GLOBAL_ITEM_PHYSMICALAX_PREFIX 0x44 /* Physical Maximum */ -#define HID_GLOBAL_ITEM_UNITEXP_PREFIX 0x54 /* Unit Exponent */ -#define HID_GLOBAL_ITEM_UNIT_PREFIX 0x64 /* Unit */ -#define HID_GLOBAL_ITEM_REPORTSIZE_PREFIX 0x74 /* Report Size */ -#define HID_GLOBAL_ITEM_REPORTID_PREFIX 0x84 /* Report ID */ -#define HID_GLOBAL_ITEM_REPORTCOUNT_PREFIX 0x94 /* Report Count */ -#define HID_GLOBAL_ITEM_PUSH_PREFIX 0xa4 /* Push */ -#define HID_GLOBAL_ITEM_POP_PREFIX 0xb4 /* Pop */ +#define HID_GLOBALITEM_TAG_USAGE_PAGE (0x00 << 4) +#define HID_GLOBALITEM_TAG_LOGICAL_MIN (0x01 << 4) +#define HID_GLOBALITEM_TAG_LOGICAL_MAX (0x02 << 4) +#define HID_GLOBALITEM_TAG_PHYSICAL_MIN (0x03 << 4) +#define HID_GLOBALITEM_TAG_PHYSICAL_MAX (0x04 << 4) +#define HID_GLOBALITEM_TAG_UNIT_EXP (0x05 << 4) +#define HID_GLOBALITEM_TAG_UNIT (0x06 << 4) +#define HID_GLOBALITEM_TAG_REPORT_SIZE (0x07 << 4) +#define HID_GLOBALITEM_TAG_REPORT_ID (0x08 << 4) +#define HID_GLOBALITEM_TAG_REPORT_COUNT (0x09 << 4) +#define HID_GLOBALITEM_TAG_PUSH (0x0a << 4) +#define HID_GLOBALITEM_TAG_POP (0x0b << 4) /* Local Items (HID 6.2.2.8) */ -#define HID_LOCAL_ITEM_SIZE(pfx) ((pfx)&HID_REPORT_ITEM_SIZE_MASK) -#define HID_LOCAL_ITEM_USAGE_PREFIX 0x08 /* Usage */ -#define HID_LOCAL_ITEM_USAGEMIN_PREFIX 0x18 /* Usage Minimum */ -#define HID_LOCAL_ITEM_USAGEMAX_PREFIX 0x28 /* Usage Maximum */ -#define HID_LOCAL_ITEM_DESIGNATORIDX_PREFIX 0x38 /* Designator Index */ -#define HID_LOCAL_ITEM_DESIGNATORMIN_PREFIX 0x48 /* Designator Minimum */ -#define HID_LOCAL_ITEM_DESIGNATORMAX_PREFIX 0x58 /* Designator Maximum */ -#define HID_LOCAL_ITEM_STRINGIDX_PREFIX 0x78 /* String Index */ -#define HID_LOCAL_ITEM_STRINGMIN_PREFIX 0x88 /* String Minimum */ -#define HID_LOCAL_ITEM_STRINGMAX_PREFIX 0x98 /* xx */ -#define HID_LOCAL_ITEM_DELIMITER_PREFIX 0xa8 /* Delimiter */ +#define HID_LOCALITEM_TAG_USAGE (0x00 << 4) +#define HID_LOCALITEM_TAG_USAGE_MIN (0x01 << 4) +#define HID_LOCALITEM_TAG_USAGE_MAX (0x02 << 4) +#define HID_LOCALITEM_TAG_DESIG_INDEX (0x03 << 4) +#define HID_LOCALITEM_TAG_DESIG_MIN (0x04 << 4) +#define HID_LOCALITEM_TAG_DESIG_MAX (0x05 << 4) +/* No 6 in spec */ +#define HID_LOCALITEM_TAG_STRING_INDEX (0x07 << 4) +#define HID_LOCALITEM_TAG_STRING_MIN (0x08 << 4) +#define HID_LOCALITEM_TAG_STRING_MAX (0x09 << 4) +#define HID_LOCALITEM_TAG_DELIMITER (0x0a << 4) /* Also listed as reserved in spec! */ + +/* Usage pages (HuT 3) */ +#define HID_USAGE_PAGE_UNDEFINED 0x00 /* Undefined */ +#define HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS 0x01 /* Generic Desktop Controls */ +#define HID_USAGE_PAGE_SIMULATION_CONTROLS 0x02 /* Simulation Controls */ +#define HID_USAGE_PAGE_VR_CONTROLS 0x03 /* VR Controls */ +#define HID_USAGE_PAGE_SPORT_CONTROLS 0x04 /* Sport Controls */ +#define HID_USAGE_PAGE_GAME_CONTROLS 0x05 /* Game Controls */ +#define HID_USAGE_PAGE_GENERIC_DEVICE_CONTROLS 0x06 /* Generic Device Controls */ +#define HID_USAGE_PAGE_KEYBOARD_KEYPAD 0x07 /* Keyboard/Keypad */ +#define HID_USAGE_PAGE_LED 0x08 /* LEDs */ +#define HID_USAGE_PAGE_BUTTON 0x09 /* Button */ +#define HID_USAGE_PAGE_ORDINAL 0x0a /* Ordinal */ +#define HID_USAGE_PAGE_TELEPHONY 0x0b /* Telephony */ +#define HID_USAGE_PAGE_CONSUMER 0x0c /* Consumer */ +#define HID_USAGE_PAGE_DIGITIZER 0x0d /* Digitizer */ +#define HID_USAGE_PAGE_HAPTICS /* 0x0e Reserved */ +#define HID_USAGE_PAGE_PID 0x0f /* PID Page Physical Interface Device */ +#define HID_USAGE_PAGE_UNICODE 0x10 /* Unicode */ +#define HID_USAGE_PAGE_SOC 0x11 /* Sensor Orientation Category */ +#define HID_USAGE_PAGE_EYE_AND_HEAD_TRACKER 0x12 /* Eye and Head Tracker */ + /* 0x13 Reserved */ +#define HID_USAGE_PAGE_ALPHA_DISPLAY 0x14 /* Alphanumeric Display */ + /* 0x15-3f Reserved */ +#define HID_USAGE_PAGE_MEDICAL 0x40 /* Medical Instruments */ +#define HID_USAGE_PAGE_BRAILLE_DISPLAY 0x41 /* Braille Display */ + /* 0x42-0x58 Reserved */ +#define HID_USAGE_PAGE_LIGHTING_AND_ILLUMINATION 0x59 /* Lighting and Illumination */ + /* 0x5a-0x7f Reserved */ +#define HID_USAGE_PAGE_USB_MONITOR 0x80 /* USB Monitor */ +#define HID_USAGE_PAGE_USB_ENUMERATED_VALUES 0x81 /* USB Enumerated Values */ +#define HID_USAGE_PAGE_VESA_VIRTUAL_CONTROLS 0x82 /* VESA Virtual Controls */ +#define HID_USAGE_PAGE_POWER_DEVICE 0x84 /* Power Device */ +#define HID_USAGE_PAGE_BATTERY_SYSTEM 0x85 /* Battery System */ +#define HID_USAGE_PAGE_BARCODE_SCANNER 0x8c /* Bar Code Scanner page */ +#define HID_USAGE_PAGE_SCALE 0x8d /* Scale page */ +#define HID_USAGE_PAGE_MSR 0x8e /* Magnetic Stripe Reading (MSR) Devices */ +#define HID_USAGE_PAGE_POS 0x8f /* Point of Sale devices */ +#define HID_USAGE_PAGE_CAMERA_CONTROL 0x90 /* Camera Control Page */ +#define HID_USAGE_PAGE_ARCADE 0x91 +#define HID_USAGE_PAGE_GAMING_DEVICE 0x92 +#define HID_USAGE_PAGE_FIDO_ALLIANCE 0xF1D0 +#define HID_USAGE_PAGE_VENDOR_PAGE_HBYTE 0xFF00 /* Modifier Keys (HID 8.3) */ #define HID_MODIFIER_LCTRL (1 << 0) /* Left Ctrl */ @@ -205,38 +220,6 @@ #define HID_JS_INPUT_REPORT_BUTTON3 (1 << 6) #define HID_JS_INPUT_REPORT_BUTTON4 (1 << 7) -/* Usage pages (HuT 3) */ -#define HID_USAGE_PAGE_UNDEFINED 0x00 /* Undefined */ -#define HID_USAGE_PAGE_GENERIC_DCTRL 0x01 /* Generic Desktop Controls */ -#define HID_USAGE_PAGE_SIMCTRL 0x02 /* Simulation Controls */ -#define HID_USAGE_PAGE_VRCTRL 0x03 /* VR Controls */ -#define HID_USAGE_PAGE_SPORTCTRL 0x04 /* Sport Controls */ -#define HID_USAGE_PAGE_GAMECTRL 0x05 /* Game Controls */ -#define HID_USAGE_PAGE_GENERIC_DEVCTRL 0x06 /* Generic Device Controls */ -#define HID_USAGE_PAGE_KBD 0x07 /* Keyboard/Keypad */ -#define HID_USAGE_PAGE_LEDS 0x08 /* LEDs */ -#define HID_USAGE_PAGE_BUTTON 0x09 /* Button */ -#define HID_USAGE_PAGE_ORDINAL 0x0a /* Ordinal */ -#define HID_USAGE_PAGE_TELEPHONY 0x0b /* Telephony */ -#define HID_USAGE_PAGE_CONSUMER 0x0c /* Consumer */ -#define HID_USAGE_PAGE_DIGITIZER 0x0d /* Digitizer */ - /* 0x0e Reserved */ -#define HID_USAGE_PAGE_PIDPAGE 0x0f /* PID Page Physical Interface Device */ -#define HID_USAGE_PAGE_UNICODE 0x10 /* Unicode */ - /* 0x11-13 Reserved */ -#define HID_USAGE_PAGE_ALPHA_DISPLAY 0x14 /* Alphanumeric Display */ - /* 0x15-3f Reserved */ -#define HID_USAGE_PAGE_MEDICAL 0x40 /* Medical Instruments */ - /* 0x41-7f Reserved */ - /* 0x80-83 Monitor Devices */ - /* 0x84-87 Power Devices */ - /* 0x88-8b Reserved */ -#define HID_USAGE_PAGE_BARCODE_SCANNER 0x8c /* Bar Code Scanner page */ -#define HID_USAGE_PAGE_SCALE 0x8d /* Scale page */ -#define HID_USAGE_PAGE_MSR 0x8e /* Magnetic Stripe Reading (MSR) Devices */ -#define HID_USAGE_PAGE_POS 0x8f /* Point of Sale devices */ -#define HID_USAGE_PAGE_CAMERA_CTRL 0x90 /* Camera Control Page */ - /* Generic Desktop Page Usage IDs (HuT 4) */ #define HID_DESKTOP_USAGE_UNDEFINED 0x00 /* Undefined */ #define HID_DESKTOP_USAGE_POINTER 0x01 /* Pointer */ diff --git a/class/hid/usbh_hid.c b/class/hid/usbh_hid.c index c4400cac..4fd1a91a 100644 --- a/class/hid/usbh_hid.c +++ b/class/hid/usbh_hid.c @@ -20,7 +20,7 @@ #define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */ #define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */ -USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hid_buf[CONFIG_USBHOST_MAX_HID_CLASS][USB_ALIGN_UP(64, CONFIG_USB_ALIGN_SIZE)]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hid_buf[CONFIG_USBHOST_MAX_HID_CLASS][USB_ALIGN_UP(32, CONFIG_USB_ALIGN_SIZE)]; static struct usbh_hid g_hid_class[CONFIG_USBHOST_MAX_HID_CLASS]; static uint32_t g_devinuse = 0; @@ -173,7 +173,6 @@ int usbh_hid_set_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t int usbh_hid_get_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen) { struct usb_setup_packet *setup; - int ret; if (!hid_class || !hid_class->hport) { return -USB_ERR_INVAL; @@ -186,18 +185,12 @@ int usbh_hid_get_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t setup->wIndex = 0; setup->wLength = buflen; - ret = usbh_control_transfer(hid_class->hport, setup, g_hid_buf[hid_class->minor]); - if (ret < 8) { - return ret; - } - memcpy(buffer, g_hid_buf[hid_class->minor], MIN((uint32_t)ret - 8, buflen)); - return ret; + return usbh_control_transfer(hid_class->hport, setup, buffer); } int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf) { struct usb_endpoint_descriptor *ep_desc; - int ret; uint8_t cur_iface = 0xff; uint8_t *p; bool found = false; @@ -249,23 +242,6 @@ int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf) return -USB_ERR_INVAL; } found: - // /* 0x0 = boot protocol, 0x1 = report protocol */ - // ret = usbh_hid_set_protocol(hid_class, 0x1); - // if (ret < 0) { - // return ret; - // } - - ret = usbh_hid_set_idle(hid_class, 0, 0); - if (ret < 0) { - USB_LOG_WRN("Do not support set idle\r\n"); - } - - /* We read report desc but do nothing (because of too much memory usage for parsing report desc, parsed by users) */ - ret = usbh_hid_get_report_descriptor(hid_class, g_hid_buf[hid_class->minor], MIN(sizeof(g_hid_buf[hid_class->minor]), hid_class->report_size)); - if (ret < 0) { - return ret; - } - for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) { ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc; if (ep_desc->bEndpointAddress & 0x80) { @@ -280,7 +256,7 @@ found: USB_LOG_INFO("Register HID Class:%s\r\n", hport->config.intf[intf].devname); usbh_hid_run(hid_class); - return ret; + return 0; } int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf) @@ -310,6 +286,427 @@ int usbh_hid_disconnect(struct usbh_hubport *hport, uint8_t intf) return ret; } +static uint32_t hid_get_itemval(const uint8_t *data, unsigned int idx, unsigned int size) +{ + uint32_t value = 0; + + for (unsigned int i = 1; i <= size; i++) + value |= data[idx + i] << (8 * (i - 1)); + + return value; +} + +struct hid_report *usbh_hid_report_parse(const uint8_t *data, uint32_t report_len, uint32_t max_usages) +{ + uint32_t i = 0; + uint32_t itemtag, itemtype, itemsize; + uint32_t itemval; + struct hid_report_field field; + uint32_t usage_page = 0, usage = 0, usage_min = 0, usage_max = 0, flags = 0; + uint32_t *usages; + struct hid_report *hid_report; + + hid_report = usb_osal_malloc(sizeof(struct hid_report)); + if (!hid_report) { + USB_LOG_ERR("hid report malloc failed\r\n"); + return NULL; + } + + usages = usb_osal_malloc(sizeof(uint32_t) * max_usages); + if (!usages) { + USB_LOG_ERR("hid usages malloc failed\r\n"); + goto err; + } + + memset(hid_report, 0, sizeof(struct hid_report)); + memset(&field, 0, sizeof(struct hid_report_field)); + + while (i < report_len) { + itemtag = data[i] & HID_TAG_MASK; + itemtype = data[i] & HID_TYPE_MASK; + itemsize = data[i] & HID_SIZE_MASK; + + if (itemsize == 3) /* HID spec: 6.2.2.2 - Short Items */ + itemsize = 4; + + itemval = hid_get_itemval(data, i, itemsize); + + USB_LOG_DBG("itemtype 0x%02x, itemtag 0x%02x, itemsize %d, itemval 0x%08x\r\n", + itemtype, itemtag, itemsize, itemval); + + switch (itemtype) { + case HID_ITEMTYPE_MAIN: + switch (itemtag) { + case HID_MAINITEM_TAG_INPUT: + if ((flags & HID_REPORT_FLAG_REQUIRED_MASK) != HID_REPORT_FLAG_REQUIRED_MASK) + goto err; + + if (hid_report->input_count >= CONFIG_USBHOST_HID_MAX_INPUT) { + USB_LOG_ERR("hid input fields exceed max limit\r\n"); + goto err; + } + + field.flags = flags; + field.properties = itemval; + field.usage_page = usage_page; + memcpy(&hid_report->input_fields[hid_report->input_count], &field, sizeof(struct hid_report_field)); + if (field.usage_count > 0) { + hid_report->input_fields[hid_report->input_count].usages = usb_osal_malloc(sizeof(uint32_t) * field.usage_count); + if (!hid_report->input_fields[hid_report->input_count].usages) { + USB_LOG_ERR("hid input usages malloc failed\r\n"); + goto err; + } + memcpy(hid_report->input_fields[hid_report->input_count].usages, usages, sizeof(uint32_t) * field.usage_count); + } + + hid_report->input_count++; + + /* only keep the global items */ + flags &= HID_REPORT_FLAG_GLOBAL_MASK; + memset(&field, 0, sizeof(struct hid_report_field)); + break; + case HID_MAINITEM_TAG_OUTPUT: + if ((flags & HID_REPORT_FLAG_REQUIRED_MASK) != HID_REPORT_FLAG_REQUIRED_MASK) + goto err; + + if (hid_report->output_count >= CONFIG_USBHOST_HID_MAX_OUTPUT) { + USB_LOG_ERR("hid output fields exceed max limit\r\n"); + goto err; + } + + field.flags = flags; + field.properties = itemval; + field.usage_page = usage_page; + memcpy(&hid_report->output_fields[hid_report->output_count], &field, sizeof(struct hid_report_field)); + if (field.usage_count > 0) { + hid_report->output_fields[hid_report->output_count].usages = usb_osal_malloc(sizeof(uint32_t) * field.usage_count); + if (!hid_report->output_fields[hid_report->output_count].usages) { + USB_LOG_ERR("hid output usages malloc failed\r\n"); + goto err; + } + memcpy(hid_report->output_fields[hid_report->output_count].usages, usages, sizeof(uint32_t) * field.usage_count); + } + + hid_report->output_count++; + + /* only keep the global items */ + flags &= HID_REPORT_FLAG_GLOBAL_MASK; + memset(&field, 0, sizeof(struct hid_report_field)); + break; + case HID_MAINITEM_TAG_COLLECTION: + memset(&field, 0, sizeof(struct hid_report_field)); + break; + case HID_MAINITEM_TAG_FEATURE: + + if (hid_report->feature_count >= CONFIG_USBHOST_HID_MAX_FEATURE) { + USB_LOG_ERR("hid feature fields exceed max limit\r\n"); + goto err; + } + + field.flags = flags; + field.properties = itemval; + field.usage_page = usage_page; + memcpy(&hid_report->feature_fields[hid_report->feature_count], &field, sizeof(struct hid_report_field)); + if (field.usage_count > 0) { + hid_report->feature_fields[hid_report->feature_count].usages = usb_osal_malloc(sizeof(uint32_t) * field.usage_count); + if (!hid_report->feature_fields[hid_report->feature_count].usages) { + USB_LOG_ERR("hid feature usages malloc failed\r\n"); + goto err; + } + memcpy(hid_report->feature_fields[hid_report->feature_count].usages, usages, sizeof(uint32_t) * field.usage_count); + } + + hid_report->feature_count++; + + memset(&field, 0, sizeof(struct hid_report_field)); + + break; + case HID_MAINITEM_TAG_ENDCOLLECTION: + break; + default: + goto err; + } + break; + case HID_ITEMTYPE_GLOBAL: + switch (itemtag) { + case HID_GLOBALITEM_TAG_USAGE_PAGE: + usage_page = itemval; + + if (usage_page > UINT16_MAX) + goto err; + + flags |= HID_REPORT_FLAG_USAGE_PAGE; + break; + case HID_GLOBALITEM_TAG_LOGICAL_MIN: + field.logical_min = (int32_t)itemval; + flags |= HID_REPORT_FLAG_LOGICAL_MIN; + break; + case HID_GLOBALITEM_TAG_LOGICAL_MAX: + field.logical_max = (int32_t)itemval; + flags |= HID_REPORT_FLAG_LOGICAL_MAX; + break; + case HID_GLOBALITEM_TAG_REPORT_SIZE: + field.report_size = itemval; + flags |= HID_REPORT_FLAG_REPORT_SIZE; + break; + case HID_GLOBALITEM_TAG_REPORT_COUNT: + field.report_count = itemval; + flags |= HID_REPORT_FLAG_REPORT_COUNT; + break; + case HID_GLOBALITEM_TAG_REPORT_ID: + hid_report->uses_report_id = true; + field.report_id = itemval; + flags |= HID_REPORT_FLAG_REPORT_ID; + break; + default: + goto err; + } + break; + case HID_ITEMTYPE_LOCAL: + switch (itemtag) { + case HID_LOCALITEM_TAG_USAGE: + usage = itemval; + /* Extended usage (size 4) combines both usage page and id */ + if (itemsize != 4) { + if (!(flags & HID_REPORT_FLAG_USAGE_PAGE)) + goto err; + usage |= usage_page << 16; + } + + usages[field.usage_count++] = usage; + + break; + case HID_LOCALITEM_TAG_USAGE_MIN: + usage_min = itemval; + if (itemsize == 4) { + /* Usage max must be extended as well */ + flags |= HID_REPORT_FLAG_EXTENDED_USAGE; + } else { + if (!(flags & HID_REPORT_FLAG_USAGE_PAGE)) + goto err; + usage_min |= usage_page << 16; + } + field.usage_min = usage_min; + flags |= HID_REPORT_FLAG_USAGE_MIN; + break; + case HID_LOCALITEM_TAG_USAGE_MAX: + if (!(flags & HID_REPORT_FLAG_USAGE_MIN)) + goto err; + + usage_max = itemval; + if (flags & HID_REPORT_FLAG_EXTENDED_USAGE) { + /* Fail if max is not extended usage (HID spec 6.2.2.8) */ + if (itemsize != 4) + goto err; + } else if (itemsize == 4) { + /* Fail because min wasn't extended, but max is */ + goto err; + } else { + if (!(flags & HID_REPORT_FLAG_USAGE_PAGE)) + goto err; + usage_max |= usage_page << 16; + } + + /* Usage min and max must be on the same page */ + if (USAGE_PAGE(usage_min) != USAGE_PAGE(usage_max)) { + goto err; + } + + if (usage_min > usage_max) { + goto err; + } + + for (uint32_t j = usage_min; j <= usage_max; j++) { + usages[field.usage_count++] = j; + } + + field.usage_max = usage_max; + flags |= HID_REPORT_FLAG_USAGE_MAX; + flags &= ~(HID_REPORT_FLAG_USAGE_MIN | HID_REPORT_FLAG_EXTENDED_USAGE); + break; + default: + goto err; + } + break; + default: + goto err; + } + + i += (1 + itemsize); + } + usb_osal_free(usages); + return hid_report; +err: + if (hid_report) { + usb_osal_free(hid_report); + + for (uint32_t j = 0; j < hid_report->input_count; j++) + usb_osal_free(hid_report->input_fields[j].usages); + + for (uint32_t j = 0; j < hid_report->output_count; j++) + usb_osal_free(hid_report->output_fields[j].usages); + + for (uint32_t j = 0; j < hid_report->feature_count; j++) + usb_osal_free(hid_report->feature_fields[j].usages); + } + + if (usages) + usb_osal_free(usages); + return NULL; +} + +void usbh_hid_report_free(struct hid_report *hid_report) +{ + if (hid_report) { + for (uint32_t j = 0; j < hid_report->input_count; j++) + usb_osal_free(hid_report->input_fields[j].usages); + + for (uint32_t j = 0; j < hid_report->output_count; j++) + usb_osal_free(hid_report->output_fields[j].usages); + + for (uint32_t j = 0; j < hid_report->feature_count; j++) + usb_osal_free(hid_report->feature_fields[j].usages); + + usb_osal_free(hid_report); + } +} + +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hid_report_buf[2048]; + +static const char *hid_property_string(uint32_t value) +{ + uint32_t off = 0; + static char buffer[160]; + + memset(buffer, 0, sizeof(buffer)); + + if (value & HID_MAINITEM_CONSTANT) + off += snprintf(buffer + off, sizeof(buffer) - off, "Constant, "); + else + off += snprintf(buffer + off, sizeof(buffer) - off, "Data, "); + + if (value & HID_MAINITEM_VARIABLE) + off += snprintf(buffer + off, sizeof(buffer) - off, "Variable, "); + else + off += snprintf(buffer + off, sizeof(buffer) - off, "Array, "); + + if (value & HID_MAINITEM_RELATIVE) + off += snprintf(buffer + off, sizeof(buffer) - off, "Relative, "); + else + off += snprintf(buffer + off, sizeof(buffer) - off, "Absolute, "); + + if (value & HID_MAINITEM_WRAP) + off += snprintf(buffer + off, sizeof(buffer) - off, "Wrap, "); + else + off += snprintf(buffer + off, sizeof(buffer) - off, "NoWrap, "); + + if (value & HID_MAINITEM_NONLINEAR) + off += snprintf(buffer + off, sizeof(buffer) - off, "NonLinear, "); + else + off += snprintf(buffer + off, sizeof(buffer) - off, "Linear, "); + + if (value & HID_MAINITEM_NOPREFERRED) + off += snprintf(buffer + off, sizeof(buffer) - off, "NoPreferred, "); + else + off += snprintf(buffer + off, sizeof(buffer) - off, "Preferred, "); + + if (value & HID_MAINITEM_NULLSTATE) + off += snprintf(buffer + off, sizeof(buffer) - off, "NullState, "); + else + off += snprintf(buffer + off, sizeof(buffer) - off, "NoNullState, "); + + if (value & HID_MAINITEM_VOLATILE) + off += snprintf(buffer + off, sizeof(buffer) - off, "Volatile, "); + else + off += snprintf(buffer + off, sizeof(buffer) - off, "NonVolatile, "); + + if (value & HID_MAINITEM_BUFFEREDBYTES) + off += snprintf(buffer + off, sizeof(buffer) - off, "BufferedBytes"); + else + off += snprintf(buffer + off, sizeof(buffer) - off, "BitField"); + + return buffer; + +} + +static void usbh_hid_field_info_print(uint32_t idx, struct hid_report_field *field) +{ + USB_LOG_RAW(" Field %u:\r\n", idx); + USB_LOG_RAW(" Usage Page: 0x%04x\r\n", (unsigned int)field->usage_page); + USB_LOG_RAW(" Report ID: %u\r\n", (unsigned int)field->report_id); + USB_LOG_RAW(" Report Size: %ubit\r\n", (unsigned int)field->report_size); + USB_LOG_RAW(" Report Count: %u\r\n", (unsigned int)field->report_count); + USB_LOG_RAW(" Logical Min: %d\r\n", field->logical_min); + USB_LOG_RAW(" Logical Max: %d\r\n", field->logical_max); + USB_LOG_RAW(" Usage Count: %u\r\n", (unsigned int)field->usage_count); + if (field->usage_count > 0) { + if (field->usage_count == 1) { + USB_LOG_RAW(" Usage: 0x%04x\r\n", USAGE_ID(field->usages[0])); + } else { + USB_LOG_RAW(" Usages(0x%04x ~ 0x%04x)\r\n", USAGE_ID(field->usage_min), USAGE_ID(field->usage_max)); + } + } + USB_LOG_RAW(" Flags: 0x%04x\r\n", (unsigned int)field->flags); + USB_LOG_RAW(" Properties: 0x%04x(%s)\r\n", (unsigned int)field->properties, hid_property_string(field->properties)); +} + +int lshid(int argc, char **argv) +{ + struct usbh_hid *hid_class; + struct hid_report *hid_report; + int ret; + + if (argc < 2) { + USB_LOG_ERR("please input correct command: lshid path\r\n"); + return -1; + } + + hid_class = usbh_find_class_instance(argv[1]); + if (!hid_class) { + USB_LOG_ERR("cannot find hid device\r\n"); + return -1; + } + + if (hid_class->report_size > sizeof(g_hid_report_buf)) { + USB_LOG_ERR("hid report buffer is too small\r\n"); + return -1; + } + + ret = usbh_hid_get_report_descriptor(hid_class, g_hid_report_buf, hid_class->report_size); + if (ret < 0) { + USB_LOG_ERR("get hid report descriptor failed, errcode: %d\r\n", ret); + return -1; + } + + hid_report = usbh_hid_report_parse(g_hid_report_buf, hid_class->report_size, 1024); + if (hid_report) { + USB_LOG_RAW("HID report parsed successfully\r\n"); + + USB_LOG_RAW("Input fields: %u\r\n", (unsigned int)hid_report->input_count); + for (uint32_t i = 0; i < hid_report->input_count; i++) { + struct hid_report_field *field = &hid_report->input_fields[i]; + usbh_hid_field_info_print(i, field); + } + + USB_LOG_RAW("Output fields: %u\r\n", (unsigned int)hid_report->output_count); + for (uint32_t i = 0; i < hid_report->output_count; i++) { + struct hid_report_field *field = &hid_report->output_fields[i]; + usbh_hid_field_info_print(i, field); + } + + USB_LOG_RAW("Feature fields: %u\r\n", (unsigned int)hid_report->feature_count); + for (uint32_t i = 0; i < hid_report->feature_count; i++) { + struct hid_report_field *field = &hid_report->feature_fields[i]; + usbh_hid_field_info_print(i, field); + } + + usbh_hid_report_free(hid_report); + } else { + USB_LOG_ERR("HID report parsed failed\r\n"); + } + return 0; +} + __WEAK void usbh_hid_run(struct usbh_hid *hid_class) { (void)hid_class; diff --git a/class/hid/usbh_hid.h b/class/hid/usbh_hid.h index 8b87e0c0..fa5c7663 100644 --- a/class/hid/usbh_hid.h +++ b/class/hid/usbh_hid.h @@ -8,6 +8,82 @@ #include "usb_hid.h" +/* local items */ +#define HID_REPORT_FLAG_USAGE_MIN (1 << 0) +#define HID_REPORT_FLAG_USAGE_MAX (1 << 1) + +/* global items */ +#define HID_REPORT_FLAG_REPORT_ID (1 << 2) +#define HID_REPORT_FLAG_REPORT_COUNT (1 << 3) +#define HID_REPORT_FLAG_REPORT_SIZE (1 << 4) +#define HID_REPORT_FLAG_LOGICAL_MIN (1 << 5) +#define HID_REPORT_FLAG_LOGICAL_MAX (1 << 6) +#define HID_REPORT_FLAG_USAGE_PAGE (1 << 7) + +/* main items */ +#define HID_REPORT_FLAG_INPUT (1 << 8) +#define HID_REPORT_FLAG_OUTPUT (1 << 9) +#define HID_REPORT_FLAG_FEATURE (1 << 10) + +#define HID_REPORT_FLAG_EXTENDED_USAGE (1 << 11) + +/* masks */ + +#define HID_REPORT_FLAG_GLOBAL_MASK (HID_REPORT_FLAG_REPORT_ID | \ + HID_REPORT_FLAG_REPORT_COUNT | \ + HID_REPORT_FLAG_REPORT_SIZE | \ + HID_REPORT_FLAG_LOGICAL_MIN | \ + HID_REPORT_FLAG_LOGICAL_MAX | \ + HID_REPORT_FLAG_USAGE_PAGE) + +#define HID_REPORT_FLAG_REQUIRED_MASK (HID_REPORT_FLAG_REPORT_COUNT | \ + HID_REPORT_FLAG_REPORT_SIZE | \ + HID_REPORT_FLAG_LOGICAL_MIN | \ + HID_REPORT_FLAG_LOGICAL_MAX) + +#define USAGE_ID(usage) (usage & 0x0000FFFF) +#define USAGE_PAGE(usage) ((usage & 0xFFFF0000) >> 16) + +#ifndef CONFIG_USBHOST_HID_MAX_INPUT +#define CONFIG_USBHOST_HID_MAX_INPUT 16 +#endif + +#ifndef CONFIG_USBHOST_HID_MAX_OUTPUT +#define CONFIG_USBHOST_HID_MAX_OUTPUT 16 +#endif + +#ifndef CONFIG_USBHOST_HID_MAX_FEATURE +#define CONFIG_USBHOST_HID_MAX_FEATURE 16 +#endif + +struct hid_report_field { + uint32_t *usages; /* usage page + usage */ + uint32_t usage_count; + uint32_t usage_page; + + uint32_t report_id; /* optional */ + uint32_t report_count; + uint32_t report_size; + int32_t logical_min; + int32_t logical_max; + uint32_t properties; + + uint32_t usage_min; + uint32_t usage_max; + + uint32_t flags; +}; + +struct hid_report { + bool uses_report_id; + uint32_t input_count; + struct hid_report_field input_fields[CONFIG_USBHOST_HID_MAX_INPUT]; + uint32_t output_count; + struct hid_report_field output_fields[CONFIG_USBHOST_HID_MAX_OUTPUT]; + uint32_t feature_count; + struct hid_report_field feature_fields[CONFIG_USBHOST_HID_MAX_FEATURE]; +}; + struct usbh_hid { struct usbh_hubport *hport; struct usb_endpoint_descriptor *intin; /* INTR IN endpoint */ @@ -36,9 +112,14 @@ int usbh_hid_get_protocol(struct usbh_hid *hid_class, uint8_t *protocol); int usbh_hid_set_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen); int usbh_hid_get_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen); +struct hid_report *usbh_hid_report_parse(const uint8_t *data, uint32_t report_len, uint32_t max_usages); +void usbh_hid_report_free(struct hid_report *hid_report); + void usbh_hid_run(struct usbh_hid *hid_class); void usbh_hid_stop(struct usbh_hid *hid_class); +int lshid(int argc, char **argv); + #ifdef __cplusplus } #endif