From 201b3ebb2c2e61c59fe940ce114884e5dc42bfa9 Mon Sep 17 00:00:00 2001 From: sakimisu <1203593632@qq.com> Date: Sun, 29 Jan 2023 16:15:28 +0800 Subject: [PATCH] add webusb support --- common/usb_def.h | 6 ++++ core/usbd_core.c | 13 ++++++++ core/usbd_core.h | 1 + demo/webusb_template.c | 71 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 demo/webusb_template.c diff --git a/common/usb_def.h b/common/usb_def.h index fdb17fb6..b48c9464 100644 --- a/common/usb_def.h +++ b/common/usb_def.h @@ -607,6 +607,12 @@ struct usb_webusb_url_descriptor { char URL[]; } __PACKED; +struct usb_webusb_url_ex_descriptor { + uint8_t vendor_code; + uint8_t *string; + uint32_t string_len; +} __PACKED; + struct usb_bos_descriptor { uint8_t *string; uint32_t string_len; diff --git a/core/usbd_core.c b/core/usbd_core.c index cbe7a97b..e2ced33c 100644 --- a/core/usbd_core.c +++ b/core/usbd_core.c @@ -821,6 +821,19 @@ static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t * return -1; } } + } else if (usbd_core_cfg.descriptors->webusb_url_descriptor) { + if (setup->bRequest == usbd_core_cfg.descriptors->webusb_url_descriptor->vendor_code) { + switch (setup->wIndex) { + case WINUSB_REQUEST_GET_DESCRIPTOR_SET: + USB_LOG_INFO("GET Webusb url Descriptor\r\n"); + *data = (uint8_t *)usbd_core_cfg.descriptors->webusb_url_descriptor->string; + *len = usbd_core_cfg.descriptors->webusb_url_descriptor->string_len; + return 0; + default: + USB_LOG_ERR("unknown vendor code\r\n"); + return -1; + } + } } #else if (msosv1_desc) { diff --git a/core/usbd_core.h b/core/usbd_core.h index 8d7d10c6..1c978d19 100644 --- a/core/usbd_core.h +++ b/core/usbd_core.h @@ -72,6 +72,7 @@ struct usb_descriptor { const char **string_descriptor; struct usb_msosv1_descriptor *msosv1_descriptor; struct usb_msosv2_descriptor *msosv2_descriptor; + struct usb_webusb_url_ex_descriptor *webusb_url_descriptor; struct usb_bos_descriptor *bos_descriptor; }; diff --git a/demo/webusb_template.c b/demo/webusb_template.c new file mode 100644 index 00000000..fe9dc8ca --- /dev/null +++ b/demo/webusb_template.c @@ -0,0 +1,71 @@ +#include "usbd_core.h" + +#define MS_OS_20_DESCRIPTOR_LENGTH (0xB2) + +#define WEBUSB_URL_STRINGS \ + 'd', 'e', 'v', 'a', 'n', 'l', 'a', 'i', '.', 'g', 'i', 't', 'h', 'u', 'b', '.', 'i', 'o', '/', 'w', 'e', 'b', 'd', 'f', 'u', '/', 'd', 'f', 'u', '-', 'u', 't', 'i', 'l' + +#define WL_REQUEST_WEBUSB (0x22) +#define WL_REQUEST_WINUSB (0x21) + +#define URL_DESCRIPTOR_LENGTH 0x2C + +// 描述符集信息 +const uint8_t MS_OS_20_DESCRIPTOR_SET[MS_OS_20_DESCRIPTOR_LENGTH] = { + // Microsoft OS 2.0 描述符集标头 + 0x0A, 0x00, // Descriptor size (10 bytes) + 0x00, 0x00, // MS OS 2.0 descriptor set header + 0x00, 0x00, 0x03, 0x06, // Windows version (8.1) (0x06030000) + MS_OS_20_DESCRIPTOR_LENGTH, 0x00, // Size, MS OS 2.0 descriptor set + + // Microsoft OS 2.0 配置子集标头 + 0x08, 0x00, // wLength + 0x01, 0x00, // wDescriptorType + 0x00, // 适用于配置 1 + 0x00, // bReserved + 0XA8, 0X00, // Size, MS OS 2.0 configuration subset + + // Microsoft OS 2.0 功能子集头 + 0x08, 0x00, // Descriptor size (8 bytes) + 0x02, 0x00, // MS OS 2.0 function subset header + 0x01, // 第2个接口 + 0x00, // 必须设置为 0 + 0xA0, 0x00, + + // Microsoft OS 2.0 兼容 ID 描述符 + // 兼容 ID 描述符告诉 Windows 此设备与 WinUSB 驱动程序兼容 + 0x14, 0x00, // wLength 20 + 0x03, 0x00, // MS_OS_20_FEATURE_COMPATIBLE_ID + 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + + // Microsoft OS 2.0 注册表属性描述符 + // 注册表属性分配设备接口 GUID + 0x84, 0x00, //wLength: 132 + 0x04, 0x00, // wDescriptorType: MS_OS_20_FEATURE_REG_PROPERTY: 0x04 (Table 9) + 0x07, 0x00, //wPropertyDataType: REG_MULTI_SZ (Table 15) + 0x2a, 0x00, //wPropertyNameLength: + //bPropertyName: “DeviceInterfaceGUID” + 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, 't', 0x00, 'e', 0x00, + 'r', 0x00, 'f', 0x00, 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, 'U', 0x00, 'I', 0x00, 'D', 0x00, 's', 0x00, + 0x00, 0x00, + 0x50, 0x00, // wPropertyDataLength + //bPropertyData: “{975F44D9-0D08-43FD-8B3E-127CA8AFFF9D}”. + '{', 0x00, '9', 0x00, 'd', 0x00, '7', 0x00, 'd', 0x00, 'e', 0x00, 'b', 0x00, 'b', 0x00, 'c', 0x00, '-', 0x00, + 'c', 0x00, '8', 0x00, '5', 0x00, 'd', 0x00, '-', 0x00, '1', 0x00, '1', 0x00, 'd', 0x00, '1', 0x00, '-', 0x00, + '9', 0x00, 'e', 0x00, 'b', 0x00, '4', 0x00, '-', 0x00, '0', 0x00, '0', 0x00, '6', 0x00, '0', 0x00, '0', 0x00, + '8', 0x00, 'c', 0x00, '3', 0x00, 'a', 0x00, '1', 0x00, '9', 0x00, 'a', 0x00, '}', 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +const uint8_t USBD_WebUSBURLDescriptor[URL_DESCRIPTOR_LENGTH] = { + URL_DESCRIPTOR_LENGTH, + WEBUSB_URL_TYPE, + WEBUSB_URL_SCHEME_HTTPS, + WEBUSB_URL_STRINGS +}; + +struct usb_webusb_url_ex_descriptor webusb_url_desc = { + .vendor_code = WL_REQUEST_WEBUSB, + .string = MS_OS_20_DESCRIPTOR_SET, + .string_len = MS_OS_20_DESCRIPTOR_LENGTH +}; \ No newline at end of file