From 164b26ee6bb0a24924e9736178808b676288845e Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 12 Jul 2019 14:53:11 +0700 Subject: [PATCH] change TUD_HID_INOUT_DESCRIPTOR epout & epin order clean up, adding template for BOS & BOS platform descriptor --- .../hid_generic_inout/src/usb_descriptors.c | 2 +- examples/device/webusb/src/usb_descriptors.c | 4 ++- src/common/tusb_types.h | 26 +++++++++++----- src/device/usbd.c | 4 +-- src/device/usbd.h | 31 +++++++++++++++---- 5 files changed, 49 insertions(+), 18 deletions(-) diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index ced91593..cb7c92a5 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -85,7 +85,7 @@ uint8_t const desc_configuration[] = TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), // Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval - TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x80 | EPNUM_HID, EPNUM_HID, CFG_TUD_HID_BUFSIZE, 10) + TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, 0x80 | EPNUM_HID, CFG_TUD_HID_BUFSIZE, 10) }; diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb/src/usb_descriptors.c index 5149d85c..4bc32f6c 100644 --- a/examples/device/webusb/src/usb_descriptors.c +++ b/examples/device/webusb/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0210, + .bcdUSB = 0x0210, // at least 2.1 or 3.x .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, @@ -56,6 +56,8 @@ tusb_desc_device_t const desc_device = .bNumConfigurations = 0x01 }; +// BOS Descriptor: required for webUSB + //------------- Configuration Descriptor -------------// enum { diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index f7a1fc22..527f8497 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -228,10 +228,10 @@ enum }; //--------------------------------------------------------------------+ -// STANDARD DESCRIPTORS +// USB Standard Descriptors //--------------------------------------------------------------------+ -/// USB Standard Device Descriptor (section 9.6.1, table 9-8) +/// USB Device Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes. @@ -253,6 +253,7 @@ typedef struct TU_ATTR_PACKED uint8_t bNumConfigurations ; ///< Number of possible configurations. } tusb_desc_device_t; +// USB Binary Device Object Store (BOS) Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes @@ -261,7 +262,7 @@ typedef struct TU_ATTR_PACKED uint8_t bNumDeviceCaps ; ///< Number of device capability descriptors in the BOS } tusb_desc_bos_t; -/// USB Standard Configuration Descriptor (section 9.6.1 table 9-10) */ +/// USB Configuration Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes @@ -275,7 +276,7 @@ typedef struct TU_ATTR_PACKED uint8_t bMaxPower ; ///< Maximum power consumption of the USB device from the bus in this specific configuration when the device is fully operational. Expressed in 2 mA units (i.e., 50 = 100 mA). } tusb_desc_configuration_t; -/// USB Standard Interface Descriptor (section 9.6.1 table 9-12) +/// USB Interface Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes @@ -290,7 +291,7 @@ typedef struct TU_ATTR_PACKED uint8_t iInterface ; ///< Index of string descriptor describing this interface } tusb_desc_interface_t; -/// USB Standard Endpoint Descriptor (section 9.6.1 table 9-13) +/// USB Endpoint Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes @@ -314,7 +315,7 @@ typedef struct TU_ATTR_PACKED uint8_t bInterval ; ///< Interval for polling endpoint for data transfers. Expressed in frames or microframes depending on the device operating speed (i.e., either 1 millisecond or 125 us units). \n- For full-/high-speed isochronous endpoints, this value must be in the range from 1 to 16. The bInterval value is used as the exponent for a \f$ 2^(bInterval-1) \f$ value; e.g., a bInterval of 4 means a period of 8 (\f$ 2^(4-1) \f$). \n- For full-/low-speed interrupt endpoints, the value of this field may be from 1 to 255. \n- For high-speed interrupt endpoints, the bInterval value is used as the exponent for a \f$ 2^(bInterval-1) \f$ value; e.g., a bInterval of 4 means a period of 8 (\f$ 2^(4-1) \f$) . This value must be from 1 to 16. \n- For high-speed bulk/control OUT endpoints, the bInterval must specify the maximum NAK rate of the endpoint. A value of 0 indicates the endpoint never NAKs. Other values indicate at most 1 NAK each bInterval number of microframes. This value must be in the range from 0 to 255. \n Refer to Chapter 5 of USB 2.0 specification for more information. } tusb_desc_endpoint_t; -/// USB Other Speed Configuration Descriptor (section 9.6.1 table 9-11) +/// USB Other Speed Configuration Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of descriptor @@ -328,7 +329,7 @@ typedef struct TU_ATTR_PACKED uint8_t bMaxPower ; ///< Same as Configuration descriptor } tusb_desc_other_speed_t; -/// USB Device Qualifier Descriptor (section 9.6.1 table 9-9) +/// USB Device Qualifier Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of descriptor @@ -373,6 +374,15 @@ typedef struct TU_ATTR_PACKED uint16_t unicode_string[]; } tusb_desc_string_t; +typedef struct TU_ATTR_PACKED +{ + uint8_t bLength; + uint8_t bDescriptorType ; + uint8_t bDevCapabilityType; + uint8_t bReserved; + uint8_t PlatformCapabilityUUID[16]; + uint8_t CapabilityData[]; +} tusb_desc_bos_platform_t; /*------------------------------------------------------------------*/ /* Types @@ -449,7 +459,7 @@ static inline uint8_t tu_desc_len(void const* desc) #define TUD_DESC_STR_HEADER(_chr_count) ( (uint16_t) ( (TUSB_DESC_STRING << 8 ) | TUD_DESC_STRLEN(_chr_count)) ) // Convert comma-separated string to descriptor unicode format -#define TUD_DESC_STRCONV( ... ) (const uint16_t[]) { TUD_DESC_STR_HEADER(VA_ARGS_NUM_(__VA_ARGS__)), __VA_ARGS__ } +#define TUD_DESC_STRCONV( ... ) (const uint16_t[]) { TUD_DESC_STR_HEADER(TU_ARGS_NUM(__VA_ARGS__)), __VA_ARGS__ } #ifdef __cplusplus } diff --git a/src/device/usbd.c b/src/device/usbd.c index fb6d53ba..6a70afed 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -593,8 +593,8 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const // String Descriptor always uses the desc set from user if ( desc_index == 0xEE ) { - // The 0xEE index string is a Microsoft USB extension. - // It can be used to tell Windows what driver it should use for the device !!! + // The 0xEE index string is a Microsoft OS Descriptors. + // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors return false; }else { diff --git a/src/device/usbd.h b/src/device/usbd.h index 9959a99a..8bd6adc3 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -93,9 +93,21 @@ TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en); TU_ATTR_WEAK void tud_resume_cb(void); //--------------------------------------------------------------------+ -// Interface Descriptor Template +// Descriptor Templates //--------------------------------------------------------------------+ +//------------- Binary Device Object Store (BOS) -------------// +#define TUD_BOS_DESC_LEN 5 + +// total length, number of device caps +#define TUD_BOS_DESCRIPTOR(_total_len, _caps_num) \ + 5, TUSB_DESC_BOS, U16_TO_U8S_LE(_total_len), _caps_num + +// Device Capability Platform 128-bit UUID + Data +#define TUD_BOS_PLATFORM_DESCRIPTOR(...) \ + 4+TU_ARGS_NUM(__VA_ARGS__), TUSB_DESC_DEVICE_CAPABILITY, DEVICE_CAPABILITY_PLATFORM, 0x00, __VA_ARGS__ + +//------------- Configuration -------------// #define TUD_CONFIG_DESC_LEN (9) // Interface count, string index, total length, attribute, power in mA @@ -164,16 +176,16 @@ TU_ATTR_WEAK void tud_resume_cb(void); #define TUD_HID_INOUT_DESC_LEN (9 + 9 + 7 + 7) // HID Input & Output descriptor -// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval -#define TUD_HID_INOUT_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epin, _epout, _epsize, _ep_interval) \ +// Interface number, string index, protocol, report descriptor len, EP OUT & IN address, size & polling interval +#define TUD_HID_INOUT_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epout, _epin, _epsize, _ep_interval) \ /* Interface */\ 9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_HID, (_boot_protocol) ? HID_SUBCLASS_BOOT : 0, _boot_protocol, _stridx,\ /* HID descriptor */\ 9, HID_DESC_TYPE_HID, U16_TO_U8S_LE(0x0111), 0, 1, HID_DESC_TYPE_REPORT, U16_TO_U8S_LE(_report_desc_len),\ - /* Endpoint In */\ - 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval,\ /* Endpoint Out */\ - 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval + 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval, \ + /* Endpoint In */\ + 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval //------------- MIDI -------------// @@ -209,6 +221,13 @@ TU_ATTR_WEAK void tud_resume_cb(void); /* MS Endpoint (connected to embedded jack out) */\ 5, TUSB_DESC_CS_ENDPOINT, MIDI_CS_ENDPOINT_GENERAL, 1, 3 +//------------- WebUSB -------------// + +#define TUD_WEBUSB_DESC_LEN + +// Interface number, string index, EP Out & IN address, size & polling interval +#define TUD_WEBUSB_DESCRIPTOR() + #ifdef __cplusplus }