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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright 2019 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::collections::BTreeMap;
use std::mem::size_of;
use std::ops::Deref;

use base::warn;
use zerocopy::FromBytes;

use crate::types;
use crate::types::Descriptor;
use crate::types::DescriptorHeader;
use crate::types::EndpointDescriptor;
use crate::Error;
use crate::Result;

#[derive(Clone)]
pub struct DeviceDescriptorTree {
    // Full descriptor tree in the original format returned by the device.
    raw: Vec<u8>,
    inner: types::DeviceDescriptor,
    // Map of bConfigurationValue to ConfigDescriptor
    config_descriptors: BTreeMap<u8, ConfigDescriptorTree>,
    // Map of config index to bConfigurationValue.
    config_values: BTreeMap<u8, u8>,
}

#[derive(Clone)]
pub struct ConfigDescriptorTree {
    offset: usize,
    inner: types::ConfigDescriptor,
    // Map of (bInterfaceNumber, bAlternateSetting) to InterfaceDescriptor
    interface_descriptors: BTreeMap<(u8, u8), InterfaceDescriptorTree>,
}

#[derive(Clone)]
pub struct InterfaceDescriptorTree {
    offset: usize,
    inner: types::InterfaceDescriptor,
    // Map of bEndpointAddress to EndpointDescriptor
    endpoint_descriptors: BTreeMap<u8, EndpointDescriptor>,
}

impl DeviceDescriptorTree {
    pub fn get_config_descriptor(&self, config_value: u8) -> Option<&ConfigDescriptorTree> {
        self.config_descriptors.get(&config_value)
    }

    /// Retrieve the Nth configuration descriptor in the device descriptor.
    /// `config_index`: 0-based index into the list of configuration descriptors.
    pub fn get_config_descriptor_by_index(
        &self,
        config_index: u8,
    ) -> Option<&ConfigDescriptorTree> {
        self.config_descriptors
            .get(self.config_values.get(&config_index)?)
    }

    /// Access the raw descriptor tree as a slice of bytes.
    pub fn raw(&self) -> &[u8] {
        &self.raw
    }
}

impl Deref for DeviceDescriptorTree {
    type Target = types::DeviceDescriptor;

    fn deref(&self) -> &types::DeviceDescriptor {
        &self.inner
    }
}

impl ConfigDescriptorTree {
    /// Get interface by number and alt setting.
    pub fn get_interface_descriptor(
        &self,
        interface_num: u8,
        alt_setting: u8,
    ) -> Option<&InterfaceDescriptorTree> {
        self.interface_descriptors
            .get(&(interface_num, alt_setting))
    }

    /// Get the offset of this configuration descriptor within the raw descriptor tree.
    pub fn offset(&self) -> usize {
        self.offset
    }
}

impl Deref for ConfigDescriptorTree {
    type Target = types::ConfigDescriptor;

    fn deref(&self) -> &types::ConfigDescriptor {
        &self.inner
    }
}

impl InterfaceDescriptorTree {
    pub fn get_endpoint_descriptor(&self, ep_idx: u8) -> Option<&EndpointDescriptor> {
        self.endpoint_descriptors.get(&ep_idx)
    }

    /// Get the offset of this interface descriptor within the raw descriptor tree.
    pub fn offset(&self) -> usize {
        self.offset
    }
}

impl Deref for InterfaceDescriptorTree {
    type Target = types::InterfaceDescriptor;

    fn deref(&self) -> &types::InterfaceDescriptor {
        &self.inner
    }
}

/// Given `data` containing a full set of descriptors as provided by the Linux kernel
/// usbdevfs `descriptors` file, parse the descriptors into a tree data structure.
pub fn parse_usbfs_descriptors(data: &[u8]) -> Result<DeviceDescriptorTree> {
    let mut offset = 0;

    // Find the next descriptor of type T and return it and its offset.
    // Any other descriptors encountered while searching for the expected type are skipped.
    // The `offset` parameter will be advanced to point to the next byte after the returned
    // descriptor.
    fn next_descriptor<T: Descriptor + FromBytes>(
        data: &[u8],
        offset: &mut usize,
    ) -> Result<(T, usize)> {
        let desc_type = T::descriptor_type() as u8;
        loop {
            let hdr = DescriptorHeader::read_from(
                data.get(*offset..*offset + size_of::<DescriptorHeader>())
                    .ok_or(Error::DescriptorParse)?,
            )
            .ok_or(Error::DescriptorParse)?;
            if hdr.bDescriptorType == desc_type {
                if usize::from(hdr.bLength) < size_of::<DescriptorHeader>() + size_of::<T>() {
                    return Err(Error::DescriptorParse);
                }

                let desc_offset = *offset;

                *offset += size_of::<DescriptorHeader>();
                let desc = T::read_from(
                    data.get(*offset..*offset + size_of::<T>())
                        .ok_or(Error::DescriptorParse)?,
                )
                .ok_or(Error::DescriptorParse)?;
                *offset += hdr.bLength as usize - size_of::<DescriptorHeader>();
                return Ok((desc, desc_offset));
            } else {
                // Finding a ConfigDescriptor while looking for InterfaceDescriptor means
                // that we should advance to the next device configuration.
                if desc_type == types::InterfaceDescriptor::descriptor_type() as u8
                    && hdr.bDescriptorType == types::ConfigDescriptor::descriptor_type() as u8
                {
                    return Err(Error::DescriptorParse);
                }

                // Make sure we make forward progress.
                if hdr.bLength == 0 {
                    return Err(Error::DescriptorParse);
                }

                // Skip this entire descriptor, since it's not the right type.
                *offset += hdr.bLength as usize;
            }
        }
    }

    let (raw_device_descriptor, _) = next_descriptor::<types::DeviceDescriptor>(data, &mut offset)?;
    let mut device_descriptor = DeviceDescriptorTree {
        raw: data.into(),
        inner: raw_device_descriptor,
        config_descriptors: BTreeMap::new(),
        config_values: BTreeMap::new(),
    };

    for cfg_idx in 0..device_descriptor.bNumConfigurations {
        if let Ok((raw_config_descriptor, config_offset)) =
            next_descriptor::<types::ConfigDescriptor>(&device_descriptor.raw, &mut offset)
        {
            let mut config_descriptor = ConfigDescriptorTree {
                offset: config_offset,
                inner: raw_config_descriptor,
                interface_descriptors: BTreeMap::new(),
            };

            while let Ok((raw_interface_descriptor, interface_offset)) =
                next_descriptor::<types::InterfaceDescriptor>(&device_descriptor.raw, &mut offset)
            {
                let mut interface_descriptor = InterfaceDescriptorTree {
                    offset: interface_offset,
                    inner: raw_interface_descriptor,
                    endpoint_descriptors: BTreeMap::new(),
                };

                for ep_idx in 0..interface_descriptor.bNumEndpoints {
                    if let Ok((endpoint_descriptor, _)) =
                        next_descriptor::<EndpointDescriptor>(&device_descriptor.raw, &mut offset)
                    {
                        interface_descriptor
                            .endpoint_descriptors
                            .insert(ep_idx, endpoint_descriptor);
                    } else {
                        warn!("Could not read endpoint descriptor {}", ep_idx);
                        break;
                    }
                }

                config_descriptor.interface_descriptors.insert(
                    (
                        interface_descriptor.bInterfaceNumber,
                        interface_descriptor.bAlternateSetting,
                    ),
                    interface_descriptor,
                );
            }

            for intf_idx in 0..config_descriptor.bNumInterfaces {
                if !config_descriptor
                    .interface_descriptors
                    .contains_key(&(intf_idx, 0))
                {
                    warn!("device interface {} has no interface descriptors", intf_idx);
                }
            }

            device_descriptor
                .config_values
                .insert(cfg_idx, config_descriptor.bConfigurationValue);
            device_descriptor
                .config_descriptors
                .insert(config_descriptor.bConfigurationValue, config_descriptor);
        } else {
            warn!("Could not read config descriptor {}", cfg_idx);
            break;
        }
    }

    Ok(device_descriptor)
}

#[cfg(test)]
#[allow(clippy::useless_conversion)]
mod tests {
    use super::*;
    #[test]
    fn parse_descriptors_mass_storage() {
        let data: &[u8] = &[
            0x12, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 0x81, 0x07, 0x80, 0x55, 0x10, 0x00,
            0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x2C, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32, 0x09,
            0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50, 0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x04,
            0x00, 0x06, 0x30, 0x0F, 0x00, 0x00, 0x00, 0x07, 0x05, 0x02, 0x02, 0x00, 0x04, 0x00,
            0x06, 0x30, 0x0F, 0x00, 0x00, 0x00,
        ];

        let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");

        // The seemingly-redundant u16::from() calls avoid borrows of packed fields.

        assert_eq!(u16::from(d.bcdUSB), 0x03_00);
        assert_eq!(d.bDeviceClass, 0x00);
        assert_eq!(d.bDeviceSubClass, 0x00);
        assert_eq!(d.bDeviceProtocol, 0x00);
        assert_eq!(d.bMaxPacketSize0, 9);
        assert_eq!(u16::from(d.idVendor), 0x0781);
        assert_eq!(u16::from(d.idProduct), 0x5580);
        assert_eq!(u16::from(d.bcdDevice), 0x00_10);
        assert_eq!(d.iManufacturer, 1);
        assert_eq!(d.iProduct, 2);
        assert_eq!(d.iSerialNumber, 3);
        assert_eq!(d.bNumConfigurations, 1);

        let c = d
            .get_config_descriptor(1)
            .expect("could not get config descriptor 1");
        assert_eq!(u16::from(c.wTotalLength), 44);
        assert_eq!(c.bNumInterfaces, 1);
        assert_eq!(c.bConfigurationValue, 1);
        assert_eq!(c.iConfiguration, 0);
        assert_eq!(c.bmAttributes, 0x80);
        assert_eq!(c.bMaxPower, 50);

        let i = c
            .get_interface_descriptor(0, 0)
            .expect("could not get interface descriptor 0 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 0);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 2);
        assert_eq!(i.bInterfaceClass, 0x08);
        assert_eq!(i.bInterfaceSubClass, 0x06);
        assert_eq!(i.bInterfaceProtocol, 0x50);
        assert_eq!(i.iInterface, 0);

        let e = i
            .get_endpoint_descriptor(0)
            .expect("could not get endpoint 0 descriptor");
        assert_eq!(e.bEndpointAddress, 0x81);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
        assert_eq!(e.bInterval, 0);

        let e = i
            .get_endpoint_descriptor(1)
            .expect("could not get endpoint 1 descriptor");
        assert_eq!(e.bEndpointAddress, 0x02);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
        assert_eq!(e.bInterval, 0);
    }

    #[test]
    fn parse_descriptors_servo() {
        let data: &[u8] = &[
            0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0x1b, 0x50, 0x00, 0x01,
            0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x7c, 0x00, 0x06, 0x01, 0x04, 0xc0, 0xfa, 0x09,
            0x04, 0x00, 0x00, 0x02, 0xff, 0x50, 0x01, 0x06, 0x07, 0x05, 0x81, 0x02, 0x40, 0x00,
            0x0a, 0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00, 0x09, 0x04, 0x02, 0x00, 0x02, 0xff,
            0x52, 0x01, 0x05, 0x07, 0x05, 0x83, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x03, 0x02,
            0x40, 0x00, 0x00, 0x09, 0x04, 0x03, 0x00, 0x02, 0xff, 0x50, 0x01, 0x07, 0x07, 0x05,
            0x84, 0x02, 0x10, 0x00, 0x0a, 0x07, 0x05, 0x04, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04,
            0x04, 0x00, 0x02, 0xff, 0x50, 0x01, 0x08, 0x07, 0x05, 0x85, 0x02, 0x10, 0x00, 0x0a,
            0x07, 0x05, 0x05, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04, 0x05, 0x00, 0x02, 0xff, 0x53,
            0xff, 0x09, 0x07, 0x05, 0x86, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x06, 0x02, 0x40,
            0x00, 0x00,
        ];

        // Note: configuration 1 has bNumInterfaces == 6, but it actually only contains 5
        // interface descriptors. This causes us to try to read beyond EOF, which should be
        // silently ignored by parse_usbfs_descriptors so that we can use the rest of the
        // descriptors.
        let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");

        // The seemingly-redundant u16::from() calls avoid borrows of packed fields.

        assert_eq!(u16::from(d.bcdUSB), 0x02_00);
        assert_eq!(d.bDeviceClass, 0x00);
        assert_eq!(d.bDeviceSubClass, 0x00);
        assert_eq!(d.bDeviceProtocol, 0x00);
        assert_eq!(d.bMaxPacketSize0, 64);
        assert_eq!(u16::from(d.idVendor), 0x18d1);
        assert_eq!(u16::from(d.idProduct), 0x501b);
        assert_eq!(u16::from(d.bcdDevice), 0x01_00);
        assert_eq!(d.iManufacturer, 1);
        assert_eq!(d.iProduct, 2);
        assert_eq!(d.iSerialNumber, 3);
        assert_eq!(d.bNumConfigurations, 1);

        let c = d
            .get_config_descriptor(1)
            .expect("could not get config descriptor 1");
        assert_eq!(u16::from(c.wTotalLength), 124);
        assert_eq!(c.bNumInterfaces, 6);
        assert_eq!(c.bConfigurationValue, 1);
        assert_eq!(c.iConfiguration, 4);
        assert_eq!(c.bmAttributes, 0xc0);
        assert_eq!(c.bMaxPower, 250);

        let i = c
            .get_interface_descriptor(0, 0)
            .expect("could not get interface descriptor 0 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 0);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 2);
        assert_eq!(i.bInterfaceClass, 0xff);
        assert_eq!(i.bInterfaceSubClass, 0x50);
        assert_eq!(i.bInterfaceProtocol, 0x01);
        assert_eq!(i.iInterface, 6);

        let e = i
            .get_endpoint_descriptor(0)
            .expect("could not get endpoint 0 descriptor");
        assert_eq!(e.bEndpointAddress, 0x81);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
        assert_eq!(e.bInterval, 10);

        let e = i
            .get_endpoint_descriptor(1)
            .expect("could not get endpoint 1 descriptor");
        assert_eq!(e.bEndpointAddress, 0x01);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
        assert_eq!(e.bInterval, 0);

        let i = c
            .get_interface_descriptor(2, 0)
            .expect("could not get interface descriptor 2 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 2);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 2);
        assert_eq!(i.bInterfaceClass, 0xff);
        assert_eq!(i.bInterfaceSubClass, 0x52);
        assert_eq!(i.bInterfaceProtocol, 0x01);
        assert_eq!(i.iInterface, 5);

        let e = i
            .get_endpoint_descriptor(0)
            .expect("could not get endpoint 0 descriptor");
        assert_eq!(e.bEndpointAddress, 0x83);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
        assert_eq!(e.bInterval, 10);

        let e = i
            .get_endpoint_descriptor(1)
            .expect("could not get endpoint 1 descriptor");
        assert_eq!(e.bEndpointAddress, 0x03);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
        assert_eq!(e.bInterval, 0);

        let i = c
            .get_interface_descriptor(3, 0)
            .expect("could not get interface descriptor 3 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 3);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 2);
        assert_eq!(i.bInterfaceClass, 0xff);
        assert_eq!(i.bInterfaceSubClass, 0x50);
        assert_eq!(i.bInterfaceProtocol, 0x01);
        assert_eq!(i.iInterface, 7);

        let e = i
            .get_endpoint_descriptor(0)
            .expect("could not get endpoint 0 descriptor");
        assert_eq!(e.bEndpointAddress, 0x84);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
        assert_eq!(e.bInterval, 10);

        let e = i
            .get_endpoint_descriptor(1)
            .expect("could not get endpoint 1 descriptor");
        assert_eq!(e.bEndpointAddress, 0x04);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
        assert_eq!(e.bInterval, 0);

        let i = c
            .get_interface_descriptor(4, 0)
            .expect("could not get interface descriptor 4 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 4);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 2);
        assert_eq!(i.bInterfaceClass, 0xff);
        assert_eq!(i.bInterfaceSubClass, 0x50);
        assert_eq!(i.bInterfaceProtocol, 0x01);
        assert_eq!(i.iInterface, 8);

        let e = i
            .get_endpoint_descriptor(0)
            .expect("could not get endpoint 0 descriptor");
        assert_eq!(e.bEndpointAddress, 0x85);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
        assert_eq!(e.bInterval, 10);

        let e = i
            .get_endpoint_descriptor(1)
            .expect("could not get endpoint 1 descriptor");
        assert_eq!(e.bEndpointAddress, 0x05);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
        assert_eq!(e.bInterval, 0);

        let i = c
            .get_interface_descriptor(5, 0)
            .expect("could not get interface descriptor 5 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 5);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 2);
        assert_eq!(i.bInterfaceClass, 0xff);
        assert_eq!(i.bInterfaceSubClass, 0x53);
        assert_eq!(i.bInterfaceProtocol, 0xff);
        assert_eq!(i.iInterface, 9);

        let e = i
            .get_endpoint_descriptor(0)
            .expect("could not get endpoint 0 descriptor");
        assert_eq!(e.bEndpointAddress, 0x86);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
        assert_eq!(e.bInterval, 10);

        let e = i
            .get_endpoint_descriptor(1)
            .expect("could not get endpoint 1 descriptor");
        assert_eq!(e.bEndpointAddress, 0x06);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
        assert_eq!(e.bInterval, 0);
    }

    #[test]
    fn parse_descriptors_adb() {
        let data: &[u8] = &[
            0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0xe7, 0x4e, 0x10, 0x03,
            0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80, 0xfa, 0x09,
            0x04, 0x00, 0x00, 0x02, 0xff, 0x42, 0x01, 0x05, 0x07, 0x05, 0x01, 0x02, 0x00, 0x02,
            0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x02, 0x00,
        ];

        let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");

        // The seemingly-redundant u16::from() calls avoid borrows of packed fields.

        assert_eq!(u16::from(d.bcdUSB), 0x02_00);
        assert_eq!(d.bDeviceClass, 0x00);
        assert_eq!(d.bDeviceSubClass, 0x00);
        assert_eq!(d.bDeviceProtocol, 0x00);
        assert_eq!(d.bMaxPacketSize0, 64);
        assert_eq!(u16::from(d.idVendor), 0x18d1);
        assert_eq!(u16::from(d.idProduct), 0x4ee7);
        assert_eq!(u16::from(d.bcdDevice), 0x03_10);
        assert_eq!(d.iManufacturer, 1);
        assert_eq!(d.iProduct, 2);
        assert_eq!(d.iSerialNumber, 3);
        assert_eq!(d.bNumConfigurations, 1);

        let c = d
            .get_config_descriptor(1)
            .expect("could not get config descriptor 1");
        assert_eq!(u16::from(c.wTotalLength), 32);
        assert_eq!(c.bNumInterfaces, 1);
        assert_eq!(c.bConfigurationValue, 1);
        assert_eq!(c.iConfiguration, 0);
        assert_eq!(c.bmAttributes, 0x80);
        assert_eq!(c.bMaxPower, 250);

        let i = c
            .get_interface_descriptor(0, 0)
            .expect("could not get interface descriptor 0 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 0);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 2);
        assert_eq!(i.bInterfaceClass, 0xff);
        assert_eq!(i.bInterfaceSubClass, 0x42);
        assert_eq!(i.bInterfaceProtocol, 0x01);
        assert_eq!(i.iInterface, 5);

        let e = i
            .get_endpoint_descriptor(0)
            .expect("could not get endpoint 0 descriptor");
        assert_eq!(e.bEndpointAddress, 0x01);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x200);
        assert_eq!(e.bInterval, 0);

        let e = i
            .get_endpoint_descriptor(1)
            .expect("could not get endpoint 1 descriptor");
        assert_eq!(e.bEndpointAddress, 0x81);
        assert_eq!(e.bmAttributes, 0x02);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x0200);
        assert_eq!(e.bInterval, 0);
    }

    #[test]
    fn parse_descriptors_multiple_altsettings() {
        let data: &[u8] = &[
            // DeviceDescriptor
            0x12, 0x01, 0x00, 0x02, 0xef, 0x02, 0x01, 0x40, 0x6d, 0x04, 0x43, 0x08, 0x13, 0x00,
            0x00, 0x02, 0x01, 0x01, // ConfigDescriptor
            0x09, 0x02, 0x0d, 0x0a, 0x03, 0x01, 0x00, 0x80, 0xfa,
            // InterfaceDescriptor 0, 0
            0x09, 0x04, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x00, 0x00, // EndpointDescriptor
            0x07, 0x05, 0x86, 0x03, 0x40, 0x00, 0x08, // InterfaceDescriptor 1, 0
            0x09, 0x04, 0x01, 0x00, 0x00, 0x0e, 0x02, 0x00, 0x00,
            // InterfaceDescriptor 1, 1
            0x09, 0x04, 0x01, 0x01, 0x01, 0x0e, 0x02, 0x00, 0x00, // EndpointDescriptor
            0x07, 0x05, 0x81, 0x05, 0xc0, 0x00, 0x01, // InterfaceDescriptor 2, 0
            0x09, 0x04, 0x02, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
        ];

        let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");

        // The seemingly-redundant u16::from() calls avoid borrows of packed fields.

        assert_eq!(u16::from(d.bcdUSB), 0x02_00);
        assert_eq!(d.bDeviceClass, 0xef);
        assert_eq!(d.bDeviceSubClass, 0x02);
        assert_eq!(d.bDeviceProtocol, 0x01);
        assert_eq!(d.bMaxPacketSize0, 64);
        assert_eq!(u16::from(d.idVendor), 0x046d);
        assert_eq!(u16::from(d.idProduct), 0x0843);
        assert_eq!(u16::from(d.bcdDevice), 0x00_13);
        assert_eq!(d.iManufacturer, 0);
        assert_eq!(d.iProduct, 2);
        assert_eq!(d.iSerialNumber, 1);
        assert_eq!(d.bNumConfigurations, 1);

        let c = d
            .get_config_descriptor(1)
            .expect("could not get config descriptor 1");
        assert_eq!(u16::from(c.wTotalLength), 2573);
        assert_eq!(c.bNumInterfaces, 3);
        assert_eq!(c.bConfigurationValue, 1);
        assert_eq!(c.iConfiguration, 0);
        assert_eq!(c.bmAttributes, 0x80);
        assert_eq!(c.bMaxPower, 250);

        let i = c
            .get_interface_descriptor(0, 0)
            .expect("could not get interface descriptor 0 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 0);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 1);
        assert_eq!(i.bInterfaceClass, 0x0e);
        assert_eq!(i.bInterfaceSubClass, 0x01);
        assert_eq!(i.bInterfaceProtocol, 0x00);
        assert_eq!(i.iInterface, 0x00);

        let e = i
            .get_endpoint_descriptor(0)
            .expect("could not get endpoint 0 descriptor");
        assert_eq!(e.bEndpointAddress, 0x86);
        assert_eq!(e.bmAttributes, 0x03);
        assert_eq!(u16::from(e.wMaxPacketSize), 0x40);
        assert_eq!(e.bInterval, 0x08);

        let i = c
            .get_interface_descriptor(1, 0)
            .expect("could not get interface descriptor 1 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 1);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 0);
        assert_eq!(i.bInterfaceClass, 0x0e);
        assert_eq!(i.bInterfaceSubClass, 0x02);
        assert_eq!(i.bInterfaceProtocol, 0x00);
        assert_eq!(i.iInterface, 0x00);

        let i = c
            .get_interface_descriptor(1, 1)
            .expect("could not get interface descriptor 1 alt setting 1");
        assert_eq!(i.bInterfaceNumber, 1);
        assert_eq!(i.bAlternateSetting, 1);
        assert_eq!(i.bNumEndpoints, 1);
        assert_eq!(i.bInterfaceClass, 0x0e);
        assert_eq!(i.bInterfaceSubClass, 0x02);
        assert_eq!(i.bInterfaceProtocol, 0x00);
        assert_eq!(i.iInterface, 0x00);

        let e = i
            .get_endpoint_descriptor(0)
            .expect("could not get endpoint 0 descriptor");
        assert_eq!(e.bEndpointAddress, 0x81);
        assert_eq!(e.bmAttributes, 0x05);
        assert_eq!(u16::from(e.wMaxPacketSize), 0xc0);
        assert_eq!(e.bInterval, 0x01);

        let i = c
            .get_interface_descriptor(2, 0)
            .expect("could not get interface descriptor 2 alt setting 0");
        assert_eq!(i.bInterfaceNumber, 2);
        assert_eq!(i.bAlternateSetting, 0);
        assert_eq!(i.bNumEndpoints, 0);
        assert_eq!(i.bInterfaceClass, 0x01);
        assert_eq!(i.bInterfaceSubClass, 0x01);
        assert_eq!(i.bInterfaceProtocol, 0x00);
        assert_eq!(i.iInterface, 0x00);
    }

    #[test]
    fn parse_descriptors_length_0() {
        // Device descriptor followed by a bogus descriptor with bLength == 0.
        // Note that this was generated by a fuzzer, so field values may not make sense.
        let data: &[u8] = &[
            0x10, 0x00, 0x18, 0x25, 0x80, 0x80, 0xAC, 0x03, 0x22, 0x05, 0x00, 0x00, 0x00, 0x00,
            0xC3, 0x2A, 0x00, 0x32, 0x00,
        ];

        let d = parse_usbfs_descriptors(data);
        if d.is_ok() {
            panic!("parse_usbfs_descriptors should have failed");
        }
    }
}