-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathusb_standard.h
207 lines (179 loc) · 4.99 KB
/
usb_standard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#pragma once
#include <stdint.h>
// Device -> host
#define USB_IN 0x80
// Host -> device
#define USB_OUT 0
typedef enum {
USB_SPEED_LOW,
USB_SPEED_FULL,
USB_SPEED_HIGH,
} USB_Speed;
#define USB_REQTYPE_DIRECTION_MASK 0x80
#define USB_REQTYPE_TYPE_MASK 0x60
#define USB_REQTYPE_RECIPIENT_MASK 0x1F
#define USB_REQTYPE_STANDARD (0 << 5)
#define USB_REQTYPE_CLASS (1 << 5)
#define USB_REQTYPE_VENDOR (2 << 5)
#define USB_RECIPIENT_DEVICE (0 << 0)
#define USB_RECIPIENT_INTERFACE (1 << 0)
#define USB_RECIPIENT_ENDPOINT (2 << 0)
#define USB_RECIPIENT_OTHER (3 << 0)
typedef struct {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
} __attribute__ ((packed)) USB_SetupPacket;
// Standard requests
enum {
USB_REQ_GetStatus = 0,
USB_REQ_ClearFeature = 1,
USB_REQ_SetFeature = 3,
USB_REQ_SetAddress = 5,
USB_REQ_GetDescriptor = 6,
USB_REQ_SetDescriptor = 7,
USB_REQ_GetConfiguration = 8,
USB_REQ_SetConfiguration = 9,
USB_REQ_GetInterface = 10,
USB_REQ_SetInterface = 11,
USB_REQ_SynchFrame = 12,
};
enum {
USB_FEATURE_EndpointHalt = 0x00,
USB_FEATURE_DeviceRemoteWakeup = 0x01,
USB_FEATURE_TestMode = 0x02,
};
enum {
USB_DTYPE_Device = 0x01,
USB_DTYPE_Configuration = 0x02,
USB_DTYPE_String = 0x03,
USB_DTYPE_Interface = 0x04,
USB_DTYPE_Endpoint = 0x05,
USB_DTYPE_DeviceQualifier = 0x06,
USB_DTYPE_Other = 0x07,
USB_DTYPE_InterfacePower = 0x08,
USB_DTYPE_InterfaceAssociation = 0x0B,
USB_DTYPE_CSInterface = 0x24,
USB_DTYPE_CSEndpoint = 0x25,
} USB_dtype;
typedef enum {
USB_CSCP_NoDeviceClass = 0x00,
USB_CSCP_NoDeviceSubclass = 0x00,
USB_CSCP_NoDeviceProtocol = 0x00,
USB_CSCP_VendorSpecificClass = 0xFF,
USB_CSCP_VendorSpecificSubclass = 0xFF,
USB_CSCP_VendorSpecificProtocol = 0xFF,
USB_CSCP_IADDeviceClass = 0xEF,
USB_CSCP_IADDeviceSubclass = 0x02,
USB_CSCP_IADDeviceProtocol = 0x01,
} USB_cscp;
#define USB_CONFIG_POWER_MA(mA) ((mA)/2)
#define USB_STRING_LEN(c) (sizeof(USB_DescriptorHeader) + ((c) * 2))
#define USB_LANGUAGE_EN_US 0x0409
#define ENDPOINT_DESCRIPTOR_DIR_IN 0x80
#define ENDPOINT_DESCRIPTOR_DIR_OUT 0x00
#define USB_CONFIG_ATTR_BUSPOWERED 0x80
#define USB_CONFIG_ATTR_SELFPOWERED 0x40
#define USB_CONFIG_ATTR_REMOTEWAKEUP 0x20
#define ENDPOINT_ATTR_NO_SYNC (0 << 2)
#define ENDPOINT_ATTR_ASYNC (1 << 2)
#define ENDPOINT_ATTR_ADAPTIVE (2 << 2)
#define ENDPOINT_ATTR_SYNC (3 << 2)
#define ENDPOINT_USAGE_DATA (0 << 4)
#define ENDPOINT_USAGE_FEEDBACK (1 << 4)
#define ENDPOINT_USAGE_IMPLICIT_FEEDBACK (2 << 4)
#define USB_EP_TYPE_CONTROL 0x00
#define USB_EP_TYPE_ISOCHRONOUS 0x01
#define USB_EP_TYPE_BULK 0x02
#define USB_EP_TYPE_INTERRUPT 0x03
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
} __attribute__ ((packed)) USB_DescriptorHeader;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdUSB;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bMaxPacketSize0;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
} __attribute__ ((packed)) USB_DeviceDescriptor;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdUSB;
uint8_t bDeviceSubClass;
uint8_t bMaxPacketSize0;
uint8_t bNumConfigurations;
uint8_t bReserved;
} __attribute__ ((packed)) USB_DeviceQualifierDescriptor;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wTotalLength;
uint8_t bNumInterfaces;
uint8_t bConfigurationValue;
uint8_t iConfiguration;
uint8_t bmAttributes;
uint8_t bMaxPower;
} __attribute__ ((packed)) USB_ConfigurationDescriptor;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bNumEndpoints;
uint8_t bInterfaceClass;
uint8_t bInterfaceSubClass;
uint8_t bInterfaceProtocol;
uint8_t iInterface;
} __attribute__ ((packed)) USB_InterfaceDescriptor;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bFirstInterface;
uint8_t bInterfaceCount;
uint8_t bFunctionClass;
uint8_t bFunctionSubClass;
uint8_t bFunctionProtocol;
uint8_t iFunction;
} __attribute__ ((packed)) USB_InterfaceAssociationDescriptor;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
} __attribute__ ((packed)) USB_EndpointDescriptor;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
__CHAR16_TYPE__ bString[];
} __attribute__ ((packed)) USB_StringDescriptor;
/// Microsoft WCID descriptor
typedef struct {
uint8_t bFirstInterfaceNumber;
uint8_t reserved1;
uint8_t compatibleID[8];
uint8_t subCompatibleID[8];
uint8_t reserved2[6];
} __attribute__((packed)) USB_MicrosoftCompatibleDescriptor_Interface;
typedef struct {
uint32_t dwLength;
uint16_t bcdVersion;
uint16_t wIndex;
uint8_t bCount;
uint8_t reserved[7];
USB_MicrosoftCompatibleDescriptor_Interface interfaces[];
} __attribute__((packed)) USB_MicrosoftCompatibleDescriptor;