#ifndef _LIBUSB20_DESC_H_
#define _LIBUSB20_DESC_H_
#ifndef LIBUSB_GLOBAL_INCLUDE_FILE
#include <stdint.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if 0
};
#endif
#define LIBUSB20__NOT(...) __VA_ARGS__
#define LIBUSB20_NOT(arg) LIBUSB20__NOT(LIBUSB20_YES arg(() LIBUSB20_NO))
#define LIBUSB20_YES(...) __VA_ARGS__
#define LIBUSB20_NO(...)
#define LIBUSB20_END(...) __VA_ARGS__
#define LIBUSB20_MAX(a,b) (((a) > (b)) ? (a) : (b))
#define LIBUSB20_MIN(a,b) (((a) < (b)) ? (a) : (b))
#define LIBUSB20_ADD_BYTES(ptr,off) \
((void *)(((const uint8_t *)(ptr)) + (off) - ((const uint8_t *)0)))
enum {
LIBUSB20_ME_INT8,
LIBUSB20_ME_INT16,
LIBUSB20_ME_INT32,
LIBUSB20_ME_INT64,
LIBUSB20_ME_STRUCT,
LIBUSB20_ME_MAX,
};
enum {
LIBUSB20_ME_IS_UNSIGNED = 0x00,
LIBUSB20_ME_IS_SIGNED = 0x80,
LIBUSB20_ME_MASK = 0x7F,
};
enum {
LIBUSB20_ME_IS_RAW,
LIBUSB20_ME_IS_ENCODED,
LIBUSB20_ME_IS_EMPTY,
LIBUSB20_ME_IS_DECODED,
};
#define LIBUSB20_ME_STRUCT_ALIGN sizeof(void *)
struct libusb20_me_struct {
void *ptr;
uint16_t len;
uint16_t type;
} __aligned(LIBUSB20_ME_STRUCT_ALIGN);
struct libusb20_me_format {
const uint8_t *format;
const char *desc;
const char *fields;
};
#define LIBUSB20_ME_STRUCT(n, field, arg, ismeta) \
ismeta ( LIBUSB20_ME_STRUCT, 1, 0, ) \
LIBUSB20_NOT(ismeta) ( struct libusb20_me_struct field; )
#define LIBUSB20_ME_STRUCT_ARRAY(n, field, arg, ismeta) \
ismeta ( LIBUSB20_ME_STRUCT , (arg) & 0xFF, \
((arg) / 0x100) & 0xFF, ) \
LIBUSB20_NOT(ismeta) ( struct libusb20_me_struct field [arg]; )
#define LIBUSB20_ME_INTEGER(n, field, ismeta, un, u, bits, a, size) \
ismeta ( LIBUSB20_ME_INT##bits | \
LIBUSB20_ME_IS_##un##SIGNED , \
(size) & 0xFF, ((size) / 0x100) & 0xFF, ) \
LIBUSB20_NOT(ismeta) ( u##int##bits##_t \
__aligned((bits) / 8) field a; )
#define LIBUSB20_ME_UINT8_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 8, , 1)
#define LIBUSB20_ME_UINT8_ARRAY_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 8, [arg], arg)
#define LIBUSB20_ME_SINT8_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta,,, 8, , 1)
#define LIBUSB20_ME_SINT8_ARRAY_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta,,, 8, [arg], arg)
#define LIBUSB20_ME_UINT16_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 16, , 1)
#define LIBUSB20_ME_UINT16_ARRAY_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 16, [arg], arg)
#define LIBUSB20_ME_SINT16_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta,,, 16, , 1)
#define LIBUSB20_ME_SINT16_ARRAY_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta,,, 16, [arg], arg)
#define LIBUSB20_ME_UINT32_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 32, , 1)
#define LIBUSB20_ME_UINT32_ARRAY_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 32, [arg], arg)
#define LIBUSB20_ME_SINT32_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta,,, 32, , 1)
#define LIBUSB20_ME_SINT32_ARRAY_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta,,, 32, [arg], arg)
#define LIBUSB20_ME_UINT64_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 64, , 1)
#define LIBUSB20_ME_UINT64_ARRAY_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta, UN, u, 64, [arg], arg)
#define LIBUSB20_ME_SINT64_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta,,, 64, , 1)
#define LIBUSB20_ME_SINT64_ARRAY_T(n, field, arg, ismeta) \
LIBUSB20_ME_INTEGER(n, field, ismeta,,, 64, [arg], arg)
#define LIBUSB20_MAKE_DECODED_FIELD(n, type, field, arg) \
LIBUSB20_ME_##type (n, field, arg, LIBUSB20_NO)
#define LIBUSB20_MAKE_STRUCT(name) \
extern const struct libusb20_me_format \
name##_FORMAT[1]; \
struct name##_DECODED { \
const struct libusb20_me_format *name##_FORMAT; \
name (LIBUSB20_MAKE_DECODED_FIELD,) \
}
#define LIBUSB20_MAKE_STRUCT_FORMAT(name) \
const struct libusb20_me_format \
name##_FORMAT[1] = {{ \
.format = LIBUSB20_MAKE_FORMAT(name), \
.desc = #name, \
.fields = NULL, \
}}
#define LIBUSB20_MAKE_FORMAT_SUB(n, type, field, arg) \
LIBUSB20_ME_##type (n, field, arg, LIBUSB20_YES)
#define LIBUSB20_MAKE_FORMAT(what) (const uint8_t []) \
{ what (LIBUSB20_MAKE_FORMAT_SUB, ) LIBUSB20_ME_MAX, 0, 0 }
#define LIBUSB20_INIT(what, ptr) do { \
memset(ptr, 0, sizeof(*(ptr))); \
(ptr)->what##_FORMAT = what##_FORMAT; \
} while (0)
#define LIBUSB20_DEVICE_DESC(m,n) \
m(n, UINT8_T, bLength, ) \
m(n, UINT8_T, bDescriptorType, ) \
m(n, UINT16_T, bcdUSB, ) \
m(n, UINT8_T, bDeviceClass, ) \
m(n, UINT8_T, bDeviceSubClass, ) \
m(n, UINT8_T, bDeviceProtocol, ) \
m(n, UINT8_T, bMaxPacketSize0, ) \
m(n, UINT16_T, idVendor, ) \
m(n, UINT16_T, idProduct, ) \
m(n, UINT16_T, bcdDevice, ) \
m(n, UINT8_T, iManufacturer, ) \
m(n, UINT8_T, iProduct, ) \
m(n, UINT8_T, iSerialNumber, ) \
m(n, UINT8_T, bNumConfigurations, ) \
LIBUSB20_MAKE_STRUCT(LIBUSB20_DEVICE_DESC);
#define LIBUSB20_ENDPOINT_DESC(m,n) \
m(n, UINT8_T, bLength, ) \
m(n, UINT8_T, bDescriptorType, ) \
m(n, UINT8_T, bEndpointAddress, ) \
m(n, UINT8_T, bmAttributes, ) \
m(n, UINT16_T, wMaxPacketSize, ) \
m(n, UINT8_T, bInterval, ) \
m(n, UINT8_T, bRefresh, ) \
m(n, UINT8_T, bSynchAddress, ) \
LIBUSB20_MAKE_STRUCT(LIBUSB20_ENDPOINT_DESC);
#define LIBUSB20_INTERFACE_DESC(m,n) \
m(n, UINT8_T, bLength, ) \
m(n, UINT8_T, bDescriptorType, ) \
m(n, UINT8_T, bInterfaceNumber, ) \
m(n, UINT8_T, bAlternateSetting, ) \
m(n, UINT8_T, bNumEndpoints, ) \
m(n, UINT8_T, bInterfaceClass, ) \
m(n, UINT8_T, bInterfaceSubClass, ) \
m(n, UINT8_T, bInterfaceProtocol, ) \
m(n, UINT8_T, iInterface, ) \
LIBUSB20_MAKE_STRUCT(LIBUSB20_INTERFACE_DESC);
#define LIBUSB20_CONFIG_DESC(m,n) \
m(n, UINT8_T, bLength, ) \
m(n, UINT8_T, bDescriptorType, ) \
m(n, UINT16_T, wTotalLength, ) \
m(n, UINT8_T, bNumInterfaces, ) \
m(n, UINT8_T, bConfigurationValue, ) \
m(n, UINT8_T, iConfiguration, ) \
m(n, UINT8_T, bmAttributes, ) \
m(n, UINT8_T, bMaxPower, ) \
LIBUSB20_MAKE_STRUCT(LIBUSB20_CONFIG_DESC);
#define LIBUSB20_CONTROL_SETUP(m,n) \
m(n, UINT8_T, bmRequestType, ) \
m(n, UINT8_T, bRequest, ) \
m(n, UINT16_T, wValue, ) \
m(n, UINT16_T, wIndex, ) \
m(n, UINT16_T, wLength, ) \
LIBUSB20_MAKE_STRUCT(LIBUSB20_CONTROL_SETUP);
#define LIBUSB20_SS_ENDPT_COMP_DESC(m,n) \
m(n, UINT8_T, bLength, ) \
m(n, UINT8_T, bDescriptorType, ) \
m(n, UINT8_T, bMaxBurst, ) \
m(n, UINT8_T, bmAttributes, ) \
m(n, UINT16_T, wBytesPerInterval, ) \
LIBUSB20_MAKE_STRUCT(LIBUSB20_SS_ENDPT_COMP_DESC);
#define LIBUSB20_USB_20_DEVCAP_DESC(m,n) \
m(n, UINT8_T, bLength, ) \
m(n, UINT8_T, bDescriptorType, ) \
m(n, UINT8_T, bDevCapabilityType, ) \
m(n, UINT32_T, bmAttributes, ) \
LIBUSB20_MAKE_STRUCT(LIBUSB20_USB_20_DEVCAP_DESC);
#define LIBUSB20_SS_USB_DEVCAP_DESC(m,n) \
m(n, UINT8_T, bLength, ) \
m(n, UINT8_T, bDescriptorType, ) \
m(n, UINT8_T, bDevCapabilityType, ) \
m(n, UINT8_T, bmAttributes, ) \
m(n, UINT16_T, wSpeedSupported, ) \
m(n, UINT8_T, bFunctionalitySupport, ) \
m(n, UINT8_T, bU1DevExitLat, ) \
m(n, UINT16_T, wU2DevExitLat, ) \
LIBUSB20_MAKE_STRUCT(LIBUSB20_SS_USB_DEVCAP_DESC);
#ifndef bNumDeviceCapabilities
#define bNumDeviceCapabilities bNumDeviceCaps
#endif
#define LIBUSB20_BOS_DESCRIPTOR(m,n) \
m(n, UINT8_T, bLength, ) \
m(n, UINT8_T, bDescriptorType, ) \
m(n, UINT16_T, wTotalLength, ) \
m(n, UINT8_T, bNumDeviceCaps, ) \
LIBUSB20_MAKE_STRUCT(LIBUSB20_BOS_DESCRIPTOR);
enum libusb20_class_code {
LIBUSB20_CLASS_PER_INTERFACE = 0,
LIBUSB20_CLASS_AUDIO = 1,
LIBUSB20_CLASS_COMM = 2,
LIBUSB20_CLASS_HID = 3,
LIBUSB20_CLASS_PRINTER = 7,
LIBUSB20_CLASS_PTP = 6,
LIBUSB20_CLASS_MASS_STORAGE = 8,
LIBUSB20_CLASS_HUB = 9,
LIBUSB20_CLASS_DATA = 10,
LIBUSB20_CLASS_VENDOR_SPEC = 0xff,
};
enum libusb20_descriptor_type {
LIBUSB20_DT_DEVICE = 0x01,
LIBUSB20_DT_CONFIG = 0x02,
LIBUSB20_DT_STRING = 0x03,
LIBUSB20_DT_INTERFACE = 0x04,
LIBUSB20_DT_ENDPOINT = 0x05,
LIBUSB20_DT_HID = 0x21,
LIBUSB20_DT_REPORT = 0x22,
LIBUSB20_DT_PHYSICAL = 0x23,
LIBUSB20_DT_HUB = 0x29,
LIBUSB20_DT_BOS = 0x0f,
LIBUSB20_DT_DEVICE_CAPABILITY = 0x10,
LIBUSB20_DT_SS_ENDPOINT_COMPANION = 0x30,
};
enum libusb20_device_capability_type {
LIBUSB20_WIRELESS_USB_DEVICE_CAPABILITY = 0x1,
LIBUSB20_USB_2_0_EXTENSION_DEVICE_CAPABILITY = 0x2,
LIBUSB20_SS_USB_DEVICE_CAPABILITY = 0x3,
LIBUSB20_CONTAINER_ID_DEVICE_CAPABILITY = 0x4,
};
#define LIBUSB20_DT_DEVICE_SIZE 18
#define LIBUSB20_DT_CONFIG_SIZE 9
#define LIBUSB20_DT_INTERFACE_SIZE 9
#define LIBUSB20_DT_ENDPOINT_SIZE 7
#define LIBUSB20_DT_ENDPOINT_AUDIO_SIZE 9
#define LIBUSB20_DT_HUB_NONVAR_SIZE 7
#define LIBUSB20_DT_SS_ENDPOINT_COMPANION_SIZE 6
#define LIBUSB20_DT_BOS_SIZE 5
#define LIBUSB20_USB_2_0_EXTENSION_DEVICE_CAPABILITY_SIZE 7
#define LIBUSB20_SS_USB_DEVICE_CAPABILITY_SIZE 10
#define LIBUSB20_ENDPOINT_ADDRESS_MASK 0x0f
#define LIBUSB20_ENDPOINT_DIR_MASK 0x80
enum libusb20_endpoint_direction {
LIBUSB20_ENDPOINT_IN = 0x80,
LIBUSB20_ENDPOINT_OUT = 0x00,
};
#define LIBUSB20_TRANSFER_TYPE_MASK 0x03
enum libusb20_transfer_type {
LIBUSB20_TRANSFER_TYPE_CONTROL = 0,
LIBUSB20_TRANSFER_TYPE_ISOCHRONOUS = 1,
LIBUSB20_TRANSFER_TYPE_BULK = 2,
LIBUSB20_TRANSFER_TYPE_INTERRUPT = 3,
};
enum libusb20_standard_request {
LIBUSB20_REQUEST_GET_STATUS = 0x00,
LIBUSB20_REQUEST_CLEAR_FEATURE = 0x01,
LIBUSB20_REQUEST_SET_FEATURE = 0x03,
LIBUSB20_REQUEST_SET_ADDRESS = 0x05,
LIBUSB20_REQUEST_GET_DESCRIPTOR = 0x06,
LIBUSB20_REQUEST_SET_DESCRIPTOR = 0x07,
LIBUSB20_REQUEST_GET_CONFIGURATION = 0x08,
LIBUSB20_REQUEST_SET_CONFIGURATION = 0x09,
LIBUSB20_REQUEST_GET_INTERFACE = 0x0A,
LIBUSB20_REQUEST_SET_INTERFACE = 0x0B,
LIBUSB20_REQUEST_SYNCH_FRAME = 0x0C,
LIBUSB20_REQUEST_SET_SEL = 0x30,
LIBUSB20_REQUEST_SET_ISOCH_DELAY = 0x31,
};
enum libusb20_request_type {
LIBUSB20_REQUEST_TYPE_STANDARD = (0x00 << 5),
LIBUSB20_REQUEST_TYPE_CLASS = (0x01 << 5),
LIBUSB20_REQUEST_TYPE_VENDOR = (0x02 << 5),
LIBUSB20_REQUEST_TYPE_RESERVED = (0x03 << 5),
};
enum libusb20_request_recipient {
LIBUSB20_RECIPIENT_DEVICE = 0x00,
LIBUSB20_RECIPIENT_INTERFACE = 0x01,
LIBUSB20_RECIPIENT_ENDPOINT = 0x02,
LIBUSB20_RECIPIENT_OTHER = 0x03,
};
#define LIBUSB20_ISO_SYNC_TYPE_MASK 0x0C
enum libusb20_iso_sync_type {
LIBUSB20_ISO_SYNC_TYPE_NONE = 0,
LIBUSB20_ISO_SYNC_TYPE_ASYNC = 1,
LIBUSB20_ISO_SYNC_TYPE_ADAPTIVE = 2,
LIBUSB20_ISO_SYNC_TYPE_SYNC = 3,
};
#define LIBUSB20_ISO_USAGE_TYPE_MASK 0x30
enum libusb20_iso_usage_type {
LIBUSB20_ISO_USAGE_TYPE_DATA = 0,
LIBUSB20_ISO_USAGE_TYPE_FEEDBACK = 1,
LIBUSB20_ISO_USAGE_TYPE_IMPLICIT = 2,
};
struct libusb20_endpoint {
struct LIBUSB20_ENDPOINT_DESC_DECODED desc;
struct libusb20_me_struct extra;
} __aligned(sizeof(void *));
struct libusb20_interface {
struct LIBUSB20_INTERFACE_DESC_DECODED desc;
struct libusb20_me_struct extra;
struct libusb20_interface *altsetting;
struct libusb20_endpoint *endpoints;
uint8_t num_altsetting;
uint8_t num_endpoints;
} __aligned(sizeof(void *));
struct libusb20_config {
struct LIBUSB20_CONFIG_DESC_DECODED desc;
struct libusb20_me_struct extra;
struct libusb20_interface *interface;
uint8_t num_interface;
} __aligned(sizeof(void *));
uint8_t libusb20_me_get_1(const struct libusb20_me_struct *ie, uint16_t offset);
uint16_t libusb20_me_get_2(const struct libusb20_me_struct *ie, uint16_t offset);
uint16_t libusb20_me_encode(void *ptr, uint16_t len, const void *pd);
uint16_t libusb20_me_decode(const void *ptr, uint16_t len, void *pd);
const uint8_t *libusb20_desc_foreach(const struct libusb20_me_struct *pdesc, const uint8_t *psubdesc);
struct libusb20_config *libusb20_parse_config_desc(const void *config_desc);
#if 0
{
#endif
#ifdef __cplusplus
}
#endif
#endif