forked from zeldin/fx3lafw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptors.c
260 lines (251 loc) · 7.95 KB
/
descriptors.c
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
#include <stdint.h>
#include <stddef.h>
#include <bsp/uart.h>
#include <bsp/usb.h>
#include "descriptors.h"
static const struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint16_t usb_version;
uint8_t device_class, sub_class, protocol, max_packet_size;
uint16_t vendor, product, dev_version;
uint8_t manuf_index, product_index, serial_index, num_configurations;
} highspeed_device_descriptor = {
.length = sizeof(highspeed_device_descriptor),
.descriptor_type = FX3_USB_DESCRIPTOR_DEVICE,
.usb_version = 0x0200,
.device_class = 0xff,
.sub_class = 0xff,
.protocol = 0xff,
.max_packet_size = 64 /* 2^9 */,
.vendor = 0x04b4,
.product = 0x00f3,
.dev_version = 0x0001,
.manuf_index = 1,
.product_index = 2,
.serial_index = 3,
.num_configurations = 1
};
static const struct __attribute__((packed)) {
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint16_t total_length;
uint8_t num_interfaces, configuration_value, configuration_string;
uint8_t attributes, max_power;
} configuration;
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint8_t interface_number, alternate_setting, num_endpoints;
uint8_t interface_class, interface_subclass, interface_protocol;
uint8_t interface_string;
} interfaces[1];
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint8_t endpoint_address, attributes;
uint16_t max_packet_size;
uint8_t interval;
} endpoints[1];
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint8_t max_burst, max_streams;
uint16_t service_interval;
} companions[1];
} highspeed_configuration_descriptor = {
.configuration = {
.length = sizeof(highspeed_configuration_descriptor.configuration),
.descriptor_type = FX3_USB_DESCRIPTOR_CONFIGURATION,
.total_length = sizeof(highspeed_configuration_descriptor),
.num_interfaces = 1,
.configuration_value = 1,
.configuration_string = 0,
.attributes = 0x80,
.max_power = 100/2
},
.interfaces[0] = {
.length = sizeof(highspeed_configuration_descriptor.interfaces[0]),
.descriptor_type = FX3_USB_DESCRIPTOR_INTERFACE,
.interface_number = 0,
.alternate_setting = 0,
.num_endpoints = 1,
.interface_class = 0xff,
.interface_subclass = 0xff,
.interface_protocol = 0xff,
.interface_string = 0
},
.endpoints[0] = {
.length = sizeof(highspeed_configuration_descriptor.endpoints[0]),
.descriptor_type = FX3_USB_DESCRIPTOR_ENDPOINT,
.endpoint_address = 0x82,
.attributes = 2 /* Bulk */,
.max_packet_size = 512,
.interval = 0
},
.companions[0] = {
.length = sizeof(highspeed_configuration_descriptor.companions[0]),
.descriptor_type = FX3_USB_DESCRIPTOR_SS_EP_COMPANION,
.max_burst = 0,
.max_streams = 0,
.service_interval = 0
},
};
static const struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint16_t usb_version;
uint8_t device_class, sub_class, protocol, max_packet_size;
uint16_t vendor, product, dev_version;
uint8_t manuf_index, product_index, serial_index, num_configurations;
} superspeed_device_descriptor = {
.length = sizeof(superspeed_device_descriptor),
.descriptor_type = FX3_USB_DESCRIPTOR_DEVICE,
.usb_version = 0x0300,
.device_class = 0xff,
.sub_class = 0xff,
.protocol = 0xff,
.max_packet_size = 9 /* 2^9 */,
.vendor = 0x04b4,
.product = 0x00f3,
.dev_version = 0x0001,
.manuf_index = 1,
.product_index = 2,
.serial_index = 3,
.num_configurations = 1
};
static const struct __attribute__((packed)) {
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint16_t total_length;
uint8_t num_interfaces, configuration_value, configuration_string;
uint8_t attributes, max_power;
} configuration;
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint8_t interface_number, alternate_setting, num_endpoints;
uint8_t interface_class, interface_subclass, interface_protocol;
uint8_t interface_string;
} interfaces[1];
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint8_t endpoint_address, attributes;
uint16_t max_packet_size;
uint8_t interval;
} endpoints[1];
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint8_t max_burst, max_streams;
uint16_t service_interval;
} companions[1];
} superspeed_configuration_descriptor = {
.configuration = {
.length = sizeof(superspeed_configuration_descriptor.configuration),
.descriptor_type = FX3_USB_DESCRIPTOR_CONFIGURATION,
.total_length = sizeof(superspeed_configuration_descriptor),
.num_interfaces = 1,
.configuration_value = 1,
.configuration_string = 0,
.attributes = 0x80,
.max_power = 100/2
},
.interfaces[0] = {
.length = sizeof(superspeed_configuration_descriptor.interfaces[0]),
.descriptor_type = FX3_USB_DESCRIPTOR_INTERFACE,
.interface_number = 0,
.alternate_setting = 0,
.num_endpoints = 1,
.interface_class = 0xff,
.interface_subclass = 0xff,
.interface_protocol = 0xff,
.interface_string = 0
},
.endpoints[0] = {
.length = sizeof(superspeed_configuration_descriptor.endpoints[0]),
.descriptor_type = FX3_USB_DESCRIPTOR_ENDPOINT,
.endpoint_address = 0x82,
.attributes = 2 /* Bulk */,
.max_packet_size = 1024,
.interval = 0
},
.companions[0] = {
.length = sizeof(superspeed_configuration_descriptor.companions[0]),
.descriptor_type = FX3_USB_DESCRIPTOR_SS_EP_COMPANION,
.max_burst = 0,
.max_streams = 0,
.service_interval = 0
},
};
static const struct __attribute__((packed)) {
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint16_t total_length;
uint8_t num_capability_descriptors;
} bos;
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint8_t usb_version;
uint32_t features;
} usb2_caps;
struct __attribute__((packed)) {
uint8_t length, descriptor_type;
uint8_t usb_version;
uint8_t dev_features;
uint16_t speeds;
uint8_t functionality;
uint8_t u1_exit_latency;
uint16_t u2_exit_latency;
} usb3_caps;
} bos_descriptor = {
.bos = {
.length = sizeof(bos_descriptor.bos),
.descriptor_type = FX3_USB_DESCRIPTOR_BOS,
.total_length = sizeof(bos_descriptor),
.num_capability_descriptors = 2
},
.usb2_caps = {
.length = sizeof(bos_descriptor.usb2_caps),
.descriptor_type = FX3_USB_DESCRIPTOR_DEV_CAPABILITY,
.usb_version = 2,
.features = 2 /* LPM support */
},
.usb3_caps = {
.length = sizeof(bos_descriptor.usb3_caps),
.descriptor_type = FX3_USB_DESCRIPTOR_DEV_CAPABILITY,
.usb_version = 3,
.dev_features = 0,
.speeds = 0xe /* SS, HS, FS */,
.functionality = 3,
.u1_exit_latency = 0,
.u2_exit_latency = 0
},
};
static const uint16_t * const string_descriptors[] = {
[0] = u"\x0304" "\x0409", /* US english only */
[1] = u"\x030e" "sigrok",
[2] = u"\x0310" "fx3lafw",
[3] = u"\x0312" "12345678",
};
const void *GetDescriptor(uint8_t descriptor_type, uint8_t descriptor_no, Fx3UsbSpeed_t s)
{
switch(descriptor_type) {
case FX3_USB_DESCRIPTOR_DEVICE:
if (descriptor_no == 0){
if (s == FX3_USB_SUPER_SPEED)
return &superspeed_device_descriptor;
return &highspeed_device_descriptor;
} else
break;
case FX3_USB_DESCRIPTOR_CONFIGURATION:
if (descriptor_no == 0){
if (s == FX3_USB_SUPER_SPEED)
return &superspeed_configuration_descriptor;
return &highspeed_configuration_descriptor;
} else
break;
case FX3_USB_DESCRIPTOR_STRING:
if (descriptor_no < sizeof(string_descriptors)/sizeof(string_descriptors[0]))
return string_descriptors[descriptor_no];
break;
case FX3_USB_DESCRIPTOR_BOS:
if (descriptor_no == 0)
return &bos_descriptor;
break;
}
return NULL;
}