-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathusb_std.h
264 lines (221 loc) · 7.8 KB
/
usb_std.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* MIT License
*
* Copyright (c) 2020 Kirill Kotyagin
*/
#ifndef USB_STD_H
#define USB_STD_H
#include <stdint.h>
/* USB Descriptor Types */
typedef enum {
usb_descriptor_type_device = 0x01,
usb_descriptor_type_configuration = 0x02,
usb_descriptor_type_string = 0x03,
usb_descriptor_type_interface = 0x04,
usb_descriptor_type_endpoint = 0x05,
usb_descriptor_type_qualifier = 0x06,
usb_descriptor_type_other = 0x07,
usb_descriptor_type_interface_power = 0x08,
usb_descriptor_type_otg = 0x09,
usb_descriptor_type_debug = 0x0a,
usb_descriptor_type_interface_assoc = 0x0b,
usb_descriptor_type_cs_interface = 0x24,
usb_descriptor_type_cs_endpoint = 0x25,
} __attribute__ ((packed)) usb_descriptor_type_t;
/* USB Device Descriptor */
typedef enum {
usb_device_class_device = 0x00,
usb_device_class_audio = 0x01,
usb_device_class_comm = 0x02,
usb_device_class_hid = 0x03,
usb_device_class_physical = 0x05,
usb_device_class_image = 0x06,
usb_device_class_printer = 0x07,
usb_device_class_mass_storage = 0x08,
usb_device_class_hub = 0x09,
usb_device_class_cdc_data = 0x0a,
usb_device_class_smart_card = 0x0b,
usb_device_class_content_security = 0x0d,
usb_device_class_video = 0x0e,
usb_device_class_personal_healthcare = 0x0f,
usb_device_class_audio_video = 0x10,
usb_device_class_billboard = 0x11,
usb_device_class_usbc_bridge = 0x12,
usb_device_class_diagnostic = 0xdc,
usb_device_class_wireless_controller = 0xe0,
usb_device_class_misc = 0xef,
usb_device_class_application_specific = 0xfe,
usb_device_class_vendor_specific = 0xff,
} __attribute__ ((packed)) usb_device_class_t;
typedef enum {
usb_device_subclass_none = 0x00,
usb_device_subclass_iad = 0x02,
} __attribute__ ((packed)) usb_device_subclass_t;
typedef enum {
usb_device_protocol_none = 0x00,
usb_device_protocol_iad = 0x01,
} __attribute__ ((packed)) usb_device_protocol_t;
#define USB_BCD_VERSION(maj, min, rev) (((maj & 0xFF) << 8) | ((min & 0x0F) << 4) | (rev & 0x0F))
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdUSB;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bMaxPacketSize;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bNumConfigurations;
} __attribute__ ((packed)) usb_device_descriptor_t;
/* USB Qualifier Descriptor */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdUSB;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bMaxPacketSize0;
uint8_t bNumConfigurations;
uint8_t bReserved;
} __attribute__((packed)) usb_qualifier_descriptor_t;
/* USB Configuration Descriptor */
#define USB_CFG_ATTR_REMOTE_WAKEUP 0x20
#define USB_CFG_ATTRI_SELF_POWERED 0x40
#define USB_CFG_ATTR_RESERVED 0x80
#define USB_CFG_POWER_MA(mA) ((mA) >> 1)
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_configuration_descriptor_t;
/* USB Interface Descriptor */
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_interface_descriptor_t;
/* USB Interface Association Descriptor */
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_iad_descriptor_t;
/* USB Endpoint Descriptor */
typedef enum {
usb_endpoint_direction_out = 0x00,
usb_endpoint_direction_in = 0x80,
} __attribute__ ((packed)) usb_endpoint_direction_t;
typedef enum {
usb_endpoint_type_control = 0x00,
usb_endpoint_type_isochronous = 0x01,
usb_endpoint_type_bulk = 0x02,
usb_endpoint_type_interrupt = 0x03,
} __attribute__ ((packed)) usb_endpoint_type_t;
typedef enum {
usb_endpoint_attribute_no_sync = 0x00,
usb_endpoint_attribute_async = 0x04,
usb_endpoint_attribute_adaptive = 0x08,
usb_endpoint_attribute_sync = 0x0c
} __attribute__ ((packed)) usb_endpoint_attribute_t;
typedef enum {
usb_endpoint_usage_data = 0x00,
usb_endpoint_usage_feedback = 0x10,
usb_endpoint_usage_implicit_feedback = 0x20
} __attribute__ ((packed)) usb_endpoint_usage_t;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
} __attribute__((packed)) usb_endpoint_descriptor_t;
/* USB String Descriptor */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wString[];
} __attribute__((packed, aligned(2))) usb_string_descriptor_t;
#define __CAT(x,y) x ## y
#define CAT(x,y) __CAT(x,y)
#define USB_ARRAY_DESC(...) {.bLength = 2 + sizeof((uint16_t[]){__VA_ARGS__}),\
.bDescriptorType = usb_descriptor_type_string,\
.wString = {__VA_ARGS__}}
#define USB_STRING_DESC(s) {.bLength = sizeof(CAT(u,s)),\
.bDescriptorType = usb_descriptor_type_string,\
.wString = {CAT(u,s)}}
typedef enum {
usb_language_code_en_US = 0x409,
} __attribute__((packed)) usb_language_code_t;
/* USB Debug Descriptor */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDebugInEndpoint;
uint8_t bDebugOutEndpoint;
} __attribute__((packed)) usb_debug_descriptor_t;
/* USB Control Endpoint Related Definitions */
typedef enum {
usb_setup_direction_host_to_device = 0,
usb_setup_direction_device_to_host = 1
} usb_setup_direction_t;
typedef enum {
usb_setup_type_standard = 0,
usb_setup_type_class = 1,
usb_setup_type_vendor = 2,
usb_setup_type_reserved = 3,
} usb_setup_type_t;
typedef enum {
usb_setup_recepient_device = 0,
usb_setup_recepient_interface = 1,
usb_setup_recepient_endpoint = 2,
usb_setup_recepient_other = 3,
} usb_setup_recepient_t;
typedef struct {
union {
uint8_t bmRequestType;
struct {
usb_setup_recepient_t recepient:5;
usb_setup_type_t type:2;
usb_setup_direction_t direction:1;
};
};
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
uint8_t payload[0];
} __attribute__ ((packed)) usb_setup_t;
typedef enum {
usb_device_request_get_status = 0x00,
usb_device_request_clear_feature = 0x01,
usb_device_request_set_feature = 0x03,
usb_device_request_set_address = 0x05,
usb_device_request_get_descriptor = 0x06,
usb_device_request_set_descriptor = 0x07,
usb_device_request_get_configuration = 0x08,
usb_device_request_set_configuration = 0x09,
} usb_device_request_t;
#endif /* USB_STD_H */