refactor usb class structure

This commit is contained in:
sakumisu
2021-11-26 23:41:59 +08:00
parent 89f8d74fb8
commit 636b30c3d8
35 changed files with 3552 additions and 2415 deletions

View File

@@ -1,7 +1,7 @@
#ifndef _USB_DC_H
#define _USB_DC_H
#include "stdint.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
@@ -25,6 +25,13 @@ extern "C" {
/** True if the endpoint is an OUT endpoint */
#define USB_EP_DIR_IS_OUT(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_OUT)
/* Default USB control EP, always 0 and 0x80 */
#define USB_CONTROL_OUT_EP0 0
#define USB_CONTROL_IN_EP0 0x80
/**< maximum packet size (MPS) for EP 0 */
#define USB_CTRL_EP_MPS 64
/**
* USB endpoint Transfer Type mask.
*/
@@ -34,32 +41,6 @@ extern "C" {
#define USBD_EP_TYPE_INTR 3
#define USBD_EP_TYPE_MASK 3
/* Default USB control EP, always 0 and 0x80 */
#define USB_CONTROL_OUT_EP0 0
#define USB_CONTROL_IN_EP0 0x80
/**
* @brief USB Device Controller API
* @defgroup _usb_device_controller_api USB Device Controller API
* @{
*/
/**< maximum packet size (MPS) for EP 0 */
#define USB_CTRL_EP_MPS 64
/**
* @brief USB Endpoint Transfer Type
*/
enum usb_dc_ep_transfer_type {
/** Control type endpoint */
USB_DC_EP_CONTROL = 0,
/** Isochronous type endpoint */
USB_DC_EP_ISOCHRONOUS,
/** Bulk type endpoint */
USB_DC_EP_BULK,
/** Interrupt type endpoint */
USB_DC_EP_INTERRUPT
};
/**
* @brief USB Endpoint Configuration.
*
@@ -72,12 +53,12 @@ struct usbd_endpoint_cfg {
* OUT EP = 0x00 | \<endpoint number\>
*/
uint8_t ep_addr;
/** Endpoint max packet size */
uint16_t ep_mps;
/** Endpoint Transfer Type.
* May be Bulk, Interrupt, Control or Isochronous
*/
enum usb_dc_ep_transfer_type ep_type;
uint8_t ep_type;
/** Endpoint max packet size */
uint16_t ep_mps;
};
/**

View File

@@ -1,5 +1,5 @@
#ifndef USB_REQUEST_H
#define USB_REQUEST_H
#ifndef USB_DEF_H
#define USB_DEF_H
/* Useful define */
#define USB_1_1 0x0110
@@ -7,45 +7,56 @@
/* Set USB version to 2.1 so that the host will request the BOS descriptor */
#define USB_2_1 0x0210
// USB Speed
#define USB_SPEED_LOW 0U
#define USB_SPEED_FULL 1U
#define USB_SPEED_HIGH 2U
/* Device speeds */
#define USB_SPEED_UNKNOWN 0 /* Transfer rate not yet set */
#define USB_SPEED_LOW 1 /* USB 1.1 */
#define USB_SPEED_FULL 2 /* USB 1.1 */
#define USB_SPEED_HIGH 3 /* USB 2.0 */
#define USB_SPEED_VARIABLE 4 /* Wireless USB 2.5 */
/* Maximum number of devices per controller */
#define USB_MAX_DEVICES (127)
// USB PID Types
#define USB_PID_RESERVED 0U
#define USB_PID_OUT 1U
#define USB_PID_ACK 2U
#define USB_PID_DATA0 3U
#define USB_PID_PING 4U
#define USB_PID_SOF 5U
#define USB_PID_DATA2 7U
#define USB_PID_NYET 6U
#define USB_PID_SPLIT 8U
#define USB_PID_IN 9U
#define USB_PID_NAK 10U
#define USB_PID_DATA1 11U
#define USB_PID_PRE 12U
#define USB_PID_ERR 12U
#define USB_PID_SETUP 13U
#define USB_PID_STALL 14U
#define USB_PID_MDATA 15U
#define USB_PID_OUT (0x01) /* Tokens */
#define USB_PID_IN (0x09)
#define USB_PID_SOF (0x05)
#define USB_PID_SETUP (0x0d)
// bmRequestType.Dir
#define USB_REQUEST_HOST_TO_DEVICE 0U
#define USB_REQUEST_DEVICE_TO_HOST 1U
#define USB_PID_DATA0 (0x03) /* Data */
#define USB_PID_DATA1 (0x0b)
#define USB_PID_DATA2 (0x07)
#define USB_PID_MDATA (0x0f)
// bmRequestType.Type
#define USB_REQUEST_STANDARD 0U
#define USB_REQUEST_CLASS 1U
#define USB_REQUEST_VENDOR 2U
#define USB_REQUEST_RESERVED 3U
#define USB_PID_ACK (0x02) /* Handshake */
#define USB_PID_NAK (0x0a)
#define USB_PID_STALL (0x0e)
#define USB_PID_NYET (0x06)
// bmRequestType.Recipient
#define USB_REQUEST_TO_DEVICE 0U
#define USB_REQUEST_TO_INTERFACE 1U
#define USB_REQUEST_TO_ENDPOINT 2U
#define USB_REQUEST_TO_OTHER 3U
#define USB_PID_PRE (0x0c) /* Special */
#define USB_PID_ERR (0x0c)
#define USB_PID_SPLIT (0x08)
#define USB_PID_PING (0x04)
#define USB_PID_RESERVED (0x00)
#define USB_REQUEST_DIR_SHIFT 7U /* Bits 7: Request dir */
#define USB_REQUEST_DIR_OUT (0U << USB_REQUEST_DIR_SHIFT) /* Bit 7=0: Host-to-device */
#define USB_REQUEST_DIR_IN (1U << USB_REQUEST_DIR_SHIFT) /* Bit 7=1: Device-to-host */
#define USB_REQUEST_DIR_MASK (1U << USB_REQUEST_DIR_SHIFT) /* Bit 7=1: Direction bit */
#define USB_REQUEST_TYPE_SHIFT 5U /* Bits 5:6: Request type */
#define USB_REQUEST_STANDARD (0U << USB_REQUEST_TYPE_SHIFT)
#define USB_REQUEST_CLASS (1U << USB_REQUEST_TYPE_SHIFT)
#define USB_REQUEST_VENDOR (2U << USB_REQUEST_TYPE_SHIFT)
#define USB_REQUEST_RESERVED (3U << USB_REQUEST_TYPE_SHIFT)
#define USB_REQUEST_TYPE_MASK (3U << USB_REQUEST_TYPE_SHIFT)
#define USB_REQUEST_RECIPIENT_SHIFT 0U /* Bits 0:4: Recipient */
#define USB_REQUEST_TO_DEVICE (0U << USB_REQUEST_RECIPIENT_SHIFT)
#define USB_REQUEST_TO_INTERFACE (1U << USB_REQUEST_RECIPIENT_SHIFT)
#define USB_REQUEST_TO_ENDPOINT (2U << USB_REQUEST_RECIPIENT_SHIFT)
#define USB_REQUEST_TO_OTHER (3U << USB_REQUEST_RECIPIENT_SHIFT)
#define USB_REQUEST_RECIPIENT_MASK (3U << USB_REQUEST_RECIPIENT_SHIFT)
/* USB Standard Request Codes */
#define USB_REQUEST_GET_STATUS 0x00
@@ -73,27 +84,22 @@
#define USB_REQUEST_LOOPBACK_DATA_READ 0x16
#define USB_REQUEST_SET_INTERFACE_DS 0x17
/* USB GET_STATUS Bit Values */
#define USB_GETSTATUS_SELF_POWERED 0x01
#define USB_GETSTATUS_REMOTE_WAKEUP 0x02
#define USB_GETSTATUS_ENDPOINT_STALL 0x01
/* USB Standard Feature selectors */
#define USB_FEATURE_ENDPOINT_STALL 0
#define USB_FEATURE_ENDPOINT_HALT 0
#define USB_FEATURE_SELF_POWERED 0
#define USB_FEATURE_REMOTE_WAKEUP 1
#define USB_FEATURE_TEST_MODE 2
#define USB_FEATURE_BATTERY 2
#define USB_FEATURE_BHNPENABLE 3
#define USB_FEATURE_WUSBDEVICE 3
#define USB_FEATURE_AHNPSUPPORT 4
#define USB_FEATURE_AALTHNPSUPPORT 5
#define USB_FEATURE_DEBUGMODE 6
/* Descriptor size in bytes */
#define USB_DEVICE_DESC_SIZE 0x12
#define USB_CONFIGURATION_DESC_SIZE 0x09
#define USB_INTERFACE_DESC_SIZE 0x09
#define USB_ENDPOINT_DESC_SIZE 0x07
#define USB_LANGID_STRING_DESC_SIZE 0x04
#define USB_OTHER_SPEED_DESC_SIZE 0x09
#define USB_DEVICE_QUAL_DESC_SIZE 0x0A
#define USB_INTERFACE_ASSOC_DESC_SIZE 0x08
#define USB_FUNCTION_DESC_SIZE 0x03
#define USB_OTG_DESC_SIZE 0x03
/* USB GET_STATUS Bit Values */
#define USB_GETSTATUS_ENDPOINT_HALT 0x01
#define USB_GETSTATUS_SELF_POWERED 0x01
#define USB_GETSTATUS_REMOTE_WAKEUP 0x02
/* USB Descriptor Types */
#define USB_DESCRIPTOR_TYPE_DEVICE 0x01U
@@ -109,10 +115,9 @@
#define USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION 0x0BU
#define USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE 0x0FU
#define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY 0x10U
#define USB_DESCRIPTOR_TYPE_WIRELESS_ENDPOINTCOMP 0x11U
#define USB_DESCRIPTOR_TYPE_FUNCTIONAL 0x21U
// Class Specific Descriptor
/* Class Specific Descriptor */
#define USB_CS_DESCRIPTOR_TYPE_DEVICE 0x21U
#define USB_CS_DESCRIPTOR_TYPE_CONFIGURATION 0x22U
#define USB_CS_DESCRIPTOR_TYPE_STRING 0x23U
@@ -161,10 +166,10 @@
#define USB_OSDESC_STRING_DESC_INDEX 0xEE
/* bmAttributes in Configuration Descriptor */
#define USB_CONFIG_REMOTE_WAKEUP 0x20
#define USB_CONFIG_POWERED_MASK 0x40
#define USB_CONFIG_BUS_POWERED 0x80
#define USB_CONFIG_SELF_POWERED 0xC0
#define USB_CONFIG_REMOTE_WAKEUP 0x20
/* bMaxPower in Configuration Descriptor */
#define USB_CONFIG_POWER_MA(mA) ((mA) / 2)
@@ -175,21 +180,27 @@
#define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
/* bmAttributes in Endpoint Descriptor */
#define USB_ENDPOINT_TYPE_MASK 0x03
#define USB_ENDPOINT_TYPE_CONTROL 0x00
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
#define USB_ENDPOINT_TYPE_BULK 0x02
#define USB_ENDPOINT_TYPE_INTERRUPT 0x03
#define USB_ENDPOINT_SYNC_MASK 0x0C
#define USB_ENDPOINT_SYNC_NO_SYNCHRONIZATION 0x00
#define USB_ENDPOINT_SYNC_ASYNCHRONOUS 0x04
#define USB_ENDPOINT_SYNC_ADAPTIVE 0x08
#define USB_ENDPOINT_SYNC_SYNCHRONOUS 0x0C
#define USB_ENDPOINT_USAGE_MASK 0x30
#define USB_ENDPOINT_USAGE_DATA 0x00
#define USB_ENDPOINT_USAGE_FEEDBACK 0x10
#define USB_ENDPOINT_USAGE_IMPLICIT_FEEDBACK 0x20
#define USB_ENDPOINT_USAGE_RESERVED 0x30
#define USB_ENDPOINT_TYPE_SHIFT 0
#define USB_ENDPOINT_TYPE_CONTROL (0 << USB_ENDPOINT_TYPE_SHIFT)
#define USB_ENDPOINT_TYPE_ISOCHRONOUS (1 << USB_ENDPOINT_TYPE_SHIFT)
#define USB_ENDPOINT_TYPE_BULK (2 << USB_ENDPOINT_TYPE_SHIFT)
#define USB_ENDPOINT_TYPE_INTERRUPT (3 << USB_ENDPOINT_TYPE_SHIFT)
#define USB_ENDPOINT_TYPE_MASK (3 << USB_ENDPOINT_TYPE_SHIFT)
#define USB_ENDPOINT_SYNC_SHIFT 2
#define USB_ENDPOINT_SYNC_NO_SYNCHRONIZATION (0 << USB_ENDPOINT_SYNC_SHIFT)
#define USB_ENDPOINT_SYNC_ASYNCHRONOUS (1 << USB_ENDPOINT_SYNC_SHIFT)
#define USB_ENDPOINT_SYNC_ADAPTIVE (2 << USB_ENDPOINT_SYNC_SHIFT)
#define USB_ENDPOINT_SYNC_SYNCHRONOUS (3 << USB_ENDPOINT_SYNC_SHIFT)
#define USB_ENDPOINT_SYNC_MASK (3 << USB_ENDPOINT_SYNC_SHIFT)
#define USB_ENDPOINT_USAGE_SHIFT 4
#define USB_ENDPOINT_USAGE_DATA (0 << USB_ENDPOINT_USAGE_SHIFT)
#define USB_ENDPOINT_USAGE_FEEDBACK (1 << USB_ENDPOINT_USAGE_SHIFT)
#define USB_ENDPOINT_USAGE_IMPLICIT_FEEDBACK (2 << USB_ENDPOINT_USAGE_SHIFT)
#define USB_ENDPOINT_USAGE_MASK (3 << USB_ENDPOINT_USAGE_SHIFT)
#define USB_ENDPOINT_MAX_ADJUSTABLE (1 << 7)
/* bDevCapabilityType in Device Capability Descriptor */
#define USB_DEVICE_CAPABILITY_WIRELESS_USB 1
@@ -208,36 +219,79 @@
#define USB_BOS_CAPABILITY_EXTENSION 0x02
#define USB_BOS_CAPABILITY_PLATFORM 0x05
/* OTG SET FEATURE Constants */
#define USB_OTG_FEATURE_B_HNP_ENABLE 3 /* Enable B device to perform HNP */
#define USB_OTG_FEATURE_A_HNP_SUPPORT 4 /* A device supports HNP */
#define USB_OTG_FEATURE_A_ALT_HNP_SUPPORT 5 /* Another port on the A device supports HNP */
/* WinUSB Microsoft OS 2.0 descriptor request codes */
#define WINUSB_REQUEST_GET_DESCRIPTOR_SET 0x07
#define WINUSB_REQUEST_SET_ALT_ENUM 0x08
/* WinUSB Microsoft OS 2.0 descriptor sizes */
#define WINUSB_DESCRIPTOR_SET_HEADER_SIZE 10
#define WINUSB_FUNCTION_SUBSET_HEADER_SIZE 8
#define WINUSB_FEATURE_COMPATIBLE_ID_SIZE 20
/* WinUSB Microsoft OS 2.0 Descriptor Types */
#define WINUSB_SET_HEADER_DESCRIPTOR_TYPE 0x00
#define WINUSB_SUBSET_HEADER_CONFIGURATION_TYPE 0x01
#define WINUSB_SUBSET_HEADER_FUNCTION_TYPE 0x02
#define WINUSB_FEATURE_COMPATIBLE_ID_TYPE 0x03
#define WINUSB_FEATURE_REG_PROPERTY_TYPE 0x04
#define WINUSB_FEATURE_MIN_RESUME_TIME_TYPE 0x05
#define WINUSB_FEATURE_MODEL_ID_TYPE 0x06
#define WINUSB_FEATURE_CCGP_DEVICE_TYPE 0x07
#define WINUSB_PROP_DATA_TYPE_REG_SZ 0x01
#define WINUSB_PROP_DATA_TYPE_REG_MULTI_SZ 0x07
/* WebUSB Descriptor Types */
#define WEBUSB_DESCRIPTOR_SET_HEADER_TYPE 0x00
#define WEBUSB_CONFIGURATION_SUBSET_HEADER_TYPE 0x01
#define WEBUSB_FUNCTION_SUBSET_HEADER_TYPE 0x02
#define WEBUSB_URL_TYPE 0x03
/* WebUSB Request Codes */
#define WEBUSB_REQUEST_GET_URL 0x02
/* bScheme in URL descriptor */
#define WEBUSB_URL_SCHEME_HTTP 0x00
#define WEBUSB_URL_SCHEME_HTTPS 0x01
/* WebUSB Descriptor sizes */
#define WEBUSB_DESCRIPTOR_SET_HEADER_SIZE 5
#define WEBUSB_CONFIGURATION_SUBSET_HEADER_SIZE 4
#define WEBUSB_FUNCTION_SUBSET_HEADER_SIZE 3
/* Setup packet definition used to read raw data from USB line */
struct usb_setup_packet {
__packed union {
uint8_t bmRequestType; /* bmRequestType */
struct
{
uint8_t Recipient : 5; /* D4..0: Recipient */
uint8_t Type : 2; /* D6..5: Type */
uint8_t Dir : 1; /* D7: Data Phase Txsfer Direction */
} bmRequestType_b;
};
/** Request type. Bits 0:4 determine recipient, see
* \ref usb_request_recipient. Bits 5:6 determine type, see
* \ref usb_request_type. Bit 7 determines data transfer direction, see
* \ref usb_endpoint_direction.
*/
uint8_t bmRequestType;
/** Request. If the type bits of bmRequestType are equal to
* \ref usb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
* "USB_REQUEST_TYPE_STANDARD" then this field refers to
* \ref usb_standard_request. For other cases, use of this field is
* application-specific. */
uint8_t bRequest;
__packed union {
uint16_t wValue; /* wValue */
struct
{
uint8_t wValueL;
uint8_t wValueH;
};
};
__packed union {
uint16_t wIndex; /* wIndex */
struct
{
uint8_t wIndexL;
uint8_t wIndexH;
};
};
/** Value. Varies according to request */
uint16_t wValue;
/** Index. Varies according to request, typically used to pass an index
* or offset */
uint16_t wIndex;
/** Number of bytes to transfer */
uint16_t wLength;
} __packed;
} __PACKED;
#define USB_SIZEOF_SETUP_PACKET 8
/** Standard Device Descriptor */
struct usb_device_descriptor {
@@ -255,20 +309,9 @@ struct usb_device_descriptor {
uint8_t iProduct; /* Index to product string */
uint8_t iSerialNumber; /* Index to serial number string */
uint8_t bNumConfigurations; /* Number of possible configurations */
} __packed;
} __PACKED;
/** USB device_qualifier descriptor */
struct usb_device_qualifier_descriptor {
uint8_t bLength; /* Descriptor size in bytes = 10 */
uint8_t bDescriptorType; /* DEVICE QUALIFIER type = 6 */
uint16_t bcdUSB; /* USB spec in BCD, e.g. 0x0200 */
uint8_t bDeviceClass; /* Class code, if 0 see interface */
uint8_t bDeviceSubClass; /* Sub-Class code, 0 if class = 0 */
uint8_t bDeviceProtocol; /* Protocol, if 0 see interface */
uint8_t bMaxPacketSize; /* Endpoint 0 max. size */
uint8_t bNumConfigurations; /* Number of possible configurations */
uint8_t bReserved; /* Reserved = 0 */
} __packed;
#define USB_SIZEOF_DEVICE_DESC 18
/** Standard Configuration Descriptor */
struct usb_configuration_descriptor {
@@ -280,7 +323,9 @@ struct usb_configuration_descriptor {
uint8_t iConfiguration; /* Index to configuration string */
uint8_t bmAttributes; /* Config. characteristics */
uint8_t bMaxPower; /* Max.power from bus, 2mA units */
} __packed;
} __PACKED;
#define USB_SIZEOF_CONFIG_DESC 9
/** Standard Interface Descriptor */
struct usb_interface_descriptor {
@@ -293,7 +338,9 @@ struct usb_interface_descriptor {
uint8_t bInterfaceSubClass; /* Sub-Class code, 0 if class = 0 */
uint8_t bInterfaceProtocol; /* Protocol, 0xFF = vendor */
uint8_t iInterface; /* Index to interface string */
} __packed;
} __PACKED;
#define USB_SIZEOF_INTERFACE_DESC 9
/** Standard Endpoint Descriptor */
struct usb_endpoint_descriptor {
@@ -303,14 +350,18 @@ struct usb_endpoint_descriptor {
uint8_t bmAttributes; /* Transfer type */
uint16_t wMaxPacketSize; /* Bits 10:0 = max. packet size */
uint8_t bInterval; /* Polling interval in (micro) frames */
} __packed;
} __PACKED;
#define USB_SIZEOF_ENDPOINT_DESC 7
/** Unicode (UTF16LE) String Descriptor */
struct usb_string_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bString;
} __packed;
} __PACKED;
#define USB_SIZEOF_STRING_LANGID_DESC 4
/* USB Interface Association Descriptor */
struct usb_interface_association_descriptor {
@@ -322,7 +373,38 @@ struct usb_interface_association_descriptor {
uint8_t bFunctionSubClass;
uint8_t bFunctionProtocol;
uint8_t iFunction;
} __packed;
} __PACKED;
#define USB_SIZEOF_IAD_DESC 8
/** USB device_qualifier descriptor */
struct usb_device_qualifier_descriptor {
uint8_t bLength; /* Descriptor size in bytes = 10 */
uint8_t bDescriptorType; /* DEVICE QUALIFIER type = 6 */
uint16_t bcdUSB; /* USB spec in BCD, e.g. 0x0200 */
uint8_t bDeviceClass; /* Class code, if 0 see interface */
uint8_t bDeviceSubClass; /* Sub-Class code, 0 if class = 0 */
uint8_t bDeviceProtocol; /* Protocol, if 0 see interface */
uint8_t bMaxPacketSize; /* Endpoint 0 max. size */
uint8_t bNumConfigurations; /* Number of possible configurations */
uint8_t bReserved; /* Reserved = 0 */
} __PACKED;
#define USB_SIZEOF_DEVICE_QUALIFIER_DESC 10
/* Microsoft OS function descriptor.
* This can be used to request a specific driver (such as WINUSB) to be
* loaded on Windows. Unlike other descriptors, it is requested by a special
* request USB_REQ_GETMSFTOSDESCRIPTOR.
* More details:
* https://msdn.microsoft.com/en-us/windows/hardware/gg463179
* And excellent explanation:
* https://github.com/pbatard/libwdi/wiki/WCID-Devices
*
* The device will have exactly one "Extended Compat ID Feature Descriptor",
* which may contain multiple "Function Descriptors" associated with
* different interfaces.
*/
/* MS OS 1.0 string descriptor */
struct usb_msosv1_string_descriptor {
@@ -331,7 +413,7 @@ struct usb_msosv1_string_descriptor {
uint8_t bString[14];
uint8_t bMS_VendorCode; /* Vendor Code, used for a control request */
uint8_t bPad; /* Padding byte for VendorCode look as UTF16 */
} __packed;
} __PACKED;
/* MS OS 1.0 Header descriptor */
struct usb_msosv1_compat_id_header_descriptor {
@@ -340,7 +422,7 @@ struct usb_msosv1_compat_id_header_descriptor {
uint16_t wIndex;
uint8_t bCount;
uint8_t reserved[7];
} __packed;
} __PACKED;
/* MS OS 1.0 Function descriptor */
struct usb_msosv1_comp_id_function_descriptor {
@@ -349,7 +431,7 @@ struct usb_msosv1_comp_id_function_descriptor {
uint8_t compatibleID[8];
uint8_t subCompatibleID[8];
uint8_t reserved2[6];
} __packed;
} __PACKED;
#define usb_msosv1_comp_id_create(x) \
struct usb_msosv1_comp_id { \
@@ -373,7 +455,7 @@ struct usb_msosv2_header_descriptor {
uint16_t bcdVersion;
uint16_t wIndex;
uint8_t bCount;
} __packed;
} __PACKED;
/*Microsoft OS 2.0 set header descriptor*/
struct usb_msosv2_set_header_descriptor {
@@ -381,7 +463,7 @@ struct usb_msosv2_set_header_descriptor {
uint16_t wDescriptorType;
uint32_t dwWindowsVersion;
uint16_t wDescriptorSetTotalLength;
} __packed;
} __PACKED;
/* Microsoft OS 2.0 compatibleID descriptor*/
struct usb_msosv2_comp_id_descriptor {
@@ -389,7 +471,7 @@ struct usb_msosv2_comp_id_descriptor {
uint16_t wDescriptorType;
uint8_t compatibleID[8];
uint8_t subCompatibleID[8];
} __packed;
} __PACKED;
/* MS OS 2.0 property descriptor */
struct usb_msosv2_property_descriptor {
@@ -409,7 +491,7 @@ struct usb_msosv2_subset_function_descriptor {
uint8_t bFirstInterface;
uint8_t bReserved;
uint16_t wSubsetLength;
} __packed;
} __PACKED;
struct usb_msosv2_descriptor {
uint8_t *compat_id;
@@ -423,7 +505,7 @@ struct usb_bos_header_descriptor {
uint8_t bDescriptorType;
uint16_t wTotalLength;
uint8_t bNumDeviceCaps;
} __packed;
} __PACKED;
/* BOS Capability platform Descriptor */
struct usb_bos_capability_platform_descriptor {
@@ -432,7 +514,7 @@ struct usb_bos_capability_platform_descriptor {
uint8_t bDevCapabilityType;
uint8_t bReserved;
uint8_t PlatformCapabilityUUID[16];
} __packed;
} __PACKED;
/* BOS Capability MS OS Descriptors version 2 */
struct usb_bos_capability_msosv2_descriptor {
@@ -440,14 +522,14 @@ struct usb_bos_capability_msosv2_descriptor {
uint16_t wMSOSDescriptorSetTotalLength;
uint8_t bVendorCode;
uint8_t bAltEnumCode;
} __packed;
} __PACKED;
/* BOS Capability webusb */
struct usb_bos_capability_webusb_descriptor {
uint16_t bcdVersion;
uint8_t bVendorCode;
uint8_t iLandingPage;
} __packed;
} __PACKED;
/* BOS Capability extension Descriptor*/
struct usb_bos_capability_extension_descriptor {
@@ -455,7 +537,7 @@ struct usb_bos_capability_extension_descriptor {
uint8_t bDescriptorType;
uint8_t bDevCapabilityType;
uint32_t bmAttributes;
} __packed;
} __PACKED;
/* Microsoft OS 2.0 Platform Capability Descriptor
* See https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/
@@ -467,7 +549,7 @@ struct usb_bos_capability_extension_descriptor {
struct usb_bos_capability_platform_msosv2_descriptor {
struct usb_bos_capability_platform_descriptor platform_msos;
struct usb_bos_capability_msosv2_descriptor data_msosv2;
} __packed;
} __PACKED;
/* WebUSB Platform Capability Descriptor:
* https://wicg.github.io/webusb/#webusb-platform-capability-descriptor
@@ -475,14 +557,14 @@ struct usb_bos_capability_platform_msosv2_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;
} __PACKED;
struct usb_webusb_url_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bScheme;
char URL[];
} __packed;
} __PACKED;
struct usb_bos_descriptor {
uint8_t *string;
@@ -494,7 +576,7 @@ struct usb_device_capability_descriptor {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDevCapabilityType;
} __packed;
} __PACKED;
/** USB descriptor header */
struct usb_desc_header {
@@ -563,4 +645,5 @@ struct usb_desc_header {
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ \
WBVAL(id) /* wLangID0 */
// clang-format on
#endif
#endif

View File

@@ -1,8 +1,8 @@
#ifndef __USB_SLIST_H__
#define __USB_SLIST_H__
#include "string.h"
#include "stdint.h"
#include <string.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
@@ -221,4 +221,4 @@ static inline int usb_slist_isempty(usb_slist_t *l)
}
#endif
#endif
#endif

View File

@@ -1,42 +1,108 @@
#ifndef _USB_UTIL_H
#define _USB_UTIL_H
#include "stdbool.h"
#include "string.h"
#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "usb_slist.h"
#ifndef __packed
#define __packed __attribute__((__packed__))
#if defined(__CC_ARM)
#ifndef __USED
#define __USED __attribute__((used))
#endif
#ifndef __aligned
#define __aligned(x) __attribute__((__aligned__(x)))
#ifndef __WEAK
#define __WEAK __attribute__((weak))
#endif
#define __may_alias __attribute__((__may_alias__))
#ifndef __printf_like
#define __printf_like(f, a) __attribute__((format(printf, f, a)))
#ifndef __PACKED
#define __PACKED __attribute__((packed))
#endif
#define __used __attribute__((__used__))
#ifndef __deprecated
#define __deprecated __attribute__((deprecated))
#ifndef __PACKED_STRUCT
#define __PACKED_STRUCT __packed struct
#endif
#ifndef __PACKED_UNION
#define __PACKED_UNION __packed union
#endif
#ifndef __ALIGNED
#define __ALIGNED(x) __attribute__((aligned(x)))
#endif
#elif defined(__GNUC__)
#ifndef __USED
#define __USED __attribute__((used))
#endif
#ifndef __WEAK
#define __WEAK __attribute__((weak))
#endif
#ifndef __PACKED
#define __PACKED __attribute__((packed, aligned(1)))
#endif
#ifndef __PACKED_STRUCT
#define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
#endif
#ifndef __PACKED_UNION
#define __PACKED_UNION union __attribute__((packed, aligned(1)))
#endif
#ifndef __ALIGNED
#define __ALIGNED(x) __attribute__((aligned(x)))
#endif
#elif defined(__ICCARM__)
#ifndef __USED
#if __ICCARM_V8
#define __USED __attribute__((used))
#else
#define __USED _Pragma("__root")
#endif
#endif
#ifndef __WEAK
#if __ICCARM_V8
#define __WEAK __attribute__((weak))
#else
#define __WEAK _Pragma("__weak")
#endif
#endif
#ifndef __PACKED
#if __ICCARM_V8
#define __PACKED __attribute__((packed, aligned(1)))
#else
/* Needs IAR language extensions */
#define __PACKED __packed
#endif
#endif
#ifndef __PACKED_STRUCT
#if __ICCARM_V8
#define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
#else
/* Needs IAR language extensions */
#define __PACKED_STRUCT __packed struct
#endif
#endif
#ifndef __PACKED_UNION
#if __ICCARM_V8
#define __PACKED_UNION union __attribute__((packed, aligned(1)))
#else
/* Needs IAR language extensions */
#define __PACKED_UNION __packed union
#endif
#endif
#ifndef __ALIGNED
#if __ICCARM_V8
#define __ALIGNED(x) __attribute__((aligned(x)))
#elif (__VER__ >= 7080000)
/* Needs IAR language extensions */
#define __ALIGNED(x) __attribute__((aligned(x)))
#else
#warning No compiler specific solution for __ALIGNED.__ALIGNED is ignored.
#define __ALIGNED(x)
#endif
#define ARG_UNUSED(x) (void)(x)
// #define likely(x) __builtin_expect((bool)!!(x), true)
// #define unlikely(x) __builtin_expect((bool)!!(x), false)
#define popcount(x) __builtin_popcount(x)
#ifndef __no_optimization
#define __no_optimization __attribute__((optimize("-O0")))
#endif
#ifndef __weak
#define __weak __attribute__((__weak__))
#endif
#define __unused __attribute__((__unused__))
#ifndef __ALIGN_BEGIN
#define __ALIGN_BEGIN
@@ -45,6 +111,10 @@
#define __ALIGN_END __attribute__((aligned(4)))
#endif
#ifndef ARG_UNUSED
#define ARG_UNUSED(x) (void)(x)
#endif
#ifndef LO_BYTE
#define LO_BYTE(x) ((uint8_t)(x & 0x00FF))
#endif
@@ -133,12 +203,13 @@
#define WBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF)
#define DBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF), ((x >> 16) & 0xFF), ((x >> 24) & 0xFF)
#define USB_DESC_SECTION __attribute__((section("usb_desc"))) __used __aligned(1)
#define USB_DESC_SECTION __attribute__((section("usb_desc"))) __USED __ALIGNED(1)
#if 0
#define USBD_LOG_WRN(a, ...) printf(a, ##__VA_ARGS__)
#define USBD_LOG_DBG(a, ...) printf(a, ##__VA_ARGS__)
#define USBD_LOG_ERR(a, ...) printf(a, ##__VA_ARGS__)
#define USBD_LOG_INFO(a, ...) printf(a, ##__VA_ARGS__)
#define USBD_LOG_DBG(a, ...) printf(a, ##__VA_ARGS__)
#define USBD_LOG_WRN(a, ...) printf(a, ##__VA_ARGS__)
#define USBD_LOG_ERR(a, ...) printf(a, ##__VA_ARGS__)
#else
#define USBD_LOG_INFO(a, ...) printf(a, ##__VA_ARGS__)
#define USBD_LOG_DBG(a, ...)
@@ -146,4 +217,4 @@
#define USBD_LOG_ERR(a, ...) printf(a, ##__VA_ARGS__)
#endif
#endif
#endif