diff --git a/components/usb_stack/CMakeLists.txt b/components/usb_stack/CMakeLists.txt index 7b630648..b140a927 100644 --- a/components/usb_stack/CMakeLists.txt +++ b/components/usb_stack/CMakeLists.txt @@ -29,9 +29,7 @@ list(APPEND ADD_SRCS ${sources}) ####################################################### ############ Add static libs ########################## -# if(CONFIG_COMPONENT1_INCLUDE_STATIC_LIB) -# list(APPEND ADD_STATIC_LIB "libxxx.a") -# endif() +#list(APPEND ADD_STATIC_LIB "libxxx.a") ####################################################### ############ Add dynamic libs ######################### @@ -41,12 +39,14 @@ list(APPEND ADD_SRCS ${sources}) ############ Add global compile option ################ #add components denpend on this component -# list(APPEND ADD_DEFINITIONS -Dxxx) +if(CONFIG_USB_HS) +list(APPEND ADD_DEFINITIONS -DCONFIG_USB_HS) +endif() ####################################################### ############ Add private compile option ################ #add compile option for this component that won't affect other modules -# list(APPEND ADD_DEFINITIONS_PRIVATE -Dxxx) +# list(APPEND ADD_PRIVATE_DEFINITIONS -Dxxx) ####################################################### generate_library() diff --git a/components/usb_stack/class/cdc/usbd_cdc.h b/components/usb_stack/class/cdc/usbd_cdc.h index 335cd323..1441fd4c 100644 --- a/components/usb_stack/class/cdc/usbd_cdc.h +++ b/components/usb_stack/class/cdc/usbd_cdc.h @@ -296,6 +296,7 @@ struct cdc_ecm_descriptor { /*Length of template descriptor: 66 bytes*/ #define CDC_ACM_DESCRIPTOR_LEN (8 + 9 + 5 + 5 + 4 + 5 + 7 + 9 + 7 + 7) // clang-format off +#ifndef CONFIG_USB_HS #define CDC_ACM_DESCRIPTOR_INIT(bFirstInterface, int_ep, out_ep, in_ep, str_idx) \ /* Interface Associate */ \ 0x08, /* bLength */ \ @@ -360,9 +361,9 @@ struct cdc_ecm_descriptor { 0x02, /* bmAttributes */ \ 0x40, 0x00, /* wMaxPacketSize */ \ 0x01 /* bInterval */ - -#define CDC_ACM_HS_DESCRIPTOR_INIT(bFirstInterface, int_ep, out_ep, in_ep, str_idx) \ - /* Interface Associate */ \ +#else +#define CDC_ACM_DESCRIPTOR_INIT(bFirstInterface, int_ep, out_ep, in_ep, str_idx) \ + /* Interface Associate */ \ 0x08, /* bLength */ \ USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION, /* bDescriptorType */ \ bFirstInterface, /* bFirstInterface */ \ @@ -425,6 +426,7 @@ struct cdc_ecm_descriptor { 0x02, /* bmAttributes */ \ 0x02, 0x00, /* wMaxPacketSize */ \ 0x01 /* bInterval */ +#endif // clang-format on void usbd_cdc_add_acm_interface(usbd_class_t *class, usbd_interface_t *intf); diff --git a/components/usb_stack/class/hid/usbd_hid.h b/components/usb_stack/class/hid/usbd_hid.h index 03d4a88c..9319e7f2 100644 --- a/components/usb_stack/class/hid/usbd_hid.h +++ b/components/usb_stack/class/hid/usbd_hid.h @@ -12,6 +12,13 @@ extern "C" { #endif +/* HID Protocol Codes */ +#define HID_PROTOCOL_NONE 0x00 +#define HID_PROTOCOL_BOOT 0x00 +#define HID_PROTOCOL_KEYBOARD 0x01 +#define HID_PROTOCOL_REPORT 0x01 +#define HID_PROTOCOL_MOUSE 0x02 + /* HID Class Descriptor Types */ #define HID_DESCRIPTOR_TYPE_HID 0x21 #define HID_DESCRIPTOR_TYPE_HID_REPORT 0x22 @@ -25,6 +32,11 @@ extern "C" { #define HID_REQUEST_SET_IDLE 0x0A #define HID_REQUEST_SET_PROTOCOL 0x0B +/* HID Report Types */ +#define HID_REPORT_INPUT 0x01 +#define HID_REPORT_OUTPUT 0x02 +#define HID_REPORT_FEATURE 0x03 + /* HID Report Definitions */ struct usb_hid_class_subdescriptor { uint8_t bDescriptorType; @@ -122,10 +134,6 @@ struct usb_hid_descriptor { #define COLLECTION_PHYSICAL 0x00 #define COLLECTION_APPLICATION 0x01 -/* Protocols */ -#define HID_PROTOCOL_BOOT 0x00 -#define HID_PROTOCOL_REPORT 0x01 - /* Example HID report descriptors */ /** * @brief Simple HID mouse report descriptor for n button mouse. diff --git a/components/usb_stack/class/msc/usbd_msc.c b/components/usb_stack/class/msc/usbd_msc.c index 086d47f5..b43d1ba7 100644 --- a/components/usb_stack/class/msc/usbd_msc.c +++ b/components/usb_stack/class/msc/usbd_msc.c @@ -25,8 +25,11 @@ #include "usbd_msc.h" /* max USB packet size */ +#ifndef CONFIG_USB_HS #define MASS_STORAGE_BULK_EP_MPS 64 -#define MASS_STORAGE_BLOCK_SIZE 512 +#else +#define MASS_STORAGE_BULK_EP_MPS 512 +#endif #define MSD_OUT_EP_IDX 0 #define MSD_IN_EP_IDX 1 @@ -60,7 +63,7 @@ struct usbd_msc_cfg_private { uint32_t scsi_blk_addr; uint32_t scsi_blk_len; - uint8_t block_buffer[MASS_STORAGE_BLOCK_SIZE]; + uint8_t *block_buffer; } usbd_msc_cfg; @@ -76,6 +79,11 @@ static void usbd_msc_reset(void) usbd_msc_cfg.scsi_blk_len = 0U; usbd_msc_get_cap(0, &usbd_msc_cfg.scsi_blk_nbr, &usbd_msc_cfg.scsi_blk_size); usbd_msc_cfg.max_lun_count = 0; + + if (usbd_msc_cfg.block_buffer) { + free(usbd_msc_cfg.block_buffer); + } + usbd_msc_cfg.block_buffer = malloc(usbd_msc_cfg.scsi_blk_size * sizeof(uint8_t)); } /** @@ -138,7 +146,6 @@ static bool usbd_msc_datain_check(void) { if (!usbd_msc_cfg.cbw.DataLength) { USBD_LOG_WRN("Zero length in CBW"); - //SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); usbd_msc_cfg.csw.Status = CSW_STATUS_PHASE_ERROR; usbd_msc_send_csw(); return false; @@ -478,11 +485,10 @@ static bool usbd_msc_read_write_process(void) { /* Logical Block Address of First Block */ uint32_t lba; - uint32_t len = 0; + uint32_t blk_num = 0; if (!usbd_msc_cfg.cbw.DataLength) { USBD_LOG_WRN("Zero length in CBW\r\n"); - //SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); usbd_msc_cfg.csw.Status = CSW_STATUS_PHASE_ERROR; usbd_msc_send_csw(); return false; @@ -498,22 +504,22 @@ static bool usbd_msc_read_write_process(void) case SCSI_READ10: case SCSI_WRITE10: case SCSI_VERIFY10: - len = GET_BE16(&usbd_msc_cfg.cbw.CB[7]); + blk_num = GET_BE16(&usbd_msc_cfg.cbw.CB[7]); break; case SCSI_READ12: case SCSI_WRITE12: - len = GET_BE32(&usbd_msc_cfg.cbw.CB[6]); + blk_num = GET_BE32(&usbd_msc_cfg.cbw.CB[6]); break; default: break; } - USBD_LOG_DBG("len (block) : 0x%x\r\n", len); - usbd_msc_cfg.scsi_blk_len = len * usbd_msc_cfg.scsi_blk_size; + USBD_LOG_DBG("num (block) : 0x%x\r\n", blk_num); + usbd_msc_cfg.scsi_blk_len = blk_num * usbd_msc_cfg.scsi_blk_size; - if ((lba + len) > usbd_msc_cfg.scsi_blk_nbr) { + if ((lba + blk_num) > usbd_msc_cfg.scsi_blk_nbr) { USBD_LOG_ERR("LBA out of range\r\n"); usbd_msc_cfg.csw.Status = CSW_STATUS_CMD_FAILED; usbd_msc_send_csw(); diff --git a/components/usb_stack/class/msc/usbd_msc.h b/components/usb_stack/class/msc/usbd_msc.h index f1009631..1d198e79 100644 --- a/components/usb_stack/class/msc/usbd_msc.h +++ b/components/usb_stack/class/msc/usbd_msc.h @@ -65,6 +65,7 @@ struct CSW { /*Length of template descriptor: 23 bytes*/ #define MSC_DESCRIPTOR_LEN (9 + 7 + 7) // clang-format off +#ifndef SUPPORT_USB_HS #define MSC_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep,str_idx) \ /* Interface */ \ 0x09, /* bLength */ \ @@ -88,9 +89,9 @@ struct CSW { 0x02, /* bmAttributes */ \ 0x40, 0x00, /* wMaxPacketSize */ \ 0x01 /* bInterval */ - -#define MSC_HS_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep,str_idx) \ - /* Interface */ \ +#else +#define MSC_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep,str_idx) \ + /* Interface */ \ 0x09, /* bLength */ \ USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \ bFirstInterface, /* bInterfaceNumber */ \ @@ -112,6 +113,7 @@ struct CSW { 0x02, /* bmAttributes */ \ 0x02, 0x00, /* wMaxPacketSize */ \ 0x01 /* bInterval */ +#endif // clang-format on void usbd_msc_class_init(uint8_t out_ep, uint8_t in_ep); diff --git a/components/usb_stack/class/webusb/usbd_webusb.h b/components/usb_stack/class/webusb/usbd_webusb.h index 6274f496..d94d1702 100644 --- a/components/usb_stack/class/webusb/usbd_webusb.h +++ b/components/usb_stack/class/webusb/usbd_webusb.h @@ -19,19 +19,4 @@ #define WEBUSB_CONFIGURATION_SUBSET_HEADER_SIZE 4 #define WEBUSB_FUNCTION_SUBSET_HEADER_SIZE 3 -/* BOS Capability webusb */ -struct usb_bos_webusb_platform_capability_descriptor { - struct usb_bos_capability_descriptor webusb_platform; - uint16_t bcdVersion; - uint8_t bVendorCode; - uint8_t iLandingPage; -} __packed; - -struct webusb_url_descriptor { - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bScheme; - char URL[]; -} __packed; - #endif \ No newline at end of file diff --git a/components/usb_stack/class/winusb/usbd_winusb.h b/components/usb_stack/class/winusb/usbd_winusb.h index 5e76bac8..dc4a51dd 100644 --- a/components/usb_stack/class/winusb/usbd_winusb.h +++ b/components/usb_stack/class/winusb/usbd_winusb.h @@ -23,13 +23,4 @@ #define WINUSB_PROP_DATA_TYPE_REG_SZ 0x01 #define WINUSB_PROP_DATA_TYPE_REG_MULTI_SZ 0x07 -/* WinUSB Microsoft OS 2.0 descriptor Platform Capability Descriptor */ -struct usb_bos_winusb_platform_capability_descriptor { - struct usb_bos_capability_descriptor winusb_platform; - uint32_t dwWindowsVersion; - uint16_t wMSOSDescriptorSetTotalLength; - uint8_t bMS_VendorCode; - uint8_t bAltEnumCode; -} __packed; - #endif \ No newline at end of file diff --git a/components/usb_stack/common/usb_def.h b/components/usb_stack/common/usb_def.h index 8a592c3a..57903f6b 100644 --- a/components/usb_stack/common/usb_def.h +++ b/components/usb_stack/common/usb_def.h @@ -351,8 +351,8 @@ struct usb_msosv1_comp_id_function_descriptor { uint8_t reserved2[6]; } __packed; -#define usb_msosv1_comp_id_property_create(x) \ - struct usb_msosv1_comp_id_property { \ +#define usb_msosv1_comp_id_create(x) \ + struct usb_msosv1_comp_id { \ struct usb_msosv1_compat_id_header_descriptor compat_id_header; \ struct usb_msosv1_comp_id_function_descriptor compat_id_function[x]; \ }; @@ -368,32 +368,23 @@ struct usb_msosv1_descriptor { }; /* MS OS 2.0 Header descriptor */ -struct usb_msosv2_property_header_descriptor { +struct usb_msosv2_header_descriptor { uint32_t dwLength; uint16_t bcdVersion; uint16_t wIndex; uint8_t bCount; } __packed; -/* WinUSB Microsoft OS 2.0 descriptor set header */ -struct winusb_header_descriptor { +/*Microsoft OS 2.0 set header descriptor*/ +struct usb_msosv2_set_header_descriptor { uint16_t wLength; uint16_t wDescriptorType; uint32_t dwWindowsVersion; uint16_t wDescriptorSetTotalLength; } __packed; -/* WinUSB Microsoft OS 2.0 subset function descriptor */ -struct winusb_subset_function_descriptor { - uint16_t wLength; - uint16_t wDescriptorType; - uint8_t bFirstInterface; - uint8_t bReserved; - uint16_t wSubsetLength; -} __packed; - -/* MS OS 2.0 Function Section */ -struct usb_msosv2_comp_id_function_descriptor { +/* Microsoft OS 2.0 compatibleID descriptor*/ +struct usb_msosv2_comp_id_descriptor { uint16_t wLength; uint16_t wDescriptorType; uint8_t compatibleID[8]; @@ -401,7 +392,7 @@ struct usb_msosv2_comp_id_function_descriptor { } __packed; /* MS OS 2.0 property descriptor */ -struct usb_msosv2_proerty_descriptor { +struct usb_msosv2_property_descriptor { uint16_t wLength; uint16_t wDescriptorType; uint32_t dwPropertyDataType; @@ -411,12 +402,22 @@ struct usb_msosv2_proerty_descriptor { const char *bPropertyData; }; +/* Microsoft OS 2.0 subset function descriptor */ +struct usb_msosv2_subset_function_descriptor { + uint16_t wLength; + uint16_t wDescriptorType; + uint8_t bFirstInterface; + uint8_t bReserved; + uint16_t wSubsetLength; +} __packed; + struct usb_msosv2_descriptor { uint8_t *compat_id; uint16_t compat_id_len; + uint8_t vendor_code; }; -/* BOS Descriptor */ +/* BOS header Descriptor */ struct usb_bos_header_descriptor { uint8_t bLength; uint8_t bDescriptorType; @@ -424,8 +425,8 @@ struct usb_bos_header_descriptor { uint8_t bNumDeviceCaps; } __packed; -/* BOS Capability Descriptor */ -struct usb_bos_capability_descriptor { +/* BOS Capability platform Descriptor */ +struct usb_bos_capability_platform_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bDevCapabilityType; @@ -433,20 +434,63 @@ struct usb_bos_capability_descriptor { uint8_t PlatformCapabilityUUID[16]; } __packed; -struct usb_bos_capability_lpm { +/* BOS Capability MS OS Descriptors version 2 */ +struct usb_bos_capability_msosv2_descriptor { + uint32_t dwWindowsVersion; + uint16_t wMSOSDescriptorSetTotalLength; + uint8_t bVendorCode; + uint8_t bAltEnumCode; +} __packed; + +/* BOS Capability webusb */ +struct usb_bos_capability_webusb_descriptor { + uint16_t bcdVersion; + uint8_t bVendorCode; + uint8_t iLandingPage; +} __packed; + +/* BOS Capability extension Descriptor*/ +struct usb_bos_capability_extension_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bDevCapabilityType; uint32_t bmAttributes; } __packed; +/* Microsoft OS 2.0 Platform Capability Descriptor +* See https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/ +* microsoft-defined-usb-descriptors +* Adapted from the source: +* https://github.com/sowbug/weblight/blob/master/firmware/webusb.c +* (BSD-2) Thanks http://janaxelson.com/files/ms_os_20_descriptors.c +*/ +struct usb_bos_capability_platform_msosv2_descriptor { + struct usb_bos_capability_platform_descriptor platform_msos; + struct usb_bos_capability_msosv2_descriptor data_msosv2; +} __packed; + +/* WebUSB Platform Capability Descriptor: +* https://wicg.github.io/webusb/#webusb-platform-capability-descriptor +*/ +struct usb_bos_capability_platform_webusb_descriptor { + struct usb_bos_capability_platform_descriptor platform_webusb; + struct usb_bos_capability_webusb_descriptor data_webusb; +} __packed; + +struct usb_webusb_url_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bScheme; + char URL[]; +} __packed; + struct usb_bos_descriptor { - uint8_t *bos_id; - uint8_t bos_id_len; + uint8_t *string; + uint32_t string_len; }; /* USB Device Capability Descriptor */ -struct usb_device_capability__descriptor { +struct usb_device_capability_descriptor { uint8_t bLength; uint8_t bDescriptorType; uint8_t bDevCapabilityType; @@ -457,66 +501,66 @@ struct usb_desc_header { uint8_t bLength; /**< descriptor length */ uint8_t bDescriptorType; /**< descriptor type */ }; - +// clang-format off #define USB_DEVICE_DESCRIPTOR_INIT(bcdUSB, bDeviceClass, bDeviceSubClass, bDeviceProtocol, idVendor, idProduct, bcdDevice, bNumConfigurations) \ - 0x12, /* bLength */ \ - USB_DESCRIPTOR_TYPE_DEVICE, /* bDescriptorType */ \ - WBVAL(bcdUSB), /* bcdUSB */ \ - bDeviceClass, /* bDeviceClass */ \ - bDeviceSubClass, /* bDeviceSubClass */ \ - bDeviceProtocol, /* bDeviceProtocol */ \ - 0x40, /* bMaxPacketSize */ \ - WBVAL(idVendor), /* idVendor */ \ - WBVAL(idProduct), /* idProduct */ \ - WBVAL(bcdDevice), /* bcdDevice */ \ - USB_STRING_MFC_INDEX, /* iManufacturer */ \ - USB_STRING_PRODUCT_INDEX, /* iProduct */ \ - USB_STRING_SERIAL_INDEX, /* iSerial */ \ - bNumConfigurations /* bNumConfigurations */ + 0x12, /* bLength */ \ + USB_DESCRIPTOR_TYPE_DEVICE, /* bDescriptorType */ \ + WBVAL(bcdUSB), /* bcdUSB */ \ + bDeviceClass, /* bDeviceClass */ \ + bDeviceSubClass, /* bDeviceSubClass */ \ + bDeviceProtocol, /* bDeviceProtocol */ \ + 0x40, /* bMaxPacketSize */ \ + WBVAL(idVendor), /* idVendor */ \ + WBVAL(idProduct), /* idProduct */ \ + WBVAL(bcdDevice), /* bcdDevice */ \ + USB_STRING_MFC_INDEX, /* iManufacturer */ \ + USB_STRING_PRODUCT_INDEX, /* iProduct */ \ + USB_STRING_SERIAL_INDEX, /* iSerial */ \ + bNumConfigurations /* bNumConfigurations */ #define USB_CONFIG_DESCRIPTOR_INIT(wTotalLength, bNumInterfaces, bConfigurationValue, bmAttributes, bMaxPower) \ - 0x09, /* bLength */ \ - USB_DESCRIPTOR_TYPE_CONFIGURATION, /* bDescriptorType */ \ - WBVAL(wTotalLength), /* wTotalLength */ \ - bNumInterfaces, /* bNumInterfaces */ \ - bConfigurationValue, /* bConfigurationValue */ \ - 0x00, /* iConfiguration */ \ - bmAttributes, /* bmAttributes */ \ - USB_CONFIG_POWER_MA(bMaxPower) /* bMaxPower */ + 0x09, /* bLength */ \ + USB_DESCRIPTOR_TYPE_CONFIGURATION, /* bDescriptorType */ \ + WBVAL(wTotalLength), /* wTotalLength */ \ + bNumInterfaces, /* bNumInterfaces */ \ + bConfigurationValue, /* bConfigurationValue */ \ + 0x00, /* iConfiguration */ \ + bmAttributes, /* bmAttributes */ \ + USB_CONFIG_POWER_MA(bMaxPower) /* bMaxPower */ #define USB_INTERFACE_DESCRIPTOR_INIT(bInterfaceNumber, bAlternateSetting, bNumEndpoints, \ bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol, iInterface) \ - 0x09, /* bLength */ \ - USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \ - bInterfaceNumber, /* bInterfaceNumber */ \ - bAlternateSetting, /* bAlternateSetting */ \ - bNumEndpoints, /* bNumEndpoints */ \ - bInterfaceClass, /* bInterfaceClass */ \ - bInterfaceSubClass, /* bInterfaceSubClass */ \ - bInterfaceProtocol, /* bInterfaceProtocol */ \ - iInterface /* iInterface */ + 0x09, /* bLength */ \ + USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \ + bInterfaceNumber, /* bInterfaceNumber */ \ + bAlternateSetting, /* bAlternateSetting */ \ + bNumEndpoints, /* bNumEndpoints */ \ + bInterfaceClass, /* bInterfaceClass */ \ + bInterfaceSubClass, /* bInterfaceSubClass */ \ + bInterfaceProtocol, /* bInterfaceProtocol */ \ + iInterface /* iInterface */ #define USB_ENDPOINT_DESCRIPTOR_INIT(bEndpointAddress, bmAttributes, wMaxPacketSize, bInterval) \ - 0x07, /* bLength */ \ - USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \ - bEndpointAddress, /* bEndpointAddress */ \ - bmAttributes, /* bmAttributes */ \ - WBVAL(wMaxPacketSize), /* wMaxPacketSize */ \ - bInterval /* bInterval */ + 0x07, /* bLength */ \ + USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \ + bEndpointAddress, /* bEndpointAddress */ \ + bmAttributes, /* bmAttributes */ \ + WBVAL(wMaxPacketSize), /* wMaxPacketSize */ \ + bInterval /* bInterval */ #define USB_IAD_INIT(bFirstInterface, bInterfaceCount, bFunctionClass, bFunctionSubClass, bFunctionProtocol) \ - 0x08, /* bLength */ \ - USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION, /* bDescriptorType */ \ - bFirstInterface, /* bFirstInterface */ \ - bInterfaceCount, /* bInterfaceCount */ \ - bFunctionClass, /* bFunctionClass */ \ - bFunctionSubClass, /* bFunctionSubClass */ \ - bFunctionProtocol, /* bFunctionProtocol */ \ - 0x00 /* iFunction */ - -#define USB_LANGID_INIT(id) \ - 0x04, /* bLength */ \ - USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ \ - WBVAL(id) /* wLangID0 */ + 0x08, /* bLength */ \ + USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION, /* bDescriptorType */ \ + bFirstInterface, /* bFirstInterface */ \ + bInterfaceCount, /* bInterfaceCount */ \ + bFunctionClass, /* bFunctionClass */ \ + bFunctionSubClass, /* bFunctionSubClass */ \ + bFunctionProtocol, /* bFunctionProtocol */ \ + 0x00 /* iFunction */ +#define USB_LANGID_INIT(id) \ + 0x04, /* bLength */ \ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ \ + WBVAL(id) /* wLangID0 */ +// clang-format on #endif \ No newline at end of file diff --git a/components/usb_stack/core/usbd_core.c b/components/usb_stack/core/usbd_core.c index a3a9e6d9..ee4373d2 100644 --- a/components/usb_stack/core/usbd_core.c +++ b/components/usb_stack/core/usbd_core.c @@ -21,6 +21,7 @@ * */ #include "usbd_core.h" +#include "usbd_winusb.h" #define USBD_EP_CALLBACK_LIST_SEARCH 0 #define USBD_EP_CALLBACK_ARR_SEARCH 1 @@ -76,6 +77,7 @@ static struct usbd_core_cfg_priv { static usb_slist_t usbd_class_head = USB_SLIST_OBJECT_INIT(usbd_class_head); static struct usb_msosv1_descriptor *msosv1_desc; +static struct usb_msosv2_descriptor *msosv2_desc; static struct usb_bos_descriptor *bos_desc; /** @@ -259,25 +261,25 @@ static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *l index = GET_DESC_INDEX(type_index); if ((type == USB_DESCRIPTOR_TYPE_STRING) && (index == USB_OSDESC_STRING_DESC_INDEX)) { - USBD_LOG("MS OS Descriptor string read\r\n"); + USBD_LOG("read MS OS 2.0 descriptor string\r\n"); if (!msosv1_desc) { return false; } *data = (uint8_t *)msosv1_desc->string; - *len = sizeof(struct usb_msosv1_string_descriptor); + *len = msosv1_desc->string_len; return true; } else if (type == USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE) { - USBD_LOG("BOS descriptor string read\r\n"); + USBD_LOG("read BOS descriptor string\r\n"); if (!bos_desc) { return false; } - *data = bos_desc->bos_id; - *len = bos_desc->bos_id_len; + *data = bos_desc->string; + *len = bos_desc->string_len; return true; } /* @@ -285,7 +287,11 @@ static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *l * see USB Spec. Revision 2.0, 9.4.3 Get Descriptor */ else if ((type == USB_DESCRIPTOR_TYPE_INTERFACE) || (type == USB_DESCRIPTOR_TYPE_ENDPOINT) || +#ifndef CONFIG_USB_HS + (type > USB_DESCRIPTOR_TYPE_ENDPOINT)) { +#else (type > USB_DESCRIPTOR_TYPE_OTHER_SPEED)) { +#endif return false; } @@ -811,19 +817,33 @@ static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t * if (setup->bRequest == msosv1_desc->vendor_code) { switch (setup->wIndex) { case 0x04: - USBD_LOG("Handle Compat ID\r\n"); + USBD_LOG("get Compat ID\r\n"); *data = (uint8_t *)msosv1_desc->compat_id; *len = msosv1_desc->compat_id_len; return 0; case 0x05: - USBD_LOG("Handle Compat properties\r\n"); + USBD_LOG("get Compat id properties\r\n"); *data = (uint8_t *)msosv1_desc->comp_id_property; *len = msosv1_desc->comp_id_property_len; return 0; default: - break; + USBD_LOG("unknown vendor code\r\n"); + return -1; + } + } + } else if (msosv2_desc) { + if (setup->bRequest == msosv2_desc->vendor_code) { + switch (setup->wIndex) { + case WINUSB_REQUEST_GET_DESCRIPTOR_SET: + USBD_LOG("GET MS OS 2.0 Descriptor\r\n"); + *data = (uint8_t *)msosv2_desc->compat_id; + *len = msosv2_desc->compat_id_len; + return 0; + default: + USBD_LOG("unknown vendor code\r\n"); + return -1; } } } @@ -1213,6 +1233,17 @@ void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc) msosv1_desc = desc; } +/* Register MS OS Descriptors version 2 */ +void usbd_msosv2_desc_register(struct usb_msosv2_descriptor *desc) +{ + msosv2_desc = desc; +} + +void usbd_bos_desc_register(struct usb_bos_descriptor *desc) +{ + bos_desc = desc; +} + void usbd_class_register(usbd_class_t *class) { usb_slist_add_tail(&usbd_class_head, &class->list); diff --git a/components/usb_stack/core/usbd_core.h b/components/usb_stack/core/usbd_core.h index 5881106e..6884a76e 100644 --- a/components/usb_stack/core/usbd_core.h +++ b/components/usb_stack/core/usbd_core.h @@ -125,6 +125,8 @@ void usbd_event_notify_handler(uint8_t event, void *arg); void usbd_desc_register(const uint8_t *desc); void usbd_class_register(usbd_class_t *class); void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc); +void usbd_msosv2_desc_register(struct usb_msosv2_descriptor *desc); +void usbd_bos_desc_register(struct usb_bos_descriptor *desc); void usbd_class_add_interface(usbd_class_t *class, usbd_interface_t *intf); void usbd_interface_add_endpoint(usbd_interface_t *intf, usbd_endpoint_t *ep); bool usb_device_is_configured(void);