-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwinusb.rs
More file actions
437 lines (425 loc) · 6.65 KB
/
winusb.rs
File metadata and controls
437 lines (425 loc) · 6.65 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use num_enum::TryFromPrimitive;
use usb_device::class_prelude::*;
use usb_device::control::RequestType;
const fn u16_low(val: u16) -> u8 {
val.to_le_bytes()[0]
}
const fn u16_high(val: u16) -> u8 {
val.to_le_bytes()[1]
}
#[allow(non_snake_case)]
#[repr(u16)]
#[derive(TryFromPrimitive)]
pub enum OSFeatureDescriptorType {
CompatibleID = 4,
Properties = 5,
Descriptor = 7,
}
const LEN: u16 = 330;
const VENDOR_CODE: u8 = 0x41;
const DAP_V2_INTERFACE: u8 = 3;
const DFU_INTERFACE: u8 = 4;
enum MsDescriptorTypes {
Header = 0x0,
HeaderConfiguration = 0x1,
HeaderFunction = 0x2,
CompatibleId = 0x3,
RegistryProperty = 0x4,
}
/// Microsoft OS 2.0 descriptor, according to https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-os-2-0-descriptors-specification
///
/// For interface ['DAP_V2_INTERFACE'] this configures:
/// - compatible ID 'WinUSB'
/// - registry property DeviceInterfaceGUIDs = ['{CDB3B5AD-293B-4663-AA36-1AAE46463776}']
///
/// For interface ['DFU_INTERFACE']:
/// - compatible ID 'WinUSB'
/// - registry property DeviceInterfaceGUIDs = ['{A5DCBF10-6530-11D2-901F-00C04FB951ED}']
const MS_OS_DESCRIPTOR: [u8; LEN as usize] = [
0xa,
0x00, // Length 10 bytes
MsDescriptorTypes::Header as u8,
0x00, // HEADER_DESCRIPTOR
0x00,
0x00,
0x03,
0x06, // Windows version
u16_low(LEN),
u16_high(LEN), // Total descriptor length
// Function header,
0x8,
0x0, // Length 8
MsDescriptorTypes::HeaderFunction as u8,
0x00,
DAP_V2_INTERFACE, // First interface (dap v2)
0x0, // reserved
8 + 20 + 132,
0x00, // Subset length, including header
// compatible ID descriptor
20,
0x00, // length 20
MsDescriptorTypes::CompatibleId as u8,
0x00,
b'W',
b'I',
b'N',
b'U',
b'S',
b'B',
0x00,
0x00, // Compatible ID: 8 bytes ASCII
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00, // Sub-Compatible ID: 8 bytes ASCII
// Registry property
80 + 2 + 42 + 2 + 2 + 2 + 2,
0x00, // length
MsDescriptorTypes::RegistryProperty as u8,
0x00,
7,
0, // Data type: multi sz
42,
0x00, // property name length,
b'D',
0,
b'e',
0,
b'v',
0,
b'i',
0,
b'c',
0,
b'e',
0,
b'I',
0,
b'n',
0,
b't',
0,
b'e',
0,
b'r',
0,
b'f',
0,
b'a',
0,
b'c',
0,
b'e',
0,
b'G',
0,
b'U',
0,
b'I',
0,
b'D',
0,
b's',
0,
0,
0,
80,
0x00, // data length
b'{',
0,
b'C',
0,
b'D',
0,
b'B',
0,
b'3',
0,
b'B',
0,
b'5',
0,
b'A',
0,
b'D',
0,
b'-',
0,
b'2',
0,
b'9',
0,
b'3',
0,
b'B',
0,
b'-',
0,
b'4',
0,
b'6',
0,
b'6',
0,
b'3',
0,
b'-',
0,
b'A',
0,
b'A',
0,
b'3',
0,
b'6',
0,
b'-',
0,
b'1',
0,
b'A',
0,
b'A',
0,
b'E',
0,
b'4',
0,
b'6',
0,
b'4',
0,
b'6',
0,
b'3',
0,
b'7',
0,
b'7',
0,
b'6',
0,
b'}',
0,
0,
0,
0,
0,
// Function header,
0x8,
0x0, // Length 8
MsDescriptorTypes::HeaderFunction as u8,
0x00,
DFU_INTERFACE, // First interface (dap v2 -> 1)
0x0, // reserved
8 + 20 + 132, // Header + compatible ID
0x00, // Subset length, including header
// compatible ID descriptor
20,
0x00, // length 20
MsDescriptorTypes::CompatibleId as u8,
0x00,
b'W',
b'I',
b'N',
b'U',
b'S',
b'B',
0x00,
0x00, // Compatible ID: 8 bytes ASCII
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00, // Sub-Compatible ID: 8 bytes ASCII
// Registry property
80 + 2 + 42 + 2 + 2 + 2 + 2,
0x00, // length
MsDescriptorTypes::RegistryProperty as u8,
0x00,
7,
0, // Data type: multi sz
42,
0x00, // property name length,
b'D',
0,
b'e',
0,
b'v',
0,
b'i',
0,
b'c',
0,
b'e',
0,
b'I',
0,
b'n',
0,
b't',
0,
b'e',
0,
b'r',
0,
b'f',
0,
b'a',
0,
b'c',
0,
b'e',
0,
b'G',
0,
b'U',
0,
b'I',
0,
b'D',
0,
b's',
0,
0,
0,
80,
0x00, // data length
b'{',
0,
b'A',
0,
b'5',
0,
b'D',
0,
b'C',
0,
b'B',
0,
b'F',
0,
b'1',
0,
b'0',
0,
b'-',
0,
b'6',
0,
b'5',
0,
b'3',
0,
b'0',
0,
b'-',
0,
b'1',
0,
b'1',
0,
b'D',
0,
b'2',
0,
b'-',
0,
b'9',
0,
b'0',
0,
b'1',
0,
b'F',
0,
b'-',
0,
b'0',
0,
b'0',
0,
b'C',
0,
b'0',
0,
b'4',
0,
b'F',
0,
b'B',
0,
b'9',
0,
b'5',
0,
b'1',
0,
b'E',
0,
b'D',
0,
b'}',
0,
0,
0,
0,
0,
];
pub struct MicrosoftDescriptors;
impl<B: UsbBus> UsbClass<B> for MicrosoftDescriptors {
fn get_bos_descriptors(&self, writer: &mut BosWriter) -> usb_device::Result<()> {
writer.capability(
5, // Platform capability
&[
0, // reserved
0xdf,
0x60,
0xdd,
0xd8,
0x89,
0x45,
0xc7,
0x4c,
0x9c,
0xd2,
0x65,
0x9d,
0x9e,
0x64,
0x8A,
0x9f, // platform capability UUID , Microsoft OS 2.0 platform compabitility
0x00,
0x00,
0x03,
0x06, // Minimum compatible Windows version (8.1)
u16_low(LEN),
u16_high(LEN), // desciptor set total len ,
VENDOR_CODE,
0x0, // Device does not support alternate enumeration
],
)
}
fn control_in(&mut self, xfer: ControlIn<B>) {
let req = xfer.request();
if req.request_type != RequestType::Vendor {
return;
}
// The Microsoft OS descriptors are requested with the vendor code which
// is returned in the BOS descriptor.
if req.request == VENDOR_CODE {
if req.index == 0x7 {
xfer.accept_with_static(&MS_OS_DESCRIPTOR).ok();
} else {
xfer.reject().ok();
}
}
}
}